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 | |
| 28 | #include <ui/PixelFormat.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 29 | #include <ui/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include "clz.h" |
| 32 | #include "Layer.h" |
| 33 | #include "LayerBitmap.h" |
| 34 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include "DisplayHardware/DisplayHardware.h" |
| 36 | |
| 37 | |
| 38 | #define DEBUG_RESIZE 0 |
| 39 | |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | // --------------------------------------------------------------------------- |
| 44 | |
| 45 | const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4; |
| 46 | const char* const Layer::typeID = "Layer"; |
| 47 | |
| 48 | // --------------------------------------------------------------------------- |
| 49 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 50 | Layer::Layer(SurfaceFlinger* flinger, DisplayID display, const sp<Client>& c, int32_t i) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | : LayerBaseClient(flinger, display, c, i), |
| 52 | mSecure(false), |
| 53 | mFrontBufferIndex(1), |
| 54 | mNeedsBlending(true), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 55 | mResizeTransactionDone(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | { |
| 57 | // no OpenGL operation is possible here, since we might not be |
| 58 | // in the OpenGL thread. |
| 59 | } |
| 60 | |
| 61 | Layer::~Layer() |
| 62 | { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 63 | destroy(); |
| 64 | // the actual buffers will be destroyed here |
| 65 | } |
| 66 | |
| 67 | void Layer::destroy() |
| 68 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 69 | for (int i=0 ; i<NUM_BUFFERS ; i++) { |
| 70 | if (mTextures[i].name != -1U) { |
Mathias Agopian | 550b79f | 2009-04-22 15:49:28 -0700 | [diff] [blame] | 71 | glDeleteTextures(1, &mTextures[i].name); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 72 | mTextures[i].name = -1U; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 73 | } |
| 74 | if (mTextures[i].image != EGL_NO_IMAGE_KHR) { |
| 75 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 76 | eglDestroyImageKHR(dpy, mTextures[i].image); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 77 | mTextures[i].image = EGL_NO_IMAGE_KHR; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 78 | } |
Mathias Agopian | 759fdb2 | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 79 | mBuffers[i].free(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
Mathias Agopian | 759fdb2 | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 81 | mSurface.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void Layer::initStates(uint32_t w, uint32_t h, uint32_t flags) |
| 85 | { |
| 86 | LayerBase::initStates(w,h,flags); |
| 87 | |
| 88 | if (flags & ISurfaceComposer::eDestroyBackbuffer) |
| 89 | lcblk->flags |= eNoCopyBack; |
| 90 | } |
| 91 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 92 | sp<LayerBaseClient::Surface> Layer::createSurface() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | { |
| 94 | return mSurface; |
| 95 | } |
| 96 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 97 | status_t Layer::ditch() |
| 98 | { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 99 | // the layer is not on screen anymore. free as much resources as possible |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 100 | destroy(); |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 101 | return NO_ERROR; |
| 102 | } |
| 103 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 104 | 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] | 105 | PixelFormat format, uint32_t flags) |
| 106 | { |
| 107 | PixelFormatInfo info; |
| 108 | status_t err = getPixelFormatInfo(format, &info); |
| 109 | if (err) return err; |
| 110 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | uint32_t bufferFlags = 0; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 112 | if (flags & ISurfaceComposer::eSecure) |
| 113 | bufferFlags |= Buffer::SECURE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 115 | mSecure = (bufferFlags & Buffer::SECURE) ? true : false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | for (int i=0 ; i<2 ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 118 | err = mBuffers[i].init(lcblk->surface + i, w, h, format, bufferFlags); |
| 119 | if (err != NO_ERROR) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | return err; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 121 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | } |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 123 | mSurface = new SurfaceLayer(mFlinger, clientIndex(), this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | return NO_ERROR; |
| 125 | } |
| 126 | |
| 127 | void Layer::reloadTexture(const Region& dirty) |
| 128 | { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 129 | const sp<Buffer>& buffer(frontBuffer().getBuffer()); |
| 130 | if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 131 | int index = mFrontBufferIndex; |
| 132 | if (LIKELY(!mTextures[index].dirty)) { |
| 133 | glBindTexture(GL_TEXTURE_2D, mTextures[index].name); |
| 134 | } else { |
| 135 | // we need to recreate the texture |
| 136 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 137 | |
| 138 | // create the new texture name if needed |
| 139 | if (UNLIKELY(mTextures[index].name == -1U)) { |
| 140 | mTextures[index].name = createTexture(); |
| 141 | } else { |
| 142 | glBindTexture(GL_TEXTURE_2D, mTextures[index].name); |
| 143 | } |
| 144 | |
| 145 | // free the previous image |
| 146 | if (mTextures[index].image != EGL_NO_IMAGE_KHR) { |
| 147 | eglDestroyImageKHR(dpy, mTextures[index].image); |
| 148 | mTextures[index].image = EGL_NO_IMAGE_KHR; |
| 149 | } |
| 150 | |
| 151 | // construct an EGL_NATIVE_BUFFER_ANDROID |
| 152 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 153 | |
| 154 | // create the new EGLImageKHR |
| 155 | const EGLint attrs[] = { |
| 156 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 157 | EGL_NONE, EGL_NONE |
| 158 | }; |
| 159 | mTextures[index].image = eglCreateImageKHR( |
| 160 | dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 161 | (EGLClientBuffer)clientBuf, attrs); |
| 162 | |
| 163 | LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR, |
| 164 | "eglCreateImageKHR() failed. err=0x%4x", |
| 165 | eglGetError()); |
| 166 | |
| 167 | if (mTextures[index].image != EGL_NO_IMAGE_KHR) { |
| 168 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, |
| 169 | (GLeglImageOES)mTextures[index].image); |
| 170 | GLint error = glGetError(); |
| 171 | if (UNLIKELY(error != GL_NO_ERROR)) { |
| 172 | // this failed, for instance, because we don't support |
| 173 | // NPOT. |
| 174 | // FIXME: do something! |
| 175 | mFlags &= ~DisplayHardware::DIRECT_TEXTURE; |
| 176 | } else { |
| 177 | // Everything went okay! |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 178 | mTextures[index].dirty = false; |
| 179 | mTextures[index].width = clientBuf->width; |
| 180 | mTextures[index].height = clientBuf->height; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | } else { |
| 185 | GGLSurface t; |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 186 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY); |
| 187 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 188 | res, strerror(res), buffer.get()); |
| 189 | if (res == NO_ERROR) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 190 | if (UNLIKELY(mTextures[0].name == -1U)) { |
| 191 | mTextures[0].name = createTexture(); |
| 192 | } |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 193 | loadTexture(&mTextures[0], mTextures[0].name, dirty, t); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 194 | buffer->unlock(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 195 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
| 200 | void Layer::onDraw(const Region& clip) const |
| 201 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 202 | const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ? |
| 203 | mFrontBufferIndex : 0; |
| 204 | GLuint textureName = mTextures[index].name; |
| 205 | |
| 206 | if (UNLIKELY(textureName == -1LU)) { |
| 207 | LOGW("Layer %p doesn't have a texture", this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | // the texture has not been created yet, this Layer has |
| 209 | // in fact never been drawn into. this happens frequently with |
| 210 | // SurfaceView. |
| 211 | clearWithOpenGL(clip); |
| 212 | return; |
| 213 | } |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 214 | drawWithOpenGL(clip, mTextures[index]); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 217 | sp<SurfaceBuffer> Layer::peekBuffer(int usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 219 | /* |
| 220 | * This is called from the client's Surface::lock(), after it locked |
| 221 | * the surface successfully. We're therefore guaranteed that the |
| 222 | * back-buffer is not in use by ourselves. |
| 223 | * Of course, we need to validate all this, which is not trivial. |
| 224 | * |
| 225 | * FIXME: A resize could happen at any time here. What to do about this? |
| 226 | * - resize() form post() |
| 227 | * - resize() from doTransaction() |
| 228 | * |
| 229 | * We'll probably need an internal lock for this. |
| 230 | * |
| 231 | * |
| 232 | * TODO: We need to make sure that post() doesn't swap |
| 233 | * the buffers under us. |
| 234 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 236 | // it's okay to read swapState for the purpose of figuring out the |
| 237 | // backbuffer index, which cannot change (since the app has locked it). |
| 238 | const uint32_t state = lcblk->swapState; |
| 239 | const int32_t backBufferIndex = layer_cblk_t::backBuffer(state); |
| 240 | |
| 241 | // get rid of the EGL image, since we shouldn't need it anymore |
| 242 | // (note that we're in a different thread than where it is being used) |
| 243 | if (mTextures[backBufferIndex].image != EGL_NO_IMAGE_KHR) { |
| 244 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 245 | eglDestroyImageKHR(dpy, mTextures[backBufferIndex].image); |
| 246 | mTextures[backBufferIndex].image = EGL_NO_IMAGE_KHR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 248 | |
| 249 | LayerBitmap& layerBitmap(mBuffers[backBufferIndex]); |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 250 | sp<SurfaceBuffer> buffer = layerBitmap.allocate(usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 251 | |
| 252 | LOGD_IF(DEBUG_RESIZE, |
| 253 | "Layer::getBuffer(this=%p), index=%d, (%d,%d), (%d,%d)", |
| 254 | this, backBufferIndex, |
| 255 | layerBitmap.getWidth(), |
| 256 | layerBitmap.getHeight(), |
| 257 | layerBitmap.getBuffer()->getWidth(), |
| 258 | layerBitmap.getBuffer()->getHeight()); |
| 259 | |
| 260 | if (UNLIKELY(buffer == 0)) { |
| 261 | // XXX: what to do, what to do? |
| 262 | } else { |
| 263 | // texture is now dirty... |
| 264 | mTextures[backBufferIndex].dirty = true; |
| 265 | // ... so it the visible region (because we consider the surface's |
| 266 | // buffer size for visibility calculations) |
| 267 | forceVisibilityTransaction(); |
| 268 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 269 | } |
| 270 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 273 | void Layer::scheduleBroadcast() |
| 274 | { |
| 275 | sp<Client> ourClient(client.promote()); |
| 276 | if (ourClient != 0) { |
| 277 | mFlinger->scheduleBroadcast(ourClient); |
| 278 | } |
| 279 | } |
| 280 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | uint32_t Layer::doTransaction(uint32_t flags) |
| 282 | { |
| 283 | const Layer::State& front(drawingState()); |
| 284 | const Layer::State& temp(currentState()); |
| 285 | |
| 286 | // the test front.{w|h} != temp.{w|h} is not enough because it is possible |
| 287 | // that the size changed back to its previous value before the buffer |
| 288 | // was resized (in the eLocked case below), in which case, we still |
| 289 | // need to execute the code below so the clients have a chance to be |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 290 | // release. resize() deals with the fact that the size can be the same. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | |
| 292 | /* |
| 293 | * Various states we could be in... |
| 294 | |
| 295 | resize = state & eResizeRequested; |
| 296 | if (backbufferChanged) { |
| 297 | if (resize == 0) { |
| 298 | // ERROR, the resized buffer doesn't have its resize flag set |
| 299 | } else if (resize == mask) { |
| 300 | // ERROR one of the buffer has already been resized |
| 301 | } else if (resize == mask ^ eResizeRequested) { |
| 302 | // ERROR, the resized buffer doesn't have its resize flag set |
| 303 | } else if (resize == eResizeRequested) { |
| 304 | // OK, Normal case, proceed with resize |
| 305 | } |
| 306 | } else { |
| 307 | if (resize == 0) { |
| 308 | // OK, nothing special, do nothing |
| 309 | } else if (resize == mask) { |
| 310 | // restarted transaction, do nothing |
| 311 | } else if (resize == mask ^ eResizeRequested) { |
| 312 | // restarted transaction, do nothing |
| 313 | } else if (resize == eResizeRequested) { |
| 314 | // OK, size reset to previous value, proceed with resize |
| 315 | } |
| 316 | } |
| 317 | */ |
| 318 | |
| 319 | // Index of the back buffer |
| 320 | const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h); |
| 321 | const uint32_t state = lcblk->swapState; |
| 322 | const int32_t clientBackBufferIndex = layer_cblk_t::backBuffer(state); |
| 323 | const uint32_t mask = clientBackBufferIndex ? eResizeBuffer1 : eResizeBuffer0; |
| 324 | uint32_t resizeFlags = state & eResizeRequested; |
| 325 | |
| 326 | if (UNLIKELY(backbufferChanged && (resizeFlags != eResizeRequested))) { |
| 327 | LOGE( "backbuffer size changed, but both resize flags are not set! " |
| 328 | "(layer=%p), state=%08x, requested (%dx%d), drawing (%d,%d), " |
| 329 | "index=%d, (%dx%d), (%dx%d)", |
| 330 | this, state, |
| 331 | int(temp.w), int(temp.h), |
| 332 | int(drawingState().w), int(drawingState().h), |
| 333 | int(clientBackBufferIndex), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 334 | int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()), |
| 335 | int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | // if we get there we're pretty screwed. the only reasonable |
| 337 | // thing to do is to pretend we should do the resize since |
| 338 | // backbufferChanged is set (this also will give a chance to |
| 339 | // client to get unblocked) |
| 340 | resizeFlags = eResizeRequested; |
| 341 | } |
| 342 | |
| 343 | if (resizeFlags == eResizeRequested) { |
| 344 | // NOTE: asserting that clientBackBufferIndex!=mFrontBufferIndex |
| 345 | // here, would be wrong and misleading because by this point |
| 346 | // mFrontBufferIndex has not been updated yet. |
| 347 | |
| 348 | LOGD_IF(DEBUG_RESIZE, |
| 349 | "resize (layer=%p), state=%08x, " |
| 350 | "requested (%dx%d), " |
| 351 | "drawing (%d,%d), " |
| 352 | "index=%d, (%dx%d), (%dx%d)", |
| 353 | this, state, |
| 354 | int(temp.w), int(temp.h), |
| 355 | int(drawingState().w), int(drawingState().h), |
| 356 | int(clientBackBufferIndex), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 357 | int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()), |
| 358 | int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | |
| 360 | if (state & eLocked) { |
| 361 | // if the buffer is locked, we can't resize anything because |
| 362 | // - the backbuffer is currently in use by the user |
| 363 | // - the front buffer is being shown |
| 364 | // We just act as if the transaction didn't happen and we |
| 365 | // reschedule it later... |
| 366 | flags |= eRestartTransaction; |
| 367 | } else { |
| 368 | // This buffer needs to be resized |
| 369 | status_t err = |
| 370 | resize(clientBackBufferIndex, temp.w, temp.h, "transaction"); |
| 371 | if (err == NO_ERROR) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 372 | const uint32_t mask = clientBackBufferIndex ? |
| 373 | eResizeBuffer1 : eResizeBuffer0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | android_atomic_and(~mask, &(lcblk->swapState)); |
| 375 | // since a buffer became available, we can let the client go... |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 376 | scheduleBroadcast(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | mResizeTransactionDone = true; |
| 378 | |
| 379 | // we're being resized and there is a freeze display request, |
| 380 | // acquire a freeze lock, so that the screen stays put |
| 381 | // until we've redrawn at the new size; this is to avoid |
| 382 | // glitches upon orientation changes. |
| 383 | if (mFlinger->hasFreezeRequest()) { |
| 384 | // if the surface is hidden, don't try to acquire the |
| 385 | // freeze lock, since hidden surfaces may never redraw |
| 386 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 387 | mFreezeLock = mFlinger->getFreezeLock(); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (temp.sequence != front.sequence) { |
| 395 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 396 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 397 | // (it may never redraw, which is fine if it is hidden) |
| 398 | mFreezeLock.clear(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return LayerBase::doTransaction(flags); |
| 403 | } |
| 404 | |
| 405 | status_t Layer::resize( |
| 406 | int32_t clientBackBufferIndex, |
| 407 | uint32_t width, uint32_t height, |
| 408 | const char* what) |
| 409 | { |
| 410 | /* |
| 411 | * handle resize (backbuffer and frontbuffer reallocation) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 412 | * this is called from post() or from doTransaction() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | */ |
| 414 | |
| 415 | const LayerBitmap& clientBackBuffer(mBuffers[clientBackBufferIndex]); |
| 416 | |
| 417 | // if the new (transaction) size is != from the the backbuffer |
| 418 | // then we need to reallocate the backbuffer |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 419 | bool backbufferChanged = (clientBackBuffer.getWidth() != width) || |
| 420 | (clientBackBuffer.getHeight() != height); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | |
| 422 | LOGD_IF(!backbufferChanged, |
| 423 | "(%s) eResizeRequested (layer=%p), but size not changed: " |
| 424 | "requested (%dx%d), drawing (%d,%d), current (%d,%d)," |
| 425 | "state=%08lx, index=%d, (%dx%d), (%dx%d)", |
| 426 | what, this, |
| 427 | int(width), int(height), |
| 428 | int(drawingState().w), int(drawingState().h), |
| 429 | int(currentState().w), int(currentState().h), |
| 430 | long(lcblk->swapState), |
| 431 | int(clientBackBufferIndex), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 432 | int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()), |
| 433 | int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | |
| 435 | // this can happen when changing the size back and forth quickly |
| 436 | status_t err = NO_ERROR; |
| 437 | if (backbufferChanged) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 438 | |
| 439 | LOGD_IF(DEBUG_RESIZE, |
| 440 | "resize (layer=%p), requested (%dx%d), " |
| 441 | "index=%d, (%dx%d), (%dx%d)", |
| 442 | this, int(width), int(height), int(clientBackBufferIndex), |
| 443 | int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()), |
| 444 | int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight())); |
| 445 | |
| 446 | err = mBuffers[clientBackBufferIndex].setSize(width, height); |
| 447 | if (UNLIKELY(err != NO_ERROR)) { |
| 448 | // This really should never happen |
| 449 | LOGE("resizing buffer %d to (%u,%u) failed [%08x] %s", |
| 450 | clientBackBufferIndex, width, height, err, strerror(err)); |
| 451 | // couldn't reallocate the surface |
| 452 | android_atomic_write(eInvalidSurface, &lcblk->swapState); |
| 453 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | } |
| 455 | return err; |
| 456 | } |
| 457 | |
| 458 | void Layer::setSizeChanged(uint32_t w, uint32_t h) |
| 459 | { |
| 460 | LOGD_IF(DEBUG_RESIZE, |
| 461 | "setSizeChanged w=%d, h=%d (old: w=%d, h=%d)", |
| 462 | w, h, mCurrentState.w, mCurrentState.h); |
| 463 | android_atomic_or(eResizeRequested, &(lcblk->swapState)); |
| 464 | } |
| 465 | |
| 466 | // ---------------------------------------------------------------------------- |
| 467 | // pageflip handling... |
| 468 | // ---------------------------------------------------------------------------- |
| 469 | |
| 470 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 471 | { |
| 472 | uint32_t state = android_atomic_or(eBusy, &(lcblk->swapState)); |
| 473 | // preemptively block the client, because he might set |
| 474 | // eFlipRequested at any time and want to use this buffer |
| 475 | // for the next frame. This will be unset below if it |
| 476 | // turns out we didn't need it. |
| 477 | |
| 478 | uint32_t mask = eInvalidSurface | eFlipRequested | eResizeRequested; |
| 479 | if (!(state & mask)) |
| 480 | return; |
| 481 | |
| 482 | if (UNLIKELY(state & eInvalidSurface)) { |
| 483 | // if eInvalidSurface is set, this means the surface |
| 484 | // became invalid during a transaction (NO_MEMORY for instance) |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 485 | scheduleBroadcast(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | |
| 489 | if (UNLIKELY(state & eFlipRequested)) { |
| 490 | uint32_t oldState; |
| 491 | mPostedDirtyRegion = post(&oldState, recomputeVisibleRegions); |
| 492 | if (oldState & eNextFlipPending) { |
| 493 | // Process another round (we know at least a buffer |
| 494 | // is ready for that client). |
| 495 | mFlinger->signalEvent(); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | Region Layer::post(uint32_t* previousSate, bool& recomputeVisibleRegions) |
| 501 | { |
| 502 | // atomically swap buffers and (re)set eFlipRequested |
| 503 | int32_t oldValue, newValue; |
| 504 | layer_cblk_t * const lcblk = this->lcblk; |
| 505 | do { |
| 506 | oldValue = lcblk->swapState; |
| 507 | // get the current value |
| 508 | |
| 509 | LOG_ASSERT(oldValue&eFlipRequested, |
| 510 | "eFlipRequested not set, yet we're flipping! (state=0x%08lx)", |
| 511 | long(oldValue)); |
| 512 | |
| 513 | newValue = (oldValue ^ eIndex); |
| 514 | // swap buffers |
| 515 | |
| 516 | newValue &= ~(eFlipRequested | eNextFlipPending); |
| 517 | // clear eFlipRequested and eNextFlipPending |
| 518 | |
| 519 | if (oldValue & eNextFlipPending) |
| 520 | newValue |= eFlipRequested; |
| 521 | // if eNextFlipPending is set (second buffer already has something |
| 522 | // in it) we need to reset eFlipRequested because the client |
| 523 | // might never do it |
| 524 | |
| 525 | } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState))); |
| 526 | *previousSate = oldValue; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 527 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | const int32_t index = (newValue & eIndex) ^ 1; |
| 529 | mFrontBufferIndex = index; |
| 530 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 531 | /* NOTE: it's safe to set this flag here because this is only touched |
| 532 | * from LayerBitmap::allocate(), which by construction cannot happen |
| 533 | * while we're in post(). |
| 534 | */ |
| 535 | lcblk->surface[index].flags &= ~surface_info_t::eBufferDirty; |
| 536 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | // ... post the new front-buffer |
| 538 | Region dirty(lcblk->region + index); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 539 | dirty.andSelf(frontBuffer().getBounds()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 541 | //LOGD("Did post oldValue=%08lx, newValue=%08lx, mFrontBufferIndex=%u\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | // oldValue, newValue, mFrontBufferIndex); |
| 543 | //dirty.dump("dirty"); |
| 544 | |
| 545 | if (UNLIKELY(oldValue & eResizeRequested)) { |
| 546 | |
| 547 | LOGD_IF(DEBUG_RESIZE, |
| 548 | "post (layer=%p), state=%08x, " |
| 549 | "index=%d, (%dx%d), (%dx%d)", |
| 550 | this, newValue, |
| 551 | int(1-index), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 552 | int(mBuffers[0].getWidth()), int(mBuffers[0].getHeight()), |
| 553 | int(mBuffers[1].getWidth()), int(mBuffers[1].getHeight())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | |
| 555 | // here, we just posted the surface and we have resolved |
| 556 | // the front/back buffer indices. The client is blocked, so |
| 557 | // it cannot start using the new backbuffer. |
| 558 | |
| 559 | // If the backbuffer was resized in THIS round, we actually cannot |
| 560 | // resize the frontbuffer because it has *just* been drawn (and we |
| 561 | // would have nothing to draw). In this case we just skip the resize |
| 562 | // it'll happen after the next page flip or during the next |
| 563 | // transaction. |
| 564 | |
| 565 | const uint32_t mask = (1-index) ? eResizeBuffer1 : eResizeBuffer0; |
| 566 | if (mResizeTransactionDone && (newValue & mask)) { |
| 567 | // Resize the layer's second buffer only if the transaction |
| 568 | // happened. It may not have happened yet if eResizeRequested |
| 569 | // was set immediately after the "transactionRequested" test, |
| 570 | // in which case the drawing state's size would be wrong. |
| 571 | mFreezeLock.clear(); |
| 572 | const Layer::State& s(drawingState()); |
| 573 | if (resize(1-index, s.w, s.h, "post") == NO_ERROR) { |
| 574 | do { |
| 575 | oldValue = lcblk->swapState; |
| 576 | if ((oldValue & eResizeRequested) == eResizeRequested) { |
| 577 | // ugh, another resize was requested since we processed |
| 578 | // the first buffer, don't free the client, and let |
| 579 | // the next transaction handle everything. |
| 580 | break; |
| 581 | } |
| 582 | newValue = oldValue & ~mask; |
| 583 | } while(android_atomic_cmpxchg(oldValue, newValue, &(lcblk->swapState))); |
| 584 | } |
| 585 | mResizeTransactionDone = false; |
| 586 | recomputeVisibleRegions = true; |
| 587 | this->contentDirty = true; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | reloadTexture(dirty); |
| 592 | |
| 593 | return dirty; |
| 594 | } |
| 595 | |
| 596 | Point Layer::getPhysicalSize() const |
| 597 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 598 | sp<const Buffer> front(frontBuffer().getBuffer()); |
Mathias Agopian | d66a979 | 2009-08-13 19:08:00 -0700 | [diff] [blame] | 599 | Point size(front->getWidth(), front->getHeight()); |
| 600 | if ((size.x | size.y) == 0) { |
| 601 | // if we don't have a buffer yet, just use the state's size. |
| 602 | size = LayerBase::getPhysicalSize(); |
| 603 | } |
| 604 | return size; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void Layer::unlockPageFlip( |
| 608 | const Transform& planeTransform, Region& outDirtyRegion) |
| 609 | { |
| 610 | Region dirtyRegion(mPostedDirtyRegion); |
| 611 | if (!dirtyRegion.isEmpty()) { |
| 612 | mPostedDirtyRegion.clear(); |
| 613 | // The dirty region is given in the layer's coordinate space |
| 614 | // transform the dirty region by the surface's transformation |
| 615 | // and the global transformation. |
| 616 | const Layer::State& s(drawingState()); |
| 617 | const Transform tr(planeTransform * s.transform); |
| 618 | dirtyRegion = tr.transform(dirtyRegion); |
| 619 | |
| 620 | // At this point, the dirty region is in screen space. |
| 621 | // Make sure it's constrained by the visible region (which |
| 622 | // is in screen space as well). |
| 623 | dirtyRegion.andSelf(visibleRegionScreen); |
| 624 | outDirtyRegion.orSelf(dirtyRegion); |
| 625 | |
| 626 | // client could be blocked, so signal them so they get a |
| 627 | // chance to reevaluate their condition. |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 628 | scheduleBroadcast(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | void Layer::finishPageFlip() |
| 633 | { |
| 634 | if (LIKELY(!(lcblk->swapState & eInvalidSurface))) { |
| 635 | LOGE_IF(!(lcblk->swapState & eBusy), |
| 636 | "layer %p wasn't locked!", this); |
| 637 | android_atomic_and(~eBusy, &(lcblk->swapState)); |
| 638 | } |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 639 | scheduleBroadcast(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 642 | // --------------------------------------------------------------------------- |
| 643 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 644 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
| 645 | SurfaceID id, const sp<Layer>& owner) |
| 646 | : Surface(flinger, id, owner->getIdentity(), owner) |
| 647 | { |
| 648 | } |
| 649 | |
| 650 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 651 | { |
| 652 | } |
| 653 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 654 | sp<SurfaceBuffer> Layer::SurfaceLayer::getBuffer(int usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 655 | { |
| 656 | sp<SurfaceBuffer> buffer = 0; |
| 657 | sp<Layer> owner(getOwner()); |
| 658 | if (owner != 0) { |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 659 | buffer = owner->peekBuffer(usage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 660 | } |
| 661 | return buffer; |
| 662 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | |
| 664 | // --------------------------------------------------------------------------- |
| 665 | |
| 666 | |
| 667 | }; // namespace android |