blob: 7e2f8f9d36b14bafa6d733d7d7ad88c6815fdef5 [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>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <gui/ISurfaceComposer.h>
39#include <gui/Surface.h>
40#include <gui/SurfaceComposerClient.h>
41#include <gui/SurfaceControl.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010042#include <private/gui/ParcelUtils.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043
44namespace android {
45
46// ============================================================================
47// SurfaceControl
48// ============================================================================
49
Valerie Hau1acd6962019-10-28 16:35:48 -070050SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
Pablo Gamitodbc31672020-09-01 18:28:58 +000051 const sp<IGraphicBufferProducer>& gbp, int32_t layerId,
Valerie Hau1acd6962019-10-28 16:35:48 -070052 uint32_t transform)
53 : mClient(client),
54 mHandle(handle),
55 mGraphicBufferProducer(gbp),
Pablo Gamitodbc31672020-09-01 18:28:58 +000056 mLayerId(layerId),
Valerie Hau1acd6962019-10-28 16:35:48 -070057 mTransformHint(transform) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070058
Robert Carrb89ea9d2018-12-10 13:01:14 -080059SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
60 mClient = other->mClient;
61 mHandle = other->mHandle;
62 mGraphicBufferProducer = other->mGraphicBufferProducer;
Valerie Hau1acd6962019-10-28 16:35:48 -070063 mTransformHint = other->mTransformHint;
Pablo Gamitodbc31672020-09-01 18:28:58 +000064 mLayerId = other->mLayerId;
Robert Carrb89ea9d2018-12-10 13:01:14 -080065}
66
Mathias Agopiane3c697f2013-02-14 17:11:02 -080067SurfaceControl::~SurfaceControl()
68{
Robert Carr87246532019-02-04 15:20:26 -080069 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080070 // happen without delay, since these resources are quite heavy.
71 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070072 mHandle.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080073 mGraphicBufferProducer.clear();
74 IPCThreadState::self()->flushCommands();
75}
76
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080077void SurfaceControl::disconnect() {
Yi Kong48a619f2018-06-05 16:34:59 -070078 if (mGraphicBufferProducer != nullptr) {
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080079 mGraphicBufferProducer->disconnect(
80 BufferQueueCore::CURRENTLY_CONNECTED_API);
81 }
82}
83
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070085 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080086{
Yi Kong48a619f2018-06-05 16:34:59 -070087 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080088 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080090}
91
Svetoslavd85084b2014-03-20 10:28:31 -070092status_t SurfaceControl::clearLayerFrameStats() const {
93 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -070094 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -070095 const sp<SurfaceComposerClient>& client(mClient);
96 return client->clearLayerFrameStats(mHandle);
97}
98
99status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) 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->getLayerFrameStats(mHandle, outStats);
104}
105
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800106status_t SurfaceControl::validate() const
107{
Yi Kong48a619f2018-06-05 16:34:59 -0700108 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109 ALOGE("invalid handle (%p) or client (%p)",
110 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800111 return NO_INIT;
112 }
113 return NO_ERROR;
114}
115
116status_t SurfaceControl::writeSurfaceToParcel(
117 const sp<SurfaceControl>& control, Parcel* parcel)
118{
119 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700120 if (control != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800121 bp = control->mGraphicBufferProducer;
122 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800123 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800124}
125
Bryce Lee4e623e22017-06-16 07:06:17 -0700126sp<Surface> SurfaceControl::generateSurfaceLocked() const
127{
128 // This surface is always consumed by SurfaceFlinger, so the
129 // producerControlledByApp value doesn't matter; using false.
130 mSurfaceData = new Surface(mGraphicBufferProducer, false);
131
132 return mSurfaceData;
133}
134
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800135sp<Surface> SurfaceControl::getSurface() const
136{
137 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700138 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700139 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800140 }
141 return mSurfaceData;
142}
143
Bryce Lee4e623e22017-06-16 07:06:17 -0700144sp<Surface> SurfaceControl::createSurface() const
145{
146 Mutex::Autolock _l(mLock);
147 return generateSurfaceLocked();
148}
149
Dan Stoza7dde5992015-05-22 09:51:44 -0700150sp<IBinder> SurfaceControl::getHandle() const
151{
Dan Stoza7dde5992015-05-22 09:51:44 -0700152 return mHandle;
153}
154
Pablo Gamitodbc31672020-09-01 18:28:58 +0000155int32_t SurfaceControl::getLayerId() const {
156 return mLayerId;
157}
158
Robert Carra35ef9f2019-01-25 14:29:21 -0800159sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const
160{
161 Mutex::Autolock _l(mLock);
162 return mGraphicBufferProducer;
163}
164
Robert Carr4cdc58f2017-08-23 14:22:20 -0700165sp<SurfaceComposerClient> SurfaceControl::getClient() const
166{
167 return mClient;
168}
169
Valerie Hau1acd6962019-10-28 16:35:48 -0700170uint32_t SurfaceControl::getTransformHint() const {
171 Mutex::Autolock _l(mLock);
172 return mTransformHint;
173}
174
175void SurfaceControl::setTransformHint(uint32_t hint) {
176 Mutex::Autolock _l(mLock);
177 mTransformHint = hint;
178}
179
Pablo Gamito421dfd52020-09-22 18:11:45 +0000180status_t SurfaceControl::writeToParcel(Parcel& parcel) {
181 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
182 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
183 SAFE_PARCEL(parcel.writeStrongBinder, IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
Pablo Gamitodbc31672020-09-01 18:28:58 +0000184 SAFE_PARCEL(parcel.writeInt32, mLayerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000185 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
186
187 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100188}
189
Pablo Gamito421dfd52020-09-22 18:11:45 +0000190status_t SurfaceControl::readFromParcel(const Parcel& parcel,
191 sp<SurfaceControl>* outSurfaceControl) {
Garfield Tan2faa6312020-06-23 15:46:10 -0700192 sp<IBinder> client;
Garfield Tan2faa6312020-06-23 15:46:10 -0700193 sp<IBinder> handle;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100194 sp<IBinder> gbp;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000195 int32_t layerId;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000196 uint32_t transformHint;
Garfield Tan2faa6312020-06-23 15:46:10 -0700197
Pablo Gamito421dfd52020-09-22 18:11:45 +0000198 SAFE_PARCEL(parcel.readStrongBinder, &client);
199 SAFE_PARCEL(parcel.readStrongBinder, &handle);
200 SAFE_PARCEL(parcel.readNullableStrongBinder, &gbp);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000201 SAFE_PARCEL(parcel.readInt32, &layerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000202 SAFE_PARCEL(parcel.readUint32, &transformHint);
203
Jorim Jaggi0b267102018-01-29 16:39:21 +0100204 // We aren't the original owner of the surface.
Pablo Gamito421dfd52020-09-22 18:11:45 +0000205 *outSurfaceControl =
206 new SurfaceControl(new SurfaceComposerClient(
207 interface_cast<ISurfaceComposerClient>(client)),
Pablo Gamitodbc31672020-09-01 18:28:58 +0000208 handle.get(), interface_cast<IGraphicBufferProducer>(gbp), layerId,
Robert Carr0e328f62020-02-06 17:12:08 -0800209 transformHint);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000210
211 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100212}
213
Pablo Gamito91512a02020-09-22 18:14:43 +0000214status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
215 sp<SurfaceControl>* outSurfaceControl) {
216 bool isNotNull;
217 SAFE_PARCEL(parcel.readBool, &isNotNull);
218 if (isNotNull) {
219 SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
220 }
221
222 return NO_ERROR;
223}
224
225status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
226 const sp<SurfaceControl>& surfaceControl) {
227 auto isNotNull = surfaceControl != nullptr;
228 SAFE_PARCEL(parcel.writeBool, isNotNull);
229 if (isNotNull) {
230 SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
231 }
232
233 return NO_ERROR;
234}
235
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800236// ----------------------------------------------------------------------------
237}; // namespace android