Home | History | Annotate | Download | only in src
      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
      6  * are met:
      7  *
      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.
     13  *
     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.
     24  */
     25 
     26 #ifndef GraphicsContext3DInternal_h
     27 #define GraphicsContext3DInternal_h
     28 
     29 #include "GraphicsContext3D.h"
     30 #include <wtf/HashSet.h>
     31 #include <wtf/OwnPtr.h>
     32 #if USE(SKIA)
     33 #include "SkBitmap.h"
     34 #endif
     35 
     36 namespace WebKit {
     37 class WebGraphicsContext3D;
     38 class WebViewImpl;
     39 } // namespace WebKit
     40 
     41 namespace WebCore {
     42 
     43 class Extensions3DChromium;
     44 #if USE(ACCELERATED_COMPOSITING)
     45 class WebGLLayerChromium;
     46 #endif
     47 class GraphicsContextLostCallbackAdapter;
     48 
     49 class GraphicsContext3DInternal {
     50 public:
     51     GraphicsContext3DInternal();
     52     ~GraphicsContext3DInternal();
     53 
     54     bool initialize(GraphicsContext3D::Attributes attrs, HostWindow* hostWindow, bool renderDirectlyToHostWindow);
     55 
     56     // Helper function to provide access to the lower-level WebGraphicsContext3D,
     57     // which is needed for subordinate contexts like WebGL's to share resources
     58     // with the compositor's context.
     59     static WebKit::WebGraphicsContext3D* extractWebGraphicsContext3D(GraphicsContext3D* context);
     60 
     61     PlatformGraphicsContext3D platformGraphicsContext3D() const;
     62     Platform3DObject platformTexture() const;
     63 
     64     bool makeContextCurrent();
     65 
     66     void reshape(int width, int height);
     67     IntSize getInternalFramebufferSize();
     68 
     69     void markContextChanged();
     70     bool layerComposited() const;
     71     void markLayerComposited();
     72 
     73     void paintRenderingResultsToCanvas(CanvasRenderingContext*);
     74     PassRefPtr<ImageData> paintRenderingResultsToImageData();
     75     bool paintsIntoCanvasBuffer() const;
     76 
     77     void prepareTexture();
     78 
     79 #if USE(ACCELERATED_COMPOSITING)
     80     WebGLLayerChromium* platformLayer() const;
     81 #endif
     82     bool isGLES2Compliant() const;
     83 
     84     void releaseShaderCompiler();
     85     bool isContextLost();
     86 
     87     //----------------------------------------------------------------------
     88     // Entry points for WebGL.
     89     //
     90     void activeTexture(GC3Denum texture);
     91     void attachShader(Platform3DObject program, Platform3DObject shader);
     92     void bindAttribLocation(Platform3DObject, GC3Duint index, const String& name);
     93     void bindBuffer(GC3Denum target, Platform3DObject);
     94     void bindFramebuffer(GC3Denum target, Platform3DObject);
     95     void bindRenderbuffer(GC3Denum target, Platform3DObject);
     96     void bindTexture(GC3Denum target, Platform3DObject);
     97     void blendColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
     98     void blendEquation(GC3Denum mode);
     99     void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
    100     void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
    101     void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
    102 
    103     void bufferData(GC3Denum target, GC3Dsizeiptr size, GC3Denum usage);
    104     void bufferData(GC3Denum target, GC3Dsizeiptr size, const void* data, GC3Denum usage);
    105     void bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr size, const void* data);
    106 
    107     GC3Denum checkFramebufferStatus(GC3Denum target);
    108     void clear(GC3Dbitfield mask);
    109     void clearColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
    110     void clearDepth(GC3Dclampf depth);
    111     void clearStencil(GC3Dint s);
    112     void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
    113     void compileShader(Platform3DObject);
    114 
    115     void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
    116     void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
    117     void cullFace(GC3Denum mode);
    118     void depthFunc(GC3Denum func);
    119     void depthMask(GC3Dboolean flag);
    120     void depthRange(GC3Dclampf zNear, GC3Dclampf zFar);
    121     void detachShader(Platform3DObject, Platform3DObject);
    122     void disable(GC3Denum cap);
    123     void disableVertexAttribArray(GC3Duint index);
    124     void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count);
    125     void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
    126 
    127     void enable(GC3Denum cap);
    128     void enableVertexAttribArray(GC3Duint index);
    129     void finish();
    130     void flush();
    131     void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, Platform3DObject);
    132     void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level);
    133     void frontFace(GC3Denum mode);
    134     void generateMipmap(GC3Denum target);
    135 
    136     bool getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo&);
    137     bool getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo&);
    138     void getAttachedShaders(Platform3DObject program, GC3Dsizei maxCount, GC3Dsizei* count, Platform3DObject* shaders);
    139     GC3Dint getAttribLocation(Platform3DObject, const String& name);
    140     void getBooleanv(GC3Denum pname, GC3Dboolean* value);
    141     void getBufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
    142     GraphicsContext3D::Attributes getContextAttributes();
    143     GC3Denum getError();
    144     void getFloatv(GC3Denum pname, GC3Dfloat* value);
    145     void getFramebufferAttachmentParameteriv(GC3Denum target, GC3Denum attachment, GC3Denum pname, GC3Dint* value);
    146     void getIntegerv(GC3Denum pname, GC3Dint* value);
    147     void getProgramiv(Platform3DObject program, GC3Denum pname, GC3Dint* value);
    148     String getProgramInfoLog(Platform3DObject);
    149     void getRenderbufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
    150     void getShaderiv(Platform3DObject, GC3Denum pname, GC3Dint* value);
    151     String getShaderInfoLog(Platform3DObject);
    152 
    153     String getShaderSource(Platform3DObject);
    154     String getString(GC3Denum name);
    155     void getTexParameterfv(GC3Denum target, GC3Denum pname, GC3Dfloat* value);
    156     void getTexParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
    157     void getUniformfv(Platform3DObject program, GC3Dint location, GC3Dfloat* value);
    158     void getUniformiv(Platform3DObject program, GC3Dint location, GC3Dint* value);
    159     GC3Dint getUniformLocation(Platform3DObject, const String& name);
    160     void getVertexAttribfv(GC3Duint index, GC3Denum pname, GC3Dfloat* value);
    161     void getVertexAttribiv(GC3Duint index, GC3Denum pname, GC3Dint* value);
    162     GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
    163 
    164     void hint(GC3Denum target, GC3Denum mode);
    165     GC3Dboolean isBuffer(Platform3DObject);
    166     GC3Dboolean isEnabled(GC3Denum cap);
    167     GC3Dboolean isFramebuffer(Platform3DObject);
    168     GC3Dboolean isProgram(Platform3DObject);
    169     GC3Dboolean isRenderbuffer(Platform3DObject);
    170     GC3Dboolean isShader(Platform3DObject);
    171     GC3Dboolean isTexture(Platform3DObject);
    172     void lineWidth(GC3Dfloat);
    173     void linkProgram(Platform3DObject);
    174     void pixelStorei(GC3Denum pname, GC3Dint param);
    175     void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
    176 
    177     void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
    178 
    179     void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
    180     void sampleCoverage(GC3Dclampf value, GC3Dboolean invert);
    181     void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
    182     void shaderSource(Platform3DObject, const String& string);
    183     void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
    184     void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
    185     void stencilMask(GC3Duint mask);
    186     void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
    187     void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
    188     void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
    189 
    190     // texImage2D return false on any error rather than using an ExceptionCode.
    191     bool texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
    192     void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
    193     void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
    194     void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels);
    195 
    196     void uniform1f(GC3Dint location, GC3Dfloat x);
    197     void uniform1fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei size);
    198     void uniform1i(GC3Dint location, GC3Dint x);
    199     void uniform1iv(GC3Dint location, GC3Dint* v, GC3Dsizei size);
    200     void uniform2f(GC3Dint location, GC3Dfloat x, float y);
    201     void uniform2fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei size);
    202     void uniform2i(GC3Dint location, GC3Dint x, GC3Dint y);
    203     void uniform2iv(GC3Dint location, GC3Dint* v, GC3Dsizei size);
    204     void uniform3f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
    205     void uniform3fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei size);
    206     void uniform3i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z);
    207     void uniform3iv(GC3Dint location, GC3Dint* v, GC3Dsizei size);
    208     void uniform4f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
    209     void uniform4fv(GC3Dint location, GC3Dfloat* v, GC3Dsizei size);
    210     void uniform4i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w);
    211     void uniform4iv(GC3Dint location, GC3Dint* v, GC3Dsizei size);
    212     void uniformMatrix2fv(GC3Dint location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size);
    213     void uniformMatrix3fv(GC3Dint location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size);
    214     void uniformMatrix4fv(GC3Dint location, GC3Dboolean transpose, GC3Dfloat* value, GC3Dsizei size);
    215 
    216     void useProgram(Platform3DObject);
    217     void validateProgram(Platform3DObject);
    218 
    219     void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
    220     void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values);
    221     void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
    222     void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values);
    223     void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
    224     void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values);
    225     void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
    226     void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values);
    227     void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
    228                              GC3Dsizei stride, GC3Dintptr offset);
    229 
    230     void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
    231 
    232     Platform3DObject createBuffer();
    233     Platform3DObject createFramebuffer();
    234     Platform3DObject createProgram();
    235     Platform3DObject createRenderbuffer();
    236     Platform3DObject createShader(GC3Denum);
    237     Platform3DObject createTexture();
    238 
    239     void deleteBuffer(Platform3DObject);
    240     void deleteFramebuffer(Platform3DObject);
    241     void deleteProgram(Platform3DObject);
    242     void deleteRenderbuffer(Platform3DObject);
    243     void deleteShader(Platform3DObject);
    244     void deleteTexture(Platform3DObject);
    245 
    246     void synthesizeGLError(GC3Denum error);
    247 
    248     void setContextLostCallback(PassOwnPtr<GraphicsContext3D::ContextLostCallback>);
    249 
    250     // Extensions3D support.
    251     Extensions3D* getExtensions();
    252     bool supportsExtension(const String& name);
    253     bool ensureExtensionEnabled(const String& name);
    254     bool isExtensionEnabled(const String& name);
    255 
    256     // EXT_texture_format_BGRA8888
    257     bool supportsBGRA();
    258 
    259     // GL_CHROMIUM_map_sub
    260     bool supportsMapSubCHROMIUM();
    261     void* mapBufferSubDataCHROMIUM(GC3Denum target, GC3Dsizeiptr offset, GC3Dsizei size, GC3Denum access);
    262     void unmapBufferSubDataCHROMIUM(const void*);
    263     void* mapTexSubImage2DCHROMIUM(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, GC3Denum access);
    264     void unmapTexSubImage2DCHROMIUM(const void*);
    265 
    266     // GL_CHROMIUM_copy_texture_to_parent_texture
    267     bool supportsCopyTextureToParentTextureCHROMIUM();
    268     void copyTextureToParentTextureCHROMIUM(Platform3DObject texture, Platform3DObject parentTexture);
    269 
    270     // GL_CHROMIUM_framebuffer_multisample
    271     void blitFramebufferCHROMIUM(GC3Dint srcX0, GC3Dint srcY0, GC3Dint srcX1, GC3Dint srcY1, GC3Dint dstX0, GC3Dint dstY0, GC3Dint dstX1, GC3Dint dstY1, GC3Dbitfield mask, GC3Denum filter);
    272     void renderbufferStorageMultisampleCHROMIUM(GC3Denum target, GC3Dsizei samples, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
    273 
    274     // Latch support
    275     void getParentToChildLatchCHROMIUM(GC3Duint* latchId);
    276     void getChildToParentLatchCHROMIUM(GC3Duint* latchId);
    277     void waitLatchCHROMIUM(GC3Duint latchId);
    278     void setLatchCHROMIUM(GC3Duint latchId);
    279 
    280 private:
    281     OwnPtr<WebKit::WebGraphicsContext3D> m_impl;
    282     OwnPtr<Extensions3DChromium> m_extensions;
    283     OwnPtr<GraphicsContextLostCallbackAdapter> m_contextLostCallbackAdapter;
    284     WebKit::WebViewImpl* m_webViewImpl;
    285     bool m_initializedAvailableExtensions;
    286     HashSet<String> m_enabledExtensions;
    287     HashSet<String> m_requestableExtensions;
    288     bool m_layerComposited;
    289 #if USE(ACCELERATED_COMPOSITING)
    290     RefPtr<WebGLLayerChromium> m_compositingLayer;
    291 #endif
    292 #if USE(SKIA)
    293     // If the width and height of the Canvas's backing store don't
    294     // match those that we were given in the most recent call to
    295     // reshape(), then we need an intermediate bitmap to read back the
    296     // frame buffer into. This seems to happen when CSS styles are
    297     // used to resize the Canvas.
    298     SkBitmap m_resizingBitmap;
    299 #endif
    300 
    301 #if USE(CG)
    302     unsigned char* m_renderOutput;
    303 #endif
    304 
    305     void initializeExtensions();
    306 };
    307 
    308 } // namespace WebCore
    309 
    310 #endif // GraphicsContext3D_h
    311