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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GraphicsContext3DPrivate_h
27 #define GraphicsContext3DPrivate_h
29 #include "Extensions3DChromium.h"
30 #include "GraphicsContext3D.h"
31 #include <wtf/HashSet.h>
32 #include <wtf/OwnArrayPtr.h>
33 #include <wtf/OwnPtr.h>
43 class WebGraphicsContext3D;
50 class Extensions3DChromium;
51 class GraphicsContextLostCallbackAdapter;
52 class GraphicsContext3DSwapBuffersCompleteCallbackAdapter;
54 class GraphicsContext3DPrivate {
56 static PassOwnPtr<GraphicsContext3DPrivate> create(WebKit::WebViewImpl*, PassOwnPtr<WebKit::WebGraphicsContext3D>, GraphicsContext3D::Attributes);
60 ForUseOnAnotherThread,
63 // createGraphicsContextForAnotherThread is equivalent to
64 // GraphicsContext3D::create, but will skip making the context
65 // current. Callers must make the context current before using it AND check
66 // that the context was created successfully via ContextLost. Once made
67 // current on a thread, the context cannot be used on any other thread.
68 static PassRefPtr<GraphicsContext3D> createGraphicsContextForAnotherThread(GraphicsContext3D::Attributes, HostWindow*, GraphicsContext3D::RenderStyle);
70 // Used in tests to create a GraphicsContext3D from a mocked WebGraphicsContext3D.
71 static PassRefPtr<GraphicsContext3D> createGraphicsContextFromWebContext(PassOwnPtr<WebKit::WebGraphicsContext3D>, GraphicsContext3D::Attributes, HostWindow*, GraphicsContext3D::RenderStyle, ThreadUsage);
73 ~GraphicsContext3DPrivate();
75 // Helper function to provide access to the lower-level WebGraphicsContext3D,
76 // which is needed for subordinate contexts like WebGL's to share resources
77 // with the compositor's context.
78 static WebKit::WebGraphicsContext3D* extractWebGraphicsContext3D(GraphicsContext3D*);
80 PlatformGraphicsContext3D platformGraphicsContext3D() const;
81 Platform3DObject platformTexture() const;
83 GrContext* grContext();
86 bool makeContextCurrent();
88 void reshape(int width, int height);
89 IntSize getInternalFramebufferSize() const;
90 bool isResourceSafe();
92 void markContextChanged();
93 bool layerComposited() const;
94 void markLayerComposited();
96 void paintRenderingResultsToCanvas(CanvasRenderingContext*, DrawingBuffer*);
97 void paintFramebufferToCanvas(int framebuffer, int width, int height, bool premultiplyAlpha, ImageBuffer*);
98 PassRefPtr<ImageData> paintRenderingResultsToImageData(DrawingBuffer*);
99 bool paintsIntoCanvasBuffer() const;
100 bool paintCompositedResultsToCanvas(CanvasRenderingContext*);
102 void prepareTexture();
104 // CHROMIUM_post_sub_buffer
105 void postSubBufferCHROMIUM(int x, int y, int width, int height);
107 bool isGLES2Compliant() const;
109 void releaseShaderCompiler();
110 bool isContextLost();
112 //----------------------------------------------------------------------
113 // Entry points for WebGL.
115 void activeTexture(GC3Denum texture);
116 void attachShader(Platform3DObject program, Platform3DObject shader);
117 void bindAttribLocation(Platform3DObject, GC3Duint index, const String& name);
118 void bindBuffer(GC3Denum target, Platform3DObject);
119 void bindFramebuffer(GC3Denum target, Platform3DObject);
120 void bindRenderbuffer(GC3Denum target, Platform3DObject);
121 void bindTexture(GC3Denum target, Platform3DObject);
122 void blendColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
123 void blendEquation(GC3Denum mode);
124 void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
125 void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
126 void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
128 void bufferData(GC3Denum target, GC3Dsizeiptr, GC3Denum usage);
129 void bufferData(GC3Denum target, GC3Dsizeiptr, const void* data, GC3Denum usage);
130 void bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr, const void* data);
132 GC3Denum checkFramebufferStatus(GC3Denum target);
133 void clear(GC3Dbitfield mask);
134 void clearColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
135 void clearDepth(GC3Dclampf depth);
136 void clearStencil(GC3Dint s);
137 void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
138 void compileShader(Platform3DObject);
140 void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
141 void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
142 void cullFace(GC3Denum mode);
143 void depthFunc(GC3Denum func);
144 void depthMask(GC3Dboolean flag);
145 void depthRange(GC3Dclampf zNear, GC3Dclampf zFar);
146 void detachShader(Platform3DObject, Platform3DObject);
147 void disable(GC3Denum cap);
148 void disableVertexAttribArray(GC3Duint index);
149 void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count);
150 void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
152 void enable(GC3Denum cap);
153 void enableVertexAttribArray(GC3Duint index);
156 void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, Platform3DObject);
157 void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level);
158 void frontFace(GC3Denum mode);
159 void generateMipmap(GC3Denum target);
161 bool getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo&);
162 bool getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo&);
163 void getAttachedShaders(Platform3DObject program, GC3Dsizei maxCount, GC3Dsizei* count, Platform3DObject* shaders);
164 GC3Dint getAttribLocation(Platform3DObject, const String& name);
165 void getBooleanv(GC3Denum pname, GC3Dboolean* value);
166 void getBufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
167 GraphicsContext3D::Attributes getContextAttributes();
169 void getFloatv(GC3Denum pname, GC3Dfloat* value);
170 void getFramebufferAttachmentParameteriv(GC3Denum target, GC3Denum attachment, GC3Denum pname, GC3Dint* value);
171 void getIntegerv(GC3Denum pname, GC3Dint* value);
172 void getProgramiv(Platform3DObject program, GC3Denum pname, GC3Dint* value);
173 String getProgramInfoLog(Platform3DObject);
174 void getRenderbufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
175 void getShaderiv(Platform3DObject, GC3Denum pname, GC3Dint* value);
176 String getShaderInfoLog(Platform3DObject);
178 String getShaderSource(Platform3DObject);
179 String getString(GC3Denum name);
180 void getTexParameterfv(GC3Denum target, GC3Denum pname, GC3Dfloat* value);
181 void getTexParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
182 void getUniformfv(Platform3DObject program, GC3Dint location, GC3Dfloat* value);
183 void getUniformiv(Platform3DObject program, GC3Dint location, GC3Dint* value);
184 GC3Dint getUniformLocation(Platform3DObject, const String& name);
185 void getVertexAttribfv(GC3Duint index, GC3Denum pname, GC3Dfloat* value);
186 void getVertexAttribiv(GC3Duint index, GC3Denum pname, GC3Dint* value);
187 GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
189 void hint(GC3Denum target, GC3Denum mode);
190 GC3Dboolean isBuffer(Platform3DObject);
191 GC3Dboolean isEnabled(GC3Denum cap);
192 GC3Dboolean isFramebuffer(Platform3DObject);
193 GC3Dboolean isProgram(Platform3DObject);
194 GC3Dboolean isRenderbuffer(Platform3DObject);
195 GC3Dboolean isShader(Platform3DObject);
196 GC3Dboolean isTexture(Platform3DObject);
197 void lineWidth(GC3Dfloat);
198 void linkProgram(Platform3DObject);
199 void pixelStorei(GC3Denum pname, GC3Dint param);
200 void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
202 void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
204 void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
205 void sampleCoverage(GC3Dclampf value, GC3Dboolean invert);
206 void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
207 void shaderSource(Platform3DObject, const String&);
208 void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
209 void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
210 void stencilMask(GC3Duint mask);
211 void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
212 void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
213 void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
215 // texImage2D return false on any error rather than using an ExceptionCode.
216 bool texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
217 void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
218 void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
219 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels);
221 void uniform1f(GC3Dint location, GC3Dfloat x);
222 void uniform1fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei);
223 void uniform1i(GC3Dint location, GC3Dint x);
224 void uniform1iv(GC3Dint location, GC3Dint* v, GC3Dsizei);
225 void uniform2f(GC3Dint location, GC3Dfloat x, float y);
226 void uniform2fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei);
227 void uniform2i(GC3Dint location, GC3Dint x, GC3Dint y);
228 void uniform2iv(GC3Dint location, GC3Dint* v, GC3Dsizei);
229 void uniform3f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
230 void uniform3fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei);
231 void uniform3i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z);
232 void uniform3iv(GC3Dint location, GC3Dint* v, GC3Dsizei);
233 void uniform4f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
234 void uniform4fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei);
235 void uniform4i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w);
236 void uniform4iv(GC3Dint location, GC3Dint* v, GC3Dsizei);
237 void uniformMatrix2fv(GC3Dint location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei);
238 void uniformMatrix3fv(GC3Dint location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei);
239 void uniformMatrix4fv(GC3Dint location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei);
241 void useProgram(Platform3DObject);
242 void validateProgram(Platform3DObject);
244 void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
245 void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values);
246 void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
247 void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values);
248 void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
249 void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values);
250 void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
251 void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values);
252 void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
253 GC3Dsizei stride, GC3Dintptr offset);
255 void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
257 Platform3DObject createBuffer();
258 Platform3DObject createFramebuffer();
259 Platform3DObject createProgram();
260 Platform3DObject createRenderbuffer();
261 Platform3DObject createShader(GC3Denum);
262 Platform3DObject createTexture();
264 void deleteBuffer(Platform3DObject);
265 void deleteFramebuffer(Platform3DObject);
266 void deleteProgram(Platform3DObject);
267 void deleteRenderbuffer(Platform3DObject);
268 void deleteShader(Platform3DObject);
269 void deleteTexture(Platform3DObject);
271 void synthesizeGLError(GC3Denum error);
273 void setContextLostCallback(PassOwnPtr<GraphicsContext3D::ContextLostCallback>);
275 // Extensions3D support.
276 Extensions3D* getExtensions();
277 bool supportsExtension(const String& name);
278 bool ensureExtensionEnabled(const String& name);
279 bool isExtensionEnabled(const String& name);
281 // EXT_texture_format_BGRA8888
284 // GL_CHROMIUM_map_sub
285 bool supportsMapSubCHROMIUM();
286 void* mapBufferSubDataCHROMIUM(GC3Denum target, GC3Dsizeiptr offset, GC3Dsizei, GC3Denum access);
287 void unmapBufferSubDataCHROMIUM(const void*);
288 void* mapTexSubImage2DCHROMIUM(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, GC3Denum access);
289 void unmapTexSubImage2DCHROMIUM(const void*);
291 // GL_CHROMIUM_set_visibility
292 void setVisibilityCHROMIUM(bool);
294 // GL_CHROMIUM_framebuffer_multisample
295 void blitFramebufferCHROMIUM(GC3Dint srcX0, GC3Dint srcY0, GC3Dint srcX1, GC3Dint srcY1, GC3Dint dstX0, GC3Dint dstY0, GC3Dint dstX1, GC3Dint dstY1, GC3Dbitfield mask, GC3Denum filter);
296 void renderbufferStorageMultisampleCHROMIUM(GC3Denum target, GC3Dsizei samples, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
298 // GL_CHROMIUM_swapbuffers_complete_callback
299 void setSwapBuffersCompleteCallbackCHROMIUM(PassOwnPtr<Extensions3DChromium::SwapBuffersCompleteCallbackCHROMIUM>);
301 // GL_CHROMIUM_rate_limit_offscreen_context
302 void rateLimitOffscreenContextCHROMIUM();
305 GC3Denum getGraphicsResetStatusARB();
307 // GL_ANGLE_translated_shader_source
308 String getTranslatedShaderSourceANGLE(Platform3DObject shader);
310 // GL_CHROMIUM_iosurface
311 void texImageIOSurface2DCHROMIUM(GC3Denum target, GC3Dint width, GC3Dint height, GC3Duint ioSurfaceId, GC3Duint plane);
314 GraphicsContext3DPrivate(WebKit::WebViewImpl*, PassOwnPtr<WebKit::WebGraphicsContext3D>, GraphicsContext3D::Attributes);
316 OwnPtr<WebKit::WebGraphicsContext3D> m_impl;
317 OwnPtr<Extensions3DChromium> m_extensions;
318 OwnPtr<GraphicsContextLostCallbackAdapter> m_contextLostCallbackAdapter;
319 OwnPtr<GraphicsContext3DSwapBuffersCompleteCallbackAdapter> m_swapBuffersCompleteCallbackAdapter;
320 WebKit::WebViewImpl* m_webViewImpl;
321 bool m_initializedAvailableExtensions;
322 HashSet<String> m_enabledExtensions;
323 HashSet<String> m_requestableExtensions;
324 bool m_layerComposited;
325 bool m_preserveDrawingBuffer;
327 enum ResourceSafety {
328 ResourceSafetyUnknown,
332 ResourceSafety m_resourceSafety;
335 // If the width and height of the Canvas's backing store don't
336 // match those that we were given in the most recent call to
337 // reshape(), then we need an intermediate bitmap to read back the
338 // frame buffer into. This seems to happen when CSS styles are
339 // used to resize the Canvas.
340 SkBitmap m_resizingBitmap;
342 GrContext* m_grContext;
346 OwnArrayPtr<unsigned char> m_renderOutput;
347 size_t m_renderOutputSize;
350 void initializeExtensions();
353 } // namespace WebCore
355 #endif // GraphicsContext3DPrivate_h