Commit cab5fb18 authored by Henrique Ferreiro's avatar Henrique Ferreiro Committed by Commit Bot

Remove lazy initialization of ImageCursors

This code was introduced to support a thread-local CursorFactoryOzone.
Now that the factory is a global singleton, it can be removed. This CL
also modernizes the code.

Bug: 1029142
Change-Id: I2292ccf1d0179f8bf8c3f24507f2e013299b1cfc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2247892
Commit-Queue: Henrique Ferreiro <hferreiro@igalia.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779516}
parent 37af29c0
......@@ -4,14 +4,7 @@
#include "ui/base/cursor/image_cursors.h"
#include <float.h>
#include <stddef.h>
#include "base/check.h"
#include "base/notreached.h"
#include "base/stl_util.h"
#include "base/strings/string16.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/cursor_loader.h"
#include "ui/base/cursor/cursors_aura.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
......@@ -21,7 +14,7 @@ namespace ui {
namespace {
const mojom::CursorType kImageCursorIds[] = {
constexpr mojom::CursorType kImageCursorIds[] = {
mojom::CursorType::kNull,
mojom::CursorType::kPointer,
mojom::CursorType::kNoDrop,
......@@ -56,49 +49,30 @@ const mojom::CursorType kImageCursorIds[] = {
mojom::CursorType::kGrabbing,
};
const mojom::CursorType kAnimatedCursorIds[] = {mojom::CursorType::kWait,
mojom::CursorType::kProgress};
constexpr mojom::CursorType kAnimatedCursorIds[] = {
mojom::CursorType::kWait, mojom::CursorType::kProgress};
} // namespace
ImageCursors::ImageCursors() : cursor_size_(CursorSize::kNormal) {}
ImageCursors::ImageCursors()
: cursor_loader_(CursorLoader::Create()),
cursor_size_(CursorSize::kNormal) {}
ImageCursors::~ImageCursors() {
}
void ImageCursors::Initialize() {
if (!cursor_loader_)
cursor_loader_.reset(CursorLoader::Create());
}
ImageCursors::~ImageCursors() = default;
float ImageCursors::GetScale() const {
if (!cursor_loader_) {
NOTREACHED();
// Returning default on release build as it's not serious enough to crash
// even if this ever happens.
return 1.0f;
}
return cursor_loader_->scale();
}
display::Display::Rotation ImageCursors::GetRotation() const {
if (!cursor_loader_) {
NOTREACHED();
// Returning default on release build as it's not serious enough to crash
// even if this ever happens.
return display::Display::ROTATE_0;
}
return cursor_loader_->rotation();
}
bool ImageCursors::SetDisplay(const display::Display& display,
float scale_factor) {
if (!cursor_loader_) {
cursor_loader_.reset(CursorLoader::Create());
} else if (cursor_loader_->rotation() == display.panel_rotation() &&
cursor_loader_->scale() == scale_factor) {
if (cursor_loader_->rotation() == display.panel_rotation() &&
cursor_loader_->scale() == scale_factor)
return false;
}
cursor_loader_->set_rotation(display.panel_rotation());
cursor_loader_->set_scale(scale_factor);
......@@ -111,25 +85,21 @@ void ImageCursors::ReloadCursors() {
cursor_loader_->UnloadAll();
for (size_t i = 0; i < base::size(kImageCursorIds); ++i) {
for (auto cursor_id : kImageCursorIds) {
int resource_id = -1;
gfx::Point hot_point;
bool success =
GetCursorDataFor(cursor_size_, kImageCursorIds[i], device_scale_factor,
&resource_id, &hot_point);
bool success = GetCursorDataFor(
cursor_size_, cursor_id, device_scale_factor, &resource_id, &hot_point);
DCHECK(success);
cursor_loader_->LoadImageCursor(kImageCursorIds[i], resource_id, hot_point);
cursor_loader_->LoadImageCursor(cursor_id, resource_id, hot_point);
}
for (size_t i = 0; i < base::size(kAnimatedCursorIds); ++i) {
for (auto cursor_id : kAnimatedCursorIds) {
int resource_id = -1;
gfx::Point hot_point;
bool success =
GetAnimatedCursorDataFor(cursor_size_, kAnimatedCursorIds[i],
device_scale_factor, &resource_id, &hot_point);
bool success = GetAnimatedCursorDataFor(
cursor_size_, cursor_id, device_scale_factor, &resource_id, &hot_point);
DCHECK(success);
cursor_loader_->LoadAnimatedCursor(kAnimatedCursorIds[i],
resource_id,
hot_point,
cursor_loader_->LoadAnimatedCursor(cursor_id, resource_id, hot_point,
kAnimatedCursorFrameDelayMs);
}
}
......@@ -140,16 +110,11 @@ void ImageCursors::SetCursorSize(CursorSize cursor_size) {
cursor_size_ = cursor_size;
if (cursor_loader_.get())
ReloadCursors();
ReloadCursors();
}
void ImageCursors::SetPlatformCursor(gfx::NativeCursor* cursor) {
cursor_loader_->SetPlatformCursor(cursor);
}
base::WeakPtr<ImageCursors> ImageCursors::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace ui
......@@ -8,8 +8,6 @@
#include <memory>
#include "base/component_export.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "ui/base/cursor/cursor_size.h"
#include "ui/display/display.h"
#include "ui/gfx/native_widget_types.h"
......@@ -23,16 +21,10 @@ class CursorLoader;
class COMPONENT_EXPORT(UI_BASE_CURSOR) ImageCursors {
public:
ImageCursors();
ImageCursors(const ImageCursors&) = delete;
ImageCursors& operator=(const ImageCursors&) = delete;
~ImageCursors();
// Creates the |cursor_loader_|. This is optional as |cursor_loader_| is
// lazily created if Initialize() isn't explictly called.
// However note that it matters which thread is used to create
// |cursor_loader_| (see CursorLoaderOzone, crbug.com/741106). Thus explicit
// call to Initialize may be useful to ensure initialization happens on the
// right thread.
void Initialize();
// Returns the scale and rotation of the currently loaded cursor.
float GetScale() const;
display::Display::Rotation GetRotation() const;
......@@ -47,17 +39,12 @@ class COMPONENT_EXPORT(UI_BASE_CURSOR) ImageCursors {
// Sets the platform cursor based on the native type of |cursor|.
void SetPlatformCursor(gfx::NativeCursor* cursor);
base::WeakPtr<ImageCursors> GetWeakPtr();
private:
// Reloads the all loaded cursors in the cursor loader.
void ReloadCursors();
std::unique_ptr<CursorLoader> cursor_loader_;
CursorSize cursor_size_;
base::WeakPtrFactory<ImageCursors> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ImageCursors);
};
} // namespace ui
......
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