Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.h
blob163ffc008c206f1edb1674d7f321d238299ef10c
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 "gpu/command_buffer/common/gles2_utils_export.h"
19 namespace gpu {
20 namespace gles2 {
22 // Does a multiply and checks for overflow. If the multiply did not overflow
23 // returns true.
25 // Multiplies 2 32 bit unsigned numbers checking for overflow.
26 // If there was no overflow returns true.
27 inline bool SafeMultiplyUint32(uint32_t a, uint32_t b, uint32_t* dst) {
28 if (b == 0) {
29 *dst = 0;
30 return true;
32 uint32_t v = a * b;
33 if (v / b != a) {
34 *dst = 0;
35 return false;
37 *dst = v;
38 return true;
41 // Does an add checking for overflow. If there was no overflow returns true.
42 inline bool SafeAddUint32(uint32_t a, uint32_t b, uint32_t* dst) {
43 if (a + b < a) {
44 *dst = 0;
45 return false;
47 *dst = a + b;
48 return true;
51 // Does an add checking for overflow. If there was no overflow returns true.
52 inline bool SafeAddInt32(int32_t a, int32_t b, int32_t* dst) {
53 int64_t sum64 = static_cast<int64_t>(a) + b;
54 int32_t sum32 = static_cast<int32_t>(sum64);
55 bool safe = sum64 == static_cast<int64_t>(sum32);
56 *dst = safe ? sum32 : 0;
57 return safe;
60 // Return false if |value| is more than a 32 bit integer can represent.
61 template<typename T>
62 inline bool FitInt32NonNegative(T value) {
63 const int32_t max = std::numeric_limits<int32_t>::max();
64 return (std::numeric_limits<T>::max() <= max ||
65 value <= static_cast<T>(max));
68 // Utilties for GLES2 support.
69 class GLES2_UTILS_EXPORT GLES2Util {
70 public:
71 static const int kNumFaces = 6;
73 // Bits returned by GetChannelsForFormat
74 enum ChannelBits {
75 kRed = 0x1,
76 kGreen = 0x2,
77 kBlue = 0x4,
78 kAlpha = 0x8,
79 kDepth = 0x10000,
80 kStencil = 0x20000,
82 kRGB = kRed | kGreen | kBlue,
83 kRGBA = kRGB | kAlpha
86 struct EnumToString {
87 uint32_t value;
88 const char* name;
91 GLES2Util()
92 : num_compressed_texture_formats_(0),
93 num_shader_binary_formats_(0) {
96 int num_compressed_texture_formats() const {
97 return num_compressed_texture_formats_;
100 void set_num_compressed_texture_formats(int num_compressed_texture_formats) {
101 num_compressed_texture_formats_ = num_compressed_texture_formats;
104 int num_shader_binary_formats() const {
105 return num_shader_binary_formats_;
108 void set_num_shader_binary_formats(int num_shader_binary_formats) {
109 num_shader_binary_formats_ = num_shader_binary_formats;
112 // Gets the number of values a particular id will return when a glGet
113 // function is called. If 0 is returned the id is invalid.
114 int GLGetNumValuesReturned(int id) const;
116 // Computes the size of a single group of elements from a format and type pair
117 static uint32_t ComputeImageGroupSize(int format, int type);
119 // Computes the size of an image row including alignment padding
120 static bool ComputeImagePaddedRowSize(
121 int width, int format, int type, int unpack_alignment,
122 uint32_t* padded_row_size);
124 // Computes the size of image data for TexImage2D and TexSubImage2D.
125 // Optionally the unpadded and padded row sizes can be returned. If height < 2
126 // then the padded_row_size will be the same as the unpadded_row_size since
127 // padding is not necessary.
128 static bool ComputeImageDataSizes(
129 int width, int height, int format, int type, int unpack_alignment,
130 uint32_t* size, uint32_t* unpadded_row_size, uint32_t* padded_row_size);
132 static size_t RenderbufferBytesPerPixel(int format);
134 static uint32_t GetGLDataTypeSizeForUniforms(int type);
136 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32_t type);
138 static uint32_t GLErrorToErrorBit(uint32_t gl_error);
140 static uint32_t GLErrorBitToGLError(uint32_t error_bit);
142 static uint32_t IndexToGLFaceTarget(int index);
144 static size_t GLTargetToFaceIndex(uint32_t target);
146 static uint32_t GetPreferredGLReadPixelsFormat(uint32_t internal_format);
148 static uint32_t GetPreferredGLReadPixelsType(
149 uint32_t internal_format, uint32_t texture_type);
151 // Returns a bitmask for the channels the given format supports.
152 // See ChannelBits.
153 static uint32_t GetChannelsForFormat(int format);
155 // Returns a bitmask for the channels the given attachment type needs.
156 static uint32_t GetChannelsNeededForAttachmentType(
157 int type, uint32_t max_color_attachments);
159 // Return true if value is neither a power of two nor zero.
160 static bool IsNPOT(uint32_t value) {
161 return (value & (value - 1)) != 0;
164 // Return true if value is a power of two or zero.
165 static bool IsPOT(uint32_t value) {
166 return (value & (value - 1)) == 0;
169 static std::string GetStringEnum(uint32_t value);
170 static std::string GetStringBool(uint32_t value);
171 static std::string GetStringError(uint32_t value);
173 // Parses a uniform name.
174 // array_pos: the position of the last '[' character in name.
175 // element_index: the index of the array element specifed in the name.
176 // getting_array: True if name refers to array.
177 // returns true of parsing was successful. Returing true does NOT mean
178 // it's a valid uniform name. On the otherhand, returning false does mean
179 // it's an invalid uniform name.
180 static bool ParseUniformName(
181 const std::string& name,
182 size_t* array_pos,
183 int* element_index,
184 bool* getting_array);
186 #include "../common/gles2_cmd_utils_autogen.h"
188 private:
189 static std::string GetQualifiedEnumString(
190 const EnumToString* table, size_t count, uint32_t value);
192 static const EnumToString* const enum_to_string_table_;
193 static const size_t enum_to_string_table_len_;
195 int num_compressed_texture_formats_;
196 int num_shader_binary_formats_;
199 struct GLES2_UTILS_EXPORT ContextCreationAttribHelper {
200 ContextCreationAttribHelper();
202 void Serialize(std::vector<int32_t>* attribs) const;
203 bool Parse(const std::vector<int32_t>& attribs);
205 // -1 if invalid or unspecified.
206 int32_t alpha_size;
207 int32_t blue_size;
208 int32_t green_size;
209 int32_t red_size;
210 int32_t depth_size;
211 int32_t stencil_size;
212 int32_t samples;
213 int32_t sample_buffers;
214 bool buffer_preserved;
215 bool bind_generates_resource;
216 bool fail_if_major_perf_caveat;
217 bool lose_context_when_out_of_memory;
220 } // namespace gles2
221 } // namespace gpu
223 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_