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