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