1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef WEBKIT_GPU_WEBGRAPHICSCONTEXT3D_IN_PROCESS_IMPL_H_
6 #define WEBKIT_GPU_WEBGRAPHICSCONTEXT3D_IN_PROCESS_IMPL_H_
11 #include "base/compiler_specific.h"
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "third_party/angle/include/GLSLANG/ShaderLang.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsContext3D.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
17 #include "ui/gfx/native_widget_types.h"
18 #include "webkit/gpu/webkit_gpu_export.h"
20 #if !defined(OS_MACOSX)
21 #define FLIP_FRAMEBUFFER_VERTICALLY
29 using WebKit::WGC3Dbyte
;
30 using WebKit::WGC3Dchar
;
31 using WebKit::WGC3Denum
;
32 using WebKit::WGC3Dboolean
;
33 using WebKit::WGC3Dbitfield
;
34 using WebKit::WGC3Dint
;
35 using WebKit::WGC3Dsizei
;
36 using WebKit::WGC3Duint
;
37 using WebKit::WGC3Dfloat
;
38 using WebKit::WGC3Dclampf
;
39 using WebKit::WGC3Dintptr
;
40 using WebKit::WGC3Dsizeiptr
;
42 using WebKit::WebGLId
;
44 using WebKit::WebString
;
46 using WebKit::WebGraphicsContext3D
;
51 // The default implementation of WebGL. In Chromium, using this class
52 // requires the sandbox to be disabled, which is strongly discouraged.
53 // It is provided for support of test_shell and any Chromium ports
54 // where an in-renderer WebGL implementation would be helpful.
56 class WEBKIT_GPU_EXPORT WebGraphicsContext3DInProcessImpl
:
57 public NON_EXPORTED_BASE(WebGraphicsContext3D
) {
59 // Creates a WebGraphicsContext3DInProcessImpl for a given window. If window
60 // is gfx::kNullPluginWindow, then it creates an offscreen context.
61 // share_group is the group this context shares namespaces with. It's only
62 // used for window-bound countexts.
63 WebGraphicsContext3DInProcessImpl(gfx::GLSurface
* surface
,
64 gfx::GLContext
* context
,
65 bool render_directly_to_webview
);
66 virtual ~WebGraphicsContext3DInProcessImpl();
67 static WebGraphicsContext3DInProcessImpl
* CreateForWebView(
68 WebGraphicsContext3D::Attributes attributes
,
69 bool render_directly_to_webview
);
70 static WebGraphicsContext3DInProcessImpl
* CreateForWindow(
71 WebGraphicsContext3D::Attributes attributes
,
72 gfx::AcceleratedWidget window
,
73 gfx::GLShareGroup
* share_group
);
75 //----------------------------------------------------------------------
76 // WebGraphicsContext3D methods
77 virtual bool makeContextCurrent();
82 virtual bool isGLES2Compliant();
84 virtual bool setParentContext(WebGraphicsContext3D
* parent_context
);
86 virtual void reshape(int width
, int height
);
88 virtual bool readBackFramebuffer(unsigned char* pixels
, size_t bufferSize
);
89 virtual bool readBackFramebuffer(unsigned char* pixels
, size_t buffer_size
,
90 WebGLId framebuffer
, int width
, int height
);
92 virtual WebGLId
getPlatformTextureId();
93 virtual void prepareTexture();
94 virtual void postSubBufferCHROMIUM(int x
, int y
, int width
, int height
);
96 virtual void synthesizeGLError(WGC3Denum error
);
97 virtual void* mapBufferSubDataCHROMIUM(WGC3Denum target
, WGC3Dintptr offset
,
98 WGC3Dsizeiptr size
, WGC3Denum access
);
99 virtual void unmapBufferSubDataCHROMIUM(const void*);
100 virtual void* mapTexSubImage2DCHROMIUM(
110 virtual void unmapTexSubImage2DCHROMIUM(const void*);
112 virtual void setVisibilityCHROMIUM(bool visible
);
114 virtual void setMemoryAllocationChangedCallbackCHROMIUM(
115 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM
* callback
);
117 virtual void discardFramebufferEXT(WGC3Denum target
,
118 WGC3Dsizei numAttachments
,
119 const WGC3Denum
* attachments
);
120 virtual void discardBackbufferCHROMIUM();
121 virtual void ensureBackbufferCHROMIUM();
123 virtual void copyTextureToParentTextureCHROMIUM(
124 WebGLId texture
, WebGLId parentTexture
);
126 virtual void rateLimitOffscreenContextCHROMIUM() { }
128 virtual WebString
getRequestableExtensionsCHROMIUM();
129 virtual void requestExtensionCHROMIUM(const char*);
131 virtual void blitFramebufferCHROMIUM(
132 WGC3Dint srcX0
, WGC3Dint srcY0
, WGC3Dint srcX1
, WGC3Dint srcY1
,
133 WGC3Dint dstX0
, WGC3Dint dstY0
, WGC3Dint dstX1
, WGC3Dint dstY1
,
134 WGC3Dbitfield mask
, WGC3Denum filter
);
135 virtual void renderbufferStorageMultisampleCHROMIUM(
136 WGC3Denum target
, WGC3Dsizei samples
, WGC3Denum internalformat
,
137 WGC3Dsizei width
, WGC3Dsizei height
);
139 virtual void activeTexture(WGC3Denum texture
);
140 virtual void attachShader(WebGLId program
, WebGLId shader
);
141 virtual void bindAttribLocation(
142 WebGLId program
, WGC3Duint index
, const WGC3Dchar
* name
);
143 virtual void bindBuffer(WGC3Denum target
, WebGLId buffer
);
144 virtual void bindFramebuffer(WGC3Denum target
, WebGLId framebuffer
);
145 virtual void bindRenderbuffer(
146 WGC3Denum target
, WebGLId renderbuffer
);
147 virtual void bindTexture(WGC3Denum target
, WebGLId texture
);
148 virtual void blendColor(
149 WGC3Dclampf red
, WGC3Dclampf green
, WGC3Dclampf blue
, WGC3Dclampf alpha
);
150 virtual void blendEquation(WGC3Denum mode
);
151 virtual void blendEquationSeparate(WGC3Denum modeRGB
, WGC3Denum modeAlpha
);
152 virtual void blendFunc(WGC3Denum sfactor
, WGC3Denum dfactor
);
153 virtual void blendFuncSeparate(WGC3Denum srcRGB
, WGC3Denum dstRGB
,
154 WGC3Denum srcAlpha
, WGC3Denum dstAlpha
);
156 virtual void bufferData(
157 WGC3Denum target
, WGC3Dsizeiptr size
, const void* data
, WGC3Denum usage
);
158 virtual void bufferSubData(WGC3Denum target
, WGC3Dintptr offset
,
159 WGC3Dsizeiptr size
, const void* data
);
161 virtual WGC3Denum
checkFramebufferStatus(WGC3Denum target
);
162 virtual void clear(WGC3Dbitfield mask
);
163 virtual void clearColor(
164 WGC3Dclampf red
, WGC3Dclampf green
, WGC3Dclampf blue
, WGC3Dclampf alpha
);
165 virtual void clearDepth(WGC3Dclampf depth
);
166 virtual void clearStencil(WGC3Dint s
);
167 virtual void colorMask(WGC3Dboolean red
, WGC3Dboolean green
,
168 WGC3Dboolean blue
, WGC3Dboolean alpha
);
169 virtual void compileShader(WebGLId shader
);
171 virtual void compressedTexImage2D(
174 WGC3Denum internalformat
,
178 WGC3Dsizei imageSize
,
180 virtual void compressedTexSubImage2D(
188 WGC3Dsizei imageSize
,
190 virtual void copyTexImage2D(
193 WGC3Denum internalformat
,
199 virtual void copyTexSubImage2D(
208 virtual void cullFace(WGC3Denum mode
);
209 virtual void depthFunc(WGC3Denum func
);
210 virtual void depthMask(WGC3Dboolean flag
);
211 virtual void depthRange(WGC3Dclampf zNear
, WGC3Dclampf zFar
);
212 virtual void detachShader(WebGLId program
, WebGLId shader
);
213 virtual void disable(WGC3Denum cap
);
214 virtual void disableVertexAttribArray(WGC3Duint index
);
215 virtual void drawArrays(WGC3Denum mode
, WGC3Dint first
, WGC3Dsizei count
);
216 virtual void drawElements(
222 virtual void enable(WGC3Denum cap
);
223 virtual void enableVertexAttribArray(WGC3Duint index
);
224 virtual void finish();
225 virtual void flush();
226 virtual void framebufferRenderbuffer(
228 WGC3Denum attachment
,
229 WGC3Denum renderbuffertarget
,
230 WebGLId renderbuffer
);
231 virtual void framebufferTexture2D(
233 WGC3Denum attachment
,
237 virtual void frontFace(WGC3Denum mode
);
238 virtual void generateMipmap(WGC3Denum target
);
240 virtual bool getActiveAttrib(WebGLId program
, WGC3Duint index
, ActiveInfo
&);
241 virtual bool getActiveUniform(WebGLId program
, WGC3Duint index
, ActiveInfo
&);
243 virtual void getAttachedShaders(WebGLId program
, WGC3Dsizei maxCount
,
244 WGC3Dsizei
* count
, WebGLId
* shaders
);
246 virtual WGC3Dint
getAttribLocation(WebGLId program
, const WGC3Dchar
* name
);
248 virtual void getBooleanv(WGC3Denum pname
, WGC3Dboolean
* value
);
250 virtual void getBufferParameteriv(
251 WGC3Denum target
, WGC3Denum pname
, WGC3Dint
* value
);
253 virtual Attributes
getContextAttributes();
255 virtual WGC3Denum
getError();
257 virtual bool isContextLost();
259 virtual void getFloatv(WGC3Denum pname
, WGC3Dfloat
* value
);
261 virtual void getFramebufferAttachmentParameteriv(
263 WGC3Denum attachment
,
267 virtual void getIntegerv(WGC3Denum pname
, WGC3Dint
* value
);
269 virtual void getProgramiv(
270 WebGLId program
, WGC3Denum pname
, WGC3Dint
* value
);
272 virtual WebString
getProgramInfoLog(WebGLId program
);
274 virtual void getRenderbufferParameteriv(
275 WGC3Denum target
, WGC3Denum pname
, WGC3Dint
* value
);
277 virtual void getShaderiv(
278 WebGLId shader
, WGC3Denum pname
, WGC3Dint
* value
);
280 virtual WebString
getShaderInfoLog(WebGLId shader
);
282 virtual void getShaderPrecisionFormat(
283 WGC3Denum shadertype
, WGC3Denum precisiontype
,
284 WGC3Dint
* range
, WGC3Dint
* precision
);
286 virtual WebString
getShaderSource(WebGLId shader
);
287 virtual WebString
getString(WGC3Denum name
);
289 virtual void getTexParameterfv(
290 WGC3Denum target
, WGC3Denum pname
, WGC3Dfloat
* value
);
291 virtual void getTexParameteriv(
292 WGC3Denum target
, WGC3Denum pname
, WGC3Dint
* value
);
294 virtual void getUniformfv(
295 WebGLId program
, WGC3Dint location
, WGC3Dfloat
* value
);
296 virtual void getUniformiv(
297 WebGLId program
, WGC3Dint location
, WGC3Dint
* value
);
299 virtual WGC3Dint
getUniformLocation(WebGLId program
, const WGC3Dchar
* name
);
301 virtual void getVertexAttribfv(
302 WGC3Duint index
, WGC3Denum pname
, WGC3Dfloat
* value
);
303 virtual void getVertexAttribiv(
304 WGC3Duint index
, WGC3Denum pname
, WGC3Dint
* value
);
306 virtual WGC3Dsizeiptr
getVertexAttribOffset(
307 WGC3Duint index
, WGC3Denum pname
);
309 virtual void hint(WGC3Denum target
, WGC3Denum mode
);
310 virtual WGC3Dboolean
isBuffer(WebGLId buffer
);
311 virtual WGC3Dboolean
isEnabled(WGC3Denum cap
);
312 virtual WGC3Dboolean
isFramebuffer(WebGLId framebuffer
);
313 virtual WGC3Dboolean
isProgram(WebGLId program
);
314 virtual WGC3Dboolean
isRenderbuffer(WebGLId renderbuffer
);
315 virtual WGC3Dboolean
isShader(WebGLId shader
);
316 virtual WGC3Dboolean
isTexture(WebGLId texture
);
317 virtual void lineWidth(WGC3Dfloat width
);
318 virtual void linkProgram(WebGLId program
);
319 virtual void pixelStorei(WGC3Denum pname
, WGC3Dint param
);
320 virtual void polygonOffset(WGC3Dfloat factor
, WGC3Dfloat units
);
322 virtual void readPixels(
323 WGC3Dint x
, WGC3Dint y
,
324 WGC3Dsizei width
, WGC3Dsizei height
,
329 virtual void releaseShaderCompiler();
330 virtual void renderbufferStorage(
332 WGC3Denum internalformat
,
335 virtual void sampleCoverage(WGC3Dclampf value
, WGC3Dboolean invert
);
336 virtual void scissor(
337 WGC3Dint x
, WGC3Dint y
, WGC3Dsizei width
, WGC3Dsizei height
);
338 virtual void shaderSource(WebGLId shader
, const WGC3Dchar
* source
);
339 virtual void stencilFunc(WGC3Denum func
, WGC3Dint ref
, WGC3Duint mask
);
340 virtual void stencilFuncSeparate(
341 WGC3Denum face
, WGC3Denum func
, WGC3Dint ref
, WGC3Duint mask
);
342 virtual void stencilMask(WGC3Duint mask
);
343 virtual void stencilMaskSeparate(WGC3Denum face
, WGC3Duint mask
);
344 virtual void stencilOp(WGC3Denum fail
, WGC3Denum zfail
, WGC3Denum zpass
);
345 virtual void stencilOpSeparate(
351 virtual void texImage2D(
354 WGC3Denum internalformat
,
362 virtual void texParameterf(
363 WGC3Denum target
, WGC3Denum pname
, WGC3Dfloat param
);
364 virtual void texParameteri(
365 WGC3Denum target
, WGC3Denum pname
, WGC3Dint param
);
367 virtual void texSubImage2D(
378 virtual void uniform1f(WGC3Dint location
, WGC3Dfloat x
);
379 virtual void uniform1fv(WGC3Dint location
, WGC3Dsizei count
,
380 const WGC3Dfloat
* v
);
381 virtual void uniform1i(WGC3Dint location
, WGC3Dint x
);
382 virtual void uniform1iv(WGC3Dint location
, WGC3Dsizei count
,
384 virtual void uniform2f(WGC3Dint location
, WGC3Dfloat x
, WGC3Dfloat y
);
385 virtual void uniform2fv(WGC3Dint location
, WGC3Dsizei count
,
386 const WGC3Dfloat
* v
);
387 virtual void uniform2i(WGC3Dint location
, WGC3Dint x
, WGC3Dint y
);
388 virtual void uniform2iv(WGC3Dint location
, WGC3Dsizei count
,
390 virtual void uniform3f(WGC3Dint location
,
391 WGC3Dfloat x
, WGC3Dfloat y
, WGC3Dfloat z
);
392 virtual void uniform3fv(WGC3Dint location
, WGC3Dsizei count
,
393 const WGC3Dfloat
* v
);
394 virtual void uniform3i(WGC3Dint location
, WGC3Dint x
, WGC3Dint y
, WGC3Dint z
);
395 virtual void uniform3iv(WGC3Dint location
, WGC3Dsizei count
,
397 virtual void uniform4f(WGC3Dint location
, WGC3Dfloat x
, WGC3Dfloat y
,
398 WGC3Dfloat z
, WGC3Dfloat w
);
399 virtual void uniform4fv(WGC3Dint location
, WGC3Dsizei count
,
400 const WGC3Dfloat
* v
);
401 virtual void uniform4i(WGC3Dint location
, WGC3Dint x
, WGC3Dint y
,
402 WGC3Dint z
, WGC3Dint w
);
403 virtual void uniform4iv(WGC3Dint location
, WGC3Dsizei count
,
405 virtual void uniformMatrix2fv(
406 WGC3Dint location
, WGC3Dsizei count
,
407 WGC3Dboolean transpose
, const WGC3Dfloat
* value
);
408 virtual void uniformMatrix3fv(
409 WGC3Dint location
, WGC3Dsizei count
,
410 WGC3Dboolean transpose
, const WGC3Dfloat
* value
);
411 virtual void uniformMatrix4fv(
412 WGC3Dint location
, WGC3Dsizei count
,
413 WGC3Dboolean transpose
, const WGC3Dfloat
* value
);
415 virtual void useProgram(WebGLId program
);
416 virtual void validateProgram(WebGLId program
);
418 virtual void vertexAttrib1f(WGC3Duint index
, WGC3Dfloat x
);
419 virtual void vertexAttrib1fv(WGC3Duint index
, const WGC3Dfloat
* values
);
420 virtual void vertexAttrib2f(WGC3Duint index
, WGC3Dfloat x
, WGC3Dfloat y
);
421 virtual void vertexAttrib2fv(WGC3Duint index
, const WGC3Dfloat
* values
);
422 virtual void vertexAttrib3f(
423 WGC3Duint index
, WGC3Dfloat x
, WGC3Dfloat y
, WGC3Dfloat z
);
424 virtual void vertexAttrib3fv(WGC3Duint index
, const WGC3Dfloat
* values
);
425 virtual void vertexAttrib4f(
426 WGC3Duint index
, WGC3Dfloat x
, WGC3Dfloat y
, WGC3Dfloat z
, WGC3Dfloat w
);
427 virtual void vertexAttrib4fv(WGC3Duint index
, const WGC3Dfloat
* values
);
428 virtual void vertexAttribPointer(
432 WGC3Dboolean normalized
,
436 virtual void viewport(
437 WGC3Dint x
, WGC3Dint y
, WGC3Dsizei width
, WGC3Dsizei height
);
439 // Support for buffer creation and deletion
440 virtual WebGLId
createBuffer();
441 virtual WebGLId
createFramebuffer();
442 virtual WebGLId
createProgram();
443 virtual WebGLId
createRenderbuffer();
444 virtual WebGLId
createShader(WGC3Denum
);
445 virtual WebGLId
createTexture();
447 virtual void deleteBuffer(WebGLId
);
448 virtual void deleteFramebuffer(WebGLId
);
449 virtual void deleteProgram(WebGLId
);
450 virtual void deleteRenderbuffer(WebGLId
);
451 virtual void deleteShader(WebGLId
);
452 virtual void deleteTexture(WebGLId
);
454 virtual void setContextLostCallback(
455 WebGraphicsContext3D::WebGraphicsContextLostCallback
* callback
) {}
456 virtual WGC3Denum
getGraphicsResetStatusARB();
458 virtual void setSwapBuffersCompleteCallbackCHROMIUM(
459 WebGraphicsContext3D::
460 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM
* callback
) {}
462 virtual void texImageIOSurface2DCHROMIUM(
463 WGC3Denum target
, WGC3Dint width
, WGC3Dint height
,
464 WGC3Duint ioSurfaceId
, WGC3Duint plane
);
466 virtual void texStorage2DEXT(
467 WGC3Denum target
, WGC3Dint levels
, WGC3Duint internalformat
,
468 WGC3Dint width
, WGC3Dint height
);
470 virtual WebGLId
createQueryEXT();
471 virtual void deleteQueryEXT(WebGLId query
);
472 virtual WGC3Dboolean
isQueryEXT(WebGLId query
);
473 virtual void beginQueryEXT(WGC3Denum target
, WebGLId query
);
474 virtual void endQueryEXT(WGC3Denum target
);
475 virtual void getQueryivEXT(
476 WGC3Denum target
, WGC3Denum pname
, WGC3Dint
* params
);
477 virtual void getQueryObjectuivEXT(
478 WebGLId query
, WGC3Denum pname
, WGC3Duint
* params
);
480 virtual void copyTextureCHROMIUM(WGC3Denum target
, WGC3Duint source_id
,
481 WGC3Duint dest_id
, WGC3Dint level
,
482 WGC3Denum internal_format
);
483 virtual void bindUniformLocationCHROMIUM(WebGLId program
, WGC3Dint location
,
484 const WGC3Dchar
* uniform
);
486 // CHROMIUM_shallow_flush
487 // Only applies to contexts that use the command buffer.
488 virtual void shallowFlushCHROMIUM() { }
490 virtual void genMailboxCHROMIUM(WGC3Dbyte
* mailbox
);
491 virtual void produceTextureCHROMIUM(WGC3Denum target
,
492 const WGC3Dbyte
* mailbox
);
493 virtual void consumeTextureCHROMIUM(WGC3Denum target
,
494 const WGC3Dbyte
* mailbox
);
496 virtual void bindTexImage2DCHROMIUM(WGC3Denum target
, WGC3Dint imageId
);
497 virtual void releaseTexImage2DCHROMIUM(WGC3Denum target
, WGC3Dint imageId
);
499 virtual void* mapBufferCHROMIUM(WGC3Denum target
, WGC3Denum access
);
500 virtual WGC3Dboolean
unmapBufferCHROMIUM(WGC3Denum target
);
503 virtual GrGLInterface
* onCreateGrGLInterface();
506 bool Initialize(Attributes attributes
);
509 struct ShaderSourceEntry
;
511 typedef base::hash_map
<WebGLId
, ShaderSourceEntry
*> ShaderSourceMap
;
513 bool AllocateOffscreenFrameBuffer(int width
, int height
);
514 void ClearRenderTarget();
516 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
517 void FlipVertically(unsigned char* framebuffer
,
519 unsigned int height
);
522 // Take into account the user's requested context creation attributes, in
523 // particular stencil and antialias, and determine which could or could
524 // not be honored based on the capabilities of the OpenGL implementation.
525 void ValidateAttributes();
527 // Resolve the given rectangle of the multisampled framebuffer if necessary.
528 void ResolveMultisampledFramebuffer(
529 WGC3Dint x
, WGC3Dint y
, WGC3Dsizei width
, WGC3Dsizei height
);
531 bool AngleCreateCompilers();
532 void AngleDestroyCompilers();
533 bool AngleValidateShaderSource(ShaderSourceEntry
* entry
);
535 WebGraphicsContext3D::Attributes attributes_
;
537 bool render_directly_to_web_view_
;
539 bool have_ext_framebuffer_object_
;
540 bool have_ext_framebuffer_multisample_
;
541 bool have_angle_framebuffer_multisample_
;
542 bool have_ext_oes_standard_derivatives_
;
543 bool have_ext_oes_egl_image_external_
;
547 WebGLId depth_stencil_buffer_
;
548 int cached_width_
, cached_height_
;
551 WebGLId multisample_fbo_
;
552 WebGLId multisample_depth_stencil_buffer_
;
553 WebGLId multisample_color_buffer_
;
555 // For tracking which FBO is bound
558 // For tracking which texture is bound
559 WebGLId bound_texture_
;
561 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
562 unsigned char* scanline_
;
565 // Errors raised by synthesizeGLError().
566 std::list
<WGC3Denum
> synthetic_errors_list_
;
567 std::set
<WGC3Denum
> synthetic_errors_set_
;
569 scoped_refptr
<gfx::GLContext
> gl_context_
;
570 scoped_refptr
<gfx::GLSurface
> gl_surface_
;
572 ShaderSourceMap shader_source_map_
;
574 ShHandle fragment_compiler_
;
575 ShHandle vertex_compiler_
;
579 } // namespace webkit
581 #endif // WEBKIT_GPU_WEBGRAPHICSCONTEXT3D_IN_PROCESS_IMPL_H_