[EFL][WebGL] Add GLES2 support in GraphicsContext3DEfl.
[WebKit-https.git] / Source / WebCore / platform / graphics / GraphicsContext3D.h
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef GraphicsContext3D_h
27 #define GraphicsContext3D_h
28
29 #include "GraphicsTypes3D.h"
30 #include "Image.h"
31 #include "IntRect.h"
32 #include "KURL.h"
33 #include "PlatformLayer.h"
34 #include <wtf/HashMap.h>
35 #include <wtf/ListHashSet.h>
36 #include <wtf/Noncopyable.h>
37 #include <wtf/RefCounted.h>
38 #include <wtf/text/WTFString.h>
39
40 // FIXME: Find a better way to avoid the name confliction for NO_ERROR.
41 #if ((PLATFORM(CHROMIUM) && OS(WINDOWS)) || PLATFORM(WIN) || (PLATFORM(QT) && OS(WINDOWS)))
42 #undef NO_ERROR
43 #elif PLATFORM(GTK)
44 // This define is from the X11 headers, but it's used below, so we must undefine it.
45 #undef VERSION
46 #endif
47
48 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(QT) || PLATFORM(EFL) || PLATFORM(BLACKBERRY)
49 #include "ANGLEWebKitBridge.h"
50 #endif
51
52 #if PLATFORM(MAC)
53 #include <OpenGL/OpenGL.h>
54 #include <wtf/RetainPtr.h>
55 OBJC_CLASS CALayer;
56 OBJC_CLASS WebGLLayer;
57 #elif PLATFORM(QT)
58 QT_BEGIN_NAMESPACE
59 class QPainter;
60 class QRect;
61 class QGLWidget;
62 class QGLContext;
63 class QOpenGLContext;
64 class QSurface;
65 QT_END_NAMESPACE
66 #elif PLATFORM(GTK) || PLATFORM(EFL)
67 typedef unsigned int GLuint;
68 #endif
69
70 #if PLATFORM(MAC)
71 typedef CGLContextObj PlatformGraphicsContext3D;
72 #elif PLATFORM(QT)
73 typedef QOpenGLContext* PlatformGraphicsContext3D;
74 typedef QSurface* PlatformGraphicsSurface3D;
75 #else
76 typedef void* PlatformGraphicsContext3D;
77 typedef void* PlatformGraphicsSurface3D;
78 #endif
79
80 #if (PLATFORM(CHROMIUM) || PLATFORM(BLACKBERRY)) && USE(SKIA)
81 class GrContext;
82 #endif
83
84 // These are currently the same among all implementations.
85 const PlatformGraphicsContext3D NullPlatformGraphicsContext3D = 0;
86 const Platform3DObject NullPlatform3DObject = 0;
87
88 #if USE(CG)
89 #include <CoreGraphics/CGContext.h>
90 #endif
91
92 namespace WebCore {
93 class DrawingBuffer;
94 class Extensions3D;
95 #if USE(OPENGL_ES_2)
96 class Extensions3DOpenGLES;
97 #else
98 class Extensions3DOpenGL;
99 #endif
100 #if PLATFORM(QT)
101 class Extensions3DQt;
102 #endif
103 class HostWindow;
104 class Image;
105 class ImageBuffer;
106 class ImageSource;
107 class ImageData;
108 class IntRect;
109 class IntSize;
110 #if USE(CAIRO)
111 class PlatformContextCairo;
112 #elif PLATFORM(BLACKBERRY)
113 class GraphicsContext;
114 #endif
115
116 struct ActiveInfo {
117     String name;
118     GC3Denum type;
119     GC3Dint size;
120 };
121
122 class GraphicsContext3DPrivate;
123
124 class GraphicsContext3D : public RefCounted<GraphicsContext3D> {
125 public:
126     enum {
127         DEPTH_BUFFER_BIT = 0x00000100,
128         STENCIL_BUFFER_BIT = 0x00000400,
129         COLOR_BUFFER_BIT = 0x00004000,
130         POINTS = 0x0000,
131         LINES = 0x0001,
132         LINE_LOOP = 0x0002,
133         LINE_STRIP = 0x0003,
134         TRIANGLES = 0x0004,
135         TRIANGLE_STRIP = 0x0005,
136         TRIANGLE_FAN = 0x0006,
137         ZERO = 0,
138         ONE = 1,
139         SRC_COLOR = 0x0300,
140         ONE_MINUS_SRC_COLOR = 0x0301,
141         SRC_ALPHA = 0x0302,
142         ONE_MINUS_SRC_ALPHA = 0x0303,
143         DST_ALPHA = 0x0304,
144         ONE_MINUS_DST_ALPHA = 0x0305,
145         DST_COLOR = 0x0306,
146         ONE_MINUS_DST_COLOR = 0x0307,
147         SRC_ALPHA_SATURATE = 0x0308,
148         FUNC_ADD = 0x8006,
149         BLEND_EQUATION = 0x8009,
150         BLEND_EQUATION_RGB = 0x8009,
151         BLEND_EQUATION_ALPHA = 0x883D,
152         FUNC_SUBTRACT = 0x800A,
153         FUNC_REVERSE_SUBTRACT = 0x800B,
154         BLEND_DST_RGB = 0x80C8,
155         BLEND_SRC_RGB = 0x80C9,
156         BLEND_DST_ALPHA = 0x80CA,
157         BLEND_SRC_ALPHA = 0x80CB,
158         CONSTANT_COLOR = 0x8001,
159         ONE_MINUS_CONSTANT_COLOR = 0x8002,
160         CONSTANT_ALPHA = 0x8003,
161         ONE_MINUS_CONSTANT_ALPHA = 0x8004,
162         BLEND_COLOR = 0x8005,
163         ARRAY_BUFFER = 0x8892,
164         ELEMENT_ARRAY_BUFFER = 0x8893,
165         ARRAY_BUFFER_BINDING = 0x8894,
166         ELEMENT_ARRAY_BUFFER_BINDING = 0x8895,
167         STREAM_DRAW = 0x88E0,
168         STATIC_DRAW = 0x88E4,
169         DYNAMIC_DRAW = 0x88E8,
170         BUFFER_SIZE = 0x8764,
171         BUFFER_USAGE = 0x8765,
172         CURRENT_VERTEX_ATTRIB = 0x8626,
173         FRONT = 0x0404,
174         BACK = 0x0405,
175         FRONT_AND_BACK = 0x0408,
176         TEXTURE_2D = 0x0DE1,
177         CULL_FACE = 0x0B44,
178         BLEND = 0x0BE2,
179         DITHER = 0x0BD0,
180         STENCIL_TEST = 0x0B90,
181         DEPTH_TEST = 0x0B71,
182         SCISSOR_TEST = 0x0C11,
183         POLYGON_OFFSET_FILL = 0x8037,
184         SAMPLE_ALPHA_TO_COVERAGE = 0x809E,
185         SAMPLE_COVERAGE = 0x80A0,
186         NO_ERROR = 0,
187         INVALID_ENUM = 0x0500,
188         INVALID_VALUE = 0x0501,
189         INVALID_OPERATION = 0x0502,
190         OUT_OF_MEMORY = 0x0505,
191         CW = 0x0900,
192         CCW = 0x0901,
193         LINE_WIDTH = 0x0B21,
194         ALIASED_POINT_SIZE_RANGE = 0x846D,
195         ALIASED_LINE_WIDTH_RANGE = 0x846E,
196         CULL_FACE_MODE = 0x0B45,
197         FRONT_FACE = 0x0B46,
198         DEPTH_RANGE = 0x0B70,
199         DEPTH_WRITEMASK = 0x0B72,
200         DEPTH_CLEAR_VALUE = 0x0B73,
201         DEPTH_FUNC = 0x0B74,
202         STENCIL_CLEAR_VALUE = 0x0B91,
203         STENCIL_FUNC = 0x0B92,
204         STENCIL_FAIL = 0x0B94,
205         STENCIL_PASS_DEPTH_FAIL = 0x0B95,
206         STENCIL_PASS_DEPTH_PASS = 0x0B96,
207         STENCIL_REF = 0x0B97,
208         STENCIL_VALUE_MASK = 0x0B93,
209         STENCIL_WRITEMASK = 0x0B98,
210         STENCIL_BACK_FUNC = 0x8800,
211         STENCIL_BACK_FAIL = 0x8801,
212         STENCIL_BACK_PASS_DEPTH_FAIL = 0x8802,
213         STENCIL_BACK_PASS_DEPTH_PASS = 0x8803,
214         STENCIL_BACK_REF = 0x8CA3,
215         STENCIL_BACK_VALUE_MASK = 0x8CA4,
216         STENCIL_BACK_WRITEMASK = 0x8CA5,
217         VIEWPORT = 0x0BA2,
218         SCISSOR_BOX = 0x0C10,
219         COLOR_CLEAR_VALUE = 0x0C22,
220         COLOR_WRITEMASK = 0x0C23,
221         UNPACK_ALIGNMENT = 0x0CF5,
222         PACK_ALIGNMENT = 0x0D05,
223         MAX_TEXTURE_SIZE = 0x0D33,
224         MAX_VIEWPORT_DIMS = 0x0D3A,
225         SUBPIXEL_BITS = 0x0D50,
226         RED_BITS = 0x0D52,
227         GREEN_BITS = 0x0D53,
228         BLUE_BITS = 0x0D54,
229         ALPHA_BITS = 0x0D55,
230         DEPTH_BITS = 0x0D56,
231         STENCIL_BITS = 0x0D57,
232         POLYGON_OFFSET_UNITS = 0x2A00,
233         POLYGON_OFFSET_FACTOR = 0x8038,
234         TEXTURE_BINDING_2D = 0x8069,
235         SAMPLE_BUFFERS = 0x80A8,
236         SAMPLES = 0x80A9,
237         SAMPLE_COVERAGE_VALUE = 0x80AA,
238         SAMPLE_COVERAGE_INVERT = 0x80AB,
239         NUM_COMPRESSED_TEXTURE_FORMATS = 0x86A2,
240         COMPRESSED_TEXTURE_FORMATS = 0x86A3,
241         DONT_CARE = 0x1100,
242         FASTEST = 0x1101,
243         NICEST = 0x1102,
244         GENERATE_MIPMAP_HINT = 0x8192,
245         BYTE = 0x1400,
246         UNSIGNED_BYTE = 0x1401,
247         SHORT = 0x1402,
248         UNSIGNED_SHORT = 0x1403,
249         INT = 0x1404,
250         UNSIGNED_INT = 0x1405,
251         FLOAT = 0x1406,
252         FIXED = 0x140C,
253         DEPTH_COMPONENT = 0x1902,
254         ALPHA = 0x1906,
255         RGB = 0x1907,
256         RGBA = 0x1908,
257         BGRA = 0x80E1,
258         LUMINANCE = 0x1909,
259         LUMINANCE_ALPHA = 0x190A,
260         UNSIGNED_SHORT_4_4_4_4 = 0x8033,
261         UNSIGNED_SHORT_5_5_5_1 = 0x8034,
262         UNSIGNED_SHORT_5_6_5 = 0x8363,
263         FRAGMENT_SHADER = 0x8B30,
264         VERTEX_SHADER = 0x8B31,
265         MAX_VERTEX_ATTRIBS = 0x8869,
266         MAX_VERTEX_UNIFORM_VECTORS = 0x8DFB,
267         MAX_VARYING_VECTORS = 0x8DFC,
268         MAX_COMBINED_TEXTURE_IMAGE_UNITS = 0x8B4D,
269         MAX_VERTEX_TEXTURE_IMAGE_UNITS = 0x8B4C,
270         MAX_TEXTURE_IMAGE_UNITS = 0x8872,
271         MAX_FRAGMENT_UNIFORM_VECTORS = 0x8DFD,
272         SHADER_TYPE = 0x8B4F,
273         DELETE_STATUS = 0x8B80,
274         LINK_STATUS = 0x8B82,
275         VALIDATE_STATUS = 0x8B83,
276         ATTACHED_SHADERS = 0x8B85,
277         ACTIVE_UNIFORMS = 0x8B86,
278         ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87,
279         ACTIVE_ATTRIBUTES = 0x8B89,
280         ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A,
281         SHADING_LANGUAGE_VERSION = 0x8B8C,
282         CURRENT_PROGRAM = 0x8B8D,
283         NEVER = 0x0200,
284         LESS = 0x0201,
285         EQUAL = 0x0202,
286         LEQUAL = 0x0203,
287         GREATER = 0x0204,
288         NOTEQUAL = 0x0205,
289         GEQUAL = 0x0206,
290         ALWAYS = 0x0207,
291         KEEP = 0x1E00,
292         REPLACE = 0x1E01,
293         INCR = 0x1E02,
294         DECR = 0x1E03,
295         INVERT = 0x150A,
296         INCR_WRAP = 0x8507,
297         DECR_WRAP = 0x8508,
298         VENDOR = 0x1F00,
299         RENDERER = 0x1F01,
300         VERSION = 0x1F02,
301         EXTENSIONS = 0x1F03,
302         NEAREST = 0x2600,
303         LINEAR = 0x2601,
304         NEAREST_MIPMAP_NEAREST = 0x2700,
305         LINEAR_MIPMAP_NEAREST = 0x2701,
306         NEAREST_MIPMAP_LINEAR = 0x2702,
307         LINEAR_MIPMAP_LINEAR = 0x2703,
308         TEXTURE_MAG_FILTER = 0x2800,
309         TEXTURE_MIN_FILTER = 0x2801,
310         TEXTURE_WRAP_S = 0x2802,
311         TEXTURE_WRAP_T = 0x2803,
312         TEXTURE = 0x1702,
313         TEXTURE_CUBE_MAP = 0x8513,
314         TEXTURE_BINDING_CUBE_MAP = 0x8514,
315         TEXTURE_CUBE_MAP_POSITIVE_X = 0x8515,
316         TEXTURE_CUBE_MAP_NEGATIVE_X = 0x8516,
317         TEXTURE_CUBE_MAP_POSITIVE_Y = 0x8517,
318         TEXTURE_CUBE_MAP_NEGATIVE_Y = 0x8518,
319         TEXTURE_CUBE_MAP_POSITIVE_Z = 0x8519,
320         TEXTURE_CUBE_MAP_NEGATIVE_Z = 0x851A,
321         MAX_CUBE_MAP_TEXTURE_SIZE = 0x851C,
322         TEXTURE0 = 0x84C0,
323         TEXTURE1 = 0x84C1,
324         TEXTURE2 = 0x84C2,
325         TEXTURE3 = 0x84C3,
326         TEXTURE4 = 0x84C4,
327         TEXTURE5 = 0x84C5,
328         TEXTURE6 = 0x84C6,
329         TEXTURE7 = 0x84C7,
330         TEXTURE8 = 0x84C8,
331         TEXTURE9 = 0x84C9,
332         TEXTURE10 = 0x84CA,
333         TEXTURE11 = 0x84CB,
334         TEXTURE12 = 0x84CC,
335         TEXTURE13 = 0x84CD,
336         TEXTURE14 = 0x84CE,
337         TEXTURE15 = 0x84CF,
338         TEXTURE16 = 0x84D0,
339         TEXTURE17 = 0x84D1,
340         TEXTURE18 = 0x84D2,
341         TEXTURE19 = 0x84D3,
342         TEXTURE20 = 0x84D4,
343         TEXTURE21 = 0x84D5,
344         TEXTURE22 = 0x84D6,
345         TEXTURE23 = 0x84D7,
346         TEXTURE24 = 0x84D8,
347         TEXTURE25 = 0x84D9,
348         TEXTURE26 = 0x84DA,
349         TEXTURE27 = 0x84DB,
350         TEXTURE28 = 0x84DC,
351         TEXTURE29 = 0x84DD,
352         TEXTURE30 = 0x84DE,
353         TEXTURE31 = 0x84DF,
354         ACTIVE_TEXTURE = 0x84E0,
355         REPEAT = 0x2901,
356         CLAMP_TO_EDGE = 0x812F,
357         MIRRORED_REPEAT = 0x8370,
358         FLOAT_VEC2 = 0x8B50,
359         FLOAT_VEC3 = 0x8B51,
360         FLOAT_VEC4 = 0x8B52,
361         INT_VEC2 = 0x8B53,
362         INT_VEC3 = 0x8B54,
363         INT_VEC4 = 0x8B55,
364         BOOL = 0x8B56,
365         BOOL_VEC2 = 0x8B57,
366         BOOL_VEC3 = 0x8B58,
367         BOOL_VEC4 = 0x8B59,
368         FLOAT_MAT2 = 0x8B5A,
369         FLOAT_MAT3 = 0x8B5B,
370         FLOAT_MAT4 = 0x8B5C,
371         SAMPLER_2D = 0x8B5E,
372         SAMPLER_CUBE = 0x8B60,
373         VERTEX_ATTRIB_ARRAY_ENABLED = 0x8622,
374         VERTEX_ATTRIB_ARRAY_SIZE = 0x8623,
375         VERTEX_ATTRIB_ARRAY_STRIDE = 0x8624,
376         VERTEX_ATTRIB_ARRAY_TYPE = 0x8625,
377         VERTEX_ATTRIB_ARRAY_NORMALIZED = 0x886A,
378         VERTEX_ATTRIB_ARRAY_POINTER = 0x8645,
379         VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = 0x889F,
380         COMPILE_STATUS = 0x8B81,
381         INFO_LOG_LENGTH = 0x8B84,
382         SHADER_SOURCE_LENGTH = 0x8B88,
383         SHADER_COMPILER = 0x8DFA,
384         SHADER_BINARY_FORMATS = 0x8DF8,
385         NUM_SHADER_BINARY_FORMATS = 0x8DF9,
386         LOW_FLOAT = 0x8DF0,
387         MEDIUM_FLOAT = 0x8DF1,
388         HIGH_FLOAT = 0x8DF2,
389         LOW_INT = 0x8DF3,
390         MEDIUM_INT = 0x8DF4,
391         HIGH_INT = 0x8DF5,
392         FRAMEBUFFER = 0x8D40,
393         RENDERBUFFER = 0x8D41,
394         RGBA4 = 0x8056,
395         RGB5_A1 = 0x8057,
396         RGB565 = 0x8D62,
397         DEPTH_COMPONENT16 = 0x81A5,
398         STENCIL_INDEX = 0x1901,
399         STENCIL_INDEX8 = 0x8D48,
400         DEPTH_STENCIL = 0x84F9,
401         UNSIGNED_INT_24_8 = 0x84FA,
402         DEPTH24_STENCIL8 = 0x88F0,
403         RENDERBUFFER_WIDTH = 0x8D42,
404         RENDERBUFFER_HEIGHT = 0x8D43,
405         RENDERBUFFER_INTERNAL_FORMAT = 0x8D44,
406         RENDERBUFFER_RED_SIZE = 0x8D50,
407         RENDERBUFFER_GREEN_SIZE = 0x8D51,
408         RENDERBUFFER_BLUE_SIZE = 0x8D52,
409         RENDERBUFFER_ALPHA_SIZE = 0x8D53,
410         RENDERBUFFER_DEPTH_SIZE = 0x8D54,
411         RENDERBUFFER_STENCIL_SIZE = 0x8D55,
412         FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = 0x8CD0,
413         FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = 0x8CD1,
414         FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL = 0x8CD2,
415         FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE = 0x8CD3,
416         COLOR_ATTACHMENT0 = 0x8CE0,
417         DEPTH_ATTACHMENT = 0x8D00,
418         STENCIL_ATTACHMENT = 0x8D20,
419         DEPTH_STENCIL_ATTACHMENT = 0x821A,
420         NONE = 0,
421         FRAMEBUFFER_COMPLETE = 0x8CD5,
422         FRAMEBUFFER_INCOMPLETE_ATTACHMENT = 0x8CD6,
423         FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = 0x8CD7,
424         FRAMEBUFFER_INCOMPLETE_DIMENSIONS = 0x8CD9,
425         FRAMEBUFFER_UNSUPPORTED = 0x8CDD,
426         FRAMEBUFFER_BINDING = 0x8CA6,
427         RENDERBUFFER_BINDING = 0x8CA7,
428         MAX_RENDERBUFFER_SIZE = 0x84E8,
429         INVALID_FRAMEBUFFER_OPERATION = 0x0506,
430
431         // WebGL-specific enums
432         UNPACK_FLIP_Y_WEBGL = 0x9240,
433         UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241,
434         CONTEXT_LOST_WEBGL = 0x9242,
435         UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243,
436         BROWSER_DEFAULT_WEBGL = 0x9244
437     };
438
439     // Context creation attributes.
440     struct Attributes {
441         Attributes()
442             : alpha(true)
443             , depth(true)
444             , stencil(false)
445             , antialias(true)
446             , premultipliedAlpha(true)
447             , preserveDrawingBuffer(false)
448             , noExtensions(false)
449             , shareResources(true)
450             , preferDiscreteGPU(false)
451         {
452         }
453
454         bool alpha;
455         bool depth;
456         bool stencil;
457         bool antialias;
458         bool premultipliedAlpha;
459         bool preserveDrawingBuffer;
460         bool noExtensions;
461         bool shareResources;
462         bool preferDiscreteGPU;
463         KURL topDocumentURL;
464     };
465
466     enum RenderStyle {
467         RenderOffscreen,
468         RenderDirectlyToHostWindow,
469         RenderToCurrentGLContext
470     };
471
472     class ContextLostCallback {
473     public:
474         virtual void onContextLost() = 0;
475         virtual ~ContextLostCallback() {}
476     };
477
478     class ErrorMessageCallback {
479     public:
480         virtual void onErrorMessage(const String& message, GC3Dint id) = 0;
481         virtual ~ErrorMessageCallback() { }
482     };
483
484     void setContextLostCallback(PassOwnPtr<ContextLostCallback>);
485     void setErrorMessageCallback(PassOwnPtr<ErrorMessageCallback>);
486
487     static PassRefPtr<GraphicsContext3D> create(Attributes, HostWindow*, RenderStyle = RenderOffscreen);
488     static PassRefPtr<GraphicsContext3D> createForCurrentGLContext();
489     ~GraphicsContext3D();
490
491 #if PLATFORM(MAC)
492     PlatformGraphicsContext3D platformGraphicsContext3D() const { return m_contextObj; }
493     Platform3DObject platformTexture() const { return m_compositorTexture; }
494     CALayer* platformLayer() const { return reinterpret_cast<CALayer*>(m_webGLLayer.get()); }
495 #elif PLATFORM(CHROMIUM) || PLATFORM(BLACKBERRY)
496     PlatformGraphicsContext3D platformGraphicsContext3D() const;
497     Platform3DObject platformTexture() const;
498 #if USE(SKIA)
499     GrContext* grContext();
500 #endif
501 #if USE(ACCELERATED_COMPOSITING)
502     PlatformLayer* platformLayer() const;
503 #endif
504 #elif PLATFORM(QT)
505     PlatformGraphicsContext3D platformGraphicsContext3D();
506     Platform3DObject platformTexture() const;
507 #if USE(ACCELERATED_COMPOSITING)
508     PlatformLayer* platformLayer() const;
509 #endif
510 #elif PLATFORM(GTK)
511     PlatformGraphicsContext3D platformGraphicsContext3D();
512     Platform3DObject platformTexture() const { return m_texture; }
513 #if USE(ACCELERATED_COMPOSITING)
514     PlatformLayer* platformLayer() const;
515 #endif
516 #elif PLATFORM(EFL)
517     PlatformGraphicsContext3D platformGraphicsContext3D();
518     Platform3DObject platformTexture() const { return m_texture; }
519 #if USE(ACCELERATED_COMPOSITING)
520     PlatformLayer* platformLayer() const;
521 #endif
522 #else
523     PlatformGraphicsContext3D platformGraphicsContext3D() const { return NullPlatformGraphicsContext3D; }
524     Platform3DObject platformTexture() const { return NullPlatform3DObject; }
525 #if USE(ACCELERATED_COMPOSITING)
526     PlatformLayer* platformLayer() const { return 0; }
527 #endif
528 #endif
529     bool makeContextCurrent();
530
531 #if PLATFORM(MAC) || PLATFORM(CHROMIUM) || PLATFORM(GTK) || PLATFORM(QT) || PLATFORM(EFL) || PLATFORM(BLACKBERRY)
532     // With multisampling on, blit from multisampleFBO to regular FBO.
533     void prepareTexture();
534 #endif
535
536     // Equivalent to ::glTexImage2D(). Allows pixels==0 with no allocation.
537     void texImage2DDirect(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
538
539     // Helper to texImage2D with pixel==0 case: pixels are initialized to 0.
540     // Return true if no GL error is synthesized.
541     // By default, alignment is 4, the OpenGL default setting.
542     bool texImage2DResourceSafe(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint alignment = 4);
543
544     bool isGLES2Compliant() const;
545
546     //----------------------------------------------------------------------
547     // Helpers for texture uploading and pixel readback.
548     //
549
550     // Computes the components per pixel and bytes per component
551     // for the given format and type combination. Returns false if
552     // either was an invalid enum.
553     static bool computeFormatAndTypeParameters(GC3Denum format,
554                                                GC3Denum type,
555                                                unsigned int* componentsPerPixel,
556                                                unsigned int* bytesPerComponent);
557
558     // Computes the image size in bytes. If paddingInBytes is not null, padding
559     // is also calculated in return. Returns NO_ERROR if succeed, otherwise
560     // return the suggested GL error indicating the cause of the failure:
561     //   INVALID_VALUE if width/height is negative or overflow happens.
562     //   INVALID_ENUM if format/type is illegal.
563     static GC3Denum computeImageSizeInBytes(GC3Denum format,
564                                      GC3Denum type,
565                                      GC3Dsizei width,
566                                      GC3Dsizei height,
567                                      GC3Dint alignment,
568                                      unsigned int* imageSizeInBytes,
569                                      unsigned int* paddingInBytes);
570
571     // Extracts the contents of the given ImageData into the passed Vector,
572     // packing the pixel data according to the given format and type,
573     // and obeying the flipY and premultiplyAlpha flags. Returns true
574     // upon success.
575     static bool extractImageData(ImageData*,
576                           GC3Denum format,
577                           GC3Denum type,
578                           bool flipY,
579                           bool premultiplyAlpha,
580                           Vector<uint8_t>& data);
581
582     // Helper function which extracts the user-supplied texture
583     // data, applying the flipY and premultiplyAlpha parameters.
584     // If the data is not tightly packed according to the passed
585     // unpackAlignment, the output data will be tightly packed.
586     // Returns true if successful, false if any error occurred.
587     static bool extractTextureData(unsigned int width, unsigned int height,
588                             GC3Denum format, GC3Denum type,
589                             unsigned int unpackAlignment,
590                             bool flipY, bool premultiplyAlpha,
591                             const void* pixels,
592                             Vector<uint8_t>& data);
593
594     // Flips the given image data vertically, in-place.
595     static void flipVertically(void* imageData,
596                         unsigned int width,
597                         unsigned int height,
598                         unsigned int bytesPerPixel,
599                         unsigned int unpackAlignment);
600
601     // Attempt to enumerate all possible native image formats to
602     // reduce the amount of temporary allocations during texture
603     // uploading. This enum must be public because it is accessed
604     // by non-member functions.
605     enum SourceDataFormat {
606         SourceFormatRGBA8 = 0,
607         SourceFormatRGBA16Little,
608         SourceFormatRGBA16Big,
609         SourceFormatRGBA32F,
610         SourceFormatRGB8,
611         SourceFormatRGB16Little,
612         SourceFormatRGB16Big,
613         SourceFormatRGB32F,
614         SourceFormatBGR8,
615         SourceFormatBGRA8,
616         SourceFormatBGRA16Little,
617         SourceFormatBGRA16Big,
618         SourceFormatARGB8,
619         SourceFormatARGB16Little,
620         SourceFormatARGB16Big,
621         SourceFormatABGR8,
622         SourceFormatRGBA5551,
623         SourceFormatRGBA4444,
624         SourceFormatRGB565,
625         SourceFormatR8,
626         SourceFormatR16Little,
627         SourceFormatR16Big,
628         SourceFormatR32F,
629         SourceFormatRA8,
630         SourceFormatRA16Little,
631         SourceFormatRA16Big,
632         SourceFormatRA32F,
633         SourceFormatAR8,
634         SourceFormatAR16Little,
635         SourceFormatAR16Big,
636         SourceFormatA8,
637         SourceFormatA16Little,
638         SourceFormatA16Big,
639         SourceFormatA32F,
640         SourceFormatNumFormats
641     };
642
643     //----------------------------------------------------------------------
644     // Entry points for WebGL.
645     //
646
647     void activeTexture(GC3Denum texture);
648     void attachShader(Platform3DObject program, Platform3DObject shader);
649     void bindAttribLocation(Platform3DObject, GC3Duint index, const String& name);
650     void bindBuffer(GC3Denum target, Platform3DObject);
651     void bindFramebuffer(GC3Denum target, Platform3DObject);
652     void bindRenderbuffer(GC3Denum target, Platform3DObject);
653     void bindTexture(GC3Denum target, Platform3DObject);
654     void blendColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
655     void blendEquation(GC3Denum mode);
656     void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
657     void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
658     void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
659
660     void bufferData(GC3Denum target, GC3Dsizeiptr size, GC3Denum usage);
661     void bufferData(GC3Denum target, GC3Dsizeiptr size, const void* data, GC3Denum usage);
662     void bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr size, const void* data);
663
664     GC3Denum checkFramebufferStatus(GC3Denum target);
665     void clear(GC3Dbitfield mask);
666     void clearColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
667     void clearDepth(GC3Dclampf depth);
668     void clearStencil(GC3Dint s);
669     void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
670     void compileShader(Platform3DObject);
671
672     void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Dsizei imageSize, const void* data);
673     void compressedTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Dsizei imageSize, const void* data);
674     void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
675     void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
676     void cullFace(GC3Denum mode);
677     void depthFunc(GC3Denum func);
678     void depthMask(GC3Dboolean flag);
679     void depthRange(GC3Dclampf zNear, GC3Dclampf zFar);
680     void detachShader(Platform3DObject, Platform3DObject);
681     void disable(GC3Denum cap);
682     void disableVertexAttribArray(GC3Duint index);
683     void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count);
684     void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
685
686     void enable(GC3Denum cap);
687     void enableVertexAttribArray(GC3Duint index);
688     void finish();
689     void flush();
690     void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, Platform3DObject);
691     void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level);
692     void frontFace(GC3Denum mode);
693     void generateMipmap(GC3Denum target);
694
695     bool getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo&);
696     bool getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo&);
697     void getAttachedShaders(Platform3DObject program, GC3Dsizei maxCount, GC3Dsizei* count, Platform3DObject* shaders);
698     GC3Dint getAttribLocation(Platform3DObject, const String& name);
699     void getBooleanv(GC3Denum pname, GC3Dboolean* value);
700     void getBufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
701     Attributes getContextAttributes();
702     GC3Denum getError();
703     void getFloatv(GC3Denum pname, GC3Dfloat* value);
704     void getFramebufferAttachmentParameteriv(GC3Denum target, GC3Denum attachment, GC3Denum pname, GC3Dint* value);
705     void getIntegerv(GC3Denum pname, GC3Dint* value);
706     void getProgramiv(Platform3DObject program, GC3Denum pname, GC3Dint* value);
707     String getProgramInfoLog(Platform3DObject);
708     void getRenderbufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
709     void getShaderiv(Platform3DObject, GC3Denum pname, GC3Dint* value);
710     String getShaderInfoLog(Platform3DObject);
711     void getShaderPrecisionFormat(GC3Denum shaderType, GC3Denum precisionType, GC3Dint* range, GC3Dint* precision);
712     String getShaderSource(Platform3DObject);
713     String getString(GC3Denum name);
714     void getTexParameterfv(GC3Denum target, GC3Denum pname, GC3Dfloat* value);
715     void getTexParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
716     void getUniformfv(Platform3DObject program, GC3Dint location, GC3Dfloat* value);
717     void getUniformiv(Platform3DObject program, GC3Dint location, GC3Dint* value);
718     GC3Dint getUniformLocation(Platform3DObject, const String& name);
719     void getVertexAttribfv(GC3Duint index, GC3Denum pname, GC3Dfloat* value);
720     void getVertexAttribiv(GC3Duint index, GC3Denum pname, GC3Dint* value);
721     GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
722
723     void hint(GC3Denum target, GC3Denum mode);
724     GC3Dboolean isBuffer(Platform3DObject);
725     GC3Dboolean isEnabled(GC3Denum cap);
726     GC3Dboolean isFramebuffer(Platform3DObject);
727     GC3Dboolean isProgram(Platform3DObject);
728     GC3Dboolean isRenderbuffer(Platform3DObject);
729     GC3Dboolean isShader(Platform3DObject);
730     GC3Dboolean isTexture(Platform3DObject);
731     void lineWidth(GC3Dfloat);
732     void linkProgram(Platform3DObject);
733     void pixelStorei(GC3Denum pname, GC3Dint param);
734     void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
735
736     void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
737
738     void releaseShaderCompiler();
739
740     void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
741     void sampleCoverage(GC3Dclampf value, GC3Dboolean invert);
742     void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
743     void shaderSource(Platform3DObject, const String& string);
744     void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
745     void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
746     void stencilMask(GC3Duint mask);
747     void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
748     void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
749     void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
750
751     bool texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
752     void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
753     void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
754     void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels);
755
756     void uniform1f(GC3Dint location, GC3Dfloat x);
757     void uniform1fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
758     void uniform1i(GC3Dint location, GC3Dint x);
759     void uniform1iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
760     void uniform2f(GC3Dint location, GC3Dfloat x, GC3Dfloat y);
761     void uniform2fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
762     void uniform2i(GC3Dint location, GC3Dint x, GC3Dint y);
763     void uniform2iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
764     void uniform3f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
765     void uniform3fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
766     void uniform3i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z);
767     void uniform3iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
768     void uniform4f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
769     void uniform4fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
770     void uniform4i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w);
771     void uniform4iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
772     void uniformMatrix2fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
773     void uniformMatrix3fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
774     void uniformMatrix4fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
775
776     void useProgram(Platform3DObject);
777     void validateProgram(Platform3DObject);
778
779     void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
780     void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values);
781     void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
782     void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values);
783     void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
784     void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values);
785     void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
786     void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values);
787     void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
788                              GC3Dsizei stride, GC3Dintptr offset);
789
790     void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
791
792     void reshape(int width, int height);
793
794 #if USE(CG)
795     static void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
796                               int canvasWidth, int canvasHeight, CGContextRef);
797 #elif PLATFORM(GTK) || PLATFORM(EFL)
798     void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
799                        int canvasWidth, int canvasHeight, PlatformContextCairo* context);
800 #elif PLATFORM(QT)
801     void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
802                        int canvasWidth, int canvasHeight, QPainter* context);
803 #elif PLATFORM(BLACKBERRY)
804     void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
805                        int canvasWidth, int canvasHeight, GraphicsContext*);
806 #endif
807
808     void markContextChanged();
809     void markLayerComposited();
810     bool layerComposited() const;
811
812     void paintRenderingResultsToCanvas(ImageBuffer*, DrawingBuffer*);
813     PassRefPtr<ImageData> paintRenderingResultsToImageData(DrawingBuffer*);
814     bool paintCompositedResultsToCanvas(ImageBuffer*);
815
816 #if PLATFORM(BLACKBERRY)
817     bool paintsIntoCanvasBuffer() const;
818 #endif
819
820     // Support for buffer creation and deletion
821     Platform3DObject createBuffer();
822     Platform3DObject createFramebuffer();
823     Platform3DObject createProgram();
824     Platform3DObject createRenderbuffer();
825     Platform3DObject createShader(GC3Denum);
826     Platform3DObject createTexture();
827
828     void deleteBuffer(Platform3DObject);
829     void deleteFramebuffer(Platform3DObject);
830     void deleteProgram(Platform3DObject);
831     void deleteRenderbuffer(Platform3DObject);
832     void deleteShader(Platform3DObject);
833     void deleteTexture(Platform3DObject);
834
835     // Synthesizes an OpenGL error which will be returned from a
836     // later call to getError. This is used to emulate OpenGL ES
837     // 2.0 behavior on the desktop and to enforce additional error
838     // checking mandated by WebGL.
839     //
840     // Per the behavior of glGetError, this stores at most one
841     // instance of any given error, and returns them from calls to
842     // getError in the order they were added.
843     void synthesizeGLError(GC3Denum error);
844
845     // Support for extensions. Returns a non-null object, though not
846     // all methods it contains may necessarily be supported on the
847     // current hardware. Must call Extensions3D::supports() to
848     // determine this.
849     Extensions3D* getExtensions();
850
851     IntSize getInternalFramebufferSize() const;
852
853     static unsigned getClearBitsByAttachmentType(GC3Denum);
854     static unsigned getClearBitsByFormat(GC3Denum);
855
856     enum ChannelBits {
857         ChannelRed = 1,
858         ChannelGreen = 2,
859         ChannelBlue = 4,
860         ChannelAlpha = 8,
861         ChannelDepth = 16,
862         ChannelStencil = 32,
863         ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
864         ChannelRGBA = ChannelRGB | ChannelAlpha,
865     };
866
867     static unsigned getChannelBitsByFormat(GC3Denum);
868
869     // Possible alpha operations that may need to occur during
870     // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
871     // be removed.
872     enum AlphaOp {
873         AlphaDoNothing = 0,
874         AlphaDoPremultiply = 1,
875         AlphaDoUnmultiply = 2
876     };
877
878     enum ImageHtmlDomSource {
879         HtmlDomImage = 0,
880         HtmlDomCanvas = 1,
881         HtmlDomVideo = 2,
882         HtmlDomNone = 3
883     };
884
885     // Packs the contents of the given Image which is passed in |pixels| into the passed Vector
886     // according to the given format and type, and obeying the flipY and AlphaOp flags.
887     // Returns true upon success.
888     static bool packImageData(Image*, const void* pixels, GC3Denum format, GC3Denum type, bool flipY, AlphaOp, SourceDataFormat sourceFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, Vector<uint8_t>& data);
889
890     class ImageExtractor {
891     public:
892         ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
893
894         // Each platform must provide an implementation of this method to deallocate or release resources
895         // associated with the image if needed.
896         ~ImageExtractor();
897
898         bool extractSucceeded() { return m_extractSucceeded; }
899         const void* imagePixelData() { return m_imagePixelData; }
900         unsigned imageWidth() { return m_imageWidth; }
901         unsigned imageHeight() { return m_imageHeight; }
902         SourceDataFormat imageSourceFormat() { return m_imageSourceFormat; }
903         AlphaOp imageAlphaOp() { return m_alphaOp; }
904         unsigned imageSourceUnpackAlignment() { return m_imageSourceUnpackAlignment; }
905         ImageHtmlDomSource imageHtmlDomSource() { return m_imageHtmlDomSource; }
906     private:
907         // Each platform must provide an implementation of this method.
908         // Extracts the image and keeps track of its status, such as width, height, Source Alignment, format and AlphaOp etc,
909         // needs to lock the resources or relevant data if needed and returns true upon success
910         bool extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
911
912 #if USE(SKIA)
913         OwnPtr<NativeImageSkia> m_nativeImage;
914         NativeImageSkia* m_skiaImage;
915 #elif USE(CAIRO)
916         ImageSource* m_decoder;
917         RefPtr<cairo_surface_t> m_imageSurface;
918 #elif USE(CG)
919         CGImageRef m_cgImage;
920         RetainPtr<CGImageRef> m_decodedImage;
921         RetainPtr<CFDataRef> m_pixelData;
922 #elif PLATFORM(QT)
923         QImage m_qtImage;
924 #endif
925         Image* m_image;
926         ImageHtmlDomSource m_imageHtmlDomSource;
927         bool m_extractSucceeded;
928         const void* m_imagePixelData;
929         unsigned m_imageWidth;
930         unsigned m_imageHeight;
931         SourceDataFormat m_imageSourceFormat;
932         AlphaOp m_alphaOp;
933         unsigned m_imageSourceUnpackAlignment;
934     };
935
936 private:
937     GraphicsContext3D(Attributes, HostWindow*, RenderStyle = RenderOffscreen);
938
939     // Helper for getImageData which implements packing of pixel
940     // data into the specified OpenGL destination format and type.
941     // A sourceUnpackAlignment of zero indicates that the source
942     // data is tightly packed. Non-zero values may take a slow path.
943     // Destination data will have no gaps between rows.
944     static bool packPixels(const uint8_t* sourceData,
945                     SourceDataFormat sourceDataFormat,
946                     unsigned int width,
947                     unsigned int height,
948                     unsigned int sourceUnpackAlignment,
949                     unsigned int destinationFormat,
950                     unsigned int destinationType,
951                     AlphaOp alphaOp,
952                     void* destinationData);
953
954 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(QT) || PLATFORM(EFL) || PLATFORM(BLACKBERRY)
955     // Take into account the user's requested context creation attributes,
956     // in particular stencil and antialias, and determine which could or
957     // could not be honored based on the capabilities of the OpenGL
958     // implementation.
959     void validateDepthStencil(const char* packedDepthStencilExtension);
960     void validateAttributes();
961
962     // Read rendering results into a pixel array with the same format as the
963     // backbuffer.
964     void readRenderingResults(unsigned char* pixels, int pixelsSize);
965     void readPixelsAndConvertToBGRAIfNecessary(int x, int y, int width, int height, unsigned char* pixels);
966 #endif
967
968 #if PLATFORM(BLACKBERRY)
969     void logFrameBufferStatus(int line);
970     void readPixelsIMG(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
971 #endif
972
973     bool reshapeFBOs(const IntSize&);
974     void resolveMultisamplingIfNecessary(const IntRect& = IntRect());
975 #if (PLATFORM(QT) || PLATFORM(EFL)) && USE(GRAPHICS_SURFACE)
976     void createGraphicsSurfaces(const IntSize&);
977 #endif
978
979     int m_currentWidth, m_currentHeight;
980     bool isResourceSafe();
981
982 #if PLATFORM(MAC)
983     CGLContextObj m_contextObj;
984     RetainPtr<WebGLLayer> m_webGLLayer;
985 #elif PLATFORM(BLACKBERRY)
986 #if USE(ACCELERATED_COMPOSITING)
987     RefPtr<PlatformLayer> m_compositingLayer;
988 #endif
989     void* m_context;
990 #endif
991
992 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(QT) || PLATFORM(EFL) || PLATFORM(BLACKBERRY)
993     struct SymbolInfo {
994         SymbolInfo()
995             : type(0)
996             , size(0)
997         {
998         }
999
1000         SymbolInfo(GC3Denum type, int size, const String& mappedName)
1001             : type(type)
1002             , size(size)
1003             , mappedName(mappedName)
1004         {
1005         }
1006
1007         bool operator==(SymbolInfo& other) const
1008         {
1009             return type == other.type && size == other.size && mappedName == other.mappedName;
1010         }
1011
1012         GC3Denum type;
1013         int size;
1014         String mappedName;
1015     };
1016
1017     typedef HashMap<String, SymbolInfo> ShaderSymbolMap;
1018
1019     struct ShaderSourceEntry {
1020         GC3Denum type;
1021         String source;
1022         String translatedSource;
1023         String log;
1024         bool isValid;
1025         ShaderSymbolMap attributeMap;
1026         ShaderSymbolMap uniformMap;
1027         ShaderSourceEntry()
1028             : type(VERTEX_SHADER)
1029             , isValid(false)
1030         {
1031         }
1032         
1033         ShaderSymbolMap& symbolMap(ANGLEShaderSymbolType symbolType)
1034         {
1035             ASSERT(symbolType == SHADER_SYMBOL_TYPE_ATTRIBUTE || symbolType == SHADER_SYMBOL_TYPE_UNIFORM);
1036             if (symbolType == SHADER_SYMBOL_TYPE_ATTRIBUTE)
1037                 return attributeMap;
1038             return uniformMap;
1039         }
1040     };
1041
1042     typedef HashMap<Platform3DObject, ShaderSourceEntry> ShaderSourceMap;
1043     ShaderSourceMap m_shaderSourceMap;
1044
1045     String mappedSymbolName(Platform3DObject program, ANGLEShaderSymbolType, const String& name);
1046     String originalSymbolName(Platform3DObject program, ANGLEShaderSymbolType, const String& name);
1047
1048     ANGLEWebKitBridge m_compiler;
1049 #endif
1050
1051 #if PLATFORM(BLACKBERRY) || (PLATFORM(QT) && defined(QT_OPENGL_ES_2)) || ((PLATFORM(GTK) || PLATFORM(EFL)) && USE(OPENGL_ES_2))
1052     friend class Extensions3DOpenGLES;
1053     OwnPtr<Extensions3DOpenGLES> m_extensions;
1054 #elif !PLATFORM(CHROMIUM)
1055     friend class Extensions3DOpenGL;
1056     OwnPtr<Extensions3DOpenGL> m_extensions;
1057 #endif
1058     friend class Extensions3DOpenGLCommon;
1059
1060     Attributes m_attrs;
1061     RenderStyle m_renderStyle;
1062     Vector<Vector<float> > m_vertexArray;
1063
1064     GC3Duint m_texture;
1065 #if !PLATFORM(BLACKBERRY)
1066     GC3Duint m_compositorTexture;
1067 #endif
1068     GC3Duint m_fbo;
1069
1070 #if !PLATFORM(BLACKBERRY)
1071     GC3Duint m_depthBuffer;
1072     GC3Duint m_stencilBuffer;
1073 #endif
1074     GC3Duint m_depthStencilBuffer;
1075
1076     bool m_layerComposited;
1077     GC3Duint m_internalColorFormat;
1078
1079     // For tracking which FBO/texture is bound
1080     GC3Duint m_boundFBO;
1081     GC3Denum m_activeTexture;
1082     GC3Duint m_boundTexture0;
1083
1084     // For multisampling
1085     GC3Duint m_multisampleFBO;
1086     GC3Duint m_multisampleDepthStencilBuffer;
1087     GC3Duint m_multisampleColorBuffer;
1088
1089     // Errors raised by synthesizeGLError().
1090     ListHashSet<GC3Denum> m_syntheticErrors;
1091
1092 #if PLATFORM(BLACKBERRY)
1093     bool m_isImaginationHardware;
1094 #endif
1095
1096 #if !PLATFORM(BLACKBERRY)
1097     friend class GraphicsContext3DPrivate;
1098     OwnPtr<GraphicsContext3DPrivate> m_private;
1099 #endif
1100 };
1101
1102 } // namespace WebCore
1103
1104 #endif // GraphicsContext3D_h