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 bool supportsBGRA();
85 virtual bool supportsMapSubCHROMIUM();
86 virtual void* mapBufferSubDataCHROMIUM(unsigned target, int offset, int size, unsigned access);
87 virtual void unmapBufferSubDataCHROMIUM(const void*);
88 virtual void* mapTexSubImage2DCHROMIUM(unsigned target, int level, int xoffset, int yoffset, int width, int height, unsigned format, unsigned type, unsigned access);
89 virtual void unmapTexSubImage2DCHROMIUM(const void*);
90 virtual bool supportsCopyTextureToParentTextureCHROMIUM();
91 virtual void copyTextureToParentTextureCHROMIUM(unsigned texture, unsigned parentTexture);
93 virtual void activeTexture(unsigned long texture);
94 virtual void attachShader(WebGLId program, WebGLId shader);
95 virtual void bindAttribLocation(WebGLId program, unsigned long index, const char* name);
96 virtual void bindBuffer(unsigned long target, WebGLId buffer);
97 virtual void bindFramebuffer(unsigned long target, WebGLId framebuffer);
98 virtual void bindRenderbuffer(unsigned long target, WebGLId renderbuffer);
99 virtual void bindTexture(unsigned long target, WebGLId texture);
100 virtual void blendColor(double red, double green, double blue, double alpha);
101 virtual void blendEquation(unsigned long mode);
102 virtual void blendEquationSeparate(unsigned long modeRGB, unsigned long modeAlpha);
103 virtual void blendFunc(unsigned long sfactor, unsigned long dfactor);
104 virtual void blendFuncSeparate(unsigned long srcRGB, unsigned long dstRGB, unsigned long srcAlpha, unsigned long dstAlpha);
106 virtual void bufferData(unsigned long target, int size, const void* data, unsigned long usage);
107 virtual void bufferSubData(unsigned long target, long offset, int size, const void* data);
109 virtual unsigned long checkFramebufferStatus(unsigned long target);
110 virtual void clear(unsigned long mask);
111 virtual void clearColor(double red, double green, double blue, double alpha);
112 virtual void clearDepth(double depth);
113 virtual void clearStencil(long s);
114 virtual void colorMask(bool red, bool green, bool blue, bool alpha);
115 virtual void compileShader(WebGLId shader);
117 virtual void copyTexImage2D(unsigned long target, long level, unsigned long internalformat, long x, long y, unsigned long width, unsigned long height, long border);
118 virtual void copyTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, long x, long y, unsigned long width, unsigned long height);
119 virtual void cullFace(unsigned long mode);
120 virtual void depthFunc(unsigned long func);
121 virtual void depthMask(bool flag);
122 virtual void depthRange(double zNear, double zFar);
123 virtual void detachShader(WebGLId program, WebGLId shader);
124 virtual void disable(unsigned long cap);
125 virtual void disableVertexAttribArray(unsigned long index);
126 virtual void drawArrays(unsigned long mode, long first, long count);
127 virtual void drawElements(unsigned long mode, unsigned long count, unsigned long type, long offset);
129 virtual void enable(unsigned long cap);
130 virtual void enableVertexAttribArray(unsigned long index);
131 virtual void finish();
132 virtual void flush();
133 virtual void framebufferRenderbuffer(unsigned long target, unsigned long attachment, unsigned long renderbuffertarget, WebGLId renderbuffer);
134 virtual void framebufferTexture2D(unsigned long target, unsigned long attachment, unsigned long textarget, WebGLId texture, long level);
135 virtual void frontFace(unsigned long mode);
136 virtual void generateMipmap(unsigned long target);
138 virtual bool getActiveAttrib(WebGLId program, unsigned long index, ActiveInfo&);
139 virtual bool getActiveUniform(WebGLId program, unsigned long index, ActiveInfo&);
141 virtual void getAttachedShaders(WebGLId program, int maxCount, int* count, unsigned int* shaders);
143 virtual int getAttribLocation(WebGLId program, const char* name);
145 virtual void getBooleanv(unsigned long pname, unsigned char* value);
147 virtual void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
149 virtual Attributes getContextAttributes();
151 virtual unsigned long getError();
153 virtual void getFloatv(unsigned long pname, float* value);
155 virtual void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
157 virtual void getIntegerv(unsigned long pname, int* value);
159 virtual void getProgramiv(WebGLId program, unsigned long pname, int* value);
161 virtual WebString getProgramInfoLog(WebGLId program);
163 virtual void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
165 virtual void getShaderiv(WebGLId shader, unsigned long pname, int* value);
167 virtual WebString getShaderInfoLog(WebGLId shader);
170 // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
172 virtual WebString getShaderSource(WebGLId shader);
173 virtual WebString getString(unsigned long name);
175 virtual void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
176 virtual void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
178 virtual void getUniformfv(WebGLId program, long location, float* value);
179 virtual void getUniformiv(WebGLId program, long location, int* value);
181 virtual long getUniformLocation(WebGLId program, const char* name);
183 virtual void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
184 virtual void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
186 virtual long getVertexAttribOffset(unsigned long index, unsigned long pname);
188 virtual void hint(unsigned long target, unsigned long mode);
189 virtual bool isBuffer(WebGLId buffer);
190 virtual bool isEnabled(unsigned long cap);
191 virtual bool isFramebuffer(WebGLId framebuffer);
192 virtual bool isProgram(WebGLId program);
193 virtual bool isRenderbuffer(WebGLId renderbuffer);
194 virtual bool isShader(WebGLId shader);
195 virtual bool isTexture(WebGLId texture);
196 virtual void lineWidth(double);
197 virtual void linkProgram(WebGLId program);
198 virtual void pixelStorei(unsigned long pname, long param);
199 virtual void polygonOffset(double factor, double units);
201 virtual void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* pixels);
203 virtual void releaseShaderCompiler();
204 virtual void renderbufferStorage(unsigned long target, unsigned long internalformat, unsigned long width, unsigned long height);
205 virtual void sampleCoverage(double value, bool invert);
206 virtual void scissor(long x, long y, unsigned long width, unsigned long height);
207 virtual void shaderSource(WebGLId shader, const char* string);
208 virtual void stencilFunc(unsigned long func, long ref, unsigned long mask);
209 virtual void stencilFuncSeparate(unsigned long face, unsigned long func, long ref, unsigned long mask);
210 virtual void stencilMask(unsigned long mask);
211 virtual void stencilMaskSeparate(unsigned long face, unsigned long mask);
212 virtual void stencilOp(unsigned long fail, unsigned long zfail, unsigned long zpass);
213 virtual void stencilOpSeparate(unsigned long face, unsigned long fail, unsigned long zfail, unsigned long zpass);
215 virtual void texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, const void* pixels);
217 virtual void texParameterf(unsigned target, unsigned pname, float param);
218 virtual void texParameteri(unsigned target, unsigned pname, int param);
220 virtual void texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, const void* pixels);
222 virtual void uniform1f(long location, float x);
223 virtual void uniform1fv(long location, int count, float* v);
224 virtual void uniform1i(long location, int x);
225 virtual void uniform1iv(long location, int count, int* v);
226 virtual void uniform2f(long location, float x, float y);
227 virtual void uniform2fv(long location, int count, float* v);
228 virtual void uniform2i(long location, int x, int y);
229 virtual void uniform2iv(long location, int count, int* v);
230 virtual void uniform3f(long location, float x, float y, float z);
231 virtual void uniform3fv(long location, int count, float* v);
232 virtual void uniform3i(long location, int x, int y, int z);
233 virtual void uniform3iv(long location, int count, int* v);
234 virtual void uniform4f(long location, float x, float y, float z, float w);
235 virtual void uniform4fv(long location, int count, float* v);
236 virtual void uniform4i(long location, int x, int y, int z, int w);
237 virtual void uniform4iv(long location, int count, int* v);
238 virtual void uniformMatrix2fv(long location, int count, bool transpose, const float* value);
239 virtual void uniformMatrix3fv(long location, int count, bool transpose, const float* value);
240 virtual void uniformMatrix4fv(long location, int count, bool transpose, const float* value);
242 virtual void useProgram(WebGLId program);
243 virtual void validateProgram(WebGLId program);
245 virtual void vertexAttrib1f(unsigned long indx, float x);
246 virtual void vertexAttrib1fv(unsigned long indx, const float* values);
247 virtual void vertexAttrib2f(unsigned long indx, float x, float y);
248 virtual void vertexAttrib2fv(unsigned long indx, const float* values);
249 virtual void vertexAttrib3f(unsigned long indx, float x, float y, float z);
250 virtual void vertexAttrib3fv(unsigned long indx, const float* values);
251 virtual void vertexAttrib4f(unsigned long indx, float x, float y, float z, float w);
252 virtual void vertexAttrib4fv(unsigned long indx, const float* values);
253 virtual void vertexAttribPointer(unsigned long indx, int size, int type, bool normalized,
254 unsigned long stride, unsigned long offset);
256 virtual void viewport(long x, long y, unsigned long width, unsigned long height);
258 // Support for buffer creation and deletion
259 virtual unsigned createBuffer();
260 virtual unsigned createFramebuffer();
261 virtual unsigned createProgram();
262 virtual unsigned createRenderbuffer();
263 virtual unsigned createShader(unsigned long);
264 virtual unsigned createTexture();
266 virtual void deleteBuffer(unsigned);
267 virtual void deleteFramebuffer(unsigned);
268 virtual void deleteProgram(unsigned);
269 virtual void deleteRenderbuffer(unsigned);
270 virtual void deleteShader(unsigned);
271 virtual void deleteTexture(unsigned);
274 WebGraphicsContext3D::Attributes m_attributes;
276 unsigned int m_texture;
278 unsigned int m_depthStencilBuffer;
279 unsigned int m_cachedWidth, m_cachedHeight;
282 unsigned int m_multisampleFBO;
283 unsigned int m_multisampleDepthStencilBuffer;
284 unsigned int m_multisampleColorBuffer;
286 // For tracking which FBO is bound
287 unsigned int m_boundFBO;
289 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
290 unsigned char* m_scanline;
291 void flipVertically(unsigned char* framebuffer,
293 unsigned int height);
296 // Take into account the user's requested context creation attributes, in
297 // particular stencil and antialias, and determine which could or could
298 // not be honored based on the capabilities of the OpenGL implementation.
299 void validateAttributes();
301 // Note: we aren't currently using this information, but we will
302 // need to in order to verify that all enabled vertex arrays have
303 // a valid buffer bound -- to avoid crashes on certain cards.
304 unsigned int m_boundArrayBuffer;
305 struct VertexAttribPointerState {
306 VertexAttribPointerState();
309 unsigned long buffer;
314 unsigned long stride;
315 unsigned long offset;
319 NumTrackedPointerStates = 2
321 VertexAttribPointerState m_vertexAttribPointerState[NumTrackedPointerStates];
323 // Errors raised by synthesizeGLError().
324 ListHashSet<unsigned long> m_syntheticErrors;
326 OwnPtr<gfx::GLContext> m_glContext;
329 struct ShaderSourceEntry {
334 , translatedSource(0)
345 if (translatedSource)
346 fastFree(translatedSource);
352 char* translatedSource;
356 bool angleCreateCompilers();
357 void angleDestroyCompilers();
358 bool angleValidateShaderSource(ShaderSourceEntry& entry);
360 typedef HashMap<WebGLId, ShaderSourceEntry> ShaderSourceMap;
361 ShaderSourceMap m_shaderSourceMap;
363 ShHandle m_fragmentCompiler;
364 ShHandle m_vertexCompiler;
367 } // namespace WebKit
369 #endif // ENABLE(3D_CANVAS)