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