blob: 6292388ac3b22334ef86b265dcfb5c822a621164 [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,
49 const sp<IGraphicBufferProducer>& gbp, bool owned,
50 uint32_t transform)
51 : mClient(client),
52 mHandle(handle),
53 mGraphicBufferProducer(gbp),
54 mOwned(owned),
55 mTransformHint(transform) {}
Jesse Hall83cafff2013-09-16 15:24:53 -070056
Robert Carrb89ea9d2018-12-10 13:01:14 -080057SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
58 mClient = other->mClient;
59 mHandle = other->mHandle;
60 mGraphicBufferProducer = other->mGraphicBufferProducer;
61 mOwned = false;
Valerie Hau1acd6962019-10-28 16:35:48 -070062 mTransformHint = other->mTransformHint;
Robert Carrb89ea9d2018-12-10 13:01:14 -080063}
64
Mathias Agopiane3c697f2013-02-14 17:11:02 -080065SurfaceControl::~SurfaceControl()
66{
Robert Carr8b6b3212019-01-29 15:27:55 -080067 // Avoid reparenting the server-side surface to null if we are not the owner of it,
68 // meaning that we retrieved it from another process.
Vishnu Nairf03652d2019-07-16 17:56:56 -070069 if (mHandle != nullptr && mOwned) {
70 SurfaceComposerClient::doDropReferenceTransaction(mHandle);
Robert Carr6fb1a7e2018-12-11 12:07:25 -080071 }
Robert Carr87246532019-02-04 15:20:26 -080072 release();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080073}
74
Robert Carr87246532019-02-04 15:20:26 -080075void SurfaceControl::release()
76{
77 // Trigger an IPC now, to make sure things
Mathias Agopiane3c697f2013-02-14 17:11:02 -080078 // happen without delay, since these resources are quite heavy.
79 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080 mHandle.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080081 mGraphicBufferProducer.clear();
82 IPCThreadState::self()->flushCommands();
83}
84
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080085void SurfaceControl::disconnect() {
Yi Kong48a619f2018-06-05 16:34:59 -070086 if (mGraphicBufferProducer != nullptr) {
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080087 mGraphicBufferProducer->disconnect(
88 BufferQueueCore::CURRENTLY_CONNECTED_API);
89 }
90}
91
Mathias Agopiane3c697f2013-02-14 17:11:02 -080092bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -070093 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080094{
Yi Kong48a619f2018-06-05 16:34:59 -070095 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080096 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098}
99
Svetoslavd85084b2014-03-20 10:28:31 -0700100status_t SurfaceControl::clearLayerFrameStats() const {
101 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700102 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700103 const sp<SurfaceComposerClient>& client(mClient);
104 return client->clearLayerFrameStats(mHandle);
105}
106
107status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
108 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700109 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700110 const sp<SurfaceComposerClient>& client(mClient);
111 return client->getLayerFrameStats(mHandle, outStats);
112}
113
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800114status_t SurfaceControl::validate() const
115{
Yi Kong48a619f2018-06-05 16:34:59 -0700116 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117 ALOGE("invalid handle (%p) or client (%p)",
118 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800119 return NO_INIT;
120 }
121 return NO_ERROR;
122}
123
124status_t SurfaceControl::writeSurfaceToParcel(
125 const sp<SurfaceControl>& control, Parcel* parcel)
126{
127 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700128 if (control != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800129 bp = control->mGraphicBufferProducer;
130 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800131 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800132}
133
Bryce Lee4e623e22017-06-16 07:06:17 -0700134sp<Surface> SurfaceControl::generateSurfaceLocked() const
135{
136 // This surface is always consumed by SurfaceFlinger, so the
137 // producerControlledByApp value doesn't matter; using false.
138 mSurfaceData = new Surface(mGraphicBufferProducer, false);
139
140 return mSurfaceData;
141}
142
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800143sp<Surface> SurfaceControl::getSurface() const
144{
145 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700146 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700147 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800148 }
149 return mSurfaceData;
150}
151
Bryce Lee4e623e22017-06-16 07:06:17 -0700152sp<Surface> SurfaceControl::createSurface() const
153{
154 Mutex::Autolock _l(mLock);
155 return generateSurfaceLocked();
156}
157
Dan Stoza7dde5992015-05-22 09:51:44 -0700158sp<IBinder> SurfaceControl::getHandle() const
159{
160 Mutex::Autolock lock(mLock);
161 return mHandle;
162}
163
Robert Carra35ef9f2019-01-25 14:29:21 -0800164sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const
165{
166 Mutex::Autolock _l(mLock);
167 return mGraphicBufferProducer;
168}
169
Robert Carr4cdc58f2017-08-23 14:22:20 -0700170sp<SurfaceComposerClient> SurfaceControl::getClient() const
171{
172 return mClient;
173}
174
Valerie Hau1acd6962019-10-28 16:35:48 -0700175uint32_t SurfaceControl::getTransformHint() const {
176 Mutex::Autolock _l(mLock);
177 return mTransformHint;
178}
179
180void SurfaceControl::setTransformHint(uint32_t hint) {
181 Mutex::Autolock _l(mLock);
182 mTransformHint = hint;
183}
184
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100185void SurfaceControl::writeToParcel(Parcel* parcel)
186{
187 parcel->writeStrongBinder(ISurfaceComposerClient::asBinder(mClient->getClient()));
188 parcel->writeStrongBinder(mHandle);
189 parcel->writeStrongBinder(IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
Valerie Hau1acd6962019-10-28 16:35:48 -0700190 parcel->writeUint32(mTransformHint);
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100191}
192
Vishnu Nair621102e2019-06-12 14:16:57 -0700193sp<SurfaceControl> SurfaceControl::readFromParcel(const Parcel* parcel) {
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100194 sp<IBinder> client = parcel->readStrongBinder();
195 sp<IBinder> handle = parcel->readStrongBinder();
196 if (client == nullptr || handle == nullptr)
197 {
198 ALOGE("Invalid parcel");
199 return nullptr;
200 }
201 sp<IBinder> gbp;
202 parcel->readNullableStrongBinder(&gbp);
Jorim Jaggi0b267102018-01-29 16:39:21 +0100203
Valerie Hau1acd6962019-10-28 16:35:48 -0700204 uint32_t transformHint = parcel->readUint32();
Jorim Jaggi0b267102018-01-29 16:39:21 +0100205 // We aren't the original owner of the surface.
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100206 return new SurfaceControl(new SurfaceComposerClient(
Valerie Hau1acd6962019-10-28 16:35:48 -0700207 interface_cast<ISurfaceComposerClient>(client)),
208 handle.get(), interface_cast<IGraphicBufferProducer>(gbp),
209 false /* owned */, transformHint);
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100210}
211
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800212// ----------------------------------------------------------------------------
213}; // namespace android