1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
gpu / command_buffer / client / shared_image_interface.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/client/shared_image_interface.h"
#include <GLES2/gl2.h>
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "base/process/memory.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "ui/gfx/buffer_format_util.h"
#if BUILDFLAG(IS_WIN)
#include "ui/gfx/win/d3d_shared_fence.h"
#endif
namespace gpu {
// static
void SharedImageInterface::CreateSharedMemoryRegionFromSIInfo(
const SharedImageInfo& si_info,
base::WritableSharedMemoryMapping& mapping,
gfx::GpuMemoryBufferHandle& handle) {
DCHECK(gpu::IsValidClientUsage(si_info.meta.usage))
<< uint32_t(si_info.meta.usage);
DCHECK_EQ(si_info.meta.usage,
gpu::SharedImageUsageSet(gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY));
DCHECK(viz::HasEquivalentBufferFormat(si_info.meta.format))
<< si_info.meta.format.ToString();
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
CHECK(!si_info.meta.format.PrefersExternalSampler())
<< si_info.meta.format.ToString();
#endif
gfx::BufferFormat buffer_format =
viz::SinglePlaneSharedImageFormatToBufferFormat(si_info.meta.format);
const size_t buffer_size =
gfx::BufferSizeForBufferFormat(si_info.meta.size, buffer_format);
auto shared_memory_region =
base::UnsafeSharedMemoryRegion::Create(buffer_size);
if (!shared_memory_region.IsValid()) {
DLOG(ERROR) << "base::UnsafeSharedMemoryRegion::Create() for SharedImage "
"with SHARED_IMAGE_USAGE_CPU_WRITE_ONLY fails!";
base::TerminateBecauseOutOfMemory(buffer_size);
}
mapping = shared_memory_region.Map();
if (!mapping.IsValid()) {
DLOG(ERROR) << "shared_memory_region.Map() for "
"SHARED_IMAGE_USAGE_CPU_WRITE_ONLY fails!";
base::TerminateBecauseOutOfMemory(buffer_size);
}
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.offset = 0;
handle.stride = static_cast<int32_t>(
gfx::RowSizeForBufferFormat(si_info.meta.size.width(), buffer_format, 0));
handle.region = std::move(shared_memory_region);
}
SharedImageInterface::SwapChainSharedImages::SwapChainSharedImages(
scoped_refptr<gpu::ClientSharedImage> front_buffer,
scoped_refptr<gpu::ClientSharedImage> back_buffer)
: front_buffer(std::move(front_buffer)),
back_buffer(std::move(back_buffer)) {}
SharedImageInterface::SwapChainSharedImages::SwapChainSharedImages(
const SwapChainSharedImages& shared_images) = default;
SharedImageInterface::SwapChainSharedImages::~SwapChainSharedImages() = default;
SharedImageInterface::SharedImageInterface()
: holder_(base::MakeRefCounted<SharedImageInterfaceHolder>(this)) {}
SharedImageInterface::~SharedImageInterface() = default;
scoped_refptr<ClientSharedImage> SharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
std::optional<SharedImagePoolId> pool_id) {
NOTREACHED();
}
scoped_refptr<ClientSharedImage>
SharedImageInterface::AddReferenceToSharedImage(
const SyncToken& sync_token,
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
uint32_t texture_target) {
return ImportSharedImage(ExportedSharedImage(
mailbox,
SharedImageMetadata{format, size, color_space, surface_origin, alpha_type,
usage},
sync_token, texture_target));
}
scoped_refptr<ClientSharedImage> SharedImageInterface::NotifyMailboxAdded(
const Mailbox& /*mailbox*/,
viz::SharedImageFormat /*format*/,
const gfx::Size& /*size*/,
const gfx::ColorSpace& /*color_space*/,
GrSurfaceOrigin /*surface_origin*/,
SkAlphaType /*alpha_type*/,
SharedImageUsageSet /*usage*/) {
return nullptr;
}
scoped_refptr<ClientSharedImage> SharedImageInterface::NotifyMailboxAdded(
const Mailbox& /*mailbox*/,
viz::SharedImageFormat /*format*/,
const gfx::Size& /*size*/,
const gfx::ColorSpace& /*color_space*/,
GrSurfaceOrigin /*surface_origin*/,
SkAlphaType /*alpha_type*/,
SharedImageUsageSet /*usage*/,
uint32_t /*texture_target*/) {
return nullptr;
}
void SharedImageInterface::CopyToGpuMemoryBuffer(const SyncToken& sync_token,
const Mailbox& mailbox) {
NOTREACHED();
}
void SharedImageInterface::CopyToGpuMemoryBufferAsync(
const SyncToken& sync_token,
const Mailbox& mailbox,
base::OnceCallback<void(bool)> callback) {
NOTREACHED();
}
bool SharedImageInterface::CopyNativeGmbToSharedMemorySync(
gfx::GpuMemoryBufferHandle buffer_handle,
base::UnsafeSharedMemoryRegion memory_region) {
NOTREACHED();
}
void SharedImageInterface::CopyNativeGmbToSharedMemoryAsync(
gfx::GpuMemoryBufferHandle buffer_handle,
base::UnsafeSharedMemoryRegion memory_region,
base::OnceCallback<void(bool)> callback) {
NOTREACHED();
}
bool SharedImageInterface::IsConnected() {
NOTREACHED();
}
void SharedImageInterface::Release() const {
bool should_destroy = false;
{
base::AutoLock auto_lock(holder_->lock_);
if (base::subtle::RefCountedThreadSafeBase::Release()) {
ANALYZER_SKIP_THIS_PATH();
holder_->OnDestroy();
should_destroy = true;
}
}
if (should_destroy) {
delete this;
}
}
#if BUILDFLAG(IS_WIN)
void SharedImageInterface::UpdateSharedImage(
const SyncToken& sync_token,
scoped_refptr<gfx::D3DSharedFence> d3d_shared_fence,
const Mailbox& mailbox) {
NOTIMPLEMENTED_LOG_ONCE();
}
#endif // BUILDFLAG(IS_WIN)
SharedImageInterface::SharedImageMapping::SharedImageMapping() = default;
SharedImageInterface::SharedImageMapping::SharedImageMapping(
SharedImageInterface::SharedImageMapping&& mapped) = default;
SharedImageInterface::SharedImageMapping::SharedImageMapping(
scoped_refptr<ClientSharedImage> shared_image,
base::WritableSharedMemoryMapping mapping)
: shared_image(std::move(shared_image)), mapping(std::move(mapping)) {}
SharedImageInterface::SharedImageMapping&
SharedImageInterface::SharedImageMapping::operator=(
SharedImageInterface::SharedImageMapping&& mapped) = default;
SharedImageInterface::SharedImageMapping::~SharedImageMapping() = default;
SharedImageInterfaceHolder::SharedImageInterfaceHolder(
SharedImageInterface* sii)
: sii_(sii) {}
SharedImageInterfaceHolder::~SharedImageInterfaceHolder() = default;
scoped_refptr<SharedImageInterface> SharedImageInterfaceHolder::Get() {
base::AutoLock auto_lock(lock_);
return scoped_refptr<SharedImageInterface>(sii_);
}
void SharedImageInterfaceHolder::OnDestroy() {
lock_.AssertAcquired();
sii_ = nullptr;
}
} // namespace gpu