Merge "Fix WebGLShaderPrecisionFormat issue." into tizen_2.1
[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 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
67         , AcceleratedMemorySaving
68 #endif
69     };
70     
71     enum BackingStoreCopy {
72         CopyBackingStore, // Guarantee subsequent draws don't affect the copy.
73         DontCopyBackingStore // Subsequent draws may affect the copy.
74     };
75
76     enum DeferralMode {
77         NonDeferred,
78         Deferred
79     };
80
81     class ImageBuffer {
82         WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
83     public:
84         // Will return a null pointer on allocation failure.
85         static PassOwnPtr<ImageBuffer> create(const IntSize& size, float resolutionScale = 1, ColorSpace colorSpace = ColorSpaceDeviceRGB, RenderingMode renderingMode = Unaccelerated, DeferralMode deferralMode = NonDeferred)
86         {
87             bool success = false;
88             OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, resolutionScale, colorSpace, renderingMode, deferralMode, success));
89             if (!success)
90                 return nullptr;
91             return buf.release();
92         }
93
94         ~ImageBuffer();
95
96         // The actual resolution of the backing store
97         const IntSize& internalSize() const { return m_size; }
98         const IntSize& logicalSize() const { return m_logicalSize; }
99
100         GraphicsContext* context() const;
101
102         PassRefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore) const;
103
104 #if USE(CAIRO)
105         cairo_surface_t* getSurface() const { return m_data.m_surface; }
106 #endif
107
108         enum CoordinateSystem { LogicalCoordinateSystem, BackingStoreCoordinateSystem };
109
110         PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const;
111         PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const;
112
113         void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem = LogicalCoordinateSystem);
114         
115         void convertToLuminanceMask();
116         
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         int getPlatformSurfaceID() const { return m_platformSurfaceID; }
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         bool lockSurface() const;
173         PassRefPtr<cairo_surface_t> querySurface() const;
174         bool unlockSurface() const;
175
176         int m_platformSurfaceID;
177         void* m_eglSurface;
178         bool m_isLockable;
179 #endif // ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
180
181     };
182
183 #if USE(CG) || USE(SKIA)
184     String ImageDataToDataURL(const ImageData&, const String& mimeType, const double* quality);
185 #endif
186
187 } // namespace WebCore
188
189 #endif // ImageBuffer_h