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 "core/platform/graphics/ColorSpace.h"
     32 #include "core/platform/graphics/FloatRect.h"
     33 #include "core/platform/graphics/GraphicsContext.h"
     34 #include "core/platform/graphics/GraphicsTypes.h"
     35 #include "core/platform/graphics/GraphicsTypes3D.h"
     36 #include "core/platform/graphics/IntSize.h"
     37 #include "core/platform/graphics/transforms/AffineTransform.h"
     38 #include "wtf/Forward.h"
     39 #include "wtf/OwnPtr.h"
     40 #include "wtf/PassOwnPtr.h"
     41 #include "wtf/PassRefPtr.h"
     42 #include "wtf/Uint8ClampedArray.h"
     43 #include "wtf/Vector.h"
     44 
     45 class SkCanvas;
     46 
     47 namespace WebKit { class WebLayer; }
     48 
     49 namespace WebCore {
     50 
     51 class Canvas2DLayerBridge;
     52 class Image;
     53 class ImageData;
     54 class IntPoint;
     55 class IntRect;
     56 class GraphicsContext3D;
     57 
     58 enum Multiply {
     59     Premultiplied,
     60     Unmultiplied
     61 };
     62 
     63 enum RenderingMode {
     64     Unaccelerated,
     65     UnacceleratedNonPlatformBuffer, // Use plain memory allocation rather than platform API to allocate backing store.
     66     Accelerated
     67 };
     68 
     69 enum BackingStoreCopy {
     70     CopyBackingStore, // Guarantee subsequent draws don't affect the copy.
     71     DontCopyBackingStore // Subsequent draws may affect the copy.
     72 };
     73 
     74 enum ScaleBehavior {
     75     Scaled,
     76     Unscaled
     77 };
     78 
     79 enum OpacityMode {
     80     NonOpaque,
     81     Opaque,
     82 };
     83 
     84 class ImageBuffer {
     85     WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
     86 public:
     87     // Will return a null pointer on allocation failure.
     88     static PassOwnPtr<ImageBuffer> create(const IntSize& size, float resolutionScale = 1, RenderingMode renderingMode = Unaccelerated, OpacityMode opacityMode = NonOpaque)
     89     {
     90         bool success = false;
     91         OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, resolutionScale, renderingMode, opacityMode, success));
     92         if (!success)
     93             return nullptr;
     94         return buf.release();
     95     }
     96 
     97     static PassOwnPtr<ImageBuffer> createCompatibleBuffer(const IntSize&, float resolutionScale, const GraphicsContext*, bool hasAlpha);
     98 
     99     ~ImageBuffer();
    100 
    101     // The actual resolution of the backing store
    102     const IntSize& internalSize() const { return m_size; }
    103     const IntSize& logicalSize() const { return m_logicalSize; }
    104 
    105     GraphicsContext* context() const;
    106 
    107     PassRefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const;
    108     // Give hints on the faster copyImage Mode, return DontCopyBackingStore if it supports the DontCopyBackingStore behavior
    109     // or return CopyBackingStore if it doesn't.
    110     static BackingStoreCopy fastCopyImageMode();
    111 
    112     enum CoordinateSystem { LogicalCoordinateSystem, BackingStoreCoordinateSystem };
    113 
    114     PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const;
    115     PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const;
    116 
    117     void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem = LogicalCoordinateSystem);
    118 
    119     void convertToLuminanceMask();
    120 
    121     String toDataURL(const String& mimeType, const double* quality = 0, CoordinateSystem = LogicalCoordinateSystem) const;
    122     AffineTransform baseTransform() const { return AffineTransform(); }
    123     void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
    124     static const Vector<uint8_t>& getLinearRgbLUT();
    125     static const Vector<uint8_t>& getDeviceRgbLUT();
    126     WebKit::WebLayer* platformLayer() const;
    127 
    128     // FIXME: current implementations of this method have the restriction that they only work
    129     // with textures that are RGB or RGBA format, UNSIGNED_BYTE type and level 0, as specified in
    130     // Extensions3D::canUseCopyTextureCHROMIUM().
    131     bool copyToPlatformTexture(GraphicsContext3D&, Platform3DObject, GC3Denum, GC3Denum, GC3Dint, bool, bool);
    132 
    133 private:
    134     bool isValid() const;
    135 
    136     void draw(GraphicsContext*, const FloatRect&, const FloatRect& = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, bool useLowQualityScale = false);
    137     void drawPattern(GraphicsContext*, const FloatRect&, const FloatSize&, const FloatPoint&, CompositeOperator, const FloatRect&, BlendMode);
    138 
    139     friend class GraphicsContext;
    140     friend class GeneratedImage;
    141     friend class CrossfadeGeneratedImage;
    142     friend class GeneratorGeneratedImage;
    143 
    144     IntSize m_size;
    145     IntSize m_logicalSize;
    146     float m_resolutionScale;
    147     OwnPtr<SkCanvas> m_canvas;
    148     OwnPtr<GraphicsContext> m_context;
    149     OwnPtr<Canvas2DLayerBridge> m_layerBridge;
    150 
    151     // This constructor will place its success into the given out-variable
    152     // so that create() knows when it should return failure.
    153     ImageBuffer(const IntSize&, float resolutionScale, RenderingMode, OpacityMode, bool& success);
    154     ImageBuffer(const IntSize&, float resolutionScale, const GraphicsContext*, bool hasAlpha, bool& success);
    155 };
    156 
    157 String ImageDataToDataURL(const ImageData&, const String& mimeType, const double* quality);
    158 
    159 } // namespace WebCore
    160 
    161 #endif // ImageBuffer_h
    162