blob: 5314306c138a2396bd33bbcb2c1fd2d21cfa6209 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 "core/dom/ActiveDOMObject.h"
30#include "core/html/canvas/CanvasRenderingContext.h"
31#include "core/html/canvas/WebGLGetInfo.h"
32#include "core/platform/Timer.h"
33#include "core/platform/graphics/GraphicsContext3D.h"
34#include "core/platform/graphics/ImageBuffer.h"
35
36#include <wtf/Float32Array.h>
37#include <wtf/Int32Array.h>
38#include <wtf/OwnArrayPtr.h>
39#include <wtf/text/WTFString.h>
40#include <wtf/Uint8Array.h>
41
42namespace WebCore {
43
44class DrawingBuffer;
45class EXTDrawBuffers;
46class EXTTextureFilterAnisotropic;
47class HTMLImageElement;
48class HTMLVideoElement;
49class ImageBuffer;
50class ImageData;
51class IntSize;
52class OESStandardDerivatives;
53class OESTextureFloat;
54class OESTextureHalfFloat;
55class OESVertexArrayObject;
56class OESElementIndexUint;
57class WebGLActiveInfo;
58class WebGLBuffer;
59class WebGLContextGroup;
60class WebGLContextObject;
61class WebGLCompressedTextureATC;
62class WebGLCompressedTexturePVRTC;
63class WebGLCompressedTextureS3TC;
64class WebGLContextAttributes;
65class WebGLDebugRendererInfo;
66class WebGLDebugShaders;
67class WebGLDepthTexture;
68class WebGLExtension;
69class WebGLFramebuffer;
70class WebGLLoseContext;
71class WebGLObject;
72class WebGLProgram;
73class WebGLRenderbuffer;
74class WebGLShader;
75class WebGLSharedObject;
76class WebGLShaderPrecisionFormat;
77class WebGLTexture;
78class WebGLUniformLocation;
79class WebGLVertexArrayObjectOES;
80
81typedef int ExceptionCode;
82
83class WebGLRenderingContext : public CanvasRenderingContext, public ActiveDOMObject {
84public:
85 static PassOwnPtr<WebGLRenderingContext> create(HTMLCanvasElement*, WebGLContextAttributes*);
86 virtual ~WebGLRenderingContext();
87
88 virtual bool is3d() const { return true; }
89 virtual bool isAccelerated() const { return true; }
90
91 int drawingBufferWidth() const;
92 int drawingBufferHeight() const;
93
94 void activeTexture(GC3Denum texture, ExceptionCode&);
95 void attachShader(WebGLProgram*, WebGLShader*, ExceptionCode&);
96 void bindAttribLocation(WebGLProgram*, GC3Duint index, const String& name, ExceptionCode&);
97 void bindBuffer(GC3Denum target, WebGLBuffer*, ExceptionCode&);
98 void bindFramebuffer(GC3Denum target, WebGLFramebuffer*, ExceptionCode&);
99 void bindRenderbuffer(GC3Denum target, WebGLRenderbuffer*, ExceptionCode&);
100 void bindTexture(GC3Denum target, WebGLTexture*, ExceptionCode&);
101 void blendColor(GC3Dfloat red, GC3Dfloat green, GC3Dfloat blue, GC3Dfloat alpha);
102 void blendEquation(GC3Denum mode);
103 void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
104 void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
105 void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
106
107 void bufferData(GC3Denum target, long long size, GC3Denum usage, ExceptionCode&);
108 void bufferData(GC3Denum target, ArrayBuffer* data, GC3Denum usage, ExceptionCode&);
109 void bufferData(GC3Denum target, ArrayBufferView* data, GC3Denum usage, ExceptionCode&);
110 void bufferSubData(GC3Denum target, long long offset, ArrayBuffer* data, ExceptionCode&);
111 void bufferSubData(GC3Denum target, long long offset, ArrayBufferView* data, ExceptionCode&);
112
113 GC3Denum checkFramebufferStatus(GC3Denum target);
114 void clear(GC3Dbitfield mask);
115 void clearColor(GC3Dfloat red, GC3Dfloat green, GC3Dfloat blue, GC3Dfloat alpha);
116 void clearDepth(GC3Dfloat);
117 void clearStencil(GC3Dint);
118 void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
119 void compileShader(WebGLShader*, ExceptionCode&);
120
121 void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width,
122 GC3Dsizei height, GC3Dint border, ArrayBufferView* data);
123 void compressedTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
124 GC3Dsizei width, GC3Dsizei height, GC3Denum format, ArrayBufferView* data);
125
126 void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
127 void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
128
129 PassRefPtr<WebGLBuffer> createBuffer();
130 PassRefPtr<WebGLFramebuffer> createFramebuffer();
131 PassRefPtr<WebGLProgram> createProgram();
132 PassRefPtr<WebGLRenderbuffer> createRenderbuffer();
133 PassRefPtr<WebGLShader> createShader(GC3Denum type, ExceptionCode&);
134 PassRefPtr<WebGLTexture> createTexture();
135
136 void cullFace(GC3Denum mode);
137
138 void deleteBuffer(WebGLBuffer*);
139 void deleteFramebuffer(WebGLFramebuffer*);
140 void deleteProgram(WebGLProgram*);
141 void deleteRenderbuffer(WebGLRenderbuffer*);
142 void deleteShader(WebGLShader*);
143 void deleteTexture(WebGLTexture*);
144
145 void depthFunc(GC3Denum);
146 void depthMask(GC3Dboolean);
147 void depthRange(GC3Dfloat zNear, GC3Dfloat zFar);
148 void detachShader(WebGLProgram*, WebGLShader*, ExceptionCode&);
149 void disable(GC3Denum cap);
150 void disableVertexAttribArray(GC3Duint index, ExceptionCode&);
151 void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count, ExceptionCode&);
152 void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, long long offset, ExceptionCode&);
153
154 void enable(GC3Denum cap);
155 void enableVertexAttribArray(GC3Duint index, ExceptionCode&);
156 void finish();
157 void flush();
158 void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, WebGLRenderbuffer*, ExceptionCode&);
159 void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, WebGLTexture*, GC3Dint level, ExceptionCode&);
160 void frontFace(GC3Denum mode);
161 void generateMipmap(GC3Denum target);
162
163 PassRefPtr<WebGLActiveInfo> getActiveAttrib(WebGLProgram*, GC3Duint index, ExceptionCode&);
164 PassRefPtr<WebGLActiveInfo> getActiveUniform(WebGLProgram*, GC3Duint index, ExceptionCode&);
165 bool getAttachedShaders(WebGLProgram*, Vector<RefPtr<WebGLShader> >&, ExceptionCode&);
166 GC3Dint getAttribLocation(WebGLProgram*, const String& name);
167 WebGLGetInfo getBufferParameter(GC3Denum target, GC3Denum pname, ExceptionCode&);
168 PassRefPtr<WebGLContextAttributes> getContextAttributes();
169 GC3Denum getError();
170 WebGLExtension* getExtension(const String& name);
171 WebGLGetInfo getFramebufferAttachmentParameter(GC3Denum target, GC3Denum attachment, GC3Denum pname, ExceptionCode&);
172 WebGLGetInfo getParameter(GC3Denum pname, ExceptionCode&);
173 WebGLGetInfo getProgramParameter(WebGLProgram*, GC3Denum pname, ExceptionCode&);
174 String getProgramInfoLog(WebGLProgram*, ExceptionCode&);
175 WebGLGetInfo getRenderbufferParameter(GC3Denum target, GC3Denum pname, ExceptionCode&);
176 WebGLGetInfo getShaderParameter(WebGLShader*, GC3Denum pname, ExceptionCode&);
177 String getShaderInfoLog(WebGLShader*, ExceptionCode&);
178 PassRefPtr<WebGLShaderPrecisionFormat> getShaderPrecisionFormat(GC3Denum shaderType, GC3Denum precisionType, ExceptionCode&);
179 String getShaderSource(WebGLShader*, ExceptionCode&);
180 Vector<String> getSupportedExtensions();
181 WebGLGetInfo getTexParameter(GC3Denum target, GC3Denum pname, ExceptionCode&);
182 WebGLGetInfo getUniform(WebGLProgram*, const WebGLUniformLocation*, ExceptionCode&);
183 PassRefPtr<WebGLUniformLocation> getUniformLocation(WebGLProgram*, const String&, ExceptionCode&);
184 WebGLGetInfo getVertexAttrib(GC3Duint index, GC3Denum pname, ExceptionCode&);
185 long long getVertexAttribOffset(GC3Duint index, GC3Denum pname);
186
187 void hint(GC3Denum target, GC3Denum mode);
188 GC3Dboolean isBuffer(WebGLBuffer*);
189 bool isContextLost();
190 GC3Dboolean isEnabled(GC3Denum cap);
191 GC3Dboolean isFramebuffer(WebGLFramebuffer*);
192 GC3Dboolean isProgram(WebGLProgram*);
193 GC3Dboolean isRenderbuffer(WebGLRenderbuffer*);
194 GC3Dboolean isShader(WebGLShader*);
195 GC3Dboolean isTexture(WebGLTexture*);
196
197 void lineWidth(GC3Dfloat);
198 void linkProgram(WebGLProgram*, ExceptionCode&);
199 void pixelStorei(GC3Denum pname, GC3Dint param);
200 void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
201 void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, ArrayBufferView* pixels, ExceptionCode&);
202 void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
203 void sampleCoverage(GC3Dfloat value, GC3Dboolean invert);
204 void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
205 void shaderSource(WebGLShader*, const String&, ExceptionCode&);
206 void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
207 void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
208 void stencilMask(GC3Duint);
209 void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
210 void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
211 void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
212
213 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
214 GC3Dsizei width, GC3Dsizei height, GC3Dint border,
215 GC3Denum format, GC3Denum type, ArrayBufferView*, ExceptionCode&);
216 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
217 GC3Denum format, GC3Denum type, ImageData*, ExceptionCode&);
218 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
219 GC3Denum format, GC3Denum type, HTMLImageElement*, ExceptionCode&);
220 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
221 GC3Denum format, GC3Denum type, HTMLCanvasElement*, ExceptionCode&);
222 void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat,
223 GC3Denum format, GC3Denum type, HTMLVideoElement*, ExceptionCode&);
224
225 void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
226 void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
227
228 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
229 GC3Dsizei width, GC3Dsizei height,
230 GC3Denum format, GC3Denum type, ArrayBufferView*, ExceptionCode&);
231 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
232 GC3Denum format, GC3Denum type, ImageData*, ExceptionCode&);
233 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
234 GC3Denum format, GC3Denum type, HTMLImageElement*, ExceptionCode&);
235 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
236 GC3Denum format, GC3Denum type, HTMLCanvasElement*, ExceptionCode&);
237 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
238 GC3Denum format, GC3Denum type, HTMLVideoElement*, ExceptionCode&);
239
240 void uniform1f(const WebGLUniformLocation* location, GC3Dfloat x, ExceptionCode&);
241 void uniform1fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
242 void uniform1fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
243 void uniform1i(const WebGLUniformLocation* location, GC3Dint x, ExceptionCode&);
244 void uniform1iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
245 void uniform1iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
246 void uniform2f(const WebGLUniformLocation* location, GC3Dfloat x, GC3Dfloat y, ExceptionCode&);
247 void uniform2fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
248 void uniform2fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
249 void uniform2i(const WebGLUniformLocation* location, GC3Dint x, GC3Dint y, ExceptionCode&);
250 void uniform2iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
251 void uniform2iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
252 void uniform3f(const WebGLUniformLocation* location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, ExceptionCode&);
253 void uniform3fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
254 void uniform3fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
255 void uniform3i(const WebGLUniformLocation* location, GC3Dint x, GC3Dint y, GC3Dint z, ExceptionCode&);
256 void uniform3iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
257 void uniform3iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
258 void uniform4f(const WebGLUniformLocation* location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w, ExceptionCode&);
259 void uniform4fv(const WebGLUniformLocation* location, Float32Array* v, ExceptionCode&);
260 void uniform4fv(const WebGLUniformLocation* location, GC3Dfloat* v, GC3Dsizei size, ExceptionCode&);
261 void uniform4i(const WebGLUniformLocation* location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w, ExceptionCode&);
262 void uniform4iv(const WebGLUniformLocation* location, Int32Array* v, ExceptionCode&);
263 void uniform4iv(const WebGLUniformLocation* location, GC3Dint* v, GC3Dsizei size, ExceptionCode&);
264 void uniformMatrix2fv(const WebGLUniformLocation* location, GC3Dboolean transpose, Float32Array* value, ExceptionCode&);
265 void uniformMatrix2fv(const WebGLUniformLocation* location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size, ExceptionCode&);
266 void uniformMatrix3fv(const WebGLUniformLocation* location, GC3Dboolean transpose, Float32Array* value, ExceptionCode&);
267 void uniformMatrix3fv(const WebGLUniformLocation* location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size, ExceptionCode&);
268 void uniformMatrix4fv(const WebGLUniformLocation* location, GC3Dboolean transpose, Float32Array* value, ExceptionCode&);
269 void uniformMatrix4fv(const WebGLUniformLocation* location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size, ExceptionCode&);
270
271 void useProgram(WebGLProgram*, ExceptionCode&);
272 void validateProgram(WebGLProgram*, ExceptionCode&);
273
274 void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
275 void vertexAttrib1fv(GC3Duint index, Float32Array* values);
276 void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
277 void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
278 void vertexAttrib2fv(GC3Duint index, Float32Array* values);
279 void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
280 void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
281 void vertexAttrib3fv(GC3Duint index, Float32Array* values);
282 void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
283 void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
284 void vertexAttrib4fv(GC3Duint index, Float32Array* values);
285 void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values, GC3Dsizei size);
286 void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
287 GC3Dsizei stride, long long offset, ExceptionCode&);
288
289 void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
290
291 // WEBKIT_lose_context support
292 enum LostContextMode {
293 // Lost context occurred at the graphics system level.
294 RealLostContext,
295
296 // Lost context provoked by WEBKIT_lose_context.
297 SyntheticLostContext
298 };
299 void forceLostContext(LostContextMode);
300 void forceRestoreContext();
301 void loseContextImpl(LostContextMode);
302
303 GraphicsContext3D* graphicsContext3D() const { return m_context.get(); }
304 WebGLContextGroup* contextGroup() const { return m_contextGroup.get(); }
305 virtual PlatformLayer* platformLayer() const;
306
307 void reshape(int width, int height);
308
309 void markLayerComposited();
310 virtual void paintRenderingResultsToCanvas();
311 virtual PassRefPtr<ImageData> paintRenderingResultsToImageData();
312
313 void removeSharedObject(WebGLSharedObject*);
314 void removeContextObject(WebGLContextObject*);
315
316 unsigned getMaxVertexAttribs() const { return m_maxVertexAttribs; }
317
318 // ActiveDOMObject notifications
319 virtual bool hasPendingActivity() const;
320 virtual void stop();
321
322 private:
323 friend class EXTDrawBuffers;
324 friend class WebGLFramebuffer;
325 friend class WebGLObject;
326 friend class OESVertexArrayObject;
327 friend class WebGLDebugShaders;
328 friend class WebGLCompressedTextureATC;
329 friend class WebGLCompressedTexturePVRTC;
330 friend class WebGLCompressedTextureS3TC;
331 friend class WebGLRenderingContextErrorMessageCallback;
332 friend class WebGLVertexArrayObjectOES;
333
334 WebGLRenderingContext(HTMLCanvasElement*, PassRefPtr<GraphicsContext3D>, GraphicsContext3D::Attributes);
335 void initializeNewContext();
336 void setupFlags();
337
338 void addSharedObject(WebGLSharedObject*);
339 void addContextObject(WebGLContextObject*);
340 void detachAndRemoveAllObjects();
341
342 void destroyGraphicsContext3D();
343 void markContextChanged();
344
345 // Query if the GL implementation is NPOT strict.
346 bool isGLES2NPOTStrict() { return m_isGLES2NPOTStrict; }
347 // Query if depth_stencil buffer is supported.
348 bool isDepthStencilSupported() { return m_isDepthStencilSupported; }
349
350 // Helper to return the size in bytes of OpenGL data types
351 // like GL_FLOAT, GL_INT, etc.
352 unsigned int sizeInBytes(GC3Denum type);
353
354 // Check if each enabled vertex attribute is bound to a buffer.
355 bool validateRenderingState();
356
357 bool validateWebGLObject(const char*, WebGLObject*);
358
359 // Adds a compressed texture format.
360 void addCompressedTextureFormat(GC3Denum);
361
362 // Template to help getSupportedExtensions
363 template<typename T>
364 void appendIfSupported(Vector<String>& strings, bool prefixed)
365 {
366 if (T::supported(this))
367 strings.append(String(prefixed ? "WEBKIT_" : "") + T::getExtensionName());
368 }
369
370 bool matchesNameWithPrefixes(const String& name, const String& baseName, const char** prefixes);
371
372 // Templates to help getExtension
373 template<typename T>
374 bool getExtensionIfMatch(const String& name, OwnPtr<T>& extensionPtr, const char** prefixes, WebGLExtension*& extension)
375 {
376 if (matchesNameWithPrefixes(name, T::getExtensionName(), prefixes) && (extensionPtr || T::supported(this))) {
377 if (!extensionPtr) {
378 extensionPtr = T::create(this);
379 }
380 extension = extensionPtr.get();
381 return true;
382 }
383 return false;
384 }
385
386 PassRefPtr<Image> videoFrameToImage(HTMLVideoElement*, BackingStoreCopy, ExceptionCode&);
387
388 RefPtr<GraphicsContext3D> m_context;
389 RefPtr<WebGLContextGroup> m_contextGroup;
390
391 // Structure for rendering to a DrawingBuffer, instead of directly
392 // to the back-buffer of m_context.
393 RefPtr<DrawingBuffer> m_drawingBuffer;
394
395 // Dispatches a context lost event once it is determined that one is needed.
396 // This is used both for synthetic and real context losses. For real ones, it's
397 // likely that there's no JavaScript on the stack, but that might be dependent
398 // on how exactly the platform discovers that the context was lost. For better
399 // portability we always defer the dispatch of the event.
400 Timer<WebGLRenderingContext> m_dispatchContextLostEventTimer;
401 bool m_restoreAllowed;
402 Timer<WebGLRenderingContext> m_restoreTimer;
403
404 bool m_needsUpdate;
405 bool m_markedCanvasDirty;
406 HashSet<WebGLContextObject*> m_contextObjects;
407
408 // List of bound VBO's. Used to maintain info about sizes for ARRAY_BUFFER and stored values for ELEMENT_ARRAY_BUFFER
409 RefPtr<WebGLBuffer> m_boundArrayBuffer;
410
411 RefPtr<WebGLVertexArrayObjectOES> m_defaultVertexArrayObject;
412 RefPtr<WebGLVertexArrayObjectOES> m_boundVertexArrayObject;
413 void setBoundVertexArrayObject(PassRefPtr<WebGLVertexArrayObjectOES> arrayObject)
414 {
415 if (arrayObject)
416 m_boundVertexArrayObject = arrayObject;
417 else
418 m_boundVertexArrayObject = m_defaultVertexArrayObject;
419 }
420
421 class VertexAttribValue {
422 public:
423 VertexAttribValue()
424 {
425 initValue();
426 }
427
428 void initValue()
429 {
430 value[0] = 0.0f;
431 value[1] = 0.0f;
432 value[2] = 0.0f;
433 value[3] = 1.0f;
434 }
435
436 GC3Dfloat value[4];
437 };
438 Vector<VertexAttribValue> m_vertexAttribValue;
439 unsigned m_maxVertexAttribs;
440 RefPtr<WebGLBuffer> m_vertexAttrib0Buffer;
441 long m_vertexAttrib0BufferSize;
442 GC3Dfloat m_vertexAttrib0BufferValue[4];
443 bool m_forceAttrib0BufferRefill;
444 bool m_vertexAttrib0UsedBefore;
445
446 RefPtr<WebGLProgram> m_currentProgram;
447 RefPtr<WebGLFramebuffer> m_framebufferBinding;
448 RefPtr<WebGLRenderbuffer> m_renderbufferBinding;
449 class TextureUnitState {
450 public:
451 RefPtr<WebGLTexture> m_texture2DBinding;
452 RefPtr<WebGLTexture> m_textureCubeMapBinding;
453 };
454 Vector<TextureUnitState> m_textureUnits;
455 unsigned long m_activeTextureUnit;
456
457 RefPtr<WebGLTexture> m_blackTexture2D;
458 RefPtr<WebGLTexture> m_blackTextureCubeMap;
459
460 Vector<GC3Denum> m_compressedTextureFormats;
461
462 // Fixed-size cache of reusable image buffers for video texImage2D calls.
463 class LRUImageBufferCache {
464 public:
465 LRUImageBufferCache(int capacity);
466 // The pointer returned is owned by the image buffer map.
467 ImageBuffer* imageBuffer(const IntSize& size);
468 private:
469 void bubbleToFront(int idx);
470 OwnArrayPtr<OwnPtr<ImageBuffer> > m_buffers;
471 int m_capacity;
472 };
473 LRUImageBufferCache m_videoCache;
474
475 GC3Dint m_maxTextureSize;
476 GC3Dint m_maxCubeMapTextureSize;
477 GC3Dint m_maxRenderbufferSize;
478 GC3Dint m_maxViewportDims[2];
479 GC3Dint m_maxTextureLevel;
480 GC3Dint m_maxCubeMapTextureLevel;
481
482 GC3Dint m_maxDrawBuffers;
483 GC3Dint m_maxColorAttachments;
484 GC3Denum m_backDrawBuffer;
485 bool m_drawBuffersWebGLRequirementsChecked;
486 bool m_drawBuffersSupported;
487
488 GC3Dint m_packAlignment;
489 GC3Dint m_unpackAlignment;
490 bool m_unpackFlipY;
491 bool m_unpackPremultiplyAlpha;
492 GC3Denum m_unpackColorspaceConversion;
493 bool m_contextLost;
494 LostContextMode m_contextLostMode;
495 GraphicsContext3D::Attributes m_attributes;
496
497 bool m_layerCleared;
498 GC3Dfloat m_clearColor[4];
499 bool m_scissorEnabled;
500 GC3Dfloat m_clearDepth;
501 GC3Dint m_clearStencil;
502 GC3Dboolean m_colorMask[4];
503 GC3Dboolean m_depthMask;
504
505 bool m_stencilEnabled;
506 GC3Duint m_stencilMask, m_stencilMaskBack;
507 GC3Dint m_stencilFuncRef, m_stencilFuncRefBack; // Note that these are the user specified values, not the internal clamped value.
508 GC3Duint m_stencilFuncMask, m_stencilFuncMaskBack;
509
510 bool m_isGLES2NPOTStrict;
511 bool m_isDepthStencilSupported;
512 bool m_isRobustnessEXTSupported;
513
514 bool m_synthesizedErrorsToConsole;
515 int m_numGLErrorsToConsoleAllowed;
516
517 // Enabled extension objects.
518 OwnPtr<EXTDrawBuffers> m_extDrawBuffers;
519 OwnPtr<EXTTextureFilterAnisotropic> m_extTextureFilterAnisotropic;
520 OwnPtr<OESTextureFloat> m_oesTextureFloat;
521 OwnPtr<OESTextureHalfFloat> m_oesTextureHalfFloat;
522 OwnPtr<OESStandardDerivatives> m_oesStandardDerivatives;
523 OwnPtr<OESVertexArrayObject> m_oesVertexArrayObject;
524 OwnPtr<OESElementIndexUint> m_oesElementIndexUint;
525 OwnPtr<WebGLLoseContext> m_webglLoseContext;
526 OwnPtr<WebGLDebugRendererInfo> m_webglDebugRendererInfo;
527 OwnPtr<WebGLDebugShaders> m_webglDebugShaders;
528 OwnPtr<WebGLCompressedTextureATC> m_webglCompressedTextureATC;
529 OwnPtr<WebGLCompressedTexturePVRTC> m_webglCompressedTexturePVRTC;
530 OwnPtr<WebGLCompressedTextureS3TC> m_webglCompressedTextureS3TC;
531 OwnPtr<WebGLDepthTexture> m_webglDepthTexture;
532
533 // Errors raised by synthesizeGLError() while the context is lost.
534 Vector<GC3Denum> lost_context_errors_;
535
536 // Helpers for getParameter and others
537 WebGLGetInfo getBooleanParameter(GC3Denum);
538 WebGLGetInfo getBooleanArrayParameter(GC3Denum);
539 WebGLGetInfo getFloatParameter(GC3Denum);
540 WebGLGetInfo getIntParameter(GC3Denum);
541 WebGLGetInfo getUnsignedIntParameter(GC3Denum);
542 WebGLGetInfo getWebGLFloatArrayParameter(GC3Denum);
543 WebGLGetInfo getWebGLIntArrayParameter(GC3Denum);
544
545 // Clear the backbuffer if it was composited since the last operation.
546 // clearMask is set to the bitfield of any clear that would happen anyway at this time
547 // and the function returns true if that clear is now unnecessary.
548 bool clearIfComposited(GC3Dbitfield clearMask = 0);
549
550 // Helper to restore state that clearing the framebuffer may destroy.
551 void restoreStateAfterClear();
552
553 void texImage2DBase(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels, ExceptionCode&);
554 void texImage2DImpl(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Denum format, GC3Denum type, Image*, GraphicsContext3D::ImageHtmlDomSource, bool flipY, bool premultiplyAlpha, ExceptionCode&);
555 void texSubImage2DBase(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels, ExceptionCode&);
556 void texSubImage2DImpl(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Denum format, GC3Denum type, Image*, GraphicsContext3D::ImageHtmlDomSource, bool flipY, bool premultiplyAlpha, ExceptionCode&);
557
558 void handleNPOTTextures(const char*, bool);
559
560 void createFallbackBlackTextures1x1();
561
562 // Helper function for copyTex{Sub}Image, check whether the internalformat
563 // and the color buffer format of the current bound framebuffer combination
564 // is valid.
565 bool isTexInternalFormatColorBufferCombinationValid(GC3Denum texInternalFormat,
566 GC3Denum colorBufferFormat);
567
568 // Helper function to get the bound framebuffer's color buffer format.
569 GC3Denum getBoundFramebufferColorFormat();
570
571 // Helper function to get the bound framebuffer's width.
572 int getBoundFramebufferWidth();
573
574 // Helper function to get the bound framebuffer's height.
575 int getBoundFramebufferHeight();
576
577 // Helper function to verify limits on the length of uniform and attribute locations.
578 bool validateLocationLength(const char* functionName, const String&);
579
580 // Helper function to check if size is non-negative.
581 // Generate GL error and return false for negative inputs; otherwise, return true.
582 bool validateSize(const char* functionName, GC3Dint x, GC3Dint y);
583
584 // Helper function to check if all characters in the string belong to the
585 // ASCII subset as defined in GLSL ES 1.0 spec section 3.1.
586 bool validateString(const char* functionName, const String&);
587
588 // Helper function to check target and texture bound to the target.
589 // Generate GL errors and return 0 if target is invalid or texture bound is
590 // null. Otherwise, return the texture bound to the target.
591 WebGLTexture* validateTextureBinding(const char* functionName, GC3Denum target, bool useSixEnumsForCubeMap);
592
593 // Helper function to check input format/type for functions {copy}Tex{Sub}Image.
594 // Generates GL error and returns false if parameters are invalid.
595 bool validateTexFuncFormatAndType(const char* functionName, GC3Denum format, GC3Denum type, GC3Dint level);
596
597 // Helper function to check input level for functions {copy}Tex{Sub}Image.
598 // Generates GL error and returns false if level is invalid.
599 bool validateTexFuncLevel(const char* functionName, GC3Denum target, GC3Dint level);
600
601 enum TexFuncValidationFunctionType {
602 NotTexSubImage2D,
603 TexSubImage2D,
604 };
605
606 enum TexFuncValidationSourceType {
607 SourceArrayBufferView,
608 SourceImageData,
609 SourceHTMLImageElement,
610 SourceHTMLCanvasElement,
611 SourceHTMLVideoElement,
612 };
613
614 // Helper function for tex{Sub}Image2D to check if the input format/type/level/target/width/height/border/xoffset/yoffset are valid.
615 // Otherwise, it would return quickly without doing other work.
616 bool validateTexFunc(const char* functionName, TexFuncValidationFunctionType, TexFuncValidationSourceType, GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width,
617 GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint xoffset, GC3Dint yoffset);
618
619 // Helper function to check input parameters for functions {copy}Tex{Sub}Image.
620 // Generates GL error and returns false if parameters are invalid.
621 bool validateTexFuncParameters(const char* functionName,
622 TexFuncValidationFunctionType,
623 GC3Denum target, GC3Dint level,
624 GC3Denum internalformat,
625 GC3Dsizei width, GC3Dsizei height, GC3Dint border,
626 GC3Denum format, GC3Denum type);
627
628 enum NullDisposition {
629 NullAllowed,
630 NullNotAllowed
631 };
632
633 // Helper function to validate that the given ArrayBufferView
634 // is of the correct type and contains enough data for the texImage call.
635 // Generates GL error and returns false if parameters are invalid.
636 bool validateTexFuncData(const char* functionName, GC3Dint level,
637 GC3Dsizei width, GC3Dsizei height,
638 GC3Denum format, GC3Denum type,
639 ArrayBufferView* pixels,
640 NullDisposition);
641
642 // Helper function to validate a given texture format is settable as in
643 // you can supply data to texImage2D, or call texImage2D, copyTexImage2D and
644 // copyTexSubImage2D.
645 // Generates GL error and returns false if the format is not settable.
646 bool validateSettableTexFormat(const char* functionName, GC3Denum format);
647
648 // Helper function to validate compressed texture data is correct size
649 // for the given format and dimensions.
650 bool validateCompressedTexFuncData(const char* functionName,
651 GC3Dsizei width, GC3Dsizei height,
652 GC3Denum format, ArrayBufferView* pixels);
653
654 // Helper function for validating compressed texture formats.
655 bool validateCompressedTexFormat(GC3Denum format);
656
657 // Helper function to validate compressed texture dimensions are valid for
658 // the given format.
659 bool validateCompressedTexDimensions(const char* functionName, GC3Dint level, GC3Dsizei width, GC3Dsizei height, GC3Denum format);
660
661 // Helper function to validate compressed texture dimensions are valid for
662 // the given format.
663 bool validateCompressedTexSubDimensions(const char* functionName, GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset,
664 GC3Dsizei width, GC3Dsizei height, GC3Denum format, WebGLTexture*);
665
666 // Helper function to validate mode for draw{Arrays/Elements}.
667 bool validateDrawMode(const char* functionName, GC3Denum);
668
669 // Helper function to validate if front/back stencilMask and stencilFunc settings are the same.
670 bool validateStencilSettings(const char* functionName);
671
672 // Helper function to validate stencil func.
673 bool validateStencilFunc(const char* functionName, GC3Denum);
674
675 // Helper function for texParameterf and texParameteri.
676 void texParameter(GC3Denum target, GC3Denum pname, GC3Dfloat parami, GC3Dint paramf, bool isFloat);
677
678 // Helper function to print GL errors to console.
679 void printGLErrorToConsole(const String&);
680 void printGLWarningToConsole(const char* function, const char* reason);
681
682 // Helper function to print warnings to console. Currently
683 // used only to warn about use of obsolete functions.
684 void printWarningToConsole(const String&);
685
686 // Helper function to validate input parameters for framebuffer functions.
687 // Generate GL error if parameters are illegal.
688 bool validateFramebufferFuncParameters(const char* functionName, GC3Denum target, GC3Denum attachment);
689
690 // Helper function to validate blend equation mode.
691 bool validateBlendEquation(const char* functionName, GC3Denum);
692
693 // Helper function to validate blend func factors.
694 bool validateBlendFuncFactors(const char* functionName, GC3Denum src, GC3Denum dst);
695
696 // Helper function to validate a GL capability.
697 bool validateCapability(const char* functionName, GC3Denum);
698
699 // Helper function to validate input parameters for uniform functions.
700 bool validateUniformParameters(const char* functionName, const WebGLUniformLocation*, Float32Array*, GC3Dsizei mod);
701 bool validateUniformParameters(const char* functionName, const WebGLUniformLocation*, Int32Array*, GC3Dsizei mod);
702 bool validateUniformParameters(const char* functionName, const WebGLUniformLocation*, void*, GC3Dsizei, GC3Dsizei mod);
703 bool validateUniformMatrixParameters(const char* functionName, const WebGLUniformLocation*, GC3Dboolean transpose, Float32Array*, GC3Dsizei mod);
704 bool validateUniformMatrixParameters(const char* functionName, const WebGLUniformLocation*, GC3Dboolean transpose, void*, GC3Dsizei, GC3Dsizei mod);
705
706 // Helper function to validate parameters for bufferData.
707 // Return the current bound buffer to target, or 0 if parameters are invalid.
708 WebGLBuffer* validateBufferDataParameters(const char* functionName, GC3Denum target, GC3Denum usage);
709
710 // Helper function for tex{Sub}Image2D to make sure image is ready and wouldn't taint Origin.
711 bool validateHTMLImageElement(const char* functionName, HTMLImageElement*, ExceptionCode&);
712
713 // Helper function for tex{Sub}Image2D to make sure canvas is ready and wouldn't taint Origin.
714 bool validateHTMLCanvasElement(const char* functionName, HTMLCanvasElement*, ExceptionCode&);
715
716 // Helper function for tex{Sub}Image2D to make sure video is ready wouldn't taint Origin.
717 bool validateHTMLVideoElement(const char* functionName, HTMLVideoElement*, ExceptionCode&);
718
719 // Helper functions for vertexAttribNf{v}.
720 void vertexAttribfImpl(const char* functionName, GC3Duint index, GC3Dsizei expectedSize, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat);
721 void vertexAttribfvImpl(const char* functionName, GC3Duint index, Float32Array*, GC3Dsizei expectedSize);
722 void vertexAttribfvImpl(const char* functionName, GC3Duint index, GC3Dfloat*, GC3Dsizei, GC3Dsizei expectedSize);
723
724 // Helper function for delete* (deleteBuffer, deleteProgram, etc) functions.
725 // Return false if caller should return without further processing.
726 bool deleteObject(WebGLObject*);
727
728 // Helper function for bind* (bindBuffer, bindTexture, etc) and useProgram.
729 // If the object has already been deleted, set deleted to true upon return.
730 // Return false if caller should return without further processing.
731 bool checkObjectToBeBound(const char* functionName, WebGLObject*, bool& deleted);
732
733 void dispatchContextLostEvent(Timer<WebGLRenderingContext>*);
734 // Helper for restoration after context lost.
735 void maybeRestoreContext(Timer<WebGLRenderingContext>*);
736
737 // Determine if we are running privileged code in the browser, for example,
738 // a Safari or Chrome extension.
739 bool allowPrivilegedExtensions() const;
740
741 enum ConsoleDisplayPreference {
742 DisplayInConsole,
743 DontDisplayInConsole
744 };
745
746 // Wrapper for GraphicsContext3D::synthesizeGLError that sends a message
747 // to the JavaScript console.
748 void synthesizeGLError(GC3Denum, const char* functionName, const char* description, ConsoleDisplayPreference = DisplayInConsole);
749
750 String ensureNotNull(const String&) const;
751
752 // Enable or disable stencil test based on user setting and
753 // whether the current FBO has a stencil buffer.
754 void applyStencilTest();
755
756 // Helper for enabling or disabling a capability.
757 void enableOrDisable(GC3Denum capability, bool enable);
758
759 // Clamp the width and height to GL_MAX_VIEWPORT_DIMS.
760 IntSize clampedCanvasSize();
761
762 // First time called, if EXT_draw_buffers is supported, query the value; otherwise return 0.
763 // Later, return the cached value.
764 GC3Dint getMaxDrawBuffers();
765 GC3Dint getMaxColorAttachments();
766
767 void setBackDrawBuffer(GC3Denum);
768
769 void restoreCurrentFramebuffer();
770 void restoreCurrentTexture2D();
771
772 friend class WebGLStateRestorer;
773 friend class WebGLRenderingContextEvictionManager;
774
775 static Vector<WebGLRenderingContext*>& activeContexts();
776 static Vector<WebGLRenderingContext*>& forciblyEvictedContexts();
777
778 static void activateContext(WebGLRenderingContext*);
779 static void deactivateContext(WebGLRenderingContext*, bool addToInactiveList);
780 static void willDestroyContext(WebGLRenderingContext*);
781 static void forciblyLoseOldestContext(const String& reason);
782 static IntSize oldestContextSize();
783};
784
785} // namespace WebCore
786
787#endif