2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WebGraphicsContext3DDefaultImpl_h
32 #define WebGraphicsContext3DDefaultImpl_h
36 #include "WebGraphicsContext3D.h"
38 #include <wtf/ListHashSet.h>
39 #include <wtf/OwnPtr.h>
42 #define FLIP_FRAMEBUFFER_VERTICALLY
50 // The default implementation of WebGL. In Chromium, using this class
51 // requires the sandbox to be disabled, which is strongly discouraged.
52 // It is provided for support of test_shell and any Chromium ports
53 // where an in-renderer WebGL implementation would be helpful.
55 class WebGraphicsContext3DDefaultImpl : public WebGraphicsContext3D {
57 WebGraphicsContext3DDefaultImpl();
58 virtual ~WebGraphicsContext3DDefaultImpl();
60 //----------------------------------------------------------------------
61 // WebGraphicsContext3D methods
62 virtual bool initialize(WebGraphicsContext3D::Attributes attributes, WebView*);
63 virtual bool makeContextCurrent();
68 virtual int sizeInBytes(int type);
70 virtual bool isGLES2Compliant();
71 virtual bool isGLES2NPOTStrict();
72 virtual bool isErrorGeneratedOnOutOfBoundsAccesses();
74 virtual void reshape(int width, int height);
76 virtual bool readBackFramebuffer(unsigned char* pixels, size_t bufferSize);
78 virtual unsigned int getPlatformTextureId();
79 virtual void prepareTexture();
81 virtual void synthesizeGLError(unsigned long error);
82 virtual bool supportsBGRA();
84 virtual void activeTexture(unsigned long texture);
85 virtual void attachShader(WebGLId program, WebGLId shader);
86 virtual void bindAttribLocation(WebGLId program, unsigned long index, const char* name);
87 virtual void bindBuffer(unsigned long target, WebGLId buffer);
88 virtual void bindFramebuffer(unsigned long target, WebGLId framebuffer);
89 virtual void bindRenderbuffer(unsigned long target, WebGLId renderbuffer);
90 virtual void bindTexture(unsigned long target, WebGLId texture);
91 virtual void blendColor(double red, double green, double blue, double alpha);
92 virtual void blendEquation(unsigned long mode);
93 virtual void blendEquationSeparate(unsigned long modeRGB, unsigned long modeAlpha);
94 virtual void blendFunc(unsigned long sfactor, unsigned long dfactor);
95 virtual void blendFuncSeparate(unsigned long srcRGB, unsigned long dstRGB, unsigned long srcAlpha, unsigned long dstAlpha);
97 virtual void bufferData(unsigned long target, int size, const void* data, unsigned long usage);
98 virtual void bufferSubData(unsigned long target, long offset, int size, const void* data);
100 virtual unsigned long checkFramebufferStatus(unsigned long target);
101 virtual void clear(unsigned long mask);
102 virtual void clearColor(double red, double green, double blue, double alpha);
103 virtual void clearDepth(double depth);
104 virtual void clearStencil(long s);
105 virtual void colorMask(bool red, bool green, bool blue, bool alpha);
106 virtual void compileShader(WebGLId shader);
108 virtual void copyTexImage2D(unsigned long target, long level, unsigned long internalformat, long x, long y, unsigned long width, unsigned long height, long border);
109 virtual void copyTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, long x, long y, unsigned long width, unsigned long height);
110 virtual void cullFace(unsigned long mode);
111 virtual void depthFunc(unsigned long func);
112 virtual void depthMask(bool flag);
113 virtual void depthRange(double zNear, double zFar);
114 virtual void detachShader(WebGLId program, WebGLId shader);
115 virtual void disable(unsigned long cap);
116 virtual void disableVertexAttribArray(unsigned long index);
117 virtual void drawArrays(unsigned long mode, long first, long count);
118 virtual void drawElements(unsigned long mode, unsigned long count, unsigned long type, long offset);
120 virtual void enable(unsigned long cap);
121 virtual void enableVertexAttribArray(unsigned long index);
122 virtual void finish();
123 virtual void flush();
124 virtual void framebufferRenderbuffer(unsigned long target, unsigned long attachment, unsigned long renderbuffertarget, WebGLId renderbuffer);
125 virtual void framebufferTexture2D(unsigned long target, unsigned long attachment, unsigned long textarget, WebGLId texture, long level);
126 virtual void frontFace(unsigned long mode);
127 virtual void generateMipmap(unsigned long target);
129 virtual bool getActiveAttrib(WebGLId program, unsigned long index, ActiveInfo&);
130 virtual bool getActiveUniform(WebGLId program, unsigned long index, ActiveInfo&);
132 virtual void getAttachedShaders(WebGLId program, int maxCount, int* count, unsigned int* shaders);
134 virtual int getAttribLocation(WebGLId program, const char* name);
136 virtual void getBooleanv(unsigned long pname, unsigned char* value);
138 virtual void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
140 virtual Attributes getContextAttributes();
142 virtual unsigned long getError();
144 virtual void getFloatv(unsigned long pname, float* value);
146 virtual void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
148 virtual void getIntegerv(unsigned long pname, int* value);
150 virtual void getProgramiv(WebGLId program, unsigned long pname, int* value);
152 virtual WebString getProgramInfoLog(WebGLId program);
154 virtual void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
156 virtual void getShaderiv(WebGLId shader, unsigned long pname, int* value);
158 virtual WebString getShaderInfoLog(WebGLId shader);
161 // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
163 virtual WebString getShaderSource(WebGLId shader);
164 virtual WebString getString(unsigned long name);
166 virtual void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
167 virtual void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
169 virtual void getUniformfv(WebGLId program, long location, float* value);
170 virtual void getUniformiv(WebGLId program, long location, int* value);
172 virtual long getUniformLocation(WebGLId program, const char* name);
174 virtual void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
175 virtual void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
177 virtual long getVertexAttribOffset(unsigned long index, unsigned long pname);
179 virtual void hint(unsigned long target, unsigned long mode);
180 virtual bool isBuffer(WebGLId buffer);
181 virtual bool isEnabled(unsigned long cap);
182 virtual bool isFramebuffer(WebGLId framebuffer);
183 virtual bool isProgram(WebGLId program);
184 virtual bool isRenderbuffer(WebGLId renderbuffer);
185 virtual bool isShader(WebGLId shader);
186 virtual bool isTexture(WebGLId texture);
187 virtual void lineWidth(double);
188 virtual void linkProgram(WebGLId program);
189 virtual void pixelStorei(unsigned long pname, long param);
190 virtual void polygonOffset(double factor, double units);
192 virtual void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* pixels);
194 virtual void releaseShaderCompiler();
195 virtual void renderbufferStorage(unsigned long target, unsigned long internalformat, unsigned long width, unsigned long height);
196 virtual void sampleCoverage(double value, bool invert);
197 virtual void scissor(long x, long y, unsigned long width, unsigned long height);
198 virtual void shaderSource(WebGLId shader, const char* string);
199 virtual void stencilFunc(unsigned long func, long ref, unsigned long mask);
200 virtual void stencilFuncSeparate(unsigned long face, unsigned long func, long ref, unsigned long mask);
201 virtual void stencilMask(unsigned long mask);
202 virtual void stencilMaskSeparate(unsigned long face, unsigned long mask);
203 virtual void stencilOp(unsigned long fail, unsigned long zfail, unsigned long zpass);
204 virtual void stencilOpSeparate(unsigned long face, unsigned long fail, unsigned long zfail, unsigned long zpass);
206 virtual void texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, const void* pixels);
208 virtual void texParameterf(unsigned target, unsigned pname, float param);
209 virtual void texParameteri(unsigned target, unsigned pname, int param);
211 virtual void texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, const void* pixels);
213 virtual void uniform1f(long location, float x);
214 virtual void uniform1fv(long location, int count, float* v);
215 virtual void uniform1i(long location, int x);
216 virtual void uniform1iv(long location, int count, int* v);
217 virtual void uniform2f(long location, float x, float y);
218 virtual void uniform2fv(long location, int count, float* v);
219 virtual void uniform2i(long location, int x, int y);
220 virtual void uniform2iv(long location, int count, int* v);
221 virtual void uniform3f(long location, float x, float y, float z);
222 virtual void uniform3fv(long location, int count, float* v);
223 virtual void uniform3i(long location, int x, int y, int z);
224 virtual void uniform3iv(long location, int count, int* v);
225 virtual void uniform4f(long location, float x, float y, float z, float w);
226 virtual void uniform4fv(long location, int count, float* v);
227 virtual void uniform4i(long location, int x, int y, int z, int w);
228 virtual void uniform4iv(long location, int count, int* v);
229 virtual void uniformMatrix2fv(long location, int count, bool transpose, const float* value);
230 virtual void uniformMatrix3fv(long location, int count, bool transpose, const float* value);
231 virtual void uniformMatrix4fv(long location, int count, bool transpose, const float* value);
233 virtual void useProgram(WebGLId program);
234 virtual void validateProgram(WebGLId program);
236 virtual void vertexAttrib1f(unsigned long indx, float x);
237 virtual void vertexAttrib1fv(unsigned long indx, const float* values);
238 virtual void vertexAttrib2f(unsigned long indx, float x, float y);
239 virtual void vertexAttrib2fv(unsigned long indx, const float* values);
240 virtual void vertexAttrib3f(unsigned long indx, float x, float y, float z);
241 virtual void vertexAttrib3fv(unsigned long indx, const float* values);
242 virtual void vertexAttrib4f(unsigned long indx, float x, float y, float z, float w);
243 virtual void vertexAttrib4fv(unsigned long indx, const float* values);
244 virtual void vertexAttribPointer(unsigned long indx, int size, int type, bool normalized,
245 unsigned long stride, unsigned long offset);
247 virtual void viewport(long x, long y, unsigned long width, unsigned long height);
249 // Support for buffer creation and deletion
250 virtual unsigned createBuffer();
251 virtual unsigned createFramebuffer();
252 virtual unsigned createProgram();
253 virtual unsigned createRenderbuffer();
254 virtual unsigned createShader(unsigned long);
255 virtual unsigned createTexture();
257 virtual void deleteBuffer(unsigned);
258 virtual void deleteFramebuffer(unsigned);
259 virtual void deleteProgram(unsigned);
260 virtual void deleteRenderbuffer(unsigned);
261 virtual void deleteShader(unsigned);
262 virtual void deleteTexture(unsigned);
265 WebGraphicsContext3D::Attributes m_attributes;
267 unsigned int m_texture;
269 unsigned int m_depthStencilBuffer;
270 unsigned int m_cachedWidth, m_cachedHeight;
273 unsigned int m_multisampleFBO;
274 unsigned int m_multisampleDepthStencilBuffer;
275 unsigned int m_multisampleColorBuffer;
277 // For tracking which FBO is bound
278 unsigned int m_boundFBO;
280 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
281 unsigned char* m_scanline;
282 void flipVertically(unsigned char* framebuffer,
284 unsigned int height);
287 // Take into account the user's requested context creation attributes, in
288 // particular stencil and antialias, and determine which could or could
289 // not be honored based on the capabilities of the OpenGL implementation.
290 void validateAttributes();
292 // Note: we aren't currently using this information, but we will
293 // need to in order to verify that all enabled vertex arrays have
294 // a valid buffer bound -- to avoid crashes on certain cards.
295 unsigned int m_boundArrayBuffer;
296 class VertexAttribPointerState {
298 VertexAttribPointerState();
301 unsigned long buffer;
306 unsigned long stride;
307 unsigned long offset;
311 NumTrackedPointerStates = 2
313 VertexAttribPointerState m_vertexAttribPointerState[NumTrackedPointerStates];
315 // Errors raised by synthesizeGLError().
316 ListHashSet<unsigned long> m_syntheticErrors;
318 OwnPtr<gfx::GLContext> m_glContext;
321 } // namespace WebKit
323 #endif // ENABLE(3D_CANVAS)