cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.h
blobb8619e61d3785dc4a619e4824faa34468defae08
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 <limits>
12 #include <string>
13 #include <vector>
15 #include "gpu/command_buffer/common/gles2_utils_export.h"
16 #include "gpu/command_buffer/common/types.h"
18 namespace gpu {
19 namespace gles2 {
21 // Does a multiply and checks for overflow. If the multiply did not overflow
22 // returns true.
24 // Multiplies 2 32 bit unsigned numbers checking for overflow.
25 // If there was no overflow returns true.
26 inline bool SafeMultiplyUint32(uint32 a, uint32 b, uint32* dst) {
27 if (b == 0) {
28 *dst = 0;
29 return true;
31 uint32 v = a * b;
32 if (v / b != a) {
33 *dst = 0;
34 return false;
36 *dst = v;
37 return true;
40 // Does an add checking for overflow. If there was no overflow returns true.
41 inline bool SafeAddUint32(uint32 a, uint32 b, uint32* dst) {
42 if (a + b < a) {
43 *dst = 0;
44 return false;
46 *dst = a + b;
47 return true;
50 // Does an add checking for overflow. If there was no overflow returns true.
51 inline bool SafeAddInt32(int32 a, int32 b, int32* dst) {
52 int64 sum64 = static_cast<int64>(a) + b;
53 int32 sum32 = static_cast<int32>(sum64);
54 bool safe = sum64 == static_cast<int64>(sum32);
55 *dst = safe ? sum32 : 0;
56 return safe;
59 // Utilties for GLES2 support.
60 class GLES2_UTILS_EXPORT GLES2Util {
61 public:
62 static const int kNumFaces = 6;
64 // Bits returned by GetChannelsForFormat
65 enum ChannelBits {
66 kRed = 0x1,
67 kGreen = 0x2,
68 kBlue = 0x4,
69 kAlpha = 0x8,
70 kDepth = 0x10000,
71 kStencil = 0x20000,
73 kRGB = kRed | kGreen | kBlue,
74 kRGBA = kRGB | kAlpha
77 struct EnumToString {
78 uint32 value;
79 const char* name;
82 GLES2Util()
83 : num_compressed_texture_formats_(0),
84 num_shader_binary_formats_(0) {
87 int num_compressed_texture_formats() const {
88 return num_compressed_texture_formats_;
91 void set_num_compressed_texture_formats(int num_compressed_texture_formats) {
92 num_compressed_texture_formats_ = num_compressed_texture_formats;
95 int num_shader_binary_formats() const {
96 return num_shader_binary_formats_;
99 void set_num_shader_binary_formats(int num_shader_binary_formats) {
100 num_shader_binary_formats_ = num_shader_binary_formats;
103 // Gets the number of values a particular id will return when a glGet
104 // function is called. If 0 is returned the id is invalid.
105 int GLGetNumValuesReturned(int id) const;
107 // Computes the size of a single group of elements from a format and type pair
108 static uint32 ComputeImageGroupSize(int format, int type);
110 // Computes the size of an image row including alignment padding
111 static bool ComputeImagePaddedRowSize(
112 int width, int format, int type, int unpack_alignment,
113 uint32* padded_row_size);
115 // Computes the size of image data for TexImage2D and TexSubImage2D.
116 // Optionally the unpadded and padded row sizes can be returned. If height < 2
117 // then the padded_row_size will be the same as the unpadded_row_size since
118 // padding is not necessary.
119 static bool ComputeImageDataSizes(
120 int width, int height, int format, int type, int unpack_alignment,
121 uint32* size, uint32* unpadded_row_size, uint32* padded_row_size);
123 static size_t RenderbufferBytesPerPixel(int format);
125 static uint32 GetGLDataTypeSizeForUniforms(int type);
127 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32 type);
129 static uint32 GLErrorToErrorBit(uint32 gl_error);
131 static uint32 GLErrorBitToGLError(uint32 error_bit);
133 static uint32 IndexToGLFaceTarget(int index);
135 // Returns a bitmask for the channels the given format supports.
136 // See ChannelBits.
137 static uint32 GetChannelsForFormat(int format);
139 // Returns a bitmask for the channels the given attachment type needs.
140 static uint32 GetChannelsNeededForAttachmentType(
141 int type, uint32 max_color_attachments);
143 static bool IsNPOT(uint32 value) {
144 return value > 0 && (value & (value - 1)) != 0;
147 static std::string GetStringEnum(uint32 value);
148 static std::string GetStringBool(uint32 value);
149 static std::string GetStringError(uint32 value);
151 // Parses a uniform name.
152 // array_pos: the position of the last '[' character in name.
153 // element_index: the index of the array element specifed in the name.
154 // getting_array: True if name refers to array.
155 // returns true of parsing was successful. Returing true does NOT mean
156 // it's a valid uniform name. On the otherhand, returning false does mean
157 // it's an invalid uniform name.
158 static bool ParseUniformName(
159 const std::string& name,
160 size_t* array_pos,
161 int* element_index,
162 bool* getting_array);
164 #include "../common/gles2_cmd_utils_autogen.h"
166 private:
167 static std::string GetQualifiedEnumString(
168 const EnumToString* table, size_t count, uint32 value);
170 static const EnumToString* const enum_to_string_table_;
171 static const size_t enum_to_string_table_len_;
173 int num_compressed_texture_formats_;
174 int num_shader_binary_formats_;
177 class GLES2_UTILS_EXPORT ContextCreationAttribHelper {
178 public:
179 ContextCreationAttribHelper();
181 void Serialize(std::vector<int32>* attribs);
182 bool Parse(const std::vector<int32>& attribs);
184 // -1 if invalid or unspecified.
185 int32 alpha_size_;
186 int32 blue_size_;
187 int32 green_size_;
188 int32 red_size_;
189 int32 depth_size_;
190 int32 stencil_size_;
191 int32 samples_;
192 int32 sample_buffers_;
193 bool buffer_preserved_;
194 bool share_resources_;
195 bool bind_generates_resource_;
198 } // namespace gles2
199 } // namespace gpu
201 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_