/src/mozilla-central/gfx/2d/DrawingJob.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef MOZILLA_GFX_COMMANDBUFFER_H_ |
8 | | #define MOZILLA_GFX_COMMANDBUFFER_H_ |
9 | | |
10 | | #include <stdint.h> |
11 | | |
12 | | #include "mozilla/RefPtr.h" |
13 | | #include "mozilla/Assertions.h" |
14 | | #include "mozilla/gfx/Matrix.h" |
15 | | #include "mozilla/gfx/JobScheduler.h" |
16 | | #include "mozilla/gfx/IterableArena.h" |
17 | | #include "mozilla/RefCounted.h" |
18 | | #include "DrawCommand.h" |
19 | | |
20 | | namespace mozilla { |
21 | | namespace gfx { |
22 | | |
23 | | class DrawingCommand; |
24 | | class PrintCommand; |
25 | | class SignalCommand; |
26 | | class DrawingJob; |
27 | | class WaitCommand; |
28 | | |
29 | | class SyncObject; |
30 | | class MultiThreadedJobQueue; |
31 | | |
32 | | class DrawTarget; |
33 | | |
34 | | class DrawingJobBuilder; |
35 | | class CommandBufferBuilder; |
36 | | |
37 | | /// Contains a sequence of immutable drawing commands that are typically used by |
38 | | /// several DrawingJobs. |
39 | | /// |
40 | | /// CommandBuffer objects are built using CommandBufferBuilder. |
41 | | class CommandBuffer : public external::AtomicRefCounted<CommandBuffer> |
42 | | { |
43 | | public: |
44 | | MOZ_DECLARE_REFCOUNTED_TYPENAME(CommandBuffer) |
45 | | |
46 | | ~CommandBuffer(); |
47 | | |
48 | | const DrawingCommand* GetDrawingCommand(ptrdiff_t aId); |
49 | | |
50 | | protected: |
51 | | explicit CommandBuffer(size_t aSize = 256) |
52 | | : mStorage(IterableArena::GROWABLE, aSize) |
53 | 0 | {} |
54 | | |
55 | | IterableArena mStorage; |
56 | | friend class CommandBufferBuilder; |
57 | | }; |
58 | | |
59 | | /// Generates CommandBuffer objects. |
60 | | /// |
61 | | /// The builder is a separate object to ensure that commands are not added to a |
62 | | /// submitted CommandBuffer. |
63 | | class CommandBufferBuilder |
64 | | { |
65 | | public: |
66 | | void BeginCommandBuffer(size_t aBufferSize = 256); |
67 | | |
68 | | already_AddRefed<CommandBuffer> EndCommandBuffer(); |
69 | | |
70 | | /// Build the CommandBuffer, command after command. |
71 | | /// This must be used between BeginCommandBuffer and EndCommandBuffer. |
72 | | template<typename T, typename... Args> |
73 | | ptrdiff_t AddCommand(Args&&... aArgs) |
74 | | { |
75 | | static_assert(IsBaseOf<DrawingCommand, T>::value, |
76 | | "T must derive from DrawingCommand"); |
77 | | return mCommands->mStorage.Alloc<T>(std::forward<Args>(aArgs)...); |
78 | | } |
79 | | |
80 | 0 | bool HasCommands() const { return !!mCommands; } |
81 | | |
82 | | protected: |
83 | | RefPtr<CommandBuffer> mCommands; |
84 | | }; |
85 | | |
86 | | /// Stores multiple commands to be executed sequencially. |
87 | | class DrawingJob : public Job { |
88 | | public: |
89 | | ~DrawingJob(); |
90 | | |
91 | | virtual JobStatus Run() override; |
92 | | |
93 | | protected: |
94 | | DrawingJob(DrawTarget* aTarget, |
95 | | IntPoint aOffset, |
96 | | SyncObject* aStart, |
97 | | SyncObject* aCompletion, |
98 | | WorkerThread* aPinToWorker = nullptr); |
99 | | |
100 | | /// Runs the tasks's destructors and resets the buffer. |
101 | | void Clear(); |
102 | | |
103 | | std::vector<ptrdiff_t> mCommandOffsets; |
104 | | RefPtr<CommandBuffer> mCommandBuffer; |
105 | | uint32_t mCursor; |
106 | | |
107 | | RefPtr<DrawTarget> mDrawTarget; |
108 | | IntPoint mOffset; |
109 | | |
110 | | friend class DrawingJobBuilder; |
111 | | }; |
112 | | |
113 | | /// Generates DrawingJob objects. |
114 | | /// |
115 | | /// The builder is a separate object to ensure that commands are not added to a |
116 | | /// submitted DrawingJob. |
117 | | class DrawingJobBuilder { |
118 | | public: |
119 | | DrawingJobBuilder(); |
120 | | |
121 | | ~DrawingJobBuilder(); |
122 | | |
123 | | /// Allocates a DrawingJob. |
124 | | /// |
125 | | /// call this method before starting to add commands. |
126 | | void BeginDrawingJob(DrawTarget* aTarget, IntPoint aOffset, |
127 | | SyncObject* aStart = nullptr); |
128 | | |
129 | | /// Build the DrawingJob, command after command. |
130 | | /// This must be used between BeginDrawingJob and EndDrawingJob. |
131 | | void AddCommand(ptrdiff_t offset) |
132 | 0 | { |
133 | 0 | mCommandOffsets.push_back(offset); |
134 | 0 | } |
135 | | |
136 | | /// Finalizes and returns the drawing task. |
137 | | /// |
138 | | /// If aCompletion is not null, the sync object will be signaled after the |
139 | | /// task buffer is destroyed (and after the destructor of the tasks have run). |
140 | | /// In most cases this means after the completion of all tasks in the task buffer, |
141 | | /// but also when the task buffer is destroyed due to an error. |
142 | | DrawingJob* EndDrawingJob(CommandBuffer* aCmdBuffer, |
143 | | SyncObject* aCompletion = nullptr, |
144 | | WorkerThread* aPinToWorker = nullptr); |
145 | | |
146 | | /// Returns true between BeginDrawingJob and EndDrawingJob, false otherwise. |
147 | 0 | bool HasDrawingJob() const { return !!mDrawTarget; } |
148 | | |
149 | | protected: |
150 | | std::vector<ptrdiff_t> mCommandOffsets; |
151 | | RefPtr<DrawTarget> mDrawTarget; |
152 | | IntPoint mOffset; |
153 | | RefPtr<SyncObject> mStart; |
154 | | }; |
155 | | |
156 | | } // namespace |
157 | | } // namespace |
158 | | |
159 | | #endif |