Home | History | Annotate | Download | only in common
      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 size_t GLTargetToFaceIndex(uint32_t target);
    145 
    146   static uint32_t GetPreferredGLReadPixelsFormat(uint32_t internal_format);
    147 
    148   static uint32_t GetPreferredGLReadPixelsType(
    149       uint32_t internal_format, uint32_t texture_type);
    150 
    151   // Returns a bitmask for the channels the given format supports.
    152   // See ChannelBits.
    153   static uint32_t GetChannelsForFormat(int format);
    154 
    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);
    158 
    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;
    162   }
    163 
    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;
    167   }
    168 
    169   static std::string GetStringEnum(uint32_t value);
    170   static std::string GetStringBool(uint32_t value);
    171   static std::string GetStringError(uint32_t value);
    172 
    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);
    185 
    186   #include "../common/gles2_cmd_utils_autogen.h"
    187 
    188  private:
    189   static std::string GetQualifiedEnumString(
    190       const EnumToString* table, size_t count, uint32_t value);
    191 
    192   static const EnumToString* const enum_to_string_table_;
    193   static const size_t enum_to_string_table_len_;
    194 
    195   int num_compressed_texture_formats_;
    196   int num_shader_binary_formats_;
    197 };
    198 
    199 struct GLES2_UTILS_EXPORT ContextCreationAttribHelper {
    200   ContextCreationAttribHelper();
    201 
    202   void Serialize(std::vector<int32_t>* attribs) const;
    203   bool Parse(const std::vector<int32_t>& attribs);
    204 
    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;
    218 };
    219 
    220 }  // namespace gles2
    221 }  // namespace gpu
    222 
    223 #endif  // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
    224 
    225