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