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