blob: a7dae630de402882cb1f593059fd355ce3337471 [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"
Khushal1b8abc012017-08-10 05:16:178#include "base/memory/ptr_util.h"
9#include "cc/paint/paint_image_generator.h"
Vladimir Levin4f2c08c2017-07-28 03:03:2810#include "cc/paint/paint_record.h"
Khushal1b8abc012017-08-10 05:16:1711#include "cc/paint/skia_paint_image_generator.h"
Vladimir Levin4f2c08c2017-07-28 03:03:2812#include "ui/gfx/skia_util.h"
vmpstr94cfa882017-04-14 01:19:3513
14namespace cc {
vmpstr81a39a32017-05-16 19:30:2115namespace {
tzik455b325fa2017-07-20 03:24:5716base::AtomicSequenceNumber s_next_id_;
vmpstr81a39a32017-05-16 19:30:2117}
vmpstr94cfa882017-04-14 01:19:3518
vmpstr55c7657ca2017-04-29 00:46:4819PaintImage::PaintImage() = default;
vmpstr55c7657ca2017-04-29 00:46:4820PaintImage::PaintImage(const PaintImage& other) = default;
21PaintImage::PaintImage(PaintImage&& other) = default;
vmpstr94cfa882017-04-14 01:19:3522PaintImage::~PaintImage() = default;
23
vmpstr55c7657ca2017-04-29 00:46:4824PaintImage& PaintImage::operator=(const PaintImage& other) = default;
25PaintImage& PaintImage::operator=(PaintImage&& other) = default;
26
khushalsagard5e13bf2017-05-17 08:08:5027bool PaintImage::operator==(const PaintImage& other) const {
Khushalb481b282017-08-24 00:06:5328 return sk_image_ == other.sk_image_ && paint_record_ == other.paint_record_ &&
29 paint_record_rect_ == other.paint_record_rect_ &&
30 paint_image_generator_ == other.paint_image_generator_ &&
31 id_ == other.id_ && animation_type_ == other.animation_type_ &&
vmpstr05729e72017-06-06 03:07:1832 completion_state_ == other.completion_state_ &&
Khushalb481b282017-08-24 00:06:5333 subset_rect_ == other.subset_rect_ &&
khushalsagar54b956cf2017-06-21 15:54:0534 frame_count_ == other.frame_count_ &&
35 is_multipart_ == other.is_multipart_;
vmpstr55c7657ca2017-04-29 00:46:4836}
37
vmpstr81a39a32017-05-16 19:30:2138PaintImage::Id PaintImage::GetNextId() {
39 return s_next_id_.GetNext();
40}
41
Vladimir Levin4f2c08c2017-07-28 03:03:2842const sk_sp<SkImage>& PaintImage::GetSkImage() const {
43 if (cached_sk_image_)
44 return cached_sk_image_;
45
46 if (sk_image_) {
47 cached_sk_image_ = sk_image_;
48 } else if (paint_record_) {
49 cached_sk_image_ = SkImage::MakeFromPicture(
50 ToSkPicture(paint_record_, gfx::RectToSkRect(paint_record_rect_)),
51 SkISize::Make(paint_record_rect_.width(), paint_record_rect_.height()),
52 nullptr, nullptr, SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
Khushal1b8abc012017-08-10 05:16:1753 } else if (paint_image_generator_) {
54 cached_sk_image_ = SkImage::MakeFromGenerator(
55 base::MakeUnique<SkiaPaintImageGenerator>(paint_image_generator_));
Vladimir Levin4f2c08c2017-07-28 03:03:2856 }
Khushalb481b282017-08-24 00:06:5357
58 if (!subset_rect_.IsEmpty() && cached_sk_image_) {
59 cached_sk_image_ =
60 cached_sk_image_->makeSubset(gfx::RectToSkIRect(subset_rect_));
61 }
Vladimir Levin4f2c08c2017-07-28 03:03:2862 return cached_sk_image_;
63}
64
Khushalb481b282017-08-24 00:06:5365PaintImage PaintImage::MakeSubset(const gfx::Rect& subset) const {
66 DCHECK(!subset.IsEmpty());
67
68 // If the subset is the same as the image bounds, we can return the same
69 // image.
70 gfx::Rect bounds(width(), height());
71 if (bounds == subset)
72 return *this;
73
74 DCHECK(bounds.Contains(subset))
75 << "Subset should not be greater than the image bounds";
76 PaintImage result(*this);
77 result.subset_rect_ = subset;
78 // Store the subset from the original image.
79 result.subset_rect_.Offset(subset_rect_.x(), subset_rect_.y());
80
81 // Creating the |cached_sk_image_| is an optimization to allow re-use of the
82 // original decode for image subsets in skia, for cases that rely on skia's
83 // image decode cache.
84 // TODO(khushalsagar): Remove this when we no longer have such cases. See
85 // crbug.com/753639.
86 result.cached_sk_image_ =
87 GetSkImage()->makeSubset(gfx::RectToSkIRect(subset));
88 return result;
89}
90
vmpstr94cfa882017-04-14 01:19:3591} // namespace cc