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
|
/* -*- 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/. */
#include "mozilla/layers/SingleTiledContentClient.h"
#include "ClientTiledPaintedLayer.h"
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "TiledLayerBuffer.h"
namespace mozilla {
namespace layers {
SingleTiledContentClient::SingleTiledContentClient(
ClientTiledPaintedLayer& aPaintedLayer, ClientLayerManager* aManager)
: TiledContentClient(aManager, "Single") {
MOZ_COUNT_CTOR(SingleTiledContentClient);
mTiledBuffer =
new ClientSingleTiledLayerBuffer(aPaintedLayer, *this, aManager);
}
void SingleTiledContentClient::ClearCachedResources() {
CompositableClient::ClearCachedResources();
mTiledBuffer->DiscardBuffers();
}
void SingleTiledContentClient::UpdatedBuffer(TiledBufferType aType) {
mForwarder->UseTiledLayerBuffer(this,
mTiledBuffer->GetSurfaceDescriptorTiles());
}
/* static */
bool SingleTiledContentClient::ClientSupportsLayerSize(
const gfx::IntSize& aSize, ClientLayerManager* aManager) {
int32_t maxTextureSize = aManager->GetMaxTextureSize();
return aSize.width <= maxTextureSize && aSize.height <= maxTextureSize;
}
ClientSingleTiledLayerBuffer::ClientSingleTiledLayerBuffer(
ClientTiledPaintedLayer& aPaintedLayer,
CompositableClient& aCompositableClient, ClientLayerManager* aManager)
: ClientTiledLayerBuffer(aPaintedLayer, aCompositableClient),
mManager(aManager),
mWasLastPaintProgressive(false),
mFormat(gfx::SurfaceFormat::UNKNOWN) {}
void ClientSingleTiledLayerBuffer::ReleaseTiles() {
if (!mTile.IsPlaceholderTile()) {
mTile.DiscardBuffers();
}
mTile.SetTextureAllocator(nullptr);
}
void ClientSingleTiledLayerBuffer::DiscardBuffers() {
if (!mTile.IsPlaceholderTile()) {
mTile.DiscardFrontBuffer();
mTile.DiscardBackBuffer();
}
}
SurfaceDescriptorTiles
ClientSingleTiledLayerBuffer::GetSurfaceDescriptorTiles() {
nsTArray<TileDescriptor> tiles;
TileDescriptor tileDesc = mTile.GetTileDescriptor();
tiles.AppendElement(tileDesc);
mTile.mUpdateRect = gfx::IntRect();
return SurfaceDescriptorTiles(mValidRegion, tiles, mTilingOrigin, mSize, 0, 0,
1, 1, 1.0, mFrameResolution.xScale,
mFrameResolution.yScale,
mWasLastPaintProgressive);
}
already_AddRefed<TextureClient>
ClientSingleTiledLayerBuffer::GetTextureClient() {
MOZ_ASSERT(mFormat != gfx::SurfaceFormat::UNKNOWN);
return mCompositableClient.CreateTextureClientForDrawing(
gfx::ImageFormatToSurfaceFormat(mFormat), mSize, BackendSelector::Content,
TextureFlags::DISALLOW_BIGIMAGE | TextureFlags::IMMEDIATE_UPLOAD |
TextureFlags::NON_BLOCKING_READ_LOCK);
}
void ClientSingleTiledLayerBuffer::PaintThebes(
const nsIntRegion& aNewValidRegion, const nsIntRegion& aPaintRegion,
const nsIntRegion& aDirtyRegion,
LayerManager::DrawPaintedLayerCallback aCallback, void* aCallbackData,
TilePaintFlags aFlags) {
mWasLastPaintProgressive = !!(aFlags & TilePaintFlags::Progressive);
bool asyncPaint = !!(aFlags & TilePaintFlags::Async);
// Compare layer valid region size to current backbuffer size, discard if not
// matching.
gfx::IntSize size = aNewValidRegion.GetBounds().Size();
gfx::IntPoint origin = aNewValidRegion.GetBounds().TopLeft();
nsIntRegion paintRegion = aPaintRegion;
RefPtr<TextureClient> discardedFrontBuffer = nullptr;
RefPtr<TextureClient> discardedFrontBufferOnWhite = nullptr;
nsIntRegion discardedValidRegion;
if (mSize != size || mTilingOrigin != origin) {
discardedFrontBuffer = mTile.mFrontBuffer;
discardedFrontBufferOnWhite = mTile.mFrontBufferOnWhite;
discardedValidRegion = mValidRegion;
TILING_LOG(
"TILING %p: Single-tile valid region changed. Discarding buffers.\n",
&mPaintedLayer);
ResetPaintedAndValidState();
mSize = size;
mTilingOrigin = origin;
paintRegion = aNewValidRegion;
}
SurfaceMode mode;
gfxContentType content = GetContentType(&mode);
mFormat = gfxPlatform::GetPlatform()->OptimalFormatForContent(content);
if (mTile.IsPlaceholderTile()) {
mTile.SetTextureAllocator(this);
}
if (mManager->AsShadowForwarder()->SupportsTextureDirectMapping()) {
AutoTArray<uint64_t, 2> syncTextureSerials;
mTile.GetSyncTextureSerials(mode, syncTextureSerials);
if (syncTextureSerials.Length() > 0) {
mManager->AsShadowForwarder()->SyncTextures(syncTextureSerials);
}
}
// The dirty region relative to the top-left of the tile.
nsIntRegion tileVisibleRegion = aNewValidRegion.MovedBy(-mTilingOrigin);
nsIntRegion tileDirtyRegion = paintRegion.MovedBy(-mTilingOrigin);
Maybe<AcquiredBackBuffer> backBuffer =
mTile.AcquireBackBuffer(mCompositableClient, tileDirtyRegion,
tileVisibleRegion, content, mode, aFlags);
if (!backBuffer) {
return;
}
// Mark the area we need to paint in the back buffer as invalid in the
// front buffer as they will become out of sync.
mTile.mInvalidFront.OrWith(tileDirtyRegion);
// Add backbuffer's invalid region to the dirty region to be painted.
// This will be empty if we were able to copy from the front in to the back.
nsIntRegion tileInvalidRegion = mTile.mInvalidBack;
tileInvalidRegion.AndWith(tileVisibleRegion);
paintRegion.OrWith(tileInvalidRegion.MovedBy(mTilingOrigin));
tileDirtyRegion.OrWith(tileInvalidRegion);
// Mark the region we will be painting and the region we copied from the front
// buffer as needing to be uploaded to the compositor
mTile.mUpdateRect =
tileDirtyRegion.GetBounds().Union(backBuffer->mUpdatedRect);
// If the old frontbuffer was discarded then attempt to copy what we
// can from it to the new backbuffer.
if (discardedFrontBuffer) {
nsIntRegion copyableRegion;
copyableRegion.And(aNewValidRegion, discardedValidRegion);
copyableRegion.SubOut(aDirtyRegion);
OpenMode readMode =
asyncPaint ? OpenMode::OPEN_READ_ASYNC : OpenMode::OPEN_READ;
DualTextureClientAutoLock discardedBuffer(
discardedFrontBuffer, discardedFrontBufferOnWhite, readMode);
if (discardedBuffer.Succeeded()) {
RefPtr<gfx::SourceSurface> discardedSurface = discardedBuffer->Snapshot();
for (auto iter = copyableRegion.RectIter(); !iter.Done(); iter.Next()) {
const gfx::IntRect src =
iter.Get() - discardedValidRegion.GetBounds().TopLeft();
const gfx::IntPoint dest = iter.Get().TopLeft() - mTilingOrigin;
backBuffer->mTarget->CopySurface(discardedSurface, src, dest);
}
TILING_LOG("TILING %p: Region copied from discarded frontbuffer %s\n",
&mPaintedLayer, Stringify(copyableRegion).c_str());
// We don't need to repaint valid content that was just copied.
paintRegion.SubOut(copyableRegion);
copyableRegion.MoveBy(-mTilingOrigin);
tileDirtyRegion.SubOut(copyableRegion);
} else {
gfxWarning() << "[Tiling:Client] Failed to aquire the discarded front "
"buffer's draw target";
}
}
if (mode != SurfaceMode::SURFACE_OPAQUE) {
for (auto iter = tileDirtyRegion.RectIter(); !iter.Done(); iter.Next()) {
const gfx::Rect drawRect(iter.Get().X(), iter.Get().Y(),
iter.Get().Width(), iter.Get().Height());
backBuffer->mTarget->ClearRect(drawRect);
}
}
// Paint into the target
{
RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(backBuffer->mTarget);
if (!ctx) {
gfxDevCrash(gfx::LogReason::InvalidContext)
<< "SingleTiledContextClient context problem "
<< gfx::hexa(backBuffer->mTarget);
return;
}
ctx->SetMatrix(
ctx->CurrentMatrix().PreTranslate(-mTilingOrigin.x, -mTilingOrigin.y));
aCallback(&mPaintedLayer, ctx, paintRegion, paintRegion,
DrawRegionClip::DRAW, nsIntRegion(), aCallbackData);
}
if (asyncPaint) {
if (!backBuffer->mCapture->IsEmpty()) {
UniquePtr<PaintTask> task(new PaintTask());
task->mCapture = backBuffer->mCapture;
task->mTarget = backBuffer->mBackBuffer;
task->mClients = std::move(backBuffer->mTextureClients);
if (discardedFrontBuffer) {
task->mClients.AppendElement(discardedFrontBuffer);
}
if (discardedFrontBufferOnWhite) {
task->mClients.AppendElement(discardedFrontBufferOnWhite);
}
// The target is an alias for the capture, and the paint thread expects
// to be the only one with a reference to the capture
backBuffer->mTarget = nullptr;
backBuffer->mCapture = nullptr;
PaintThread::Get()->QueuePaintTask(std::move(task));
mManager->SetQueuedAsyncPaints();
}
} else {
MOZ_ASSERT(backBuffer->mTarget == backBuffer->mBackBuffer);
MOZ_ASSERT(!backBuffer->mCapture);
}
// The new buffer is now validated, remove the dirty region from it.
mTile.mInvalidBack.SubOut(tileDirtyRegion);
backBuffer = Nothing();
mTile.Flip();
UnlockTile(mTile);
mValidRegion = aNewValidRegion;
mLastPaintSurfaceMode = mode;
mLastPaintContentType = content;
}
} // namespace layers
} // namespace mozilla
|