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)26Surface::Surface(Surfaces& surfaces) : surfaces_(surfaces) {} 27 SetRegion(const Region & region)28void Surface::SetRegion(const Region& region) { 29 std::unique_lock<std::mutex> lock(state_mutex_); 30 state_.region = region; 31 } 32 Attach(struct wl_resource * buffer)33void Surface::Attach(struct wl_resource* buffer) { 34 std::unique_lock<std::mutex> lock(state_mutex_); 35 state_.pending_buffer = buffer; 36 } 37 Commit()38void Surface::Commit() { 39 std::unique_lock<std::mutex> lock(state_mutex_); 40 state_.current_buffer = state_.pending_buffer; 41 state_.pending_buffer = nullptr; 42 43 if (state_.current_buffer == nullptr) { 44 return; 45 } 46 47 if (state_.virtio_gpu_metadata_.scanout_id.has_value()) { 48 const uint32_t display_number = *state_.virtio_gpu_metadata_.scanout_id; 49 50 struct wl_shm_buffer* shm_buffer = wl_shm_buffer_get(state_.current_buffer); 51 CHECK(shm_buffer != nullptr); 52 53 wl_shm_buffer_begin_access(shm_buffer); 54 55 const int32_t buffer_w = wl_shm_buffer_get_width(shm_buffer); 56 CHECK(buffer_w == state_.region.w); 57 const int32_t buffer_h = wl_shm_buffer_get_height(shm_buffer); 58 CHECK(buffer_h == state_.region.h); 59 const int32_t buffer_stride_bytes = wl_shm_buffer_get_stride(shm_buffer); 60 61 uint8_t* buffer_pixels = 62 reinterpret_cast<uint8_t*>(wl_shm_buffer_get_data(shm_buffer)); 63 64 surfaces_.HandleSurfaceFrame(display_number, buffer_w, buffer_h, 65 buffer_stride_bytes, buffer_pixels); 66 67 wl_shm_buffer_end_access(shm_buffer); 68 } 69 70 wl_buffer_send_release(state_.current_buffer); 71 wl_client_flush(wl_resource_get_client(state_.current_buffer)); 72 73 state_.current_buffer = nullptr; 74 state_.current_frame_number++; 75 } 76 SetVirtioGpuScanoutId(uint32_t scanout_id)77void Surface::SetVirtioGpuScanoutId(uint32_t scanout_id) { 78 std::unique_lock<std::mutex> lock(state_mutex_); 79 state_.virtio_gpu_metadata_.scanout_id = scanout_id; 80 } 81 82 } // namespace wayland