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 && std::is_move_assignable<T>::value;
33 };
34 
35 // this callback type is going directly to socket-based or wayland
36 // 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_fourcc_format*/,  //
42                        std::uint32_t /*frame_stride_bytes*/,   //
43                        std::uint8_t* /*frame_pixels*/)>;
44 
45 struct ScreenConnectorInfo {
46   // functions are intended to be inlined
BytesPerPixelScreenConnectorInfo47   static constexpr std::uint32_t BytesPerPixel() { return 4; }
ScreenCountScreenConnectorInfo48   static std::uint32_t ScreenCount() {
49     auto config = ChkAndGetConfig();
50     auto instance = config->ForDefaultInstance();
51     auto display_configs = instance.display_configs();
52     return static_cast<std::uint32_t>(display_configs.size());
53   }
ScreenHeightScreenConnectorInfo54   static std::uint32_t ScreenHeight(std::uint32_t display_number) {
55     auto config = ChkAndGetConfig();
56     auto instance = config->ForDefaultInstance();
57     auto display_configs = instance.display_configs();
58     CHECK_GT(display_configs.size(), display_number);
59     return display_configs[display_number].height;
60   }
ScreenWidthScreenConnectorInfo61   static std::uint32_t ScreenWidth(std::uint32_t display_number) {
62     auto config = ChkAndGetConfig();
63     auto instance = config->ForDefaultInstance();
64     auto display_configs = instance.display_configs();
65     CHECK_GE(display_configs.size(), display_number);
66     return display_configs[display_number].width;
67   }
ComputeScreenStrideBytesScreenConnectorInfo68   static std::uint32_t ComputeScreenStrideBytes(const std::uint32_t w) {
69     return AlignToPowerOf2(w * BytesPerPixel(), 4);
70   }
ComputeScreenSizeInBytesScreenConnectorInfo71   static std::uint32_t ComputeScreenSizeInBytes(const std::uint32_t w,
72                                                 const std::uint32_t h) {
73     return ComputeScreenStrideBytes(w) * h;
74   }
ScreenStrideBytesScreenConnectorInfo75   static std::uint32_t ScreenStrideBytes(const std::uint32_t display_number) {
76     return ComputeScreenStrideBytes(ScreenWidth(display_number));
77   }
ScreenSizeInBytesScreenConnectorInfo78   static std::uint32_t ScreenSizeInBytes(const std::uint32_t display_number) {
79     return ComputeScreenStrideBytes(ScreenWidth(display_number)) *
80            ScreenHeight(display_number);
81   }
82 
83  private:
84   static auto ChkAndGetConfig()
85       -> decltype(cuttlefish::CuttlefishConfig::Get()) {
86     auto config = cuttlefish::CuttlefishConfig::Get();
87     CHECK(config) << "Config is Missing";
88     return config;
89   }
90 };
91 
92 struct ScreenConnectorFrameRenderer {
93   virtual bool RenderConfirmationUi(std::uint32_t display_number,
94                                     std::uint32_t frame_width,
95                                     std::uint32_t frame_height,
96                                     std::uint32_t frame_fourcc_format,
97                                     std::uint32_t frame_stride_bytes,
98                                     std::uint8_t* frame_bytes) = 0;
99   virtual bool IsCallbackSet() const = 0;
100   virtual ~ScreenConnectorFrameRenderer() = default;
101 };
102 
103 // this is inherited by the data type that represents the processed frame
104 // being moved around.
105 struct ScreenConnectorFrameInfo {
106   std::uint32_t display_number_;
107   bool is_success_;
108 };
109 
110 }  // namespace cuttlefish
111