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 WebGLRenderingContext_h
27 #define WebGLRenderingContext_h
29 #include "CanvasRenderingContext.h"
30 #include "ExceptionCode.h"
31 #include "Float32Array.h"
32 #include "GraphicsContext3D.h"
33 #include "Int32Array.h"
34 #include "PlatformString.h"
36 #include "Uint8Array.h"
37 #include "WebGLGetInfo.h"
39 #include <wtf/OwnArrayPtr.h>
43 class WebGLActiveInfo;
45 class WebGLContextAttributes;
47 class WebGLFramebuffer;
50 class WebGLRenderbuffer;
53 class WebGLUniformLocation;
54 class WebKitLoseContext;
55 class HTMLImageElement;
56 class HTMLVideoElement;
60 class OESStandardDerivatives;
61 class OESTextureFloat;
62 class OESVertexArrayObject;
63 class WebGLVertexArrayObjectOES;
65 class WebGLRenderingContext : public CanvasRenderingContext {
67 static PassOwnPtr<WebGLRenderingContext> create(HTMLCanvasElement*, WebGLContextAttributes*);
68 virtual ~WebGLRenderingContext();
70 virtual bool is3d() const { return true; }
71 virtual bool isAccelerated() const { return true; }
72 virtual bool paintsIntoCanvasBuffer() const;
74 int drawingBufferWidth() const;
75 int drawingBufferHeight() const;
77 void activeTexture(GC3Denum texture, ExceptionCode&);
78 void attachShader(WebGLProgram*, WebGLShader*, ExceptionCode&);
79 void bindAttribLocation(WebGLProgram*, GC3Duint index, const String& name, ExceptionCode&);
80 void bindBuffer(GC3Denum target, WebGLBuffer*, ExceptionCode&);
81 void bindFramebuffer(GC3Denum target, WebGLFramebuffer*, ExceptionCode&);
82 void bindRenderbuffer(GC3Denum target, WebGLRenderbuffer*, ExceptionCode&);
83 void bindTexture(GC3Denum target, WebGLTexture*, ExceptionCode&);
84 void blendColor(GC3Dfloat red, GC3Dfloat green, GC3Dfloat blue, GC3Dfloat alpha);
85 void blendEquation(GC3Denum mode);
86 void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
87 void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
88 void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
90 void bufferData(GC3Denum target, GC3Dsizeiptr size, GC3Denum usage, ExceptionCode&);
91 void bufferData(GC3Denum target, ArrayBuffer* data, GC3Denum usage, ExceptionCode&);
92 void bufferData(GC3Denum target, ArrayBufferView* data, GC3Denum usage, ExceptionCode&);
93 void bufferSubData(GC3Denum target, GC3Dintptr offset, ArrayBuffer* data, ExceptionCode&);
94 void bufferSubData(GC3Denum target, GC3Dintptr offset, ArrayBufferView* data, ExceptionCode&);
96 GC3Denum checkFramebufferStatus(GC3Denum target);
97 void clear(GC3Dbitfield mask);
98 void clearColor(GC3Dfloat red, GC3Dfloat green, GC3Dfloat blue, GC3Dfloat alpha);
99 void clearDepth(GC3Dfloat);
100 void clearStencil(GC3Dint);
101 void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
102 void compileShader(WebGLShader*, ExceptionCode&);
104 // void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Dsizei imageSize, const void* data);
105 // void compressedTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei GC3Dsizei height, GC3Denum format, GC3Dsizei imageSize, const void* data);
107 void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
108 void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
110 PassRefPtr<WebGLBuffer> createBuffer();
111 PassRefPtr<WebGLFramebuffer> createFramebuffer();
112 PassRefPtr<WebGLProgram> createProgram();
113 PassRefPtr<WebGLRenderbuffer> createRenderbuffer();
114 PassRefPtr<WebGLShader> createShader(GC3Denum type, ExceptionCode&);
115 PassRefPtr<WebGLTexture> createTexture();
117 void cullFace(GC3Denum mode);
119 void deleteBuffer(WebGLBuffer*);
120 void deleteFramebuffer(WebGLFramebuffer*);
121 void deleteProgram(WebGLProgram*);
122 void deleteRenderbuffer(WebGLRenderbuffer*);
123 void deleteShader(WebGLShader*);
124 void deleteTexture(WebGLTexture*);
126 void depthFunc(GC3Denum);
127 void depthMask(GC3Dboolean);
128 void depthRange(GC3Dfloat zNear, GC3Dfloat zFar);
129 void detachShader(WebGLProgram*, WebGLShader*, ExceptionCode&);
130 void disable(GC3Denum cap);
131 void disableVertexAttribArray(GC3Duint index, ExceptionCode&);
132 void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count, ExceptionCode&);
133 void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset, ExceptionCode&);
135 void enable(GC3Denum cap);
136 void enableVertexAttribArray(GC3Duint index, ExceptionCode&);
139 void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, WebGLRenderbuffer*, ExceptionCode&);
140 void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, WebGLTexture*, GC3Dint level, ExceptionCode&);
141 void frontFace(GC3Denum mode);
142 void generateMipmap(GC3Denum target);
144 PassRefPtr<WebGLActiveInfo> getActiveAttrib(WebGLProgram*, GC3Duint index, ExceptionCode&);
145 PassRefPtr<WebGLActiveInfo> getActiveUniform(WebGLProgram*, GC3Duint index, ExceptionCode&);
146 bool getAttachedShaders(WebGLProgram*, Vector<WebGLShader*>&, ExceptionCode&);
147 GC3Dint getAttribLocation(WebGLProgram*, const String& name);
148 WebGLGetInfo getBufferParameter(GC3Denum target, GC3Denum pname, ExceptionCode&);
149 PassRefPtr<WebGLContextAttributes> getContextAttributes();
151 WebGLExtension* getExtension(const String& name);
152 WebGLGetInfo getFramebufferAttachmentParameter(GC3Denum target, GC3Denum attachment, GC3Denum pname, ExceptionCode&);
153 WebGLGetInfo getParameter(GC3Denum pname, ExceptionCode&);
154 WebGLGetInfo getProgramParameter(WebGLProgram*, GC3Denum pname, ExceptionCode&);
155 String getProgramInfoLog(WebGLProgram*, ExceptionCode&);
156 WebGLGetInfo getRenderbufferParameter(GC3Denum target, GC3Denum pname, ExceptionCode&);
157 WebGLGetInfo getShaderParameter(WebGLShader*, GC3Denum pname, ExceptionCode&);
158 String getShaderInfoLog(WebGLShader*, ExceptionCode&);
161 // void glGetShaderPrecisionFormat (GC3Denum shadertype, GC3Denum precisiontype, GC3Dint* range, GC3Dint* precision);
163 String getShaderSource(WebGLShader*, ExceptionCode&);
164 Vector<String> getSupportedExtensions();
165 WebGLGetInfo getTexParameter(GC3Denum target, GC3Denum pname, ExceptionCode&);
166 WebGLGetInfo getUniform(WebGLProgram*, const WebGLUniformLocation*, ExceptionCode&);
167 PassRefPtr<WebGLUniformLocation> getUniformLocation(WebGLProgram*, const String&, ExceptionCode&);
168 WebGLGetInfo getVertexAttrib(GC3Duint index, GC3Denum pname, ExceptionCode&);
169 GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
171 void hint(GC3Denum target, GC3Denum mode);
172 GC3Dboolean isBuffer(WebGLBuffer*);
173 bool isContextLost();
174 GC3Dboolean isEnabled(GC3Denum cap);
175 GC3Dboolean isFramebuffer(WebGLFramebuffer*);
176 GC3Dboolean isProgram(WebGLProgram*);
177 GC3Dboolean isRenderbuffer(WebGLRenderbuffer*);
178 GC3Dboolean isShader(WebGLShader*);
179 GC3Dboolean isTexture(WebGLTexture*);
181 void lineWidth(GC3Dfloat);
182 void linkProgram(WebGLProgram*, ExceptionCode&);
183 void pixelStorei(GC3Denum pname, GC3Dint param);
184 void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
185 void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, ArrayBufferView* pixels, ExceptionCode&);
186 void releaseShaderCompiler();
187 void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
188 void sampleCoverage(GC3Dfloat value, GC3Dboolean invert);
189 void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
190 void shaderSource(WebGLShader*, const String&, ExceptionCode&);
191 void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
192 void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
193 void stencilMask(GC3Duint);
194 void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
195 void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
196 void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
198 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
199 GC3Dsizei width, GC3Dsizei height, GC3Dint border,
200 GC3Denum format, GC3Denum type, ArrayBufferView*, ExceptionCode&);
201 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
202 GC3Denum format, GC3Denum type, ImageData*, ExceptionCode&);
203 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
204 GC3Denum format, GC3Denum type, HTMLImageElement*, ExceptionCode&);
205 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
206 GC3Denum format, GC3Denum type, HTMLCanvasElement*, ExceptionCode&);
208 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
209 GC3Denum format, GC3Denum type, HTMLVideoElement*, ExceptionCode&);
212 void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
213 void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
215 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
216 GC3Dsizei width, GC3Dsizei height,
217 GC3Denum format, GC3Denum type, ArrayBufferView*, ExceptionCode&);
218 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
219 GC3Denum format, GC3Denum type, ImageData*, ExceptionCode&);
220 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
221 GC3Denum format, GC3Denum type, HTMLImageElement*, ExceptionCode&);
222 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
223 GC3Denum format, GC3Denum type, HTMLCanvasElement*, ExceptionCode&);
225 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
226 GC3Denum format, GC3Denum type, HTMLVideoElement*, ExceptionCode&);
229 void uniform1f(const WebGLUniformLocation* location, GC3Dfloat x, ExceptionCode&);
230 void uniform1fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
231 void uniform1fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
232 void uniform1i(const WebGLUniformLocation* location, GC3Dint x, ExceptionCode&);
233 void uniform1iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
234 void uniform1iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
235 void uniform2f(const WebGLUniformLocation* location, GC3Dfloat x, GC3Dfloat y, ExceptionCode&);
236 void uniform2fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
237 void uniform2fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
238 void uniform2i(const WebGLUniformLocation* location, GC3Dint x, GC3Dint y, ExceptionCode&);
239 void uniform2iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
240 void uniform2iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
241 void uniform3f(const WebGLUniformLocation* location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, ExceptionCode&);
242 void uniform3fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
243 void uniform3fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
244 void uniform3i(const WebGLUniformLocation* location, GC3Dint x, GC3Dint y, GC3Dint z, ExceptionCode&);
245 void uniform3iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
246 void uniform3iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
247 void uniform4f(const WebGLUniformLocation* location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w, ExceptionCode&);
248 void uniform4fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
249 void uniform4fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
250 void uniform4i(const WebGLUniformLocation* location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w, ExceptionCode&);
251 void uniform4iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
252 void uniform4iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
253 void uniformMatrix2fv(const WebGLUniformLocation* location, GC3Dboolean transpose, Float32Array* value, ExceptionCode&);
254 void uniformMatrix2fv(const WebGLUniformLocation* location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size, ExceptionCode&);
255 void uniformMatrix3fv(const WebGLUniformLocation* location, GC3Dboolean transpose, Float32Array* value, ExceptionCode&);
256 void uniformMatrix3fv(const WebGLUniformLocation* location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size, ExceptionCode&);
257 void uniformMatrix4fv(const WebGLUniformLocation* location, GC3Dboolean transpose, Float32Array* value, ExceptionCode&);
258 void uniformMatrix4fv(const WebGLUniformLocation* location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size, ExceptionCode&);
260 void useProgram(WebGLProgram*, ExceptionCode&);
261 void validateProgram(WebGLProgram*, ExceptionCode&);
263 void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
264 void vertexAttrib1fv(GC3Duint index, Float32Array* values);
265 void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
266 void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
267 void vertexAttrib2fv(GC3Duint index, Float32Array* values);
268 void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
269 void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
270 void vertexAttrib3fv(GC3Duint index, Float32Array* values);
271 void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
272 void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
273 void vertexAttrib4fv(GC3Duint index, Float32Array* values);
274 void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
275 void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
276 GC3Dsizei stride, GC3Dintptr offset, ExceptionCode&);
278 void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
280 // WEBKIT_lose_context support
281 enum LostContextMode {
282 // Lost context occurred at the graphics system level.
285 // Lost context provoked by WEBKIT_lose_context.
288 void forceLostContext(LostContextMode);
289 void forceRestoreContext();
291 GraphicsContext3D* graphicsContext3D() const { return m_context.get(); }
292 #if USE(ACCELERATED_COMPOSITING)
293 virtual PlatformLayer* platformLayer() const { return m_context->platformLayer(); }
296 void reshape(int width, int height);
298 void markLayerComposited();
299 virtual void paintRenderingResultsToCanvas();
300 virtual PassRefPtr<ImageData> paintRenderingResultsToImageData();
302 void removeObject(WebGLObject*);
304 unsigned getMaxVertexAttribs() const { return m_maxVertexAttribs; }
307 friend class WebGLObject;
308 friend class OESVertexArrayObject;
310 WebGLRenderingContext(HTMLCanvasElement*, PassRefPtr<GraphicsContext3D>, GraphicsContext3D::Attributes);
311 void initializeNewContext();
314 void addObject(WebGLObject*);
315 void detachAndRemoveAllObjects();
317 void markContextChanged();
318 void cleanupAfterGraphicsCall(bool changed)
321 markContextChanged();
324 // Query whether it is built on top of compliant GLES2 implementation.
325 bool isGLES2Compliant() { return m_isGLES2Compliant; }
326 // Query if the GL implementation is NPOT strict.
327 bool isGLES2NPOTStrict() { return m_isGLES2NPOTStrict; }
328 // Query if the GL implementation generates errors on out-of-bounds buffer accesses.
329 bool isErrorGeneratedOnOutOfBoundsAccesses() { return m_isErrorGeneratedOnOutOfBoundsAccesses; }
330 // Query if the GL implementation initializes textures/renderbuffers to 0.
331 bool isResourceSafe() { return m_isResourceSafe; }
332 // Query if depth_stencil buffer is supported.
333 bool isDepthStencilSupported() { return m_isDepthStencilSupported; }
335 // Helper to return the size in bytes of OpenGL data types
336 // like GL_FLOAT, GL_INT, etc.
337 unsigned int sizeInBytes(GC3Denum type);
339 // Basic validation of count and offset against number of elements in element array buffer
340 bool validateElementArraySize(GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
342 // Conservative but quick index validation
343 bool validateIndexArrayConservative(GC3Denum type, int& numElementsRequired);
345 // Precise but slow index validation -- only done if conservative checks fail
346 bool validateIndexArrayPrecise(GC3Dsizei count, GC3Denum type, GC3Dintptr offset, int& numElementsRequired);
347 // If numElements <= 0, we only check if each enabled vertex attribute is bound to a buffer.
348 bool validateRenderingState(int numElements);
350 bool validateWebGLObject(WebGLObject*);
353 PassRefPtr<Image> videoFrameToImage(HTMLVideoElement*, ExceptionCode&);
356 RefPtr<GraphicsContext3D> m_context;
358 class WebGLRenderingContextRestoreTimer : public TimerBase {
360 explicit WebGLRenderingContextRestoreTimer(WebGLRenderingContext* context) : m_context(context) { }
362 virtual void fired();
363 WebGLRenderingContext* m_context;
366 bool m_restoreAllowed;
367 WebGLRenderingContextRestoreTimer m_restoreTimer;
370 bool m_markedCanvasDirty;
371 HashSet<WebGLObject*> m_canvasObjects;
373 // List of bound VBO's. Used to maintain info about sizes for ARRAY_BUFFER and stored values for ELEMENT_ARRAY_BUFFER
374 RefPtr<WebGLBuffer> m_boundArrayBuffer;
376 RefPtr<WebGLVertexArrayObjectOES> m_defaultVertexArrayObject;
377 RefPtr<WebGLVertexArrayObjectOES> m_boundVertexArrayObject;
378 void setBoundVertexArrayObject(PassRefPtr<WebGLVertexArrayObjectOES> arrayObject)
381 m_boundVertexArrayObject = arrayObject;
383 m_boundVertexArrayObject = m_defaultVertexArrayObject;
386 class VertexAttribValue {
403 Vector<VertexAttribValue> m_vertexAttribValue;
404 unsigned m_maxVertexAttribs;
405 RefPtr<WebGLBuffer> m_vertexAttrib0Buffer;
406 long m_vertexAttrib0BufferSize;
407 GC3Dfloat m_vertexAttrib0BufferValue[4];
408 bool m_forceAttrib0BufferRefill;
409 bool m_vertexAttrib0UsedBefore;
411 RefPtr<WebGLProgram> m_currentProgram;
412 RefPtr<WebGLFramebuffer> m_framebufferBinding;
413 RefPtr<WebGLRenderbuffer> m_renderbufferBinding;
414 class TextureUnitState {
416 RefPtr<WebGLTexture> m_texture2DBinding;
417 RefPtr<WebGLTexture> m_textureCubeMapBinding;
419 Vector<TextureUnitState> m_textureUnits;
420 unsigned long m_activeTextureUnit;
422 RefPtr<WebGLTexture> m_blackTexture2D;
423 RefPtr<WebGLTexture> m_blackTextureCubeMap;
425 // Fixed-size cache of reusable image buffers for video texImage2D calls.
426 class LRUImageBufferCache {
428 LRUImageBufferCache(int capacity);
429 // The pointer returned is owned by the image buffer map.
430 ImageBuffer* imageBuffer(const IntSize& size);
432 void bubbleToFront(int idx);
433 OwnArrayPtr<OwnPtr<ImageBuffer> > m_buffers;
436 LRUImageBufferCache m_videoCache;
438 GC3Dint m_maxTextureSize;
439 GC3Dint m_maxCubeMapTextureSize;
440 GC3Dint m_maxRenderbufferSize;
441 GC3Dint m_maxViewportDims[2];
442 GC3Dint m_maxTextureLevel;
443 GC3Dint m_maxCubeMapTextureLevel;
445 GC3Dint m_packAlignment;
446 GC3Dint m_unpackAlignment;
448 bool m_unpackPremultiplyAlpha;
449 GC3Denum m_unpackColorspaceConversion;
451 GraphicsContext3D::Attributes m_attributes;
454 GC3Dfloat m_clearColor[4];
455 bool m_scissorEnabled;
456 GC3Dfloat m_clearDepth;
457 GC3Dint m_clearStencil;
458 GC3Dboolean m_colorMask[4];
459 GC3Dboolean m_depthMask;
462 GC3Duint m_stencilMask, m_stencilMaskBack;
463 GC3Dint m_stencilFuncRef, m_stencilFuncRefBack; // Note that these are the user specified values, not the internal clamped value.
464 GC3Duint m_stencilFuncMask, m_stencilFuncMaskBack;
466 bool m_isGLES2Compliant;
467 bool m_isGLES2NPOTStrict;
468 bool m_isErrorGeneratedOnOutOfBoundsAccesses;
469 bool m_isResourceSafe;
470 bool m_isDepthStencilSupported;
472 // Enabled extension objects.
473 OwnPtr<OESTextureFloat> m_oesTextureFloat;
474 OwnPtr<OESStandardDerivatives> m_oesStandardDerivatives;
475 OwnPtr<OESVertexArrayObject> m_oesVertexArrayObject;
476 OwnPtr<WebKitLoseContext> m_webkitLoseContext;
478 // Helpers for getParameter and others
479 WebGLGetInfo getBooleanParameter(GC3Denum);
480 WebGLGetInfo getBooleanArrayParameter(GC3Denum);
481 WebGLGetInfo getFloatParameter(GC3Denum);
482 WebGLGetInfo getIntParameter(GC3Denum);
483 WebGLGetInfo getUnsignedIntParameter(GC3Denum);
484 WebGLGetInfo getWebGLFloatArrayParameter(GC3Denum);
485 WebGLGetInfo getWebGLIntArrayParameter(GC3Denum);
487 // Clear the backbuffer if it was composited since the last operation.
488 // clearMask is set to the bitfield of any clear that would happen anyway at this time
489 // and the function returns true if that clear is now unnecessary.
490 bool clearIfComposited(GC3Dbitfield clearMask = 0);
492 void texImage2DBase(GC3Denum target, GC3Dint level, GC3Denum internalformat,
493 GC3Dsizei width, GC3Dsizei height, GC3Dint border,
494 GC3Denum format, GC3Denum type, void* pixels, ExceptionCode&);
495 void texImage2DImpl(GC3Denum target, GC3Dint level, GC3Denum internalformat,
496 GC3Denum format, GC3Denum type, Image*,
497 bool flipY, bool premultiplyAlpha, ExceptionCode&);
498 void texSubImage2DBase(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
499 GC3Dsizei width, GC3Dsizei height,
500 GC3Denum format, GC3Denum type, void* pixels, ExceptionCode&);
501 void texSubImage2DImpl(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
502 GC3Denum format, GC3Denum type,
503 Image* image, bool flipY, bool premultiplyAlpha, ExceptionCode&);
505 void handleNPOTTextures(bool prepareToDraw);
507 void createFallbackBlackTextures1x1();
509 // Helper function for copyTex{Sub}Image, check whether the internalformat
510 // and the color buffer format of the current bound framebuffer combination
512 bool isTexInternalFormatColorBufferCombinationValid(GC3Denum texInternalFormat,
513 GC3Denum colorBufferFormat);
515 // Helper function to get the bound framebuffer's color buffer format.
516 GC3Denum getBoundFramebufferColorFormat();
518 // Helper function to get the bound framebuffer's width.
519 int getBoundFramebufferWidth();
521 // Helper function to get the bound framebuffer's height.
522 int getBoundFramebufferHeight();
524 // Helper function to check if size is non-negative.
525 // Generate GL error and return false for negative inputs; otherwise, return true.
526 bool validateSize(GC3Dint x, GC3Dint y);
528 // Helper function to check if all characters in the string belong to the
529 // ASCII subset as defined in GLSL ES 1.0 spec section 3.1.
530 bool validateString(const String&);
532 // Helper function to check target and texture bound to the target.
533 // Generate GL errors and return 0 if target is invalid or texture bound is
534 // null. Otherwise, return the texture bound to the target.
535 WebGLTexture* validateTextureBinding(GC3Denum target, bool useSixEnumsForCubeMap);
537 // Helper function to check input format/type for functions {copy}Tex{Sub}Image.
538 // Generates GL error and returns false if parameters are invalid.
539 bool validateTexFuncFormatAndType(GC3Denum format, GC3Denum type);
541 // Helper function to check input level for functions {copy}Tex{Sub}Image.
542 // Generates GL error and returns false if level is invalid.
543 bool validateTexFuncLevel(GC3Denum target, GC3Dint level);
545 // Helper function to check input parameters for functions {copy}Tex{Sub}Image.
546 // Generates GL error and returns false if parameters are invalid.
547 bool validateTexFuncParameters(GC3Denum target, GC3Dint level,
548 GC3Denum internalformat,
549 GC3Dsizei width, GC3Dsizei height, GC3Dint border,
550 GC3Denum format, GC3Denum type);
552 // Helper function to validate that the given ArrayBufferView
553 // is of the correct type and contains enough data for the texImage call.
554 // Generates GL error and returns false if parameters are invalid.
555 bool validateTexFuncData(GC3Dsizei width, GC3Dsizei height,
556 GC3Denum format, GC3Denum type,
557 ArrayBufferView* pixels);
559 // Helper function to validate mode for draw{Arrays/Elements}.
560 bool validateDrawMode(GC3Denum);
562 // Helper function to validate if front/back stencilMask and stencilFunc settings are the same.
563 bool validateStencilSettings();
565 // Helper function to validate stencil func.
566 bool validateStencilFunc(GC3Denum);
568 // Helper function for texParameterf and texParameteri.
569 void texParameter(GC3Denum target, GC3Denum pname, GC3Dfloat parami, GC3Dint paramf, bool isFloat);
571 // Helper function to print warnings to console. Currently
572 // used only to warn about use of obsolete functions.
573 void printWarningToConsole(const String&);
575 // Helper function to validate input parameters for framebuffer functions.
576 // Generate GL error if parameters are illegal.
577 bool validateFramebufferFuncParameters(GC3Denum target, GC3Denum attachment);
579 // Helper function to validate blend equation mode.
580 bool validateBlendEquation(GC3Denum);
582 // Helper function to validate blend func factors.
583 bool validateBlendFuncFactors(GC3Denum src, GC3Denum dst);
585 // Helper function to validate a GL capability.
586 bool validateCapability(GC3Denum);
588 // Helper function to validate input parameters for uniform functions.
589 bool validateUniformParameters(const WebGLUniformLocation*, Float32Array*, GC3Dsizei mod);
590 bool validateUniformParameters(const WebGLUniformLocation*, Int32Array*, GC3Dsizei mod);
591 bool validateUniformParameters(const WebGLUniformLocation*, void*, GC3Dsizei size, GC3Dsizei mod);
592 bool validateUniformMatrixParameters(const WebGLUniformLocation*, GC3Dboolean transpose, Float32Array*, GC3Dsizei mod);
593 bool validateUniformMatrixParameters(const WebGLUniformLocation*, GC3Dboolean transpose, void*, GC3Dsizei size, GC3Dsizei mod);
595 // Helper function to validate parameters for bufferData.
596 // Return the current bound buffer to target, or 0 if parameters are invalid.
597 WebGLBuffer* validateBufferDataParameters(GC3Denum target, GC3Denum usage);
599 // Helper function for tex{Sub}Image2D to make sure image is ready.
600 bool validateHTMLImageElement(HTMLImageElement*);
602 // Helper functions for vertexAttribNf{v}.
603 void vertexAttribfImpl(GC3Duint index, GC3Dsizei expectedSize, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat);
604 void vertexAttribfvImpl(GC3Duint index, Float32Array*, GC3Dsizei expectedSize);
605 void vertexAttribfvImpl(GC3Duint index, GC3Dfloat*, GC3Dsizei size, GC3Dsizei expectedSize);
607 // Helper function for delete* (deleteBuffer, deleteProgram, etc) functions.
608 // Return false if caller should return without further processing.
609 bool deleteObject(WebGLObject*);
611 // Helper function for bind* (bindBuffer, bindTexture, etc) and useProgram.
612 // If the object has already been deleted, set deleted to true upon return.
613 // Return false if caller should return without further processing.
614 bool checkObjectToBeBound(WebGLObject*, bool& deleted);
616 // Helpers for simulating vertexAttrib0
617 void initVertexAttrib0();
618 bool simulateVertexAttrib0(GC3Dsizei numVertex);
619 void restoreStatesAfterVertexAttrib0Simulation();
622 // Helper for restoration after context lost.
623 void maybeRestoreContext(LostContextMode);
625 friend class WebGLStateRestorer;
628 } // namespace WebCore