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