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_UPDATE_CONTROLLER_H_
6#define CC_RESOURCES_RESOURCE_UPDATE_CONTROLLER_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "base/time/time.h"
12#include "cc/base/cc_export.h"
13#include "cc/resources/resource_update_queue.h"
14
15namespace base { class SingleThreadTaskRunner; }
16
17namespace cc {
18
19class ResourceProvider;
20
21class ResourceUpdateControllerClient {
22 public:
23  virtual void ReadyToFinalizeTextureUpdates() = 0;
24
25 protected:
26  virtual ~ResourceUpdateControllerClient() {}
27};
28
29class CC_EXPORT ResourceUpdateController {
30 public:
31  static scoped_ptr<ResourceUpdateController> Create(
32      ResourceUpdateControllerClient* client,
33      base::SingleThreadTaskRunner* task_runner,
34      scoped_ptr<ResourceUpdateQueue> queue,
35      ResourceProvider* resource_provider) {
36    return make_scoped_ptr(new ResourceUpdateController(
37        client, task_runner, queue.Pass(), resource_provider));
38  }
39  static size_t MaxPartialTextureUpdates();
40
41  virtual ~ResourceUpdateController();
42
43  // Discard uploads to textures that were evicted on the impl thread.
44  void DiscardUploadsToEvictedResources();
45
46  void PerformMoreUpdates(base::TimeTicks time_limit);
47  void Finalize();
48
49
50  // Virtual for testing.
51  virtual size_t UpdateMoreTexturesSize() const;
52  virtual base::TimeTicks UpdateMoreTexturesCompletionTime();
53
54 protected:
55  ResourceUpdateController(ResourceUpdateControllerClient* client,
56                           base::SingleThreadTaskRunner* task_runner,
57                           scoped_ptr<ResourceUpdateQueue> queue,
58                           ResourceProvider* resource_provider);
59
60 private:
61  static size_t MaxFullUpdatesPerTick(ResourceProvider* resource_provider);
62
63  size_t MaxBlockingUpdates() const;
64
65  void UpdateTexture(ResourceUpdate update);
66
67  // This returns true when there were textures left to update.
68  bool UpdateMoreTexturesIfEnoughTimeRemaining();
69  void UpdateMoreTexturesNow();
70  void OnTimerFired();
71
72  ResourceUpdateControllerClient* client_;
73  scoped_ptr<ResourceUpdateQueue> queue_;
74  bool contents_textures_purged_;
75  ResourceProvider* resource_provider_;
76  base::TimeTicks time_limit_;
77  size_t texture_updates_per_tick_;
78  bool first_update_attempt_;
79  base::SingleThreadTaskRunner* task_runner_;
80  bool task_posted_;
81  bool ready_to_finalize_;
82  base::WeakPtrFactory<ResourceUpdateController> weak_factory_;
83
84  DISALLOW_COPY_AND_ASSIGN(ResourceUpdateController);
85};
86
87}  // namespace cc
88
89#endif  // CC_RESOURCES_RESOURCE_UPDATE_CONTROLLER_H_
90