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
41namespace WebCore {
42
43class WebGLActiveInfo;
44class WebGLBuffer;
45class WebGLContextAttributes;
46class WebGLExtension;
47class WebGLFramebuffer;
48class WebGLObject;
49class WebGLProgram;
50class WebGLRenderbuffer;
51class WebGLShader;
52class WebGLTexture;
53class WebGLUniformLocation;
54class WebKitLoseContext;
55class HTMLImageElement;
56class HTMLVideoElement;
57class ImageBuffer;
58class ImageData;
59class IntSize;
60class OESStandardDerivatives;
61class OESTextureFloat;
62class OESVertexArrayObject;
63class WebGLVertexArrayObjectOES;
64
65class WebGLRenderingContext : public CanvasRenderingContext {
66public:
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    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
281    GraphicsContext3D* graphicsContext3D() const { return m_context.get(); }
282#if USE(ACCELERATED_COMPOSITING)
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
294    unsigned getMaxVertexAttribs() const { return m_maxVertexAttribs; }
295
296    // Helpers for JSC bindings.
297    int getNumberOfExtensions();
298    WebGLExtension* getExtensionNumber(int i);
299
300  private:
301    friend class WebGLObject;
302    friend class OESVertexArrayObject;
303
304    WebGLRenderingContext(HTMLCanvasElement*, PassRefPtr<GraphicsContext3D>, GraphicsContext3D::Attributes);
305    void initializeNewContext();
306    void setupFlags();
307
308    void addObject(WebGLObject*);
309    void detachAndRemoveAllObjects();
310    WebGLTexture* findTexture(Platform3DObject);
311    WebGLRenderbuffer* findRenderbuffer(Platform3DObject);
312    WebGLBuffer* findBuffer(Platform3DObject);
313    WebGLShader* findShader(Platform3DObject);
314
315    void markContextChanged();
316    void cleanupAfterGraphicsCall(bool changed)
317    {
318        if (changed)
319            markContextChanged();
320    }
321
322    // Query whether it is built on top of compliant GLES2 implementation.
323    bool isGLES2Compliant() { return m_isGLES2Compliant; }
324    // Query if the GL implementation is NPOT strict.
325    bool isGLES2NPOTStrict() { return m_isGLES2NPOTStrict; }
326    // Query if the GL implementation generates errors on out-of-bounds buffer accesses.
327    bool isErrorGeneratedOnOutOfBoundsAccesses() { return m_isErrorGeneratedOnOutOfBoundsAccesses; }
328    // Query if the GL implementation initializes textures/renderbuffers to 0.
329    bool isResourceSafe() { return m_isResourceSafe; }
330    // Query if depth_stencil buffer is supported.
331    bool isDepthStencilSupported() { return m_isDepthStencilSupported; }
332
333    // Helper to return the size in bytes of OpenGL data types
334    // like GL_FLOAT, GL_INT, etc.
335    unsigned int sizeInBytes(GC3Denum type);
336
337    // Basic validation of count and offset against number of elements in element array buffer
338    bool validateElementArraySize(GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
339
340    // Conservative but quick index validation
341    bool validateIndexArrayConservative(GC3Denum type, int& numElementsRequired);
342
343    // Precise but slow index validation -- only done if conservative checks fail
344    bool validateIndexArrayPrecise(GC3Dsizei count, GC3Denum type, GC3Dintptr offset, int& numElementsRequired);
345    // If numElements <= 0, we only check if each enabled vertex attribute is bound to a buffer.
346    bool validateRenderingState(int numElements);
347
348    bool validateWebGLObject(WebGLObject*);
349
350#if ENABLE(VIDEO)
351    PassRefPtr<Image> videoFrameToImage(HTMLVideoElement*);
352#endif
353
354    RefPtr<GraphicsContext3D> m_context;
355
356    class WebGLRenderingContextRestoreTimer : public TimerBase {
357    public:
358        WebGLRenderingContextRestoreTimer(WebGLRenderingContext* context) : m_context(context) { }
359    private:
360        virtual void fired();
361        WebGLRenderingContext* m_context;
362    };
363
364    WebGLRenderingContextRestoreTimer m_restoreTimer;
365
366    bool m_needsUpdate;
367    bool m_markedCanvasDirty;
368    HashSet<RefPtr<WebGLObject> > m_canvasObjects;
369
370    // List of bound VBO's. Used to maintain info about sizes for ARRAY_BUFFER and stored values for ELEMENT_ARRAY_BUFFER
371    RefPtr<WebGLBuffer> m_boundArrayBuffer;
372
373    RefPtr<WebGLVertexArrayObjectOES> m_defaultVertexArrayObject;
374    RefPtr<WebGLVertexArrayObjectOES> m_boundVertexArrayObject;
375    void setBoundVertexArrayObject(PassRefPtr<WebGLVertexArrayObjectOES> arrayObject)
376    {
377        if (arrayObject)
378            m_boundVertexArrayObject = arrayObject;
379        else
380            m_boundVertexArrayObject = m_defaultVertexArrayObject;
381    }
382
383    class VertexAttribValue {
384    public:
385        VertexAttribValue()
386        {
387            initValue();
388        }
389
390        void initValue()
391        {
392            value[0] = 0.0f;
393            value[1] = 0.0f;
394            value[2] = 0.0f;
395            value[3] = 1.0f;
396        }
397
398        GC3Dfloat value[4];
399    };
400    Vector<VertexAttribValue> m_vertexAttribValue;
401    unsigned m_maxVertexAttribs;
402    RefPtr<WebGLBuffer> m_vertexAttrib0Buffer;
403    long m_vertexAttrib0BufferSize;
404    GC3Dfloat m_vertexAttrib0BufferValue[4];
405    bool m_forceAttrib0BufferRefill;
406    bool m_vertexAttrib0UsedBefore;
407
408    RefPtr<WebGLProgram> m_currentProgram;
409    RefPtr<WebGLFramebuffer> m_framebufferBinding;
410    RefPtr<WebGLRenderbuffer> m_renderbufferBinding;
411    class TextureUnitState {
412    public:
413        RefPtr<WebGLTexture> m_texture2DBinding;
414        RefPtr<WebGLTexture> m_textureCubeMapBinding;
415    };
416    Vector<TextureUnitState> m_textureUnits;
417    unsigned long m_activeTextureUnit;
418
419    RefPtr<WebGLTexture> m_blackTexture2D;
420    RefPtr<WebGLTexture> m_blackTextureCubeMap;
421
422    // Fixed-size cache of reusable image buffers for video texImage2D calls.
423    class LRUImageBufferCache {
424    public:
425        LRUImageBufferCache(int capacity);
426        // The pointer returned is owned by the image buffer map.
427        ImageBuffer* imageBuffer(const IntSize& size);
428    private:
429        void bubbleToFront(int idx);
430        OwnArrayPtr<OwnPtr<ImageBuffer> > m_buffers;
431        int m_capacity;
432    };
433    LRUImageBufferCache m_videoCache;
434
435    GC3Dint m_maxTextureSize;
436    GC3Dint m_maxCubeMapTextureSize;
437    GC3Dint m_maxTextureLevel;
438    GC3Dint m_maxCubeMapTextureLevel;
439
440    GC3Dint m_packAlignment;
441    GC3Dint m_unpackAlignment;
442    bool m_unpackFlipY;
443    bool m_unpackPremultiplyAlpha;
444    GC3Denum m_unpackColorspaceConversion;
445    bool m_contextLost;
446    GraphicsContext3D::Attributes m_attributes;
447
448    bool m_layerCleared;
449    GC3Dfloat m_clearColor[4];
450    bool m_scissorEnabled;
451    GC3Dfloat m_clearDepth;
452    GC3Dint m_clearStencil;
453    GC3Dboolean m_colorMask[4];
454
455    long m_stencilBits;
456    GC3Duint m_stencilMask, m_stencilMaskBack;
457    GC3Dint m_stencilFuncRef, m_stencilFuncRefBack; // Note that these are the user specified values, not the internal clamped value.
458    GC3Duint m_stencilFuncMask, m_stencilFuncMaskBack;
459
460    bool m_isGLES2Compliant;
461    bool m_isGLES2NPOTStrict;
462    bool m_isErrorGeneratedOnOutOfBoundsAccesses;
463    bool m_isResourceSafe;
464    bool m_isDepthStencilSupported;
465
466    // Enabled extension objects.
467    RefPtr<OESTextureFloat> m_oesTextureFloat;
468    RefPtr<OESStandardDerivatives> m_oesStandardDerivatives;
469    RefPtr<OESVertexArrayObject> m_oesVertexArrayObject;
470    RefPtr<WebKitLoseContext> m_webkitLoseContext;
471
472    // Helpers for getParameter and others
473    WebGLGetInfo getBooleanParameter(GC3Denum);
474    WebGLGetInfo getBooleanArrayParameter(GC3Denum);
475    WebGLGetInfo getFloatParameter(GC3Denum);
476    WebGLGetInfo getIntParameter(GC3Denum);
477    WebGLGetInfo getUnsignedIntParameter(GC3Denum);
478    WebGLGetInfo getWebGLFloatArrayParameter(GC3Denum);
479    WebGLGetInfo getWebGLIntArrayParameter(GC3Denum);
480
481    // Clear the backbuffer if it was composited since the last operation.
482    // clearMask is set to the bitfield of any clear that would happen anyway at this time
483    // and the function returns true if that clear is now unnecessary.
484    bool clearIfComposited(GC3Dbitfield clearMask = 0);
485
486    void texImage2DBase(GC3Denum target, GC3Dint level, GC3Denum internalformat,
487                        GC3Dsizei width, GC3Dsizei height, GC3Dint border,
488                        GC3Denum format, GC3Denum type, void* pixels, ExceptionCode&);
489    void texImage2DImpl(GC3Denum target, GC3Dint level, GC3Denum internalformat,
490                        GC3Denum format, GC3Denum type, Image*,
491                        bool flipY, bool premultiplyAlpha, ExceptionCode&);
492    void texSubImage2DBase(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
493                           GC3Dsizei width, GC3Dsizei height,
494                           GC3Denum format, GC3Denum type, void* pixels, ExceptionCode&);
495    void texSubImage2DImpl(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
496                           GC3Denum format, GC3Denum type,
497                           Image* image, bool flipY, bool premultiplyAlpha, ExceptionCode&);
498
499    void handleNPOTTextures(bool prepareToDraw);
500
501    void createFallbackBlackTextures1x1();
502
503    // Helper function for copyTex{Sub}Image, check whether the internalformat
504    // and the color buffer format of the current bound framebuffer combination
505    // is valid.
506    bool isTexInternalFormatColorBufferCombinationValid(GC3Denum texInternalFormat,
507                                                        GC3Denum colorBufferFormat);
508
509    // Helper function to get the bound framebuffer's color buffer format.
510    GC3Denum getBoundFramebufferColorFormat();
511
512    // Helper function to get the bound framebuffer's width.
513    int getBoundFramebufferWidth();
514
515    // Helper function to get the bound framebuffer's height.
516    int getBoundFramebufferHeight();
517
518    // Helper function to check if size is non-negative.
519    // Generate GL error and return false for negative inputs; otherwise, return true.
520    bool validateSize(GC3Dint x, GC3Dint y);
521
522    // Helper function to check if all characters in the string belong to the
523    // ASCII subset as defined in GLSL ES 1.0 spec section 3.1.
524    bool validateString(const String&);
525
526    // Helper function to check target and texture bound to the target.
527    // Generate GL errors and return 0 if target is invalid or texture bound is
528    // null.  Otherwise, return the texture bound to the target.
529    WebGLTexture* validateTextureBinding(GC3Denum target, bool useSixEnumsForCubeMap);
530
531    // Helper function to check input format/type for functions {copy}Tex{Sub}Image.
532    // Generates GL error and returns false if parameters are invalid.
533    bool validateTexFuncFormatAndType(GC3Denum format, GC3Denum type);
534
535    // Helper function to check input level for functions {copy}Tex{Sub}Image.
536    // Generates GL error and returns false if level is invalid.
537    bool validateTexFuncLevel(GC3Denum target, GC3Dint level);
538
539    // Helper function to check input parameters for functions {copy}Tex{Sub}Image.
540    // Generates GL error and returns false if parameters are invalid.
541    bool validateTexFuncParameters(GC3Denum target, GC3Dint level,
542                                   GC3Denum internalformat,
543                                   GC3Dsizei width, GC3Dsizei height, GC3Dint border,
544                                   GC3Denum format, GC3Denum type);
545
546    // Helper function to validate that the given ArrayBufferView
547    // is of the correct type and contains enough data for the texImage call.
548    // Generates GL error and returns false if parameters are invalid.
549    bool validateTexFuncData(GC3Dsizei width, GC3Dsizei height,
550                             GC3Denum format, GC3Denum type,
551                             ArrayBufferView* pixels);
552
553    // Helper function to validate mode for draw{Arrays/Elements}.
554    bool validateDrawMode(GC3Denum);
555
556    // Helper function to validate if front/back stencilMask and stencilFunc settings are the same.
557    bool validateStencilSettings();
558
559    // Helper function to validate stencil func.
560    bool validateStencilFunc(GC3Denum);
561
562    // Helper function for texParameterf and texParameteri.
563    void texParameter(GC3Denum target, GC3Denum pname, GC3Dfloat parami, GC3Dint paramf, bool isFloat);
564
565    // Helper function to print warnings to console. Currently
566    // used only to warn about use of obsolete functions.
567    void printWarningToConsole(const String&);
568
569    // Helper function to validate input parameters for framebuffer functions.
570    // Generate GL error if parameters are illegal.
571    bool validateFramebufferFuncParameters(GC3Denum target, GC3Denum attachment);
572
573    // Helper function to validate blend equation mode.
574    bool validateBlendEquation(GC3Denum);
575
576    // Helper function to validate blend func factors.
577    bool validateBlendFuncFactors(GC3Denum src, GC3Denum dst);
578
579    // Helper function to validate a GL capability.
580    bool validateCapability(GC3Denum);
581
582    // Helper function to validate input parameters for uniform functions.
583    bool validateUniformParameters(const WebGLUniformLocation*, Float32Array*, GC3Dsizei mod);
584    bool validateUniformParameters(const WebGLUniformLocation*, Int32Array*, GC3Dsizei mod);
585    bool validateUniformParameters(const WebGLUniformLocation*, void*, GC3Dsizei size, GC3Dsizei mod);
586    bool validateUniformMatrixParameters(const WebGLUniformLocation*, GC3Dboolean transpose, Float32Array*, GC3Dsizei mod);
587    bool validateUniformMatrixParameters(const WebGLUniformLocation*, GC3Dboolean transpose, void*, GC3Dsizei size, GC3Dsizei mod);
588
589    // Helper function to validate parameters for bufferData.
590    // Return the current bound buffer to target, or 0 if parameters are invalid.
591    WebGLBuffer* validateBufferDataParameters(GC3Denum target, GC3Denum usage);
592
593    // Helper function for tex{Sub}Image2D to make sure image is ready.
594    bool validateHTMLImageElement(HTMLImageElement*);
595
596    // Helper functions for vertexAttribNf{v}.
597    void vertexAttribfImpl(GC3Duint index, GC3Dsizei expectedSize, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat);
598    void vertexAttribfvImpl(GC3Duint index, Float32Array*, GC3Dsizei expectedSize);
599    void vertexAttribfvImpl(GC3Duint index, GC3Dfloat*, GC3Dsizei size, GC3Dsizei expectedSize);
600
601    // Helper function for delete* (deleteBuffer, deleteProgram, etc) functions.
602    // Return false if caller should return without further processing.
603    bool deleteObject(WebGLObject*);
604
605    // Helper function for bind* (bindBuffer, bindTexture, etc) and useProgram.
606    // If the object has already been deleted, set deleted to true upon return.
607    // Return false if caller should return without further processing.
608    bool checkObjectToBeBound(WebGLObject*, bool& deleted);
609
610    // Helpers for simulating vertexAttrib0
611    void initVertexAttrib0();
612    bool simulateVertexAttrib0(GC3Dsizei numVertex);
613    void restoreStatesAfterVertexAttrib0Simulation();
614
615    friend class WebGLStateRestorer;
616};
617
618} // namespace WebCore
619
620#endif
621