Android-cuttlefish cvd tool
screen_connector.h
Go to the documentation of this file.
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#pragma once
18
19#include <cstdint>
20#include <functional>
21#include <mutex>
22#include <string>
23#include <string_view>
24#include <type_traits>
25#include <unordered_set>
26
28#include <fruit/fruit.h>
29
41
42namespace cuttlefish {
43
44template <typename ProcessedFrameType>
46 public:
48 "ProcessedFrameType should be std::move-able.");
49 static_assert(
50 std::is_base_of<ScreenConnectorFrameInfo, ProcessedFrameType>::value,
51 "ProcessedFrameType should inherit ScreenConnectorFrameInfo");
52
54
56 HostModeCtrl& host_mode_ctrl))
57 : sc_android_src_(sc_android_src),
58 host_mode_ctrl_{host_mode_ctrl},
63 if (!config) {
64 LOG(FATAL) << "CuttlefishConfig is not available.";
65 }
66 auto instance = config->ForDefaultInstance();
67 std::unordered_set<std::string_view> valid_gpu_modes{
75 if (!Contains(valid_gpu_modes, instance.gpu_mode())) {
76 LOG(FATAL) << "Invalid gpu mode: " << instance.gpu_mode();
77 }
78 }
79
88 using GenerateProcessedFrameCallback = std::function<void(
89 std::uint32_t /*display_number*/, std::uint32_t /*frame_width*/,
90 std::uint32_t /*frame_height*/, std::uint32_t /*frame_fourcc_format*/,
91 std::uint32_t /*frame_stride_bytes*/, std::uint8_t* /*frame_bytes*/,
92 /* ScImpl enqueues this type into the Q */
93 ProcessedFrameType& msg)>;
94
95 virtual ~ScreenConnector() = default;
96
103 std::lock_guard<std::mutex> lock(streamer_callback_mutex_);
104 callback_from_streamer_ = std::move(frame_callback);
105 streamer_callback_set_cv_.notify_all();
106
108 [this](std::uint32_t display_number, std::uint32_t frame_w,
109 std::uint32_t frame_h, std::uint32_t frame_fourcc_format,
110 std::uint32_t frame_stride_bytes, std::uint8_t* frame_bytes) {
111 InjectFrame(display_number, frame_w, frame_h, frame_fourcc_format,
112 frame_stride_bytes, frame_bytes);
113 });
114 }
115
116 void InjectFrame(std::uint32_t display_number, std::uint32_t frame_w,
117 std::uint32_t frame_h, std::uint32_t frame_fourcc_format,
118 std::uint32_t frame_stride_bytes,
119 std::uint8_t* frame_bytes) {
120 const bool is_confui_mode = host_mode_ctrl_.IsConfirmatioUiMode();
121 if (is_confui_mode) {
122 return;
123 }
124
125 ProcessedFrameType processed_frame;
126
127 {
128 std::lock_guard<std::mutex> lock(streamer_callback_mutex_);
129 callback_from_streamer_(display_number, frame_w, frame_h,
130 frame_fourcc_format, frame_stride_bytes,
131 frame_bytes, processed_frame);
132 }
133
134 sc_frame_multiplexer_.PushToAndroidQueue(std::move(processed_frame));
135 }
136
137 bool IsCallbackSet() const override {
139 return true;
140 }
141 return false;
142 }
143
145 sc_android_src_.SetDisplayEventCallback(std::move(event_callback));
146 }
147
148 /* returns the processed frame that also includes meta-info such as
149 * success/fail and display number from the guest
150 *
151 * NOTE THAT THIS IS THE ONLY CONSUMER OF THE TWO QUEUES
152 */
153 ProcessedFrameType OnNextFrame() { return sc_frame_multiplexer_.Pop(); }
154
162 bool RenderConfirmationUi(std::uint32_t display_number,
163 std::uint32_t frame_width,
164 std::uint32_t frame_height,
165 std::uint32_t frame_fourcc_format,
166 std::uint32_t frame_stride_bytes,
167 std::uint8_t* frame_bytes) override {
169 // wait callback is not set, the streamer is not ready
170 // return with LOG(ERROR)
171 if (!IsCallbackSet()) {
172 ConfUiLog(ERROR) << "callback function to process frames is not yet set";
173 return false;
174 }
175 ProcessedFrameType processed_frame;
176 auto this_thread_name = cuttlefish::confui::thread::GetName();
177 ConfUiLog(DEBUG) << this_thread_name
178 << "is sending a #" + std::to_string(render_confui_cnt_)
179 << "Conf UI frame";
180 callback_from_streamer_(display_number, frame_width, frame_height,
181 frame_fourcc_format, frame_stride_bytes,
182 frame_bytes, processed_frame);
183 // now add processed_frame to the queue
184 sc_frame_multiplexer_.PushToConfUiQueue(std::move(processed_frame));
185 return true;
186 }
187
188 protected:
189 ScreenConnector() = delete;
190
191 private:
194 unsigned long long int on_next_frame_cnt_;
195 unsigned long long int render_confui_cnt_;
204 std::mutex
205 streamer_callback_mutex_; // mutex to set & read callback_from_streamer_
206 std::condition_variable streamer_callback_set_cv_;
207};
208
209} // namespace cuttlefish
std::string to_string(ConnectionState state)
Definition: adb.cpp:144
static const CuttlefishConfig * Get()
Definition: cuttlefish_config.cpp:552
Definition: host_mode_ctrl.h:38
auto IsConfirmatioUiMode()
Definition: host_mode_ctrl.h:102
Definition: screen_connector_multiplexer.h:29
ProcessedFrameType Pop()
Definition: screen_connector_multiplexer.h:53
void PushToAndroidQueue(ProcessedFrameType &&t)
Definition: screen_connector_multiplexer.h:44
void PushToConfUiQueue(ProcessedFrameType &&t)
Definition: screen_connector_multiplexer.h:48
Definition: screen_connector.h:45
ProcessedFrameType OnNextFrame()
Definition: screen_connector.h:153
void SetCallback(GenerateProcessedFrameCallback &&frame_callback)
Definition: screen_connector.h:102
WaylandScreenConnector & sc_android_src_
Definition: screen_connector.h:192
bool RenderConfirmationUi(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) override
Definition: screen_connector.h:162
std::function< void(std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, std::uint8_t *, ProcessedFrameType &msg)> GenerateProcessedFrameCallback
Definition: screen_connector.h:93
unsigned long long int on_next_frame_cnt_
Definition: screen_connector.h:194
std::mutex streamer_callback_mutex_
Definition: screen_connector.h:205
HostModeCtrl & host_mode_ctrl_
Definition: screen_connector.h:193
unsigned long long int render_confui_cnt_
Definition: screen_connector.h:195
void InjectFrame(std::uint32_t display_number, std::uint32_t frame_w, std::uint32_t frame_h, std::uint32_t frame_fourcc_format, std::uint32_t frame_stride_bytes, std::uint8_t *frame_bytes)
Definition: screen_connector.h:116
std::condition_variable streamer_callback_set_cv_
Definition: screen_connector.h:206
GenerateProcessedFrameCallback callback_from_streamer_
Definition: screen_connector.h:203
bool IsCallbackSet() const override
Definition: screen_connector.h:137
INJECT(ScreenConnector(WaylandScreenConnector &sc_android_src, HostModeCtrl &host_mode_ctrl))
Definition: screen_connector.h:55
void SetDisplayEventCallback(DisplayEventCallback event_callback)
Definition: screen_connector.h:144
FrameMultiplexer sc_frame_multiplexer_
Definition: screen_connector.h:202
virtual ~ScreenConnector()=default
Definition: wayland_screen_connector.h:28
void SetDisplayEventCallback(DisplayEventCallback event_callback)
Definition: wayland_screen_connector.cpp:45
void SetFrameCallback(GenerateProcessedFrameCallbackImpl frame_callback)
Definition: wayland_screen_connector.cpp:40
#define ConfUiLog(LOG_LEVEL)
Definition: utils.h:61
#define FATAL(x...)
Definition: image.h:31
#define LOG(severity)
Definition: logging.h:223
@ DEBUG
Definition: logging.h:89
@ ERROR
Definition: logging.h:92
std::string GetName(const std::thread::id tid)
Definition: host_utils.cc:63
Definition: alloc_utils.cpp:23
constexpr char kGpuModeCustom[]
Definition: config_constants.h:70
constexpr bool Contains(Container &&container, U &&u)
Definition: contains.h:98
constexpr char kGpuModeGuestSwiftshader[]
Definition: config_constants.h:78
constexpr char kGpuModeGfxstream[]
Definition: config_constants.h:72
constexpr char kGpuModeGfxstreamGuestAngleHostSwiftShader[]
Definition: config_constants.h:74
constexpr char kGpuModeGfxstreamGuestAngleHostLavapipe[]
Definition: config_constants.h:76
constexpr char kGpuModeGfxstreamGuestAngle[]
Definition: config_constants.h:73
constexpr char kGpuModeDrmVirgl[]
Definition: config_constants.h:71
Definition: screen_connector_common.h:51
Definition: screen_connector_common.h:27
std::function< void(const DisplayEvent &)> DisplayEventCallback
Definition: wayland_server_callbacks.h:34