1 // Copyright 2010 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.
5 #include "cc/layers/picture_image_layer.h"
7 #include "cc/layers/picture_image_layer_impl.h"
8 #include "cc/playback/display_item_list_settings.h"
9 #include "cc/playback/drawing_display_item.h"
10 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "third_party/skia/include/core/SkImage.h"
12 #include "third_party/skia/include/core/SkPictureRecorder.h"
13 #include "ui/gfx/skia_util.h"
17 scoped_refptr
<PictureImageLayer
> PictureImageLayer::Create(
18 const LayerSettings
& settings
) {
19 return make_scoped_refptr(new PictureImageLayer(settings
));
22 PictureImageLayer::PictureImageLayer(const LayerSettings
& settings
)
23 : PictureLayer(settings
, this) {
26 PictureImageLayer::~PictureImageLayer() {
30 scoped_ptr
<LayerImpl
> PictureImageLayer::CreateLayerImpl(
31 LayerTreeImpl
* tree_impl
) {
32 return PictureImageLayerImpl::Create(tree_impl
, id(), is_mask());
35 bool PictureImageLayer::HasDrawableContent() const {
36 return image_
&& PictureLayer::HasDrawableContent();
39 void PictureImageLayer::SetImage(skia::RefPtr
<const SkImage
> image
) {
40 // SetImage() currently gets called whenever there is any
41 // style change that affects the layer even if that change doesn't
42 // affect the actual contents of the image (e.g. a CSS animation).
43 // With this check in place we avoid unecessary texture uploads.
44 if (image_
.get() == image
.get())
47 image_
= image
.Pass();
48 UpdateDrawsContent(HasDrawableContent());
52 void PictureImageLayer::PaintContents(
54 const gfx::Rect
& clip
,
55 ContentLayerClient::PaintingControlSetting painting_control
) {
57 DCHECK_GT(image_
->width(), 0);
58 DCHECK_GT(image_
->height(), 0);
60 SkScalar content_to_layer_scale_x
=
61 SkFloatToScalar(static_cast<float>(bounds().width()) / image_
->width());
62 SkScalar content_to_layer_scale_y
=
63 SkFloatToScalar(static_cast<float>(bounds().height()) / image_
->height());
64 canvas
->scale(content_to_layer_scale_x
, content_to_layer_scale_y
);
66 // Because Android WebView resourceless software draw mode rasters directly
67 // to the root canvas, this draw must use the kSrcOver_Mode so that
68 // transparent images blend correctly.
69 canvas
->drawImage(image_
.get(), 0, 0);
72 scoped_refptr
<DisplayItemList
> PictureImageLayer::PaintContentsToDisplayList(
73 const gfx::Rect
& clip
,
74 ContentLayerClient::PaintingControlSetting painting_control
) {
75 // Picture image layers can be used with GatherPixelRefs, so cached SkPictures
76 // are currently required.
77 DisplayItemListSettings settings
;
78 settings
.use_cached_picture
= true;
79 scoped_refptr
<DisplayItemList
> display_list
=
80 DisplayItemList::Create(clip
, settings
);
82 SkPictureRecorder recorder
;
83 SkCanvas
* canvas
= recorder
.beginRecording(gfx::RectToSkRect(clip
));
84 PaintContents(canvas
, clip
, painting_control
);
86 skia::RefPtr
<SkPicture
> picture
=
87 skia::AdoptRef(recorder
.endRecordingAsPicture());
88 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
89 item
->SetNew(picture
.Pass());
91 display_list
->Finalize();
95 bool PictureImageLayer::FillsBoundsCompletely() const {
99 size_t PictureImageLayer::GetApproximateUnsharedMemoryUsage() const {