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. 4 5 // This file is here so other GLES2 related files can have a common set of 6 // includes where appropriate. 7 8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ 9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ 10 11 #include <stdint.h> 12 13 #include <limits> 14 #include <string> 15 #include <vector> 16 17 #include "gpu/command_buffer/common/gles2_utils_export.h" 18 19 namespace gpu { 20 namespace gles2 { 21 22 // Does a multiply and checks for overflow. If the multiply did not overflow 23 // returns true. 24 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; 31 } 32 uint32_t v = a * b; 33 if (v / b != a) { 34 *dst = 0; 35 return false; 36 } 37 *dst = v; 38 return true; 39 } 40 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; 46 } 47 *dst = a + b; 48 return true; 49 } 50 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; 58 } 59 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)); 66 } 67 68 // Utilties for GLES2 support. 69 class GLES2_UTILS_EXPORT GLES2Util { 70 public: 71 static const int kNumFaces = 6; 72 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, 81 82 kRGB = kRed | kGreen | kBlue, 83 kRGBA = kRGB | kAlpha 84 }; 85 86 struct EnumToString { 87 uint32_t value; 88 const char* name; 89 }; 90 91 GLES2Util() 92 : num_compressed_texture_formats_(0), 93 num_shader_binary_formats_(0) { 94 } 95 96 int num_compressed_texture_formats() const { 97 return num_compressed_texture_formats_; 98 } 99 100 void set_num_compressed_texture_formats(int num_compressed_texture_formats) { 101 num_compressed_texture_formats_ = num_compressed_texture_formats; 102 } 103 104 int num_shader_binary_formats() const { 105 return num_shader_binary_formats_; 106 } 107 108 void set_num_shader_binary_formats(int num_shader_binary_formats) { 109 num_shader_binary_formats_ = num_shader_binary_formats; 110 } 111 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; 115 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); 118 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); 123 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); 131 132 static size_t RenderbufferBytesPerPixel(int format); 133 134 static uint32_t GetGLDataTypeSizeForUniforms(int type); 135 136 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32_t type); 137 138 static uint32_t GLErrorToErrorBit(uint32_t gl_error); 139 140 static uint32_t GLErrorBitToGLError(uint32_t error_bit); 141 142 static uint32_t IndexToGLFaceTarget(int index); 143 144 static uint32_t GetPreferredGLReadPixelsFormat(uint32_t internal_format); 145 146 static uint32_t GetPreferredGLReadPixelsType( 147 uint32_t internal_format, uint32_t texture_type); 148 149 // Returns a bitmask for the channels the given format supports. 150 // See ChannelBits. 151 static uint32_t GetChannelsForFormat(int format); 152 153 // Returns a bitmask for the channels the given attachment type needs. 154 static uint32_t GetChannelsNeededForAttachmentType( 155 int type, uint32_t max_color_attachments); 156 157 static bool IsNPOT(uint32_t value) { 158 return value > 0 && (value & (value - 1)) != 0; 159 } 160 161 static std::string GetStringEnum(uint32_t value); 162 static std::string GetStringBool(uint32_t value); 163 static std::string GetStringError(uint32_t value); 164 165 // Parses a uniform name. 166 // array_pos: the position of the last '[' character in name. 167 // element_index: the index of the array element specifed in the name. 168 // getting_array: True if name refers to array. 169 // returns true of parsing was successful. Returing true does NOT mean 170 // it's a valid uniform name. On the otherhand, returning false does mean 171 // it's an invalid uniform name. 172 static bool ParseUniformName( 173 const std::string& name, 174 size_t* array_pos, 175 int* element_index, 176 bool* getting_array); 177 178 #include "../common/gles2_cmd_utils_autogen.h" 179 180 private: 181 static std::string GetQualifiedEnumString( 182 const EnumToString* table, size_t count, uint32_t value); 183 184 static const EnumToString* const enum_to_string_table_; 185 static const size_t enum_to_string_table_len_; 186 187 int num_compressed_texture_formats_; 188 int num_shader_binary_formats_; 189 }; 190 191 class GLES2_UTILS_EXPORT ContextCreationAttribHelper { 192 public: 193 ContextCreationAttribHelper(); 194 195 void Serialize(std::vector<int32_t>* attribs); 196 bool Parse(const std::vector<int32_t>& attribs); 197 198 // -1 if invalid or unspecified. 199 int32_t alpha_size_; 200 int32_t blue_size_; 201 int32_t green_size_; 202 int32_t red_size_; 203 int32_t depth_size_; 204 int32_t stencil_size_; 205 int32_t samples_; 206 int32_t sample_buffers_; 207 bool buffer_preserved_; 208 bool share_resources_; 209 bool bind_generates_resource_; 210 bool fail_if_major_perf_caveat_; 211 bool lose_context_when_out_of_memory_; 212 }; 213 214 } // namespace gles2 215 } // namespace gpu 216 217 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ 218 219