Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / base / cursor / ozone / bitmap_cursor_factory_ozone.cc
1 // Copyright 2014 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 "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h"
6
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/base/cursor/cursors_aura.h"
10
11 namespace ui {
12
13 namespace {
14
15 BitmapCursorOzone* ToBitmapCursorOzone(PlatformCursor cursor) {
16   return static_cast<BitmapCursorOzone*>(cursor);
17 }
18
19 PlatformCursor ToPlatformCursor(BitmapCursorOzone* cursor) {
20   return static_cast<PlatformCursor>(cursor);
21 }
22
23 scoped_refptr<BitmapCursorOzone> CreateDefaultBitmapCursor(int type) {
24   SkBitmap bitmap;
25   gfx::Point hotspot;
26   if (GetCursorBitmap(type, &bitmap, &hotspot))
27     return new BitmapCursorOzone(bitmap, hotspot);
28   return NULL;
29 }
30
31 }  // namespace
32
33 BitmapCursorOzone::BitmapCursorOzone(const SkBitmap& bitmap,
34                                      const gfx::Point& hotspot)
35     : hotspot_(hotspot), frame_delay_ms_(0) {
36   bitmaps_.push_back(bitmap);
37 }
38
39 BitmapCursorOzone::BitmapCursorOzone(const std::vector<SkBitmap>& bitmaps,
40                                      const gfx::Point& hotspot,
41                                      int frame_delay_ms)
42     : bitmaps_(bitmaps), hotspot_(hotspot), frame_delay_ms_(frame_delay_ms) {
43   DCHECK_LT(0U, bitmaps.size());
44   DCHECK_LE(0, frame_delay_ms);
45 }
46
47 BitmapCursorOzone::~BitmapCursorOzone() {
48 }
49
50 const gfx::Point& BitmapCursorOzone::hotspot() {
51   return hotspot_;
52 }
53
54 const SkBitmap& BitmapCursorOzone::bitmap() {
55   return bitmaps_[0];
56 }
57
58 const std::vector<SkBitmap>& BitmapCursorOzone::bitmaps() {
59   return bitmaps_;
60 }
61
62 int BitmapCursorOzone::frame_delay_ms() {
63   return frame_delay_ms_;
64 }
65
66 BitmapCursorFactoryOzone::BitmapCursorFactoryOzone() {}
67
68 BitmapCursorFactoryOzone::~BitmapCursorFactoryOzone() {}
69
70 // static
71 scoped_refptr<BitmapCursorOzone> BitmapCursorFactoryOzone::GetBitmapCursor(
72     PlatformCursor platform_cursor) {
73   return make_scoped_refptr(ToBitmapCursorOzone(platform_cursor));
74 }
75
76 PlatformCursor BitmapCursorFactoryOzone::GetDefaultCursor(int type) {
77   return GetDefaultCursorInternal(type).get();
78 }
79
80 PlatformCursor BitmapCursorFactoryOzone::CreateImageCursor(
81     const SkBitmap& bitmap,
82     const gfx::Point& hotspot) {
83   BitmapCursorOzone* cursor = new BitmapCursorOzone(bitmap, hotspot);
84   cursor->AddRef();  // Balanced by UnrefImageCursor.
85   return ToPlatformCursor(cursor);
86 }
87
88 PlatformCursor BitmapCursorFactoryOzone::CreateAnimatedCursor(
89     const std::vector<SkBitmap>& bitmaps,
90     const gfx::Point& hotspot,
91     int frame_delay_ms) {
92   DCHECK_LT(0U, bitmaps.size());
93   BitmapCursorOzone* cursor =
94       new BitmapCursorOzone(bitmaps, hotspot, frame_delay_ms);
95   cursor->AddRef();  // Balanced by UnrefImageCursor.
96   return ToPlatformCursor(cursor);
97 }
98
99 void BitmapCursorFactoryOzone::RefImageCursor(PlatformCursor cursor) {
100   ToBitmapCursorOzone(cursor)->AddRef();
101 }
102
103 void BitmapCursorFactoryOzone::UnrefImageCursor(PlatformCursor cursor) {
104   ToBitmapCursorOzone(cursor)->Release();
105 }
106
107 scoped_refptr<BitmapCursorOzone>
108 BitmapCursorFactoryOzone::GetDefaultCursorInternal(int type) {
109   if (type == kCursorNone)
110     return NULL;  // NULL is used for hidden cursor.
111
112   if (!default_cursors_.count(type)) {
113     // Create new image cursor from default aura bitmap for this type. We hold a
114     // ref forever because clients do not do refcounting for default cursors.
115     scoped_refptr<BitmapCursorOzone> cursor = CreateDefaultBitmapCursor(type);
116     if (!cursor.get() && type != kCursorPointer)
117       cursor = GetDefaultCursorInternal(kCursorPointer);
118     DCHECK(cursor.get()) << "Failed to load default cursor bitmap";
119     default_cursors_[type] = cursor;
120   }
121
122   // Returned owned default cursor for this type.
123   return default_cursors_[type];
124 }
125
126 }  // namespace ui