initial import
[vuplus_webkit] / Source / WebCore / html / canvas / WebGLRenderingContext.h
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #ifndef 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
70     virtual bool is3d() const { return true; }
71     virtual bool isAccelerated() const { return true; }
72     virtual bool paintsIntoCanvasBuffer() const;
73
74     int drawingBufferWidth() const;
75     int drawingBufferHeight() const;
76
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);
89
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&);
95
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&);
103
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);
106
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);
109
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();
116
117     void cullFace(GC3Denum mode);
118
119     void deleteBuffer(WebGLBuffer*);
120     void deleteFramebuffer(WebGLFramebuffer*);
121     void deleteProgram(WebGLProgram*);
122     void deleteRenderbuffer(WebGLRenderbuffer*);
123     void deleteShader(WebGLShader*);
124     void deleteTexture(WebGLTexture*);
125
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&);
134
135     void enable(GC3Denum cap);
136     void enableVertexAttribArray(GC3Duint index, ExceptionCode&);
137     void finish();
138     void flush();
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);
143
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();
150     GC3Denum getError();
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&);
159
160     // TBD
161     // void glGetShaderPrecisionFormat (GC3Denum shadertype, GC3Denum precisiontype, GC3Dint* range, GC3Dint* precision);
162
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);
170
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*);
180
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);
197
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&);
207 #if ENABLE(VIDEO)
208     void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
209                     GC3Denum format, GC3Denum type, HTMLVideoElement*, ExceptionCode&);
210 #endif
211
212     void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
213     void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
214
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&);
224 #if ENABLE(VIDEO)
225     void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
226                        GC3Denum format, GC3Denum type, HTMLVideoElement*, ExceptionCode&);
227 #endif
228
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&);
259
260     void useProgram(WebGLProgram*, ExceptionCode&);
261     void validateProgram(WebGLProgram*, ExceptionCode&);
262
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&);
277
278     void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
279
280     // WEBKIT_lose_context support
281     enum LostContextMode {
282         // Lost context occurred at the graphics system level.
283         RealLostContext,
284
285         // Lost context provoked by WEBKIT_lose_context.
286         SyntheticLostContext
287     };
288     void forceLostContext(LostContextMode);
289     void forceRestoreContext();
290
291     GraphicsContext3D* graphicsContext3D() const { return m_context.get(); }
292 #if USE(ACCELERATED_COMPOSITING)
293     virtual PlatformLayer* platformLayer() const { return m_context->platformLayer(); }
294 #endif
295
296     void reshape(int width, int height);
297
298     void markLayerComposited();
299     virtual void paintRenderingResultsToCanvas();
300     virtual PassRefPtr<ImageData> paintRenderingResultsToImageData();
301
302     void removeObject(WebGLObject*);
303     
304     unsigned getMaxVertexAttribs() const { return m_maxVertexAttribs; }
305
306   private:
307     friend class WebGLObject;
308     friend class OESVertexArrayObject;
309
310     WebGLRenderingContext(HTMLCanvasElement*, PassRefPtr<GraphicsContext3D>, GraphicsContext3D::Attributes);
311     void initializeNewContext();
312     void setupFlags();
313
314     void addObject(WebGLObject*);
315     void detachAndRemoveAllObjects();
316
317     void markContextChanged();
318     void cleanupAfterGraphicsCall(bool changed)
319     {
320         if (changed)
321             markContextChanged();
322     }
323
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; }
334
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);
338
339     // Basic validation of count and offset against number of elements in element array buffer
340     bool validateElementArraySize(GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
341
342     // Conservative but quick index validation
343     bool validateIndexArrayConservative(GC3Denum type, int& numElementsRequired);
344
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);
349
350     bool validateWebGLObject(WebGLObject*);
351
352 #if ENABLE(VIDEO)
353     PassRefPtr<Image> videoFrameToImage(HTMLVideoElement*, ExceptionCode&);
354 #endif
355
356     RefPtr<GraphicsContext3D> m_context;
357
358     class WebGLRenderingContextRestoreTimer : public TimerBase {
359     public:
360         explicit WebGLRenderingContextRestoreTimer(WebGLRenderingContext* context) : m_context(context) { }
361     private:
362         virtual void fired();
363         WebGLRenderingContext* m_context;
364     };
365
366     bool m_restoreAllowed;
367     WebGLRenderingContextRestoreTimer m_restoreTimer;
368
369     bool m_needsUpdate;
370     bool m_markedCanvasDirty;
371     HashSet<WebGLObject*> m_canvasObjects;
372
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;
375     
376     RefPtr<WebGLVertexArrayObjectOES> m_defaultVertexArrayObject;
377     RefPtr<WebGLVertexArrayObjectOES> m_boundVertexArrayObject;
378     void setBoundVertexArrayObject(PassRefPtr<WebGLVertexArrayObjectOES> arrayObject)
379     {
380         if (arrayObject)
381             m_boundVertexArrayObject = arrayObject;
382         else
383             m_boundVertexArrayObject = m_defaultVertexArrayObject;
384     }
385     
386     class VertexAttribValue {
387     public:
388         VertexAttribValue()
389         {
390             initValue();
391         }
392         
393         void initValue()
394         {
395             value[0] = 0.0f;
396             value[1] = 0.0f;
397             value[2] = 0.0f;
398             value[3] = 1.0f;
399         }
400         
401         GC3Dfloat value[4];
402     };
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;
410
411     RefPtr<WebGLProgram> m_currentProgram;
412     RefPtr<WebGLFramebuffer> m_framebufferBinding;
413     RefPtr<WebGLRenderbuffer> m_renderbufferBinding;
414     class TextureUnitState {
415     public:
416         RefPtr<WebGLTexture> m_texture2DBinding;
417         RefPtr<WebGLTexture> m_textureCubeMapBinding;
418     };
419     Vector<TextureUnitState> m_textureUnits;
420     unsigned long m_activeTextureUnit;
421
422     RefPtr<WebGLTexture> m_blackTexture2D;
423     RefPtr<WebGLTexture> m_blackTextureCubeMap;
424
425     // Fixed-size cache of reusable image buffers for video texImage2D calls.
426     class LRUImageBufferCache {
427     public:
428         LRUImageBufferCache(int capacity);
429         // The pointer returned is owned by the image buffer map.
430         ImageBuffer* imageBuffer(const IntSize& size);
431     private:
432         void bubbleToFront(int idx);
433         OwnArrayPtr<OwnPtr<ImageBuffer> > m_buffers;
434         int m_capacity;
435     };
436     LRUImageBufferCache m_videoCache;
437
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;
444
445     GC3Dint m_packAlignment;
446     GC3Dint m_unpackAlignment;
447     bool m_unpackFlipY;
448     bool m_unpackPremultiplyAlpha;
449     GC3Denum m_unpackColorspaceConversion;
450     bool m_contextLost;
451     GraphicsContext3D::Attributes m_attributes;
452
453     bool m_layerCleared;
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;
460
461     long m_stencilBits;
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;
465
466     bool m_isGLES2Compliant;
467     bool m_isGLES2NPOTStrict;
468     bool m_isErrorGeneratedOnOutOfBoundsAccesses;
469     bool m_isResourceSafe;
470     bool m_isDepthStencilSupported;
471
472     // Enabled extension objects.
473     OwnPtr<OESTextureFloat> m_oesTextureFloat;
474     OwnPtr<OESStandardDerivatives> m_oesStandardDerivatives;
475     OwnPtr<OESVertexArrayObject> m_oesVertexArrayObject;
476     OwnPtr<WebKitLoseContext> m_webkitLoseContext;
477
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);
486
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);
491
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&);
504
505     void handleNPOTTextures(bool prepareToDraw);
506
507     void createFallbackBlackTextures1x1();
508
509     // Helper function for copyTex{Sub}Image, check whether the internalformat
510     // and the color buffer format of the current bound framebuffer combination
511     // is valid.
512     bool isTexInternalFormatColorBufferCombinationValid(GC3Denum texInternalFormat,
513                                                         GC3Denum colorBufferFormat);
514
515     // Helper function to get the bound framebuffer's color buffer format.
516     GC3Denum getBoundFramebufferColorFormat();
517
518     // Helper function to get the bound framebuffer's width.
519     int getBoundFramebufferWidth();
520
521     // Helper function to get the bound framebuffer's height.
522     int getBoundFramebufferHeight();
523
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);
527
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&);
531
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);
536
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);
540
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);
544
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);
551
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);
558
559     // Helper function to validate mode for draw{Arrays/Elements}.
560     bool validateDrawMode(GC3Denum);
561
562     // Helper function to validate if front/back stencilMask and stencilFunc settings are the same.
563     bool validateStencilSettings();
564
565     // Helper function to validate stencil func.
566     bool validateStencilFunc(GC3Denum);
567
568     // Helper function for texParameterf and texParameteri.
569     void texParameter(GC3Denum target, GC3Denum pname, GC3Dfloat parami, GC3Dint paramf, bool isFloat);
570
571     // Helper function to print warnings to console. Currently
572     // used only to warn about use of obsolete functions.
573     void printWarningToConsole(const String&);
574
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);
578
579     // Helper function to validate blend equation mode.
580     bool validateBlendEquation(GC3Denum);
581
582     // Helper function to validate blend func factors.
583     bool validateBlendFuncFactors(GC3Denum src, GC3Denum dst);
584
585     // Helper function to validate a GL capability.
586     bool validateCapability(GC3Denum);
587
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);
594
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);
598
599     // Helper function for tex{Sub}Image2D to make sure image is ready.
600     bool validateHTMLImageElement(HTMLImageElement*);
601
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);
606
607     // Helper function for delete* (deleteBuffer, deleteProgram, etc) functions.
608     // Return false if caller should return without further processing.
609     bool deleteObject(WebGLObject*);
610
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);
615
616     // Helpers for simulating vertexAttrib0
617     void initVertexAttrib0();
618     bool simulateVertexAttrib0(GC3Dsizei numVertex);
619     void restoreStatesAfterVertexAttrib0Simulation();
620
621     void loseContext();
622     // Helper for restoration after context lost.
623     void maybeRestoreContext(LostContextMode);
624
625     friend class WebGLStateRestorer;
626 };
627
628 } // namespace WebCore
629
630 #endif