1 /*
2  * Copyright (C) 2020 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_surfaces.h"
18 
19 #include <android-base/logging.h>
20 #include <drm/drm_fourcc.h>
21 #include <wayland-server-protocol.h>
22 
23 #include "host/libs/wayland/wayland_surface.h"
24 
25 namespace wayland {
26 
SetFrameCallback(FrameCallback callback)27 void Surfaces::SetFrameCallback(FrameCallback callback) {
28   std::unique_lock<std::mutex> lock(callback_mutex_);
29   callback_.emplace(std::move(callback));
30 }
31 
SetDisplayEventCallback(DisplayEventCallback callback)32 void Surfaces::SetDisplayEventCallback(DisplayEventCallback callback) {
33   std::unique_lock<std::mutex> lock(callback_mutex_);
34   event_callback_.emplace(std::move(callback));
35 }
36 
SetFramesAreRGBA(bool frames_are_rgba)37 void Surfaces::SetFramesAreRGBA(bool frames_are_rgba) {
38   frames_are_rgba_ = frames_are_rgba;
39 }
40 
HandleSurfaceFrame(std::uint32_t display_number,std::uint32_t frame_width,std::uint32_t frame_height,std::uint32_t frame_fourcc_format,std::uint32_t frame_stride_bytes,std::uint8_t * frame_bytes)41 void Surfaces::HandleSurfaceFrame(std::uint32_t display_number,
42                                   std::uint32_t frame_width,
43                                   std::uint32_t frame_height,
44                                   std::uint32_t frame_fourcc_format,
45                                   std::uint32_t frame_stride_bytes,
46                                   std::uint8_t* frame_bytes) {
47   if (frames_are_rgba_) {
48     frame_fourcc_format = DRM_FORMAT_ABGR8888;
49   }
50 
51   std::unique_lock<std::mutex> lock(callback_mutex_);
52 
53   if (callback_) {
54     (callback_.value())(display_number, frame_width, frame_height,
55                         frame_fourcc_format, frame_stride_bytes, frame_bytes);
56   }
57 }
58 
HandleSurfaceCreated(std::uint32_t display_number,std::uint32_t display_width,std::uint32_t display_height)59 void Surfaces::HandleSurfaceCreated(std::uint32_t display_number,
60                                     std::uint32_t display_width,
61                                     std::uint32_t display_height) {
62   const DisplayEvent event{DisplayCreatedEvent{
63       .display_number = display_number,
64       .display_width = display_width,
65       .display_height = display_height,
66   }};
67 
68   std::unique_lock<std::mutex> lock(callback_mutex_);
69   if (event_callback_) {
70     (event_callback_.value())(event);
71   }
72 }
73 
HandleSurfaceDestroyed(std::uint32_t display_number)74 void Surfaces::HandleSurfaceDestroyed(std::uint32_t display_number) {
75   const DisplayEvent event{DisplayDestroyedEvent{
76       .display_number = display_number,
77   }};
78 
79   std::unique_lock<std::mutex> lock(callback_mutex_);
80   if (event_callback_) {
81     (event_callback_.value())(event);
82   }
83 }
84 
85 }  // namespace wayland