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_RESOURCE_POOL_H_
      6 #define CC_RESOURCES_RESOURCE_POOL_H_
      7 
      8 #include <list>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "cc/base/cc_export.h"
     12 #include "cc/output/renderer.h"
     13 #include "cc/resources/resource.h"
     14 #include "cc/resources/resource_format.h"
     15 
     16 namespace cc {
     17 class ScopedResource;
     18 
     19 class CC_EXPORT ResourcePool {
     20  public:
     21   static scoped_ptr<ResourcePool> Create(ResourceProvider* resource_provider,
     22                                          GLenum target,
     23                                          ResourceFormat format) {
     24     return make_scoped_ptr(new ResourcePool(resource_provider, target, format));
     25   }
     26 
     27   virtual ~ResourcePool();
     28 
     29   scoped_ptr<ScopedResource> AcquireResource(const gfx::Size& size);
     30   void ReleaseResource(scoped_ptr<ScopedResource>);
     31 
     32   void SetResourceUsageLimits(size_t max_memory_usage_bytes,
     33                               size_t max_unused_memory_usage_bytes,
     34                               size_t max_resource_count);
     35 
     36   void ReduceResourceUsage();
     37   void CheckBusyResources();
     38 
     39   size_t total_memory_usage_bytes() const { return memory_usage_bytes_; }
     40   size_t acquired_memory_usage_bytes() const {
     41     return memory_usage_bytes_ - unused_memory_usage_bytes_;
     42   }
     43   size_t total_resource_count() const { return resource_count_; }
     44   size_t acquired_resource_count() const {
     45     return resource_count_ - unused_resources_.size();
     46   }
     47 
     48   ResourceFormat resource_format() const { return format_; }
     49 
     50  protected:
     51   ResourcePool(ResourceProvider* resource_provider,
     52                GLenum target,
     53                ResourceFormat format);
     54 
     55   bool ResourceUsageTooHigh();
     56 
     57  private:
     58   void DidFinishUsingResource(ScopedResource* resource);
     59 
     60   ResourceProvider* resource_provider_;
     61   const GLenum target_;
     62   const ResourceFormat format_;
     63   size_t max_memory_usage_bytes_;
     64   size_t max_unused_memory_usage_bytes_;
     65   size_t max_resource_count_;
     66   size_t memory_usage_bytes_;
     67   size_t unused_memory_usage_bytes_;
     68   size_t resource_count_;
     69 
     70   typedef std::list<ScopedResource*> ResourceList;
     71   ResourceList unused_resources_;
     72   ResourceList busy_resources_;
     73 
     74   DISALLOW_COPY_AND_ASSIGN(ResourcePool);
     75 };
     76 
     77 }  // namespace cc
     78 
     79 #endif  // CC_RESOURCES_RESOURCE_POOL_H_
     80