a65ac8c9318c498a22ca904a00edc1ab5a7994ee
[platform/framework/web/crosswalk.git] / src / cc / layers / picture_image_layer.cc
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.
4
5 #include "cc/layers/picture_image_layer.h"
6
7 #include "cc/layers/picture_image_layer_impl.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9
10 namespace cc {
11
12 scoped_refptr<PictureImageLayer> PictureImageLayer::Create() {
13   return make_scoped_refptr(new PictureImageLayer());
14 }
15
16 PictureImageLayer::PictureImageLayer() : PictureLayer(this) {}
17
18 PictureImageLayer::~PictureImageLayer() {
19   ClearClient();
20 }
21
22 scoped_ptr<LayerImpl> PictureImageLayer::CreateLayerImpl(
23     LayerTreeImpl* tree_impl) {
24   return PictureImageLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
25 }
26
27 bool PictureImageLayer::DrawsContent() const {
28   return !bitmap_.isNull() && PictureLayer::DrawsContent();
29 }
30
31 void PictureImageLayer::SetBitmap(const SkBitmap& bitmap) {
32   // SetBitmap() currently gets called whenever there is any
33   // style change that affects the layer even if that change doesn't
34   // affect the actual contents of the image (e.g. a CSS animation).
35   // With this check in place we avoid unecessary texture uploads.
36   if (bitmap.pixelRef() && bitmap.pixelRef() == bitmap_.pixelRef())
37     return;
38
39   bitmap_ = bitmap;
40   SetNeedsDisplay();
41 }
42
43 void PictureImageLayer::PaintContents(
44     SkCanvas* canvas,
45     const gfx::Rect& clip,
46     gfx::RectF* opaque,
47     ContentLayerClient::GraphicsContextStatus gc_status) {
48   if (!bitmap_.width() || !bitmap_.height())
49     return;
50
51   SkScalar content_to_layer_scale_x =
52       SkFloatToScalar(static_cast<float>(bounds().width()) / bitmap_.width());
53   SkScalar content_to_layer_scale_y =
54       SkFloatToScalar(static_cast<float>(bounds().height()) / bitmap_.height());
55   canvas->scale(content_to_layer_scale_x, content_to_layer_scale_y);
56
57   // Because PictureImageLayer always FillsBoundsCompletely it will not clear
58   // before painting on playback. As a result we must configure the paint to
59   // copy over the uncleared destination, rather than blending with it.
60   SkPaint paint;
61   paint.setXfermodeMode(SkXfermode::kSrc_Mode);
62   canvas->drawBitmap(bitmap_, 0, 0, &paint);
63 }
64
65 bool PictureImageLayer::FillsBoundsCompletely() const {
66   // PictureImageLayer will always paint to the entire layer bounds.
67   return true;
68 }
69
70 }  // namespace cc