File: WebGLImageConversion.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (293 lines) | stat: -rw-r--r-- 10,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WebGLImageConversion_h
#define WebGLImageConversion_h

#include "platform/PlatformExport.h"
#include "platform/graphics/Image.h"
#include "platform/graphics/skia/ImagePixelLocker.h"
#include "platform/heap/Heap.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/khronos/GLES3/gl3.h"
#include "wtf/Allocator.h"
#include "wtf/Optional.h"
#include "wtf/RefPtr.h"

namespace blink {
class Image;
class IntSize;

// Helper functions for texture uploading and pixel readback.
class PLATFORM_EXPORT WebGLImageConversion final {
  STATIC_ONLY(WebGLImageConversion);

 public:
  // Attempt to enumerate all possible native image formats to
  // reduce the amount of temporary allocations during texture
  // uploading. This enum must be public because it is accessed
  // by non-member functions.
  // "_S" postfix indicates signed type.
  enum DataFormat {
    DataFormatRGBA8 = 0,
    DataFormatRGBA8_S,
    DataFormatRGBA16,
    DataFormatRGBA16_S,
    DataFormatRGBA32,
    DataFormatRGBA32_S,
    DataFormatRGBA16F,
    DataFormatRGBA32F,
    DataFormatRGBA2_10_10_10,
    DataFormatRGB8,
    DataFormatRGB8_S,
    DataFormatRGB16,
    DataFormatRGB16_S,
    DataFormatRGB32,
    DataFormatRGB32_S,
    DataFormatRGB16F,
    DataFormatRGB32F,
    DataFormatBGR8,
    DataFormatBGRA8,
    DataFormatARGB8,
    DataFormatABGR8,
    DataFormatRGBA5551,
    DataFormatRGBA4444,
    DataFormatRGB565,
    DataFormatRGB10F11F11F,
    DataFormatRGB5999,
    DataFormatRG8,
    DataFormatRG8_S,
    DataFormatRG16,
    DataFormatRG16_S,
    DataFormatRG32,
    DataFormatRG32_S,
    DataFormatRG16F,
    DataFormatRG32F,
    DataFormatR8,
    DataFormatR8_S,
    DataFormatR16,
    DataFormatR16_S,
    DataFormatR32,
    DataFormatR32_S,
    DataFormatR16F,
    DataFormatR32F,
    DataFormatRA8,
    DataFormatRA16F,
    DataFormatRA32F,
    DataFormatAR8,
    DataFormatA8,
    DataFormatA16F,
    DataFormatA32F,
    DataFormatD16,
    DataFormatD32,
    DataFormatD32F,
    DataFormatDS24_8,
    DataFormatNumFormats
  };

  enum ChannelBits {
    ChannelRed = 1,
    ChannelGreen = 2,
    ChannelBlue = 4,
    ChannelAlpha = 8,
    ChannelDepth = 16,
    ChannelStencil = 32,
    ChannelRG = ChannelRed | ChannelGreen,
    ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
    ChannelRGBA = ChannelRGB | ChannelAlpha,
    ChannelDepthStencil = ChannelDepth | ChannelStencil,
  };

  // Possible alpha operations that may need to occur during
  // pixel packing. FIXME: kAlphaDoUnmultiply is lossy and must
  // be removed.
  enum AlphaOp {
    AlphaDoNothing = 0,
    AlphaDoPremultiply = 1,
    AlphaDoUnmultiply = 2
  };

  enum ImageHtmlDomSource {
    HtmlDomImage = 0,
    HtmlDomCanvas = 1,
    HtmlDomVideo = 2,
    HtmlDomNone = 3
  };

  struct PLATFORM_EXPORT PixelStoreParams final {
    PixelStoreParams();

    GLint alignment;
    GLint rowLength;
    GLint imageHeight;
    GLint skipPixels;
    GLint skipRows;
    GLint skipImages;
  };

  class PLATFORM_EXPORT ImageExtractor final {
    STACK_ALLOCATED();
    WTF_MAKE_NONCOPYABLE(ImageExtractor);

   public:
    ImageExtractor(Image*,
                   ImageHtmlDomSource,
                   bool premultiplyAlpha,
                   bool ignoreColorSpace);

    const void* imagePixelData() {
      return m_imagePixelLocker ? m_imagePixelLocker->pixels() : nullptr;
    }
    unsigned imageWidth() { return m_imageWidth; }
    unsigned imageHeight() { return m_imageHeight; }
    DataFormat imageSourceFormat() { return m_imageSourceFormat; }
    AlphaOp imageAlphaOp() { return m_alphaOp; }
    unsigned imageSourceUnpackAlignment() {
      return m_imageSourceUnpackAlignment;
    }

   private:
    // Extracts the image and keeps track of its status, such as width, height,
    // Source Alignment, format, AlphaOp, etc. This needs to lock the resources
    // or relevant data if needed.
    void extractImage(bool premultiplyAlpha, bool ignoreColorSpace);

    Image* m_image;
    Optional<ImagePixelLocker> m_imagePixelLocker;
    ImageHtmlDomSource m_imageHtmlDomSource;
    unsigned m_imageWidth;
    unsigned m_imageHeight;
    DataFormat m_imageSourceFormat;
    AlphaOp m_alphaOp;
    unsigned m_imageSourceUnpackAlignment;
  };

