blob: 9e1d7b6647235bca04af5d6c71275310d26e89d8 [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,
51 const sp<IGraphicBufferProducer>& gbp)
52 : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp)
Mathias Agopiane3c697f2013-02-14 17:11:02 -080053{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080054}
Jesse Hall83cafff2013-09-16 15:24:53 -070055
Mathias Agopiane3c697f2013-02-14 17:11:02 -080056SurfaceControl::~SurfaceControl()
57{
58 destroy();
59}
60
61void SurfaceControl::destroy()
62{
63 if (isValid()) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070064 mClient->destroySurface(mHandle);
Mathias Agopiane3c697f2013-02-14 17:11:02 -080065 }
66 // clear all references and trigger an IPC now, to make sure things
67 // happen without delay, since these resources are quite heavy.
68 mClient.clear();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070069 mHandle.clear();
Mathias Agopiane3c697f2013-02-14 17:11:02 -080070 mGraphicBufferProducer.clear();
71 IPCThreadState::self()->flushCommands();
72}
73
Jesse Hall83cafff2013-09-16 15:24:53 -070074void SurfaceControl::clear()
Mathias Agopiane3c697f2013-02-14 17:11:02 -080075{
76 // here, the window manager tells us explicitly that we should destroy
77 // the surface's resource. Soon after this call, it will also release
78 // its last reference (which will call the dtor); however, it is possible
79 // that a client living in the same process still holds references which
80 // would delay the call to the dtor -- that is why we need this explicit
81 // "clear()" call.
82 destroy();
83}
84
Chong Zhang1b3a9ac2016-02-29 16:47:47 -080085void SurfaceControl::disconnect() {
86 if (mGraphicBufferProducer != NULL) {
87 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{
95 if (lhs == 0 || rhs == 0)
96 return false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097 return lhs->mHandle == rhs->mHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098}
99
Dan Stozad723bd72014-11-18 10:24:03 -0800100status_t SurfaceControl::setLayerStack(uint32_t layerStack) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800101 status_t err = validate();
102 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700103 return mClient->setLayerStack(mHandle, layerStack);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800104}
Robert Carrdb66e622017-04-10 16:55:57 -0700105
Robert Carrae060832016-11-28 10:51:00 -0800106status_t SurfaceControl::setLayer(int32_t layer) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800107 status_t err = validate();
108 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700109 return mClient->setLayer(mHandle, layer);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800110}
Robert Carrdb66e622017-04-10 16:55:57 -0700111
112status_t SurfaceControl::setRelativeLayer(const sp<IBinder>& relativeTo, int32_t layer) {
113 status_t err = validate();
114 if (err < 0) return err;
115 return mClient->setRelativeLayer(mHandle, relativeTo, layer);
116}
117
Ramanan Rajeswarand6480c02013-03-21 15:49:59 +0000118status_t SurfaceControl::setPosition(float x, float y) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800119 status_t err = validate();
120 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700121 return mClient->setPosition(mHandle, x, y);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800122}
Robert Carr99e27f02016-06-16 15:18:02 -0700123status_t SurfaceControl::setGeometryAppliesWithResize() {
Robert Carr82364e32016-05-15 11:27:47 -0700124 status_t err = validate();
125 if (err < 0) return err;
Robert Carr99e27f02016-06-16 15:18:02 -0700126 return mClient->setGeometryAppliesWithResize(mHandle);
Robert Carr82364e32016-05-15 11:27:47 -0700127}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800128status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
129 status_t err = validate();
130 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700131 return mClient->setSize(mHandle, w, h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800132}
133status_t SurfaceControl::hide() {
134 status_t err = validate();
135 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700136 return mClient->hide(mHandle);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800137}
138status_t SurfaceControl::show() {
139 status_t err = validate();
140 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700141 return mClient->show(mHandle);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800142}
143status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
144 status_t err = validate();
145 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700146 return mClient->setFlags(mHandle, flags, mask);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800147}
148status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
149 status_t err = validate();
150 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700151 return mClient->setTransparentRegionHint(mHandle, transparent);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800152}
153status_t SurfaceControl::setAlpha(float alpha) {
154 status_t err = validate();
155 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700156 return mClient->setAlpha(mHandle, alpha);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800157}
chaviw13fdc492017-06-27 12:40:18 -0700158status_t SurfaceControl::setColor(const half3& color) {
159 status_t err = validate();
160 if (err < 0) return err;
161 return mClient->setColor(mHandle, color);
162}
Robert Carrcb6e1e32017-02-21 19:48:26 -0800163status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800164 status_t err = validate();
165 if (err < 0) return err;
Robert Carrcb6e1e32017-02-21 19:48:26 -0800166 return mClient->setMatrix(mHandle, dsdx, dtdx, dtdy, dsdy);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800167}
168status_t SurfaceControl::setCrop(const Rect& crop) {
169 status_t err = validate();
170 if (err < 0) return err;
Dan Stoza3b3ba782014-04-10 13:50:03 -0700171 return mClient->setCrop(mHandle, crop);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800172}
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000173status_t SurfaceControl::setFinalCrop(const Rect& crop) {
174 status_t err = validate();
175 if (err < 0) return err;
176 return mClient->setFinalCrop(mHandle, crop);
177}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800178
Robert Carr1db73f62016-12-21 12:58:51 -0800179status_t SurfaceControl::deferTransactionUntil(const sp<IBinder>& handle,
Dan Stoza7dde5992015-05-22 09:51:44 -0700180 uint64_t frameNumber) {
181 status_t err = validate();
182 if (err < 0) return err;
183 return mClient->deferTransactionUntil(mHandle, handle, frameNumber);
184}
185
Robert Carr0d480722017-01-10 16:42:54 -0800186status_t SurfaceControl::deferTransactionUntil(const sp<Surface>& handle,
187 uint64_t frameNumber) {
188 status_t err = validate();
189 if (err < 0) return err;
190 return mClient->deferTransactionUntil(mHandle, handle, frameNumber);
191}
192
Robert Carr1db73f62016-12-21 12:58:51 -0800193status_t SurfaceControl::reparentChildren(const sp<IBinder>& newParentHandle) {
194 status_t err = validate();
195 if (err < 0) return err;
196 return mClient->reparentChildren(mHandle, newParentHandle);
197}
198
chaviwf1961f72017-09-18 16:41:07 -0700199status_t SurfaceControl::reparent(const sp<IBinder>& newParentHandle) {
chaviw06178942017-07-27 10:25:59 -0700200 status_t err = validate();
201 if (err < 0) return err;
chaviwf1961f72017-09-18 16:41:07 -0700202 return mClient->reparent(mHandle, newParentHandle);
chaviw06178942017-07-27 10:25:59 -0700203}
204
Robert Carr9524cb32017-02-13 11:32:32 -0800205status_t SurfaceControl::detachChildren() {
206 status_t err = validate();
207 if (err < 0) return err;
208 return mClient->detachChildren(mHandle);
209}
210
Robert Carrc3574f72016-03-24 12:19:32 -0700211status_t SurfaceControl::setOverrideScalingMode(int32_t overrideScalingMode) {
212 status_t err = validate();
213 if (err < 0) return err;
214 return mClient->setOverrideScalingMode(mHandle, overrideScalingMode);
215}
216
Svetoslavd85084b2014-03-20 10:28:31 -0700217status_t SurfaceControl::clearLayerFrameStats() const {
218 status_t err = validate();
219 if (err < 0) return err;
220 const sp<SurfaceComposerClient>& client(mClient);
221 return client->clearLayerFrameStats(mHandle);
222}
223
224status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
225 status_t err = validate();
226 if (err < 0) return err;
227 const sp<SurfaceComposerClient>& client(mClient);
228 return client->getLayerFrameStats(mHandle, outStats);
229}
230
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800231status_t SurfaceControl::validate() const
232{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700233 if (mHandle==0 || mClient==0) {
234 ALOGE("invalid handle (%p) or client (%p)",
235 mHandle.get(), mClient.get());
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800236 return NO_INIT;
237 }
238 return NO_ERROR;
239}
240
241status_t SurfaceControl::writeSurfaceToParcel(
242 const sp<SurfaceControl>& control, Parcel* parcel)
243{
244 sp<IGraphicBufferProducer> bp;
245 if (control != NULL) {
246 bp = control->mGraphicBufferProducer;
247 }
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800248 return parcel->writeStrongBinder(IInterface::asBinder(bp));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800249}
250
Bryce Lee4e623e22017-06-16 07:06:17 -0700251sp<Surface> SurfaceControl::generateSurfaceLocked() const
252{
253 // This surface is always consumed by SurfaceFlinger, so the
254 // producerControlledByApp value doesn't matter; using false.
255 mSurfaceData = new Surface(mGraphicBufferProducer, false);
256
257 return mSurfaceData;
258}
259
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800260sp<Surface> SurfaceControl::getSurface() const
261{
262 Mutex::Autolock _l(mLock);
263 if (mSurfaceData == 0) {
Bryce Lee4e623e22017-06-16 07:06:17 -0700264 return generateSurfaceLocked();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800265 }
266 return mSurfaceData;
267}
268
Bryce Lee4e623e22017-06-16 07:06:17 -0700269sp<Surface> SurfaceControl::createSurface() const
270{
271 Mutex::Autolock _l(mLock);
272 return generateSurfaceLocked();
273}
274
Dan Stoza7dde5992015-05-22 09:51:44 -0700275sp<IBinder> SurfaceControl::getHandle() const
276{
277 Mutex::Autolock lock(mLock);
278 return mHandle;
279}
280
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800281// ----------------------------------------------------------------------------
282}; // namespace android