Chromium Code Reviews| Index: ui/ozone/platform/wayland/wayland_surface_factory.cc |
| diff --git a/ui/ozone/platform/wayland/wayland_surface_factory.cc b/ui/ozone/platform/wayland/wayland_surface_factory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1a37cd899fcd75f51558a82e2c1d6f7553a1e052 |
| --- /dev/null |
| +++ b/ui/ozone/platform/wayland/wayland_surface_factory.cc |
| @@ -0,0 +1,189 @@ |
| +// Copyright 2016 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_surface_factory.h" |
| + |
| +#include <fcntl.h> |
| +#include <sys/mman.h> |
| + |
| +#include "third_party/skia/include/core/SkSurface.h" |
| +#include "ui/gfx/vsync_provider.h" |
| +#include "ui/ozone/platform/wayland/wayland_display.h" |
| +#include "ui/ozone/platform/wayland/wayland_object.h" |
| +#include "ui/ozone/platform/wayland/wayland_window.h" |
| +#include "ui/ozone/public/surface_ozone_canvas.h" |
| +#include "ui/ozone/public/surface_ozone_egl.h" |
| + |
| +namespace ui { |
| + |
| +void UnmapPixels(void* pixels, void* context) { |
| + size_t* length = static_cast<size_t*>(context); |
| + munmap(pixels, *length); |
| + delete length; |
| +} |
| + |
| +class WaylandCanvasSurface : public SurfaceOzoneCanvas { |
| + public: |
| + WaylandCanvasSurface(WaylandDisplay* display, WaylandWindow* window_); |
| + ~WaylandCanvasSurface() override; |
| + |
| + // SurfaceOzoneCanvas |
| + skia::RefPtr<SkSurface> GetSurface() override; |
| + void ResizeCanvas(const gfx::Size& viewport_size) override; |
| + void PresentCanvas(const gfx::Rect& damage) override; |
| + scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() override; |
| + |
| + private: |
| + WaylandDisplay* display_; |
| + WaylandWindow* window_; |
| + |
| + gfx::Size size_; |
| + skia::RefPtr<SkSurface> sksurface_; |
|
spang
2016/01/22 23:42:32
SkSurface would become sk_surface_. Most code seem
Michael Forney
2016/01/23 01:08:05
I called it sk_surface_ to make the distinction be
|
| + wl::Object<wl_shm_pool> pool_; |
| + wl::Object<wl_buffer> buffer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WaylandCanvasSurface); |
| +}; |
| + |
| +WaylandCanvasSurface::WaylandCanvasSurface(WaylandDisplay* display, |
| + WaylandWindow* window) |
| + : display_(display), window_(window) {} |
| + |
| +WaylandCanvasSurface::~WaylandCanvasSurface() {} |
| + |
| +skia::RefPtr<SkSurface> WaylandCanvasSurface::GetSurface() { |
| + if (sksurface_) |
| + return sksurface_; |
| + |
| + char tmp[] = "/tmp/chromium-XXXXXX"; |
| + base::ScopedFD fd(mkostemp(tmp, O_CLOEXEC)); |
|
spang
2016/01/22 23:42:32
I think you want to use base::SharedMemory which w
Michael Forney
2016/01/23 01:08:05
I should've guessed something like this would alre
|
| + unlink(tmp); |
| + if (!fd.is_valid()) { |
| + LOG(ERROR) << "Failed to create temporary file to back SHM buffer"; |
| + return nullptr; |
| + } |
| + size_t length = size_.width() * size_.height() * 4; |
| + if (posix_fallocate(fd.get(), 0, length) != 0) { |
| + LOG(ERROR) << "Failed to allocate memory for SHM buffer"; |
| + return nullptr; |
| + } |
| + |
| + wl::Object<wl_shm_pool> pool( |
| + wl_shm_create_pool(display_->GetShm(), fd.get(), length)); |
| + if (!pool) { |
| + LOG(ERROR) << "Failed to create SHM pool"; |
| + return nullptr; |
| + } |
| + wl::Object<wl_buffer> buffer( |
| + wl_shm_pool_create_buffer(pool.get(), 0, size_.width(), size_.height(), |
| + size_.width() * 4, WL_SHM_FORMAT_ARGB8888)); |
| + if (!buffer) { |
| + LOG(ERROR) << "Failed to create SHM buffer"; |
| + return nullptr; |
| + } |
| + |
| + void* pixels = |
| + mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0); |
| + if (pixels == MAP_FAILED) { |
| + LOG(ERROR) << "Failed to mmap contents of SHM buffer"; |
| + return nullptr; |
| + } |
| + |
| + sksurface_ = skia::AdoptRef(SkSurface::NewRasterDirectReleaseProc( |
| + SkImageInfo::MakeN32Premul(size_.width(), size_.height()), pixels, |
| + size_.width() * 4, &UnmapPixels, new size_t(length), nullptr)); |
| + pool_ = std::move(pool); |
| + buffer_ = std::move(buffer); |
| + return sksurface_; |
| +} |
| + |
| +void WaylandCanvasSurface::ResizeCanvas(const gfx::Size& viewport_size) { |
| + if (size_ == viewport_size) |
| + return; |
| + // TODO(forney): We could implement more efficient resizes by allocating |
| + // buffers rounded up to larger sizes, and then reusing them if the new size |
| + // still fits (but still reallocate if the new size is much smaller than the |
| + // old size). |
| + if (sksurface_) { |
| + sksurface_.clear(); |
| + buffer_.reset(); |
| + pool_.reset(); |
| + } |
| + size_ = viewport_size; |
| +} |
| + |
| +void WaylandCanvasSurface::PresentCanvas(const gfx::Rect& damage) { |
| + // TODO(forney): This is just a naive implementation that allows chromium to |
| + // draw to the buffer at any time, even if it is being used by the Wayland |
| + // compositor. Instead, we should track buffer releases and frame callbacks |
| + // from Wayland to ensure perfect frames (while minimizing copies). |
| + wl_surface* surface = window_->GetSurface(); |
| + wl_surface_damage(surface, damage.x(), damage.y(), damage.width(), |
| + damage.height()); |
| + wl_surface_attach(surface, buffer_.get(), 0, 0); |
| + wl_surface_commit(surface); |
| + display_->Flush(); |
| +} |
| + |
| +scoped_ptr<gfx::VSyncProvider> WaylandCanvasSurface::CreateVSyncProvider() { |
| + // TODO(forney): This can be implemented with information from frame |
| + // callbacks, and possibly output refresh rate. |
| + NOTIMPLEMENTED(); |
| + return nullptr; |
| +} |
| + |
| +WaylandSurfaceFactory::WaylandSurfaceFactory(WaylandDisplay* display) |
| + : display_(display) {} |
| + |
| +WaylandSurfaceFactory::~WaylandSurfaceFactory() {} |
| + |
| +intptr_t WaylandSurfaceFactory::GetNativeDisplay() { |
| + NOTIMPLEMENTED(); |
| + return 0; |
| +} |
| + |
| +const int32_t* WaylandSurfaceFactory::GetEGLSurfaceProperties( |
| + const int32_t* desired_list) { |
| + NOTIMPLEMENTED(); |
| + return nullptr; |
| +} |
| + |
| +bool WaylandSurfaceFactory::LoadEGLGLES2Bindings( |
| + AddGLLibraryCallback add_gl_library, |
| + SetGLGetProcAddressProcCallback set_gl_get_proc_address) { |
| + // This Ozone implementation does not support multi-process rendering so |
| + // disable EGL unconditionally for now. |
| + return false; |
|
Michael Forney
2016/01/21 22:11:48
Even with returning false here, I have to comment
spang
2016/01/22 23:42:33
Your link is cut off :(
Michael Forney
2016/01/23 01:08:05
Sorry, I copied from a draft comment on a previous
spang
2016/01/25 18:57:57
Some CrOS-only code doesn't support software rende
|
| +} |
| + |
| +scoped_ptr<SurfaceOzoneCanvas> WaylandSurfaceFactory::CreateCanvasForWidget( |
| + gfx::AcceleratedWidget widget) { |
| + DCHECK(display_); |
| + WaylandWindow* window = display_->GetWindow(widget); |
| + DCHECK(window); |
| + return make_scoped_ptr(new WaylandCanvasSurface(display_, window)); |
| +} |
| + |
| +scoped_ptr<SurfaceOzoneEGL> WaylandSurfaceFactory::CreateEGLSurfaceForWidget( |
| + gfx::AcceleratedWidget widget) { |
| + NOTREACHED(); |
| + return nullptr; |
| +} |
| + |
| +scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmap( |
| + gfx::AcceleratedWidget widget, |
| + gfx::Size size, |
| + gfx::BufferFormat format, |
| + gfx::BufferUsage usage) { |
| + NOTIMPLEMENTED(); |
| + return nullptr; |
| +} |
| + |
| +scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmapFromHandle( |
| + const gfx::NativePixmapHandle& handle) { |
| + NOTIMPLEMENTED(); |
| + return nullptr; |
| +} |
| + |
| +} // namespace ui |