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 #pragma once 18 19 #include <cstdint> 20 #include <functional> 21 #include <type_traits> 22 23 #include <android-base/logging.h> 24 #include "common/libs/utils/size_utils.h" 25 #include "host/libs/config/cuttlefish_config.h" 26 27 namespace cuttlefish { 28 29 template<typename T> 30 struct is_movable { 31 static constexpr const bool value = 32 std::is_move_constructible<T>::value && 33 std::is_move_assignable<T>::value; 34 }; 35 36 // this callback type is going directly to socket-based or wayland ScreenConnector 37 using GenerateProcessedFrameCallbackImpl = std::function<void(std::uint32_t /*display_number*/, 38 std::uint8_t* /*frame_pixels*/)>; 39 40 class ScreenConnectorSource { 41 public: 42 virtual ~ScreenConnectorSource() = default; 43 // Runs the given callback on the next available frame after the given 44 // frame number and returns true if successful. 45 virtual bool OnNextFrame( 46 const GenerateProcessedFrameCallbackImpl& frame_callback) = 0; ReportClientsConnected(bool)47 virtual void ReportClientsConnected(bool /*have_clients*/) { /* ignore by default */ } 48 ScreenConnectorSource() = default; 49 }; 50 51 struct ScreenConnectorInfo { 52 // functions are intended to be inlined BytesPerPixelScreenConnectorInfo53 static constexpr std::uint32_t BytesPerPixel() { return 4; } ScreenCountScreenConnectorInfo54 static std::uint32_t ScreenCount() { 55 auto config = ChkAndGetConfig(); 56 auto display_configs = config->display_configs(); 57 return static_cast<std::uint32_t>(display_configs.size()); 58 } ScreenHeightScreenConnectorInfo59 static std::uint32_t ScreenHeight(std::uint32_t display_number) { 60 auto config = ChkAndGetConfig(); 61 auto display_configs = config->display_configs(); 62 CHECK_GT(display_configs.size(), display_number); 63 return display_configs[display_number].height; 64 } ScreenWidthScreenConnectorInfo65 static std::uint32_t ScreenWidth(std::uint32_t display_number) { 66 auto config = ChkAndGetConfig(); 67 auto display_configs = config->display_configs(); 68 CHECK_GE(display_configs.size(), display_number); 69 return display_configs[display_number].width; 70 } ScreenStrideBytesScreenConnectorInfo71 static std::uint32_t ScreenStrideBytes(std::uint32_t display_number) { 72 return AlignToPowerOf2(ScreenWidth(display_number) * BytesPerPixel(), 4); 73 } ScreenSizeInBytesScreenConnectorInfo74 static std::uint32_t ScreenSizeInBytes(std::uint32_t display_number) { 75 return ScreenStrideBytes(display_number) * ScreenHeight(display_number); 76 } 77 private: 78 static auto ChkAndGetConfig() -> decltype(cuttlefish::CuttlefishConfig::Get()) { 79 auto config = cuttlefish::CuttlefishConfig::Get(); 80 CHECK(config) << "Config is Missing"; 81 return config; 82 } 83 }; 84 85 struct ScreenConnectorFrameRenderer { 86 virtual bool RenderConfirmationUi(const std::uint32_t display, 87 std::uint8_t* raw_frame) = 0; 88 virtual bool IsCallbackSet() const = 0; 89 virtual ~ScreenConnectorFrameRenderer() = default; 90 }; 91 92 // this is inherited by the data type that represents the processed frame 93 // being moved around. 94 struct ScreenConnectorFrameInfo { 95 std::uint32_t display_number_; 96 bool is_success_; 97 }; 98 99 } // namespace cuttlefish 100