2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef GraphicsContext3D_h
27 #define GraphicsContext3D_h
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"
45 // FIXME: Find a better way to avoid the name confliction for NO_ERROR.
53 class WebGraphicsContext3D;
54 class WebGraphicsContext3DProvider;
63 class PLATFORM_EXPORT GraphicsContext3D : public RefCounted<GraphicsContext3D> {
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);
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>);
77 GrContext* grContext();
78 blink::WebGraphicsContext3D* webContext() const { return m_impl; }
80 //----------------------------------------------------------------------
81 // Helpers for texture uploading and pixel readback.
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);
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);
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.
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)
132 return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
135 static unsigned getClearBitsByFormat(GLenum);
144 ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
145 ChannelRGBA = ChannelRGB | ChannelAlpha,
148 static unsigned getChannelBitsByFormat(GLenum);
150 // Possible alpha operations that may need to occur during
151 // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
155 AlphaDoPremultiply = 1,
156 AlphaDoUnmultiply = 2
159 enum ImageHtmlDomSource {
166 class PLATFORM_EXPORT ImageExtractor {
168 ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
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; }
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);
185 RefPtr<NativeImageSkia> m_nativeImage;
186 RefPtr<NativeImageSkia> m_skiaImage;
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;
195 unsigned m_imageSourceUnpackAlignment;
198 // The Following functions are implemented in GraphicsContext3DImagePacking.cpp
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);
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
209 static bool extractImageData(const uint8_t*, const IntSize&, GLenum format, GLenum type, bool flipY, bool premultiplyAlpha, Vector<uint8_t>& data);
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);
218 // End GraphicsContext3DImagePacking.cpp functions
220 // Extension support.
221 bool supportsExtension(const String& name);
222 bool ensureExtensionEnabled(const String& name);
223 bool isExtensionEnabled(const String& name);
225 static bool canUseCopyTextureCHROMIUM(GLenum destFormat, GLenum destType, GLint level);
228 GraphicsContext3D(PassOwnPtr<blink::WebGraphicsContext3DProvider>);
229 GraphicsContext3D(blink::WebGraphicsContext3D* webContext);
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);
239 void initializeExtensions();
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;
249 } // namespace WebCore
251 #endif // GraphicsContext3D_h