nine_patch_layer.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
1// Copyright 2012 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#ifndef CC_LAYERS_NINE_PATCH_LAYER_H_
6#define CC_LAYERS_NINE_PATCH_LAYER_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "cc/base/cc_export.h"
10#include "cc/layers/layer.h"
11#include "cc/resources/ui_resource_client.h"
12#include "ui/gfx/rect.h"
13
14namespace cc {
15
16class LayerTreeHost;
17class ScopedUIResource;
18
19class CC_EXPORT NinePatchLayer : public Layer {
20 public:
21  static scoped_refptr<NinePatchLayer> Create();
22
23  virtual bool DrawsContent() const OVERRIDE;
24
25  virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE;
26
27  virtual void SetLayerTreeHost(LayerTreeHost* host) OVERRIDE;
28
29  // |border| is the space around the center rectangular region in layer space
30  // (known as aperture in image space).  |border.x()| and |border.y()| are the
31  // size of the left and top boundary, respectively.
32  // |border.width()-border.x()| and |border.height()-border.y()| are the size
33  // of the right and bottom boundary, respectively.
34  void SetBorder(gfx::Rect border);
35
36  // aperture is in the pixel space of the bitmap resource and refers to
37  // the center patch of the ninepatch (which is unused in this
38  // implementation). We split off eight rects surrounding it and stick them
39  // on the edges of the layer. The corners are unscaled, the top and bottom
40  // rects are x-stretched to fit, and the left and right rects are
41  // y-stretched to fit.
42  void SetBitmap(const SkBitmap& skbitmap, gfx::Rect aperture);
43
44  // An alternative way of setting the resource to allow for sharing.
45  void SetUIResourceId(UIResourceId resource_id, gfx::Rect aperture);
46  void SetFillCenter(bool fill_center);
47
48  class UIResourceHolder {
49   public:
50    virtual UIResourceId id() = 0;
51    virtual ~UIResourceHolder();
52  };
53
54 private:
55  NinePatchLayer();
56  virtual ~NinePatchLayer();
57  virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
58      OVERRIDE;
59  void RecreateUIResourceHolder();
60
61  gfx::Rect border_;
62  bool fill_center_;
63  scoped_ptr<UIResourceHolder> ui_resource_holder_;
64  SkBitmap bitmap_;
65
66  // The transparent center region that shows the parent layer's contents in
67  // image space.
68  gfx::Rect image_aperture_;
69
70  DISALLOW_COPY_AND_ASSIGN(NinePatchLayer);
71};
72
73}  // namespace cc
74
75#endif  // CC_LAYERS_NINE_PATCH_LAYER_H_
76