blob: 84257dee9b195318628c2cfa0cac76756a90a358 [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>
29#include <utils/threads.h>
30
31#include <binder/IPCThreadState.h>
32
Mathias Agopiane3c697f2013-02-14 17:11:02 -080033#include <ui/GraphicBuffer.h>
34#include <ui/Rect.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010035#include <ui/StaticDisplayInfo.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080037#include <gui/BufferQueueCore.h>
Robert Carr5b3b9142021-02-22 12:27:32 -080038#include <gui/BLASTBufferQueue.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080039#include <gui/ISurfaceComposer.h>
40#include <gui/Surface.h>
41#include <gui/SurfaceComposerClient.h>
42#include <gui/SurfaceControl.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010043#include <private/gui/ParcelUtils.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044
45namespace android {
46
47// ============================================================================
48// SurfaceControl
49// ============================================================================
50
Valerie Hau1acd6962019-10-28 16:35:48 -070051SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
Huihong Luod3d8f8e2022-03-08 14:48:46 -080052 int32_t layerId, uint32_t w, uint32_t h, PixelFormat format,
53 uint32_t transform, uint32_t flags)
Valerie Hau1acd6962019-10-28 16:35:48 -070054 : mClient(client),
55 mHandle(handle),
Pablo Gamitodbc31672020-09-01 18:28:58 +000056 mLayerId(layerId),
Robert Carr5b3b9142021-02-22 12:27:32 -080057 mTransformHint(transform),
58 mWidth(w),
59 mHeight(h),
60 mFormat(format),
61 mCreateFlags(flags) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070062
Robert Carrb89ea9d2018-12-10 13:01:14 -080063SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
64 mClient = other->mClient;
65 mHandle = other->mHandle;
Valerie Hau1acd6962019-10-28 16:35:48 -070066 mTransformHint = other->mTransformHint;
Pablo Gamitodbc31672020-09-01 18:28:58 +000067 mLayerId = other->mLayerId;
Robert Carr5b3b9142021-02-22 12:27:32 -080068 mWidth = other->mWidth;
69 mHeight = other->mHeight;
Robert Carrc2cf02c2022-03-22 15:25:54 -070070 mFormat = other->mFormat;
Robert Carr5b3b9142021-02-22 12:27:32 -080071 mCreateFlags = other->mCreateFlags;
Robert Carrb89ea9d2018-12-10 13:01:14 -080072}
73
Mathias Agopiane3c697f2013-02-14 17:11:02 -080074SurfaceControl::~SurfaceControl()
75{
Robert Carr87246532019-02-04 15:20:26 -080076 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080077 // happen without delay, since these resources are quite heavy.
78 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079 mHandle.clear();
Robert Carr5b3b9142021-02-22 12:27:32 -080080 mBbq.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080081 IPCThreadState::self()->flushCommands();
82}
83
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080084void SurfaceControl::disconnect() {
Robert Carr5b3b9142021-02-22 12:27:32 -080085 if (getIGraphicBufferProducer() != nullptr) {
86 getIGraphicBufferProducer()->disconnect(
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080087 BufferQueueCore::CURRENTLY_CONNECTED_API);
88 }
89}
90
Mathias Agopiane3c697f2013-02-14 17:11:02 -080091bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070092 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080093{
Yi Kong48a619f2018-06-05 16:34:59 -070094 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080095 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070096 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080097}
98
Svetoslavd85084b2014-03-20 10:28:31 -070099status_t SurfaceControl::clearLayerFrameStats() const {
100 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700101 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700102 const sp<SurfaceComposerClient>& client(mClient);
103 return client->clearLayerFrameStats(mHandle);
104}
105
106status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
107 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700108 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700109 const sp<SurfaceComposerClient>& client(mClient);
110 return client->getLayerFrameStats(mHandle, outStats);
111}
112
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800113status_t SurfaceControl::validate() const
114{
Yi Kong48a619f2018-06-05 16:34:59 -0700115 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116 ALOGE("invalid handle (%p) or client (%p)",
117 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800118 return NO_INIT;
119 }
120 return NO_ERROR;
121}
122
123status_t SurfaceControl::writeSurfaceToParcel(
124 const sp<SurfaceControl>& control, Parcel* parcel)
125{
126 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700127 if (control != nullptr) {
Robert Carr5b3b9142021-02-22 12:27:32 -0800128 bp = control->getIGraphicBufferProducer();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800129 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800130 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800131}
132
Robert Carr5b3b9142021-02-22 12:27:32 -0800133sp<Surface> SurfaceControl::generateSurfaceLocked()
Bryce Lee4e623e22017-06-16 07:06:17 -0700134{
Robert Carr5b3b9142021-02-22 12:27:32 -0800135 uint32_t ignore;
136 auto flags = mCreateFlags & (ISurfaceComposerClient::eCursorWindow |
137 ISurfaceComposerClient::eOpaque);
138 mBbqChild = mClient->createSurface(String8("bbq-wrapper"), 0, 0, mFormat,
139 flags, mHandle, {}, &ignore);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700140 mBbq = sp<BLASTBufferQueue>::make("bbq-adapter", mBbqChild, mWidth, mHeight, mFormat);
Robert Carr5b3b9142021-02-22 12:27:32 -0800141
Bryce Lee4e623e22017-06-16 07:06:17 -0700142 // This surface is always consumed by SurfaceFlinger, so the
143 // producerControlledByApp value doesn't matter; using false.
Robert Carr5b3b9142021-02-22 12:27:32 -0800144 mSurfaceData = mBbq->getSurface(true);
Bryce Lee4e623e22017-06-16 07:06:17 -0700145
146 return mSurfaceData;
147}
148
Robert Carr5b3b9142021-02-22 12:27:32 -0800149sp<Surface> SurfaceControl::getSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800150{
151 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700152 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700153 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800154 }
155 return mSurfaceData;
156}
157
Robert Carr5b3b9142021-02-22 12:27:32 -0800158sp<Surface> SurfaceControl::createSurface()
Bryce Lee4e623e22017-06-16 07:06:17 -0700159{
Robert Carr5b3b9142021-02-22 12:27:32 -0800160 return getSurface();
Bryce Lee4e623e22017-06-16 07:06:17 -0700161}
162
Robert Carr5b3b9142021-02-22 12:27:32 -0800163void SurfaceControl::updateDefaultBufferSize(uint32_t width, uint32_t height) {
164 Mutex::Autolock _l(mLock);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800165 mWidth = width;
166 mHeight = height;
Robert Carr5b3b9142021-02-22 12:27:32 -0800167 if (mBbq) {
Vishnu Nairf9cb20a2021-05-10 16:27:51 -0700168 mBbq->update(mBbqChild, width, height, mFormat);
Robert Carr5b3b9142021-02-22 12:27:32 -0800169 }
Robert Carr5b3b9142021-02-22 12:27:32 -0800170}
171
172sp<IBinder> SurfaceControl::getLayerStateHandle() const
Dan Stoza7dde5992015-05-22 09:51:44 -0700173{
Dan Stoza7dde5992015-05-22 09:51:44 -0700174 return mHandle;
175}
176
Robert Carr5b3b9142021-02-22 12:27:32 -0800177sp<IBinder> SurfaceControl::getHandle() const {
178 if (mBbqChild != nullptr) {
179 return mBbqChild->getHandle();
180 }
181 return getLayerStateHandle();
182}
183
Pablo Gamitodbc31672020-09-01 18:28:58 +0000184int32_t SurfaceControl::getLayerId() const {
185 return mLayerId;
186}
187
Robert Carr5b3b9142021-02-22 12:27:32 -0800188sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer()
Robert Carra35ef9f2019-01-25 14:29:21 -0800189{
Robert Carr5b3b9142021-02-22 12:27:32 -0800190 getSurface();
Robert Carra35ef9f2019-01-25 14:29:21 -0800191 Mutex::Autolock _l(mLock);
Robert Carr5b3b9142021-02-22 12:27:32 -0800192
193 return mBbq->getIGraphicBufferProducer();
Robert Carra35ef9f2019-01-25 14:29:21 -0800194}
195
Robert Carr4cdc58f2017-08-23 14:22:20 -0700196sp<SurfaceComposerClient> SurfaceControl::getClient() const
197{
198 return mClient;
199}
200
Valerie Hau1acd6962019-10-28 16:35:48 -0700201uint32_t SurfaceControl::getTransformHint() const {
202 Mutex::Autolock _l(mLock);
203 return mTransformHint;
204}
205
206void SurfaceControl::setTransformHint(uint32_t hint) {
207 Mutex::Autolock _l(mLock);
208 mTransformHint = hint;
209}
210
Pablo Gamito421dfd52020-09-22 18:11:45 +0000211status_t SurfaceControl::writeToParcel(Parcel& parcel) {
212 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
213 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000214 SAFE_PARCEL(parcel.writeInt32, mLayerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000215 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
Robert Carr5b3b9142021-02-22 12:27:32 -0800216 SAFE_PARCEL(parcel.writeUint32, mWidth);
217 SAFE_PARCEL(parcel.writeUint32, mHeight);
218 SAFE_PARCEL(parcel.writeUint32, mFormat);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000219
220 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100221}
222
Pablo Gamito421dfd52020-09-22 18:11:45 +0000223status_t SurfaceControl::readFromParcel(const Parcel& parcel,
224 sp<SurfaceControl>* outSurfaceControl) {
Garfield Tan2faa6312020-06-23 15:46:10 -0700225 sp<IBinder> client;
Garfield Tan2faa6312020-06-23 15:46:10 -0700226 sp<IBinder> handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000227 int32_t layerId;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000228 uint32_t transformHint;
Robert Carr5b3b9142021-02-22 12:27:32 -0800229 uint32_t width;
230 uint32_t height;
231 uint32_t format;
Garfield Tan2faa6312020-06-23 15:46:10 -0700232
Pablo Gamito421dfd52020-09-22 18:11:45 +0000233 SAFE_PARCEL(parcel.readStrongBinder, &client);
234 SAFE_PARCEL(parcel.readStrongBinder, &handle);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000235 SAFE_PARCEL(parcel.readInt32, &layerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000236 SAFE_PARCEL(parcel.readUint32, &transformHint);
Robert Carr5b3b9142021-02-22 12:27:32 -0800237 SAFE_PARCEL(parcel.readUint32, &width);
238 SAFE_PARCEL(parcel.readUint32, &height);
239 SAFE_PARCEL(parcel.readUint32, &format);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000240
Jorim Jaggi0b267102018-01-29 16:39:21 +0100241 // We aren't the original owner of the surface.
Pablo Gamito421dfd52020-09-22 18:11:45 +0000242 *outSurfaceControl =
243 new SurfaceControl(new SurfaceComposerClient(
244 interface_cast<ISurfaceComposerClient>(client)),
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800245 handle.get(), layerId, width, height, format, transformHint);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000246
247 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100248}
249
Pablo Gamito91512a02020-09-22 18:14:43 +0000250status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
251 sp<SurfaceControl>* outSurfaceControl) {
252 bool isNotNull;
253 SAFE_PARCEL(parcel.readBool, &isNotNull);
254 if (isNotNull) {
255 SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
256 }
257
258 return NO_ERROR;
259}
260
261status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
262 const sp<SurfaceControl>& surfaceControl) {
263 auto isNotNull = surfaceControl != nullptr;
264 SAFE_PARCEL(parcel.writeBool, isNotNull);
265 if (isNotNull) {
266 SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
267 }
268
269 return NO_ERROR;
270}
271
Robert Carr5b3b9142021-02-22 12:27:32 -0800272sp<SurfaceControl> SurfaceControl::getParentingLayer() {
273 if (mBbqChild != nullptr) {
274 return mBbqChild;
275 }
276 return this;
277}
278
John Reck57f748c2022-03-17 17:23:49 -0400279uint64_t SurfaceControl::resolveFrameNumber(const std::optional<uint64_t>& frameNumber) {
280 if (frameNumber.has_value()) {
281 auto ret = frameNumber.value();
282 // Set the fallback to something far enough ahead that in the unlikely event of mixed
283 // "real" frame numbers and fallback frame numbers, we still won't collide in any
284 // meaningful capacity
285 mFallbackFrameNumber = ret + 100;
286 return ret;
287 } else {
288 return mFallbackFrameNumber++;
289 }
290}
291
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800292// ----------------------------------------------------------------------------
293}; // namespace android