Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
      4  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef ImageBuffer_h
     29 #define ImageBuffer_h
     30 
     31 #include "platform/PlatformExport.h"
     32 #include "platform/geometry/FloatRect.h"
     33 #include "platform/geometry/IntSize.h"
     34 #include "platform/graphics/Canvas2DLayerBridge.h"
     35 #include "platform/graphics/ColorSpace.h"
     36 #include "platform/graphics/GraphicsTypes.h"
     37 #include "platform/graphics/GraphicsTypes3D.h"
     38 #include "platform/graphics/ImageBufferSurface.h"
     39 #include "platform/transforms/AffineTransform.h"
     40 #include "wtf/Forward.h"
     41 #include "wtf/OwnPtr.h"
     42 #include "wtf/PassOwnPtr.h"
     43 #include "wtf/PassRefPtr.h"
     44 #include "wtf/Uint8ClampedArray.h"
     45 #include "wtf/Vector.h"
     46 
     47 class SkCanvas;
     48 
     49 namespace blink {
     50 class WebGraphicsContext3D;
     51 }
     52 
     53 namespace WebCore {
     54 
     55 class DrawingBuffer;
     56 class GraphicsContext;
     57 class Image;
     58 class ImageBufferClient;
     59 class IntPoint;
     60 class IntRect;
     61 
     62 enum Multiply {
     63     Premultiplied,
     64     Unmultiplied
     65 };
     66 
     67 enum BackingStoreCopy {
     68     CopyBackingStore, // Guarantee subsequent draws don't affect the copy.
     69     DontCopyBackingStore // Subsequent draws may affect the copy.
     70 };
     71 
     72 enum ScaleBehavior {
     73     Scaled,
     74     Unscaled
     75 };
     76 
     77 class PLATFORM_EXPORT ImageBuffer {
     78     WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
     79 public:
     80     static PassOwnPtr<ImageBuffer> create(const IntSize&, OpacityMode = NonOpaque);
     81     static PassOwnPtr<ImageBuffer> create(PassOwnPtr<ImageBufferSurface>);
     82 
     83     ~ImageBuffer();
     84 
     85     void setClient(ImageBufferClient* client) { m_client = client; }
     86 
     87     const IntSize& size() const { return m_surface->size(); }
     88     bool isAccelerated() const { return m_surface->isAccelerated(); }
     89     bool isSurfaceValid() const;
     90     bool restoreSurface() const;
     91 
     92     void setIsHidden(bool hidden) { m_surface->setIsHidden(hidden); }
     93 
     94     GraphicsContext* context() const;
     95 
     96     const SkBitmap& bitmap() const;
     97 
     98     PassRefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const;
     99     // Give hints on the faster copyImage Mode, return DontCopyBackingStore if it supports the DontCopyBackingStore behavior
    100     // or return CopyBackingStore if it doesn't.
    101     static BackingStoreCopy fastCopyImageMode();
    102 
    103     PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&) const;
    104     PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&) const;
    105 
    106     void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint);
    107 
    108     String toDataURL(const String& mimeType, const double* quality = 0) const;
    109     AffineTransform baseTransform() const { return AffineTransform(); }
    110     void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
    111     blink::WebLayer* platformLayer() const;
    112 
    113     // FIXME: current implementations of this method have the restriction that they only work
    114     // with textures that are RGB or RGBA format, UNSIGNED_BYTE type and level 0, as specified in
    115     // Extensions3D::canUseCopyTextureCHROMIUM().
    116     // Destroys the TEXTURE_2D binding for the active texture unit of the passed context
    117     bool copyToPlatformTexture(blink::WebGraphicsContext3D*, Platform3DObject, GLenum, GLenum, GLint, bool, bool);
    118 
    119     Platform3DObject getBackingTexture();
    120 
    121     bool copyRenderingResultsFromDrawingBuffer(DrawingBuffer*, bool fromFrontBuffer = false);
    122 
    123     void flush();
    124 
    125     void notifySurfaceInvalid();
    126 
    127 private:
    128     ImageBuffer(PassOwnPtr<ImageBufferSurface>);
    129 
    130     void draw(GraphicsContext*, const FloatRect&, const FloatRect* = 0, CompositeOperator = CompositeSourceOver);
    131     void drawPattern(GraphicsContext*, const FloatRect&, const FloatSize&, const FloatPoint&, CompositeOperator, const FloatRect&, blink::WebBlendMode, const IntSize& repeatSpacing = IntSize());
    132     static PassRefPtr<SkColorFilter> createColorSpaceFilter(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
    133 
    134     friend class GraphicsContext;
    135     friend class GeneratedImage;
    136     friend class CrossfadeGeneratedImage;
    137     friend class GradientGeneratedImage;
    138     friend class SkiaImageFilterBuilder;
    139 
    140     OwnPtr<ImageBufferSurface> m_surface;
    141     OwnPtr<GraphicsContext> m_context;
    142     ImageBufferClient* m_client;
    143 };
    144 
    145 struct ImageDataBuffer {
    146     ImageDataBuffer(const IntSize& size, PassRefPtr<Uint8ClampedArray> data) : m_size(size), m_data(data) { }
    147     IntSize size() const { return m_size; }
    148     unsigned char* data() const { return m_data->data(); }
    149 
    150     IntSize m_size;
    151     RefPtr<Uint8ClampedArray> m_data;
    152 };
    153 
    154 String PLATFORM_EXPORT ImageDataToDataURL(const ImageDataBuffer&, const String& mimeType, const double* quality);
    155 
    156 } // namespace WebCore
    157 
    158 #endif // ImageBuffer_h
    159