• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@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/GraphicsContext.h"
37 #include "platform/graphics/GraphicsTypes.h"
38 #include "platform/graphics/GraphicsTypes3D.h"
39 #include "platform/graphics/ImageBufferSurface.h"
40 #include "platform/transforms/AffineTransform.h"
41 #include "wtf/Forward.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassOwnPtr.h"
44 #include "wtf/PassRefPtr.h"
45 #include "wtf/Uint8ClampedArray.h"
46 #include "wtf/Vector.h"
47 
48 class SkCanvas;
49 
50 namespace WebCore {
51 
52 class DrawingBuffer;
53 class GraphicsContext3D;
54 class Image;
55 class IntPoint;
56 class IntRect;
57 
58 enum Multiply {
59     Premultiplied,
60     Unmultiplied
61 };
62 
63 enum BackingStoreCopy {
64     CopyBackingStore, // Guarantee subsequent draws don't affect the copy.
65     DontCopyBackingStore // Subsequent draws may affect the copy.
66 };
67 
68 enum ScaleBehavior {
69     Scaled,
70     Unscaled
71 };
72 
73 class PLATFORM_EXPORT ImageBuffer {
74     WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
75 public:
76     static PassOwnPtr<ImageBuffer> create(const IntSize&, OpacityMode = NonOpaque);
77     static PassOwnPtr<ImageBuffer> create(PassOwnPtr<ImageBufferSurface>);
78 
79     ~ImageBuffer();
80 
size()81     const IntSize& size() const { return m_surface->size(); }
isAccelerated()82     bool isAccelerated() const { return m_surface->isAccelerated(); }
83 
84     GraphicsContext* context() const;
85 
86     const SkBitmap& bitmap() const;
87 
88     PassRefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const;
89     // Give hints on the faster copyImage Mode, return DontCopyBackingStore if it supports the DontCopyBackingStore behavior
90     // or return CopyBackingStore if it doesn't.
91     static BackingStoreCopy fastCopyImageMode();
92 
93     PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&) const;
94     PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&) const;
95 
96     void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint);
97 
98     String toDataURL(const String& mimeType, const double* quality = 0) const;
baseTransform()99     AffineTransform baseTransform() const { return AffineTransform(); }
100     void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
101     blink::WebLayer* platformLayer() const;
102 
103     // FIXME: current implementations of this method have the restriction that they only work
104     // with textures that are RGB or RGBA format, UNSIGNED_BYTE type and level 0, as specified in
105     // Extensions3D::canUseCopyTextureCHROMIUM().
106     bool copyToPlatformTexture(GraphicsContext3D&, Platform3DObject, GC3Denum, GC3Denum, GC3Dint, bool, bool);
107 
108     Platform3DObject getBackingTexture();
109     bool copyRenderingResultsFromDrawingBuffer(DrawingBuffer*);
110 
111     void flush();
112 
113 private:
114     ImageBuffer(PassOwnPtr<ImageBufferSurface>);
115     bool isValid() const;
116 
117     void draw(GraphicsContext*, const FloatRect&, const FloatRect& = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, blink::WebBlendMode = blink::WebBlendModeNormal, bool useLowQualityScale = false);
118     void drawPattern(GraphicsContext*, const FloatRect&, const FloatSize&, const FloatPoint&, CompositeOperator, const FloatRect&, blink::WebBlendMode, const IntSize& repeatSpacing = IntSize());
119     static PassRefPtr<SkColorFilter> createColorSpaceFilter(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
120 
121     friend class GraphicsContext;
122     friend class GeneratedImage;
123     friend class CrossfadeGeneratedImage;
124     friend class GradientGeneratedImage;
125     friend class SkiaImageFilterBuilder;
126 
127     OwnPtr<ImageBufferSurface> m_surface;
128     OwnPtr<GraphicsContext> m_context;
129 };
130 
131 struct ImageDataBuffer {
ImageDataBufferImageDataBuffer132     ImageDataBuffer(const IntSize& size, PassRefPtr<Uint8ClampedArray> data) : m_size(size), m_data(data) { }
sizeImageDataBuffer133     IntSize size() const { return m_size; }
dataImageDataBuffer134     unsigned char* data() const { return m_data->data(); }
135 
136     IntSize m_size;
137     RefPtr<Uint8ClampedArray> m_data;
138 };
139 
140 String PLATFORM_EXPORT ImageDataToDataURL(const ImageDataBuffer&, const String& mimeType, const double* quality);
141 
142 } // namespace WebCore
143 
144 #endif // ImageBuffer_h
145