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, |
| 51 | const sp<IGraphicBufferProducer>& gbp) |
| 52 | : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 53 | { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 54 | } |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 55 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 56 | SurfaceControl::~SurfaceControl() |
| 57 | { |
| 58 | destroy(); |
| 59 | } |
| 60 | |
| 61 | void SurfaceControl::destroy() |
| 62 | { |
| 63 | if (isValid()) { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 64 | mClient->destroySurface(mHandle); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 65 | } |
| 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 Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 69 | mHandle.clear(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 70 | mGraphicBufferProducer.clear(); |
| 71 | IPCThreadState::self()->flushCommands(); |
| 72 | } |
| 73 | |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 74 | void SurfaceControl::clear() |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 75 | { |
| 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 Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 85 | void SurfaceControl::disconnect() { |
| 86 | if (mGraphicBufferProducer != NULL) { |
| 87 | mGraphicBufferProducer->disconnect( |
| 88 | BufferQueueCore::CURRENTLY_CONNECTED_API); |
| 89 | } |
| 90 | } |
| 91 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 92 | bool SurfaceControl::isSameSurface( |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 93 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 94 | { |
| 95 | if (lhs == 0 || rhs == 0) |
| 96 | return false; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 97 | return lhs->mHandle == rhs->mHandle; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 100 | status_t SurfaceControl::setLayerStack(uint32_t layerStack) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 101 | status_t err = validate(); |
| 102 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 103 | return mClient->setLayerStack(mHandle, layerStack); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 104 | } |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 105 | status_t SurfaceControl::setLayer(uint32_t layer) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 106 | status_t err = validate(); |
| 107 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 108 | return mClient->setLayer(mHandle, layer); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 109 | } |
Ramanan Rajeswaran | d6480c0 | 2013-03-21 15:49:59 +0000 | [diff] [blame] | 110 | status_t SurfaceControl::setPosition(float x, float y) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 111 | status_t err = validate(); |
| 112 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 113 | return mClient->setPosition(mHandle, x, y); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 114 | } |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 115 | status_t SurfaceControl::setPositionAppliesWithResize() { |
| 116 | status_t err = validate(); |
| 117 | if (err < 0) return err; |
| 118 | return mClient->setPositionAppliesWithResize(mHandle); |
| 119 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 120 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { |
| 121 | status_t err = validate(); |
| 122 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 123 | return mClient->setSize(mHandle, w, h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 124 | } |
| 125 | status_t SurfaceControl::hide() { |
| 126 | status_t err = validate(); |
| 127 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 128 | return mClient->hide(mHandle); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 129 | } |
| 130 | status_t SurfaceControl::show() { |
| 131 | status_t err = validate(); |
| 132 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 133 | return mClient->show(mHandle); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 134 | } |
| 135 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { |
| 136 | status_t err = validate(); |
| 137 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 138 | return mClient->setFlags(mHandle, flags, mask); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 139 | } |
| 140 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { |
| 141 | status_t err = validate(); |
| 142 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 143 | return mClient->setTransparentRegionHint(mHandle, transparent); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 144 | } |
| 145 | status_t SurfaceControl::setAlpha(float alpha) { |
| 146 | status_t err = validate(); |
| 147 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 148 | return mClient->setAlpha(mHandle, alpha); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 149 | } |
| 150 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { |
| 151 | status_t err = validate(); |
| 152 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 153 | return mClient->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 154 | } |
| 155 | status_t SurfaceControl::setCrop(const Rect& crop) { |
| 156 | status_t err = validate(); |
| 157 | if (err < 0) return err; |
Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 158 | return mClient->setCrop(mHandle, crop); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 159 | } |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 160 | status_t SurfaceControl::setFinalCrop(const Rect& crop) { |
| 161 | status_t err = validate(); |
| 162 | if (err < 0) return err; |
| 163 | return mClient->setFinalCrop(mHandle, crop); |
| 164 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 165 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 166 | status_t SurfaceControl::deferTransactionUntil(sp<IBinder> handle, |
| 167 | uint64_t frameNumber) { |
| 168 | status_t err = validate(); |
| 169 | if (err < 0) return err; |
| 170 | return mClient->deferTransactionUntil(mHandle, handle, frameNumber); |
| 171 | } |
| 172 | |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 173 | status_t SurfaceControl::setOverrideScalingMode(int32_t overrideScalingMode) { |
| 174 | status_t err = validate(); |
| 175 | if (err < 0) return err; |
| 176 | return mClient->setOverrideScalingMode(mHandle, overrideScalingMode); |
| 177 | } |
| 178 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 179 | status_t SurfaceControl::clearLayerFrameStats() const { |
| 180 | status_t err = validate(); |
| 181 | if (err < 0) return err; |
| 182 | const sp<SurfaceComposerClient>& client(mClient); |
| 183 | return client->clearLayerFrameStats(mHandle); |
| 184 | } |
| 185 | |
| 186 | status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const { |
| 187 | status_t err = validate(); |
| 188 | if (err < 0) return err; |
| 189 | const sp<SurfaceComposerClient>& client(mClient); |
| 190 | return client->getLayerFrameStats(mHandle, outStats); |
| 191 | } |
| 192 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 193 | status_t SurfaceControl::validate() const |
| 194 | { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 195 | if (mHandle==0 || mClient==0) { |
| 196 | ALOGE("invalid handle (%p) or client (%p)", |
| 197 | mHandle.get(), mClient.get()); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 198 | return NO_INIT; |
| 199 | } |
| 200 | return NO_ERROR; |
| 201 | } |
| 202 | |
| 203 | status_t SurfaceControl::writeSurfaceToParcel( |
| 204 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 205 | { |
| 206 | sp<IGraphicBufferProducer> bp; |
| 207 | if (control != NULL) { |
| 208 | bp = control->mGraphicBufferProducer; |
| 209 | } |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 210 | return parcel->writeStrongBinder(IInterface::asBinder(bp)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | sp<Surface> SurfaceControl::getSurface() const |
| 214 | { |
| 215 | Mutex::Autolock _l(mLock); |
| 216 | if (mSurfaceData == 0) { |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 217 | // This surface is always consumed by SurfaceFlinger, so the |
| 218 | // producerControlledByApp value doesn't matter; using false. |
| 219 | mSurfaceData = new Surface(mGraphicBufferProducer, false); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 220 | } |
| 221 | return mSurfaceData; |
| 222 | } |
| 223 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 224 | sp<IBinder> SurfaceControl::getHandle() const |
| 225 | { |
| 226 | Mutex::Autolock lock(mLock); |
| 227 | return mHandle; |
| 228 | } |
| 229 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 230 | // ---------------------------------------------------------------------------- |
| 231 | }; // namespace android |