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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
cc / paint / paint_image.cc [blame]
// 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 "cc/paint/paint_image.h"
#include <memory>
#include <sstream>
#include <utility>
#include "base/atomic_sequence_num.h"
#include "base/hash/hash.h"
#include "base/logging.h"
#include "base/types/optional_util.h"
#include "cc/paint/paint_image_builder.h"
#include "cc/paint/paint_image_generator.h"
#include "cc/paint/paint_record.h"
#include "cc/paint/skia_paint_image_generator.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/include/core/SkYUVAPixmaps.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
#include "ui/gfx/geometry/skia_conversions.h"
namespace cc {
namespace {
base::AtomicSequenceNumber g_next_image_id;
base::AtomicSequenceNumber g_next_image_content_id;
base::AtomicSequenceNumber g_next_generator_client_id;
} // namespace
const PaintImage::Id PaintImage::kNonLazyStableId = -1;
const size_t PaintImage::kDefaultFrameIndex = 0u;
const PaintImage::Id PaintImage::kInvalidId = -2;
const PaintImage::ContentId PaintImage::kInvalidContentId = -1;
const PaintImage::GeneratorClientId PaintImage::kDefaultGeneratorClientId = 0;
ImageHeaderMetadata::ImageHeaderMetadata() = default;
ImageHeaderMetadata::ImageHeaderMetadata(const ImageHeaderMetadata& other) =
default;
ImageHeaderMetadata& ImageHeaderMetadata::operator=(
const ImageHeaderMetadata& other) = default;
ImageHeaderMetadata::ImageHeaderMetadata::~ImageHeaderMetadata() = default;
PaintImage::PaintImage() = default;
PaintImage::PaintImage(const PaintImage& other) = default;
PaintImage::PaintImage(PaintImage&& other) = default;
PaintImage::~PaintImage() = default;
PaintImage& PaintImage::operator=(const PaintImage& other) = default;
PaintImage& PaintImage::operator=(PaintImage&& other) = default;
bool PaintImage::IsSameForTesting(const PaintImage& other) const {
return sk_image_ == other.sk_image_ &&
!!paint_record_ == !!other.paint_record_ &&
(!paint_record_ ||
&paint_record_->GetFirstOp() == &other.paint_record_->GetFirstOp()) &&
paint_record_rect_ == other.paint_record_rect_ &&
content_id_ == other.content_id_ &&
paint_image_generator_ == other.paint_image_generator_ &&
id_ == other.id_ && animation_type_ == other.animation_type_ &&
completion_state_ == other.completion_state_ &&
is_multipart_ == other.is_multipart_ &&
texture_backing_ == other.texture_backing_ &&
deferred_paint_record_ == other.deferred_paint_record_;
// Do not check may_be_lcp_candidate_ as it should not affect any rendering
// operation, only metrics collection.
}
// static
PaintImage::DecodingMode PaintImage::GetConservative(DecodingMode one,
DecodingMode two) {
if (one == two)
return one;
if (one == DecodingMode::kSync || two == DecodingMode::kSync)
return DecodingMode::kSync;
if (one == DecodingMode::kUnspecified || two == DecodingMode::kUnspecified)
return DecodingMode::kUnspecified;
DCHECK_EQ(one, DecodingMode::kAsync);
DCHECK_EQ(two, DecodingMode::kAsync);
return DecodingMode::kAsync;
}
// static
PaintImage::Id PaintImage::GetNextId() {
return g_next_image_id.GetNext();
}
// static
PaintImage::ContentId PaintImage::GetNextContentId() {
return g_next_image_content_id.GetNext();
}
// static
PaintImage::GeneratorClientId PaintImage::GetNextGeneratorClientId() {
// These IDs must start from 1, since 0 is the kDefaultGeneratorClientId.
return g_next_generator_client_id.GetNext() + 1;
}
// static
PaintImage PaintImage::CreateFromBitmap(SkBitmap bitmap) {
if (bitmap.drawsNothing())
return PaintImage();
return PaintImageBuilder::WithDefault()
.set_id(PaintImage::GetNextId())
.set_image(SkImages::RasterFromBitmap(bitmap),
PaintImage::GetNextContentId())
.TakePaintImage();
}
const sk_sp<SkImage>& PaintImage::GetSkImage() const {
return cached_sk_image_;
}
sk_sp<SkImage> PaintImage::GetSwSkImage() const {
if (texture_backing_) {
auto image = texture_backing_->GetSkImageViaReadback();
if (image && reinterpret_as_srgb_) {
image = image->reinterpretColorSpace(SkColorSpace::MakeSRGB());
}
return image;
} else if (cached_sk_image_ && cached_sk_image_->isTextureBacked()) {
return cached_sk_image_->makeNonTextureImage();
}
return cached_sk_image_;
}
sk_sp<SkImage> PaintImage::GetAcceleratedSkImage() const {
DCHECK(!cached_sk_image_ || cached_sk_image_->isTextureBacked());
return cached_sk_image_;
}
bool PaintImage::readPixels(const SkImageInfo& dst_info,
void* dst_pixels,
size_t dst_row_bytes,
int src_x,
int src_y) const {
if (texture_backing_) {
auto dst_info_adjusted = dst_info;
if (reinterpret_as_srgb_) {
// To reinterpret the read pixels as sRGB, set `dst_info_adjusted`'s
// color space to match the texture's space. This only works when the
// caller expects sRGB pixels.
CHECK(!dst_info.colorSpace() ||
SkColorSpace::Equals(dst_info.colorSpace(),
SkColorSpace::MakeSRGB().get()));
dst_info_adjusted = dst_info.makeColorSpace(
texture_backing_->GetSkImageInfo().refColorSpace());
}
return texture_backing_->readPixels(dst_info_adjusted, dst_pixels,
dst_row_bytes, src_x, src_y);
} else if (cached_sk_image_) {
return cached_sk_image_->readPixels(dst_info, dst_pixels, dst_row_bytes,
src_x, src_y);
}
return false;
}
SkImageInfo PaintImage::GetSkImageInfo(AuxImage aux_image) const {
switch (aux_image) {
case AuxImage::kDefault:
if (paint_image_generator_) {
const auto info = paint_image_generator_->GetSkImageInfo();
return reinterpret_as_srgb_
? info.makeColorSpace(SkColorSpace::MakeSRGB())
: info;
} else if (texture_backing_) {
const auto info = texture_backing_->GetSkImageInfo();
return reinterpret_as_srgb_
? info.makeColorSpace(SkColorSpace::MakeSRGB())
: info;
} else if (cached_sk_image_) {
return cached_sk_image_->imageInfo();
} else if (deferred_paint_record_) {
auto size = deferred_paint_record_->GetSize();
return SkImageInfo::MakeUnknown(base::ClampCeil(size.width()),
base::ClampCeil(size.height()));
}
return SkImageInfo::MakeUnknown();
case AuxImage::kGainmap:
DCHECK(gainmap_paint_image_generator_);
return gainmap_paint_image_generator_->GetSkImageInfo();
}
}
gpu::Mailbox PaintImage::GetMailbox() const {
DCHECK(texture_backing_);
return texture_backing_->GetMailbox();
}
const scoped_refptr<PaintWorkletInput> PaintImage::GetPaintWorkletInput()
const {
if (!IsPaintWorklet()) {
return nullptr;
}
scoped_refptr<PaintWorkletInput> paint_worklet_input(
static_cast<PaintWorkletInput*>(deferred_paint_record().get()));
return paint_worklet_input;
}
bool PaintImage::IsOpaque() const {
if (IsPaintWorklet())
return deferred_paint_record_->KnownToBeOpaque();
return GetSkImageInfo().isOpaque();
}
void PaintImage::CreateSkImage() {
DCHECK(!cached_sk_image_);
if (sk_image_) {
cached_sk_image_ = sk_image_;
} else if (paint_record_) {
cached_sk_image_ = SkImages::DeferredFromPicture(
paint_record_->ToSkPicture(gfx::RectToSkRect(paint_record_rect_)),
SkISize::Make(paint_record_rect_.width(), paint_record_rect_.height()),
nullptr, nullptr, SkImages::BitDepth::kU8, SkColorSpace::MakeSRGB());
} else if (paint_image_generator_) {
cached_sk_image_ = SkImages::DeferredFromGenerator(
std::make_unique<SkiaPaintImageGenerator>(paint_image_generator_,
kDefaultFrameIndex,
kDefaultGeneratorClientId));
} else if (texture_backing_) {
cached_sk_image_ = texture_backing_->GetAcceleratedSkImage();
}
}
SkISize PaintImage::GetSupportedDecodeSize(const SkISize& requested_size,
AuxImage aux_image) const {
switch (aux_image) {
case AuxImage::kDefault:
if (paint_image_generator_) {
return paint_image_generator_->GetSupportedDecodeSize(requested_size);
}
return SkISize::Make(width(), height());
case AuxImage::kGainmap:
return gainmap_paint_image_generator_->GetSupportedDecodeSize(
requested_size);
}
}
bool PaintImage::Decode(SkPixmap pixmap,
size_t frame_index,
AuxImage aux_image,
GeneratorClientId client_id) const {
// We only support decode to supported decode size.
DCHECK(pixmap.dimensions() ==
GetSupportedDecodeSize(pixmap.dimensions(), aux_image));
switch (aux_image) {
case AuxImage::kDefault:
if (reinterpret_as_srgb_) {
// To reinterpret the generated pixels as sRGB, set `pixmap`'s color
// space to match the generator's space. This only works when the
// caller expects sRGB pixels.
CHECK(!pixmap.colorSpace() ||
SkColorSpace::Equals(pixmap.colorSpace(),
SkColorSpace::MakeSRGB().get()));
pixmap.setColorSpace(
paint_image_generator_->GetSkImageInfo().refColorSpace());
}
if (paint_image_generator_) {
return paint_image_generator_->GetPixels(pixmap, frame_index, client_id,
stable_id());
}
return DecodeFromSkImage(pixmap, frame_index, client_id);
case AuxImage::kGainmap:
DCHECK(gainmap_paint_image_generator_);
return gainmap_paint_image_generator_->GetPixels(pixmap, frame_index,
client_id, stable_id());
}
}
bool PaintImage::DecodeYuv(const SkYUVAPixmaps& pixmaps,
size_t frame_index,
AuxImage aux_image,
GeneratorClientId client_id) const {
// This function assumes that the color space being applied to `pixmaps`
// is the same color space returned by `GetSkImageInfo()`.
DCHECK(pixmaps.isValid());
const uint32_t lazy_pixel_ref = stable_id();
switch (aux_image) {
case AuxImage::kDefault:
DCHECK(paint_image_generator_);
return paint_image_generator_->GetYUVAPlanes(pixmaps, frame_index,
lazy_pixel_ref, client_id);
case AuxImage::kGainmap:
DCHECK(gainmap_paint_image_generator_);
return gainmap_paint_image_generator_->GetYUVAPlanes(
pixmaps, frame_index, lazy_pixel_ref, client_id);
}
}
bool PaintImage::DecodeFromSkImage(SkPixmap pixmap,
size_t frame_index,
GeneratorClientId client_id) const {
sk_sp<SkColorSpace> color_space = pixmap.refColorSpace();
auto image = GetSkImageForFrame(frame_index, client_id);
DCHECK(image);
if (color_space) {
image = image->makeColorSpace(static_cast<GrDirectContext*>(nullptr),
color_space);
if (!image)
return false;
}
return image->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint);
}
bool PaintImage::ShouldAnimate() const {
return animation_type_ == AnimationType::kAnimated &&
repetition_count_ != kAnimationNone && FrameCount() > 1;
}
PaintImage::FrameKey PaintImage::GetKeyForFrame(size_t frame_index) const {
DCHECK_LT(frame_index, FrameCount());
return FrameKey(GetContentIdForFrame(frame_index), frame_index);
}
PaintImage::ContentId PaintImage::GetContentIdForFrame(
size_t frame_index) const {
if (paint_image_generator_)
return paint_image_generator_->GetContentIdForFrame(frame_index);
DCHECK_NE(content_id_, kInvalidContentId);
return content_id_;
}
bool PaintImage::IsTextureBacked() const {
if (texture_backing_)
return true;
if (cached_sk_image_)
return cached_sk_image_->isTextureBacked();
return false;
}
void PaintImage::FlushPendingSkiaOps() {
if (texture_backing_)
texture_backing_->FlushPendingSkiaOps();
}
gfx::Size PaintImage::GetSize(AuxImage aux_image) const {
return gfx::SkISizeToSize(GetSkISize(aux_image));
}
gfx::ContentColorUsage PaintImage::GetContentColorUsage(bool* is_hlg) const {
if (is_hlg)
*is_hlg = false;
// Right now, JS paint worklets can only be in sRGB
if (IsPaintWorklet()) {
return gfx::ContentColorUsage::kSRGB;
}
// Gainmap images are always HDR.
if (HasGainmap()) {
return gfx::ContentColorUsage::kHDR;
}
const auto* color_space = GetSkImageInfo().colorSpace();
// Assume the image will be sRGB if we don't know yet.
if (!color_space || color_space->isSRGB()) {
return gfx::ContentColorUsage::kSRGB;
}
skcms_TransferFunction fn;
if (!color_space->isNumericalTransferFn(&fn)) {
if (skcms_TransferFunction_isPQish(&fn))
return gfx::ContentColorUsage::kHDR;
if (skcms_TransferFunction_isHLGish(&fn)) {
if (is_hlg)
*is_hlg = true;
return gfx::ContentColorUsage::kHDR;
}
}
// If it's not HDR and not SRGB, report it as WCG.
return gfx::ContentColorUsage::kWideColorGamut;
}
const ImageHeaderMetadata* PaintImage::GetImageHeaderMetadata() const {
if (paint_image_generator_)
return paint_image_generator_->GetMetadataForDecodeAcceleration();
return nullptr;
}
bool PaintImage::IsYuv(
const SkYUVAPixmapInfo::SupportedDataTypes& supported_data_types,
AuxImage aux_image,
SkYUVAPixmapInfo* info) const {
SkYUVAPixmapInfo temp_info;
if (!info)
info = &temp_info;
// ImageDecoder will fill out the SkYUVColorSpace in |info| depending on the
// codec's specification.
switch (aux_image) {
case AuxImage::kDefault:
return paint_image_generator_ &&
paint_image_generator_->QueryYUVA(supported_data_types, info);
case AuxImage::kGainmap:
return gainmap_paint_image_generator_ &&
gainmap_paint_image_generator_->QueryYUVA(supported_data_types,
info);
}
}
bool PaintImage::NeedsLayer() const {
return IsPaintWorklet() && deferred_paint_record_->NeedsLayer();
}
const std::vector<FrameMetadata>& PaintImage::GetFrameMetadata() const {
DCHECK_EQ(animation_type_, AnimationType::kAnimated);
DCHECK(paint_image_generator_);
return paint_image_generator_->GetFrameMetadata();
}
size_t PaintImage::FrameCount() const {
if (!*this)
return 0u;
return paint_image_generator_
? paint_image_generator_->GetFrameMetadata().size()
: 1u;
}
sk_sp<SkImage> PaintImage::GetSkImageForFrame(
size_t index,
GeneratorClientId client_id) const {
DCHECK_LT(index, FrameCount());
DCHECK(!IsTextureBacked());
// |client_id| and |index| are only relevant for generator backed images which
// perform lazy decoding and can be multi-frame.
if (!paint_image_generator_) {
DCHECK_EQ(index, kDefaultFrameIndex);
return GetSwSkImage();
}
// The internally cached SkImage is constructed using the default frame index
// and GeneratorClientId. Avoid creating a new SkImage.
if (index == kDefaultFrameIndex && client_id == kDefaultGeneratorClientId)
return GetSwSkImage();
sk_sp<SkImage> image =
SkImages::DeferredFromGenerator(std::make_unique<SkiaPaintImageGenerator>(
paint_image_generator_, index, client_id));
if (image && reinterpret_as_srgb_) {
image = image->reinterpretColorSpace(SkColorSpace::MakeSRGB());
}
return image;
}
std::string PaintImage::ToString() const {
std::ostringstream str;
str << "sk_image_: " << sk_image_
<< " paint_record_: " << base::OptionalToPtr(paint_record_)
<< " paint_record_rect_: " << paint_record_rect_.ToString()
<< " paint_image_generator_: " << paint_image_generator_
<< " id_: " << id_
<< " animation_type_: " << static_cast<int>(animation_type_)
<< " completion_state_: " << static_cast<int>(completion_state_)
<< " is_multipart_: " << is_multipart_
<< " may_be_lcp_candidate_: " << may_be_lcp_candidate_
<< " has gainmap: " << HasGainmap() << " is YUV: "
<< IsYuv(SkYUVAPixmapInfo::SupportedDataTypes::All(), AuxImage::kDefault);
return str.str();
}
PaintImage::FrameKey::FrameKey(ContentId content_id, size_t frame_index)
: content_id_(content_id), frame_index_(frame_index) {
hash_ = base::HashInts(static_cast<uint64_t>(content_id_),
static_cast<uint64_t>(frame_index_));
}
bool PaintImage::FrameKey::operator==(const FrameKey& other) const {
return content_id_ == other.content_id_ && frame_index_ == other.frame_index_;
}
bool PaintImage::FrameKey::operator!=(const FrameKey& other) const {
return !(*this == other);
}
std::string PaintImage::FrameKey::ToString() const {
std::ostringstream str;
str << "content_id: " << content_id_ << ","
<< "frame_index: " << frame_index_;
return str.str();
}
} // namespace cc