OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[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 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);
89
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);
102
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);
105
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);
113
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);
125
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);
134
135     virtual bool getActiveAttrib(WebGLId program, unsigned long index, ActiveInfo&);
136     virtual bool getActiveUniform(WebGLId program, unsigned long index, ActiveInfo&);
137
138     virtual void getAttachedShaders(WebGLId program, int maxCount, int* count, unsigned int* shaders);
139
140     virtual int  getAttribLocation(WebGLId program, const char* name);
141
142     virtual void getBooleanv(unsigned long pname, unsigned char* value);
143
144     virtual void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
145
146     virtual Attributes getContextAttributes();
147
148     virtual unsigned long getError();
149
150     virtual void getFloatv(unsigned long pname, float* value);
151
152     virtual void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
153
154     virtual void getIntegerv(unsigned long pname, int* value);
155
156     virtual void getProgramiv(WebGLId program, unsigned long pname, int* value);
157
158     virtual WebString getProgramInfoLog(WebGLId program);
159
160     virtual void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
161
162     virtual void getShaderiv(WebGLId shader, unsigned long pname, int* value);
163
164     virtual WebString getShaderInfoLog(WebGLId shader);
165
166     // TBD
167     // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
168
169     virtual WebString getShaderSource(WebGLId shader);
170     virtual WebString getString(unsigned long name);
171
172     virtual void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
173     virtual void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
174
175     virtual void getUniformfv(WebGLId program, long location, float* value);
176     virtual void getUniformiv(WebGLId program, long location, int* value);
177
178     virtual long getUniformLocation(WebGLId program, const char* name);
179
180     virtual void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
181     virtual void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
182
183     virtual long getVertexAttribOffset(unsigned long index, unsigned long pname);
184
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);
197
198     virtual void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* pixels);
199
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);
211
212     virtual void texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, const void* pixels);
213
214     virtual void texParameterf(unsigned target, unsigned pname, float param);
215     virtual void texParameteri(unsigned target, unsigned pname, int param);
216
217     virtual void texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, const void* pixels);
218
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);
238
239     virtual void useProgram(WebGLId program);
240     virtual void validateProgram(WebGLId program);
241
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);
252
253     virtual void viewport(long x, long y, unsigned long width, unsigned long height);
254
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();
262
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);
269
270 private:
271     WebGraphicsContext3D::Attributes m_attributes;
272     bool m_initialized;
273     bool m_renderDirectlyToWebView;
274
275     unsigned int m_texture;
276     unsigned int m_fbo;
277     unsigned int m_depthStencilBuffer;
278     unsigned int m_cachedWidth, m_cachedHeight;
279
280     // For multisampling
281     unsigned int m_multisampleFBO;
282     unsigned int m_multisampleDepthStencilBuffer;
283     unsigned int m_multisampleColorBuffer;
284
285     // For tracking which FBO is bound
286     unsigned int m_boundFBO;
287
288     // For tracking which texture is bound
289     unsigned int m_boundTexture;
290     
291     // FBO used for copying child texture to parent texture.
292     unsigned m_copyTextureToParentTextureFBO;
293
294 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
295     unsigned char* m_scanline;
296     void flipVertically(unsigned char* framebuffer,
297                         unsigned int width,
298                         unsigned int height);
299 #endif
300
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();
305
306     // Resolve the given rectangle of the multisampled framebuffer if necessary.
307     void resolveMultisampledFramebuffer(unsigned x, unsigned y, unsigned width, unsigned height);
308
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();
315
316         bool enabled;
317         unsigned long buffer;
318         unsigned long indx;
319         int size;
320         int type;
321         bool normalized;
322         unsigned long stride;
323         unsigned long offset;
324     };
325
326     enum {
327         NumTrackedPointerStates = 2
328     };
329     VertexAttribPointerState m_vertexAttribPointerState[NumTrackedPointerStates];
330
331     // Errors raised by synthesizeGLError().
332     ListHashSet<unsigned long> m_syntheticErrors;
333
334     OwnPtr<gfx::GLContext> m_glContext;
335
336     // ANGLE related.
337     struct ShaderSourceEntry {
338         ShaderSourceEntry(unsigned long shaderType)
339                 : type(shaderType)
340                 , source(0)
341                 , log(0)
342                 , translatedSource(0)
343                 , isValid(false)
344         {
345         }
346
347         ~ShaderSourceEntry()
348         {
349             if (source)
350                 fastFree(source);
351             if (log)
352                 fastFree(log);
353             if (translatedSource)
354                 fastFree(translatedSource);
355         }
356
357         unsigned long type;
358         char* source;
359         char* log;
360         char* translatedSource;
361         bool isValid;
362     };
363
364     bool angleCreateCompilers();
365     void angleDestroyCompilers();
366     bool angleValidateShaderSource(ShaderSourceEntry& entry);
367
368     typedef HashMap<WebGLId, ShaderSourceEntry*> ShaderSourceMap;
369     ShaderSourceMap m_shaderSourceMap;
370
371     ShHandle m_fragmentCompiler;
372     ShHandle m_vertexCompiler;
373 };
374
375 } // namespace WebKit
376
377 #endif // ENABLE(3D_CANVAS)
378
379 #endif