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 22 #include <android-base/logging.h> 23 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 = 38 std::function<void(std::uint32_t /*display_number*/, // 39 std::uint32_t /*frame_width*/, // 40 std::uint32_t /*frame_height*/, // 41 std::uint32_t /*frame_stride_bytes*/, // 42 std::uint8_t* /*frame_pixels*/)>; 43 44 struct ScreenConnectorInfo { 45 // functions are intended to be inlined BytesPerPixelScreenConnectorInfo46 static constexpr std::uint32_t BytesPerPixel() { return 4; } ScreenCountScreenConnectorInfo47 static std::uint32_t ScreenCount() { 48 auto config = ChkAndGetConfig(); 49 auto instance = config->ForDefaultInstance(); 50 auto display_configs = instance.display_configs(); 51 return static_cast<std::uint32_t>(display_configs.size()); 52 } ScreenHeightScreenConnectorInfo53 static std::uint32_t ScreenHeight(std::uint32_t display_number) { 54 auto config = ChkAndGetConfig(); 55 auto instance = config->ForDefaultInstance(); 56 auto display_configs = instance.display_configs(); 57 CHECK_GT(display_configs.size(), display_number); 58 return display_configs[display_number].height; 59 } ScreenWidthScreenConnectorInfo60 static std::uint32_t ScreenWidth(std::uint32_t display_number) { 61 auto config = ChkAndGetConfig(); 62 auto instance = config->ForDefaultInstance(); 63 auto display_configs = instance.display_configs(); 64 CHECK_GE(display_configs.size(), display_number); 65 return display_configs[display_number].width; 66 } ComputeScreenStrideBytesScreenConnectorInfo67 static std::uint32_t ComputeScreenStrideBytes(const std::uint32_t w) { 68 return AlignToPowerOf2(w * BytesPerPixel(), 4); 69 } ComputeScreenSizeInBytesScreenConnectorInfo70 static std::uint32_t ComputeScreenSizeInBytes(const std::uint32_t w, 71 const std::uint32_t h) { 72 return ComputeScreenStrideBytes(w) * h; 73 } ScreenStrideBytesScreenConnectorInfo74 static std::uint32_t ScreenStrideBytes(const std::uint32_t display_number) { 75 return ComputeScreenStrideBytes(ScreenWidth(display_number)); 76 } ScreenSizeInBytesScreenConnectorInfo77 static std::uint32_t ScreenSizeInBytes(const std::uint32_t display_number) { 78 return ComputeScreenStrideBytes(ScreenWidth(display_number)) * 79 ScreenHeight(display_number); 80 } 81 82 private: 83 static auto ChkAndGetConfig() -> decltype(cuttlefish::CuttlefishConfig::Get()) { 84 auto config = cuttlefish::CuttlefishConfig::Get(); 85 CHECK(config) << "Config is Missing"; 86 return config; 87 } 88 }; 89 90 struct ScreenConnectorFrameRenderer { 91 virtual bool RenderConfirmationUi(std::uint32_t display_number, 92 std::uint32_t frame_width, 93 std::uint32_t frame_height, 94 std::uint32_t frame_stride_bytes, 95 std::uint8_t* frame_bytes) = 0; 96 virtual bool IsCallbackSet() const = 0; 97 virtual ~ScreenConnectorFrameRenderer() = default; 98 }; 99 100 // this is inherited by the data type that represents the processed frame 101 // being moved around. 102 struct ScreenConnectorFrameInfo { 103 std::uint32_t display_number_; 104 bool is_success_; 105 }; 106 107 } // namespace cuttlefish 108