Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.h
blob806b297326f2584689c005b0fea476621d692fad
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file is here so other GLES2 related files can have a common set of
6 // includes where appropriate.
8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
11 #include <stdint.h>
13 #include <limits>
14 #include <string>
15 #include <vector>
17 #include "base/numerics/safe_math.h"
18 #include "gpu/command_buffer/common/gles2_utils_export.h"
20 namespace gpu {
21 namespace gles2 {
23 // Does a multiply and checks for overflow. If the multiply did not overflow
24 // returns true.
26 // Multiplies 2 32 bit unsigned numbers checking for overflow.
27 // If there was no overflow returns true.
28 inline bool SafeMultiplyUint32(uint32_t a, uint32_t b, uint32_t* dst) {
29 DCHECK(dst);
30 base::CheckedNumeric<uint32_t> checked = a;
31 checked *= b;
32 *dst = checked.ValueOrDefault(0);
33 return checked.IsValid();
36 // Does an add checking for overflow. If there was no overflow returns true.
37 inline bool SafeAddUint32(uint32_t a, uint32_t b, uint32_t* dst) {
38 DCHECK(dst);
39 base::CheckedNumeric<uint32_t> checked = a;
40 checked += b;
41 *dst = checked.ValueOrDefault(0);
42 return checked.IsValid();
45 // Does an add checking for overflow. If there was no overflow returns true.
46 inline bool SafeAddInt32(int32_t a, int32_t b, int32_t* dst) {
47 DCHECK(dst);
48 base::CheckedNumeric<int32_t> checked = a;
49 checked += b;
50 *dst = checked.ValueOrDefault(0);
51 return checked.IsValid();
54 // Utilties for GLES2 support.
55 class GLES2_UTILS_EXPORT GLES2Util {
56 public:
57 static const int kNumFaces = 6;
59 // Bits returned by GetChannelsForFormat
60 enum ChannelBits {
61 kRed = 0x1,
62 kGreen = 0x2,
63 kBlue = 0x4,
64 kAlpha = 0x8,
65 kDepth = 0x10000,
66 kStencil = 0x20000,
68 kRGB = kRed | kGreen | kBlue,
69 kRGBA = kRGB | kAlpha
72 struct EnumToString {
73 uint32_t value;
74 const char* name;
77 GLES2Util()
78 : num_compressed_texture_formats_(0),
79 num_shader_binary_formats_(0) {
82 int num_compressed_texture_formats() const {
83 return num_compressed_texture_formats_;
86 void set_num_compressed_texture_formats(int num_compressed_texture_formats) {
87 num_compressed_texture_formats_ = num_compressed_texture_formats;
90 int num_shader_binary_formats() const {
91 return num_shader_binary_formats_;
94 void set_num_shader_binary_formats(int num_shader_binary_formats) {
95 num_shader_binary_formats_ = num_shader_binary_formats;
98 // Gets the number of values a particular id will return when a glGet
99 // function is called. If 0 is returned the id is invalid.
100 int GLGetNumValuesReturned(int id) const;
102 // Computes the size of a single group of elements from a format and type pair
103 static uint32_t ComputeImageGroupSize(int format, int type);
105 // Computes the size of an image row including alignment padding
106 static bool ComputeImagePaddedRowSize(
107 int width, int format, int type, int unpack_alignment,
108 uint32_t* padded_row_size);
110 // Computes the size of image data for TexImage2D and TexSubImage2D.
111 // Optionally the unpadded and padded row sizes can be returned. If height < 2
112 // then the padded_row_size will be the same as the unpadded_row_size since
113 // padding is not necessary.
114 static bool ComputeImageDataSizes(
115 int width, int height, int depth, int format, int type,
116 int unpack_alignment, uint32_t* size, uint32_t* unpadded_row_size,
117 uint32_t* padded_row_size);
119 static size_t RenderbufferBytesPerPixel(int format);
121 // Return the element's number of bytes.
122 // For example, GL_FLOAT_MAT3 returns sizeof(GLfloat).
123 static uint32_t GetElementSizeForUniformType(int type);
124 // Return the number of elements.
125 // For example, GL_FLOAT_MAT3 returns 9.
126 static uint32_t GetElementCountForUniformType(int type);
128 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32_t type);
130 static size_t GetGLTypeSizeForPathCoordType(uint32_t type);
132 static uint32_t GLErrorToErrorBit(uint32_t gl_error);
134 static uint32_t GLErrorBitToGLError(uint32_t error_bit);
136 static uint32_t IndexToGLFaceTarget(int index);
138 static size_t GLTargetToFaceIndex(uint32_t target);
140 static uint32_t GetGLReadPixelsImplementationFormat(
141 uint32_t internal_format);
143 static uint32_t GetGLReadPixelsImplementationType(
144 uint32_t internal_format, uint32_t texture_type);
146 // Returns a bitmask for the channels the given format supports.
147 // See ChannelBits.
148 static uint32_t GetChannelsForFormat(int format);
150 // Returns a bitmask for the channels the given attachment type needs.
151 static uint32_t GetChannelsNeededForAttachmentType(
152 int type, uint32_t max_color_attachments);
154 // Return true if value is neither a power of two nor zero.
155 static bool IsNPOT(uint32_t value) {
156 return (value & (value - 1)) != 0;
159 // Return true if value is a power of two or zero.
160 static bool IsPOT(uint32_t value) {
161 return (value & (value - 1)) == 0;
164 static std::string GetStringEnum(uint32_t value);
165 static std::string GetStringBool(uint32_t value);
166 static std::string GetStringError(uint32_t value);
168 // Parses a uniform name.
169 // array_pos: the position of the last '[' character in name.
170 // element_index: the index of the array element specifed in the name.
171 // getting_array: True if name refers to array.
172 // returns true of parsing was successful. Returing true does NOT mean
173 // it's a valid uniform name. On the otherhand, returning false does mean
174 // it's an invalid uniform name.
175 static bool ParseUniformName(
176 const std::string& name,
177 size_t* array_pos,
178 int* element_index,
179 bool* getting_array);
181 static size_t CalcClearBufferivDataCount(int buffer);
182 static size_t CalcClearBufferfvDataCount(int buffer);
184 static void MapUint64ToTwoUint32(
185 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1);
186 static uint64_t MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1);
188 static uint32_t MapBufferTargetToBindingEnum(uint32_t target);
190 static bool IsUnsignedIntegerFormat(uint32_t internal_format);
191 static bool IsSignedIntegerFormat(uint32_t internal_format);
192 static bool IsIntegerFormat(uint32_t internal_format);
194 #include "../common/gles2_cmd_utils_autogen.h"
196 private:
197 static std::string GetQualifiedEnumString(
198 const EnumToString* table, size_t count, uint32_t value);
200 static const EnumToString* const enum_to_string_table_;
201 static const size_t enum_to_string_table_len_;
203 int num_compressed_texture_formats_;
204 int num_shader_binary_formats_;
207 struct GLES2_UTILS_EXPORT ContextCreationAttribHelper {
208 ContextCreationAttribHelper();
210 void Serialize(std::vector<int32_t>* attribs) const;
211 bool Parse(const std::vector<int32_t>& attribs);
213 // -1 if invalid or unspecified.
214 int32_t alpha_size;
215 int32_t blue_size;
216 int32_t green_size;
217 int32_t red_size;
218 int32_t depth_size;
219 int32_t stencil_size;
220 int32_t samples;
221 int32_t sample_buffers;
222 bool buffer_preserved;
223 bool bind_generates_resource;
224 bool fail_if_major_perf_caveat;
225 bool lose_context_when_out_of_memory;
226 // 0 if not a WebGL context.
227 unsigned webgl_version;
230 } // namespace gles2
231 } // namespace gpu
233 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_