2010-08-16 Paul Sawaya <psawaya@apple.com>
[WebKit-https.git] / 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 "ANGLEWebKitBridge.h"
30 #include "GraphicsLayer.h"
31 #include "PlatformString.h"
32
33 #include <wtf/HashMap.h>
34 #include <wtf/ListHashSet.h>
35 #include <wtf/Noncopyable.h>
36 #include <wtf/PassOwnPtr.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 #endif
42
43 #if PLATFORM(MAC)
44 #include <OpenGL/OpenGL.h>
45 #include <wtf/RetainPtr.h>
46
47 typedef CGLContextObj PlatformGraphicsContext3D;
48 const PlatformGraphicsContext3D NullPlatformGraphicsContext3D = 0;
49 typedef GLuint Platform3DObject;
50 const Platform3DObject NullPlatform3DObject = 0;
51
52 #ifdef __OBJC__
53 @class CALayer;
54 @class WebGLLayer;
55 #else
56 typedef void* CALayer;
57 typedef void* WebGLLayer;
58 #endif
59 #elif PLATFORM(QT)
60 class QPainter;
61 class QRect;
62 typedef void* PlatformGraphicsContext3D;
63 const PlatformGraphicsContext3D NullPlatformGraphicsContext3D = 0;
64 typedef int Platform3DObject;
65 const Platform3DObject NullPlatform3DObject = 0;
66 #else
67 typedef void* PlatformGraphicsContext3D;
68 const PlatformGraphicsContext3D NullPlatformGraphicsContext3D = 0;
69 typedef int Platform3DObject;
70 const Platform3DObject NullPlatform3DObject = 0;
71 #endif
72
73 #if PLATFORM(CG)
74 #include <CoreGraphics/CGContext.h>
75 #endif
76
77 namespace WebCore {
78 class ArrayBuffer;
79 class ArrayBufferView;
80 class CanvasRenderingContext;
81 class Float32Array;
82 class HostWindow;
83 class Image;
84 class ImageData;
85 class Int32Array;
86 class Uint8Array;
87 class WebGLActiveInfo;
88
89 struct ActiveInfo {
90     String name;
91     unsigned type;
92     int size;
93 };
94
95 // FIXME: ideally this would be used on all platforms.
96 #if PLATFORM(CHROMIUM) || PLATFORM(QT)
97 class GraphicsContext3DInternal;
98 #endif
99
100 class GraphicsContext3D : public Noncopyable {
101 public:
102     enum WebGLEnumType {
103         DEPTH_BUFFER_BIT = 0x00000100,
104         STENCIL_BUFFER_BIT = 0x00000400,
105         COLOR_BUFFER_BIT = 0x00004000,
106         POINTS = 0x0000,
107         LINES = 0x0001,
108         LINE_LOOP = 0x0002,
109         LINE_STRIP = 0x0003,
110         TRIANGLES = 0x0004,
111         TRIANGLE_STRIP = 0x0005,
112         TRIANGLE_FAN = 0x0006,
113         ZERO = 0,
114         ONE = 1,
115         SRC_COLOR = 0x0300,
116         ONE_MINUS_SRC_COLOR = 0x0301,
117         SRC_ALPHA = 0x0302,
118         ONE_MINUS_SRC_ALPHA = 0x0303,
119         DST_ALPHA = 0x0304,
120         ONE_MINUS_DST_ALPHA = 0x0305,
121         DST_COLOR = 0x0306,
122         ONE_MINUS_DST_COLOR = 0x0307,
123         SRC_ALPHA_SATURATE = 0x0308,
124         FUNC_ADD = 0x8006,
125         BLEND_EQUATION = 0x8009,
126         BLEND_EQUATION_RGB = 0x8009,
127         BLEND_EQUATION_ALPHA = 0x883D,
128         FUNC_SUBTRACT = 0x800A,
129         FUNC_REVERSE_SUBTRACT = 0x800B,
130         BLEND_DST_RGB = 0x80C8,
131         BLEND_SRC_RGB = 0x80C9,
132         BLEND_DST_ALPHA = 0x80CA,
133         BLEND_SRC_ALPHA = 0x80CB,
134         CONSTANT_COLOR = 0x8001,
135         ONE_MINUS_CONSTANT_COLOR = 0x8002,
136         CONSTANT_ALPHA = 0x8003,
137         ONE_MINUS_CONSTANT_ALPHA = 0x8004,
138         BLEND_COLOR = 0x8005,
139         ARRAY_BUFFER = 0x8892,
140         ELEMENT_ARRAY_BUFFER = 0x8893,
141         ARRAY_BUFFER_BINDING = 0x8894,
142         ELEMENT_ARRAY_BUFFER_BINDING = 0x8895,
143         STREAM_DRAW = 0x88E0,
144         STATIC_DRAW = 0x88E4,
145         DYNAMIC_DRAW = 0x88E8,
146         BUFFER_SIZE = 0x8764,
147         BUFFER_USAGE = 0x8765,
148         CURRENT_VERTEX_ATTRIB = 0x8626,
149         FRONT = 0x0404,
150         BACK = 0x0405,
151         FRONT_AND_BACK = 0x0408,
152         TEXTURE_2D = 0x0DE1,
153         CULL_FACE = 0x0B44,
154         BLEND = 0x0BE2,
155         DITHER = 0x0BD0,
156         STENCIL_TEST = 0x0B90,
157         DEPTH_TEST = 0x0B71,
158         SCISSOR_TEST = 0x0C11,
159         POLYGON_OFFSET_FILL = 0x8037,
160         SAMPLE_ALPHA_TO_COVERAGE = 0x809E,
161         SAMPLE_COVERAGE = 0x80A0,
162         NO_ERROR = 0,
163         INVALID_ENUM = 0x0500,
164         INVALID_VALUE = 0x0501,
165         INVALID_OPERATION = 0x0502,
166         OUT_OF_MEMORY = 0x0505,
167         CW = 0x0900,
168         CCW = 0x0901,
169         LINE_WIDTH = 0x0B21,
170         ALIASED_POINT_SIZE_RANGE = 0x846D,
171         ALIASED_LINE_WIDTH_RANGE = 0x846E,
172         CULL_FACE_MODE = 0x0B45,
173         FRONT_FACE = 0x0B46,
174         DEPTH_RANGE = 0x0B70,
175         DEPTH_WRITEMASK = 0x0B72,
176         DEPTH_CLEAR_VALUE = 0x0B73,
177         DEPTH_FUNC = 0x0B74,
178         STENCIL_CLEAR_VALUE = 0x0B91,
179         STENCIL_FUNC = 0x0B92,
180         STENCIL_FAIL = 0x0B94,
181         STENCIL_PASS_DEPTH_FAIL = 0x0B95,
182         STENCIL_PASS_DEPTH_PASS = 0x0B96,
183         STENCIL_REF = 0x0B97,
184         STENCIL_VALUE_MASK = 0x0B93,
185         STENCIL_WRITEMASK = 0x0B98,
186         STENCIL_BACK_FUNC = 0x8800,
187         STENCIL_BACK_FAIL = 0x8801,
188         STENCIL_BACK_PASS_DEPTH_FAIL = 0x8802,
189         STENCIL_BACK_PASS_DEPTH_PASS = 0x8803,
190         STENCIL_BACK_REF = 0x8CA3,
191         STENCIL_BACK_VALUE_MASK = 0x8CA4,
192         STENCIL_BACK_WRITEMASK = 0x8CA5,
193         VIEWPORT = 0x0BA2,
194         SCISSOR_BOX = 0x0C10,
195         COLOR_CLEAR_VALUE = 0x0C22,
196         COLOR_WRITEMASK = 0x0C23,
197         UNPACK_ALIGNMENT = 0x0CF5,
198         PACK_ALIGNMENT = 0x0D05,
199         MAX_TEXTURE_SIZE = 0x0D33,
200         MAX_VIEWPORT_DIMS = 0x0D3A,
201         SUBPIXEL_BITS = 0x0D50,
202         RED_BITS = 0x0D52,
203         GREEN_BITS = 0x0D53,
204         BLUE_BITS = 0x0D54,
205         ALPHA_BITS = 0x0D55,
206         DEPTH_BITS = 0x0D56,
207         STENCIL_BITS = 0x0D57,
208         POLYGON_OFFSET_UNITS = 0x2A00,
209         POLYGON_OFFSET_FACTOR = 0x8038,
210         TEXTURE_BINDING_2D = 0x8069,
211         SAMPLE_BUFFERS = 0x80A8,
212         SAMPLES = 0x80A9,
213         SAMPLE_COVERAGE_VALUE = 0x80AA,
214         SAMPLE_COVERAGE_INVERT = 0x80AB,
215         NUM_COMPRESSED_TEXTURE_FORMATS = 0x86A2,
216         COMPRESSED_TEXTURE_FORMATS = 0x86A3,
217         DONT_CARE = 0x1100,
218         FASTEST = 0x1101,
219         NICEST = 0x1102,
220         GENERATE_MIPMAP_HINT = 0x8192,
221         BYTE = 0x1400,
222         UNSIGNED_BYTE = 0x1401,
223         SHORT = 0x1402,
224         UNSIGNED_SHORT = 0x1403,
225         INT = 0x1404,
226         UNSIGNED_INT = 0x1405,
227         FLOAT = 0x1406,
228         FIXED = 0x140C,
229         DEPTH_COMPONENT = 0x1902,
230         ALPHA = 0x1906,
231         RGB = 0x1907,
232         RGBA = 0x1908,
233         LUMINANCE = 0x1909,
234         LUMINANCE_ALPHA = 0x190A,
235         UNSIGNED_SHORT_4_4_4_4 = 0x8033,
236         UNSIGNED_SHORT_5_5_5_1 = 0x8034,
237         UNSIGNED_SHORT_5_6_5 = 0x8363,
238         FRAGMENT_SHADER = 0x8B30,
239         VERTEX_SHADER = 0x8B31,
240         MAX_VERTEX_ATTRIBS = 0x8869,
241         MAX_VERTEX_UNIFORM_VECTORS = 0x8DFB,
242         MAX_VARYING_VECTORS = 0x8DFC,
243         MAX_COMBINED_TEXTURE_IMAGE_UNITS = 0x8B4D,
244         MAX_VERTEX_TEXTURE_IMAGE_UNITS = 0x8B4C,
245         MAX_TEXTURE_IMAGE_UNITS = 0x8872,
246         MAX_FRAGMENT_UNIFORM_VECTORS = 0x8DFD,
247         SHADER_TYPE = 0x8B4F,
248         DELETE_STATUS = 0x8B80,
249         LINK_STATUS = 0x8B82,
250         VALIDATE_STATUS = 0x8B83,
251         ATTACHED_SHADERS = 0x8B85,
252         ACTIVE_UNIFORMS = 0x8B86,
253         ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87,
254         ACTIVE_ATTRIBUTES = 0x8B89,
255         ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A,
256         SHADING_LANGUAGE_VERSION = 0x8B8C,
257         CURRENT_PROGRAM = 0x8B8D,
258         NEVER = 0x0200,
259         LESS = 0x0201,
260         EQUAL = 0x0202,
261         LEQUAL = 0x0203,
262         GREATER = 0x0204,
263         NOTEQUAL = 0x0205,
264         GEQUAL = 0x0206,
265         ALWAYS = 0x0207,
266         KEEP = 0x1E00,
267         REPLACE = 0x1E01,
268         INCR = 0x1E02,
269         DECR = 0x1E03,
270         INVERT = 0x150A,
271         INCR_WRAP = 0x8507,
272         DECR_WRAP = 0x8508,
273         VENDOR = 0x1F00,
274         RENDERER = 0x1F01,
275         VERSION = 0x1F02,
276         EXTENSIONS = 0x1F03,
277         NEAREST = 0x2600,
278         LINEAR = 0x2601,
279         NEAREST_MIPMAP_NEAREST = 0x2700,
280         LINEAR_MIPMAP_NEAREST = 0x2701,
281         NEAREST_MIPMAP_LINEAR = 0x2702,
282         LINEAR_MIPMAP_LINEAR = 0x2703,
283         TEXTURE_MAG_FILTER = 0x2800,
284         TEXTURE_MIN_FILTER = 0x2801,
285         TEXTURE_WRAP_S = 0x2802,
286         TEXTURE_WRAP_T = 0x2803,
287         TEXTURE = 0x1702,
288         TEXTURE_CUBE_MAP = 0x8513,
289         TEXTURE_BINDING_CUBE_MAP = 0x8514,
290         TEXTURE_CUBE_MAP_POSITIVE_X = 0x8515,
291         TEXTURE_CUBE_MAP_NEGATIVE_X = 0x8516,
292         TEXTURE_CUBE_MAP_POSITIVE_Y = 0x8517,
293         TEXTURE_CUBE_MAP_NEGATIVE_Y = 0x8518,
294         TEXTURE_CUBE_MAP_POSITIVE_Z = 0x8519,
295         TEXTURE_CUBE_MAP_NEGATIVE_Z = 0x851A,
296         MAX_CUBE_MAP_TEXTURE_SIZE = 0x851C,
297         TEXTURE0 = 0x84C0,
298         TEXTURE1 = 0x84C1,
299         TEXTURE2 = 0x84C2,
300         TEXTURE3 = 0x84C3,
301         TEXTURE4 = 0x84C4,
302         TEXTURE5 = 0x84C5,
303         TEXTURE6 = 0x84C6,
304         TEXTURE7 = 0x84C7,
305         TEXTURE8 = 0x84C8,
306         TEXTURE9 = 0x84C9,
307         TEXTURE10 = 0x84CA,
308         TEXTURE11 = 0x84CB,
309         TEXTURE12 = 0x84CC,
310         TEXTURE13 = 0x84CD,
311         TEXTURE14 = 0x84CE,
312         TEXTURE15 = 0x84CF,
313         TEXTURE16 = 0x84D0,
314         TEXTURE17 = 0x84D1,
315         TEXTURE18 = 0x84D2,
316         TEXTURE19 = 0x84D3,
317         TEXTURE20 = 0x84D4,
318         TEXTURE21 = 0x84D5,
319         TEXTURE22 = 0x84D6,
320         TEXTURE23 = 0x84D7,
321         TEXTURE24 = 0x84D8,
322         TEXTURE25 = 0x84D9,
323         TEXTURE26 = 0x84DA,
324         TEXTURE27 = 0x84DB,
325         TEXTURE28 = 0x84DC,
326         TEXTURE29 = 0x84DD,
327         TEXTURE30 = 0x84DE,
328         TEXTURE31 = 0x84DF,
329         ACTIVE_TEXTURE = 0x84E0,
330         REPEAT = 0x2901,
331         CLAMP_TO_EDGE = 0x812F,
332         MIRRORED_REPEAT = 0x8370,
333         FLOAT_VEC2 = 0x8B50,
334         FLOAT_VEC3 = 0x8B51,
335         FLOAT_VEC4 = 0x8B52,
336         INT_VEC2 = 0x8B53,
337         INT_VEC3 = 0x8B54,
338         INT_VEC4 = 0x8B55,
339         BOOL = 0x8B56,
340         BOOL_VEC2 = 0x8B57,
341         BOOL_VEC3 = 0x8B58,
342         BOOL_VEC4 = 0x8B59,
343         FLOAT_MAT2 = 0x8B5A,
344         FLOAT_MAT3 = 0x8B5B,
345         FLOAT_MAT4 = 0x8B5C,
346         SAMPLER_2D = 0x8B5E,
347         SAMPLER_CUBE = 0x8B60,
348         VERTEX_ATTRIB_ARRAY_ENABLED = 0x8622,
349         VERTEX_ATTRIB_ARRAY_SIZE = 0x8623,
350         VERTEX_ATTRIB_ARRAY_STRIDE = 0x8624,
351         VERTEX_ATTRIB_ARRAY_TYPE = 0x8625,
352         VERTEX_ATTRIB_ARRAY_NORMALIZED = 0x886A,
353         VERTEX_ATTRIB_ARRAY_POINTER = 0x8645,
354         VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = 0x889F,
355         IMPLEMENTATION_COLOR_READ_TYPE = 0x8B9A,
356         IMPLEMENTATION_COLOR_READ_FORMAT = 0x8B9B,
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
410         BGRA_EXT = 0x80E1
411     };
412
413     // Context creation attributes.
414     struct Attributes {
415         Attributes()
416             : alpha(true)
417             , depth(true)
418             , stencil(false)
419             , antialias(true)
420             , premultipliedAlpha(true)
421         {
422         }
423
424         bool alpha;
425         bool depth;
426         bool stencil;
427         bool antialias;
428         bool premultipliedAlpha;
429     };
430
431     static PassOwnPtr<GraphicsContext3D> create(Attributes attrs, HostWindow* hostWindow);
432     virtual ~GraphicsContext3D();
433
434 #if PLATFORM(MAC)
435     PlatformGraphicsContext3D platformGraphicsContext3D() const { return m_contextObj; }
436     Platform3DObject platformTexture() const { return m_texture; }
437     CALayer* platformLayer() const { return static_cast<CALayer*>(m_webGLLayer.get()); }
438 #elif PLATFORM(CHROMIUM)
439     PlatformGraphicsContext3D platformGraphicsContext3D() const;
440     Platform3DObject platformTexture() const;
441 #if USE(ACCELERATED_COMPOSITING)
442     PlatformLayer* platformLayer() const;
443 #endif
444 #elif PLATFORM(QT)
445     PlatformGraphicsContext3D platformGraphicsContext3D();
446     Platform3DObject platformTexture() const;
447 #if USE(ACCELERATED_COMPOSITING)
448     PlatformLayer* platformLayer() const { return 0; }
449 #endif
450 #else
451     PlatformGraphicsContext3D platformGraphicsContext3D() const { return NullPlatformGraphicsContext3D; }
452     Platform3DObject platformTexture() const { return NullPlatform3DObject; }
453 #if USE(ACCELERATED_COMPOSITING)
454     PlatformLayer* platformLayer() const { return 0; }
455 #endif
456 #endif
457     void makeContextCurrent();
458
459 #if PLATFORM(MAC) || PLATFORM(CHROMIUM)
460     // With multisampling on, blit from multisampleFBO to regular FBO.
461     void prepareTexture();
462 #endif
463
464     // Helper to return the size in bytes of OpenGL data types
465     // like GL_FLOAT, GL_INT, etc.
466     int sizeInBytes(int type);
467
468     bool isGLES2Compliant() const;
469
470     //----------------------------------------------------------------------
471     // Helpers for texture uploading and pixel readback.
472     //
473
474     // Computes the components per pixel and bytes per component
475     // for the given format and type combination. Returns false if
476     // either was an invalid enum.
477     bool computeFormatAndTypeParameters(unsigned int format,
478                                         unsigned int type,
479                                         unsigned long* componentsPerPixel,
480                                         unsigned long* bytesPerComponent);
481
482     // Extracts the contents of the given Image into the passed Vector,
483     // packing the pixel data according to the given format and type,
484     // and obeying the flipY and premultiplyAlpha flags. Returns true
485     // upon success.
486     bool extractImageData(Image* image,
487                           unsigned int format,
488                           unsigned int type,
489                           bool flipY,
490                           bool premultiplyAlpha,
491                           Vector<uint8_t>& data);
492
493     // Extracts the contents of the given ImageData into the passed Vector,
494     // packing the pixel data according to the given format and type,
495     // and obeying the flipY and premultiplyAlpha flags. Returns true
496     // upon success.
497     bool extractImageData(ImageData*,
498                           unsigned int format,
499                           unsigned int type,
500                           bool flipY,
501                           bool premultiplyAlpha,
502                           Vector<uint8_t>& data);
503
504     // Helper function which extracts the user-supplied texture
505     // data, applying the flipY and premultiplyAlpha parameters.
506     // If the data is not tightly packed according to the passed
507     // unpackAlignment, the output data will be tightly packed.
508     // Returns true if successful, false if any error occurred.
509     bool extractTextureData(unsigned int width, unsigned int height,
510                             unsigned int format, unsigned int type,
511                             unsigned int unpackAlignment,
512                             bool flipY, bool premultiplyAlpha,
513                             ArrayBufferView* pixels,
514                             Vector<uint8_t>& data);
515
516     // Flips the given image data vertically, in-place.
517     void flipVertically(void* imageData,
518                         unsigned int width,
519                         unsigned int height,
520                         unsigned int bytesPerPixel,
521                         unsigned int unpackAlignment);
522
523     // Attempt to enumerate all possible native image formats to
524     // reduce the amount of temporary allocations during texture
525     // uploading. This enum must be public because it is accessed
526     // by non-member functions.
527     enum SourceDataFormat {
528         kSourceFormatRGBA8,
529         kSourceFormatRGB8,
530         kSourceFormatBGRA8,
531         kSourceFormatRGBA5551,
532         kSourceFormatRGBA4444,
533         kSourceFormatRGB565,
534         kSourceFormatR8,
535         kSourceFormatRA8,
536         kSourceFormatA8
537     };
538
539     //----------------------------------------------------------------------
540     // Entry points for WebGL.
541     //
542
543     void activeTexture(unsigned long texture);
544     void attachShader(Platform3DObject program, Platform3DObject shader);
545     void bindAttribLocation(Platform3DObject, unsigned long index, const String& name);
546     void bindBuffer(unsigned long target, Platform3DObject);
547     void bindFramebuffer(unsigned long target, Platform3DObject);
548     void bindRenderbuffer(unsigned long target, Platform3DObject);
549     void bindTexture(unsigned long target, Platform3DObject texture);
550     void blendColor(double red, double green, double blue, double alpha);
551     void blendEquation(unsigned long mode);
552     void blendEquationSeparate(unsigned long modeRGB, unsigned long modeAlpha);
553     void blendFunc(unsigned long sfactor, unsigned long dfactor);
554     void blendFuncSeparate(unsigned long srcRGB, unsigned long dstRGB, unsigned long srcAlpha, unsigned long dstAlpha);
555
556     void bufferData(unsigned long target, int size, unsigned long usage);
557     void bufferData(unsigned long target, ArrayBuffer* data, unsigned long usage);
558     void bufferData(unsigned long target, ArrayBufferView* data, unsigned long usage);
559     void bufferSubData(unsigned long target, long offset, ArrayBuffer* data);
560     void bufferSubData(unsigned long target, long offset, ArrayBufferView* data);
561
562     unsigned long checkFramebufferStatus(unsigned long target);
563     void clear(unsigned long mask);
564     void clearColor(double red, double green, double blue, double alpha);
565     void clearDepth(double depth);
566     void clearStencil(long s);
567     void colorMask(bool red, bool green, bool blue, bool alpha);
568     void compileShader(Platform3DObject);
569
570     // void compressedTexImage2D(unsigned long target, long level, unsigned long internalformat, unsigned long width, unsigned long height, long border, unsigned long imageSize, const void* data);
571     // void compressedTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, unsigned long width, unsigned long height, unsigned long format, unsigned long imageSize, const void* data);
572
573     void copyTexImage2D(unsigned long target, long level, unsigned long internalformat, long x, long y, unsigned long width, unsigned long height, long border);
574     void copyTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, long x, long y, unsigned long width, unsigned long height);
575     void cullFace(unsigned long mode);
576     void depthFunc(unsigned long func);
577     void depthMask(bool flag);
578     void depthRange(double zNear, double zFar);
579     void detachShader(Platform3DObject, Platform3DObject);
580     void disable(unsigned long cap);
581     void disableVertexAttribArray(unsigned long index);
582     void drawArrays(unsigned long mode, long first, long count);
583     void drawElements(unsigned long mode, unsigned long count, unsigned long type, long offset);
584
585     void enable(unsigned long cap);
586     void enableVertexAttribArray(unsigned long index);
587     void finish();
588     void flush();
589     void framebufferRenderbuffer(unsigned long target, unsigned long attachment, unsigned long renderbuffertarget, Platform3DObject);
590     void framebufferTexture2D(unsigned long target, unsigned long attachment, unsigned long textarget, Platform3DObject, long level);
591     void frontFace(unsigned long mode);
592     void generateMipmap(unsigned long target);
593
594     bool getActiveAttrib(Platform3DObject program, unsigned long index, ActiveInfo&);
595     bool getActiveUniform(Platform3DObject program, unsigned long index, ActiveInfo&);
596
597     void getAttachedShaders(Platform3DObject program, int maxCount, int* count, unsigned int* shaders);
598
599     int getAttribLocation(Platform3DObject, const String& name);
600
601     void getBooleanv(unsigned long pname, unsigned char* value);
602
603     void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
604
605     Attributes getContextAttributes();
606
607     unsigned long getError();
608
609     void getFloatv(unsigned long pname, float* value);
610
611     void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
612
613     void getIntegerv(unsigned long pname, int* value);
614
615     void getProgramiv(Platform3DObject program, unsigned long pname, int* value);
616
617     String getProgramInfoLog(Platform3DObject);
618
619     void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
620
621     void getShaderiv(Platform3DObject, unsigned long pname, int* value);
622
623     String getShaderInfoLog(Platform3DObject);
624
625     // TBD
626     // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
627
628     String getShaderSource(Platform3DObject);
629     String getString(unsigned long name);
630
631     void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
632     void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
633
634     void getUniformfv(Platform3DObject program, long location, float* value);
635     void getUniformiv(Platform3DObject program, long location, int* value);
636
637     long getUniformLocation(Platform3DObject, const String& name);
638
639     void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
640     void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
641
642     long getVertexAttribOffset(unsigned long index, unsigned long pname);
643
644     void hint(unsigned long target, unsigned long mode);
645     bool isBuffer(Platform3DObject);
646     bool isEnabled(unsigned long cap);
647     bool isFramebuffer(Platform3DObject);
648     bool isProgram(Platform3DObject);
649     bool isRenderbuffer(Platform3DObject);
650     bool isShader(Platform3DObject);
651     bool isTexture(Platform3DObject);
652     void lineWidth(double);
653     void linkProgram(Platform3DObject);
654     void pixelStorei(unsigned long pname, long param);
655     void polygonOffset(double factor, double units);
656
657     void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* data);
658
659     void releaseShaderCompiler();
660     void renderbufferStorage(unsigned long target, unsigned long internalformat, unsigned long width, unsigned long height);
661     void sampleCoverage(double value, bool invert);
662     void scissor(long x, long y, unsigned long width, unsigned long height);
663     void shaderSource(Platform3DObject, const String& string);
664     void stencilFunc(unsigned long func, long ref, unsigned long mask);
665     void stencilFuncSeparate(unsigned long face, unsigned long func, long ref, unsigned long mask);
666     void stencilMask(unsigned long mask);
667     void stencilMaskSeparate(unsigned long face, unsigned long mask);
668     void stencilOp(unsigned long fail, unsigned long zfail, unsigned long zpass);
669     void stencilOpSeparate(unsigned long face, unsigned long fail, unsigned long zfail, unsigned long zpass);
670
671     int texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, void* pixels);
672
673     void texParameterf(unsigned target, unsigned pname, float param);
674     void texParameteri(unsigned target, unsigned pname, int param);
675
676     int texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, void* pixels);
677
678     void uniform1f(long location, float x);
679     void uniform1fv(long location, float* v, int size);
680     void uniform1i(long location, int x);
681     void uniform1iv(long location, int* v, int size);
682     void uniform2f(long location, float x, float y);
683     void uniform2fv(long location, float* v, int size);
684     void uniform2i(long location, int x, int y);
685     void uniform2iv(long location, int* v, int size);
686     void uniform3f(long location, float x, float y, float z);
687     void uniform3fv(long location, float* v, int size);
688     void uniform3i(long location, int x, int y, int z);
689     void uniform3iv(long location, int* v, int size);
690     void uniform4f(long location, float x, float y, float z, float w);
691     void uniform4fv(long location, float* v, int size);
692     void uniform4i(long location, int x, int y, int z, int w);
693     void uniform4iv(long location, int* v, int size);
694     void uniformMatrix2fv(long location, bool transpose, float* value, int size);
695     void uniformMatrix3fv(long location, bool transpose, float* value, int size);
696     void uniformMatrix4fv(long location, bool transpose, float* value, int size);
697
698     void useProgram(Platform3DObject);
699     void validateProgram(Platform3DObject);
700
701     void vertexAttrib1f(unsigned long indx, float x);
702     void vertexAttrib1fv(unsigned long indx, float* values);
703     void vertexAttrib2f(unsigned long indx, float x, float y);
704     void vertexAttrib2fv(unsigned long indx, float* values);
705     void vertexAttrib3f(unsigned long indx, float x, float y, float z);
706     void vertexAttrib3fv(unsigned long indx, float* values);
707     void vertexAttrib4f(unsigned long indx, float x, float y, float z, float w);
708     void vertexAttrib4fv(unsigned long indx, float* values);
709     void vertexAttribPointer(unsigned long indx, int size, int type, bool normalized,
710                              unsigned long stride, unsigned long offset);
711
712     void viewport(long x, long y, unsigned long width, unsigned long height);
713
714     void reshape(int width, int height);
715
716 #if PLATFORM(CG)
717     void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
718                        int canvasWidth, int canvasHeight, CGContextRef context);
719 #endif
720
721     void paintRenderingResultsToCanvas(CanvasRenderingContext* context);
722
723     // Helpers for notification about paint events
724     void beginPaint(CanvasRenderingContext* context);
725     void endPaint();
726 #if PLATFORM(QT)
727     void paint(QPainter* painter, const QRect& rect) const;
728 #endif
729
730     // Support for buffer creation and deletion
731     unsigned createBuffer();
732     unsigned createFramebuffer();
733     unsigned createProgram();
734     unsigned createRenderbuffer();
735     unsigned createShader(unsigned long);
736     unsigned createTexture();
737
738     void deleteBuffer(unsigned);
739     void deleteFramebuffer(unsigned);
740     void deleteProgram(unsigned);
741     void deleteRenderbuffer(unsigned);
742     void deleteShader(unsigned);
743     void deleteTexture(unsigned);
744
745     // Synthesizes an OpenGL error which will be returned from a
746     // later call to getError. This is used to emulate OpenGL ES
747     // 2.0 behavior on the desktop and to enforce additional error
748     // checking mandated by WebGL.
749     //
750     // Per the behavior of glGetError, this stores at most one
751     // instance of any given error, and returns them from calls to
752     // getError in the order they were added.
753     void synthesizeGLError(unsigned long error);
754
755     bool supportsBGRA();
756
757   private:
758     GraphicsContext3D(Attributes attrs, HostWindow* hostWindow);
759
760     // Each platform must provide an implementation of this method.
761     //
762     // Gets the data for the given Image into outputVector in the
763     // format specified by the (OpenGL-style) format and type
764     // arguments. Despite the fact that the outputVector contains
765     // uint8_t, if the format and type specify packed pixels, then
766     // it will essentially contain uint16_t after the extraction
767     // process.
768     //
769     // If premultiplyAlpha is true, the alpha channel, if any,
770     // will be multiplied into the color channels during the
771     // extraction process. This premultiplication occurs before
772     // any packing of pixel data.
773     //
774     // No vertical flip of the image data is performed by this
775     // method.
776     bool getImageData(Image* image,
777                       unsigned int format,
778                       unsigned int type,
779                       bool premultiplyAlpha,
780                       Vector<uint8_t>& outputVector);
781
782     // Possible alpha operations that may need to occur during
783     // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
784     // be removed.
785     enum AlphaOp {
786         kAlphaDoNothing = 0,
787         kAlphaDoPremultiply = 1,
788         kAlphaDoUnmultiply = 2
789     };
790
791     // Helper for getImageData which implements packing of pixel
792     // data into the specified OpenGL destination format and type.
793     // A sourceUnpackAlignment of zero indicates that the source
794     // data is tightly packed. Non-zero values may take a slow path.
795     // Destination data will have no gaps between rows.
796     bool packPixels(const uint8_t* sourceData,
797                     SourceDataFormat sourceDataFormat,
798                     unsigned int width,
799                     unsigned int height,
800                     unsigned int sourceUnpackAlignment,
801                     unsigned int destinationFormat,
802                     unsigned int destinationType,
803                     AlphaOp alphaOp,
804                     void* destinationData);
805
806 #if PLATFORM(MAC)
807     // Take into account the user's requested context creation attributes,
808     // in particular stencil and antialias, and determine which could or
809     // could not be honored based on the capabilities of the OpenGL
810     // implementation.
811     void validateAttributes();
812 #endif
813
814     int m_currentWidth, m_currentHeight;
815
816     typedef struct { 
817         String source; 
818         String log; 
819         bool isValid;
820     } ShaderSourceEntry;
821     HashMap<Platform3DObject, ShaderSourceEntry> m_shaderSourceMap;
822
823     ANGLEWebKitBridge m_compiler;
824
825 #if PLATFORM(MAC)
826     Attributes m_attrs;
827     Vector<Vector<float> > m_vertexArray;
828
829     CGLContextObj m_contextObj;
830     RetainPtr<WebGLLayer> m_webGLLayer;
831     GLuint m_texture;
832     GLuint m_fbo;
833     GLuint m_depthStencilBuffer;
834
835     // For tracking which FBO is bound
836     GLuint m_boundFBO;
837
838     // For multisampling
839     GLuint m_multisampleFBO;
840     GLuint m_multisampleDepthStencilBuffer;
841     GLuint m_multisampleColorBuffer;
842
843     // Errors raised by synthesizeGLError().
844     ListHashSet<unsigned long> m_syntheticErrors;
845 #endif
846
847     // FIXME: ideally this would be used on all platforms.
848 #if PLATFORM(CHROMIUM) || PLATFORM(QT)
849     friend class GraphicsContext3DInternal;
850     OwnPtr<GraphicsContext3DInternal> m_internal;
851 #endif
852 };
853
854 } // namespace WebCore
855
856 #endif // GraphicsContext3D_h