| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h" | 5 #include "ui/base/cursor/ozone/bitmap_cursor_factory_ozone.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "ui/base/cursor/cursors_aura.h" | 9 #include "ui/base/cursor/cursors_aura.h" |
| 10 | 10 |
| 11 namespace ui { | 11 namespace ui { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 BitmapCursorOzone* ToBitmapCursorOzone(PlatformCursor cursor) { | 15 BitmapCursorOzone* ToBitmapCursorOzone(PlatformCursor cursor) { |
| 16 return static_cast<BitmapCursorOzone*>(cursor); | 16 return static_cast<BitmapCursorOzone*>(cursor); |
| 17 } | 17 } |
| 18 | 18 |
| 19 PlatformCursor ToPlatformCursor(BitmapCursorOzone* cursor) { | 19 PlatformCursor ToPlatformCursor(BitmapCursorOzone* cursor) { |
| 20 return static_cast<PlatformCursor>(cursor); | 20 return static_cast<PlatformCursor>(cursor); |
| 21 } | 21 } |
| 22 | 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 |
| 23 } // namespace | 31 } // namespace |
| 24 | 32 |
| 25 BitmapCursorFactoryOzone::BitmapCursorFactoryOzone() {} | 33 BitmapCursorFactoryOzone::BitmapCursorFactoryOzone() {} |
| 26 | 34 |
| 27 BitmapCursorFactoryOzone::~BitmapCursorFactoryOzone() {} | 35 BitmapCursorFactoryOzone::~BitmapCursorFactoryOzone() {} |
| 28 | 36 |
| 29 // static | 37 // static |
| 30 scoped_refptr<BitmapCursorOzone> BitmapCursorFactoryOzone::GetBitmapCursor( | 38 scoped_refptr<BitmapCursorOzone> BitmapCursorFactoryOzone::GetBitmapCursor( |
| 31 PlatformCursor platform_cursor) { | 39 PlatformCursor platform_cursor) { |
| 32 return make_scoped_refptr(ToBitmapCursorOzone(platform_cursor)); | 40 return make_scoped_refptr(ToBitmapCursorOzone(platform_cursor)); |
| 33 } | 41 } |
| 34 | 42 |
| 35 PlatformCursor BitmapCursorFactoryOzone::GetDefaultCursor(int type) { | 43 PlatformCursor BitmapCursorFactoryOzone::GetDefaultCursor(int type) { |
| 36 if (type == kCursorNone) | 44 return GetDefaultCursorInternal(type); |
| 37 return NULL; // NULL is used for hidden cursor. | |
| 38 | |
| 39 if (!default_cursors_.count(type)) { | |
| 40 // Create new owned image cursor from default aura bitmap for this type. | |
| 41 SkBitmap bitmap; | |
| 42 gfx::Point hotspot; | |
| 43 CHECK(GetCursorBitmap(type, &bitmap, &hotspot)); | |
| 44 default_cursors_[type] = new BitmapCursorOzone(bitmap, hotspot); | |
| 45 } | |
| 46 | |
| 47 // Returned owned default cursor for this type. | |
| 48 return default_cursors_[type]; | |
| 49 } | 45 } |
| 50 | 46 |
| 51 PlatformCursor BitmapCursorFactoryOzone::CreateImageCursor( | 47 PlatformCursor BitmapCursorFactoryOzone::CreateImageCursor( |
| 52 const SkBitmap& bitmap, | 48 const SkBitmap& bitmap, |
| 53 const gfx::Point& hotspot) { | 49 const gfx::Point& hotspot) { |
| 54 BitmapCursorOzone* cursor = new BitmapCursorOzone(bitmap, hotspot); | 50 BitmapCursorOzone* cursor = new BitmapCursorOzone(bitmap, hotspot); |
| 55 cursor->AddRef(); // Balanced by UnrefImageCursor. | 51 cursor->AddRef(); // Balanced by UnrefImageCursor. |
| 56 return ToPlatformCursor(cursor); | 52 return ToPlatformCursor(cursor); |
| 57 } | 53 } |
| 58 | 54 |
| 59 void BitmapCursorFactoryOzone::RefImageCursor(PlatformCursor cursor) { | 55 void BitmapCursorFactoryOzone::RefImageCursor(PlatformCursor cursor) { |
| 60 ToBitmapCursorOzone(cursor)->AddRef(); | 56 ToBitmapCursorOzone(cursor)->AddRef(); |
| 61 } | 57 } |
| 62 | 58 |
| 63 void BitmapCursorFactoryOzone::UnrefImageCursor(PlatformCursor cursor) { | 59 void BitmapCursorFactoryOzone::UnrefImageCursor(PlatformCursor cursor) { |
| 64 ToBitmapCursorOzone(cursor)->Release(); | 60 ToBitmapCursorOzone(cursor)->Release(); |
| 65 } | 61 } |
| 66 | 62 |
| 63 scoped_refptr<BitmapCursorOzone> |
| 64 BitmapCursorFactoryOzone::GetDefaultCursorInternal(int type) { |
| 65 if (type == kCursorNone) |
| 66 return NULL; // NULL is used for hidden cursor. |
| 67 |
| 68 if (!default_cursors_.count(type)) { |
| 69 // Create new image cursor from default aura bitmap for this type. We hold a |
| 70 // ref forever because clients do not do refcounting for default cursors. |
| 71 scoped_refptr<BitmapCursorOzone> cursor = CreateDefaultBitmapCursor(type); |
| 72 if (!cursor && type != kCursorPointer) |
| 73 cursor = GetDefaultCursorInternal(kCursorPointer); |
| 74 DCHECK(cursor) << "Failed to load default cursor bitmap"; |
| 75 default_cursors_[type] = cursor; |
| 76 } |
| 77 |
| 78 // Returned owned default cursor for this type. |
| 79 return default_cursors_[type]; |
| 80 } |
| 81 |
| 67 } // namespace ui | 82 } // namespace ui |
| OLD | NEW |