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 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 31 | #include "Buffer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "clz.h" |
| 33 | #include "Layer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 50 | Layer::Layer(SurfaceFlinger* flinger, DisplayID display, |
| 51 | const sp<Client>& c, int32_t i) |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 52 | : LayerBaseClient(flinger, display, c, i), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | mSecure(false), |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 54 | mNeedsBlending(true) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | { |
| 56 | // no OpenGL operation is possible here, since we might not be |
| 57 | // in the OpenGL thread. |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 58 | mFrontBufferIndex = lcblk->getFrontBuffer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 65 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 66 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 67 | // called with SurfaceFlinger::mStateLock as soon as the layer is entered |
| 68 | // in the purgatory list |
| 69 | void Layer::onRemoved() |
| 70 | { |
| 71 | // wake up the condition |
| 72 | lcblk->setStatus(NO_INIT); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Layer::destroy() |
| 76 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 77 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 78 | if (mTextures[i].name != -1U) { |
Mathias Agopian | 550b79f | 2009-04-22 15:49:28 -0700 | [diff] [blame] | 79 | glDeleteTextures(1, &mTextures[i].name); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 80 | mTextures[i].name = -1U; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 81 | } |
| 82 | if (mTextures[i].image != EGL_NO_IMAGE_KHR) { |
| 83 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 84 | eglDestroyImageKHR(dpy, mTextures[i].image); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 85 | mTextures[i].image = EGL_NO_IMAGE_KHR; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 86 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 87 | Mutex::Autolock _l(mLock); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 88 | mBuffers[i].clear(); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 89 | mWidth = mHeight = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
Mathias Agopian | 759fdb2 | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 91 | mSurface.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 94 | sp<LayerBaseClient::Surface> Layer::createSurface() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | { |
| 96 | return mSurface; |
| 97 | } |
| 98 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 99 | status_t Layer::ditch() |
| 100 | { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 101 | // 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] | 102 | destroy(); |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 103 | return NO_ERROR; |
| 104 | } |
| 105 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 106 | 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] | 107 | PixelFormat format, uint32_t flags) |
| 108 | { |
| 109 | PixelFormatInfo info; |
| 110 | status_t err = getPixelFormatInfo(format, &info); |
| 111 | if (err) return err; |
| 112 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 113 | uint32_t bufferFlags = 0; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 114 | if (flags & ISurfaceComposer::eSecure) |
| 115 | bufferFlags |= Buffer::SECURE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 117 | mFormat = format; |
| 118 | mWidth = w; |
| 119 | mHeight = h; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 120 | mSecure = (bufferFlags & Buffer::SECURE) ? true : false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 122 | mBufferFlags = bufferFlags; |
| 123 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
| 124 | mBuffers[i] = new Buffer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 126 | mSurface = new SurfaceLayer(mFlinger, clientIndex(), this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | return NO_ERROR; |
| 128 | } |
| 129 | |
| 130 | void Layer::reloadTexture(const Region& dirty) |
| 131 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 132 | Mutex::Autolock _l(mLock); |
| 133 | sp<Buffer> buffer(getFrontBuffer()); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 134 | if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 135 | int index = mFrontBufferIndex; |
| 136 | if (LIKELY(!mTextures[index].dirty)) { |
| 137 | glBindTexture(GL_TEXTURE_2D, mTextures[index].name); |
| 138 | } else { |
| 139 | // we need to recreate the texture |
| 140 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 141 | |
| 142 | // create the new texture name if needed |
| 143 | if (UNLIKELY(mTextures[index].name == -1U)) { |
| 144 | mTextures[index].name = createTexture(); |
| 145 | } else { |
| 146 | glBindTexture(GL_TEXTURE_2D, mTextures[index].name); |
| 147 | } |
| 148 | |
| 149 | // free the previous image |
| 150 | if (mTextures[index].image != EGL_NO_IMAGE_KHR) { |
| 151 | eglDestroyImageKHR(dpy, mTextures[index].image); |
| 152 | mTextures[index].image = EGL_NO_IMAGE_KHR; |
| 153 | } |
| 154 | |
| 155 | // construct an EGL_NATIVE_BUFFER_ANDROID |
| 156 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 157 | |
| 158 | // create the new EGLImageKHR |
| 159 | const EGLint attrs[] = { |
| 160 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 161 | EGL_NONE, EGL_NONE |
| 162 | }; |
| 163 | mTextures[index].image = eglCreateImageKHR( |
| 164 | dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 165 | (EGLClientBuffer)clientBuf, attrs); |
| 166 | |
| 167 | LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR, |
| 168 | "eglCreateImageKHR() failed. err=0x%4x", |
| 169 | eglGetError()); |
| 170 | |
| 171 | if (mTextures[index].image != EGL_NO_IMAGE_KHR) { |
| 172 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, |
| 173 | (GLeglImageOES)mTextures[index].image); |
| 174 | GLint error = glGetError(); |
| 175 | if (UNLIKELY(error != GL_NO_ERROR)) { |
| 176 | // this failed, for instance, because we don't support |
| 177 | // NPOT. |
| 178 | // FIXME: do something! |
Mathias Agopian | 816d7d0 | 2009-09-14 18:10:30 -0700 | [diff] [blame^] | 179 | LOGD("layer=%p, glEGLImageTargetTexture2DOES(%d) " |
| 180 | "failed err=0x%04x", |
| 181 | this, mTextures[index].image, error); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 182 | mFlags &= ~DisplayHardware::DIRECT_TEXTURE; |
| 183 | } else { |
| 184 | // Everything went okay! |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 185 | mTextures[index].dirty = false; |
| 186 | mTextures[index].width = clientBuf->width; |
| 187 | mTextures[index].height = clientBuf->height; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | } else { |
| 192 | GGLSurface t; |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 193 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY); |
| 194 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 195 | res, strerror(res), buffer.get()); |
| 196 | if (res == NO_ERROR) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 197 | if (UNLIKELY(mTextures[0].name == -1U)) { |
| 198 | mTextures[0].name = createTexture(); |
| 199 | } |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 200 | loadTexture(&mTextures[0], mTextures[0].name, dirty, t); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 201 | buffer->unlock(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 202 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
| 207 | void Layer::onDraw(const Region& clip) const |
| 208 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 209 | const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ? |
| 210 | mFrontBufferIndex : 0; |
| 211 | GLuint textureName = mTextures[index].name; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 212 | if (UNLIKELY(textureName == -1LU)) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 213 | //LOGW("Layer %p doesn't have a texture", this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | // the texture has not been created yet, this Layer has |
| 215 | // in fact never been drawn into. this happens frequently with |
| 216 | // SurfaceView. |
| 217 | clearWithOpenGL(clip); |
| 218 | return; |
| 219 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 220 | |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 221 | drawWithOpenGL(clip, mTextures[index]); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 224 | sp<SurfaceBuffer> Layer::requestBuffer(int index, int usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 226 | sp<Buffer> buffer; |
| 227 | |
| 228 | // this ensures our client doesn't go away while we're accessing |
| 229 | // the shared area. |
| 230 | sp<Client> ourClient(client.promote()); |
| 231 | if (ourClient == 0) { |
| 232 | // oops, the client is already gone |
| 233 | return buffer; |
| 234 | } |
| 235 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 236 | /* |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 237 | * This is called from the client's Surface::dequeue(). This can happen |
| 238 | * at any time, especially while we're in the middle of using the |
| 239 | * buffer 'index' as our front buffer. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 240 | * |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 241 | * Make sure the buffer we're resizing is not the front buffer and has been |
| 242 | * dequeued. Once this condition is asserted, we are guaranteed that this |
| 243 | * buffer cannot become the front buffer under our feet, since we're called |
| 244 | * from Surface::dequeue() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 245 | */ |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 246 | status_t err = lcblk->assertReallocate(index); |
| 247 | LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 248 | if (err != NO_ERROR) { |
| 249 | // the surface may have died |
| 250 | return buffer; |
| 251 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 253 | uint32_t w, h; |
| 254 | { // scope for the lock |
| 255 | Mutex::Autolock _l(mLock); |
| 256 | w = mWidth; |
| 257 | h = mHeight; |
| 258 | buffer = mBuffers[index]; |
| 259 | mBuffers[index].clear(); |
| 260 | } |
| 261 | |
| 262 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 263 | if (buffer->getStrongCount() == 1) { |
| 264 | err = buffer->reallocate(w, h, mFormat, usage, mBufferFlags); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 265 | } else { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 266 | // here we have to reallocate a new buffer because we could have a |
| 267 | // client in our process with a reference to it (eg: status bar), |
| 268 | // and we can't release the handle under its feet. |
| 269 | buffer.clear(); |
| 270 | buffer = new Buffer(w, h, mFormat, usage, mBufferFlags); |
| 271 | err = buffer->initCheck(); |
| 272 | } |
| 273 | |
| 274 | if (err || buffer->handle == 0) { |
| 275 | LOGE_IF(err || buffer->handle == 0, |
| 276 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 277 | this, index, w, h, strerror(-err)); |
| 278 | } else { |
| 279 | LOGD_IF(DEBUG_RESIZE, |
| 280 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d", |
| 281 | this, index, w, h); |
| 282 | } |
| 283 | |
| 284 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 285 | Mutex::Autolock _l(mLock); |
| 286 | if (mWidth && mHeight) { |
| 287 | // and we have new buffer |
| 288 | mBuffers[index] = buffer; |
| 289 | // texture is now dirty... |
| 290 | mTextures[index].dirty = true; |
| 291 | } else { |
| 292 | // oops we got killed while we were allocating the buffer |
| 293 | buffer.clear(); |
| 294 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 295 | } |
| 296 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | uint32_t Layer::doTransaction(uint32_t flags) |
| 300 | { |
| 301 | const Layer::State& front(drawingState()); |
| 302 | const Layer::State& temp(currentState()); |
| 303 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | // Index of the back buffer |
| 305 | const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 307 | if (backbufferChanged) { |
| 308 | // 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] | 309 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 310 | "resize (layer=%p), requested (%dx%d), " |
| 311 | "drawing (%d,%d), (%dx%d), (%dx%d)", |
| 312 | this, int(temp.w), int(temp.h), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | int(drawingState().w), int(drawingState().h), |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 314 | int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()), |
| 315 | int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 317 | // record the new size, form this point on, when the client request a |
| 318 | // buffer, it'll get the new size. |
| 319 | setDrawingSize(temp.w, temp.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 321 | // all buffers need reallocation |
| 322 | lcblk->reallocate(); |
| 323 | |
| 324 | // recompute the visible region |
| 325 | // FIXME: ideally we would do that only when we have received |
| 326 | // a buffer of the right size |
| 327 | flags |= Layer::eVisibleRegion; |
| 328 | this->contentDirty = true; |
| 329 | |
| 330 | #if 0 |
| 331 | // FIXME: handle freeze lock |
| 332 | // we're being resized and there is a freeze display request, |
| 333 | // acquire a freeze lock, so that the screen stays put |
| 334 | // until we've redrawn at the new size; this is to avoid |
| 335 | // glitches upon orientation changes. |
| 336 | if (mFlinger->hasFreezeRequest()) { |
| 337 | // if the surface is hidden, don't try to acquire the |
| 338 | // freeze lock, since hidden surfaces may never redraw |
| 339 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 340 | mFreezeLock = mFlinger->getFreezeLock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
| 342 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 343 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 345 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | if (temp.sequence != front.sequence) { |
| 347 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 348 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 349 | // (it may never redraw, which is fine if it is hidden) |
| 350 | mFreezeLock.clear(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return LayerBase::doTransaction(flags); |
| 355 | } |
| 356 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 357 | void Layer::setDrawingSize(uint32_t w, uint32_t h) { |
| 358 | Mutex::Autolock _l(mLock); |
| 359 | mWidth = w; |
| 360 | mHeight = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // ---------------------------------------------------------------------------- |
| 364 | // pageflip handling... |
| 365 | // ---------------------------------------------------------------------------- |
| 366 | |
| 367 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 368 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 369 | ssize_t buf = lcblk->retireAndLock(); |
| 370 | if (buf < NO_ERROR) { |
| 371 | //LOGW("nothing to retire (%s)", strerror(-buf)); |
| 372 | // NOTE: here the buffer is locked because we will used |
| 373 | // for composition later in the loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | return; |
| 375 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 376 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 377 | // we retired a buffer, which becomes the new front buffer |
| 378 | mFrontBufferIndex = buf; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 380 | // get the dirty region |
| 381 | sp<Buffer> newFrontBuffer(getBuffer(buf)); |
| 382 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 383 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 384 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 385 | // FIXME: signal an event if we have more buffers waiting |
| 386 | // mFlinger->signalEvent(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 388 | reloadTexture( mPostedDirtyRegion ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void Layer::unlockPageFlip( |
| 392 | const Transform& planeTransform, Region& outDirtyRegion) |
| 393 | { |
| 394 | Region dirtyRegion(mPostedDirtyRegion); |
| 395 | if (!dirtyRegion.isEmpty()) { |
| 396 | mPostedDirtyRegion.clear(); |
| 397 | // The dirty region is given in the layer's coordinate space |
| 398 | // transform the dirty region by the surface's transformation |
| 399 | // and the global transformation. |
| 400 | const Layer::State& s(drawingState()); |
| 401 | const Transform tr(planeTransform * s.transform); |
| 402 | dirtyRegion = tr.transform(dirtyRegion); |
| 403 | |
| 404 | // At this point, the dirty region is in screen space. |
| 405 | // Make sure it's constrained by the visible region (which |
| 406 | // is in screen space as well). |
| 407 | dirtyRegion.andSelf(visibleRegionScreen); |
| 408 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | void Layer::finishPageFlip() |
| 413 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 414 | status_t err = lcblk->unlock( mFrontBufferIndex ); |
| 415 | LOGE_IF(err!=NO_ERROR, |
| 416 | "layer %p, buffer=%d wasn't locked!", |
| 417 | this, mFrontBufferIndex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 420 | // --------------------------------------------------------------------------- |
| 421 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 422 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
| 423 | SurfaceID id, const sp<Layer>& owner) |
| 424 | : Surface(flinger, id, owner->getIdentity(), owner) |
| 425 | { |
| 426 | } |
| 427 | |
| 428 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 429 | { |
| 430 | } |
| 431 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 432 | sp<SurfaceBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 433 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 434 | sp<SurfaceBuffer> buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 435 | sp<Layer> owner(getOwner()); |
| 436 | if (owner != 0) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 437 | LOGE_IF(uint32_t(index)>=NUM_BUFFERS, |
| 438 | "getBuffer() index (%d) out of range", index); |
| 439 | if (uint32_t(index) < NUM_BUFFERS) { |
| 440 | buffer = owner->requestBuffer(index, usage); |
| 441 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 442 | } |
| 443 | return buffer; |
| 444 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | |
| 446 | // --------------------------------------------------------------------------- |
| 447 | |
| 448 | |
| 449 | }; // namespace android |