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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/graphics/gpu/image_layer_bridge.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "cc/layers/texture_layer.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "components/viz/common/resources/transferable_resource.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/public/platform/web_graphics_shared_image_interface_provider.h"
#include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/color_behavior.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/image_orientation.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "ui/gfx/geometry/size.h"
namespace blink {
namespace {
scoped_refptr<StaticBitmapImage> MakeAccelerated(
const scoped_refptr<StaticBitmapImage>& source,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>
context_provider_wrapper) {
bool can_use_source_directly = source->IsTextureBacked();
#if BUILDFLAG(IS_MAC)
// On MacOS, if |source| doesn't have SCANOUT usage, it is worth copying it
// to a new buffer with the SCANOUT even when |source| is
// already on the GPU, to keep using delegated compositing.
can_use_source_directly =
can_use_source_directly &&
source->GetSharedImage()->usage().Has(gpu::SHARED_IMAGE_USAGE_SCANOUT);
#endif
if (can_use_source_directly) {
return source;
}
const auto paint_image = source->PaintImageForCurrentFrame();
const auto image_info = paint_image.GetSkImageInfo();
#if BUILDFLAG(IS_LINUX)
// TODO(b/330865436): On Linux, CanvasResourceProvider doesn't always check
// for SCANOUT support correctly on X11 and it's never supported in
// practice. Therefore, don't include it until this flow is reworked.
constexpr gpu::SharedImageUsageSet kSharedImageUsageFlags =
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ;
#else
// Always request gpu::SHARED_IMAGE_USAGE_SCANOUT when using gpu compositing,
// if possible. This is safe because the prerequisite capabilities are checked
// downstream in CanvasResourceProvider::CreateSharedImageProvider.
constexpr gpu::SharedImageUsageSet kSharedImageUsageFlags =
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ | gpu::SHARED_IMAGE_USAGE_SCANOUT;
#endif // BUILDFLAG(IS_LINUX)
auto provider = CanvasResourceProvider::CreateSharedImageProvider(
source->Size(),
viz::SkColorTypeToSinglePlaneSharedImageFormat(image_info.colorType()),
image_info.alphaType(),
SkColorSpaceToGfxColorSpace(image_info.refColorSpace()),
CanvasResourceProvider::ShouldInitialize::kNo, context_provider_wrapper,
RasterMode::kGPU, kSharedImageUsageFlags);
if (!provider || !provider->IsAccelerated())
return nullptr;
cc::PaintFlags paint;
paint.setBlendMode(SkBlendMode::kSrc);
provider->Canvas().drawImage(paint_image, 0, 0, SkSamplingOptions(), &paint);
return provider->Snapshot(FlushReason::kNon2DCanvas);
}
} // namespace
ImageLayerBridge::ImageLayerBridge(OpacityMode opacity_mode)
: is_opaque_(opacity_mode == kOpaque) {
layer_ = cc::TextureLayer::Create(this);
layer_->SetIsDrawable(true);
layer_->SetHitTestable(true);
if (is_opaque_) {
layer_->SetContentsOpaque(true);
layer_->SetBlendBackgroundColor(false);
}
}
ImageLayerBridge::~ImageLayerBridge() {
if (!disposed_)
Dispose();
}
void ImageLayerBridge::SetImage(scoped_refptr<StaticBitmapImage> image) {
if (disposed_)
return;
// There could be the case that the current PaintImage is null, meaning
// that something went wrong during the creation of the image and we should
// not try and setImage with it
if (image && !image->PaintImageForCurrentFrame())
return;
image_ = std::move(image);
if (image_) {
const bool image_is_opaque = image_->IsOpaque();
if (is_opaque_) {
// If we in opaque mode but image might have transparency we need to
// ensure its opacity is not used.
layer_->SetForceTextureToOpaque(!image_is_opaque);
} else {
layer_->SetContentsOpaque(image_is_opaque);
layer_->SetBlendBackgroundColor(!image_is_opaque);
}
if (!has_presented_since_last_set_image_ && image_->IsTextureBacked()) {
// If the layer bridge is not presenting, the GrContext may not be getting
// flushed regularly. The flush is normally triggered inside the
// m_image->EnsureMailbox() call of
// ImageLayerBridge::PrepareTransferableResource. To prevent a potential
// memory leak we must flush the GrContext here.
image_->PaintImageForCurrentFrame().FlushPendingSkiaOps();
}
}
has_presented_since_last_set_image_ = false;
}
void ImageLayerBridge::SetUV(const gfx::PointF& left_top,
const gfx::PointF& right_bottom) {
if (disposed_)
return;
layer_->SetUV(left_top, right_bottom);
}
void ImageLayerBridge::Dispose() {
if (layer_) {
layer_->ClearClient();
layer_ = nullptr;
}
image_ = nullptr;
disposed_ = true;
}
bool ImageLayerBridge::PrepareTransferableResource(
viz::TransferableResource* out_resource,
viz::ReleaseCallback* out_release_callback) {
if (disposed_)
return false;
if (!image_)
return false;
if (has_presented_since_last_set_image_)
return false;
has_presented_since_last_set_image_ = true;
const bool gpu_compositing = SharedGpuContext::IsGpuCompositingEnabled();
if (gpu_compositing) {
scoped_refptr<StaticBitmapImage> image_for_compositor =
MakeAccelerated(image_, SharedGpuContext::ContextProviderWrapper());
if (!image_for_compositor || !image_for_compositor->ContextProvider())
return false;
auto shared_image = image_for_compositor->GetSharedImage();
if (!shared_image) {
// This can happen, for example, if an ImageBitmap is produced from a
// WebGL-rendered OffscreenCanvas and then the WebGL context is forcibly
// lost. This seems to be the only reliable point where this can be
// detected.
return false;
}
const gfx::Size size(image_for_compositor->width(),
image_for_compositor->height());
viz::TransferableResource::MetadataOverride overrides = {
.format = image_for_compositor->GetSharedImageFormat(),
.size = size,
.color_space = gfx::ColorSpace(),
.alpha_type = kPremul_SkAlphaType,
};
*out_resource = viz::TransferableResource::Make(
shared_image,
viz::TransferableResource::ResourceSource::kImageLayerBridge,
image_for_compositor->GetSyncToken(), overrides);
auto func = WTF::BindOnce(&ImageLayerBridge::ResourceReleasedGpu,
WrapWeakPersistent(this),
std::move(image_for_compositor));
*out_release_callback = std::move(func);
} else {
image_ = image_->MakeUnaccelerated();
if (!image_) {
return false;
}
sk_sp<SkImage> sk_image =
image_->PaintImageForCurrentFrame().GetSwSkImage();
if (!sk_image)
return false;
const gfx::Size size(image_->width(), image_->height());
// Always convert to N32 format. This is a constraint of the software
// compositor.
constexpr SkColorType dst_color_type = kN32_SkColorType;
// TODO(vasilyt): this used to be
// viz::SkColorTypeToResourceFormat(dst_color_type), but on some platforms
// (including Mac), kN32_SkColorType is BGRA8888 which is disallowed as a
// bitmap format. Deeper refactorings are needed to fix this properly; in
// the meantime, force the use of viz::SinglePlaneFormat::kRGBA_8888 as the
// resource format. This addresses assertion failures when serializing these
// bitmaps to the GPU process.
viz::SharedImageFormat format = viz::SinglePlaneFormat::kBGRA_8888;
SoftwareResource resource = CreateOrRecycleSoftwareResource(size, format);
if (!resource.shared_image) {
return false;
}
SkImageInfo dst_info =
SkImageInfo::Make(size.width(), size.height(), dst_color_type,
kPremul_SkAlphaType, sk_image->refColorSpace());
// Copy from SkImage into SharedMemory owned by |resource|.
auto dst_mapping = resource.shared_image->Map();
if (!sk_image->readPixels(/*context=*/nullptr,
dst_mapping->GetSkPixmapForPlane(0, dst_info), 0,
0)) {
return false;
}
auto resource_color_space = sk_image->colorSpace()
? gfx::ColorSpace(*sk_image->colorSpace())
: gfx::ColorSpace::CreateSRGB();
viz::TransferableResource::MetadataOverride overrides = {
.color_space = resource_color_space,
};
*out_resource = viz::TransferableResource::Make(
resource.shared_image,
viz::TransferableResource::ResourceSource::kImageLayerBridge,
resource.sync_token, overrides);
auto func = WTF::BindOnce(&ImageLayerBridge::ResourceReleasedSoftware,
WrapWeakPersistent(this), std::move(resource));
*out_release_callback = std::move(func);
}
return true;
}
ImageLayerBridge::SoftwareResource
ImageLayerBridge::CreateOrRecycleSoftwareResource(
const gfx::Size& size,
viz::SharedImageFormat format) {
// Must call SharedImageInterfaceProvider() first so all base::WeakPtr
// restored in |resource.sii_provider| is updated.
auto* sii_provider = SharedGpuContext::SharedImageInterfaceProvider();
DCHECK(sii_provider);
auto it = std::remove_if(
recycled_software_resources_.begin(), recycled_software_resources_.end(),
[&size](const SoftwareResource& resource) {
return resource.shared_image->size() != size || !resource.sii_provider;
});
recycled_software_resources_.Shrink(
static_cast<wtf_size_t>(it - recycled_software_resources_.begin()));
if (!recycled_software_resources_.empty()) {
SoftwareResource resource = std::move(recycled_software_resources_.back());
recycled_software_resources_.pop_back();
return resource;
}
// There are no resources to recycle so allocate a new one.
SoftwareResource resource;
auto* shared_image_interface = sii_provider->SharedImageInterface();
if (!shared_image_interface) {
return resource;
}
resource.shared_image =
shared_image_interface->CreateSharedImageForSoftwareCompositor(
{format, size, gfx::ColorSpace(),
gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY, "ImageLayerBridgeBitmap"});
resource.sii_provider = sii_provider->GetWeakPtr();
resource.sync_token = shared_image_interface->GenVerifiedSyncToken();
return resource;
}
void ImageLayerBridge::ResourceReleasedGpu(
scoped_refptr<StaticBitmapImage> image,
const gpu::SyncToken& token,
bool lost_resource) {
if (image && image->IsValid()) {
DCHECK(image->IsTextureBacked());
if (token.HasData() && image->ContextProvider() &&
image->ContextProvider()->InterfaceBase()) {
image->ContextProvider()->InterfaceBase()->WaitSyncTokenCHROMIUM(
token.GetConstData());
}
}
// let 'image' go out of scope to release gpu resources.
}
void ImageLayerBridge::ResourceReleasedSoftware(
SoftwareResource resource,
const gpu::SyncToken& sync_token,
bool lost_resource) {
if (!disposed_ && !lost_resource) {
recycled_software_resources_.push_back(std::move(resource));
}
}
cc::Layer* ImageLayerBridge::CcLayer() const {
return layer_.get();
}
ImageLayerBridge::SoftwareResource::SoftwareResource() = default;
ImageLayerBridge::SoftwareResource::SoftwareResource(SoftwareResource&& other) =
default;
ImageLayerBridge::SoftwareResource&
ImageLayerBridge::SoftwareResource::operator=(SoftwareResource&& other) =
default;
} // namespace blink
|