1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef WEBGLTYPES_H_
7 #define WEBGLTYPES_H_
8 
9 // Most WebIDL typedefs are identical to their OpenGL counterparts.
10 #include "GLTypes.h"
11 
12 // Manual reflection of WebIDL typedefs that are different from their
13 // OpenGL counterparts.
14 typedef int64_t WebGLsizeiptr;
15 typedef int64_t WebGLintptr;
16 typedef bool WebGLboolean;
17 
18 namespace mozilla {
19 namespace gl {
20 class GLContext;  // This is going to be needed a lot.
21 }  // namespace gl
22 
23 /*
24  * WebGLTextureFakeBlackStatus is an enum to track what needs to use a dummy 1x1
25  * black texture, which we refer to as a 'fake black' texture.
26  *
27  * There are two things that can cause us to use such 'fake black' textures:
28  *
29  *   (1) OpenGL ES rules on sampling incomplete textures specify that they
30  *       must be sampled as RGBA(0, 0, 0, 1) (opaque black). We have to
31  * implement these rules ourselves, if only because we do not always run on
32  * OpenGL ES, and also because this is dangerously close to the kind of
33  * case where we don't want to trust the driver with corner cases of
34  * texture memory accesses.
35  *
36  *   (2) OpenGL has cases where a renderbuffer, or a texture image, can contain
37  *       uninitialized image data. See below the comment about
38  * WebGLImageDataStatus. WebGL must never have access to uninitialized image
39  * data. The WebGL 1 spec, section 4.1 'Resource Restrictions', specifies
40  * that in any such case, the uninitialized image data must be exposed to
41  * WebGL as if it were filled with zero bytes, which means it's either
42  * opaque or transparent black depending on whether the image format has
43  * alpha.
44  */
45 
46 enum class FakeBlackType : uint8_t {
47   None,
48   RGBA0001,  // Incomplete textures and uninitialized no-alpha color textures.
49   RGBA0000,  // Uninitialized with-alpha color textures.
50 };
51 
52 /*
53  * Implementing WebGL (or OpenGL ES 2.0) on top of desktop OpenGL requires
54  * emulating the vertex attrib 0 array when it's not enabled. Indeed,
55  * OpenGL ES 2.0 allows drawing without vertex attrib 0 array enabled, but
56  * desktop OpenGL does not allow that.
57  */
58 enum class WebGLVertexAttrib0Status : uint8_t {
59   Default,                     // default status - no emulation needed
60   EmulatedUninitializedArray,  // need an artificial attrib 0 array, but
61                                // contents may be left uninitialized
62   EmulatedInitializedArray  // need an artificial attrib 0 array, and contents
63                             // must be initialized
64 };
65 
66 /*
67  * Enum to track the status of image data (renderbuffer or texture image)
68  * presence and initialization.
69  *
70  * - NoImageData is the initial state before any image data is allocated.
71  * - InitializedImageData is the state after image data is allocated and
72  * initialized.
73  * - UninitializedImageData is an intermediate state where data is allocated but
74  * not initialized. It is the state that renderbuffers are in after a
75  * renderbufferStorage call, and it is the state that texture images are in
76  * after a texImage2D call with null data.
77  */
78 enum class WebGLImageDataStatus : uint8_t {
79   NoImageData,
80   UninitializedImageData,
81   InitializedImageData
82 };
83 
84 /*
85  * The formats that may participate, either as source or destination formats,
86  * in WebGL texture conversions. This includes:
87  *  - all the formats accepted by WebGL.texImage2D, e.g. RGBA4444
88  *  - additional formats provided by extensions, e.g. RGB32F
89  *  - additional source formats, depending on browser details, used when
90  * uploading textures from DOM elements. See gfxImageSurface::Format().
91  */
92 enum class WebGLTexelFormat : uint8_t {
93   // returned by SurfaceFromElementResultToImageSurface to indicate absence of
94   // image data
95   None,
96   // common value for formats for which format conversions are not supported
97   FormatNotSupportingAnyConversion,
98   // dummy pseudo-format meaning "use the other format".
99   // For example, if SrcFormat=Auto and DstFormat=RGB8, then the source
100   // is implicitly treated as being RGB8 itself.
101   Auto,
102   // 1-channel formats
103   A8,
104   A16F,  // OES_texture_half_float
105   A32F,  // OES_texture_float
106   R8,
107   R16F,  // OES_texture_half_float
108   R32F,  // OES_texture_float
109   // 2-channel formats
110   RA8,
111   RA16F,  // OES_texture_half_float
112   RA32F,  // OES_texture_float
113   RG8,
114   RG16F,
115   RG32F,
116   // 3-channel formats
117   RGB8,
118   RGB565,
119   RGB11F11F10F,
120   RGB16F,  // OES_texture_half_float
121   RGB32F,  // OES_texture_float
122   // 4-channel formats
123   RGBA8,
124   RGBA5551,
125   RGBA4444,
126   RGBA16F,  // OES_texture_half_float
127   RGBA32F,  // OES_texture_float
128   // DOM element source only formats.
129   RGBX8,
130   BGRX8,
131   BGRA8
132 };
133 
134 enum class WebGLTexImageFunc : uint8_t {
135   TexImage,
136   TexSubImage,
137   CopyTexImage,
138   CopyTexSubImage,
139   CompTexImage,
140   CompTexSubImage,
141 };
142 
143 enum class WebGLTexDimensions : uint8_t { Tex2D, Tex3D };
144 
145 // Please keep extensions in alphabetic order.
146 enum class WebGLExtensionID : uint8_t {
147   ANGLE_instanced_arrays,
148   EXT_blend_minmax,
149   EXT_color_buffer_float,
150   EXT_color_buffer_half_float,
151   EXT_frag_depth,
152   EXT_sRGB,
153   EXT_shader_texture_lod,
154   EXT_texture_filter_anisotropic,
155   EXT_disjoint_timer_query,
156   MOZ_debug,
157   OES_element_index_uint,
158   OES_standard_derivatives,
159   OES_texture_float,
160   OES_texture_float_linear,
161   OES_texture_half_float,
162   OES_texture_half_float_linear,
163   OES_vertex_array_object,
164   WEBGL_color_buffer_float,
165   WEBGL_compressed_texture_astc,
166   WEBGL_compressed_texture_atc,
167   WEBGL_compressed_texture_etc,
168   WEBGL_compressed_texture_etc1,
169   WEBGL_compressed_texture_pvrtc,
170   WEBGL_compressed_texture_s3tc,
171   WEBGL_compressed_texture_s3tc_srgb,
172   WEBGL_debug_renderer_info,
173   WEBGL_debug_shaders,
174   WEBGL_depth_texture,
175   WEBGL_draw_buffers,
176   WEBGL_lose_context,
177   Max,
178   Unknown
179 };
180 
181 class UniqueBuffer {
182   // Like UniquePtr<>, but for void* and malloc/calloc/free.
183   void* mBuffer;
184 
185  public:
UniqueBuffer()186   UniqueBuffer() : mBuffer(nullptr) {}
187 
UniqueBuffer(void * buffer)188   MOZ_IMPLICIT UniqueBuffer(void* buffer) : mBuffer(buffer) {}
189 
~UniqueBuffer()190   ~UniqueBuffer() { free(mBuffer); }
191 
UniqueBuffer(UniqueBuffer && other)192   UniqueBuffer(UniqueBuffer&& other) {
193     this->mBuffer = other.mBuffer;
194     other.mBuffer = nullptr;
195   }
196 
197   UniqueBuffer& operator=(UniqueBuffer&& other) {
198     free(this->mBuffer);
199     this->mBuffer = other.mBuffer;
200     other.mBuffer = nullptr;
201     return *this;
202   }
203 
204   UniqueBuffer& operator=(void* newBuffer) {
205     free(this->mBuffer);
206     this->mBuffer = newBuffer;
207     return *this;
208   }
209 
210   explicit operator bool() const { return bool(mBuffer); }
211 
get()212   void* get() const { return mBuffer; }
213 
214   UniqueBuffer(const UniqueBuffer& other) = delete;  // construct using Move()!
215   void operator=(const UniqueBuffer& other) = delete;  // assign using Move()!
216 };
217 
218 }  // namespace mozilla
219 
220 #endif
221