1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "host/libs/wayland/wayland_compositor.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <virtio-gpu-metadata-v1.h>
22 #include <wayland-server-core.h>
23 #include <wayland-server-protocol.h>
24 
25 #include "host/libs/wayland/wayland_surface.h"
26 #include "host/libs/wayland/wayland_utils.h"
27 
28 namespace wayland {
29 namespace {
30 
virtio_gpu_surface_metadata_set_scanout_id(struct wl_client *,struct wl_resource * surface_metadata_resource,uint32_t scanout_id)31 void virtio_gpu_surface_metadata_set_scanout_id(
32     struct wl_client*, struct wl_resource* surface_metadata_resource,
33     uint32_t scanout_id) {
34   GetUserData<Surface>(surface_metadata_resource)
35       ->SetVirtioGpuScanoutId(scanout_id);
36 }
37 
38 const struct wp_virtio_gpu_surface_metadata_v1_interface
39     virtio_gpu_surface_metadata_implementation = {
40         .set_scanout_id = virtio_gpu_surface_metadata_set_scanout_id};
41 
destroy_virtio_gpu_surface_metadata_resource_callback(struct wl_resource *)42 void destroy_virtio_gpu_surface_metadata_resource_callback(
43     struct wl_resource*) {
44   // This is only expected to occur upon surface destruction so no need to
45   // update the scanout id in `Surface`.
46 }
47 
virtio_gpu_metadata_get_surface_metadata(struct wl_client * client,struct wl_resource *,uint32_t id,struct wl_resource * surface_resource)48 void virtio_gpu_metadata_get_surface_metadata(
49     struct wl_client* client, struct wl_resource* /*metadata_impl_resource*/,
50     uint32_t id, struct wl_resource* surface_resource) {
51   Surface* surface = GetUserData<Surface>(surface_resource);
52 
53   wl_resource* virtio_gpu_metadata_surface_resource = wl_resource_create(
54       client, &wp_virtio_gpu_surface_metadata_v1_interface, 1, id);
55 
56   wl_resource_set_implementation(
57       virtio_gpu_metadata_surface_resource,
58       &virtio_gpu_surface_metadata_implementation, surface,
59       destroy_virtio_gpu_surface_metadata_resource_callback);
60 }
61 
62 const struct wp_virtio_gpu_metadata_v1_interface
63     virtio_gpu_metadata_implementation = {
64         .get_surface_metadata = virtio_gpu_metadata_get_surface_metadata,
65 };
66 
destroy_virtio_gpu_metadata_resource_callback(struct wl_resource *)67 void destroy_virtio_gpu_metadata_resource_callback(struct wl_resource*) {}
68 
bind_virtio_gpu_metadata(wl_client * client,void * data,uint32_t,uint32_t id)69 void bind_virtio_gpu_metadata(wl_client* client, void* data,
70                               uint32_t /*version*/, uint32_t id) {
71   wl_resource* resource =
72       wl_resource_create(client, &wp_virtio_gpu_metadata_v1_interface, 1, id);
73 
74   wl_resource_set_implementation(resource, &virtio_gpu_metadata_implementation,
75                                  data,
76                                  destroy_virtio_gpu_metadata_resource_callback);
77 }
78 
79 }  // namespace
80 
BindVirtioGpuMetadataInterface(wl_display * display,Surfaces * surfaces)81 void BindVirtioGpuMetadataInterface(wl_display* display, Surfaces* surfaces) {
82   wl_global_create(display, &wp_virtio_gpu_metadata_v1_interface, 1, surfaces,
83                    bind_virtio_gpu_metadata);
84 }
85 
86 }  // namespace wayland