Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: components/exo/wayland/clients/motion_events.cc

Issue 2477163002: exo: Add drm support to motion_events.
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/exo/wayland/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Implementation of a client that produces output in the form of RGBA 5 // Implementation of a client that produces output in the form of RGBA
6 // buffers when receiving pointer/touch events. RGB contains the lower 6 // buffers when receiving pointer/touch events. RGB contains the lower
7 // 24 bits of the event timestamp and A is 0xff. 7 // 24 bits of the event timestamp and A is 0xff.
8 8
9 #include <fcntl.h>
10 #include <linux-dmabuf-unstable-v1-client-protocol.h>
9 #include <wayland-client-core.h> 11 #include <wayland-client-core.h>
10 #include <wayland-client-protocol.h> 12 #include <wayland-client-protocol.h>
11 13
12 #include <iostream> 14 #include <iostream>
13 #include <vector> 15 #include <vector>
14 16
17 #include "base/at_exit.h"
15 #include "base/command_line.h" 18 #include "base/command_line.h"
16 #include "base/logging.h" 19 #include "base/logging.h"
17 #include "base/macros.h" 20 #include "base/macros.h"
18 #include "base/memory/shared_memory.h" 21 #include "base/memory/shared_memory.h"
19 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
20 #include "base/time/time.h" 23 #include "base/time/time.h"
21 #include "third_party/skia/include/core/SkCanvas.h" 24 #include "third_party/skia/include/core/SkCanvas.h"
22 #include "third_party/skia/include/core/SkRefCnt.h" 25 #include "third_party/skia/include/core/SkRefCnt.h"
23 #include "third_party/skia/include/core/SkSurface.h" 26 #include "third_party/skia/include/core/SkSurface.h"
27 #include "third_party/skia/include/gpu/GrContext.h"
28 #include "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h"
29 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
30 #include "ui/gl/gl_bindings.h"
31 #include "ui/gl/gl_context.h"
32 #include "ui/gl/gl_enums.h"
33 #include "ui/gl/gl_surface.h"
34 #include "ui/gl/gl_surface_egl.h"
35 #include "ui/gl/init/gl_factory.h"
36 #include "ui/gl/scoped_make_current.h"
24 37
25 // Convenient macro that is used to define default deleters for wayland object 38 #if defined(OZONE_PLATFORM_GBM)
39 #include <drm_fourcc.h>
40 #include <gbm.h>
41 #endif
42
43 // Convenient macro that is used to define default deleters for object
26 // types allowing them to be used with std::unique_ptr. 44 // types allowing them to be used with std::unique_ptr.
27 #define DEFAULT_DELETER(TypeName, DeleteFunction) \ 45 #define DEFAULT_DELETER(TypeName, DeleteFunction) \
28 namespace std { \ 46 namespace std { \
29 template <> \ 47 template <> \
30 struct default_delete<TypeName> { \ 48 struct default_delete<TypeName> { \
31 void operator()(TypeName* ptr) { DeleteFunction(ptr); } \ 49 void operator()(TypeName* ptr) { DeleteFunction(ptr); } \
32 }; \ 50 }; \
33 } 51 }
34 52
53 #if defined(OZONE_PLATFORM_GBM)
54 DEFAULT_DELETER(gbm_device, gbm_device_destroy)
55 DEFAULT_DELETER(gbm_bo, gbm_bo_destroy)
56 #endif
57
35 DEFAULT_DELETER(wl_display, wl_display_disconnect) 58 DEFAULT_DELETER(wl_display, wl_display_disconnect)
36 DEFAULT_DELETER(wl_compositor, wl_compositor_destroy) 59 DEFAULT_DELETER(wl_compositor, wl_compositor_destroy)
37 DEFAULT_DELETER(wl_shm, wl_shm_destroy) 60 DEFAULT_DELETER(wl_shm, wl_shm_destroy)
38 DEFAULT_DELETER(wl_shm_pool, wl_shm_pool_destroy) 61 DEFAULT_DELETER(wl_shm_pool, wl_shm_pool_destroy)
39 DEFAULT_DELETER(wl_buffer, wl_buffer_destroy) 62 DEFAULT_DELETER(wl_buffer, wl_buffer_destroy)
40 DEFAULT_DELETER(wl_surface, wl_surface_destroy) 63 DEFAULT_DELETER(wl_surface, wl_surface_destroy)
41 DEFAULT_DELETER(wl_region, wl_region_destroy) 64 DEFAULT_DELETER(wl_region, wl_region_destroy)
42 DEFAULT_DELETER(wl_shell, wl_shell_destroy) 65 DEFAULT_DELETER(wl_shell, wl_shell_destroy)
43 DEFAULT_DELETER(wl_shell_surface, wl_shell_surface_destroy) 66 DEFAULT_DELETER(wl_shell_surface, wl_shell_surface_destroy)
44 DEFAULT_DELETER(wl_seat, wl_seat_destroy) 67 DEFAULT_DELETER(wl_seat, wl_seat_destroy)
45 DEFAULT_DELETER(wl_pointer, wl_pointer_destroy) 68 DEFAULT_DELETER(wl_pointer, wl_pointer_destroy)
46 DEFAULT_DELETER(wl_touch, wl_touch_destroy) 69 DEFAULT_DELETER(wl_touch, wl_touch_destroy)
47 DEFAULT_DELETER(wl_callback, wl_callback_destroy) 70 DEFAULT_DELETER(wl_callback, wl_callback_destroy)
71 DEFAULT_DELETER(zwp_linux_buffer_params_v1, zwp_linux_buffer_params_v1_destroy)
72 DEFAULT_DELETER(zwp_linux_dmabuf_v1, zwp_linux_dmabuf_v1_destroy)
48 73
49 namespace exo { 74 namespace exo {
50 namespace wayland { 75 namespace wayland {
51 namespace clients { 76 namespace clients {
52 namespace { 77 namespace {
53 78
54 // Buffer format. 79 // Buffer format.
55 const int32_t kFormat = WL_SHM_FORMAT_ARGB8888; 80 const int32_t kShmFormat = WL_SHM_FORMAT_ARGB8888;
56 const SkColorType kColorType = kBGRA_8888_SkColorType; 81 const SkColorType kColorType = kBGRA_8888_SkColorType;
82 #if defined(OZONE_PLATFORM_GBM)
83 const int32_t kDrmFormat = DRM_FORMAT_ABGR8888;
84 #endif
57 const size_t kBytesPerPixel = 4; 85 const size_t kBytesPerPixel = 4;
58 86
87 #if defined(OZONE_PLATFORM_GBM)
88 // DRI render node path
89 const char kDriRenderNode[] = "/dev/dri/renderD128";
90 #endif
91
59 // Number of buffers. 92 // Number of buffers.
60 const size_t kBuffers = 2; 93 const size_t kBuffers = 3;
61 94
62 // Rotation speed (degrees/second). 95 // Rotation speed (degrees/second).
63 const double kRotationSpeed = 360.0; 96 const double kRotationSpeed = 360.0;
64 97
65 // Benchmark interval in seconds. 98 // Benchmark interval in seconds.
66 const int kBenchmarkInterval = 5; 99 const int kBenchmarkInterval = 5;
67 100
68 struct Globals { 101 struct Globals {
69 std::unique_ptr<wl_compositor> compositor; 102 std::unique_ptr<wl_compositor> compositor;
70 std::unique_ptr<wl_shm> shm; 103 std::unique_ptr<wl_shm> shm;
104 std::unique_ptr<zwp_linux_dmabuf_v1> linux_dmabuf;
71 std::unique_ptr<wl_shell> shell; 105 std::unique_ptr<wl_shell> shell;
72 std::unique_ptr<wl_seat> seat; 106 std::unique_ptr<wl_seat> seat;
73 }; 107 };
74 108
75 void RegistryHandler(void* data, 109 void RegistryHandler(void* data,
76 wl_registry* registry, 110 wl_registry* registry,
77 uint32_t id, 111 uint32_t id,
78 const char* interface, 112 const char* interface,
79 uint32_t version) { 113 uint32_t version) {
80 Globals* globals = static_cast<Globals*>(data); 114 Globals* globals = static_cast<Globals*>(data);
81 115
82 if (strcmp(interface, "wl_compositor") == 0) { 116 if (strcmp(interface, "wl_compositor") == 0) {
83 globals->compositor.reset(static_cast<wl_compositor*>( 117 globals->compositor.reset(static_cast<wl_compositor*>(
84 wl_registry_bind(registry, id, &wl_compositor_interface, 3))); 118 wl_registry_bind(registry, id, &wl_compositor_interface, 3)));
85 } else if (strcmp(interface, "wl_shm") == 0) { 119 } else if (strcmp(interface, "wl_shm") == 0) {
86 globals->shm.reset(static_cast<wl_shm*>( 120 globals->shm.reset(static_cast<wl_shm*>(
87 wl_registry_bind(registry, id, &wl_shm_interface, 1))); 121 wl_registry_bind(registry, id, &wl_shm_interface, 1)));
88 } else if (strcmp(interface, "wl_shell") == 0) { 122 } else if (strcmp(interface, "wl_shell") == 0) {
89 globals->shell.reset(static_cast<wl_shell*>( 123 globals->shell.reset(static_cast<wl_shell*>(
90 wl_registry_bind(registry, id, &wl_shell_interface, 1))); 124 wl_registry_bind(registry, id, &wl_shell_interface, 1)));
91 } else if (strcmp(interface, "wl_seat") == 0) { 125 } else if (strcmp(interface, "wl_seat") == 0) {
92 globals->seat.reset(static_cast<wl_seat*>( 126 globals->seat.reset(static_cast<wl_seat*>(
93 wl_registry_bind(registry, id, &wl_seat_interface, 5))); 127 wl_registry_bind(registry, id, &wl_seat_interface, 5)));
128 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
129 globals->linux_dmabuf.reset(static_cast<zwp_linux_dmabuf_v1*>(
130 wl_registry_bind(registry, id, &zwp_linux_dmabuf_v1_interface, 1)));
94 } 131 }
95 } 132 }
96 133
97 void RegistryRemover(void* data, wl_registry* registry, uint32_t id) { 134 void RegistryRemover(void* data, wl_registry* registry, uint32_t id) {
98 LOG(WARNING) << "Got a registry losing event for " << id; 135 LOG(WARNING) << "Got a registry losing event for " << id;
99 } 136 }
100 137
138 #if defined(OZONE_PLATFORM_GBM)
139 class ScopedTexture {
140 public:
141 ScopedTexture() { glGenTextures(1, &id_); }
142 ~ScopedTexture() { glDeleteTextures(1, &id_); }
143 GLuint id_ = 0;
144 };
145
146 class ScopedEglImage {
147 public:
148 ScopedEglImage(EGLImageKHR image) : image_(image) {}
149 ~ScopedEglImage() { eglDestroyImageKHR(eglGetCurrentDisplay(), image_); }
150 EGLImageKHR image_ = 0;
151 };
152 #endif
153
101 struct Buffer { 154 struct Buffer {
102 std::unique_ptr<wl_buffer> buffer; 155 std::unique_ptr<wl_buffer> buffer;
103 sk_sp<SkSurface> sk_surface; 156 sk_sp<SkSurface> sk_surface;
104 bool busy = false; 157 bool busy = false;
158 base::SharedMemory shared_memory;
159 std::unique_ptr<wl_shm_pool> shm_pool;
160 #if defined(OZONE_PLATFORM_GBM)
161 std::unique_ptr<gbm_bo> bo;
162 std::unique_ptr<ScopedEglImage> egl_image;
163 std::unique_ptr<ScopedTexture> texture;
164 #endif
165 std::unique_ptr<zwp_linux_buffer_params_v1> params;
166 sk_sp<SkImage> sk_image;
105 }; 167 };
106 168
107 void BufferRelease(void* data, wl_buffer* /* buffer */) { 169 void BufferRelease(void* data, wl_buffer* /* buffer */) {
108 Buffer* buffer = static_cast<Buffer*>(data); 170 Buffer* buffer = static_cast<Buffer*>(data);
109 171
110 buffer->busy = false; 172 buffer->busy = false;
111 } 173 }
112 174
113 using EventTimeStack = std::vector<uint32_t>; 175 using EventTimeStack = std::vector<uint32_t>;
114 176
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 }; 259 };
198 260
199 void FrameCallback(void* data, wl_callback* callback, uint32_t time) { 261 void FrameCallback(void* data, wl_callback* callback, uint32_t time) {
200 Frame* frame = static_cast<Frame*>(data); 262 Frame* frame = static_cast<Frame*>(data);
201 263
202 static uint32_t initial_time = time; 264 static uint32_t initial_time = time;
203 frame->time = time - initial_time; 265 frame->time = time - initial_time;
204 frame->callback_pending = false; 266 frame->callback_pending = false;
205 } 267 }
206 268
269 #if defined(OZONE_PLATFORM_GBM)
270 void LinuxBufferParamsCreated(void* data,
271 zwp_linux_buffer_params_v1* params,
272 wl_buffer* new_buffer) {
273 Buffer* buffer = static_cast<Buffer*>(data);
274 buffer->buffer.reset(new_buffer);
275 }
276
277 void LinuxBufferParamsFailed(void* data, zwp_linux_buffer_params_v1* params) {
278 LOG(ERROR) << "Linux buffer params failed";
279 }
280 #endif
281
207 } // namespace 282 } // namespace
208 283
284 static GrGLFuncPtr egl_get_gl_proc(void* ctx, const char name[]) {
285 GrGLFuncPtr ptr = eglGetProcAddress(name);
286 return ptr;
287 }
288
289 const GrGLInterface* GrGLCreateNativeInterface() {
290 return GrGLAssembleInterface(nullptr, egl_get_gl_proc);
291 }
292
209 class MotionEvents { 293 class MotionEvents {
210 public: 294 public:
211 MotionEvents(size_t width, 295 MotionEvents(size_t width,
212 size_t height, 296 size_t height,
213 int scale, 297 int scale,
214 size_t num_rects, 298 size_t num_rects,
215 bool fullscreen) 299 bool fullscreen,
300 bool use_drm)
216 : width_(width), 301 : width_(width),
217 height_(height), 302 height_(height),
218 scale_(scale), 303 scale_(scale),
219 num_rects_(num_rects), 304 num_rects_(num_rects),
220 fullscreen_(fullscreen) {} 305 fullscreen_(fullscreen),
306 use_drm_(use_drm) {}
221 307
222 // Initialize and run client main loop. 308 // Initialize and run client main loop.
223 int Run(); 309 int Run();
224 310
225 private: 311 private:
312 bool CreateBuffer(Buffer* buffer);
313
226 size_t stride() const { return width_ * kBytesPerPixel; } 314 size_t stride() const { return width_ * kBytesPerPixel; }
227 size_t buffer_size() const { return stride() * height_; } 315 size_t buffer_size() const { return stride() * height_; }
228 316
229 const size_t width_; 317 const size_t width_;
230 const size_t height_; 318 const size_t height_;
231 const int scale_; 319 const int scale_;
232 const size_t num_rects_; 320 const size_t num_rects_;
233 const bool fullscreen_; 321 const bool fullscreen_;
322 const bool use_drm_;
323
324 Globals globals_;
325 std::unique_ptr<wl_display> display_;
326 Buffer buffers_[kBuffers];
327 #if defined(OZONE_PLATFORM_GBM)
328 std::unique_ptr<gbm_device> device_;
329 #endif
330 sk_sp<GrContext> gr_context_;
234 331
235 DISALLOW_COPY_AND_ASSIGN(MotionEvents); 332 DISALLOW_COPY_AND_ASSIGN(MotionEvents);
236 }; 333 };
237 334
238 int MotionEvents::Run() { 335 int MotionEvents::Run() {
239 std::unique_ptr<wl_display> display(wl_display_connect(nullptr)); 336 display_.reset(wl_display_connect(nullptr));
240 if (!display) { 337 if (!display_) {
241 LOG(ERROR) << "wl_display_connect failed"; 338 LOG(ERROR) << "wl_display_connect failed";
242 return 1; 339 return 1;
243 } 340 }
244 341
342 bool gl_initialized = gl::init::InitializeGLOneOff();
343 DCHECK(gl_initialized);
344 scoped_refptr<gl::GLSurface> gl_surface =
345 gl::init::CreateOffscreenGLSurface(gfx::Size());
346 scoped_refptr<gl::GLContext> gl_context =
347 gl::init::CreateGLContext(nullptr, // share_group
348 gl_surface.get(), gl::PreferIntegratedGpu);
349 ui::ScopedMakeCurrent smc(gl_context.get(), gl_surface.get());
350
245 wl_registry_listener registry_listener = {RegistryHandler, RegistryRemover}; 351 wl_registry_listener registry_listener = {RegistryHandler, RegistryRemover};
246 352
247 Globals globals; 353 wl_registry* registry = wl_display_get_registry(display_.get());
248 wl_registry* registry = wl_display_get_registry(display.get()); 354 wl_registry_add_listener(registry, &registry_listener, &globals_);
249 wl_registry_add_listener(registry, &registry_listener, &globals);
250 355
251 wl_display_roundtrip(display.get()); 356 wl_display_roundtrip(display_.get());
252 357
253 if (!globals.compositor) { 358 if (!globals_.compositor) {
254 LOG(ERROR) << "Can't find compositor interface"; 359 LOG(ERROR) << "Can't find compositor interface";
255 return 1; 360 return 1;
256 } 361 }
257 if (!globals.shm) { 362 if (!globals_.shm) {
258 LOG(ERROR) << "Can't find shm interface"; 363 LOG(ERROR) << "Can't find shm interface";
259 return 1; 364 return 1;
260 } 365 }
261 if (!globals.shell) { 366 if (use_drm_ && !globals_.linux_dmabuf) {
367 LOG(ERROR) << "Can't find linux_dmabuf interface";
368 return 1;
369 }
370 if (!globals_.shell) {
262 LOG(ERROR) << "Can't find shell interface"; 371 LOG(ERROR) << "Can't find shell interface";
263 return 1; 372 return 1;
264 } 373 }
265 if (!globals.seat) { 374 if (!globals_.seat) {
266 LOG(ERROR) << "Can't find seat interface"; 375 LOG(ERROR) << "Can't find seat interface";
267 return 1; 376 return 1;
268 } 377 }
269 378
379 #if defined(OZONE_PLATFORM_GBM)
380 base::ScopedFD fd(open(kDriRenderNode, O_RDWR));
381 if (fd.get() < 0) {
382 LOG(ERROR) << "Can't open drm device '/dev/dri/renderD128'";
383 return 1;
384 }
385
386 device_.reset(gbm_create_device(fd.get()));
387 if (!device_) {
388 LOG(ERROR) << "Can't create gbm device";
389 return 1;
390 }
391 sk_sp<const GrGLInterface> native_interface(GrGLCreateNativeInterface());
392 DCHECK(native_interface);
393 gr_context_ = sk_sp<GrContext>(GrContext::Create(
394 kOpenGL_GrBackend,
395 reinterpret_cast<GrBackendContext>(native_interface.get())));
396 DCHECK(gr_context_);
397 #endif
398
270 wl_buffer_listener buffer_listener = {BufferRelease}; 399 wl_buffer_listener buffer_listener = {BufferRelease};
271 400
272 Buffer buffers[kBuffers];
273 base::SharedMemory shared_memory;
274 shared_memory.CreateAndMapAnonymous(buffer_size() * kBuffers);
275 std::unique_ptr<wl_shm_pool> shm_pool(
276 wl_shm_create_pool(globals.shm.get(), shared_memory.handle().fd,
277 shared_memory.requested_size()));
278 for (size_t i = 0; i < kBuffers; ++i) { 401 for (size_t i = 0; i < kBuffers; ++i) {
279 buffers[i].buffer.reset(static_cast<wl_buffer*>( 402 if (!CreateBuffer(&buffers_[i]))
280 wl_shm_pool_create_buffer(shm_pool.get(), i * buffer_size(), width_,
281 height_, stride(), kFormat)));
282 if (!buffers[i].buffer) {
283 LOG(ERROR) << "Can't create buffer";
284 return 1; 403 return 1;
285 } 404 }
286 buffers[i].sk_surface = SkSurface::MakeRasterDirect( 405 wl_display_roundtrip(display_.get());
287 SkImageInfo::Make(width_, height_, kColorType, kOpaque_SkAlphaType), 406 for (size_t i = 0; i < kBuffers; ++i) {
288 static_cast<uint8_t*>(shared_memory.memory()) + buffer_size() * i, 407 if (!buffers_[i].buffer)
289 stride());
290 if (!buffers[i].sk_surface) {
291 LOG(ERROR) << "Can't create SkSurface";
292 return 1; 408 return 1;
293 } 409
294 wl_buffer_add_listener(buffers[i].buffer.get(), &buffer_listener, 410 wl_buffer_add_listener(buffers_[i].buffer.get(), &buffer_listener,
295 &buffers[i]); 411 &buffers_[i]);
296 } 412 }
297 413
298 std::unique_ptr<wl_surface> surface(static_cast<wl_surface*>( 414 std::unique_ptr<wl_surface> surface(static_cast<wl_surface*>(
299 wl_compositor_create_surface(globals.compositor.get()))); 415 wl_compositor_create_surface(globals_.compositor.get())));
300 if (!surface) { 416 if (!surface) {
301 LOG(ERROR) << "Can't create surface"; 417 LOG(ERROR) << "Can't create surface";
302 return 1; 418 return 1;
303 } 419 }
304 420
305 std::unique_ptr<wl_region> opaque_region(static_cast<wl_region*>( 421 std::unique_ptr<wl_region> opaque_region(static_cast<wl_region*>(
306 wl_compositor_create_region(globals.compositor.get()))); 422 wl_compositor_create_region(globals_.compositor.get())));
307 if (!opaque_region) { 423 if (!opaque_region) {
308 LOG(ERROR) << "Can't create region"; 424 LOG(ERROR) << "Can't create region";
309 return 1; 425 return 1;
310 } 426 }
311 427
312 wl_region_add(opaque_region.get(), 0, 0, width_, height_); 428 wl_region_add(opaque_region.get(), 0, 0, width_, height_);
313 wl_surface_set_opaque_region(surface.get(), opaque_region.get()); 429 wl_surface_set_opaque_region(surface.get(), opaque_region.get());
314 430
315 std::unique_ptr<wl_shell_surface> shell_surface( 431 std::unique_ptr<wl_shell_surface> shell_surface(
316 static_cast<wl_shell_surface*>( 432 static_cast<wl_shell_surface*>(
317 wl_shell_get_shell_surface(globals.shell.get(), surface.get()))); 433 wl_shell_get_shell_surface(globals_.shell.get(), surface.get())));
318 if (!shell_surface) { 434 if (!shell_surface) {
319 LOG(ERROR) << "Can't get shell surface"; 435 LOG(ERROR) << "Can't get shell surface";
320 return 1; 436 return 1;
321 } 437 }
322 438
323 wl_shell_surface_set_title(shell_surface.get(), "Test Client"); 439 wl_shell_surface_set_title(shell_surface.get(), "Test Client");
324 if (fullscreen_) { 440 if (fullscreen_) {
325 wl_shell_surface_set_fullscreen(shell_surface.get(), 441 wl_shell_surface_set_fullscreen(shell_surface.get(),
326 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, 442 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
327 0, nullptr); 443 0, nullptr);
328 } else { 444 } else {
329 wl_shell_surface_set_toplevel(shell_surface.get()); 445 wl_shell_surface_set_toplevel(shell_surface.get());
330 } 446 }
331 447
332 EventTimeStack event_times; 448 EventTimeStack event_times;
333 449
334 std::unique_ptr<wl_pointer> pointer( 450 std::unique_ptr<wl_pointer> pointer(
335 static_cast<wl_pointer*>(wl_seat_get_pointer(globals.seat.get()))); 451 static_cast<wl_pointer*>(wl_seat_get_pointer(globals_.seat.get())));
336 if (!pointer) { 452 if (!pointer) {
337 LOG(ERROR) << "Can't get pointer"; 453 LOG(ERROR) << "Can't get pointer";
338 return 1; 454 return 1;
339 } 455 }
340 456
341 wl_pointer_listener pointer_listener = { 457 wl_pointer_listener pointer_listener = {
342 PointerEnter, PointerLeave, PointerMotion, 458 PointerEnter, PointerLeave, PointerMotion,
343 PointerButton, PointerAxis, PointerFrame, 459 PointerButton, PointerAxis, PointerFrame,
344 PointerAxisSource, PointerAxisStop, PointerDiscrete}; 460 PointerAxisSource, PointerAxisStop, PointerDiscrete};
345 wl_pointer_add_listener(pointer.get(), &pointer_listener, &event_times); 461 wl_pointer_add_listener(pointer.get(), &pointer_listener, &event_times);
346 462
347 std::unique_ptr<wl_touch> touch( 463 std::unique_ptr<wl_touch> touch(
348 static_cast<wl_touch*>(wl_seat_get_touch(globals.seat.get()))); 464 static_cast<wl_touch*>(wl_seat_get_touch(globals_.seat.get())));
349 if (!touch) { 465 if (!touch) {
350 LOG(ERROR) << "Can't get touch"; 466 LOG(ERROR) << "Can't get touch";
351 return 1; 467 return 1;
352 } 468 }
353 469
354 wl_touch_listener touch_listener = {TouchDown, TouchUp, TouchMotion, 470 wl_touch_listener touch_listener = {TouchDown, TouchUp, TouchMotion,
355 TouchFrame, TouchCancel}; 471 TouchFrame, TouchCancel};
356 wl_touch_add_listener(touch.get(), &touch_listener, &event_times); 472 wl_touch_add_listener(touch.get(), &touch_listener, &event_times);
357 473
358 Frame frame; 474 Frame frame;
359 std::unique_ptr<wl_callback> frame_callback; 475 std::unique_ptr<wl_callback> frame_callback;
360 wl_callback_listener frame_listener = {FrameCallback}; 476 wl_callback_listener frame_listener = {FrameCallback};
361 477
362 uint32_t frames = 0; 478 uint32_t frames = 0;
363 base::TimeTicks benchmark_time = base::TimeTicks::Now(); 479 base::TimeTicks benchmark_time = base::TimeTicks::Now();
364 base::TimeDelta benchmark_interval = 480 base::TimeDelta benchmark_interval =
365 base::TimeDelta::FromSeconds(kBenchmarkInterval); 481 base::TimeDelta::FromSeconds(kBenchmarkInterval);
366 482
367 do { 483 do {
368 if (frame.callback_pending) 484 if (frame.callback_pending)
369 continue; 485 continue;
370 486
371 Buffer* buffer = 487 Buffer* buffer =
372 std::find_if(std::begin(buffers), std::end(buffers), 488 std::find_if(std::begin(buffers_), std::end(buffers_),
373 [](const Buffer& buffer) { return !buffer.busy; }); 489 [](const Buffer& buffer) { return !buffer.busy; });
374 if (buffer == std::end(buffers)) 490 if (buffer == std::end(buffers_))
375 continue; 491 continue;
376 492
377 base::TimeTicks current_time = base::TimeTicks::Now(); 493 base::TimeTicks current_time = base::TimeTicks::Now();
378 if ((current_time - benchmark_time) > benchmark_interval) { 494 if ((current_time - benchmark_time) > benchmark_interval) {
379 std::cout << frames << " frames in " << benchmark_interval.InSeconds() 495 std::cout << frames << " frames in " << benchmark_interval.InSeconds()
380 << " seconds: " 496 << " seconds: "
381 << static_cast<double>(frames) / benchmark_interval.InSeconds() 497 << static_cast<double>(frames) / benchmark_interval.InSeconds()
382 << " fps" << std::endl; 498 << " fps" << std::endl;
383 benchmark_time = current_time; 499 benchmark_time = current_time;
384 frames = 0; 500 frames = 0;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 for (size_t i = 0; i < num_rects_; ++i) { 535 for (size_t i = 0; i < num_rects_; ++i) {
420 const SkColor kColors[] = {SK_ColorBLUE, SK_ColorGREEN, 536 const SkColor kColors[] = {SK_ColorBLUE, SK_ColorGREEN,
421 SK_ColorRED, SK_ColorYELLOW, 537 SK_ColorRED, SK_ColorYELLOW,
422 SK_ColorCYAN, SK_ColorMAGENTA}; 538 SK_ColorCYAN, SK_ColorMAGENTA};
423 paint.setColor(SkColorSetA(kColors[i % arraysize(kColors)], 0xA0)); 539 paint.setColor(SkColorSetA(kColors[i % arraysize(kColors)], 0xA0));
424 canvas->rotate(rotation / num_rects_); 540 canvas->rotate(rotation / num_rects_);
425 canvas->drawIRect(rect, paint); 541 canvas->drawIRect(rect, paint);
426 } 542 }
427 canvas->restore(); 543 canvas->restore();
428 544
545 if (gr_context_) {
546 gr_context_->flush();
547 glFinish();
548 }
549
429 wl_surface_set_buffer_scale(surface.get(), scale_); 550 wl_surface_set_buffer_scale(surface.get(), scale_);
430 wl_surface_attach(surface.get(), buffer->buffer.get(), 0, 0); 551 wl_surface_attach(surface.get(), buffer->buffer.get(), 0, 0);
431 buffer->busy = true; 552 buffer->busy = true;
432 553
433 frame_callback.reset(wl_surface_frame(surface.get())); 554 frame_callback.reset(wl_surface_frame(surface.get()));
434 wl_callback_add_listener(frame_callback.get(), &frame_listener, &frame); 555 wl_callback_add_listener(frame_callback.get(), &frame_listener, &frame);
435 frame.callback_pending = true; 556 frame.callback_pending = true;
436 557
437 ++frames; 558 ++frames;
438 559
439 wl_surface_commit(surface.get()); 560 wl_surface_commit(surface.get());
440 } while (wl_display_dispatch(display.get()) != -1); 561 } while (wl_display_dispatch(display_.get()) != -1);
441 562
442 return 0; 563 return 0;
443 } 564 }
444 565
566 bool MotionEvents::CreateBuffer(Buffer* buffer) {
567 #if defined(OZONE_PLATFORM_GBM)
568 if (use_drm_) {
569 buffer->bo.reset(gbm_bo_create(device_.get(), width_, height_, kDrmFormat,
570 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING));
571 if (!buffer->bo) {
572 LOG(ERROR) << "Can't create gbm buffer.";
573 return false;
574 }
575 base::ScopedFD fd(gbm_bo_get_plane_fd(buffer->bo.get(), 0));
576 static const zwp_linux_buffer_params_v1_listener params_listener = {
577 LinuxBufferParamsCreated, LinuxBufferParamsFailed};
578
579 buffer->params.reset(
580 zwp_linux_dmabuf_v1_create_params(globals_.linux_dmabuf.get()));
581 zwp_linux_buffer_params_v1_add_listener(buffer->params.get(),
582 &params_listener, buffer);
583 zwp_linux_buffer_params_v1_add(buffer->params.get(), fd.get(), 0, 0,
584 stride(), 0, 0);
585 zwp_linux_buffer_params_v1_create(buffer->params.get(), width_, height_,
586 kDrmFormat, 0);
587
588 EGLint khr_image_attrs[] = {EGL_DMA_BUF_PLANE0_FD_EXT,
589 fd.get(),
590 EGL_WIDTH,
591 width_,
592 EGL_HEIGHT,
593 height_,
594 EGL_LINUX_DRM_FOURCC_EXT,
595 kDrmFormat,
596 EGL_DMA_BUF_PLANE0_PITCH_EXT,
597 stride(),
598 EGL_DMA_BUF_PLANE0_OFFSET_EXT,
599 0,
600 EGL_NONE};
601
602 buffer->egl_image.reset(new ScopedEglImage(eglCreateImageKHR(
603 eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT,
604 nullptr /* no client buffer */, khr_image_attrs)));
605 buffer->texture.reset(new ScopedTexture());
606 glBindTexture(GL_TEXTURE_2D, buffer->texture->id_);
607 glEGLImageTargetTexture2DOES(
608 GL_TEXTURE_2D, static_cast<GLeglImageOES>(buffer->egl_image->image_));
609 glBindTexture(GL_TEXTURE_2D, 0);
610
611 GrGLTextureInfo texture_info;
612 texture_info.fID = buffer->texture->id_;
613 texture_info.fTarget = GL_TEXTURE_2D;
614 GrBackendTextureDesc desc;
615 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
616 desc.fWidth = width_;
617 desc.fHeight = height_;
618 desc.fConfig = kBGRA_8888_GrPixelConfig;
619 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
620 desc.fTextureHandle = reinterpret_cast<GrBackendObject>(&texture_info);
621
622 buffer->sk_surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
623 gr_context_.get(), desc, nullptr);
624 DCHECK(buffer->sk_surface);
625
626 return true;
627 }
628 #endif
629 buffer->shared_memory.CreateAndMapAnonymous(buffer_size());
630 buffer->shm_pool.reset(
631 wl_shm_create_pool(globals_.shm.get(), buffer->shared_memory.handle().fd,
632 buffer->shared_memory.requested_size()));
633
634 buffer->buffer.reset(static_cast<wl_buffer*>(wl_shm_pool_create_buffer(
635 buffer->shm_pool.get(), 0, width_, height_, stride(), kShmFormat)));
636 if (!buffer->buffer) {
637 LOG(ERROR) << "Can't create buffer";
638 return false;
639 }
640 buffer->sk_surface = SkSurface::MakeRasterDirect(
641 SkImageInfo::Make(width_, height_, kColorType, kOpaque_SkAlphaType),
642 static_cast<uint8_t*>(buffer->shared_memory.memory()), stride());
643
644 return true;
645 }
646
445 } // namespace clients 647 } // namespace clients
446 } // namespace wayland 648 } // namespace wayland
447 } // namespace exo 649 } // namespace exo
448 650
449 namespace switches { 651 namespace switches {
450 652
451 // Specifies the client buffer size. 653 // Specifies the client buffer size.
452 const char kSize[] = "size"; 654 const char kSize[] = "size";
453 655
454 // Specifies the client scale factor (ie. number of physical pixels per DIP). 656 // Specifies the client scale factor (ie. number of physical pixels per DIP).
455 const char kScale[] = "scale"; 657 const char kScale[] = "scale";
456 658
457 // Specifies the number of rotating rects to draw. 659 // Specifies the number of rotating rects to draw.
458 const char kNumRects[] = "num-rects"; 660 const char kNumRects[] = "num-rects";
459 661
460 // Specifies if client should be fullscreen. 662 // Specifies if client should be fullscreen.
461 const char kFullscreen[] = "fullscreen"; 663 const char kFullscreen[] = "fullscreen";
462 664
665 // Use drm buffer instead of shared memory.
666 const char kUseDrm[] = "use-drm";
667
463 } // namespace switches 668 } // namespace switches
464 669
465 int main(int argc, char* argv[]) { 670 int main(int argc, char* argv[]) {
466 base::CommandLine command_line(argc, argv); 671 base::AtExitManager exit_manager;
672 base::CommandLine::Init(argc, argv);
673 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
467 674
468 int width = 256; 675 int width = 256;
469 int height = 256; 676 int height = 256;
470 if (command_line.HasSwitch(switches::kSize)) { 677 if (command_line->HasSwitch(switches::kSize)) {
471 std::string size_str = command_line.GetSwitchValueASCII(switches::kSize); 678 std::string size_str = command_line->GetSwitchValueASCII(switches::kSize);
472 if (sscanf(size_str.c_str(), "%dx%d", &width, &height) != 2) { 679 if (sscanf(size_str.c_str(), "%dx%d", &width, &height) != 2) {
473 LOG(ERROR) << "Invalid value for " << switches::kSize; 680 LOG(ERROR) << "Invalid value for " << switches::kSize;
474 return 1; 681 return 1;
475 } 682 }
476 } 683 }
477
478 int scale = 1; 684 int scale = 1;
479 if (command_line.HasSwitch(switches::kScale) && 685 if (command_line->HasSwitch(switches::kScale) &&
480 !base::StringToInt(command_line.GetSwitchValueASCII(switches::kScale), 686 !base::StringToInt(command_line->GetSwitchValueASCII(switches::kScale),
481 &scale)) { 687 &scale)) {
482 LOG(ERROR) << "Invalid value for " << switches::kScale; 688 LOG(ERROR) << "Invalid value for " << switches::kScale;
483 return 1; 689 return 1;
484 } 690 }
485
486 size_t num_rects = 1; 691 size_t num_rects = 1;
487 if (command_line.HasSwitch(switches::kNumRects) && 692 if (command_line->HasSwitch(switches::kNumRects) &&
488 !base::StringToSizeT( 693 !base::StringToSizeT(
489 command_line.GetSwitchValueASCII(switches::kNumRects), &num_rects)) { 694 command_line->GetSwitchValueASCII(switches::kNumRects), &num_rects)) {
490 LOG(ERROR) << "Invalid value for " << switches::kNumRects; 695 LOG(ERROR) << "Invalid value for " << switches::kNumRects;
491 return 1; 696 return 1;
492 } 697 }
493 698 bool fullscreen = command_line->HasSwitch(switches::kFullscreen);
494 bool fullscreen = command_line.HasSwitch(switches::kFullscreen); 699 bool use_drm = command_line->HasSwitch(switches::kUseDrm);
495 700
496 exo::wayland::clients::MotionEvents client(width, height, scale, num_rects, 701 exo::wayland::clients::MotionEvents client(width, height, scale, num_rects,
497 fullscreen); 702 fullscreen, use_drm);
498 return client.Run(); 703 return client.Run();
499 } 704 }
OLDNEW
« no previous file with comments | « components/exo/wayland/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698