blob: 654fb336fe9d03527a6a2e0872dcb8ac17876bde [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,
Pablo Gamitodbc31672020-09-01 18:28:58 +000052 const sp<IGraphicBufferProducer>& gbp, int32_t layerId,
Robert Carr5b3b9142021-02-22 12:27:32 -080053 uint32_t w, uint32_t h, PixelFormat format, uint32_t transform,
54 uint32_t flags)
Valerie Hau1acd6962019-10-28 16:35:48 -070055 : mClient(client),
56 mHandle(handle),
57 mGraphicBufferProducer(gbp),
Pablo Gamitodbc31672020-09-01 18:28:58 +000058 mLayerId(layerId),
Robert Carr5b3b9142021-02-22 12:27:32 -080059 mTransformHint(transform),
60 mWidth(w),
61 mHeight(h),
62 mFormat(format),
63 mCreateFlags(flags) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070064
Robert Carrb89ea9d2018-12-10 13:01:14 -080065SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
66 mClient = other->mClient;
67 mHandle = other->mHandle;
68 mGraphicBufferProducer = other->mGraphicBufferProducer;
Valerie Hau1acd6962019-10-28 16:35:48 -070069 mTransformHint = other->mTransformHint;
Pablo Gamitodbc31672020-09-01 18:28:58 +000070 mLayerId = other->mLayerId;
Robert Carr5b3b9142021-02-22 12:27:32 -080071 mWidth = other->mWidth;
72 mHeight = other->mHeight;
Robert Carrc2cf02c2022-03-22 15:25:54 -070073 mFormat = other->mFormat;
Robert Carr5b3b9142021-02-22 12:27:32 -080074 mCreateFlags = other->mCreateFlags;
Robert Carrb89ea9d2018-12-10 13:01:14 -080075}
76
Mathias Agopiane3c697f2013-02-14 17:11:02 -080077SurfaceControl::~SurfaceControl()
78{
Robert Carr87246532019-02-04 15:20:26 -080079 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 // happen without delay, since these resources are quite heavy.
81 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082 mHandle.clear();
Robert Carr5b3b9142021-02-22 12:27:32 -080083 mBbq.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084 IPCThreadState::self()->flushCommands();
85}
86
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080087void SurfaceControl::disconnect() {
Robert Carr5b3b9142021-02-22 12:27:32 -080088 if (getIGraphicBufferProducer() != nullptr) {
89 getIGraphicBufferProducer()->disconnect(
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080090 BufferQueueCore::CURRENTLY_CONNECTED_API);
91 }
92}
93
Mathias Agopiane3c697f2013-02-14 17:11:02 -080094bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070095 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080096{
Yi Kong48a619f2018-06-05 16:34:59 -070097 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800100}
101
Svetoslavd85084b2014-03-20 10:28:31 -0700102status_t SurfaceControl::clearLayerFrameStats() const {
103 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700104 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700105 const sp<SurfaceComposerClient>& client(mClient);
106 return client->clearLayerFrameStats(mHandle);
107}
108
109status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
110 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700111 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700112 const sp<SurfaceComposerClient>& client(mClient);
113 return client->getLayerFrameStats(mHandle, outStats);
114}
115
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800116status_t SurfaceControl::validate() const
117{
Yi Kong48a619f2018-06-05 16:34:59 -0700118 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 ALOGE("invalid handle (%p) or client (%p)",
120 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800121 return NO_INIT;
122 }
123 return NO_ERROR;
124}
125
126status_t SurfaceControl::writeSurfaceToParcel(
127 const sp<SurfaceControl>& control, Parcel* parcel)
128{
129 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700130 if (control != nullptr) {
Robert Carr5b3b9142021-02-22 12:27:32 -0800131 bp = control->getIGraphicBufferProducer();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800132 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800133 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800134}
135
Robert Carr5b3b9142021-02-22 12:27:32 -0800136sp<Surface> SurfaceControl::generateSurfaceLocked()
Bryce Lee4e623e22017-06-16 07:06:17 -0700137{
Robert Carr5b3b9142021-02-22 12:27:32 -0800138 uint32_t ignore;
139 auto flags = mCreateFlags & (ISurfaceComposerClient::eCursorWindow |
140 ISurfaceComposerClient::eOpaque);
141 mBbqChild = mClient->createSurface(String8("bbq-wrapper"), 0, 0, mFormat,
142 flags, mHandle, {}, &ignore);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700143 mBbq = sp<BLASTBufferQueue>::make("bbq-adapter", mBbqChild, mWidth, mHeight, mFormat);
Robert Carr5b3b9142021-02-22 12:27:32 -0800144
Bryce Lee4e623e22017-06-16 07:06:17 -0700145 // This surface is always consumed by SurfaceFlinger, so the
146 // producerControlledByApp value doesn't matter; using false.
Robert Carr5b3b9142021-02-22 12:27:32 -0800147 mSurfaceData = mBbq->getSurface(true);
Bryce Lee4e623e22017-06-16 07:06:17 -0700148
149 return mSurfaceData;
150}
151
Robert Carr5b3b9142021-02-22 12:27:32 -0800152sp<Surface> SurfaceControl::getSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800153{
154 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700155 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700156 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800157 }
158 return mSurfaceData;
159}
160
Robert Carr5b3b9142021-02-22 12:27:32 -0800161sp<Surface> SurfaceControl::createSurface()
Bryce Lee4e623e22017-06-16 07:06:17 -0700162{
Robert Carr5b3b9142021-02-22 12:27:32 -0800163 return getSurface();
Bryce Lee4e623e22017-06-16 07:06:17 -0700164}
165
Robert Carr5b3b9142021-02-22 12:27:32 -0800166void SurfaceControl::updateDefaultBufferSize(uint32_t width, uint32_t height) {
167 Mutex::Autolock _l(mLock);
168 mWidth = width; mHeight = height;
169 if (mBbq) {
Vishnu Nairf9cb20a2021-05-10 16:27:51 -0700170 mBbq->update(mBbqChild, width, height, mFormat);
Robert Carr5b3b9142021-02-22 12:27:32 -0800171 }
172
173}
174
175sp<IBinder> SurfaceControl::getLayerStateHandle() const
Dan Stoza7dde5992015-05-22 09:51:44 -0700176{
Dan Stoza7dde5992015-05-22 09:51:44 -0700177 return mHandle;
178}
179
Robert Carr5b3b9142021-02-22 12:27:32 -0800180sp<IBinder> SurfaceControl::getHandle() const {
181 if (mBbqChild != nullptr) {
182 return mBbqChild->getHandle();
183 }
184 return getLayerStateHandle();
185}
186
Pablo Gamitodbc31672020-09-01 18:28:58 +0000187int32_t SurfaceControl::getLayerId() const {
188 return mLayerId;
189}
190
Robert Carr5b3b9142021-02-22 12:27:32 -0800191sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer()
Robert Carra35ef9f2019-01-25 14:29:21 -0800192{
Robert Carr5b3b9142021-02-22 12:27:32 -0800193 getSurface();
Robert Carra35ef9f2019-01-25 14:29:21 -0800194 Mutex::Autolock _l(mLock);
Robert Carr5b3b9142021-02-22 12:27:32 -0800195
196 return mBbq->getIGraphicBufferProducer();
Robert Carra35ef9f2019-01-25 14:29:21 -0800197}
198
Robert Carr4cdc58f2017-08-23 14:22:20 -0700199sp<SurfaceComposerClient> SurfaceControl::getClient() const
200{
201 return mClient;
202}
203
Valerie Hau1acd6962019-10-28 16:35:48 -0700204uint32_t SurfaceControl::getTransformHint() const {
205 Mutex::Autolock _l(mLock);
206 return mTransformHint;
207}
208
209void SurfaceControl::setTransformHint(uint32_t hint) {
210 Mutex::Autolock _l(mLock);
211 mTransformHint = hint;
212}
213
Pablo Gamito421dfd52020-09-22 18:11:45 +0000214status_t SurfaceControl::writeToParcel(Parcel& parcel) {
215 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
216 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000217 SAFE_PARCEL(parcel.writeInt32, mLayerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000218 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
Robert Carr5b3b9142021-02-22 12:27:32 -0800219 SAFE_PARCEL(parcel.writeUint32, mWidth);
220 SAFE_PARCEL(parcel.writeUint32, mHeight);
221 SAFE_PARCEL(parcel.writeUint32, mFormat);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000222
223 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100224}
225
Pablo Gamito421dfd52020-09-22 18:11:45 +0000226status_t SurfaceControl::readFromParcel(const Parcel& parcel,
227 sp<SurfaceControl>* outSurfaceControl) {
Garfield Tan2faa6312020-06-23 15:46:10 -0700228 sp<IBinder> client;
Garfield Tan2faa6312020-06-23 15:46:10 -0700229 sp<IBinder> handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000230 int32_t layerId;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000231 uint32_t transformHint;
Robert Carr5b3b9142021-02-22 12:27:32 -0800232 uint32_t width;
233 uint32_t height;
234 uint32_t format;
Garfield Tan2faa6312020-06-23 15:46:10 -0700235
Pablo Gamito421dfd52020-09-22 18:11:45 +0000236 SAFE_PARCEL(parcel.readStrongBinder, &client);
237 SAFE_PARCEL(parcel.readStrongBinder, &handle);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000238 SAFE_PARCEL(parcel.readInt32, &layerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000239 SAFE_PARCEL(parcel.readUint32, &transformHint);
Robert Carr5b3b9142021-02-22 12:27:32 -0800240 SAFE_PARCEL(parcel.readUint32, &width);
241 SAFE_PARCEL(parcel.readUint32, &height);
242 SAFE_PARCEL(parcel.readUint32, &format);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000243
Jorim Jaggi0b267102018-01-29 16:39:21 +0100244 // We aren't the original owner of the surface.
Pablo Gamito421dfd52020-09-22 18:11:45 +0000245 *outSurfaceControl =
246 new SurfaceControl(new SurfaceComposerClient(
247 interface_cast<ISurfaceComposerClient>(client)),
Robert Carr5b3b9142021-02-22 12:27:32 -0800248 handle.get(), nullptr, layerId,
249 width, height, format,
Robert Carr0e328f62020-02-06 17:12:08 -0800250 transformHint);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000251
252 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100253}
254
Pablo Gamito91512a02020-09-22 18:14:43 +0000255status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
256 sp<SurfaceControl>* outSurfaceControl) {
257 bool isNotNull;
258 SAFE_PARCEL(parcel.readBool, &isNotNull);
259 if (isNotNull) {
260 SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
261 }
262
263 return NO_ERROR;
264}
265
266status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
267 const sp<SurfaceControl>& surfaceControl) {
268 auto isNotNull = surfaceControl != nullptr;
269 SAFE_PARCEL(parcel.writeBool, isNotNull);
270 if (isNotNull) {
271 SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
272 }
273
274 return NO_ERROR;
275}
276
Robert Carr5b3b9142021-02-22 12:27:32 -0800277sp<SurfaceControl> SurfaceControl::getParentingLayer() {
278 if (mBbqChild != nullptr) {
279 return mBbqChild;
280 }
281 return this;
282}
283
John Reck57f748c2022-03-17 17:23:49 -0400284uint64_t SurfaceControl::resolveFrameNumber(const std::optional<uint64_t>& frameNumber) {
285 if (frameNumber.has_value()) {
286 auto ret = frameNumber.value();
287 // Set the fallback to something far enough ahead that in the unlikely event of mixed
288 // "real" frame numbers and fallback frame numbers, we still won't collide in any
289 // meaningful capacity
290 mFallbackFrameNumber = ret + 100;
291 return ret;
292 } else {
293 return mFallbackFrameNumber++;
294 }
295}
296
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800297// ----------------------------------------------------------------------------
298}; // namespace android