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" |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 10 | #include "base/hash.h" |
Khushal | 1b8abc01 | 2017-08-10 05:16:17 | [diff] [blame] | 11 | #include "cc/paint/paint_image_generator.h" |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 12 | #include "cc/paint/paint_record.h" |
Khushal | 1b8abc01 | 2017-08-10 05:16:17 | [diff] [blame] | 13 | #include "cc/paint/skia_paint_image_generator.h" |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 14 | #include "ui/gfx/skia_util.h" |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 15 | |
| 16 | namespace cc { |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 17 | namespace { |
Vladimir Levin | 5d3f4ab | 2017-12-12 19:43:38 | [diff] [blame] | 18 | base::AtomicSequenceNumber g_next_id_; |
| 19 | base::AtomicSequenceNumber g_next_content_id_; |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 20 | } // namespace |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 21 | |
Khushal | 512439c | 2017-10-18 17:36:53 | [diff] [blame] | 22 | const PaintImage::Id PaintImage::kNonLazyStableId = -1; |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 23 | const size_t PaintImage::kDefaultFrameIndex = 0u; |
Khushal | 512439c | 2017-10-18 17:36:53 | [diff] [blame] | 24 | const PaintImage::Id PaintImage::kInvalidId = -2; |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 25 | |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 26 | PaintImage::PaintImage() = default; |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 27 | PaintImage::PaintImage(const PaintImage& other) = default; |
| 28 | PaintImage::PaintImage(PaintImage&& other) = default; |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 29 | PaintImage::~PaintImage() = default; |
| 30 | |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 31 | PaintImage& PaintImage::operator=(const PaintImage& other) = default; |
| 32 | PaintImage& PaintImage::operator=(PaintImage&& other) = default; |
| 33 | |
khushalsagar | d5e13bf | 2017-05-17 08:08:50 | [diff] [blame] | 34 | bool PaintImage::operator==(const PaintImage& other) const { |
Adrienne Walker | 6ac3063 | 2017-11-18 01:52:07 | [diff] [blame] | 35 | if (sk_image_ != other.sk_image_) |
| 36 | return false; |
| 37 | if (paint_record_ != other.paint_record_) |
| 38 | return false; |
| 39 | if (paint_record_rect_ != other.paint_record_rect_) |
| 40 | return false; |
| 41 | if (paint_record_content_id_ != other.paint_record_content_id_) |
| 42 | return false; |
| 43 | if (paint_image_generator_ != other.paint_image_generator_) |
| 44 | return false; |
| 45 | if (id_ != other.id_) |
| 46 | return false; |
| 47 | if (animation_type_ != other.animation_type_) |
| 48 | return false; |
| 49 | if (completion_state_ != other.completion_state_) |
| 50 | return false; |
| 51 | if (subset_rect_ != other.subset_rect_) |
| 52 | return false; |
| 53 | if (frame_index_ != other.frame_index_) |
| 54 | return false; |
| 55 | if (is_multipart_ != other.is_multipart_) |
| 56 | return false; |
| 57 | return true; |
vmpstr | 55c7657ca | 2017-04-29 00:46:48 | [diff] [blame] | 58 | } |
| 59 | |
Vladimir Levin | 9167e1c | 2017-10-18 21:28:24 | [diff] [blame] | 60 | // static |
| 61 | PaintImage::DecodingMode PaintImage::GetConservative(DecodingMode one, |
| 62 | DecodingMode two) { |
| 63 | if (one == two) |
| 64 | return one; |
| 65 | if (one == DecodingMode::kSync || two == DecodingMode::kSync) |
| 66 | return DecodingMode::kSync; |
| 67 | if (one == DecodingMode::kUnspecified || two == DecodingMode::kUnspecified) |
| 68 | return DecodingMode::kUnspecified; |
| 69 | DCHECK_EQ(one, DecodingMode::kAsync); |
| 70 | DCHECK_EQ(two, DecodingMode::kAsync); |
| 71 | return DecodingMode::kAsync; |
| 72 | } |
| 73 | |
| 74 | // static |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 75 | PaintImage::Id PaintImage::GetNextId() { |
Vladimir Levin | 5d3f4ab | 2017-12-12 19:43:38 | [diff] [blame] | 76 | return g_next_id_.GetNext(); |
vmpstr | 81a39a3 | 2017-05-16 19:30:21 | [diff] [blame] | 77 | } |
| 78 | |
Vladimir Levin | 9167e1c | 2017-10-18 21:28:24 | [diff] [blame] | 79 | // static |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 80 | PaintImage::ContentId PaintImage::GetNextContentId() { |
Vladimir Levin | 5d3f4ab | 2017-12-12 19:43:38 | [diff] [blame] | 81 | return g_next_content_id_.GetNext(); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 82 | } |
| 83 | |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 84 | const sk_sp<SkImage>& PaintImage::GetSkImage() const { |
Vladimir Levin | 4f2c08c | 2017-07-28 03:03:28 | [diff] [blame] | 85 | return cached_sk_image_; |
| 86 | } |
| 87 | |
Khushal | b481b28 | 2017-08-24 00:06:53 | [diff] [blame] | 88 | PaintImage PaintImage::MakeSubset(const gfx::Rect& subset) const { |
| 89 | DCHECK(!subset.IsEmpty()); |
| 90 | |
| 91 | // If the subset is the same as the image bounds, we can return the same |
| 92 | // image. |
| 93 | gfx::Rect bounds(width(), height()); |
| 94 | if (bounds == subset) |
| 95 | return *this; |
| 96 | |
| 97 | DCHECK(bounds.Contains(subset)) |
| 98 | << "Subset should not be greater than the image bounds"; |
| 99 | PaintImage result(*this); |
| 100 | result.subset_rect_ = subset; |
| 101 | // Store the subset from the original image. |
| 102 | result.subset_rect_.Offset(subset_rect_.x(), subset_rect_.y()); |
| 103 | |
Khushal | a866a80 | 2017-10-05 18:21:00 | [diff] [blame] | 104 | // Creating the |cached_sk_image_| using the SkImage from the original |
| 105 | // PaintImage is an optimization to allow re-use of the original decode for |
| 106 | // image subsets in skia, for cases that rely on skia's image decode cache. |
Khushal | b481b28 | 2017-08-24 00:06:53 | [diff] [blame] | 107 | result.cached_sk_image_ = |
| 108 | GetSkImage()->makeSubset(gfx::RectToSkIRect(subset)); |
| 109 | return result; |
| 110 | } |
| 111 | |
Khushal | a866a80 | 2017-10-05 18:21:00 | [diff] [blame] | 112 | void PaintImage::CreateSkImage() { |
| 113 | DCHECK(!cached_sk_image_); |
| 114 | |
| 115 | if (sk_image_) { |
| 116 | cached_sk_image_ = sk_image_; |
| 117 | } else if (paint_record_) { |
| 118 | cached_sk_image_ = SkImage::MakeFromPicture( |
| 119 | ToSkPicture(paint_record_, gfx::RectToSkRect(paint_record_rect_)), |
| 120 | SkISize::Make(paint_record_rect_.width(), paint_record_rect_.height()), |
| 121 | nullptr, nullptr, SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB()); |
| 122 | } else if (paint_image_generator_) { |
| 123 | cached_sk_image_ = |
Gyuyoung Kim | e8366401 | 2017-12-02 00:12:49 | [diff] [blame] | 124 | SkImage::MakeFromGenerator(std::make_unique<SkiaPaintImageGenerator>( |
Khushal | a866a80 | 2017-10-05 18:21:00 | [diff] [blame] | 125 | paint_image_generator_, frame_index_)); |
| 126 | } |
| 127 | |
| 128 | if (!subset_rect_.IsEmpty() && cached_sk_image_) { |
| 129 | cached_sk_image_ = |
| 130 | cached_sk_image_->makeSubset(gfx::RectToSkIRect(subset_rect_)); |
| 131 | } |
| 132 | } |
| 133 | |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 134 | SkISize PaintImage::GetSupportedDecodeSize( |
| 135 | const SkISize& requested_size) const { |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 136 | // TODO(vmpstr): If this image is using subset_rect, then we don't support |
| 137 | // decoding to any scale other than the original. See the comment in Decode() |
| 138 | // explaining this in more detail. |
| 139 | // TODO(vmpstr): For now, always decode to the original size. This can be |
| 140 | // enabled with the following code, and should be done as a follow-up. |
| 141 | // if (paint_image_generator_ && subset_rect_.IsEmpty()) |
| 142 | // return paint_image_generator_->GetSupportedDecodeSize(requested_size); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 143 | return SkISize::Make(width(), height()); |
| 144 | } |
| 145 | |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 146 | bool PaintImage::Decode(void* memory, |
| 147 | SkImageInfo* info, |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 148 | sk_sp<SkColorSpace> color_space, |
| 149 | size_t frame_index) const { |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 150 | // We only support decode to supported decode size. |
| 151 | DCHECK(info->dimensions() == GetSupportedDecodeSize(info->dimensions())); |
| 152 | |
Eric Karl | 79c2946 | 2018-01-27 07:19:28 | [diff] [blame^] | 153 | // We don't support SkImageInfo's with color spaces on them. Color spaces |
| 154 | // should always be passed via the |color_space| arg. |
| 155 | DCHECK(!info->colorSpace()); |
| 156 | |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 157 | // TODO(vmpstr): If we're using a subset_rect_ then the info specifies the |
| 158 | // requested size relative to the subset. However, the generator isn't aware |
| 159 | // of this subsetting and would need a size that is relative to the original |
| 160 | // image size. We could still implement this case, but we need to convert the |
| 161 | // requested size into the space of the original image. For now, fallback to |
| 162 | // DecodeFromSkImage(). |
| 163 | if (paint_image_generator_ && subset_rect_.IsEmpty()) |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 164 | return DecodeFromGenerator(memory, info, std::move(color_space), |
| 165 | frame_index); |
| 166 | return DecodeFromSkImage(memory, info, std::move(color_space), frame_index); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool PaintImage::DecodeFromGenerator(void* memory, |
| 170 | SkImageInfo* info, |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 171 | sk_sp<SkColorSpace> color_space, |
| 172 | size_t frame_index) const { |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 173 | DCHECK(subset_rect_.IsEmpty()); |
| 174 | |
| 175 | // First convert the info to have the requested color space, since the decoder |
| 176 | // will convert this for us. |
| 177 | *info = info->makeColorSpace(std::move(color_space)); |
| 178 | if (info->colorType() != kN32_SkColorType) { |
| 179 | // Since the decoders only support N32 color types, make one of those and |
| 180 | // decode into temporary memory. Then read the bitmap which will convert it |
| 181 | // to the target color type. |
| 182 | SkImageInfo n32info = info->makeColorType(kN32_SkColorType); |
| 183 | std::unique_ptr<char[]> n32memory( |
| 184 | new char[n32info.minRowBytes() * n32info.height()]); |
| 185 | |
| 186 | bool result = paint_image_generator_->GetPixels(n32info, n32memory.get(), |
| 187 | n32info.minRowBytes(), |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 188 | frame_index, unique_id()); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 189 | if (!result) |
| 190 | return false; |
| 191 | |
| 192 | // The following block will use Skia to do the color type conversion from |
| 193 | // N32 to the destination color type. Since color space conversion was |
| 194 | // already done in GetPixels() above, remove the color space information |
| 195 | // first in case Skia tries to use it for something. In practice, n32info |
| 196 | // and *info color spaces match, so it should work without removing the |
| 197 | // color spaces, but better be safe. |
| 198 | SkImageInfo n32info_no_colorspace = n32info.makeColorSpace(nullptr); |
| 199 | SkImageInfo info_no_colorspace = info->makeColorSpace(nullptr); |
| 200 | |
| 201 | SkBitmap bitmap; |
| 202 | bitmap.installPixels(n32info_no_colorspace, n32memory.get(), |
| 203 | n32info.minRowBytes()); |
| 204 | return bitmap.readPixels(info_no_colorspace, memory, info->minRowBytes(), 0, |
| 205 | 0); |
| 206 | } |
| 207 | |
| 208 | return paint_image_generator_->GetPixels(*info, memory, info->minRowBytes(), |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 209 | frame_index, unique_id()); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | bool PaintImage::DecodeFromSkImage(void* memory, |
| 213 | SkImageInfo* info, |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 214 | sk_sp<SkColorSpace> color_space, |
| 215 | size_t frame_index) const { |
| 216 | auto image = GetSkImageForFrame(frame_index); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 217 | DCHECK(image); |
| 218 | if (color_space) { |
| 219 | image = |
| 220 | image->makeColorSpace(color_space, SkTransferFunctionBehavior::kIgnore); |
| 221 | if (!image) |
| 222 | return false; |
| 223 | } |
| 224 | // Note that the readPixels has to happen before converting the info to the |
| 225 | // given color space, since it can produce incorrect results. |
| 226 | bool result = image->readPixels(*info, memory, info->minRowBytes(), 0, 0, |
| 227 | SkImage::kDisallow_CachingHint); |
Vladimir Levin | c9aa9740 | 2017-09-07 21:24:57 | [diff] [blame] | 228 | *info = info->makeColorSpace(std::move(color_space)); |
Vladimir Levin | 772dc5f | 2017-08-25 01:54:29 | [diff] [blame] | 229 | return result; |
| 230 | } |
| 231 | |
Khushal | b42fb24d | 2017-09-14 19:15:15 | [diff] [blame] | 232 | bool PaintImage::ShouldAnimate() const { |
| 233 | return animation_type_ == AnimationType::ANIMATED && |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 234 | repetition_count_ != kAnimationNone && FrameCount() > 1; |
Khushal | b42fb24d | 2017-09-14 19:15:15 | [diff] [blame] | 235 | } |
| 236 | |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 237 | PaintImage::FrameKey PaintImage::GetKeyForFrame(size_t frame_index) const { |
| 238 | DCHECK_LT(frame_index, FrameCount()); |
| 239 | DCHECK(paint_image_generator_ || paint_record_); |
| 240 | |
| 241 | // Query the content id that uniquely identifies the content for this frame |
| 242 | // from the content provider. |
| 243 | ContentId content_id = kInvalidContentId; |
| 244 | if (paint_image_generator_) |
| 245 | content_id = paint_image_generator_->GetContentIdForFrame(frame_index); |
| 246 | else |
| 247 | content_id = paint_record_content_id_; |
| 248 | |
| 249 | DCHECK_NE(content_id, kInvalidContentId); |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 250 | return FrameKey(content_id, frame_index, subset_rect_); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | const std::vector<FrameMetadata>& PaintImage::GetFrameMetadata() const { |
| 254 | DCHECK_EQ(animation_type_, AnimationType::ANIMATED); |
| 255 | DCHECK(paint_image_generator_); |
| 256 | |
| 257 | return paint_image_generator_->GetFrameMetadata(); |
| 258 | } |
| 259 | |
| 260 | size_t PaintImage::FrameCount() const { |
| 261 | if (!GetSkImage()) |
| 262 | return 0u; |
| 263 | return paint_image_generator_ |
| 264 | ? paint_image_generator_->GetFrameMetadata().size() |
| 265 | : 1u; |
| 266 | } |
| 267 | |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 268 | sk_sp<SkImage> PaintImage::GetSkImageForFrame(size_t index) const { |
| 269 | DCHECK_LT(index, FrameCount()); |
| 270 | |
| 271 | if (index == frame_index_) |
| 272 | return GetSkImage(); |
| 273 | |
| 274 | sk_sp<SkImage> image = SkImage::MakeFromGenerator( |
Gyuyoung Kim | e8366401 | 2017-12-02 00:12:49 | [diff] [blame] | 275 | std::make_unique<SkiaPaintImageGenerator>(paint_image_generator_, index)); |
Khushal | fdacdc9 | 2017-09-22 22:40:52 | [diff] [blame] | 276 | if (!subset_rect_.IsEmpty()) |
| 277 | image = image->makeSubset(gfx::RectToSkIRect(subset_rect_)); |
| 278 | return image; |
| 279 | } |
| 280 | |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 281 | std::string PaintImage::ToString() const { |
| 282 | std::ostringstream str; |
| 283 | str << "sk_image_: " << sk_image_ << " paint_record_: " << paint_record_ |
| 284 | << " paint_record_rect_: " << paint_record_rect_.ToString() |
| 285 | << " paint_image_generator_: " << paint_image_generator_ |
| 286 | << " id_: " << id_ |
| 287 | << " animation_type_: " << static_cast<int>(animation_type_) |
| 288 | << " completion_state_: " << static_cast<int>(completion_state_) |
| 289 | << " subset_rect_: " << subset_rect_.ToString() |
| 290 | << " frame_index_: " << frame_index_ |
Khushal | 65395439 | 2017-09-25 22:20:06 | [diff] [blame] | 291 | << " is_multipart_: " << is_multipart_; |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 292 | return str.str(); |
| 293 | } |
| 294 | |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 295 | PaintImage::FrameKey::FrameKey(ContentId content_id, |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 296 | size_t frame_index, |
| 297 | gfx::Rect subset_rect) |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 298 | : content_id_(content_id), |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 299 | frame_index_(frame_index), |
| 300 | subset_rect_(subset_rect) { |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 301 | size_t original_hash = base::HashInts(static_cast<uint64_t>(content_id_), |
| 302 | static_cast<uint64_t>(frame_index_)); |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 303 | if (subset_rect_.IsEmpty()) { |
| 304 | hash_ = original_hash; |
| 305 | } else { |
| 306 | size_t subset_hash = |
| 307 | base::HashInts(static_cast<uint64_t>( |
| 308 | base::HashInts(subset_rect_.x(), subset_rect_.y())), |
| 309 | static_cast<uint64_t>(base::HashInts( |
| 310 | subset_rect_.width(), subset_rect_.height()))); |
| 311 | hash_ = base::HashInts(original_hash, subset_hash); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | bool PaintImage::FrameKey::operator==(const FrameKey& other) const { |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 316 | return content_id_ == other.content_id_ && |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 317 | frame_index_ == other.frame_index_ && |
| 318 | subset_rect_ == other.subset_rect_; |
| 319 | } |
| 320 | |
| 321 | bool PaintImage::FrameKey::operator!=(const FrameKey& other) const { |
| 322 | return !(*this == other); |
| 323 | } |
| 324 | |
| 325 | std::string PaintImage::FrameKey::ToString() const { |
| 326 | std::ostringstream str; |
Khushal | 7ec0d58a | 2017-09-06 19:51:59 | [diff] [blame] | 327 | str << "content_id: " << content_id_ << "," |
Khushal | b41caaa | 2017-08-31 02:40:16 | [diff] [blame] | 328 | << "frame_index: " << frame_index_ << "," |
| 329 | << "subset_rect: " << subset_rect_.ToString(); |
| 330 | return str.str(); |
| 331 | } |
| 332 | |
vmpstr | 94cfa88 | 2017-04-14 01:19:35 | [diff] [blame] | 333 | } // namespace cc |