| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/cursor_data_factory_ozone.h" |
| 6 |
| 7 #include "ui/base/cursor/cursor.h" |
| 8 |
| 9 namespace ui { |
| 10 |
| 11 namespace { |
| 12 |
| 13 // A magic value that we store at the start of an instance. |
| 14 const uint32_t kCookie = 0xF60D214C; |
| 15 |
| 16 const uint32_t kBadCookie = 0xBADBADCC; |
| 17 |
| 18 CursorDataOzone* ToCursorDataOzone(PlatformCursor cursor) { |
| 19 CursorDataOzone* ozone = static_cast<CursorDataOzone*>(cursor); |
| 20 #if DCHECK_IS_ON() |
| 21 ozone->AssertIsACusrorDataOzone(); |
| 22 #endif |
| 23 return ozone; |
| 24 } |
| 25 |
| 26 PlatformCursor ToPlatformCursor(CursorDataOzone* cursor) { |
| 27 return static_cast<PlatformCursor>(cursor); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 CursorDataOzone::CursorDataOzone(const ui::CursorData& data) |
| 33 : magic_cookie_(kCookie), data_(data) {} |
| 34 |
| 35 void CursorDataOzone::AssertIsACusrorDataOzone() { |
| 36 CHECK_EQ(magic_cookie_, kCookie); |
| 37 } |
| 38 |
| 39 CursorDataOzone::~CursorDataOzone() { |
| 40 magic_cookie_ = kBadCookie; |
| 41 } |
| 42 |
| 43 CursorDataFactoryOzone::CursorDataFactoryOzone() {} |
| 44 |
| 45 CursorDataFactoryOzone::~CursorDataFactoryOzone() {} |
| 46 |
| 47 // static |
| 48 const ui::CursorData& CursorDataFactoryOzone::GetCursorData( |
| 49 PlatformCursor platform_cursor) { |
| 50 return ToCursorDataOzone(platform_cursor)->data(); |
| 51 } |
| 52 |
| 53 PlatformCursor CursorDataFactoryOzone::GetDefaultCursor(int type) { |
| 54 // Unlike BitmapCursorFactoryOzone, we aren't making heavyweight bitmaps, but |
| 55 // we still have to cache these forever because objects that come out of the |
| 56 // GetDefaultCursor() method aren't treated as refcounted by the ozone |
| 57 // interfaces. |
| 58 return GetDefaultCursorInternal(type).get(); |
| 59 } |
| 60 |
| 61 PlatformCursor CursorDataFactoryOzone::CreateImageCursor( |
| 62 const SkBitmap& bitmap, |
| 63 const gfx::Point& hotspot, |
| 64 float bitmap_dpi) { |
| 65 CursorDataOzone* cursor = new CursorDataOzone( |
| 66 ui::CursorData(hotspot, {bitmap}, bitmap_dpi, base::TimeDelta())); |
| 67 cursor->AddRef(); // Balanced by UnrefImageCursor. |
| 68 return ToPlatformCursor(cursor); |
| 69 } |
| 70 |
| 71 PlatformCursor CursorDataFactoryOzone::CreateAnimatedCursor( |
| 72 const std::vector<SkBitmap>& bitmaps, |
| 73 const gfx::Point& hotspot, |
| 74 int frame_delay_ms, |
| 75 float bitmap_dpi) { |
| 76 CursorDataOzone* cursor = new CursorDataOzone( |
| 77 ui::CursorData(hotspot, bitmaps, bitmap_dpi, |
| 78 base::TimeDelta::FromMilliseconds(frame_delay_ms))); |
| 79 cursor->AddRef(); // Balanced by UnrefImageCursor. |
| 80 return ToPlatformCursor(cursor); |
| 81 } |
| 82 |
| 83 void CursorDataFactoryOzone::RefImageCursor(PlatformCursor cursor) { |
| 84 ToCursorDataOzone(cursor)->AddRef(); |
| 85 } |
| 86 |
| 87 void CursorDataFactoryOzone::UnrefImageCursor(PlatformCursor cursor) { |
| 88 ToCursorDataOzone(cursor)->Release(); |
| 89 } |
| 90 |
| 91 scoped_refptr<CursorDataOzone> CursorDataFactoryOzone::GetDefaultCursorInternal( |
| 92 int type) { |
| 93 if (type == kCursorNone) |
| 94 return nullptr; // nullptr is used for hidden cursor. |
| 95 |
| 96 if (!default_cursors_.count(type)) { |
| 97 // We hold a ref forever because clients do not do refcounting for default |
| 98 // cursors. |
| 99 scoped_refptr<CursorDataOzone> cursor = |
| 100 make_scoped_refptr(new CursorDataOzone(ui::CursorData(type))); |
| 101 default_cursors_[type] = std::move(cursor); |
| 102 } |
| 103 |
| 104 // Returned owned default cursor for this type. |
| 105 return default_cursors_[type]; |
| 106 } |
| 107 |
| 108 } // namespace ui |
| OLD | NEW |