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   // Returns a bitmask for the channels the given format supports.
    136   // See ChannelBits.
    137   static uint32 GetChannelsForFormat(int format);
    138 
    139   // Returns a bitmask for the channels the given attachment type needs.
    140   static uint32 GetChannelsNeededForAttachmentType(
    141       int type, uint32 max_color_attachments);
    142 
    143   static bool IsNPOT(uint32 value) {
    144     return value > 0 && (value & (value - 1)) != 0;
    145   }
    146 
    147   static std::string GetStringEnum(uint32 value);
    148   static std::string GetStringBool(uint32 value);
    149   static std::string GetStringError(uint32 value);
    150 
    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);
    163 
    164   #include "../common/gles2_cmd_utils_autogen.h"
    165 
    166  private:
    167   static std::string GetQualifiedEnumString(
    168       const EnumToString* table, size_t count, uint32 value);
    169 
    170   static const EnumToString* const enum_to_string_table_;
    171   static const size_t enum_to_string_table_len_;
    172 
    173   int num_compressed_texture_formats_;
    174   int num_shader_binary_formats_;
    175 };
    176 
    177 class GLES2_UTILS_EXPORT ContextCreationAttribParser {
    178  public:
    179   ContextCreationAttribParser();
    180   bool Parse(const std::vector<int32>& attribs);
    181 
    182   // -1 if invalid or unspecified.
    183   int32 alpha_size_;
    184   int32 blue_size_;
    185   int32 green_size_;
    186   int32 red_size_;
    187   int32 depth_size_;
    188   int32 stencil_size_;
    189   int32 samples_;
    190   int32 sample_buffers_;
    191   bool buffer_preserved_;
    192   bool share_resources_;
    193   bool bind_generates_resource_;
    194 };
    195 
    196 }  // namespace gles2
    197 }  // namespace gpu
    198 
    199 #endif  // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
    200 
    201