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