Home | History | Annotate | Download | only in public
      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 WebGraphicsContext3D_h
     32 #define WebGraphicsContext3D_h
     33 
     34 #include "WebCommon.h"
     35 #include "WebNonCopyable.h"
     36 #include "WebString.h"
     37 
     38 #define USE_WGC3D_TYPES
     39 
     40 namespace WebKit {
     41 
     42 // WGC3D types match the corresponding GL types as defined in OpenGL ES 2.0
     43 // header file gl2.h from khronos.org.
     44 typedef char WGC3Dchar;
     45 typedef unsigned int WGC3Denum;
     46 typedef unsigned char WGC3Dboolean;
     47 typedef unsigned int WGC3Dbitfield;
     48 typedef signed char WGC3Dbyte;
     49 typedef unsigned char WGC3Dubyte;
     50 typedef short WGC3Dshort;
     51 typedef unsigned short WGC3Dushort;
     52 typedef int WGC3Dint;
     53 typedef int WGC3Dsizei;
     54 typedef unsigned int WGC3Duint;
     55 typedef float WGC3Dfloat;
     56 typedef float WGC3Dclampf;
     57 typedef signed long int WGC3Dintptr;
     58 typedef signed long int WGC3Dsizeiptr;
     59 
     60 // Typedef for server-side objects like OpenGL textures and program objects.
     61 typedef WGC3Duint WebGLId;
     62 
     63 class WebView;
     64 
     65 // This interface abstracts the operations performed by the
     66 // GraphicsContext3D in order to implement WebGL. Nearly all of the
     67 // methods exposed on this interface map directly to entry points in
     68 // the OpenGL ES 2.0 API.
     69 
     70 class WebGraphicsContext3D : public WebNonCopyable {
     71 public:
     72     // Return value from getActiveUniform and getActiveAttrib.
     73     struct ActiveInfo {
     74         WebString name;
     75         WGC3Denum type;
     76         WGC3Dint size;
     77     };
     78 
     79     // Context creation attributes.
     80     struct Attributes {
     81         Attributes()
     82             : alpha(true)
     83             , depth(true)
     84             , stencil(true)
     85             , antialias(true)
     86             , premultipliedAlpha(true)
     87             , canRecoverFromContextLoss(true)
     88         {
     89         }
     90 
     91         bool alpha;
     92         bool depth;
     93         bool stencil;
     94         bool antialias;
     95         bool premultipliedAlpha;
     96         bool canRecoverFromContextLoss;
     97     };
     98 
     99     class WebGraphicsContextLostCallback {
    100     public:
    101         virtual void onContextLost() = 0;
    102         virtual ~WebGraphicsContextLostCallback() {}
    103     };
    104 
    105     // This destructor needs to be public so that using classes can destroy instances if initialization fails.
    106     virtual ~WebGraphicsContext3D() {}
    107 
    108     // Initializes the graphics context; should be the first operation performed
    109     // on newly-constructed instances. Returns true on success.
    110     virtual bool initialize(Attributes, WebView*, bool renderDirectlyToWebView) = 0;
    111 
    112     // Makes the OpenGL context current on the current thread. Returns true on
    113     // success.
    114     virtual bool makeContextCurrent() = 0;
    115 
    116     // The size of the region into which this WebGraphicsContext3D is rendering.
    117     // Returns the last values passed to reshape().
    118     virtual int width() = 0;
    119     virtual int height() = 0;
    120 
    121     // Resizes the region into which this WebGraphicsContext3D is drawing.
    122     virtual void reshape(int width, int height) = 0;
    123 
    124     // Query whether it is built on top of compliant GLES2 implementation.
    125     virtual bool isGLES2Compliant() = 0;
    126 
    127     // Helper for software compositing path. Reads back the frame buffer into
    128     // the memory region pointed to by "pixels" with size "bufferSize". It is
    129     // expected that the storage for "pixels" covers (4 * width * height) bytes.
    130     // Returns true on success.
    131     virtual bool readBackFramebuffer(unsigned char* pixels, size_t bufferSize) = 0;
    132 
    133     // Returns the id of the texture which is used for storing the contents of
    134     // the framebuffer associated with this context. This texture is accessible
    135     // by the gpu-based page compositor.
    136     virtual WebGLId getPlatformTextureId() = 0;
    137 
    138     // Copies the contents of the off-screen render target used by the WebGL
    139     // context to the corresponding texture used by the compositor.
    140     virtual void prepareTexture() = 0;
    141 
    142     // Synthesizes an OpenGL error which will be returned from a
    143     // later call to getError. This is used to emulate OpenGL ES
    144     // 2.0 behavior on the desktop and to enforce additional error
    145     // checking mandated by WebGL.
    146     //
    147     // Per the behavior of glGetError, this stores at most one
    148     // instance of any given error, and returns them from calls to
    149     // getError in the order they were added.
    150     virtual void synthesizeGLError(WGC3Denum) = 0;
    151 
    152     virtual bool isContextLost() = 0;
    153 
    154     // GL_CHROMIUM_map_sub
    155     virtual void* mapBufferSubDataCHROMIUM(WGC3Denum target, WGC3Dintptr offset, WGC3Dsizeiptr size, WGC3Denum access) = 0;
    156     virtual void unmapBufferSubDataCHROMIUM(const void*) = 0;
    157     virtual void* mapTexSubImage2DCHROMIUM(WGC3Denum target, WGC3Dint level, WGC3Dint xoffset, WGC3Dint yoffset, WGC3Dsizei width, WGC3Dsizei height, WGC3Denum format, WGC3Denum type, WGC3Denum access) = 0;
    158     virtual void unmapTexSubImage2DCHROMIUM(const void*) = 0;
    159 
    160     // GL_CHROMIUM_copy_texture_to_parent_texture
    161     virtual void copyTextureToParentTextureCHROMIUM(WebGLId texture, WebGLId parentTexture) = 0;
    162 
    163     // GL_CHROMIUM_request_extension
    164     virtual WebString getRequestableExtensionsCHROMIUM() = 0;
    165     virtual void requestExtensionCHROMIUM(const char*) = 0;
    166 
    167     // GL_CHROMIUM_framebuffer_multisample
    168     virtual void blitFramebufferCHROMIUM(WGC3Dint srcX0, WGC3Dint srcY0, WGC3Dint srcX1, WGC3Dint srcY1, WGC3Dint dstX0, WGC3Dint dstY0, WGC3Dint dstX1, WGC3Dint dstY1, WGC3Dbitfield mask, WGC3Denum filter) = 0;
    169     virtual void renderbufferStorageMultisampleCHROMIUM(WGC3Denum target, WGC3Dsizei samples, WGC3Denum internalformat, WGC3Dsizei width, WGC3Dsizei height) = 0;
    170 
    171     // GL_CHROMIUM_latch
    172     virtual void getParentToChildLatchCHROMIUM(WGC3Duint* latchId) = 0;
    173     virtual void getChildToParentLatchCHROMIUM(WGC3Duint* latchId) = 0;
    174     virtual void waitLatchCHROMIUM(WGC3Duint latchId) = 0;
    175     virtual void setLatchCHROMIUM(WGC3Duint latchId) = 0;
    176 
    177     // The entry points below map directly to the OpenGL ES 2.0 API.
    178     // See: http://www.khronos.org/registry/gles/
    179     // and: http://www.khronos.org/opengles/sdk/docs/man/
    180     virtual void activeTexture(WGC3Denum texture) = 0;
    181     virtual void attachShader(WebGLId program, WebGLId shader) = 0;
    182     virtual void bindAttribLocation(WebGLId program, WGC3Duint index, const WGC3Dchar* name) = 0;
    183     virtual void bindBuffer(WGC3Denum target, WebGLId buffer) = 0;
    184     virtual void bindFramebuffer(WGC3Denum target, WebGLId framebuffer) = 0;
    185     virtual void bindRenderbuffer(WGC3Denum target, WebGLId renderbuffer) = 0;
    186     virtual void bindTexture(WGC3Denum target, WebGLId texture) = 0;
    187     virtual void blendColor(WGC3Dclampf red, WGC3Dclampf green, WGC3Dclampf blue, WGC3Dclampf alpha) = 0;
    188     virtual void blendEquation(WGC3Denum mode) = 0;
    189     virtual void blendEquationSeparate(WGC3Denum modeRGB, WGC3Denum modeAlpha) = 0;
    190     virtual void blendFunc(WGC3Denum sfactor, WGC3Denum dfactor) = 0;
    191     virtual void blendFuncSeparate(WGC3Denum srcRGB, WGC3Denum dstRGB, WGC3Denum srcAlpha, WGC3Denum dstAlpha) = 0;
    192 
    193     virtual void bufferData(WGC3Denum target, WGC3Dsizeiptr size, const void* data, WGC3Denum usage) = 0;
    194     virtual void bufferSubData(WGC3Denum target, WGC3Dintptr offset, WGC3Dsizeiptr size, const void* data) = 0;
    195 
    196     virtual WGC3Denum checkFramebufferStatus(WGC3Denum target) = 0;
    197     virtual void clear(WGC3Dbitfield mask) = 0;
    198     virtual void clearColor(WGC3Dclampf red, WGC3Dclampf green, WGC3Dclampf blue, WGC3Dclampf alpha) = 0;
    199     virtual void clearDepth(WGC3Dclampf depth) = 0;
    200     virtual void clearStencil(WGC3Dint s) = 0;
    201     virtual void colorMask(WGC3Dboolean red, WGC3Dboolean green, WGC3Dboolean blue, WGC3Dboolean alpha) = 0;
    202     virtual void compileShader(WebGLId shader) = 0;
    203 
    204     virtual void copyTexImage2D(WGC3Denum target, WGC3Dint level, WGC3Denum internalformat, WGC3Dint x, WGC3Dint y, WGC3Dsizei width, WGC3Dsizei height, WGC3Dint border) = 0;
    205     virtual void copyTexSubImage2D(WGC3Denum target, WGC3Dint level, WGC3Dint xoffset, WGC3Dint yoffset, WGC3Dint x, WGC3Dint y, WGC3Dsizei width, WGC3Dsizei height) = 0;
    206     virtual void cullFace(WGC3Denum mode) = 0;
    207     virtual void depthFunc(WGC3Denum func) = 0;
    208     virtual void depthMask(WGC3Dboolean flag) = 0;
    209     virtual void depthRange(WGC3Dclampf zNear, WGC3Dclampf zFar) = 0;
    210     virtual void detachShader(WebGLId program, WebGLId shader) = 0;
    211     virtual void disable(WGC3Denum cap) = 0;
    212     virtual void disableVertexAttribArray(WGC3Duint index) = 0;
    213     virtual void drawArrays(WGC3Denum mode, WGC3Dint first, WGC3Dsizei count) = 0;
    214     virtual void drawElements(WGC3Denum mode, WGC3Dsizei count, WGC3Denum type, WGC3Dintptr offset) = 0;
    215 
    216     virtual void enable(WGC3Denum cap) = 0;
    217     virtual void enableVertexAttribArray(WGC3Duint index) = 0;
    218     virtual void finish() = 0;
    219     virtual void flush() = 0;
    220     virtual void framebufferRenderbuffer(WGC3Denum target, WGC3Denum attachment, WGC3Denum renderbuffertarget, WebGLId renderbuffer) = 0;
    221     virtual void framebufferTexture2D(WGC3Denum target, WGC3Denum attachment, WGC3Denum textarget, WebGLId texture, WGC3Dint level) = 0;
    222     virtual void frontFace(WGC3Denum mode) = 0;
    223     virtual void generateMipmap(WGC3Denum target) = 0;
    224 
    225     virtual bool getActiveAttrib(WebGLId program, WGC3Duint index, ActiveInfo&) = 0;
    226     virtual bool getActiveUniform(WebGLId program, WGC3Duint index, ActiveInfo&) = 0;
    227     virtual void getAttachedShaders(WebGLId program, WGC3Dsizei maxCount, WGC3Dsizei* count, WebGLId* shaders) = 0;
    228     virtual WGC3Dint getAttribLocation(WebGLId program, const WGC3Dchar* name) = 0;
    229     virtual void getBooleanv(WGC3Denum pname, WGC3Dboolean* value) = 0;
    230     virtual void getBufferParameteriv(WGC3Denum target, WGC3Denum pname, WGC3Dint* value) = 0;
    231     virtual Attributes getContextAttributes() = 0;
    232     virtual WGC3Denum getError() = 0;
    233     virtual void getFloatv(WGC3Denum pname, WGC3Dfloat* value) = 0;
    234     virtual void getFramebufferAttachmentParameteriv(WGC3Denum target, WGC3Denum attachment, WGC3Denum pname, WGC3Dint* value) = 0;
    235     virtual void getIntegerv(WGC3Denum pname, WGC3Dint* value) = 0;
    236     virtual void getProgramiv(WebGLId program, WGC3Denum pname, WGC3Dint* value) = 0;
    237     virtual WebString getProgramInfoLog(WebGLId program) = 0;
    238     virtual void getRenderbufferParameteriv(WGC3Denum target, WGC3Denum pname, WGC3Dint* value) = 0;
    239     virtual void getShaderiv(WebGLId shader, WGC3Denum pname, WGC3Dint* value) = 0;
    240     virtual WebString getShaderInfoLog(WebGLId shader) = 0;
    241 
    242     // TBD
    243     // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
    244 
    245     virtual WebString getShaderSource(WebGLId shader) = 0;
    246     virtual WebString getString(WGC3Denum name) = 0;
    247     virtual void getTexParameterfv(WGC3Denum target, WGC3Denum pname, WGC3Dfloat* value) = 0;
    248     virtual void getTexParameteriv(WGC3Denum target, WGC3Denum pname, WGC3Dint* value) = 0;
    249     virtual void getUniformfv(WebGLId program, WGC3Dint location, WGC3Dfloat* value) = 0;
    250     virtual void getUniformiv(WebGLId program, WGC3Dint location, WGC3Dint* value) = 0;
    251     virtual WGC3Dint getUniformLocation(WebGLId program, const WGC3Dchar* name) = 0;
    252     virtual void getVertexAttribfv(WGC3Duint index, WGC3Denum pname, WGC3Dfloat* value) = 0;
    253     virtual void getVertexAttribiv(WGC3Duint index, WGC3Denum pname, WGC3Dint* value) = 0;
    254     virtual WGC3Dsizeiptr getVertexAttribOffset(WGC3Duint index, WGC3Denum pname) = 0;
    255 
    256     virtual void hint(WGC3Denum target, WGC3Denum mode) = 0;
    257     virtual WGC3Dboolean isBuffer(WebGLId buffer) = 0;
    258     virtual WGC3Dboolean isEnabled(WGC3Denum cap) = 0;
    259     virtual WGC3Dboolean isFramebuffer(WebGLId framebuffer) = 0;
    260     virtual WGC3Dboolean isProgram(WebGLId program) = 0;
    261     virtual WGC3Dboolean isRenderbuffer(WebGLId renderbuffer) = 0;
    262     virtual WGC3Dboolean isShader(WebGLId shader) = 0;
    263     virtual WGC3Dboolean isTexture(WebGLId texture) = 0;
    264     virtual void lineWidth(WGC3Dfloat) = 0;
    265     virtual void linkProgram(WebGLId program) = 0;
    266     virtual void pixelStorei(WGC3Denum pname, WGC3Dint param) = 0;
    267     virtual void polygonOffset(WGC3Dfloat factor, WGC3Dfloat units) = 0;
    268 
    269     virtual void readPixels(WGC3Dint x, WGC3Dint y, WGC3Dsizei width, WGC3Dsizei height, WGC3Denum format, WGC3Denum type, void* pixels) = 0;
    270 
    271     virtual void releaseShaderCompiler() = 0;
    272 
    273     virtual void renderbufferStorage(WGC3Denum target, WGC3Denum internalformat, WGC3Dsizei width, WGC3Dsizei height) = 0;
    274     virtual void sampleCoverage(WGC3Dclampf value, WGC3Dboolean invert) = 0;
    275     virtual void scissor(WGC3Dint x, WGC3Dint y, WGC3Dsizei width, WGC3Dsizei height) = 0;
    276     virtual void shaderSource(WebGLId shader, const WGC3Dchar* string) = 0;
    277     virtual void stencilFunc(WGC3Denum func, WGC3Dint ref, WGC3Duint mask) = 0;
    278     virtual void stencilFuncSeparate(WGC3Denum face, WGC3Denum func, WGC3Dint ref, WGC3Duint mask) = 0;
    279     virtual void stencilMask(WGC3Duint mask) = 0;
    280     virtual void stencilMaskSeparate(WGC3Denum face, WGC3Duint mask) = 0;
    281     virtual void stencilOp(WGC3Denum fail, WGC3Denum zfail, WGC3Denum zpass) = 0;
    282     virtual void stencilOpSeparate(WGC3Denum face, WGC3Denum fail, WGC3Denum zfail, WGC3Denum zpass) = 0;
    283 
    284     virtual void texImage2D(WGC3Denum target, WGC3Dint level, WGC3Denum internalformat, WGC3Dsizei width, WGC3Dsizei height, WGC3Dint border, WGC3Denum format, WGC3Denum type, const void* pixels) = 0;
    285 
    286     virtual void texParameterf(WGC3Denum target, WGC3Denum pname, WGC3Dfloat param) = 0;
    287     virtual void texParameteri(WGC3Denum target, WGC3Denum pname, WGC3Dint param) = 0;
    288 
    289     virtual void texSubImage2D(WGC3Denum target, WGC3Dint level, WGC3Dint xoffset, WGC3Dint yoffset, WGC3Dsizei width, WGC3Dsizei height, WGC3Denum format, WGC3Denum type, const void* pixels) = 0;
    290 
    291     virtual void uniform1f(WGC3Dint location, WGC3Dfloat x) = 0;
    292     virtual void uniform1fv(WGC3Dint location, WGC3Dsizei count, const WGC3Dfloat* v) = 0;
    293     virtual void uniform1i(WGC3Dint location, WGC3Dint x) = 0;
    294     virtual void uniform1iv(WGC3Dint location, WGC3Dsizei count, const WGC3Dint* v) = 0;
    295     virtual void uniform2f(WGC3Dint location, WGC3Dfloat x, WGC3Dfloat y) = 0;
    296     virtual void uniform2fv(WGC3Dint location, WGC3Dsizei count, const WGC3Dfloat* v) = 0;
    297     virtual void uniform2i(WGC3Dint location, WGC3Dint x, WGC3Dint y) = 0;
    298     virtual void uniform2iv(WGC3Dint location, WGC3Dsizei count, const WGC3Dint* v) = 0;
    299     virtual void uniform3f(WGC3Dint location, WGC3Dfloat x, WGC3Dfloat y, WGC3Dfloat z) = 0;
    300     virtual void uniform3fv(WGC3Dint location, WGC3Dsizei count, const WGC3Dfloat* v) = 0;
    301     virtual void uniform3i(WGC3Dint location, WGC3Dint x, WGC3Dint y, WGC3Dint z) = 0;
    302     virtual void uniform3iv(WGC3Dint location, WGC3Dsizei count, const WGC3Dint* v) = 0;
    303     virtual void uniform4f(WGC3Dint location, WGC3Dfloat x, WGC3Dfloat y, WGC3Dfloat z, WGC3Dfloat w) = 0;
    304     virtual void uniform4fv(WGC3Dint location, WGC3Dsizei count, const WGC3Dfloat* v) = 0;
    305     virtual void uniform4i(WGC3Dint location, WGC3Dint x, WGC3Dint y, WGC3Dint z, WGC3Dint w) = 0;
    306     virtual void uniform4iv(WGC3Dint location, WGC3Dsizei count, const WGC3Dint* v) = 0;
    307     virtual void uniformMatrix2fv(WGC3Dint location, WGC3Dsizei count, WGC3Dboolean transpose, const WGC3Dfloat* value) = 0;
    308     virtual void uniformMatrix3fv(WGC3Dint location, WGC3Dsizei count, WGC3Dboolean transpose, const WGC3Dfloat* value) = 0;
    309     virtual void uniformMatrix4fv(WGC3Dint location, WGC3Dsizei count, WGC3Dboolean transpose, const WGC3Dfloat* value) = 0;
    310 
    311     virtual void useProgram(WebGLId program) = 0;
    312     virtual void validateProgram(WebGLId program) = 0;
    313 
    314     virtual void vertexAttrib1f(WGC3Duint index, WGC3Dfloat x) = 0;
    315     virtual void vertexAttrib1fv(WGC3Duint index, const WGC3Dfloat* values) = 0;
    316     virtual void vertexAttrib2f(WGC3Duint index, WGC3Dfloat x, WGC3Dfloat y) = 0;
    317     virtual void vertexAttrib2fv(WGC3Duint index, const WGC3Dfloat* values) = 0;
    318     virtual void vertexAttrib3f(WGC3Duint index, WGC3Dfloat x, WGC3Dfloat y, WGC3Dfloat z) = 0;
    319     virtual void vertexAttrib3fv(WGC3Duint index, const WGC3Dfloat* values) = 0;
    320     virtual void vertexAttrib4f(WGC3Duint index, WGC3Dfloat x, WGC3Dfloat y, WGC3Dfloat z, WGC3Dfloat w) = 0;
    321     virtual void vertexAttrib4fv(WGC3Duint index, const WGC3Dfloat* values) = 0;
    322     virtual void vertexAttribPointer(WGC3Duint index, WGC3Dint size, WGC3Denum type, WGC3Dboolean normalized,
    323                                      WGC3Dsizei stride, WGC3Dintptr offset) = 0;
    324 
    325     virtual void viewport(WGC3Dint x, WGC3Dint y, WGC3Dsizei width, WGC3Dsizei height) = 0;
    326 
    327     // Support for buffer creation and deletion.
    328     virtual WebGLId createBuffer() = 0;
    329     virtual WebGLId createFramebuffer() = 0;
    330     virtual WebGLId createProgram() = 0;
    331     virtual WebGLId createRenderbuffer() = 0;
    332     virtual WebGLId createShader(WGC3Denum) = 0;
    333     virtual WebGLId createTexture() = 0;
    334 
    335     virtual void deleteBuffer(WebGLId) = 0;
    336     virtual void deleteFramebuffer(WebGLId) = 0;
    337     virtual void deleteProgram(WebGLId) = 0;
    338     virtual void deleteRenderbuffer(WebGLId) = 0;
    339     virtual void deleteShader(WebGLId) = 0;
    340     virtual void deleteTexture(WebGLId) = 0;
    341 
    342     virtual void setContextLostCallback(WebGraphicsContextLostCallback* callback) {}
    343 };
    344 
    345 } // namespace WebKit
    346 
    347 #endif
    348