Fix flaky PDFExtensionTest
[chromium-blink-merge.git] / cc / layers / picture_image_layer.cc
blob6d903bd40bcadea78d2c687d0d11675d70fbb531
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/SkPictureRecorder.h"
11 #include "ui/gfx/skia_util.h"
13 namespace cc {
15 scoped_refptr<PictureImageLayer> PictureImageLayer::Create(
16 const LayerSettings& settings) {
17 return make_scoped_refptr(new PictureImageLayer(settings));
20 PictureImageLayer::PictureImageLayer(const LayerSettings& settings)
21 : PictureLayer(settings, this) {
24 PictureImageLayer::~PictureImageLayer() {
25 ClearClient();
28 scoped_ptr<LayerImpl> PictureImageLayer::CreateLayerImpl(
29 LayerTreeImpl* tree_impl) {
30 return PictureImageLayerImpl::Create(tree_impl, id(), is_mask());
33 bool PictureImageLayer::HasDrawableContent() const {
34 return !bitmap_.isNull() && PictureLayer::HasDrawableContent();
37 void PictureImageLayer::SetBitmap(const SkBitmap& bitmap) {
38 // SetBitmap() currently gets called whenever there is any
39 // style change that affects the layer even if that change doesn't
40 // affect the actual contents of the image (e.g. a CSS animation).
41 // With this check in place we avoid unecessary texture uploads.
42 if (bitmap.pixelRef() && bitmap.pixelRef() == bitmap_.pixelRef())
43 return;
45 bitmap_ = bitmap;
46 UpdateDrawsContent(HasDrawableContent());
47 SetNeedsDisplay();
50 void PictureImageLayer::PaintContents(
51 SkCanvas* canvas,
52 const gfx::Rect& clip,
53 ContentLayerClient::PaintingControlSetting painting_control) {
54 if (!bitmap_.width() || !bitmap_.height())
55 return;
57 SkScalar content_to_layer_scale_x =
58 SkFloatToScalar(static_cast<float>(bounds().width()) / bitmap_.width());
59 SkScalar content_to_layer_scale_y =
60 SkFloatToScalar(static_cast<float>(bounds().height()) / bitmap_.height());
61 canvas->scale(content_to_layer_scale_x, content_to_layer_scale_y);
63 // Because Android WebView resourceless software draw mode rasters directly
64 // to the root canvas, this draw must use the kSrcOver_Mode so that
65 // transparent images blend correctly.
66 canvas->drawBitmap(bitmap_, 0, 0);
69 scoped_refptr<DisplayItemList> PictureImageLayer::PaintContentsToDisplayList(
70 const gfx::Rect& clip,
71 ContentLayerClient::PaintingControlSetting painting_control) {
72 // Picture image layers can be used with GatherPixelRefs, so cached SkPictures
73 // are currently required.
74 const bool use_cached_picture = true;
75 scoped_refptr<DisplayItemList> display_list =
76 DisplayItemList::Create(clip, use_cached_picture);
78 SkPictureRecorder recorder;
79 SkCanvas* canvas = recorder.beginRecording(gfx::RectToSkRect(clip));
80 PaintContents(canvas, clip, painting_control);
82 skia::RefPtr<SkPicture> picture =
83 skia::AdoptRef(recorder.endRecordingAsPicture());
84 auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
85 item->SetNew(picture.Pass());
87 display_list->Finalize();
88 return display_list;
91 bool PictureImageLayer::FillsBoundsCompletely() const {
92 return false;
95 } // namespace cc