Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2009 Apple 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  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef GraphicsContext3D_h
     27 #define GraphicsContext3D_h
     28 
     29 #include "platform/PlatformExport.h"
     30 #include "platform/geometry/IntRect.h"
     31 #include "platform/graphics/Extensions3D.h"
     32 #include "platform/graphics/GraphicsTypes3D.h"
     33 #include "platform/graphics/Image.h"
     34 #include "platform/weborigin/KURL.h"
     35 #include "third_party/khronos/GLES2/gl2.h"
     36 #include "third_party/khronos/GLES2/gl2ext.h"
     37 #include "third_party/skia/include/core/SkBitmap.h"
     38 #include "wtf/HashMap.h"
     39 #include "wtf/HashSet.h"
     40 #include "wtf/ListHashSet.h"
     41 #include "wtf/Noncopyable.h"
     42 #include "wtf/OwnPtr.h"
     43 #include "wtf/PassOwnPtr.h"
     44 #include "wtf/RefCounted.h"
     45 #include "wtf/text/WTFString.h"
     46 
     47 // FIXME: Find a better way to avoid the name confliction for NO_ERROR.
     48 #if OS(WIN)
     49 #undef NO_ERROR
     50 #endif
     51 
     52 class GrContext;
     53 
     54 namespace blink {
     55 class WebGraphicsContext3D;
     56 class WebGraphicsContext3DProvider;
     57 }
     58 
     59 namespace WebCore {
     60 class DrawingBuffer;
     61 class Extensions3D;
     62 class GraphicsContext3DContextLostCallbackAdapter;
     63 class GraphicsContext3DErrorMessageCallbackAdapter;
     64 class Image;
     65 class ImageBuffer;
     66 class IntRect;
     67 class IntSize;
     68 
     69 struct ActiveInfo {
     70     String name;
     71     GC3Denum type;
     72     GC3Dint size;
     73 };
     74 
     75 class PLATFORM_EXPORT GraphicsContext3D : public RefCounted<GraphicsContext3D> {
     76 public:
     77     // Context creation attributes.
     78     struct Attributes {
     79         Attributes()
     80             : alpha(true)
     81             , depth(true)
     82             , stencil(false)
     83             , antialias(true)
     84             , premultipliedAlpha(true)
     85             , preserveDrawingBuffer(false)
     86             , noExtensions(false)
     87             , shareResources(true)
     88             , preferDiscreteGPU(false)
     89             , failIfMajorPerformanceCaveat(false)
     90         {
     91         }
     92 
     93         bool alpha;
     94         bool depth;
     95         bool stencil;
     96         bool antialias;
     97         bool premultipliedAlpha;
     98         bool preserveDrawingBuffer;
     99         bool noExtensions;
    100         bool shareResources;
    101         bool preferDiscreteGPU;
    102         bool failIfMajorPerformanceCaveat;
    103         KURL topDocumentURL;
    104     };
    105 
    106     class ContextLostCallback {
    107     public:
    108         virtual void onContextLost() = 0;
    109         virtual ~ContextLostCallback() {}
    110     };
    111 
    112     class ErrorMessageCallback {
    113     public:
    114         virtual void onErrorMessage(const String& message, GC3Dint id) = 0;
    115         virtual ~ErrorMessageCallback() { }
    116     };
    117 
    118     void setContextLostCallback(PassOwnPtr<ContextLostCallback>);
    119     void setErrorMessageCallback(PassOwnPtr<ErrorMessageCallback>);
    120 
    121     static PassRefPtr<GraphicsContext3D> create(Attributes);
    122 
    123     // Callers must make the context current before using it AND check that the context was created successfully
    124     // via ContextLost before using the context in any way. Once made current on a thread, the context cannot
    125     // be used on any other thread.
    126     static PassRefPtr<GraphicsContext3D> createGraphicsContextFromWebContext(PassOwnPtr<blink::WebGraphicsContext3D>, bool preserveDrawingBuffer = false);
    127     static PassRefPtr<GraphicsContext3D> createGraphicsContextFromProvider(PassOwnPtr<blink::WebGraphicsContext3DProvider>, bool preserveDrawingBuffer = false);
    128 
    129     ~GraphicsContext3D();
    130 
    131     GrContext* grContext();
    132     blink::WebGraphicsContext3D* webContext() const { return m_impl; }
    133 
    134     bool makeContextCurrent();
    135 
    136     uint32_t lastFlushID();
    137 
    138     // Helper to texImage2D with pixel==0 case: pixels are initialized to 0.
    139     // Return true if no GL error is synthesized.
    140     // By default, alignment is 4, the OpenGL default setting.
    141     bool texImage2DResourceSafe(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint alignment = 4);
    142 
    143     //----------------------------------------------------------------------
    144     // Helpers for texture uploading and pixel readback.
    145     //
    146 
    147     // Computes the components per pixel and bytes per component
    148     // for the given format and type combination. Returns false if
    149     // either was an invalid enum.
    150     static bool computeFormatAndTypeParameters(GC3Denum format,
    151                                                GC3Denum type,
    152                                                unsigned int* componentsPerPixel,
    153                                                unsigned int* bytesPerComponent);
    154 
    155     // Computes the image size in bytes. If paddingInBytes is not null, padding
    156     // is also calculated in return. Returns NO_ERROR if succeed, otherwise
    157     // return the suggested GL error indicating the cause of the failure:
    158     //   INVALID_VALUE if width/height is negative or overflow happens.
    159     //   INVALID_ENUM if format/type is illegal.
    160     static GC3Denum computeImageSizeInBytes(GC3Denum format,
    161                                      GC3Denum type,
    162                                      GC3Dsizei width,
    163                                      GC3Dsizei height,
    164                                      GC3Dint alignment,
    165                                      unsigned int* imageSizeInBytes,
    166                                      unsigned int* paddingInBytes);
    167 
    168     // Attempt to enumerate all possible native image formats to
    169     // reduce the amount of temporary allocations during texture
    170     // uploading. This enum must be public because it is accessed
    171     // by non-member functions.
    172     enum DataFormat {
    173         DataFormatRGBA8 = 0,
    174         DataFormatRGBA16F,
    175         DataFormatRGBA32F,
    176         DataFormatRGB8,
    177         DataFormatRGB16F,
    178         DataFormatRGB32F,
    179         DataFormatBGR8,
    180         DataFormatBGRA8,
    181         DataFormatARGB8,
    182         DataFormatABGR8,
    183         DataFormatRGBA5551,
    184         DataFormatRGBA4444,
    185         DataFormatRGB565,
    186         DataFormatR8,
    187         DataFormatR16F,
    188         DataFormatR32F,
    189         DataFormatRA8,
    190         DataFormatRA16F,
    191         DataFormatRA32F,
    192         DataFormatAR8,
    193         DataFormatA8,
    194         DataFormatA16F,
    195         DataFormatA32F,
    196         DataFormatNumFormats
    197     };
    198 
    199     // Check if the format is one of the formats from the ImageData or DOM elements.
    200     // The formats from ImageData is always RGBA8.
    201     // The formats from DOM elements vary with Graphics ports. It can only be RGBA8 or BGRA8.
    202     static ALWAYS_INLINE bool srcFormatComeFromDOMElementOrImageData(DataFormat SrcFormat)
    203     {
    204     return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
    205     }
    206 
    207     //----------------------------------------------------------------------
    208     // Entry points for WebGL.
    209     //
    210 
    211     void activeTexture(GC3Denum texture);
    212     void attachShader(Platform3DObject program, Platform3DObject shader);
    213     void bindAttribLocation(Platform3DObject, GC3Duint index, const String& name);
    214     void bindBuffer(GC3Denum target, Platform3DObject);
    215     void bindFramebuffer(GC3Denum target, Platform3DObject);
    216     void bindRenderbuffer(GC3Denum target, Platform3DObject);
    217     void bindTexture(GC3Denum target, Platform3DObject);
    218     void blendColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
    219     void blendEquation(GC3Denum mode);
    220     void blendEquationSeparate(GC3Denum modeRGB, GC3Denum modeAlpha);
    221     void blendFunc(GC3Denum sfactor, GC3Denum dfactor);
    222     void blendFuncSeparate(GC3Denum srcRGB, GC3Denum dstRGB, GC3Denum srcAlpha, GC3Denum dstAlpha);
    223 
    224     void bufferData(GC3Denum target, GC3Dsizeiptr size, GC3Denum usage);
    225     void bufferData(GC3Denum target, GC3Dsizeiptr size, const void* data, GC3Denum usage);
    226     void bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr size, const void* data);
    227 
    228     GC3Denum checkFramebufferStatus(GC3Denum target);
    229     void clear(GC3Dbitfield mask);
    230     void clearColor(GC3Dclampf red, GC3Dclampf green, GC3Dclampf blue, GC3Dclampf alpha);
    231     void clearDepth(GC3Dclampf depth);
    232     void clearStencil(GC3Dint s);
    233     void colorMask(GC3Dboolean red, GC3Dboolean green, GC3Dboolean blue, GC3Dboolean alpha);
    234     void compileShader(Platform3DObject);
    235 
    236     void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Dsizei imageSize, const void* data);
    237     void compressedTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Dsizei imageSize, const void* data);
    238     void copyTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Dint border);
    239     void copyTexSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
    240     void cullFace(GC3Denum mode);
    241     void depthFunc(GC3Denum func);
    242     void depthMask(GC3Dboolean flag);
    243     void depthRange(GC3Dclampf zNear, GC3Dclampf zFar);
    244     void detachShader(Platform3DObject, Platform3DObject);
    245     void disable(GC3Denum cap);
    246     void disableVertexAttribArray(GC3Duint index);
    247     void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count);
    248     void drawElements(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dintptr offset);
    249 
    250     void enable(GC3Denum cap);
    251     void enableVertexAttribArray(GC3Duint index);
    252     void finish();
    253     void flush();
    254     void framebufferRenderbuffer(GC3Denum target, GC3Denum attachment, GC3Denum renderbuffertarget, Platform3DObject);
    255     void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level);
    256     void frontFace(GC3Denum mode);
    257     void generateMipmap(GC3Denum target);
    258 
    259     bool getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo&);
    260     bool getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo&);
    261     void getAttachedShaders(Platform3DObject program, GC3Dsizei maxCount, GC3Dsizei* count, Platform3DObject* shaders);
    262     GC3Dint getAttribLocation(Platform3DObject, const String& name);
    263     void getBooleanv(GC3Denum pname, GC3Dboolean* value);
    264     void getBufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
    265     Attributes getContextAttributes();
    266     GC3Denum getError();
    267     void getFloatv(GC3Denum pname, GC3Dfloat* value);
    268     void getFramebufferAttachmentParameteriv(GC3Denum target, GC3Denum attachment, GC3Denum pname, GC3Dint* value);
    269     void getIntegerv(GC3Denum pname, GC3Dint* value);
    270     void getProgramiv(Platform3DObject program, GC3Denum pname, GC3Dint* value);
    271     String getProgramInfoLog(Platform3DObject);
    272     void getRenderbufferParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
    273     void getShaderiv(Platform3DObject, GC3Denum pname, GC3Dint* value);
    274     String getShaderInfoLog(Platform3DObject);
    275     void getShaderPrecisionFormat(GC3Denum shaderType, GC3Denum precisionType, GC3Dint* range, GC3Dint* precision);
    276     String getShaderSource(Platform3DObject);
    277     String getString(GC3Denum name);
    278     void getTexParameterfv(GC3Denum target, GC3Denum pname, GC3Dfloat* value);
    279     void getTexParameteriv(GC3Denum target, GC3Denum pname, GC3Dint* value);
    280     void getUniformfv(Platform3DObject program, GC3Dint location, GC3Dfloat* value);
    281     void getUniformiv(Platform3DObject program, GC3Dint location, GC3Dint* value);
    282     GC3Dint getUniformLocation(Platform3DObject, const String& name);
    283     void getVertexAttribfv(GC3Duint index, GC3Denum pname, GC3Dfloat* value);
    284     void getVertexAttribiv(GC3Duint index, GC3Denum pname, GC3Dint* value);
    285     GC3Dsizeiptr getVertexAttribOffset(GC3Duint index, GC3Denum pname);
    286 
    287     void hint(GC3Denum target, GC3Denum mode);
    288     GC3Dboolean isBuffer(Platform3DObject);
    289     GC3Dboolean isEnabled(GC3Denum cap);
    290     GC3Dboolean isFramebuffer(Platform3DObject);
    291     GC3Dboolean isProgram(Platform3DObject);
    292     GC3Dboolean isRenderbuffer(Platform3DObject);
    293     GC3Dboolean isShader(Platform3DObject);
    294     GC3Dboolean isTexture(Platform3DObject);
    295     void lineWidth(GC3Dfloat);
    296     void linkProgram(Platform3DObject);
    297     void pixelStorei(GC3Denum pname, GC3Dint param);
    298     void polygonOffset(GC3Dfloat factor, GC3Dfloat units);
    299 
    300     void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data);
    301 
    302     void releaseShaderCompiler();
    303 
    304     void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height);
    305     void sampleCoverage(GC3Dclampf value, GC3Dboolean invert);
    306     void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
    307     void shaderSource(Platform3DObject, const String& string);
    308     void stencilFunc(GC3Denum func, GC3Dint ref, GC3Duint mask);
    309     void stencilFuncSeparate(GC3Denum face, GC3Denum func, GC3Dint ref, GC3Duint mask);
    310     void stencilMask(GC3Duint mask);
    311     void stencilMaskSeparate(GC3Denum face, GC3Duint mask);
    312     void stencilOp(GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
    313     void stencilOpSeparate(GC3Denum face, GC3Denum fail, GC3Denum zfail, GC3Denum zpass);
    314 
    315     void texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels);
    316     void texParameterf(GC3Denum target, GC3Denum pname, GC3Dfloat param);
    317     void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param);
    318     void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels);
    319 
    320     void uniform1f(GC3Dint location, GC3Dfloat x);
    321     void uniform1fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
    322     void uniform1i(GC3Dint location, GC3Dint x);
    323     void uniform1iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
    324     void uniform2f(GC3Dint location, GC3Dfloat x, GC3Dfloat y);
    325     void uniform2fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
    326     void uniform2i(GC3Dint location, GC3Dint x, GC3Dint y);
    327     void uniform2iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
    328     void uniform3f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
    329     void uniform3fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
    330     void uniform3i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z);
    331     void uniform3iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
    332     void uniform4f(GC3Dint location, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
    333     void uniform4fv(GC3Dint location, GC3Dsizei, GC3Dfloat* v);
    334     void uniform4i(GC3Dint location, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w);
    335     void uniform4iv(GC3Dint location, GC3Dsizei, GC3Dint* v);
    336     void uniformMatrix2fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
    337     void uniformMatrix3fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
    338     void uniformMatrix4fv(GC3Dint location, GC3Dsizei, GC3Dboolean transpose, GC3Dfloat* value);
    339 
    340     void useProgram(Platform3DObject);
    341     void validateProgram(Platform3DObject);
    342 
    343     void vertexAttrib1f(GC3Duint index, GC3Dfloat x);
    344     void vertexAttrib1fv(GC3Duint index, GC3Dfloat* values);
    345     void vertexAttrib2f(GC3Duint index, GC3Dfloat x, GC3Dfloat y);
    346     void vertexAttrib2fv(GC3Duint index, GC3Dfloat* values);
    347     void vertexAttrib3f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z);
    348     void vertexAttrib3fv(GC3Duint index, GC3Dfloat* values);
    349     void vertexAttrib4f(GC3Duint index, GC3Dfloat x, GC3Dfloat y, GC3Dfloat z, GC3Dfloat w);
    350     void vertexAttrib4fv(GC3Duint index, GC3Dfloat* values);
    351     void vertexAttribPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dboolean normalized,
    352                              GC3Dsizei stride, GC3Dintptr offset);
    353 
    354     void viewport(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height);
    355 
    356     void markContextChanged();
    357     void markLayerComposited();
    358     bool layerComposited() const;
    359 
    360     void paintRenderingResultsToCanvas(ImageBuffer*, DrawingBuffer*);
    361     PassRefPtr<Uint8ClampedArray> paintRenderingResultsToImageData(DrawingBuffer*, int&, int&);
    362 
    363     // Support for buffer creation and deletion
    364     Platform3DObject createBuffer();
    365     Platform3DObject createFramebuffer();
    366     Platform3DObject createProgram();
    367     Platform3DObject createRenderbuffer();
    368     Platform3DObject createShader(GC3Denum);
    369     Platform3DObject createTexture();
    370 
    371     void deleteBuffer(Platform3DObject);
    372     void deleteFramebuffer(Platform3DObject);
    373     void deleteProgram(Platform3DObject);
    374     void deleteRenderbuffer(Platform3DObject);
    375     void deleteShader(Platform3DObject);
    376     void deleteTexture(Platform3DObject);
    377 
    378     // Synthesizes an OpenGL error which will be returned from a
    379     // later call to getError. This is used to emulate OpenGL ES
    380     // 2.0 behavior on the desktop and to enforce additional error
    381     // checking mandated by WebGL.
    382     //
    383     // Per the behavior of glGetError, this stores at most one
    384     // instance of any given error, and returns them from calls to
    385     // getError in the order they were added.
    386     void synthesizeGLError(GC3Denum error);
    387 
    388     // Support for extensions. Returns a non-null object, though not
    389     // all methods it contains may necessarily be supported on the
    390     // current hardware. Must call Extensions3D::supports() to
    391     // determine this.
    392     Extensions3D* extensions();
    393 
    394     static unsigned getClearBitsByFormat(GC3Denum);
    395 
    396     enum ChannelBits {
    397         ChannelRed = 1,
    398         ChannelGreen = 2,
    399         ChannelBlue = 4,
    400         ChannelAlpha = 8,
    401         ChannelDepth = 16,
    402         ChannelStencil = 32,
    403         ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
    404         ChannelRGBA = ChannelRGB | ChannelAlpha,
    405     };
    406 
    407     static unsigned getChannelBitsByFormat(GC3Denum);
    408 
    409     // Possible alpha operations that may need to occur during
    410     // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
    411     // be removed.
    412     enum AlphaOp {
    413         AlphaDoNothing = 0,
    414         AlphaDoPremultiply = 1,
    415         AlphaDoUnmultiply = 2
    416     };
    417 
    418     enum ImageHtmlDomSource {
    419         HtmlDomImage = 0,
    420         HtmlDomCanvas = 1,
    421         HtmlDomVideo = 2,
    422         HtmlDomNone = 3
    423     };
    424 
    425     class PLATFORM_EXPORT ImageExtractor {
    426     public:
    427         ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
    428 
    429         ~ImageExtractor();
    430 
    431         bool extractSucceeded() { return m_extractSucceeded; }
    432         const void* imagePixelData() { return m_imagePixelData; }
    433         unsigned imageWidth() { return m_imageWidth; }
    434         unsigned imageHeight() { return m_imageHeight; }
    435         DataFormat imageSourceFormat() { return m_imageSourceFormat; }
    436         AlphaOp imageAlphaOp() { return m_alphaOp; }
    437         unsigned imageSourceUnpackAlignment() { return m_imageSourceUnpackAlignment; }
    438         ImageHtmlDomSource imageHtmlDomSource() { return m_imageHtmlDomSource; }
    439     private:
    440         // Extract the image and keeps track of its status, such as width, height, Source Alignment, format and AlphaOp etc.
    441         // This needs to lock the resources or relevant data if needed and return true upon success
    442         bool extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
    443 
    444         RefPtr<NativeImageSkia> m_nativeImage;
    445         RefPtr<NativeImageSkia> m_skiaImage;
    446         Image* m_image;
    447         ImageHtmlDomSource m_imageHtmlDomSource;
    448         bool m_extractSucceeded;
    449         const void* m_imagePixelData;
    450         unsigned m_imageWidth;
    451         unsigned m_imageHeight;
    452         DataFormat m_imageSourceFormat;
    453         AlphaOp m_alphaOp;
    454         unsigned m_imageSourceUnpackAlignment;
    455     };
    456 
    457     // The Following functions are implemented in GraphicsContext3DImagePacking.cpp
    458 
    459     // Packs the contents of the given Image which is passed in |pixels| into the passed Vector
    460     // according to the given format and type, and obeying the flipY and AlphaOp flags.
    461     // Returns true upon success.
    462     static bool packImageData(Image*, const void* pixels, GC3Denum format, GC3Denum type, bool flipY, AlphaOp, DataFormat sourceFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, Vector<uint8_t>& data);
    463 
    464     // Extracts the contents of the given ImageData into the passed Vector,
    465     // packing the pixel data according to the given format and type,
    466     // and obeying the flipY and premultiplyAlpha flags. Returns true
    467     // upon success.
    468     static bool extractImageData(const uint8_t*, const IntSize&, GC3Denum format, GC3Denum type, bool flipY, bool premultiplyAlpha, Vector<uint8_t>& data);
    469 
    470     // Helper function which extracts the user-supplied texture
    471     // data, applying the flipY and premultiplyAlpha parameters.
    472     // If the data is not tightly packed according to the passed
    473     // unpackAlignment, the output data will be tightly packed.
    474     // Returns true if successful, false if any error occurred.
    475     static bool extractTextureData(unsigned width, unsigned height, GC3Denum format, GC3Denum type, unsigned unpackAlignment, bool flipY, bool premultiplyAlpha, const void* pixels, Vector<uint8_t>& data);
    476 
    477     // End GraphicsContext3DImagePacking.cpp functions
    478 
    479     // This is the order of bytes to use when doing a readback.
    480     enum ReadbackOrder {
    481         ReadbackRGBA,
    482         ReadbackSkia
    483     };
    484 
    485     // Helper function which does a readback from the currently-bound
    486     // framebuffer into a buffer of a certain size with 4-byte pixels.
    487     void readBackFramebuffer(unsigned char* pixels, int width, int height, ReadbackOrder, AlphaOp);
    488 
    489 private:
    490     friend class Extensions3D;
    491 
    492     GraphicsContext3D(PassOwnPtr<blink::WebGraphicsContext3D>, bool preserveDrawingBuffer);
    493     GraphicsContext3D(PassOwnPtr<blink::WebGraphicsContext3DProvider>, bool preserveDrawingBuffer);
    494 
    495     // Helper for packImageData/extractImageData/extractTextureData which implement packing of pixel
    496     // data into the specified OpenGL destination format and type.
    497     // A sourceUnpackAlignment of zero indicates that the source
    498     // data is tightly packed. Non-zero values may take a slow path.
    499     // Destination data will have no gaps between rows.
    500     // Implemented in GraphicsContext3DImagePacking.cpp
    501     static bool packPixels(const uint8_t* sourceData, DataFormat sourceDataFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, unsigned destinationFormat, unsigned destinationType, AlphaOp, void* destinationData, bool flipY);
    502 
    503     void paintFramebufferToCanvas(int framebuffer, int width, int height, bool premultiplyAlpha, ImageBuffer*);
    504     // Helper function to flip a bitmap vertically.
    505     void flipVertically(uint8_t* data, int width, int height);
    506 
    507     // Extensions3D support.
    508     bool supportsExtension(const String& name);
    509     bool ensureExtensionEnabled(const String& name);
    510     bool isExtensionEnabled(const String& name);
    511 
    512     void initializeExtensions();
    513 
    514     bool preserveDrawingBuffer() const { return m_preserveDrawingBuffer; }
    515 
    516     OwnPtr<blink::WebGraphicsContext3DProvider> m_provider;
    517     blink::WebGraphicsContext3D* m_impl;
    518     OwnPtr<GraphicsContext3DContextLostCallbackAdapter> m_contextLostCallbackAdapter;
    519     OwnPtr<GraphicsContext3DErrorMessageCallbackAdapter> m_errorMessageCallbackAdapter;
    520     OwnPtr<blink::WebGraphicsContext3D> m_ownedWebContext;
    521     OwnPtr<Extensions3D> m_extensions;
    522     bool m_initializedAvailableExtensions;
    523     HashSet<String> m_enabledExtensions;
    524     HashSet<String> m_requestableExtensions;
    525     bool m_layerComposited;
    526     bool m_preserveDrawingBuffer;
    527     int m_packAlignment;
    528 
    529     enum ResourceSafety {
    530         ResourceSafetyUnknown,
    531         ResourceSafe,
    532         ResourceUnsafe
    533     };
    534     ResourceSafety m_resourceSafety;
    535 
    536     // If the width and height of the Canvas's backing store don't
    537     // match those that we were given in the most recent call to
    538     // reshape(), then we need an intermediate bitmap to read back the
    539     // frame buffer into. This seems to happen when CSS styles are
    540     // used to resize the Canvas.
    541     SkBitmap m_resizingBitmap;
    542 
    543     GrContext* m_grContext;
    544 
    545     // Used to flip a bitmap vertically.
    546     Vector<uint8_t> m_scanline;
    547 };
    548 
    549 } // namespace WebCore
    550 
    551 #endif // GraphicsContext3D_h
    552