[go: nahoru, domu]

blob: 08caa3dc7207b3c0749380038809942d7e9f69da [file] [log] [blame]
Avi Drissman3f7a9d82022-09-08 20:55:421// Copyright 2013 The Chromium Authors
prashant.nb4d4f492016-04-29 12:51:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/raster/zero_copy_raster_buffer_provider.h"
6
7#include <stdint.h>
8
9#include <algorithm>
Chris Blumed4354332020-11-11 08:40:1610#include <utility>
prashant.nb4d4f492016-04-29 12:51:2811
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Antoine Laboure5a2101a2018-11-07 23:36:2713#include "base/trace_event/process_memory_dump.h"
prashant.nb4d4f492016-04-29 12:51:2814#include "base/trace_event/trace_event.h"
David 'Digit' Turner59a87482018-10-29 13:27:1615#include "base/trace_event/traced_value.h"
danakjbd636052018-02-06 18:28:4916#include "cc/resources/resource_pool.h"
danakj57baa772018-05-29 15:59:1417#include "components/viz/client/client_resource_provider.h"
kylecharffcf0912023-06-09 18:57:2218#include "components/viz/common/gpu/raster_context_provider.h"
Fady Samuel555c8d12017-07-07 23:14:0919#include "components/viz/common/resources/platform_color.h"
Colin Blundell81fb20ca2023-05-30 09:37:5420#include "components/viz/common/resources/shared_image_format_utils.h"
Mingjing Zhang3f1265c2023-10-26 02:16:5521#include "gpu/command_buffer/client/client_shared_image.h"
Antoine Laboure5a2101a2018-11-07 23:36:2722#include "gpu/command_buffer/client/shared_image_interface.h"
Antoine Laboure5a2101a2018-11-07 23:36:2723#include "gpu/command_buffer/common/shared_image_trace_utils.h"
24#include "gpu/command_buffer/common/shared_image_usage.h"
prashant.nb4d4f492016-04-29 12:51:2825#include "ui/gfx/buffer_format_util.h"
prashant.nb4d4f492016-04-29 12:51:2826
27namespace cc {
28namespace {
29
danakjbd636052018-02-06 18:28:4930constexpr static auto kBufferUsage = gfx::BufferUsage::GPU_READ_CPU_READ_WRITE;
31
32// Subclass for InUsePoolResource that holds ownership of a zero-copy backing
33// and does cleanup of the backing when destroyed.
34class ZeroCopyGpuBacking : public ResourcePool::GpuBacking {
35 public:
36 ~ZeroCopyGpuBacking() override {
Mingjing Zhang9a99ee42023-11-22 16:23:1937 if (!shared_image) {
Antoine Laboure5a2101a2018-11-07 23:36:2738 return;
luci-bisection@appspot.gserviceaccount.come9ab19e2023-11-10 17:28:3139 }
danakjbd636052018-02-06 18:28:4940 if (returned_sync_token.HasData())
Mingjing Zhang9a99ee42023-11-22 16:23:1941 shared_image_interface->DestroySharedImage(returned_sync_token,
42 std::move(shared_image));
Antoine Laboure5a2101a2018-11-07 23:36:2743 else if (mailbox_sync_token.HasData())
Mingjing Zhang9a99ee42023-11-22 16:23:1944 shared_image_interface->DestroySharedImage(mailbox_sync_token,
45 std::move(shared_image));
danakjbd636052018-02-06 18:28:4946 }
47
Alexandr Ilin0443a8f2018-07-20 20:14:5048 void OnMemoryDump(
49 base::trace_event::ProcessMemoryDump* pmd,
50 const base::trace_event::MemoryAllocatorDumpGuid& buffer_dump_guid,
51 uint64_t tracing_process_id,
52 int importance) const override {
Colin Blundell0fd5a822023-12-08 17:31:4753 if (!shared_image) {
54 return;
Colin Blundell42819692023-09-25 15:24:2855 }
Colin Blundell0fd5a822023-12-08 17:31:4756 auto mapping = shared_image->Map();
57 if (!mapping) {
58 return;
59 }
60 mapping->OnMemoryDump(pmd, buffer_dump_guid, tracing_process_id,
61 importance);
danakjbd636052018-02-06 18:28:4962 }
63
Antoine Laboure5a2101a2018-11-07 23:36:2764 // The SharedImageInterface used to clean up the shared image.
Keishi Hattori0e45c022021-11-27 09:25:5265 raw_ptr<gpu::SharedImageInterface> shared_image_interface = nullptr;
danakjbd636052018-02-06 18:28:4966};
67
68// RasterBuffer for the zero copy upload, which is given to the raster worker
69// threads for raster/upload.
Daniel Bratell1bddcb332017-11-21 11:08:2970class ZeroCopyRasterBufferImpl : public RasterBuffer {
prashant.nb4d4f492016-04-29 12:51:2871 public:
danakj4e871d82018-01-18 21:56:5772 ZeroCopyRasterBufferImpl(
John Abd-El-Malek61fd3e12021-04-29 19:01:3073 base::WaitableEvent* shutdown_event,
danakjbd636052018-02-06 18:28:4974 const ResourcePool::InUsePoolResource& in_use_resource,
75 ZeroCopyGpuBacking* backing)
76 : backing_(backing),
John Abd-El-Malek61fd3e12021-04-29 19:01:3077 shutdown_event_(shutdown_event),
danakj4e871d82018-01-18 21:56:5778 resource_size_(in_use_resource.size()),
Colin Blundell1190bd92023-03-21 11:49:0579 format_(in_use_resource.format()),
Colin Blundell0fd5a822023-12-08 17:31:4780 resource_color_space_(in_use_resource.color_space()) {}
Vladimir Levinf06d1cd72019-03-13 18:24:1081 ZeroCopyRasterBufferImpl(const ZeroCopyRasterBufferImpl&) = delete;
danakjbd636052018-02-06 18:28:4982
83 ~ZeroCopyRasterBufferImpl() override {
Colin Blundell0fd5a822023-12-08 17:31:4784 // If MappableSharedImage allocation failed (https://crbug.com/554541), then
85 // we don't have anything to give to the display compositor, so we report a
86 // zero mailbox that will result in checkerboarding.
87 if (!backing_->shared_image) {
88 return;
Antoine Labour6363d8202018-10-29 22:36:0189 }
90
danakjbd636052018-02-06 18:28:4991 // This is destroyed on the compositor thread when raster is complete, but
92 // before the backing is prepared for export to the display compositor. So
93 // we can set up the texture and SyncToken here.
94 // TODO(danakj): This could be done with the worker context in Playback. Do
95 // we need to do things in IsResourceReadyToDraw() and OrderingBarrier then?
Antoine Laboure5a2101a2018-11-07 23:36:2796 gpu::SharedImageInterface* sii = backing_->shared_image_interface;
Colin Blundell0fd5a822023-12-08 17:31:4797 sii->UpdateSharedImage(backing_->returned_sync_token,
98 backing_->shared_image->mailbox());
danakjbd636052018-02-06 18:28:4999
Antoine Laboure5a2101a2018-11-07 23:36:27100 backing_->mailbox_sync_token = sii->GenUnverifiedSyncToken();
danakjbd636052018-02-06 18:28:49101 }
prashant.nb4d4f492016-04-29 12:51:28102
Vladimir Levinf06d1cd72019-03-13 18:24:10103 ZeroCopyRasterBufferImpl& operator=(const ZeroCopyRasterBufferImpl&) = delete;
104
prashant.nb4d4f492016-04-29 12:51:28105 // Overridden from RasterBuffer:
Khushal49836ab2018-07-25 02:08:45106 void Playback(const RasterSource* raster_source,
107 const gfx::Rect& raster_full_rect,
108 const gfx::Rect& raster_dirty_rect,
109 uint64_t new_content_id,
110 const gfx::AxisTransform2d& transform,
111 const RasterSource::PlaybackSettings& playback_settings,
112 const GURL& url) override {
prashant.n60e135b02016-06-08 04:12:23113 TRACE_EVENT0("cc", "ZeroCopyRasterBuffer::Playback");
prashant.nb4d4f492016-04-29 12:51:28114
Colin Blundell0fd5a822023-12-08 17:31:47115 gpu::SharedImageInterface* sii = backing_->shared_image_interface;
danakjbd636052018-02-06 18:28:49116
Colin Blundell0fd5a822023-12-08 17:31:47117 // Create a MappableSI if necessary.
118 if (!backing_->shared_image) {
119 uint32_t usage = gpu::SHARED_IMAGE_USAGE_DISPLAY_READ |
120 gpu::SHARED_IMAGE_USAGE_SCANOUT;
121 backing_->shared_image = sii->CreateSharedImage(
122 format_, resource_size_, resource_color_space_,
123 kTopLeft_GrSurfaceOrigin, kPremul_SkAlphaType, usage,
124 "ZeroCopyRasterTile", gpu::kNullSurfaceHandle, kBufferUsage);
Mingjing Zhang9a99ee42023-11-22 16:23:19125 if (!backing_->shared_image) {
Colin Blundell0fd5a822023-12-08 17:31:47126 LOG(ERROR) << "Creation of MappableSharedImage failed.";
Colin Blundell42819692023-09-25 15:24:28127 return;
128 }
Colin Blundell0fd5a822023-12-08 17:31:47129 }
Colin Blundell42819692023-09-25 15:24:28130
Colin Blundell0fd5a822023-12-08 17:31:47131 std::unique_ptr<gpu::ClientSharedImage::ScopedMapping> mapping =
132 backing_->shared_image->Map();
133 if (!mapping) {
134 LOG(ERROR) << "MapSharedImage Failed.";
135 sii->DestroySharedImage(gpu::SyncToken(),
136 std::move(backing_->shared_image));
137 return;
Colin Blundell42819692023-09-25 15:24:28138 }
prashant.nb4d4f492016-04-29 12:51:28139
140 // TODO(danakj): Implement partial raster with raster_dirty_rect.
141 RasterBufferProvider::PlaybackToMemory(
Colin Blundell0fd5a822023-12-08 17:31:47142 mapping->Memory(0), format_, resource_size_, mapping->Stride(0),
143 raster_source, raster_full_rect, raster_full_rect, transform,
144 resource_color_space_,
danakja32578c2018-04-25 21:18:36145 /*gpu_compositing=*/true, playback_settings);
prashant.nb4d4f492016-04-29 12:51:28146 }
147
Francois Dorayaffe0912020-06-30 20:29:21148 bool SupportsBackgroundThreadPriority() const override { return true; }
149
prashant.nb4d4f492016-04-29 12:51:28150 private:
danakjbd636052018-02-06 18:28:49151 // This field may only be used on the compositor thread.
Keishi Hattori0e45c022021-11-27 09:25:52152 raw_ptr<ZeroCopyGpuBacking> backing_;
danakjbd636052018-02-06 18:28:49153
154 // These fields are for use on the worker thread.
Keishi Hattori0e45c022021-11-27 09:25:52155 raw_ptr<base::WaitableEvent> shutdown_event_;
danakj4e871d82018-01-18 21:56:57156 gfx::Size resource_size_;
Colin Blundell04316232023-03-16 08:14:15157 viz::SharedImageFormat format_;
danakjbd636052018-02-06 18:28:49158 gfx::ColorSpace resource_color_space_;
prashant.nb4d4f492016-04-29 12:51:28159};
160
161} // namespace
162
prashant.nb4d4f492016-04-29 12:51:28163ZeroCopyRasterBufferProvider::ZeroCopyRasterBufferProvider(
kylecharffcf0912023-06-09 18:57:22164 viz::RasterContextProvider* compositor_context_provider,
kylechar8cce4942023-06-20 18:03:03165 const RasterCapabilities& raster_caps)
Colin Blundell0fd5a822023-12-08 17:31:47166 : compositor_context_provider_(compositor_context_provider),
kylechar58c09f72023-07-11 21:27:34167 tile_format_(raster_caps.tile_format),
168 tile_texture_target_(raster_caps.tile_texture_target) {}
prashant.nb4d4f492016-04-29 12:51:28169
Chris Watkinsf6353292017-12-04 02:36:05170ZeroCopyRasterBufferProvider::~ZeroCopyRasterBufferProvider() = default;
prashant.nb4d4f492016-04-29 12:51:28171
172std::unique_ptr<RasterBuffer>
173ZeroCopyRasterBufferProvider::AcquireBufferForRaster(
danakj4e871d82018-01-18 21:56:57174 const ResourcePool::InUsePoolResource& resource,
prashant.nb4d4f492016-04-29 12:51:28175 uint64_t resource_content_id,
Andres Calderon Jaramillob6b26dc2019-11-25 21:24:05176 uint64_t previous_content_id,
Andres Calderon Jaramillo5057f232019-11-29 23:05:48177 bool depends_on_at_raster_decodes,
178 bool depends_on_hardware_accelerated_jpeg_candidates,
179 bool depends_on_hardware_accelerated_webp_candidates) {
danakjbd636052018-02-06 18:28:49180 if (!resource.gpu_backing()) {
181 auto backing = std::make_unique<ZeroCopyGpuBacking>();
Antoine Laboure5a2101a2018-11-07 23:36:27182 backing->overlay_candidate = true;
kylechar58c09f72023-07-11 21:27:34183 backing->texture_target = tile_texture_target_;
Antoine Laboure5a2101a2018-11-07 23:36:27184 // This RasterBufferProvider will modify the resource outside of the
185 // GL command stream. So resources should not become available for reuse
186 // until they are not in use by the gpu anymore, which a fence is used
187 // to determine.
188 backing->wait_on_fence_required = true;
189 backing->shared_image_interface =
190 compositor_context_provider_->SharedImageInterface();
danakjbd636052018-02-06 18:28:49191 resource.set_gpu_backing(std::move(backing));
192 }
193 ZeroCopyGpuBacking* backing =
194 static_cast<ZeroCopyGpuBacking*>(resource.gpu_backing());
195
Colin Blundell0fd5a822023-12-08 17:31:47196 return std::make_unique<ZeroCopyRasterBufferImpl>(shutdown_event_, resource,
197 backing);
prashant.nb4d4f492016-04-29 12:51:28198}
199
Sunny Sachanandani5f5419e22017-05-12 20:35:30200void ZeroCopyRasterBufferProvider::Flush() {}
201
Colin Blundell68d83e72023-03-17 07:49:28202viz::SharedImageFormat ZeroCopyRasterBufferProvider::GetFormat() const {
danakja32578c2018-04-25 21:18:36203 return tile_format_;
prashant.nb4d4f492016-04-29 12:51:28204}
205
danakja32578c2018-04-25 21:18:36206bool ZeroCopyRasterBufferProvider::IsResourcePremultiplied() const {
Eric Karl247f09c2018-03-15 02:06:36207 return true;
208}
209
ericrk5ac42f322016-07-14 01:06:51210bool ZeroCopyRasterBufferProvider::CanPartialRasterIntoProvidedResource()
211 const {
ericrkeeda58992016-07-07 02:34:27212 return false;
213}
214
ericrk7f6a27f2017-01-31 22:34:32215bool ZeroCopyRasterBufferProvider::IsResourceReadyToDraw(
Steve Kobesad278432023-07-05 21:35:41216 const ResourcePool::InUsePoolResource& resource) {
ericrk7f6a27f2017-01-31 22:34:32217 // Zero-copy resources are immediately ready to draw.
218 return true;
219}
220
221uint64_t ZeroCopyRasterBufferProvider::SetReadyToDrawCallback(
danakj4e871d82018-01-18 21:56:57222 const std::vector<const ResourcePool::InUsePoolResource*>& resources,
kylechar4bb144d2019-01-11 20:42:07223 base::OnceClosure callback,
Steve Kobesad278432023-07-05 21:35:41224 uint64_t pending_callback_id) {
ericrk7f6a27f2017-01-31 22:34:32225 // Zero-copy resources are immediately ready to draw.
226 return 0;
227}
228
John Abd-El-Malek61fd3e12021-04-29 19:01:30229void ZeroCopyRasterBufferProvider::SetShutdownEvent(
230 base::WaitableEvent* shutdown_event) {
231 shutdown_event_ = shutdown_event;
232}
233
prashant.nb4d4f492016-04-29 12:51:28234void ZeroCopyRasterBufferProvider::Shutdown() {}
235
prashant.nb4d4f492016-04-29 12:51:28236} // namespace cc