diff options
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/cursor/cursor_loader_x11.cc | 87 | ||||
-rw-r--r-- | ui/base/cursor/cursor_loader_x11.h | 10 | ||||
-rw-r--r-- | ui/base/cursor/cursor_loader_x11_unittest.cc | 57 | ||||
-rw-r--r-- | ui/ui_unittests.gypi | 5 |
4 files changed, 118 insertions, 41 deletions
diff --git a/ui/base/cursor/cursor_loader_x11.cc b/ui/base/cursor/cursor_loader_x11.cc index 8d9f36f..e30b24c 100644 --- a/ui/base/cursor/cursor_loader_x11.cc +++ b/ui/base/cursor/cursor_loader_x11.cc @@ -137,25 +137,6 @@ int CursorShapeFromNative(const gfx::NativeCursor& native_cursor) { return XC_left_ptr; } -void ScaleCursorImageAndHotpoint(float scale, - SkBitmap* bitmap, - gfx::Point* hotpoint) { - if (scale < FLT_EPSILON) { - NOTREACHED() << "Scale must be larger than 0."; - scale = 1.0f; - } - - gfx::Size scaled_size = gfx::ToFlooredSize( - gfx::ScaleSize(gfx::Size(bitmap->width(), bitmap->height()), scale)); - - *bitmap = skia::ImageOperations::Resize( - *bitmap, - skia::ImageOperations::RESIZE_BETTER, - scaled_size.width(), - scaled_size.height()); - *hotpoint = gfx::ToFlooredPoint(gfx::ScalePoint(*hotpoint, scale)); -} - } // namespace namespace ui { @@ -181,28 +162,8 @@ void CursorLoaderX11::LoadImageCursor(int id, GetScaleFactorFromScale(display().device_scale_factor())); SkBitmap bitmap = image_rep.sk_bitmap(); gfx::Point hotpoint = hot; - switch (display().rotation()) { - case gfx::Display::ROTATE_0: - break; - case gfx::Display::ROTATE_90: - hotpoint.SetPoint(bitmap.height() - hot.y(), hot.x()); - bitmap = SkBitmapOperations::Rotate( - bitmap, SkBitmapOperations::ROTATION_90_CW); - break; - case gfx::Display::ROTATE_180: - hotpoint.SetPoint(bitmap.width() - hot.x(), bitmap.height() - hot.y()); - bitmap = SkBitmapOperations::Rotate( - bitmap, SkBitmapOperations::ROTATION_180_CW); - break; - case gfx::Display::ROTATE_270: - hotpoint.SetPoint(hot.y(), bitmap.width() - hot.x()); - bitmap = SkBitmapOperations::Rotate( - bitmap, SkBitmapOperations::ROTATION_270_CW); - break; - } - - if (scale() != 1.f) - ScaleCursorImageAndHotpoint(scale(), &bitmap, &hotpoint); + ScaleAndRotateCursorBitmapAndHotpoint( + scale(), display().rotation(), &bitmap, &hotpoint); XcursorImage* x_image = SkBitmapToXcursorImage(&bitmap, hotpoint); cursors_[id] = CreateReffedCustomXCursor(x_image); @@ -298,4 +259,48 @@ bool CursorLoaderX11::IsImageCursor(gfx::NativeCursor native_cursor) { return GetXCursor(CursorShapeFromNative(native_cursor)); } +void ScaleAndRotateCursorBitmapAndHotpoint(float scale, + gfx::Display::Rotation rotation, + SkBitmap* bitmap, + gfx::Point* hotpoint) { + switch (rotation) { + case gfx::Display::ROTATE_0: + break; + case gfx::Display::ROTATE_90: + hotpoint->SetPoint(bitmap->height() - hotpoint->y(), hotpoint->x()); + *bitmap = SkBitmapOperations::Rotate( + *bitmap, SkBitmapOperations::ROTATION_90_CW); + break; + case gfx::Display::ROTATE_180: + hotpoint->SetPoint( + bitmap->width() - hotpoint->x(), bitmap->height() - hotpoint->y()); + *bitmap = SkBitmapOperations::Rotate( + *bitmap, SkBitmapOperations::ROTATION_180_CW); + break; + case gfx::Display::ROTATE_270: + hotpoint->SetPoint(hotpoint->y(), bitmap->width() - hotpoint->x()); + *bitmap = SkBitmapOperations::Rotate( + *bitmap, SkBitmapOperations::ROTATION_270_CW); + break; + } + + if (scale < FLT_EPSILON) { + NOTREACHED() << "Scale must be larger than 0."; + scale = 1.0f; + } + + if (scale == 1.0f) + return; + + gfx::Size scaled_size = gfx::ToFlooredSize( + gfx::ScaleSize(gfx::Size(bitmap->width(), bitmap->height()), scale)); + + *bitmap = skia::ImageOperations::Resize( + *bitmap, + skia::ImageOperations::RESIZE_BETTER, + scaled_size.width(), + scaled_size.height()); + *hotpoint = gfx::ToFlooredPoint(gfx::ScalePoint(*hotpoint, scale)); +} + } // namespace ui diff --git a/ui/base/cursor/cursor_loader_x11.h b/ui/base/cursor/cursor_loader_x11.h index 4947ca0..a0265ff 100644 --- a/ui/base/cursor/cursor_loader_x11.h +++ b/ui/base/cursor/cursor_loader_x11.h @@ -13,6 +13,7 @@ #include "ui/base/cursor/cursor_loader.h" #include "ui/base/ui_export.h" #include "ui/base/x/x11_util.h" +#include "ui/gfx/display.h" namespace ui { @@ -55,6 +56,15 @@ class UI_EXPORT CursorLoaderX11 : public CursorLoader { DISALLOW_COPY_AND_ASSIGN(CursorLoaderX11); }; +// Scale and rotate the cursor's bitmap and hotpoint. +// |bitmap_in_out| and |hotpoint_in_out| are used as +// both input and output. +UI_EXPORT void ScaleAndRotateCursorBitmapAndHotpoint( + float scale, + gfx::Display::Rotation rotation, + SkBitmap* bitmap_in_out, + gfx::Point* hotpoint_in_out); + } // namespace ui #endif // UI_BASE_CURSOR_CURSOR_LOADER_X11_H_ diff --git a/ui/base/cursor/cursor_loader_x11_unittest.cc b/ui/base/cursor/cursor_loader_x11_unittest.cc new file mode 100644 index 0000000..c2413b6 --- /dev/null +++ b/ui/base/cursor/cursor_loader_x11_unittest.cc @@ -0,0 +1,57 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/base/cursor/cursor_loader_x11.h" + +#undef None +#undef Bool + +#include "base/logging.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/skia/include/core/SkBitmap.h" + +namespace ui { + +TEST(CursorLoaderX11Test, ScaleAndRotate) { + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 5); + bitmap.allocPixels(); + + gfx::Point hotpoint(3,4); + + ScaleAndRotateCursorBitmapAndHotpoint(1.0f, + gfx::Display::ROTATE_0, + &bitmap, + &hotpoint); + EXPECT_EQ(10, bitmap.width()); + EXPECT_EQ(5, bitmap.height()); + EXPECT_EQ("3,4", hotpoint.ToString()); + + ScaleAndRotateCursorBitmapAndHotpoint(1.0f, + gfx::Display::ROTATE_90, + &bitmap, + &hotpoint); + + EXPECT_EQ(5, bitmap.width()); + EXPECT_EQ(10, bitmap.height()); + EXPECT_EQ("1,3", hotpoint.ToString()); + + ScaleAndRotateCursorBitmapAndHotpoint(2.0f, + gfx::Display::ROTATE_180, + &bitmap, + &hotpoint); + EXPECT_EQ(10, bitmap.width()); + EXPECT_EQ(20, bitmap.height()); + EXPECT_EQ("8,14", hotpoint.ToString()); + + ScaleAndRotateCursorBitmapAndHotpoint(1.0f, + gfx::Display::ROTATE_270, + &bitmap, + &hotpoint); + EXPECT_EQ(20, bitmap.width()); + EXPECT_EQ(10, bitmap.height()); + EXPECT_EQ("14,2", hotpoint.ToString()); +} + +} // namespace ui diff --git a/ui/ui_unittests.gypi b/ui/ui_unittests.gypi index f76871b..1c63a0d 100644 --- a/ui/ui_unittests.gypi +++ b/ui/ui_unittests.gypi @@ -296,6 +296,11 @@ 'base/view_prop_unittest.cc', ], }], + ['use_x11==1 and use_aura==1', { + 'sources': [ + 'base/cursor/cursor_loader_x11_unittest.cc', + ], + }], ['use_aura==1 or toolkit_views==1', { 'sources': [ 'base/dragdrop/os_exchange_data_unittest.cc', |