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 <limits>
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "gpu/command_buffer/common/gles2_utils_export.h"
     16 #include "gpu/command_buffer/common/types.h"
     17 
     18 namespace gpu {
     19 namespace gles2 {
     20 
     21 // Does a multiply and checks for overflow.  If the multiply did not overflow
     22 // returns true.
     23 
     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;
     30   }
     31   uint32 v = a * b;
     32   if (v / b != a) {
     33     *dst = 0;
     34     return false;
     35   }
     36   *dst = v;
     37   return true;
     38 }
     39 
     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;
     45   }
     46   *dst = a + b;
     47   return true;
     48 }
     49 
     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;
     57 }
     58 
     59 // Utilties for GLES2 support.
     60 class GLES2_UTILS_EXPORT GLES2Util {
     61  public:
     62   static const int kNumFaces = 6;
     63 
     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,
     72 
     73     kRGB = kRed | kGreen | kBlue,
     74     kRGBA = kRGB | kAlpha
     75   };
     76 
     77   struct EnumToString {
     78     uint32 value;
     79     const char* name;
     80   };
     81 
     82   GLES2Util()
     83       : num_compressed_texture_formats_(0),
     84         num_shader_binary_formats_(0) {
     85   }
     86 
     87   int num_compressed_texture_formats() const {
     88     return num_compressed_texture_formats_;
     89   }
     90 
     91   void set_num_compressed_texture_formats(int num_compressed_texture_formats) {
     92     num_compressed_texture_formats_ = num_compressed_texture_formats;
     93   }
     94 
     95   int num_shader_binary_formats() const {
     96     return num_shader_binary_formats_;
     97   }
     98 
     99   void set_num_shader_binary_formats(int num_shader_binary_formats) {
    100     num_shader_binary_formats_ = num_shader_binary_formats;
    101   }
    102 
    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;
    106 
    107   // Computes the size of a single group of elements from a format and type pair
    108   static uint32 ComputeImageGroupSize(int format, int type);
    109 
    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);
    114 
    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);
    122 
    123   static size_t RenderbufferBytesPerPixel(int format);
    124 
    125   static uint32 GetGLDataTypeSizeForUniforms(int type);
    126 
    127   static size_t GetGLTypeSizeForTexturesAndBuffers(uint32 type);
    128 
    129   static uint32 GLErrorToErrorBit(uint32 gl_error);
    130 
    131   static uint32 GLErrorBitToGLError(uint32 error_bit);
    132 
    133   static uint32 IndexToGLFaceTarget(int index);
    134 
    135   static uint32 GetPreferredGLReadPixelsFormat(uint32 internal_format);
    136 
    137   static uint32 GetPreferredGLReadPixelsType(
    138       uint32 internal_format, uint32 texture_type);
    139 
    140   // Returns a bitmask for the channels the given format supports.
    141   // See ChannelBits.
    142   static uint32 GetChannelsForFormat(int format);
    143 
    144   // Returns a bitmask for the channels the given attachment type needs.
    145   static uint32 GetChannelsNeededForAttachmentType(
    146       int type, uint32 max_color_attachments);
    147 
    148   static bool IsNPOT(uint32 value) {
    149     return value > 0 && (value & (value - 1)) != 0;
    150   }
    151 
    152   static std::string GetStringEnum(uint32 value);
    153   static std::string GetStringBool(uint32 value);
    154   static std::string GetStringError(uint32 value);
    155 
    156   // Parses a uniform name.
    157   //   array_pos: the position of the last '[' character in name.
    158   //   element_index: the index of the array element specifed in the name.
    159   //   getting_array: True if name refers to array.
    160   // returns true of parsing was successful. Returing true does NOT mean
    161   // it's a valid uniform name. On the otherhand, returning false does mean
    162   // it's an invalid uniform name.
    163   static bool ParseUniformName(
    164       const std::string& name,
    165       size_t* array_pos,
    166       int* element_index,
    167       bool* getting_array);
    168 
    169   #include "../common/gles2_cmd_utils_autogen.h"
    170 
    171  private:
    172   static std::string GetQualifiedEnumString(
    173       const EnumToString* table, size_t count, uint32 value);
    174 
    175   static const EnumToString* const enum_to_string_table_;
    176   static const size_t enum_to_string_table_len_;
    177 
    178   int num_compressed_texture_formats_;
    179   int num_shader_binary_formats_;
    180 };
    181 
    182 class GLES2_UTILS_EXPORT ContextCreationAttribHelper {
    183  public:
    184   ContextCreationAttribHelper();
    185 
    186   void Serialize(std::vector<int32>* attribs);
    187   bool Parse(const std::vector<int32>& attribs);
    188 
    189   // -1 if invalid or unspecified.
    190   int32 alpha_size_;
    191   int32 blue_size_;
    192   int32 green_size_;
    193   int32 red_size_;
    194   int32 depth_size_;
    195   int32 stencil_size_;
    196   int32 samples_;
    197   int32 sample_buffers_;
    198   bool buffer_preserved_;
    199   bool share_resources_;
    200   bool bind_generates_resource_;
    201   bool fail_if_major_perf_caveat_;
    202 };
    203 
    204 }  // namespace gles2
    205 }  // namespace gpu
    206 
    207 #endif  // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
    208 
    209