cc: Use worker context for one-copy tile initialization.
[chromium-blink-merge.git] / cc / raster / one_copy_tile_task_worker_pool.h
blob6554fccb256945ee2573f495407b856a8b20a449
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 unsigned image_target,
53 int max_staging_buffers);
55 // Overridden from TileTaskWorkerPool:
56 TileTaskRunner* AsTileTaskRunner() override;
58 // Overridden from TileTaskRunner:
59 void SetClient(TileTaskRunnerClient* client) override;
60 void Shutdown() override;
61 void ScheduleTasks(TileTaskQueue* queue) override;
62 void CheckForCompletedTasks() override;
63 ResourceFormat GetResourceFormat() const override;
64 bool GetResourceRequiresSwizzle() const override;
66 // Overridden from TileTaskClient:
67 scoped_ptr<RasterBuffer> AcquireBufferForRaster(
68 const Resource* resource,
69 uint64_t resource_content_id,
70 uint64_t previous_content_id) override;
71 void ReleaseBufferForRaster(scoped_ptr<RasterBuffer> buffer) override;
73 // Overridden from base::trace_event::MemoryDumpProvider:
74 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
75 base::trace_event::ProcessMemoryDump* pmd) override;
77 // Playback raster source and copy result into |resource|.
78 void PlaybackAndCopyOnWorkerThread(
79 const Resource* resource,
80 const ResourceProvider::ScopedWriteLockGL* resource_lock,
81 const RasterSource* raster_source,
82 const gfx::Rect& raster_full_rect,
83 const gfx::Rect& raster_dirty_rect,
84 float scale,
85 bool include_images,
86 uint64_t resource_content_id,
87 uint64_t previous_content_id);
89 protected:
90 OneCopyTileTaskWorkerPool(base::SequencedTaskRunner* task_runner,
91 TaskGraphRunner* task_graph_runner,
92 ResourceProvider* resource_provider,
93 int max_copy_texture_chromium_size,
94 bool use_persistent_gpu_memory_buffers,
95 unsigned image_target,
96 int max_staging_buffers);
98 private:
99 struct StagingBuffer {
100 explicit StagingBuffer(const gfx::Size& size);
101 ~StagingBuffer();
103 void DestroyGLResources(gpu::gles2::GLES2Interface* gl);
104 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
105 ResourceFormat format,
106 bool is_free) const;
108 const gfx::Size size;
109 scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer;
110 base::TimeTicks last_usage;
111 unsigned texture_id;
112 unsigned image_id;
113 unsigned query_id;
114 uint64_t content_id;
117 scoped_ptr<StagingBuffer> AcquireStagingBuffer(const Resource* resource,
118 uint64_t previous_content_id);
119 base::TimeTicks GetUsageTimeForLRUBuffer();
120 void ScheduleReduceMemoryUsage();
121 void ReduceMemoryUsage();
122 void ReleaseBuffersNotUsedSince(base::TimeTicks time);
124 void OnTaskSetFinished(TaskSet task_set);
125 scoped_refptr<base::trace_event::ConvertableToTraceFormat> StateAsValue()
126 const;
127 void StagingStateAsValueInto(
128 base::trace_event::TracedValue* staging_state) const;
130 scoped_refptr<base::SequencedTaskRunner> task_runner_;
131 TaskGraphRunner* task_graph_runner_;
132 const NamespaceToken namespace_token_;
133 TileTaskRunnerClient* client_;
134 ResourceProvider* const resource_provider_;
135 const int max_bytes_per_copy_operation_;
136 const bool use_persistent_gpu_memory_buffers_;
137 const unsigned image_target_;
138 TaskSetCollection tasks_pending_;
139 scoped_refptr<TileTask> task_set_finished_tasks_[kNumberOfTaskSets];
141 // Task graph used when scheduling tasks and vector used to gather
142 // completed tasks.
143 TaskGraph graph_;
144 Task::Vector completed_tasks_;
146 mutable base::Lock lock_;
147 // |lock_| must be acquired when accessing the following members.
148 using StagingBufferSet = std::set<StagingBuffer*>;
149 StagingBufferSet buffers_;
150 using StagingBufferDeque = ScopedPtrDeque<StagingBuffer>;
151 StagingBufferDeque free_buffers_;
152 StagingBufferDeque busy_buffers_;
153 int bytes_scheduled_since_last_flush_;
154 size_t max_staging_buffers_;
155 const base::TimeDelta staging_buffer_expiration_delay_;
156 bool reduce_memory_usage_pending_;
157 base::Closure reduce_memory_usage_callback_;
159 base::WeakPtrFactory<OneCopyTileTaskWorkerPool> weak_ptr_factory_;
160 // "raster finished" tasks need their own factory as they need to be
161 // canceled when ScheduleTasks() is called.
162 base::WeakPtrFactory<OneCopyTileTaskWorkerPool>
163 task_set_finished_weak_ptr_factory_;
165 DISALLOW_COPY_AND_ASSIGN(OneCopyTileTaskWorkerPool);
168 } // namespace cc
170 #endif // CC_RASTER_ONE_COPY_TILE_TASK_WORKER_POOL_H_