summaryrefslogtreecommitdiffstats
path: root/cc/resources/resource_pool.h
blob: d05534d40f04c08d3faf1b08603ce2e1d994539c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_RESOURCES_RESOURCE_POOL_H_
#define CC_RESOURCES_RESOURCE_POOL_H_

#include <list>

#include "base/memory/scoped_ptr.h"
#include "cc/base/cc_export.h"
#include "cc/output/renderer.h"
#include "cc/resources/resource.h"
#include "cc/resources/resource_format.h"

namespace cc {
class ScopedResource;

class CC_EXPORT ResourcePool {
 public:
  static scoped_ptr<ResourcePool> Create(ResourceProvider* resource_provider,
                                         GLenum target,
                                         ResourceFormat format) {
    return make_scoped_ptr(new ResourcePool(resource_provider, target, format));
  }

  virtual ~ResourcePool();

  scoped_ptr<ScopedResource> AcquireResource(gfx::Size size);
  void ReleaseResource(scoped_ptr<ScopedResource>);

  void SetResourceUsageLimits(size_t max_memory_usage_bytes,
                              size_t max_unused_memory_usage_bytes,
                              size_t max_resource_count);

  void ReduceResourceUsage();
  void CheckBusyResources();

  size_t total_memory_usage_bytes() const { return memory_usage_bytes_; }
  size_t acquired_memory_usage_bytes() const {
    return memory_usage_bytes_ - unused_memory_usage_bytes_;
  }
  size_t acquired_resource_count() const {
    return resource_count_ - unused_resources_.size();
  }

 protected:
  ResourcePool(ResourceProvider* resource_provider,
               GLenum target,
               ResourceFormat format);

  bool ResourceUsageTooHigh();

 private:
  void DidFinishUsingResource(ScopedResource* resource);

  ResourceProvider* resource_provider_;
  const GLenum target_;
  const ResourceFormat format_;
  size_t max_memory_usage_bytes_;
  size_t max_unused_memory_usage_bytes_;
  size_t max_resource_count_;
  size_t memory_usage_bytes_;
  size_t unused_memory_usage_bytes_;
  size_t resource_count_;

  typedef std::list<ScopedResource*> ResourceList;
  ResourceList unused_resources_;
  ResourceList busy_resources_;

  DISALLOW_COPY_AND_ASSIGN(ResourcePool);
};

}  // namespace cc

#endif  // CC_RESOURCES_RESOURCE_POOL_H_