blob: 1dcfe2e804457801dc6ee11db01cd524ab099358 [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>
42
43namespace android {
44
45// ============================================================================
46// SurfaceControl
47// ============================================================================
48
Valerie Hau1acd6962019-10-28 16:35:48 -070049SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
Pablo Gamitodbc31672020-09-01 18:28:58 +000050 const sp<IGraphicBufferProducer>& gbp, int32_t layerId,
Valerie Hau1acd6962019-10-28 16:35:48 -070051 uint32_t transform)
52 : mClient(client),
53 mHandle(handle),
54 mGraphicBufferProducer(gbp),
Pablo Gamitodbc31672020-09-01 18:28:58 +000055 mLayerId(layerId),
Valerie Hau1acd6962019-10-28 16:35:48 -070056 mTransformHint(transform) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070057
Robert Carrb89ea9d2018-12-10 13:01:14 -080058SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
59 mClient = other->mClient;
60 mHandle = other->mHandle;
61 mGraphicBufferProducer = other->mGraphicBufferProducer;
Valerie Hau1acd6962019-10-28 16:35:48 -070062 mTransformHint = other->mTransformHint;
Pablo Gamitodbc31672020-09-01 18:28:58 +000063 mLayerId = other->mLayerId;
Robert Carrb89ea9d2018-12-10 13:01:14 -080064}
65
Mathias Agopiane3c697f2013-02-14 17:11:02 -080066SurfaceControl::~SurfaceControl()
67{
Robert Carr87246532019-02-04 15:20:26 -080068 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080069 // happen without delay, since these resources are quite heavy.
70 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070071 mHandle.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080072 mGraphicBufferProducer.clear();
73 IPCThreadState::self()->flushCommands();
74}
75
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080076void SurfaceControl::disconnect() {
Yi Kong48a619f2018-06-05 16:34:59 -070077 if (mGraphicBufferProducer != nullptr) {
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080078 mGraphicBufferProducer->disconnect(
79 BufferQueueCore::CURRENTLY_CONNECTED_API);
80 }
81}
82
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070084 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080085{
Yi Kong48a619f2018-06-05 16:34:59 -070086 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080087 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070088 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080089}
90
Svetoslavd85084b2014-03-20 10:28:31 -070091status_t SurfaceControl::clearLayerFrameStats() const {
92 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -070093 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -070094 const sp<SurfaceComposerClient>& client(mClient);
95 return client->clearLayerFrameStats(mHandle);
96}
97
98status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
99 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700100 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700101 const sp<SurfaceComposerClient>& client(mClient);
102 return client->getLayerFrameStats(mHandle, outStats);
103}
104
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800105status_t SurfaceControl::validate() const
106{
Yi Kong48a619f2018-06-05 16:34:59 -0700107 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108 ALOGE("invalid handle (%p) or client (%p)",
109 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800110 return NO_INIT;
111 }
112 return NO_ERROR;
113}
114
115status_t SurfaceControl::writeSurfaceToParcel(
116 const sp<SurfaceControl>& control, Parcel* parcel)
117{
118 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700119 if (control != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800120 bp = control->mGraphicBufferProducer;
121 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800122 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800123}
124
Bryce Lee4e623e22017-06-16 07:06:17 -0700125sp<Surface> SurfaceControl::generateSurfaceLocked() const
126{
127 // This surface is always consumed by SurfaceFlinger, so the
128 // producerControlledByApp value doesn't matter; using false.
129 mSurfaceData = new Surface(mGraphicBufferProducer, false);
130
131 return mSurfaceData;
132}
133
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800134sp<Surface> SurfaceControl::getSurface() const
135{
136 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700137 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700138 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800139 }
140 return mSurfaceData;
141}
142
Bryce Lee4e623e22017-06-16 07:06:17 -0700143sp<Surface> SurfaceControl::createSurface() const
144{
145 Mutex::Autolock _l(mLock);
146 return generateSurfaceLocked();
147}
148
Dan Stoza7dde5992015-05-22 09:51:44 -0700149sp<IBinder> SurfaceControl::getHandle() const
150{
Dan Stoza7dde5992015-05-22 09:51:44 -0700151 return mHandle;
152}
153
Pablo Gamitodbc31672020-09-01 18:28:58 +0000154int32_t SurfaceControl::getLayerId() const {
155 return mLayerId;
156}
157
Robert Carra35ef9f2019-01-25 14:29:21 -0800158sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const
159{
160 Mutex::Autolock _l(mLock);
161 return mGraphicBufferProducer;
162}
163
Robert Carr4cdc58f2017-08-23 14:22:20 -0700164sp<SurfaceComposerClient> SurfaceControl::getClient() const
165{
166 return mClient;
167}
168
Valerie Hau1acd6962019-10-28 16:35:48 -0700169uint32_t SurfaceControl::getTransformHint() const {
170 Mutex::Autolock _l(mLock);
171 return mTransformHint;
172}
173
174void SurfaceControl::setTransformHint(uint32_t hint) {
175 Mutex::Autolock _l(mLock);
176 mTransformHint = hint;
177}
178
Pablo Gamito421dfd52020-09-22 18:11:45 +0000179status_t SurfaceControl::writeToParcel(Parcel& parcel) {
180 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
181 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
182 SAFE_PARCEL(parcel.writeStrongBinder, IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
Pablo Gamitodbc31672020-09-01 18:28:58 +0000183 SAFE_PARCEL(parcel.writeInt32, mLayerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000184 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
185
186 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100187}
188
Pablo Gamito421dfd52020-09-22 18:11:45 +0000189status_t SurfaceControl::readFromParcel(const Parcel& parcel,
190 sp<SurfaceControl>* outSurfaceControl) {
Garfield Tan2faa6312020-06-23 15:46:10 -0700191 sp<IBinder> client;
Garfield Tan2faa6312020-06-23 15:46:10 -0700192 sp<IBinder> handle;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100193 sp<IBinder> gbp;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000194 int32_t layerId;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000195 uint32_t transformHint;
Garfield Tan2faa6312020-06-23 15:46:10 -0700196
Pablo Gamito421dfd52020-09-22 18:11:45 +0000197 SAFE_PARCEL(parcel.readStrongBinder, &client);
198 SAFE_PARCEL(parcel.readStrongBinder, &handle);
199 SAFE_PARCEL(parcel.readNullableStrongBinder, &gbp);
Pablo Gamitodbc31672020-09-01 18:28:58 +0000200 SAFE_PARCEL(parcel.readInt32, &layerId);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000201 SAFE_PARCEL(parcel.readUint32, &transformHint);
202
Jorim Jaggi0b267102018-01-29 16:39:21 +0100203 // We aren't the original owner of the surface.
Pablo Gamito421dfd52020-09-22 18:11:45 +0000204 *outSurfaceControl =
205 new SurfaceControl(new SurfaceComposerClient(
206 interface_cast<ISurfaceComposerClient>(client)),
Pablo Gamitodbc31672020-09-01 18:28:58 +0000207 handle.get(), interface_cast<IGraphicBufferProducer>(gbp), layerId,
Robert Carr0e328f62020-02-06 17:12:08 -0800208 transformHint);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000209
210 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100211}
212
Pablo Gamito91512a02020-09-22 18:14:43 +0000213status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
214 sp<SurfaceControl>* outSurfaceControl) {
215 bool isNotNull;
216 SAFE_PARCEL(parcel.readBool, &isNotNull);
217 if (isNotNull) {
218 SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
219 }
220
221 return NO_ERROR;
222}
223
224status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
225 const sp<SurfaceControl>& surfaceControl) {
226 auto isNotNull = surfaceControl != nullptr;
227 SAFE_PARCEL(parcel.writeBool, isNotNull);
228 if (isNotNull) {
229 SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
230 }
231
232 return NO_ERROR;
233}
234
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800235// ----------------------------------------------------------------------------
236}; // namespace android