GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / resources / one_copy_tile_task_worker_pool.h
blob51996677623909aa88ee1aeb1b93f2d0559f5ba8
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_RESOURCES_ONE_COPY_TILE_TASK_WORKER_POOL_H_
6 #define CC_RESOURCES_ONE_COPY_TILE_TASK_WORKER_POOL_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/synchronization/lock.h"
10 #include "base/values.h"
11 #include "cc/base/scoped_ptr_deque.h"
12 #include "cc/output/context_provider.h"
13 #include "cc/resources/resource_provider.h"
14 #include "cc/resources/tile_task_runner.h"
15 #include "cc/resources/tile_task_worker_pool.h"
17 namespace base {
18 namespace trace_event {
19 class ConvertableToTraceFormat;
20 class TracedValue;
24 namespace cc {
25 class ResourcePool;
26 class ScopedResource;
28 typedef int64 CopySequenceNumber;
30 class CC_EXPORT OneCopyTileTaskWorkerPool : public TileTaskWorkerPool,
31 public TileTaskRunner,
32 public TileTaskClient {
33 public:
34 ~OneCopyTileTaskWorkerPool() override;
36 static scoped_ptr<TileTaskWorkerPool> Create(
37 base::SequencedTaskRunner* task_runner,
38 TaskGraphRunner* task_graph_runner,
39 ContextProvider* context_provider,
40 ResourceProvider* resource_provider,
41 ResourcePool* resource_pool);
43 // Overridden from TileTaskWorkerPool:
44 TileTaskRunner* AsTileTaskRunner() override;
46 // Overridden from TileTaskRunner:
47 void SetClient(TileTaskRunnerClient* client) override;
48 void Shutdown() override;
49 void ScheduleTasks(TileTaskQueue* queue) override;
50 void CheckForCompletedTasks() override;
51 ResourceFormat GetResourceFormat() override;
53 // Overridden from TileTaskClient:
54 scoped_ptr<RasterBuffer> AcquireBufferForRaster(
55 const Resource* resource) override;
56 void ReleaseBufferForRaster(scoped_ptr<RasterBuffer> buffer) override;
58 // Playback raster source and schedule copy of |src| resource to |dst|
59 // resource. Returns a non-zero sequence number for this copy operation.
60 CopySequenceNumber PlaybackAndScheduleCopyOnWorkerThread(
61 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock,
62 scoped_ptr<ScopedResource> src,
63 const Resource* dst,
64 const RasterSource* raster_source,
65 const gfx::Rect& rect,
66 float scale);
68 // Issues copy operations until |sequence| has been processed. This will
69 // return immediately if |sequence| has already been processed.
70 void AdvanceLastIssuedCopyTo(CopySequenceNumber sequence);
72 protected:
73 OneCopyTileTaskWorkerPool(base::SequencedTaskRunner* task_runner,
74 TaskGraphRunner* task_graph_runner,
75 ContextProvider* context_provider,
76 ResourceProvider* resource_provider,
77 ResourcePool* resource_pool);
79 private:
80 struct CopyOperation {
81 typedef ScopedPtrDeque<CopyOperation> Deque;
83 CopyOperation(
84 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock,
85 scoped_ptr<ScopedResource> src,
86 const Resource* dst);
87 ~CopyOperation();
89 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> write_lock;
90 scoped_ptr<ScopedResource> src;
91 const Resource* dst;
94 void OnTaskSetFinished(TaskSet task_set);
95 void AdvanceLastFlushedCopyTo(CopySequenceNumber sequence);
96 void IssueCopyOperations(int64 count);
97 void ScheduleCheckForCompletedCopyOperationsWithLockAcquired(
98 bool wait_if_needed);
99 void CheckForCompletedCopyOperations(bool wait_if_needed);
100 scoped_refptr<base::trace_event::ConvertableToTraceFormat> StateAsValue()
101 const;
102 void StagingStateAsValueInto(
103 base::trace_event::TracedValue* staging_state) const;
105 scoped_refptr<base::SequencedTaskRunner> task_runner_;
106 TaskGraphRunner* task_graph_runner_;
107 const NamespaceToken namespace_token_;
108 TileTaskRunnerClient* client_;
109 ContextProvider* context_provider_;
110 ResourceProvider* resource_provider_;
111 ResourcePool* resource_pool_;
112 TaskSetCollection tasks_pending_;
113 scoped_refptr<TileTask> task_set_finished_tasks_[kNumberOfTaskSets];
114 CopySequenceNumber last_issued_copy_operation_;
115 CopySequenceNumber last_flushed_copy_operation_;
117 // Task graph used when scheduling tasks and vector used to gather
118 // completed tasks.
119 TaskGraph graph_;
120 Task::Vector completed_tasks_;
122 base::Lock lock_;
123 // |lock_| must be acquired when accessing the following members.
124 base::ConditionVariable copy_operation_count_cv_;
125 size_t scheduled_copy_operation_count_;
126 size_t issued_copy_operation_count_;
127 CopyOperation::Deque pending_copy_operations_;
128 CopySequenceNumber next_copy_operation_sequence_;
129 bool check_for_completed_copy_operations_pending_;
130 base::TimeTicks last_check_for_completed_copy_operations_time_;
131 bool shutdown_;
133 base::WeakPtrFactory<OneCopyTileTaskWorkerPool> weak_ptr_factory_;
134 // "raster finished" tasks need their own factory as they need to be
135 // canceled when ScheduleTasks() is called.
136 base::WeakPtrFactory<OneCopyTileTaskWorkerPool>
137 task_set_finished_weak_ptr_factory_;
139 DISALLOW_COPY_AND_ASSIGN(OneCopyTileTaskWorkerPool);
142 } // namespace cc
144 #endif // CC_RESOURCES_ONE_COPY_TILE_TASK_WORKER_POOL_H_