Merge "Refactor ImageBufferCairo" into tizen
[platform/framework/web/webkit-efl.git] / Source / WebCore / platform / graphics / ImageBuffer.h
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 "AffineTransform.h"
32 #include "ColorSpace.h"
33 #include "FloatRect.h"
34 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
35 #include "GLPlatformSurface.h"
36 #endif
37 #include "GraphicsContext.h"
38 #if USE(ACCELERATED_COMPOSITING)
39 #include "PlatformLayer.h"
40 #endif
41 #include "GraphicsTypes.h"
42 #include "GraphicsTypes3D.h"
43 #include "IntSize.h"
44 #include "ImageBufferData.h"
45 #include <wtf/Forward.h>
46 #include <wtf/OwnPtr.h>
47 #include <wtf/PassOwnPtr.h>
48 #include <wtf/PassRefPtr.h>
49 #include <wtf/Uint8ClampedArray.h>
50 #include <wtf/Vector.h>
51
52 namespace WebCore {
53
54     class Image;
55     class ImageData;
56     class IntPoint;
57     class IntRect;
58     class GraphicsContext3D;
59
60     enum Multiply {
61         Premultiplied,
62         Unmultiplied
63     };
64
65     enum RenderingMode {
66         Unaccelerated,
67         UnacceleratedNonPlatformBuffer, // Use plain memory allocation rather than platform API to allocate backing store.
68         Accelerated
69 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
70         , AcceleratedMemorySaving
71 #endif
72     };
73     enum BackingStoreCopy {
74         CopyBackingStore, // Guarantee subsequent draws don't affect the copy.
75         DontCopyBackingStore // Subsequent draws may affect the copy.
76     };
77
78     enum DeferralMode {
79         NonDeferred,
80         Deferred
81     };
82
83     class ImageBuffer {
84         WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
85     public:
86         // Will return a null pointer on allocation failure.
87         static PassOwnPtr<ImageBuffer> create(const IntSize& size, float resolutionScale = 1, ColorSpace colorSpace = ColorSpaceDeviceRGB, RenderingMode renderingMode = Unaccelerated, DeferralMode deferralMode = NonDeferred)
88         {
89             bool success = false;
90             OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, resolutionScale, colorSpace, renderingMode, deferralMode, success));
91             if (!success)
92                 return nullptr;
93             return buf.release();
94         }
95
96         ~ImageBuffer();
97
98         // The actual resolution of the backing store
99         const IntSize& internalSize() const { return m_size; }
100         const IntSize& logicalSize() const { return m_logicalSize; }
101
102         GraphicsContext* context() const;
103
104         PassRefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore) const;
105
106 #if USE(CAIRO)
107         cairo_surface_t* getSurface() const { return m_data.m_surface; }
108 #endif
109
110         enum CoordinateSystem { LogicalCoordinateSystem, BackingStoreCoordinateSystem };
111
112         PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const;
113         PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const;
114
115         void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem = LogicalCoordinateSystem);
116         void convertToLuminanceMask();
117         String toDataURL(const String& mimeType, const double* quality = 0, CoordinateSystem = LogicalCoordinateSystem) const;
118 #if !USE(CG)
119         AffineTransform baseTransform() const { return AffineTransform(); }
120         void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
121         void platformTransformColorSpace(const Vector<int>&);
122 #else
123         AffineTransform baseTransform() const { return AffineTransform(1, 0, 0, -1, 0, internalSize().height()); }
124 #endif
125 #if USE(ACCELERATED_COMPOSITING)
126         PlatformLayer* platformLayer() const;
127 #endif
128
129         bool copyToPlatformTexture(GraphicsContext3D&, Platform3DObject, GC3Denum, bool, bool);
130 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
131         PlatformBufferHandle getPlatformSurfaceID() const { return m_offScreenSurface->handle(); }
132         bool isLockable() const { return m_isLockable; }
133         void swapPlatformSurfaces();
134 #endif
135
136     private:
137 #if USE(CG)
138         NativeImagePtr copyNativeImage(BackingStoreCopy = CopyBackingStore) const;
139 #endif
140         void clip(GraphicsContext*, const FloatRect&) const;
141
142         void draw(GraphicsContext*, ColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
143         void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect);
144
145         inline void genericConvertToLuminanceMask();
146
147         friend class GraphicsContext;
148         friend class GeneratedImage;
149         friend class CrossfadeGeneratedImage;
150         friend class GeneratorGeneratedImage;
151
152     private:
153         ImageBufferData m_data;
154         IntSize m_size;
155         IntSize m_logicalSize;
156         float m_resolutionScale;
157         OwnPtr<GraphicsContext> m_context;
158
159         // This constructor will place its success into the given out-variable
160         // so that create() knows when it should return failure.
161         ImageBuffer(const IntSize&, float resolutionScale, ColorSpace, RenderingMode, DeferralMode, bool& success);
162
163 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING)
164         bool setBindingTexture();
165
166         unsigned m_bindingTexID;
167         void* m_eglImage;
168 #endif // ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING)
169
170 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
171         void initializeEGL();
172         void destroy();
173         bool lockSurface() const;
174         PassRefPtr<cairo_surface_t> querySurface() const;
175         bool unlockSurface() const;
176         bool m_isLockable;
177         OwnPtr<GLPlatformSurface> m_offScreenSurface;
178 #endif // ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
179
180     };
181
182 #if USE(CG) || USE(SKIA)
183     String ImageDataToDataURL(const ImageData&, const String& mimeType, const double* quality);
184 #endif
185
186 } // namespace WebCore
187
188 #endif // ImageBuffer_h