Commit 2f1d7794 authored by Julie Jeongeun Kim's avatar Julie Jeongeun Kim Committed by Commit Bot

[ozone/wayland] Add wayland_util.cc/h to create utiliy APIs

It introduces wayland_util.cc/h and moves the code to create shared
memory buffer and draw bitmap on it from WaylandCursor, as it could
be used generally rather than specific to cursor area.
On upcoming patch for drag and drop on Wayland, it's also required
to pass the drag icon surface to Wayland.

It doesn't bring behavioral changes.

Bug: 578890, 875164
Change-Id: I0cd4603c2abacaa899a9fcf19d97186d6f493146
Reviewed-on: https://chromium-review.googlesource.com/1179487
Commit-Queue: Julie Jeongeun Kim <jkim@igalia.com>
Reviewed-by: default avatarAntonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarMaksim Sisov <msisov@igalia.com>
Cr-Commit-Position: refs/heads/master@{#584328}
parent 5f99a2c0
...@@ -49,6 +49,8 @@ source_set("wayland") { ...@@ -49,6 +49,8 @@ source_set("wayland") {
"wayland_surface_factory.h", "wayland_surface_factory.h",
"wayland_touch.cc", "wayland_touch.cc",
"wayland_touch.h", "wayland_touch.h",
"wayland_util.cc",
"wayland_util.h",
"wayland_window.cc", "wayland_window.cc",
"wayland_window.h", "wayland_window.h",
"xdg_popup_wrapper.h", "xdg_popup_wrapper.h",
......
...@@ -12,16 +12,10 @@ ...@@ -12,16 +12,10 @@
#include "ui/gfx/skia_util.h" #include "ui/gfx/skia_util.h"
#include "ui/ozone/platform/wayland/wayland_connection.h" #include "ui/ozone/platform/wayland/wayland_connection.h"
#include "ui/ozone/platform/wayland/wayland_pointer.h" #include "ui/ozone/platform/wayland/wayland_pointer.h"
#include "ui/ozone/platform/wayland/wayland_util.h"
namespace ui { namespace ui {
namespace {
const uint32_t kShmFormat = WL_SHM_FORMAT_ARGB8888;
const SkColorType kColorType = kBGRA_8888_SkColorType;
} // namespace
WaylandCursor::WaylandCursor() : shared_memory_(new base::SharedMemory()) {} WaylandCursor::WaylandCursor() : shared_memory_(new base::SharedMemory()) {}
void WaylandCursor::Init(wl_pointer* pointer, WaylandConnection* connection) { void WaylandCursor::Init(wl_pointer* pointer, WaylandConnection* connection) {
...@@ -64,21 +58,19 @@ void WaylandCursor::UpdateBitmap(const std::vector<SkBitmap>& cursor_image, ...@@ -64,21 +58,19 @@ void WaylandCursor::UpdateBitmap(const std::vector<SkBitmap>& cursor_image,
return; return;
} }
if (!CreateSHMBuffer(gfx::SkISizeToSize(size))) { gfx::Size image_size = gfx::SkISizeToSize(size);
LOG(ERROR) << "Failed to create SHM buffer for Cursor Bitmap."; if (image_size != size_) {
wl_pointer_set_cursor(input_pointer_, serial, nullptr, 0, 0); wl_buffer* buffer =
return; wl::CreateSHMBuffer(image_size, shared_memory_.get(), shm_);
if (!buffer) {
LOG(ERROR) << "Failed to create SHM buffer for Cursor Bitmap.";
wl_pointer_set_cursor(input_pointer_, serial, nullptr, 0, 0);
return;
}
buffer_.reset(buffer);
size_ = image_size;
} }
wl::DrawBitmapToSHMB(size_, *shared_memory_, image);
// The |bitmap| contains ARGB image, so update our wl_buffer, which is
// backed by a SkSurface.
SkRect damage;
image.getBounds(&damage);
// Clear to transparent in case |image| is smaller than the canvas.
SkCanvas* canvas = sk_surface_->getCanvas();
canvas->clear(SK_ColorTRANSPARENT);
canvas->drawBitmapRect(image, damage, nullptr);
wl_pointer_set_cursor(input_pointer_, serial, pointer_surface_.get(), wl_pointer_set_cursor(input_pointer_, serial, pointer_surface_.get(),
location.x(), location.y()); location.x(), location.y());
...@@ -88,43 +80,6 @@ void WaylandCursor::UpdateBitmap(const std::vector<SkBitmap>& cursor_image, ...@@ -88,43 +80,6 @@ void WaylandCursor::UpdateBitmap(const std::vector<SkBitmap>& cursor_image,
wl_surface_commit(pointer_surface_.get()); wl_surface_commit(pointer_surface_.get());
} }
bool WaylandCursor::CreateSHMBuffer(const gfx::Size& size) {
if (size == size_)
return true;
size_ = size;
SkImageInfo info = SkImageInfo::MakeN32Premul(size_.width(), size_.height());
int stride = info.minRowBytes();
size_t image_buffer_size = info.computeByteSize(stride);
if (image_buffer_size == SIZE_MAX)
return false;
if (shared_memory_->handle().GetHandle()) {
shared_memory_->Unmap();
shared_memory_->Close();
}
if (!shared_memory_->CreateAndMapAnonymous(image_buffer_size)) {
LOG(ERROR) << "Create and mmap failed.";
return false;
}
// TODO(tonikitoo): Use SharedMemory::requested_size instead of
// 'image_buffer_size'?
wl::Object<wl_shm_pool> pool;
pool.reset(wl_shm_create_pool(shm_, shared_memory_->handle().GetHandle(),
image_buffer_size));
buffer_.reset(wl_shm_pool_create_buffer(pool.get(), 0, size_.width(),
size_.height(), stride, kShmFormat));
sk_surface_ = SkSurface::MakeRasterDirect(
SkImageInfo::Make(size_.width(), size_.height(), kColorType,
kOpaque_SkAlphaType),
static_cast<uint8_t*>(shared_memory_->memory()), stride);
return true;
}
void WaylandCursor::HideCursor(uint32_t serial) { void WaylandCursor::HideCursor(uint32_t serial) {
size_ = gfx::Size(); size_ = gfx::Size();
wl_pointer_set_cursor(input_pointer_, serial, nullptr, 0, 0); wl_pointer_set_cursor(input_pointer_, serial, nullptr, 0, 0);
......
...@@ -44,7 +44,6 @@ class WaylandCursor { ...@@ -44,7 +44,6 @@ class WaylandCursor {
uint32_t serial); uint32_t serial);
private: private:
bool CreateSHMBuffer(const gfx::Size& size);
void HideCursor(uint32_t serial); void HideCursor(uint32_t serial);
wl_shm* shm_ = nullptr; // Owned by WaylandConnection. wl_shm* shm_ = nullptr; // Owned by WaylandConnection.
......
// Copyright 2018 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/ozone/platform/wayland/wayland_util.h"
#include "base/memory/shared_memory.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/skia_util.h"
namespace wl {
namespace {
const uint32_t kShmFormat = WL_SHM_FORMAT_ARGB8888;
const SkColorType kColorType = kBGRA_8888_SkColorType;
} // namespace
wl_buffer* CreateSHMBuffer(const gfx::Size& size,
base::SharedMemory* shared_memory,
wl_shm* shm) {
SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
int stride = info.minRowBytes();
size_t image_buffer_size = info.computeByteSize(stride);
if (image_buffer_size == SIZE_MAX)
return nullptr;
if (shared_memory->handle().GetHandle()) {
shared_memory->Unmap();
shared_memory->Close();
}
if (!shared_memory->CreateAndMapAnonymous(image_buffer_size)) {
LOG(ERROR) << "Create and mmap failed.";
return nullptr;
}
// TODO(tonikitoo): Use SharedMemory::requested_size instead of
// 'image_buffer_size'?
wl::Object<wl_shm_pool> pool;
pool.reset(wl_shm_create_pool(shm, shared_memory->handle().GetHandle(),
image_buffer_size));
wl_buffer* buffer = wl_shm_pool_create_buffer(
pool.get(), 0, size.width(), size.height(), stride, kShmFormat);
return buffer;
}
void DrawBitmapToSHMB(const gfx::Size& size,
const base::SharedMemory& shared_memory,
const SkBitmap& bitmap) {
SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
int stride = info.minRowBytes();
sk_sp<SkSurface> sk_surface = SkSurface::MakeRasterDirect(
SkImageInfo::Make(size.width(), size.height(), kColorType,
kOpaque_SkAlphaType),
static_cast<uint8_t*>(shared_memory.memory()), stride);
// The |bitmap| contains ARGB image, so update our wl_buffer, which is
// backed by a SkSurface.
SkRect damage;
bitmap.getBounds(&damage);
// Clear to transparent in case |bitmap| is smaller than the canvas.
SkCanvas* canvas = sk_surface->getCanvas();
canvas->clear(SK_ColorTRANSPARENT);
canvas->drawBitmapRect(bitmap, damage, nullptr);
}
} // namespace wl
// Copyright 2018 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.
#ifndef UI_OZONE_PLATFORM_WAYLAND_WAYLAND_UTIL_H_
#define UI_OZONE_PLATFORM_WAYLAND_WAYLAND_UTIL_H_
#include <wayland-client.h>
#include "base/macros.h"
#include "ui/ozone/platform/wayland/wayland_object.h"
class SkBitmap;
namespace base {
class SharedMemory;
}
namespace gfx {
class Size;
}
namespace wl {
wl_buffer* CreateSHMBuffer(const gfx::Size& size,
base::SharedMemory* shared_memory,
wl_shm* shm);
void DrawBitmapToSHMB(const gfx::Size& size,
const base::SharedMemory& shared_memory,
const SkBitmap& bitmap);
} // namespace wl
#endif // UI_OZONE_PLATFORM_WAYLAND_WAYLAND_UTIL_H_
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