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