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 | |
Valerie Hau | 1acd696 | 2019-10-28 16:35:48 -0700 | [diff] [blame] | 48 | SurfaceControl::SurfaceControl(const sp<SurfaceComposerClient>& client, const sp<IBinder>& handle, |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 49 | const sp<IGraphicBufferProducer>& gbp, int32_t layerId, |
Valerie Hau | 1acd696 | 2019-10-28 16:35:48 -0700 | [diff] [blame] | 50 | uint32_t transform) |
| 51 | : mClient(client), |
| 52 | mHandle(handle), |
| 53 | mGraphicBufferProducer(gbp), |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 54 | mLayerId(layerId), |
Valerie Hau | 1acd696 | 2019-10-28 16:35:48 -0700 | [diff] [blame] | 55 | mTransformHint(transform) {} |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 56 | |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 57 | SurfaceControl::SurfaceControl(const sp<SurfaceControl>& other) { |
| 58 | mClient = other->mClient; |
| 59 | mHandle = other->mHandle; |
| 60 | mGraphicBufferProducer = other->mGraphicBufferProducer; |
Valerie Hau | 1acd696 | 2019-10-28 16:35:48 -0700 | [diff] [blame] | 61 | mTransformHint = other->mTransformHint; |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 62 | mLayerId = other->mLayerId; |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 65 | SurfaceControl::~SurfaceControl() |
| 66 | { |
Robert Carr | 8724653 | 2019-02-04 15:20:26 -0800 | [diff] [blame] | 67 | // Trigger an IPC now, to make sure things |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 68 | // happen without delay, since these resources are quite heavy. |
| 69 | mClient.clear(); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 70 | mHandle.clear(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 71 | mGraphicBufferProducer.clear(); |
| 72 | IPCThreadState::self()->flushCommands(); |
| 73 | } |
| 74 | |
Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 75 | void SurfaceControl::disconnect() { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 76 | if (mGraphicBufferProducer != nullptr) { |
Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 77 | mGraphicBufferProducer->disconnect( |
| 78 | BufferQueueCore::CURRENTLY_CONNECTED_API); |
| 79 | } |
| 80 | } |
| 81 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 82 | bool SurfaceControl::isSameSurface( |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 83 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 84 | { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 85 | if (lhs == nullptr || rhs == nullptr) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 86 | return false; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 87 | return lhs->mHandle == rhs->mHandle; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 90 | status_t SurfaceControl::clearLayerFrameStats() const { |
| 91 | status_t err = validate(); |
Aleks Rozman | 7274173 | 2018-08-04 22:15:13 -0700 | [diff] [blame] | 92 | if (err != NO_ERROR) return err; |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 93 | const sp<SurfaceComposerClient>& client(mClient); |
| 94 | return client->clearLayerFrameStats(mHandle); |
| 95 | } |
| 96 | |
| 97 | status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const { |
| 98 | status_t err = validate(); |
Aleks Rozman | 7274173 | 2018-08-04 22:15:13 -0700 | [diff] [blame] | 99 | if (err != NO_ERROR) return err; |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 100 | const sp<SurfaceComposerClient>& client(mClient); |
| 101 | return client->getLayerFrameStats(mHandle, outStats); |
| 102 | } |
| 103 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 104 | status_t SurfaceControl::validate() const |
| 105 | { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 106 | if (mHandle==nullptr || mClient==nullptr) { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 107 | ALOGE("invalid handle (%p) or client (%p)", |
| 108 | mHandle.get(), mClient.get()); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 109 | return NO_INIT; |
| 110 | } |
| 111 | return NO_ERROR; |
| 112 | } |
| 113 | |
| 114 | status_t SurfaceControl::writeSurfaceToParcel( |
| 115 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 116 | { |
| 117 | sp<IGraphicBufferProducer> bp; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 118 | if (control != nullptr) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 119 | bp = control->mGraphicBufferProducer; |
| 120 | } |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 121 | return parcel->writeStrongBinder(IInterface::asBinder(bp)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Bryce Lee | 4e623e2 | 2017-06-16 07:06:17 -0700 | [diff] [blame] | 124 | sp<Surface> SurfaceControl::generateSurfaceLocked() const |
| 125 | { |
| 126 | // This surface is always consumed by SurfaceFlinger, so the |
| 127 | // producerControlledByApp value doesn't matter; using false. |
| 128 | mSurfaceData = new Surface(mGraphicBufferProducer, false); |
| 129 | |
| 130 | return mSurfaceData; |
| 131 | } |
| 132 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 133 | sp<Surface> SurfaceControl::getSurface() const |
| 134 | { |
| 135 | Mutex::Autolock _l(mLock); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 136 | if (mSurfaceData == nullptr) { |
Bryce Lee | 4e623e2 | 2017-06-16 07:06:17 -0700 | [diff] [blame] | 137 | return generateSurfaceLocked(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 138 | } |
| 139 | return mSurfaceData; |
| 140 | } |
| 141 | |
Bryce Lee | 4e623e2 | 2017-06-16 07:06:17 -0700 | [diff] [blame] | 142 | sp<Surface> SurfaceControl::createSurface() const |
| 143 | { |
| 144 | Mutex::Autolock _l(mLock); |
| 145 | return generateSurfaceLocked(); |
| 146 | } |
| 147 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 148 | sp<IBinder> SurfaceControl::getHandle() const |
| 149 | { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 150 | return mHandle; |
| 151 | } |
| 152 | |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 153 | int32_t SurfaceControl::getLayerId() const { |
| 154 | return mLayerId; |
| 155 | } |
| 156 | |
Robert Carr | a35ef9f | 2019-01-25 14:29:21 -0800 | [diff] [blame] | 157 | sp<IGraphicBufferProducer> SurfaceControl::getIGraphicBufferProducer() const |
| 158 | { |
| 159 | Mutex::Autolock _l(mLock); |
| 160 | return mGraphicBufferProducer; |
| 161 | } |
| 162 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 163 | sp<SurfaceComposerClient> SurfaceControl::getClient() const |
| 164 | { |
| 165 | return mClient; |
| 166 | } |
| 167 | |
Valerie Hau | 1acd696 | 2019-10-28 16:35:48 -0700 | [diff] [blame] | 168 | uint32_t SurfaceControl::getTransformHint() const { |
| 169 | Mutex::Autolock _l(mLock); |
| 170 | return mTransformHint; |
| 171 | } |
| 172 | |
| 173 | void SurfaceControl::setTransformHint(uint32_t hint) { |
| 174 | Mutex::Autolock _l(mLock); |
| 175 | mTransformHint = hint; |
| 176 | } |
| 177 | |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 178 | status_t SurfaceControl::writeToParcel(Parcel& parcel) { |
| 179 | SAFE_PARCEL(parcel.writeStrongBinder, ISurfaceComposerClient::asBinder(mClient->getClient())); |
| 180 | SAFE_PARCEL(parcel.writeStrongBinder, mHandle); |
| 181 | SAFE_PARCEL(parcel.writeStrongBinder, IGraphicBufferProducer::asBinder(mGraphicBufferProducer)); |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 182 | SAFE_PARCEL(parcel.writeInt32, mLayerId); |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 183 | SAFE_PARCEL(parcel.writeUint32, mTransformHint); |
| 184 | |
| 185 | return NO_ERROR; |
Jorim Jaggi | f3cf4bc | 2017-11-30 14:19:23 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 188 | status_t SurfaceControl::readFromParcel(const Parcel& parcel, |
| 189 | sp<SurfaceControl>* outSurfaceControl) { |
Garfield Tan | 2faa631 | 2020-06-23 15:46:10 -0700 | [diff] [blame] | 190 | sp<IBinder> client; |
Garfield Tan | 2faa631 | 2020-06-23 15:46:10 -0700 | [diff] [blame] | 191 | sp<IBinder> handle; |
Jorim Jaggi | f3cf4bc | 2017-11-30 14:19:23 +0100 | [diff] [blame] | 192 | sp<IBinder> gbp; |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 193 | int32_t layerId; |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 194 | uint32_t transformHint; |
Garfield Tan | 2faa631 | 2020-06-23 15:46:10 -0700 | [diff] [blame] | 195 | |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 196 | SAFE_PARCEL(parcel.readStrongBinder, &client); |
| 197 | SAFE_PARCEL(parcel.readStrongBinder, &handle); |
| 198 | SAFE_PARCEL(parcel.readNullableStrongBinder, &gbp); |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 199 | SAFE_PARCEL(parcel.readInt32, &layerId); |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 200 | SAFE_PARCEL(parcel.readUint32, &transformHint); |
| 201 | |
Jorim Jaggi | 0b26710 | 2018-01-29 16:39:21 +0100 | [diff] [blame] | 202 | // We aren't the original owner of the surface. |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 203 | *outSurfaceControl = |
| 204 | new SurfaceControl(new SurfaceComposerClient( |
| 205 | interface_cast<ISurfaceComposerClient>(client)), |
Pablo Gamito | dbc3167 | 2020-09-01 18:28:58 +0000 | [diff] [blame] | 206 | handle.get(), interface_cast<IGraphicBufferProducer>(gbp), layerId, |
Robert Carr | 0e328f6 | 2020-02-06 17:12:08 -0800 | [diff] [blame] | 207 | transformHint); |
Pablo Gamito | 421dfd5 | 2020-09-22 18:11:45 +0000 | [diff] [blame] | 208 | |
| 209 | return NO_ERROR; |
Jorim Jaggi | f3cf4bc | 2017-11-30 14:19:23 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Pablo Gamito | 91512a0 | 2020-09-22 18:14:43 +0000 | [diff] [blame] | 212 | status_t SurfaceControl::readNullableFromParcel(const Parcel& parcel, |
| 213 | sp<SurfaceControl>* outSurfaceControl) { |
| 214 | bool isNotNull; |
| 215 | SAFE_PARCEL(parcel.readBool, &isNotNull); |
| 216 | if (isNotNull) { |
| 217 | SAFE_PARCEL(SurfaceControl::readFromParcel, parcel, outSurfaceControl); |
| 218 | } |
| 219 | |
| 220 | return NO_ERROR; |
| 221 | } |
| 222 | |
| 223 | status_t SurfaceControl::writeNullableToParcel(Parcel& parcel, |
| 224 | const sp<SurfaceControl>& surfaceControl) { |
| 225 | auto isNotNull = surfaceControl != nullptr; |
| 226 | SAFE_PARCEL(parcel.writeBool, isNotNull); |
| 227 | if (isNotNull) { |
| 228 | SAFE_PARCEL(surfaceControl->writeToParcel, parcel); |
| 229 | } |
| 230 | |
| 231 | return NO_ERROR; |
| 232 | } |
| 233 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 234 | // ---------------------------------------------------------------------------- |
| 235 | }; // namespace android |