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::format("[BBQ] %s", mName.c_str()), 0, 0, mFormat,
143 flags, mHandle, {}, &ignore);
144 mBbq = sp<BLASTBufferQueue>::make("[BBQ] " + mName, /* updateDestinationFrame */ true);
145 mBbq->update(mBbqChild, mWidth, mHeight, mFormat);
146
147 // This surface is always consumed by SurfaceFlinger, so the
148 // producerControlledByApp value doesn't matter; using false.
149 mSurfaceData = mBbq->getSurface(true);
150
151 return mSurfaceData;
152 }
153
getSurface()154 sp<Surface> SurfaceControl::getSurface()
155 {
156 Mutex::Autolock _l(mLock);
157 if (mSurfaceData == nullptr) {
158 return generateSurfaceLocked();
159 }
160 return mSurfaceData;
161 }
162
createSurface()163 sp<Surface> SurfaceControl::createSurface()
164 {
165 return getSurface();
166 }
167
updateDefaultBufferSize(uint32_t width,uint32_t height)168 void SurfaceControl::updateDefaultBufferSize(uint32_t width, uint32_t height) {
169 Mutex::Autolock _l(mLock);
170 mWidth = width;
171 mHeight = height;
172 if (mBbq) {
173 mBbq->update(mBbqChild, width, height, mFormat);
174 }
175 }
176
getLayerStateHandle() const177 sp<IBinder> SurfaceControl::getLayerStateHandle() const
178 {
179 return mHandle;
180 }
181
getHandle() const182 sp<IBinder> SurfaceControl::getHandle() const {
183 if (mBbqChild != nullptr) {
184 return mBbqChild->getHandle();
185 }
186 return getLayerStateHandle();
187 }
188
getLayerId() const189 int32_t SurfaceControl::getLayerId() const {
190 return mLayerId;
191 }
192
getName() const193 const std::string& SurfaceControl::getName() const {
194 return mName;
195 }
196
getChoreographer()197 std::shared_ptr<Choreographer> SurfaceControl::getChoreographer() {
198 if (mChoreographer) {
199 return mChoreographer;
200 }
201 sp<Looper> looper = Looper::getForThread();
202 if (!looper.get()) {
203 ALOGE("%s: No looper prepared for thread", __func__);
204 return nullptr;
205 }
206 mChoreographer = std::make_shared<Choreographer>(looper, getHandle());
207 status_t result = mChoreographer->initialize();
208 if (result != OK) {
209 ALOGE("Failed to initialize choreographer");
210 mChoreographer = nullptr;
211 }
212 return mChoreographer;
213 }
214
getIGraphicBufferProducer()215 sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer()
216 {
217 getSurface();
218 Mutex::Autolock _l(mLock);
219
220 return mBbq->getIGraphicBufferProducer();
221 }
222
getClient() const223 sp<SurfaceComposerClient> SurfaceControl::getClient() const
224 {
225 return mClient;
226 }
227
getTransformHint() const228 uint32_t SurfaceControl::getTransformHint() const {
229 Mutex::Autolock _l(mLock);
230 return mTransformHint;
231 }
232
setTransformHint(uint32_t hint)233 void SurfaceControl::setTransformHint(uint32_t hint) {
234 Mutex::Autolock _l(mLock);
235 mTransformHint = hint;
236 }
237
writeToParcel(Parcel & parcel)238 status_t SurfaceControl::writeToParcel(Parcel& parcel) {
239 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
240 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
241 SAFE_PARCEL(parcel.writeInt32, mLayerId);
242 SAFE_PARCEL(parcel.writeUtf8AsUtf16, mName);
243 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
244 SAFE_PARCEL(parcel.writeUint32, mWidth);
245 SAFE_PARCEL(parcel.writeUint32, mHeight);
246 SAFE_PARCEL(parcel.writeUint32, mFormat);
247
248 return NO_ERROR;
249 }
250
readFromParcel(const Parcel & parcel,sp<SurfaceControl> * outSurfaceControl)251 status_t SurfaceControl::readFromParcel(const Parcel& parcel,
252 sp<SurfaceControl>* outSurfaceControl) {
253 sp<IBinder> client;
254 sp<IBinder> handle;
255 int32_t layerId;
256 std::string layerName;
257 uint32_t transformHint;
258 uint32_t width;
259 uint32_t height;
260 uint32_t format;
261
262 SAFE_PARCEL(parcel.readStrongBinder, &client);
263 SAFE_PARCEL(parcel.readStrongBinder, &handle);
264 SAFE_PARCEL(parcel.readInt32, &layerId);
265 SAFE_PARCEL(parcel.readUtf8FromUtf16, &layerName);
266 SAFE_PARCEL(parcel.readUint32, &transformHint);
267 SAFE_PARCEL(parcel.readUint32, &width);
268 SAFE_PARCEL(parcel.readUint32, &height);
269 SAFE_PARCEL(parcel.readUint32, &format);
270
271 // We aren't the original owner of the surface.
272 *outSurfaceControl =
273 sp<SurfaceControl>::make(sp<SurfaceComposerClient>::make(
274 interface_cast<ISurfaceComposerClient>(client)),
275 handle, layerId, layerName, width, height, format,
276 transformHint);
277
278 return NO_ERROR;
279 }
280
readNullableFromParcel(const Parcel & parcel,sp<SurfaceControl> * outSurfaceControl)281 status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
282 sp<SurfaceControl>* outSurfaceControl) {
283 bool isNotNull;
284 SAFE_PARCEL(parcel.readBool, &isNotNull);
285 if (isNotNull) {
286 SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
287 }
288
289 return NO_ERROR;
290 }
291
writeNullableToParcel(Parcel & parcel,const sp<SurfaceControl> & surfaceControl)292 status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
293 const sp<SurfaceControl>& surfaceControl) {
294 auto isNotNull = surfaceControl != nullptr;
295 SAFE_PARCEL(parcel.writeBool, isNotNull);
296 if (isNotNull) {
297 SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
298 }
299
300 return NO_ERROR;
301 }
302
getParentingLayer()303 sp<SurfaceControl> SurfaceControl::getParentingLayer() {
304 if (mBbqChild != nullptr) {
305 return mBbqChild;
306 }
307 return this;
308 }
309
resolveFrameNumber(const std::optional<uint64_t> & frameNumber)310 uint64_t SurfaceControl::resolveFrameNumber(const std::optional<uint64_t>& frameNumber) {
311 if (frameNumber.has_value()) {
312 auto ret = frameNumber.value();
313 // Set the fallback to something far enough ahead that in the unlikely event of mixed
314 // "real" frame numbers and fallback frame numbers, we still won't collide in any
315 // meaningful capacity
316 mFallbackFrameNumber = ret + 100;
317 return ret;
318 } else {
319 return mFallbackFrameNumber++;
320 }
321 }
322
323 // ----------------------------------------------------------------------------
324 }; // namespace android
325