forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_raster_buffer_provider.cc
329 lines (282 loc) · 12.2 KB
/
gpu_raster_buffer_provider.cc
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/raster/gpu_raster_buffer_provider.h"
#include <stdint.h>
#include <algorithm>
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "cc/base/histograms.h"
#include "cc/paint/display_item_list.h"
#include "cc/paint/paint_canvas.h"
#include "cc/paint/paint_recorder.h"
#include "cc/raster/raster_source.h"
#include "cc/raster/scoped_gpu_raster.h"
#include "cc/resources/resource.h"
#include "gpu/command_buffer/client/context_support.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "third_party/skia/include/core/SkMultiPictureDraw.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/GrContext.h"
#include "ui/gfx/geometry/axis_transform2d.h"
namespace cc {
namespace {
static void RasterizeSourceOOP(
const RasterSource* raster_source,
bool resource_has_previous_content,
const gfx::Size& resource_size,
const gfx::Rect& raster_full_rect,
const gfx::Rect& playback_rect,
const gfx::AxisTransform2d& transform,
const RasterSource::PlaybackSettings& playback_settings,
viz::ContextProvider* context_provider,
ResourceProvider::ScopedWriteLockGL* resource_lock,
bool use_distance_field_text,
int msaa_sample_count) {
gpu::gles2::GLES2Interface* gl = context_provider->ContextGL();
GLuint texture_id = resource_lock->ConsumeTexture(gl);
gl->BeginRasterCHROMIUM(texture_id, raster_source->background_color(),
msaa_sample_count, playback_settings.use_lcd_text,
use_distance_field_text,
resource_lock->PixelConfig());
// TODO(enne): need to pass color space into this function as well.
float recording_to_raster_scale =
transform.scale() / raster_source->recording_scale_factor();
gl->RasterCHROMIUM(raster_source->GetDisplayItemList().get(),
raster_full_rect.x(), raster_full_rect.y(),
playback_rect.x(), playback_rect.y(),
playback_rect.width(), playback_rect.height(),
transform.translation().x(), transform.translation().y(),
recording_to_raster_scale);
gl->EndRasterCHROMIUM();
gl->DeleteTextures(1, &texture_id);
}
static void RasterizeSource(
const RasterSource* raster_source,
bool resource_has_previous_content,
const gfx::Size& resource_size,
const gfx::Rect& raster_full_rect,
const gfx::Rect& playback_rect,
const gfx::AxisTransform2d& transform,
const RasterSource::PlaybackSettings& playback_settings,
viz::ContextProvider* context_provider,
ResourceProvider::ScopedWriteLockGL* resource_lock,
bool use_distance_field_text,
int msaa_sample_count) {
ScopedGpuRaster gpu_raster(context_provider);
gpu::gles2::GLES2Interface* gl = context_provider->ContextGL();
GLuint texture_id = resource_lock->ConsumeTexture(gl);
{
ResourceProvider::ScopedSkSurface scoped_surface(
context_provider->GrContext(), texture_id, resource_lock->target(),
resource_lock->size(), resource_lock->format(), use_distance_field_text,
playback_settings.use_lcd_text, msaa_sample_count);
SkSurface* surface = scoped_surface.surface();
// Allocating an SkSurface will fail after a lost context. Pretend we
// rasterized, as the contents of the resource don't matter anymore.
if (!surface) {
DLOG(ERROR) << "Failed to allocate raster surface";
return;
}
SkCanvas* canvas = surface->getCanvas();
// As an optimization, inform Skia to discard when not doing partial raster.
if (raster_full_rect == playback_rect)
canvas->discard();
raster_source->PlaybackToCanvas(
canvas, resource_lock->color_space_for_raster(), raster_full_rect,
playback_rect, transform, playback_settings);
}
gl->DeleteTextures(1, &texture_id);
}
} // namespace
GpuRasterBufferProvider::RasterBufferImpl::RasterBufferImpl(
GpuRasterBufferProvider* client,
LayerTreeResourceProvider* resource_provider,
viz::ResourceId resource_id,
bool resource_has_previous_content)
: client_(client),
lock_(resource_provider, resource_id),
resource_has_previous_content_(resource_has_previous_content) {
client_->pending_raster_buffers_.insert(this);
lock_.CreateMailbox();
}
GpuRasterBufferProvider::RasterBufferImpl::~RasterBufferImpl() {
client_->pending_raster_buffers_.erase(this);
}
void GpuRasterBufferProvider::RasterBufferImpl::Playback(
const RasterSource* raster_source,
const gfx::Rect& raster_full_rect,
const gfx::Rect& raster_dirty_rect,
uint64_t new_content_id,
const gfx::AxisTransform2d& transform,
const RasterSource::PlaybackSettings& playback_settings) {
TRACE_EVENT0("cc", "GpuRasterBuffer::Playback");
client_->PlaybackOnWorkerThread(&lock_, sync_token_,
resource_has_previous_content_, raster_source,
raster_full_rect, raster_dirty_rect,
new_content_id, transform, playback_settings);
}
GpuRasterBufferProvider::GpuRasterBufferProvider(
viz::ContextProvider* compositor_context_provider,
viz::ContextProvider* worker_context_provider,
LayerTreeResourceProvider* resource_provider,
bool use_distance_field_text,
int gpu_rasterization_msaa_sample_count,
viz::ResourceFormat preferred_tile_format,
bool async_worker_context_enabled,
bool enable_oop_rasterization)
: compositor_context_provider_(compositor_context_provider),
worker_context_provider_(worker_context_provider),
resource_provider_(resource_provider),
use_distance_field_text_(use_distance_field_text),
msaa_sample_count_(gpu_rasterization_msaa_sample_count),
preferred_tile_format_(preferred_tile_format),
async_worker_context_enabled_(async_worker_context_enabled),
enable_oop_rasterization_(enable_oop_rasterization) {
DCHECK(compositor_context_provider);
DCHECK(worker_context_provider);
}
GpuRasterBufferProvider::~GpuRasterBufferProvider() {
DCHECK(pending_raster_buffers_.empty());
}
std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster(
const Resource* resource,
uint64_t resource_content_id,
uint64_t previous_content_id) {
bool resource_has_previous_content =
resource_content_id && resource_content_id == previous_content_id;
return std::make_unique<RasterBufferImpl>(
this, resource_provider_, resource->id(), resource_has_previous_content);
}
void GpuRasterBufferProvider::OrderingBarrier() {
TRACE_EVENT0("cc", "GpuRasterBufferProvider::OrderingBarrier");
gpu::gles2::GLES2Interface* gl = compositor_context_provider_->ContextGL();
if (async_worker_context_enabled_) {
gpu::SyncToken sync_token = ResourceProvider::GenerateSyncTokenHelper(gl);
for (RasterBufferImpl* buffer : pending_raster_buffers_)
buffer->set_sync_token(sync_token);
} else {
gl->OrderingBarrierCHROMIUM();
}
pending_raster_buffers_.clear();
}
void GpuRasterBufferProvider::Flush() {
if (async_worker_context_enabled_)
compositor_context_provider_->ContextSupport()->FlushPendingWork();
}
viz::ResourceFormat GpuRasterBufferProvider::GetResourceFormat(
bool must_support_alpha) const {
if (resource_provider_->IsRenderBufferFormatSupported(
preferred_tile_format_) &&
(DoesResourceFormatSupportAlpha(preferred_tile_format_) ||
!must_support_alpha)) {
return preferred_tile_format_;
}
return resource_provider_->best_render_buffer_format();
}
bool GpuRasterBufferProvider::IsResourceSwizzleRequired(
bool must_support_alpha) const {
// This doesn't require a swizzle because we rasterize to the correct format.
return false;
}
bool GpuRasterBufferProvider::CanPartialRasterIntoProvidedResource() const {
// Partial raster doesn't support MSAA, as the MSAA resolve is unaware of clip
// rects.
// TODO(crbug.com/629683): See if we can work around this limitation.
return msaa_sample_count_ == 0;
}
bool GpuRasterBufferProvider::IsResourceReadyToDraw(
viz::ResourceId resource_id) const {
if (!async_worker_context_enabled_)
return true;
gpu::SyncToken sync_token =
resource_provider_->GetSyncTokenForResources({resource_id});
if (!sync_token.HasData())
return true;
// IsSyncTokenSignaled is thread-safe, no need for worker context lock.
return worker_context_provider_->ContextSupport()->IsSyncTokenSignaled(
sync_token);
}
uint64_t GpuRasterBufferProvider::SetReadyToDrawCallback(
const ResourceProvider::ResourceIdArray& resource_ids,
const base::Closure& callback,
uint64_t pending_callback_id) const {
if (!async_worker_context_enabled_)
return 0;
gpu::SyncToken sync_token =
resource_provider_->GetSyncTokenForResources(resource_ids);
uint64_t callback_id = sync_token.release_count();
DCHECK_NE(callback_id, 0u);
// If the callback is different from the one the caller is already waiting on,
// pass the callback through to SignalSyncToken. Otherwise the request is
// redundant.
if (callback_id != pending_callback_id) {
// Use the compositor context because we want this callback on the impl
// thread.
compositor_context_provider_->ContextSupport()->SignalSyncToken(sync_token,
callback);
}
return callback_id;
}
void GpuRasterBufferProvider::Shutdown() {
pending_raster_buffers_.clear();
}
void GpuRasterBufferProvider::PlaybackOnWorkerThread(
ResourceProvider::ScopedWriteLockGL* resource_lock,
const gpu::SyncToken& sync_token,
bool resource_has_previous_content,
const RasterSource* raster_source,
const gfx::Rect& raster_full_rect,
const gfx::Rect& raster_dirty_rect,
uint64_t new_content_id,
const gfx::AxisTransform2d& transform,
const RasterSource::PlaybackSettings& playback_settings) {
viz::ContextProvider::ScopedContextLock scoped_context(
worker_context_provider_);
gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL();
DCHECK(gl);
// Synchronize with compositor. Nop if sync token is empty.
gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
gfx::Rect playback_rect = raster_full_rect;
if (resource_has_previous_content) {
playback_rect.Intersect(raster_dirty_rect);
}
DCHECK(!playback_rect.IsEmpty())
<< "Why are we rastering a tile that's not dirty?";
// Log a histogram of the percentage of pixels that were saved due to
// partial raster.
const char* client_name = GetClientNameForMetrics();
float full_rect_size = raster_full_rect.size().GetArea();
if (full_rect_size > 0 && client_name) {
float fraction_partial_rastered =
static_cast<float>(playback_rect.size().GetArea()) / full_rect_size;
float fraction_saved = 1.0f - fraction_partial_rastered;
UMA_HISTOGRAM_PERCENTAGE(
base::StringPrintf("Renderer4.%s.PartialRasterPercentageSaved.Gpu",
client_name),
100.0f * fraction_saved);
}
if (enable_oop_rasterization_) {
RasterizeSourceOOP(raster_source, resource_has_previous_content,
resource_lock->size(), raster_full_rect, playback_rect,
transform, playback_settings, worker_context_provider_,
resource_lock, use_distance_field_text_,
msaa_sample_count_);
} else {
RasterizeSource(raster_source, resource_has_previous_content,
resource_lock->size(), raster_full_rect, playback_rect,
transform, playback_settings, worker_context_provider_,
resource_lock, use_distance_field_text_,
msaa_sample_count_);
}
// Generate sync token for cross context synchronization.
resource_lock->set_sync_token(ResourceProvider::GenerateSyncTokenHelper(gl));
// Mark resource as synchronized when worker and compositor are in same stream
// to prevent extra wait sync token calls.
if (!async_worker_context_enabled_)
resource_lock->set_synchronized();
}
} // namespace cc