Home | History | Annotate | Download | only in resources
      1 // Copyright 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 CC_RESOURCES_PRIORITIZED_RESOURCE_H_
      6 #define CC_RESOURCES_PRIORITIZED_RESOURCE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/logging.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "cc/base/cc_export.h"
     12 #include "cc/resources/priority_calculator.h"
     13 #include "cc/resources/resource.h"
     14 #include "cc/resources/resource_provider.h"
     15 #include "third_party/khronos/GLES2/gl2.h"
     16 #include "ui/gfx/rect.h"
     17 #include "ui/gfx/size.h"
     18 #include "ui/gfx/vector2d.h"
     19 
     20 namespace cc {
     21 
     22 class PrioritizedResourceManager;
     23 class Proxy;
     24 
     25 class CC_EXPORT PrioritizedResource {
     26  public:
     27   static scoped_ptr<PrioritizedResource>
     28   Create(PrioritizedResourceManager* manager, gfx::Size size, GLenum format) {
     29     return make_scoped_ptr(new PrioritizedResource(manager, size, format));
     30   }
     31   static scoped_ptr<PrioritizedResource> Create(
     32       PrioritizedResourceManager* manager) {
     33     return make_scoped_ptr(new PrioritizedResource(manager, gfx::Size(), 0));
     34   }
     35   ~PrioritizedResource();
     36 
     37   // Texture properties. Changing these causes the backing texture to be lost.
     38   // Setting these to the same value is a no-op.
     39   void SetTextureManager(PrioritizedResourceManager* manager);
     40   PrioritizedResourceManager* resource_manager() { return manager_; }
     41   void SetDimensions(gfx::Size size, GLenum format);
     42   GLenum format() const { return format_; }
     43   gfx::Size size() const { return size_; }
     44   size_t bytes() const { return bytes_; }
     45   bool contents_swizzled() const { return contents_swizzled_; }
     46 
     47   // Set priority for the requested texture.
     48   void set_request_priority(int priority) { priority_ = priority; }
     49   int request_priority() const { return priority_; }
     50 
     51   // After PrioritizedResource::PrioritizeTextures() is called, this returns
     52   // if the the request succeeded and this texture can be acquired for use.
     53   bool can_acquire_backing_texture() const { return is_above_priority_cutoff_; }
     54 
     55   // This returns whether we still have a backing texture. This can continue
     56   // to be true even after CanAcquireBackingTexture() becomes false. In this
     57   // case the texture can be used but shouldn't be updated since it will get
     58   // taken away "soon".
     59   bool have_backing_texture() const { return !!backing(); }
     60 
     61   bool BackingResourceWasEvicted() const;
     62 
     63   // If CanAcquireBackingTexture() is true AcquireBackingTexture() will acquire
     64   // a backing texture for use. Call this whenever the texture is actually
     65   // needed.
     66   void AcquireBackingTexture(ResourceProvider* resource_provider);
     67 
     68   // TODO(epenner): Request late is really a hack for when we are totally out of
     69   // memory (all textures are visible) but we can still squeeze into the limit
     70   // by not painting occluded textures. In this case the manager refuses all
     71   // visible textures and RequestLate() will enable CanAcquireBackingTexture()
     72   // on a call-order basis. We might want to just remove this in the future
     73   // (carefully) and just make sure we don't regress OOMs situations.
     74   bool RequestLate();
     75 
     76   // Update pixels of backing resource from image. This functions will aquire
     77   // the backing if needed.
     78   void SetPixels(ResourceProvider* resource_provider,
     79                  const uint8_t* image,
     80                  gfx::Rect image_rect,
     81                  gfx::Rect source_rect,
     82                  gfx::Vector2d dest_offset);
     83 
     84   ResourceProvider::ResourceId resource_id() const {
     85     return backing_ ? backing_->id() : 0;
     86   }
     87 
     88   // Self-managed textures are accounted for when prioritizing other textures,
     89   // but they are not allocated/recycled/deleted, so this needs to be done
     90   // externally. CanAcquireBackingTexture() indicates if the texture would have
     91   // been allowed given its priority.
     92   void set_is_self_managed(bool is_self_managed) {
     93     is_self_managed_ = is_self_managed;
     94   }
     95   bool is_self_managed() { return is_self_managed_; }
     96   void SetToSelfManagedMemoryPlaceholder(size_t bytes);
     97 
     98   void ReturnBackingTexture();
     99 
    100  private:
    101   friend class PrioritizedResourceManager;
    102   friend class PrioritizedResourceTest;
    103 
    104   class Backing : public Resource {
    105    public:
    106     Backing(unsigned id,
    107             ResourceProvider* resource_provider,
    108             gfx::Size size,
    109             GLenum format);
    110     ~Backing();
    111     void UpdatePriority();
    112     void UpdateInDrawingImplTree();
    113 
    114     PrioritizedResource* owner() { return owner_; }
    115     bool CanBeRecycled() const;
    116     int request_priority_at_last_priority_update() const {
    117       return priority_at_last_priority_update_;
    118     }
    119     bool was_above_priority_cutoff_at_last_priority_update() const {
    120       return was_above_priority_cutoff_at_last_priority_update_;
    121     }
    122     bool in_drawing_impl_tree() const { return in_drawing_impl_tree_; }
    123 
    124     void DeleteResource(ResourceProvider* resource_provider);
    125     bool ResourceHasBeenDeleted() const;
    126 
    127    private:
    128     const Proxy* proxy() const;
    129 
    130     friend class PrioritizedResource;
    131     friend class PrioritizedResourceManager;
    132     PrioritizedResource* owner_;
    133     int priority_at_last_priority_update_;
    134     bool was_above_priority_cutoff_at_last_priority_update_;
    135 
    136     // Set if this is currently-drawing impl tree.
    137     bool in_drawing_impl_tree_;
    138 
    139     bool resource_has_been_deleted_;
    140 
    141 #ifndef NDEBUG
    142     ResourceProvider* resource_provider_;
    143 #endif
    144     DISALLOW_COPY_AND_ASSIGN(Backing);
    145   };
    146 
    147   PrioritizedResource(PrioritizedResourceManager* resource_manager,
    148                       gfx::Size size,
    149                       GLenum format);
    150 
    151   bool is_above_priority_cutoff() { return is_above_priority_cutoff_; }
    152   void set_above_priority_cutoff(bool is_above_priority_cutoff) {
    153     is_above_priority_cutoff_ = is_above_priority_cutoff;
    154   }
    155   void set_manager_internal(PrioritizedResourceManager* manager) {
    156     manager_ = manager;
    157   }
    158 
    159   Backing* backing() const { return backing_; }
    160   void Link(Backing* backing);
    161   void Unlink();
    162 
    163   gfx::Size size_;
    164   GLenum format_;
    165   size_t bytes_;
    166   bool contents_swizzled_;
    167 
    168   int priority_;
    169   bool is_above_priority_cutoff_;
    170   bool is_self_managed_;
    171 
    172   Backing* backing_;
    173   PrioritizedResourceManager* manager_;
    174 
    175   DISALLOW_COPY_AND_ASSIGN(PrioritizedResource);
    176 };
    177 
    178 }  // namespace cc
    179 
    180 #endif  // CC_RESOURCES_PRIORITIZED_RESOURCE_H_
    181