1 // Copyright (C) 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <cstdint>
18 #include <memory>
19 #include <mutex>
20 #include <unordered_set>
21 
22 namespace gfxstream {
23 
24 class DisplaySurface;
25 class DisplaySurfaceUser;
26 
27 // Base class used for controlling the lifetime of a particular surface
28 // used for a display (e.g. EGLSurface or VkSurfaceKHR).
29 class DisplaySurfaceImpl {
30   public:
~DisplaySurfaceImpl()31     virtual ~DisplaySurfaceImpl() {}
32 };
33 
34 class DisplaySurface {
35   public:
36     DisplaySurface(uint32_t width,
37                    uint32_t height,
38                    std::unique_ptr<DisplaySurfaceImpl> impl);
39     ~DisplaySurface();
40 
41     DisplaySurface(const DisplaySurface&) = delete;
42     DisplaySurface& operator=(const DisplaySurface&) = delete;
43 
44     // Return the API specific implementation of a DisplaySurface. This
45     // should only be called by API specific components such as DisplayGl
46     // or DisplayVk.
getImpl()47     const DisplaySurfaceImpl* getImpl() const { return mImpl.get(); }
48 
49     uint32_t getWidth() const;
50     uint32_t getHeight() const;
51 
52     void updateSize(uint32_t newWidth, uint32_t newHeight);
53 
54   private:
55     friend class DisplaySurfaceUser;
56 
57     void registerUser(DisplaySurfaceUser* user);
58     void unregisterUser(DisplaySurfaceUser* user);
59 
60     mutable std::mutex mParamsMutex;
61     uint32_t mWidth = 0;
62     uint32_t mHeight = 0;
63 
64     std::unique_ptr<DisplaySurfaceImpl> mImpl;
65     std::unordered_set<DisplaySurfaceUser*> mBoundUsers;
66 };
67 
68 }  // namespace gfxstream
69