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 | |
| 26 | #include <utils/CallStack.h> |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/Log.h> |
| 29 | #include <utils/threads.h> |
| 30 | |
| 31 | #include <binder/IPCThreadState.h> |
| 32 | |
| 33 | #include <ui/DisplayInfo.h> |
| 34 | #include <ui/GraphicBuffer.h> |
| 35 | #include <ui/Rect.h> |
| 36 | |
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 | |
| 85 | bool SurfaceControl::isSameSurface( |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 86 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 87 | { |
| 88 | if (lhs == 0 || rhs == 0) |
| 89 | return false; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 90 | return lhs->mHandle == rhs->mHandle; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | status_t SurfaceControl::setLayerStack(int32_t layerStack) { |
| 94 | status_t err = validate(); |
| 95 | if (err < 0) return err; |
| 96 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 97 | return client->setLayerStack(mHandle, layerStack); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 98 | } |
| 99 | status_t SurfaceControl::setLayer(int32_t layer) { |
| 100 | status_t err = validate(); |
| 101 | if (err < 0) return err; |
| 102 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 103 | return client->setLayer(mHandle, layer); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 104 | } |
Ramanan Rajeswaran | d6480c0 | 2013-03-21 15:49:59 +0000 | [diff] [blame] | 105 | status_t SurfaceControl::setPosition(float x, float y) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 106 | status_t err = validate(); |
| 107 | if (err < 0) return err; |
| 108 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 109 | return client->setPosition(mHandle, x, y); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 110 | } |
| 111 | status_t SurfaceControl::setSize(uint32_t w, uint32_t h) { |
| 112 | status_t err = validate(); |
| 113 | if (err < 0) return err; |
| 114 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 115 | return client->setSize(mHandle, w, h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 116 | } |
| 117 | status_t SurfaceControl::hide() { |
| 118 | status_t err = validate(); |
| 119 | if (err < 0) return err; |
| 120 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 121 | return client->hide(mHandle); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 122 | } |
| 123 | status_t SurfaceControl::show() { |
| 124 | status_t err = validate(); |
| 125 | if (err < 0) return err; |
| 126 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 127 | return client->show(mHandle); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 128 | } |
| 129 | status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) { |
| 130 | status_t err = validate(); |
| 131 | if (err < 0) return err; |
| 132 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 133 | return client->setFlags(mHandle, flags, mask); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 134 | } |
| 135 | status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) { |
| 136 | status_t err = validate(); |
| 137 | if (err < 0) return err; |
| 138 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 139 | return client->setTransparentRegionHint(mHandle, transparent); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 140 | } |
| 141 | status_t SurfaceControl::setAlpha(float alpha) { |
| 142 | status_t err = validate(); |
| 143 | if (err < 0) return err; |
| 144 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 145 | return client->setAlpha(mHandle, alpha); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 146 | } |
| 147 | status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { |
| 148 | status_t err = validate(); |
| 149 | if (err < 0) return err; |
| 150 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 151 | return client->setMatrix(mHandle, dsdx, dtdx, dsdy, dtdy); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 152 | } |
| 153 | status_t SurfaceControl::setCrop(const Rect& crop) { |
| 154 | status_t err = validate(); |
| 155 | if (err < 0) return err; |
| 156 | const sp<SurfaceComposerClient>& client(mClient); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 157 | return client->setCrop(mHandle, crop); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | status_t SurfaceControl::validate() const |
| 161 | { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 162 | if (mHandle==0 || mClient==0) { |
| 163 | ALOGE("invalid handle (%p) or client (%p)", |
| 164 | mHandle.get(), mClient.get()); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 165 | return NO_INIT; |
| 166 | } |
| 167 | return NO_ERROR; |
| 168 | } |
| 169 | |
| 170 | status_t SurfaceControl::writeSurfaceToParcel( |
| 171 | const sp<SurfaceControl>& control, Parcel* parcel) |
| 172 | { |
| 173 | sp<IGraphicBufferProducer> bp; |
| 174 | if (control != NULL) { |
| 175 | bp = control->mGraphicBufferProducer; |
| 176 | } |
| 177 | return parcel->writeStrongBinder(bp->asBinder()); |
| 178 | } |
| 179 | |
| 180 | sp<Surface> SurfaceControl::getSurface() const |
| 181 | { |
| 182 | Mutex::Autolock _l(mLock); |
| 183 | if (mSurfaceData == 0) { |
Jesse Hall | 83cafff | 2013-09-16 15:24:53 -0700 | [diff] [blame] | 184 | // This surface is always consumed by SurfaceFlinger, so the |
| 185 | // producerControlledByApp value doesn't matter; using false. |
| 186 | mSurfaceData = new Surface(mGraphicBufferProducer, false); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 187 | } |
| 188 | return mSurfaceData; |
| 189 | } |
| 190 | |
| 191 | // ---------------------------------------------------------------------------- |
| 192 | }; // namespace android |