Support passing preferLowPowerToHighPerformance and failIfMajorPerformanceCaveat
[WebKit-https.git] / Source / WebCore / platform / graphics / GraphicsContext3D.h
1 /*
2  * Copyright (C) 2009, 2014 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 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 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 "ANGLEWebKitBridge.h"
30 #include "GraphicsTypes3D.h"
31 #include "Image.h"
32 #include "IntRect.h"
33 #include "PlatformLayer.h"
34 #include <memory>
35 #include <wtf/HashMap.h>
36 #include <wtf/ListHashSet.h>
37 #include <wtf/Noncopyable.h>
38 #include <wtf/RefCounted.h>
39 #include <wtf/text/WTFString.h>
40
41 #if USE(CA)
42 #include "PlatformCALayer.h"
43 #endif
44
45 // FIXME: Find a better way to avoid the name confliction for NO_ERROR.
46 #if PLATFORM(WIN)
47 #undef NO_ERROR
48 #elif PLATFORM(GTK)
49 // This define is from the X11 headers, but it's used below, so we must undefine it.
50 #undef VERSION
51 #endif
52
53 #if PLATFORM(COCOA)
54 #if PLATFORM(IOS)
55 #include <OpenGLES/ES2/gl.h>
56 #ifdef __OBJC__
57 #import <OpenGLES/EAGL.h>
58 #endif // __OBJC__
59 #endif // PLATFORM(IOS)
60 #include <wtf/RetainPtr.h>
61 OBJC_CLASS CALayer;
62 OBJC_CLASS WebGLLayer;
63 #elif PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(WIN_CAIRO)
64 typedef unsigned int GLuint;
65 #endif
66
67 #if PLATFORM(IOS)
68 #ifdef __OBJC__
69 typedef EAGLContext* PlatformGraphicsContext3D;
70 #else
71 typedef void* PlatformGraphicsContext3D;
72 #endif // __OBJC__
73 #elif PLATFORM(MAC)
74 typedef struct _CGLContextObject *CGLContextObj;
75
76 typedef CGLContextObj PlatformGraphicsContext3D;
77 #else
78 typedef void* PlatformGraphicsContext3D;
79 typedef void* PlatformGraphicsSurface3D;
80 #endif
81
82 // These are currently the same among all implementations.
83 const PlatformGraphicsContext3D NullPlatformGraphicsContext3D = 0;
84 const Platform3DObject NullPlatform3DObject = 0;
85
86 namespace WebCore {
87 class Extensions3D;
88 #if USE(OPENGL_ES_2)
89 class Extensions3DOpenGLES;
90 #else
91 class Extensions3DOpenGL;
92 #endif
93 class HostWindow;
94 class Image;
95 class ImageBuffer;
96 class ImageSource;
97 class ImageData;
98 class IntRect;
99 class IntSize;
100 class WebGLRenderingContextBase;
101 #if USE(CAIRO)
102 class PlatformContextCairo;
103 #endif
104
105 typedef WTF::HashMap<CString, uint64_t> ShaderNameHash;
106
107 struct ActiveInfo {
108     String name;
109     GC3Denum type;
110     GC3Dint size;
111 };
112
113 class GraphicsContext3DPrivate;
114
115 class GraphicsContext3D : public RefCounted<GraphicsContext3D> {
116 public:
117     enum {
118         // WebGL 1 constants
119         DEPTH_BUFFER_BIT = 0x00000100,
120         STENCIL_BUFFER_BIT = 0x00000400,
121         COLOR_BUFFER_BIT = 0x00004000,
122         POINTS = 0x0000,
123         LINES = 0x0001,
124         LINE_LOOP = 0x0002,
125         LINE_STRIP = 0x0003,
126         TRIANGLES = 0x0004,
127         TRIANGLE_STRIP = 0x0005,
128         TRIANGLE_FAN = 0x0006,
129         ZERO = 0,
130         ONE = 1,
131         SRC_COLOR = 0x0300,
132         ONE_MINUS_SRC_COLOR = 0x0301,
133         SRC_ALPHA = 0x0302,
134         ONE_MINUS_SRC_ALPHA = 0x0303,
135         DST_ALPHA = 0x0304,
136         ONE_MINUS_DST_ALPHA = 0x0305,
137         DST_COLOR = 0x0306,
138         ONE_MINUS_DST_COLOR = 0x0307,
139         SRC_ALPHA_SATURATE = 0x0308,
140         FUNC_ADD = 0x8006,
141         BLEND_EQUATION = 0x8009,
142         BLEND_EQUATION_RGB = 0x8009,
143         BLEND_EQUATION_ALPHA = 0x883D,
144         FUNC_SUBTRACT = 0x800A,
145         FUNC_REVERSE_SUBTRACT = 0x800B,
146         BLEND_DST_RGB = 0x80C8,
147         BLEND_SRC_RGB = 0x80C9,
148         BLEND_DST_ALPHA = 0x80CA,
149         BLEND_SRC_ALPHA = 0x80CB,
150         CONSTANT_COLOR = 0x8001,
151         ONE_MINUS_CONSTANT_COLOR = 0x8002,
152         CONSTANT_ALPHA = 0x8003,
153         ONE_MINUS_CONSTANT_ALPHA = 0x8004,
154         BLEND_COLOR = 0x8005,
155         ARRAY_BUFFER = 0x8892,
156         ELEMENT_ARRAY_BUFFER = 0x8893,
157         ARRAY_BUFFER_BINDING = 0x8894,
158         ELEMENT_ARRAY_BUFFER_BINDING = 0x8895,
159         STREAM_DRAW = 0x88E0,
160         STATIC_DRAW = 0x88E4,
161         DYNAMIC_DRAW = 0x88E8,
162         BUFFER_SIZE = 0x8764,
163         BUFFER_USAGE = 0x8765,
164         CURRENT_VERTEX_ATTRIB = 0x8626,
165         FRONT = 0x0404,
166         BACK = 0x0405,
167         FRONT_AND_BACK = 0x0408,
168         TEXTURE_2D = 0x0DE1,
169         CULL_FACE = 0x0B44,
170         BLEND = 0x0BE2,
171         DITHER = 0x0BD0,
172         STENCIL_TEST = 0x0B90,
173         DEPTH_TEST = 0x0B71,
174         SCISSOR_TEST = 0x0C11,
175         POLYGON_OFFSET_FILL = 0x8037,
176         SAMPLE_ALPHA_TO_COVERAGE = 0x809E,
177         SAMPLE_COVERAGE = 0x80A0,
178         NO_ERROR = 0,
179         INVALID_ENUM = 0x0500,
180         INVALID_VALUE = 0x0501,
181         INVALID_OPERATION = 0x0502,
182         OUT_OF_MEMORY = 0x0505,
183         CW = 0x0900,
184         CCW = 0x0901,
185         LINE_WIDTH = 0x0B21,
186         ALIASED_POINT_SIZE_RANGE = 0x846D,
187         ALIASED_LINE_WIDTH_RANGE = 0x846E,
188         CULL_FACE_MODE = 0x0B45,
189         FRONT_FACE = 0x0B46,
190         DEPTH_RANGE = 0x0B70,
191         DEPTH_WRITEMASK = 0x0B72,
192         DEPTH_CLEAR_VALUE = 0x0B73,
193         DEPTH_FUNC = 0x0B74,
194         STENCIL_CLEAR_VALUE = 0x0B91,
195         STENCIL_FUNC = 0x0B92,
196         STENCIL_FAIL = 0x0B94,
197         STENCIL_PASS_DEPTH_FAIL = 0x0B95,
198         STENCIL_PASS_DEPTH_PASS = 0x0B96,
199         STENCIL_REF = 0x0B97,
200         STENCIL_VALUE_MASK = 0x0B93,
201         STENCIL_WRITEMASK = 0x0B98,
202         STENCIL_BACK_FUNC = 0x8800,
203         STENCIL_BACK_FAIL = 0x8801,
204         STENCIL_BACK_PASS_DEPTH_FAIL = 0x8802,
205         STENCIL_BACK_PASS_DEPTH_PASS = 0x8803,
206         STENCIL_BACK_REF = 0x8CA3,
207         STENCIL_BACK_VALUE_MASK = 0x8CA4,
208         STENCIL_BACK_WRITEMASK = 0x8CA5,
209         VIEWPORT = 0x0BA2,
210         SCISSOR_BOX = 0x0C10,
211         COLOR_CLEAR_VALUE = 0x0C22,
212         COLOR_WRITEMASK = 0x0C23,
213         UNPACK_ALIGNMENT = 0x0CF5,
214         PACK_ALIGNMENT = 0x0D05,
215         MAX_TEXTURE_SIZE = 0x0D33,
216         MAX_VIEWPORT_DIMS = 0x0D3A,
217         SUBPIXEL_BITS = 0x0D50,
218         RED_BITS = 0x0D52,
219         GREEN_BITS = 0x0D53,
220         BLUE_BITS = 0x0D54,
221         ALPHA_BITS = 0x0D55,
222         DEPTH_BITS = 0x0D56,
223         STENCIL_BITS = 0x0D57,
224         POLYGON_OFFSET_UNITS = 0x2A00,
225         POLYGON_OFFSET_FACTOR = 0x8038,
226         TEXTURE_BINDING_2D = 0x8069,
227         SAMPLE_BUFFERS = 0x80A8,
228         SAMPLES = 0x80A9,
229         SAMPLE_COVERAGE_VALUE = 0x80AA,
230         SAMPLE_COVERAGE_INVERT = 0x80AB,
231         NUM_COMPRESSED_TEXTURE_FORMATS = 0x86A2,
232         COMPRESSED_TEXTURE_FORMATS = 0x86A3,
233         DONT_CARE = 0x1100,
234         FASTEST = 0x1101,
235         NICEST = 0x1102,
236         GENERATE_MIPMAP_HINT = 0x8192,
237         BYTE = 0x1400,
238         UNSIGNED_BYTE = 0x1401,
239         SHORT = 0x1402,
240         UNSIGNED_SHORT = 0x1403,
241         INT = 0x1404,
242         UNSIGNED_INT = 0x1405,
243         FLOAT = 0x1406,
244         HALF_FLOAT_OES = 0x8D61,
245         FIXED = 0x140C,
246         DEPTH_COMPONENT = 0x1902,
247         ALPHA = 0x1906,
248         RGB = 0x1907,
249         RGBA = 0x1908,
250         BGRA = 0x80E1,
251         LUMINANCE = 0x1909,
252         LUMINANCE_ALPHA = 0x190A,
253         UNSIGNED_SHORT_4_4_4_4 = 0x8033,
254         UNSIGNED_SHORT_5_5_5_1 = 0x8034,
255         UNSIGNED_SHORT_5_6_5 = 0x8363,
256         FRAGMENT_SHADER = 0x8B30,
257         VERTEX_SHADER = 0x8B31,
258         MAX_VERTEX_ATTRIBS = 0x8869,
259         MAX_VERTEX_UNIFORM_VECTORS = 0x8DFB,
260         MAX_VARYING_VECTORS = 0x8DFC,
261         MAX_COMBINED_TEXTURE_IMAGE_UNITS = 0x8B4D,
262         MAX_VERTEX_TEXTURE_IMAGE_UNITS = 0x8B4C,
263         MAX_TEXTURE_IMAGE_UNITS = 0x8872,
264         MAX_FRAGMENT_UNIFORM_VECTORS = 0x8DFD,
265         SHADER_TYPE = 0x8B4F,
266         DELETE_STATUS = 0x8B80,
267         LINK_STATUS = 0x8B82,
268         VALIDATE_STATUS = 0x8B83,
269         ATTACHED_SHADERS = 0x8B85,
270         ACTIVE_UNIFORMS = 0x8B86,
271         ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87,
272         ACTIVE_ATTRIBUTES = 0x8B89,
273         ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A,
274         SHADING_LANGUAGE_VERSION = 0x8B8C,
275         CURRENT_PROGRAM = 0x8B8D,
276         NEVER = 0x0200,
277         LESS = 0x0201,
278         EQUAL = 0x0202,
279         LEQUAL = 0x0203,
280         GREATER = 0x0204,
281         NOTEQUAL = 0x0205,
282         GEQUAL = 0x0206,
283         ALWAYS = 0x0207,
284         KEEP = 0x1E00,
285         REPLACE = 0x1E01,
286         INCR = 0x1E02,
287         DECR = 0x1E03,
288         INVERT = 0x150A,
289         INCR_WRAP = 0x8507,
290         DECR_WRAP = 0x8508,
291         VENDOR = 0x1F00,
292         RENDERER = 0x1F01,
293         VERSION = 0x1F02,
294         EXTENSIONS = 0x1F03,
295         NEAREST = 0x2600,
296         LINEAR = 0x2601,
297         NEAREST_MIPMAP_NEAREST = 0x2700,
298         LINEAR_MIPMAP_NEAREST = 0x2701,
299         NEAREST_MIPMAP_LINEAR = 0x2702,
300         LINEAR_MIPMAP_LINEAR = 0x2703,
301         TEXTURE_MAG_FILTER = 0x2800,
302         TEXTURE_MIN_FILTER = 0x2801,
303         TEXTURE_WRAP_S = 0x2802,
304         TEXTURE_WRAP_T = 0x2803,
305         TEXTURE = 0x1702,
306         TEXTURE_CUBE_MAP = 0x8513,
307         TEXTURE_BINDING_CUBE_MAP = 0x8514,
308         TEXTURE_CUBE_MAP_POSITIVE_X = 0x8515,
309         TEXTURE_CUBE_MAP_NEGATIVE_X = 0x8516,
310         TEXTURE_CUBE_MAP_POSITIVE_Y = 0x8517,
311         TEXTURE_CUBE_MAP_NEGATIVE_Y = 0x8518,
312         TEXTURE_CUBE_MAP_POSITIVE_Z = 0x8519,
313         TEXTURE_CUBE_MAP_NEGATIVE_Z = 0x851A,
314         MAX_CUBE_MAP_TEXTURE_SIZE = 0x851C,
315         TEXTURE0 = 0x84C0,
316         TEXTURE1 = 0x84C1,
317         TEXTURE2 = 0x84C2,
318         TEXTURE3 = 0x84C3,
319         TEXTURE4 = 0x84C4,
320         TEXTURE5 = 0x84C5,
321         TEXTURE6 = 0x84C6,
322         TEXTURE7 = 0x84C7,
323         TEXTURE8 = 0x84C8,
324         TEXTURE9 = 0x84C9,
325         TEXTURE10 = 0x84CA,
326         TEXTURE11 = 0x84CB,
327         TEXTURE12 = 0x84CC,
328         TEXTURE13 = 0x84CD,
329         TEXTURE14 = 0x84CE,
330         TEXTURE15 = 0x84CF,
331         TEXTURE16 = 0x84D0,
332         TEXTURE17 = 0x84D1,
333         TEXTURE18 = 0x84D2,
334         TEXTURE19 = 0x84D3,
335         TEXTURE20 = 0x84D4,
336         TEXTURE21 = 0x84D5,
337         TEXTURE22 = 0x84D6,
338         TEXTURE23 = 0x84D7,
339         TEXTURE24 = 0x84D8,
340         TEXTURE25 = 0x84D9,
341         TEXTURE26 = 0x84DA,
342         TEXTURE27 = 0x84DB,
343         TEXTURE28 = 0x84DC,
344         TEXTURE29 = 0x84DD,
345         TEXTURE30 = 0x84DE,
346         TEXTURE31 = 0x84DF,
347         ACTIVE_TEXTURE = 0x84E0,
348         REPEAT = 0x2901,
349         CLAMP_TO_EDGE = 0x812F,
350         MIRRORED_REPEAT = 0x8370,
351         FLOAT_VEC2 = 0x8B50,
352         FLOAT_VEC3 = 0x8B51,
353         FLOAT_VEC4 = 0x8B52,
354         INT_VEC2 = 0x8B53,
355         INT_VEC3 = 0x8B54,
356         INT_VEC4 = 0x8B55,
357         BOOL = 0x8B56,
358         BOOL_VEC2 = 0x8B57,
359         BOOL_VEC3 = 0x8B58,
360         BOOL_VEC4 = 0x8B59,
361         FLOAT_MAT2 = 0x8B5A,
362         FLOAT_MAT3 = 0x8B5B,
363         FLOAT_MAT4 = 0x8B5C,
364         SAMPLER_2D = 0x8B5E,
365         SAMPLER_CUBE = 0x8B60,
366         VERTEX_ATTRIB_ARRAY_ENABLED = 0x8622,
367         VERTEX_ATTRIB_ARRAY_SIZE = 0x8623,
368         VERTEX_ATTRIB_ARRAY_STRIDE = 0x8624,
369         VERTEX_ATTRIB_ARRAY_TYPE = 0x8625,
370         VERTEX_ATTRIB_ARRAY_NORMALIZED = 0x886A,
371         VERTEX_ATTRIB_ARRAY_POINTER = 0x8645,
372         VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = 0x889F,
373         IMPLEMENTATION_COLOR_READ_TYPE = 0x8B9A,
374         IMPLEMENTATION_COLOR_READ_FORMAT = 0x8B9B,
375         COMPILE_STATUS = 0x8B81,
376         INFO_LOG_LENGTH = 0x8B84,
377         SHADER_SOURCE_LENGTH = 0x8B88,
378         SHADER_COMPILER = 0x8DFA,
379         SHADER_BINARY_FORMATS = 0x8DF8,
380         NUM_SHADER_BINARY_FORMATS = 0x8DF9,
381         LOW_FLOAT = 0x8DF0,
382         MEDIUM_FLOAT = 0x8DF1,
383         HIGH_FLOAT = 0x8DF2,
384         LOW_INT = 0x8DF3,
385         MEDIUM_INT = 0x8DF4,
386         HIGH_INT = 0x8DF5,
387         FRAMEBUFFER = 0x8D40,
388         RENDERBUFFER = 0x8D41,
389         RGBA4 = 0x8056,
390         RGB5_A1 = 0x8057,
391         RGB565 = 0x8D62,
392         DEPTH_COMPONENT16 = 0x81A5,
393         STENCIL_INDEX = 0x1901,
394         STENCIL_INDEX8 = 0x8D48,
395         RENDERBUFFER_WIDTH = 0x8D42,
396         RENDERBUFFER_HEIGHT = 0x8D43,
397         RENDERBUFFER_INTERNAL_FORMAT = 0x8D44,
398         RENDERBUFFER_RED_SIZE = 0x8D50,
399         RENDERBUFFER_GREEN_SIZE = 0x8D51,
400         RENDERBUFFER_BLUE_SIZE = 0x8D52,
401         RENDERBUFFER_ALPHA_SIZE = 0x8D53,
402         RENDERBUFFER_DEPTH_SIZE = 0x8D54,
403         RENDERBUFFER_STENCIL_SIZE = 0x8D55,
404         FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = 0x8CD0,
405         FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = 0x8CD1,
406         FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL = 0x8CD2,
407         FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE = 0x8CD3,
408         COLOR_ATTACHMENT0 = 0x8CE0,
409         DEPTH_ATTACHMENT = 0x8D00,
410         STENCIL_ATTACHMENT = 0x8D20,
411         NONE = 0,
412         FRAMEBUFFER_COMPLETE = 0x8CD5,
413         FRAMEBUFFER_INCOMPLETE_ATTACHMENT = 0x8CD6,
414         FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = 0x8CD7,
415         FRAMEBUFFER_INCOMPLETE_DIMENSIONS = 0x8CD9,
416         FRAMEBUFFER_UNSUPPORTED = 0x8CDD,
417         FRAMEBUFFER_BINDING = 0x8CA6,
418         RENDERBUFFER_BINDING = 0x8CA7,
419         MAX_RENDERBUFFER_SIZE = 0x84E8,
420         INVALID_FRAMEBUFFER_OPERATION = 0x0506,
421
422         // WebGL-specific enums
423         UNPACK_FLIP_Y_WEBGL = 0x9240,
424         UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241,
425         CONTEXT_LOST_WEBGL = 0x9242,
426         UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243,
427         BROWSER_DEFAULT_WEBGL = 0x9244,
428         VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88FE,
429         
430         // WebGL2 constants
431         READ_BUFFER = 0x0C02,
432         UNPACK_ROW_LENGTH = 0x0CF2,
433         UNPACK_SKIP_ROWS = 0x0CF3,
434         UNPACK_SKIP_PIXELS = 0x0CF4,
435         PACK_ROW_LENGTH = 0x0D02,
436         PACK_SKIP_ROWS = 0x0D03,
437         PACK_SKIP_PIXELS = 0x0D04,
438         COLOR = 0x1800,
439         DEPTH = 0x1801,
440         STENCIL = 0x1802,
441         RED = 0x1903,
442         RGB8 = 0x8051,
443         RGBA8 = 0x8058,
444         RGB10_A2 = 0x8059,
445         TEXTURE_BINDING_3D = 0x806A,
446         UNPACK_SKIP_IMAGES = 0x806D,
447         UNPACK_IMAGE_HEIGHT = 0x806E,
448         TEXTURE_3D = 0x806F,
449         TEXTURE_WRAP_R = 0x8072,
450         MAX_3D_TEXTURE_SIZE = 0x8073,
451         UNSIGNED_INT_2_10_10_10_REV = 0x8368,
452         MAX_ELEMENTS_VERTICES = 0x80E8,
453         MAX_ELEMENTS_INDICES = 0x80E9,
454         TEXTURE_MIN_LOD = 0x813A,
455         TEXTURE_MAX_LOD = 0x813B,
456         TEXTURE_BASE_LEVEL = 0x813C,
457         TEXTURE_MAX_LEVEL = 0x813D,
458         MIN = 0x8007,
459         MAX = 0x8008,
460         DEPTH_COMPONENT24 = 0x81A6,
461         MAX_TEXTURE_LOD_BIAS = 0x84FD,
462         TEXTURE_COMPARE_MODE = 0x884C,
463         TEXTURE_COMPARE_FUNC = 0x884D,
464         CURRENT_QUERY = 0x8865,
465         QUERY_RESULT = 0x8866,
466         QUERY_RESULT_AVAILABLE = 0x8867,
467         STREAM_READ = 0x88E1,
468         STREAM_COPY = 0x88E2,
469         STATIC_READ = 0x88E5,
470         STATIC_COPY = 0x88E6,
471         DYNAMIC_READ = 0x88E9,
472         DYNAMIC_COPY = 0x88EA,
473         MAX_DRAW_BUFFERS = 0x8824,
474         DRAW_BUFFER0 = 0x8825,
475         DRAW_BUFFER1 = 0x8826,
476         DRAW_BUFFER2 = 0x8827,
477         DRAW_BUFFER3 = 0x8828,
478         DRAW_BUFFER4 = 0x8829,
479         DRAW_BUFFER5 = 0x882A,
480         DRAW_BUFFER6 = 0x882B,
481         DRAW_BUFFER7 = 0x882C,
482         DRAW_BUFFER8 = 0x882D,
483         DRAW_BUFFER9 = 0x882E,
484         DRAW_BUFFER10 = 0x882F,
485         DRAW_BUFFER11 = 0x8830,
486         DRAW_BUFFER12 = 0x8831,
487         DRAW_BUFFER13 = 0x8832,
488         DRAW_BUFFER14 = 0x8833,
489         DRAW_BUFFER15 = 0x8834,
490         MAX_FRAGMENT_UNIFORM_COMPONENTS = 0x8B49,
491         MAX_VERTEX_UNIFORM_COMPONENTS = 0x8B4A,
492         SAMPLER_3D = 0x8B5F,
493         SAMPLER_2D_SHADOW = 0x8B62,
494         FRAGMENT_SHADER_DERIVATIVE_HINT = 0x8B8B,
495         PIXEL_PACK_BUFFER = 0x88EB,
496         PIXEL_UNPACK_BUFFER = 0x88EC,
497         PIXEL_PACK_BUFFER_BINDING = 0x88ED,
498         PIXEL_UNPACK_BUFFER_BINDING = 0x88EF,
499         FLOAT_MAT2x3 = 0x8B65,
500         FLOAT_MAT2x4 = 0x8B66,
501         FLOAT_MAT3x2 = 0x8B67,
502         FLOAT_MAT3x4 = 0x8B68,
503         FLOAT_MAT4x2 = 0x8B69,
504         FLOAT_MAT4x3 = 0x8B6A,
505         SRGB = 0x8C40,
506         SRGB8 = 0x8C41,
507         SRGB_ALPHA = 0x8C42,
508         SRGB8_ALPHA8 = 0x8C43,
509         COMPARE_REF_TO_TEXTURE = 0x884E,
510         RGBA32F = 0x8814,
511         RGB32F = 0x8815,
512         RGBA16F = 0x881A,
513         RGB16F = 0x881B,
514         VERTEX_ATTRIB_ARRAY_INTEGER = 0x88FD,
515         MAX_ARRAY_TEXTURE_LAYERS = 0x88FF,
516         MIN_PROGRAM_TEXEL_OFFSET = 0x8904,
517         MAX_PROGRAM_TEXEL_OFFSET = 0x8905,
518         MAX_VARYING_COMPONENTS = 0x8B4B,
519         TEXTURE_2D_ARRAY = 0x8C1A,
520         TEXTURE_BINDING_2D_ARRAY = 0x8C1D,
521         R11F_G11F_B10F = 0x8C3A,
522         UNSIGNED_INT_10F_11F_11F_REV = 0x8C3B,
523         RGB9_E5 = 0x8C3D,
524         UNSIGNED_INT_5_9_9_9_REV = 0x8C3E,
525         TRANSFORM_FEEDBACK_BUFFER_MODE = 0x8C7F,
526         MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS = 0x8C80,
527         TRANSFORM_FEEDBACK_VARYINGS = 0x8C83,
528         TRANSFORM_FEEDBACK_BUFFER_START = 0x8C84,
529         TRANSFORM_FEEDBACK_BUFFER_SIZE = 0x8C85,
530         TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN = 0x8C88,
531         RASTERIZER_DISCARD = 0x8C89,
532         MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS = 0x8C8A,
533         MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS = 0x8C8B,
534         INTERLEAVED_ATTRIBS = 0x8C8C,
535         SEPARATE_ATTRIBS = 0x8C8D,
536         TRANSFORM_FEEDBACK_BUFFER = 0x8C8E,
537         TRANSFORM_FEEDBACK_BUFFER_BINDING = 0x8C8F,
538         RGBA32UI = 0x8D70,
539         RGB32UI = 0x8D71,
540         RGBA16UI = 0x8D76,
541         RGB16UI = 0x8D77,
542         RGBA8UI = 0x8D7C,
543         RGB8UI = 0x8D7D,
544         RGBA32I = 0x8D82,
545         RGB32I = 0x8D83,
546         RGBA16I = 0x8D88,
547         RGB16I = 0x8D89,
548         RGBA8I = 0x8D8E,
549         RGB8I = 0x8D8F,
550         RED_INTEGER = 0x8D94,
551         RGB_INTEGER = 0x8D98,
552         RGBA_INTEGER = 0x8D99,
553         SAMPLER_2D_ARRAY = 0x8DC1,
554         SAMPLER_2D_ARRAY_SHADOW = 0x8DC4,
555         SAMPLER_CUBE_SHADOW = 0x8DC5,
556         UNSIGNED_INT_VEC2 = 0x8DC6,
557         UNSIGNED_INT_VEC3 = 0x8DC7,
558         UNSIGNED_INT_VEC4 = 0x8DC8,
559         INT_SAMPLER_2D = 0x8DCA,
560         INT_SAMPLER_3D = 0x8DCB,
561         INT_SAMPLER_CUBE = 0x8DCC,
562         INT_SAMPLER_2D_ARRAY = 0x8DCF,
563         UNSIGNED_INT_SAMPLER_2D = 0x8DD2,
564         UNSIGNED_INT_SAMPLER_3D = 0x8DD3,
565         UNSIGNED_INT_SAMPLER_CUBE = 0x8DD4,
566         UNSIGNED_INT_SAMPLER_2D_ARRAY = 0x8DD7,
567         DEPTH_COMPONENT32F = 0x8CAC,
568         DEPTH32F_STENCIL8 = 0x8CAD,
569         FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD,
570         FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING = 0x8210,
571         FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE = 0x8211,
572         FRAMEBUFFER_ATTACHMENT_RED_SIZE = 0x8212,
573         FRAMEBUFFER_ATTACHMENT_GREEN_SIZE = 0x8213,
574         FRAMEBUFFER_ATTACHMENT_BLUE_SIZE = 0x8214,
575         FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE = 0x8215,
576         FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE = 0x8216,
577         FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE = 0x8217,
578         FRAMEBUFFER_DEFAULT = 0x8218,
579         DEPTH_STENCIL_ATTACHMENT = 0x821A,
580         DEPTH_STENCIL = 0x84F9,
581         UNSIGNED_INT_24_8 = 0x84FA,
582         DEPTH24_STENCIL8 = 0x88F0,
583         UNSIGNED_NORMALIZED = 0x8C17,
584         DRAW_FRAMEBUFFER_BINDING = 0x8CA6, /* Same as FRAMEBUFFER_BINDING */
585         READ_FRAMEBUFFER = 0x8CA8,
586         DRAW_FRAMEBUFFER = 0x8CA9,
587         READ_FRAMEBUFFER_BINDING = 0x8CAA,
588         RENDERBUFFER_SAMPLES = 0x8CAB,
589         FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER = 0x8CD4,
590         MAX_COLOR_ATTACHMENTS = 0x8CDF,
591         COLOR_ATTACHMENT1 = 0x8CE1,
592         COLOR_ATTACHMENT2 = 0x8CE2,
593         COLOR_ATTACHMENT3 = 0x8CE3,
594         COLOR_ATTACHMENT4 = 0x8CE4,
595         COLOR_ATTACHMENT5 = 0x8CE5,
596         COLOR_ATTACHMENT6 = 0x8CE6,
597         COLOR_ATTACHMENT7 = 0x8CE7,
598         COLOR_ATTACHMENT8 = 0x8CE8,
599         COLOR_ATTACHMENT9 = 0x8CE9,
600         COLOR_ATTACHMENT10 = 0x8CEA,
601         COLOR_ATTACHMENT11 = 0x8CEB,
602         COLOR_ATTACHMENT12 = 0x8CEC,
603         COLOR_ATTACHMENT13 = 0x8CED,
604         COLOR_ATTACHMENT14 = 0x8CEE,
605         COLOR_ATTACHMENT15 = 0x8CEF,
606         FRAMEBUFFER_INCOMPLETE_MULTISAMPLE = 0x8D56,
607         MAX_SAMPLES = 0x8D57,
608         HALF_FLOAT = 0x140B,
609         RG = 0x8227,
610         RG_INTEGER = 0x8228,
611         R8 = 0x8229,
612         RG8 = 0x822B,
613         R16F = 0x822D,
614         R32F = 0x822E,
615         RG16F = 0x822F,
616         RG32F = 0x8230,
617         R8I = 0x8231,
618         R8UI = 0x8232,
619         R16I = 0x8233,
620         R16UI = 0x8234,
621         R32I = 0x8235,
622         R32UI = 0x8236,
623         RG8I = 0x8237,
624         RG8UI = 0x8238,
625         RG16I = 0x8239,
626         RG16UI = 0x823A,
627         RG32I = 0x823B,
628         RG32UI = 0x823C,
629         VERTEX_ARRAY_BINDING = 0x85B5,
630         R8_SNORM = 0x8F94,
631         RG8_SNORM = 0x8F95,
632         RGB8_SNORM = 0x8F96,
633         RGBA8_SNORM = 0x8F97,
634         SIGNED_NORMALIZED = 0x8F9C,
635         COPY_READ_BUFFER = 0x8F36,
636         COPY_WRITE_BUFFER = 0x8F37,
637         COPY_READ_BUFFER_BINDING = 0x8F36, /* Same as COPY_READ_BUFFER */
638         COPY_WRITE_BUFFER_BINDING = 0x8F37, /* Same as COPY_WRITE_BUFFER */
639         UNIFORM_BUFFER = 0x8A11,
640         UNIFORM_BUFFER_BINDING = 0x8A28,
641         UNIFORM_BUFFER_START = 0x8A29,
642         UNIFORM_BUFFER_SIZE = 0x8A2A,
643         MAX_VERTEX_UNIFORM_BLOCKS = 0x8A2B,
644         MAX_FRAGMENT_UNIFORM_BLOCKS = 0x8A2D,
645         MAX_COMBINED_UNIFORM_BLOCKS = 0x8A2E,
646         MAX_UNIFORM_BUFFER_BINDINGS = 0x8A2F,
647         MAX_UNIFORM_BLOCK_SIZE = 0x8A30,
648         MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS = 0x8A31,
649         MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS = 0x8A33,
650         UNIFORM_BUFFER_OFFSET_ALIGNMENT = 0x8A34,
651         ACTIVE_UNIFORM_BLOCKS = 0x8A36,
652         UNIFORM_TYPE = 0x8A37,
653         UNIFORM_SIZE = 0x8A38,
654         UNIFORM_BLOCK_INDEX = 0x8A3A,
655         UNIFORM_OFFSET = 0x8A3B,
656         UNIFORM_ARRAY_STRIDE = 0x8A3C,
657         UNIFORM_MATRIX_STRIDE = 0x8A3D,
658         UNIFORM_IS_ROW_MAJOR = 0x8A3E,
659         UNIFORM_BLOCK_BINDING = 0x8A3F,
660         UNIFORM_BLOCK_DATA_SIZE = 0x8A40,
661         UNIFORM_BLOCK_ACTIVE_UNIFORMS = 0x8A42,
662         UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES = 0x8A43,
663         UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER = 0x8A44,
664         UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER = 0x8A46,
665         INVALID_INDEX = 0xFFFFFFFF,
666         MAX_VERTEX_OUTPUT_COMPONENTS = 0x9122,
667         MAX_FRAGMENT_INPUT_COMPONENTS = 0x9125,
668         MAX_SERVER_WAIT_TIMEOUT = 0x9111,
669         OBJECT_TYPE = 0x9112,
670         SYNC_CONDITION = 0x9113,
671         SYNC_STATUS = 0x9114,
672         SYNC_FLAGS = 0x9115,
673         SYNC_FENCE = 0x9116,
674         SYNC_GPU_COMMANDS_COMPLETE = 0x9117,
675         UNSIGNALED = 0x9118,
676         SIGNALED = 0x9119,
677         ALREADY_SIGNALED = 0x911A,
678         TIMEOUT_EXPIRED = 0x911B,
679         CONDITION_SATISFIED = 0x911C,
680 #if PLATFORM(WIN)
681         WAIT_FAILED_WIN = 0x911D,
682 #else
683         WAIT_FAILED = 0x911D,
684 #endif
685         SYNC_FLUSH_COMMANDS_BIT = 0x00000001,
686         VERTEX_ATTRIB_ARRAY_DIVISOR = 0x88FE,
687         ANY_SAMPLES_PASSED = 0x8C2F,
688         ANY_SAMPLES_PASSED_CONSERVATIVE = 0x8D6A,
689         SAMPLER_BINDING = 0x8919,
690         RGB10_A2UI = 0x906F,
691         TEXTURE_SWIZZLE_R = 0x8E42,
692         TEXTURE_SWIZZLE_G = 0x8E43,
693         TEXTURE_SWIZZLE_B = 0x8E44,
694         TEXTURE_SWIZZLE_A = 0x8E45,
695         GREEN = 0x1904,
696         BLUE = 0x1905,
697         INT_2_10_10_10_REV = 0x8D9F,
698         TRANSFORM_FEEDBACK = 0x8E22,
699         TRANSFORM_FEEDBACK_PAUSED = 0x8E23,
700         TRANSFORM_FEEDBACK_ACTIVE = 0x8E24,
701         TRANSFORM_FEEDBACK_BINDING = 0x8E25,
702         COMPRESSED_R11_EAC = 0x9270,
703         COMPRESSED_SIGNED_R11_EAC = 0x9271,
704         COMPRESSED_RG11_EAC = 0x9272,
705         COMPRESSED_SIGNED_RG11_EAC = 0x9273,
706         COMPRESSED_RGB8_ETC2 = 0x9274,
707         COMPRESSED_SRGB8_ETC2 = 0x9275,
708         COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276,
709         COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277,
710         COMPRESSED_RGBA8_ETC2_EAC = 0x9278,
711         COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279,
712         TEXTURE_IMMUTABLE_FORMAT = 0x912F,
713         MAX_ELEMENT_INDEX = 0x8D6B,
714         NUM_SAMPLE_COUNTS = 0x9380,
715         TEXTURE_IMMUTABLE_LEVELS = 0x82DF
716     };
717
718     // Context creation attributes.
719     struct Attributes {
720         bool alpha { true };
721         bool depth { true };
722         bool stencil { false };
723         bool antialias { true };
724         bool premultipliedAlpha { true };
725         bool preserveDrawingBuffer { false };
726         bool noExtensions { false };
727         bool shareResources { true };
728         bool preferLowPowerToHighPerformance { false };
729         bool forceSoftwareRenderer { false };
730         bool failIfMajorPerformanceCaveat { false };
731         bool useGLES3 { false };
732         float devicePixelRatio { 1 };
733     };
734
735     enum RenderStyle {
736         RenderOffscreen,
737         RenderDirectlyToHostWindow,
738         RenderToCurrentGLContext
739     };
740
741     class ContextLostCallback {
742     public:
743         virtual void onContextLost() = 0;
744         virtual ~ContextLostCallback() {}
745     };
746
747     class ErrorMessageCallback {
748     public:
749         virtual void onErrorMessage(const String& message, GC3Dint id) = 0;
750         virtual ~ErrorMessageCallback() { }
751     };
752
753     void setContextLostCallback(std::unique_ptr<ContextLostCallback>);
754     void setErrorMessageCallback(std::unique_ptr<ErrorMessageCallback>);
755
756     static RefPtr<GraphicsContext3D> create(Attributes, HostWindow*, RenderStyle = RenderOffscreen);
757     static PassRefPtr<GraphicsContext3D> createForCurrentGLContext();
758     ~GraphicsContext3D();
759
760 #if PLATFORM(COCOA)
761     PlatformGraphicsContext3D platformGraphicsContext3D() const { return m_contextObj; }
762     Platform3DObject platformTexture() const { return m_compositorTexture; }
763     CALayer* platformLayer() const { return reinterpret_cast<CALayer*>(m_webGLLayer.get()); }
764 #else
765     PlatformGraphicsContext3D platformGraphicsContext3D();
766     Platform3DObject platformTexture() const;
767     PlatformLayer* platformLayer() const;
768 #endif
769
770     bool makeContextCurrent();
771     void setWebGLContext(WebGLRenderingContextBase* base) { m_webglContext = base; }
772
773     // With multisampling on, blit from multisampleFBO to regular FBO.
774     void prepareTexture();
775
776     // Equivalent to ::glTexImage2D(). Allows pixels==0 with no allocation.
777     void texImage2DDirect(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
778
779     // Helper to texImage2D with pixel==0 case: pixels are initialized to 0.
780     // Return true if no GL error is synthesized.
781     // By default, alignment is 4, the OpenGL default setting.
782     bool texImage2DResourceSafe(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint alignment = 4);
783
784     bool isGLES2Compliant() const;
785
786     //----------------------------------------------------------------------
787     // Helpers for texture uploading and pixel readback.
788     //
789
790     // Computes the components per pixel and bytes per component
791     // for the given format and type combination. Returns false if
792     // either was an invalid enum.
793     static bool computeFormatAndTypeParameters(GC3Denum format,
794                                                GC3Denum type,
795                                                unsigned int* componentsPerPixel,
796                                                unsigned int* bytesPerComponent);
797
798     // Computes the image size in bytes. If paddingInBytes is not null, padding
799     // is also calculated in return. Returns NO_ERROR if succeed, otherwise
800     // return the suggested GL error indicating the cause of the failure:
801     //   INVALID_VALUE if width/height is negative or overflow happens.
802     //   INVALID_ENUM if format/type is illegal.
803     static GC3Denum computeImageSizeInBytes(GC3Denum format,
804                                      GC3Denum type,
805                                      GC3Dsizei width,
806                                      GC3Dsizei height,
807                                      GC3Dint alignment,
808                                      unsigned int* imageSizeInBytes,
809                                      unsigned int* paddingInBytes);
810
811     // Extracts the contents of the given ImageData into the passed Vector,
812     // packing the pixel data according to the given format and type,
813     // and obeying the flipY and premultiplyAlpha flags. Returns true
814     // upon success.
815     static bool extractImageData(ImageData*,
816                           GC3Denum format,
817                           GC3Denum type,
818                           bool flipY,
819                           bool premultiplyAlpha,
820                           Vector<uint8_t>& data);
821
822     // Helper function which extracts the user-supplied texture
823     // data, applying the flipY and premultiplyAlpha parameters.
824     // If the data is not tightly packed according to the passed
825     // unpackAlignment, the output data will be tightly packed.
826     // Returns true if successful, false if any error occurred.
827     static bool extractTextureData(unsigned int width, unsigned int height,
828                             GC3Denum format, GC3Denum type,
829                             unsigned int unpackAlignment,
830                             bool flipY, bool premultiplyAlpha,
831                             const void* pixels,
832                             Vector<uint8_t>& data);
833
834
835     // Attempt to enumerate all possible native image formats to
836     // reduce the amount of temporary allocations during texture
837     // uploading. This enum must be public because it is accessed
838     // by non-member functions.
839     enum DataFormat {
840         DataFormatRGBA8 = 0,
841         DataFormatRGBA16Little,
842         DataFormatRGBA16Big,
843         DataFormatRGBA16F,
844         DataFormatRGBA32F,
845         DataFormatRGB8,
846         DataFormatRGB16Little,
847         DataFormatRGB16Big,
848         DataFormatRGB16F,
849         DataFormatRGB32F,
850         DataFormatBGR8,
851         DataFormatBGRA8,
852         DataFormatBGRA16Little,
853         DataFormatBGRA16Big,
854         DataFormatARGB8,
855         DataFormatARGB16Little,
856         DataFormatARGB16Big,
857         DataFormatABGR8,
858         DataFormatRGBA5551,
859         DataFormatRGBA4444,
860         DataFormatRGB565,
861         DataFormatR8,
862         DataFormatR16Little,
863         DataFormatR16Big,
864         DataFormatR16F,
865         DataFormatR32F,
866         DataFormatRA8,
867         DataFormatRA16Little,
868         DataFormatRA16Big,
869         DataFormatRA16F,
870         DataFormatRA32F,
871         DataFormatAR8,
872         DataFormatAR16Little,
873         DataFormatAR16Big,
874         DataFormatA8,
875         DataFormatA16Little,
876         DataFormatA16Big,
877         DataFormatA16F,
878         DataFormatA32F,
879         DataFormatNumFormats
880     };
881
882     ALWAYS_INLINE static bool hasAlpha(DataFormat format)
883     {
884         return format == GraphicsContext3D::DataFormatA8
885             || format == GraphicsContext3D::DataFormatA16F
886             || format == GraphicsContext3D::DataFormatA32F
887             || format == GraphicsContext3D::DataFormatRA8
888             || format == GraphicsContext3D::DataFormatAR8
889             || format == GraphicsContext3D::DataFormatRA16F
890             || format == GraphicsContext3D::DataFormatRA32F
891             || format == GraphicsContext3D::DataFormatRGBA8
892             || format == GraphicsContext3D::DataFormatBGRA8
893             || format == GraphicsContext3D::DataFormatARGB8
894             || format == GraphicsContext3D::DataFormatABGR8
895             || format == GraphicsContext3D::DataFormatRGBA16F
896             || format == GraphicsContext3D::DataFormatRGBA32F
897             || format == GraphicsContext3D::DataFormatRGBA4444
898             || format == GraphicsContext3D::DataFormatRGBA5551;
899     }
900
901     ALWAYS_INLINE static bool hasColor(DataFormat format)
902     {
903         return format == GraphicsContext3D::DataFormatRGBA8
904             || format == GraphicsContext3D::DataFormatRGBA16F
905             || format == GraphicsContext3D::DataFormatRGBA32F
906             || format == GraphicsContext3D::DataFormatRGB8
907             || format == GraphicsContext3D::DataFormatRGB16F
908             || format == GraphicsContext3D::DataFormatRGB32F
909             || format == GraphicsContext3D::DataFormatBGR8
910             || format == GraphicsContext3D::DataFormatBGRA8
911             || format == GraphicsContext3D::DataFormatARGB8
912             || format == GraphicsContext3D::DataFormatABGR8
913             || format == GraphicsContext3D::DataFormatRGBA5551
914             || format == GraphicsContext3D::DataFormatRGBA4444
915             || format == GraphicsContext3D::DataFormatRGB565
916             || format == GraphicsContext3D::DataFormatR8
917             || format == GraphicsContext3D::DataFormatR16F
918             || format == GraphicsContext3D::DataFormatR32F
919             || format == GraphicsContext3D::DataFormatRA8
920             || format == GraphicsContext3D::DataFormatRA16F
921             || format == GraphicsContext3D::DataFormatRA32F
922             || format == GraphicsContext3D::DataFormatAR8;
923     }
924
925     // Check if the format is one of the formats from the ImageData or DOM elements.
926     // The formats from ImageData is always RGBA8.
927     // The formats from DOM elements vary with Graphics ports. It can only be RGBA8 or BGRA8 for non-CG port while a little more for CG port.
928     static ALWAYS_INLINE bool srcFormatComesFromDOMElementOrImageData(DataFormat SrcFormat)
929     {
930 #if USE(CG)
931 #if CPU(BIG_ENDIAN)
932     return SrcFormat == DataFormatRGBA8 || SrcFormat == DataFormatARGB8 || SrcFormat == DataFormatRGB8
933         || SrcFormat == DataFormatRA8 || SrcFormat == DataFormatAR8 || SrcFormat == DataFormatR8 || SrcFormat == DataFormatA8;
934 #else
935     // That LITTLE_ENDIAN case has more possible formats than BIG_ENDIAN case is because some decoded image data is actually big endian
936     // even on little endian architectures.
937     return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatABGR8 || SrcFormat == DataFormatBGR8
938         || SrcFormat == DataFormatRGBA8 || SrcFormat == DataFormatARGB8 || SrcFormat == DataFormatRGB8
939         || SrcFormat == DataFormatR8 || SrcFormat == DataFormatA8
940         || SrcFormat == DataFormatRA8 || SrcFormat == DataFormatAR8;
941 #endif
942 #else
943     return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
944 #endif
945     }
946
947     //----------------------------------------------------------------------
948     // Entry points for WebGL.
949     //
950
951     void activeTexture(GC3Denum texture);
952     void attachShader(Platform3DObject program, Platform3DObject shader);
953     void bindAttribLocation(Platform3DObject, GC3Duint index, const String& name);
954     void bindBuffer(GC3Denum target, Platform3DObject);
955     void bindFramebuffer(GC3Denum target, Platform3DObject);
956     void bindRenderbuffer(GC3Denum target, Platform3DObject);
957     void bindTexture(GC3Denum target, Platform3DObject);
958     void blendColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
959     void blendEquation(GC3Denum mode);
960     void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
961     void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
962     void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
963
964     void bufferData(GC3Denum target, GC3Dsizeiptr size, GC3Denum usage);
965     void bufferData(GC3Denum target, GC3Dsizeiptr size, const void* data, GC3Denum usage);
966     void bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr size, const void* data);
967
968     GC3Denum checkFramebufferStatus(GC3Denum target);
969     void clear(GC3Dbitfield mask);
970     void clearColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
971     void clearDepth(GC3Dclampf depth);
972     void clearStencil(GC3Dint s);
973     void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
974     void compileShader(Platform3DObject);
975
976     void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Dsizei imageSize, const void* data);
977     void compressedTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Dsizei imageSize, const void* data);
978     void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
979     void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
980     void cullFace(GC3Denum mode);
981     void depthFunc(GC3Denum func);
982     void depthMask(GC3Dboolean flag);
983     void depthRange(GC3Dclampf zNear, GC3Dclampf zFar);
984     void detachShader(Platform3DObject, Platform3DObject);
985     void disable(GC3Denum cap);
986     void disableVertexAttribArray(GC3Duint index);
987     void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count);
988     void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
989
990     void enable(GC3Denum cap);
991     void enableVertexAttribArray(GC3Duint index);
992     void finish();
993     void flush();
994     void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, Platform3DObject);
995     void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level);
996     void frontFace(GC3Denum mode);
997     void generateMipmap(GC3Denum target);
998
999     bool getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo&);
1000     bool getActiveAttribImpl(Platform3DObject program, GC3Duint index, ActiveInfo&);
1001     bool getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo&);
1002     bool getActiveUniformImpl(Platform3DObject program, GC3Duint index, ActiveInfo&);
1003     void getAttachedShaders(Platform3DObject program, GC3Dsizei maxCount, GC3Dsizei* count, Platform3DObject* shaders);
1004     GC3Dint getAttribLocation(Platform3DObject, const String& name);
1005     void getBooleanv(GC3Denum pname, GC3Dboolean* value);
1006     void getBufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
1007     Attributes getContextAttributes();
1008     GC3Denum getError();
1009     void getFloatv(GC3Denum pname, GC3Dfloat* value);
1010     void getFramebufferAttachmentParameteriv(GC3Denum target, GC3Denum attachment, GC3Denum pname, GC3Dint* value);
1011     void getIntegerv(GC3Denum pname, GC3Dint* value);
1012     void getInteger64v(GC3Denum pname, GC3Dint64* value);
1013     void getProgramiv(Platform3DObject program, GC3Denum pname, GC3Dint* value);
1014     void getNonBuiltInActiveSymbolCount(Platform3DObject program, GC3Denum pname, GC3Dint* value);
1015     String getProgramInfoLog(Platform3DObject);
1016     String getUnmangledInfoLog(Platform3DObject[2], GC3Dsizei, const String&);
1017     void getRenderbufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
1018     void getShaderiv(Platform3DObject, GC3Denum pname, GC3Dint* value);
1019     String getShaderInfoLog(Platform3DObject);
1020     void getShaderPrecisionFormat(GC3Denum shaderType, GC3Denum precisionType, GC3Dint* range, GC3Dint* precision);
1021     String getShaderSource(Platform3DObject);
1022     String getString(GC3Denum name);
1023     void getTexParameterfv(GC3Denum target, GC3Denum pname, GC3Dfloat* value);
1024     void getTexParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
1025     void getUniformfv(Platform3DObject program, GC3Dint location, GC3Dfloat* value);
1026     void getUniformiv(Platform3DObject program, GC3Dint location, GC3Dint* value);
1027     GC3Dint getUniformLocation(Platform3DObject, const String& name);
1028     void getVertexAttribfv(GC3Duint index, GC3Denum pname, GC3Dfloat* value);
1029     void getVertexAttribiv(GC3Duint index, GC3Denum pname, GC3Dint* value);
1030     GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
1031
1032     void hint(GC3Denum target, GC3Denum mode);
1033     GC3Dboolean isBuffer(Platform3DObject);
1034     GC3Dboolean isEnabled(GC3Denum cap);
1035     GC3Dboolean isFramebuffer(Platform3DObject);
1036     GC3Dboolean isProgram(Platform3DObject);
1037     GC3Dboolean isRenderbuffer(Platform3DObject);
1038     GC3Dboolean isShader(Platform3DObject);
1039     GC3Dboolean isTexture(Platform3DObject);
1040     void lineWidth(GC3Dfloat);
1041     void linkProgram(Platform3DObject);
1042     void pixelStorei(GC3Denum pname, GC3Dint param);
1043     void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
1044
1045     void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
1046
1047     void releaseShaderCompiler();
1048
1049     void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
1050     void sampleCoverage(GC3Dclampf value, GC3Dboolean invert);
1051     void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
1052     void shaderSource(Platform3DObject, const String& string);
1053     void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
1054     void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
1055     void stencilMask(GC3Duint mask);
1056     void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
1057     void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
1058     void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
1059
1060     bool texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
1061     void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
1062     void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
1063     void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels);
1064
1065     void uniform1f(GC3Dint location, GC3Dfloat x);
1066     void uniform1fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
1067     void uniform1i(GC3Dint location, GC3Dint x);
1068     void uniform1iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
1069     void uniform2f(GC3Dint location, GC3Dfloat x, GC3Dfloat y);
1070     void uniform2fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
1071     void uniform2i(GC3Dint location, GC3Dint x, GC3Dint y);
1072     void uniform2iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
1073     void uniform3f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
1074     void uniform3fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
1075     void uniform3i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z);
1076     void uniform3iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
1077     void uniform4f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
1078     void uniform4fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
1079     void uniform4i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w);
1080     void uniform4iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
1081     void uniformMatrix2fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
1082     void uniformMatrix3fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
1083     void uniformMatrix4fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
1084
1085     void useProgram(Platform3DObject);
1086     void validateProgram(Platform3DObject);
1087     bool checkVaryingsPacking(Platform3DObject vertexShader, Platform3DObject fragmentShader) const;
1088     bool precisionsMatch(Platform3DObject vertexShader, Platform3DObject fragmentShader) const;
1089
1090     void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
1091     void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values);
1092     void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
1093     void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values);
1094     void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
1095     void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values);
1096     void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
1097     void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values);
1098     void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
1099                              GC3Dsizei stride, GC3Dintptr offset);
1100
1101     void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
1102
1103     void reshape(int width, int height);
1104
1105     void drawArraysInstanced(GC3Denum mode, GC3Dint first, GC3Dsizei count, GC3Dsizei primcount);
1106     void drawElementsInstanced(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset, GC3Dsizei primcount);
1107     void vertexAttribDivisor(GC3Duint index, GC3Duint divisor);
1108
1109     // VertexArrayOject calls
1110     Platform3DObject createVertexArray();
1111     void deleteVertexArray(Platform3DObject);
1112     GC3Dboolean isVertexArray(Platform3DObject);
1113     void bindVertexArray(Platform3DObject);
1114
1115 #if PLATFORM(GTK) || PLATFORM(EFL) || USE(CAIRO)
1116     void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
1117                        int canvasWidth, int canvasHeight, PlatformContextCairo* context);
1118 #elif USE(CG)
1119     void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight, int canvasWidth, int canvasHeight, GraphicsContext&);
1120 #endif
1121
1122     void markContextChanged();
1123     void markLayerComposited();
1124     bool layerComposited() const;
1125     void forceContextLost();
1126     void recycleContext();
1127
1128     void paintRenderingResultsToCanvas(ImageBuffer*);
1129     PassRefPtr<ImageData> paintRenderingResultsToImageData();
1130     bool paintCompositedResultsToCanvas(ImageBuffer*);
1131
1132 #if PLATFORM(IOS)
1133     void endPaint();
1134 #endif
1135
1136     // Support for buffer creation and deletion
1137     Platform3DObject createBuffer();
1138     Platform3DObject createFramebuffer();
1139     Platform3DObject createProgram();
1140     Platform3DObject createRenderbuffer();
1141     Platform3DObject createShader(GC3Denum);
1142     Platform3DObject createTexture();
1143
1144     void deleteBuffer(Platform3DObject);
1145     void deleteFramebuffer(Platform3DObject);
1146     void deleteProgram(Platform3DObject);
1147     void deleteRenderbuffer(Platform3DObject);
1148     void deleteShader(Platform3DObject);
1149     void deleteTexture(Platform3DObject);
1150
1151     // Synthesizes an OpenGL error which will be returned from a
1152     // later call to getError. This is used to emulate OpenGL ES
1153     // 2.0 behavior on the desktop and to enforce additional error
1154     // checking mandated by WebGL.
1155     //
1156     // Per the behavior of glGetError, this stores at most one
1157     // instance of any given error, and returns them from calls to
1158     // getError in the order they were added.
1159     void synthesizeGLError(GC3Denum error);
1160
1161     // Read real OpenGL errors, and move them to the synthetic
1162     // error list. Return true if at least one error is moved.
1163     bool moveErrorsToSyntheticErrorList();
1164
1165     // Support for extensions. Returns a non-null object, though not
1166     // all methods it contains may necessarily be supported on the
1167     // current hardware. Must call Extensions3D::supports() to
1168     // determine this.
1169     Extensions3D* getExtensions();
1170
1171     IntSize getInternalFramebufferSize() const;
1172
1173     static unsigned getClearBitsByAttachmentType(GC3Denum);
1174     static unsigned getClearBitsByFormat(GC3Denum);
1175
1176     enum ChannelBits {
1177         ChannelRed = 1,
1178         ChannelGreen = 2,
1179         ChannelBlue = 4,
1180         ChannelAlpha = 8,
1181         ChannelDepth = 16,
1182         ChannelStencil = 32,
1183         ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
1184         ChannelRGBA = ChannelRGB | ChannelAlpha,
1185     };
1186
1187     static unsigned getChannelBitsByFormat(GC3Denum);
1188
1189     // Possible alpha operations that may need to occur during
1190     // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
1191     // be removed.
1192     enum AlphaOp {
1193         AlphaDoNothing = 0,
1194         AlphaDoPremultiply = 1,
1195         AlphaDoUnmultiply = 2
1196     };
1197
1198     enum ImageHtmlDomSource {
1199         HtmlDomImage = 0,
1200         HtmlDomCanvas = 1,
1201         HtmlDomVideo = 2,
1202         HtmlDomNone = 3
1203     };
1204
1205     // Packs the contents of the given Image which is passed in |pixels| into the passed Vector
1206     // according to the given format and type, and obeying the flipY and AlphaOp flags.
1207     // Returns true upon success.
1208     static bool packImageData(Image*, const void* pixels, GC3Denum format, GC3Denum type, bool flipY, AlphaOp, DataFormat sourceFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, Vector<uint8_t>& data);
1209
1210     class ImageExtractor {
1211     public:
1212         ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
1213
1214         // Each platform must provide an implementation of this method to deallocate or release resources
1215         // associated with the image if needed.
1216         ~ImageExtractor();
1217
1218         bool extractSucceeded() { return m_extractSucceeded; }
1219         const void* imagePixelData() { return m_imagePixelData; }
1220         unsigned imageWidth() { return m_imageWidth; }
1221         unsigned imageHeight() { return m_imageHeight; }
1222         DataFormat imageSourceFormat() { return m_imageSourceFormat; }
1223         AlphaOp imageAlphaOp() { return m_alphaOp; }
1224         unsigned imageSourceUnpackAlignment() { return m_imageSourceUnpackAlignment; }
1225         ImageHtmlDomSource imageHtmlDomSource() { return m_imageHtmlDomSource; }
1226     private:
1227         // Each platform must provide an implementation of this method.
1228         // Extracts the image and keeps track of its status, such as width, height, Source Alignment, format and AlphaOp etc,
1229         // needs to lock the resources or relevant data if needed and returns true upon success
1230         bool extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
1231
1232 #if USE(CAIRO)
1233         ImageSource* m_decoder;
1234         RefPtr<cairo_surface_t> m_imageSurface;
1235 #elif USE(CG)
1236         RetainPtr<CGImageRef> m_cgImage;
1237         RetainPtr<CGImageRef> m_decodedImage;
1238         RetainPtr<CFDataRef> m_pixelData;
1239         std::unique_ptr<uint8_t[]> m_formalizedRGBA8Data;
1240 #endif
1241         Image* m_image;
1242         ImageHtmlDomSource m_imageHtmlDomSource;
1243         bool m_extractSucceeded;
1244         const void* m_imagePixelData;
1245         unsigned m_imageWidth;
1246         unsigned m_imageHeight;
1247         DataFormat m_imageSourceFormat;
1248         AlphaOp m_alphaOp;
1249         unsigned m_imageSourceUnpackAlignment;
1250     };
1251
1252 private:
1253     GraphicsContext3D(Attributes, HostWindow*, RenderStyle = RenderOffscreen);
1254     static int GPUCheckCounter;
1255
1256     // Helper for packImageData/extractImageData/extractTextureData which implement packing of pixel
1257     // data into the specified OpenGL destination format and type.
1258     // A sourceUnpackAlignment of zero indicates that the source
1259     // data is tightly packed. Non-zero values may take a slow path.
1260     // Destination data will have no gaps between rows.
1261     static bool packPixels(const uint8_t* sourceData, DataFormat sourceDataFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, unsigned destinationFormat, unsigned destinationType, AlphaOp, void* destinationData, bool flipY);
1262
1263     // Take into account the user's requested context creation attributes,
1264     // in particular stencil and antialias, and determine which could or
1265     // could not be honored based on the capabilities of the OpenGL
1266     // implementation.
1267     void validateDepthStencil(const char* packedDepthStencilExtension);
1268     void validateAttributes();
1269     
1270     // Call to make during draw calls to check on the GPU's status.
1271     void checkGPUStatusIfNecessary();
1272
1273     // Read rendering results into a pixel array with the same format as the
1274     // backbuffer.
1275     void readRenderingResults(unsigned char* pixels, int pixelsSize);
1276     void readPixelsAndConvertToBGRAIfNecessary(int x, int y, int width, int height, unsigned char* pixels);
1277
1278 #if PLATFORM(IOS)
1279     void setRenderbufferStorageFromDrawable(GC3Dsizei width, GC3Dsizei height);
1280 #endif
1281
1282     bool reshapeFBOs(const IntSize&);
1283     void resolveMultisamplingIfNecessary(const IntRect& = IntRect());
1284     void attachDepthAndStencilBufferIfNeeded(GLuint internalDepthStencilFormat, int width, int height);
1285 #if PLATFORM(EFL) && USE(GRAPHICS_SURFACE)
1286     void createGraphicsSurfaces(const IntSize&);
1287 #endif
1288
1289     int m_currentWidth, m_currentHeight;
1290     bool isResourceSafe();
1291
1292 #if PLATFORM(COCOA)
1293     RetainPtr<WebGLLayer> m_webGLLayer;
1294     PlatformGraphicsContext3D m_contextObj;
1295 #endif
1296
1297 #if PLATFORM(WIN) && USE(CA)
1298     RefPtr<PlatformCALayer> m_webGLLayer;
1299 #endif
1300
1301     struct SymbolInfo {
1302         SymbolInfo()
1303             : type(0)
1304             , size(0)
1305             , precision(GL_NONE) // Invalid precision.
1306             , staticUse(0)
1307         {
1308         }
1309
1310         SymbolInfo(GC3Denum type, int size, const String& mappedName, sh::GLenum precision, int staticUse)
1311             : type(type)
1312             , size(size)
1313             , mappedName(mappedName)
1314             , precision(precision)
1315             , staticUse(staticUse)
1316         {
1317         }
1318
1319         bool operator==(SymbolInfo& other) const
1320         {
1321             return type == other.type && size == other.size && mappedName == other.mappedName;
1322         }
1323
1324         GC3Denum type;
1325         int size;
1326         String mappedName;
1327         sh::GLenum precision;
1328         int staticUse;
1329     };
1330
1331     typedef HashMap<String, SymbolInfo> ShaderSymbolMap;
1332
1333     struct ShaderSourceEntry {
1334         GC3Denum type;
1335         String source;
1336         String translatedSource;
1337         String log;
1338         bool isValid;
1339         ShaderSymbolMap attributeMap;
1340         ShaderSymbolMap uniformMap;
1341         ShaderSymbolMap varyingMap;
1342         ShaderSourceEntry()
1343             : type(VERTEX_SHADER)
1344             , isValid(false)
1345         {
1346         }
1347         
1348         ShaderSymbolMap& symbolMap(enum ANGLEShaderSymbolType symbolType)
1349         {
1350             ASSERT(symbolType == SHADER_SYMBOL_TYPE_ATTRIBUTE || symbolType == SHADER_SYMBOL_TYPE_UNIFORM || symbolType == SHADER_SYMBOL_TYPE_VARYING);
1351             if (symbolType == SHADER_SYMBOL_TYPE_ATTRIBUTE)
1352                 return attributeMap;
1353             if (symbolType == SHADER_SYMBOL_TYPE_VARYING)
1354                 return varyingMap;
1355             return uniformMap;
1356         }
1357     };
1358
1359     typedef HashMap<Platform3DObject, ShaderSourceEntry> ShaderSourceMap;
1360     ShaderSourceMap m_shaderSourceMap;
1361
1362     struct ActiveShaderSymbolCounts {
1363         Vector<GC3Dint> filteredToActualAttributeIndexMap;
1364         Vector<GC3Dint> filteredToActualUniformIndexMap;
1365
1366         ActiveShaderSymbolCounts()
1367         {
1368         }
1369
1370         GC3Dint countForType(GC3Denum activeType)
1371         {
1372             ASSERT(activeType == ACTIVE_ATTRIBUTES || activeType == ACTIVE_UNIFORMS);
1373             if (activeType == ACTIVE_ATTRIBUTES)
1374                 return filteredToActualAttributeIndexMap.size();
1375
1376             return filteredToActualUniformIndexMap.size();
1377         }
1378     };
1379     typedef HashMap<Platform3DObject, ActiveShaderSymbolCounts> ShaderProgramSymbolCountMap;
1380     ShaderProgramSymbolCountMap m_shaderProgramSymbolCountMap;
1381
1382     typedef HashMap<String, String> HashedSymbolMap;
1383     HashedSymbolMap m_possiblyUnusedAttributeMap;
1384
1385     String mappedSymbolName(Platform3DObject program, ANGLEShaderSymbolType, const String& name);
1386     String mappedSymbolName(Platform3DObject shaders[2], size_t count, const String& name);
1387     String originalSymbolName(Platform3DObject program, ANGLEShaderSymbolType, const String& name);
1388
1389     ANGLEWebKitBridge m_compiler;
1390
1391     std::unique_ptr<ShaderNameHash> nameHashMapForShaders;
1392
1393 #if ((PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(WIN)) && USE(OPENGL_ES_2))
1394     friend class Extensions3DOpenGLES;
1395     std::unique_ptr<Extensions3DOpenGLES> m_extensions;
1396 #else
1397     friend class Extensions3DOpenGL;
1398     std::unique_ptr<Extensions3DOpenGL> m_extensions;
1399 #endif
1400     friend class Extensions3DOpenGLCommon;
1401
1402     Attributes m_attrs;
1403     RenderStyle m_renderStyle;
1404     Vector<Vector<float>> m_vertexArray;
1405
1406     GC3Duint m_texture;
1407     GC3Duint m_compositorTexture;
1408     GC3Duint m_fbo;
1409 #if USE(COORDINATED_GRAPHICS_THREADED)
1410     GC3Duint m_intermediateTexture;
1411 #endif
1412
1413     GC3Duint m_depthBuffer;
1414     GC3Duint m_stencilBuffer;
1415     GC3Duint m_depthStencilBuffer;
1416
1417     bool m_layerComposited;
1418     GC3Duint m_internalColorFormat;
1419
1420     struct GraphicsContext3DState {
1421         GraphicsContext3DState()
1422             : boundFBO(0)
1423             , activeTexture(GraphicsContext3D::TEXTURE0)
1424             , boundTexture0(0)
1425         { }
1426
1427         GC3Duint boundFBO;
1428         GC3Denum activeTexture;
1429         GC3Duint boundTexture0;
1430     };
1431
1432     GraphicsContext3DState m_state;
1433
1434     // For multisampling
1435     GC3Duint m_multisampleFBO;
1436     GC3Duint m_multisampleDepthStencilBuffer;
1437     GC3Duint m_multisampleColorBuffer;
1438
1439     // Errors raised by synthesizeGLError().
1440     ListHashSet<GC3Denum> m_syntheticErrors;
1441
1442     friend class GraphicsContext3DPrivate;
1443     std::unique_ptr<GraphicsContext3DPrivate> m_private;
1444     
1445     WebGLRenderingContextBase* m_webglContext;
1446 };
1447
1448 } // namespace WebCore
1449
1450 #endif // GraphicsContext3D_h