1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// Copyright 2019 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/service/shared_image/external_vk_image_skia_representation.h"
#include <utility>
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/vulkan/vulkan_fence_helper.h"
#include "gpu/vulkan/vulkan_implementation.h"
#include "gpu/vulkan/vulkan_util.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkColorType.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"
#include "third_party/skia/include/gpu/MutableTextureState.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSemaphore.h"
#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/vk/GrVkTypes.h"
#include "third_party/skia/include/gpu/vk/VulkanMutableTextureState.h"
#include "third_party/skia/include/private/chromium/GrPromiseImageTexture.h"
namespace gpu {
ExternalVkImageSkiaImageRepresentation::ExternalVkImageSkiaImageRepresentation(
GrDirectContext* gr_context,
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker)
: SkiaGaneshImageRepresentation(gr_context, manager, backing, tracker),
context_state_(backing_impl()->context_state()) {}
ExternalVkImageSkiaImageRepresentation::
~ExternalVkImageSkiaImageRepresentation() {
DCHECK_EQ(access_mode_, AccessMode::kNone);
DCHECK(!end_access_semaphore_);
for (auto& promise_texture : backing_impl()->GetPromiseTextures()) {
context_state_->EraseCachedSkSurface(promise_texture.get());
}
}
std::vector<sk_sp<SkSurface>>
ExternalVkImageSkiaImageRepresentation::BeginWriteAccess(
int final_msaa_count,
const SkSurfaceProps& surface_props,
const gfx::Rect& update_rect,
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores,
std::unique_ptr<skgpu::MutableTextureState>* end_state) {
DCHECK_EQ(access_mode_, AccessMode::kNone);
auto* gr_context = context_state_->gr_context();
if (gr_context->abandoned()) {
DLOG(ERROR) << "GrContext is abandonded.";
return {};
}
auto promise_textures =
BeginAccess(/*readonly=*/false, begin_semaphores, end_semaphores);
if (promise_textures.empty()) {
DLOG(ERROR) << "BeginAccess failed";
return {};
}
std::vector<sk_sp<SkSurface>> surfaces;
surfaces.reserve(promise_textures.size());
for (size_t plane = 0; plane < promise_textures.size(); ++plane) {
auto promise_texture = promise_textures[plane];
auto surface = context_state_->GetCachedSkSurface(promise_texture.get());
// If surface properties are different from the last access, then we cannot
// reuse the cached SkSurface.
if (!surface || surface_props != surface->props() ||
final_msaa_count != surface_msaa_count_) {
SkColorType sk_color_type = viz::ToClosestSkColorType(format(), plane);
surface = SkSurfaces::WrapBackendTexture(
gr_context, promise_texture->backendTexture(), surface_origin(),
final_msaa_count, sk_color_type,
backing_impl()->color_space().ToSkColorSpace(), &surface_props);
if (!surface) {
DLOG(ERROR) << "MakeFromBackendTexture() failed.";
context_state_->EraseCachedSkSurface(promise_texture.get());
return {};
}
context_state_->CacheSkSurface(promise_texture.get(), surface);
}
[[maybe_unused]] int count = surface->getCanvas()->save();
DCHECK_EQ(count, 1);
surfaces.push_back(std::move(surface));
}
surface_msaa_count_ = final_msaa_count;
access_mode_ = AccessMode::kWrite;
if (backing_impl()->need_synchronization()) {
// If Vulkan/GL/Dawn share the same memory backing, we need to set
// |end_state| VK_QUEUE_FAMILY_EXTERNAL, and then the caller will set the
// VkImage to VK_QUEUE_FAMILY_EXTERNAL before calling EndAccess().
*end_state = std::make_unique<skgpu::MutableTextureState>(
skgpu::MutableTextureStates::MakeVulkan(VK_IMAGE_LAYOUT_UNDEFINED,
VK_QUEUE_FAMILY_EXTERNAL));
}
write_surfaces_ = surfaces;
return surfaces;
}
std::vector<sk_sp<GrPromiseImageTexture>>
ExternalVkImageSkiaImageRepresentation::BeginWriteAccess(
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores,
std::unique_ptr<skgpu::MutableTextureState>* end_state) {
DCHECK_EQ(access_mode_, AccessMode::kNone);
auto promise_textures =
BeginAccess(/*readonly=*/false, begin_semaphores, end_semaphores);
if (promise_textures.empty()) {
DLOG(ERROR) << "BeginAccess failed";
return {};
}
access_mode_ = AccessMode::kWrite;
// If Vulkan/GL/Dawn share the same memory backing, we need to set
// |end_state| VK_QUEUE_FAMILY_EXTERNAL, and then the caller will set the
// VkImage to VK_QUEUE_FAMILY_EXTERNAL before calling EndAccess().
if (backing_impl()->need_synchronization()) {
*end_state = std::make_unique<skgpu::MutableTextureState>(
skgpu::MutableTextureStates::MakeVulkan(VK_IMAGE_LAYOUT_UNDEFINED,
VK_QUEUE_FAMILY_EXTERNAL));
}
return promise_textures;
}
void ExternalVkImageSkiaImageRepresentation::EndWriteAccess() {
DCHECK_EQ(access_mode_, AccessMode::kWrite);
for (auto& write_surface : write_surfaces_) {
write_surface->getCanvas()->restoreToCount(1);
}
write_surfaces_.clear();
#if DCHECK_IS_ON()
for (auto& promise_texture : backing_impl()->GetPromiseTextures()) {
DCHECK(context_state_->CachedSkSurfaceIsUnique(promise_texture.get()));
}
#endif
EndAccess(/*readonly=*/false);
access_mode_ = AccessMode::kNone;
}
std::vector<sk_sp<GrPromiseImageTexture>>
ExternalVkImageSkiaImageRepresentation::BeginReadAccess(
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores,
std::unique_ptr<skgpu::MutableTextureState>* end_state) {
DCHECK_EQ(access_mode_, AccessMode::kNone);
auto promise_textures =
BeginAccess(/*readonly=*/true, begin_semaphores, end_semaphores);
if (promise_textures.empty()) {
LOG(ERROR) << "BeginAccess failed";
return {};
}
// If Vulkan/GL/Dawn share the same memory backing, we need set |end_state|
// VK_QUEUE_FAMILY_EXTERNAL, and then the caller will set the VkImage to
// VK_QUEUE_FAMILY_EXTERNAL before calling EndAccess().
if (backing_impl()->need_synchronization()) {
*end_state = std::make_unique<skgpu::MutableTextureState>(
skgpu::MutableTextureStates::MakeVulkan(VK_IMAGE_LAYOUT_UNDEFINED,
VK_QUEUE_FAMILY_EXTERNAL));
}
access_mode_ = AccessMode::kRead;
return promise_textures;
}
void ExternalVkImageSkiaImageRepresentation::EndReadAccess() {
DCHECK_EQ(access_mode_, AccessMode::kRead);
EndAccess(/*readonly=*/true);
access_mode_ = AccessMode::kNone;
}
std::vector<sk_sp<GrPromiseImageTexture>>
ExternalVkImageSkiaImageRepresentation::BeginAccess(
bool readonly,
std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores) {
DCHECK_EQ(access_mode_, AccessMode::kNone);
DCHECK(!end_access_semaphore_);
DCHECK(begin_access_semaphores_.empty());
if (!backing_impl()->BeginAccess(readonly, &begin_access_semaphores_,
/*is_gl=*/false)) {
return {};
}
for (auto& external_semaphore : begin_access_semaphores_) {
DCHECK(external_semaphore);
VkSemaphore semaphore = external_semaphore.GetVkSemaphore();
DCHECK(semaphore != VK_NULL_HANDLE);
// The ownership of semaphore is passed to caller.
begin_semaphores->emplace_back(GrBackendSemaphores::MakeVk(semaphore));
}
if (backing_impl()->need_synchronization()) {
DCHECK(end_semaphores);
// Create an |end_access_semaphore_| which will be signalled by the caller.
end_access_semaphore_ =
backing_impl()->external_semaphore_pool()->GetOrCreateSemaphore();
if (!end_access_semaphore_)
return {};
end_semaphores->emplace_back(
GrBackendSemaphores::MakeVk(end_access_semaphore_.GetVkSemaphore()));
}
return backing_impl()->GetPromiseTextures();
}
void ExternalVkImageSkiaImageRepresentation::EndAccess(bool readonly) {
DCHECK_NE(access_mode_, AccessMode::kNone);
DCHECK(backing_impl()->need_synchronization() || !end_access_semaphore_);
// TODO(crbug.com/40218936): This check is specific to the interop case i.e.
// when need_synchronization() is true, but we can generalize this by making
// the client TakeEndState() and asserting that the |end_state_| is null here.
#if DCHECK_IS_ON()
GrVkImageInfo info;
auto result = GrBackendTextures::GetVkImageInfo(
backing_impl()->backend_texture(), &info);
DCHECK(result);
DCHECK(!backing_impl()->need_synchronization() ||
info.fCurrentQueueFamily == VK_QUEUE_FAMILY_EXTERNAL);
#endif
backing_impl()->EndAccess(readonly, std::move(end_access_semaphore_),
/*is_gl=*/false);
// All pending semaphores have been waited on directly or indirectly. They can
// be reused when the next submitted GPU work is done by GPU.
backing_impl()->ReturnPendingSemaphoresWithFenceHelper(
std::move(begin_access_semaphores_));
begin_access_semaphores_.clear();
}
} // namespace gpu
|