• 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     mCreateFlags = other->mCreateFlags;
74 }
75 
~SurfaceControl()76 SurfaceControl::~SurfaceControl()
77 {
78     // Trigger an IPC now, to make sure things
79     // happen without delay, since these resources are quite heavy.
80     mClient.clear();
81     mHandle.clear();
82     mBbq.clear();
83     IPCThreadState::self()->flushCommands();
84 }
85 
disconnect()86 void SurfaceControl::disconnect() {
87     if (getIGraphicBufferProducer() != nullptr) {
88         getIGraphicBufferProducer()->disconnect(
89                 BufferQueueCore::CURRENTLY_CONNECTED_API);
90     }
91 }
92 
isSameSurface(const sp<SurfaceControl> & lhs,const sp<SurfaceControl> & rhs)93 bool SurfaceControl::isSameSurface(
94         const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
95 {
96     if (lhs == nullptr || rhs == nullptr)
97         return false;
98     return lhs->mHandle == rhs->mHandle;
99 }
100 
clearLayerFrameStats() const101 status_t SurfaceControl::clearLayerFrameStats() const {
102     status_t err = validate();
103     if (err != NO_ERROR) return err;
104     const sp<SurfaceComposerClient>& client(mClient);
105     return client->clearLayerFrameStats(mHandle);
106 }
107 
getLayerFrameStats(FrameStats * outStats) const108 status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
109     status_t err = validate();
110     if (err != NO_ERROR) return err;
111     const sp<SurfaceComposerClient>& client(mClient);
112     return client->getLayerFrameStats(mHandle, outStats);
113 }
114 
validate() const115 status_t SurfaceControl::validate() const
116 {
117     if (mHandle==nullptr || mClient==nullptr) {
118         ALOGE("invalid handle (%p) or client (%p)",
119                 mHandle.get(), mClient.get());
120         return NO_INIT;
121     }
122     return NO_ERROR;
123 }
124 
writeSurfaceToParcel(const sp<SurfaceControl> & control,Parcel * parcel)125 status_t SurfaceControl::writeSurfaceToParcel(
126         const sp<SurfaceControl>& control, Parcel* parcel)
127 {
128     sp<IGraphicBufferProducer> bp;
129     if (control != nullptr) {
130         bp = control->getIGraphicBufferProducer();
131     }
132     return parcel->writeStrongBinder(IInterface::asBinder(bp));
133 }
134 
generateSurfaceLocked()135 sp<Surface> SurfaceControl::generateSurfaceLocked()
136 {
137     uint32_t ignore;
138     auto flags = mCreateFlags & (ISurfaceComposerClient::eCursorWindow |
139                                  ISurfaceComposerClient::eOpaque);
140     mBbqChild = mClient->createSurface(String8("bbq-wrapper"), 0, 0, mFormat,
141                                        flags, mHandle, {}, &ignore);
142     mBbq = sp<BLASTBufferQueue>::make("bbq-adapter", mBbqChild, mWidth, mHeight, mFormat);
143 
144     // This surface is always consumed by SurfaceFlinger, so the
145     // producerControlledByApp value doesn't matter; using false.
146     mSurfaceData = mBbq->getSurface(true);
147 
148     return mSurfaceData;
149 }
150 
getSurface()151 sp<Surface> SurfaceControl::getSurface()
152 {
153     Mutex::Autolock _l(mLock);
154     if (mSurfaceData == nullptr) {
155         return generateSurfaceLocked();
156     }
157     return mSurfaceData;
158 }
159 
createSurface()160 sp<Surface> SurfaceControl::createSurface()
161 {
162     return getSurface();
163 }
164 
updateDefaultBufferSize(uint32_t width,uint32_t height)165 void SurfaceControl::updateDefaultBufferSize(uint32_t width, uint32_t height) {
166     Mutex::Autolock _l(mLock);
167     mWidth = width; mHeight = height;
168     if (mBbq) {
169         mBbq->update(mBbqChild, width, height, mFormat);
170     }
171 
172 }
173 
getLayerStateHandle() const174 sp<IBinder> SurfaceControl::getLayerStateHandle() const
175 {
176     return mHandle;
177 }
178 
getHandle() const179 sp<IBinder> SurfaceControl::getHandle() const {
180     if (mBbqChild != nullptr) {
181         return mBbqChild->getHandle();
182     }
183     return getLayerStateHandle();
184 }
185 
getLayerId() const186 int32_t SurfaceControl::getLayerId() const {
187     return mLayerId;
188 }
189 
getIGraphicBufferProducer()190 sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer()
191 {
192     getSurface();
193     Mutex::Autolock _l(mLock);
194 
195     return mBbq->getIGraphicBufferProducer();
196 }
197 
getClient() const198 sp<SurfaceComposerClient> SurfaceControl::getClient() const
199 {
200     return mClient;
201 }
202 
getTransformHint() const203 uint32_t SurfaceControl::getTransformHint() const {
204     Mutex::Autolock _l(mLock);
205     return mTransformHint;
206 }
207 
setTransformHint(uint32_t hint)208 void SurfaceControl::setTransformHint(uint32_t hint) {
209     Mutex::Autolock _l(mLock);
210     mTransformHint = hint;
211 }
212 
writeToParcel(Parcel & parcel)213 status_t SurfaceControl::writeToParcel(Parcel& parcel) {
214     SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
215     SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
216     SAFE_PARCEL(parcel.writeInt32, mLayerId);
217     SAFE_PARCEL(parcel.writeUint32, mTransformHint);
218     SAFE_PARCEL(parcel.writeUint32, mWidth);
219     SAFE_PARCEL(parcel.writeUint32, mHeight);
220     SAFE_PARCEL(parcel.writeUint32, mFormat);
221 
222     return NO_ERROR;
223 }
224 
readFromParcel(const Parcel & parcel,sp<SurfaceControl> * outSurfaceControl)225 status_t SurfaceControl::readFromParcel(const Parcel& parcel,
226                                         sp<SurfaceControl>* outSurfaceControl) {
227     sp<IBinder> client;
228     sp<IBinder> handle;
229     int32_t layerId;
230     uint32_t transformHint;
231     uint32_t width;
232     uint32_t height;
233     uint32_t format;
234 
235     SAFE_PARCEL(parcel.readStrongBinder, &client);
236     SAFE_PARCEL(parcel.readStrongBinder, &handle);
237     SAFE_PARCEL(parcel.readInt32, &layerId);
238     SAFE_PARCEL(parcel.readUint32, &transformHint);
239     SAFE_PARCEL(parcel.readUint32, &width);
240     SAFE_PARCEL(parcel.readUint32, &height);
241     SAFE_PARCEL(parcel.readUint32, &format);
242 
243     // We aren't the original owner of the surface.
244     *outSurfaceControl =
245             new SurfaceControl(new SurfaceComposerClient(
246                                        interface_cast<ISurfaceComposerClient>(client)),
247                                handle.get(), nullptr, layerId,
248                                width, height, format,
249                                transformHint);
250 
251     return NO_ERROR;
252 }
253 
readNullableFromParcel(const Parcel & parcel,sp<SurfaceControl> * outSurfaceControl)254 status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
255                                                 sp<SurfaceControl>* outSurfaceControl) {
256     bool isNotNull;
257     SAFE_PARCEL(parcel.readBool, &isNotNull);
258     if (isNotNull) {
259         SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
260     }
261 
262     return NO_ERROR;
263 }
264 
writeNullableToParcel(Parcel & parcel,const sp<SurfaceControl> & surfaceControl)265 status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
266                                                const sp<SurfaceControl>& surfaceControl) {
267     auto isNotNull = surfaceControl != nullptr;
268     SAFE_PARCEL(parcel.writeBool, isNotNull);
269     if (isNotNull) {
270         SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
271     }
272 
273     return NO_ERROR;
274 }
275 
getParentingLayer()276 sp<SurfaceControl> SurfaceControl::getParentingLayer() {
277     if (mBbqChild != nullptr) {
278         return mBbqChild;
279     }
280     return this;
281 }
282 
283 // ----------------------------------------------------------------------------
284 }; // namespace android
285