[go: nahoru, domu]

blob: 481e7be9a5ce24ae11a6ccbbca4e9e06e246ba29 [file] [log] [blame]
reveman@chromium.orgb5641b92014-02-15 14:21:581// 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.
4
prashant.nb4d4f492016-04-29 12:51:285#include "cc/raster/gpu_raster_buffer_provider.h"
reveman@chromium.orgb5641b92014-02-15 14:21:586
avi02a4d172015-12-21 06:14:367#include <stdint.h>
8
ernstm69fedbd2014-09-18 01:23:419#include <algorithm>
Mike Reedf7074ff2019-03-14 14:28:1610#include <utility>
ernstm69fedbd2014-09-18 01:23:4111
ericrke4027312016-06-30 00:12:4212#include "base/metrics/histogram_macros.h"
Khushal3c3873a2018-11-06 18:14:0113#include "base/rand_util.h"
Antoine Labour6283c792018-09-27 16:59:2814#include "base/strings/stringprintf.h"
Alexandr Ilin0443a8f2018-07-20 20:14:5015#include "base/trace_event/process_memory_dump.h"
ssid904ce3b2015-01-27 15:20:1616#include "base/trace_event/trace_event.h"
ericrkc7c9e3f2016-07-01 17:30:1617#include "cc/base/histograms.h"
Adrienne Walker436a7752017-08-28 23:33:0918#include "cc/paint/display_item_list.h"
enne5a9630362017-02-24 23:41:0319#include "cc/paint/paint_canvas.h"
Adrienne Walker436a7752017-08-28 23:33:0920#include "cc/paint/paint_recorder.h"
chrishtrac41ff92017-03-17 05:07:3021#include "cc/raster/raster_source.h"
danakj920156852015-05-18 20:22:2922#include "cc/raster/scoped_gpu_raster.h"
Khushal3cfc77f2018-08-15 07:19:5823#include "cc/raster/scoped_grcontext_access.h"
danakj57baa772018-05-29 15:59:1424#include "components/viz/client/client_resource_provider.h"
Victor Miura29b7ea3d2017-12-19 20:23:5925#include "components/viz/common/gpu/context_provider.h"
26#include "components/viz/common/gpu/raster_context_provider.h"
danakjaf3170e2018-02-09 17:31:5827#include "gpu/GLES2/gl2extchromium.h"
ericrk7f6a27f2017-01-31 22:34:3228#include "gpu/command_buffer/client/context_support.h"
reveman@chromium.orgb5641b92014-02-15 14:21:5829#include "gpu/command_buffer/client/gles2_interface.h"
Victor Miura3a4ad4f82017-12-13 06:03:4530#include "gpu/command_buffer/client/raster_interface.h"
Antoine Labour6283c792018-09-27 16:59:2831#include "gpu/command_buffer/client/shared_image_interface.h"
Antoine Labour6283c792018-09-27 16:59:2832#include "gpu/command_buffer/common/shared_image_trace_utils.h"
33#include "gpu/command_buffer/common/shared_image_usage.h"
hendrikw04cea972014-09-23 20:50:5334#include "third_party/skia/include/core/SkMultiPictureDraw.h"
35#include "third_party/skia/include/core/SkPictureRecorder.h"
reveman47560ab2014-09-18 19:39:2136#include "third_party/skia/include/core/SkSurface.h"
reveman@chromium.orgb5641b92014-02-15 14:21:5837#include "third_party/skia/include/gpu/GrContext.h"
Adrienne Walker436a7752017-08-28 23:33:0938#include "ui/gfx/geometry/axis_transform2d.h"
Khushal49836ab2018-07-25 02:08:4539#include "url/gurl.h"
reveman@chromium.orgb5641b92014-02-15 14:21:5840
41namespace cc {
reveman47560ab2014-09-18 19:39:2142namespace {
43
Eric Karl247f09c2018-03-15 02:06:3644class ScopedSkSurfaceForUnpremultiplyAndDither {
45 public:
46 ScopedSkSurfaceForUnpremultiplyAndDither(
47 viz::RasterContextProvider* context_provider,
Mike Reedf7074ff2019-03-14 14:28:1648 sk_sp<SkColorSpace> color_space,
Eric Karl247f09c2018-03-15 02:06:3649 const gfx::Rect& playback_rect,
50 const gfx::Rect& raster_full_rect,
51 const gfx::Size& max_tile_size,
52 GLuint texture_id,
53 const gfx::Size& texture_size,
Eric Karl247f09c2018-03-15 02:06:3654 bool can_use_lcd_text,
55 int msaa_sample_count)
56 : context_provider_(context_provider),
57 texture_id_(texture_id),
58 offset_(playback_rect.OffsetFromOrigin() -
59 raster_full_rect.OffsetFromOrigin()),
60 size_(playback_rect.size()) {
61 // Determine the |intermediate_size| to use for our 32-bit texture. If we
62 // know the max tile size, use that. This prevents GPU cache explosion due
63 // to using lots of different 32-bit texture sizes. Otherwise just use the
64 // exact size of the target texture.
65 gfx::Size intermediate_size;
66 if (!max_tile_size.IsEmpty()) {
67 DCHECK_GE(max_tile_size.width(), texture_size.width());
68 DCHECK_GE(max_tile_size.height(), texture_size.height());
69 intermediate_size = max_tile_size;
70 } else {
71 intermediate_size = texture_size;
72 }
73
74 // Allocate a 32-bit surface for raster. We will copy from that into our
75 // actual surface in destruction.
Mike Reedf7074ff2019-03-14 14:28:1676 SkImageInfo n32Info = SkImageInfo::MakeN32Premul(intermediate_size.width(),
77 intermediate_size.height(),
78 std::move(color_space));
Eric Karl247f09c2018-03-15 02:06:3679 SkSurfaceProps surface_props =
danakj57baa772018-05-29 15:59:1480 viz::ClientResourceProvider::ScopedSkSurface::ComputeSurfaceProps(
Adrienne Walker7774c3c2018-03-21 23:40:3481 can_use_lcd_text);
Eric Karl247f09c2018-03-15 02:06:3682 surface_ = SkSurface::MakeRenderTarget(
83 context_provider->GrContext(), SkBudgeted::kNo, n32Info,
84 msaa_sample_count, kTopLeft_GrSurfaceOrigin, &surface_props);
85 }
86
87 ~ScopedSkSurfaceForUnpremultiplyAndDither() {
88 // In lost-context cases, |surface_| may be null and there's nothing
89 // meaningful to do here.
90 if (!surface_)
91 return;
92
Greg Daniel0691c672018-04-17 17:21:1493 GrBackendTexture backend_texture =
94 surface_->getBackendTexture(SkSurface::kFlushRead_BackendHandleAccess);
95 if (!backend_texture.isValid()) {
96 return;
97 }
98 GrGLTextureInfo info;
99 if (!backend_texture.getGLTextureInfo(&info)) {
100 return;
101 }
Eric Karl247f09c2018-03-15 02:06:36102 context_provider_->ContextGL()->UnpremultiplyAndDitherCopyCHROMIUM(
Greg Daniel0691c672018-04-17 17:21:14103 info.fID, texture_id_, offset_.x(), offset_.y(), size_.width(),
Eric Karl247f09c2018-03-15 02:06:36104 size_.height());
105 }
106
107 SkSurface* surface() { return surface_.get(); }
108
109 private:
110 viz::RasterContextProvider* context_provider_;
111 GLuint texture_id_;
112 gfx::Vector2d offset_;
113 gfx::Size size_;
114 sk_sp<SkSurface> surface_;
115};
116
Adrienne Walker436a7752017-08-28 23:33:09117static void RasterizeSourceOOP(
118 const RasterSource* raster_source,
119 bool resource_has_previous_content,
Antoine Labour6283c792018-09-27 16:59:28120 gpu::Mailbox* mailbox,
121 const gpu::SyncToken& sync_token,
danakjaf3170e2018-02-09 17:31:58122 GLenum texture_target,
123 bool texture_is_overlay_candidate,
danakjaf3170e2018-02-09 17:31:58124 const gfx::Size& resource_size,
125 viz::ResourceFormat resource_format,
126 const gfx::ColorSpace& color_space,
Adrienne Walker436a7752017-08-28 23:33:09127 const gfx::Rect& raster_full_rect,
128 const gfx::Rect& playback_rect,
129 const gfx::AxisTransform2d& transform,
130 const RasterSource::PlaybackSettings& playback_settings,
Khushalec3ba5dc2019-11-04 22:30:21131 viz::RasterContextProvider* context_provider) {
Victor Miura29b7ea3d2017-12-19 20:23:59132 gpu::raster::RasterInterface* ri = context_provider->RasterInterface();
Antoine Labour6283c792018-09-27 16:59:28133 if (mailbox->IsZero()) {
134 DCHECK(!sync_token.HasData());
135 auto* sii = context_provider->SharedImageInterface();
Peng Huange3b8c1d2019-03-05 17:21:12136 uint32_t flags = gpu::SHARED_IMAGE_USAGE_DISPLAY |
137 gpu::SHARED_IMAGE_USAGE_RASTER |
Jonathan Backera1f3d7c2018-10-16 14:46:32138 gpu::SHARED_IMAGE_USAGE_OOP_RASTERIZATION;
Antoine Labour6283c792018-09-27 16:59:28139 if (texture_is_overlay_candidate)
140 flags |= gpu::SHARED_IMAGE_USAGE_SCANOUT;
141 *mailbox = sii->CreateSharedImage(resource_format, resource_size,
142 color_space, flags);
143 ri->WaitSyncTokenCHROMIUM(sii->GenUnverifiedSyncToken().GetConstData());
144 } else {
145 ri->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
146 }
147
Khushalec3ba5dc2019-11-04 22:30:21148 ri->BeginRasterCHROMIUM(
149 raster_source->background_color(), playback_settings.msaa_sample_count,
150 playback_settings.use_lcd_text, color_space, mailbox->name);
Adrienne Walkerbc5cd82e2017-10-31 21:58:15151 float recording_to_raster_scale =
152 transform.scale() / raster_source->recording_scale_factor();
Adrienne Walker51c8e382018-02-06 20:30:33153 gfx::Size content_size = raster_source->GetContentSize(transform.scale());
Khushalcd8fbb772018-10-16 22:46:14154
Adrienne Walker51c8e382018-02-06 20:30:33155 // TODO(enne): could skip the clear on new textures, as the service side has
156 // to do that anyway. resource_has_previous_content implies that the texture
157 // is not new, but the reverse does not hold, so more plumbing is needed.
Khushal6cabe802019-03-26 17:58:46158 ri->RasterCHROMIUM(
159 raster_source->GetDisplayItemList().get(),
160 playback_settings.image_provider, content_size, raster_full_rect,
161 playback_rect, transform.translation(), recording_to_raster_scale,
162 raster_source->requires_clear(),
163 const_cast<RasterSource*>(raster_source)->max_op_size_hint());
Victor Miura3a4ad4f82017-12-13 06:03:45164 ri->EndRasterCHROMIUM();
Adrienne Walker436a7752017-08-28 23:33:09165
Eric Karl247f09c2018-03-15 02:06:36166 // TODO(ericrk): Handle unpremultiply+dither for 4444 cases.
167 // https://crbug.com/789153
Adrienne Walker436a7752017-08-28 23:33:09168}
169
vmiuraf7c765c2016-12-03 21:02:32170static void RasterizeSource(
sunnyps5d6ff0d02016-06-28 00:40:11171 const RasterSource* raster_source,
172 bool resource_has_previous_content,
Antoine Labour6283c792018-09-27 16:59:28173 gpu::Mailbox* mailbox,
174 const gpu::SyncToken& sync_token,
danakjaf3170e2018-02-09 17:31:58175 GLenum texture_target,
176 bool texture_is_overlay_candidate,
danakjaf3170e2018-02-09 17:31:58177 const gfx::Size& resource_size,
178 viz::ResourceFormat resource_format,
179 const gfx::ColorSpace& color_space,
sunnyps5d6ff0d02016-06-28 00:40:11180 const gfx::Rect& raster_full_rect,
Adrienne Walker436a7752017-08-28 23:33:09181 const gfx::Rect& playback_rect,
trchen178ac912017-04-04 10:11:10182 const gfx::AxisTransform2d& transform,
vmiuraf7c765c2016-12-03 21:02:32183 const RasterSource::PlaybackSettings& playback_settings,
Victor Miura29b7ea3d2017-12-19 20:23:59184 viz::RasterContextProvider* context_provider,
Eric Karl247f09c2018-03-15 02:06:36185 bool unpremultiply_and_dither,
186 const gfx::Size& max_tile_size) {
Victor Miura29b7ea3d2017-12-19 20:23:59187 gpu::raster::RasterInterface* ri = context_provider->RasterInterface();
Antoine Labour6283c792018-09-27 16:59:28188 if (mailbox->IsZero()) {
189 auto* sii = context_provider->SharedImageInterface();
Peng Huange3b8c1d2019-03-05 17:21:12190 uint32_t flags = gpu::SHARED_IMAGE_USAGE_DISPLAY |
191 gpu::SHARED_IMAGE_USAGE_GLES2 |
Antoine Labour6283c792018-09-27 16:59:28192 gpu::SHARED_IMAGE_USAGE_GLES2_FRAMEBUFFER_HINT;
193 if (texture_is_overlay_candidate)
194 flags |= gpu::SHARED_IMAGE_USAGE_SCANOUT;
195 *mailbox = sii->CreateSharedImage(resource_format, resource_size,
196 color_space, flags);
197 ri->WaitSyncTokenCHROMIUM(sii->GenUnverifiedSyncToken().GetConstData());
198 } else {
199 // Wait on the SyncToken that was created on the compositor thread after
200 // making the mailbox. This ensures that the mailbox we consume here is
201 // valid by the time the consume command executes.
202 ri->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
203 }
Antoine Labouraeb4bd70e2019-01-16 02:33:28204 GLuint texture_id = ri->CreateAndConsumeForGpuRaster(mailbox->name);
Sunny Sachanandani0cb875e2017-08-15 02:54:52205 {
Justin Novosad60f840e2018-03-21 22:00:50206 ScopedGrContextAccess gr_context_access(context_provider);
danakj57baa772018-05-29 15:59:14207 base::Optional<viz::ClientResourceProvider::ScopedSkSurface> scoped_surface;
Eric Karl247f09c2018-03-15 02:06:36208 base::Optional<ScopedSkSurfaceForUnpremultiplyAndDither>
209 scoped_dither_surface;
210 SkSurface* surface;
Mike Reedf7074ff2019-03-14 14:28:16211 sk_sp<SkColorSpace> sk_color_space = color_space.ToSkColorSpace();
Eric Karl247f09c2018-03-15 02:06:36212 if (!unpremultiply_and_dither) {
Mike Reedf7074ff2019-03-14 14:28:16213 scoped_surface.emplace(context_provider->GrContext(), sk_color_space,
214 texture_id, texture_target, resource_size,
215 resource_format, playback_settings.use_lcd_text,
Khushalec3ba5dc2019-11-04 22:30:21216 playback_settings.msaa_sample_count);
Eric Karl247f09c2018-03-15 02:06:36217 surface = scoped_surface->surface();
218 } else {
219 scoped_dither_surface.emplace(
Mike Reedf7074ff2019-03-14 14:28:16220 context_provider, sk_color_space, playback_rect, raster_full_rect,
221 max_tile_size, texture_id, resource_size,
Khushalec3ba5dc2019-11-04 22:30:21222 playback_settings.use_lcd_text, playback_settings.msaa_sample_count);
Eric Karl247f09c2018-03-15 02:06:36223 surface = scoped_dither_surface->surface();
224 }
Sunny Sachanandani0cb875e2017-08-15 02:54:52225
226 // Allocating an SkSurface will fail after a lost context. Pretend we
227 // rasterized, as the contents of the resource don't matter anymore.
228 if (!surface) {
229 DLOG(ERROR) << "Failed to allocate raster surface";
230 return;
231 }
232
Sunny Sachanandani0cb875e2017-08-15 02:54:52233 SkCanvas* canvas = surface->getCanvas();
234
235 // As an optimization, inform Skia to discard when not doing partial raster.
236 if (raster_full_rect == playback_rect)
237 canvas->discard();
238
Adrienne Walker51c8e382018-02-06 20:30:33239 gfx::Size content_size = raster_source->GetContentSize(transform.scale());
Mike Reedf7074ff2019-03-14 14:28:16240 raster_source->PlaybackToCanvas(canvas, content_size, raster_full_rect,
241 playback_rect, transform,
danakjaf3170e2018-02-09 17:31:58242 playback_settings);
ccameron220942362017-02-06 20:29:19243 }
vmiuraf7c765c2016-12-03 21:02:32244
Antoine Labouraeb4bd70e2019-01-16 02:33:28245 ri->DeleteGpuRasterTexture(texture_id);
sunnyps5d6ff0d02016-06-28 00:40:11246}
reveman47560ab2014-09-18 19:39:21247
248} // namespace
vmiura78b69282015-02-14 00:01:17249
danakjaf3170e2018-02-09 17:31:58250// Subclass for InUsePoolResource that holds ownership of a gpu-rastered backing
251// and does cleanup of the backing when destroyed.
252class GpuRasterBufferProvider::GpuRasterBacking
253 : public ResourcePool::GpuBacking {
254 public:
255 ~GpuRasterBacking() override {
Antoine Labour6283c792018-09-27 16:59:28256 if (mailbox.IsZero())
257 return;
258 auto* sii = worker_context_provider->SharedImageInterface();
danakjaf3170e2018-02-09 17:31:58259 if (returned_sync_token.HasData())
Antoine Labour6283c792018-09-27 16:59:28260 sii->DestroySharedImage(returned_sync_token, mailbox);
261 else if (mailbox_sync_token.HasData())
262 sii->DestroySharedImage(mailbox_sync_token, mailbox);
danakjaf3170e2018-02-09 17:31:58263 }
264
Alexandr Ilin0443a8f2018-07-20 20:14:50265 void OnMemoryDump(
266 base::trace_event::ProcessMemoryDump* pmd,
267 const base::trace_event::MemoryAllocatorDumpGuid& buffer_dump_guid,
268 uint64_t tracing_process_id,
269 int importance) const override {
Antoine Labour6283c792018-09-27 16:59:28270 if (mailbox.IsZero())
Alexandr Ilin0443a8f2018-07-20 20:14:50271 return;
272
Antoine Labour6283c792018-09-27 16:59:28273 auto tracing_guid = gpu::GetSharedImageGUIDForTracing(mailbox);
274 pmd->CreateSharedGlobalAllocatorDump(tracing_guid);
275 pmd->AddOwnershipEdge(buffer_dump_guid, tracing_guid, importance);
danakjaf3170e2018-02-09 17:31:58276 }
danakjaf3170e2018-02-09 17:31:58277
Antoine Labour6283c792018-09-27 16:59:28278 // The ContextProvider used to clean up the mailbox
279 viz::RasterContextProvider* worker_context_provider = nullptr;
danakjaf3170e2018-02-09 17:31:58280};
281
sunnyps5d6ff0d02016-06-28 00:40:11282GpuRasterBufferProvider::RasterBufferImpl::RasterBufferImpl(
283 GpuRasterBufferProvider* client,
danakjaf3170e2018-02-09 17:31:58284 const ResourcePool::InUsePoolResource& in_use_resource,
285 GpuRasterBacking* backing,
sunnyps5d6ff0d02016-06-28 00:40:11286 bool resource_has_previous_content)
287 : client_(client),
danakjaf3170e2018-02-09 17:31:58288 backing_(backing),
289 resource_size_(in_use_resource.size()),
290 resource_format_(in_use_resource.format()),
291 color_space_(in_use_resource.color_space()),
292 resource_has_previous_content_(resource_has_previous_content),
Sunny Sachanandani2f7fef22018-05-04 01:48:37293 before_raster_sync_token_(backing->returned_sync_token),
danakjaf3170e2018-02-09 17:31:58294 texture_target_(backing->texture_target),
295 texture_is_overlay_candidate_(backing->overlay_candidate),
Andres Calderon Jaramillo01d329552019-08-23 17:14:30296 mailbox_(backing->mailbox) {
297#if defined(OS_CHROMEOS)
298 // Only do this in Chrome OS with OOP-R because:
299 // 1) We will use this timestamp to measure raster scheduling delay and we
300 // only need to collect that data to assess the impact of hardware
301 // acceleration of image decodes which works only on Chrome OS with
302 // OOP-R.
303 // 2) We use CLOCK_MONOTONIC in that OS to get timestamps, so we can assert
304 // certain assumptions.
305 if (client_->enable_oop_rasterization_)
306 creation_time_ = base::TimeTicks::Now();
307#endif
308}
sunnyps5d6ff0d02016-06-28 00:40:11309
310GpuRasterBufferProvider::RasterBufferImpl::~RasterBufferImpl() {
danakjaf3170e2018-02-09 17:31:58311 // This SyncToken was created on the worker context after rastering the
312 // texture content.
313 backing_->mailbox_sync_token = after_raster_sync_token_;
314 if (after_raster_sync_token_.HasData()) {
315 // The returned SyncToken was waited on in Playback. We know Playback
316 // happened if the |after_raster_sync_token_| was set.
317 backing_->returned_sync_token = gpu::SyncToken();
318 }
Antoine Labour6283c792018-09-27 16:59:28319 backing_->mailbox = mailbox_;
sunnyps5d6ff0d02016-06-28 00:40:11320}
321
322void GpuRasterBufferProvider::RasterBufferImpl::Playback(
323 const RasterSource* raster_source,
324 const gfx::Rect& raster_full_rect,
325 const gfx::Rect& raster_dirty_rect,
326 uint64_t new_content_id,
trchen178ac912017-04-04 10:11:10327 const gfx::AxisTransform2d& transform,
Khushal49836ab2018-07-25 02:08:45328 const RasterSource::PlaybackSettings& playback_settings,
329 const GURL& url) {
sunnyps5d6ff0d02016-06-28 00:40:11330 TRACE_EVENT0("cc", "GpuRasterBuffer::Playback");
danakjd1dd03152018-02-23 18:13:46331 // The |before_raster_sync_token_| passed in here was created on the
332 // compositor thread, or given back with the texture for reuse. This call
333 // returns another SyncToken generated on the worker thread to synchronize
334 // with after the raster is complete.
danakjaf3170e2018-02-09 17:31:58335 after_raster_sync_token_ = client_->PlaybackOnWorkerThread(
Antoine Labour6283c792018-09-27 16:59:28336 &mailbox_, texture_target_, texture_is_overlay_candidate_,
337 before_raster_sync_token_, resource_size_, resource_format_, color_space_,
338 resource_has_previous_content_, raster_source, raster_full_rect,
Andres Calderon Jaramillo01d329552019-08-23 17:14:30339 raster_dirty_rect, new_content_id, transform, playback_settings, url,
340 creation_time_);
sunnyps5d6ff0d02016-06-28 00:40:11341}
342
prashant.nb4d4f492016-04-29 12:51:28343GpuRasterBufferProvider::GpuRasterBufferProvider(
Xu Xing32549162017-07-17 22:25:43344 viz::ContextProvider* compositor_context_provider,
Victor Miura29b7ea3d2017-12-19 20:23:59345 viz::RasterContextProvider* worker_context_provider,
danakjaf3170e2018-02-09 17:31:58346 bool use_gpu_memory_buffer_resources,
danakja32578c2018-04-25 21:18:36347 viz::ResourceFormat tile_format,
Eric Karl247f09c2018-03-15 02:06:36348 const gfx::Size& max_tile_size,
Eric Karla6ff8862018-04-16 20:21:06349 bool unpremultiply_and_dither_low_bit_depth_tiles,
Khushale898b992018-10-19 22:25:16350 bool enable_oop_rasterization,
Aaron Krajeski6392a86f2019-07-18 13:40:47351 float raster_metric_probability)
danakj0de0c95a2016-05-25 01:42:49352 : compositor_context_provider_(compositor_context_provider),
sunnyps5d6ff0d02016-06-28 00:40:11353 worker_context_provider_(worker_context_provider),
danakjaf3170e2018-02-09 17:31:58354 use_gpu_memory_buffer_resources_(use_gpu_memory_buffer_resources),
danakja32578c2018-04-25 21:18:36355 tile_format_(tile_format),
Eric Karl247f09c2018-03-15 02:06:36356 max_tile_size_(max_tile_size),
Eric Karla6ff8862018-04-16 20:21:06357 unpremultiply_and_dither_low_bit_depth_tiles_(
358 unpremultiply_and_dither_low_bit_depth_tiles),
Khushale898b992018-10-19 22:25:16359 enable_oop_rasterization_(enable_oop_rasterization),
Aaron Krajeski6392a86f2019-07-18 13:40:47360 random_generator_((uint32_t)base::RandUint64()),
361 bernoulli_distribution_(raster_metric_probability) {
sunnyps5d6ff0d02016-06-28 00:40:11362 DCHECK(compositor_context_provider);
363 DCHECK(worker_context_provider);
danakj0de0c95a2016-05-25 01:42:49364}
reveman@chromium.orgb5641b92014-02-15 14:21:58365
sunnyps5d6ff0d02016-06-28 00:40:11366GpuRasterBufferProvider::~GpuRasterBufferProvider() {
sunnyps5d6ff0d02016-06-28 00:40:11367}
reveman@chromium.orgb5641b92014-02-15 14:21:58368
prashant.nb4d4f492016-04-29 12:51:28369std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster(
danakj4e871d82018-01-18 21:56:57370 const ResourcePool::InUsePoolResource& resource,
danakj510822aa2015-06-01 20:23:02371 uint64_t resource_content_id,
danakje0b54782015-05-29 22:47:52372 uint64_t previous_content_id) {
danakjaf3170e2018-02-09 17:31:58373 if (!resource.gpu_backing()) {
374 auto backing = std::make_unique<GpuRasterBacking>();
Antoine Labour6283c792018-09-27 16:59:28375 backing->worker_context_provider = worker_context_provider_;
Jonathan Backere29d0232018-10-01 21:59:15376 backing->InitOverlayCandidateAndTextureTarget(
377 resource.format(), compositor_context_provider_->ContextCapabilities(),
378 use_gpu_memory_buffer_resources_);
danakjaf3170e2018-02-09 17:31:58379 resource.set_gpu_backing(std::move(backing));
380 }
381 GpuRasterBacking* backing =
382 static_cast<GpuRasterBacking*>(resource.gpu_backing());
sunnyps5d6ff0d02016-06-28 00:40:11383 bool resource_has_previous_content =
384 resource_content_id && resource_content_id == previous_content_id;
danakjaf3170e2018-02-09 17:31:58385 return std::make_unique<RasterBufferImpl>(this, resource, backing,
danakj4e871d82018-01-18 21:56:57386 resource_has_previous_content);
reveman@chromium.orgb5641b92014-02-15 14:21:58387}
388
Sunny Sachanandani5f5419e22017-05-12 20:35:30389void GpuRasterBufferProvider::Flush() {
Victor Miuraff6488612017-12-21 04:16:15390 compositor_context_provider_->ContextSupport()->FlushPendingWork();
Sunny Sachanandani5f5419e22017-05-12 20:35:30391}
392
danakja32578c2018-04-25 21:18:36393viz::ResourceFormat GpuRasterBufferProvider::GetResourceFormat() const {
394 return tile_format_;
prashant.nb4d4f492016-04-29 12:51:28395}
396
danakja32578c2018-04-25 21:18:36397bool GpuRasterBufferProvider::IsResourcePremultiplied() const {
398 return !ShouldUnpremultiplyAndDitherResource(GetResourceFormat());
Eric Karl247f09c2018-03-15 02:06:36399}
400
ericrk7f6a27f2017-01-31 22:34:32401bool GpuRasterBufferProvider::IsResourceReadyToDraw(
danakj4e871d82018-01-18 21:56:57402 const ResourcePool::InUsePoolResource& resource) const {
danakjaf3170e2018-02-09 17:31:58403 const gpu::SyncToken& sync_token = resource.gpu_backing()->mailbox_sync_token;
404 // This SyncToken() should have been set by calling OrderingBarrier() before
405 // calling this.
406 DCHECK(sync_token.HasData());
ericrk7f6a27f2017-01-31 22:34:32407
sunnyps74996292017-03-15 02:35:48408 // IsSyncTokenSignaled is thread-safe, no need for worker context lock.
409 return worker_context_provider_->ContextSupport()->IsSyncTokenSignaled(
ericrk7f6a27f2017-01-31 22:34:32410 sync_token);
411}
412
Khushalec3ba5dc2019-11-04 22:30:21413bool GpuRasterBufferProvider::CanPartialRasterIntoProvidedResource() const {
414 return true;
415}
416
ericrk7f6a27f2017-01-31 22:34:32417uint64_t GpuRasterBufferProvider::SetReadyToDrawCallback(
danakj4e871d82018-01-18 21:56:57418 const std::vector<const ResourcePool::InUsePoolResource*>& resources,
kylechar4bb144d2019-01-11 20:42:07419 base::OnceClosure callback,
ericrk7f6a27f2017-01-31 22:34:32420 uint64_t pending_callback_id) const {
danakjaf3170e2018-02-09 17:31:58421 gpu::SyncToken latest_sync_token;
422 for (const auto* in_use : resources) {
423 const gpu::SyncToken& sync_token =
424 in_use->gpu_backing()->mailbox_sync_token;
425 if (sync_token.release_count() > latest_sync_token.release_count())
426 latest_sync_token = sync_token;
427 }
428 uint64_t callback_id = latest_sync_token.release_count();
ericrk7f6a27f2017-01-31 22:34:32429 DCHECK_NE(callback_id, 0u);
430
431 // If the callback is different from the one the caller is already waiting on,
sunnyps31c92fe2017-02-10 23:46:55432 // pass the callback through to SignalSyncToken. Otherwise the request is
ericrk7f6a27f2017-01-31 22:34:32433 // redundant.
434 if (callback_id != pending_callback_id) {
danakjaf3170e2018-02-09 17:31:58435 // Use the compositor context because we want this callback on the
436 // compositor thread.
437 compositor_context_provider_->ContextSupport()->SignalSyncToken(
kylechar4bb144d2019-01-11 20:42:07438 latest_sync_token, std::move(callback));
ericrk7f6a27f2017-01-31 22:34:32439 }
440
441 return callback_id;
442}
443
sunnyps5d6ff0d02016-06-28 00:40:11444void GpuRasterBufferProvider::Shutdown() {
sunnyps5d6ff0d02016-06-28 00:40:11445}
446
danakjaf3170e2018-02-09 17:31:58447gpu::SyncToken GpuRasterBufferProvider::PlaybackOnWorkerThread(
Antoine Labour6283c792018-09-27 16:59:28448 gpu::Mailbox* mailbox,
danakjaf3170e2018-02-09 17:31:58449 GLenum texture_target,
450 bool texture_is_overlay_candidate,
sunnyps5d6ff0d02016-06-28 00:40:11451 const gpu::SyncToken& sync_token,
danakjaf3170e2018-02-09 17:31:58452 const gfx::Size& resource_size,
453 viz::ResourceFormat resource_format,
454 const gfx::ColorSpace& color_space,
sunnyps5d6ff0d02016-06-28 00:40:11455 bool resource_has_previous_content,
456 const RasterSource* raster_source,
457 const gfx::Rect& raster_full_rect,
458 const gfx::Rect& raster_dirty_rect,
459 uint64_t new_content_id,
trchen178ac912017-04-04 10:11:10460 const gfx::AxisTransform2d& transform,
Khushal49836ab2018-07-25 02:08:45461 const RasterSource::PlaybackSettings& playback_settings,
Andres Calderon Jaramillo01d329552019-08-23 17:14:30462 const GURL& url,
463 base::TimeTicks raster_buffer_creation_time) {
Khushal5d4e0962018-10-18 18:04:07464 PendingRasterQuery query;
465 gpu::SyncToken raster_finished_token = PlaybackOnWorkerThreadInternal(
466 mailbox, texture_target, texture_is_overlay_candidate, sync_token,
467 resource_size, resource_format, color_space,
468 resource_has_previous_content, raster_source, raster_full_rect,
469 raster_dirty_rect, new_content_id, transform, playback_settings, url,
470 &query);
471
Andres Calderon Jaramillo01d329552019-08-23 17:14:30472 if (query.raster_duration_query_id) {
473 if (query.raster_start_query_id)
474 query.raster_buffer_creation_time = raster_buffer_creation_time;
475
Khushal5d4e0962018-10-18 18:04:07476 // Note that it is important to scope the raster context lock to
477 // PlaybackOnWorkerThreadInternal and release it before acquiring this lock
478 // to avoid a deadlock in CheckRasterFinishedQueries which acquires the
479 // raster context lock while holding this lock.
480 base::AutoLock hold(pending_raster_queries_lock_);
481 pending_raster_queries_.push_back(query);
482 }
Andres Calderon Jaramillo01d329552019-08-23 17:14:30483 DCHECK(!query.raster_start_query_id || query.raster_duration_query_id);
Khushal5d4e0962018-10-18 18:04:07484
485 return raster_finished_token;
486}
487
488gpu::SyncToken GpuRasterBufferProvider::PlaybackOnWorkerThreadInternal(
489 gpu::Mailbox* mailbox,
490 GLenum texture_target,
491 bool texture_is_overlay_candidate,
492 const gpu::SyncToken& sync_token,
493 const gfx::Size& resource_size,
494 viz::ResourceFormat resource_format,
495 const gfx::ColorSpace& color_space,
496 bool resource_has_previous_content,
497 const RasterSource* raster_source,
498 const gfx::Rect& raster_full_rect,
499 const gfx::Rect& raster_dirty_rect,
500 uint64_t new_content_id,
501 const gfx::AxisTransform2d& transform,
502 const RasterSource::PlaybackSettings& playback_settings,
503 const GURL& url,
504 PendingRasterQuery* query) {
Victor Miura29b7ea3d2017-12-19 20:23:59505 viz::RasterContextProvider::ScopedRasterContextLock scoped_context(
Khushal49836ab2018-07-25 02:08:45506 worker_context_provider_, url.possibly_invalid_spec().c_str());
Victor Miura29b7ea3d2017-12-19 20:23:59507 gpu::raster::RasterInterface* ri = scoped_context.RasterInterface();
Victor Miura3a4ad4f82017-12-13 06:03:45508 DCHECK(ri);
sunnyps5d6ff0d02016-06-28 00:40:11509
Aaron Krajeski6392a86f2019-07-18 13:40:47510 const bool measure_raster_metric = bernoulli_distribution_(random_generator_);
Khushale898b992018-10-19 22:25:16511
Adrienne Walker436a7752017-08-28 23:33:09512 gfx::Rect playback_rect = raster_full_rect;
513 if (resource_has_previous_content) {
514 playback_rect.Intersect(raster_dirty_rect);
515 }
516 DCHECK(!playback_rect.IsEmpty())
517 << "Why are we rastering a tile that's not dirty?";
518
519 // Log a histogram of the percentage of pixels that were saved due to
520 // partial raster.
521 const char* client_name = GetClientNameForMetrics();
522 float full_rect_size = raster_full_rect.size().GetArea();
523 if (full_rect_size > 0 && client_name) {
524 float fraction_partial_rastered =
525 static_cast<float>(playback_rect.size().GetArea()) / full_rect_size;
526 float fraction_saved = 1.0f - fraction_partial_rastered;
527 UMA_HISTOGRAM_PERCENTAGE(
528 base::StringPrintf("Renderer4.%s.PartialRasterPercentageSaved.Gpu",
529 client_name),
530 100.0f * fraction_saved);
531 }
532
Khushale898b992018-10-19 22:25:16533 if (measure_raster_metric) {
Andres Calderon Jaramillo01d329552019-08-23 17:14:30534#if defined(OS_CHROMEOS)
535 // Use a query to detect when the GPU side is ready to start issuing raster
536 // work to the driver. We will use the resulting timestamp to measure raster
537 // scheduling delay. We only care about this in Chrome OS and when OOP-R is
538 // enabled because we will use this timestamp to measure raster scheduling
539 // delay and we only need to collect that data to assess the impact of
540 // hardware acceleration of image decodes which work only in Chrome OS with
541 // OOP-R.
542 if (enable_oop_rasterization_) {
543 ri->GenQueriesEXT(1, &query->raster_start_query_id);
544 DCHECK_GT(query->raster_start_query_id, 0u);
545 ri->QueryCounterEXT(query->raster_start_query_id,
546 GL_COMMANDS_ISSUED_TIMESTAMP_CHROMIUM);
547 }
548#endif
Andres Calderon Jaramillob805fa82019-08-24 05:58:19549
550 // Use a query to time the GPU side work for rasterizing this tile.
551 ri->GenQueriesEXT(1, &query->raster_duration_query_id);
552 DCHECK_GT(query->raster_duration_query_id, 0u);
553 ri->BeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM,
554 query->raster_duration_query_id);
Khushale898b992018-10-19 22:25:16555 }
Khushalcd8fbb772018-10-16 22:46:14556
557 {
Khushale898b992018-10-19 22:25:16558 base::Optional<base::ElapsedTimer> timer;
559 if (measure_raster_metric)
560 timer.emplace();
Khushalcd8fbb772018-10-16 22:46:14561 if (enable_oop_rasterization_) {
Khushalec3ba5dc2019-11-04 22:30:21562 RasterizeSourceOOP(
563 raster_source, resource_has_previous_content, mailbox, sync_token,
564 texture_target, texture_is_overlay_candidate, resource_size,
565 resource_format, color_space, raster_full_rect, playback_rect,
566 transform, playback_settings, worker_context_provider_);
Khushalcd8fbb772018-10-16 22:46:14567 } else {
568 RasterizeSource(raster_source, resource_has_previous_content, mailbox,
569 sync_token, texture_target, texture_is_overlay_candidate,
570 resource_size, resource_format, color_space,
571 raster_full_rect, playback_rect, transform,
572 playback_settings, worker_context_provider_,
Khushalcd8fbb772018-10-16 22:46:14573 ShouldUnpremultiplyAndDitherResource(resource_format),
574 max_tile_size_);
575 }
Andres Calderon Jaramillo01d329552019-08-23 17:14:30576 if (measure_raster_metric) {
577 query->worker_raster_duration = timer->Elapsed();
578 ri->EndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
579 }
Adrienne Walker436a7752017-08-28 23:33:09580 }
sunnyps5d6ff0d02016-06-28 00:40:11581
Sunny Sachanandani0cb875e2017-08-15 02:54:52582 // Generate sync token for cross context synchronization.
danakj57baa772018-05-29 15:59:14583 return viz::ClientResourceProvider::GenerateSyncTokenHelper(ri);
sunnyps5d6ff0d02016-06-28 00:40:11584}
prashant.nb4d4f492016-04-29 12:51:28585
Eric Karla6ff8862018-04-16 20:21:06586bool GpuRasterBufferProvider::ShouldUnpremultiplyAndDitherResource(
587 viz::ResourceFormat format) const {
588 switch (format) {
589 case viz::RGBA_4444:
590 return unpremultiply_and_dither_low_bit_depth_tiles_;
591 default:
592 return false;
593 }
594}
595
Khushalcd8fbb772018-10-16 22:46:14596#define UMA_HISTOGRAM_RASTER_TIME_CUSTOM_MICROSECONDS(name, total_time) \
597 UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES( \
598 name, total_time, base::TimeDelta::FromMicroseconds(1), \
599 base::TimeDelta::FromMilliseconds(100), 100);
600
601bool GpuRasterBufferProvider::CheckRasterFinishedQueries() {
Khushal5d4e0962018-10-18 18:04:07602 base::AutoLock hold(pending_raster_queries_lock_);
603 if (pending_raster_queries_.empty())
604 return false;
605
Khushalcd8fbb772018-10-16 22:46:14606 viz::RasterContextProvider::ScopedRasterContextLock scoped_context(
607 worker_context_provider_);
608 auto* ri = scoped_context.RasterInterface();
609
610 auto it = pending_raster_queries_.begin();
611 while (it != pending_raster_queries_.end()) {
Andres Calderon Jaramillo01d329552019-08-23 17:14:30612 GLuint complete = 0;
613 ri->GetQueryObjectuivEXT(it->raster_duration_query_id,
Khushalcd8fbb772018-10-16 22:46:14614 GL_QUERY_RESULT_AVAILABLE_NO_FLUSH_CHROMIUM_EXT,
615 &complete);
616 if (!complete)
617 break;
618
Andres Calderon Jaramillo01d329552019-08-23 17:14:30619#if DCHECK_IS_ON()
620 if (it->raster_start_query_id) {
621 // We issued the GL_COMMANDS_ISSUED_TIMESTAMP_CHROMIUM query prior to the
622 // GL_COMMANDS_ISSUED_CHROMIUM query. Therefore, if the result of the
623 // latter is available, the result of the former should be too.
624 complete = 0;
625 ri->GetQueryObjectuivEXT(it->raster_start_query_id,
626 GL_QUERY_RESULT_AVAILABLE_NO_FLUSH_CHROMIUM_EXT,
627 &complete);
628 DCHECK(complete);
629 }
630#endif
Khushalcd8fbb772018-10-16 22:46:14631
Andres Calderon Jaramillo01d329552019-08-23 17:14:30632 GLuint gpu_raster_duration = 0u;
633 ri->GetQueryObjectuivEXT(it->raster_duration_query_id, GL_QUERY_RESULT_EXT,
634 &gpu_raster_duration);
635 ri->DeleteQueriesEXT(1, &it->raster_duration_query_id);
636
637 base::TimeDelta raster_duration =
638 it->worker_raster_duration +
639 base::TimeDelta::FromMicroseconds(gpu_raster_duration);
Khushalcd8fbb772018-10-16 22:46:14640
641 // It is safe to use the UMA macros here with runtime generated strings
642 // because the client name should be initialized once in the process, before
643 // recording any metrics here.
644 const char* client_name = GetClientNameForMetrics();
Andres Calderon Jaramillo01d329552019-08-23 17:14:30645
646 if (it->raster_start_query_id) {
647 GLuint64 gpu_raster_start_time = 0u;
648 ri->GetQueryObjectui64vEXT(it->raster_start_query_id, GL_QUERY_RESULT_EXT,
649 &gpu_raster_start_time);
650 ri->DeleteQueriesEXT(1, &it->raster_start_query_id);
651
652 // The base::checked_cast<int64_t> should not crash as long as the GPU
653 // process was not compromised: that's because the result of the query
654 // should have been generated using base::TimeDelta::InMicroseconds()
655 // there, so the result should fit in an int64_t.
656 base::TimeDelta raster_scheduling_delay =
657 base::TimeDelta::FromMicroseconds(
658 base::checked_cast<int64_t>(gpu_raster_start_time)) -
659 it->raster_buffer_creation_time.since_origin();
660
661 // We expect the clock we're using to be monotonic, so we shouldn't get a
662 // negative scheduling delay.
663 DCHECK_GE(raster_scheduling_delay.InMicroseconds(), 0u);
664 UMA_HISTOGRAM_RASTER_TIME_CUSTOM_MICROSECONDS(
665 base::StringPrintf("Renderer4.%s.RasterTaskSchedulingDelay.All",
666 client_name),
667 raster_scheduling_delay);
668 }
669
Khushalcd8fbb772018-10-16 22:46:14670 if (enable_oop_rasterization_) {
671 UMA_HISTOGRAM_RASTER_TIME_CUSTOM_MICROSECONDS(
672 base::StringPrintf("Renderer4.%s.RasterTaskTotalDuration.Oop",
673 client_name),
Andres Calderon Jaramillo01d329552019-08-23 17:14:30674 raster_duration);
Khushalcd8fbb772018-10-16 22:46:14675 } else {
676 UMA_HISTOGRAM_RASTER_TIME_CUSTOM_MICROSECONDS(
677 base::StringPrintf("Renderer4.%s.RasterTaskTotalDuration.Gpu",
678 client_name),
Andres Calderon Jaramillo01d329552019-08-23 17:14:30679 raster_duration);
Khushalcd8fbb772018-10-16 22:46:14680 }
681
682 it = pending_raster_queries_.erase(it);
683 }
684
685 return pending_raster_queries_.size() > 0u;
686}
687
reveman@chromium.orgb5641b92014-02-15 14:21:58688} // namespace cc