blob: 008f520bb0dc8a8682bb421e1903d931377ad6bf [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
48SurfaceControl::SurfaceControl(
Jesse Hall83cafff2013-09-16 15:24:53 -070049 const sp<SurfaceComposerClient>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -070050 const sp<IBinder>& handle,
Jorim Jaggi0b267102018-01-29 16:39:21 +010051 const sp<IGraphicBufferProducer>& gbp,
52 bool owned)
53 : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp), mOwned(owned)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080054{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080055}
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;
62}
63
Mathias Agopiane3c697f2013-02-14 17:11:02 -080064SurfaceControl::~SurfaceControl()
65{
Robert Carr6fb1a7e2018-12-11 12:07:25 -080066 if (mClient != nullptr && mHandle != nullptr && mOwned) {
67 SurfaceComposerClient::doDropReferenceTransaction(mHandle, mClient->getClient());
68 }
69 mClient.clear();
70 mHandle.clear();
71 mGraphicBufferProducer.clear();
72 IPCThreadState::self()->flushCommands();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080073}
74
75void SurfaceControl::destroy()
76{
Jorim Jaggi0b267102018-01-29 16:39:21 +010077 // Avoid destroying the server-side surface if we are not the owner of it, meaning that we
78 // retrieved it from another process.
79 if (isValid() && mOwned) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -080080 SurfaceComposerClient::Transaction().reparent(this, nullptr).apply();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080081 }
82 // clear all references and trigger an IPC now, to make sure things
83 // happen without delay, since these resources are quite heavy.
84 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085 mHandle.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080086 mGraphicBufferProducer.clear();
87 IPCThreadState::self()->flushCommands();
88}
89
Jesse Hall83cafff2013-09-16 15:24:53 -070090void SurfaceControl::clear()
Mathias Agopiane3c697f2013-02-14 17:11:02 -080091{
92 // here, the window manager tells us explicitly that we should destroy
93 // the surface's resource. Soon after this call, it will also release
94 // its last reference (which will call the dtor); however, it is possible
95 // that a client living in the same process still holds references which
96 // would delay the call to the dtor -- that is why we need this explicit
97 // "clear()" call.
98 destroy();
99}
100
Chong Zhang1b3a9ac2016-02-29 16:47:47 -0800101void SurfaceControl::disconnect() {
Yi Kong48a619f2018-06-05 16:34:59 -0700102 if (mGraphicBufferProducer != nullptr) {
Chong Zhang1b3a9ac2016-02-29 16:47:47 -0800103 mGraphicBufferProducer->disconnect(
104 BufferQueueCore::CURRENTLY_CONNECTED_API);
105 }
106}
107
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800108bool SurfaceControl::isSameSurface(
Jesse Hall83cafff2013-09-16 15:24:53 -0700109 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800110{
Yi Kong48a619f2018-06-05 16:34:59 -0700111 if (lhs == nullptr || rhs == nullptr)
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800112 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800114}
115
Svetoslavd85084b2014-03-20 10:28:31 -0700116status_t SurfaceControl::clearLayerFrameStats() const {
117 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700118 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700119 const sp<SurfaceComposerClient>& client(mClient);
120 return client->clearLayerFrameStats(mHandle);
121}
122
123status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
124 status_t err = validate();
Aleks Rozman72741732018-08-04 22:15:13 -0700125 if (err != NO_ERROR) return err;
Svetoslavd85084b2014-03-20 10:28:31 -0700126 const sp<SurfaceComposerClient>& client(mClient);
127 return client->getLayerFrameStats(mHandle, outStats);
128}
129
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800130status_t SurfaceControl::validate() const
131{
Yi Kong48a619f2018-06-05 16:34:59 -0700132 if (mHandle==nullptr || mClient==nullptr) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700133 ALOGE("invalid handle (%p) or client (%p)",
134 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800135 return NO_INIT;
136 }
137 return NO_ERROR;
138}
139
140status_t SurfaceControl::writeSurfaceToParcel(
141 const sp<SurfaceControl>& control, Parcel* parcel)
142{
143 sp<IGraphicBufferProducer> bp;
Yi Kong48a619f2018-06-05 16:34:59 -0700144 if (control != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800145 bp = control->mGraphicBufferProducer;
146 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800147 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800148}
149
Bryce Lee4e623e22017-06-16 07:06:17 -0700150sp<Surface> SurfaceControl::generateSurfaceLocked() const
151{
152 // This surface is always consumed by SurfaceFlinger, so the
153 // producerControlledByApp value doesn't matter; using false.
154 mSurfaceData = new Surface(mGraphicBufferProducer, false);
155
156 return mSurfaceData;
157}
158
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800159sp<Surface> SurfaceControl::getSurface() const
160{
161 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700162 if (mSurfaceData == nullptr) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700163 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800164 }
165 return mSurfaceData;
166}
167
Bryce Lee4e623e22017-06-16 07:06:17 -0700168sp<Surface> SurfaceControl::createSurface() const
169{
170 Mutex::Autolock _l(mLock);
171 return generateSurfaceLocked();
172}
173
Dan Stoza7dde5992015-05-22 09:51:44 -0700174sp<IBinder> SurfaceControl::getHandle() const
175{
176 Mutex::Autolock lock(mLock);
177 return mHandle;
178}
179
Robert Carra35ef9f2019-01-25 14:29:21 -0800180sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const
181{
182 Mutex::Autolock _l(mLock);
183 return mGraphicBufferProducer;
184}
185
Robert Carr4cdc58f2017-08-23 14:22:20 -0700186sp<SurfaceComposerClient> SurfaceControl::getClient() const
187{
188 return mClient;
189}
190
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100191void SurfaceControl::writeToParcel(Parcel* parcel)
192{
193 parcel->writeStrongBinder(ISurfaceComposerClient::asBinder(mClient->getClient()));
194 parcel->writeStrongBinder(mHandle);
195 parcel->writeStrongBinder(IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
196}
197
198sp<SurfaceControl> SurfaceControl::readFromParcel(Parcel* parcel)
199{
200 sp<IBinder> client = parcel->readStrongBinder();
201 sp<IBinder> handle = parcel->readStrongBinder();
202 if (client == nullptr || handle == nullptr)
203 {
204 ALOGE("Invalid parcel");
205 return nullptr;
206 }
207 sp<IBinder> gbp;
208 parcel->readNullableStrongBinder(&gbp);
Jorim Jaggi0b267102018-01-29 16:39:21 +0100209
210 // We aren't the original owner of the surface.
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100211 return new SurfaceControl(new SurfaceComposerClient(
212 interface_cast<ISurfaceComposerClient>(client)),
Jorim Jaggi0b267102018-01-29 16:39:21 +0100213 handle.get(), interface_cast<IGraphicBufferProducer>(gbp), false /* owned */);
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +0100214}
215
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800216// ----------------------------------------------------------------------------
217}; // namespace android