OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/ozone/platform/wayland/wayland_surface_factory.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> |
| 9 |
| 10 #include "base/memory/shared_memory.h" |
| 11 #include "third_party/skia/include/core/SkSurface.h" |
| 12 #include "ui/gfx/vsync_provider.h" |
| 13 #include "ui/ozone/platform/wayland/wayland_display.h" |
| 14 #include "ui/ozone/platform/wayland/wayland_object.h" |
| 15 #include "ui/ozone/platform/wayland/wayland_window.h" |
| 16 #include "ui/ozone/public/surface_ozone_canvas.h" |
| 17 #include "ui/ozone/public/surface_ozone_egl.h" |
| 18 |
| 19 namespace ui { |
| 20 |
| 21 static void DeleteSharedMemory(void* pixels, void* context) { |
| 22 delete static_cast<base::SharedMemory*>(context); |
| 23 } |
| 24 |
| 25 class WaylandCanvasSurface : public SurfaceOzoneCanvas { |
| 26 public: |
| 27 WaylandCanvasSurface(WaylandDisplay* display, WaylandWindow* window_); |
| 28 ~WaylandCanvasSurface() override; |
| 29 |
| 30 // SurfaceOzoneCanvas |
| 31 skia::RefPtr<SkSurface> GetSurface() override; |
| 32 void ResizeCanvas(const gfx::Size& viewport_size) override; |
| 33 void PresentCanvas(const gfx::Rect& damage) override; |
| 34 scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() override; |
| 35 |
| 36 private: |
| 37 WaylandDisplay* display_; |
| 38 WaylandWindow* window_; |
| 39 |
| 40 gfx::Size size_; |
| 41 skia::RefPtr<SkSurface> sk_surface_; |
| 42 wl::Object<wl_shm_pool> pool_; |
| 43 wl::Object<wl_buffer> buffer_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(WaylandCanvasSurface); |
| 46 }; |
| 47 |
| 48 WaylandCanvasSurface::WaylandCanvasSurface(WaylandDisplay* display, |
| 49 WaylandWindow* window) |
| 50 : display_(display), window_(window) {} |
| 51 |
| 52 WaylandCanvasSurface::~WaylandCanvasSurface() {} |
| 53 |
| 54 skia::RefPtr<SkSurface> WaylandCanvasSurface::GetSurface() { |
| 55 if (sk_surface_) |
| 56 return sk_surface_; |
| 57 |
| 58 size_t length = size_.width() * size_.height() * 4; |
| 59 auto shared_memory = make_scoped_ptr(new base::SharedMemory); |
| 60 if (!shared_memory->CreateAndMapAnonymous(length)) |
| 61 return nullptr; |
| 62 |
| 63 wl::Object<wl_shm_pool> pool( |
| 64 wl_shm_create_pool(display_->shm(), shared_memory->handle().fd, length)); |
| 65 if (!pool) |
| 66 return nullptr; |
| 67 wl::Object<wl_buffer> buffer( |
| 68 wl_shm_pool_create_buffer(pool.get(), 0, size_.width(), size_.height(), |
| 69 size_.width() * 4, WL_SHM_FORMAT_ARGB8888)); |
| 70 if (!buffer) |
| 71 return nullptr; |
| 72 |
| 73 sk_surface_ = skia::AdoptRef(SkSurface::NewRasterDirectReleaseProc( |
| 74 SkImageInfo::MakeN32Premul(size_.width(), size_.height()), |
| 75 shared_memory->memory(), size_.width() * 4, &DeleteSharedMemory, |
| 76 shared_memory.get(), nullptr)); |
| 77 if (!sk_surface_) |
| 78 return nullptr; |
| 79 pool_ = std::move(pool); |
| 80 buffer_ = std::move(buffer); |
| 81 (void)shared_memory.release(); |
| 82 return sk_surface_; |
| 83 } |
| 84 |
| 85 void WaylandCanvasSurface::ResizeCanvas(const gfx::Size& viewport_size) { |
| 86 if (size_ == viewport_size) |
| 87 return; |
| 88 // TODO(forney): We could implement more efficient resizes by allocating |
| 89 // buffers rounded up to larger sizes, and then reusing them if the new size |
| 90 // still fits (but still reallocate if the new size is much smaller than the |
| 91 // old size). |
| 92 if (sk_surface_) { |
| 93 sk_surface_.clear(); |
| 94 buffer_.reset(); |
| 95 pool_.reset(); |
| 96 } |
| 97 size_ = viewport_size; |
| 98 } |
| 99 |
| 100 void WaylandCanvasSurface::PresentCanvas(const gfx::Rect& damage) { |
| 101 // TODO(forney): This is just a naive implementation that allows chromium to |
| 102 // draw to the buffer at any time, even if it is being used by the Wayland |
| 103 // compositor. Instead, we should track buffer releases and frame callbacks |
| 104 // from Wayland to ensure perfect frames (while minimizing copies). |
| 105 wl_surface* surface = window_->GetSurface(); |
| 106 wl_surface_damage(surface, damage.x(), damage.y(), damage.width(), |
| 107 damage.height()); |
| 108 wl_surface_attach(surface, buffer_.get(), 0, 0); |
| 109 wl_surface_commit(surface); |
| 110 display_->Flush(); |
| 111 } |
| 112 |
| 113 scoped_ptr<gfx::VSyncProvider> WaylandCanvasSurface::CreateVSyncProvider() { |
| 114 // TODO(forney): This can be implemented with information from frame |
| 115 // callbacks, and possibly output refresh rate. |
| 116 NOTIMPLEMENTED(); |
| 117 return nullptr; |
| 118 } |
| 119 |
| 120 WaylandSurfaceFactory::WaylandSurfaceFactory(WaylandDisplay* display) |
| 121 : display_(display) {} |
| 122 |
| 123 WaylandSurfaceFactory::~WaylandSurfaceFactory() {} |
| 124 |
| 125 intptr_t WaylandSurfaceFactory::GetNativeDisplay() { |
| 126 NOTIMPLEMENTED(); |
| 127 return 0; |
| 128 } |
| 129 |
| 130 const int32_t* WaylandSurfaceFactory::GetEGLSurfaceProperties( |
| 131 const int32_t* desired_list) { |
| 132 NOTIMPLEMENTED(); |
| 133 return nullptr; |
| 134 } |
| 135 |
| 136 bool WaylandSurfaceFactory::LoadEGLGLES2Bindings( |
| 137 AddGLLibraryCallback add_gl_library, |
| 138 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { |
| 139 // This Ozone implementation does not support multi-process rendering so |
| 140 // disable EGL unconditionally for now. |
| 141 return false; |
| 142 } |
| 143 |
| 144 scoped_ptr<SurfaceOzoneCanvas> WaylandSurfaceFactory::CreateCanvasForWidget( |
| 145 gfx::AcceleratedWidget widget) { |
| 146 DCHECK(display_); |
| 147 WaylandWindow* window = display_->GetWindow(widget); |
| 148 DCHECK(window); |
| 149 return make_scoped_ptr(new WaylandCanvasSurface(display_, window)); |
| 150 } |
| 151 |
| 152 scoped_ptr<SurfaceOzoneEGL> WaylandSurfaceFactory::CreateEGLSurfaceForWidget( |
| 153 gfx::AcceleratedWidget widget) { |
| 154 NOTREACHED(); |
| 155 return nullptr; |
| 156 } |
| 157 |
| 158 scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmap( |
| 159 gfx::AcceleratedWidget widget, |
| 160 gfx::Size size, |
| 161 gfx::BufferFormat format, |
| 162 gfx::BufferUsage usage) { |
| 163 NOTIMPLEMENTED(); |
| 164 return nullptr; |
| 165 } |
| 166 |
| 167 scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmapFromHandle( |
| 168 const gfx::NativePixmapHandle& handle) { |
| 169 NOTIMPLEMENTED(); |
| 170 return nullptr; |
| 171 } |
| 172 |
| 173 } // namespace ui |
OLD | NEW |