OSDN Git Service

319d9d83f9afb3e29779cee6c7af1d4f1ee82891
[android-x86/external-webkit.git] / WebKit / chromium / src / WebGraphicsContext3DDefaultImpl.h
1 /*
2  * Copyright (C) 2010 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #ifndef WebGraphicsContext3DDefaultImpl_h
32 #define WebGraphicsContext3DDefaultImpl_h
33
34 #if ENABLE(3D_CANVAS)
35
36 #include "GLSLANG/ShaderLang.h"
37 #include "WebGraphicsContext3D.h"
38
39 #include <wtf/HashMap.h>
40 #include <wtf/ListHashSet.h>
41 #include <wtf/OwnPtr.h>
42
43 #if !PLATFORM(CG)
44 #define FLIP_FRAMEBUFFER_VERTICALLY
45 #endif
46 namespace gfx {
47 class GLContext;
48 }
49
50 namespace WebKit {
51
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.
56
57 class WebGraphicsContext3DDefaultImpl : public WebGraphicsContext3D {
58 public:
59     WebGraphicsContext3DDefaultImpl();
60     virtual ~WebGraphicsContext3DDefaultImpl();
61
62     //----------------------------------------------------------------------
63     // WebGraphicsContext3D methods
64     virtual bool initialize(WebGraphicsContext3D::Attributes attributes, WebView*, bool);
65     virtual bool makeContextCurrent();
66
67     virtual int width();
68     virtual int height();
69
70     virtual int sizeInBytes(int type);
71
72     virtual bool isGLES2Compliant();
73     virtual bool isGLES2NPOTStrict();
74     virtual bool isErrorGeneratedOnOutOfBoundsAccesses();
75
76     virtual void reshape(int width, int height);
77
78     virtual bool readBackFramebuffer(unsigned char* pixels, size_t bufferSize);
79
80     virtual unsigned int getPlatformTextureId();
81     virtual void prepareTexture();
82
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);
92
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);
105
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);
108
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);
116
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);
128
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);
137
138     virtual bool getActiveAttrib(WebGLId program, unsigned long index, ActiveInfo&);
139     virtual bool getActiveUniform(WebGLId program, unsigned long index, ActiveInfo&);
140
141     virtual void getAttachedShaders(WebGLId program, int maxCount, int* count, unsigned int* shaders);
142
143     virtual int  getAttribLocation(WebGLId program, const char* name);
144
145     virtual void getBooleanv(unsigned long pname, unsigned char* value);
146
147     virtual void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
148
149     virtual Attributes getContextAttributes();
150
151     virtual unsigned long getError();
152
153     virtual void getFloatv(unsigned long pname, float* value);
154
155     virtual void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
156
157     virtual void getIntegerv(unsigned long pname, int* value);
158
159     virtual void getProgramiv(WebGLId program, unsigned long pname, int* value);
160
161     virtual WebString getProgramInfoLog(WebGLId program);
162
163     virtual void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
164
165     virtual void getShaderiv(WebGLId shader, unsigned long pname, int* value);
166
167     virtual WebString getShaderInfoLog(WebGLId shader);
168
169     // TBD
170     // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
171
172     virtual WebString getShaderSource(WebGLId shader);
173     virtual WebString getString(unsigned long name);
174
175     virtual void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
176     virtual void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
177
178     virtual void getUniformfv(WebGLId program, long location, float* value);
179     virtual void getUniformiv(WebGLId program, long location, int* value);
180
181     virtual long getUniformLocation(WebGLId program, const char* name);
182
183     virtual void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
184     virtual void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
185
186     virtual long getVertexAttribOffset(unsigned long index, unsigned long pname);
187
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);
200
201     virtual void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* pixels);
202
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);
214
215     virtual void texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, const void* pixels);
216
217     virtual void texParameterf(unsigned target, unsigned pname, float param);
218     virtual void texParameteri(unsigned target, unsigned pname, int param);
219
220     virtual void texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, const void* pixels);
221
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);
241
242     virtual void useProgram(WebGLId program);
243     virtual void validateProgram(WebGLId program);
244
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);
255
256     virtual void viewport(long x, long y, unsigned long width, unsigned long height);
257
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();
265
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);
272
273 private:
274     WebGraphicsContext3D::Attributes m_attributes;
275     bool m_initialized;
276     unsigned int m_texture;
277     unsigned int m_fbo;
278     unsigned int m_depthStencilBuffer;
279     unsigned int m_cachedWidth, m_cachedHeight;
280
281     // For multisampling
282     unsigned int m_multisampleFBO;
283     unsigned int m_multisampleDepthStencilBuffer;
284     unsigned int m_multisampleColorBuffer;
285
286     // For tracking which FBO is bound
287     unsigned int m_boundFBO;
288
289 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
290     unsigned char* m_scanline;
291     void flipVertically(unsigned char* framebuffer,
292                         unsigned int width,
293                         unsigned int height);
294 #endif
295
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();
300
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();
307
308         bool enabled;
309         unsigned long buffer;
310         unsigned long indx;
311         int size;
312         int type;
313         bool normalized;
314         unsigned long stride;
315         unsigned long offset;
316     };
317
318     enum {
319         NumTrackedPointerStates = 2
320     };
321     VertexAttribPointerState m_vertexAttribPointerState[NumTrackedPointerStates];
322
323     // Errors raised by synthesizeGLError().
324     ListHashSet<unsigned long> m_syntheticErrors;
325
326     OwnPtr<gfx::GLContext> m_glContext;
327
328     // ANGLE related.
329     struct ShaderSourceEntry {
330         ShaderSourceEntry()
331                 : type(0)
332                 , source(0)
333                 , log(0)
334                 , translatedSource(0)
335                 , isValid(false)
336         {
337         }
338
339         ~ShaderSourceEntry()
340         {
341             if (source)
342                 fastFree(source);
343             if (log)
344                 fastFree(log);
345             if (translatedSource)
346                 fastFree(translatedSource);
347         }
348
349         unsigned long type;
350         char* source;
351         char* log;
352         char* translatedSource;
353         bool isValid;
354     };
355
356     bool angleCreateCompilers();
357     void angleDestroyCompilers();
358     bool angleValidateShaderSource(ShaderSourceEntry& entry);
359
360     typedef HashMap<WebGLId, ShaderSourceEntry> ShaderSourceMap;
361     ShaderSourceMap m_shaderSourceMap;
362
363     ShHandle m_fragmentCompiler;
364     ShHandle m_vertexCompiler;
365 };
366
367 } // namespace WebKit
368
369 #endif // ENABLE(3D_CANVAS)
370
371 #endif