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