| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
| Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 36 | #include <gui/BufferQueueCore.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
|  | 48 | SurfaceControl::SurfaceControl( | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 49 | const sp<SurfaceComposerClient>& client, | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 50 | const sp<IBinder>& handle, | 
| Jorim Jaggi | 0b26710 | 2018-01-29 16:39:21 +0100 | [diff] [blame] | 51 | const sp<IGraphicBufferProducer>& gbp, | 
|  | 52 | bool owned) | 
|  | 53 | : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp), mOwned(owned) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 54 | { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 55 | } | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 56 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 57 | SurfaceControl::~SurfaceControl() | 
|  | 58 | { | 
|  | 59 | destroy(); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | void SurfaceControl::destroy() | 
|  | 63 | { | 
| Jorim Jaggi | 0b26710 | 2018-01-29 16:39:21 +0100 | [diff] [blame] | 64 | // Avoid destroying the server-side surface if we are not the owner of it, meaning that we | 
|  | 65 | // retrieved it from another process. | 
|  | 66 | if (isValid() && mOwned) { | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 67 | mClient->destroySurface(mHandle); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 68 | } | 
|  | 69 | // clear all references and trigger an IPC now, to make sure things | 
|  | 70 | // happen without delay, since these resources are quite heavy. | 
|  | 71 | mClient.clear(); | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 72 | mHandle.clear(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 73 | mGraphicBufferProducer.clear(); | 
|  | 74 | IPCThreadState::self()->flushCommands(); | 
|  | 75 | } | 
|  | 76 |  | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 77 | void SurfaceControl::clear() | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 78 | { | 
|  | 79 | // here, the window manager tells us explicitly that we should destroy | 
|  | 80 | // the surface's resource. Soon after this call, it will also release | 
|  | 81 | // its last reference (which will call the dtor); however, it is possible | 
|  | 82 | // that a client living in the same process still holds references which | 
|  | 83 | // would delay the call to the dtor -- that is why we need this explicit | 
|  | 84 | // "clear()" call. | 
|  | 85 | destroy(); | 
|  | 86 | } | 
|  | 87 |  | 
| Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 88 | void SurfaceControl::disconnect() { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 89 | if (mGraphicBufferProducer != nullptr) { | 
| Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 90 | mGraphicBufferProducer->disconnect( | 
|  | 91 | BufferQueueCore::CURRENTLY_CONNECTED_API); | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 95 | bool SurfaceControl::isSameSurface( | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 96 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 97 | { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 98 | if (lhs == nullptr || rhs == nullptr) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 99 | return false; | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 100 | return lhs->mHandle == rhs->mHandle; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 103 | status_t SurfaceControl::clearLayerFrameStats() const { | 
|  | 104 | status_t err = validate(); | 
| Aleks Rozman | 7274173 | 2018-08-04 22:15:13 -0700 | [diff] [blame] | 105 | if (err != NO_ERROR) return err; | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 106 | const sp<SurfaceComposerClient>& client(mClient); | 
|  | 107 | return client->clearLayerFrameStats(mHandle); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const { | 
|  | 111 | status_t err = validate(); | 
| Aleks Rozman | 7274173 | 2018-08-04 22:15:13 -0700 | [diff] [blame] | 112 | if (err != NO_ERROR) return err; | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 113 | const sp<SurfaceComposerClient>& client(mClient); | 
|  | 114 | return client->getLayerFrameStats(mHandle, outStats); | 
|  | 115 | } | 
|  | 116 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 117 | status_t SurfaceControl::validate() const | 
|  | 118 | { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 119 | if (mHandle==nullptr || mClient==nullptr) { | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 120 | ALOGE("invalid handle (%p) or client (%p)", | 
|  | 121 | mHandle.get(), mClient.get()); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 122 | return NO_INIT; | 
|  | 123 | } | 
|  | 124 | return NO_ERROR; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | status_t SurfaceControl::writeSurfaceToParcel( | 
|  | 128 | const sp<SurfaceControl>& control, Parcel* parcel) | 
|  | 129 | { | 
|  | 130 | sp<IGraphicBufferProducer> bp; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 131 | if (control != nullptr) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 132 | bp = control->mGraphicBufferProducer; | 
|  | 133 | } | 
| Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 134 | return parcel->writeStrongBinder(IInterface::asBinder(bp)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Bryce Lee | 4e623e2 | 2017-06-16 07:06:17 -0700 | [diff] [blame] | 137 | sp<Surface> SurfaceControl::generateSurfaceLocked() const | 
|  | 138 | { | 
|  | 139 | // This surface is always consumed by SurfaceFlinger, so the | 
|  | 140 | // producerControlledByApp value doesn't matter; using false. | 
|  | 141 | mSurfaceData = new Surface(mGraphicBufferProducer, false); | 
|  | 142 |  | 
|  | 143 | return mSurfaceData; | 
|  | 144 | } | 
|  | 145 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 146 | sp<Surface> SurfaceControl::getSurface() const | 
|  | 147 | { | 
|  | 148 | Mutex::Autolock _l(mLock); | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 149 | if (mSurfaceData == nullptr) { | 
| Bryce Lee | 4e623e2 | 2017-06-16 07:06:17 -0700 | [diff] [blame] | 150 | return generateSurfaceLocked(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 151 | } | 
|  | 152 | return mSurfaceData; | 
|  | 153 | } | 
|  | 154 |  | 
| Bryce Lee | 4e623e2 | 2017-06-16 07:06:17 -0700 | [diff] [blame] | 155 | sp<Surface> SurfaceControl::createSurface() const | 
|  | 156 | { | 
|  | 157 | Mutex::Autolock _l(mLock); | 
|  | 158 | return generateSurfaceLocked(); | 
|  | 159 | } | 
|  | 160 |  | 
| Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 161 | sp<IBinder> SurfaceControl::getHandle() const | 
|  | 162 | { | 
|  | 163 | Mutex::Autolock lock(mLock); | 
|  | 164 | return mHandle; | 
|  | 165 | } | 
|  | 166 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 167 | sp<SurfaceComposerClient> SurfaceControl::getClient() const | 
|  | 168 | { | 
|  | 169 | return mClient; | 
|  | 170 | } | 
|  | 171 |  | 
| Jorim Jaggi | f3cf4bc | 2017-11-30 14:19:23 +0100 | [diff] [blame] | 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 | } | 
|  | 178 |  | 
|  | 179 | sp<SurfaceControl> SurfaceControl::readFromParcel(Parcel* parcel) | 
|  | 180 | { | 
|  | 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); | 
| Jorim Jaggi | 0b26710 | 2018-01-29 16:39:21 +0100 | [diff] [blame] | 190 |  | 
|  | 191 | // We aren't the original owner of the surface. | 
| Jorim Jaggi | f3cf4bc | 2017-11-30 14:19:23 +0100 | [diff] [blame] | 192 | return new SurfaceControl(new SurfaceComposerClient( | 
|  | 193 | interface_cast<ISurfaceComposerClient>(client)), | 
| Jorim Jaggi | 0b26710 | 2018-01-29 16:39:21 +0100 | [diff] [blame] | 194 | handle.get(), interface_cast<IGraphicBufferProducer>(gbp), false /* owned */); | 
| Jorim Jaggi | f3cf4bc | 2017-11-30 14:19:23 +0100 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 197 | // ---------------------------------------------------------------------------- | 
|  | 198 | }; // namespace android |