vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/paint/paint_image.h" |
Khushal | 1b8abc01 | 2017-08-10 05:16:17 | [diff] [blame] | 6 | |
Gyuyoung Kim | e8366401 | 2017-12-02 00:12:49 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 9 | #include "base/atomic_sequence_num.h" |
Daniel Cheng | c058199 | 2019-03-29 04:52:56 | [diff] [blame] | 10 | #include "base/hash/hash.h" |
Khushal | 01e8678 | 2018-06-26 21:10:53 | [diff] [blame] | 11 | #include "cc/paint/paint_image_builder.h" |
Khushal | 1b8abc01 | 2017-08-10 05:16:17 | [diff] [blame] | 12 | #include "cc/paint/paint_image_generator.h" |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 13 | #include "cc/paint/paint_record.h" |
Xida Chen | c5cb856 | 2019-08-14 22:41:56 | [diff] [blame] | 14 | #include "cc/paint/paint_worklet_input.h" |
Khushal | 1b8abc01 | 2017-08-10 05:16:17 | [diff] [blame] | 15 | #include "cc/paint/skia_paint_image_generator.h" |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 16 | #include "ui/gfx/skia_util.h" |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 17 | |
| 18 | namespace cc { |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 19 | namespace { |
Lei Zhang | 868717c | 2018-05-09 04:33:56 | [diff] [blame] | 20 | base::AtomicSequenceNumber g_next_image_id; |
| 21 | base::AtomicSequenceNumber g_next_image_content_id; |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 22 | base::AtomicSequenceNumber g_next_generator_client_id; |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 23 | } // namespace |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 24 | |
Khushal | 512439c | 2017-10-18 17:36:53 | [diff] [blame] | 25 | const PaintImage::Id PaintImage::kNonLazyStableId = -1; |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 26 | const size_t PaintImage::kDefaultFrameIndex = 0u; |
Khushal | 512439c | 2017-10-18 17:36:53 | [diff] [blame] | 27 | const PaintImage::Id PaintImage::kInvalidId = -2; |
Khushal | cace4d4 | 2018-02-26 22:06:25 | [diff] [blame] | 28 | const PaintImage::ContentId PaintImage::kInvalidContentId = -1; |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 29 | const PaintImage::GeneratorClientId PaintImage::kDefaultGeneratorClientId = 0; |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 30 | |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 31 | PaintImage::PaintImage() = default; |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 32 | PaintImage::PaintImage(const PaintImage& other) = default; |
| 33 | PaintImage::PaintImage(PaintImage&& other) = default; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 34 | PaintImage::~PaintImage() = default; |
| 35 | |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 36 | PaintImage& PaintImage::operator=(const PaintImage& other) = default; |
| 37 | PaintImage& PaintImage::operator=(PaintImage&& other) = default; |
| 38 | |
khushalsagar | d5e13bf | 2017-05-17 08:08:50 | [diff] [blame] | 39 | bool PaintImage::operator==(const PaintImage& other) const { |
Adrienne Walker | 6ac3063 | 2017-11-18 01:52:07 | [diff] [blame] | 40 | if (sk_image_ != other.sk_image_) |
| 41 | return false; |
| 42 | if (paint_record_ != other.paint_record_) |
| 43 | return false; |
| 44 | if (paint_record_rect_ != other.paint_record_rect_) |
| 45 | return false; |
Khushal | 73760a3 | 2018-02-16 19:29:51 | [diff] [blame] | 46 | if (content_id_ != other.content_id_) |
Adrienne Walker | 6ac3063 | 2017-11-18 01:52:07 | [diff] [blame] | 47 | return false; |
| 48 | if (paint_image_generator_ != other.paint_image_generator_) |
| 49 | return false; |
| 50 | if (id_ != other.id_) |
| 51 | return false; |
| 52 | if (animation_type_ != other.animation_type_) |
| 53 | return false; |
| 54 | if (completion_state_ != other.completion_state_) |
| 55 | return false; |
| 56 | if (subset_rect_ != other.subset_rect_) |
| 57 | return false; |
Adrienne Walker | 6ac3063 | 2017-11-18 01:52:07 | [diff] [blame] | 58 | if (is_multipart_ != other.is_multipart_) |
| 59 | return false; |
| 60 | return true; |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 61 | } |
| 62 | |
Vladimir Levin | 9167e1c | 2017-10-18 21:28:24 | [diff] [blame] | 63 | // static |
| 64 | PaintImage::DecodingMode PaintImage::GetConservative(DecodingMode one, |
| 65 | DecodingMode two) { |
| 66 | if (one == two) |
| 67 | return one; |
| 68 | if (one == DecodingMode::kSync || two == DecodingMode::kSync) |
| 69 | return DecodingMode::kSync; |
| 70 | if (one == DecodingMode::kUnspecified || two == DecodingMode::kUnspecified) |
| 71 | return DecodingMode::kUnspecified; |
| 72 | DCHECK_EQ(one, DecodingMode::kAsync); |
| 73 | DCHECK_EQ(two, DecodingMode::kAsync); |
| 74 | return DecodingMode::kAsync; |
| 75 | } |
| 76 | |
| 77 | // static |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 78 | PaintImage::Id PaintImage::GetNextId() { |
Lei Zhang | 868717c | 2018-05-09 04:33:56 | [diff] [blame] | 79 | return g_next_image_id.GetNext(); |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 80 | } |
| 81 | |
Vladimir Levin | 9167e1c | 2017-10-18 21:28:24 | [diff] [blame] | 82 | // static |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 83 | PaintImage::ContentId PaintImage::GetNextContentId() { |
Lei Zhang | 868717c | 2018-05-09 04:33:56 | [diff] [blame] | 84 | return g_next_image_content_id.GetNext(); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 85 | } |
| 86 | |
Khushal | 01e8678 | 2018-06-26 21:10:53 | [diff] [blame] | 87 | // static |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 88 | PaintImage::GeneratorClientId PaintImage::GetNextGeneratorClientId() { |
| 89 | // These IDs must start from 1, since 0 is the kDefaultGeneratorClientId. |
| 90 | return g_next_generator_client_id.GetNext() + 1; |
| 91 | } |
| 92 | |
| 93 | // static |
Khushal | 01e8678 | 2018-06-26 21:10:53 | [diff] [blame] | 94 | PaintImage PaintImage::CreateFromBitmap(SkBitmap bitmap) { |
| 95 | if (bitmap.drawsNothing()) |
| 96 | return PaintImage(); |
| 97 | |
| 98 | return PaintImageBuilder::WithDefault() |
| 99 | .set_id(PaintImage::GetNextId()) |
| 100 | .set_image(SkImage::MakeFromBitmap(bitmap), |
| 101 | PaintImage::GetNextContentId()) |
| 102 | .TakePaintImage(); |
| 103 | } |
| 104 | |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 105 | const sk_sp<SkImage>& PaintImage::GetSkImage() const { |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 106 | return cached_sk_image_; |
| 107 | } |
| 108 | |
Khushal | b481b28 | 2017-08-24 00:06:53 | [diff] [blame] | 109 | PaintImage PaintImage::MakeSubset(const gfx::Rect& subset) const { |
| 110 | DCHECK(!subset.IsEmpty()); |
| 111 | |
| 112 | // If the subset is the same as the image bounds, we can return the same |
| 113 | // image. |
| 114 | gfx::Rect bounds(width(), height()); |
| 115 | if (bounds == subset) |
| 116 | return *this; |
| 117 | |
| 118 | DCHECK(bounds.Contains(subset)) |
| 119 | << "Subset should not be greater than the image bounds"; |
| 120 | PaintImage result(*this); |
| 121 | result.subset_rect_ = subset; |
| 122 | // Store the subset from the original image. |
| 123 | result.subset_rect_.Offset(subset_rect_.x(), subset_rect_.y()); |
| 124 | |
Khushal | a866a80 | 2017-10-05 18:21:00 | [diff] [blame] | 125 | // Creating the |cached_sk_image_| using the SkImage from the original |
| 126 | // PaintImage is an optimization to allow re-use of the original decode for |
| 127 | // image subsets in skia, for cases that rely on skia's image decode cache. |
Khushal | b481b28 | 2017-08-24 00:06:53 | [diff] [blame] | 128 | result.cached_sk_image_ = |
| 129 | GetSkImage()->makeSubset(gfx::RectToSkIRect(subset)); |
| 130 | return result; |
| 131 | } |
| 132 | |
Khushal | a866a80 | 2017-10-05 18:21:00 | [diff] [blame] | 133 | void PaintImage::CreateSkImage() { |
| 134 | DCHECK(!cached_sk_image_); |
| 135 | |
| 136 | if (sk_image_) { |
| 137 | cached_sk_image_ = sk_image_; |
| 138 | } else if (paint_record_) { |
| 139 | cached_sk_image_ = SkImage::MakeFromPicture( |
| 140 | ToSkPicture(paint_record_, gfx::RectToSkRect(paint_record_rect_)), |
| 141 | SkISize::Make(paint_record_rect_.width(), paint_record_rect_.height()), |
| 142 | nullptr, nullptr, SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB()); |
| 143 | } else if (paint_image_generator_) { |
| 144 | cached_sk_image_ = |
Gyuyoung Kim | e8366401 | 2017-12-02 00:12:49 | [diff] [blame] | 145 | SkImage::MakeFromGenerator(std::make_unique<SkiaPaintImageGenerator>( |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 146 | paint_image_generator_, kDefaultFrameIndex, |
| 147 | kDefaultGeneratorClientId)); |
Khushal | a866a80 | 2017-10-05 18:21:00 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | if (!subset_rect_.IsEmpty() && cached_sk_image_) { |
| 151 | cached_sk_image_ = |
| 152 | cached_sk_image_->makeSubset(gfx::RectToSkIRect(subset_rect_)); |
| 153 | } |
| 154 | } |
| 155 | |
Andres Calderon Jaramillo | ae8e59d | 2019-04-01 18:21:34 | [diff] [blame] | 156 | bool PaintImage::IsEligibleForAcceleratedDecoding() const { |
| 157 | if (!CanDecodeFromGenerator()) |
| 158 | return false; |
| 159 | DCHECK(paint_image_generator_); |
| 160 | return paint_image_generator_->IsEligibleForAcceleratedDecoding(); |
| 161 | } |
| 162 | |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 163 | SkISize PaintImage::GetSupportedDecodeSize( |
| 164 | const SkISize& requested_size) const { |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 165 | // TODO(vmpstr): In some cases we do not support decoding to any other |
| 166 | // size than the original. See the comment in CanDecodeFromGenerator() |
| 167 | // for more detail. |
| 168 | if (CanDecodeFromGenerator()) |
Khushal | f9e3faed | 2018-06-20 21:31:13 | [diff] [blame] | 169 | return paint_image_generator_->GetSupportedDecodeSize(requested_size); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 170 | return SkISize::Make(width(), height()); |
| 171 | } |
| 172 | |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 173 | bool PaintImage::Decode(void* memory, |
| 174 | SkImageInfo* info, |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 175 | sk_sp<SkColorSpace> color_space, |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 176 | size_t frame_index, |
| 177 | GeneratorClientId client_id) const { |
Eric Karl | 79c2946 | 2018-01-27 07:19:28 | [diff] [blame] | 178 | // We don't support SkImageInfo's with color spaces on them. Color spaces |
| 179 | // should always be passed via the |color_space| arg. |
| 180 | DCHECK(!info->colorSpace()); |
| 181 | |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 182 | // We only support decode to supported decode size. |
| 183 | DCHECK(info->dimensions() == GetSupportedDecodeSize(info->dimensions())); |
| 184 | |
| 185 | // TODO(vmpstr): In some cases we do not support decoding to any other |
| 186 | // size than the original. See the comment in CanDecodeFromGenerator() |
| 187 | // for more detail. For now, fallback to DecodeFromSkImage(). |
| 188 | if (CanDecodeFromGenerator()) { |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 189 | return DecodeFromGenerator(memory, info, std::move(color_space), |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 190 | frame_index, client_id); |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 191 | } |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 192 | return DecodeFromSkImage(memory, info, std::move(color_space), frame_index, |
| 193 | client_id); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 194 | } |
| 195 | |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 196 | bool PaintImage::DecodeYuv(void* planes[SkYUVASizeInfo::kMaxCount], |
| 197 | size_t frame_index, |
| 198 | GeneratorClientId client_id, |
| 199 | const SkYUVASizeInfo& yuva_size_info) const { |
| 200 | SkYUVAIndex indices[SkYUVAIndex::kIndexCount]; |
| 201 | // Passing nullptr for the SkYUVASizeInfo forces IsYuv to create and fill out |
| 202 | // a temporary object instead because |yuva_size_info| is const. |
| 203 | bool is_yuv = IsYuv(nullptr, indices); |
| 204 | DCHECK(is_yuv); |
| 205 | DCHECK(CanDecodeFromGenerator()); |
| 206 | const uint32_t lazy_pixel_ref = unique_id(); |
| 207 | return paint_image_generator_->GetYUVA8Planes(yuva_size_info, indices, planes, |
| 208 | frame_index, lazy_pixel_ref); |
| 209 | } |
| 210 | |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 211 | bool PaintImage::DecodeFromGenerator(void* memory, |
| 212 | SkImageInfo* info, |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 213 | sk_sp<SkColorSpace> color_space, |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 214 | size_t frame_index, |
| 215 | GeneratorClientId client_id) const { |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 216 | DCHECK(CanDecodeFromGenerator()); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 217 | // First convert the info to have the requested color space, since the decoder |
| 218 | // will convert this for us. |
| 219 | *info = info->makeColorSpace(std::move(color_space)); |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 220 | const uint32_t lazy_pixel_ref = unique_id(); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 221 | return paint_image_generator_->GetPixels(*info, memory, info->minRowBytes(), |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 222 | frame_index, client_id, |
| 223 | lazy_pixel_ref); |
| 224 | } |
| 225 | |
| 226 | // TODO(vmpstr): If we're using a subset_rect_ then the info specifies the |
| 227 | // requested size relative to the subset. However, the generator isn't aware |
| 228 | // of this subsetting and would need a size that is relative to the original |
| 229 | // image size. We could still implement this case, but we need to convert the |
| 230 | // requested size into the space of the original image. |
| 231 | bool PaintImage::CanDecodeFromGenerator() const { |
| 232 | return paint_image_generator_ && subset_rect_.IsEmpty(); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool PaintImage::DecodeFromSkImage(void* memory, |
| 236 | SkImageInfo* info, |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 237 | sk_sp<SkColorSpace> color_space, |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 238 | size_t frame_index, |
| 239 | GeneratorClientId client_id) const { |
| 240 | auto image = GetSkImageForFrame(frame_index, client_id); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 241 | DCHECK(image); |
| 242 | if (color_space) { |
Brian Osman | 5459bae1 | 2018-07-17 12:32:26 | [diff] [blame] | 243 | image = image->makeColorSpace(color_space); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 244 | if (!image) |
| 245 | return false; |
| 246 | } |
| 247 | // Note that the readPixels has to happen before converting the info to the |
| 248 | // given color space, since it can produce incorrect results. |
| 249 | bool result = image->readPixels(*info, memory, info->minRowBytes(), 0, 0, |
| 250 | SkImage::kDisallow_CachingHint); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 251 | *info = info->makeColorSpace(std::move(color_space)); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 252 | return result; |
| 253 | } |
| 254 | |
Khushal | b42fb24d | 2017-09-14 19:15:15 | [diff] [blame] | 255 | bool PaintImage::ShouldAnimate() const { |
| 256 | return animation_type_ == AnimationType::ANIMATED && |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 257 | repetition_count_ != kAnimationNone && FrameCount() > 1; |
Khushal | b42fb24d | 2017-09-14 19:15:15 | [diff] [blame] | 258 | } |
| 259 | |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 260 | PaintImage::FrameKey PaintImage::GetKeyForFrame(size_t frame_index) const { |
| 261 | DCHECK_LT(frame_index, FrameCount()); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 262 | |
| 263 | // Query the content id that uniquely identifies the content for this frame |
| 264 | // from the content provider. |
| 265 | ContentId content_id = kInvalidContentId; |
| 266 | if (paint_image_generator_) |
| 267 | content_id = paint_image_generator_->GetContentIdForFrame(frame_index); |
Khushal | 73760a3 | 2018-02-16 19:29:51 | [diff] [blame] | 268 | else if (paint_record_ || sk_image_) |
| 269 | content_id = content_id_; |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 270 | |
| 271 | DCHECK_NE(content_id, kInvalidContentId); |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 272 | return FrameKey(content_id, frame_index, subset_rect_); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 273 | } |
| 274 | |
Reza.Zakerinasab | 9afecdf | 2018-09-25 11:26:26 | [diff] [blame] | 275 | SkColorType PaintImage::GetColorType() const { |
| 276 | if (paint_image_generator_) |
| 277 | return paint_image_generator_->GetSkImageInfo().colorType(); |
| 278 | if (GetSkImage()) |
| 279 | return GetSkImage()->colorType(); |
| 280 | return kUnknown_SkColorType; |
| 281 | } |
| 282 | |
Xida Chen | c5cb856 | 2019-08-14 22:41:56 | [diff] [blame] | 283 | int PaintImage::width() const { |
| 284 | return paint_worklet_input_ |
| 285 | ? static_cast<int>(paint_worklet_input_->GetSize().width()) |
| 286 | : GetSkImage()->width(); |
| 287 | } |
| 288 | |
| 289 | int PaintImage::height() const { |
| 290 | return paint_worklet_input_ |
| 291 | ? static_cast<int>(paint_worklet_input_->GetSize().height()) |
| 292 | : GetSkImage()->height(); |
| 293 | } |
| 294 | |
Madeleine Barowsky | 5521e0b | 2019-08-08 19:38:10 | [diff] [blame] | 295 | PaintImage::ImageType PaintImage::GetImageType() const { |
| 296 | if (paint_image_generator_) |
| 297 | return paint_image_generator_->GetImageType(); |
| 298 | return PaintImage::ImageType::kInvalid; |
| 299 | } |
| 300 | |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 301 | bool PaintImage::IsYuv(SkYUVASizeInfo* yuva_size_info, |
Madeleine Barowsky | 0c1ac18 | 2019-08-09 23:08:35 | [diff] [blame] | 302 | SkYUVAIndex* plane_indices, |
| 303 | SkYUVColorSpace* yuv_color_space) const { |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 304 | SkYUVASizeInfo temp_yuva_size_info; |
| 305 | SkYUVAIndex temp_plane_indices[SkYUVAIndex::kIndexCount]; |
Madeleine Barowsky | 0c1ac18 | 2019-08-09 23:08:35 | [diff] [blame] | 306 | SkYUVColorSpace temp_yuv_color_space; |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 307 | if (!yuva_size_info) { |
| 308 | yuva_size_info = &temp_yuva_size_info; |
| 309 | } |
| 310 | if (!plane_indices) { |
| 311 | plane_indices = temp_plane_indices; |
| 312 | } |
Madeleine Barowsky | 0c1ac18 | 2019-08-09 23:08:35 | [diff] [blame] | 313 | if (!yuv_color_space) { |
| 314 | yuv_color_space = &temp_yuv_color_space; |
| 315 | } |
| 316 | // ImageDecoder will fill out the value of |yuv_color_space| depending on |
| 317 | // the codec's specification. |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 318 | return CanDecodeFromGenerator() && |
| 319 | paint_image_generator_->QueryYUVA8(yuva_size_info, plane_indices, |
Madeleine Barowsky | 0c1ac18 | 2019-08-09 23:08:35 | [diff] [blame] | 320 | yuv_color_space); |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 321 | } |
| 322 | |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 323 | const std::vector<FrameMetadata>& PaintImage::GetFrameMetadata() const { |
| 324 | DCHECK_EQ(animation_type_, AnimationType::ANIMATED); |
| 325 | DCHECK(paint_image_generator_); |
| 326 | |
| 327 | return paint_image_generator_->GetFrameMetadata(); |
| 328 | } |
| 329 | |
| 330 | size_t PaintImage::FrameCount() const { |
| 331 | if (!GetSkImage()) |
| 332 | return 0u; |
| 333 | return paint_image_generator_ |
| 334 | ? paint_image_generator_->GetFrameMetadata().size() |
| 335 | : 1u; |
| 336 | } |
| 337 | |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 338 | sk_sp<SkImage> PaintImage::GetSkImageForFrame( |
| 339 | size_t index, |
| 340 | GeneratorClientId client_id) const { |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 341 | DCHECK_LT(index, FrameCount()); |
| 342 | |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 343 | // |client_id| and |index| are only relevant for generator backed images which |
| 344 | // perform lazy decoding and can be multi-frame. |
| 345 | if (!paint_image_generator_) { |
| 346 | DCHECK_EQ(index, kDefaultFrameIndex); |
| 347 | return GetSkImage(); |
| 348 | } |
| 349 | |
| 350 | // The internally cached SkImage is constructed using the default frame index |
| 351 | // and GeneratorClientId. Avoid creating a new SkImage. |
| 352 | if (index == kDefaultFrameIndex && client_id == kDefaultGeneratorClientId) |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 353 | return GetSkImage(); |
| 354 | |
Khushal | 123f71d | 2018-08-24 08:21:16 | [diff] [blame] | 355 | sk_sp<SkImage> image = |
| 356 | SkImage::MakeFromGenerator(std::make_unique<SkiaPaintImageGenerator>( |
| 357 | paint_image_generator_, index, client_id)); |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 358 | if (!subset_rect_.IsEmpty()) |
| 359 | image = image->makeSubset(gfx::RectToSkIRect(subset_rect_)); |
| 360 | return image; |
| 361 | } |
| 362 | |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 363 | std::string PaintImage::ToString() const { |
| 364 | std::ostringstream str; |
| 365 | str << "sk_image_: " << sk_image_ << " paint_record_: " << paint_record_ |
| 366 | << " paint_record_rect_: " << paint_record_rect_.ToString() |
| 367 | << " paint_image_generator_: " << paint_image_generator_ |
| 368 | << " id_: " << id_ |
| 369 | << " animation_type_: " << static_cast<int>(animation_type_) |
| 370 | << " completion_state_: " << static_cast<int>(completion_state_) |
| 371 | << " subset_rect_: " << subset_rect_.ToString() |
Madeleine Barowsky | 1052a40 | 2019-01-29 18:46:02 | [diff] [blame] | 372 | << " is_multipart_: " << is_multipart_ << " is YUV: " << IsYuv(); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 373 | return str.str(); |
| 374 | } |
| 375 | |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 376 | PaintImage::FrameKey::FrameKey(ContentId content_id, |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 377 | size_t frame_index, |
| 378 | gfx::Rect subset_rect) |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 379 | : content_id_(content_id), |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 380 | frame_index_(frame_index), |
| 381 | subset_rect_(subset_rect) { |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 382 | size_t original_hash = base::HashInts(static_cast<uint64_t>(content_id_), |
| 383 | static_cast<uint64_t>(frame_index_)); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 384 | if (subset_rect_.IsEmpty()) { |
| 385 | hash_ = original_hash; |
| 386 | } else { |
| 387 | size_t subset_hash = |
| 388 | base::HashInts(static_cast<uint64_t>( |
| 389 | base::HashInts(subset_rect_.x(), subset_rect_.y())), |
| 390 | static_cast<uint64_t>(base::HashInts( |
| 391 | subset_rect_.width(), subset_rect_.height()))); |
| 392 | hash_ = base::HashInts(original_hash, subset_hash); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | bool PaintImage::FrameKey::operator==(const FrameKey& other) const { |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 397 | return content_id_ == other.content_id_ && |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 398 | frame_index_ == other.frame_index_ && |
| 399 | subset_rect_ == other.subset_rect_; |
| 400 | } |
| 401 | |
| 402 | bool PaintImage::FrameKey::operator!=(const FrameKey& other) const { |
| 403 | return !(*this == other); |
| 404 | } |
| 405 | |
| 406 | std::string PaintImage::FrameKey::ToString() const { |
| 407 | std::ostringstream str; |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 408 | str << "content_id: " << content_id_ << "," |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 409 | << "frame_index: " << frame_index_ << "," |
| 410 | << "subset_rect: " << subset_rect_.ToString(); |
| 411 | return str.str(); |
| 412 | } |
| 413 | |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 414 | } // namespace cc |