• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "SurfaceControl"
18 
19 #include <stdint.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #include <android/native_window.h>
25 
26 #include <utils/Errors.h>
27 #include <utils/KeyedVector.h>
28 #include <utils/Log.h>
29 #include <utils/threads.h>
30 
31 #include <binder/IPCThreadState.h>
32 
33 #include <ui/GraphicBuffer.h>
34 #include <ui/Rect.h>
35 #include <ui/StaticDisplayInfo.h>
36 
37 #include <gui/BufferQueueCore.h>
38 #include <gui/BLASTBufferQueue.h>
39 #include <gui/ISurfaceComposer.h>
40 #include <gui/Surface.h>
41 #include <gui/SurfaceComposerClient.h>
42 #include <gui/SurfaceControl.h>
43 #include <private/gui/ParcelUtils.h>
44 
45 namespace android {
46 
47 // ============================================================================
48 //  SurfaceControl
49 // ============================================================================
50 
SurfaceControl(const sp<SurfaceComposerClient> & client,const sp<IBinder> & handle,const sp<IGraphicBufferProducer> & gbp,int32_t layerId,uint32_t w,uint32_t h,PixelFormat format,uint32_t transform,uint32_t flags)51 SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
52                                const sp<IGraphicBufferProducer>& gbp, int32_t layerId,
53                                uint32_t w, uint32_t h, PixelFormat format, uint32_t transform,
54                                uint32_t flags)
55       : mClient(client),
56         mHandle(handle),
57         mGraphicBufferProducer(gbp),
58         mLayerId(layerId),
59         mTransformHint(transform),
60         mWidth(w),
61         mHeight(h),
62         mFormat(format),
63         mCreateFlags(flags) {}
64 
SurfaceControl(const sp<SurfaceControl> & other)65 SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
66     mClient = other->mClient;
67     mHandle = other->mHandle;
68     mGraphicBufferProducer = other->mGraphicBufferProducer;
69     mTransformHint = other->mTransformHint;
70     mLayerId = other->mLayerId;
71     mWidth = other->mWidth;
72     mHeight = other->mHeight;
73     mFormat = other->mFormat;
74     mCreateFlags = other->mCreateFlags;
75 }
76 
~SurfaceControl()77 SurfaceControl::~SurfaceControl()
78 {
79     // Trigger an IPC now, to make sure things
80     // happen without delay, since these resources are quite heavy.
81     mClient.clear();
82     mHandle.clear();
83     mBbq.clear();
84     IPCThreadState::self()->flushCommands();
85 }
86 
disconnect()87 void SurfaceControl::disconnect() {
88     if (getIGraphicBufferProducer() != nullptr) {
89         getIGraphicBufferProducer()->disconnect(
90                 BufferQueueCore::CURRENTLY_CONNECTED_API);
91     }
92 }
93 
isSameSurface(const sp<SurfaceControl> & lhs,const sp<SurfaceControl> & rhs)94 bool SurfaceControl::isSameSurface(
95         const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
96 {
97     if (lhs == nullptr || rhs == nullptr)
98         return false;
99     return lhs->mHandle == rhs->mHandle;
100 }
101 
clearLayerFrameStats() const102 status_t SurfaceControl::clearLayerFrameStats() const {
103     status_t err = validate();
104     if (err != NO_ERROR) return err;
105     const sp<SurfaceComposerClient>& client(mClient);
106     return client->clearLayerFrameStats(mHandle);
107 }
108 
getLayerFrameStats(FrameStats * outStats) const109 status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
110     status_t err = validate();
111     if (err != NO_ERROR) return err;
112     const sp<SurfaceComposerClient>& client(mClient);
113     return client->getLayerFrameStats(mHandle, outStats);
114 }
115 
validate() const116 status_t SurfaceControl::validate() const
117 {
118     if (mHandle==nullptr || mClient==nullptr) {
119         ALOGE("invalid handle (%p) or client (%p)",
120                 mHandle.get(), mClient.get());
121         return NO_INIT;
122     }
123     return NO_ERROR;
124 }
125 
writeSurfaceToParcel(const sp<SurfaceControl> & control,Parcel * parcel)126 status_t SurfaceControl::writeSurfaceToParcel(
127         const sp<SurfaceControl>& control, Parcel* parcel)
128 {
129     sp<IGraphicBufferProducer> bp;
130     if (control != nullptr) {
131         bp = control->getIGraphicBufferProducer();
132     }
133     return parcel->writeStrongBinder(IInterface::asBinder(bp));
134 }
135 
generateSurfaceLocked()136 sp<Surface> SurfaceControl::generateSurfaceLocked()
137 {
138     uint32_t ignore;
139     auto flags = mCreateFlags & (ISurfaceComposerClient::eCursorWindow |
140                                  ISurfaceComposerClient::eOpaque);
141     mBbqChild = mClient->createSurface(String8("bbq-wrapper"), 0, 0, mFormat,
142                                        flags, mHandle, {}, &ignore);
143     mBbq = sp<BLASTBufferQueue>::make("bbq-adapter", mBbqChild, mWidth, mHeight, mFormat);
144 
145     // This surface is always consumed by SurfaceFlinger, so the
146     // producerControlledByApp value doesn't matter; using false.
147     mSurfaceData = mBbq->getSurface(true);
148 
149     return mSurfaceData;
150 }
151 
getSurface()152 sp<Surface> SurfaceControl::getSurface()
153 {
154     Mutex::Autolock _l(mLock);
155     if (mSurfaceData == nullptr) {
156         return generateSurfaceLocked();
157     }
158     return mSurfaceData;
159 }
160 
createSurface()161 sp<Surface> SurfaceControl::createSurface()
162 {
163     return getSurface();
164 }
165 
updateDefaultBufferSize(uint32_t width,uint32_t height)166 void SurfaceControl::updateDefaultBufferSize(uint32_t width, uint32_t height) {
167     Mutex::Autolock _l(mLock);
168     mWidth = width; mHeight = height;
169     if (mBbq) {
170         mBbq->update(mBbqChild, width, height, mFormat);
171     }
172 
173 }
174 
getLayerStateHandle() const175 sp<IBinder> SurfaceControl::getLayerStateHandle() const
176 {
177     return mHandle;
178 }
179 
getHandle() const180 sp<IBinder> SurfaceControl::getHandle() const {
181     if (mBbqChild != nullptr) {
182         return mBbqChild->getHandle();
183     }
184     return getLayerStateHandle();
185 }
186 
getLayerId() const187 int32_t SurfaceControl::getLayerId() const {
188     return mLayerId;
189 }
190 
getIGraphicBufferProducer()191 sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer()
192 {
193     getSurface();
194     Mutex::Autolock _l(mLock);
195 
196     return mBbq->getIGraphicBufferProducer();
197 }
198 
getClient() const199 sp<SurfaceComposerClient> SurfaceControl::getClient() const
200 {
201     return mClient;
202 }
203 
getTransformHint() const204 uint32_t SurfaceControl::getTransformHint() const {
205     Mutex::Autolock _l(mLock);
206     return mTransformHint;
207 }
208 
setTransformHint(uint32_t hint)209 void SurfaceControl::setTransformHint(uint32_t hint) {
210     Mutex::Autolock _l(mLock);
211     mTransformHint = hint;
212 }
213 
writeToParcel(Parcel & parcel)214 status_t SurfaceControl::writeToParcel(Parcel& parcel) {
215     SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
216     SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
217     SAFE_PARCEL(parcel.writeInt32, mLayerId);
218     SAFE_PARCEL(parcel.writeUint32, mTransformHint);
219     SAFE_PARCEL(parcel.writeUint32, mWidth);
220     SAFE_PARCEL(parcel.writeUint32, mHeight);
221     SAFE_PARCEL(parcel.writeUint32, mFormat);
222 
223     return NO_ERROR;
224 }
225 
readFromParcel(const Parcel & parcel,sp<SurfaceControl> * outSurfaceControl)226 status_t SurfaceControl::readFromParcel(const Parcel& parcel,
227                                         sp<SurfaceControl>* outSurfaceControl) {
228     sp<IBinder> client;
229     sp<IBinder> handle;
230     int32_t layerId;
231     uint32_t transformHint;
232     uint32_t width;
233     uint32_t height;
234     uint32_t format;
235 
236     SAFE_PARCEL(parcel.readStrongBinder, &client);
237     SAFE_PARCEL(parcel.readStrongBinder, &handle);
238     SAFE_PARCEL(parcel.readInt32, &layerId);
239     SAFE_PARCEL(parcel.readUint32, &transformHint);
240     SAFE_PARCEL(parcel.readUint32, &width);
241     SAFE_PARCEL(parcel.readUint32, &height);
242     SAFE_PARCEL(parcel.readUint32, &format);
243 
244     // We aren't the original owner of the surface.
245     *outSurfaceControl =
246             new SurfaceControl(new SurfaceComposerClient(
247                                        interface_cast<ISurfaceComposerClient>(client)),
248                                handle.get(), nullptr, layerId,
249                                width, height, format,
250                                transformHint);
251 
252     return NO_ERROR;
253 }
254 
readNullableFromParcel(const Parcel & parcel,sp<SurfaceControl> * outSurfaceControl)255 status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
256                                                 sp<SurfaceControl>* outSurfaceControl) {
257     bool isNotNull;
258     SAFE_PARCEL(parcel.readBool, &isNotNull);
259     if (isNotNull) {
260         SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
261     }
262 
263     return NO_ERROR;
264 }
265 
writeNullableToParcel(Parcel & parcel,const sp<SurfaceControl> & surfaceControl)266 status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
267                                                const sp<SurfaceControl>& surfaceControl) {
268     auto isNotNull = surfaceControl != nullptr;
269     SAFE_PARCEL(parcel.writeBool, isNotNull);
270     if (isNotNull) {
271         SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
272     }
273 
274     return NO_ERROR;
275 }
276 
getParentingLayer()277 sp<SurfaceControl> SurfaceControl::getParentingLayer() {
278     if (mBbqChild != nullptr) {
279         return mBbqChild;
280     }
281     return this;
282 }
283 
resolveFrameNumber(const std::optional<uint64_t> & frameNumber)284 uint64_t SurfaceControl::resolveFrameNumber(const std::optional<uint64_t>& frameNumber) {
285     if (frameNumber.has_value()) {
286         auto ret = frameNumber.value();
287         // Set the fallback to something far enough ahead that in the unlikely event of mixed
288         // "real" frame numbers and fallback frame numbers, we still won't collide in any
289         // meaningful capacity
290         mFallbackFrameNumber = ret + 100;
291         return ret;
292     } else {
293         return mFallbackFrameNumber++;
294     }
295 }
296 
297 // ----------------------------------------------------------------------------
298 }; // namespace android
299