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 <stdint.h>
20
21#include <functional>
22#include <mutex>
23#include <string>
24#include <string_view>
25#include <type_traits>
26#include <unordered_set>
27
28#include "absl/log/log.h"
29#include "fruit/fruit.h"
30
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},
62 auto config = CuttlefishConfig::Get();
63 if (!config) {
64 LOG(FATAL) << "CuttlefishConfig is not available.";
65 }
66 auto instance = config->ForDefaultInstance();
67 std::unordered_set<GpuMode> valid_gpu_modes{
76 };
77 if (!Contains(valid_gpu_modes, instance.gpu_mode())) {
78 LOG(FATAL) << "Invalid gpu mode: " << GpuModeString(instance.gpu_mode());
79 }
80 }
81
90 using GenerateProcessedFrameCallback = std::function<void(
91 uint32_t /*display_number*/, uint32_t /*frame_width*/,
92 uint32_t /*frame_height*/, uint32_t /*frame_fourcc_format*/,
93 uint32_t /*frame_stride_bytes*/, uint8_t* /*frame_bytes*/,
94 /* ScImpl enqueues this type into the Q */
95 ProcessedFrameType& msg)>;
96
97 virtual ~ScreenConnector() = default;
98
105 std::lock_guard<std::mutex> lock(streamer_callback_mutex_);
106 callback_from_streamer_ = std::move(frame_callback);
107 streamer_callback_set_cv_.notify_all();
108
110 [this](uint32_t display_number, uint32_t frame_w, uint32_t frame_h,
111 uint32_t frame_fourcc_format, uint32_t frame_stride_bytes,
112 uint8_t* frame_bytes) {
113 InjectFrame(display_number, frame_w, frame_h, frame_fourcc_format,
114 frame_stride_bytes, frame_bytes);
115 });
116 }
117
118 void InjectFrame(uint32_t display_number, uint32_t frame_w, uint32_t frame_h,
119 uint32_t frame_fourcc_format, uint32_t frame_stride_bytes,
120 uint8_t* frame_bytes) {
121 const bool is_confui_mode = host_mode_ctrl_.IsConfirmatioUiMode();
122 if (is_confui_mode) {
123 return;
124 }
125
126 ProcessedFrameType processed_frame;
127
128 {
129 std::lock_guard<std::mutex> lock(streamer_callback_mutex_);
130 callback_from_streamer_(display_number, frame_w, frame_h,
131 frame_fourcc_format, frame_stride_bytes,
132 frame_bytes, processed_frame);
133 }
134
135 sc_frame_multiplexer_.PushToAndroidQueue(std::move(processed_frame));
136 }
137
138 bool IsCallbackSet() const override {
140 return true;
141 }
142 return false;
143 }
144
146 sc_android_src_.SetDisplayEventCallback(std::move(event_callback));
147 }
148
149 /* returns the processed frame that also includes meta-info such as
150 * success/fail and display number from the guest
151 *
152 * NOTE THAT THIS IS THE ONLY CONSUMER OF THE TWO QUEUES
153 */
154 ProcessedFrameType OnNextFrame() { return sc_frame_multiplexer_.Pop(); }
155
163 bool RenderConfirmationUi(uint32_t display_number, uint32_t frame_width,
164 uint32_t frame_height, uint32_t frame_fourcc_format,
165 uint32_t frame_stride_bytes,
166 uint8_t* frame_bytes) override {
168 // wait callback is not set, the streamer is not ready
169 // return with LOG(ERROR)
170 if (!IsCallbackSet()) {
171 ConfUiLog(ERROR) << "callback function to process frames is not yet set";
172 return false;
173 }
174 ProcessedFrameType processed_frame;
175 auto this_thread_name = confui::thread::GetName();
176 ConfUiLogDebug << this_thread_name
177 << "is sending a #" + std::to_string(render_confui_cnt_)
178 << "Conf UI frame";
179 callback_from_streamer_(display_number, frame_width, frame_height,
180 frame_fourcc_format, frame_stride_bytes,
181 frame_bytes, processed_frame);
182 // now add processed_frame to the queue
183 sc_frame_multiplexer_.PushToConfUiQueue(std::move(processed_frame));
184 return true;
185 }
186
187 protected:
188 ScreenConnector() = delete;
189
190 private:
193 unsigned long long int on_next_frame_cnt_;
194 unsigned long long int render_confui_cnt_;
203 std::mutex
204 streamer_callback_mutex_; // mutex to set & read callback_from_streamer_
205 std::condition_variable streamer_callback_set_cv_;
206};
207
208} // namespace cuttlefish
std::string to_string(ConnectionState state)
Definition: adb.cpp:144
static const CuttlefishConfig * Get()
Definition: cuttlefish_config.cpp:461
Definition: host_mode_ctrl.h:39
auto IsConfirmatioUiMode()
Definition: host_mode_ctrl.h:103
Definition: screen_connector_multiplexer.h:27
ProcessedFrameType Pop()
Definition: screen_connector_multiplexer.h:51
void PushToAndroidQueue(ProcessedFrameType &&t)
Definition: screen_connector_multiplexer.h:42
void PushToConfUiQueue(ProcessedFrameType &&t)
Definition: screen_connector_multiplexer.h:46
Definition: screen_connector.h:45
ProcessedFrameType OnNextFrame()
Definition: screen_connector.h:154
void SetCallback(GenerateProcessedFrameCallback &&frame_callback)
Definition: screen_connector.h:104
WaylandScreenConnector & sc_android_src_
Definition: screen_connector.h:191
unsigned long long int on_next_frame_cnt_
Definition: screen_connector.h:193
std::mutex streamer_callback_mutex_
Definition: screen_connector.h:204
void InjectFrame(uint32_t display_number, uint32_t frame_w, uint32_t frame_h, uint32_t frame_fourcc_format, uint32_t frame_stride_bytes, uint8_t *frame_bytes)
Definition: screen_connector.h:118
HostModeCtrl & host_mode_ctrl_
Definition: screen_connector.h:192
unsigned long long int render_confui_cnt_
Definition: screen_connector.h:194
std::function< void(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint8_t *, ProcessedFrameType &msg)> GenerateProcessedFrameCallback
Definition: screen_connector.h:95
bool RenderConfirmationUi(uint32_t display_number, uint32_t frame_width, uint32_t frame_height, uint32_t frame_fourcc_format, uint32_t frame_stride_bytes, uint8_t *frame_bytes) override
Definition: screen_connector.h:163
std::condition_variable streamer_callback_set_cv_
Definition: screen_connector.h:205
GenerateProcessedFrameCallback callback_from_streamer_
Definition: screen_connector.h:202
bool IsCallbackSet() const override
Definition: screen_connector.h:138
INJECT(ScreenConnector(WaylandScreenConnector &sc_android_src, HostModeCtrl &host_mode_ctrl))
Definition: screen_connector.h:55
void SetDisplayEventCallback(DisplayEventCallback event_callback)
Definition: screen_connector.h:145
FrameMultiplexer sc_frame_multiplexer_
Definition: screen_connector.h:201
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 ConfUiLogDebug
Definition: utils.h:61
#define ConfUiLog(LOG_LEVEL)
Definition: utils.h:60
#define FATAL(x...)
Definition: image.h:31
#define LOG(severity)
Definition: logging.h:223
@ ERROR
Definition: logging.h:92
std::string GetName(const std::thread::id tid)
Definition: host_utils.cc:63
Definition: alloc_driver.h:20
std::string GpuModeString(GpuMode mode)
Definition: gpu_mode.cc:54
constexpr bool Contains(Container &&container, U &&u)
Definition: contains.h:98
Definition: screen_connector_common.h:51
Definition: screen_connector_common.h:27
std::function< void(const DisplayEvent &)> DisplayEventCallback
Definition: wayland_server_callbacks.h:35