Commit baa7e5a4 authored by spang's avatar spang Committed by Commit bot

ozone: Plumb animated cursors from CursorLoaderOzone to CursorFactoryOzone

Slice up the tiled image just like CursorLoaderX11 does and pass it on
to CursorFactory as a vector, hotspot, and frame delay.

Also add stub implementation for BitmapCursorFactoryOzone that just
creates a static cursor with the initial frame.

R=sky
BUG=343245
TEST=viewed css "wait" cursor on cros link_freon

Review URL: https://codereview.chromium.org/543823002

Cr-Commit-Position: refs/heads/master@{#293980}
parent 6fc35d4d
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
#include "ui/base/cursor/cursor_loader_ozone.h" #include "ui/base/cursor/cursor_loader_ozone.h"
#include <vector>
#include "ui/base/cursor/cursor.h" #include "ui/base/cursor/cursor.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/cursor/cursor_util.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/ozone/public/cursor_factory_ozone.h" #include "ui/ozone/public/cursor_factory_ozone.h"
namespace ui { namespace ui {
...@@ -18,21 +19,27 @@ CursorLoaderOzone::~CursorLoaderOzone() {} ...@@ -18,21 +19,27 @@ CursorLoaderOzone::~CursorLoaderOzone() {}
void CursorLoaderOzone::LoadImageCursor(int id, void CursorLoaderOzone::LoadImageCursor(int id,
int resource_id, int resource_id,
const gfx::Point& hot) { const gfx::Point& hot) {
const gfx::ImageSkia* image = SkBitmap bitmap;
ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); gfx::Point hotspot = hot;
const gfx::ImageSkiaRep& image_rep =
image->GetRepresentation(scale()); GetImageCursorBitmap(resource_id, scale(), rotation(), &hotspot, &bitmap);
SkBitmap bitmap = image_rep.sk_bitmap();
cursors_[id] = cursors_[id] =
CursorFactoryOzone::GetInstance()->CreateImageCursor(bitmap, hot); CursorFactoryOzone::GetInstance()->CreateImageCursor(bitmap, hotspot);
} }
void CursorLoaderOzone::LoadAnimatedCursor(int id, void CursorLoaderOzone::LoadAnimatedCursor(int id,
int resource_id, int resource_id,
const gfx::Point& hot, const gfx::Point& hot,
int frame_delay_ms) { int frame_delay_ms) {
// TODO(dnicoara) Add support: crbug.com/343245 std::vector<SkBitmap> bitmaps;
NOTIMPLEMENTED(); gfx::Point hotspot = hot;
GetAnimatedCursorBitmaps(
resource_id, scale(), rotation(), &hotspot, &bitmaps);
cursors_[id] = CursorFactoryOzone::GetInstance()->CreateAnimatedCursor(
bitmaps, hotspot, frame_delay_ms);
} }
void CursorLoaderOzone::UnloadAll() { void CursorLoaderOzone::UnloadAll() {
......
...@@ -12,10 +12,8 @@ ...@@ -12,10 +12,8 @@
#include "skia/ext/image_operations.h" #include "skia/ext/image_operations.h"
#include "ui/base/cursor/cursor.h" #include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/cursor_util.h" #include "ui/base/cursor/cursor_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/x/x11_util.h" #include "ui/base/x/x11_util.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/point_conversions.h" #include "ui/gfx/point_conversions.h"
#include "ui/gfx/size_conversions.h" #include "ui/gfx/size_conversions.h"
#include "ui/gfx/skbitmap_operations.h" #include "ui/gfx/skbitmap_operations.h"
...@@ -156,58 +154,35 @@ CursorLoaderX11::~CursorLoaderX11() { ...@@ -156,58 +154,35 @@ CursorLoaderX11::~CursorLoaderX11() {
void CursorLoaderX11::LoadImageCursor(int id, void CursorLoaderX11::LoadImageCursor(int id,
int resource_id, int resource_id,
const gfx::Point& hot) { const gfx::Point& hot) {
const gfx::ImageSkia* image = SkBitmap bitmap;
ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); gfx::Point hotspot = hot;
const gfx::ImageSkiaRep& image_rep = image->GetRepresentation(scale());
SkBitmap bitmap = image_rep.sk_bitmap();
gfx::Point hotpoint = hot;
// TODO(oshima): The cursor should use resource scale factor when
// fractional scale factor is enabled. crbug.com/372212
ScaleAndRotateCursorBitmapAndHotpoint(
scale() / image_rep.scale(), rotation(), &bitmap, &hotpoint);
XcursorImage* x_image = SkBitmapToXcursorImage(&bitmap, hotpoint); GetImageCursorBitmap(resource_id, scale(), rotation(), &hotspot, &bitmap);
XcursorImage* x_image = SkBitmapToXcursorImage(&bitmap, hotspot);
cursors_[id] = CreateReffedCustomXCursor(x_image); cursors_[id] = CreateReffedCustomXCursor(x_image);
// |image_rep| is owned by the resource bundle. So we do not need to free it.
} }
void CursorLoaderX11::LoadAnimatedCursor(int id, void CursorLoaderX11::LoadAnimatedCursor(int id,
int resource_id, int resource_id,
const gfx::Point& hot, const gfx::Point& hot,
int frame_delay_ms) { int frame_delay_ms) {
// TODO(oshima|tdanderson): Support rotation and fractional scale factor. std::vector<SkBitmap> bitmaps;
const gfx::ImageSkia* image = gfx::Point hotspot = hot;
ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
const gfx::ImageSkiaRep& image_rep = image->GetRepresentation(scale());
SkBitmap bitmap = image_rep.sk_bitmap();
int frame_width = bitmap.height();
int frame_height = frame_width;
int total_width = bitmap.width();
DCHECK_EQ(total_width % frame_width, 0);
int frame_count = total_width / frame_width;
DCHECK_GT(frame_count, 0);
XcursorImages* x_images = XcursorImagesCreate(frame_count);
x_images->nimage = frame_count;
for (int frame = 0; frame < frame_count; ++frame) { GetAnimatedCursorBitmaps(
gfx::Point hotpoint = hot; resource_id, scale(), rotation(), &hotspot, &bitmaps);
int x_offset = frame_width * frame;
DCHECK_LE(x_offset + frame_width, total_width);
SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap( XcursorImages* x_images = XcursorImagesCreate(bitmaps.size());
bitmap, x_offset, 0, frame_width, frame_height); x_images->nimage = bitmaps.size();
DCHECK_EQ(frame_width, cropped.width());
DCHECK_EQ(frame_height, cropped.height());
XcursorImage* x_image = SkBitmapToXcursorImage(&cropped, hotpoint);
for (unsigned int frame = 0; frame < bitmaps.size(); ++frame) {
XcursorImage* x_image = SkBitmapToXcursorImage(&bitmaps[frame], hotspot);
x_image->delay = frame_delay_ms; x_image->delay = frame_delay_ms;
x_images->images[frame] = x_image; x_images->images[frame] = x_image;
} }
animated_cursors_[id] = std::make_pair( animated_cursors_[id] = std::make_pair(
XcursorImagesLoadCursor(gfx::GetXDisplay(), x_images), x_images); XcursorImagesLoadCursor(gfx::GetXDisplay(), x_images), x_images);
// |bitmap| is owned by the resource bundle. So we do not need to free it.
} }
void CursorLoaderX11::UnloadAll() { void CursorLoaderX11::UnloadAll() {
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include "base/logging.h" #include "base/logging.h"
#include "skia/ext/image_operations.h" #include "skia/ext/image_operations.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/point_conversions.h" #include "ui/gfx/point_conversions.h"
#include "ui/gfx/size_conversions.h" #include "ui/gfx/size_conversions.h"
#include "ui/gfx/skbitmap_operations.h" #include "ui/gfx/skbitmap_operations.h"
...@@ -57,4 +59,52 @@ void ScaleAndRotateCursorBitmapAndHotpoint(float scale, ...@@ -57,4 +59,52 @@ void ScaleAndRotateCursorBitmapAndHotpoint(float scale,
*hotpoint = gfx::ToFlooredPoint(gfx::ScalePoint(*hotpoint, scale)); *hotpoint = gfx::ToFlooredPoint(gfx::ScalePoint(*hotpoint, scale));
} }
void GetImageCursorBitmap(int resource_id,
float scale,
gfx::Display::Rotation rotation,
gfx::Point* hotspot,
SkBitmap* bitmap) {
const gfx::ImageSkia* image =
ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
const gfx::ImageSkiaRep& image_rep = image->GetRepresentation(scale);
// TODO(oshima): The cursor should use resource scale factor when
// fractional scale factor is enabled. crbug.com/372212
(*bitmap) = image_rep.sk_bitmap();
ScaleAndRotateCursorBitmapAndHotpoint(
scale / image_rep.scale(), rotation, bitmap, hotspot);
// |image_rep| is owned by the resource bundle. So we do not need to free it.
}
void GetAnimatedCursorBitmaps(int resource_id,
float scale,
gfx::Display::Rotation rotation,
gfx::Point* hotspot,
std::vector<SkBitmap>* bitmaps) {
// TODO(oshima|tdanderson): Support rotation and fractional scale factor.
const gfx::ImageSkia* image =
ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
const gfx::ImageSkiaRep& image_rep = image->GetRepresentation(scale);
SkBitmap bitmap = image_rep.sk_bitmap();
int frame_width = bitmap.height();
int frame_height = frame_width;
int total_width = bitmap.width();
DCHECK_EQ(total_width % frame_width, 0);
int frame_count = total_width / frame_width;
DCHECK_GT(frame_count, 0);
bitmaps->resize(frame_count);
for (int frame = 0; frame < frame_count; ++frame) {
int x_offset = frame_width * frame;
DCHECK_LE(x_offset + frame_width, total_width);
SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(
bitmap, x_offset, 0, frame_width, frame_height);
DCHECK_EQ(frame_width, cropped.width());
DCHECK_EQ(frame_height, cropped.height());
(*bitmaps)[frame] = cropped;
}
}
} // namespace ui } // namespace ui
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef UI_BASE_CURSOR_CURSOR_UTIL_H_ #ifndef UI_BASE_CURSOR_CURSOR_UTIL_H_
#define UI_BASE_CURSOR_CURSOR_UTIL_H_ #define UI_BASE_CURSOR_CURSOR_UTIL_H_
#include <vector>
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/ui_base_export.h" #include "ui/base/ui_base_export.h"
...@@ -22,6 +24,18 @@ UI_BASE_EXPORT void ScaleAndRotateCursorBitmapAndHotpoint( ...@@ -22,6 +24,18 @@ UI_BASE_EXPORT void ScaleAndRotateCursorBitmapAndHotpoint(
SkBitmap* bitmap_in_out, SkBitmap* bitmap_in_out,
gfx::Point* hotpoint_in_out); gfx::Point* hotpoint_in_out);
// Helpers for CursorLoader.
void GetImageCursorBitmap(int resource_id,
float scale,
gfx::Display::Rotation rotation,
gfx::Point* hotspot,
SkBitmap* bitmap);
void GetAnimatedCursorBitmaps(int resource_id,
float scale,
gfx::Display::Rotation rotation,
gfx::Point* hotspot,
std::vector<SkBitmap>* bitmaps);
} // namespace ui } // namespace ui
#endif // UI_BASE_CURSOR_CURSOR_UTIL_H_ #endif // UI_BASE_CURSOR_CURSOR_UTIL_H_
...@@ -52,6 +52,15 @@ PlatformCursor BitmapCursorFactoryOzone::CreateImageCursor( ...@@ -52,6 +52,15 @@ PlatformCursor BitmapCursorFactoryOzone::CreateImageCursor(
return ToPlatformCursor(cursor); return ToPlatformCursor(cursor);
} }
PlatformCursor BitmapCursorFactoryOzone::CreateAnimatedCursor(
const std::vector<SkBitmap>& bitmaps,
const gfx::Point& hotspot,
int frame_delay_ms) {
DCHECK_LT(0U, bitmaps.size());
NOTIMPLEMENTED();
return CreateImageCursor(bitmaps[0], hotspot);
}
void BitmapCursorFactoryOzone::RefImageCursor(PlatformCursor cursor) { void BitmapCursorFactoryOzone::RefImageCursor(PlatformCursor cursor) {
ToBitmapCursorOzone(cursor)->AddRef(); ToBitmapCursorOzone(cursor)->AddRef();
} }
......
...@@ -56,6 +56,10 @@ class UI_BASE_EXPORT BitmapCursorFactoryOzone : public CursorFactoryOzone { ...@@ -56,6 +56,10 @@ class UI_BASE_EXPORT BitmapCursorFactoryOzone : public CursorFactoryOzone {
virtual PlatformCursor GetDefaultCursor(int type) OVERRIDE; virtual PlatformCursor GetDefaultCursor(int type) OVERRIDE;
virtual PlatformCursor CreateImageCursor(const SkBitmap& bitmap, virtual PlatformCursor CreateImageCursor(const SkBitmap& bitmap,
const gfx::Point& hotspot) OVERRIDE; const gfx::Point& hotspot) OVERRIDE;
virtual PlatformCursor CreateAnimatedCursor(
const std::vector<SkBitmap>& bitmaps,
const gfx::Point& hotspot,
int frame_delay_ms) OVERRIDE;
virtual void RefImageCursor(PlatformCursor cursor) OVERRIDE; virtual void RefImageCursor(PlatformCursor cursor) OVERRIDE;
virtual void UnrefImageCursor(PlatformCursor cursor) OVERRIDE; virtual void UnrefImageCursor(PlatformCursor cursor) OVERRIDE;
......
...@@ -38,6 +38,14 @@ PlatformCursor CursorFactoryOzone::CreateImageCursor( ...@@ -38,6 +38,14 @@ PlatformCursor CursorFactoryOzone::CreateImageCursor(
return NULL; return NULL;
} }
PlatformCursor CursorFactoryOzone::CreateAnimatedCursor(
const std::vector<SkBitmap>& bitmaps,
const gfx::Point& hotspot,
int frame_delay_ms) {
NOTIMPLEMENTED();
return NULL;
}
void CursorFactoryOzone::RefImageCursor(PlatformCursor cursor) { void CursorFactoryOzone::RefImageCursor(PlatformCursor cursor) {
NOTIMPLEMENTED(); NOTIMPLEMENTED();
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef UI_OZONE_PUBLIC_CURSOR_FACTORY_OZONE_H_ #ifndef UI_OZONE_PUBLIC_CURSOR_FACTORY_OZONE_H_
#define UI_OZONE_PUBLIC_CURSOR_FACTORY_OZONE_H_ #define UI_OZONE_PUBLIC_CURSOR_FACTORY_OZONE_H_
#include <vector>
#include "ui/gfx/native_widget_types.h" #include "ui/gfx/native_widget_types.h"
#include "ui/ozone/ozone_base_export.h" #include "ui/ozone/ozone_base_export.h"
...@@ -35,6 +37,15 @@ class OZONE_BASE_EXPORT CursorFactoryOzone { ...@@ -35,6 +37,15 @@ class OZONE_BASE_EXPORT CursorFactoryOzone {
virtual PlatformCursor CreateImageCursor(const SkBitmap& bitmap, virtual PlatformCursor CreateImageCursor(const SkBitmap& bitmap,
const gfx::Point& hotspot); const gfx::Point& hotspot);
// Return a animated cursor from the specified image & hotspot. Animated
// cursors are referenced counted and have an initial refcount of 1.
// Therefore, each CreateAnimatedCursor call must be matched with a call to
// UnrefImageCursor.
virtual PlatformCursor CreateAnimatedCursor(
const std::vector<SkBitmap>& bitmaps,
const gfx::Point& hotspot,
int frame_delay_ms);
// Increment platform image cursor refcount. // Increment platform image cursor refcount.
virtual void RefImageCursor(PlatformCursor cursor); virtual void RefImageCursor(PlatformCursor cursor);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment