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" |
| 38 | |
| 39 | |
| 40 | #define DEBUG_RESIZE 0 |
| 41 | |
| 42 | |
| 43 | namespace android { |
| 44 | |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 45 | template <typename T> inline T min(T a, T b) { |
| 46 | return a<b ? a : b; |
| 47 | } |
| 48 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | // --------------------------------------------------------------------------- |
| 50 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 51 | Layer::Layer(SurfaceFlinger* flinger, |
| 52 | DisplayID display, const sp<Client>& client) |
| 53 | : LayerBaseClient(flinger, display, client), |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 54 | mGLExtensions(GLExtensions::getInstance()), |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 55 | mNeedsBlending(true), |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 56 | mNeedsDithering(false), |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 57 | mSecure(false), |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 58 | mTextureManager(), |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 59 | mBufferManager(mTextureManager), |
| 60 | mWidth(0), mHeight(0), mFixedSize(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Layer::~Layer() |
| 65 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 66 | // FIXME: must be called from the main UI thread |
| 67 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 68 | mBufferManager.destroy(dpy); |
| 69 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 70 | // we can use getUserClientUnsafe here because we know we're |
| 71 | // single-threaded at that point. |
| 72 | sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe()); |
| 73 | if (ourClient != 0) { |
| 74 | ourClient->detachLayer(this); |
| 75 | } |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 78 | status_t Layer::setToken(const sp<UserClient>& userClient, |
| 79 | SharedClient* sharedClient, int32_t token) |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 80 | { |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 81 | sp<SharedBufferServer> lcblk = new SharedBufferServer( |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 82 | sharedClient, token, mBufferManager.getDefaultBufferCount(), |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 83 | getIdentity()); |
| 84 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 85 | status_t err = mUserClientRef.setToken(userClient, lcblk, token); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 86 | |
| 87 | LOGE_IF(err != NO_ERROR, |
| 88 | "ClientRef::setToken(%p, %p, %u) failed", |
| 89 | userClient.get(), lcblk.get(), token); |
| 90 | |
| 91 | if (err == NO_ERROR) { |
| 92 | // we need to free the buffers associated with this surface |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | int32_t Layer::getToken() const |
| 99 | { |
| 100 | return mUserClientRef.getToken(); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 103 | sp<UserClient> Layer::getClient() const |
| 104 | { |
| 105 | return mUserClientRef.getClient(); |
| 106 | } |
| 107 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 108 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 109 | // in the purgatory list |
| 110 | void Layer::onRemoved() |
| 111 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 112 | ClientRef::Access sharedClient(mUserClientRef); |
| 113 | SharedBufferServer* lcblk(sharedClient.get()); |
| 114 | if (lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 115 | // wake up the condition |
| 116 | lcblk->setStatus(NO_INIT); |
| 117 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 118 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 119 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 120 | sp<LayerBaseClient::Surface> Layer::createSurface() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | { |
| 122 | return mSurface; |
| 123 | } |
| 124 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 125 | status_t Layer::ditch() |
| 126 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 127 | // NOTE: Called from the main UI thread |
| 128 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 129 | // 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] | 130 | mFreezeLock.clear(); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 131 | |
| 132 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 133 | mBufferManager.destroy(dpy); |
| 134 | mSurface.clear(); |
| 135 | |
| 136 | Mutex::Autolock _l(mLock); |
| 137 | mWidth = mHeight = 0; |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 141 | 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] | 142 | PixelFormat format, uint32_t flags) |
| 143 | { |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 144 | // this surfaces pixel format |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | PixelFormatInfo info; |
| 146 | status_t err = getPixelFormatInfo(format, &info); |
| 147 | if (err) return err; |
| 148 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 149 | // the display's pixel format |
| 150 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 151 | uint32_t const maxSurfaceDims = min( |
| 152 | hw.getMaxTextureSize(), hw.getMaxViewportDims()); |
| 153 | |
| 154 | // never allow a surface larger than what our underlying GL implementation |
| 155 | // can handle. |
| 156 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
| 157 | return BAD_VALUE; |
| 158 | } |
| 159 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 160 | PixelFormatInfo displayInfo; |
| 161 | getPixelFormatInfo(hw.getFormat(), &displayInfo); |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 162 | const uint32_t hwFlags = hw.getFlags(); |
| 163 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 164 | mFormat = format; |
Mathias Agopian | f955be9 | 2010-06-21 15:19:26 -0700 | [diff] [blame] | 165 | mReqFormat = format; |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 166 | mWidth = w; |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 167 | mHeight = h; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 168 | mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 170 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 171 | // we use the red index |
| 172 | int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 173 | int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); |
| 174 | mNeedsDithering = layerRedsize > displayRedSize; |
| 175 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 176 | mSurface = new SurfaceLayer(mFlinger, this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | return NO_ERROR; |
| 178 | } |
| 179 | |
| 180 | void Layer::reloadTexture(const Region& dirty) |
| 181 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 182 | sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer()); |
Mathias Agopian | 8f03b47 | 2009-12-10 15:52:29 -0800 | [diff] [blame] | 183 | if (buffer == NULL) { |
| 184 | // this situation can happen if we ran out of memory for instance. |
| 185 | // not much we can do. continue to use whatever texture was bound |
| 186 | // to this context. |
| 187 | return; |
| 188 | } |
| 189 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 190 | if (mGLExtensions.haveDirectTexture()) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 191 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 192 | if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) { |
| 193 | // not sure what we can do here... |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 194 | goto slowpath; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 195 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 196 | } else { |
Mathias Agopian | fcfeb4b | 2010-03-08 11:14:20 -0800 | [diff] [blame] | 197 | slowpath: |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 198 | GGLSurface t; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 199 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 200 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 201 | res, strerror(res), buffer.get()); |
| 202 | if (res == NO_ERROR) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 203 | mBufferManager.loadTexture(dirty, t); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 204 | buffer->unlock(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 205 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | void Layer::onDraw(const Region& clip) const |
| 210 | { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 211 | Texture tex(mBufferManager.getActiveTexture()); |
| 212 | if (tex.name == -1LU) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 214 | // in fact never been drawn into. This happens frequently with |
| 215 | // SurfaceView because the WindowManager can't know when the client |
| 216 | // has drawn the first time. |
| 217 | |
| 218 | // If there is nothing under us, we paint the screen in black, otherwise |
| 219 | // we just skip this update. |
| 220 | |
| 221 | // figure out if there is something below us |
| 222 | Region under; |
| 223 | const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ); |
| 224 | const size_t count = drawingLayers.size(); |
| 225 | for (size_t i=0 ; i<count ; ++i) { |
| 226 | const sp<LayerBase>& layer(drawingLayers[i]); |
| 227 | if (layer.get() == static_cast<LayerBase const*>(this)) |
| 228 | break; |
| 229 | under.orSelf(layer->visibleRegionScreen); |
| 230 | } |
| 231 | // if not everything below us is covered, we plug the holes! |
| 232 | Region holes(clip.subtract(under)); |
| 233 | if (!holes.isEmpty()) { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 234 | clearWithOpenGL(holes, 0, 0, 0, 1); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 235 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | return; |
| 237 | } |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 238 | drawWithOpenGL(clip, tex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 241 | bool Layer::needsFiltering() const |
| 242 | { |
| 243 | if (!(mFlags & DisplayHardware::SLOW_CONFIG)) { |
| 244 | // NOTE: there is a race here, because mFixedSize is updated in a |
| 245 | // binder transaction. however, it doesn't really matter since it is |
| 246 | // evaluated each time we draw. To be perfectly correct, this flag |
| 247 | // would have to be associated with a buffer. |
| 248 | if (mFixedSize) |
| 249 | return true; |
| 250 | } |
| 251 | return LayerBase::needsFiltering(); |
| 252 | } |
| 253 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 254 | |
| 255 | status_t Layer::setBufferCount(int bufferCount) |
| 256 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 257 | ClientRef::Access sharedClient(mUserClientRef); |
| 258 | SharedBufferServer* lcblk(sharedClient.get()); |
| 259 | if (!lcblk) { |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 260 | // oops, the client is already gone |
| 261 | return DEAD_OBJECT; |
| 262 | } |
| 263 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 264 | // NOTE: lcblk->resize() is protected by an internal lock |
| 265 | status_t err = lcblk->resize(bufferCount); |
| 266 | if (err == NO_ERROR) |
| 267 | mBufferManager.resize(bufferCount); |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 268 | |
| 269 | return err; |
| 270 | } |
| 271 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 272 | sp<GraphicBuffer> Layer::requestBuffer(int index, |
| 273 | uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat, |
| 274 | uint32_t usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 276 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 277 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 278 | if (int32_t(reqWidth | reqHeight | reqFormat) < 0) |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 279 | return buffer; |
| 280 | |
| 281 | if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight)) |
| 282 | return buffer; |
| 283 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 284 | // this ensures our client doesn't go away while we're accessing |
| 285 | // the shared area. |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 286 | ClientRef::Access sharedClient(mUserClientRef); |
| 287 | SharedBufferServer* lcblk(sharedClient.get()); |
| 288 | if (!lcblk) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 289 | // oops, the client is already gone |
| 290 | return buffer; |
| 291 | } |
| 292 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 293 | /* |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 294 | * This is called from the client's Surface::dequeue(). This can happen |
| 295 | * at any time, especially while we're in the middle of using the |
| 296 | * buffer 'index' as our front buffer. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 297 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
Mathias Agopian | 208cb07 | 2010-07-27 20:11:35 -0700 | [diff] [blame] | 299 | status_t err = NO_ERROR; |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 300 | uint32_t w, h, f; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 301 | { // scope for the lock |
| 302 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 303 | const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight); |
| 304 | const bool formatChanged = mReqFormat != reqFormat; |
| 305 | mReqWidth = reqWidth; |
| 306 | mReqHeight = reqHeight; |
| 307 | mReqFormat = reqFormat; |
| 308 | mFixedSize = reqWidth && reqHeight; |
| 309 | w = reqWidth ? reqWidth : mWidth; |
| 310 | h = reqHeight ? reqHeight : mHeight; |
| 311 | f = reqFormat ? reqFormat : mFormat; |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 312 | if (fixedSizeChanged || formatChanged) { |
| 313 | lcblk->reallocateAllExcept(index); |
| 314 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Mathias Agopian | 208cb07 | 2010-07-27 20:11:35 -0700 | [diff] [blame] | 317 | // here we have to reallocate a new buffer because the buffer could be |
| 318 | // used as the front buffer, or by a client in our process |
| 319 | // (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] | 320 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | 208cb07 | 2010-07-27 20:11:35 -0700 | [diff] [blame] | 321 | buffer = new GraphicBuffer(w, h, f, effectiveUsage); |
| 322 | err = buffer->initCheck(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 323 | |
| 324 | if (err || buffer->handle == 0) { |
| 325 | LOGE_IF(err || buffer->handle == 0, |
| 326 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 327 | this, index, w, h, strerror(-err)); |
| 328 | } else { |
| 329 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 330 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 331 | this, index, w, h, buffer->handle); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 335 | Mutex::Autolock _l(mLock); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 336 | mBufferManager.attachBuffer(index, buffer); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 337 | } |
| 338 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 341 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 342 | { |
| 343 | /* |
| 344 | * buffers used for software rendering, but h/w composition |
| 345 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 346 | * |
| 347 | * buffers used for h/w rendering and h/w composition |
| 348 | * are allocated with HW_RENDER | HW_TEXTURE |
| 349 | * |
| 350 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 351 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 352 | * |
| 353 | */ |
| 354 | |
| 355 | if (mSecure) { |
| 356 | // secure buffer, don't store it into the GPU |
| 357 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 358 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 359 | } else { |
| 360 | // it's allowed to modify the usage flags here, but generally |
| 361 | // the requested flags should be honored. |
Mathias Agopian | 89141f9 | 2010-05-10 20:10:10 -0700 | [diff] [blame] | 362 | // request EGLImage for all buffers |
| 363 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 364 | } |
| 365 | return usage; |
| 366 | } |
| 367 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | uint32_t Layer::doTransaction(uint32_t flags) |
| 369 | { |
| 370 | const Layer::State& front(drawingState()); |
| 371 | const Layer::State& temp(currentState()); |
| 372 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 373 | const bool sizeChanged = (front.requested_w != temp.requested_w) || |
| 374 | (front.requested_h != temp.requested_h); |
| 375 | |
| 376 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 377 | // 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] | 378 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 379 | "resize (layer=%p), requested (%dx%d), drawing (%d,%d)", |
| 380 | this, |
| 381 | int(temp.requested_w), int(temp.requested_h), |
| 382 | int(front.requested_w), int(front.requested_h)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 384 | if (!isFixedSize()) { |
| 385 | // we're being resized and there is a freeze display request, |
| 386 | // acquire a freeze lock, so that the screen stays put |
| 387 | // until we've redrawn at the new size; this is to avoid |
| 388 | // glitches upon orientation changes. |
| 389 | if (mFlinger->hasFreezeRequest()) { |
| 390 | // if the surface is hidden, don't try to acquire the |
| 391 | // freeze lock, since hidden surfaces may never redraw |
| 392 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 393 | mFreezeLock = mFlinger->getFreezeLock(); |
| 394 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 396 | |
| 397 | // this will make sure LayerBase::doTransaction doesn't update |
| 398 | // the drawing state's size |
| 399 | Layer::State& editDraw(mDrawingState); |
| 400 | editDraw.requested_w = temp.requested_w; |
| 401 | editDraw.requested_h = temp.requested_h; |
| 402 | |
| 403 | // record the new size, form this point on, when the client request |
| 404 | // a buffer, it'll get the new size. |
| 405 | setBufferSize(temp.requested_w, temp.requested_h); |
| 406 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 407 | ClientRef::Access sharedClient(mUserClientRef); |
| 408 | SharedBufferServer* lcblk(sharedClient.get()); |
| 409 | if (lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 410 | // all buffers need reallocation |
| 411 | lcblk->reallocateAll(); |
| 412 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 413 | } else { |
| 414 | // record the new size |
| 415 | setBufferSize(temp.requested_w, temp.requested_h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 418 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | if (temp.sequence != front.sequence) { |
| 420 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 421 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 422 | // (it may never redraw, which is fine if it is hidden) |
| 423 | mFreezeLock.clear(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return LayerBase::doTransaction(flags); |
| 428 | } |
| 429 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 430 | void Layer::setBufferSize(uint32_t w, uint32_t h) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 431 | Mutex::Autolock _l(mLock); |
| 432 | mWidth = w; |
| 433 | mHeight = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 436 | bool Layer::isFixedSize() const { |
| 437 | Mutex::Autolock _l(mLock); |
| 438 | return mFixedSize; |
| 439 | } |
| 440 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | // ---------------------------------------------------------------------------- |
| 442 | // pageflip handling... |
| 443 | // ---------------------------------------------------------------------------- |
| 444 | |
| 445 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 446 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 447 | ClientRef::Access sharedClient(mUserClientRef); |
| 448 | SharedBufferServer* lcblk(sharedClient.get()); |
| 449 | if (!lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 450 | // client died |
| 451 | recomputeVisibleRegions = true; |
| 452 | return; |
| 453 | } |
| 454 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 455 | ssize_t buf = lcblk->retireAndLock(); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 456 | if (buf == NOT_ENOUGH_DATA) { |
| 457 | // NOTE: This is not an error, it simply means there is nothing to |
| 458 | // retire. The buffer is locked because we will use it |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 459 | // for composition later in the loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | return; |
| 461 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 462 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 463 | if (buf < NO_ERROR) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 464 | LOGE("retireAndLock() buffer index (%d) out of range", int(buf)); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 465 | mPostedDirtyRegion.clear(); |
| 466 | return; |
| 467 | } |
| 468 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 469 | // we retired a buffer, which becomes the new front buffer |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 470 | if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 471 | LOGE("retireAndLock() buffer index (%d) out of range", int(buf)); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 472 | mPostedDirtyRegion.clear(); |
| 473 | return; |
| 474 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 476 | // get the dirty region |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 477 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 478 | if (newFrontBuffer != NULL) { |
| 479 | // compute the posted region |
| 480 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 481 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 482 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 483 | // update the layer size and release freeze-lock |
| 484 | const Layer::State& front(drawingState()); |
| 485 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 486 | newFrontBuffer->getHeight() == front.requested_h) |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 487 | { |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 488 | if ((front.w != front.requested_w) || |
| 489 | (front.h != front.requested_h)) |
| 490 | { |
| 491 | // Here we pretend the transaction happened by updating the |
| 492 | // current and drawing states. Drawing state is only accessed |
| 493 | // in this thread, no need to have it locked |
| 494 | Layer::State& editDraw(mDrawingState); |
| 495 | editDraw.w = editDraw.requested_w; |
| 496 | editDraw.h = editDraw.requested_h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 497 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 498 | // We also need to update the current state so that we don't |
| 499 | // end-up doing too much work during the next transaction. |
| 500 | // NOTE: We actually don't need hold the transaction lock here |
| 501 | // because State::w and State::h are only accessed from |
| 502 | // this thread |
| 503 | Layer::State& editTemp(currentState()); |
| 504 | editTemp.w = editDraw.w; |
| 505 | editTemp.h = editDraw.h; |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 506 | |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 507 | // recompute visible region |
| 508 | recomputeVisibleRegions = true; |
| 509 | } |
| 510 | |
| 511 | // we now have the correct size, unfreeze the screen |
| 512 | mFreezeLock.clear(); |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 513 | } |
Mathias Agopian | d343e3d | 2010-03-15 18:15:20 -0700 | [diff] [blame] | 514 | } else { |
| 515 | // this should not happen unless we ran out of memory while |
| 516 | // allocating the buffer. we're hoping that things will get back |
| 517 | // to normal the next time the app tries to draw into this buffer. |
| 518 | // meanwhile, pretend the screen didn't update. |
| 519 | mPostedDirtyRegion.clear(); |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Mathias Agopian | e700501 | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 522 | if (lcblk->getQueuedCount()) { |
| 523 | // signal an event if we have more buffers waiting |
| 524 | mFlinger->signalEvent(); |
| 525 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | |
Mathias Agopian | 245e4d7 | 2010-04-21 15:24:11 -0700 | [diff] [blame] | 527 | /* a buffer was posted, so we need to call reloadTexture(), which |
| 528 | * will update our internal data structures (eg: EGLImageKHR or |
| 529 | * texture names). we need to do this even if mPostedDirtyRegion is |
| 530 | * empty -- it's orthogonal to the fact that a new buffer was posted, |
| 531 | * for instance, a degenerate case could be that the user did an empty |
| 532 | * update but repainted the buffer with appropriate content (after a |
| 533 | * resize for instance). |
| 534 | */ |
| 535 | reloadTexture( mPostedDirtyRegion ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void Layer::unlockPageFlip( |
| 539 | const Transform& planeTransform, Region& outDirtyRegion) |
| 540 | { |
| 541 | Region dirtyRegion(mPostedDirtyRegion); |
| 542 | if (!dirtyRegion.isEmpty()) { |
| 543 | mPostedDirtyRegion.clear(); |
| 544 | // The dirty region is given in the layer's coordinate space |
| 545 | // transform the dirty region by the surface's transformation |
| 546 | // and the global transformation. |
| 547 | const Layer::State& s(drawingState()); |
| 548 | const Transform tr(planeTransform * s.transform); |
| 549 | dirtyRegion = tr.transform(dirtyRegion); |
| 550 | |
| 551 | // At this point, the dirty region is in screen space. |
| 552 | // Make sure it's constrained by the visible region (which |
| 553 | // is in screen space as well). |
| 554 | dirtyRegion.andSelf(visibleRegionScreen); |
| 555 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | } |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 557 | if (visibleRegionScreen.isEmpty()) { |
| 558 | // an invisible layer should not hold a freeze-lock |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 559 | // (because it may never be updated and therefore never release it) |
Mathias Agopian | c61de17 | 2009-11-30 11:15:41 -0800 | [diff] [blame] | 560 | mFreezeLock.clear(); |
| 561 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void Layer::finishPageFlip() |
| 565 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 566 | ClientRef::Access sharedClient(mUserClientRef); |
| 567 | SharedBufferServer* lcblk(sharedClient.get()); |
| 568 | if (lcblk) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 569 | int buf = mBufferManager.getActiveBufferIndex(); |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 570 | if (buf >= 0) { |
| 571 | status_t err = lcblk->unlock( buf ); |
| 572 | LOGE_IF(err!=NO_ERROR, |
| 573 | "layer %p, buffer=%d wasn't locked!", |
| 574 | this, buf); |
| 575 | } |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 576 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 579 | |
| 580 | void Layer::dump(String8& result, char* buffer, size_t SIZE) const |
| 581 | { |
| 582 | LayerBaseClient::dump(result, buffer, SIZE); |
| 583 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 584 | ClientRef::Access sharedClient(mUserClientRef); |
| 585 | SharedBufferServer* lcblk(sharedClient.get()); |
| 586 | uint32_t totalTime = 0; |
| 587 | if (lcblk) { |
| 588 | SharedBufferStack::Statistics stats = lcblk->getStats(); |
| 589 | totalTime= stats.totalTime; |
| 590 | result.append( lcblk->dump(" ") ); |
| 591 | } |
| 592 | |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 593 | sp<const GraphicBuffer> buf0(getBuffer(0)); |
| 594 | sp<const GraphicBuffer> buf1(getBuffer(1)); |
| 595 | uint32_t w0=0, h0=0, s0=0; |
| 596 | uint32_t w1=0, h1=0, s1=0; |
| 597 | if (buf0 != 0) { |
| 598 | w0 = buf0->getWidth(); |
| 599 | h0 = buf0->getHeight(); |
| 600 | s0 = buf0->getStride(); |
| 601 | } |
| 602 | if (buf1 != 0) { |
| 603 | w1 = buf1->getWidth(); |
| 604 | h1 = buf1->getHeight(); |
| 605 | s1 = buf1->getStride(); |
| 606 | } |
| 607 | snprintf(buffer, SIZE, |
| 608 | " " |
| 609 | "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u]," |
| 610 | " freezeLock=%p, dq-q-time=%u us\n", |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 611 | mFormat, w0, h0, s0, w1, h1, s1, |
| 612 | getFreezeLock().get(), totalTime); |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 613 | |
| 614 | result.append(buffer); |
| 615 | } |
| 616 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 617 | // --------------------------------------------------------------------------- |
| 618 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 619 | Layer::ClientRef::ClientRef() |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 620 | : mControlBlock(0), mToken(-1) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | Layer::ClientRef::~ClientRef() { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | int32_t Layer::ClientRef::getToken() const { |
| 627 | Mutex::Autolock _l(mLock); |
| 628 | return mToken; |
| 629 | } |
| 630 | |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 631 | sp<UserClient> Layer::ClientRef::getClient() const { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 632 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 633 | return mUserClient.promote(); |
| 634 | } |
| 635 | |
| 636 | status_t Layer::ClientRef::setToken(const sp<UserClient>& uc, |
| 637 | const sp<SharedBufferServer>& sharedClient, int32_t token) { |
| 638 | Mutex::Autolock _l(mLock); |
| 639 | |
| 640 | { // scope for strong mUserClient reference |
| 641 | sp<UserClient> userClient(mUserClient.promote()); |
| 642 | if (mUserClient != 0 && mControlBlock != 0) { |
| 643 | mControlBlock->setStatus(NO_INIT); |
| 644 | } |
| 645 | } |
| 646 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 647 | mUserClient = uc; |
| 648 | mToken = token; |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 649 | mControlBlock = sharedClient; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 650 | return NO_ERROR; |
| 651 | } |
| 652 | |
| 653 | sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const { |
| 654 | return mUserClient.promote(); |
| 655 | } |
| 656 | |
| 657 | // this class gives us access to SharedBufferServer safely |
| 658 | // it makes sure the UserClient (and its associated shared memory) |
| 659 | // won't go away while we're accessing it. |
| 660 | Layer::ClientRef::Access::Access(const ClientRef& ref) |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 661 | : mControlBlock(0) |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 662 | { |
| 663 | Mutex::Autolock _l(ref.mLock); |
| 664 | mUserClientStrongRef = ref.mUserClient.promote(); |
| 665 | if (mUserClientStrongRef != 0) |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 666 | mControlBlock = ref.mControlBlock; |
| 667 | } |
| 668 | |
| 669 | Layer::ClientRef::Access::~Access() |
| 670 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | // --------------------------------------------------------------------------- |
| 674 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 675 | Layer::BufferManager::BufferManager(TextureManager& tm) |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 676 | : mNumBuffers(NUM_BUFFERS), mTextureManager(tm), |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 677 | mActiveBuffer(-1), mFailover(false) |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 678 | { |
| 679 | } |
| 680 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 681 | Layer::BufferManager::~BufferManager() |
| 682 | { |
| 683 | } |
| 684 | |
| 685 | status_t Layer::BufferManager::resize(size_t size) |
| 686 | { |
| 687 | Mutex::Autolock _l(mLock); |
| 688 | mNumBuffers = size; |
| 689 | return NO_ERROR; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | // only for debugging |
| 693 | sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const { |
| 694 | return mBufferData[index].buffer; |
| 695 | } |
| 696 | |
| 697 | status_t Layer::BufferManager::setActiveBufferIndex(size_t index) { |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 698 | mActiveBuffer = index; |
| 699 | return NO_ERROR; |
| 700 | } |
| 701 | |
| 702 | size_t Layer::BufferManager::getActiveBufferIndex() const { |
| 703 | return mActiveBuffer; |
| 704 | } |
| 705 | |
| 706 | Texture Layer::BufferManager::getActiveTexture() const { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 707 | Texture res; |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 708 | if (mFailover || mActiveBuffer<0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 709 | res = mFailoverTexture; |
| 710 | } else { |
| 711 | static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture; |
| 712 | } |
| 713 | return res; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 717 | sp<GraphicBuffer> result; |
| 718 | const ssize_t activeBuffer = mActiveBuffer; |
| 719 | if (activeBuffer >= 0) { |
| 720 | BufferData const * const buffers = mBufferData; |
| 721 | Mutex::Autolock _l(mLock); |
| 722 | result = buffers[activeBuffer].buffer; |
| 723 | } |
| 724 | return result; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index) |
| 728 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 729 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 730 | sp<GraphicBuffer> buffer; |
| 731 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 732 | buffer = buffers[index].buffer; |
| 733 | buffers[index].buffer = 0; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 734 | return buffer; |
| 735 | } |
| 736 | |
| 737 | status_t Layer::BufferManager::attachBuffer(size_t index, |
| 738 | const sp<GraphicBuffer>& buffer) |
| 739 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 740 | BufferData* const buffers = mBufferData; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 741 | Mutex::Autolock _l(mLock); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 742 | buffers[index].buffer = buffer; |
| 743 | buffers[index].texture.dirty = true; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 744 | return NO_ERROR; |
| 745 | } |
| 746 | |
| 747 | status_t Layer::BufferManager::destroy(EGLDisplay dpy) |
| 748 | { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 749 | BufferData* const buffers = mBufferData; |
| 750 | size_t num; |
| 751 | { // scope for the lock |
| 752 | Mutex::Autolock _l(mLock); |
| 753 | num = mNumBuffers; |
| 754 | for (size_t i=0 ; i<num ; i++) { |
| 755 | buffers[i].buffer = 0; |
| 756 | } |
| 757 | } |
| 758 | for (size_t i=0 ; i<num ; i++) { |
| 759 | destroyTexture(&buffers[i].texture, dpy); |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 760 | } |
| 761 | destroyTexture(&mFailoverTexture, dpy); |
| 762 | return NO_ERROR; |
| 763 | } |
| 764 | |
| 765 | status_t Layer::BufferManager::initEglImage(EGLDisplay dpy, |
| 766 | const sp<GraphicBuffer>& buffer) |
| 767 | { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 768 | status_t err = NO_INIT; |
| 769 | ssize_t index = mActiveBuffer; |
| 770 | if (index >= 0) { |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 771 | if (!mFailover) { |
| 772 | Image& texture(mBufferData[index].texture); |
| 773 | err = mTextureManager.initEglImage(&texture, dpy, buffer); |
| 774 | // if EGLImage fails, we switch to regular texture mode, and we |
| 775 | // free all resources associated with using EGLImages. |
| 776 | if (err == NO_ERROR) { |
| 777 | mFailover = false; |
| 778 | destroyTexture(&mFailoverTexture, dpy); |
| 779 | } else { |
| 780 | mFailover = true; |
| 781 | const size_t num = mNumBuffers; |
| 782 | for (size_t i=0 ; i<num ; i++) { |
| 783 | destroyTexture(&mBufferData[i].texture, dpy); |
| 784 | } |
Andreas Huber | e049a95 | 2010-06-25 09:25:19 -0700 | [diff] [blame] | 785 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 786 | } else { |
| 787 | // we failed once, don't try again |
| 788 | err = BAD_VALUE; |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | return err; |
| 792 | } |
| 793 | |
| 794 | status_t Layer::BufferManager::loadTexture( |
| 795 | const Region& dirty, const GGLSurface& t) |
| 796 | { |
| 797 | return mTextureManager.loadTexture(&mFailoverTexture, dirty, t); |
| 798 | } |
| 799 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 800 | status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy) |
| 801 | { |
| 802 | if (tex->name != -1U) { |
| 803 | glDeleteTextures(1, &tex->name); |
| 804 | tex->name = -1U; |
| 805 | } |
| 806 | if (tex->image != EGL_NO_IMAGE_KHR) { |
| 807 | eglDestroyImageKHR(dpy, tex->image); |
| 808 | tex->image = EGL_NO_IMAGE_KHR; |
| 809 | } |
| 810 | return NO_ERROR; |
| 811 | } |
| 812 | |
Mathias Agopian | d606de6 | 2010-05-10 20:06:11 -0700 | [diff] [blame] | 813 | // --------------------------------------------------------------------------- |
| 814 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 815 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 816 | const sp<Layer>& owner) |
| 817 | : Surface(flinger, owner->getIdentity(), owner) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 818 | { |
| 819 | } |
| 820 | |
| 821 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 822 | { |
| 823 | } |
| 824 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 825 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, |
| 826 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 827 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 828 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 829 | sp<Layer> owner(getOwner()); |
| 830 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 831 | /* |
| 832 | * requestBuffer() cannot be called from the main thread |
| 833 | * as it could cause a dead-lock, since it may have to wait |
| 834 | * on conditions updated my the main thread. |
| 835 | */ |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 836 | buffer = owner->requestBuffer(index, w, h, format, usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 837 | } |
| 838 | return buffer; |
| 839 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 841 | status_t Layer::SurfaceLayer::setBufferCount(int bufferCount) |
| 842 | { |
| 843 | status_t err = DEAD_OBJECT; |
| 844 | sp<Layer> owner(getOwner()); |
| 845 | if (owner != 0) { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 846 | /* |
| 847 | * setBufferCount() cannot be called from the main thread |
| 848 | * as it could cause a dead-lock, since it may have to wait |
| 849 | * on conditions updated my the main thread. |
| 850 | */ |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 851 | err = owner->setBufferCount(bufferCount); |
| 852 | } |
| 853 | return err; |
| 854 | } |
| 855 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | // --------------------------------------------------------------------------- |
| 857 | |
| 858 | |
| 859 | }; // namespace android |