1 /*
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 
26 #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 
36 #include <gui/BufferQueueCore.h>
37 #include <gui/ISurfaceComposer.h>
38 #include <gui/Surface.h>
39 #include <gui/SurfaceComposerClient.h>
40 #include <gui/SurfaceControl.h>
41 
42 namespace android {
43 
44 // ============================================================================
45 //  SurfaceControl
46 // ============================================================================
47 
SurfaceControl(const sp<SurfaceComposerClient> & client,const sp<IBinder> & handle,const sp<IGraphicBufferProducer> & gbp,uint32_t transform)48 SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle,
49                                const sp<IGraphicBufferProducer>& gbp,
50                                uint32_t transform)
51       : mClient(client),
52         mHandle(handle),
53         mGraphicBufferProducer(gbp),
54         mTransformHint(transform) {}
55 
SurfaceControl(const sp<SurfaceControl> & other)56 SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) {
57     mClient = other->mClient;
58     mHandle = other->mHandle;
59     mGraphicBufferProducer = other->mGraphicBufferProducer;
60     mTransformHint = other->mTransformHint;
61 }
62 
~SurfaceControl()63 SurfaceControl::~SurfaceControl()
64 {
65     // Trigger an IPC now, to make sure things
66     // happen without delay, since these resources are quite heavy.
67     mClient.clear();
68     mHandle.clear();
69     mGraphicBufferProducer.clear();
70     IPCThreadState::self()->flushCommands();
71 }
72 
disconnect()73 void SurfaceControl::disconnect() {
74     if (mGraphicBufferProducer != nullptr) {
75         mGraphicBufferProducer->disconnect(
76                 BufferQueueCore::CURRENTLY_CONNECTED_API);
77     }
78 }
79 
isSameSurface(const sp<SurfaceControl> & lhs,const sp<SurfaceControl> & rhs)80 bool SurfaceControl::isSameSurface(
81         const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
82 {
83     if (lhs == nullptr || rhs == nullptr)
84         return false;
85     return lhs->mHandle == rhs->mHandle;
86 }
87 
clearLayerFrameStats() const88 status_t SurfaceControl::clearLayerFrameStats() const {
89     status_t err = validate();
90     if (err != NO_ERROR) return err;
91     const sp<SurfaceComposerClient>& client(mClient);
92     return client->clearLayerFrameStats(mHandle);
93 }
94 
getLayerFrameStats(FrameStats * outStats) const95 status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const {
96     status_t err = validate();
97     if (err != NO_ERROR) return err;
98     const sp<SurfaceComposerClient>& client(mClient);
99     return client->getLayerFrameStats(mHandle, outStats);
100 }
101 
validate() const102 status_t SurfaceControl::validate() const
103 {
104     if (mHandle==nullptr || mClient==nullptr) {
105         ALOGE("invalid handle (%p) or client (%p)",
106                 mHandle.get(), mClient.get());
107         return NO_INIT;
108     }
109     return NO_ERROR;
110 }
111 
writeSurfaceToParcel(const sp<SurfaceControl> & control,Parcel * parcel)112 status_t SurfaceControl::writeSurfaceToParcel(
113         const sp<SurfaceControl>& control, Parcel* parcel)
114 {
115     sp<IGraphicBufferProducer> bp;
116     if (control != nullptr) {
117         bp = control->mGraphicBufferProducer;
118     }
119     return parcel->writeStrongBinder(IInterface::asBinder(bp));
120 }
121 
generateSurfaceLocked() const122 sp<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 
getSurface() const131 sp<Surface> SurfaceControl::getSurface() const
132 {
133     Mutex::Autolock _l(mLock);
134     if (mSurfaceData == nullptr) {
135         return generateSurfaceLocked();
136     }
137     return mSurfaceData;
138 }
139 
createSurface() const140 sp<Surface> SurfaceControl::createSurface() const
141 {
142     Mutex::Autolock _l(mLock);
143     return generateSurfaceLocked();
144 }
145 
getHandle() const146 sp<IBinder> SurfaceControl::getHandle() const
147 {
148     return mHandle;
149 }
150 
getIGraphicBufferProducer() const151 sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const
152 {
153     Mutex::Autolock _l(mLock);
154     return mGraphicBufferProducer;
155 }
156 
getClient() const157 sp<SurfaceComposerClient> SurfaceControl::getClient() const
158 {
159     return mClient;
160 }
161 
getTransformHint() const162 uint32_t SurfaceControl::getTransformHint() const {
163     Mutex::Autolock _l(mLock);
164     return mTransformHint;
165 }
166 
setTransformHint(uint32_t hint)167 void SurfaceControl::setTransformHint(uint32_t hint) {
168     Mutex::Autolock _l(mLock);
169     mTransformHint = hint;
170 }
171 
writeToParcel(Parcel * parcel)172 void SurfaceControl::writeToParcel(Parcel* parcel)
173 {
174     parcel->writeStrongBinder(ISurfaceComposerClient::asBinder(mClient->getClient()));
175     parcel->writeStrongBinder(mHandle);
176     parcel->writeStrongBinder(IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
177     parcel->writeUint32(mTransformHint);
178 }
179 
readFromParcel(const Parcel * parcel)180 sp<SurfaceControl> SurfaceControl::readFromParcel(const Parcel* parcel) {
181     sp<IBinder> client = parcel->readStrongBinder();
182     sp<IBinder> handle = parcel->readStrongBinder();
183     if (client == nullptr || handle == nullptr)
184     {
185         ALOGE("Invalid parcel");
186         return nullptr;
187     }
188     sp<IBinder> gbp;
189     parcel->readNullableStrongBinder(&gbp);
190 
191     uint32_t transformHint = parcel->readUint32();
192     // We aren't the original owner of the surface.
193     return new SurfaceControl(new SurfaceComposerClient(
194                                       interface_cast<ISurfaceComposerClient>(client)),
195                               handle.get(), interface_cast<IGraphicBufferProducer>(gbp),
196                                transformHint);
197 }
198 
199 // ----------------------------------------------------------------------------
200 }; // namespace android
201