Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.h
blob3528cfdbddcd0e1ce4098ee8bb9378953073822f
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 depth, int format, int type,
130 int unpack_alignment, uint32_t* size, uint32_t* unpadded_row_size,
131 uint32_t* padded_row_size);
133 static size_t RenderbufferBytesPerPixel(int format);
135 static uint32_t GetGLDataTypeSizeForUniforms(int type);
137 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32_t type);
139 static uint32_t GLErrorToErrorBit(uint32_t gl_error);
141 static uint32_t GLErrorBitToGLError(uint32_t error_bit);
143 static uint32_t IndexToGLFaceTarget(int index);
145 static size_t GLTargetToFaceIndex(uint32_t target);
147 static uint32_t GetPreferredGLReadPixelsFormat(uint32_t internal_format);
149 static uint32_t GetPreferredGLReadPixelsType(
150 uint32_t internal_format, uint32_t texture_type);
152 // Returns a bitmask for the channels the given format supports.
153 // See ChannelBits.
154 static uint32_t GetChannelsForFormat(int format);
156 // Returns a bitmask for the channels the given attachment type needs.
157 static uint32_t GetChannelsNeededForAttachmentType(
158 int type, uint32_t max_color_attachments);
160 // Return true if value is neither a power of two nor zero.
161 static bool IsNPOT(uint32_t value) {
162 return (value & (value - 1)) != 0;
165 // Return true if value is a power of two or zero.
166 static bool IsPOT(uint32_t value) {
167 return (value & (value - 1)) == 0;
170 static std::string GetStringEnum(uint32_t value);
171 static std::string GetStringBool(uint32_t value);
172 static std::string GetStringError(uint32_t value);
174 // Parses a uniform name.
175 // array_pos: the position of the last '[' character in name.
176 // element_index: the index of the array element specifed in the name.
177 // getting_array: True if name refers to array.
178 // returns true of parsing was successful. Returing true does NOT mean
179 // it's a valid uniform name. On the otherhand, returning false does mean
180 // it's an invalid uniform name.
181 static bool ParseUniformName(
182 const std::string& name,
183 size_t* array_pos,
184 int* element_index,
185 bool* getting_array);
187 static size_t CalcClearBufferivDataCount(int buffer);
188 static size_t CalcClearBufferfvDataCount(int buffer);
190 static void MapUint64ToTwoUint32(
191 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1);
192 static uint64_t MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1);
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;
228 } // namespace gles2
229 } // namespace gpu
231 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_