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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/common/frame_sinks/copy_output_result.h"
#include <cstddef>
#include <utility>
#include "base/check_op.h"
#include "base/memory/scoped_refptr.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "third_party/libyuv/include/libyuv.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "ui/gfx/color_space.h"
namespace viz {
namespace {
// Translate `CopyOutputResult::Format to `SharedImageFormat`
SharedImageFormat GetSharedImageFormatFor(CopyOutputResult::Format format) {
switch (format) {
case CopyOutputResult::Format::RGBA:
return SinglePlaneFormat::kRGBA_8888;
case CopyOutputResult::Format::I420_PLANES:
return MultiPlaneFormat::kI420;
case CopyOutputResult::Format::NV12:
return MultiPlaneFormat::kNV12;
}
}
} // namespace
CopyOutputResult::TextureResult::TextureResult(
const CopyOutputResult::TextureResult& other) = default;
CopyOutputResult::TextureResult& CopyOutputResult::TextureResult::operator=(
const CopyOutputResult::TextureResult& other) = default;
CopyOutputResult::TextureResult::TextureResult(
const gpu::Mailbox& mailbox,
const gfx::ColorSpace& color_space)
: mailbox(mailbox), color_space(color_space) {}
CopyOutputResult::CopyOutputResult(Format format,
Destination destination,
const gfx::Rect& rect,
bool needs_lock_for_bitmap)
: format_(format),
destination_(destination),
rect_(rect),
needs_lock_for_bitmap_(needs_lock_for_bitmap) {
DCHECK(format_ == Format::RGBA || format_ == Format::I420_PLANES ||
format == Format::NV12);
DCHECK(destination_ == Destination::kSystemMemory ||
destination_ == Destination::kNativeTextures);
}
CopyOutputResult::~CopyOutputResult() = default;
bool CopyOutputResult::IsEmpty() const {
return rect_.IsEmpty();
}
bool CopyOutputResult::LockSkBitmap() const {
return true;
}
void CopyOutputResult::UnlockSkBitmap() const {}
const SkBitmap& CopyOutputResult::AsSkBitmap() const {
DCHECK(!cached_bitmap_.readyToDraw() || cached_bitmap_.colorSpace());
return cached_bitmap_;
}
CopyOutputResult::ScopedSkBitmap CopyOutputResult::ScopedAccessSkBitmap()
const {
return ScopedSkBitmap(this);
}
const CopyOutputResult::TextureResult* CopyOutputResult::GetTextureResult()
const {
return nullptr;
}
CopyOutputResult::ReleaseCallbacks CopyOutputResult::TakeTextureOwnership() {
return {};
}
scoped_refptr<gpu::ClientSharedImage> CopyOutputResult::GetSharedImage() {
return nullptr;
}
bool CopyOutputResult::ReadI420Planes(base::span<uint8_t> y_out,
int y_out_stride,
base::span<uint8_t> u_out,
int u_out_stride,
base::span<uint8_t> v_out,
int v_out_stride) const {
auto scoped_sk_bitmap = ScopedAccessSkBitmap();
const SkBitmap& bitmap = scoped_sk_bitmap.bitmap();
if (!bitmap.readyToDraw())
return false;
const uint8_t* pixels = static_cast<uint8_t*>(bitmap.getPixels());
// The conversion below ignores color space completely, and it's not even
// sRGB→Rec.709. Unfortunately, hand-optimized routines are not available, and
// a perfect conversion using gfx::ColorTransform would execute way too
// slowly. See SoftwareRenderer for related comments on its lack of color
// space management (due to performance concerns).
// TODO(crbug.com/384959115): Verify span size before calling into libyuv.
if (bitmap.colorType() == kBGRA_8888_SkColorType) {
return 0 == libyuv::ARGBToI420(pixels, bitmap.rowBytes(), y_out.data(),
y_out_stride, u_out.data(), u_out_stride,
v_out.data(), v_out_stride, bitmap.width(),
bitmap.height());
} else if (bitmap.colorType() == kRGBA_8888_SkColorType) {
return 0 == libyuv::ABGRToI420(pixels, bitmap.rowBytes(), y_out.data(),
y_out_stride, u_out.data(), u_out_stride,
v_out.data(), v_out_stride, bitmap.width(),
bitmap.height());
}
// Other SkBitmap color types could be supported, but are currently never
// being used.
NOTIMPLEMENTED() << "Unsupported format, bitmap.colorType()="
<< bitmap.colorType();
return false;
}
bool CopyOutputResult::ReadNV12Planes(base::span<uint8_t> y_out,
int y_out_stride,
base::span<uint8_t> uv_out,
int uv_out_stride) const {
auto scoped_sk_bitmap = ScopedAccessSkBitmap();
const SkBitmap& bitmap = scoped_sk_bitmap.bitmap();
if (!bitmap.readyToDraw())
return false;
const uint8_t* pixels = static_cast<uint8_t*>(bitmap.getPixels());
// The conversion below ignores color space completely, and it's not even
// sRGB→Rec.709. Unfortunately, hand-optimized routines are not available, and
// a perfect conversion using gfx::ColorTransform would execute way too
// slowly. See SoftwareRenderer for related comments on its lack of color
// space management (due to performance concerns).
// TODO(crbug.com/384959115): Verify span size before calling into libyuv.
if (bitmap.colorType() == kBGRA_8888_SkColorType) {
return 0 == libyuv::ARGBToNV12(pixels, bitmap.rowBytes(), y_out.data(),
y_out_stride, uv_out.data(), uv_out_stride,
bitmap.width(), bitmap.height());
} else if (bitmap.colorType() == kRGBA_8888_SkColorType) {
return 0 == libyuv::ABGRToNV12(pixels, bitmap.rowBytes(), y_out.data(),
y_out_stride, uv_out.data(), uv_out_stride,
bitmap.width(), bitmap.height());
}
// Other SkBitmap color types could be supported, but are currently never
// being used.
NOTIMPLEMENTED() << "Unsupported format, bitmap.colorType()="
<< bitmap.colorType();
return false;
}
bool CopyOutputResult::ReadRGBAPlane(base::span<uint8_t> dest,
int stride) const {
auto scoped_sk_bitmap = ScopedAccessSkBitmap();
const SkBitmap& bitmap = scoped_sk_bitmap.bitmap();
if (!bitmap.readyToDraw())
return false;
DCHECK(bitmap.colorSpace());
SkImageInfo image_info =
SkImageInfo::MakeN32(bitmap.width(), bitmap.height(), kPremul_SkAlphaType,
bitmap.refColorSpace());
CHECK_GE(dest.size(), image_info.computeByteSize(stride));
bitmap.readPixels(image_info, dest.data(), stride, 0, 0);
return true;
}
gfx::ColorSpace CopyOutputResult::GetRGBAColorSpace() const {
auto scoped_sk_bitmap = ScopedAccessSkBitmap();
const SkBitmap& bitmap = scoped_sk_bitmap.bitmap();
if (!bitmap.readyToDraw())
return gfx::ColorSpace();
DCHECK(bitmap.colorSpace());
return gfx::ColorSpace(*(bitmap.colorSpace()));
}
CopyOutputSkBitmapResult::CopyOutputSkBitmapResult(const gfx::Rect& rect,
SkBitmap bitmap)
: CopyOutputSkBitmapResult(Format::RGBA, rect, std::move(bitmap)) {}
CopyOutputSkBitmapResult::CopyOutputSkBitmapResult(Format format,
const gfx::Rect& rect,
SkBitmap bitmap)
: CopyOutputResult(format, Destination::kSystemMemory, rect, false) {
if (!rect.IsEmpty()) {
DCHECK(!bitmap.pixelRef() || bitmap.pixelRef()->unique());
DCHECK(!bitmap.readyToDraw() || bitmap.colorSpace());
// Hold a reference to the |bitmap|'s pixels, for AsSkBitmap().
*(cached_bitmap()) = std::move(bitmap);
}
}
const SkBitmap& CopyOutputSkBitmapResult::AsSkBitmap() const {
SkBitmap* const bitmap = cached_bitmap();
if (rect().IsEmpty())
return *bitmap; // Return "null" bitmap for empty result.
const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
rect().width(), rect().height(), bitmap->refColorSpace());
if (bitmap->info() == image_info && bitmap->readyToDraw())
return *bitmap; // Return bitmap in expected format.
// The bitmap is not in the "native optimized" format. Convert it once for
// this and all future calls of this method.
SkBitmap replacement;
replacement.allocPixels(image_info);
replacement.eraseColor(SK_ColorBLACK);
SkPixmap src_pixmap;
if (bitmap->peekPixels(&src_pixmap)) {
// Note: writePixels() can fail, but then the replacement bitmap will be
// left with part/all solid black due to the eraseColor() call above.
replacement.writePixels(src_pixmap);
}
*bitmap = replacement;
return *bitmap;
}
CopyOutputSkBitmapResult::~CopyOutputSkBitmapResult() = default;
CopyOutputTextureResult::CopyOutputTextureResult(
Format format,
const gfx::Rect& rect,
TextureResult texture_result,
ReleaseCallbacks release_callbacks)
: CopyOutputResult(format, Destination::kNativeTextures, rect, false),
texture_result_(std::move(texture_result)),
release_callbacks_(std::move(release_callbacks)) {
// If we're constructing empty result, all mailbox_holders must be zero.
// Otherwise, the first mailbox must be non-zero.
DCHECK_EQ(rect.IsEmpty(), texture_result_.mailbox.IsZero());
// If we're constructing empty result, the callbacks must be empty.
// From definition of implication: p => q <=> !p || q.
DCHECK(!rect.IsEmpty() || release_callbacks_.empty());
// Color space must be valid for non-empty results.
DCHECK(rect.IsEmpty() || texture_result_.color_space.IsValid());
}
CopyOutputTextureResult::~CopyOutputTextureResult() {
for (auto& release_callback : release_callbacks_) {
// No need to check if release_callback is valid, when texture ownership
// is taken away from us, we zero out release_callbacks_ and the loop would
// not be entered.
std::move(release_callback).Run(gpu::SyncToken(), false);
}
}
const CopyOutputResult::TextureResult*
CopyOutputTextureResult::GetTextureResult() const {
return &texture_result_;
}
CopyOutputResult::ReleaseCallbacks
CopyOutputTextureResult::TakeTextureOwnership() {
texture_result_.mailbox = {};
texture_result_.color_space = {};
CopyOutputResult::ReleaseCallbacks result = std::move(release_callbacks_);
release_callbacks_.clear();
return result;
}
scoped_refptr<gpu::ClientSharedImage>
CopyOutputTextureResult::GetSharedImage() {
// matches usage from
// `SkiaOutputSurfaceImplOnGpu::CreateSharedImageRepresentationSkia()`
constexpr gpu::SharedImageUsageSet kUsage =
gpu::SHARED_IMAGE_USAGE_RASTER_READ |
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ |
gpu::SHARED_IMAGE_USAGE_DISPLAY_WRITE;
return {new gpu::ClientSharedImage(
texture_result_.mailbox,
gpu::SharedImageInfo{GetSharedImageFormatFor(format()), size(),
texture_result_.color_space, kUsage,
"CopyOutputResults"})};
}
CopyOutputResult::ScopedSkBitmap::ScopedSkBitmap() = default;
CopyOutputResult::ScopedSkBitmap::ScopedSkBitmap(
const CopyOutputResult* result) {
DCHECK(result);
if (!result->needs_lock_for_bitmap_ || result->LockSkBitmap())
result_ = result;
}
CopyOutputResult::ScopedSkBitmap::ScopedSkBitmap(ScopedSkBitmap&& other) {
*this = std::move(other);
}
CopyOutputResult::ScopedSkBitmap::~ScopedSkBitmap() {
reset();
}
CopyOutputResult::ScopedSkBitmap& CopyOutputResult::ScopedSkBitmap::operator=(
ScopedSkBitmap&& other) {
DCHECK_CALLED_ON_VALID_THREAD(other.thread_checker_);
reset();
std::swap(result_, other.result_);
return *this;
}
void CopyOutputResult::ScopedSkBitmap::reset() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!result_)
return;
if (result_->needs_lock_for_bitmap_) {
#if DCHECK_IS_ON()
// We are going to unlock the content of the bitmap, so we need to make
// sure there is no other ref of the bitmap content.
auto* ref = bitmap().pixelRef();
DCHECK(!ref || ref->unique());
#endif
result_->UnlockSkBitmap();
result_ = nullptr;
}
}
SkBitmap CopyOutputResult::ScopedSkBitmap::GetOutScopedBitmap() const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!result_)
return SkBitmap();
const auto& bitmap = result_->AsSkBitmap();
// If result_->needs_lock_for_bitmap_ is false, then bitmap can be used out of
// the scope.
if (!result_->needs_lock_for_bitmap_)
return bitmap;
// Make a new SkBitmap and copy content to this new SkBitmap.
SkBitmap bitmap_copy;
if (bitmap.readyToDraw()) {
bitmap_copy.allocPixels(bitmap.info());
bitmap.readPixels(bitmap_copy.pixmap(), 0, 0);
}
return bitmap_copy;
}
} // namespace viz
|