OLD | NEW |
| (Empty) |
1 // Copyright (c) 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 #include "webkit/glue/webcursor.h" | |
6 | |
7 #include <X11/Xcursor/Xcursor.h> | |
8 #include <X11/Xlib.h> | |
9 #include <X11/cursorfont.h> | |
10 | |
11 #include "base/logging.h" | |
12 #include "skia/ext/image_operations.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" | |
14 #include "ui/base/cursor/cursor.h" | |
15 #include "ui/base/x/x11_util.h" | |
16 #include "ui/gfx/point_conversions.h" | |
17 #include "ui/gfx/size_conversions.h" | |
18 | |
19 const ui::PlatformCursor WebCursor::GetPlatformCursor() { | |
20 if (platform_cursor_) | |
21 return platform_cursor_; | |
22 | |
23 if (custom_data_.size() == 0) | |
24 return 0; | |
25 | |
26 SkBitmap bitmap; | |
27 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
28 custom_size_.width(), custom_size_.height()); | |
29 bitmap.allocPixels(); | |
30 memcpy(bitmap.getAddr32(0, 0), custom_data_.data(), custom_data_.size()); | |
31 | |
32 gfx::Point hotspot = hotspot_; | |
33 if (device_scale_factor_ != custom_scale_) { | |
34 float scale = device_scale_factor_ / custom_scale_; | |
35 gfx::Size scaled_size = | |
36 gfx::ToFlooredSize(gfx::ScaleSize(custom_size_, scale)); | |
37 bitmap = skia::ImageOperations::Resize(bitmap, | |
38 skia::ImageOperations::RESIZE_BETTER, | |
39 scaled_size.width(), | |
40 scaled_size.height()); | |
41 hotspot = gfx::ToFlooredPoint(gfx::ScalePoint(hotspot, scale)); | |
42 } | |
43 | |
44 XcursorImage* image = ui::SkBitmapToXcursorImage(&bitmap, hotspot); | |
45 platform_cursor_ = ui::CreateReffedCustomXCursor(image); | |
46 return platform_cursor_; | |
47 } | |
48 | |
49 void WebCursor::SetDeviceScaleFactor(float scale_factor) { | |
50 if (device_scale_factor_ == scale_factor) | |
51 return; | |
52 | |
53 device_scale_factor_ = scale_factor; | |
54 if (platform_cursor_) | |
55 ui::UnrefCustomXCursor(platform_cursor_); | |
56 platform_cursor_ = 0; | |
57 // It is not necessary to recreate platform_cursor_ yet, since it will be | |
58 // recreated on demand when GetPlatformCursor is called. | |
59 } | |
60 | |
61 void WebCursor::InitPlatformData() { | |
62 platform_cursor_ = 0; | |
63 device_scale_factor_ = 1.f; | |
64 } | |
65 | |
66 bool WebCursor::SerializePlatformData(Pickle* pickle) const { | |
67 return true; | |
68 } | |
69 | |
70 bool WebCursor::DeserializePlatformData(PickleIterator* iter) { | |
71 return true; | |
72 } | |
73 | |
74 bool WebCursor::IsPlatformDataEqual(const WebCursor& other) const { | |
75 return true; | |
76 } | |
77 | |
78 void WebCursor::CleanupPlatformData() { | |
79 if (platform_cursor_) { | |
80 ui::UnrefCustomXCursor(platform_cursor_); | |
81 platform_cursor_ = 0; | |
82 } | |
83 } | |
84 | |
85 void WebCursor::CopyPlatformData(const WebCursor& other) { | |
86 if (platform_cursor_) | |
87 ui::UnrefCustomXCursor(platform_cursor_); | |
88 platform_cursor_ = other.platform_cursor_; | |
89 if (platform_cursor_) | |
90 ui::RefCustomXCursor(platform_cursor_); | |
91 | |
92 device_scale_factor_ = other.device_scale_factor_; | |
93 } | |
OLD | NEW |