1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/exo/wayland/clients/client_helper.h"
6 
7 #include <input-timestamps-unstable-v1-client-protocol.h>
8 #include <linux-dmabuf-unstable-v1-client-protocol.h>
9 #include <linux-explicit-synchronization-unstable-v1-client-protocol.h>
10 #include <presentation-time-client-protocol.h>
11 #include <wayland-client-core.h>
12 #include <wayland-client-protocol.h>
13 
14 #include "ui/gl/gl_bindings.h"
15 #include "ui/gl/gl_enums.h"
16 
17 #if defined(USE_GBM)
18 #include <gbm.h>
19 #if defined(USE_VULKAN)
20 #include "gpu/vulkan/vulkan_function_pointers.h"
21 #endif  // defined(USE_VULKAN)
22 #endif  // defined(USE_GBM)
23 
24 // Convenient macro that is used to define default deleters for object
25 // types allowing them to be used with std::unique_ptr.
26 #define DEFAULT_DELETER(TypeName, DeleteFunction)            \
27   namespace std {                                            \
28   void default_delete<TypeName>::operator()(TypeName* ptr) { \
29     DeleteFunction(ptr);                                     \
30   }                                                          \
31   }
32 
33 DEFAULT_DELETER(wl_buffer, wl_buffer_destroy)
34 DEFAULT_DELETER(wl_callback, wl_callback_destroy)
35 DEFAULT_DELETER(wl_compositor, wl_compositor_destroy)
36 DEFAULT_DELETER(wl_display, wl_display_disconnect)
37 DEFAULT_DELETER(wl_pointer, wl_pointer_destroy)
38 DEFAULT_DELETER(wl_region, wl_region_destroy)
39 DEFAULT_DELETER(wl_registry, wl_registry_destroy)
40 DEFAULT_DELETER(wl_seat, wl_seat_destroy)
41 DEFAULT_DELETER(wl_shell, wl_shell_destroy)
42 DEFAULT_DELETER(wl_shell_surface, wl_shell_surface_destroy)
43 DEFAULT_DELETER(wl_shm, wl_shm_destroy)
44 DEFAULT_DELETER(wl_shm_pool, wl_shm_pool_destroy)
45 DEFAULT_DELETER(wl_subcompositor, wl_subcompositor_destroy)
46 DEFAULT_DELETER(wl_subsurface, wl_subsurface_destroy)
47 DEFAULT_DELETER(wl_surface, wl_surface_destroy)
48 DEFAULT_DELETER(wl_touch, wl_touch_destroy)
49 DEFAULT_DELETER(wl_output, wl_output_destroy)
50 DEFAULT_DELETER(wp_presentation, wp_presentation_destroy)
51 DEFAULT_DELETER(struct wp_presentation_feedback,
52                 wp_presentation_feedback_destroy)
53 DEFAULT_DELETER(zaura_shell, zaura_shell_destroy)
54 DEFAULT_DELETER(zaura_surface, zaura_surface_destroy)
55 DEFAULT_DELETER(zaura_output, zaura_output_destroy)
56 DEFAULT_DELETER(zwp_linux_buffer_release_v1,
57                 zwp_linux_buffer_release_v1_destroy)
58 DEFAULT_DELETER(zwp_fullscreen_shell_v1, zwp_fullscreen_shell_v1_destroy)
59 DEFAULT_DELETER(zwp_input_timestamps_manager_v1,
60                 zwp_input_timestamps_manager_v1_destroy)
61 DEFAULT_DELETER(zwp_input_timestamps_v1, zwp_input_timestamps_v1_destroy)
62 DEFAULT_DELETER(zwp_linux_buffer_params_v1, zwp_linux_buffer_params_v1_destroy)
63 DEFAULT_DELETER(zwp_linux_dmabuf_v1, zwp_linux_dmabuf_v1_destroy)
64 DEFAULT_DELETER(zwp_linux_explicit_synchronization_v1,
65                 zwp_linux_explicit_synchronization_v1_destroy)
66 DEFAULT_DELETER(zwp_linux_surface_synchronization_v1,
67                 zwp_linux_surface_synchronization_v1_destroy)
68 DEFAULT_DELETER(zcr_color_space_v1, zcr_color_space_v1_destroy)
69 DEFAULT_DELETER(zcr_vsync_feedback_v1, zcr_vsync_feedback_v1_destroy)
70 DEFAULT_DELETER(zcr_vsync_timing_v1, zcr_vsync_timing_v1_destroy)
71 
72 #if defined(USE_GBM)
73 DEFAULT_DELETER(gbm_bo, gbm_bo_destroy)
74 DEFAULT_DELETER(gbm_device, gbm_device_destroy)
75 #endif
76 
77 namespace exo {
78 namespace wayland {
79 namespace clients {
80 
81 #if defined(USE_GBM)
InvalidValue()82 GLuint DeleteTextureTraits::InvalidValue() {
83   return 0;
84 }
Free(GLuint texture)85 void DeleteTextureTraits::Free(GLuint texture) {
86   glDeleteTextures(1, &texture);
87 }
88 
InvalidValue()89 EGLImageKHR DeleteEglImageTraits::InvalidValue() {
90   return 0;
91 }
Free(EGLImageKHR image)92 void DeleteEglImageTraits::Free(EGLImageKHR image) {
93   eglDestroyImageKHR(eglGetCurrentDisplay(), image);
94 }
95 
InvalidValue()96 EGLSyncKHR DeleteEglSyncTraits::InvalidValue() {
97   return 0;
98 }
Free(EGLSyncKHR sync)99 void DeleteEglSyncTraits::Free(EGLSyncKHR sync) {
100   eglDestroySyncKHR(eglGetCurrentDisplay(), sync);
101 }
102 
103 #if defined(USE_VULKAN)
InvalidValue()104 VkInstance DeleteVkInstanceTraits::InvalidValue() {
105   return VK_NULL_HANDLE;
106 }
Free(VkInstance instance)107 void DeleteVkInstanceTraits::Free(VkInstance instance) {
108   vkDestroyInstance(instance, nullptr);
109 }
110 
InvalidValue()111 VkDevice DeleteVkDeviceTraits::InvalidValue() {
112   return VK_NULL_HANDLE;
113 }
Free(VkDevice device)114 void DeleteVkDeviceTraits::Free(VkDevice device) {
115   vkDestroyDevice(device, nullptr);
116 }
117 
InvalidValue()118 VkCommandPool DeleteVkCommandPoolTraits::InvalidValue() {
119   return VK_NULL_HANDLE;
120 }
Free(VkCommandPool command_pool)121 void DeleteVkCommandPoolTraits::Free(VkCommandPool command_pool) {
122   vkDestroyCommandPool(vk_device, command_pool, nullptr);
123 }
124 
InvalidValue()125 VkRenderPass DeleteVkRenderPassTraits::InvalidValue() {
126   return VK_NULL_HANDLE;
127 }
Free(VkRenderPass render_pass)128 void DeleteVkRenderPassTraits::Free(VkRenderPass render_pass) {
129   vkDestroyRenderPass(vk_device, render_pass, nullptr);
130 }
131 
InvalidValue()132 VkDeviceMemory DeleteVkDeviceMemoryTraits::InvalidValue() {
133   return VK_NULL_HANDLE;
134 }
Free(VkDeviceMemory device_memory)135 void DeleteVkDeviceMemoryTraits::Free(VkDeviceMemory device_memory) {
136   vkFreeMemory(vk_device, device_memory, nullptr);
137 }
138 
InvalidValue()139 VkImage DeleteVkImageTraits::InvalidValue() {
140   return VK_NULL_HANDLE;
141 }
Free(VkImage image)142 void DeleteVkImageTraits::Free(VkImage image) {
143   vkDestroyImage(vk_device, image, nullptr);
144 }
145 
InvalidValue()146 VkImageView DeleteVkImageViewTraits::InvalidValue() {
147   return VK_NULL_HANDLE;
148 }
Free(VkImageView image_view)149 void DeleteVkImageViewTraits::Free(VkImageView image_view) {
150   vkDestroyImageView(vk_device, image_view, nullptr);
151 }
152 
InvalidValue()153 VkFramebuffer DeleteVkFramebufferTraits::InvalidValue() {
154   return VK_NULL_HANDLE;
155 }
Free(VkFramebuffer framebuffer)156 void DeleteVkFramebufferTraits::Free(VkFramebuffer framebuffer) {
157   vkDestroyFramebuffer(vk_device, framebuffer, nullptr);
158 }
159 
160 #endif  // defined(USE_VULKAN)
161 #endif  // defined(USE_GBM)
162 
163 }  // namespace clients
164 }  // namespace wayland
165 }  // namespace exo
166