Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / canvas / WebGLRenderingContextBase.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 WebGLRenderingContextBase_h
27 #define WebGLRenderingContextBase_h
28
29 #include "bindings/core/v8/Nullable.h"
30 #include "core/dom/ActiveDOMObject.h"
31 #include "core/dom/DOMTypedArray.h"
32 #include "core/html/canvas/CanvasRenderingContext.h"
33 #include "core/html/canvas/WebGLExtensionName.h"
34 #include "core/html/canvas/WebGLGetInfo.h"
35 #include "core/page/Page.h"
36 #include "core/rendering/RenderBoxModelObject.h"
37 #include "platform/Timer.h"
38 #include "platform/graphics/GraphicsTypes3D.h"
39 #include "platform/graphics/ImageBuffer.h"
40 #include "platform/graphics/gpu/DrawingBuffer.h"
41 #include "platform/graphics/gpu/Extensions3DUtil.h"
42 #include "platform/graphics/gpu/WebGLImageConversion.h"
43 #include "public/platform/WebGraphicsContext3D.h"
44 #include "wtf/OwnPtr.h"
45 #include "wtf/text/WTFString.h"
46
47 namespace blink {
48 class WebLayer;
49 }
50
51 namespace blink {
52
53 class ANGLEInstancedArrays;
54 class EXTBlendMinMax;
55 class EXTFragDepth;
56 class EXTShaderTextureLOD;
57 class EXTsRGB;
58 class EXTTextureFilterAnisotropic;
59 class ExceptionState;
60 class HTMLImageElement;
61 class HTMLVideoElement;
62 class ImageBuffer;
63 class ImageData;
64 class IntSize;
65 class OESElementIndexUint;
66 class OESStandardDerivatives;
67 class OESTextureFloat;
68 class OESTextureFloatLinear;
69 class OESTextureHalfFloat;
70 class OESTextureHalfFloatLinear;
71 class OESVertexArrayObject;
72 class WebGLActiveInfo;
73 class WebGLBuffer;
74 class WebGLCompressedTextureATC;
75 class WebGLCompressedTextureETC1;
76 class WebGLCompressedTexturePVRTC;
77 class WebGLCompressedTextureS3TC;
78 class WebGLContextAttributes;
79 class WebGLContextGroup;
80 class WebGLContextObject;
81 class WebGLDebugRendererInfo;
82 class WebGLDebugShaders;
83 class WebGLDepthTexture;
84 class WebGLDrawBuffers;
85 class WebGLExtension;
86 class WebGLFramebuffer;
87 class WebGLLoseContext;
88 class WebGLObject;
89 class WebGLProgram;
90 class WebGLRenderbuffer;
91 class WebGLShader;
92 class WebGLShaderPrecisionFormat;
93 class WebGLSharedObject;
94 class WebGLSharedWebGraphicsContext3D;
95 class WebGLTexture;
96 class WebGLUniformLocation;
97 class WebGLVertexArrayObjectOES;
98
99 class WebGLRenderingContextLostCallback;
100 class WebGLRenderingContextErrorMessageCallback;
101
102 class WebGLRenderingContextBase: public CanvasRenderingContext, public ActiveDOMObject, public Page::MultisamplingChangedObserver {
103     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WebGLRenderingContextBase);
104 public:
105     virtual ~WebGLRenderingContextBase();
106
107     virtual unsigned version() const = 0;
108     virtual String contextName() const = 0;
109     virtual void registerContextExtensions() = 0;
110
111     static unsigned getWebGLVersion(const CanvasRenderingContext*);
112
113     int drawingBufferWidth() const;
114     int drawingBufferHeight() const;
115
116     void activeTexture(GLenum texture);
117     void attachShader(WebGLProgram*, WebGLShader*);
118     void bindAttribLocation(WebGLProgram*, GLuint index, const String& name);
119     void bindBuffer(GLenum target, WebGLBuffer*);
120     void bindFramebuffer(GLenum target, WebGLFramebuffer*);
121     void bindRenderbuffer(GLenum target, WebGLRenderbuffer*);
122     void bindTexture(GLenum target, WebGLTexture*);
123     void blendColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
124     void blendEquation(GLenum mode);
125     void blendEquationSeparate(GLenum modeRGB, GLenum modeAlpha);
126     void blendFunc(GLenum sfactor, GLenum dfactor);
127     void blendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha);
128
129     void bufferData(GLenum target, long long size, GLenum usage);
130     void bufferData(GLenum target, DOMArrayBuffer* data, GLenum usage);
131     void bufferData(GLenum target, DOMArrayBufferView* data, GLenum usage);
132     void bufferSubData(GLenum target, long long offset, DOMArrayBuffer* data);
133     void bufferSubData(GLenum target, long long offset, DOMArrayBufferView* data);
134
135     GLenum checkFramebufferStatus(GLenum target);
136     void clear(GLbitfield mask);
137     void clearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
138     void clearDepth(GLfloat);
139     void clearStencil(GLint);
140     void colorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
141     void compileShader(WebGLShader*);
142
143     void compressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, DOMArrayBufferView* data);
144     void compressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, DOMArrayBufferView* data);
145
146     void copyTexImage2D(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
147     void copyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height);
148
149     PassRefPtrWillBeRawPtr<WebGLBuffer> createBuffer();
150     PassRefPtrWillBeRawPtr<WebGLFramebuffer> createFramebuffer();
151     PassRefPtrWillBeRawPtr<WebGLProgram> createProgram();
152     PassRefPtrWillBeRawPtr<WebGLRenderbuffer> createRenderbuffer();
153     PassRefPtrWillBeRawPtr<WebGLShader> createShader(GLenum type);
154     PassRefPtrWillBeRawPtr<WebGLTexture> createTexture();
155
156     void cullFace(GLenum mode);
157
158     void deleteBuffer(WebGLBuffer*);
159     void deleteFramebuffer(WebGLFramebuffer*);
160     void deleteProgram(WebGLProgram*);
161     void deleteRenderbuffer(WebGLRenderbuffer*);
162     void deleteShader(WebGLShader*);
163     void deleteTexture(WebGLTexture*);
164
165     void depthFunc(GLenum);
166     void depthMask(GLboolean);
167     void depthRange(GLfloat zNear, GLfloat zFar);
168     void detachShader(WebGLProgram*, WebGLShader*);
169     void disable(GLenum cap);
170     void disableVertexAttribArray(GLuint index);
171     void drawArrays(GLenum mode, GLint first, GLsizei count);
172     void drawElements(GLenum mode, GLsizei count, GLenum type, long long offset);
173
174     void drawArraysInstancedANGLE(GLenum mode, GLint first, GLsizei count, GLsizei primcount);
175     void drawElementsInstancedANGLE(GLenum mode, GLsizei count, GLenum type, long long offset, GLsizei primcount);
176
177     void enable(GLenum cap);
178     void enableVertexAttribArray(GLuint index);
179     void finish();
180     void flush();
181     void framebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, WebGLRenderbuffer*);
182     void framebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, WebGLTexture*, GLint level);
183     void frontFace(GLenum mode);
184     void generateMipmap(GLenum target);
185
186     PassRefPtrWillBeRawPtr<WebGLActiveInfo> getActiveAttrib(WebGLProgram*, GLuint index);
187     PassRefPtrWillBeRawPtr<WebGLActiveInfo> getActiveUniform(WebGLProgram*, GLuint index);
188     bool getAttachedShaders(WebGLProgram*, WillBeHeapVector<RefPtrWillBeMember<WebGLShader>>&);
189     Nullable<WillBeHeapVector<RefPtrWillBeMember<WebGLShader>>> getAttachedShaders(WebGLProgram*);
190     GLint getAttribLocation(WebGLProgram*, const String& name);
191     WebGLGetInfo getBufferParameter(GLenum target, GLenum pname);
192     PassRefPtrWillBeRawPtr<WebGLContextAttributes> getContextAttributes();
193     GLenum getError();
194     PassRefPtrWillBeRawPtr<WebGLExtension> getExtension(const String& name);
195     WebGLGetInfo getFramebufferAttachmentParameter(GLenum target, GLenum attachment, GLenum pname);
196     WebGLGetInfo getParameter(GLenum pname);
197     WebGLGetInfo getProgramParameter(WebGLProgram*, GLenum pname);
198     String getProgramInfoLog(WebGLProgram*);
199     WebGLGetInfo getRenderbufferParameter(GLenum target, GLenum pname);
200     WebGLGetInfo getShaderParameter(WebGLShader*, GLenum pname);
201     String getShaderInfoLog(WebGLShader*);
202     PassRefPtrWillBeRawPtr<WebGLShaderPrecisionFormat> getShaderPrecisionFormat(GLenum shaderType, GLenum precisionType);
203     String getShaderSource(WebGLShader*);
204     Nullable<Vector<String>> getSupportedExtensions();
205     WebGLGetInfo getTexParameter(GLenum target, GLenum pname);
206     WebGLGetInfo getUniform(WebGLProgram*, const WebGLUniformLocation*);
207     PassRefPtrWillBeRawPtr<WebGLUniformLocation> getUniformLocation(WebGLProgram*, const String&);
208     WebGLGetInfo getVertexAttrib(GLuint index, GLenum pname);
209     long long getVertexAttribOffset(GLuint index, GLenum pname);
210
211     void hint(GLenum target, GLenum mode);
212     GLboolean isBuffer(WebGLBuffer*);
213     bool isContextLost() const;
214     GLboolean isEnabled(GLenum cap);
215     GLboolean isFramebuffer(WebGLFramebuffer*);
216     GLboolean isProgram(WebGLProgram*);
217     GLboolean isRenderbuffer(WebGLRenderbuffer*);
218     GLboolean isShader(WebGLShader*);
219     GLboolean isTexture(WebGLTexture*);
220
221     void lineWidth(GLfloat);
222     void linkProgram(WebGLProgram*);
223     void pixelStorei(GLenum pname, GLint param);
224     void polygonOffset(GLfloat factor, GLfloat units);
225     void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, DOMArrayBufferView* pixels);
226     void renderbufferStorage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
227     void sampleCoverage(GLfloat value, GLboolean invert);
228     void scissor(GLint x, GLint y, GLsizei width, GLsizei height);
229     void shaderSource(WebGLShader*, const String&);
230     void stencilFunc(GLenum func, GLint ref, GLuint mask);
231     void stencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask);
232     void stencilMask(GLuint);
233     void stencilMaskSeparate(GLenum face, GLuint mask);
234     void stencilOp(GLenum fail, GLenum zfail, GLenum zpass);
235     void stencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);
236
237     void texImage2D(GLenum target, GLint level, GLenum internalformat,
238         GLsizei width, GLsizei height, GLint border,
239         GLenum format, GLenum type, DOMArrayBufferView*, ExceptionState&);
240     void texImage2D(GLenum target, GLint level, GLenum internalformat,
241         GLenum format, GLenum type, ImageData*, ExceptionState&);
242     void texImage2D(GLenum target, GLint level, GLenum internalformat,
243         GLenum format, GLenum type, HTMLImageElement*, ExceptionState&);
244     void texImage2D(GLenum target, GLint level, GLenum internalformat,
245         GLenum format, GLenum type, HTMLCanvasElement*, ExceptionState&);
246     void texImage2D(GLenum target, GLint level, GLenum internalformat,
247         GLenum format, GLenum type, HTMLVideoElement*, ExceptionState&);
248
249     void texParameterf(GLenum target, GLenum pname, GLfloat param);
250     void texParameteri(GLenum target, GLenum pname, GLint param);
251
252     void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
253         GLsizei width, GLsizei height,
254         GLenum format, GLenum type, DOMArrayBufferView*, ExceptionState&);
255     void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
256         GLenum format, GLenum type, ImageData*, ExceptionState&);
257     void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
258         GLenum format, GLenum type, HTMLImageElement*, ExceptionState&);
259     void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
260         GLenum format, GLenum type, HTMLCanvasElement*, ExceptionState&);
261     void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
262         GLenum format, GLenum type, HTMLVideoElement*, ExceptionState&);
263
264     void uniform1f(const WebGLUniformLocation*, GLfloat x);
265     void uniform1fv(const WebGLUniformLocation*, DOMFloat32Array* v);
266     void uniform1fv(const WebGLUniformLocation*, GLfloat* v, GLsizei);
267     void uniform1i(const WebGLUniformLocation*, GLint x);
268     void uniform1iv(const WebGLUniformLocation*, DOMInt32Array* v);
269     void uniform1iv(const WebGLUniformLocation*, GLint* v, GLsizei);
270     void uniform2f(const WebGLUniformLocation*, GLfloat x, GLfloat y);
271     void uniform2fv(const WebGLUniformLocation*, DOMFloat32Array* v);
272     void uniform2fv(const WebGLUniformLocation*, GLfloat* v, GLsizei);
273     void uniform2i(const WebGLUniformLocation*, GLint x, GLint y);
274     void uniform2iv(const WebGLUniformLocation*, DOMInt32Array* v);
275     void uniform2iv(const WebGLUniformLocation*, GLint* v, GLsizei);
276     void uniform3f(const WebGLUniformLocation*, GLfloat x, GLfloat y, GLfloat z);
277     void uniform3fv(const WebGLUniformLocation*, DOMFloat32Array* v);
278     void uniform3fv(const WebGLUniformLocation*, GLfloat* v, GLsizei);
279     void uniform3i(const WebGLUniformLocation*, GLint x, GLint y, GLint z);
280     void uniform3iv(const WebGLUniformLocation*, DOMInt32Array* v);
281     void uniform3iv(const WebGLUniformLocation*, GLint* v, GLsizei);
282     void uniform4f(const WebGLUniformLocation*, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
283     void uniform4fv(const WebGLUniformLocation*, DOMFloat32Array* v);
284     void uniform4fv(const WebGLUniformLocation*, GLfloat* v, GLsizei);
285     void uniform4i(const WebGLUniformLocation*, GLint x, GLint y, GLint z, GLint w);
286     void uniform4iv(const WebGLUniformLocation*, DOMInt32Array* v);
287     void uniform4iv(const WebGLUniformLocation*, GLint* v, GLsizei);
288     void uniformMatrix2fv(const WebGLUniformLocation*, GLboolean transpose, DOMFloat32Array* value);
289     void uniformMatrix2fv(const WebGLUniformLocation*, GLboolean transpose, GLfloat* value, GLsizei);
290     void uniformMatrix3fv(const WebGLUniformLocation*, GLboolean transpose, DOMFloat32Array* value);
291     void uniformMatrix3fv(const WebGLUniformLocation*, GLboolean transpose, GLfloat* value, GLsizei);
292     void uniformMatrix4fv(const WebGLUniformLocation*, GLboolean transpose, DOMFloat32Array* value);
293     void uniformMatrix4fv(const WebGLUniformLocation*, GLboolean transpose, GLfloat* value, GLsizei);
294
295     void useProgram(WebGLProgram*);
296     void validateProgram(WebGLProgram*);
297
298     void vertexAttrib1f(GLuint index, GLfloat x);
299     void vertexAttrib1fv(GLuint index, DOMFloat32Array* values);
300     void vertexAttrib1fv(GLuint index, GLfloat* values, GLsizei);
301     void vertexAttrib2f(GLuint index, GLfloat x, GLfloat y);
302     void vertexAttrib2fv(GLuint index, DOMFloat32Array* values);
303     void vertexAttrib2fv(GLuint index, GLfloat* values, GLsizei);
304     void vertexAttrib3f(GLuint index, GLfloat x, GLfloat y, GLfloat z);
305     void vertexAttrib3fv(GLuint index, DOMFloat32Array* values);
306     void vertexAttrib3fv(GLuint index, GLfloat* values, GLsizei);
307     void vertexAttrib4f(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
308     void vertexAttrib4fv(GLuint index, DOMFloat32Array* values);
309     void vertexAttrib4fv(GLuint index, GLfloat* values, GLsizei);
310     void vertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized,
311         GLsizei stride, long long offset);
312
313     void vertexAttribDivisorANGLE(GLuint index, GLuint divisor);
314
315     void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
316
317     // WEBGL_lose_context support
318     enum LostContextMode {
319         NotLostContext,
320
321         // Lost context occurred at the graphics system level.
322         RealLostContext,
323
324         // Lost context provoked by WEBGL_lose_context.
325         WebGLLoseContextLostContext,
326
327         // Lost context occurred due to internal implementation reasons.
328         SyntheticLostContext,
329     };
330     enum AutoRecoveryMethod {
331         // Don't restore automatically.
332         Manual,
333
334         // Restore when resources are available.
335         WhenAvailable,
336
337         // Restore as soon as possible.
338         Auto
339     };
340     void forceLostContext(LostContextMode, AutoRecoveryMethod);
341     void forceRestoreContext();
342     void loseContextImpl(LostContextMode, AutoRecoveryMethod);
343
344     blink::WebGraphicsContext3D* webContext() const { return drawingBuffer()->context(); }
345     WebGLContextGroup* contextGroup() const { return m_contextGroup.get(); }
346     Extensions3DUtil* extensionsUtil();
347
348     void reshape(int width, int height);
349
350     void markLayerComposited();
351     PassRefPtrWillBeRawPtr<ImageData> paintRenderingResultsToImageData();
352
353     void removeSharedObject(WebGLSharedObject*);
354     void removeContextObject(WebGLContextObject*);
355
356     unsigned maxVertexAttribs() const { return m_maxVertexAttribs; }
357
358     // ActiveDOMObject notifications
359     virtual bool hasPendingActivity() const override;
360     virtual void stop() override;
361
362     void setSavingImage(bool isSaving) { m_savingImage = isSaving; }
363
364     virtual void trace(Visitor*) override;
365
366     class TextureUnitState {
367         ALLOW_ONLY_INLINE_ALLOCATION();
368     public:
369         RefPtrWillBeMember<WebGLTexture> m_texture2DBinding;
370         RefPtrWillBeMember<WebGLTexture> m_textureCubeMapBinding;
371
372         void trace(Visitor*);
373     };
374
375 protected:
376     friend class WebGLDrawBuffers;
377     friend class WebGLFramebuffer;
378     friend class WebGLObject;
379     friend class WebGLContextObject;
380     friend class OESVertexArrayObject;
381     friend class WebGLDebugShaders;
382     friend class WebGLCompressedTextureATC;
383     friend class WebGLCompressedTextureETC1;
384     friend class WebGLCompressedTexturePVRTC;
385     friend class WebGLCompressedTextureS3TC;
386     friend class WebGLRenderingContextErrorMessageCallback;
387     friend class WebGLVertexArrayObjectOES;
388     friend class ScopedTexture2DRestorer;
389
390     WebGLRenderingContextBase(HTMLCanvasElement*, PassOwnPtr<blink::WebGraphicsContext3D>, WebGLContextAttributes*);
391     PassRefPtr<DrawingBuffer> createDrawingBuffer(PassOwnPtr<blink::WebGraphicsContext3D>);
392     void initializeNewContext();
393     void setupFlags();
394
395 #if ENABLE(OILPAN)
396     PassRefPtr<WebGLSharedWebGraphicsContext3D> sharedWebGraphicsContext3D() const;
397 #endif
398
399     // CanvasRenderingContext implementation.
400     virtual bool is3d() const override { return true; }
401     virtual bool isAccelerated() const override { return true; }
402     virtual void setIsHidden(bool) override;
403     virtual void paintRenderingResultsToCanvas() override;
404     virtual blink::WebLayer* platformLayer() const override;
405
406     void addSharedObject(WebGLSharedObject*);
407     void addContextObject(WebGLContextObject*);
408     void detachAndRemoveAllObjects();
409
410     void destroyContext();
411     void markContextChanged(ContentChangeType);
412
413     // Query if the GL implementation is NPOT strict.
414     bool isGLES2NPOTStrict() { return m_isGLES2NPOTStrict; }
415     // Query if depth_stencil buffer is supported.
416     bool isDepthStencilSupported() { return m_isDepthStencilSupported; }
417
418     // Helper to return the size in bytes of OpenGL data types
419     // like GL_FLOAT, GL_INT, etc.
420     unsigned sizeInBytes(GLenum type);
421
422     // Check if each enabled vertex attribute is bound to a buffer.
423     bool validateRenderingState(const char*);
424
425     bool validateWebGLObject(const char*, WebGLObject*);
426
427     // Adds a compressed texture format.
428     void addCompressedTextureFormat(GLenum);
429     void removeAllCompressedTextureFormats();
430
431     PassRefPtr<Image> drawImageIntoBuffer(Image*, int width, int height, const char* functionName);
432
433     PassRefPtr<Image> videoFrameToImage(HTMLVideoElement*, BackingStoreCopy);
434
435     WebGLRenderbuffer* ensureEmulatedStencilBuffer(GLenum target, WebGLRenderbuffer*);
436
437     // Structure for rendering to a DrawingBuffer, instead of directly
438     // to the back-buffer of m_context.
439 #if ENABLE(OILPAN)
440     RefPtr<WebGLSharedWebGraphicsContext3D> m_sharedWebGraphicsContext3D;
441 #else
442     RefPtr<DrawingBuffer> m_drawingBuffer;
443 #endif
444     DrawingBuffer* drawingBuffer() const;
445
446     RefPtr<WebGLContextGroup> m_contextGroup;
447
448     LostContextMode m_contextLostMode;
449     AutoRecoveryMethod m_autoRecoveryMethod;
450     // Dispatches a context lost event once it is determined that one is needed.
451     // This is used for synthetic, WEBGL_lose_context and real context losses. For real ones, it's
452     // likely that there's no JavaScript on the stack, but that might be dependent
453     // on how exactly the platform discovers that the context was lost. For better
454     // portability we always defer the dispatch of the event.
455     Timer<WebGLRenderingContextBase> m_dispatchContextLostEventTimer;
456     bool m_restoreAllowed;
457     Timer<WebGLRenderingContextBase> m_restoreTimer;
458
459     bool m_markedCanvasDirty;
460     WillBeHeapHashSet<RawPtrWillBeWeakMember<WebGLContextObject>> m_contextObjects;
461
462     OwnPtrWillBeMember<WebGLRenderingContextLostCallback> m_contextLostCallbackAdapter;
463     OwnPtrWillBeMember<WebGLRenderingContextErrorMessageCallback> m_errorMessageCallbackAdapter;
464
465     // List of bound VBO's. Used to maintain info about sizes for ARRAY_BUFFER and stored values for ELEMENT_ARRAY_BUFFER
466     RefPtrWillBeMember<WebGLBuffer> m_boundArrayBuffer;
467
468     RefPtrWillBeMember<WebGLVertexArrayObjectOES> m_defaultVertexArrayObject;
469     RefPtrWillBeMember<WebGLVertexArrayObjectOES> m_boundVertexArrayObject;
470     void setBoundVertexArrayObject(PassRefPtrWillBeRawPtr<WebGLVertexArrayObjectOES> arrayObject)
471     {
472         if (arrayObject)
473             m_boundVertexArrayObject = arrayObject;
474         else
475             m_boundVertexArrayObject = m_defaultVertexArrayObject;
476     }
477
478     class VertexAttribValue {
479     public:
480         VertexAttribValue()
481         {
482             initValue();
483         }
484
485         void initValue()
486         {
487             value[0] = 0.0f;
488             value[1] = 0.0f;
489             value[2] = 0.0f;
490             value[3] = 1.0f;
491         }
492
493         GLfloat value[4];
494     };
495     Vector<VertexAttribValue> m_vertexAttribValue;
496     unsigned m_maxVertexAttribs;
497     RefPtrWillBeMember<WebGLBuffer> m_vertexAttrib0Buffer;
498     long m_vertexAttrib0BufferSize;
499     GLfloat m_vertexAttrib0BufferValue[4];
500     bool m_forceAttrib0BufferRefill;
501     bool m_vertexAttrib0UsedBefore;
502
503     RefPtrWillBeMember<WebGLProgram> m_currentProgram;
504     RefPtrWillBeMember<WebGLFramebuffer> m_framebufferBinding;
505     RefPtrWillBeMember<WebGLRenderbuffer> m_renderbufferBinding;
506
507     WillBeHeapVector<TextureUnitState> m_textureUnits;
508     unsigned long m_activeTextureUnit;
509
510     RefPtrWillBeMember<WebGLTexture> m_blackTexture2D;
511     RefPtrWillBeMember<WebGLTexture> m_blackTextureCubeMap;
512
513     Vector<GLenum> m_compressedTextureFormats;
514
515     // Fixed-size cache of reusable image buffers for video texImage2D calls.
516     class LRUImageBufferCache {
517     public:
518         LRUImageBufferCache(int capacity);
519         // The pointer returned is owned by the image buffer map.
520         ImageBuffer* imageBuffer(const IntSize& size);
521     private:
522         void bubbleToFront(int idx);
523         OwnPtr<OwnPtr<ImageBuffer>[]> m_buffers;
524         int m_capacity;
525     };
526     LRUImageBufferCache m_generatedImageCache;
527
528     GLint m_maxTextureSize;
529     GLint m_maxCubeMapTextureSize;
530     GLint m_maxRenderbufferSize;
531     GLint m_maxViewportDims[2];
532     GLint m_maxTextureLevel;
533     GLint m_maxCubeMapTextureLevel;
534
535     GLint m_maxDrawBuffers;
536     GLint m_maxColorAttachments;
537     GLenum m_backDrawBuffer;
538     bool m_drawBuffersWebGLRequirementsChecked;
539     bool m_drawBuffersSupported;
540
541     GLint m_packAlignment;
542     GLint m_unpackAlignment;
543     bool m_unpackFlipY;
544     bool m_unpackPremultiplyAlpha;
545     GLenum m_unpackColorspaceConversion;
546     RefPtrWillBeMember<WebGLContextAttributes> m_requestedAttributes;
547
548     bool m_layerCleared;
549     GLfloat m_clearColor[4];
550     bool m_scissorEnabled;
551     GLfloat m_clearDepth;
552     GLint m_clearStencil;
553     GLboolean m_colorMask[4];
554     GLboolean m_depthMask;
555
556     bool m_stencilEnabled;
557     GLuint m_stencilMask, m_stencilMaskBack;
558     GLint m_stencilFuncRef, m_stencilFuncRefBack; // Note that these are the user specified values, not the internal clamped value.
559     GLuint m_stencilFuncMask, m_stencilFuncMaskBack;
560
561     bool m_isGLES2NPOTStrict;
562     bool m_isDepthStencilSupported;
563
564     bool m_synthesizedErrorsToConsole;
565     int m_numGLErrorsToConsoleAllowed;
566
567     bool m_multisamplingAllowed;
568     bool m_multisamplingObserverRegistered;
569
570     unsigned long m_onePlusMaxNonDefaultTextureUnit;
571
572     OwnPtr<Extensions3DUtil> m_extensionsUtil;
573
574     bool m_savingImage;
575
576     enum ExtensionFlags {
577         ApprovedExtension               = 0x00,
578         // Extension that is behind the draft extensions runtime flag:
579         DraftExtension                  = 0x01,
580     };
581
582     class ExtensionTracker : public NoBaseWillBeGarbageCollected<ExtensionTracker> {
583     public:
584         ExtensionTracker(ExtensionFlags flags, const char* const* prefixes)
585             : m_draft(flags & DraftExtension)
586             , m_prefixes(prefixes)
587         {
588         }
589
590 #if !ENABLE(OILPAN)
591         virtual ~ExtensionTracker()
592         {
593         }
594 #endif
595
596         bool draft() const
597         {
598             return m_draft;
599         }
600
601         const char* const* prefixes() const;
602         bool matchesNameWithPrefixes(const String&) const;
603
604         virtual PassRefPtrWillBeRawPtr<WebGLExtension> getExtension(WebGLRenderingContextBase*) = 0;
605         virtual bool supported(WebGLRenderingContextBase*) const = 0;
606         virtual const char* extensionName() const = 0;
607         virtual void loseExtension() = 0;
608
609         virtual void trace(Visitor*) { }
610
611     private:
612         bool m_draft;
613         const char* const* m_prefixes;
614     };
615
616     template <typename T>
617     class TypedExtensionTracker final : public ExtensionTracker {
618     public:
619         static PassOwnPtrWillBeRawPtr<TypedExtensionTracker<T>> create(RefPtrWillBeMember<T>& extensionField, ExtensionFlags flags, const char* const* prefixes)
620         {
621             return adoptPtrWillBeNoop(new TypedExtensionTracker<T>(extensionField, flags, prefixes));
622         }
623
624 #if !ENABLE(OILPAN)
625         virtual ~TypedExtensionTracker()
626         {
627             if (m_extension) {
628                 m_extension->lose(true);
629                 m_extension = nullptr;
630             }
631         }
632 #endif
633
634         virtual PassRefPtrWillBeRawPtr<WebGLExtension> getExtension(WebGLRenderingContextBase* context) override
635         {
636             if (!m_extension) {
637                 m_extension = T::create(context);
638                 m_extensionField = m_extension;
639             }
640
641             return m_extension;
642         }
643
644         virtual bool supported(WebGLRenderingContextBase* context) const override
645         {
646             return T::supported(context);
647         }
648
649         virtual const char* extensionName() const override
650         {
651             return T::extensionName();
652         }
653
654         virtual void loseExtension() override
655         {
656             if (m_extension) {
657                 m_extension->lose(false);
658                 if (m_extension->isLost())
659                     m_extension = nullptr;
660             }
661         }
662
663         virtual void trace(Visitor* visitor) override
664         {
665             visitor->trace(m_extension);
666             ExtensionTracker::trace(visitor);
667         }
668
669     private:
670         TypedExtensionTracker(RefPtrWillBeMember<T>& extensionField, ExtensionFlags flags, const char* const* prefixes)
671             : ExtensionTracker(flags, prefixes)
672             , m_extensionField(extensionField)
673         {
674         }
675
676         RefPtrWillBeMember<T>& m_extensionField;
677         // ExtensionTracker holds it's own reference to the extension to ensure
678         // that it is not deleted before this object's destructor is called
679         RefPtrWillBeMember<T> m_extension;
680     };
681
682     bool m_extensionEnabled[WebGLExtensionNameCount];
683     WillBeHeapVector<OwnPtrWillBeMember<ExtensionTracker>> m_extensions;
684
685     template <typename T>
686     void registerExtension(RefPtrWillBeMember<T>& extensionPtr, ExtensionFlags flags = ApprovedExtension, const char* const* prefixes = 0)
687     {
688         m_extensions.append(TypedExtensionTracker<T>::create(extensionPtr, flags, prefixes));
689     }
690
691     bool extensionSupportedAndAllowed(const ExtensionTracker*);
692
693     inline bool extensionEnabled(WebGLExtensionName name)
694     {
695         return m_extensionEnabled[name];
696     }
697
698     // Errors raised by synthesizeGLError() while the context is lost.
699     Vector<GLenum> m_lostContextErrors;
700
701     // Helpers for getParameter and others
702     WebGLGetInfo getBooleanParameter(GLenum);
703     WebGLGetInfo getBooleanArrayParameter(GLenum);
704     WebGLGetInfo getFloatParameter(GLenum);
705     WebGLGetInfo getIntParameter(GLenum);
706     WebGLGetInfo getUnsignedIntParameter(GLenum);
707     WebGLGetInfo getWebGLFloatArrayParameter(GLenum);
708     WebGLGetInfo getWebGLIntArrayParameter(GLenum);
709
710     // Clear the backbuffer if it was composited since the last operation.
711     // clearMask is set to the bitfield of any clear that would happen anyway at this time
712     // and the function returns true if that clear is now unnecessary.
713     bool clearIfComposited(GLbitfield clearMask = 0);
714
715     // Helper to restore state that clearing the framebuffer may destroy.
716     void restoreStateAfterClear();
717
718     // Convert texture internal format.
719     GLenum convertTexInternalFormat(GLenum internalformat, GLenum type);
720
721     void texImage2DBase(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* pixels, ExceptionState&);
722     void texImage2DImpl(GLenum target, GLint level, GLenum internalformat, GLenum format, GLenum type, Image*, WebGLImageConversion::ImageHtmlDomSource, bool flipY, bool premultiplyAlpha, ExceptionState&);
723     void texSubImage2DBase(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* pixels, ExceptionState&);
724     void texSubImage2DImpl(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLenum format, GLenum type, Image*, WebGLImageConversion::ImageHtmlDomSource, bool flipY, bool premultiplyAlpha, ExceptionState&);
725
726     void handleTextureCompleteness(const char*, bool);
727     void createFallbackBlackTextures1x1();
728
729     // Helper function for copyTex{Sub}Image, check whether the internalformat
730     // and the color buffer format of the current bound framebuffer combination
731     // is valid.
732     bool isTexInternalFormatColorBufferCombinationValid(GLenum texInternalFormat, GLenum colorBufferFormat);
733
734     // Helper function to get the bound framebuffer's color buffer format.
735     GLenum boundFramebufferColorFormat();
736
737     // Helper function to verify limits on the length of uniform and attribute locations.
738     bool validateLocationLength(const char* functionName, const String&);
739
740     // Helper function to check if size is non-negative.
741     // Generate GL error and return false for negative inputs; otherwise, return true.
742     bool validateSize(const char* functionName, GLint x, GLint y);
743
744     // Helper function to check if all characters in the string belong to the
745     // ASCII subset as defined in GLSL ES 1.0 spec section 3.1.
746     bool validateString(const char* functionName, const String&);
747
748     // Helper function to check target and texture bound to the target.
749     // Generate GL errors and return 0 if target is invalid or texture bound is
750     // null.  Otherwise, return the texture bound to the target.
751     WebGLTexture* validateTextureBinding(const char* functionName, GLenum target, bool useSixEnumsForCubeMap);
752
753     // Helper function to check input format/type for functions {copy}Tex{Sub}Image.
754     // Generates GL error and returns false if parameters are invalid.
755     bool validateTexFuncFormatAndType(const char* functionName, GLenum format, GLenum type, GLint level);
756
757     // Helper function to check input level for functions {copy}Tex{Sub}Image.
758     // Generates GL error and returns false if level is invalid.
759     bool validateTexFuncLevel(const char* functionName, GLenum target, GLint level);
760
761     // Helper function to check if a 64-bit value is non-negative and can fit into a 32-bit integer.
762     // Generates GL error and returns false if not.
763     bool validateValueFitNonNegInt32(const char* functionName, const char* paramName, long long value);
764
765     enum TexFuncValidationFunctionType {
766         NotTexSubImage2D,
767         TexSubImage2D,
768     };
769
770     enum TexFuncValidationSourceType {
771         SourceArrayBufferView,
772         SourceImageData,
773         SourceHTMLImageElement,
774         SourceHTMLCanvasElement,
775         SourceHTMLVideoElement,
776     };
777
778     // Helper function for tex{Sub}Image2D to check if the input format/type/level/target/width/height/border/xoffset/yoffset are valid.
779     // Otherwise, it would return quickly without doing other work.
780     bool validateTexFunc(const char* functionName, TexFuncValidationFunctionType, TexFuncValidationSourceType, GLenum target, GLint level, GLenum internalformat, GLsizei width,
781         GLsizei height, GLint border, GLenum format, GLenum type, GLint xoffset, GLint yoffset);
782
783     // Helper function to check input width and height for functions {copy, compressed}Tex{Sub}Image.
784     // Generates GL error and returns false if width or height is invalid.
785     bool validateTexFuncDimensions(const char* functionName, TexFuncValidationFunctionType, GLenum target, GLint level, GLsizei width, GLsizei height);
786
787     // Helper function to check input parameters for functions {copy}Tex{Sub}Image.
788     // Generates GL error and returns false if parameters are invalid.
789     bool validateTexFuncParameters(const char* functionName, TexFuncValidationFunctionType, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type);
790
791     enum NullDisposition {
792         NullAllowed,
793         NullNotAllowed
794     };
795
796     // Helper function to validate that the given ArrayBufferView
797     // is of the correct type and contains enough data for the texImage call.
798     // Generates GL error and returns false if parameters are invalid.
799     bool validateTexFuncData(const char* functionName, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, DOMArrayBufferView* pixels, NullDisposition);
800
801     // Helper function to validate a given texture format is settable as in
802     // you can supply data to texImage2D, or call texImage2D, copyTexImage2D and
803     // copyTexSubImage2D.
804     // Generates GL error and returns false if the format is not settable.
805     bool validateSettableTexFormat(const char* functionName, GLenum format);
806
807     // Helper function to validate compressed texture data is correct size
808     // for the given format and dimensions.
809     bool validateCompressedTexFuncData(const char* functionName, GLsizei width, GLsizei height, GLenum format, DOMArrayBufferView* pixels);
810
811     // Helper function for validating compressed texture formats.
812     bool validateCompressedTexFormat(GLenum format);
813
814     // Helper function to validate compressed texture dimensions are valid for
815     // the given format.
816     bool validateCompressedTexDimensions(const char* functionName, TexFuncValidationFunctionType, GLenum target, GLint level, GLsizei width, GLsizei height, GLenum format);
817
818     // Helper function to validate compressed texture dimensions are valid for
819     // the given format.
820     bool validateCompressedTexSubDimensions(const char* functionName, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, WebGLTexture*);
821
822     // Helper function to validate mode for draw{Arrays/Elements}.
823     bool validateDrawMode(const char* functionName, GLenum);
824
825     // Helper function to validate if front/back stencilMask and stencilFunc settings are the same.
826     bool validateStencilSettings(const char* functionName);
827
828     // Helper function to validate stencil or depth func.
829     bool validateStencilOrDepthFunc(const char* functionName, GLenum);
830
831     // Helper function for texParameterf and texParameteri.
832     void texParameter(GLenum target, GLenum pname, GLfloat parami, GLint paramf, bool isFloat);
833
834     // Helper function to print GL errors to console.
835     void printGLErrorToConsole(const String&);
836
837     // Helper function to print warnings to console. Currently
838     // used only to warn about use of obsolete functions.
839     void printWarningToConsole(const String&);
840
841     // Helper function to validate input parameters for framebuffer functions.
842     // Generate GL error if parameters are illegal.
843     bool validateFramebufferFuncParameters(const char* functionName, GLenum target, GLenum attachment);
844
845     // Helper function to validate blend equation mode.
846     bool validateBlendEquation(const char* functionName, GLenum);
847
848     // Helper function to validate blend func factors.
849     bool validateBlendFuncFactors(const char* functionName, GLenum src, GLenum dst);
850
851     // Helper function to validate a GL capability.
852     bool validateCapability(const char* functionName, GLenum);
853
854     // Helper function to validate input parameters for uniform functions.
855     bool validateUniformParameters(const char* functionName, const WebGLUniformLocation*, DOMFloat32Array*, GLsizei mod);
856     bool validateUniformParameters(const char* functionName, const WebGLUniformLocation*, DOMInt32Array*, GLsizei mod);
857     bool validateUniformParameters(const char* functionName, const WebGLUniformLocation*, void*, GLsizei, GLsizei mod);
858     bool validateUniformMatrixParameters(const char* functionName, const WebGLUniformLocation*, GLboolean transpose, DOMFloat32Array*, GLsizei mod);
859     bool validateUniformMatrixParameters(const char* functionName, const WebGLUniformLocation*, GLboolean transpose, void*, GLsizei, GLsizei mod);
860
861     // Helper function to validate the target for bufferData.
862     // Return the current bound buffer to target, or 0 if the target is invalid.
863     WebGLBuffer* validateBufferDataTarget(const char* functionName, GLenum target);
864
865     // Helper function for tex{Sub}Image2D to make sure image is ready and wouldn't taint Origin.
866     bool validateHTMLImageElement(const char* functionName, HTMLImageElement*, ExceptionState&);
867
868     // Helper function for tex{Sub}Image2D to make sure canvas is ready and wouldn't taint Origin.
869     bool validateHTMLCanvasElement(const char* functionName, HTMLCanvasElement*, ExceptionState&);
870
871     // Helper function for tex{Sub}Image2D to make sure video is ready wouldn't taint Origin.
872     bool validateHTMLVideoElement(const char* functionName, HTMLVideoElement*, ExceptionState&);
873
874     // Helper function to validate drawArrays(Instanced) calls
875     bool validateDrawArrays(const char* functionName, GLenum mode, GLint first, GLsizei count);
876
877     // Helper function to validate drawElements(Instanced) calls
878     bool validateDrawElements(const char* functionName, GLenum mode, GLsizei count, GLenum type, long long offset);
879
880     // Helper function to validate draw*Instanced calls
881     bool validateDrawInstanced(const char* functionName, GLsizei primcount);
882
883     // Helper functions for vertexAttribNf{v}.
884     void vertexAttribfImpl(const char* functionName, GLuint index, GLsizei expectedSize, GLfloat, GLfloat, GLfloat, GLfloat);
885     void vertexAttribfvImpl(const char* functionName, GLuint index, DOMFloat32Array*, GLsizei expectedSize);
886     void vertexAttribfvImpl(const char* functionName, GLuint index, GLfloat*, GLsizei, GLsizei expectedSize);
887
888     // Helper functions to bufferData() and bufferSubData().
889     void bufferDataImpl(GLenum target, long long size, const void* data, GLenum usage);
890     void bufferSubDataImpl(GLenum target, long long offset, GLsizeiptr size, const void* data);
891
892     // Helper function for delete* (deleteBuffer, deleteProgram, etc) functions.
893     // Return false if caller should return without further processing.
894     bool deleteObject(WebGLObject*);
895
896     // Helper function for bind* (bindBuffer, bindTexture, etc) and useProgram.
897     // If the object has already been deleted, set deleted to true upon return.
898     // Return false if caller should return without further processing.
899     bool checkObjectToBeBound(const char* functionName, WebGLObject*, bool& deleted);
900
901     void dispatchContextLostEvent(Timer<WebGLRenderingContextBase>*);
902     // Helper for restoration after context lost.
903     void maybeRestoreContext(Timer<WebGLRenderingContextBase>*);
904
905     enum ConsoleDisplayPreference {
906         DisplayInConsole,
907         DontDisplayInConsole
908     };
909
910     // Wrapper for WebGraphicsContext3D::synthesizeGLError that sends a message
911     // to the JavaScript console.
912     void synthesizeGLError(GLenum, const char* functionName, const char* description, ConsoleDisplayPreference = DisplayInConsole);
913     void emitGLWarning(const char* function, const char* reason);
914
915     String ensureNotNull(const String&) const;
916
917     // Enable or disable stencil test based on user setting and
918     // whether the current FBO has a stencil buffer.
919     void applyStencilTest();
920
921     // Helper for enabling or disabling a capability.
922     void enableOrDisable(GLenum capability, bool enable);
923
924     // Clamp the width and height to GL_MAX_VIEWPORT_DIMS.
925     IntSize clampedCanvasSize();
926
927     // First time called, if EXT_draw_buffers is supported, query the value; otherwise return 0.
928     // Later, return the cached value.
929     GLint maxDrawBuffers();
930     GLint maxColorAttachments();
931
932     void setBackDrawBuffer(GLenum);
933
934     void restoreCurrentFramebuffer();
935     void restoreCurrentTexture2D();
936
937     virtual void multisamplingChanged(bool) override;
938
939     void findNewMaxNonDefaultTextureUnit();
940
941     friend class WebGLStateRestorer;
942     friend class WebGLRenderingContextEvictionManager;
943
944     static Vector<WebGLRenderingContextBase*>& activeContexts();
945     static Vector<WebGLRenderingContextBase*>& forciblyEvictedContexts();
946
947     static void activateContext(WebGLRenderingContextBase*);
948     static void deactivateContext(WebGLRenderingContextBase*);
949     static void addToEvictedList(WebGLRenderingContextBase*);
950     static void removeFromEvictedList(WebGLRenderingContextBase*);
951     static void willDestroyContext(WebGLRenderingContextBase*);
952     static void forciblyLoseOldestContext(const String& reason);
953     // Return the least recently used context's position in the active context vector.
954     // If the vector is empty, return the maximum allowed active context number.
955     static size_t oldestContextIndex();
956     static IntSize oldestContextSize();
957 };
958
959 DEFINE_TYPE_CASTS(WebGLRenderingContextBase, CanvasRenderingContext, context, context->is3d(), context.is3d());
960
961 } // namespace blink
962
963 WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(blink::WebGLRenderingContextBase::TextureUnitState);
964
965 #endif // WebGLRenderingContextBase_h