blob: ba2d80d1b3bad057ecf90ea2af96ef671ce01cd8 [file] [log] [blame]
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001/*
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
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026#include <utils/Errors.h>
Pablo Gamito11dcc222020-09-12 15:49:39 +000027#include <utils/KeyedVector.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080028#include <utils/Log.h>
Rachel Lee2248f522023-01-27 16:45:23 -080029#include <utils/Looper.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080030#include <utils/threads.h>
31
32#include <binder/IPCThreadState.h>
33
Mathias Agopiane3c697f2013-02-14 17:11:02 -080034#include <ui/GraphicBuffer.h>
35#include <ui/Rect.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010036#include <ui/StaticDisplayInfo.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080037
Robert Carr5b3b9142021-02-22 12:27:32 -080038#include <gui/BLASTBufferQueue.h>
Rachel Lee2248f522023-01-27 16:45:23 -080039#include <gui/BufferQueueCore.h>
40#include <gui/Choreographer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/ISurfaceComposer.h>
42#include <gui/Surface.h>
43#include <gui/SurfaceComposerClient.h>
44#include <gui/SurfaceControl.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010045#include <private/gui/ParcelUtils.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080046
47namespace android {
48
49// ============================================================================
50// SurfaceControl
51// ============================================================================
52
Valerie Hau1acd6962019-10-28 16:35:48 -070053SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
Patrick Williamsa361de62022-10-06 20:34:10 +000054 int32_t layerId, const std::string& name, uint32_t w, uint32_t h,
55 PixelFormat format, uint32_t transform, uint32_t flags)
Valerie Hau1acd6962019-10-28 16:35:48 -070056 : mClient(client),
57 mHandle(handle),
Pablo Gamitodbc31672020-09-01 18:28:58 +000058 mLayerId(layerId),
Patrick Williamsa361de62022-10-06 20:34:10 +000059 mName(name),
Robert Carr5b3b9142021-02-22 12:27:32 -080060 mTransformHint(transform),
61 mWidth(w),
62 mHeight(h),
63 mFormat(format),
64 mCreateFlags(flags) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070065
Robert Carrb89ea9d2018-12-10 13:01:14 -080066SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
67 mClient = other->mClient;
68 mHandle = other->mHandle;
Valerie Hau1acd6962019-10-28 16:35:48 -070069 mTransformHint = other->mTransformHint;
Pablo Gamitodbc31672020-09-01 18:28:58 +000070 mLayerId = other->mLayerId;
Patrick Williamsa361de62022-10-06 20:34:10 +000071 mName = other->mName;
Robert Carr5b3b9142021-02-22 12:27:32 -080072 mWidth = other->mWidth;
73 mHeight = other->mHeight;
Robert Carrc2cf02c2022-03-22 15:25:54 -070074 mFormat = other->mFormat;
Robert Carr5b3b9142021-02-22 12:27:32 -080075 mCreateFlags = other->mCreateFlags;
Robert Carrb89ea9d2018-12-10 13:01:14 -080076}
77
Mathias Agopiane3c697f2013-02-14 17:11:02 -080078SurfaceControl::~SurfaceControl()
79{
Robert Carr87246532019-02-04 15:20:26 -080080 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080081 // happen without delay, since these resources are quite heavy.
82 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083 mHandle.clear();
Robert Carr5b3b9142021-02-22 12:27:32 -080084 mBbq.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080085 IPCThreadState::self()->flushCommands();
86}
87
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080088void SurfaceControl::disconnect() {
Robert Carr5b3b9142021-02-22 12:27:32 -080089 if (getIGraphicBufferProducer() != nullptr) {
90 getIGraphicBufferProducer()->disconnect(
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080091 BufferQueueCore::CURRENTLY_CONNECTED_API);
92 }
93}
94
Mathias Agopiane3c697f2013-02-14 17:11:02 -080095bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070096 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080097{
Yi Kong48a619f2018-06-05 16:34:59 -070098 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080099 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800101}
102
Svetoslavd85084b2014-03-20 10:28:31 -0700103status_t SurfaceControl::clearLayerFrameStats() const {
104 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700105 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700106 const sp<SurfaceComposerClient>& client(mClient);
107 return client->clearLayerFrameStats(mHandle);
108}
109
110status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
111 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700112 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700113 const sp<SurfaceComposerClient>& client(mClient);
114 return client->getLayerFrameStats(mHandle, outStats);
115}
116
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800117status_t SurfaceControl::validate() const
118{
Yi Kong48a619f2018-06-05 16:34:59 -0700119 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 ALOGE("invalid handle (%p) or client (%p)",
121 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800122 return NO_INIT;
123 }
124 return NO_ERROR;
125}
126
127status_t SurfaceControl::writeSurfaceToParcel(
128 const sp<SurfaceControl>& control, Parcel* parcel)
129{
130 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700131 if (control != nullptr) {
Robert Carr5b3b9142021-02-22 12:27:32 -0800132 bp = control->getIGraphicBufferProducer();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800133 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800134 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800135}
136
Robert Carr5b3b9142021-02-22 12:27:32 -0800137sp<Surface> SurfaceControl::generateSurfaceLocked()
Bryce Lee4e623e22017-06-16 07:06:17 -0700138{
Robert Carr5b3b9142021-02-22 12:27:32 -0800139 uint32_t ignore;
140 auto flags = mCreateFlags & (ISurfaceComposerClient::eCursorWindow |
141 ISurfaceComposerClient::eOpaque);
Wenhui Yangab89d812024-09-11 23:21:38 +0000142 mBbqChild = mClient->createSurface(String8::format("[BBQ] %s", mName.c_str()), 0, 0, mFormat,
Robert Carr5b3b9142021-02-22 12:27:32 -0800143 flags, mHandle, {}, &ignore);
Vishnu Nair9c0a86b2025-01-14 12:47:04 -0800144 mBbq = sp<BLASTBufferQueue>::make("[BBQ] " + mName, /* updateDestinationFrame */ true);
145 mBbq->update(mBbqChild, mWidth, mHeight, mFormat);
Robert Carr5b3b9142021-02-22 12:27:32 -0800146
Bryce Lee4e623e22017-06-16 07:06:17 -0700147 // This surface is always consumed by SurfaceFlinger, so the
148 // producerControlledByApp value doesn't matter; using false.
Robert Carr5b3b9142021-02-22 12:27:32 -0800149 mSurfaceData = mBbq->getSurface(true);
Bryce Lee4e623e22017-06-16 07:06:17 -0700150
151 return mSurfaceData;
152}
153
Robert Carr5b3b9142021-02-22 12:27:32 -0800154sp<Surface> SurfaceControl::getSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800155{
156 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700157 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700158 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800159 }
160 return mSurfaceData;
161}
162
Robert Carr5b3b9142021-02-22 12:27:32 -0800163sp<Surface> SurfaceControl::createSurface()
Bryce Lee4e623e22017-06-16 07:06:17 -0700164{
Robert Carr5b3b9142021-02-22 12:27:32 -0800165 return getSurface();
Bryce Lee4e623e22017-06-16 07:06:17 -0700166}
167
Robert Carr5b3b9142021-02-22 12:27:32 -0800168void SurfaceControl::updateDefaultBufferSize(uint32_t width, uint32_t height) {
169 Mutex::Autolock _l(mLock);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800170 mWidth = width;
171 mHeight = height;
Robert Carr5b3b9142021-02-22 12:27:32 -0800172 if (mBbq) {
Vishnu Nairf9cb20a2021-05-10 16:27:51 -0700173 mBbq->update(mBbqChild, width, height, mFormat);
Robert Carr5b3b9142021-02-22 12:27:32 -0800174 }
Robert Carr5b3b9142021-02-22 12:27:32 -0800175}
176
177sp<IBinder> SurfaceControl::getLayerStateHandle() const
Dan Stoza7dde5992015-05-22 09:51:44 -0700178{
Dan Stoza7dde5992015-05-22 09:51:44 -0700179 return mHandle;
180}
181
Robert Carr5b3b9142021-02-22 12:27:32 -0800182sp<IBinder> SurfaceControl::getHandle() const {
183 if (mBbqChild != nullptr) {
184 return mBbqChild->getHandle();
185 }
186 return getLayerStateHandle();
187}
188
Pablo Gamitodbc31672020-09-01 18:28:58 +0000189int32_t SurfaceControl::getLayerId() const {
190 return mLayerId;
191}
192
Patrick Williamsa361de62022-10-06 20:34:10 +0000193const std::string& SurfaceControl::getName() const {
194 return mName;
195}
196
Rachel Lee2248f522023-01-27 16:45:23 -0800197std::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
Robert Carr5b3b9142021-02-22 12:27:32 -0800215sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer()
Robert Carra35ef9f2019-01-25 14:29:21 -0800216{
Robert Carr5b3b9142021-02-22 12:27:32 -0800217 getSurface();
Robert Carra35ef9f2019-01-25 14:29:21 -0800218 Mutex::Autolock _l(mLock);
Robert Carr5b3b9142021-02-22 12:27:32 -0800219
220 return mBbq->getIGraphicBufferProducer();
Robert Carra35ef9f2019-01-25 14:29:21 -0800221}
222
Robert Carr4cdc58f2017-08-23 14:22:20 -0700223sp<SurfaceComposerClient> SurfaceControl::getClient() const
224{
225 return mClient;
226}
227
Valerie Hau1acd6962019-10-28 16:35:48 -0700228uint32_t SurfaceControl::getTransformHint() const {
229 Mutex::Autolock _l(mLock);
230 return mTransformHint;
231}
232
233void SurfaceControl::setTransformHint(uint32_t hint) {
234 Mutex::Autolock _l(mLock);
235 mTransformHint = hint;
236}
237
Pablo Gamito421dfd52020-09-22 18:11:45 +0000238status_t SurfaceControl::writeToParcel(Parcel& parcel) {
239 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
240 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000241 SAFE_PARCEL(parcel.writeInt32, mLayerId);
Patrick Williamsa361de62022-10-06 20:34:10 +0000242 SAFE_PARCEL(parcel.writeUtf8AsUtf16, mName);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000243 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
Robert Carr5b3b9142021-02-22 12:27:32 -0800244 SAFE_PARCEL(parcel.writeUint32, mWidth);
245 SAFE_PARCEL(parcel.writeUint32, mHeight);
246 SAFE_PARCEL(parcel.writeUint32, mFormat);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000247
248 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100249}
250
Pablo Gamito421dfd52020-09-22 18:11:45 +0000251status_t SurfaceControl::readFromParcel(const Parcel& parcel,
252 sp<SurfaceControl>* outSurfaceControl) {
Garfield Tan2faa6312020-06-23 15:46:10 -0700253 sp<IBinder> client;
Garfield Tan2faa6312020-06-23 15:46:10 -0700254 sp<IBinder> handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000255 int32_t layerId;
Patrick Williamsa361de62022-10-06 20:34:10 +0000256 std::string layerName;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000257 uint32_t transformHint;
Robert Carr5b3b9142021-02-22 12:27:32 -0800258 uint32_t width;
259 uint32_t height;
260 uint32_t format;
Garfield Tan2faa6312020-06-23 15:46:10 -0700261
Pablo Gamito421dfd52020-09-22 18:11:45 +0000262 SAFE_PARCEL(parcel.readStrongBinder, &client);
263 SAFE_PARCEL(parcel.readStrongBinder, &handle);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000264 SAFE_PARCEL(parcel.readInt32, &layerId);
Patrick Williamsa361de62022-10-06 20:34:10 +0000265 SAFE_PARCEL(parcel.readUtf8FromUtf16, &layerName);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000266 SAFE_PARCEL(parcel.readUint32, &transformHint);
Robert Carr5b3b9142021-02-22 12:27:32 -0800267 SAFE_PARCEL(parcel.readUint32, &width);
268 SAFE_PARCEL(parcel.readUint32, &height);
269 SAFE_PARCEL(parcel.readUint32, &format);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000270
Jorim Jaggi0b267102018-01-29 16:39:21 +0100271 // We aren't the original owner of the surface.
Anton Ivanov81793802025-02-13 22:57:24 -0800272 *outSurfaceControl =
273 sp<SurfaceControl>::make(sp<SurfaceComposerClient>::make(
274 interface_cast<ISurfaceComposerClient>(client)),
275 handle, layerId, layerName, width, height, format,
276 transformHint);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000277
278 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100279}
280
Pablo Gamito91512a02020-09-22 18:14:43 +0000281status_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
292status_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
Robert Carr5b3b9142021-02-22 12:27:32 -0800303sp<SurfaceControl> SurfaceControl::getParentingLayer() {
304 if (mBbqChild != nullptr) {
305 return mBbqChild;
306 }
Anton Ivanov81793802025-02-13 22:57:24 -0800307 return sp<SurfaceControl>::fromExisting(this);
Robert Carr5b3b9142021-02-22 12:27:32 -0800308}
309
John Reck57f748c2022-03-17 17:23:49 -0400310uint64_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
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800323// ----------------------------------------------------------------------------
324}; // namespace android