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