Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.h
blob4946205b365b74c001c1ecf76ad37e8688af89df
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 GetPreferredGLReadPixelsFormat(uint32_t internal_format);
142 static uint32_t GetPreferredGLReadPixelsType(
143 uint32_t internal_format, uint32_t texture_type);
145 // Returns a bitmask for the channels the given format supports.
146 // See ChannelBits.
147 static uint32_t GetChannelsForFormat(int format);
149 // Returns a bitmask for the channels the given attachment type needs.
150 static uint32_t GetChannelsNeededForAttachmentType(
151 int type, uint32_t max_color_attachments);
153 // Return true if value is neither a power of two nor zero.
154 static bool IsNPOT(uint32_t value) {
155 return (value & (value - 1)) != 0;
158 // Return true if value is a power of two or zero.
159 static bool IsPOT(uint32_t value) {
160 return (value & (value - 1)) == 0;
163 static std::string GetStringEnum(uint32_t value);
164 static std::string GetStringBool(uint32_t value);
165 static std::string GetStringError(uint32_t value);
167 // Parses a uniform name.
168 // array_pos: the position of the last '[' character in name.
169 // element_index: the index of the array element specifed in the name.
170 // getting_array: True if name refers to array.
171 // returns true of parsing was successful. Returing true does NOT mean
172 // it's a valid uniform name. On the otherhand, returning false does mean
173 // it's an invalid uniform name.
174 static bool ParseUniformName(
175 const std::string& name,
176 size_t* array_pos,
177 int* element_index,
178 bool* getting_array);
180 static size_t CalcClearBufferivDataCount(int buffer);
181 static size_t CalcClearBufferfvDataCount(int buffer);
183 static void MapUint64ToTwoUint32(
184 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1);
185 static uint64_t MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1);
187 static uint32_t MapBufferTargetToBindingEnum(uint32_t target);
189 #include "../common/gles2_cmd_utils_autogen.h"
191 private:
192 static std::string GetQualifiedEnumString(
193 const EnumToString* table, size_t count, uint32_t value);
195 static const EnumToString* const enum_to_string_table_;
196 static const size_t enum_to_string_table_len_;
198 int num_compressed_texture_formats_;
199 int num_shader_binary_formats_;
202 struct GLES2_UTILS_EXPORT ContextCreationAttribHelper {
203 ContextCreationAttribHelper();
205 void Serialize(std::vector<int32_t>* attribs) const;
206 bool Parse(const std::vector<int32_t>& attribs);
208 // -1 if invalid or unspecified.
209 int32_t alpha_size;
210 int32_t blue_size;
211 int32_t green_size;
212 int32_t red_size;
213 int32_t depth_size;
214 int32_t stencil_size;
215 int32_t samples;
216 int32_t sample_buffers;
217 bool buffer_preserved;
218 bool bind_generates_resource;
219 bool fail_if_major_perf_caveat;
220 bool lose_context_when_out_of_memory;
221 // 0 if not a WebGL context.
222 unsigned webgl_version;
225 } // namespace gles2
226 } // namespace gpu
228 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_