Support swap promises that are pinned to a particular layer tree.
[chromium-blink-merge.git] / cc / layers / picture_image_layer.cc
blob24a00a7ffcf25a3dffa35d3ce9cba63f534d632a
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/drawing_display_item.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkImage.h"
11 #include "third_party/skia/include/core/SkPictureRecorder.h"
12 #include "ui/gfx/skia_util.h"
14 namespace cc {
16 scoped_refptr<PictureImageLayer> PictureImageLayer::Create(
17 const LayerSettings& settings) {
18 return make_scoped_refptr(new PictureImageLayer(settings));
21 PictureImageLayer::PictureImageLayer(const LayerSettings& settings)
22 : PictureLayer(settings, this) {
25 PictureImageLayer::~PictureImageLayer() {
26 ClearClient();
29 scoped_ptr<LayerImpl> PictureImageLayer::CreateLayerImpl(
30 LayerTreeImpl* tree_impl) {
31 return PictureImageLayerImpl::Create(tree_impl, id(), is_mask());
34 bool PictureImageLayer::HasDrawableContent() const {
35 return image_ && PictureLayer::HasDrawableContent();
38 void PictureImageLayer::SetImage(skia::RefPtr<const SkImage> image) {
39 // SetImage() currently gets called whenever there is any
40 // style change that affects the layer even if that change doesn't
41 // affect the actual contents of the image (e.g. a CSS animation).
42 // With this check in place we avoid unecessary texture uploads.
43 if (image_.get() == image.get())
44 return;
46 image_ = image.Pass();
47 UpdateDrawsContent(HasDrawableContent());
48 SetNeedsDisplay();
51 void PictureImageLayer::PaintContents(
52 SkCanvas* canvas,
53 const gfx::Rect& clip,
54 ContentLayerClient::PaintingControlSetting painting_control) {
55 DCHECK(image_);
56 DCHECK_GT(image_->width(), 0);
57 DCHECK_GT(image_->height(), 0);
59 SkScalar content_to_layer_scale_x =
60 SkFloatToScalar(static_cast<float>(bounds().width()) / image_->width());
61 SkScalar content_to_layer_scale_y =
62 SkFloatToScalar(static_cast<float>(bounds().height()) / image_->height());
63 canvas->scale(content_to_layer_scale_x, content_to_layer_scale_y);
65 // Because Android WebView resourceless software draw mode rasters directly
66 // to the root canvas, this draw must use the kSrcOver_Mode so that
67 // transparent images blend correctly.
68 canvas->drawImage(image_.get(), 0, 0);
71 scoped_refptr<DisplayItemList> PictureImageLayer::PaintContentsToDisplayList(
72 const gfx::Rect& clip,
73 ContentLayerClient::PaintingControlSetting painting_control) {
74 // Picture image layers can be used with GatherPixelRefs, so cached SkPictures
75 // are currently required.
76 const bool use_cached_picture = true;
77 scoped_refptr<DisplayItemList> display_list =
78 DisplayItemList::Create(clip, use_cached_picture);
80 SkPictureRecorder recorder;
81 SkCanvas* canvas = recorder.beginRecording(gfx::RectToSkRect(clip));
82 PaintContents(canvas, clip, painting_control);
84 skia::RefPtr<SkPicture> picture =
85 skia::AdoptRef(recorder.endRecordingAsPicture());
86 auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
87 item->SetNew(picture.Pass());
89 display_list->Finalize();
90 return display_list;
93 bool PictureImageLayer::FillsBoundsCompletely() const {
94 return false;
97 size_t PictureImageLayer::GetApproximateUnsharedMemoryUsage() const {
98 return 0;
101 } // namespace cc