  // Computes the components per pixel and bytes per component
  // for the given format and type combination. Returns false if
  // either was an invalid enum.
  static bool computeFormatAndTypeParameters(GLenum format,
                                             GLenum type,
                                             unsigned* componentsPerPixel,
                                             unsigned* bytesPerComponent);

  // Computes the image size in bytes. If paddingInBytes is not null, padding
  // is also calculated in return. Returns NO_ERROR if succeed, otherwise
  // return the suggested GL error indicating the cause of the failure:
  //   INVALID_VALUE if width/height/depth is negative or overflow happens.
  //   INVALID_ENUM if format/type is illegal.
  // Note that imageSizeBytes does not include skipSizeInBytes, but it is
  // guaranteed if NO_ERROR is returned, adding the two sizes won't cause
  // overflow.
  // |paddingInBytes| and |skipSizeInBytes| are optional and can be null, but
  // the overflow validation is still performed.
  static GLenum computeImageSizeInBytes(GLenum format,
                                        GLenum type,
                                        GLsizei width,
                                        GLsizei height,
                                        GLsizei depth,
                                        const PixelStoreParams&,
                                        unsigned* imageSizeInBytes,
                                        unsigned* paddingInBytes,
                                        unsigned* skipSizeInBytes);

  // Check if the format is one of the formats from the ImageData or DOM
  // elements. The format from ImageData is always RGBA8. The formats from DOM
  // elements vary with Graphics ports, but can only be RGBA8 or BGRA8.
  static ALWAYS_INLINE bool srcFormatComeFromDOMElementOrImageData(
      DataFormat SrcFormat) {
    return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
  }

  // The input can be either format or internalformat.
  static unsigned getChannelBitsByFormat(GLenum);

  // The Following functions are implemented in
  // GraphicsContext3DImagePacking.cpp.

  // Packs the contents of the given Image, which is passed in |pixels|, into
  // the passed Vector according to the given format and type, and obeying the
  // flipY and AlphaOp flags. Returns true upon success.
  static bool packImageData(Image*,
                            const void* pixels,
                            GLenum format,
                            GLenum type,
                            bool flipY,
                            AlphaOp,
                            DataFormat sourceFormat,
                            unsigned sourceImageWidth,
                            unsigned sourceImageHeight,
                            const IntRect& sourceImageSubRectangle,
                            int depth,
                            unsigned sourceUnpackAlignment,
                            int unpackImageHeight,
                            Vector<uint8_t>& data);

  // Extracts the contents of the given ImageData into the passed Vector,
  // packing the pixel data according to the given format and type,
  // and obeying the flipY and premultiplyAlpha flags. Returns true
  // upon success.
  static bool extractImageData(const uint8_t* imageData,
                               DataFormat sourceDataFormat,
                               const IntSize& imageDataSize,
                               const IntRect& sourceImageSubRectangle,
                               int depth,
                               int unpackImageHeight,
                               GLenum format,
                               GLenum type,
                               bool flipY,
                               bool premultiplyAlpha,
                               Vector<uint8_t>& data);

  // Helper function which extracts the user-supplied texture
  // data, applying the flipY and premultiplyAlpha parameters.
  // If the data is not tightly packed according to the passed
  // unpackAlignment, the output data will be tightly packed.
  // Returns true if successful, false if any error occurred.
  static bool extractTextureData(unsigned width,
                                 unsigned height,
                                 GLenum format,
                                 GLenum type,
                                 unsigned unpackAlignment,
                                 bool flipY,
                                 bool premultiplyAlpha,
                                 const void* pixels,
                                 Vector<uint8_t>& data);

  // End GraphicsContext3DImagePacking.cpp functions

 private:
  friend class WebGLImageConversionTest;
  // Helper for packImageData/extractImageData/extractTextureData, which
  // implement packing of pixel data into the specified OpenGL destination
  // format and type. A sourceUnpackAlignment of zero indicates that the source
  // data is tightly packed. Non-zero values may take a slow path. Destination
  // data will have no gaps between rows. Implemented in
  // GraphicsContext3DImagePacking.cpp.
  static bool packPixels(const uint8_t* sourceData,
                         DataFormat sourceDataFormat,
                         unsigned sourceDataWidth,
                         unsigned sourceDataHeight,
                         const IntRect& sourceDataSubRectangle,
                         int depth,
                         unsigned sourceUnpackAlignment,
                         int unpackImageHeight,
                         unsigned destinationFormat,
                         unsigned destinationType,
                         AlphaOp,
                         void* destinationData,
                         bool flipY);
  static void unpackPixels(const uint16_t* sourceData,
                           DataFormat sourceDataFormat,
                           unsigned pixelsPerRow,
                           uint8_t* destinationData);
  static void packPixels(const uint8_t* sourceData,
                         DataFormat sourceDataFormat,
                         unsigned pixelsPerRow,
                         uint8_t* destinationData);
};

}  // namespace blink

#endif  // WebGLImageConversion_h