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