2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef GraphicsContext3D_h
27 #define GraphicsContext3D_h
29 #include "GraphicsTypes3D.h"
32 #include "PlatformLayer.h"
34 #include <wtf/HashMap.h>
35 #include <wtf/ListHashSet.h>
36 #include <wtf/Noncopyable.h>
37 #include <wtf/RefCounted.h>
38 #include <wtf/text/WTFString.h>
41 #include "PlatformCALayer.h"
44 // FIXME: Find a better way to avoid the name confliction for NO_ERROR.
48 // This define is from the X11 headers, but it's used below, so we must undefine it.
52 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(BLACKBERRY) || PLATFORM(WIN) || PLATFORM(NIX)
53 #include "ANGLEWebKitBridge.h"
57 #include <wtf/RetainPtr.h>
59 OBJC_CLASS WebGLLayer;
60 #elif PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(NIX)
61 typedef unsigned int GLuint;
65 typedef struct _CGLContextObject *CGLContextObj;
67 typedef CGLContextObj PlatformGraphicsContext3D;
69 typedef void* PlatformGraphicsContext3D;
70 typedef void* PlatformGraphicsSurface3D;
73 // These are currently the same among all implementations.
74 const PlatformGraphicsContext3D NullPlatformGraphicsContext3D = 0;
75 const Platform3DObject NullPlatform3DObject = 0;
81 class Extensions3DOpenGLES;
83 class Extensions3DOpenGL;
93 class PlatformContextCairo;
94 #elif PLATFORM(BLACKBERRY)
95 class GraphicsContext;
98 typedef WTF::HashMap<CString, uint64_t> ShaderNameHash;
106 class GraphicsContext3DPrivate;
108 class GraphicsContext3D : public RefCounted<GraphicsContext3D> {
111 DEPTH_BUFFER_BIT = 0x00000100,
112 STENCIL_BUFFER_BIT = 0x00000400,
113 COLOR_BUFFER_BIT = 0x00004000,
119 TRIANGLE_STRIP = 0x0005,
120 TRIANGLE_FAN = 0x0006,
124 ONE_MINUS_SRC_COLOR = 0x0301,
126 ONE_MINUS_SRC_ALPHA = 0x0303,
128 ONE_MINUS_DST_ALPHA = 0x0305,
130 ONE_MINUS_DST_COLOR = 0x0307,
131 SRC_ALPHA_SATURATE = 0x0308,
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,
159 FRONT_AND_BACK = 0x0408,
164 STENCIL_TEST = 0x0B90,
166 SCISSOR_TEST = 0x0C11,
167 POLYGON_OFFSET_FILL = 0x8037,
168 SAMPLE_ALPHA_TO_COVERAGE = 0x809E,
169 SAMPLE_COVERAGE = 0x80A0,
171 INVALID_ENUM = 0x0500,
172 INVALID_VALUE = 0x0501,
173 INVALID_OPERATION = 0x0502,
174 OUT_OF_MEMORY = 0x0505,
178 ALIASED_POINT_SIZE_RANGE = 0x846D,
179 ALIASED_LINE_WIDTH_RANGE = 0x846E,
180 CULL_FACE_MODE = 0x0B45,
182 DEPTH_RANGE = 0x0B70,
183 DEPTH_WRITEMASK = 0x0B72,
184 DEPTH_CLEAR_VALUE = 0x0B73,
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,
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,
215 STENCIL_BITS = 0x0D57,
216 POLYGON_OFFSET_UNITS = 0x2A00,
217 POLYGON_OFFSET_FACTOR = 0x8038,
218 TEXTURE_BINDING_2D = 0x8069,
219 SAMPLE_BUFFERS = 0x80A8,
221 SAMPLE_COVERAGE_VALUE = 0x80AA,
222 SAMPLE_COVERAGE_INVERT = 0x80AB,
223 NUM_COMPRESSED_TEXTURE_FORMATS = 0x86A2,
224 COMPRESSED_TEXTURE_FORMATS = 0x86A3,
228 GENERATE_MIPMAP_HINT = 0x8192,
230 UNSIGNED_BYTE = 0x1401,
232 UNSIGNED_SHORT = 0x1403,
234 UNSIGNED_INT = 0x1405,
236 HALF_FLOAT_OES = 0x8D61,
238 DEPTH_COMPONENT = 0x1902,
244 LUMINANCE_ALPHA = 0x190A,
245 UNSIGNED_SHORT_4_4_4_4 = 0x8033,
246 UNSIGNED_SHORT_5_5_5_1 = 0x8034,
247 UNSIGNED_SHORT_5_6_5 = 0x8363,
248 FRAGMENT_SHADER = 0x8B30,
249 VERTEX_SHADER = 0x8B31,
250 MAX_VERTEX_ATTRIBS = 0x8869,
251 MAX_VERTEX_UNIFORM_VECTORS = 0x8DFB,
252 MAX_VARYING_VECTORS = 0x8DFC,
253 MAX_COMBINED_TEXTURE_IMAGE_UNITS = 0x8B4D,
254 MAX_VERTEX_TEXTURE_IMAGE_UNITS = 0x8B4C,
255 MAX_TEXTURE_IMAGE_UNITS = 0x8872,
256 MAX_FRAGMENT_UNIFORM_VECTORS = 0x8DFD,
257 SHADER_TYPE = 0x8B4F,
258 DELETE_STATUS = 0x8B80,
259 LINK_STATUS = 0x8B82,
260 VALIDATE_STATUS = 0x8B83,
261 ATTACHED_SHADERS = 0x8B85,
262 ACTIVE_UNIFORMS = 0x8B86,
263 ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87,
264 ACTIVE_ATTRIBUTES = 0x8B89,
265 ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A,
266 SHADING_LANGUAGE_VERSION = 0x8B8C,
267 CURRENT_PROGRAM = 0x8B8D,
289 NEAREST_MIPMAP_NEAREST = 0x2700,
290 LINEAR_MIPMAP_NEAREST = 0x2701,
291 NEAREST_MIPMAP_LINEAR = 0x2702,
292 LINEAR_MIPMAP_LINEAR = 0x2703,
293 TEXTURE_MAG_FILTER = 0x2800,
294 TEXTURE_MIN_FILTER = 0x2801,
295 TEXTURE_WRAP_S = 0x2802,
296 TEXTURE_WRAP_T = 0x2803,
298 TEXTURE_CUBE_MAP = 0x8513,
299 TEXTURE_BINDING_CUBE_MAP = 0x8514,
300 TEXTURE_CUBE_MAP_POSITIVE_X = 0x8515,
301 TEXTURE_CUBE_MAP_NEGATIVE_X = 0x8516,
302 TEXTURE_CUBE_MAP_POSITIVE_Y = 0x8517,
303 TEXTURE_CUBE_MAP_NEGATIVE_Y = 0x8518,
304 TEXTURE_CUBE_MAP_POSITIVE_Z = 0x8519,
305 TEXTURE_CUBE_MAP_NEGATIVE_Z = 0x851A,
306 MAX_CUBE_MAP_TEXTURE_SIZE = 0x851C,
339 ACTIVE_TEXTURE = 0x84E0,
341 CLAMP_TO_EDGE = 0x812F,
342 MIRRORED_REPEAT = 0x8370,
357 SAMPLER_CUBE = 0x8B60,
358 VERTEX_ATTRIB_ARRAY_ENABLED = 0x8622,
359 VERTEX_ATTRIB_ARRAY_SIZE = 0x8623,
360 VERTEX_ATTRIB_ARRAY_STRIDE = 0x8624,
361 VERTEX_ATTRIB_ARRAY_TYPE = 0x8625,
362 VERTEX_ATTRIB_ARRAY_NORMALIZED = 0x886A,
363 VERTEX_ATTRIB_ARRAY_POINTER = 0x8645,
364 VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = 0x889F,
365 COMPILE_STATUS = 0x8B81,
366 INFO_LOG_LENGTH = 0x8B84,
367 SHADER_SOURCE_LENGTH = 0x8B88,
368 SHADER_COMPILER = 0x8DFA,
369 SHADER_BINARY_FORMATS = 0x8DF8,
370 NUM_SHADER_BINARY_FORMATS = 0x8DF9,
372 MEDIUM_FLOAT = 0x8DF1,
377 FRAMEBUFFER = 0x8D40,
378 RENDERBUFFER = 0x8D41,
382 DEPTH_COMPONENT16 = 0x81A5,
383 STENCIL_INDEX = 0x1901,
384 STENCIL_INDEX8 = 0x8D48,
385 DEPTH_STENCIL = 0x84F9,
386 UNSIGNED_INT_24_8 = 0x84FA,
387 DEPTH24_STENCIL8 = 0x88F0,
388 RENDERBUFFER_WIDTH = 0x8D42,
389 RENDERBUFFER_HEIGHT = 0x8D43,
390 RENDERBUFFER_INTERNAL_FORMAT = 0x8D44,
391 RENDERBUFFER_RED_SIZE = 0x8D50,
392 RENDERBUFFER_GREEN_SIZE = 0x8D51,
393 RENDERBUFFER_BLUE_SIZE = 0x8D52,
394 RENDERBUFFER_ALPHA_SIZE = 0x8D53,
395 RENDERBUFFER_DEPTH_SIZE = 0x8D54,
396 RENDERBUFFER_STENCIL_SIZE = 0x8D55,
397 FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = 0x8CD0,
398 FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = 0x8CD1,
399 FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL = 0x8CD2,
400 FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE = 0x8CD3,
401 COLOR_ATTACHMENT0 = 0x8CE0,
402 DEPTH_ATTACHMENT = 0x8D00,
403 STENCIL_ATTACHMENT = 0x8D20,
404 DEPTH_STENCIL_ATTACHMENT = 0x821A,
406 FRAMEBUFFER_COMPLETE = 0x8CD5,
407 FRAMEBUFFER_INCOMPLETE_ATTACHMENT = 0x8CD6,
408 FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = 0x8CD7,
409 FRAMEBUFFER_INCOMPLETE_DIMENSIONS = 0x8CD9,
410 FRAMEBUFFER_UNSUPPORTED = 0x8CDD,
411 FRAMEBUFFER_BINDING = 0x8CA6,
412 RENDERBUFFER_BINDING = 0x8CA7,
413 MAX_RENDERBUFFER_SIZE = 0x84E8,
414 INVALID_FRAMEBUFFER_OPERATION = 0x0506,
416 // WebGL-specific enums
417 UNPACK_FLIP_Y_WEBGL = 0x9240,
418 UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241,
419 CONTEXT_LOST_WEBGL = 0x9242,
420 UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243,
421 BROWSER_DEFAULT_WEBGL = 0x9244
424 // Context creation attributes.
431 , premultipliedAlpha(true)
432 , preserveDrawingBuffer(false)
433 , noExtensions(false)
434 , shareResources(true)
435 , preferDiscreteGPU(false)
436 , multithreaded(false)
437 , forceSoftwareRenderer(false)
445 bool premultipliedAlpha;
446 bool preserveDrawingBuffer;
449 bool preferDiscreteGPU;
451 bool forceSoftwareRenderer;
456 RenderDirectlyToHostWindow,
457 RenderToCurrentGLContext
460 class ContextLostCallback {
462 virtual void onContextLost() = 0;
463 virtual ~ContextLostCallback() {}
466 class ErrorMessageCallback {
468 virtual void onErrorMessage(const String& message, GC3Dint id) = 0;
469 virtual ~ErrorMessageCallback() { }
472 void setContextLostCallback(PassOwnPtr<ContextLostCallback>);
473 void setErrorMessageCallback(PassOwnPtr<ErrorMessageCallback>);
475 static PassRefPtr<GraphicsContext3D> create(Attributes, HostWindow*, RenderStyle = RenderOffscreen);
476 static PassRefPtr<GraphicsContext3D> createForCurrentGLContext();
477 ~GraphicsContext3D();
480 PlatformGraphicsContext3D platformGraphicsContext3D() const { return m_contextObj; }
481 Platform3DObject platformTexture() const { return m_compositorTexture; }
482 CALayer* platformLayer() const { return reinterpret_cast<CALayer*>(m_webGLLayer.get()); }
484 PlatformGraphicsContext3D platformGraphicsContext3D();
485 Platform3DObject platformTexture() const;
486 #if USE(ACCELERATED_COMPOSITING)
487 PlatformLayer* platformLayer() const;
491 bool makeContextCurrent();
493 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(BLACKBERRY) || PLATFORM(WIN) || PLATFORM(NIX)
494 // With multisampling on, blit from multisampleFBO to regular FBO.
495 void prepareTexture();
498 // Equivalent to ::glTexImage2D(). Allows pixels==0 with no allocation.
499 void texImage2DDirect(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
501 // Helper to texImage2D with pixel==0 case: pixels are initialized to 0.
502 // Return true if no GL error is synthesized.
503 // By default, alignment is 4, the OpenGL default setting.
504 bool texImage2DResourceSafe(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint alignment = 4);
506 bool isGLES2Compliant() const;
508 //----------------------------------------------------------------------
509 // Helpers for texture uploading and pixel readback.
512 // Computes the components per pixel and bytes per component
513 // for the given format and type combination. Returns false if
514 // either was an invalid enum.
515 static bool computeFormatAndTypeParameters(GC3Denum format,
517 unsigned int* componentsPerPixel,
518 unsigned int* bytesPerComponent);
520 // Computes the image size in bytes. If paddingInBytes is not null, padding
521 // is also calculated in return. Returns NO_ERROR if succeed, otherwise
522 // return the suggested GL error indicating the cause of the failure:
523 // INVALID_VALUE if width/height is negative or overflow happens.
524 // INVALID_ENUM if format/type is illegal.
525 static GC3Denum computeImageSizeInBytes(GC3Denum format,
530 unsigned int* imageSizeInBytes,
531 unsigned int* paddingInBytes);
533 // Extracts the contents of the given ImageData into the passed Vector,
534 // packing the pixel data according to the given format and type,
535 // and obeying the flipY and premultiplyAlpha flags. Returns true
537 static bool extractImageData(ImageData*,
541 bool premultiplyAlpha,
542 Vector<uint8_t>& data);
544 // Helper function which extracts the user-supplied texture
545 // data, applying the flipY and premultiplyAlpha parameters.
546 // If the data is not tightly packed according to the passed
547 // unpackAlignment, the output data will be tightly packed.
548 // Returns true if successful, false if any error occurred.
549 static bool extractTextureData(unsigned int width, unsigned int height,
550 GC3Denum format, GC3Denum type,
551 unsigned int unpackAlignment,
552 bool flipY, bool premultiplyAlpha,
554 Vector<uint8_t>& data);
557 // Attempt to enumerate all possible native image formats to
558 // reduce the amount of temporary allocations during texture
559 // uploading. This enum must be public because it is accessed
560 // by non-member functions.
563 DataFormatRGBA16Little,
568 DataFormatRGB16Little,
574 DataFormatBGRA16Little,
577 DataFormatARGB16Little,
589 DataFormatRA16Little,
594 DataFormatAR16Little,
604 // Check if the format is one of the formats from the ImageData or DOM elements.
605 // The formats from ImageData is always RGBA8.
606 // The formats from DOM elements vary with Graphics ports. It can only be RGBA8 or BGRA8 for non-CG port while a little more for CG port.
607 static ALWAYS_INLINE bool srcFormatComesFromDOMElementOrImageData(DataFormat SrcFormat)
611 return SrcFormat == DataFormatRGBA8 || SrcFormat == DataFormatARGB8 || SrcFormat == DataFormatRGB8
612 || SrcFormat == DataFormatRA8 || SrcFormat == DataFormatAR8 || SrcFormat == DataFormatR8 || SrcFormat == DataFormatA8;
614 // That LITTLE_ENDIAN case has more possible formats than BIG_ENDIAN case is because some decoded image data is actually big endian
615 // even on little endian architectures.
616 return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatABGR8 || SrcFormat == DataFormatBGR8
617 || SrcFormat == DataFormatRGBA8 || SrcFormat == DataFormatARGB8 || SrcFormat == DataFormatRGB8
618 || SrcFormat == DataFormatR8 || SrcFormat == DataFormatA8
619 || SrcFormat == DataFormatRA8 || SrcFormat == DataFormatAR8;
622 return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
626 //----------------------------------------------------------------------
627 // Entry points for WebGL.
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);
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);
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);
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 void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
658 void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
659 void cullFace(GC3Denum mode);
660 void depthFunc(GC3Denum func);
661 void depthMask(GC3Dboolean flag);
662 void depthRange(GC3Dclampf zNear, GC3Dclampf zFar);
663 void detachShader(Platform3DObject, Platform3DObject);
664 void disable(GC3Denum cap);
665 void disableVertexAttribArray(GC3Duint index);
666 void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count);
667 void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
669 void enable(GC3Denum cap);
670 void enableVertexAttribArray(GC3Duint index);
673 void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, Platform3DObject);
674 void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level);
675 void frontFace(GC3Denum mode);
676 void generateMipmap(GC3Denum target);
678 bool getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo&);
679 bool getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo&);
680 void getAttachedShaders(Platform3DObject program, GC3Dsizei maxCount, GC3Dsizei* count, Platform3DObject* shaders);
681 GC3Dint getAttribLocation(Platform3DObject, const String& name);
682 void getBooleanv(GC3Denum pname, GC3Dboolean* value);
683 void getBufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
684 Attributes getContextAttributes();
686 void getFloatv(GC3Denum pname, GC3Dfloat* value);
687 void getFramebufferAttachmentParameteriv(GC3Denum target, GC3Denum attachment, GC3Denum pname, GC3Dint* value);
688 void getIntegerv(GC3Denum pname, GC3Dint* value);
689 void getProgramiv(Platform3DObject program, GC3Denum pname, GC3Dint* value);
690 String getProgramInfoLog(Platform3DObject);
691 void getRenderbufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
692 void getShaderiv(Platform3DObject, GC3Denum pname, GC3Dint* value);
693 String getShaderInfoLog(Platform3DObject);
694 void getShaderPrecisionFormat(GC3Denum shaderType, GC3Denum precisionType, GC3Dint* range, GC3Dint* precision);
695 String getShaderSource(Platform3DObject);
696 String getString(GC3Denum name);
697 void getTexParameterfv(GC3Denum target, GC3Denum pname, GC3Dfloat* value);
698 void getTexParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
699 void getUniformfv(Platform3DObject program, GC3Dint location, GC3Dfloat* value);
700 void getUniformiv(Platform3DObject program, GC3Dint location, GC3Dint* value);
701 GC3Dint getUniformLocation(Platform3DObject, const String& name);
702 void getVertexAttribfv(GC3Duint index, GC3Denum pname, GC3Dfloat* value);
703 void getVertexAttribiv(GC3Duint index, GC3Denum pname, GC3Dint* value);
704 GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
706 void hint(GC3Denum target, GC3Denum mode);
707 GC3Dboolean isBuffer(Platform3DObject);
708 GC3Dboolean isEnabled(GC3Denum cap);
709 GC3Dboolean isFramebuffer(Platform3DObject);
710 GC3Dboolean isProgram(Platform3DObject);
711 GC3Dboolean isRenderbuffer(Platform3DObject);
712 GC3Dboolean isShader(Platform3DObject);
713 GC3Dboolean isTexture(Platform3DObject);
714 void lineWidth(GC3Dfloat);
715 void linkProgram(Platform3DObject);
716 void pixelStorei(GC3Denum pname, GC3Dint param);
717 void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
719 void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
721 void releaseShaderCompiler();
723 void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
724 void sampleCoverage(GC3Dclampf value, GC3Dboolean invert);
725 void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
726 void shaderSource(Platform3DObject, const String& string);
727 void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
728 void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
729 void stencilMask(GC3Duint mask);
730 void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
731 void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
732 void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
734 bool texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
735 void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
736 void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
737 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels);
739 void uniform1f(GC3Dint location, GC3Dfloat x);
740 void uniform1fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
741 void uniform1i(GC3Dint location, GC3Dint x);
742 void uniform1iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
743 void uniform2f(GC3Dint location, GC3Dfloat x, GC3Dfloat y);
744 void uniform2fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
745 void uniform2i(GC3Dint location, GC3Dint x, GC3Dint y);
746 void uniform2iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
747 void uniform3f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
748 void uniform3fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
749 void uniform3i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z);
750 void uniform3iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
751 void uniform4f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
752 void uniform4fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
753 void uniform4i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w);
754 void uniform4iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
755 void uniformMatrix2fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
756 void uniformMatrix3fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
757 void uniformMatrix4fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
759 void useProgram(Platform3DObject);
760 void validateProgram(Platform3DObject);
761 bool areProgramSymbolsValid(Platform3DObject vertexShader, Platform3DObject fragmentShader) const;
763 void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
764 void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values);
765 void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
766 void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values);
767 void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
768 void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values);
769 void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
770 void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values);
771 void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
772 GC3Dsizei stride, GC3Dintptr offset);
774 void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
776 void reshape(int width, int height);
778 #if PLATFORM(GTK) || PLATFORM(EFL) || USE(CAIRO)
779 void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
780 int canvasWidth, int canvasHeight, PlatformContextCairo* context);
781 #elif PLATFORM(BLACKBERRY) || USE(CG)
782 void paintToCanvas(const unsigned char* imagePixels, int imageWidth, int imageHeight,
783 int canvasWidth, int canvasHeight, GraphicsContext*);
786 void markContextChanged();
787 void markLayerComposited();
788 bool layerComposited() const;
790 void paintRenderingResultsToCanvas(ImageBuffer*, DrawingBuffer*);
791 PassRefPtr<ImageData> paintRenderingResultsToImageData(DrawingBuffer*);
792 bool paintCompositedResultsToCanvas(ImageBuffer*);
794 #if PLATFORM(BLACKBERRY)
795 bool paintsIntoCanvasBuffer() const;
798 // Support for buffer creation and deletion
799 Platform3DObject createBuffer();
800 Platform3DObject createFramebuffer();
801 Platform3DObject createProgram();
802 Platform3DObject createRenderbuffer();
803 Platform3DObject createShader(GC3Denum);
804 Platform3DObject createTexture();
806 void deleteBuffer(Platform3DObject);
807 void deleteFramebuffer(Platform3DObject);
808 void deleteProgram(Platform3DObject);
809 void deleteRenderbuffer(Platform3DObject);
810 void deleteShader(Platform3DObject);
811 void deleteTexture(Platform3DObject);
813 // Synthesizes an OpenGL error which will be returned from a
814 // later call to getError. This is used to emulate OpenGL ES
815 // 2.0 behavior on the desktop and to enforce additional error
816 // checking mandated by WebGL.
818 // Per the behavior of glGetError, this stores at most one
819 // instance of any given error, and returns them from calls to
820 // getError in the order they were added.
821 void synthesizeGLError(GC3Denum error);
823 // Support for extensions. Returns a non-null object, though not
824 // all methods it contains may necessarily be supported on the
825 // current hardware. Must call Extensions3D::supports() to
827 Extensions3D* getExtensions();
829 IntSize getInternalFramebufferSize() const;
831 static unsigned getClearBitsByAttachmentType(GC3Denum);
832 static unsigned getClearBitsByFormat(GC3Denum);
841 ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
842 ChannelRGBA = ChannelRGB | ChannelAlpha,
845 static unsigned getChannelBitsByFormat(GC3Denum);
847 // Possible alpha operations that may need to occur during
848 // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
852 AlphaDoPremultiply = 1,
853 AlphaDoUnmultiply = 2
856 enum ImageHtmlDomSource {
863 // Packs the contents of the given Image which is passed in |pixels| into the passed Vector
864 // according to the given format and type, and obeying the flipY and AlphaOp flags.
865 // Returns true upon success.
866 static bool packImageData(Image*, const void* pixels, GC3Denum format, GC3Denum type, bool flipY, AlphaOp, DataFormat sourceFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, Vector<uint8_t>& data);
868 class ImageExtractor {
870 ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
872 // Each platform must provide an implementation of this method to deallocate or release resources
873 // associated with the image if needed.
876 bool extractSucceeded() { return m_extractSucceeded; }
877 const void* imagePixelData() { return m_imagePixelData; }
878 unsigned imageWidth() { return m_imageWidth; }
879 unsigned imageHeight() { return m_imageHeight; }
880 DataFormat imageSourceFormat() { return m_imageSourceFormat; }
881 AlphaOp imageAlphaOp() { return m_alphaOp; }
882 unsigned imageSourceUnpackAlignment() { return m_imageSourceUnpackAlignment; }
883 ImageHtmlDomSource imageHtmlDomSource() { return m_imageHtmlDomSource; }
885 // Each platform must provide an implementation of this method.
886 // Extracts the image and keeps track of its status, such as width, height, Source Alignment, format and AlphaOp etc,
887 // needs to lock the resources or relevant data if needed and returns true upon success
888 bool extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
891 ImageSource* m_decoder;
892 RefPtr<cairo_surface_t> m_imageSurface;
894 CGImageRef m_cgImage;
895 RetainPtr<CGImageRef> m_decodedImage;
896 RetainPtr<CFDataRef> m_pixelData;
897 std::unique_ptr<uint8_t[]> m_formalizedRGBA8Data;
898 #elif PLATFORM(BLACKBERRY)
899 Vector<unsigned> m_imageData;
902 ImageHtmlDomSource m_imageHtmlDomSource;
903 bool m_extractSucceeded;
904 const void* m_imagePixelData;
905 unsigned m_imageWidth;
906 unsigned m_imageHeight;
907 DataFormat m_imageSourceFormat;
909 unsigned m_imageSourceUnpackAlignment;
913 GraphicsContext3D(Attributes, HostWindow*, RenderStyle = RenderOffscreen);
915 // Helper for packImageData/extractImageData/extractTextureData which implement packing of pixel
916 // data into the specified OpenGL destination format and type.
917 // A sourceUnpackAlignment of zero indicates that the source
918 // data is tightly packed. Non-zero values may take a slow path.
919 // Destination data will have no gaps between rows.
920 static bool packPixels(const uint8_t* sourceData, DataFormat sourceDataFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, unsigned destinationFormat, unsigned destinationType, AlphaOp, void* destinationData, bool flipY);
922 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(BLACKBERRY) || PLATFORM(WIN) || PLATFORM(NIX)
923 // Take into account the user's requested context creation attributes,
924 // in particular stencil and antialias, and determine which could or
925 // could not be honored based on the capabilities of the OpenGL
927 void validateDepthStencil(const char* packedDepthStencilExtension);
928 void validateAttributes();
930 // Read rendering results into a pixel array with the same format as the
932 void readRenderingResults(unsigned char* pixels, int pixelsSize);
933 void readPixelsAndConvertToBGRAIfNecessary(int x, int y, int width, int height, unsigned char* pixels);
936 #if PLATFORM(BLACKBERRY)
937 void logFrameBufferStatus(int line);
938 void readPixelsIMG(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
941 bool reshapeFBOs(const IntSize&);
942 void resolveMultisamplingIfNecessary(const IntRect& = IntRect());
943 #if (PLATFORM(EFL) || PLATFORM(NIX)) && USE(GRAPHICS_SURFACE)
944 void createGraphicsSurfaces(const IntSize&);
947 int m_currentWidth, m_currentHeight;
948 bool isResourceSafe();
951 CGLContextObj m_contextObj;
952 RetainPtr<WebGLLayer> m_webGLLayer;
953 #elif PLATFORM(WIN) && USE(CA)
954 RefPtr<PlatformCALayer> m_webGLLayer;
955 #elif PLATFORM(BLACKBERRY)
956 #if USE(ACCELERATED_COMPOSITING)
957 RefPtr<PlatformLayer> m_compositingLayer;
962 #if PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(BLACKBERRY) || PLATFORM(WIN) || PLATFORM(NIX)
967 , precision(SH_PRECISION_UNDEFINED)
972 SymbolInfo(GC3Denum type, int size, const String& mappedName, ShPrecisionType precision, int staticUse)
975 , mappedName(mappedName)
976 , precision(precision)
977 , staticUse(staticUse)
981 bool operator==(SymbolInfo& other) const
983 return type == other.type && size == other.size && mappedName == other.mappedName;
989 ShPrecisionType precision;
993 typedef HashMap<String, SymbolInfo> ShaderSymbolMap;
995 struct ShaderSourceEntry {
998 String translatedSource;
1001 ShaderSymbolMap attributeMap;
1002 ShaderSymbolMap uniformMap;
1003 ShaderSymbolMap varyingMap;
1005 : type(VERTEX_SHADER)
1010 ShaderSymbolMap& symbolMap(enum ANGLEShaderSymbolType symbolType)
1012 ASSERT(symbolType == SHADER_SYMBOL_TYPE_ATTRIBUTE || symbolType == SHADER_SYMBOL_TYPE_UNIFORM || symbolType == SHADER_SYMBOL_TYPE_VARYING);
1013 if (symbolType == SHADER_SYMBOL_TYPE_ATTRIBUTE)
1014 return attributeMap;
1015 if (symbolType == SHADER_SYMBOL_TYPE_VARYING)
1021 typedef HashMap<Platform3DObject, ShaderSourceEntry> ShaderSourceMap;
1022 ShaderSourceMap m_shaderSourceMap;
1024 String mappedSymbolName(Platform3DObject program, ANGLEShaderSymbolType, const String& name);
1025 String originalSymbolName(Platform3DObject program, ANGLEShaderSymbolType, const String& name);
1027 ANGLEWebKitBridge m_compiler;
1029 OwnPtr<ShaderNameHash> nameHashMapForShaders;
1032 #if PLATFORM(BLACKBERRY) || ((PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(WIN) || PLATFORM(NIX)) && USE(OPENGL_ES_2))
1033 friend class Extensions3DOpenGLES;
1034 OwnPtr<Extensions3DOpenGLES> m_extensions;
1036 friend class Extensions3DOpenGL;
1037 OwnPtr<Extensions3DOpenGL> m_extensions;
1039 friend class Extensions3DOpenGLCommon;
1042 RenderStyle m_renderStyle;
1043 Vector<Vector<float>> m_vertexArray;
1046 #if !PLATFORM(BLACKBERRY)
1047 GC3Duint m_compositorTexture;
1051 #if !PLATFORM(BLACKBERRY)
1052 GC3Duint m_depthBuffer;
1053 GC3Duint m_stencilBuffer;
1055 GC3Duint m_depthStencilBuffer;
1057 bool m_layerComposited;
1058 GC3Duint m_internalColorFormat;
1060 struct GraphicsContext3DState {
1061 GraphicsContext3DState()
1063 , activeTexture(GraphicsContext3D::TEXTURE0)
1068 GC3Denum activeTexture;
1069 GC3Duint boundTexture0;
1072 GraphicsContext3DState m_state;
1074 // For multisampling
1075 GC3Duint m_multisampleFBO;
1076 GC3Duint m_multisampleDepthStencilBuffer;
1077 GC3Duint m_multisampleColorBuffer;
1079 // Errors raised by synthesizeGLError().
1080 ListHashSet<GC3Denum> m_syntheticErrors;
1082 #if PLATFORM(BLACKBERRY)
1083 bool m_isImaginationHardware;
1086 #if !PLATFORM(BLACKBERRY)
1087 friend class GraphicsContext3DPrivate;
1088 OwnPtr<GraphicsContext3DPrivate> m_private;
1092 } // namespace WebCore
1094 #endif // GraphicsContext3D_h