Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsContext3D.h
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef GraphicsContext3D_h
27 #define GraphicsContext3D_h
28
29 #include "platform/PlatformExport.h"
30 #include "platform/geometry/IntRect.h"
31 #include "platform/graphics/GraphicsTypes3D.h"
32 #include "platform/graphics/Image.h"
33 #include "platform/weborigin/KURL.h"
34 #include "third_party/khronos/GLES2/gl2.h"
35 #include "third_party/khronos/GLES2/gl2ext.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/HashSet.h"
38 #include "wtf/ListHashSet.h"
39 #include "wtf/Noncopyable.h"
40 #include "wtf/OwnPtr.h"
41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/RefCounted.h"
43 #include "wtf/text/WTFString.h"
44
45 // FIXME: Find a better way to avoid the name confliction for NO_ERROR.
46 #if OS(WIN)
47 #undef NO_ERROR
48 #endif
49
50 class GrContext;
51
52 namespace blink {
53 class WebGraphicsContext3D;
54 class WebGraphicsContext3DProvider;
55 }
56
57 namespace WebCore {
58 class Image;
59 class ImageBuffer;
60 class IntRect;
61 class IntSize;
62
63 class PLATFORM_EXPORT GraphicsContext3D : public RefCounted<GraphicsContext3D> {
64 public:
65     // This is the preferred method for creating an instance of this class. When created this way the webContext
66     // is not owned by the GraphicsContext3D
67     static PassRefPtr<GraphicsContext3D> createContextSupport(blink::WebGraphicsContext3D* webContext);
68
69     // Callers must make the context current before using it AND check that the context was created successfully
70     // via ContextLost before using the context in any way. Once made current on a thread, the context cannot
71     // be used on any other thread.
72     // This creation method is obsolete and should not be used by new code. They will be removed soon.
73     static PassRefPtr<GraphicsContext3D> createGraphicsContextFromProvider(PassOwnPtr<blink::WebGraphicsContext3DProvider>);
74
75     ~GraphicsContext3D();
76
77     GrContext* grContext();
78     blink::WebGraphicsContext3D* webContext() const { return m_impl; }
79
80     //----------------------------------------------------------------------
81     // Helpers for texture uploading and pixel readback.
82     //
83
84     // Computes the components per pixel and bytes per component
85     // for the given format and type combination. Returns false if
86     // either was an invalid enum.
87     static bool computeFormatAndTypeParameters(GLenum format, GLenum type, unsigned* componentsPerPixel, unsigned* bytesPerComponent);
88
89     // Computes the image size in bytes. If paddingInBytes is not null, padding
90     // is also calculated in return. Returns NO_ERROR if succeed, otherwise
91     // return the suggested GL error indicating the cause of the failure:
92     //   INVALID_VALUE if width/height is negative or overflow happens.
93     //   INVALID_ENUM if format/type is illegal.
94     static GLenum computeImageSizeInBytes(GLenum format, GLenum type, GLsizei width, GLsizei height, GLint alignment, unsigned* imageSizeInBytes, unsigned* paddingInBytes);
95
96     // Attempt to enumerate all possible native image formats to
97     // reduce the amount of temporary allocations during texture
98     // uploading. This enum must be public because it is accessed
99     // by non-member functions.
100     enum DataFormat {
101         DataFormatRGBA8 = 0,
102         DataFormatRGBA16F,
103         DataFormatRGBA32F,
104         DataFormatRGB8,
105         DataFormatRGB16F,
106         DataFormatRGB32F,
107         DataFormatBGR8,
108         DataFormatBGRA8,
109         DataFormatARGB8,
110         DataFormatABGR8,
111         DataFormatRGBA5551,
112         DataFormatRGBA4444,
113         DataFormatRGB565,
114         DataFormatR8,
115         DataFormatR16F,
116         DataFormatR32F,
117         DataFormatRA8,
118         DataFormatRA16F,
119         DataFormatRA32F,
120         DataFormatAR8,
121         DataFormatA8,
122         DataFormatA16F,
123         DataFormatA32F,
124         DataFormatNumFormats
125     };
126
127     // Check if the format is one of the formats from the ImageData or DOM elements.
128     // The formats from ImageData is always RGBA8.
129     // The formats from DOM elements vary with Graphics ports. It can only be RGBA8 or BGRA8.
130     static ALWAYS_INLINE bool srcFormatComeFromDOMElementOrImageData(DataFormat SrcFormat)
131     {
132     return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
133     }
134
135     static unsigned getClearBitsByFormat(GLenum);
136
137     enum ChannelBits {
138         ChannelRed = 1,
139         ChannelGreen = 2,
140         ChannelBlue = 4,
141         ChannelAlpha = 8,
142         ChannelDepth = 16,
143         ChannelStencil = 32,
144         ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
145         ChannelRGBA = ChannelRGB | ChannelAlpha,
146     };
147
148     static unsigned getChannelBitsByFormat(GLenum);
149
150     // Possible alpha operations that may need to occur during
151     // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
152     // be removed.
153     enum AlphaOp {
154         AlphaDoNothing = 0,
155         AlphaDoPremultiply = 1,
156         AlphaDoUnmultiply = 2
157     };
158
159     enum ImageHtmlDomSource {
160         HtmlDomImage = 0,
161         HtmlDomCanvas = 1,
162         HtmlDomVideo = 2,
163         HtmlDomNone = 3
164     };
165
166     class PLATFORM_EXPORT ImageExtractor {
167     public:
168         ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
169
170         ~ImageExtractor();
171
172         bool extractSucceeded() { return m_extractSucceeded; }
173         const void* imagePixelData() { return m_imagePixelData; }
174         unsigned imageWidth() { return m_imageWidth; }
175         unsigned imageHeight() { return m_imageHeight; }
176         DataFormat imageSourceFormat() { return m_imageSourceFormat; }
177         AlphaOp imageAlphaOp() { return m_alphaOp; }
178         unsigned imageSourceUnpackAlignment() { return m_imageSourceUnpackAlignment; }
179         ImageHtmlDomSource imageHtmlDomSource() { return m_imageHtmlDomSource; }
180     private:
181         // Extract the image and keeps track of its status, such as width, height, Source Alignment, format and AlphaOp etc.
182         // This needs to lock the resources or relevant data if needed and return true upon success
183         bool extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
184
185         RefPtr<NativeImageSkia> m_nativeImage;
186         RefPtr<NativeImageSkia> m_skiaImage;
187         Image* m_image;
188         ImageHtmlDomSource m_imageHtmlDomSource;
189         bool m_extractSucceeded;
190         const void* m_imagePixelData;
191         unsigned m_imageWidth;
192         unsigned m_imageHeight;
193         DataFormat m_imageSourceFormat;
194         AlphaOp m_alphaOp;
195         unsigned m_imageSourceUnpackAlignment;
196     };
197
198     // The Following functions are implemented in GraphicsContext3DImagePacking.cpp
199
200     // Packs the contents of the given Image which is passed in |pixels| into the passed Vector
201     // according to the given format and type, and obeying the flipY and AlphaOp flags.
202     // Returns true upon success.
203     static bool packImageData(Image*, const void* pixels, GLenum format, GLenum type, bool flipY, AlphaOp, DataFormat sourceFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, Vector<uint8_t>& data);
204
205     // Extracts the contents of the given ImageData into the passed Vector,
206     // packing the pixel data according to the given format and type,
207     // and obeying the flipY and premultiplyAlpha flags. Returns true
208     // upon success.
209     static bool extractImageData(const uint8_t*, const IntSize&, GLenum format, GLenum type, bool flipY, bool premultiplyAlpha, Vector<uint8_t>& data);
210
211     // Helper function which extracts the user-supplied texture
212     // data, applying the flipY and premultiplyAlpha parameters.
213     // If the data is not tightly packed according to the passed
214     // unpackAlignment, the output data will be tightly packed.
215     // Returns true if successful, false if any error occurred.
216     static bool extractTextureData(unsigned width, unsigned height, GLenum format, GLenum type, unsigned unpackAlignment, bool flipY, bool premultiplyAlpha, const void* pixels, Vector<uint8_t>& data);
217
218     // End GraphicsContext3DImagePacking.cpp functions
219
220     // Extension support.
221     bool supportsExtension(const String& name);
222     bool ensureExtensionEnabled(const String& name);
223     bool isExtensionEnabled(const String& name);
224
225     static bool canUseCopyTextureCHROMIUM(GLenum destFormat, GLenum destType, GLint level);
226
227 private:
228     GraphicsContext3D(PassOwnPtr<blink::WebGraphicsContext3DProvider>);
229     GraphicsContext3D(blink::WebGraphicsContext3D* webContext);
230
231     // Helper for packImageData/extractImageData/extractTextureData which implement packing of pixel
232     // data into the specified OpenGL destination format and type.
233     // A sourceUnpackAlignment of zero indicates that the source
234     // data is tightly packed. Non-zero values may take a slow path.
235     // Destination data will have no gaps between rows.
236     // Implemented in GraphicsContext3DImagePacking.cpp
237     static bool packPixels(const uint8_t* sourceData, DataFormat sourceDataFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, unsigned destinationFormat, unsigned destinationType, AlphaOp, void* destinationData, bool flipY);
238
239     void initializeExtensions();
240
241     OwnPtr<blink::WebGraphicsContext3DProvider> m_provider;
242     blink::WebGraphicsContext3D* m_impl;
243     bool m_initializedAvailableExtensions;
244     HashSet<String> m_enabledExtensions;
245     HashSet<String> m_requestableExtensions;
246     GrContext* m_grContext;
247 };
248
249 } // namespace WebCore
250
251 #endif // GraphicsContext3D_h