Home | History | Annotate | Download | only in service
      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 #ifndef GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
      6 #define GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
      7 
      8 #include <algorithm>
      9 #include <list>
     10 #include <set>
     11 #include <string>
     12 #include <vector>
     13 #include "base/basictypes.h"
     14 #include "base/containers/hash_tables.h"
     15 #include "base/memory/ref_counted.h"
     16 #include "gpu/command_buffer/service/async_pixel_transfer_delegate.h"
     17 #include "gpu/command_buffer/service/gl_utils.h"
     18 #include "gpu/command_buffer/service/memory_tracking.h"
     19 #include "gpu/gpu_export.h"
     20 #include "ui/gl/gl_image.h"
     21 
     22 namespace gpu {
     23 namespace gles2 {
     24 
     25 class GLES2Decoder;
     26 struct ContextState;
     27 struct DecoderFramebufferState;
     28 class Display;
     29 class ErrorState;
     30 class FeatureInfo;
     31 class FramebufferManager;
     32 class MailboxManager;
     33 class TextureManager;
     34 class TextureRef;
     35 
     36 // Info about Textures currently in the system.
     37 // This class wraps a real GL texture, keeping track of its meta-data. It is
     38 // jointly owned by possibly multiple TextureRef.
     39 class GPU_EXPORT Texture {
     40  public:
     41   explicit Texture(GLuint service_id);
     42 
     43   GLenum min_filter() const {
     44     return min_filter_;
     45   }
     46 
     47   GLenum mag_filter() const {
     48     return mag_filter_;
     49   }
     50 
     51   GLenum wrap_s() const {
     52     return wrap_s_;
     53   }
     54 
     55   GLenum wrap_t() const {
     56     return wrap_t_;
     57   }
     58 
     59   GLenum usage() const {
     60     return usage_;
     61   }
     62 
     63   GLenum pool() const {
     64     return pool_;
     65   }
     66 
     67   int num_uncleared_mips() const {
     68     return num_uncleared_mips_;
     69   }
     70 
     71   uint32 estimated_size() const {
     72     return estimated_size_;
     73   }
     74 
     75   bool CanRenderTo() const {
     76     return target_ != GL_TEXTURE_EXTERNAL_OES;
     77   }
     78 
     79   // The service side OpenGL id of the texture.
     80   GLuint service_id() const {
     81     return service_id_;
     82   }
     83 
     84   void SetServiceId(GLuint service_id) {
     85     DCHECK(service_id);
     86     service_id_ = service_id;
     87   }
     88 
     89   // Returns the target this texure was first bound to or 0 if it has not
     90   // been bound. Once a texture is bound to a specific target it can never be
     91   // bound to a different target.
     92   GLenum target() const {
     93     return target_;
     94   }
     95 
     96   bool SafeToRenderFrom() const {
     97     return cleared_;
     98   }
     99 
    100   // Get the width and height for a particular level. Returns false if level
    101   // does not exist.
    102   bool GetLevelSize(
    103       GLint target, GLint level, GLsizei* width, GLsizei* height) const;
    104 
    105   // Get the type of a level. Returns false if level does not exist.
    106   bool GetLevelType(
    107       GLint target, GLint level, GLenum* type, GLenum* internal_format) const;
    108 
    109   // Get the image bound to a particular level. Returns NULL if level
    110   // does not exist.
    111   gfx::GLImage* GetLevelImage(GLint target, GLint level) const;
    112 
    113   bool HasImages() const {
    114     return has_images_;
    115   }
    116 
    117   // Returns true of the given dimensions are inside the dimensions of the
    118   // level and if the type matches the level.
    119   bool ValidForTexture(
    120       GLint target,
    121       GLint level,
    122       GLint xoffset,
    123       GLint yoffset,
    124       GLsizei width,
    125       GLsizei height,
    126       GLenum type) const;
    127 
    128   bool IsValid() const {
    129     return !!target();
    130   }
    131 
    132   bool IsAttachedToFramebuffer() const {
    133     return framebuffer_attachment_count_ != 0;
    134   }
    135 
    136   void AttachToFramebuffer() {
    137     ++framebuffer_attachment_count_;
    138   }
    139 
    140   void DetachFromFramebuffer() {
    141     DCHECK_GT(framebuffer_attachment_count_, 0);
    142     --framebuffer_attachment_count_;
    143   }
    144 
    145   void SetImmutable(bool immutable) {
    146     immutable_ = immutable;
    147   }
    148 
    149   bool IsImmutable() const {
    150     return immutable_;
    151   }
    152 
    153   // Whether a particular level/face is cleared.
    154   bool IsLevelCleared(GLenum target, GLint level) const;
    155 
    156   // Whether the texture has been defined
    157   bool IsDefined() const {
    158     return estimated_size() > 0;
    159   }
    160 
    161   // Initialize TEXTURE_MAX_ANISOTROPY to 1 if we haven't done so yet.
    162   void InitTextureMaxAnisotropyIfNeeded(GLenum target);
    163 
    164   void OnWillModifyPixels();
    165   void OnDidModifyPixels();
    166 
    167  private:
    168   friend class MailboxManager;
    169   friend class MailboxManagerTest;
    170   friend class TextureDefinition;
    171   friend class TextureManager;
    172   friend class TextureRef;
    173   friend class TextureTestHelper;
    174 
    175   ~Texture();
    176   void AddTextureRef(TextureRef* ref);
    177   void RemoveTextureRef(TextureRef* ref, bool have_context);
    178   MemoryTypeTracker* GetMemTracker();
    179 
    180   // Condition on which this texture is renderable. Can be ONLY_IF_NPOT if it
    181   // depends on context support for non-power-of-two textures (i.e. will be
    182   // renderable if NPOT support is in the context, otherwise not, e.g. texture
    183   // with a NPOT level). ALWAYS means it doesn't depend on context features
    184   // (e.g. complete POT), NEVER means it's not renderable regardless (e.g.
    185   // incomplete).
    186   enum CanRenderCondition {
    187     CAN_RENDER_ALWAYS,
    188     CAN_RENDER_NEVER,
    189     CAN_RENDER_ONLY_IF_NPOT
    190   };
    191 
    192   struct LevelInfo {
    193     LevelInfo();
    194     LevelInfo(const LevelInfo& rhs);
    195     ~LevelInfo();
    196 
    197     bool cleared;
    198     GLenum target;
    199     GLint level;
    200     GLenum internal_format;
    201     GLsizei width;
    202     GLsizei height;
    203     GLsizei depth;
    204     GLint border;
    205     GLenum format;
    206     GLenum type;
    207     scoped_refptr<gfx::GLImage> image;
    208     uint32 estimated_size;
    209   };
    210 
    211   // Set the info for a particular level.
    212   void SetLevelInfo(
    213       const FeatureInfo* feature_info,
    214       GLenum target,
    215       GLint level,
    216       GLenum internal_format,
    217       GLsizei width,
    218       GLsizei height,
    219       GLsizei depth,
    220       GLint border,
    221       GLenum format,
    222       GLenum type,
    223       bool cleared);
    224 
    225   // In GLES2 "texture complete" means it has all required mips for filtering
    226   // down to a 1x1 pixel texture, they are in the correct order, they are all
    227   // the same format.
    228   bool texture_complete() const {
    229     return texture_complete_;
    230   }
    231 
    232   // In GLES2 "cube complete" means all 6 faces level 0 are defined, all the
    233   // same format, all the same dimensions and all width = height.
    234   bool cube_complete() const {
    235     return cube_complete_;
    236   }
    237 
    238   // Whether or not this texture is a non-power-of-two texture.
    239   bool npot() const {
    240     return npot_;
    241   }
    242 
    243   // Marks a particular level as cleared or uncleared.
    244   void SetLevelCleared(GLenum target, GLint level, bool cleared);
    245 
    246   // Updates the cleared flag for this texture by inspecting all the mips.
    247   void UpdateCleared();
    248 
    249   // Clears any renderable uncleared levels.
    250   // Returns false if a GL error was generated.
    251   bool ClearRenderableLevels(GLES2Decoder* decoder);
    252 
    253   // Clears the level.
    254   // Returns false if a GL error was generated.
    255   bool ClearLevel(GLES2Decoder* decoder, GLenum target, GLint level);
    256 
    257   // Sets a texture parameter.
    258   // TODO(gman): Expand to SetParameteriv,fv
    259   // Returns GL_NO_ERROR on success. Otherwise the error to generate.
    260   GLenum SetParameteri(
    261       const FeatureInfo* feature_info, GLenum pname, GLint param);
    262   GLenum SetParameterf(
    263       const FeatureInfo* feature_info, GLenum pname, GLfloat param);
    264 
    265   // Makes each of the mip levels as though they were generated.
    266   bool MarkMipmapsGenerated(const FeatureInfo* feature_info);
    267 
    268   bool NeedsMips() const {
    269     return min_filter_ != GL_NEAREST && min_filter_ != GL_LINEAR;
    270   }
    271 
    272   // True if this texture meets all the GLES2 criteria for rendering.
    273   // See section 3.8.2 of the GLES2 spec.
    274   bool CanRender(const FeatureInfo* feature_info) const;
    275 
    276   // Returns true if mipmaps can be generated by GL.
    277   bool CanGenerateMipmaps(const FeatureInfo* feature_info) const;
    278 
    279   // Sets the Texture's target
    280   // Parameters:
    281   //   target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP or
    282   //           GL_TEXTURE_EXTERNAL_OES or GL_TEXTURE_RECTANGLE_ARB
    283   //   max_levels: The maximum levels this type of target can have.
    284   void SetTarget(
    285       const FeatureInfo* feature_info, GLenum target, GLint max_levels);
    286 
    287   // Update info about this texture.
    288   void Update(const FeatureInfo* feature_info);
    289 
    290   // Set the image for a particular level.
    291   void SetLevelImage(
    292       const FeatureInfo* feature_info,
    293       GLenum target,
    294       GLint level,
    295       gfx::GLImage* image);
    296 
    297   // Appends a signature for the given level.
    298   void AddToSignature(
    299       const FeatureInfo* feature_info,
    300       GLenum target, GLint level, std::string* signature) const;
    301 
    302   void SetMailboxManager(MailboxManager* mailbox_manager);
    303 
    304   // Updates the unsafe textures count in all the managers referencing this
    305   // texture.
    306   void UpdateSafeToRenderFrom(bool cleared);
    307 
    308   // Updates the uncleared mip count in all the managers referencing this
    309   // texture.
    310   void UpdateMipCleared(LevelInfo* info, bool cleared);
    311 
    312   // Computes the CanRenderCondition flag.
    313   CanRenderCondition GetCanRenderCondition() const;
    314 
    315   // Updates the unrenderable texture count in all the managers referencing this
    316   // texture.
    317   void UpdateCanRenderCondition();
    318 
    319   // Updates the images count in all the managers referencing this
    320   // texture.
    321   void UpdateHasImages();
    322 
    323   // Increment the framebuffer state change count in all the managers
    324   // referencing this texture.
    325   void IncAllFramebufferStateChangeCount();
    326 
    327   MailboxManager* mailbox_manager_;
    328 
    329   // Info about each face and level of texture.
    330   std::vector<std::vector<LevelInfo> > level_infos_;
    331 
    332   // The texture refs that point to this Texture.
    333   typedef std::set<TextureRef*> RefSet;
    334   RefSet refs_;
    335 
    336   // The single TextureRef that accounts for memory for this texture. Must be
    337   // one of refs_.
    338   TextureRef* memory_tracking_ref_;
    339 
    340   // The id of the texure
    341   GLuint service_id_;
    342 
    343   // Whether all renderable mips of this texture have been cleared.
    344   bool cleared_;
    345 
    346   int num_uncleared_mips_;
    347 
    348   // The target. 0 if unset, otherwise GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
    349   GLenum target_;
    350 
    351   // Texture parameters.
    352   GLenum min_filter_;
    353   GLenum mag_filter_;
    354   GLenum wrap_s_;
    355   GLenum wrap_t_;
    356   GLenum usage_;
    357   GLenum pool_;
    358 
    359   // The maximum level that has been set.
    360   GLint max_level_set_;
    361 
    362   // Whether or not this texture is "texture complete"
    363   bool texture_complete_;
    364 
    365   // Whether or not this texture is "cube complete"
    366   bool cube_complete_;
    367 
    368   // Whether or not this texture is non-power-of-two
    369   bool npot_;
    370 
    371   // Whether this texture has ever been bound.
    372   bool has_been_bound_;
    373 
    374   // The number of framebuffers this texture is attached to.
    375   int framebuffer_attachment_count_;
    376 
    377   // Whether the texture is immutable and no further changes to the format
    378   // or dimensions of the texture object can be made.
    379   bool immutable_;
    380 
    381   // Whether or not this texture has images.
    382   bool has_images_;
    383 
    384   // Size in bytes this texture is assumed to take in memory.
    385   uint32 estimated_size_;
    386 
    387   // Cache of the computed CanRenderCondition flag.
    388   CanRenderCondition can_render_condition_;
    389 
    390   // Whether we have initialized TEXTURE_MAX_ANISOTROPY to 1.
    391   bool texture_max_anisotropy_initialized_;
    392 
    393   DISALLOW_COPY_AND_ASSIGN(Texture);
    394 };
    395 
    396 // This class represents a texture in a client context group. It's mostly 1:1
    397 // with a client id, though it can outlive the client id if it's still bound to
    398 // a FBO or another context when destroyed.
    399 // Multiple TextureRef can point to the same texture with cross-context sharing.
    400 class GPU_EXPORT TextureRef : public base::RefCounted<TextureRef> {
    401  public:
    402   TextureRef(TextureManager* manager, GLuint client_id, Texture* texture);
    403   static scoped_refptr<TextureRef> Create(TextureManager* manager,
    404                                           GLuint client_id,
    405                                           GLuint service_id);
    406 
    407   void AddObserver() { num_observers_++; }
    408   void RemoveObserver() { num_observers_--; }
    409 
    410   const Texture* texture() const { return texture_; }
    411   Texture* texture() { return texture_; }
    412   GLuint client_id() const { return client_id_; }
    413   GLuint service_id() const { return texture_->service_id(); }
    414   GLint num_observers() const { return num_observers_; }
    415 
    416  private:
    417   friend class base::RefCounted<TextureRef>;
    418   friend class Texture;
    419   friend class TextureManager;
    420 
    421   ~TextureRef();
    422   const TextureManager* manager() const { return manager_; }
    423   TextureManager* manager() { return manager_; }
    424   void reset_client_id() { client_id_ = 0; }
    425 
    426   TextureManager* manager_;
    427   Texture* texture_;
    428   GLuint client_id_;
    429   GLint num_observers_;
    430 
    431   DISALLOW_COPY_AND_ASSIGN(TextureRef);
    432 };
    433 
    434 // Holds data that is per gles2_cmd_decoder, but is related to to the
    435 // TextureManager.
    436 struct DecoderTextureState {
    437   // total_texture_upload_time automatically initialized to 0 in default
    438   // constructor.
    439   explicit DecoderTextureState(bool texsubimage2d_faster_than_teximage2d)
    440       : tex_image_2d_failed(false),
    441         texture_upload_count(0),
    442         texsubimage2d_faster_than_teximage2d(
    443             texsubimage2d_faster_than_teximage2d) {}
    444 
    445   // This indicates all the following texSubImage2D calls that are part of the
    446   // failed texImage2D call should be ignored.
    447   bool tex_image_2d_failed;
    448 
    449   // Command buffer stats.
    450   int texture_upload_count;
    451   base::TimeDelta total_texture_upload_time;
    452 
    453   bool texsubimage2d_faster_than_teximage2d;
    454 };
    455 
    456 // This class keeps track of the textures and their sizes so we can do NPOT and
    457 // texture complete checking.
    458 //
    459 // NOTE: To support shared resources an instance of this class will need to be
    460 // shared by multiple GLES2Decoders.
    461 class GPU_EXPORT TextureManager {
    462  public:
    463   class GPU_EXPORT DestructionObserver {
    464    public:
    465     DestructionObserver();
    466     virtual ~DestructionObserver();
    467 
    468     // Called in ~TextureManager.
    469     virtual void OnTextureManagerDestroying(TextureManager* manager) = 0;
    470 
    471     // Called via ~TextureRef.
    472     virtual void OnTextureRefDestroying(TextureRef* texture) = 0;
    473 
    474    private:
    475     DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
    476   };
    477 
    478   enum DefaultAndBlackTextures {
    479     kTexture2D,
    480     kCubeMap,
    481     kExternalOES,
    482     kRectangleARB,
    483     kNumDefaultTextures
    484   };
    485 
    486   TextureManager(MemoryTracker* memory_tracker,
    487                  FeatureInfo* feature_info,
    488                  GLsizei max_texture_size,
    489                  GLsizei max_cube_map_texture_size,
    490                  bool use_default_textures);
    491   ~TextureManager();
    492 
    493   void set_framebuffer_manager(FramebufferManager* manager) {
    494     framebuffer_manager_ = manager;
    495   }
    496 
    497   // Init the texture manager.
    498   bool Initialize();
    499 
    500   // Must call before destruction.
    501   void Destroy(bool have_context);
    502 
    503   // Returns the maximum number of levels.
    504   GLint MaxLevelsForTarget(GLenum target) const {
    505     switch (target) {
    506       case GL_TEXTURE_2D:
    507         return  max_levels_;
    508       case GL_TEXTURE_EXTERNAL_OES:
    509         return 1;
    510       default:
    511         return max_cube_map_levels_;
    512     }
    513   }
    514 
    515   // Returns the maximum size.
    516   GLsizei MaxSizeForTarget(GLenum target) const {
    517     switch (target) {
    518       case GL_TEXTURE_2D:
    519       case GL_TEXTURE_EXTERNAL_OES:
    520         return max_texture_size_;
    521       default:
    522         return max_cube_map_texture_size_;
    523     }
    524   }
    525 
    526   // Returns the maxium number of levels a texture of the given size can have.
    527   static GLsizei ComputeMipMapCount(GLenum target,
    528                                     GLsizei width,
    529                                     GLsizei height,
    530                                     GLsizei depth);
    531 
    532   // Checks if a dimensions are valid for a given target.
    533   bool ValidForTarget(
    534       GLenum target, GLint level,
    535       GLsizei width, GLsizei height, GLsizei depth);
    536 
    537   // True if this texture meets all the GLES2 criteria for rendering.
    538   // See section 3.8.2 of the GLES2 spec.
    539   bool CanRender(const TextureRef* ref) const {
    540     return ref->texture()->CanRender(feature_info_.get());
    541   }
    542 
    543   // Returns true if mipmaps can be generated by GL.
    544   bool CanGenerateMipmaps(const TextureRef* ref) const {
    545     return ref->texture()->CanGenerateMipmaps(feature_info_.get());
    546   }
    547 
    548   // Sets the Texture's target
    549   // Parameters:
    550   //   target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
    551   //   max_levels: The maximum levels this type of target can have.
    552   void SetTarget(
    553       TextureRef* ref,
    554       GLenum target);
    555 
    556   // Set the info for a particular level in a TexureInfo.
    557   void SetLevelInfo(
    558       TextureRef* ref,
    559       GLenum target,
    560       GLint level,
    561       GLenum internal_format,
    562       GLsizei width,
    563       GLsizei height,
    564       GLsizei depth,
    565       GLint border,
    566       GLenum format,
    567       GLenum type,
    568       bool cleared);
    569 
    570   // Adapter to call above function.
    571   void SetLevelInfoFromParams(TextureRef* ref,
    572                               const gpu::AsyncTexImage2DParams& params) {
    573     SetLevelInfo(
    574         ref, params.target, params.level, params.internal_format,
    575         params.width, params.height, 1 /* depth */,
    576         params.border, params.format,
    577         params.type, true /* cleared */);
    578   }
    579 
    580   Texture* Produce(TextureRef* ref);
    581 
    582   // Maps an existing texture into the texture manager, at a given client ID.
    583   TextureRef* Consume(GLuint client_id, Texture* texture);
    584 
    585   // Sets a mip as cleared.
    586   void SetLevelCleared(TextureRef* ref, GLenum target,
    587                        GLint level, bool cleared);
    588 
    589   // Sets a texture parameter of a Texture
    590   // Returns GL_NO_ERROR on success. Otherwise the error to generate.
    591   // TODO(gman): Expand to SetParameteriv,fv
    592   void SetParameteri(
    593       const char* function_name, ErrorState* error_state,
    594       TextureRef* ref, GLenum pname, GLint param);
    595   void SetParameterf(
    596       const char* function_name, ErrorState* error_state,
    597       TextureRef* ref, GLenum pname, GLfloat param);
    598 
    599   // Makes each of the mip levels as though they were generated.
    600   // Returns false if that's not allowed for the given texture.
    601   bool MarkMipmapsGenerated(TextureRef* ref);
    602 
    603   // Clears any uncleared renderable levels.
    604   bool ClearRenderableLevels(GLES2Decoder* decoder, TextureRef* ref);
    605 
    606   // Clear a specific level.
    607   bool ClearTextureLevel(
    608       GLES2Decoder* decoder, TextureRef* ref, GLenum target, GLint level);
    609 
    610   // Creates a new texture info.
    611   TextureRef* CreateTexture(GLuint client_id, GLuint service_id);
    612 
    613   // Gets the texture info for the given texture.
    614   TextureRef* GetTexture(GLuint client_id) const;
    615 
    616   // Removes a texture info.
    617   void RemoveTexture(GLuint client_id);
    618 
    619   // Gets a Texture for a given service id (note: it assumes the texture object
    620   // is still mapped in this TextureManager).
    621   Texture* GetTextureForServiceId(GLuint service_id) const;
    622 
    623   TextureRef* GetDefaultTextureInfo(GLenum target) {
    624     switch (target) {
    625       case GL_TEXTURE_2D:
    626         return default_textures_[kTexture2D].get();
    627       case GL_TEXTURE_CUBE_MAP:
    628         return default_textures_[kCubeMap].get();
    629       case GL_TEXTURE_EXTERNAL_OES:
    630         return default_textures_[kExternalOES].get();
    631       case GL_TEXTURE_RECTANGLE_ARB:
    632         return default_textures_[kRectangleARB].get();
    633       default:
    634         NOTREACHED();
    635         return NULL;
    636     }
    637   }
    638 
    639   bool HaveUnrenderableTextures() const {
    640     return num_unrenderable_textures_ > 0;
    641   }
    642 
    643   bool HaveUnsafeTextures() const {
    644     return num_unsafe_textures_ > 0;
    645   }
    646 
    647   bool HaveUnclearedMips() const {
    648     return num_uncleared_mips_ > 0;
    649   }
    650 
    651   bool HaveImages() const {
    652     return num_images_ > 0;
    653   }
    654 
    655   GLuint black_texture_id(GLenum target) const {
    656     switch (target) {
    657       case GL_SAMPLER_2D:
    658         return black_texture_ids_[kTexture2D];
    659       case GL_SAMPLER_CUBE:
    660         return black_texture_ids_[kCubeMap];
    661       case GL_SAMPLER_EXTERNAL_OES:
    662         return black_texture_ids_[kExternalOES];
    663       case GL_SAMPLER_2D_RECT_ARB:
    664         return black_texture_ids_[kRectangleARB];
    665       default:
    666         NOTREACHED();
    667         return 0;
    668     }
    669   }
    670 
    671   size_t mem_represented() const {
    672     return
    673         memory_tracker_managed_->GetMemRepresented() +
    674         memory_tracker_unmanaged_->GetMemRepresented();
    675   }
    676 
    677   void SetLevelImage(
    678       TextureRef* ref,
    679       GLenum target,
    680       GLint level,
    681       gfx::GLImage* image);
    682 
    683   void AddToSignature(
    684       TextureRef* ref,
    685       GLenum target,
    686       GLint level,
    687       std::string* signature) const;
    688 
    689   void AddObserver(DestructionObserver* observer) {
    690     destruction_observers_.push_back(observer);
    691   }
    692 
    693   void RemoveObserver(DestructionObserver* observer) {
    694     for (unsigned int i = 0; i < destruction_observers_.size(); i++) {
    695       if (destruction_observers_[i] == observer) {
    696         std::swap(destruction_observers_[i], destruction_observers_.back());
    697         destruction_observers_.pop_back();
    698         return;
    699       }
    700     }
    701     NOTREACHED();
    702   }
    703 
    704   struct DoTextImage2DArguments {
    705     GLenum target;
    706     GLint level;
    707     GLenum internal_format;
    708     GLsizei width;
    709     GLsizei height;
    710     GLint border;
    711     GLenum format;
    712     GLenum type;
    713     const void* pixels;
    714     uint32 pixels_size;
    715   };
    716 
    717   bool ValidateTexImage2D(
    718     ContextState* state,
    719     const char* function_name,
    720     const DoTextImage2DArguments& args,
    721     // Pointer to TextureRef filled in if validation successful.
    722     // Presumes the pointer is valid.
    723     TextureRef** texture_ref);
    724 
    725   void ValidateAndDoTexImage2D(
    726     DecoderTextureState* texture_state,
    727     ContextState* state,
    728     DecoderFramebufferState* framebuffer_state,
    729     const DoTextImage2DArguments& args);
    730 
    731   // TODO(kloveless): Make GetTexture* private once this is no longer called
    732   // from gles2_cmd_decoder.
    733   TextureRef* GetTextureInfoForTarget(ContextState* state, GLenum target);
    734   TextureRef* GetTextureInfoForTargetUnlessDefault(
    735       ContextState* state, GLenum target);
    736 
    737   bool ValidateFormatAndTypeCombination(
    738     ErrorState* error_state, const char* function_name,
    739     GLenum format, GLenum type);
    740 
    741   // Note that internal_format is only checked in relation to the format
    742   // parameter, so that this function may be used to validate texSubImage2D.
    743   bool ValidateTextureParameters(
    744     ErrorState* error_state, const char* function_name,
    745     GLenum format, GLenum type, GLenum internal_format, GLint level);
    746 
    747  private:
    748   friend class Texture;
    749   friend class TextureRef;
    750 
    751   // Helper for Initialize().
    752   scoped_refptr<TextureRef> CreateDefaultAndBlackTextures(
    753       GLenum target,
    754       GLuint* black_texture);
    755 
    756   void DoTexImage2D(
    757     DecoderTextureState* texture_state,
    758     ErrorState* error_state,
    759     DecoderFramebufferState* framebuffer_state,
    760     TextureRef* texture_ref,
    761     const DoTextImage2DArguments& args);
    762 
    763   void StartTracking(TextureRef* texture);
    764   void StopTracking(TextureRef* texture);
    765 
    766   void UpdateSafeToRenderFrom(int delta);
    767   void UpdateUnclearedMips(int delta);
    768   void UpdateCanRenderCondition(Texture::CanRenderCondition old_condition,
    769                                 Texture::CanRenderCondition new_condition);
    770   void UpdateNumImages(int delta);
    771   void IncFramebufferStateChangeCount();
    772 
    773   MemoryTypeTracker* GetMemTracker(GLenum texture_pool);
    774   scoped_ptr<MemoryTypeTracker> memory_tracker_managed_;
    775   scoped_ptr<MemoryTypeTracker> memory_tracker_unmanaged_;
    776 
    777   scoped_refptr<FeatureInfo> feature_info_;
    778 
    779   FramebufferManager* framebuffer_manager_;
    780 
    781   // Info for each texture in the system.
    782   typedef base::hash_map<GLuint, scoped_refptr<TextureRef> > TextureMap;
    783   TextureMap textures_;
    784 
    785   GLsizei max_texture_size_;
    786   GLsizei max_cube_map_texture_size_;
    787   GLint max_levels_;
    788   GLint max_cube_map_levels_;
    789 
    790   const bool use_default_textures_;
    791 
    792   int num_unrenderable_textures_;
    793   int num_unsafe_textures_;
    794   int num_uncleared_mips_;
    795   int num_images_;
    796 
    797   // Counts the number of Textures allocated with 'this' as its manager.
    798   // Allows to check no Texture will outlive this.
    799   unsigned int texture_count_;
    800 
    801   bool have_context_;
    802 
    803   // Black (0,0,0,1) textures for when non-renderable textures are used.
    804   // NOTE: There is no corresponding Texture for these textures.
    805   // TextureInfos are only for textures the client side can access.
    806   GLuint black_texture_ids_[kNumDefaultTextures];
    807 
    808   // The default textures for each target (texture name = 0)
    809   scoped_refptr<TextureRef> default_textures_[kNumDefaultTextures];
    810 
    811   std::vector<DestructionObserver*> destruction_observers_;
    812 
    813   DISALLOW_COPY_AND_ASSIGN(TextureManager);
    814 };
    815 
    816 // This class records texture upload time when in scope.
    817 class ScopedTextureUploadTimer {
    818  public:
    819   explicit ScopedTextureUploadTimer(DecoderTextureState* texture_state);
    820   ~ScopedTextureUploadTimer();
    821 
    822  private:
    823   DecoderTextureState* texture_state_;
    824   base::TimeTicks begin_time_;
    825   DISALLOW_COPY_AND_ASSIGN(ScopedTextureUploadTimer);
    826 };
    827 
    828 }  // namespace gles2
    829 }  // namespace gpu
    830 
    831 #endif  // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
    832