The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <cutils/properties.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/StopWatch.h> |
| 27 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 28 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <ui/PixelFormat.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 30 | |
| 31 | #include <surfaceflinger/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | #include "clz.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 34 | #include "GLExtensions.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include "Layer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include "DisplayHardware/DisplayHardware.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 38 | #include "DisplayHardware/HWComposer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | #define DEBUG_RESIZE 0 |
| 42 | |
| 43 | |
| 44 | namespace android { |
| 45 | |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 46 | template <typename T> inline T min(T a, T b) { |
| 47 | return a<b ? a : b; |
| 48 | } |
| 49 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | // --------------------------------------------------------------------------- |
| 51 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 52 | Layer::Layer(SurfaceFlinger* flinger, |
| 53 | DisplayID display, const sp<Client>& client) |
| 54 | : LayerBaseClient(flinger, display, client), |
Mathias Agopian | 5bf3abe | 2011-03-11 17:01:07 -0800 | [diff] [blame] | 55 | mFormat(PIXEL_FORMAT_NONE), |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 56 | mGLExtensions(GLExtensions::getInstance()), |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 57 | mNeedsBlending(true), |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 58 | mNeedsDithering(false), |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 59 | mSecure(false), |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 60 | mProtectedByApp(false), |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 61 | mTextureManager(), |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 62 | mBufferManager(mTextureManager), |
Mathias Agopian | 5bf3abe | 2011-03-11 17:01:07 -0800 | [diff] [blame] | 63 | mWidth(0), mHeight(0), |
Eric Hassold | 7ffe380 | 2011-03-11 12:24:23 -0800 | [diff] [blame] | 64 | mNeedsScaling(false), mFixedSize(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | Layer::~Layer() |
| 69 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 70 | // FIXME: must be called from the main UI thread |
| 71 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 72 | mBufferManager.destroy(dpy); |
| 73 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 74 | // we can use getUserClientUnsafe here because we know we're |
| 75 | // single-threaded at that point. |
| 76 | sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe()); |
| 77 | if (ourClient != 0) { |
| 78 | ourClient->detachLayer(this); |
| 79 | } |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 82 | status_t Layer::setToken(const sp<UserClient>& userClient, |
| 83 | SharedClient* sharedClient, int32_t token) |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 84 | { |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 85 | sp<SharedBufferServer> lcblk = new SharedBufferServer( |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 86 | sharedClient, token, mBufferManager.getDefaultBufferCount(), |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 87 | getIdentity()); |
| 88 | |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 89 | |
Mathias Agopian | dd17b3e | 2010-12-13 16:49:05 -0800 | [diff] [blame] | 90 | sp<UserClient> ourClient(mUserClientRef.getClient()); |
| 91 | |
| 92 | /* |
| 93 | * Here it is guaranteed that userClient != ourClient |
| 94 | * (see UserClient::getTokenForSurface()). |
| 95 | * |
| 96 | * We release the token used by this surface in ourClient below. |
| 97 | * This should be safe to do so now, since this layer won't be attached |
| 98 | * to this client, it should be okay to reuse that id. |
| 99 | * |
| 100 | * If this causes problems, an other solution would be to keep a list |
| 101 | * of all the {UserClient, token} ever used and release them when the |
| 102 | * Layer is destroyed. |
| 103 | * |
| 104 | */ |
| 105 | |
| 106 | if (ourClient != 0) { |
| 107 | ourClient->detachLayer(this); |
| 108 | } |
| 109 | |
| 110 | status_t err = mUserClientRef.setToken(userClient, lcblk, token); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 111 | LOGE_IF(err != NO_ERROR, |
| 112 | "ClientRef::setToken(%p, %p, %u) failed", |
| 113 | userClient.get(), lcblk.get(), token); |
| 114 | |
| 115 | if (err == NO_ERROR) { |
| 116 | // we need to free the buffers associated with this surface |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return err; |
| 120 | } |
| 121 | |
| 122 | int32_t Layer::getToken() const |
| 123 | { |
| 124 | return mUserClientRef.getToken(); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 127 | sp<UserClient> Layer::getClient() const |
| 128 | { |
| 129 | return mUserClientRef.getClient(); |
| 130 | } |
| 131 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 132 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 133 | // in the purgatory list |
| 134 | void Layer::onRemoved() |
| 135 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 136 | ClientRef::Access sharedClient(mUserClientRef); |
| 137 | SharedBufferServer* lcblk(sharedClient.get()); |
| 138 | if (lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 139 | // wake up the condition |
| 140 | lcblk->setStatus(NO_INIT); |
| 141 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 142 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 143 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 144 | sp<LayerBaseClient::Surface> Layer::createSurface() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | { |
Mathias Agopian | a1f47b9 | 2011-02-15 19:01:06 -0800 | [diff] [blame] | 146 | sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this))); |
| 147 | return sur; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 150 | status_t Layer::ditch() |
| 151 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 152 | // NOTE: Called from the main UI thread |
| 153 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 154 | // the layer is not on screen anymore. free as much resources as possible |
Mathias Agopian | f5430db | 2009-12-11 00:56:10 -0800 | [diff] [blame] | 155 | mFreezeLock.clear(); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 156 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 157 | Mutex::Autolock _l(mLock); |
| 158 | mWidth = mHeight = 0; |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 159 | return NO_ERROR; |
| 160 | } |
| 161 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 162 | status_t Layer::setBuffers( uint32_t w, uint32_t h, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | PixelFormat format, uint32_t flags) |
| 164 | { |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 165 | // this surfaces pixel format |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | PixelFormatInfo info; |
| 167 | status_t err = getPixelFormatInfo(format, &info); |
| 168 | if (err) return err; |
| 169 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 170 | // the display's pixel format |
| 171 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 172 | uint32_t const maxSurfaceDims = min( |
| 173 | hw.getMaxTextureSize(), hw.getMaxViewportDims()); |
| 174 | |
| 175 | // never allow a surface larger than what our underlying GL implementation |
| 176 | // can handle. |
| 177 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
| 178 | return BAD_VALUE; |
| 179 | } |
| 180 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 181 | PixelFormatInfo displayInfo; |
| 182 | getPixelFormatInfo(hw.getFormat(), &displayInfo); |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 183 | const uint32_t hwFlags = hw.getFlags(); |
| 184 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 185 | mFormat = format; |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 186 | mWidth = w; |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 187 | mHeight = h; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 188 | |
| 189 | mReqFormat = format; |
| 190 | mReqWidth = w; |
| 191 | mReqHeight = h; |
| 192 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 193 | mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 194 | mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false; |
Romain Guy | 3b996c9 | 2010-10-10 13:33:22 -0700 | [diff] [blame] | 195 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 && |
| 196 | (flags & ISurfaceComposer::eOpaque) == 0; |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 197 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 198 | // we use the red index |
| 199 | int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 200 | int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); |
| 201 | mNeedsDithering = layerRedsize > displayRedSize; |
| 202 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | return NO_ERROR; |
| 204 | } |
| 205 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 206 | void Layer::setGeometry(hwc_layer_t* hwcl) |
| 207 | { |
| 208 | hwcl->compositionType = HWC_FRAMEBUFFER; |
| 209 | hwcl->hints = 0; |
| 210 | hwcl->flags = 0; |
| 211 | hwcl->transform = 0; |
| 212 | hwcl->blending = HWC_BLENDING_NONE; |
| 213 | |
| 214 | // we can't do alpha-fade with the hwc HAL |
| 215 | const State& s(drawingState()); |
| 216 | if (s.alpha < 0xFF) { |
| 217 | hwcl->flags = HWC_SKIP_LAYER; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | // we can only handle simple transformation |
| 222 | if (mOrientation & Transform::ROT_INVALID) { |
| 223 | hwcl->flags = HWC_SKIP_LAYER; |
| 224 | return; |
| 225 | } |
| 226 | |
Mathias Agopian | 86bdb2f | 2010-12-08 17:23:18 -0800 | [diff] [blame] | 227 | Transform tr(Transform(mOrientation) * Transform(mBufferTransform)); |
| 228 | hwcl->transform = tr.getOrientation(); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 229 | |
| 230 | if (needsBlending()) { |
| 231 | hwcl->blending = mPremultipliedAlpha ? |
| 232 | HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE; |
| 233 | } |
| 234 | |
| 235 | hwcl->displayFrame.left = mTransformedBounds.left; |
| 236 | hwcl->displayFrame.top = mTransformedBounds.top; |
| 237 | hwcl->displayFrame.right = mTransformedBounds.right; |
| 238 | hwcl->displayFrame.bottom = mTransformedBounds.bottom; |
| 239 | |
| 240 | hwcl->visibleRegionScreen.rects = |
| 241 | reinterpret_cast<hwc_rect_t const *>( |
| 242 | visibleRegionScreen.getArray( |
| 243 | &hwcl->visibleRegionScreen.numRects)); |
| 244 | } |
| 245 | |
| 246 | void Layer::setPerFrameData(hwc_layer_t* hwcl) { |
| 247 | sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer()); |
| 248 | if (buffer == NULL) { |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 249 | // this can happen if the client never drew into this layer yet, |
| 250 | // or if we ran out of memory. In that case, don't let |
| 251 | // HWC handle it. |
| 252 | hwcl->flags |= HWC_SKIP_LAYER; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 253 | hwcl->handle = NULL; |
| 254 | return; |
| 255 | } |
Louis Huemiller | 0404814 | 2010-12-01 12:29:36 -0800 | [diff] [blame] | 256 | hwcl->handle = buffer->handle; |
Mathias Agopian | f345069 | 2010-12-08 17:40:28 -0800 | [diff] [blame] | 257 | |
| 258 | if (!mBufferCrop.isEmpty()) { |
| 259 | hwcl->sourceCrop.left = mBufferCrop.left; |
| 260 | hwcl->sourceCrop.top = mBufferCrop.top; |
| 261 | hwcl->sourceCrop.right = mBufferCrop.right; |
| 262 | hwcl->sourceCrop.bottom = mBufferCrop.bottom; |
| 263 | } else { |
| 264 | hwcl->sourceCrop.left = 0; |
| 265 | hwcl->sourceCrop.top = 0; |
| 266 | hwcl->sourceCrop.right = buffer->width; |
| 267 | hwcl->sourceCrop.bottom = buffer->height; |
| 268 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 269 | } |
| 270 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | void Layer::reloadTexture(const Region& dirty) |
| 272 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 273 | sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer()); |
Mathias Agopian | 8f03b47 | 2009-12-10 15:52:29 -0800 | [diff] [blame] | 274 | if (buffer == NULL) { |
| 275 | // this situation can happen if we ran out of memory for instance. |
| 276 | // not much we can do. continue to use whatever texture was bound |
| 277 | // to this context. |
| 278 | return; |
| 279 | } |
| 280 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 281 | if (mGLExtensions.haveDirectTexture()) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 282 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 283 | if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) { |
| 284 | // not sure what we can do here... |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 285 | goto slowpath; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 286 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 287 | } else { |
Mathias Agopian | fcfeb4b | 2010-03-08 11:14:20 -0800 | [diff] [blame] | 288 | slowpath: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 289 | GGLSurface t; |
Mathias Agopian | f1b3824 | 2010-08-20 15:59:53 -0700 | [diff] [blame] | 290 | if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) { |
| 291 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
| 292 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 293 | res, strerror(res), buffer.get()); |
| 294 | if (res == NO_ERROR) { |
| 295 | mBufferManager.loadTexture(dirty, t); |
| 296 | buffer->unlock(); |
| 297 | } |
| 298 | } else { |
| 299 | // we can't do anything |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 300 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 304 | void Layer::drawForSreenShot() const |
| 305 | { |
Mathias Agopian | 733189d | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 306 | const bool currentFiltering = mNeedsFiltering; |
| 307 | const_cast<Layer*>(this)->mNeedsFiltering = true; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 308 | LayerBase::drawForSreenShot(); |
Mathias Agopian | 733189d | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 309 | const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | void Layer::onDraw(const Region& clip) const |
| 313 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 314 | Texture tex(mBufferManager.getActiveTexture()); |
| 315 | if (tex.name == -1LU) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 317 | // in fact never been drawn into. This happens frequently with |
| 318 | // SurfaceView because the WindowManager can't know when the client |
| 319 | // has drawn the first time. |
| 320 | |
| 321 | // If there is nothing under us, we paint the screen in black, otherwise |
| 322 | // we just skip this update. |
| 323 | |
| 324 | // figure out if there is something below us |
| 325 | Region under; |
| 326 | const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ); |
| 327 | const size_t count = drawingLayers.size(); |
| 328 | for (size_t i=0 ; i<count ; ++i) { |
| 329 | const sp<LayerBase>& layer(drawingLayers[i]); |
| 330 | if (layer.get() == static_cast<LayerBase const*>(this)) |
| 331 | break; |
| 332 | under.orSelf(layer->visibleRegionScreen); |
| 333 | } |
| 334 | // if not everything below us is covered, we plug the holes! |
| 335 | Region holes(clip.subtract(under)); |
| 336 | if (!holes.isEmpty()) { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 337 | clearWithOpenGL(holes, 0, 0, 0, 1); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 338 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | return; |
| 340 | } |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 341 | drawWithOpenGL(clip, tex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 344 | // As documented in libhardware header, formats in the range |
| 345 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 346 | // are known to have no alpha channel |
| 347 | // TODO: move definition for device-specific range into |
| 348 | // hardware.h, instead of using hard-coded values here. |
| 349 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 350 | |
| 351 | bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const |
| 352 | { |
| 353 | // If buffers where set with eOpaque flag, all buffers are known to |
| 354 | // be opaque without having to check their actual format |
| 355 | if (mNeedsBlending && buffer != NULL) { |
| 356 | PixelFormat format = buffer->getPixelFormat(); |
| 357 | |
| 358 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | PixelFormatInfo info; |
| 363 | status_t err = getPixelFormatInfo(format, &info); |
| 364 | if (!err && info.h_alpha <= info.l_alpha) { |
| 365 | return false; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Return opacity as determined from flags and format options |
| 370 | // passed to setBuffers() |
| 371 | return mNeedsBlending; |
| 372 | } |
| 373 | |
| 374 | bool Layer::needsBlending() const |
| 375 | { |
| 376 | if (mBufferManager.hasActiveBuffer()) { |
| 377 | return needsBlending(mBufferManager.getActiveBuffer()); |
| 378 | } |
| 379 | |
| 380 | return mNeedsBlending; |
| 381 | } |
| 382 | |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 383 | bool Layer::needsFiltering() const |
| 384 | { |
| 385 | if (!(mFlags & DisplayHardware::SLOW_CONFIG)) { |
Mathias Agopian | 733189d | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 386 | // if our buffer is not the same size than ourselves, |
| 387 | // we need filtering. |
| 388 | Mutex::Autolock _l(mLock); |
| 389 | if (mNeedsScaling) |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | return LayerBase::needsFiltering(); |
| 393 | } |
| 394 | |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 395 | bool Layer::isProtected() const |
| 396 | { |
| 397 | sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer()); |
| 398 | return (activeBuffer != 0) && |
| 399 | (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 400 | } |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 401 | |
| 402 | status_t Layer::setBufferCount(int bufferCount) |
| 403 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 404 | ClientRef::Access sharedClient(mUserClientRef); |
| 405 | SharedBufferServer* lcblk(sharedClient.get()); |
| 406 | if (!lcblk) { |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 407 | // oops, the client is already gone |
| 408 | return DEAD_OBJECT; |
| 409 | } |
| 410 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 411 | // NOTE: lcblk->resize() is protected by an internal lock |
| 412 | status_t err = lcblk->resize(bufferCount); |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 413 | if (err == NO_ERROR) { |
| 414 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 415 | mBufferManager.resize(bufferCount, mFlinger, dpy); |
| 416 | } |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 417 | |
| 418 | return err; |
| 419 | } |
| 420 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 421 | sp<GraphicBuffer> Layer::requestBuffer(int index, |
| 422 | uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat, |
| 423 | uint32_t usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 425 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 426 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 427 | if (int32_t(reqWidth | reqHeight | reqFormat) < 0) |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 428 | return buffer; |
| 429 | |
| 430 | if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight)) |
| 431 | return buffer; |
| 432 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 433 | // this ensures our client doesn't go away while we're accessing |
| 434 | // the shared area. |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 435 | ClientRef::Access sharedClient(mUserClientRef); |
| 436 | SharedBufferServer* lcblk(sharedClient.get()); |
| 437 | if (!lcblk) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 438 | // oops, the client is already gone |
| 439 | return buffer; |
| 440 | } |
| 441 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 442 | /* |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 443 | * This is called from the client's Surface::dequeue(). This can happen |
| 444 | * at any time, especially while we're in the middle of using the |
| 445 | * buffer 'index' as our front buffer. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 446 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | |
Mathias Agopian | 208cb07 | 2010-07-27 20:11:35 -0700 | [diff] [blame] | 448 | status_t err = NO_ERROR; |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 449 | uint32_t w, h, f; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 450 | { // scope for the lock |
| 451 | Mutex::Autolock _l(mLock); |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 452 | |
| 453 | // zero means default |
Mathias Agopian | e44d21a | 2010-09-21 10:52:42 -0700 | [diff] [blame] | 454 | const bool fixedSize = reqWidth && reqHeight; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 455 | if (!reqFormat) reqFormat = mFormat; |
| 456 | if (!reqWidth) reqWidth = mWidth; |
| 457 | if (!reqHeight) reqHeight = mHeight; |
| 458 | |
| 459 | w = reqWidth; |
| 460 | h = reqHeight; |
| 461 | f = reqFormat; |
| 462 | |
| 463 | if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) || |
| 464 | (reqFormat != mReqFormat)) { |
| 465 | mReqWidth = reqWidth; |
| 466 | mReqHeight = reqHeight; |
| 467 | mReqFormat = reqFormat; |
Mathias Agopian | e44d21a | 2010-09-21 10:52:42 -0700 | [diff] [blame] | 468 | mFixedSize = fixedSize; |
Mathias Agopian | 733189d | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 469 | mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 470 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 471 | lcblk->reallocateAllExcept(index); |
| 472 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Mathias Agopian | 208cb07 | 2010-07-27 20:11:35 -0700 | [diff] [blame] | 475 | // here we have to reallocate a new buffer because the buffer could be |
| 476 | // used as the front buffer, or by a client in our process |
| 477 | // (eg: status bar), and we can't release the handle under its feet. |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 478 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | 208cb07 | 2010-07-27 20:11:35 -0700 | [diff] [blame] | 479 | buffer = new GraphicBuffer(w, h, f, effectiveUsage); |
| 480 | err = buffer->initCheck(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 481 | |
| 482 | if (err || buffer->handle == 0) { |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 483 | GraphicBuffer::dumpAllocationsToSystemLog(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 484 | LOGE_IF(err || buffer->handle == 0, |
| 485 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 486 | this, index, w, h, strerror(-err)); |
| 487 | } else { |
| 488 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 489 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 490 | this, index, w, h, buffer->handle); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 494 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 495 | mBufferManager.attachBuffer(index, buffer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 496 | } |
| 497 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 500 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 501 | { |
| 502 | /* |
| 503 | * buffers used for software rendering, but h/w composition |
| 504 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 505 | * |
| 506 | * buffers used for h/w rendering and h/w composition |
| 507 | * are allocated with HW_RENDER | HW_TEXTURE |
| 508 | * |
| 509 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 510 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 511 | * |
| 512 | */ |
| 513 | |
| 514 | if (mSecure) { |
| 515 | // secure buffer, don't store it into the GPU |
| 516 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 517 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 518 | } else { |
| 519 | // it's allowed to modify the usage flags here, but generally |
| 520 | // the requested flags should be honored. |
Mathias Agopian | 89141f9 | 2010-05-10 20:10:10 -0700 | [diff] [blame] | 521 | // request EGLImage for all buffers |
| 522 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 523 | } |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 524 | if (mProtectedByApp) { |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 525 | // need a hardware-protected path to external video sink |
| 526 | usage |= GraphicBuffer::USAGE_PROTECTED; |
| 527 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 528 | return usage; |
| 529 | } |
| 530 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | uint32_t Layer::doTransaction(uint32_t flags) |
| 532 | { |
| 533 | const Layer::State& front(drawingState()); |
| 534 | const Layer::State& temp(currentState()); |
| 535 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 536 | const bool sizeChanged = (front.requested_w != temp.requested_w) || |
| 537 | (front.requested_h != temp.requested_h); |
| 538 | |
| 539 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 540 | // the size changed, we need to ask our client to request a new buffer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 542 | "resize (layer=%p), requested (%dx%d), drawing (%d,%d)", |
| 543 | this, |
| 544 | int(temp.requested_w), int(temp.requested_h), |
| 545 | int(front.requested_w), int(front.requested_h)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 547 | if (!isFixedSize()) { |
| 548 | // we're being resized and there is a freeze display request, |
| 549 | // acquire a freeze lock, so that the screen stays put |
| 550 | // until we've redrawn at the new size; this is to avoid |
| 551 | // glitches upon orientation changes. |
| 552 | if (mFlinger->hasFreezeRequest()) { |
| 553 | // if the surface is hidden, don't try to acquire the |
| 554 | // freeze lock, since hidden surfaces may never redraw |
| 555 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 556 | mFreezeLock = mFlinger->getFreezeLock(); |
| 557 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 559 | |
| 560 | // this will make sure LayerBase::doTransaction doesn't update |
| 561 | // the drawing state's size |
| 562 | Layer::State& editDraw(mDrawingState); |
| 563 | editDraw.requested_w = temp.requested_w; |
| 564 | editDraw.requested_h = temp.requested_h; |
| 565 | |
| 566 | // record the new size, form this point on, when the client request |
| 567 | // a buffer, it'll get the new size. |
| 568 | setBufferSize(temp.requested_w, temp.requested_h); |
| 569 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 570 | ClientRef::Access sharedClient(mUserClientRef); |
| 571 | SharedBufferServer* lcblk(sharedClient.get()); |
| 572 | if (lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 573 | // all buffers need reallocation |
| 574 | lcblk->reallocateAll(); |
| 575 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 576 | } else { |
| 577 | // record the new size |
| 578 | setBufferSize(temp.requested_w, temp.requested_h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | } |
| 580 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 581 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | if (temp.sequence != front.sequence) { |
| 583 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 584 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 585 | // (it may never redraw, which is fine if it is hidden) |
| 586 | mFreezeLock.clear(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | return LayerBase::doTransaction(flags); |
| 591 | } |
| 592 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 593 | void Layer::setBufferSize(uint32_t w, uint32_t h) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 594 | Mutex::Autolock _l(mLock); |
| 595 | mWidth = w; |
| 596 | mHeight = h; |
Mathias Agopian | 733189d | 2010-12-02 21:32:29 -0800 | [diff] [blame] | 597 | mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 600 | bool Layer::isFixedSize() const { |
| 601 | Mutex::Autolock _l(mLock); |
| 602 | return mFixedSize; |
| 603 | } |
| 604 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | // ---------------------------------------------------------------------------- |
| 606 | // pageflip handling... |
| 607 | // ---------------------------------------------------------------------------- |
| 608 | |
| 609 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 610 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 611 | ClientRef::Access sharedClient(mUserClientRef); |
| 612 | SharedBufferServer* lcblk(sharedClient.get()); |
| 613 | if (!lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 614 | // client died |
| 615 | recomputeVisibleRegions = true; |
| 616 | return; |
| 617 | } |
| 618 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 619 | ssize_t buf = lcblk->retireAndLock(); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 620 | if (buf == NOT_ENOUGH_DATA) { |
| 621 | // NOTE: This is not an error, it simply means there is nothing to |
| 622 | // retire. The buffer is locked because we will use it |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 623 | // for composition later in the loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | return; |
| 625 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 626 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 627 | if (buf < NO_ERROR) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 628 | LOGE("retireAndLock() buffer index (%d) out of range", int(buf)); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 629 | mPostedDirtyRegion.clear(); |
| 630 | return; |
| 631 | } |
| 632 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 633 | // we retired a buffer, which becomes the new front buffer |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 634 | |
| 635 | const bool noActiveBuffer = !mBufferManager.hasActiveBuffer(); |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 636 | const bool activeBlending = |
| 637 | noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer()); |
| 638 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 639 | if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 640 | LOGE("retireAndLock() buffer index (%d) out of range", int(buf)); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 641 | mPostedDirtyRegion.clear(); |
| 642 | return; |
| 643 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 645 | if (noActiveBuffer) { |
| 646 | // we didn't have an active buffer, we need to recompute |
| 647 | // our visible region |
| 648 | recomputeVisibleRegions = true; |
| 649 | } |
| 650 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 651 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 652 | if (newFrontBuffer != NULL) { |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 653 | if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) { |
| 654 | // new buffer has different opacity than previous active buffer, need |
| 655 | // to recompute visible regions accordingly |
| 656 | recomputeVisibleRegions = true; |
| 657 | } |
| 658 | |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 659 | // get the dirty region |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 660 | // compute the posted region |
| 661 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 662 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 663 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 664 | // update the layer size and release freeze-lock |
| 665 | const Layer::State& front(drawingState()); |
| 666 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 667 | newFrontBuffer->getHeight() == front.requested_h) |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 668 | { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 669 | if ((front.w != front.requested_w) || |
| 670 | (front.h != front.requested_h)) |
| 671 | { |
| 672 | // Here we pretend the transaction happened by updating the |
| 673 | // current and drawing states. Drawing state is only accessed |
| 674 | // in this thread, no need to have it locked |
| 675 | Layer::State& editDraw(mDrawingState); |
| 676 | editDraw.w = editDraw.requested_w; |
| 677 | editDraw.h = editDraw.requested_h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 678 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 679 | // We also need to update the current state so that we don't |
| 680 | // end-up doing too much work during the next transaction. |
| 681 | // NOTE: We actually don't need hold the transaction lock here |
| 682 | // because State::w and State::h are only accessed from |
| 683 | // this thread |
| 684 | Layer::State& editTemp(currentState()); |
| 685 | editTemp.w = editDraw.w; |
| 686 | editTemp.h = editDraw.h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 687 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 688 | // recompute visible region |
| 689 | recomputeVisibleRegions = true; |
| 690 | } |
| 691 | |
| 692 | // we now have the correct size, unfreeze the screen |
| 693 | mFreezeLock.clear(); |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 694 | } |
Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 695 | |
| 696 | // get the crop region |
| 697 | setBufferCrop( lcblk->getCrop(buf) ); |
| 698 | |
| 699 | // get the transformation |
| 700 | setBufferTransform( lcblk->getTransform(buf) ); |
| 701 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 702 | } else { |
| 703 | // this should not happen unless we ran out of memory while |
| 704 | // allocating the buffer. we're hoping that things will get back |
| 705 | // to normal the next time the app tries to draw into this buffer. |
| 706 | // meanwhile, pretend the screen didn't update. |
| 707 | mPostedDirtyRegion.clear(); |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Mathias Agopian | e700501 | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 710 | if (lcblk->getQueuedCount()) { |
| 711 | // signal an event if we have more buffers waiting |
| 712 | mFlinger->signalEvent(); |
| 713 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | |
Mathias Agopian | 245e4d7 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 715 | /* a buffer was posted, so we need to call reloadTexture(), which |
| 716 | * will update our internal data structures (eg: EGLImageKHR or |
| 717 | * texture names). we need to do this even if mPostedDirtyRegion is |
| 718 | * empty -- it's orthogonal to the fact that a new buffer was posted, |
| 719 | * for instance, a degenerate case could be that the user did an empty |
| 720 | * update but repainted the buffer with appropriate content (after a |
| 721 | * resize for instance). |
| 722 | */ |
| 723 | reloadTexture( mPostedDirtyRegion ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void Layer::unlockPageFlip( |
| 727 | const Transform& planeTransform, Region& outDirtyRegion) |
| 728 | { |
| 729 | Region dirtyRegion(mPostedDirtyRegion); |
| 730 | if (!dirtyRegion.isEmpty()) { |
| 731 | mPostedDirtyRegion.clear(); |
| 732 | // The dirty region is given in the layer's coordinate space |
| 733 | // transform the dirty region by the surface's transformation |
| 734 | // and the global transformation. |
| 735 | const Layer::State& s(drawingState()); |
| 736 | const Transform tr(planeTransform * s.transform); |
| 737 | dirtyRegion = tr.transform(dirtyRegion); |
| 738 | |
| 739 | // At this point, the dirty region is in screen space. |
| 740 | // Make sure it's constrained by the visible region (which |
| 741 | // is in screen space as well). |
| 742 | dirtyRegion.andSelf(visibleRegionScreen); |
| 743 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | } |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 745 | if (visibleRegionScreen.isEmpty()) { |
| 746 | // an invisible layer should not hold a freeze-lock |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 747 | // (because it may never be updated and therefore never release it) |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 748 | mFreezeLock.clear(); |
| 749 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 752 | void Layer::dump(String8& result, char* buffer, size_t SIZE) const |
| 753 | { |
| 754 | LayerBaseClient::dump(result, buffer, SIZE); |
| 755 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 756 | ClientRef::Access sharedClient(mUserClientRef); |
| 757 | SharedBufferServer* lcblk(sharedClient.get()); |
| 758 | uint32_t totalTime = 0; |
| 759 | if (lcblk) { |
| 760 | SharedBufferStack::Statistics stats = lcblk->getStats(); |
| 761 | totalTime= stats.totalTime; |
| 762 | result.append( lcblk->dump(" ") ); |
| 763 | } |
| 764 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 765 | sp<const GraphicBuffer> buf0(getBuffer(0)); |
| 766 | sp<const GraphicBuffer> buf1(getBuffer(1)); |
| 767 | uint32_t w0=0, h0=0, s0=0; |
| 768 | uint32_t w1=0, h1=0, s1=0; |
| 769 | if (buf0 != 0) { |
| 770 | w0 = buf0->getWidth(); |
| 771 | h0 = buf0->getHeight(); |
| 772 | s0 = buf0->getStride(); |
| 773 | } |
| 774 | if (buf1 != 0) { |
| 775 | w1 = buf1->getWidth(); |
| 776 | h1 = buf1->getHeight(); |
| 777 | s1 = buf1->getStride(); |
| 778 | } |
| 779 | snprintf(buffer, SIZE, |
| 780 | " " |
| 781 | "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u]," |
| 782 | " freezeLock=%p, dq-q-time=%u us\n", |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 783 | mFormat, w0, h0, s0, w1, h1, s1, |
| 784 | getFreezeLock().get(), totalTime); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 785 | |
| 786 | result.append(buffer); |
| 787 | } |
| 788 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 789 | // --------------------------------------------------------------------------- |
| 790 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 791 | Layer::ClientRef::ClientRef() |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 792 | : mControlBlock(0), mToken(-1) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | Layer::ClientRef::~ClientRef() { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | int32_t Layer::ClientRef::getToken() const { |
| 799 | Mutex::Autolock _l(mLock); |
| 800 | return mToken; |
| 801 | } |
| 802 | |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 803 | sp<UserClient> Layer::ClientRef::getClient() const { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 804 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 805 | return mUserClient.promote(); |
| 806 | } |
| 807 | |
| 808 | status_t Layer::ClientRef::setToken(const sp<UserClient>& uc, |
| 809 | const sp<SharedBufferServer>& sharedClient, int32_t token) { |
| 810 | Mutex::Autolock _l(mLock); |
| 811 | |
| 812 | { // scope for strong mUserClient reference |
| 813 | sp<UserClient> userClient(mUserClient.promote()); |
Jamie Gennis | 5fd799d | 2011-03-18 16:35:13 -0700 | [diff] [blame] | 814 | if (userClient != 0 && mControlBlock != 0) { |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 815 | mControlBlock->setStatus(NO_INIT); |
| 816 | } |
| 817 | } |
| 818 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 819 | mUserClient = uc; |
| 820 | mToken = token; |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 821 | mControlBlock = sharedClient; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 822 | return NO_ERROR; |
| 823 | } |
| 824 | |
| 825 | sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const { |
| 826 | return mUserClient.promote(); |
| 827 | } |
| 828 | |
| 829 | // this class gives us access to SharedBufferServer safely |
| 830 | // it makes sure the UserClient (and its associated shared memory) |
| 831 | // won't go away while we're accessing it. |
| 832 | Layer::ClientRef::Access::Access(const ClientRef& ref) |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 833 | : mControlBlock(0) |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 834 | { |
| 835 | Mutex::Autolock _l(ref.mLock); |
| 836 | mUserClientStrongRef = ref.mUserClient.promote(); |
| 837 | if (mUserClientStrongRef != 0) |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 838 | mControlBlock = ref.mControlBlock; |
| 839 | } |
| 840 | |
| 841 | Layer::ClientRef::Access::~Access() |
| 842 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | // --------------------------------------------------------------------------- |
| 846 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 847 | Layer::BufferManager::BufferManager(TextureManager& tm) |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 848 | : mNumBuffers(NUM_BUFFERS), mTextureManager(tm), |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 849 | mActiveBufferIndex(-1), mFailover(false) |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 850 | { |
| 851 | } |
| 852 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 853 | Layer::BufferManager::~BufferManager() |
| 854 | { |
| 855 | } |
| 856 | |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 857 | status_t Layer::BufferManager::resize(size_t size, |
| 858 | const sp<SurfaceFlinger>& flinger, EGLDisplay dpy) |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 859 | { |
| 860 | Mutex::Autolock _l(mLock); |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 861 | |
| 862 | if (size < mNumBuffers) { |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 863 | // If there is an active texture, move it into slot 0 if needed |
| 864 | if (mActiveBufferIndex > 0) { |
| 865 | BufferData activeBufferData = mBufferData[mActiveBufferIndex]; |
| 866 | mBufferData[mActiveBufferIndex] = mBufferData[0]; |
| 867 | mBufferData[0] = activeBufferData; |
| 868 | mActiveBufferIndex = 0; |
| 869 | } |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 870 | |
| 871 | // Free the buffers that are no longer needed. |
| 872 | for (size_t i = size; i < mNumBuffers; i++) { |
| 873 | mBufferData[i].buffer = 0; |
| 874 | |
| 875 | // Create a message to destroy the textures on SurfaceFlinger's GL |
| 876 | // thread. |
| 877 | class MessageDestroyTexture : public MessageBase { |
| 878 | Image mTexture; |
| 879 | EGLDisplay mDpy; |
| 880 | public: |
| 881 | MessageDestroyTexture(const Image& texture, EGLDisplay dpy) |
| 882 | : mTexture(texture), mDpy(dpy) { } |
| 883 | virtual bool handler() { |
| 884 | status_t err = Layer::BufferManager::destroyTexture( |
| 885 | &mTexture, mDpy); |
| 886 | LOGE_IF(err<0, "error destroying texture: %d (%s)", |
| 887 | mTexture.name, strerror(-err)); |
| 888 | return true; // XXX: err == 0; ???? |
| 889 | } |
| 890 | }; |
| 891 | |
| 892 | MessageDestroyTexture *msg = new MessageDestroyTexture( |
| 893 | mBufferData[i].texture, dpy); |
| 894 | |
| 895 | // Don't allow this texture to be cleaned up by |
| 896 | // BufferManager::destroy. |
| 897 | mBufferData[i].texture.name = -1U; |
| 898 | mBufferData[i].texture.image = EGL_NO_IMAGE_KHR; |
| 899 | |
| 900 | // Post the message to the SurfaceFlinger object. |
| 901 | flinger->postMessageAsync(msg); |
| 902 | } |
| 903 | } |
| 904 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 905 | mNumBuffers = size; |
| 906 | return NO_ERROR; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // only for debugging |
| 910 | sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const { |
| 911 | return mBufferData[index].buffer; |
| 912 | } |
| 913 | |
| 914 | status_t Layer::BufferManager::setActiveBufferIndex(size_t index) { |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 915 | BufferData const * const buffers = mBufferData; |
| 916 | Mutex::Autolock _l(mLock); |
| 917 | mActiveBuffer = buffers[index].buffer; |
| 918 | mActiveBufferIndex = index; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 919 | return NO_ERROR; |
| 920 | } |
| 921 | |
| 922 | size_t Layer::BufferManager::getActiveBufferIndex() const { |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 923 | return mActiveBufferIndex; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | Texture Layer::BufferManager::getActiveTexture() const { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 927 | Texture res; |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 928 | if (mFailover || mActiveBufferIndex<0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 929 | res = mFailoverTexture; |
| 930 | } else { |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 931 | static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 932 | } |
| 933 | return res; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const { |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 937 | return mActiveBuffer; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 940 | bool Layer::BufferManager::hasActiveBuffer() const { |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 941 | return mActiveBufferIndex >= 0; |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 944 | sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index) |
| 945 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 946 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 947 | sp<GraphicBuffer> buffer; |
| 948 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 949 | buffer = buffers[index].buffer; |
| 950 | buffers[index].buffer = 0; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 951 | return buffer; |
| 952 | } |
| 953 | |
| 954 | status_t Layer::BufferManager::attachBuffer(size_t index, |
| 955 | const sp<GraphicBuffer>& buffer) |
| 956 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 957 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 958 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 959 | buffers[index].buffer = buffer; |
| 960 | buffers[index].texture.dirty = true; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 961 | return NO_ERROR; |
| 962 | } |
| 963 | |
| 964 | status_t Layer::BufferManager::destroy(EGLDisplay dpy) |
| 965 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 966 | BufferData* const buffers = mBufferData; |
| 967 | size_t num; |
| 968 | { // scope for the lock |
| 969 | Mutex::Autolock _l(mLock); |
| 970 | num = mNumBuffers; |
| 971 | for (size_t i=0 ; i<num ; i++) { |
| 972 | buffers[i].buffer = 0; |
| 973 | } |
| 974 | } |
| 975 | for (size_t i=0 ; i<num ; i++) { |
| 976 | destroyTexture(&buffers[i].texture, dpy); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 977 | } |
| 978 | destroyTexture(&mFailoverTexture, dpy); |
| 979 | return NO_ERROR; |
| 980 | } |
| 981 | |
| 982 | status_t Layer::BufferManager::initEglImage(EGLDisplay dpy, |
| 983 | const sp<GraphicBuffer>& buffer) |
| 984 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 985 | status_t err = NO_INIT; |
Mathias Agopian | 420a283 | 2010-12-14 20:30:37 -0800 | [diff] [blame] | 986 | ssize_t index = mActiveBufferIndex; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 987 | if (index >= 0) { |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 988 | if (!mFailover) { |
Kobi Cohen Arazi | 0d11baf | 2011-04-15 10:38:33 -0700 | [diff] [blame] | 989 | { |
| 990 | // Without that lock, there is a chance of race condition |
| 991 | // where while composing a specific index, requestBuf |
| 992 | // with the same index can be executed and touch the same data |
| 993 | // that is being used in initEglImage. |
| 994 | // (e.g. dirty flag in texture) |
| 995 | Mutex::Autolock _l(mLock); |
| 996 | Image& texture(mBufferData[index].texture); |
| 997 | err = mTextureManager.initEglImage(&texture, dpy, buffer); |
| 998 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 999 | // if EGLImage fails, we switch to regular texture mode, and we |
| 1000 | // free all resources associated with using EGLImages. |
| 1001 | if (err == NO_ERROR) { |
| 1002 | mFailover = false; |
| 1003 | destroyTexture(&mFailoverTexture, dpy); |
| 1004 | } else { |
| 1005 | mFailover = true; |
| 1006 | const size_t num = mNumBuffers; |
| 1007 | for (size_t i=0 ; i<num ; i++) { |
| 1008 | destroyTexture(&mBufferData[i].texture, dpy); |
| 1009 | } |
Andreas Huber | e049a95 | 2010-06-25 09:25:19 -0700 | [diff] [blame] | 1010 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 1011 | } else { |
| 1012 | // we failed once, don't try again |
| 1013 | err = BAD_VALUE; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | return err; |
| 1017 | } |
| 1018 | |
| 1019 | status_t Layer::BufferManager::loadTexture( |
| 1020 | const Region& dirty, const GGLSurface& t) |
| 1021 | { |
| 1022 | return mTextureManager.loadTexture(&mFailoverTexture, dirty, t); |
| 1023 | } |
| 1024 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1025 | status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy) |
| 1026 | { |
| 1027 | if (tex->name != -1U) { |
| 1028 | glDeleteTextures(1, &tex->name); |
| 1029 | tex->name = -1U; |
| 1030 | } |
| 1031 | if (tex->image != EGL_NO_IMAGE_KHR) { |
| 1032 | eglDestroyImageKHR(dpy, tex->image); |
| 1033 | tex->image = EGL_NO_IMAGE_KHR; |
| 1034 | } |
| 1035 | return NO_ERROR; |
| 1036 | } |
| 1037 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 1038 | // --------------------------------------------------------------------------- |
| 1039 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1040 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1041 | const sp<Layer>& owner) |
| 1042 | : Surface(flinger, owner->getIdentity(), owner) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1043 | { |
| 1044 | } |
| 1045 | |
| 1046 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1047 | { |
| 1048 | } |
| 1049 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1050 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, |
| 1051 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1052 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1053 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1054 | sp<Layer> owner(getOwner()); |
| 1055 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1056 | /* |
| 1057 | * requestBuffer() cannot be called from the main thread |
| 1058 | * as it could cause a dead-lock, since it may have to wait |
| 1059 | * on conditions updated my the main thread. |
| 1060 | */ |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1061 | buffer = owner->requestBuffer(index, w, h, format, usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1062 | } |
| 1063 | return buffer; |
| 1064 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 1066 | status_t Layer::SurfaceLayer::setBufferCount(int bufferCount) |
| 1067 | { |
| 1068 | status_t err = DEAD_OBJECT; |
| 1069 | sp<Layer> owner(getOwner()); |
| 1070 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1071 | /* |
| 1072 | * setBufferCount() cannot be called from the main thread |
| 1073 | * as it could cause a dead-lock, since it may have to wait |
| 1074 | * on conditions updated my the main thread. |
| 1075 | */ |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 1076 | err = owner->setBufferCount(bufferCount); |
| 1077 | } |
| 1078 | return err; |
| 1079 | } |
| 1080 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | // --------------------------------------------------------------------------- |
| 1082 | |
| 1083 | |
| 1084 | }; // namespace android |