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