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
|
// 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 "third_party/blink/renderer/platform/graphics/mailbox_texture_backing.h"
#include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/mailbox_ref.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/graphics/web_graphics_context_3d_provider_wrapper.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "ui/gfx/geometry/skia_conversions.h"
namespace blink {
MailboxTextureBacking::MailboxTextureBacking(
sk_sp<SkImage> sk_image,
scoped_refptr<MailboxRef> mailbox_ref,
const gfx::Size& size,
const viz::SharedImageFormat& format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper)
: sk_image_(std::move(sk_image)),
mailbox_ref_(std::move(mailbox_ref)),
sk_image_info_(SkImageInfo::Make(gfx::SizeToSkISize(size),
ToClosestSkColorType(format),
alpha_type,
color_space.ToSkColorSpace())),
context_provider_wrapper_(std::move(context_provider_wrapper)) {}
MailboxTextureBacking::MailboxTextureBacking(
const gpu::Mailbox& mailbox,
scoped_refptr<MailboxRef> mailbox_ref,
const gfx::Size& size,
const viz::SharedImageFormat& format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper)
: mailbox_(mailbox),
mailbox_ref_(std::move(mailbox_ref)),
sk_image_info_(SkImageInfo::Make(gfx::SizeToSkISize(size),
ToClosestSkColorType(format),
alpha_type,
color_space.ToSkColorSpace())),
context_provider_wrapper_(std::move(context_provider_wrapper)) {}
MailboxTextureBacking::~MailboxTextureBacking() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (context_provider_wrapper_) {
gpu::raster::RasterInterface* ri =
context_provider_wrapper_->ContextProvider().RasterInterface();
// Update the sync token for MailboxRef.
ri->WaitSyncTokenCHROMIUM(mailbox_ref_->sync_token().GetConstData());
gpu::SyncToken sync_token;
ri->GenUnverifiedSyncTokenCHROMIUM(sync_token.GetData());
mailbox_ref_->set_sync_token(sync_token);
}
}
const SkImageInfo& MailboxTextureBacking::GetSkImageInfo() {
return sk_image_info_;
}
gpu::Mailbox MailboxTextureBacking::GetMailbox() const {
return mailbox_;
}
sk_sp<SkImage> MailboxTextureBacking::GetAcceleratedSkImage() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return sk_image_;
}
sk_sp<SkImage> MailboxTextureBacking::GetSkImageViaReadback() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!mailbox_.IsZero()) {
if (!context_provider_wrapper_)
return nullptr;
// TODO(jochin): Consider doing some caching and using discardable memory.
sk_sp<SkData> image_pixels =
TryAllocateSkData(sk_image_info_.computeMinByteSize());
if (!image_pixels)
return nullptr;
uint8_t* writable_pixels =
static_cast<uint8_t*>(image_pixels->writable_data());
gpu::raster::RasterInterface* ri =
context_provider_wrapper_->ContextProvider().RasterInterface();
if (!ri->ReadbackImagePixels(
mailbox_, sk_image_info_,
static_cast<GLuint>(sk_image_info_.minRowBytes()), 0, 0,
/*plane_index=*/0, writable_pixels)) {
return nullptr;
}
return SkImages::RasterFromData(sk_image_info_, std::move(image_pixels),
sk_image_info_.minRowBytes());
} else if (sk_image_) {
return sk_image_->makeNonTextureImage();
}
return nullptr;
}
bool MailboxTextureBacking::readPixels(const SkImageInfo& dst_info,
void* dst_pixels,
size_t dst_row_bytes,
int src_x,
int src_y) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!mailbox_.IsZero()) {
if (!context_provider_wrapper_)
return false;
gpu::raster::RasterInterface* ri =
context_provider_wrapper_->ContextProvider().RasterInterface();
return ri->ReadbackImagePixels(mailbox_, dst_info,
static_cast<GLuint>(dst_info.minRowBytes()),
src_x, src_y, /*plane_index=*/0, dst_pixels);
} else if (sk_image_) {
return sk_image_->readPixels(dst_info, dst_pixels, dst_row_bytes, src_x,
src_y);
}
return false;
}
void MailboxTextureBacking::FlushPendingSkiaOps() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!context_provider_wrapper_ || !sk_image_) {
return;
}
GrDirectContext* ctx =
context_provider_wrapper_->ContextProvider().GetGrContext();
if (!ctx) {
return;
}
ctx->flushAndSubmit(sk_image_);
}
} // namespace blink
|