The Android Open Source Project | 9066cfe | 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 | 9066cfe | 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 | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
The Android Open Source Project | 9066cfe | 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 | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 28 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <ui/PixelFormat.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | #include <ui/Surface.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | #include "clz.h" |
| 33 | #include "Layer.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include "SurfaceFlinger.h" |
The Android Open Source Project | 9066cfe | 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 | 9779b22 | 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 | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 52 | : LayerBaseClient(flinger, display, c, i), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | mSecure(false), |
Mathias Agopian | 351a707 | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 54 | mNoEGLImageForSwBuffers(false), |
Mathias Agopian | cc93476 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 55 | mNeedsBlending(true), |
| 56 | mNeedsDithering(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | { |
| 58 | // no OpenGL operation is possible here, since we might not be |
| 59 | // in the OpenGL thread. |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 60 | mFrontBufferIndex = lcblk->getFrontBuffer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Layer::~Layer() |
| 64 | { |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 65 | destroy(); |
| 66 | // the actual buffers will be destroyed here |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 67 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 68 | |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 69 | void Layer::destroy() |
| 70 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 71 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 72 | if (mTextures[i].name != -1U) { |
Mathias Agopian | 81b0aa6 | 2009-04-22 15:49:28 -0700 | [diff] [blame] | 73 | glDeleteTextures(1, &mTextures[i].name); |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 74 | mTextures[i].name = -1U; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 75 | } |
| 76 | if (mTextures[i].image != EGL_NO_IMAGE_KHR) { |
| 77 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 78 | eglDestroyImageKHR(dpy, mTextures[i].image); |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 79 | mTextures[i].image = EGL_NO_IMAGE_KHR; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 80 | } |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 81 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 82 | mBuffers[i].clear(); |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 83 | mWidth = mHeight = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
Mathias Agopian | 2e4b68d | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 85 | mSurface.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 88 | sp<LayerBaseClient::Surface> Layer::createSurface() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | { |
| 90 | return mSurface; |
| 91 | } |
| 92 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 93 | status_t Layer::ditch() |
| 94 | { |
Mathias Agopian | a3aa6c9 | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 95 | // the layer is not on screen anymore. free as much resources as possible |
Mathias Agopian | 2e4b68d | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 96 | destroy(); |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 97 | return NO_ERROR; |
| 98 | } |
| 99 | |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 100 | status_t Layer::setBuffers( uint32_t w, uint32_t h, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | PixelFormat format, uint32_t flags) |
| 102 | { |
Mathias Agopian | cc93476 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 103 | // this surfaces pixel format |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | PixelFormatInfo info; |
| 105 | status_t err = getPixelFormatInfo(format, &info); |
| 106 | if (err) return err; |
| 107 | |
Mathias Agopian | cc93476 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 108 | // the display's pixel format |
| 109 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 110 | PixelFormatInfo displayInfo; |
| 111 | getPixelFormatInfo(hw.getFormat(), &displayInfo); |
Mathias Agopian | 351a707 | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 112 | const uint32_t hwFlags = hw.getFlags(); |
| 113 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 114 | mFormat = format; |
| 115 | mWidth = w; |
| 116 | mHeight = h; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 117 | mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; |
Mathias Agopian | 351a707 | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 119 | mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS); |
| 120 | |
Mathias Agopian | cc93476 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 121 | // we use the red index |
| 122 | int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 123 | int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); |
| 124 | mNeedsDithering = layerRedsize > displayRedSize; |
| 125 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 126 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 127 | mBuffers[i] = new GraphicBuffer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 129 | mSurface = new SurfaceLayer(mFlinger, clientIndex(), this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | return NO_ERROR; |
| 131 | } |
| 132 | |
| 133 | void Layer::reloadTexture(const Region& dirty) |
| 134 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 135 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 4961c95 | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 136 | sp<GraphicBuffer> buffer(getFrontBufferLocked()); |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 137 | int index = mFrontBufferIndex; |
| 138 | |
| 139 | // create the new texture name if needed |
| 140 | if (UNLIKELY(mTextures[index].name == -1U)) { |
| 141 | mTextures[index].name = createTexture(); |
| 142 | mTextures[index].width = 0; |
| 143 | mTextures[index].height = 0; |
| 144 | } |
| 145 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 146 | #ifdef EGL_ANDROID_image_native_buffer |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 147 | if (mFlags & DisplayHardware::DIRECT_TEXTURE) { |
| 148 | if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) { |
| 149 | if (mTextures[index].dirty) { |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 150 | initializeEglImage(buffer, &mTextures[index]); |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 151 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 152 | } else { |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 153 | if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width || |
| 154 | mHybridBuffer->height != buffer->height)) { |
| 155 | mHybridBuffer.clear(); |
| 156 | mHybridBuffer = new GraphicBuffer( |
| 157 | buffer->width, buffer->height, buffer->format, |
| 158 | GraphicBuffer::USAGE_SW_WRITE_OFTEN | |
| 159 | GraphicBuffer::USAGE_HW_TEXTURE); |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 160 | initializeEglImage( |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 161 | mHybridBuffer, &mTextures[0]); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 164 | GGLSurface t; |
| 165 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
| 166 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 167 | res, strerror(res), buffer.get()); |
| 168 | if (res == NO_ERROR) { |
| 169 | Texture* const texture(&mTextures[0]); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 170 | |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 171 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 172 | |
| 173 | sp<GraphicBuffer> buf(mHybridBuffer); |
| 174 | void* vaddr; |
| 175 | res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr); |
| 176 | if (res == NO_ERROR) { |
| 177 | int bpp = 0; |
| 178 | switch (t.format) { |
| 179 | case GGL_PIXEL_FORMAT_RGB_565: |
| 180 | case GGL_PIXEL_FORMAT_RGBA_4444: |
| 181 | bpp = 2; |
| 182 | break; |
| 183 | case GGL_PIXEL_FORMAT_RGBA_8888: |
| 184 | case GGL_PIXEL_FORMAT_RGBX_8888: |
| 185 | bpp = 4; |
| 186 | break; |
| 187 | case GGL_PIXEL_FORMAT_YCbCr_422_SP: |
| 188 | case GGL_PIXEL_FORMAT_YCbCr_420_SP: |
| 189 | // just show the Y plane of YUV buffers |
| 190 | bpp = 1; |
| 191 | break; |
| 192 | default: |
| 193 | // oops, we don't handle this format! |
| 194 | LOGE("layer %p, texture=%d, using format %d, which is not " |
| 195 | "supported by the GL", this, texture->name, t.format); |
| 196 | } |
| 197 | if (bpp) { |
| 198 | const Rect bounds(dirty.getBounds()); |
| 199 | size_t src_stride = t.stride; |
| 200 | size_t dst_stride = buf->stride; |
| 201 | if (src_stride == dst_stride && |
| 202 | bounds.width() == t.width && |
| 203 | bounds.height() == t.height) |
| 204 | { |
| 205 | memcpy(vaddr, t.data, t.height * t.stride * bpp); |
| 206 | } else { |
| 207 | GLubyte const * src = t.data + |
| 208 | (bounds.left + bounds.top * src_stride) * bpp; |
| 209 | GLubyte * dst = (GLubyte *)vaddr + |
| 210 | (bounds.left + bounds.top * dst_stride) * bpp; |
| 211 | const size_t length = bounds.width() * bpp; |
| 212 | size_t h = bounds.height(); |
| 213 | src_stride *= bpp; |
| 214 | dst_stride *= bpp; |
| 215 | while (h--) { |
| 216 | memcpy(dst, src, length); |
| 217 | dst += dst_stride; |
| 218 | src += src_stride; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | buf->unlock(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 223 | } |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 224 | buffer->unlock(); |
| 225 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 226 | } |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 227 | } else |
| 228 | #endif |
| 229 | { |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 230 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 231 | mTextures[i].image = EGL_NO_IMAGE_KHR; |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame] | 232 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 233 | GGLSurface t; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 234 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 235 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 236 | res, strerror(res), buffer.get()); |
| 237 | if (res == NO_ERROR) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 238 | loadTexture(&mTextures[0], dirty, t); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 239 | buffer->unlock(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 240 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
| 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | void Layer::onDraw(const Region& clip) const |
| 245 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 246 | int index = mFrontBufferIndex; |
| 247 | if (mTextures[index].image == EGL_NO_IMAGE_KHR) |
| 248 | index = 0; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 249 | GLuint textureName = mTextures[index].name; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 250 | if (UNLIKELY(textureName == -1LU)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | // the texture has not been created yet, this Layer has |
| 252 | // in fact never been drawn into. this happens frequently with |
| 253 | // SurfaceView. |
| 254 | clearWithOpenGL(clip); |
| 255 | return; |
| 256 | } |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 257 | drawWithOpenGL(clip, mTextures[index]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 260 | sp<GraphicBuffer> Layer::requestBuffer(int index, int usage) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 262 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 263 | |
| 264 | // this ensures our client doesn't go away while we're accessing |
| 265 | // the shared area. |
| 266 | sp<Client> ourClient(client.promote()); |
| 267 | if (ourClient == 0) { |
| 268 | // oops, the client is already gone |
| 269 | return buffer; |
| 270 | } |
| 271 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 272 | /* |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 273 | * This is called from the client's Surface::dequeue(). This can happen |
| 274 | * at any time, especially while we're in the middle of using the |
| 275 | * buffer 'index' as our front buffer. |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 276 | * |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 277 | * Make sure the buffer we're resizing is not the front buffer and has been |
| 278 | * dequeued. Once this condition is asserted, we are guaranteed that this |
| 279 | * buffer cannot become the front buffer under our feet, since we're called |
| 280 | * from Surface::dequeue() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 281 | */ |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 282 | status_t err = lcblk->assertReallocate(index); |
| 283 | LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 284 | if (err != NO_ERROR) { |
| 285 | // the surface may have died |
| 286 | return buffer; |
| 287 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 289 | uint32_t w, h; |
| 290 | { // scope for the lock |
| 291 | Mutex::Autolock _l(mLock); |
| 292 | w = mWidth; |
| 293 | h = mHeight; |
| 294 | buffer = mBuffers[index]; |
Mathias Agopian | ac7f13b | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 295 | |
| 296 | // destroy() could have been called before we get here, we log it |
| 297 | // because it's uncommon, and the code below should handle it |
| 298 | LOGW_IF(buffer==0, |
| 299 | "mBuffers[%d] is null (mWidth=%d, mHeight=%d)", |
| 300 | index, w, h); |
| 301 | |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 302 | mBuffers[index].clear(); |
| 303 | } |
| 304 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 305 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | ac7f13b | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 306 | if (buffer!=0 && buffer->getStrongCount() == 1) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 307 | err = buffer->reallocate(w, h, mFormat, effectiveUsage); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 308 | } else { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 309 | // here we have to reallocate a new buffer because we could have a |
| 310 | // client in our process with a reference to it (eg: status bar), |
| 311 | // and we can't release the handle under its feet. |
| 312 | buffer.clear(); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 313 | buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 314 | err = buffer->initCheck(); |
| 315 | } |
| 316 | |
| 317 | if (err || buffer->handle == 0) { |
| 318 | LOGE_IF(err || buffer->handle == 0, |
| 319 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 320 | this, index, w, h, strerror(-err)); |
| 321 | } else { |
| 322 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 323 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 324 | this, index, w, h, buffer->handle); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 328 | Mutex::Autolock _l(mLock); |
| 329 | if (mWidth && mHeight) { |
| 330 | // and we have new buffer |
| 331 | mBuffers[index] = buffer; |
| 332 | // texture is now dirty... |
| 333 | mTextures[index].dirty = true; |
| 334 | } else { |
| 335 | // oops we got killed while we were allocating the buffer |
| 336 | buffer.clear(); |
| 337 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 338 | } |
| 339 | return buffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 342 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 343 | { |
| 344 | /* |
| 345 | * buffers used for software rendering, but h/w composition |
| 346 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 347 | * |
| 348 | * buffers used for h/w rendering and h/w composition |
| 349 | * are allocated with HW_RENDER | HW_TEXTURE |
| 350 | * |
| 351 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 352 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 353 | * |
| 354 | */ |
| 355 | |
| 356 | if (mSecure) { |
| 357 | // secure buffer, don't store it into the GPU |
| 358 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 359 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 360 | } else { |
| 361 | // it's allowed to modify the usage flags here, but generally |
| 362 | // the requested flags should be honored. |
Mathias Agopian | 351a707 | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 363 | if (mNoEGLImageForSwBuffers) { |
| 364 | if (usage & GraphicBuffer::USAGE_HW_MASK) { |
| 365 | // request EGLImage for h/w buffers only |
| 366 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 367 | } |
| 368 | } else { |
| 369 | // request EGLImage for all buffers |
| 370 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 371 | } |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 372 | } |
| 373 | return usage; |
| 374 | } |
| 375 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | uint32_t Layer::doTransaction(uint32_t flags) |
| 377 | { |
| 378 | const Layer::State& front(drawingState()); |
| 379 | const Layer::State& temp(currentState()); |
| 380 | |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 381 | if ((front.requested_w != temp.requested_w) || |
| 382 | (front.requested_h != temp.requested_h)) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 383 | // the size changed, we need to ask our client to request a new buffer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 385 | "resize (layer=%p), requested (%dx%d), " |
| 386 | "drawing (%d,%d), (%dx%d), (%dx%d)", |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 387 | this, |
| 388 | int(temp.requested_w), int(temp.requested_h), |
| 389 | int(front.requested_w), int(front.requested_h), |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 390 | int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()), |
| 391 | int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 393 | // we're being resized and there is a freeze display request, |
| 394 | // acquire a freeze lock, so that the screen stays put |
| 395 | // until we've redrawn at the new size; this is to avoid |
| 396 | // glitches upon orientation changes. |
| 397 | if (mFlinger->hasFreezeRequest()) { |
| 398 | // if the surface is hidden, don't try to acquire the |
| 399 | // freeze lock, since hidden surfaces may never redraw |
| 400 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 401 | mFreezeLock = mFlinger->getFreezeLock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 404 | |
Mathias Agopian | bd23e30 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 405 | // this will make sure LayerBase::doTransaction doesn't update |
| 406 | // the drawing state's size |
| 407 | Layer::State& editDraw(mDrawingState); |
| 408 | editDraw.requested_w = temp.requested_w; |
| 409 | editDraw.requested_h = temp.requested_h; |
| 410 | |
Mathias Agopian | 70cab91 | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 411 | // record the new size, form this point on, when the client request a |
| 412 | // buffer, it'll get the new size. |
| 413 | setDrawingSize(temp.requested_w, temp.requested_h); |
| 414 | |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 415 | // all buffers need reallocation |
| 416 | lcblk->reallocate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 418 | |
The Android Open Source Project | 9066cfe | 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 | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 430 | void Layer::setDrawingSize(uint32_t w, uint32_t h) { |
| 431 | Mutex::Autolock _l(mLock); |
| 432 | mWidth = w; |
| 433 | mHeight = h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // ---------------------------------------------------------------------------- |
| 437 | // pageflip handling... |
| 438 | // ---------------------------------------------------------------------------- |
| 439 | |
| 440 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 441 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 442 | ssize_t buf = lcblk->retireAndLock(); |
| 443 | if (buf < NO_ERROR) { |
| 444 | //LOGW("nothing to retire (%s)", strerror(-buf)); |
| 445 | // NOTE: here the buffer is locked because we will used |
| 446 | // for composition later in the loop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | return; |
| 448 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 449 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 450 | // we retired a buffer, which becomes the new front buffer |
| 451 | mFrontBufferIndex = buf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 453 | // get the dirty region |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 454 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 455 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 456 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 457 | |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 458 | const Layer::State& front(drawingState()); |
Mathias Agopian | bd23e30 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 459 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 460 | newFrontBuffer->getHeight() == front.requested_h) |
| 461 | { |
| 462 | if ((front.w != front.requested_w) || |
| 463 | (front.h != front.requested_h)) |
| 464 | { |
| 465 | // Here we pretend the transaction happened by updating the |
| 466 | // current and drawing states. Drawing state is only accessed |
| 467 | // in this thread, no need to have it locked |
| 468 | Layer::State& editDraw(mDrawingState); |
| 469 | editDraw.w = editDraw.requested_w; |
| 470 | editDraw.h = editDraw.requested_h; |
| 471 | |
| 472 | // We also need to update the current state so that we don't |
| 473 | // end-up doing too much work during the next transaction. |
| 474 | // NOTE: We actually don't need hold the transaction lock here |
| 475 | // because State::w and State::h are only accessed from |
| 476 | // this thread |
| 477 | Layer::State& editTemp(currentState()); |
| 478 | editTemp.w = editDraw.w; |
| 479 | editTemp.h = editDraw.h; |
| 480 | |
| 481 | // recompute visible region |
| 482 | recomputeVisibleRegions = true; |
Mathias Agopian | bd23e30 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 483 | } |
Mathias Agopian | 46b2df1 | 2009-10-07 17:58:29 -0700 | [diff] [blame] | 484 | |
| 485 | // we now have the correct size, unfreeze the screen |
| 486 | mFreezeLock.clear(); |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Mathias Agopian | e05f07d | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 489 | if (lcblk->getQueuedCount()) { |
| 490 | // signal an event if we have more buffers waiting |
| 491 | mFlinger->signalEvent(); |
| 492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 494 | if (!mPostedDirtyRegion.isEmpty()) { |
| 495 | reloadTexture( mPostedDirtyRegion ); |
| 496 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void Layer::unlockPageFlip( |
| 500 | const Transform& planeTransform, Region& outDirtyRegion) |
| 501 | { |
| 502 | Region dirtyRegion(mPostedDirtyRegion); |
| 503 | if (!dirtyRegion.isEmpty()) { |
| 504 | mPostedDirtyRegion.clear(); |
| 505 | // The dirty region is given in the layer's coordinate space |
| 506 | // transform the dirty region by the surface's transformation |
| 507 | // and the global transformation. |
| 508 | const Layer::State& s(drawingState()); |
| 509 | const Transform tr(planeTransform * s.transform); |
| 510 | dirtyRegion = tr.transform(dirtyRegion); |
| 511 | |
| 512 | // At this point, the dirty region is in screen space. |
| 513 | // Make sure it's constrained by the visible region (which |
| 514 | // is in screen space as well). |
| 515 | dirtyRegion.andSelf(visibleRegionScreen); |
| 516 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | void Layer::finishPageFlip() |
| 521 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 522 | status_t err = lcblk->unlock( mFrontBufferIndex ); |
| 523 | LOGE_IF(err!=NO_ERROR, |
| 524 | "layer %p, buffer=%d wasn't locked!", |
| 525 | this, mFrontBufferIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 528 | // --------------------------------------------------------------------------- |
| 529 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 530 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
| 531 | SurfaceID id, const sp<Layer>& owner) |
| 532 | : Surface(flinger, id, owner->getIdentity(), owner) |
| 533 | { |
| 534 | } |
| 535 | |
| 536 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 537 | { |
| 538 | } |
| 539 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 540 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 541 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 542 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 543 | sp<Layer> owner(getOwner()); |
| 544 | if (owner != 0) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 545 | LOGE_IF(uint32_t(index)>=NUM_BUFFERS, |
| 546 | "getBuffer() index (%d) out of range", index); |
| 547 | if (uint32_t(index) < NUM_BUFFERS) { |
| 548 | buffer = owner->requestBuffer(index, usage); |
| 549 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 550 | } |
| 551 | return buffer; |
| 552 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | |
| 554 | // --------------------------------------------------------------------------- |
| 555 | |
| 556 | |
| 557 | }; // namespace android |