1 /*
2  * Copyright (C) 2007 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 #ifndef ANDROID_GUI_SURFACE_CONTROL_H
18 #define ANDROID_GUI_SURFACE_CONTROL_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <optional>
23 
24 #include <utils/RefBase.h>
25 #include <utils/threads.h>
26 
27 #include <android/gui/ISurfaceComposerClient.h>
28 
29 #include <ui/FrameStats.h>
30 #include <ui/PixelFormat.h>
31 #include <ui/Region.h>
32 
33 #include <math/vec3.h>
34 
35 namespace android {
36 
37 // ---------------------------------------------------------------------------
38 
39 class Choreographer;
40 class IGraphicBufferProducer;
41 class Surface;
42 class SurfaceComposerClient;
43 class BLASTBufferQueue;
44 
45 // ---------------------------------------------------------------------------
46 
47 class SurfaceControl : public RefBase
48 {
49 public:
50     static status_t readFromParcel(const Parcel& parcel, sp<SurfaceControl>* outSurfaceControl);
51     status_t writeToParcel(Parcel& parcel);
52 
53     static status_t readNullableFromParcel(const Parcel& parcel,
54                                            sp<SurfaceControl>* outSurfaceControl);
55     static status_t writeNullableToParcel(Parcel& parcel, const sp<SurfaceControl>& surfaceControl);
56 
isValid(const sp<SurfaceControl> & surface)57     static bool isValid(const sp<SurfaceControl>& surface) {
58         return (surface != nullptr) && surface->isValid();
59     }
60 
isValid()61     bool isValid() {
62         return mHandle!=nullptr && mClient!=nullptr;
63     }
64 
65     static bool isSameSurface(
66             const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
67 
68     // Reparent off-screen and release. This is invoked by the destructor.
69     void destroy();
70 
71     // disconnect any api that's connected
72     void        disconnect();
73 
74     static status_t writeSurfaceToParcel(
75             const sp<SurfaceControl>& control, Parcel* parcel);
76 
77     sp<Surface> getSurface();
78     sp<Surface> createSurface();
79     sp<IBinder> getHandle() const;
80     sp<IBinder> getLayerStateHandle() const;
81     int32_t getLayerId() const;
82     const std::string& getName() const;
83 
84     // TODO(b/267195698): Consider renaming.
85     std::shared_ptr<Choreographer> getChoreographer();
86 
87     sp<IGraphicBufferProducer> getIGraphicBufferProducer();
88 
89     status_t clearLayerFrameStats() const;
90     status_t getLayerFrameStats(FrameStats* outStats) const;
91 
92     sp<SurfaceComposerClient> getClient() const;
93 
94     uint32_t getTransformHint() const;
95 
96     void setTransformHint(uint32_t hint);
97     void updateDefaultBufferSize(uint32_t width, uint32_t height);
98 
99     explicit SurfaceControl(const sp<SurfaceControl>& other);
100 
101     SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
102                    int32_t layerId, const std::string& layerName, uint32_t width = 0,
103                    uint32_t height = 0, PixelFormat format = 0, uint32_t transformHint = 0,
104                    uint32_t flags = 0);
105 
106     sp<SurfaceControl> getParentingLayer();
107 
108     uint64_t resolveFrameNumber(const std::optional<uint64_t>& frameNumber);
109 
110 private:
111     // can't be copied
112     SurfaceControl& operator = (SurfaceControl& rhs);
113     SurfaceControl(const SurfaceControl& rhs);
114 
115     friend class SurfaceComposerClient;
116     friend class Surface;
117 
118     ~SurfaceControl();
119 
120     sp<Surface> generateSurfaceLocked();
121     status_t validate() const;
122 
123     sp<SurfaceComposerClient>   mClient;
124     sp<IBinder> mHandle;
125     mutable Mutex               mLock;
126     mutable sp<Surface>         mSurfaceData;
127     mutable sp<BLASTBufferQueue> mBbq;
128     mutable sp<SurfaceControl> mBbqChild;
129     int32_t mLayerId = 0;
130     std::string mName;
131     uint32_t mTransformHint = 0;
132     uint32_t mWidth = 0;
133     uint32_t mHeight = 0;
134     PixelFormat mFormat = PIXEL_FORMAT_NONE;
135     uint32_t mCreateFlags = 0;
136     uint64_t mFallbackFrameNumber = 100;
137     std::shared_ptr<Choreographer> mChoreographer;
138 };
139 
140 }; // namespace android
141 
142 #endif // ANDROID_GUI_SURFACE_CONTROL_H
143