1 /*
2 * Copyright (C) 2019 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_surface.h"
18
19 #include <android-base/logging.h>
20 #include <wayland-server-protocol.h>
21
22 #include "host/libs/wayland/wayland_surfaces.h"
23
24 namespace wayland {
25
Surface(Surfaces & surfaces)26 Surface::Surface(Surfaces& surfaces) : surfaces_(surfaces) {}
27
~Surface()28 Surface::~Surface() {
29 if (state_.virtio_gpu_metadata_.scanout_id.has_value()) {
30 const uint32_t display_number = *state_.virtio_gpu_metadata_.scanout_id;
31 surfaces_.HandleSurfaceDestroyed(display_number);
32 }
33 }
34
SetRegion(const Region & region)35 void Surface::SetRegion(const Region& region) {
36 std::unique_lock<std::mutex> lock(state_mutex_);
37 state_.region = region;
38 }
39
Attach(struct wl_resource * buffer)40 void Surface::Attach(struct wl_resource* buffer) {
41 std::unique_lock<std::mutex> lock(state_mutex_);
42 state_.pending_buffer = buffer;
43 }
44
Commit()45 void Surface::Commit() {
46 std::unique_lock<std::mutex> lock(state_mutex_);
47 state_.current_buffer = state_.pending_buffer;
48 state_.pending_buffer = nullptr;
49
50 if (state_.current_buffer == nullptr) {
51 return;
52 }
53
54 if (state_.virtio_gpu_metadata_.scanout_id.has_value()) {
55 const uint32_t display_number = *state_.virtio_gpu_metadata_.scanout_id;
56
57 struct wl_shm_buffer* shm_buffer = wl_shm_buffer_get(state_.current_buffer);
58 CHECK(shm_buffer != nullptr);
59
60 wl_shm_buffer_begin_access(shm_buffer);
61
62 const int32_t buffer_w = wl_shm_buffer_get_width(shm_buffer);
63 CHECK(buffer_w == state_.region.w);
64 const int32_t buffer_h = wl_shm_buffer_get_height(shm_buffer);
65 CHECK(buffer_h == state_.region.h);
66 const int32_t buffer_stride_bytes = wl_shm_buffer_get_stride(shm_buffer);
67
68 if (!state_.has_notified_surface_create) {
69 surfaces_.HandleSurfaceCreated(display_number, buffer_w, buffer_h);
70 state_.has_notified_surface_create = true;
71 }
72
73 uint8_t* buffer_pixels =
74 reinterpret_cast<uint8_t*>(wl_shm_buffer_get_data(shm_buffer));
75
76 surfaces_.HandleSurfaceFrame(display_number, buffer_w, buffer_h,
77 buffer_stride_bytes, buffer_pixels);
78
79 wl_shm_buffer_end_access(shm_buffer);
80 }
81
82 wl_buffer_send_release(state_.current_buffer);
83 wl_client_flush(wl_resource_get_client(state_.current_buffer));
84
85 state_.current_buffer = nullptr;
86 state_.current_frame_number++;
87 }
88
SetVirtioGpuScanoutId(uint32_t scanout_id)89 void Surface::SetVirtioGpuScanoutId(uint32_t scanout_id) {
90 std::unique_lock<std::mutex> lock(state_mutex_);
91 state_.virtio_gpu_metadata_.scanout_id = scanout_id;
92 }
93
94 } // namespace wayland