blob: 6e153d9acc493cf3db1e4181ee454a16944d8118 [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>
27#include <utils/Log.h>
28#include <utils/threads.h>
29
30#include <binder/IPCThreadState.h>
31
32#include <ui/DisplayInfo.h>
33#include <ui/GraphicBuffer.h>
34#include <ui/Rect.h>
35
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080036#include <gui/BufferQueueCore.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080037#include <gui/ISurfaceComposer.h>
38#include <gui/Surface.h>
39#include <gui/SurfaceComposerClient.h>
40#include <gui/SurfaceControl.h>
41
42namespace android {
43
44// ============================================================================
45// SurfaceControl
46// ============================================================================
47
Valerie Hau1acd6962019-10-28 16:35:48 -070048SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
Robert Carr0e328f62020-02-06 17:12:08 -080049 const sp<IGraphicBufferProducer>& gbp,
Valerie Hau1acd6962019-10-28 16:35:48 -070050 uint32_t transform)
51 : mClient(client),
52 mHandle(handle),
53 mGraphicBufferProducer(gbp),
Valerie Hau1acd6962019-10-28 16:35:48 -070054 mTransformHint(transform) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070055
Robert Carrb89ea9d2018-12-10 13:01:14 -080056SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
57 mClient = other->mClient;
58 mHandle = other->mHandle;
59 mGraphicBufferProducer = other->mGraphicBufferProducer;
Valerie Hau1acd6962019-10-28 16:35:48 -070060 mTransformHint = other->mTransformHint;
Robert Carrb89ea9d2018-12-10 13:01:14 -080061}
62
Mathias Agopiane3c697f2013-02-14 17:11:02 -080063SurfaceControl::~SurfaceControl()
64{
Robert Carr87246532019-02-04 15:20:26 -080065 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080066 // happen without delay, since these resources are quite heavy.
67 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070068 mHandle.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080069 mGraphicBufferProducer.clear();
70 IPCThreadState::self()->flushCommands();
71}
72
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080073void SurfaceControl::disconnect() {
Yi Kong48a619f2018-06-05 16:34:59 -070074 if (mGraphicBufferProducer != nullptr) {
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080075 mGraphicBufferProducer->disconnect(
76 BufferQueueCore::CURRENTLY_CONNECTED_API);
77 }
78}
79
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070081 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080082{
Yi Kong48a619f2018-06-05 16:34:59 -070083 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080086}
87
Svetoslavd85084b2014-03-20 10:28:31 -070088status_t SurfaceControl::clearLayerFrameStats() const {
89 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -070090 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -070091 const sp<SurfaceComposerClient>& client(mClient);
92 return client->clearLayerFrameStats(mHandle);
93}
94
95status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
96 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -070097 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -070098 const sp<SurfaceComposerClient>& client(mClient);
99 return client->getLayerFrameStats(mHandle, outStats);
100}
101
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800102status_t SurfaceControl::validate() const
103{
Yi Kong48a619f2018-06-05 16:34:59 -0700104 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105 ALOGE("invalid handle (%p) or client (%p)",
106 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800107 return NO_INIT;
108 }
109 return NO_ERROR;
110}
111
112status_t SurfaceControl::writeSurfaceToParcel(
113 const sp<SurfaceControl>& control, Parcel* parcel)
114{
115 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700116 if (control != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800117 bp = control->mGraphicBufferProducer;
118 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800119 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800120}
121
Bryce Lee4e623e22017-06-16 07:06:17 -0700122sp<Surface> SurfaceControl::generateSurfaceLocked() const
123{
124 // This surface is always consumed by SurfaceFlinger, so the
125 // producerControlledByApp value doesn't matter; using false.
126 mSurfaceData = new Surface(mGraphicBufferProducer, false);
127
128 return mSurfaceData;
129}
130
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800131sp<Surface> SurfaceControl::getSurface() const
132{
133 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700134 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700135 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800136 }
137 return mSurfaceData;
138}
139
Bryce Lee4e623e22017-06-16 07:06:17 -0700140sp<Surface> SurfaceControl::createSurface() const
141{
142 Mutex::Autolock _l(mLock);
143 return generateSurfaceLocked();
144}
145
Dan Stoza7dde5992015-05-22 09:51:44 -0700146sp<IBinder> SurfaceControl::getHandle() const
147{
Dan Stoza7dde5992015-05-22 09:51:44 -0700148 return mHandle;
149}
150
Robert Carra35ef9f2019-01-25 14:29:21 -0800151sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const
152{
153 Mutex::Autolock _l(mLock);
154 return mGraphicBufferProducer;
155}
156
Robert Carr4cdc58f2017-08-23 14:22:20 -0700157sp<SurfaceComposerClient> SurfaceControl::getClient() const
158{
159 return mClient;
160}
161
Valerie Hau1acd6962019-10-28 16:35:48 -0700162uint32_t SurfaceControl::getTransformHint() const {
163 Mutex::Autolock _l(mLock);
164 return mTransformHint;
165}
166
167void SurfaceControl::setTransformHint(uint32_t hint) {
168 Mutex::Autolock _l(mLock);
169 mTransformHint = hint;
170}
171
Pablo Gamito421dfd52020-09-22 18:11:45 +0000172status_t SurfaceControl::writeToParcel(Parcel& parcel) {
173 SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient()));
174 SAFE_PARCEL(parcel.writeStrongBinder, mHandle);
175 SAFE_PARCEL(parcel.writeStrongBinder, IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
176 SAFE_PARCEL(parcel.writeUint32, mTransformHint);
177
178 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100179}
180
Pablo Gamito421dfd52020-09-22 18:11:45 +0000181status_t SurfaceControl::readFromParcel(const Parcel& parcel,
182 sp<SurfaceControl>* outSurfaceControl) {
Garfield Tan2faa6312020-06-23 15:46:10 -0700183 sp<IBinder> client;
Garfield Tan2faa6312020-06-23 15:46:10 -0700184 sp<IBinder> handle;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100185 sp<IBinder> gbp;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000186 uint32_t transformHint;
Garfield Tan2faa6312020-06-23 15:46:10 -0700187
Pablo Gamito421dfd52020-09-22 18:11:45 +0000188 SAFE_PARCEL(parcel.readStrongBinder, &client);
189 SAFE_PARCEL(parcel.readStrongBinder, &handle);
190 SAFE_PARCEL(parcel.readNullableStrongBinder, &gbp);
191 SAFE_PARCEL(parcel.readUint32, &transformHint);
192
Jorim Jaggi0b267102018-01-29 16:39:21 +0100193 // We aren't the original owner of the surface.
Pablo Gamito421dfd52020-09-22 18:11:45 +0000194 *outSurfaceControl =
195 new SurfaceControl(new SurfaceComposerClient(
196 interface_cast<ISurfaceComposerClient>(client)),
197 handle.get(), interface_cast<IGraphicBufferProducer>(gbp),
Robert Carr0e328f62020-02-06 17:12:08 -0800198 transformHint);
Pablo Gamito421dfd52020-09-22 18:11:45 +0000199
200 return NO_ERROR;
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100201}
202
Pablo Gamito91512a02020-09-22 18:14:43 +0000203status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel,
204 sp<SurfaceControl>* outSurfaceControl) {
205 bool isNotNull;
206 SAFE_PARCEL(parcel.readBool, &isNotNull);
207 if (isNotNull) {
208 SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl);
209 }
210
211 return NO_ERROR;
212}
213
214status_t SurfaceControl::writeNullableToParcel(Parcel& parcel,
215 const sp<SurfaceControl>& surfaceControl) {
216 auto isNotNull = surfaceControl != nullptr;
217 SAFE_PARCEL(parcel.writeBool, isNotNull);
218 if (isNotNull) {
219 SAFE_PARCEL(surfaceControl->writeToParcel, parcel);
220 }
221
222 return NO_ERROR;
223}
224
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800225// ----------------------------------------------------------------------------
226}; // namespace android