blob: c0a255ca56f655ac59596f21fd9ec80e50583960 [file] [log] [blame]
vmpstr94cfa882017-04-14 01:19:351// 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"
Khushal1b8abc012017-08-10 05:16:176
vmpstr81a39a32017-05-16 19:30:217#include "base/atomic_sequence_num.h"
Khushalb41caaa2017-08-31 02:40:168#include "base/hash.h"
Khushal1b8abc012017-08-10 05:16:179#include "base/memory/ptr_util.h"
10#include "cc/paint/paint_image_generator.h"
Vladimir Levin4f2c08c2017-07-28 03:03:2811#include "cc/paint/paint_record.h"
Khushal1b8abc012017-08-10 05:16:1712#include "cc/paint/skia_paint_image_generator.h"
Vladimir Levin4f2c08c2017-07-28 03:03:2813#include "ui/gfx/skia_util.h"
vmpstr94cfa882017-04-14 01:19:3514
15namespace cc {
vmpstr81a39a32017-05-16 19:30:2116namespace {
tzik455b325fa2017-07-20 03:24:5717base::AtomicSequenceNumber s_next_id_;
Khushalb41caaa2017-08-31 02:40:1618base::AtomicSequenceNumber s_next_content_id_;
19} // namespace
vmpstr94cfa882017-04-14 01:19:3520
Khushalfdacdc92017-09-22 22:40:5221const int PaintImage::kNonLazyStableId = -1;
22const size_t PaintImage::kDefaultFrameIndex = 0u;
23
vmpstr55c7657ca2017-04-29 00:46:4824PaintImage::PaintImage() = default;
vmpstr55c7657ca2017-04-29 00:46:4825PaintImage::PaintImage(const PaintImage& other) = default;
26PaintImage::PaintImage(PaintImage&& other) = default;
vmpstr94cfa882017-04-14 01:19:3527PaintImage::~PaintImage() = default;
28
vmpstr55c7657ca2017-04-29 00:46:4829PaintImage& PaintImage::operator=(const PaintImage& other) = default;
30PaintImage& PaintImage::operator=(PaintImage&& other) = default;
31
khushalsagard5e13bf2017-05-17 08:08:5032bool PaintImage::operator==(const PaintImage& other) const {
Khushalb481b282017-08-24 00:06:5333 return sk_image_ == other.sk_image_ && paint_record_ == other.paint_record_ &&
34 paint_record_rect_ == other.paint_record_rect_ &&
Khushalb41caaa2017-08-31 02:40:1635 paint_record_content_id_ == other.paint_record_content_id_ &&
Khushalb481b282017-08-24 00:06:5336 paint_image_generator_ == other.paint_image_generator_ &&
37 id_ == other.id_ && animation_type_ == other.animation_type_ &&
vmpstr05729e72017-06-06 03:07:1838 completion_state_ == other.completion_state_ &&
Khushalb481b282017-08-24 00:06:5339 subset_rect_ == other.subset_rect_ &&
Khushalb41caaa2017-08-31 02:40:1640 frame_index_ == other.frame_index_ &&
Khushal653954392017-09-25 22:20:0641 is_multipart_ == other.is_multipart_;
vmpstr55c7657ca2017-04-29 00:46:4842}
43
vmpstr81a39a32017-05-16 19:30:2144PaintImage::Id PaintImage::GetNextId() {
45 return s_next_id_.GetNext();
46}
47
Khushalb41caaa2017-08-31 02:40:1648PaintImage::ContentId PaintImage::GetNextContentId() {
49 return s_next_content_id_.GetNext();
50}
51
Vladimir Levin4f2c08c2017-07-28 03:03:2852const 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());
Khushal1b8abc012017-08-10 05:16:1763 } else if (paint_image_generator_) {
Khushalb41caaa2017-08-31 02:40:1664 cached_sk_image_ =
65 SkImage::MakeFromGenerator(base::MakeUnique<SkiaPaintImageGenerator>(
Khushal653954392017-09-25 22:20:0666 paint_image_generator_, frame_index_));
Vladimir Levin4f2c08c2017-07-28 03:03:2867 }
Khushalb481b282017-08-24 00:06:5368
69 if (!subset_rect_.IsEmpty() && cached_sk_image_) {
70 cached_sk_image_ =
71 cached_sk_image_->makeSubset(gfx::RectToSkIRect(subset_rect_));
72 }
Vladimir Levin4f2c08c2017-07-28 03:03:2873 return cached_sk_image_;
74}
75
Khushalb481b282017-08-24 00:06:5376PaintImage 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 Levin772dc5f2017-08-25 01:54:29102SkISize PaintImage::GetSupportedDecodeSize(
103 const SkISize& requested_size) const {
Vladimir Levinc9aa97402017-09-07 21:24:57104 // 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 Levin772dc5f2017-08-25 01:54:29111 return SkISize::Make(width(), height());
112}
113
114SkImageInfo 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
121bool PaintImage::Decode(void* memory,
122 SkImageInfo* info,
Khushalfdacdc92017-09-22 22:40:52123 sk_sp<SkColorSpace> color_space,
124 size_t frame_index) const {
Vladimir Levinc9aa97402017-09-07 21:24:57125 // 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())
Khushalfdacdc92017-09-22 22:40:52135 return DecodeFromGenerator(memory, info, std::move(color_space),
136 frame_index);
137 return DecodeFromSkImage(memory, info, std::move(color_space), frame_index);
Vladimir Levinc9aa97402017-09-07 21:24:57138}
139
140bool PaintImage::DecodeFromGenerator(void* memory,
141 SkImageInfo* info,
Khushalfdacdc92017-09-22 22:40:52142 sk_sp<SkColorSpace> color_space,
143 size_t frame_index) const {
Vladimir Levinc9aa97402017-09-07 21:24:57144 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(),
Khushalfdacdc92017-09-22 22:40:52159 frame_index, unique_id());
Vladimir Levinc9aa97402017-09-07 21:24:57160 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(),
Khushalfdacdc92017-09-22 22:40:52180 frame_index, unique_id());
Vladimir Levinc9aa97402017-09-07 21:24:57181}
182
183bool PaintImage::DecodeFromSkImage(void* memory,
184 SkImageInfo* info,
Khushalfdacdc92017-09-22 22:40:52185 sk_sp<SkColorSpace> color_space,
186 size_t frame_index) const {
187 auto image = GetSkImageForFrame(frame_index);
Vladimir Levin772dc5f2017-08-25 01:54:29188 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 Levinc9aa97402017-09-07 21:24:57199 *info = info->makeColorSpace(std::move(color_space));
Vladimir Levin772dc5f2017-08-25 01:54:29200 return result;
201}
202
Khushalb42fb24d2017-09-14 19:15:15203bool PaintImage::ShouldAnimate() const {
204 return animation_type_ == AnimationType::ANIMATED &&
Khushalfdacdc92017-09-22 22:40:52205 repetition_count_ != kAnimationNone && FrameCount() > 1;
Khushalb42fb24d2017-09-14 19:15:15206}
207
Khushalb41caaa2017-08-31 02:40:16208PaintImage::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);
Khushal7ec0d58a2017-09-06 19:51:59221 return FrameKey(content_id, frame_index, subset_rect_);
Khushalb41caaa2017-08-31 02:40:16222}
223
224const 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
231size_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
Khushalfdacdc92017-09-22 22:40:52239sk_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
Khushalb41caaa2017-08-31 02:40:16252std::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_
Khushal653954392017-09-25 22:20:06262 << " is_multipart_: " << is_multipart_;
Khushalb41caaa2017-08-31 02:40:16263 return str.str();
264}
265
Khushal7ec0d58a2017-09-06 19:51:59266PaintImage::FrameKey::FrameKey(ContentId content_id,
Khushalb41caaa2017-08-31 02:40:16267 size_t frame_index,
268 gfx::Rect subset_rect)
Khushal7ec0d58a2017-09-06 19:51:59269 : content_id_(content_id),
Khushalb41caaa2017-08-31 02:40:16270 frame_index_(frame_index),
271 subset_rect_(subset_rect) {
Khushal7ec0d58a2017-09-06 19:51:59272 size_t original_hash = base::HashInts(static_cast<uint64_t>(content_id_),
273 static_cast<uint64_t>(frame_index_));
Khushalb41caaa2017-08-31 02:40:16274 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
286bool PaintImage::FrameKey::operator==(const FrameKey& other) const {
Khushal7ec0d58a2017-09-06 19:51:59287 return content_id_ == other.content_id_ &&
Khushalb41caaa2017-08-31 02:40:16288 frame_index_ == other.frame_index_ &&
289 subset_rect_ == other.subset_rect_;
290}
291
292bool PaintImage::FrameKey::operator!=(const FrameKey& other) const {
293 return !(*this == other);
294}
295
296std::string PaintImage::FrameKey::ToString() const {
297 std::ostringstream str;
Khushal7ec0d58a2017-09-06 19:51:59298 str << "content_id: " << content_id_ << ","
Khushalb41caaa2017-08-31 02:40:16299 << "frame_index: " << frame_index_ << ","
300 << "subset_rect: " << subset_rect_.ToString();
301 return str.str();
302}
303
vmpstr94cfa882017-04-14 01:19:35304} // namespace cc