WebKit/chromium/src/WebGraphicsContext3DDefaultImpl.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "WebGraphicsContext3D.h"
       
    37 
       
    38 #include <wtf/ListHashSet.h>
       
    39 
       
    40 #if OS(WINDOWS)
       
    41 #include <windows.h>
       
    42 #endif
       
    43 
       
    44 #include "GL/glew.h"
       
    45 #if OS(WINDOWS)
       
    46 #include "GL/wglew.h"
       
    47 #endif
       
    48 
       
    49 #if PLATFORM(CG)
       
    50 #include <OpenGL/OpenGL.h>
       
    51 #else
       
    52 #define FLIP_FRAMEBUFFER_VERTICALLY
       
    53 #endif
       
    54 
       
    55 #if OS(LINUX)
       
    56 #include "GL/glxew.h"
       
    57 #endif
       
    58 
       
    59 namespace WebKit {
       
    60 
       
    61 // The default implementation of WebGL. In Chromium, using this class
       
    62 // requires the sandbox to be disabled, which is strongly discouraged.
       
    63 // It is provided for support of test_shell and any Chromium ports
       
    64 // where an in-renderer WebGL implementation would be helpful.
       
    65 
       
    66 class WebGraphicsContext3DDefaultImpl : public WebGraphicsContext3D {
       
    67 public:
       
    68     WebGraphicsContext3DDefaultImpl();
       
    69     virtual ~WebGraphicsContext3DDefaultImpl();
       
    70 
       
    71     //----------------------------------------------------------------------
       
    72     // WebGraphicsContext3D methods
       
    73     virtual bool initialize(WebGraphicsContext3D::Attributes attributes, WebView*);
       
    74     virtual bool makeContextCurrent();
       
    75 
       
    76     virtual int width();
       
    77     virtual int height();
       
    78 
       
    79     virtual int sizeInBytes(int type);
       
    80 
       
    81     virtual bool isGLES2Compliant();
       
    82 
       
    83     virtual void reshape(int width, int height);
       
    84 
       
    85     virtual bool readBackFramebuffer(unsigned char* pixels, size_t bufferSize);
       
    86 
       
    87     virtual unsigned int getPlatformTextureId();
       
    88     virtual void prepareTexture();
       
    89 
       
    90     virtual void synthesizeGLError(unsigned long error);
       
    91 
       
    92     virtual void activeTexture(unsigned long texture);
       
    93     virtual void attachShader(WebGLId program, WebGLId shader);
       
    94     virtual void bindAttribLocation(WebGLId program, unsigned long index, const char* name);
       
    95     virtual void bindBuffer(unsigned long target, WebGLId buffer);
       
    96     virtual void bindFramebuffer(unsigned long target, WebGLId framebuffer);
       
    97     virtual void bindRenderbuffer(unsigned long target, WebGLId renderbuffer);
       
    98     virtual void bindTexture(unsigned long target, WebGLId texture);
       
    99     virtual void blendColor(double red, double green, double blue, double alpha);
       
   100     virtual void blendEquation(unsigned long mode);
       
   101     virtual void blendEquationSeparate(unsigned long modeRGB, unsigned long modeAlpha);
       
   102     virtual void blendFunc(unsigned long sfactor, unsigned long dfactor);
       
   103     virtual void blendFuncSeparate(unsigned long srcRGB, unsigned long dstRGB, unsigned long srcAlpha, unsigned long dstAlpha);
       
   104 
       
   105     virtual void bufferData(unsigned long target, int size, const void* data, unsigned long usage);
       
   106     virtual void bufferSubData(unsigned long target, long offset, int size, const void* data);
       
   107 
       
   108     virtual unsigned long checkFramebufferStatus(unsigned long target);
       
   109     virtual void clear(unsigned long mask);
       
   110     virtual void clearColor(double red, double green, double blue, double alpha);
       
   111     virtual void clearDepth(double depth);
       
   112     virtual void clearStencil(long s);
       
   113     virtual void colorMask(bool red, bool green, bool blue, bool alpha);
       
   114     virtual void compileShader(WebGLId shader);
       
   115 
       
   116     virtual void copyTexImage2D(unsigned long target, long level, unsigned long internalformat, long x, long y, unsigned long width, unsigned long height, long border);
       
   117     virtual void copyTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, long x, long y, unsigned long width, unsigned long height);
       
   118     virtual void cullFace(unsigned long mode);
       
   119     virtual void depthFunc(unsigned long func);
       
   120     virtual void depthMask(bool flag);
       
   121     virtual void depthRange(double zNear, double zFar);
       
   122     virtual void detachShader(WebGLId program, WebGLId shader);
       
   123     virtual void disable(unsigned long cap);
       
   124     virtual void disableVertexAttribArray(unsigned long index);
       
   125     virtual void drawArrays(unsigned long mode, long first, long count);
       
   126     virtual void drawElements(unsigned long mode, unsigned long count, unsigned long type, long offset);
       
   127 
       
   128     virtual void enable(unsigned long cap);
       
   129     virtual void enableVertexAttribArray(unsigned long index);
       
   130     virtual void finish();
       
   131     virtual void flush();
       
   132     virtual void framebufferRenderbuffer(unsigned long target, unsigned long attachment, unsigned long renderbuffertarget, WebGLId renderbuffer);
       
   133     virtual void framebufferTexture2D(unsigned long target, unsigned long attachment, unsigned long textarget, WebGLId texture, long level);
       
   134     virtual void frontFace(unsigned long mode);
       
   135     virtual void generateMipmap(unsigned long target);
       
   136 
       
   137     virtual bool getActiveAttrib(WebGLId program, unsigned long index, ActiveInfo&);
       
   138     virtual bool getActiveUniform(WebGLId program, unsigned long index, ActiveInfo&);
       
   139 
       
   140     virtual void getAttachedShaders(WebGLId program, int maxCount, int* count, unsigned int* shaders);
       
   141 
       
   142     virtual int  getAttribLocation(WebGLId program, const char* name);
       
   143 
       
   144     virtual void getBooleanv(unsigned long pname, unsigned char* value);
       
   145 
       
   146     virtual void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
       
   147 
       
   148     virtual Attributes getContextAttributes();
       
   149 
       
   150     virtual unsigned long getError();
       
   151 
       
   152     virtual void getFloatv(unsigned long pname, float* value);
       
   153 
       
   154     virtual void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
       
   155 
       
   156     virtual void getIntegerv(unsigned long pname, int* value);
       
   157 
       
   158     virtual void getProgramiv(WebGLId program, unsigned long pname, int* value);
       
   159 
       
   160     virtual WebString getProgramInfoLog(WebGLId program);
       
   161 
       
   162     virtual void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
       
   163 
       
   164     virtual void getShaderiv(WebGLId shader, unsigned long pname, int* value);
       
   165 
       
   166     virtual WebString getShaderInfoLog(WebGLId shader);
       
   167 
       
   168     // TBD
       
   169     // void glGetShaderPrecisionFormat (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
       
   170 
       
   171     virtual WebString getShaderSource(WebGLId shader);
       
   172     virtual WebString getString(unsigned long name);
       
   173 
       
   174     virtual void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
       
   175     virtual void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
       
   176 
       
   177     virtual void getUniformfv(WebGLId program, long location, float* value);
       
   178     virtual void getUniformiv(WebGLId program, long location, int* value);
       
   179 
       
   180     virtual long getUniformLocation(WebGLId program, const char* name);
       
   181 
       
   182     virtual void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
       
   183     virtual void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
       
   184 
       
   185     virtual long getVertexAttribOffset(unsigned long index, unsigned long pname);
       
   186 
       
   187     virtual void hint(unsigned long target, unsigned long mode);
       
   188     virtual bool isBuffer(WebGLId buffer);
       
   189     virtual bool isEnabled(unsigned long cap);
       
   190     virtual bool isFramebuffer(WebGLId framebuffer);
       
   191     virtual bool isProgram(WebGLId program);
       
   192     virtual bool isRenderbuffer(WebGLId renderbuffer);
       
   193     virtual bool isShader(WebGLId shader);
       
   194     virtual bool isTexture(WebGLId texture);
       
   195     virtual void lineWidth(double);
       
   196     virtual void linkProgram(WebGLId program);
       
   197     virtual void pixelStorei(unsigned long pname, long param);
       
   198     virtual void polygonOffset(double factor, double units);
       
   199 
       
   200     virtual void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* pixels);
       
   201 
       
   202     virtual void releaseShaderCompiler();
       
   203     virtual void renderbufferStorage(unsigned long target, unsigned long internalformat, unsigned long width, unsigned long height);
       
   204     virtual void sampleCoverage(double value, bool invert);
       
   205     virtual void scissor(long x, long y, unsigned long width, unsigned long height);
       
   206     virtual void shaderSource(WebGLId shader, const char* string);
       
   207     virtual void stencilFunc(unsigned long func, long ref, unsigned long mask);
       
   208     virtual void stencilFuncSeparate(unsigned long face, unsigned long func, long ref, unsigned long mask);
       
   209     virtual void stencilMask(unsigned long mask);
       
   210     virtual void stencilMaskSeparate(unsigned long face, unsigned long mask);
       
   211     virtual void stencilOp(unsigned long fail, unsigned long zfail, unsigned long zpass);
       
   212     virtual void stencilOpSeparate(unsigned long face, unsigned long fail, unsigned long zfail, unsigned long zpass);
       
   213 
       
   214     virtual void texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, const void* pixels);
       
   215 
       
   216     virtual void texParameterf(unsigned target, unsigned pname, float param);
       
   217     virtual void texParameteri(unsigned target, unsigned pname, int param);
       
   218 
       
   219     virtual void texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, const void* pixels);
       
   220 
       
   221     virtual void uniform1f(long location, float x);
       
   222     virtual void uniform1fv(long location, int count, float* v);
       
   223     virtual void uniform1i(long location, int x);
       
   224     virtual void uniform1iv(long location, int count, int* v);
       
   225     virtual void uniform2f(long location, float x, float y);
       
   226     virtual void uniform2fv(long location, int count, float* v);
       
   227     virtual void uniform2i(long location, int x, int y);
       
   228     virtual void uniform2iv(long location, int count, int* v);
       
   229     virtual void uniform3f(long location, float x, float y, float z);
       
   230     virtual void uniform3fv(long location, int count, float* v);
       
   231     virtual void uniform3i(long location, int x, int y, int z);
       
   232     virtual void uniform3iv(long location, int count, int* v);
       
   233     virtual void uniform4f(long location, float x, float y, float z, float w);
       
   234     virtual void uniform4fv(long location, int count, float* v);
       
   235     virtual void uniform4i(long location, int x, int y, int z, int w);
       
   236     virtual void uniform4iv(long location, int count, int* v);
       
   237     virtual void uniformMatrix2fv(long location, int count, bool transpose, const float* value);
       
   238     virtual void uniformMatrix3fv(long location, int count, bool transpose, const float* value);
       
   239     virtual void uniformMatrix4fv(long location, int count, bool transpose, const float* value);
       
   240 
       
   241     virtual void useProgram(WebGLId program);
       
   242     virtual void validateProgram(WebGLId program);
       
   243 
       
   244     virtual void vertexAttrib1f(unsigned long indx, float x);
       
   245     virtual void vertexAttrib1fv(unsigned long indx, const float* values);
       
   246     virtual void vertexAttrib2f(unsigned long indx, float x, float y);
       
   247     virtual void vertexAttrib2fv(unsigned long indx, const float* values);
       
   248     virtual void vertexAttrib3f(unsigned long indx, float x, float y, float z);
       
   249     virtual void vertexAttrib3fv(unsigned long indx, const float* values);
       
   250     virtual void vertexAttrib4f(unsigned long indx, float x, float y, float z, float w);
       
   251     virtual void vertexAttrib4fv(unsigned long indx, const float* values);
       
   252     virtual void vertexAttribPointer(unsigned long indx, int size, int type, bool normalized,
       
   253                                      unsigned long stride, unsigned long offset);
       
   254 
       
   255     virtual void viewport(long x, long y, unsigned long width, unsigned long height);
       
   256 
       
   257     // Support for buffer creation and deletion
       
   258     virtual unsigned createBuffer();
       
   259     virtual unsigned createFramebuffer();
       
   260     virtual unsigned createProgram();
       
   261     virtual unsigned createRenderbuffer();
       
   262     virtual unsigned createShader(unsigned long);
       
   263     virtual unsigned createTexture();
       
   264 
       
   265     virtual void deleteBuffer(unsigned);
       
   266     virtual void deleteFramebuffer(unsigned);
       
   267     virtual void deleteProgram(unsigned);
       
   268     virtual void deleteRenderbuffer(unsigned);
       
   269     virtual void deleteShader(unsigned);
       
   270     virtual void deleteTexture(unsigned);
       
   271 
       
   272 private:
       
   273     WebGraphicsContext3D::Attributes m_attributes;
       
   274     bool m_initialized;
       
   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 #ifdef FLIP_FRAMEBUFFER_VERTICALLY
       
   289     unsigned char* m_scanline;
       
   290     void flipVertically(unsigned char* framebuffer,
       
   291                         unsigned int width,
       
   292                         unsigned int height);
       
   293 #endif
       
   294 
       
   295     // Take into account the user's requested context creation attributes, in
       
   296     // particular stencil and antialias, and determine which could or could
       
   297     // not be honored based on the capabilities of the OpenGL implementation.
       
   298     void validateAttributes();
       
   299 
       
   300     // Note: we aren't currently using this information, but we will
       
   301     // need to in order to verify that all enabled vertex arrays have
       
   302     // a valid buffer bound -- to avoid crashes on certain cards.
       
   303     unsigned int m_boundArrayBuffer;
       
   304     class VertexAttribPointerState {
       
   305     public:
       
   306         VertexAttribPointerState();
       
   307 
       
   308         bool enabled;
       
   309         unsigned long buffer;
       
   310         unsigned long indx;
       
   311         int size;
       
   312         int type;
       
   313         bool normalized;
       
   314         unsigned long stride;
       
   315         unsigned long offset;
       
   316     };
       
   317 
       
   318     enum {
       
   319         NumTrackedPointerStates = 2
       
   320     };
       
   321     VertexAttribPointerState m_vertexAttribPointerState[NumTrackedPointerStates];
       
   322 
       
   323     // Errors raised by synthesizeGLError().
       
   324     ListHashSet<unsigned long> m_syntheticErrors;
       
   325 
       
   326     static bool s_initializedGLEW;
       
   327 #if OS(WINDOWS)
       
   328     HWND  m_canvasWindow;
       
   329     HDC   m_canvasDC;
       
   330     HGLRC m_contextObj;
       
   331 #elif PLATFORM(CG)
       
   332     CGLPBufferObj m_pbuffer;
       
   333     CGLContextObj m_contextObj;
       
   334     unsigned char* m_renderOutput;
       
   335 #elif OS(LINUX)
       
   336     GLXContext m_contextObj;
       
   337     GLXPbuffer m_pbuffer;
       
   338 
       
   339     // In order to avoid problems caused by linking against libGL, we
       
   340     // dynamically look up all the symbols we need.
       
   341     // http://code.google.com/p/chromium/issues/detail?id=16800
       
   342     class GLConnection {
       
   343       public:
       
   344         ~GLConnection();
       
   345 
       
   346         static GLConnection* create();
       
   347 
       
   348         GLXFBConfig* chooseFBConfig(int screen, const int *attrib_list, int *nelements)
       
   349         {
       
   350             return m_glXChooseFBConfig(m_display, screen, attrib_list, nelements);
       
   351         }
       
   352 
       
   353         GLXContext createNewContext(GLXFBConfig config, int renderType, GLXContext shareList, Bool direct)
       
   354         {
       
   355             return m_glXCreateNewContext(m_display, config, renderType, shareList, direct);
       
   356         }
       
   357 
       
   358         GLXPbuffer createPbuffer(GLXFBConfig config, const int *attribList)
       
   359         {
       
   360             return m_glXCreatePbuffer(m_display, config, attribList);
       
   361         }
       
   362 
       
   363         void destroyPbuffer(GLXPbuffer pbuf)
       
   364         {
       
   365             m_glXDestroyPbuffer(m_display, pbuf);
       
   366         }
       
   367 
       
   368         Bool makeCurrent(GLXDrawable drawable, GLXContext ctx)
       
   369         {
       
   370             return m_glXMakeCurrent(m_display, drawable, ctx);
       
   371         }
       
   372 
       
   373         void destroyContext(GLXContext ctx)
       
   374         {
       
   375             m_glXDestroyContext(m_display, ctx);
       
   376         }
       
   377 
       
   378         GLXContext getCurrentContext()
       
   379         {
       
   380             return m_glXGetCurrentContext();
       
   381         }
       
   382 
       
   383       private:
       
   384         Display* m_display;
       
   385         void* m_libGL;
       
   386         PFNGLXCHOOSEFBCONFIGPROC m_glXChooseFBConfig;
       
   387         PFNGLXCREATENEWCONTEXTPROC m_glXCreateNewContext;
       
   388         PFNGLXCREATEPBUFFERPROC m_glXCreatePbuffer;
       
   389         PFNGLXDESTROYPBUFFERPROC m_glXDestroyPbuffer;
       
   390         typedef Bool (* PFNGLXMAKECURRENTPROC)(Display* dpy, GLXDrawable drawable, GLXContext ctx);
       
   391         PFNGLXMAKECURRENTPROC m_glXMakeCurrent;
       
   392         typedef void (* PFNGLXDESTROYCONTEXTPROC)(Display* dpy, GLXContext ctx);
       
   393         PFNGLXDESTROYCONTEXTPROC m_glXDestroyContext;
       
   394         typedef GLXContext (* PFNGLXGETCURRENTCONTEXTPROC)(void);
       
   395         PFNGLXGETCURRENTCONTEXTPROC m_glXGetCurrentContext;
       
   396 
       
   397         GLConnection(Display* display,
       
   398                      void* libGL,
       
   399                      PFNGLXCHOOSEFBCONFIGPROC chooseFBConfig,
       
   400                      PFNGLXCREATENEWCONTEXTPROC createNewContext,
       
   401                      PFNGLXCREATEPBUFFERPROC createPbuffer,
       
   402                      PFNGLXDESTROYPBUFFERPROC destroyPbuffer,
       
   403                      PFNGLXMAKECURRENTPROC makeCurrent,
       
   404                      PFNGLXDESTROYCONTEXTPROC destroyContext,
       
   405                      PFNGLXGETCURRENTCONTEXTPROC getCurrentContext)
       
   406             : m_libGL(libGL)
       
   407             , m_display(display)
       
   408             , m_glXChooseFBConfig(chooseFBConfig)
       
   409             , m_glXCreateNewContext(createNewContext)
       
   410             , m_glXCreatePbuffer(createPbuffer)
       
   411             , m_glXDestroyPbuffer(destroyPbuffer)
       
   412             , m_glXMakeCurrent(makeCurrent)
       
   413             , m_glXDestroyContext(destroyContext)
       
   414             , m_glXGetCurrentContext(getCurrentContext)
       
   415         {
       
   416         }
       
   417     };
       
   418 
       
   419     static GLConnection* s_gl;
       
   420 #else
       
   421     #error Must port WebGraphicsContext3DDefaultImpl to your platform
       
   422 #endif
       
   423 };
       
   424 
       
   425 } // namespace WebKit
       
   426 
       
   427 #endif // ENABLE(3D_CANVAS)
       
   428 
       
   429 #endif