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

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

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