blob: f6a2b8fed6dbf28d93d2b1acb8bc58453d00f915 [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
Svetoslavd85084b2014-03-20 10:28:31 -0700100status_t SurfaceControl::clearLayerFrameStats() const {
101 status_t err = validate();
102 if (err < 0) return err;
103 const sp<SurfaceComposerClient>& client(mClient);
104 return client->clearLayerFrameStats(mHandle);
105}
106
107status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
108 status_t err = validate();
109 if (err < 0) return err;
110 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{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116 if (mHandle==0 || mClient==0) {
117 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;
128 if (control != NULL) {
129 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);
146 if (mSurfaceData == 0) {
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 Carr4cdc58f2017-08-23 14:22:20 -0700164sp<SurfaceComposerClient> SurfaceControl::getClient() const
165{
166 return mClient;
167}
168
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800169// ----------------------------------------------------------------------------
170}; // namespace android