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 "GLSLANG/ShaderLang.h"
37 #include "WebGraphicsContext3D.h"
39 #include <wtf/HashMap.h>
40 #include <wtf/ListHashSet.h>
41 #include <wtf/OwnPtr.h>
44 #define FLIP_FRAMEBUFFER_VERTICALLY
52 // The default implementation of WebGL. In Chromium, using this class
53 // requires the sandbox to be disabled, which is strongly discouraged.
54 // It is provided for support of test_shell and any Chromium ports
55 // where an in-renderer WebGL implementation would be helpful.
57 class WebGraphicsContext3DDefaultImpl : public WebGraphicsContext3D {
59 WebGraphicsContext3DDefaultImpl();
60 virtual ~WebGraphicsContext3DDefaultImpl();
62 //----------------------------------------------------------------------
63 // WebGraphicsContext3D methods
64 virtual bool initialize(WebGraphicsContext3D::Attributes attributes, WebView*, bool);
65 virtual bool makeContextCurrent();
70 virtual int sizeInBytes(int type);
72 virtual bool isGLES2Compliant();
73 virtual bool isGLES2NPOTStrict();
74 virtual bool isErrorGeneratedOnOutOfBoundsAccesses();
76 virtual void reshape(int width, int height);
78 virtual bool readBackFramebuffer(unsigned char* pixels, size_t bufferSize);
80 virtual unsigned int getPlatformTextureId();
81 virtual void prepareTexture();
83 virtual void synthesizeGLError(unsigned long error);
84 virtual void* mapBufferSubDataCHROMIUM(unsigned target, int offset, int size, unsigned access);
85 virtual void unmapBufferSubDataCHROMIUM(const void*);
86 virtual void* mapTexSubImage2DCHROMIUM(unsigned target, int level, int xoffset, int yoffset, int width, int height, unsigned format, unsigned type, unsigned access);
87 virtual void unmapTexSubImage2DCHROMIUM(const void*);
88 virtual void copyTextureToParentTextureCHROMIUM(unsigned texture, unsigned parentTexture);
90 virtual void activeTexture(unsigned long texture);
91 virtual void attachShader(WebGLId program, WebGLId shader);
92 virtual void bindAttribLocation(WebGLId program, unsigned long index, const char* name);
93 virtual void bindBuffer(unsigned long target, WebGLId buffer);
94 virtual void bindFramebuffer(unsigned long target, WebGLId framebuffer);
95 virtual void bindRenderbuffer(unsigned long target, WebGLId renderbuffer);
96 virtual void bindTexture(unsigned long target, WebGLId texture);
97 virtual void blendColor(double red, double green, double blue, double alpha);
98 virtual void blendEquation(unsigned long mode);
99 virtual void blendEquationSeparate(unsigned long modeRGB, unsigned long modeAlpha);
100 virtual void blendFunc(unsigned long sfactor, unsigned long dfactor);
101 virtual void blendFuncSeparate(unsigned long srcRGB, unsigned long dstRGB, unsigned long srcAlpha, unsigned long dstAlpha);
103 virtual void bufferData(unsigned long target, int size, const void* data, unsigned long usage);
104 virtual void bufferSubData(unsigned long target, long offset, int size, const void* data);
106 virtual unsigned long checkFramebufferStatus(unsigned long target);
107 virtual void clear(unsigned long mask);
108 virtual void clearColor(double red, double green, double blue, double alpha);
109 virtual void clearDepth(double depth);
110 virtual void clearStencil(long s);
111 virtual void colorMask(bool red, bool green, bool blue, bool alpha);
112 virtual void compileShader(WebGLId shader);
114 virtual void copyTexImage2D(unsigned long target, long level, unsigned long internalformat, long x, long y, unsigned long width, unsigned long height, long border);
115 virtual void copyTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, long x, long y, unsigned long width, unsigned long height);
116 virtual void cullFace(unsigned long mode);
117 virtual void depthFunc(unsigned long func);
118 virtual void depthMask(bool flag);
119 virtual void depthRange(double zNear, double zFar);
120 virtual void detachShader(WebGLId program, WebGLId shader);
121 virtual void disable(unsigned long cap);
122 virtual void disableVertexAttribArray(unsigned long index);
123 virtual void drawArrays(unsigned long mode, long first, long count);
124 virtual void drawElements(unsigned long mode, unsigned long count, unsigned long type, long offset);
126 virtual void enable(unsigned long cap);
127 virtual void enableVertexAttribArray(unsigned long index);
128 virtual void finish();
129 virtual void flush();
130 virtual void framebufferRenderbuffer(unsigned long target, unsigned long attachment, unsigned long renderbuffertarget, WebGLId renderbuffer);
131 virtual void framebufferTexture2D(unsigned long target, unsigned long attachment, unsigned long textarget, WebGLId texture, long level);
132 virtual void frontFace(unsigned long mode);
133 virtual void generateMipmap(unsigned long target);
135 virtual bool getActiveAttrib(WebGLId program, unsigned long index, ActiveInfo&);
136 virtual bool getActiveUniform(WebGLId program, unsigned long index, ActiveInfo&);
138 virtual void getAttachedShaders(WebGLId program, int maxCount, int* count, unsigned int* shaders);
140 virtual int getAttribLocation(WebGLId program, const char* name);
142 virtual void getBooleanv(unsigned long pname, unsigned char* value);
144 virtual void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
146 virtual Attributes getContextAttributes();
148 virtual unsigned long getError();
150 virtual void getFloatv(unsigned long pname, float* value);
152 virtual void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
154 virtual void getIntegerv(unsigned long pname, int* value);
156 virtual void getProgramiv(WebGLId program, unsigned long pname, int* value);
158 virtual WebString getProgramInfoLog(WebGLId program);
160 virtual void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
162 virtual void getShaderiv(WebGLId shader, unsigned long pname, int* value);
164 virtual WebString getShaderInfoLog(WebGLId shader);
167 // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
169 virtual WebString getShaderSource(WebGLId shader);
170 virtual WebString getString(unsigned long name);
172 virtual void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
173 virtual void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
175 virtual void getUniformfv(WebGLId program, long location, float* value);
176 virtual void getUniformiv(WebGLId program, long location, int* value);
178 virtual long getUniformLocation(WebGLId program, const char* name);
180 virtual void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
181 virtual void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
183 virtual long getVertexAttribOffset(unsigned long index, unsigned long pname);
185 virtual void hint(unsigned long target, unsigned long mode);
186 virtual bool isBuffer(WebGLId buffer);
187 virtual bool isEnabled(unsigned long cap);
188 virtual bool isFramebuffer(WebGLId framebuffer);
189 virtual bool isProgram(WebGLId program);
190 virtual bool isRenderbuffer(WebGLId renderbuffer);
191 virtual bool isShader(WebGLId shader);
192 virtual bool isTexture(WebGLId texture);
193 virtual void lineWidth(double);
194 virtual void linkProgram(WebGLId program);
195 virtual void pixelStorei(unsigned long pname, long param);
196 virtual void polygonOffset(double factor, double units);
198 virtual void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* pixels);
200 virtual void releaseShaderCompiler();
201 virtual void renderbufferStorage(unsigned long target, unsigned long internalformat, unsigned long width, unsigned long height);
202 virtual void sampleCoverage(double value, bool invert);
203 virtual void scissor(long x, long y, unsigned long width, unsigned long height);
204 virtual void shaderSource(WebGLId shader, const char* string);
205 virtual void stencilFunc(unsigned long func, long ref, unsigned long mask);
206 virtual void stencilFuncSeparate(unsigned long face, unsigned long func, long ref, unsigned long mask);
207 virtual void stencilMask(unsigned long mask);
208 virtual void stencilMaskSeparate(unsigned long face, unsigned long mask);
209 virtual void stencilOp(unsigned long fail, unsigned long zfail, unsigned long zpass);
210 virtual void stencilOpSeparate(unsigned long face, unsigned long fail, unsigned long zfail, unsigned long zpass);
212 virtual void texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, const void* pixels);
214 virtual void texParameterf(unsigned target, unsigned pname, float param);
215 virtual void texParameteri(unsigned target, unsigned pname, int param);
217 virtual void texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, const void* pixels);
219 virtual void uniform1f(long location, float x);
220 virtual void uniform1fv(long location, int count, float* v);
221 virtual void uniform1i(long location, int x);
222 virtual void uniform1iv(long location, int count, int* v);
223 virtual void uniform2f(long location, float x, float y);
224 virtual void uniform2fv(long location, int count, float* v);
225 virtual void uniform2i(long location, int x, int y);
226 virtual void uniform2iv(long location, int count, int* v);
227 virtual void uniform3f(long location, float x, float y, float z);
228 virtual void uniform3fv(long location, int count, float* v);
229 virtual void uniform3i(long location, int x, int y, int z);
230 virtual void uniform3iv(long location, int count, int* v);
231 virtual void uniform4f(long location, float x, float y, float z, float w);
232 virtual void uniform4fv(long location, int count, float* v);
233 virtual void uniform4i(long location, int x, int y, int z, int w);
234 virtual void uniform4iv(long location, int count, int* v);
235 virtual void uniformMatrix2fv(long location, int count, bool transpose, const float* value);
236 virtual void uniformMatrix3fv(long location, int count, bool transpose, const float* value);
237 virtual void uniformMatrix4fv(long location, int count, bool transpose, const float* value);
239 virtual void useProgram(WebGLId program);
240 virtual void validateProgram(WebGLId program);
242 virtual void vertexAttrib1f(unsigned long indx, float x);
243 virtual void vertexAttrib1fv(unsigned long indx, const float* values);
244 virtual void vertexAttrib2f(unsigned long indx, float x, float y);
245 virtual void vertexAttrib2fv(unsigned long indx, const float* values);
246 virtual void vertexAttrib3f(unsigned long indx, float x, float y, float z);
247 virtual void vertexAttrib3fv(unsigned long indx, const float* values);
248 virtual void vertexAttrib4f(unsigned long indx, float x, float y, float z, float w);
249 virtual void vertexAttrib4fv(unsigned long indx, const float* values);
250 virtual void vertexAttribPointer(unsigned long indx, int size, int type, bool normalized,
251 unsigned long stride, unsigned long offset);
253 virtual void viewport(long x, long y, unsigned long width, unsigned long height);
255 // Support for buffer creation and deletion
256 virtual unsigned createBuffer();
257 virtual unsigned createFramebuffer();
258 virtual unsigned createProgram();
259 virtual unsigned createRenderbuffer();
260 virtual unsigned createShader(unsigned long);
261 virtual unsigned createTexture();
263 virtual void deleteBuffer(unsigned);
264 virtual void deleteFramebuffer(unsigned);
265 virtual void deleteProgram(unsigned);
266 virtual void deleteRenderbuffer(unsigned);
267 virtual void deleteShader(unsigned);
268 virtual void deleteTexture(unsigned);
271 WebGraphicsContext3D::Attributes m_attributes;
273 bool m_renderDirectlyToWebView;
275 unsigned int m_texture;
277 unsigned int m_depthStencilBuffer;
278 unsigned int m_cachedWidth, m_cachedHeight;
281 unsigned int m_multisampleFBO;
282 unsigned int m_multisampleDepthStencilBuffer;
283 unsigned int m_multisampleColorBuffer;
285 // For tracking which FBO is bound
286 unsigned int m_boundFBO;
288 // For tracking which texture is bound
289 unsigned int m_boundTexture;
291 // FBO used for copying child texture to parent texture.
292 unsigned m_copyTextureToParentTextureFBO;
294 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
295 unsigned char* m_scanline;
296 void flipVertically(unsigned char* framebuffer,
298 unsigned int height);
301 // Take into account the user's requested context creation attributes, in
302 // particular stencil and antialias, and determine which could or could
303 // not be honored based on the capabilities of the OpenGL implementation.
304 void validateAttributes();
306 // Resolve the given rectangle of the multisampled framebuffer if necessary.
307 void resolveMultisampledFramebuffer(unsigned x, unsigned y, unsigned width, unsigned height);
309 // Note: we aren't currently using this information, but we will
310 // need to in order to verify that all enabled vertex arrays have
311 // a valid buffer bound -- to avoid crashes on certain cards.
312 unsigned int m_boundArrayBuffer;
313 struct VertexAttribPointerState {
314 VertexAttribPointerState();
317 unsigned long buffer;
322 unsigned long stride;
323 unsigned long offset;
327 NumTrackedPointerStates = 2
329 VertexAttribPointerState m_vertexAttribPointerState[NumTrackedPointerStates];
331 // Errors raised by synthesizeGLError().
332 ListHashSet<unsigned long> m_syntheticErrors;
334 OwnPtr<gfx::GLContext> m_glContext;
337 struct ShaderSourceEntry {
338 ShaderSourceEntry(unsigned long shaderType)
342 , translatedSource(0)
353 if (translatedSource)
354 fastFree(translatedSource);
360 char* translatedSource;
364 bool angleCreateCompilers();
365 void angleDestroyCompilers();
366 bool angleValidateShaderSource(ShaderSourceEntry& entry);
368 typedef HashMap<WebGLId, ShaderSourceEntry*> ShaderSourceMap;
369 ShaderSourceMap m_shaderSourceMap;
371 ShHandle m_fragmentCompiler;
372 ShHandle m_vertexCompiler;
375 } // namespace WebKit
377 #endif // ENABLE(3D_CANVAS)