OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/ozone/platform/wayland/wayland_surface_factory.h" | 5 #include "ui/ozone/platform/wayland/wayland_surface_factory.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
9 #include <wayland-client.h> | 9 #include <wayland-client.h> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
13 #include "third_party/skia/include/core/SkSurface.h" | 13 #include "third_party/skia/include/core/SkSurface.h" |
14 #include "ui/gfx/vsync_provider.h" | 14 #include "ui/gfx/vsync_provider.h" |
15 #include "ui/ozone/common/egl_util.h" | 15 #include "ui/ozone/common/egl_util.h" |
16 #include "ui/ozone/platform/wayland/wayland_display.h" | 16 #include "ui/ozone/platform/wayland/wayland_connection.h" |
17 #include "ui/ozone/platform/wayland/wayland_object.h" | 17 #include "ui/ozone/platform/wayland/wayland_object.h" |
18 #include "ui/ozone/platform/wayland/wayland_window.h" | 18 #include "ui/ozone/platform/wayland/wayland_window.h" |
19 #include "ui/ozone/public/surface_ozone_canvas.h" | 19 #include "ui/ozone/public/surface_ozone_canvas.h" |
20 #include "ui/ozone/public/surface_ozone_egl.h" | 20 #include "ui/ozone/public/surface_ozone_egl.h" |
21 | 21 |
22 #if defined(USE_WAYLAND_EGL) | 22 #if defined(USE_WAYLAND_EGL) |
23 #include "ui/ozone/platform/wayland/wayland_egl_surface.h" | 23 #include "ui/ozone/platform/wayland/wayland_egl_surface.h" |
24 #endif | 24 #endif |
25 | 25 |
26 namespace ui { | 26 namespace ui { |
27 | 27 |
28 static void DeleteSharedMemory(void* pixels, void* context) { | 28 static void DeleteSharedMemory(void* pixels, void* context) { |
29 delete static_cast<base::SharedMemory*>(context); | 29 delete static_cast<base::SharedMemory*>(context); |
30 } | 30 } |
31 | 31 |
32 class WaylandCanvasSurface : public SurfaceOzoneCanvas { | 32 class WaylandCanvasSurface : public SurfaceOzoneCanvas { |
33 public: | 33 public: |
34 WaylandCanvasSurface(WaylandDisplay* display, WaylandWindow* window_); | 34 WaylandCanvasSurface(WaylandConnection* connection, WaylandWindow* window_); |
35 ~WaylandCanvasSurface() override; | 35 ~WaylandCanvasSurface() override; |
36 | 36 |
37 // SurfaceOzoneCanvas | 37 // SurfaceOzoneCanvas |
38 sk_sp<SkSurface> GetSurface() override; | 38 sk_sp<SkSurface> GetSurface() override; |
39 void ResizeCanvas(const gfx::Size& viewport_size) override; | 39 void ResizeCanvas(const gfx::Size& viewport_size) override; |
40 void PresentCanvas(const gfx::Rect& damage) override; | 40 void PresentCanvas(const gfx::Rect& damage) override; |
41 std::unique_ptr<gfx::VSyncProvider> CreateVSyncProvider() override; | 41 std::unique_ptr<gfx::VSyncProvider> CreateVSyncProvider() override; |
42 | 42 |
43 private: | 43 private: |
44 WaylandDisplay* display_; | 44 WaylandConnection* connection_; |
45 WaylandWindow* window_; | 45 WaylandWindow* window_; |
46 | 46 |
47 gfx::Size size_; | 47 gfx::Size size_; |
48 sk_sp<SkSurface> sk_surface_; | 48 sk_sp<SkSurface> sk_surface_; |
49 wl::Object<wl_shm_pool> pool_; | 49 wl::Object<wl_shm_pool> pool_; |
50 wl::Object<wl_buffer> buffer_; | 50 wl::Object<wl_buffer> buffer_; |
51 | 51 |
52 DISALLOW_COPY_AND_ASSIGN(WaylandCanvasSurface); | 52 DISALLOW_COPY_AND_ASSIGN(WaylandCanvasSurface); |
53 }; | 53 }; |
54 | 54 |
55 WaylandCanvasSurface::WaylandCanvasSurface(WaylandDisplay* display, | 55 WaylandCanvasSurface::WaylandCanvasSurface(WaylandConnection* connection, |
56 WaylandWindow* window) | 56 WaylandWindow* window) |
57 : display_(display), window_(window), size_(window->GetBounds().size()) {} | 57 : connection_(connection), |
| 58 window_(window), |
| 59 size_(window->GetBounds().size()) {} |
58 | 60 |
59 WaylandCanvasSurface::~WaylandCanvasSurface() {} | 61 WaylandCanvasSurface::~WaylandCanvasSurface() {} |
60 | 62 |
61 sk_sp<SkSurface> WaylandCanvasSurface::GetSurface() { | 63 sk_sp<SkSurface> WaylandCanvasSurface::GetSurface() { |
62 if (sk_surface_) | 64 if (sk_surface_) |
63 return sk_surface_; | 65 return sk_surface_; |
64 | 66 |
65 size_t length = size_.width() * size_.height() * 4; | 67 size_t length = size_.width() * size_.height() * 4; |
66 auto shared_memory = base::WrapUnique(new base::SharedMemory); | 68 auto shared_memory = base::WrapUnique(new base::SharedMemory); |
67 if (!shared_memory->CreateAndMapAnonymous(length)) | 69 if (!shared_memory->CreateAndMapAnonymous(length)) |
68 return nullptr; | 70 return nullptr; |
69 | 71 |
70 wl::Object<wl_shm_pool> pool( | 72 wl::Object<wl_shm_pool> pool(wl_shm_create_pool( |
71 wl_shm_create_pool(display_->shm(), shared_memory->handle().fd, length)); | 73 connection_->shm(), shared_memory->handle().fd, length)); |
72 if (!pool) | 74 if (!pool) |
73 return nullptr; | 75 return nullptr; |
74 wl::Object<wl_buffer> buffer( | 76 wl::Object<wl_buffer> buffer( |
75 wl_shm_pool_create_buffer(pool.get(), 0, size_.width(), size_.height(), | 77 wl_shm_pool_create_buffer(pool.get(), 0, size_.width(), size_.height(), |
76 size_.width() * 4, WL_SHM_FORMAT_ARGB8888)); | 78 size_.width() * 4, WL_SHM_FORMAT_ARGB8888)); |
77 if (!buffer) | 79 if (!buffer) |
78 return nullptr; | 80 return nullptr; |
79 | 81 |
80 sk_surface_ = SkSurface::MakeRasterDirectReleaseProc( | 82 sk_surface_ = SkSurface::MakeRasterDirectReleaseProc( |
81 SkImageInfo::MakeN32Premul(size_.width(), size_.height()), | 83 SkImageInfo::MakeN32Premul(size_.width(), size_.height()), |
(...skipping 25 matching lines...) Expand all Loading... |
107 void WaylandCanvasSurface::PresentCanvas(const gfx::Rect& damage) { | 109 void WaylandCanvasSurface::PresentCanvas(const gfx::Rect& damage) { |
108 // TODO(forney): This is just a naive implementation that allows chromium to | 110 // TODO(forney): This is just a naive implementation that allows chromium to |
109 // draw to the buffer at any time, even if it is being used by the Wayland | 111 // draw to the buffer at any time, even if it is being used by the Wayland |
110 // compositor. Instead, we should track buffer releases and frame callbacks | 112 // compositor. Instead, we should track buffer releases and frame callbacks |
111 // from Wayland to ensure perfect frames (while minimizing copies). | 113 // from Wayland to ensure perfect frames (while minimizing copies). |
112 wl_surface* surface = window_->surface(); | 114 wl_surface* surface = window_->surface(); |
113 wl_surface_damage(surface, damage.x(), damage.y(), damage.width(), | 115 wl_surface_damage(surface, damage.x(), damage.y(), damage.width(), |
114 damage.height()); | 116 damage.height()); |
115 wl_surface_attach(surface, buffer_.get(), 0, 0); | 117 wl_surface_attach(surface, buffer_.get(), 0, 0); |
116 wl_surface_commit(surface); | 118 wl_surface_commit(surface); |
117 display_->ScheduleFlush(); | 119 connection_->ScheduleFlush(); |
118 } | 120 } |
119 | 121 |
120 std::unique_ptr<gfx::VSyncProvider> | 122 std::unique_ptr<gfx::VSyncProvider> |
121 WaylandCanvasSurface::CreateVSyncProvider() { | 123 WaylandCanvasSurface::CreateVSyncProvider() { |
122 // TODO(forney): This can be implemented with information from frame | 124 // TODO(forney): This can be implemented with information from frame |
123 // callbacks, and possibly output refresh rate. | 125 // callbacks, and possibly output refresh rate. |
124 NOTIMPLEMENTED(); | 126 NOTIMPLEMENTED(); |
125 return nullptr; | 127 return nullptr; |
126 } | 128 } |
127 | 129 |
128 WaylandSurfaceFactory::WaylandSurfaceFactory(WaylandDisplay* display) | 130 WaylandSurfaceFactory::WaylandSurfaceFactory(WaylandConnection* connection) |
129 : display_(display) {} | 131 : connection_(connection) {} |
130 | 132 |
131 WaylandSurfaceFactory::~WaylandSurfaceFactory() {} | 133 WaylandSurfaceFactory::~WaylandSurfaceFactory() {} |
132 | 134 |
133 intptr_t WaylandSurfaceFactory::GetNativeDisplay() { | 135 intptr_t WaylandSurfaceFactory::GetNativeDisplay() { |
134 return reinterpret_cast<intptr_t>(display_->display()); | 136 return reinterpret_cast<intptr_t>(connection_->display()); |
135 } | 137 } |
136 | 138 |
137 bool WaylandSurfaceFactory::LoadEGLGLES2Bindings( | 139 bool WaylandSurfaceFactory::LoadEGLGLES2Bindings( |
138 AddGLLibraryCallback add_gl_library, | 140 AddGLLibraryCallback add_gl_library, |
139 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { | 141 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { |
140 #if defined(USE_WAYLAND_EGL) | 142 #if defined(USE_WAYLAND_EGL) |
141 if (!display_) | 143 if (!connection_) |
142 return false; | 144 return false; |
143 setenv("EGL_PLATFORM", "wayland", 0); | 145 setenv("EGL_PLATFORM", "wayland", 0); |
144 return LoadDefaultEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address); | 146 return LoadDefaultEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address); |
145 #else | 147 #else |
146 return false; | 148 return false; |
147 #endif | 149 #endif |
148 } | 150 } |
149 | 151 |
150 std::unique_ptr<SurfaceOzoneCanvas> | 152 std::unique_ptr<SurfaceOzoneCanvas> |
151 WaylandSurfaceFactory::CreateCanvasForWidget(gfx::AcceleratedWidget widget) { | 153 WaylandSurfaceFactory::CreateCanvasForWidget(gfx::AcceleratedWidget widget) { |
152 WaylandWindow* window = display_->GetWindow(widget); | 154 WaylandWindow* window = connection_->GetWindow(widget); |
153 DCHECK(window); | 155 DCHECK(window); |
154 return base::WrapUnique(new WaylandCanvasSurface(display_, window)); | 156 return base::WrapUnique(new WaylandCanvasSurface(connection_, window)); |
155 } | 157 } |
156 | 158 |
157 std::unique_ptr<SurfaceOzoneEGL> | 159 std::unique_ptr<SurfaceOzoneEGL> |
158 WaylandSurfaceFactory::CreateEGLSurfaceForWidget( | 160 WaylandSurfaceFactory::CreateEGLSurfaceForWidget( |
159 gfx::AcceleratedWidget widget) { | 161 gfx::AcceleratedWidget widget) { |
160 #if defined(USE_WAYLAND_EGL) | 162 #if defined(USE_WAYLAND_EGL) |
161 WaylandWindow* window = display_->GetWindow(widget); | 163 WaylandWindow* window = connection_->GetWindow(widget); |
162 DCHECK(window); | 164 DCHECK(window); |
163 auto surface = base::WrapUnique( | 165 auto surface = base::WrapUnique( |
164 new WaylandEGLSurface(window, window->GetBounds().size())); | 166 new WaylandEGLSurface(window, window->GetBounds().size())); |
165 if (!surface->Initialize()) | 167 if (!surface->Initialize()) |
166 return nullptr; | 168 return nullptr; |
167 return std::move(surface); | 169 return std::move(surface); |
168 #else | 170 #else |
169 return nullptr; | 171 return nullptr; |
170 #endif | 172 #endif |
171 } | 173 } |
172 | 174 |
173 scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmap( | 175 scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmap( |
174 gfx::AcceleratedWidget widget, | 176 gfx::AcceleratedWidget widget, |
175 gfx::Size size, | 177 gfx::Size size, |
176 gfx::BufferFormat format, | 178 gfx::BufferFormat format, |
177 gfx::BufferUsage usage) { | 179 gfx::BufferUsage usage) { |
178 NOTIMPLEMENTED(); | 180 NOTIMPLEMENTED(); |
179 return nullptr; | 181 return nullptr; |
180 } | 182 } |
181 | 183 |
182 scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmapFromHandle( | 184 scoped_refptr<NativePixmap> WaylandSurfaceFactory::CreateNativePixmapFromHandle( |
183 gfx::Size size, | 185 gfx::Size size, |
184 gfx::BufferFormat format, | 186 gfx::BufferFormat format, |
185 const gfx::NativePixmapHandle& handle) { | 187 const gfx::NativePixmapHandle& handle) { |
186 NOTIMPLEMENTED(); | 188 NOTIMPLEMENTED(); |
187 return nullptr; | 189 return nullptr; |
188 } | 190 } |
189 | 191 |
190 } // namespace ui | 192 } // namespace ui |
OLD | NEW |