| 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 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 36 | #include <gui/ISurfaceComposer.h> | 
|  | 37 | #include <gui/Surface.h> | 
|  | 38 | #include <gui/SurfaceComposerClient.h> | 
|  | 39 | #include <gui/SurfaceControl.h> | 
|  | 40 |  | 
|  | 41 | namespace android { | 
|  | 42 |  | 
|  | 43 | // ============================================================================ | 
|  | 44 | //  SurfaceControl | 
|  | 45 | // ============================================================================ | 
|  | 46 |  | 
|  | 47 | SurfaceControl::SurfaceControl( | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 48 | const sp<SurfaceComposerClient>& client, | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 49 | const sp<IBinder>& handle, | 
|  | 50 | const sp<IGraphicBufferProducer>& gbp) | 
|  | 51 | : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 52 | { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 53 | } | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 54 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 55 | SurfaceControl::~SurfaceControl() | 
|  | 56 | { | 
|  | 57 | destroy(); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | void SurfaceControl::destroy() | 
|  | 61 | { | 
|  | 62 | if (isValid()) { | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 63 | mClient->destroySurface(mHandle); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 64 | } | 
|  | 65 | // clear all references and trigger an IPC now, to make sure things | 
|  | 66 | // happen without delay, since these resources are quite heavy. | 
|  | 67 | mClient.clear(); | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 68 | mHandle.clear(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 69 | mGraphicBufferProducer.clear(); | 
|  | 70 | IPCThreadState::self()->flushCommands(); | 
|  | 71 | } | 
|  | 72 |  | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 73 | void SurfaceControl::clear() | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 74 | { | 
|  | 75 | // here, the window manager tells us explicitly that we should destroy | 
|  | 76 | // the surface's resource. Soon after this call, it will also release | 
|  | 77 | // its last reference (which will call the dtor); however, it is possible | 
|  | 78 | // that a client living in the same process still holds references which | 
|  | 79 | // would delay the call to the dtor -- that is why we need this explicit | 
|  | 80 | // "clear()" call. | 
|  | 81 | destroy(); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | bool SurfaceControl::isSameSurface( | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 85 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 86 | { | 
|  | 87 | if (lhs == 0 || rhs == 0) | 
|  | 88 | return false; | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 89 | return lhs->mHandle == rhs->mHandle; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | status_t SurfaceControl::setLayerStack(int32_t layerStack) { | 
|  | 93 | status_t err = validate(); | 
|  | 94 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 95 | return mClient->setLayerStack(mHandle, layerStack); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 96 | } | 
|  | 97 | status_t SurfaceControl::setLayer(int32_t layer) { | 
|  | 98 | status_t err = validate(); | 
|  | 99 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 100 | return mClient->setLayer(mHandle, layer); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 101 | } | 
| Ramanan Rajeswaran | d6480c0 | 2013-03-21 15:49:59 +0000 | [diff] [blame] | 102 | status_t SurfaceControl::setPosition(float x, float y) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 103 | status_t err = validate(); | 
|  | 104 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 105 | return mClient->setPosition(mHandle, x, y); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 106 | } | 
|  | 107 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { | 
|  | 108 | status_t err = validate(); | 
|  | 109 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 110 | return mClient->setSize(mHandle, w, h); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 111 | } | 
|  | 112 | status_t SurfaceControl::hide() { | 
|  | 113 | status_t err = validate(); | 
|  | 114 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 115 | return mClient->hide(mHandle); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 116 | } | 
|  | 117 | status_t SurfaceControl::show() { | 
|  | 118 | status_t err = validate(); | 
|  | 119 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 120 | return mClient->show(mHandle); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 121 | } | 
|  | 122 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { | 
|  | 123 | status_t err = validate(); | 
|  | 124 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 125 | return mClient->setFlags(mHandle, flags, mask); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 126 | } | 
|  | 127 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { | 
|  | 128 | status_t err = validate(); | 
|  | 129 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 130 | return mClient->setTransparentRegionHint(mHandle, transparent); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 131 | } | 
|  | 132 | status_t SurfaceControl::setAlpha(float alpha) { | 
|  | 133 | status_t err = validate(); | 
|  | 134 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 135 | return mClient->setAlpha(mHandle, alpha); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 136 | } | 
|  | 137 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { | 
|  | 138 | status_t err = validate(); | 
|  | 139 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 140 | return mClient->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 141 | } | 
|  | 142 | status_t SurfaceControl::setCrop(const Rect& crop) { | 
|  | 143 | status_t err = validate(); | 
|  | 144 | if (err < 0) return err; | 
| Dan Stoza | 3b3ba78 | 2014-04-10 13:50:03 -0700 | [diff] [blame] | 145 | return mClient->setCrop(mHandle, crop); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 148 | status_t SurfaceControl::clearLayerFrameStats() const { | 
|  | 149 | status_t err = validate(); | 
|  | 150 | if (err < 0) return err; | 
|  | 151 | const sp<SurfaceComposerClient>& client(mClient); | 
|  | 152 | return client->clearLayerFrameStats(mHandle); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | status_t SurfaceControl::getLayerFrameStats(FrameStats* outStats) const { | 
|  | 156 | status_t err = validate(); | 
|  | 157 | if (err < 0) return err; | 
|  | 158 | const sp<SurfaceComposerClient>& client(mClient); | 
|  | 159 | return client->getLayerFrameStats(mHandle, outStats); | 
|  | 160 | } | 
|  | 161 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 162 | status_t SurfaceControl::validate() const | 
|  | 163 | { | 
| Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 164 | if (mHandle==0 || mClient==0) { | 
|  | 165 | ALOGE("invalid handle (%p) or client (%p)", | 
|  | 166 | mHandle.get(), mClient.get()); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 167 | return NO_INIT; | 
|  | 168 | } | 
|  | 169 | return NO_ERROR; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | status_t SurfaceControl::writeSurfaceToParcel( | 
|  | 173 | const sp<SurfaceControl>& control, Parcel* parcel) | 
|  | 174 | { | 
|  | 175 | sp<IGraphicBufferProducer> bp; | 
|  | 176 | if (control != NULL) { | 
|  | 177 | bp = control->mGraphicBufferProducer; | 
|  | 178 | } | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 179 | return parcel->writeStrongBinder(IInterface::asBinder(bp)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
|  | 182 | sp<Surface> SurfaceControl::getSurface() const | 
|  | 183 | { | 
|  | 184 | Mutex::Autolock _l(mLock); | 
|  | 185 | if (mSurfaceData == 0) { | 
| Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 186 | // This surface is always consumed by SurfaceFlinger, so the | 
|  | 187 | // producerControlledByApp value doesn't matter; using false. | 
|  | 188 | mSurfaceData = new Surface(mGraphicBufferProducer, false); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 189 | } | 
|  | 190 | return mSurfaceData; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | // ---------------------------------------------------------------------------- | 
|  | 194 | }; // namespace android |