Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / raster / one_copy_tile_task_worker_pool.h
blob783d103c850ac1f44c008591ee8a7bbc44b50c65
1 // Copyright 2014 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.
5 #ifndef CC_RASTER_ONE_COPY_TILE_TASK_WORKER_POOL_H_
6 #define CC_RASTER_ONE_COPY_TILE_TASK_WORKER_POOL_H_
8 #include <set>
10 #include "base/memory/weak_ptr.h"
11 #include "base/synchronization/lock.h"
12 #include "base/time/time.h"
13 #include "base/trace_event/memory_dump_provider.h"
14 #include "base/values.h"
15 #include "cc/base/scoped_ptr_deque.h"
16 #include "cc/output/context_provider.h"
17 #include "cc/raster/tile_task_runner.h"
18 #include "cc/raster/tile_task_worker_pool.h"
19 #include "cc/resources/resource_provider.h"
21 namespace base {
22 namespace trace_event {
23 class ConvertableToTraceFormat;
24 class TracedValue;
28 namespace gpu {
29 namespace gles2 {
30 class GLES2Interface;
34 namespace cc {
35 class ResourcePool;
37 class CC_EXPORT OneCopyTileTaskWorkerPool
38 : public TileTaskWorkerPool,
39 public TileTaskRunner,
40 public TileTaskClient,
41 public base::trace_event::MemoryDumpProvider {
42 public:
43 ~OneCopyTileTaskWorkerPool() override;
45 static scoped_ptr<TileTaskWorkerPool> Create(
46 base::SequencedTaskRunner* task_runner,
47 TaskGraphRunner* task_graph_runner,
48 ContextProvider* context_provider,
49 ResourceProvider* resource_provider,
50 int max_copy_texture_chromium_size,
51 bool use_persistent_gpu_memory_buffers,
52 int max_staging_buffer_usage_in_bytes);
54 // Overridden from TileTaskWorkerPool:
55 TileTaskRunner* AsTileTaskRunner() override;
57 // Overridden from TileTaskRunner:
58 void SetClient(TileTaskRunnerClient* client) override;
59 void Shutdown() override;
60 void ScheduleTasks(TileTaskQueue* queue) override;
61 void CheckForCompletedTasks() override;
62 ResourceFormat GetResourceFormat() const override;
63 bool GetResourceRequiresSwizzle() const override;
65 // Overridden from TileTaskClient:
66 scoped_ptr<RasterBuffer> AcquireBufferForRaster(
67 const Resource* resource,
68 uint64_t resource_content_id,
69 uint64_t previous_content_id) override;
70 void ReleaseBufferForRaster(scoped_ptr<RasterBuffer> buffer) override;
72 // Overridden from base::trace_event::MemoryDumpProvider:
73 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
74 base::trace_event::ProcessMemoryDump* pmd) override;
76 // Playback raster source and copy result into |resource|.
77 void PlaybackAndCopyOnWorkerThread(
78 const Resource* resource,
79 const ResourceProvider::ScopedWriteLockGL* resource_lock,
80 const RasterSource* raster_source,
81 const gfx::Rect& raster_full_rect,
82 const gfx::Rect& raster_dirty_rect,
83 float scale,
84 bool include_images,
85 uint64_t resource_content_id,
86 uint64_t previous_content_id);
88 protected:
89 OneCopyTileTaskWorkerPool(base::SequencedTaskRunner* task_runner,
90 TaskGraphRunner* task_graph_runner,
91 ResourceProvider* resource_provider,
92 int max_copy_texture_chromium_size,
93 bool use_persistent_gpu_memory_buffers,
94 int max_staging_buffer_usage_in_bytes);
96 private:
97 struct StagingBuffer {
98 explicit StagingBuffer(const gfx::Size& size);
99 ~StagingBuffer();
101 void DestroyGLResources(gpu::gles2::GLES2Interface* gl);
102 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
103 ResourceFormat format,
104 bool is_free) const;
106 const gfx::Size size;
107 scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer;
108 base::TimeTicks last_usage;
109 unsigned texture_id;
110 unsigned image_id;
111 unsigned query_id;
112 uint64_t content_id;
115 void AddStagingBuffer(const StagingBuffer* staging_buffer);
116 void RemoveStagingBuffer(const StagingBuffer* staging_buffer);
117 void MarkStagingBufferAsFree(const StagingBuffer* staging_buffer);
118 void MarkStagingBufferAsBusy(const StagingBuffer* staging_buffer);
119 scoped_ptr<StagingBuffer> AcquireStagingBuffer(const Resource* resource,
120 uint64_t previous_content_id);
121 base::TimeTicks GetUsageTimeForLRUBuffer();
122 void ScheduleReduceMemoryUsage();
123 void ReduceMemoryUsage();
124 void ReleaseBuffersNotUsedSince(base::TimeTicks time);
126 void OnTaskSetFinished(TaskSet task_set);
127 scoped_refptr<base::trace_event::ConvertableToTraceFormat> StateAsValue()
128 const;
129 void StagingStateAsValueInto(
130 base::trace_event::TracedValue* staging_state) const;
132 scoped_refptr<base::SequencedTaskRunner> task_runner_;
133 TaskGraphRunner* task_graph_runner_;
134 const NamespaceToken namespace_token_;
135 TileTaskRunnerClient* client_;
136 ResourceProvider* const resource_provider_;
137 const int max_bytes_per_copy_operation_;
138 const bool use_persistent_gpu_memory_buffers_;
139 TaskSetCollection tasks_pending_;
140 scoped_refptr<TileTask> task_set_finished_tasks_[kNumberOfTaskSets];
142 // Task graph used when scheduling tasks and vector used to gather
143 // completed tasks.
144 TaskGraph graph_;
145 Task::Vector completed_tasks_;
147 mutable base::Lock lock_;
148 // |lock_| must be acquired when accessing the following members.
149 using StagingBufferSet = std::set<const StagingBuffer*>;
150 StagingBufferSet buffers_;
151 using StagingBufferDeque = ScopedPtrDeque<StagingBuffer>;
152 StagingBufferDeque free_buffers_;
153 StagingBufferDeque busy_buffers_;
154 int bytes_scheduled_since_last_flush_;
155 const int max_staging_buffer_usage_in_bytes_;
156 int staging_buffer_usage_in_bytes_;
157 int free_staging_buffer_usage_in_bytes_;
158 const base::TimeDelta staging_buffer_expiration_delay_;
159 bool reduce_memory_usage_pending_;
160 base::Closure reduce_memory_usage_callback_;
162 base::WeakPtrFactory<OneCopyTileTaskWorkerPool> weak_ptr_factory_;
163 // "raster finished" tasks need their own factory as they need to be
164 // canceled when ScheduleTasks() is called.
165 base::WeakPtrFactory<OneCopyTileTaskWorkerPool>
166 task_set_finished_weak_ptr_factory_;
168 DISALLOW_COPY_AND_ASSIGN(OneCopyTileTaskWorkerPool);
171 } // namespace cc
173 #endif // CC_RASTER_ONE_COPY_TILE_TASK_WORKER_POOL_H_