1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef MOZILLA_GFX_CONTENTCLIENT_H
#define MOZILLA_GFX_CONTENTCLIENT_H
#include <stdint.h> // for uint32_t
#include "RotatedBuffer.h" // for RotatedBuffer, etc
#include "gfxTypes.h"
#include "gfxPlatform.h" // for gfxPlatform
#include "mozilla/Assertions.h" // for MOZ_CRASH
#include "mozilla/Attributes.h" // for override
#include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed
#include "mozilla/gfx/Point.h" // for IntSize
#include "mozilla/layers/CompositableClient.h" // for CompositableClient
#include "mozilla/layers/CompositableForwarder.h"
#include "mozilla/layers/CompositorTypes.h" // for TextureInfo, etc
#include "mozilla/layers/ISurfaceAllocator.h"
#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor
#include "mozilla/layers/LayersTypes.h" // for TextureDumpMode
#include "mozilla/layers/TextureClient.h" // for TextureClient
#include "mozilla/layers/PaintThread.h" // for PaintTask
#include "mozilla/Maybe.h" // for Maybe
#include "mozilla/mozalloc.h" // for operator delete
#include "mozilla/UniquePtr.h" // for UniquePtr
#include "ReadbackProcessor.h" // For ReadbackProcessor::Update
#include "nsCOMPtr.h" // for already_AddRefed
#include "nsPoint.h" // for nsIntPoint
#include "nsRect.h" // for mozilla::gfx::IntRect
#include "nsRegion.h" // for nsIntRegion
#include "nsTArray.h" // for nsTArray
namespace mozilla {
namespace gfx {
class DrawTarget;
} // namespace gfx
namespace layers {
class PaintedLayer;
/**
* A compositable client for PaintedLayers. These are different to Image/Canvas
* clients due to sending a valid region across IPC and because we do a lot more
* optimisation work, encapsulated in RotatedBuffers.
*
* We use content clients for OMTC and non-OMTC, basic rendering so that
* BasicPaintedLayer has only one interface to deal with. We support single and
* double buffered flavours. For tiled layers, we do not use a ContentClient
* although we do have a ContentHost, and we do use texture clients and texture
* hosts.
*
* The interface presented by ContentClient is used by the BasicPaintedLayer
* methods - PaintThebes, which is the same for MT and OMTC, and PaintBuffer
* which is different (the OMTC one does a little more).
*/
class ContentClient : public CompositableClient {
public:
typedef gfxContentType ContentType;
/**
* Creates, configures, and returns a new content client. If necessary, a
* message will be sent to the compositor to create a corresponding content
* host.
*/
static already_AddRefed<ContentClient> CreateContentClient(
CompositableForwarder* aFwd);
/**
* Controls the size of the backing buffer of this.
* - SizedToVisibleBounds: the backing buffer is exactly the same
* size as the bounds of PaintedLayer's visible region
* - ContainsVisibleBounds: the backing buffer is large enough to
* fit visible bounds. May be larger.
*/
enum BufferSizePolicy { SizedToVisibleBounds, ContainsVisibleBounds };
ContentClient(CompositableForwarder* aForwarder,
BufferSizePolicy aBufferSizePolicy)
: CompositableClient(aForwarder), mBufferSizePolicy(aBufferSizePolicy) {}
virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
virtual void Clear();
/**
* This is returned by BeginPaint. The caller should draw into mTarget.
* mRegionToDraw must be drawn. mRegionToInvalidate has been invalidated
* by ContentClient and must be redrawn on the screen.
* mRegionToInvalidate is set when the buffer has changed from
* opaque to transparent or vice versa, since the details of rendering can
* depend on the buffer type.
*/
struct PaintState {
PaintState()
: mRegionToDraw(),
mRegionToInvalidate(),
mMode(SurfaceMode::SURFACE_NONE),
mClip(DrawRegionClip::NONE),
mContentType(gfxContentType::SENTINEL),
mAsyncPaint(false),
mAsyncTask(nullptr) {}
nsIntRegion mRegionToDraw;
nsIntRegion mRegionToInvalidate;
SurfaceMode mMode;
DrawRegionClip mClip;
gfxContentType mContentType;
bool mAsyncPaint;
UniquePtr<PaintTask> mAsyncTask;
};
enum {
PAINT_WILL_RESAMPLE = 0x01,
PAINT_NO_ROTATION = 0x02,
PAINT_CAN_DRAW_ROTATED = 0x04,
PAINT_ASYNC = 0x08,
};
/**
* Start a drawing operation. This returns a PaintState describing what
* needs to be drawn to bring the buffer up to date in the visible region.
* This queries aLayer to get the currently valid and visible regions.
* The returned mTarget may be null if mRegionToDraw is empty.
* Otherwise it must not be null.
* mRegionToInvalidate will contain mRegionToDraw.
* @param aFlags when PAINT_WILL_RESAMPLE is passed, this indicates that
* buffer will be resampled when rendering (i.e the effective transform
* combined with the scale for the resolution is not just an integer
* translation). This will disable buffer rotation (since we don't want
* to resample across the rotation boundary) and will ensure that we
* make the entire buffer contents valid (since we don't want to sample
* invalid pixels outside the visible region, if the visible region doesn't
* fill the buffer bounds).
* PAINT_CAN_DRAW_ROTATED can be passed if the caller supports drawing
* rotated content that crosses the physical buffer boundary. The caller
* will need to call BorrowDrawTargetForPainting multiple times to achieve
* this.
*/
virtual PaintState BeginPaint(PaintedLayer* aLayer, uint32_t aFlags);
virtual void EndPaint(
PaintState& aPaintState,
nsTArray<ReadbackProcessor::Update>* aReadbackUpdates = nullptr);
/**
* Fetch a DrawTarget for rendering. The DrawTarget remains owned by
* this. See notes on BorrowDrawTargetForQuadrantUpdate.
* May return null. If the return value is non-null, it must be
* 'un-borrowed' using ReturnDrawTarget.
*
* If PAINT_CAN_DRAW_ROTATED was specified for BeginPaint, then the caller
* must call this function repeatedly (with an iterator) until it returns
* nullptr. The caller should draw the mDrawRegion of the iterator instead
* of mRegionToDraw in the PaintState.
*
* @param aPaintState Paint state data returned by a call to BeginPaint
* @param aIter Paint state iterator. Only required if PAINT_CAN_DRAW_ROTATED
* was specified to BeginPaint.
*/
virtual gfx::DrawTarget* BorrowDrawTargetForPainting(
PaintState& aPaintState, RotatedBuffer::DrawIterator* aIter = nullptr);
void ReturnDrawTarget(gfx::DrawTarget*& aReturned);
enum {
BUFFER_COMPONENT_ALPHA = 0x02 // Dual buffers should be created for drawing
// with component alpha.
};
protected:
struct BufferDecision {
nsIntRegion mNeededRegion;
nsIntRegion mValidRegion;
gfx::IntRect mBufferRect;
SurfaceMode mBufferMode;
gfxContentType mBufferContentType;
bool mCanReuseBuffer;
bool mCanKeepBufferContents;
};
/**
* Decide whether we can keep our current buffer and its contents,
* and return a struct containing the regions to paint, invalidate,
* the new buffer rect, surface mode, and content type.
*/
BufferDecision CalculateBufferForPaint(PaintedLayer* aLayer, uint32_t aFlags);
static bool ValidBufferSize(BufferSizePolicy aPolicy,
const gfx::IntSize& aBufferSize,
const gfx::IntSize& aVisibleBoundsSize);
/**
* Any actions that should be performed at the last moment before we begin
* rendering the next frame. I.e., after we calculate what we will draw,
* but before we rotate the buffer and possibly create new buffers.
* aRegionToDraw is the region which is guaranteed to be overwritten when
* drawing the next frame.
*/
virtual void FinalizeFrame(PaintState& aPaintState) {}
virtual RefPtr<RotatedBuffer> GetFrontBuffer() const { return mBuffer; }
/**
* Create a new rotated buffer for the specified content type, buffer rect,
* and buffer flags.
*/
virtual RefPtr<RotatedBuffer> CreateBuffer(gfxContentType aType,
const gfx::IntRect& aRect,
uint32_t aFlags) = 0;
RefPtr<RotatedBuffer> mBuffer;
BufferSizePolicy mBufferSizePolicy;
};
// Thin wrapper around DrawTargetRotatedBuffer, for on-mtc
class ContentClientBasic final : public ContentClient {
public:
explicit ContentClientBasic(gfx::BackendType aBackend);
void DrawTo(PaintedLayer* aLayer, gfx::DrawTarget* aTarget, float aOpacity,
gfx::CompositionOp aOp, gfx::SourceSurface* aMask,
const gfx::Matrix* aMaskTransform);
TextureInfo GetTextureInfo() const override {
MOZ_CRASH("GFX: Should not be called on non-remote ContentClient");
}
protected:
RefPtr<RotatedBuffer> CreateBuffer(gfxContentType aType,
const gfx::IntRect& aRect,
uint32_t aFlags) override;
private:
gfx::BackendType mBackend;
};
/**
* A ContentClient backed by a RemoteRotatedBuffer.
*
* When using a ContentClientRemoteBuffer, SurfaceDescriptors are created on
* the rendering side and destroyed on the compositing side. They are only
* passed from one side to the other when the TextureClient/Hosts are created.
* *Ownership* of the SurfaceDescriptor moves from the rendering side to the
* compositing side with the create message (send from CreateBuffer) which
* tells the compositor that TextureClients have been created and that the
* compositor should assign the corresponding TextureHosts to our corresponding
* ContentHost.
*
* If the size or type of our buffer(s) change(s), then we simply destroy and
* create them.
*/
class ContentClientRemoteBuffer : public ContentClient {
public:
explicit ContentClientRemoteBuffer(CompositableForwarder* aForwarder)
: ContentClient(aForwarder, ContainsVisibleBounds), mIsNewBuffer(false) {}
void Dump(std::stringstream& aStream, const char* aPrefix = "",
bool aDumpHtml = false,
TextureDumpMode aCompress = TextureDumpMode::Compress) override;
void EndPaint(
PaintState& aPaintState,
nsTArray<ReadbackProcessor::Update>* aReadbackUpdates = nullptr) override;
void Updated(const nsIntRegion& aRegionToDraw,
const nsIntRegion& aVisibleRegion);
TextureFlags ExtraTextureFlags() const {
return TextureFlags::IMMEDIATE_UPLOAD;
}
protected:
/**
* Called when we have been updated and should swap references to our
* buffers.
*/
virtual void SwapBuffers(const nsIntRegion& aFrontUpdatedRegion) {}
virtual nsIntRegion GetUpdatedRegion(const nsIntRegion& aRegionToDraw,
const nsIntRegion& aVisibleRegion);
RefPtr<RotatedBuffer> CreateBuffer(gfxContentType aType,
const gfx::IntRect& aRect,
uint32_t aFlags) override;
RefPtr<RotatedBuffer> CreateBufferInternal(const gfx::IntRect& aRect,
gfx::SurfaceFormat aFormat,
TextureFlags aFlags);
RemoteRotatedBuffer* GetRemoteBuffer() const {
return static_cast<RemoteRotatedBuffer*>(mBuffer.get());
}
bool mIsNewBuffer;
};
/**
* A double buffered ContentClientRemoteBuffer. mBuffer is the back buffer,
* which we draw into. mFrontBuffer is the front buffer which we may read from,
* but not write to, when the compositor does not have the 'soft' lock.
*
* The ContentHost keeps a reference to both corresponding texture hosts, in
* response to our UpdateTextureRegion message, the compositor swaps its
* references.
*/
class ContentClientDoubleBuffered : public ContentClientRemoteBuffer {
public:
explicit ContentClientDoubleBuffered(CompositableForwarder* aFwd)
: ContentClientRemoteBuffer(aFwd), mFrontAndBackBufferDiffer(false) {}
void Dump(std::stringstream& aStream, const char* aPrefix = "",
bool aDumpHtml = false,
TextureDumpMode aCompress = TextureDumpMode::Compress) override;
void Clear() override;
void SwapBuffers(const nsIntRegion& aFrontUpdatedRegion) override;
PaintState BeginPaint(PaintedLayer* aLayer, uint32_t aFlags) override;
void FinalizeFrame(PaintState& aPaintState) override;
RefPtr<RotatedBuffer> GetFrontBuffer() const override { return mFrontBuffer; }
TextureInfo GetTextureInfo() const override {
return TextureInfo(CompositableType::CONTENT_DOUBLE, mTextureFlags);
}
private:
void EnsureBackBufferIfFrontBuffer();
RefPtr<RemoteRotatedBuffer> mFrontBuffer;
nsIntRegion mFrontUpdatedRegion;
bool mFrontAndBackBufferDiffer;
};
/**
* A single buffered ContentClientRemoteBuffer. We have a single
* TextureClient/Host which we update and then send a message to the
* compositor that we are done updating. It is not safe for the compositor
* to use the corresponding TextureHost's memory directly, it must upload
* it to video memory of some kind. We are free to modify the TextureClient
* once we receive reply from the compositor.
*/
class ContentClientSingleBuffered : public ContentClientRemoteBuffer {
public:
explicit ContentClientSingleBuffered(CompositableForwarder* aFwd)
: ContentClientRemoteBuffer(aFwd) {}
TextureInfo GetTextureInfo() const override {
return TextureInfo(CompositableType::CONTENT_SINGLE,
mTextureFlags | ExtraTextureFlags());
}
};
} // namespace layers
} // namespace mozilla
#endif
|