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.
5 #include "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h"
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/base/cursor/cursors_aura.h"
15 BitmapCursorOzone
* ToBitmapCursorOzone(PlatformCursor cursor
) {
16 return static_cast<BitmapCursorOzone
*>(cursor
);
19 PlatformCursor
ToPlatformCursor(BitmapCursorOzone
* cursor
) {
20 return static_cast<PlatformCursor
>(cursor
);
23 scoped_refptr
<BitmapCursorOzone
> CreateDefaultBitmapCursor(int type
) {
26 if (GetCursorBitmap(type
, &bitmap
, &hotspot
))
27 return new BitmapCursorOzone(bitmap
, hotspot
);
33 BitmapCursorOzone::BitmapCursorOzone(const SkBitmap
& bitmap
,
34 const gfx::Point
& hotspot
)
35 : hotspot_(hotspot
), frame_delay_ms_(0) {
36 bitmaps_
.push_back(bitmap
);
39 BitmapCursorOzone::BitmapCursorOzone(const std::vector
<SkBitmap
>& bitmaps
,
40 const gfx::Point
& hotspot
,
42 : bitmaps_(bitmaps
), hotspot_(hotspot
), frame_delay_ms_(frame_delay_ms
) {
43 DCHECK_LT(0U, bitmaps
.size());
44 DCHECK_LE(0, frame_delay_ms
);
47 BitmapCursorOzone::~BitmapCursorOzone() {
50 const gfx::Point
& BitmapCursorOzone::hotspot() {
54 const SkBitmap
& BitmapCursorOzone::bitmap() {
58 const std::vector
<SkBitmap
>& BitmapCursorOzone::bitmaps() {
62 int BitmapCursorOzone::frame_delay_ms() {
63 return frame_delay_ms_
;
66 BitmapCursorFactoryOzone::BitmapCursorFactoryOzone() {}
68 BitmapCursorFactoryOzone::~BitmapCursorFactoryOzone() {}
71 scoped_refptr
<BitmapCursorOzone
> BitmapCursorFactoryOzone::GetBitmapCursor(
72 PlatformCursor platform_cursor
) {
73 return make_scoped_refptr(ToBitmapCursorOzone(platform_cursor
));
76 PlatformCursor
BitmapCursorFactoryOzone::GetDefaultCursor(int type
) {
77 return GetDefaultCursorInternal(type
).get();
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
);
88 PlatformCursor
BitmapCursorFactoryOzone::CreateAnimatedCursor(
89 const std::vector
<SkBitmap
>& bitmaps
,
90 const gfx::Point
& hotspot
,
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
);
99 void BitmapCursorFactoryOzone::RefImageCursor(PlatformCursor cursor
) {
100 ToBitmapCursorOzone(cursor
)->AddRef();
103 void BitmapCursorFactoryOzone::UnrefImageCursor(PlatformCursor cursor
) {
104 ToBitmapCursorOzone(cursor
)->Release();
107 scoped_refptr
<BitmapCursorOzone
>
108 BitmapCursorFactoryOzone::GetDefaultCursorInternal(int type
) {
109 if (type
== kCursorNone
)
110 return NULL
; // NULL is used for hidden cursor.
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
;
122 // Returned owned default cursor for this type.
123 return default_cursors_
[type
];