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

Side by Side Diff: components/exo/wayland/wayland_bindings.h

Issue 1412093006: components: Add Exosphere component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase on improved shared memory support Created 5 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
OLDNEW
(Empty)
1 // Copyright 2015 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 // TODO(reveman): Most of code in this file could be auto-generated from
6 // the Wayland protocol description.
7
8 #ifndef COMPONENTS_EXO_WAYLAND_WAYLAND_BINDINGS_H_
9 #define COMPONENTS_EXO_WAYLAND_WAYLAND_BINDINGS_H_
10
11 template <class T>
12 T* DataAs(void* data) {
13 return static_cast<T*>(data);
14 }
15
16 template <class T>
17 T* GetUserDataAs(wl_resource* resource) {
18 return DataAs<T>(wl_resource_get_user_data(resource));
19 }
20
21 template <class T>
22 scoped_ptr<T> TakeUserDataAs(wl_resource* resource) {
23 scoped_ptr<T> user_data = make_scoped_ptr(GetUserDataAs<T>(resource));
24 wl_resource_set_user_data(resource, nullptr);
25 return user_data.Pass();
26 }
27
28 template <class T>
29 void DestroyUserData(wl_resource* resource) {
30 TakeUserDataAs<T>(resource);
31 }
32
33 template <class T>
34 void SetImplementation(wl_resource* resource,
35 const void* implementation,
36 scoped_ptr<T> user_data) {
37 wl_resource_set_implementation(resource, implementation, user_data.release(),
38 DestroyUserData<T>);
39 }
40
41 ////////////////////////////////////////////////////////////////////////////////
42 // wl_buffer_interface:
43
44 void buffer_destroy(wl_client* client, wl_resource* resource) {
45 wl_resource_destroy(resource);
46 }
47
48 const struct wl_buffer_interface buffer_implementation = {buffer_destroy};
49
50 ////////////////////////////////////////////////////////////////////////////////
51 // wl_surface_interface:
52
53 void surface_destroy(wl_client* client, wl_resource* resource) {
54 wl_resource_destroy(resource);
55 }
56
57 void surface_attach(wl_client* client,
58 wl_resource* resource,
59 wl_resource* buffer,
60 int32_t x,
61 int32_t y) {
62 GetUserDataAs<Surface>(resource)->Attach(
63 buffer ? GetUserDataAs<Buffer>(buffer) : nullptr, gfx::Point(x, y));
64 }
65
66 void surface_damage(wl_client* client,
67 wl_resource* resource,
68 int32_t x,
69 int32_t y,
70 int32_t width,
71 int32_t height) {
72 GetUserDataAs<Surface>(resource)->Damage(gfx::Rect(x, y, width, height));
73 }
74
75 void handle_surface_frame_callback(wl_resource* resource,
76 base::TimeTicks frame_time) {
77 if (!frame_time.is_null()) {
78 wl_callback_send_done(resource,
79 (frame_time - base::TimeTicks()).InMilliseconds());
80 // TODO(reveman): Remove this potentially blocking flush and instead watch
81 // the file descriptor to be ready for write without blocking.
82 wl_client_flush(wl_resource_get_client(resource));
83 }
84 wl_resource_destroy(resource);
85 }
86
87 void surface_frame(wl_client* client,
88 wl_resource* resource,
89 uint32_t callback) {
90 wl_resource* callback_resource =
91 wl_resource_create(client, &wl_callback_interface, 1, callback);
92 if (!callback_resource) {
93 wl_resource_post_no_memory(resource);
94 return;
95 }
96
97 // base::Unretained is safe as the resource owns the callback.
98 scoped_ptr<base::CancelableCallback<void(base::TimeTicks)>>
99 cancelable_callback(new base::CancelableCallback<void(base::TimeTicks)>(
100 base::Bind(&handle_surface_frame_callback,
101 base::Unretained(callback_resource))));
102
103 GetUserDataAs<Surface>(resource)
104 ->RequestFrameCallback(cancelable_callback->callback());
105
106 SetImplementation(callback_resource, nullptr, cancelable_callback.Pass());
107 }
108
109 void surface_set_opaque_region(wl_client* client,
110 wl_resource* resource,
111 wl_resource* region_resource) {
112 GetUserDataAs<Surface>(resource)->SetOpaqueRegion(
113 region_resource ? *GetUserDataAs<cc::Region>(region_resource)
114 : cc::Region());
115 }
116
117 void surface_set_input_region(wl_client* client,
118 wl_resource* resource,
119 wl_resource* region_resource) {
120 NOTIMPLEMENTED();
121 }
122
123 void surface_commit(wl_client* client, wl_resource* resource) {
124 GetUserDataAs<Surface>(resource)->Commit();
125 }
126
127 void surface_set_buffer_transform(wl_client* client,
128 wl_resource* resource,
129 int transform) {
130 NOTIMPLEMENTED();
131 }
132
133 void surface_set_buffer_scale(wl_client* client,
134 wl_resource* resource,
135 int32_t scale) {
136 NOTIMPLEMENTED();
137 }
138
139 const struct wl_surface_interface surface_implementation = {
140 surface_destroy,
141 surface_attach,
142 surface_damage,
143 surface_frame,
144 surface_set_opaque_region,
145 surface_set_input_region,
146 surface_commit,
147 surface_set_buffer_transform,
148 surface_set_buffer_scale};
149
150 ////////////////////////////////////////////////////////////////////////////////
151 // wl_region_interface:
152
153 void region_destroy(wl_client* client, wl_resource* resource) {
154 wl_resource_destroy(resource);
155 }
156
157 void region_add(wl_client* client,
158 wl_resource* resource,
159 int32_t x,
160 int32_t y,
161 int32_t width,
162 int32_t height) {
163 GetUserDataAs<cc::Region>(resource)->Union(gfx::Rect(x, y, width, height));
164 }
165
166 static void region_subtract(wl_client* client,
167 wl_resource* resource,
168 int32_t x,
169 int32_t y,
170 int32_t width,
171 int32_t height) {
172 GetUserDataAs<cc::Region>(resource)->Subtract(gfx::Rect(x, y, width, height));
173 }
174
175 const struct wl_region_interface region_implementation = {
176 region_destroy, region_add, region_subtract};
177
178 ////////////////////////////////////////////////////////////////////////////////
179 // wl_compositor_interface:
180
181 void compositor_create_surface(wl_client* client,
182 wl_resource* resource,
183 uint32_t id) {
184 scoped_ptr<Surface> surface =
185 GetUserDataAs<Display>(resource)->CreateSurface();
186 DCHECK(surface);
187
188 wl_resource* surface_resource = wl_resource_create(
189 client, &wl_surface_interface, wl_resource_get_version(resource), id);
190 if (!surface_resource) {
191 wl_resource_post_no_memory(resource);
192 return;
193 }
194
195 SetImplementation(surface_resource, &surface_implementation, surface.Pass());
196 }
197
198 void compositor_create_region(wl_client* client,
199 wl_resource* resource,
200 uint32_t id) {
201 scoped_ptr<cc::Region> region(new cc::Region);
202
203 wl_resource* region_resource =
204 wl_resource_create(client, &wl_region_interface, 1, id);
205 if (!region_resource) {
206 wl_resource_post_no_memory(resource);
207 return;
208 }
209
210 SetImplementation(region_resource, &region_implementation, region.Pass());
211 }
212
213 const struct wl_compositor_interface compositor_implementation = {
214 compositor_create_surface, compositor_create_region};
215
216 const uint32_t compositor_version = 3;
217
218 void bind_compositor(wl_client* client,
219 void* data,
220 uint32_t version,
221 uint32_t id) {
222 wl_resource* resource =
223 wl_resource_create(client, &wl_compositor_interface,
224 std::min(version, compositor_version), id);
225 if (!resource) {
226 wl_client_post_no_memory(client);
227 return;
228 }
229
230 wl_resource_set_implementation(resource, &compositor_implementation, data,
231 nullptr);
232 }
233
234 ////////////////////////////////////////////////////////////////////////////////
235 // wl_shm_pool_interface:
236
237 const struct shm_supported_format {
238 uint32_t shm_format;
239 gfx::BufferFormat buffer_format;
240 } shm_supported_formats[] = {
241 {WL_SHM_FORMAT_XBGR8888, gfx::BufferFormat::RGBX_8888},
242 {WL_SHM_FORMAT_ABGR8888, gfx::BufferFormat::RGBA_8888},
243 {WL_SHM_FORMAT_XRGB8888, gfx::BufferFormat::BGRX_8888},
244 {WL_SHM_FORMAT_ARGB8888, gfx::BufferFormat::BGRA_8888}};
245
246 void shm_pool_create_buffer(wl_client* client,
247 wl_resource* resource,
248 uint32_t id,
249 int32_t offset,
250 int32_t width,
251 int32_t height,
252 int32_t stride,
253 uint32_t format) {
254 const auto supported_format =
255 std::find_if(shm_supported_formats,
256 shm_supported_formats + arraysize(shm_supported_formats),
257 [format](const shm_supported_format& supported_format) {
258 return supported_format.shm_format == format;
259 });
260 if (supported_format ==
261 (shm_supported_formats + arraysize(shm_supported_formats))) {
262 wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FORMAT,
263 "invalid format 0x%x", format);
264 return;
265 }
266
267 if (offset < 0) {
268 wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FORMAT,
269 "invalid offset %d", offset);
270 return;
271 }
272
273 scoped_ptr<Buffer> buffer =
274 GetUserDataAs<SharedMemory>(resource)
275 ->CreateBuffer(gfx::Size(width, height),
276 supported_format->buffer_format, offset, stride);
277 if (!buffer) {
278 wl_resource_post_no_memory(resource);
279 return;
280 }
281
282 wl_resource* buffer_resource =
283 wl_resource_create(client, &wl_buffer_interface, 1, id);
284 if (!buffer_resource) {
285 wl_resource_post_no_memory(resource);
286 return;
287 }
288
289 buffer->set_release_callback(
290 base::Bind(&wl_buffer_send_release, base::Unretained(buffer_resource)));
291
292 SetImplementation(buffer_resource, &buffer_implementation, buffer.Pass());
293 }
294
295 void shm_pool_destroy(wl_client* client, wl_resource* resource) {
296 wl_resource_destroy(resource);
297 }
298
299 void shm_pool_resize(wl_client* client, wl_resource* resource, int32_t size) {
300 // Nothing to do here.
301 }
302
303 const struct wl_shm_pool_interface shm_pool_implementation = {
304 shm_pool_create_buffer, shm_pool_destroy, shm_pool_resize};
305
306 ////////////////////////////////////////////////////////////////////////////////
307 // wl_shm_interface:
308
309 void shm_create_pool(wl_client* client,
310 wl_resource* resource,
311 uint32_t id,
312 int fd,
313 int32_t size) {
314 scoped_ptr<SharedMemory> shared_memory =
315 GetUserDataAs<Display>(resource)
316 ->CreateSharedMemory(base::FileDescriptor(fd, true), size);
317 if (!shared_memory) {
318 wl_resource_post_no_memory(resource);
319 return;
320 }
321
322 wl_resource* shm_pool_resource =
323 wl_resource_create(client, &wl_shm_pool_interface, 1, id);
324 if (!shm_pool_resource) {
325 wl_resource_post_no_memory(resource);
326 return;
327 }
328
329 SetImplementation(shm_pool_resource, &shm_pool_implementation,
330 shared_memory.Pass());
331 }
332
333 const struct wl_shm_interface shm_implementation = {shm_create_pool};
334
335 void bind_shm(wl_client* client, void* data, uint32_t version, uint32_t id) {
336 wl_resource* resource = wl_resource_create(client, &wl_shm_interface, 1, id);
337 if (!resource) {
338 wl_client_post_no_memory(client);
339 return;
340 }
341
342 wl_resource_set_implementation(resource, &shm_implementation, data, nullptr);
343
344 for (const auto& supported_format : shm_supported_formats)
345 wl_shm_send_format(resource, supported_format.shm_format);
346 }
347
348 ////////////////////////////////////////////////////////////////////////////////
349 // wl_shell_surface_interface:
350
351 void shell_surface_pong(wl_client* client,
352 wl_resource* resource,
353 uint32_t serial) {
354 NOTIMPLEMENTED();
355 }
356
357 void shell_surface_move(wl_client* client,
358 wl_resource* resource,
359 wl_resource* seat_resource,
360 uint32_t serial) {
361 NOTIMPLEMENTED();
362 }
363
364 void shell_surface_resize(wl_client* client,
365 wl_resource* resource,
366 wl_resource* seat_resource,
367 uint32_t serial,
368 uint32_t edges) {
369 NOTIMPLEMENTED();
370 }
371
372 void shell_surface_set_toplevel(wl_client* client, wl_resource* resource) {
373 GetUserDataAs<ShellSurface>(resource)->SetToplevel();
374 }
375
376 void shell_surface_set_transient(wl_client* client,
377 wl_resource* resource,
378 wl_resource* parent_resource,
379 int x,
380 int y,
381 uint32_t flags) {
382 NOTIMPLEMENTED();
383 }
384
385 void shell_surface_set_fullscreen(wl_client* client,
386 wl_resource* resource,
387 uint32_t method,
388 uint32_t framerate,
389 wl_resource* output_resource) {
390 GetUserDataAs<ShellSurface>(resource)->SetFullscreen(true);
391 }
392
393 void shell_surface_set_popup(wl_client* client,
394 wl_resource* resource,
395 wl_resource* seat_resource,
396 uint32_t serial,
397 wl_resource* parent_resource,
398 int32_t x,
399 int32_t y,
400 uint32_t flags) {
401 NOTIMPLEMENTED();
402 }
403
404 void shell_surface_set_maximized(wl_client* client,
405 wl_resource* resource,
406 wl_resource* output_resource) {
407 NOTIMPLEMENTED();
408 }
409
410 void shell_surface_set_title(wl_client* client,
411 wl_resource* resource,
412 const char* title) {
413 GetUserDataAs<ShellSurface>(resource)
414 ->SetTitle(base::string16(base::ASCIIToUTF16(title)));
415 }
416
417 void shell_surface_set_class(wl_client* client,
418 wl_resource* resource,
419 const char* clazz) {
420 NOTIMPLEMENTED();
421 }
422
423 const struct wl_shell_surface_interface shell_surface_implementation = {
424 shell_surface_pong, shell_surface_move,
425 shell_surface_resize, shell_surface_set_toplevel,
426 shell_surface_set_transient, shell_surface_set_fullscreen,
427 shell_surface_set_popup, shell_surface_set_maximized,
428 shell_surface_set_title, shell_surface_set_class};
429
430 ////////////////////////////////////////////////////////////////////////////////
431 // wl_shell_interface:
432
433 void shell_get_shell_surface(wl_client* client,
434 wl_resource* resource,
435 uint32_t id,
436 wl_resource* surface) {
437 scoped_ptr<ShellSurface> shell_surface =
438 GetUserDataAs<Display>(resource)
439 ->GetShellSurface(GetUserDataAs<Surface>(surface));
440 if (!shell_surface) {
441 wl_resource_post_no_memory(resource);
442 return;
443 }
444
445 wl_resource* shell_surface_resource =
446 wl_resource_create(client, &wl_shell_surface_interface, 1, id);
447 if (!shell_surface_resource) {
448 wl_resource_post_no_memory(resource);
449 return;
450 }
451
452 SetImplementation(shell_surface_resource, &shell_surface_implementation,
453 shell_surface.Pass());
454 }
455
456 const struct wl_shell_interface shell_implementation = {
457 shell_get_shell_surface};
458
459 void bind_shell(wl_client* client, void* data, uint32_t version, uint32_t id) {
460 wl_resource* resource =
461 wl_resource_create(client, &wl_shell_interface, 1, id);
462 if (!resource) {
463 wl_client_post_no_memory(client);
464 return;
465 }
466 wl_resource_set_implementation(resource, &shell_implementation, data,
467 nullptr);
468 }
469
470 #endif // COMPONENTS_EXO_WAYLAND_WAYLAND_BINDINGS_H_
OLDNEW
« components/exo/shared_memory.h ('K') | « components/exo/wayland/server_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698