Avi Drissman | 05dfbc82 | 2022-09-13 21:25:34 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Kevin Haslett | 927f234 | 2022-08-02 20:10:58 | [diff] [blame] | 5 | #include "gpu/command_buffer/service/shared_image_interface_in_process.h" |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 6 | |
Avi Drissman | 93a273d | 2023-01-11 00:38:27 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
David Sanders | 10242c1 | 2022-03-17 07:36:44 | [diff] [blame] | 9 | #include "base/synchronization/waitable_event.h" |
Xiaohan Wang | fa22d3e | 2022-01-15 02:02:43 | [diff] [blame] | 10 | #include "build/build_config.h" |
Mingjing Zhang | 65eedcc | 2023-10-24 15:12:42 | [diff] [blame] | 11 | #include "gpu/command_buffer/client/client_shared_image.h" |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 12 | #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" |
kylechar | bee4cd4 | 2022-07-15 15:22:08 | [diff] [blame] | 13 | #include "gpu/command_buffer/common/shared_image_usage.h" |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 14 | #include "gpu/command_buffer/common/sync_token.h" |
Kevin Haslett | 927f234 | 2022-08-02 20:10:58 | [diff] [blame] | 15 | #include "gpu/command_buffer/service/command_buffer_task_executor.h" |
| 16 | #include "gpu/command_buffer/service/display_compositor_memory_and_task_controller_on_gpu.h" |
| 17 | #include "gpu/command_buffer/service/gpu_command_buffer_memory_tracker.h" |
Peter McNeeley | d9ea411 | 2023-05-08 15:33:43 | [diff] [blame] | 18 | #include "gpu/command_buffer/service/gr_shader_cache.h" |
Saifuddin Hitawala | 81cbd38 | 2022-07-20 19:14:53 | [diff] [blame] | 19 | #include "gpu/command_buffer/service/shared_image/shared_image_factory.h" |
Kevin Haslett | 927f234 | 2022-08-02 20:10:58 | [diff] [blame] | 20 | #include "gpu/command_buffer/service/single_task_sequence.h" |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 21 | #include "gpu/command_buffer/service/sync_point_manager.h" |
Kevin Haslett | 927f234 | 2022-08-02 20:10:58 | [diff] [blame] | 22 | #include "gpu/config/gpu_driver_bug_workarounds.h" |
| 23 | #include "gpu/config/gpu_feature_info.h" |
| 24 | #include "gpu/config/gpu_preferences.h" |
Vikas Soni | c6b087a | 2023-05-18 21:22:10 | [diff] [blame] | 25 | #include "gpu/ipc/common/gpu_client_ids.h" |
Peter McNeeley | d9ea411 | 2023-05-08 15:33:43 | [diff] [blame] | 26 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 27 | #include "ui/gl/gl_context.h" |
| 28 | |
| 29 | namespace gpu { |
Weiliang Chen | 25ebb7a | 2020-10-28 00:16:05 | [diff] [blame] | 30 | |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 31 | struct SharedImageInterfaceInProcess::SetUpOnGpuParams { |
| 32 | const GpuPreferences gpu_preferences; |
| 33 | const GpuDriverBugWorkarounds gpu_workarounds; |
| 34 | const GpuFeatureInfo gpu_feature_info; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 35 | const raw_ptr<gpu::SharedContextState> context_state; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 36 | const raw_ptr<SharedImageManager> shared_image_manager; |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 37 | const bool is_for_display_compositor; |
| 38 | |
| 39 | SetUpOnGpuParams(const GpuPreferences& gpu_preferences, |
| 40 | const GpuDriverBugWorkarounds& gpu_workarounds, |
| 41 | const GpuFeatureInfo& gpu_feature_info, |
| 42 | gpu::SharedContextState* context_state, |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 43 | SharedImageManager* shared_image_manager, |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 44 | bool is_for_display_compositor) |
| 45 | : gpu_preferences(gpu_preferences), |
| 46 | gpu_workarounds(gpu_workarounds), |
| 47 | gpu_feature_info(gpu_feature_info), |
| 48 | context_state(context_state), |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 49 | shared_image_manager(shared_image_manager), |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 50 | is_for_display_compositor(is_for_display_compositor) {} |
| 51 | |
| 52 | ~SetUpOnGpuParams() = default; |
| 53 | |
| 54 | SetUpOnGpuParams(const SetUpOnGpuParams& other) = delete; |
| 55 | SetUpOnGpuParams& operator=(const SetUpOnGpuParams& other) = delete; |
| 56 | }; |
| 57 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 58 | SharedImageInterfaceInProcess::SharedImageInterfaceInProcess( |
Weiliang Chen | 25ebb7a | 2020-10-28 00:16:05 | [diff] [blame] | 59 | SingleTaskSequence* task_sequence, |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 60 | DisplayCompositorMemoryAndTaskControllerOnGpu* display_controller) |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 61 | : SharedImageInterfaceInProcess( |
| 62 | task_sequence, |
| 63 | display_controller->sync_point_manager(), |
| 64 | display_controller->gpu_preferences(), |
| 65 | display_controller->gpu_driver_bug_workarounds(), |
| 66 | display_controller->gpu_feature_info(), |
| 67 | display_controller->shared_context_state(), |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 68 | display_controller->shared_image_manager(), |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 69 | /*is_for_display_compositor=*/true) {} |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 70 | |
| 71 | SharedImageInterfaceInProcess::SharedImageInterfaceInProcess( |
| 72 | SingleTaskSequence* task_sequence, |
| 73 | SyncPointManager* sync_point_manager, |
| 74 | const GpuPreferences& gpu_preferences, |
| 75 | const GpuDriverBugWorkarounds& gpu_workarounds, |
| 76 | const GpuFeatureInfo& gpu_feature_info, |
| 77 | gpu::SharedContextState* context_state, |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 78 | SharedImageManager* shared_image_manager, |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 79 | bool is_for_display_compositor) |
Weiliang Chen | 25ebb7a | 2020-10-28 00:16:05 | [diff] [blame] | 80 | : task_sequence_(task_sequence), |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 81 | command_buffer_id_( |
| 82 | DisplayCompositorMemoryAndTaskControllerOnGpu::NextCommandBufferId()), |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 83 | shared_image_manager_(shared_image_manager), |
| 84 | sync_point_manager_(sync_point_manager) { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 85 | DETACH_FROM_SEQUENCE(gpu_sequence_checker_); |
| 86 | task_sequence_->ScheduleTask( |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 87 | base::BindOnce( |
| 88 | &SharedImageInterfaceInProcess::SetUpOnGpu, base::Unretained(this), |
| 89 | std::make_unique<SetUpOnGpuParams>( |
| 90 | gpu_preferences, gpu_workarounds, gpu_feature_info, context_state, |
Vasiliy Telezhnikov | 9b1a7df | 2022-12-16 21:09:12 | [diff] [blame] | 91 | shared_image_manager, is_for_display_compositor)), |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 92 | {}); |
| 93 | } |
| 94 | |
| 95 | SharedImageInterfaceInProcess::~SharedImageInterfaceInProcess() { |
| 96 | base::WaitableEvent completion( |
| 97 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 98 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 99 | |
| 100 | task_sequence_->ScheduleTask( |
| 101 | base::BindOnce(&SharedImageInterfaceInProcess::DestroyOnGpu, |
| 102 | base::Unretained(this), &completion), |
| 103 | {}); |
| 104 | completion.Wait(); |
| 105 | } |
Mingjing Zhang | 5915cc7 | 2023-09-27 15:13:46 | [diff] [blame] | 106 | |
| 107 | const SharedImageCapabilities& |
| 108 | SharedImageInterfaceInProcess::GetCapabilities() { |
| 109 | base::WaitableEvent completion( |
| 110 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 111 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 112 | |
| 113 | if (!shared_image_capabilities_) { |
| 114 | shared_image_capabilities_ = std::make_unique<SharedImageCapabilities>(); |
| 115 | task_sequence_->ScheduleTask( |
| 116 | base::BindOnce(&SharedImageInterfaceInProcess::GetCapabilitiesOnGpu, |
| 117 | base::Unretained(this), &completion, |
| 118 | shared_image_capabilities_.get()), |
| 119 | {}); |
| 120 | completion.Wait(); |
| 121 | } |
| 122 | return *shared_image_capabilities_; |
| 123 | } |
| 124 | |
| 125 | void SharedImageInterfaceInProcess::GetCapabilitiesOnGpu( |
| 126 | base::WaitableEvent* completion, |
| 127 | SharedImageCapabilities* out_capabilities) { |
| 128 | if (!LazyCreateSharedImageFactory()) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | DCHECK(shared_image_factory_); |
| 133 | *out_capabilities = shared_image_factory_->MakeCapabilities(); |
| 134 | completion->Signal(); |
| 135 | } |
| 136 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 137 | void SharedImageInterfaceInProcess::SetUpOnGpu( |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 138 | std::unique_ptr<SetUpOnGpuParams> params) { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 139 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 140 | context_state_ = params->context_state.get(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 141 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 142 | create_factory_ = base::BindOnce( |
kylechar | 5436fb5 | 2022-07-04 20:56:00 | [diff] [blame] | 143 | [](std::unique_ptr<SetUpOnGpuParams> params) { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 144 | auto shared_image_factory = std::make_unique<SharedImageFactory>( |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 145 | params->gpu_preferences, params->gpu_workarounds, |
| 146 | params->gpu_feature_info, params->context_state, |
Vasiliy Telezhnikov | 9b1a7df | 2022-12-16 21:09:12 | [diff] [blame] | 147 | params->shared_image_manager, |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 148 | params->context_state->memory_tracker(), |
| 149 | params->is_for_display_compositor); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 150 | return shared_image_factory; |
| 151 | }, |
Piotr Bialecki | 0296b22c | 2021-11-16 21:25:24 | [diff] [blame] | 152 | std::move(params)); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 153 | |
| 154 | // Make the SharedImageInterface use the same sequence as the command buffer, |
| 155 | // it's necessary for WebView because of the blocking behavior. |
| 156 | // TODO(piman): see if it's worth using a different sequence for non-WebView. |
| 157 | sync_point_client_state_ = sync_point_manager_->CreateSyncPointClientState( |
| 158 | CommandBufferNamespace::IN_PROCESS, command_buffer_id_, |
| 159 | task_sequence_->GetSequenceId()); |
| 160 | } |
| 161 | |
| 162 | void SharedImageInterfaceInProcess::DestroyOnGpu( |
| 163 | base::WaitableEvent* completion) { |
| 164 | bool have_context = MakeContextCurrent(); |
Peng Huang | 5010365 | 2020-09-29 13:50:44 | [diff] [blame] | 165 | if (shared_image_factory_) { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 166 | shared_image_factory_->DestroyAllSharedImages(have_context); |
Peng Huang | 5010365 | 2020-09-29 13:50:44 | [diff] [blame] | 167 | shared_image_factory_ = nullptr; |
| 168 | } |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 169 | |
| 170 | if (sync_point_client_state_) { |
| 171 | sync_point_client_state_->Destroy(); |
| 172 | sync_point_client_state_ = nullptr; |
| 173 | } |
Peng Huang | 38b4905f | 2021-06-18 21:20:09 | [diff] [blame] | 174 | |
| 175 | context_state_ = nullptr; |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 176 | completion->Signal(); |
| 177 | } |
| 178 | |
Peng Huang | ca587f99 | 2020-06-05 17:19:28 | [diff] [blame] | 179 | bool SharedImageInterfaceInProcess::MakeContextCurrent(bool needs_gl) { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 180 | if (!context_state_) |
| 181 | return false; |
| 182 | |
| 183 | if (context_state_->context_lost()) |
| 184 | return false; |
| 185 | |
| 186 | // |shared_image_factory_| never writes to the surface, so skip unnecessary |
| 187 | // MakeCurrent to improve performance. https://crbug.com/457431 |
| 188 | auto* context = context_state_->real_context(); |
Jonathan Backer | baf79d9 | 2020-06-01 21:30:30 | [diff] [blame] | 189 | if (context->IsCurrent(nullptr)) |
Peng Huang | ca587f99 | 2020-06-05 17:19:28 | [diff] [blame] | 190 | return !context_state_->CheckResetStatus(needs_gl); |
| 191 | return context_state_->MakeCurrent(/*surface=*/nullptr, needs_gl); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 192 | } |
| 193 | |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 194 | bool SharedImageInterfaceInProcess::LazyCreateSharedImageFactory() { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 195 | if (shared_image_factory_) |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 196 | return true; |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 197 | |
Peng Huang | ca587f99 | 2020-06-05 17:19:28 | [diff] [blame] | 198 | // Some shared image backing factories will use GL in ctor, so we need GL even |
| 199 | // if chrome is using non-GL backing. |
| 200 | if (!MakeContextCurrent(/*needs_gl=*/true)) |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 201 | return false; |
Peng Huang | ca587f99 | 2020-06-05 17:19:28 | [diff] [blame] | 202 | |
kylechar | 5436fb5 | 2022-07-04 20:56:00 | [diff] [blame] | 203 | shared_image_factory_ = std::move(create_factory_).Run(); |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 204 | return true; |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 205 | } |
| 206 | |
Mingjing Zhang | deb5256 | 2023-11-01 13:57:07 | [diff] [blame] | 207 | scoped_refptr<ClientSharedImage> |
| 208 | SharedImageInterfaceInProcess::CreateSharedImage( |
Colin Blundell | a679a47 | 2023-02-13 09:05:27 | [diff] [blame] | 209 | viz::SharedImageFormat format, |
| 210 | const gfx::Size& size, |
| 211 | const gfx::ColorSpace& color_space, |
| 212 | GrSurfaceOrigin surface_origin, |
| 213 | SkAlphaType alpha_type, |
| 214 | uint32_t usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 215 | base::StringPiece debug_label, |
Colin Blundell | a679a47 | 2023-02-13 09:05:27 | [diff] [blame] | 216 | gpu::SurfaceHandle surface_handle) { |
kylechar | bee4cd4 | 2022-07-15 15:22:08 | [diff] [blame] | 217 | DCHECK(gpu::IsValidClientUsage(usage)); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 218 | auto mailbox = Mailbox::GenerateForSharedImage(); |
| 219 | { |
| 220 | base::AutoLock lock(lock_); |
| 221 | // Note: we enqueue the task under the lock to guarantee monotonicity of |
| 222 | // the release ids as seen by the service. Unretained is safe because |
| 223 | // SharedImageInterfaceInProcess synchronizes with the GPU thread at |
| 224 | // destruction time, cancelling tasks, before |this| is destroyed. |
| 225 | ScheduleGpuTask( |
| 226 | base::BindOnce( |
| 227 | &SharedImageInterfaceInProcess::CreateSharedImageOnGpuThread, |
Colin Blundell | a679a47 | 2023-02-13 09:05:27 | [diff] [blame] | 228 | base::Unretained(this), mailbox, format, surface_handle, size, |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 229 | color_space, surface_origin, alpha_type, usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 230 | std::string(debug_label), |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 231 | MakeSyncToken(next_fence_sync_release_++)), |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 232 | {}); |
| 233 | } |
Mingjing Zhang | deb5256 | 2023-11-01 13:57:07 | [diff] [blame] | 234 | return base::MakeRefCounted<ClientSharedImage>(mailbox); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void SharedImageInterfaceInProcess::CreateSharedImageOnGpuThread( |
| 238 | const Mailbox& mailbox, |
Colin Blundell | 79b4f1d8 | 2023-02-10 09:31:39 | [diff] [blame] | 239 | viz::SharedImageFormat format, |
Paulo Warren | fad0b11 | 2020-03-03 21:40:26 | [diff] [blame] | 240 | gpu::SurfaceHandle surface_handle, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 241 | const gfx::Size& size, |
| 242 | const gfx::ColorSpace& color_space, |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 243 | GrSurfaceOrigin surface_origin, |
| 244 | SkAlphaType alpha_type, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 245 | uint32_t usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 246 | std::string debug_label, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 247 | const SyncToken& sync_token) { |
| 248 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 249 | if (!LazyCreateSharedImageFactory()) |
| 250 | return; |
| 251 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 252 | if (!MakeContextCurrent()) |
| 253 | return; |
| 254 | |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 255 | DCHECK(shared_image_factory_); |
Paulo Warren | fad0b11 | 2020-03-03 21:40:26 | [diff] [blame] | 256 | if (!shared_image_factory_->CreateSharedImage( |
Colin Blundell | 79b4f1d8 | 2023-02-10 09:31:39 | [diff] [blame] | 257 | mailbox, format, size, color_space, surface_origin, alpha_type, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 258 | surface_handle, usage, std::string(debug_label))) { |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 259 | context_state_->MarkContextLost(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 260 | return; |
| 261 | } |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 262 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
| 263 | } |
| 264 | |
Mingjing Zhang | 65eedcc | 2023-10-24 15:12:42 | [diff] [blame] | 265 | scoped_refptr<ClientSharedImage> |
| 266 | SharedImageInterfaceInProcess::CreateSharedImage( |
Colin Blundell | a679a47 | 2023-02-13 09:05:27 | [diff] [blame] | 267 | viz::SharedImageFormat format, |
| 268 | const gfx::Size& size, |
| 269 | const gfx::ColorSpace& color_space, |
| 270 | GrSurfaceOrigin surface_origin, |
| 271 | SkAlphaType alpha_type, |
| 272 | uint32_t usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 273 | base::StringPiece debug_label, |
Colin Blundell | a679a47 | 2023-02-13 09:05:27 | [diff] [blame] | 274 | base::span<const uint8_t> pixel_data) { |
kylechar | bee4cd4 | 2022-07-15 15:22:08 | [diff] [blame] | 275 | DCHECK(gpu::IsValidClientUsage(usage)); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 276 | auto mailbox = Mailbox::GenerateForSharedImage(); |
| 277 | std::vector<uint8_t> pixel_data_copy(pixel_data.begin(), pixel_data.end()); |
| 278 | { |
| 279 | base::AutoLock lock(lock_); |
| 280 | // Note: we enqueue the task under the lock to guarantee monotonicity of |
| 281 | // the release ids as seen by the service. Unretained is safe because |
| 282 | // InProcessCommandBuffer synchronizes with the GPU thread at destruction |
| 283 | // time, cancelling tasks, before |this| is destroyed. |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 284 | ScheduleGpuTask(base::BindOnce(&SharedImageInterfaceInProcess:: |
| 285 | CreateSharedImageWithDataOnGpuThread, |
| 286 | base::Unretained(this), mailbox, format, |
| 287 | size, color_space, surface_origin, |
| 288 | alpha_type, usage, std::string(debug_label), |
| 289 | MakeSyncToken(next_fence_sync_release_++), |
| 290 | std::move(pixel_data_copy)), |
| 291 | {}); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 292 | } |
Mingjing Zhang | 65eedcc | 2023-10-24 15:12:42 | [diff] [blame] | 293 | return base::MakeRefCounted<ClientSharedImage>(mailbox); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void SharedImageInterfaceInProcess::CreateSharedImageWithDataOnGpuThread( |
| 297 | const Mailbox& mailbox, |
Colin Blundell | 79b4f1d8 | 2023-02-10 09:31:39 | [diff] [blame] | 298 | viz::SharedImageFormat format, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 299 | const gfx::Size& size, |
| 300 | const gfx::ColorSpace& color_space, |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 301 | GrSurfaceOrigin surface_origin, |
| 302 | SkAlphaType alpha_type, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 303 | uint32_t usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 304 | std::string debug_label, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 305 | const SyncToken& sync_token, |
| 306 | std::vector<uint8_t> pixel_data) { |
| 307 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 308 | if (!LazyCreateSharedImageFactory()) |
| 309 | return; |
| 310 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 311 | if (!MakeContextCurrent()) |
| 312 | return; |
| 313 | |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 314 | DCHECK(shared_image_factory_); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 315 | if (!shared_image_factory_->CreateSharedImage( |
Colin Blundell | 79b4f1d8 | 2023-02-10 09:31:39 | [diff] [blame] | 316 | mailbox, format, size, color_space, surface_origin, alpha_type, usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 317 | std::move(debug_label), pixel_data)) { |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 318 | context_state_->MarkContextLost(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 319 | return; |
| 320 | } |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 321 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
| 322 | } |
| 323 | |
Mingjing Zhang | 3f1265c | 2023-10-26 02:16:55 | [diff] [blame] | 324 | scoped_refptr<ClientSharedImage> |
| 325 | SharedImageInterfaceInProcess::CreateSharedImage( |
kylechar | 7205de1 | 2022-12-07 18:43:48 | [diff] [blame] | 326 | viz::SharedImageFormat format, |
| 327 | const gfx::Size& size, |
| 328 | const gfx::ColorSpace& color_space, |
| 329 | GrSurfaceOrigin surface_origin, |
| 330 | SkAlphaType alpha_type, |
| 331 | uint32_t usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 332 | base::StringPiece debug_label, |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 333 | SurfaceHandle surface_handle, |
| 334 | gfx::BufferUsage buffer_usage) { |
| 335 | DCHECK(gpu::IsValidClientUsage(usage)); |
| 336 | auto mailbox = Mailbox::GenerateForSharedImage(); |
| 337 | { |
| 338 | base::AutoLock lock(lock_); |
| 339 | // Note: we enqueue the task under the lock to guarantee monotonicity of |
| 340 | // the release ids as seen by the service. Unretained is safe because |
| 341 | // InProcessCommandBuffer synchronizes with the GPU thread at destruction |
| 342 | // time, cancelling tasks, before |this| is destroyed. |
| 343 | ScheduleGpuTask( |
| 344 | base::BindOnce(&SharedImageInterfaceInProcess:: |
| 345 | CreateSharedImageWithBufferUsageOnGpuThread, |
| 346 | base::Unretained(this), mailbox, format, size, |
| 347 | color_space, surface_origin, alpha_type, usage, |
| 348 | std::string(debug_label), surface_handle, buffer_usage, |
| 349 | MakeSyncToken(next_fence_sync_release_++)), |
| 350 | {}); |
| 351 | } |
Mingjing Zhang | 3f1265c | 2023-10-26 02:16:55 | [diff] [blame] | 352 | return base::MakeRefCounted<ClientSharedImage>(mailbox); |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void SharedImageInterfaceInProcess::CreateSharedImageWithBufferUsageOnGpuThread( |
| 356 | const Mailbox& mailbox, |
| 357 | viz::SharedImageFormat format, |
| 358 | const gfx::Size& size, |
| 359 | const gfx::ColorSpace& color_space, |
| 360 | GrSurfaceOrigin surface_origin, |
| 361 | SkAlphaType alpha_type, |
| 362 | uint32_t usage, |
| 363 | std::string debug_label, |
| 364 | SurfaceHandle surface_handle, |
| 365 | gfx::BufferUsage buffer_usage, |
| 366 | const SyncToken& sync_token) { |
| 367 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
| 368 | if (!LazyCreateSharedImageFactory()) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | if (!MakeContextCurrent()) { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | DCHECK(shared_image_factory_); |
Vikas Soni | 034fa47 | 2023-10-03 16:07:42 | [diff] [blame] | 377 | |
| 378 | // Note that SharedImageInterfaceInProcess implementation here uses |
| 379 | // SharedImageFactory::CreateSharedImage() to create a shared image backed by |
| 380 | // native buffer/shared memory in GPU process. This is different |
| 381 | // implementation and code path compared to ClientSharedImage implementation |
| 382 | // which creates native buffer/shared memory on IO thread and then creates a |
| 383 | // mailbox from it on GPU thread. |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 384 | if (!shared_image_factory_->CreateSharedImage( |
| 385 | mailbox, format, size, color_space, surface_origin, alpha_type, |
| 386 | surface_handle, usage, std::move(debug_label), buffer_usage)) { |
| 387 | context_state_->MarkContextLost(); |
| 388 | return; |
| 389 | } |
| 390 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
| 391 | } |
| 392 | |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 393 | gfx::GpuMemoryBuffer* SharedImageInterfaceInProcess::GetGpuMemoryBuffer( |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 394 | const Mailbox& mailbox) { |
| 395 | { |
| 396 | base::AutoLock lock(lock_); |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 397 | auto it = gpu_memory_buffers_.find(mailbox); |
| 398 | if (it != gpu_memory_buffers_.end()) { |
| 399 | return it->second.get(); |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 403 | base::WaitableEvent completion( |
| 404 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 405 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 406 | |
| 407 | gfx::GpuMemoryBufferHandle handle; |
| 408 | viz::SharedImageFormat format; |
| 409 | gfx::Size size; |
| 410 | gfx::BufferUsage buffer_usage; |
| 411 | |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 412 | task_sequence_->ScheduleTask( |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 413 | base::BindOnce(&SharedImageInterfaceInProcess:: |
| 414 | GetGpuMemoryBufferHandleInfoOnGpuThread, |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 415 | base::Unretained(this), mailbox, &handle, &format, &size, |
| 416 | &buffer_usage, &completion), |
| 417 | {}); |
| 418 | completion.Wait(); |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 419 | auto gpu_memory_buffer = |
| 420 | SharedImageInterface::CreateGpuMemoryBufferForUseByScopedMapping( |
| 421 | GpuMemoryBufferHandleInfo(std::move(handle), format, size, |
| 422 | buffer_usage)); |
| 423 | auto* raw_gpu_memory_buffer = gpu_memory_buffer.get(); |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 424 | { |
| 425 | base::AutoLock lock(lock_); |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 426 | gpu_memory_buffers_[mailbox] = std::move(gpu_memory_buffer); |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 427 | } |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 428 | |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 429 | return raw_gpu_memory_buffer; |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | std::unique_ptr<SharedImageInterface::ScopedMapping> |
| 433 | SharedImageInterfaceInProcess::MapSharedImage(const Mailbox& mailbox) { |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 434 | auto* gpu_memory_buffer = GetGpuMemoryBuffer(mailbox); |
| 435 | if (!gpu_memory_buffer) { |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 436 | LOG(ERROR) << "Buffer is null."; |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 437 | return nullptr; |
| 438 | } |
| 439 | |
Colin Blundell | 0485582 | 2023-09-29 09:37:22 | [diff] [blame] | 440 | auto scoped_mapping = |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 441 | SharedImageInterface::ScopedMapping::Create(gpu_memory_buffer); |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 442 | |
| 443 | if (!scoped_mapping) { |
| 444 | LOG(ERROR) << "Unable to create ScopedMapping."; |
| 445 | } |
| 446 | |
| 447 | return scoped_mapping; |
| 448 | } |
| 449 | |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 450 | void SharedImageInterfaceInProcess::GetGpuMemoryBufferHandleInfoOnGpuThread( |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 451 | const Mailbox& mailbox, |
| 452 | gfx::GpuMemoryBufferHandle* handle, |
| 453 | viz::SharedImageFormat* format, |
| 454 | gfx::Size* size, |
| 455 | gfx::BufferUsage* buffer_usage, |
| 456 | base::WaitableEvent* completion) { |
| 457 | base::ScopedClosureRunner completion_runner(base::BindOnce( |
| 458 | [](base::WaitableEvent* completion) { completion->Signal(); }, |
| 459 | completion)); |
| 460 | |
| 461 | DCHECK(shared_image_factory_); |
| 462 | |
| 463 | if (!mailbox.IsSharedImage()) { |
| 464 | LOG(ERROR) << "SharedImageInterfaceInProcess: Trying to access a " |
| 465 | "SharedImage with a " |
| 466 | "non-SharedImage mailbox."; |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | // Note that we are not making |context_state_| current here as of now since |
| 471 | // it is not needed to get the handle from the backings. Make context current |
| 472 | // if we find that it is required. |
| 473 | if (!shared_image_factory_->GetGpuMemoryBufferHandleInfo( |
| 474 | mailbox, *handle, *format, *size, *buffer_usage)) { |
| 475 | LOG(ERROR) |
| 476 | << "SharedImageInterfaceInProcess: Unable to get GpuMemoryBufferHandle"; |
| 477 | } |
| 478 | } |
| 479 | |
Mingjing Zhang | 0245356 | 2023-10-30 20:28:31 | [diff] [blame] | 480 | scoped_refptr<ClientSharedImage> |
| 481 | SharedImageInterfaceInProcess::CreateSharedImage( |
Colin Blundell | 7204dc6 | 2023-09-20 09:21:42 | [diff] [blame] | 482 | viz::SharedImageFormat format, |
| 483 | const gfx::Size& size, |
| 484 | const gfx::ColorSpace& color_space, |
| 485 | GrSurfaceOrigin surface_origin, |
| 486 | SkAlphaType alpha_type, |
| 487 | uint32_t usage, |
| 488 | base::StringPiece debug_label, |
kylechar | 7205de1 | 2022-12-07 18:43:48 | [diff] [blame] | 489 | gfx::GpuMemoryBufferHandle buffer_handle) { |
Saifuddin Hitawala | 4342727 | 2023-05-29 17:18:47 | [diff] [blame] | 490 | DCHECK(gpu::IsValidClientUsage(usage)); |
| 491 | |
Saifuddin Hitawala | e168230 | 2023-07-06 15:32:33 | [diff] [blame] | 492 | #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN) |
| 493 | CHECK(!format.PrefersExternalSampler()); |
| 494 | #endif |
| 495 | |
Saifuddin Hitawala | 4342727 | 2023-05-29 17:18:47 | [diff] [blame] | 496 | auto mailbox = Mailbox::GenerateForSharedImage(); |
| 497 | { |
| 498 | base::AutoLock lock(lock_); |
| 499 | SyncToken sync_token = MakeSyncToken(next_fence_sync_release_++); |
| 500 | // Note: we enqueue the task under the lock to guarantee monotonicity of |
| 501 | // the release ids as seen by the service. Unretained is safe because |
| 502 | // InProcessCommandBuffer synchronizes with the GPU thread at destruction |
| 503 | // time, cancelling tasks, before |this| is destroyed. |
| 504 | ScheduleGpuTask(base::BindOnce(&SharedImageInterfaceInProcess:: |
| 505 | CreateSharedImageWithBufferOnGpuThread, |
| 506 | base::Unretained(this), mailbox, format, |
| 507 | size, color_space, surface_origin, |
| 508 | alpha_type, usage, std::move(buffer_handle), |
| 509 | std::string(debug_label), sync_token), |
| 510 | {}); |
| 511 | } |
| 512 | |
Mingjing Zhang | 0245356 | 2023-10-30 20:28:31 | [diff] [blame] | 513 | return base::MakeRefCounted<ClientSharedImage>(mailbox); |
Saifuddin Hitawala | 4342727 | 2023-05-29 17:18:47 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void SharedImageInterfaceInProcess::CreateSharedImageWithBufferOnGpuThread( |
| 517 | const Mailbox& mailbox, |
| 518 | viz::SharedImageFormat format, |
| 519 | const gfx::Size& size, |
| 520 | const gfx::ColorSpace& color_space, |
| 521 | GrSurfaceOrigin surface_origin, |
| 522 | SkAlphaType alpha_type, |
| 523 | uint32_t usage, |
| 524 | gfx::GpuMemoryBufferHandle buffer_handle, |
| 525 | std::string debug_label, |
| 526 | const SyncToken& sync_token) { |
| 527 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
| 528 | if (!LazyCreateSharedImageFactory()) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | if (!MakeContextCurrent()) { |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | DCHECK(shared_image_factory_); |
| 537 | if (!shared_image_factory_->CreateSharedImage( |
| 538 | mailbox, format, size, color_space, surface_origin, alpha_type, usage, |
| 539 | std::move(debug_label), std::move(buffer_handle))) { |
| 540 | context_state_->MarkContextLost(); |
| 541 | return; |
| 542 | } |
| 543 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
kylechar | 7205de1 | 2022-12-07 18:43:48 | [diff] [blame] | 544 | } |
| 545 | |
Mingjing Zhang | 2f559147 | 2023-11-06 18:49:07 | [diff] [blame] | 546 | scoped_refptr<ClientSharedImage> |
| 547 | SharedImageInterfaceInProcess::CreateSharedImage( |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 548 | gfx::GpuMemoryBuffer* gpu_memory_buffer, |
| 549 | GpuMemoryBufferManager* gpu_memory_buffer_manager, |
Christopher Cameron | 2adb77f | 2021-04-27 17:59:39 | [diff] [blame] | 550 | gfx::BufferPlane plane, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 551 | const gfx::ColorSpace& color_space, |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 552 | GrSurfaceOrigin surface_origin, |
| 553 | SkAlphaType alpha_type, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 554 | uint32_t usage, |
| 555 | base::StringPiece debug_label) { |
kylechar | bee4cd4 | 2022-07-15 15:22:08 | [diff] [blame] | 556 | DCHECK(gpu::IsValidClientUsage(usage)); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 557 | // TODO(piman): DCHECK GMB format support. |
| 558 | DCHECK(IsImageSizeValidForGpuMemoryBufferFormat( |
Sunny Sachanandani | 8b1c6a4 | 2021-09-18 00:38:21 | [diff] [blame] | 559 | gpu_memory_buffer->GetSize(), gpu_memory_buffer->GetFormat())); |
Christopher Cameron | 2adb77f | 2021-04-27 17:59:39 | [diff] [blame] | 560 | DCHECK(IsPlaneValidForGpuMemoryBufferFormat(plane, |
| 561 | gpu_memory_buffer->GetFormat())); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 562 | |
| 563 | auto mailbox = Mailbox::GenerateForSharedImage(); |
| 564 | gfx::GpuMemoryBufferHandle handle = gpu_memory_buffer->CloneHandle(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 565 | { |
| 566 | base::AutoLock lock(lock_); |
kylechar | 2fde678 | 2023-01-04 17:22:42 | [diff] [blame] | 567 | SyncToken sync_token = MakeSyncToken(next_fence_sync_release_++); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 568 | // Note: we enqueue the task under the lock to guarantee monotonicity of |
| 569 | // the release ids as seen by the service. Unretained is safe because |
| 570 | // InProcessCommandBuffer synchronizes with the GPU thread at destruction |
| 571 | // time, cancelling tasks, before |this| is destroyed. |
| 572 | ScheduleGpuTask( |
| 573 | base::BindOnce( |
| 574 | &SharedImageInterfaceInProcess::CreateGMBSharedImageOnGpuThread, |
| 575 | base::Unretained(this), mailbox, std::move(handle), |
Christopher Cameron | 2adb77f | 2021-04-27 17:59:39 | [diff] [blame] | 576 | gpu_memory_buffer->GetFormat(), plane, gpu_memory_buffer->GetSize(), |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 577 | color_space, surface_origin, alpha_type, usage, |
| 578 | std::string(debug_label), sync_token), |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 579 | {}); |
| 580 | } |
kylechar | 2fde678 | 2023-01-04 17:22:42 | [diff] [blame] | 581 | |
Mingjing Zhang | 2f559147 | 2023-11-06 18:49:07 | [diff] [blame] | 582 | return base::MakeRefCounted<ClientSharedImage>(mailbox); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void SharedImageInterfaceInProcess::CreateGMBSharedImageOnGpuThread( |
| 586 | const Mailbox& mailbox, |
| 587 | gfx::GpuMemoryBufferHandle handle, |
| 588 | gfx::BufferFormat format, |
Christopher Cameron | 2adb77f | 2021-04-27 17:59:39 | [diff] [blame] | 589 | gfx::BufferPlane plane, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 590 | const gfx::Size& size, |
| 591 | const gfx::ColorSpace& color_space, |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 592 | GrSurfaceOrigin surface_origin, |
| 593 | SkAlphaType alpha_type, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 594 | uint32_t usage, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 595 | std::string debug_label, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 596 | const SyncToken& sync_token) { |
| 597 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 598 | if (!LazyCreateSharedImageFactory()) |
| 599 | return; |
| 600 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 601 | if (!MakeContextCurrent()) |
| 602 | return; |
| 603 | |
Saifuddin Hitawala | ad61a0a5 | 2021-08-11 12:18:35 | [diff] [blame] | 604 | DCHECK(shared_image_factory_); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 605 | if (!shared_image_factory_->CreateSharedImage( |
kylechar | 0617764 | 2023-01-12 18:03:19 | [diff] [blame] | 606 | mailbox, std::move(handle), format, plane, size, color_space, |
Rafael Cintron | de3d9b8 | 2023-04-19 20:19:28 | [diff] [blame] | 607 | surface_origin, alpha_type, usage, std::move(debug_label))) { |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 608 | context_state_->MarkContextLost(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 609 | return; |
| 610 | } |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 611 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
| 612 | } |
| 613 | |
| 614 | SharedImageInterface::SwapChainMailboxes |
| 615 | SharedImageInterfaceInProcess::CreateSwapChain( |
Colin Blundell | 680c1e5 | 2023-02-24 10:25:12 | [diff] [blame] | 616 | viz::SharedImageFormat format, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 617 | const gfx::Size& size, |
| 618 | const gfx::ColorSpace& color_space, |
Nathan Zabriskie | 659c274 | 2020-07-16 03:49:32 | [diff] [blame] | 619 | GrSurfaceOrigin surface_origin, |
| 620 | SkAlphaType alpha_type, |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 621 | uint32_t usage) { |
| 622 | NOTREACHED(); |
| 623 | return {}; |
| 624 | } |
| 625 | |
| 626 | void SharedImageInterfaceInProcess::PresentSwapChain( |
| 627 | const SyncToken& sync_token, |
| 628 | const Mailbox& mailbox) { |
| 629 | NOTREACHED(); |
| 630 | } |
| 631 | |
Xiaohan Wang | fa22d3e | 2022-01-15 02:02:43 | [diff] [blame] | 632 | #if BUILDFLAG(IS_FUCHSIA) |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 633 | void SharedImageInterfaceInProcess::RegisterSysmemBufferCollection( |
Sergey Ulanov | 7f507a0f | 2022-10-28 19:11:13 | [diff] [blame] | 634 | zx::eventpair service_handle, |
| 635 | zx::channel sysmem_token, |
Sergey Ulanov | 2806856 | 2020-06-09 23:28:07 | [diff] [blame] | 636 | gfx::BufferFormat format, |
Emircan Uysaler | 23c657d | 2020-09-30 16:28:09 | [diff] [blame] | 637 | gfx::BufferUsage usage, |
| 638 | bool register_with_image_pipe) { |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 639 | NOTREACHED(); |
| 640 | } |
Xiaohan Wang | fa22d3e | 2022-01-15 02:02:43 | [diff] [blame] | 641 | #endif // BUILDFLAG(IS_FUCHSIA) |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 642 | |
| 643 | void SharedImageInterfaceInProcess::UpdateSharedImage( |
| 644 | const SyncToken& sync_token, |
| 645 | const Mailbox& mailbox) { |
| 646 | UpdateSharedImage(sync_token, nullptr, mailbox); |
| 647 | } |
| 648 | |
| 649 | void SharedImageInterfaceInProcess::UpdateSharedImage( |
| 650 | const SyncToken& sync_token, |
| 651 | std::unique_ptr<gfx::GpuFence> acquire_fence, |
| 652 | const Mailbox& mailbox) { |
| 653 | DCHECK(!acquire_fence); |
| 654 | base::AutoLock lock(lock_); |
| 655 | // Note: we enqueue the task under the lock to guarantee monotonicity of |
| 656 | // the release ids as seen by the service. Unretained is safe because |
| 657 | // InProcessCommandBuffer synchronizes with the GPU thread at destruction |
| 658 | // time, cancelling tasks, before |this| is destroyed. |
| 659 | ScheduleGpuTask( |
| 660 | base::BindOnce( |
| 661 | &SharedImageInterfaceInProcess::UpdateSharedImageOnGpuThread, |
| 662 | base::Unretained(this), mailbox, |
| 663 | MakeSyncToken(next_fence_sync_release_++)), |
| 664 | {sync_token}); |
| 665 | } |
| 666 | |
| 667 | void SharedImageInterfaceInProcess::UpdateSharedImageOnGpuThread( |
| 668 | const Mailbox& mailbox, |
| 669 | const SyncToken& sync_token) { |
| 670 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
| 671 | if (!MakeContextCurrent()) |
| 672 | return; |
| 673 | |
| 674 | if (!shared_image_factory_ || |
| 675 | !shared_image_factory_->UpdateSharedImage(mailbox)) { |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 676 | context_state_->MarkContextLost(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 677 | return; |
| 678 | } |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 679 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
| 680 | } |
| 681 | |
| 682 | void SharedImageInterfaceInProcess::DestroySharedImage( |
| 683 | const SyncToken& sync_token, |
| 684 | const Mailbox& mailbox) { |
| 685 | // Use sync token dependency to ensure that the destroy task does not run |
| 686 | // before sync token is released. |
| 687 | ScheduleGpuTask( |
| 688 | base::BindOnce( |
| 689 | &SharedImageInterfaceInProcess::DestroySharedImageOnGpuThread, |
| 690 | base::Unretained(this), mailbox), |
| 691 | {sync_token}); |
| 692 | } |
| 693 | |
Mingjing Zhang | 6b7036f | 2023-11-03 15:38:42 | [diff] [blame] | 694 | void SharedImageInterfaceInProcess::DestroySharedImage( |
| 695 | const SyncToken& sync_token, |
| 696 | scoped_refptr<ClientSharedImage> client_shared_image) { |
| 697 | CHECK(client_shared_image->HasOneRef()); |
| 698 | DestroySharedImage(sync_token, client_shared_image->mailbox()); |
| 699 | } |
| 700 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 701 | void SharedImageInterfaceInProcess::DestroySharedImageOnGpuThread( |
| 702 | const Mailbox& mailbox) { |
| 703 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
| 704 | if (!MakeContextCurrent()) |
| 705 | return; |
| 706 | |
| 707 | if (!shared_image_factory_ || |
| 708 | !shared_image_factory_->DestroySharedImage(mailbox)) { |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 709 | context_state_->MarkContextLost(); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 710 | } |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 711 | |
| 712 | { |
| 713 | base::AutoLock lock(lock_); |
Colin Blundell | 5eb24ad2 | 2023-10-23 08:23:33 | [diff] [blame] | 714 | gpu_memory_buffers_.erase(mailbox); |
Colin Blundell | c1a9b3fd | 2023-09-28 16:00:19 | [diff] [blame] | 715 | } |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 716 | } |
| 717 | |
Vikas Soni | 08dfc58 | 2020-06-09 21:29:57 | [diff] [blame] | 718 | void SharedImageInterfaceInProcess::WaitSyncTokenOnGpuThread( |
| 719 | const SyncToken& sync_token) { |
| 720 | DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_); |
| 721 | if (!MakeContextCurrent()) |
| 722 | return; |
| 723 | |
Vikas Soni | 08dfc58 | 2020-06-09 21:29:57 | [diff] [blame] | 724 | sync_point_client_state_->ReleaseFenceSync(sync_token.release_count()); |
| 725 | } |
| 726 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 727 | SyncToken SharedImageInterfaceInProcess::GenUnverifiedSyncToken() { |
| 728 | base::AutoLock lock(lock_); |
| 729 | return MakeSyncToken(next_fence_sync_release_ - 1); |
| 730 | } |
| 731 | |
| 732 | SyncToken SharedImageInterfaceInProcess::GenVerifiedSyncToken() { |
| 733 | base::AutoLock lock(lock_); |
| 734 | SyncToken sync_token = MakeSyncToken(next_fence_sync_release_ - 1); |
| 735 | sync_token.SetVerifyFlush(); |
| 736 | return sync_token; |
| 737 | } |
| 738 | |
Vikas Soni | 08dfc58 | 2020-06-09 21:29:57 | [diff] [blame] | 739 | void SharedImageInterfaceInProcess::WaitSyncToken(const SyncToken& sync_token) { |
| 740 | base::AutoLock lock(lock_); |
| 741 | |
| 742 | ScheduleGpuTask( |
| 743 | base::BindOnce(&SharedImageInterfaceInProcess::WaitSyncTokenOnGpuThread, |
| 744 | base::Unretained(this), |
| 745 | MakeSyncToken(next_fence_sync_release_++)), |
| 746 | {sync_token}); |
| 747 | } |
| 748 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 749 | void SharedImageInterfaceInProcess::Flush() { |
| 750 | // No need to flush in this implementation. |
| 751 | } |
| 752 | |
| 753 | scoped_refptr<gfx::NativePixmap> SharedImageInterfaceInProcess::GetNativePixmap( |
| 754 | const gpu::Mailbox& mailbox) { |
| 755 | DCHECK(shared_image_manager_->is_thread_safe()); |
| 756 | return shared_image_manager_->GetNativePixmap(mailbox); |
| 757 | } |
| 758 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 759 | void SharedImageInterfaceInProcess::ScheduleGpuTask( |
| 760 | base::OnceClosure task, |
| 761 | std::vector<SyncToken> sync_token_fences) { |
kylechar | 0155a06 | 2022-09-01 22:09:49 | [diff] [blame] | 762 | task_sequence_->ScheduleTask(std::move(task), std::move(sync_token_fences)); |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 763 | } |
| 764 | |
Vasiliy Telezhnikov | 67468c9 | 2023-03-08 16:43:46 | [diff] [blame] | 765 | void SharedImageInterfaceInProcess::AddReferenceToSharedImage( |
| 766 | const SyncToken& sync_token, |
| 767 | const Mailbox& mailbox, |
| 768 | uint32_t usage) { |
| 769 | // Secondary references are required only by client processes, so it shouldn't |
| 770 | // be reachable here. |
| 771 | NOTREACHED(); |
| 772 | } |
| 773 | |
Weiliang Chen | 7cf2b532 | 2020-01-23 02:04:17 | [diff] [blame] | 774 | } // namespace gpu |