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 <stdint.h>
20 #include <future>
21 #include <memory>
22 #include <mutex>
23 #include <optional>
24 #include <thread>
25 #include <unordered_map>
26 
27 #include "host/libs/wayland/wayland_server_callbacks.h"
28 
29 namespace wayland {
30 
31 class Surface;
32 
33 class Surfaces {
34  public:
35   Surfaces() = default;
36   virtual ~Surfaces() = default;
37 
38   Surfaces(const Surfaces& rhs) = delete;
39   Surfaces& operator=(const Surfaces& rhs) = delete;
40 
41   Surfaces(Surfaces&& rhs) = delete;
42   Surfaces& operator=(Surfaces&& rhs) = delete;
43 
44   using FrameCallback =
45       std::function<void(std::uint32_t /*display_number*/,       //
46                          std::uint32_t /*frame_width*/,          //
47                          std::uint32_t /*frame_height*/,         //
48                          std::uint32_t /*frame_fourcc_format*/,  //
49                          std::uint32_t /*frame_stride_bytes*/,   //
50                          std::uint8_t* /*frame_bytes*/)>;
51 
52   void SetFrameCallback(FrameCallback callback);
53 
54   void SetDisplayEventCallback(DisplayEventCallback callback);
55 
56   void SetFramesAreRGBA(bool frames_are_rgba);
57 
58  private:
59   friend class Surface;
60   void HandleSurfaceFrame(std::uint32_t display_number,       //
61                           std::uint32_t frame_width,          //
62                           std::uint32_t frame_height,         //
63                           std::uint32_t frame_fourcc_format,  //
64                           std::uint32_t frame_stride_bytes,   //
65                           std::uint8_t* frame_bytes);
66 
67   void HandleSurfaceCreated(std::uint32_t display_number,
68                             std::uint32_t display_width,
69                             std::uint32_t display_height);
70 
71   void HandleSurfaceDestroyed(std::uint32_t display_number);
72 
73   std::mutex callback_mutex_;
74   std::optional<FrameCallback> callback_;
75   std::optional<DisplayEventCallback> event_callback_;
76   // If true, report that the received frames are RGBA regardless
77   // of the format reported by the wayland client.
78   bool frames_are_rgba_ = false;
79 };
80 
81 }  // namespace wayland
82