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