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 | |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 133 | status_t Layer::initializeEglImageLocked( |
| 134 | const sp<GraphicBuffer>& buffer, Texture* texture) |
| 135 | { |
| 136 | status_t err = NO_ERROR; |
| 137 | |
| 138 | // we need to recreate the texture |
| 139 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 140 | |
| 141 | // free the previous image |
| 142 | if (texture->image != EGL_NO_IMAGE_KHR) { |
| 143 | eglDestroyImageKHR(dpy, texture->image); |
| 144 | texture->image = EGL_NO_IMAGE_KHR; |
| 145 | } |
| 146 | |
| 147 | // construct an EGL_NATIVE_BUFFER_ANDROID |
| 148 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 149 | |
| 150 | // create the new EGLImageKHR |
| 151 | const EGLint attrs[] = { |
| 152 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 153 | EGL_NONE, EGL_NONE |
| 154 | }; |
| 155 | texture->image = eglCreateImageKHR( |
| 156 | dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 157 | (EGLClientBuffer)clientBuf, attrs); |
| 158 | |
| 159 | LOGE_IF(texture->image == EGL_NO_IMAGE_KHR, |
| 160 | "eglCreateImageKHR() failed. err=0x%4x", |
| 161 | eglGetError()); |
| 162 | |
| 163 | if (texture->image != EGL_NO_IMAGE_KHR) { |
| 164 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 165 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, |
| 166 | (GLeglImageOES)texture->image); |
| 167 | GLint error = glGetError(); |
| 168 | if (UNLIKELY(error != GL_NO_ERROR)) { |
| 169 | // this failed, for instance, because we don't support NPOT. |
| 170 | // FIXME: do something! |
| 171 | LOGE("layer=%p, glEGLImageTargetTexture2DOES(%p) " |
| 172 | "failed err=0x%04x", |
| 173 | this, texture->image, error); |
| 174 | mFlags &= ~DisplayHardware::DIRECT_TEXTURE; |
| 175 | err = INVALID_OPERATION; |
| 176 | } else { |
| 177 | // Everything went okay! |
| 178 | texture->NPOTAdjust = false; |
| 179 | texture->dirty = false; |
| 180 | texture->width = clientBuf->width; |
| 181 | texture->height = clientBuf->height; |
| 182 | } |
| 183 | } else { |
| 184 | err = INVALID_OPERATION; |
| 185 | } |
| 186 | return err; |
| 187 | } |
| 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | void Layer::reloadTexture(const Region& dirty) |
| 190 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 191 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 4961c95 | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 192 | sp<GraphicBuffer> buffer(getFrontBufferLocked()); |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 193 | int index = mFrontBufferIndex; |
| 194 | |
| 195 | // create the new texture name if needed |
| 196 | if (UNLIKELY(mTextures[index].name == -1U)) { |
| 197 | mTextures[index].name = createTexture(); |
| 198 | mTextures[index].width = 0; |
| 199 | mTextures[index].height = 0; |
| 200 | } |
| 201 | |
| 202 | if (mFlags & DisplayHardware::DIRECT_TEXTURE) { |
| 203 | if (buffer->usage & GraphicBuffer::USAGE_HW_TEXTURE) { |
| 204 | if (mTextures[index].dirty) { |
| 205 | initializeEglImageLocked(buffer, &mTextures[index]); |
| 206 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 207 | } else { |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 208 | if (mHybridBuffer==0 || (mHybridBuffer->width != buffer->width || |
| 209 | mHybridBuffer->height != buffer->height)) { |
| 210 | mHybridBuffer.clear(); |
| 211 | mHybridBuffer = new GraphicBuffer( |
| 212 | buffer->width, buffer->height, buffer->format, |
| 213 | GraphicBuffer::USAGE_SW_WRITE_OFTEN | |
| 214 | GraphicBuffer::USAGE_HW_TEXTURE); |
| 215 | initializeEglImageLocked( |
| 216 | mHybridBuffer, &mTextures[0]); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 219 | GGLSurface t; |
| 220 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
| 221 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 222 | res, strerror(res), buffer.get()); |
| 223 | if (res == NO_ERROR) { |
| 224 | Texture* const texture(&mTextures[0]); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 225 | |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 226 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 227 | |
| 228 | sp<GraphicBuffer> buf(mHybridBuffer); |
| 229 | void* vaddr; |
| 230 | res = buf->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, &vaddr); |
| 231 | if (res == NO_ERROR) { |
| 232 | int bpp = 0; |
| 233 | switch (t.format) { |
| 234 | case GGL_PIXEL_FORMAT_RGB_565: |
| 235 | case GGL_PIXEL_FORMAT_RGBA_4444: |
| 236 | bpp = 2; |
| 237 | break; |
| 238 | case GGL_PIXEL_FORMAT_RGBA_8888: |
| 239 | case GGL_PIXEL_FORMAT_RGBX_8888: |
| 240 | bpp = 4; |
| 241 | break; |
| 242 | case GGL_PIXEL_FORMAT_YCbCr_422_SP: |
| 243 | case GGL_PIXEL_FORMAT_YCbCr_420_SP: |
| 244 | // just show the Y plane of YUV buffers |
| 245 | bpp = 1; |
| 246 | break; |
| 247 | default: |
| 248 | // oops, we don't handle this format! |
| 249 | LOGE("layer %p, texture=%d, using format %d, which is not " |
| 250 | "supported by the GL", this, texture->name, t.format); |
| 251 | } |
| 252 | if (bpp) { |
| 253 | const Rect bounds(dirty.getBounds()); |
| 254 | size_t src_stride = t.stride; |
| 255 | size_t dst_stride = buf->stride; |
| 256 | if (src_stride == dst_stride && |
| 257 | bounds.width() == t.width && |
| 258 | bounds.height() == t.height) |
| 259 | { |
| 260 | memcpy(vaddr, t.data, t.height * t.stride * bpp); |
| 261 | } else { |
| 262 | GLubyte const * src = t.data + |
| 263 | (bounds.left + bounds.top * src_stride) * bpp; |
| 264 | GLubyte * dst = (GLubyte *)vaddr + |
| 265 | (bounds.left + bounds.top * dst_stride) * bpp; |
| 266 | const size_t length = bounds.width() * bpp; |
| 267 | size_t h = bounds.height(); |
| 268 | src_stride *= bpp; |
| 269 | dst_stride *= bpp; |
| 270 | while (h--) { |
| 271 | memcpy(dst, src, length); |
| 272 | dst += dst_stride; |
| 273 | src += src_stride; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | buf->unlock(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 278 | } |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 279 | buffer->unlock(); |
| 280 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 281 | } |
| 282 | } else { |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 283 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 284 | mTextures[i].image = EGL_NO_IMAGE_KHR; |
Mathias Agopian | 382e17d | 2009-10-21 16:27:21 -0700 | [diff] [blame^] | 285 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 286 | GGLSurface t; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 287 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 288 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 289 | res, strerror(res), buffer.get()); |
| 290 | if (res == NO_ERROR) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 291 | loadTexture(&mTextures[0], dirty, t); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 292 | buffer->unlock(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | void Layer::onDraw(const Region& clip) const |
| 298 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 299 | int index = mFrontBufferIndex; |
| 300 | if (mTextures[index].image == EGL_NO_IMAGE_KHR) |
| 301 | index = 0; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 302 | GLuint textureName = mTextures[index].name; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 303 | if (UNLIKELY(textureName == -1LU)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | // the texture has not been created yet, this Layer has |
| 305 | // in fact never been drawn into. this happens frequently with |
| 306 | // SurfaceView. |
| 307 | clearWithOpenGL(clip); |
| 308 | return; |
| 309 | } |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 310 | drawWithOpenGL(clip, mTextures[index]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 313 | sp<GraphicBuffer> Layer::requestBuffer(int index, int usage) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 315 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 316 | |
| 317 | // this ensures our client doesn't go away while we're accessing |
| 318 | // the shared area. |
| 319 | sp<Client> ourClient(client.promote()); |
| 320 | if (ourClient == 0) { |
| 321 | // oops, the client is already gone |
| 322 | return buffer; |
| 323 | } |
| 324 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 325 | /* |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 326 | * This is called from the client's Surface::dequeue(). This can happen |
| 327 | * at any time, especially while we're in the middle of using the |
| 328 | * buffer 'index' as our front buffer. |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 329 | * |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 330 | * Make sure the buffer we're resizing is not the front buffer and has been |
| 331 | * dequeued. Once this condition is asserted, we are guaranteed that this |
| 332 | * buffer cannot become the front buffer under our feet, since we're called |
| 333 | * from Surface::dequeue() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 334 | */ |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 335 | status_t err = lcblk->assertReallocate(index); |
| 336 | LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 337 | if (err != NO_ERROR) { |
| 338 | // the surface may have died |
| 339 | return buffer; |
| 340 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 342 | uint32_t w, h; |
| 343 | { // scope for the lock |
| 344 | Mutex::Autolock _l(mLock); |
| 345 | w = mWidth; |
| 346 | h = mHeight; |
| 347 | buffer = mBuffers[index]; |
Mathias Agopian | ac7f13b | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 348 | |
| 349 | // destroy() could have been called before we get here, we log it |
| 350 | // because it's uncommon, and the code below should handle it |
| 351 | LOGW_IF(buffer==0, |
| 352 | "mBuffers[%d] is null (mWidth=%d, mHeight=%d)", |
| 353 | index, w, h); |
| 354 | |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 355 | mBuffers[index].clear(); |
| 356 | } |
| 357 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 358 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | ac7f13b | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 359 | if (buffer!=0 && buffer->getStrongCount() == 1) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 360 | err = buffer->reallocate(w, h, mFormat, effectiveUsage); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 361 | } else { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 362 | // here we have to reallocate a new buffer because we could have a |
| 363 | // client in our process with a reference to it (eg: status bar), |
| 364 | // and we can't release the handle under its feet. |
| 365 | buffer.clear(); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 366 | buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 367 | err = buffer->initCheck(); |
| 368 | } |
| 369 | |
| 370 | if (err || buffer->handle == 0) { |
| 371 | LOGE_IF(err || buffer->handle == 0, |
| 372 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 373 | this, index, w, h, strerror(-err)); |
| 374 | } else { |
| 375 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 376 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 377 | this, index, w, h, buffer->handle); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 248b5bd | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 381 | Mutex::Autolock _l(mLock); |
| 382 | if (mWidth && mHeight) { |
| 383 | // and we have new buffer |
| 384 | mBuffers[index] = buffer; |
| 385 | // texture is now dirty... |
| 386 | mTextures[index].dirty = true; |
| 387 | } else { |
| 388 | // oops we got killed while we were allocating the buffer |
| 389 | buffer.clear(); |
| 390 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 391 | } |
| 392 | return buffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 395 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 396 | { |
| 397 | /* |
| 398 | * buffers used for software rendering, but h/w composition |
| 399 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 400 | * |
| 401 | * buffers used for h/w rendering and h/w composition |
| 402 | * are allocated with HW_RENDER | HW_TEXTURE |
| 403 | * |
| 404 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 405 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 406 | * |
| 407 | */ |
| 408 | |
| 409 | if (mSecure) { |
| 410 | // secure buffer, don't store it into the GPU |
| 411 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 412 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 413 | } else { |
| 414 | // it's allowed to modify the usage flags here, but generally |
| 415 | // the requested flags should be honored. |
Mathias Agopian | 351a707 | 2009-10-05 18:20:39 -0700 | [diff] [blame] | 416 | if (mNoEGLImageForSwBuffers) { |
| 417 | if (usage & GraphicBuffer::USAGE_HW_MASK) { |
| 418 | // request EGLImage for h/w buffers only |
| 419 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 420 | } |
| 421 | } else { |
| 422 | // request EGLImage for all buffers |
| 423 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 424 | } |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 425 | } |
| 426 | return usage; |
| 427 | } |
| 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | uint32_t Layer::doTransaction(uint32_t flags) |
| 430 | { |
| 431 | const Layer::State& front(drawingState()); |
| 432 | const Layer::State& temp(currentState()); |
| 433 | |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 434 | if ((front.requested_w != temp.requested_w) || |
| 435 | (front.requested_h != temp.requested_h)) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 436 | // 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] | 437 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 438 | "resize (layer=%p), requested (%dx%d), " |
| 439 | "drawing (%d,%d), (%dx%d), (%dx%d)", |
Mathias Agopian | e1b6f24 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 440 | this, |
| 441 | int(temp.requested_w), int(temp.requested_h), |
| 442 | int(front.requested_w), int(front.requested_h), |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 443 | int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()), |
| 444 | int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 446 | // we're being resized and there is a freeze display request, |
| 447 | // acquire a freeze lock, so that the screen stays put |
| 448 | // until we've redrawn at the new size; this is to avoid |
| 449 | // glitches upon orientation changes. |
| 450 | if (mFlinger->hasFreezeRequest()) { |
| 451 | // if the surface is hidden, don't try to acquire the |
| 452 | // freeze lock, since hidden surfaces may never redraw |
| 453 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 454 | mFreezeLock = mFlinger->getFreezeLock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 457 | |
Mathias Agopian | bd23e30 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 458 | // this will make sure LayerBase::doTransaction doesn't update |
| 459 | // the drawing state's size |
| 460 | Layer::State& editDraw(mDrawingState); |
| 461 | editDraw.requested_w = temp.requested_w; |
| 462 | editDraw.requested_h = temp.requested_h; |
| 463 | |
Mathias Agopian | 70cab91 | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 464 | // record the new size, form this point on, when the client request a |
| 465 | // buffer, it'll get the new size. |
| 466 | setDrawingSize(temp.requested_w, temp.requested_h); |
| 467 | |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 468 | // all buffers need reallocation |
| 469 | lcblk->reallocate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 471 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | if (temp.sequence != front.sequence) { |
| 473 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 474 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 475 | // (it may never redraw, which is fine if it is hidden) |
| 476 | mFreezeLock.clear(); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return LayerBase::doTransaction(flags); |
| 481 | } |
| 482 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 483 | void Layer::setDrawingSize(uint32_t w, uint32_t h) { |
| 484 | Mutex::Autolock _l(mLock); |
| 485 | mWidth = w; |
| 486 | mHeight = h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // ---------------------------------------------------------------------------- |
| 490 | // pageflip handling... |
| 491 | // ---------------------------------------------------------------------------- |
| 492 | |
| 493 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 494 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 495 | ssize_t buf = lcblk->retireAndLock(); |
| 496 | if (buf < NO_ERROR) { |
| 497 | //LOGW("nothing to retire (%s)", strerror(-buf)); |
| 498 | // NOTE: here the buffer is locked because we will used |
| 499 | // for composition later in the loop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | return; |
| 501 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 502 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 503 | // we retired a buffer, which becomes the new front buffer |
| 504 | mFrontBufferIndex = buf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 506 | // get the dirty region |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 507 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 508 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 509 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 510 | |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 511 | const Layer::State& front(drawingState()); |
Mathias Agopian | bd23e30 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 512 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 513 | newFrontBuffer->getHeight() == front.requested_h) |
| 514 | { |
| 515 | if ((front.w != front.requested_w) || |
| 516 | (front.h != front.requested_h)) |
| 517 | { |
| 518 | // Here we pretend the transaction happened by updating the |
| 519 | // current and drawing states. Drawing state is only accessed |
| 520 | // in this thread, no need to have it locked |
| 521 | Layer::State& editDraw(mDrawingState); |
| 522 | editDraw.w = editDraw.requested_w; |
| 523 | editDraw.h = editDraw.requested_h; |
| 524 | |
| 525 | // We also need to update the current state so that we don't |
| 526 | // end-up doing too much work during the next transaction. |
| 527 | // NOTE: We actually don't need hold the transaction lock here |
| 528 | // because State::w and State::h are only accessed from |
| 529 | // this thread |
| 530 | Layer::State& editTemp(currentState()); |
| 531 | editTemp.w = editDraw.w; |
| 532 | editTemp.h = editDraw.h; |
| 533 | |
| 534 | // recompute visible region |
| 535 | recomputeVisibleRegions = true; |
Mathias Agopian | bd23e30 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 536 | } |
Mathias Agopian | 46b2df1 | 2009-10-07 17:58:29 -0700 | [diff] [blame] | 537 | |
| 538 | // we now have the correct size, unfreeze the screen |
| 539 | mFreezeLock.clear(); |
Mathias Agopian | 7cf03ba | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Mathias Agopian | e05f07d | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 542 | if (lcblk->getQueuedCount()) { |
| 543 | // signal an event if we have more buffers waiting |
| 544 | mFlinger->signalEvent(); |
| 545 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 547 | if (!mPostedDirtyRegion.isEmpty()) { |
| 548 | reloadTexture( mPostedDirtyRegion ); |
| 549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void Layer::unlockPageFlip( |
| 553 | const Transform& planeTransform, Region& outDirtyRegion) |
| 554 | { |
| 555 | Region dirtyRegion(mPostedDirtyRegion); |
| 556 | if (!dirtyRegion.isEmpty()) { |
| 557 | mPostedDirtyRegion.clear(); |
| 558 | // The dirty region is given in the layer's coordinate space |
| 559 | // transform the dirty region by the surface's transformation |
| 560 | // and the global transformation. |
| 561 | const Layer::State& s(drawingState()); |
| 562 | const Transform tr(planeTransform * s.transform); |
| 563 | dirtyRegion = tr.transform(dirtyRegion); |
| 564 | |
| 565 | // At this point, the dirty region is in screen space. |
| 566 | // Make sure it's constrained by the visible region (which |
| 567 | // is in screen space as well). |
| 568 | dirtyRegion.andSelf(visibleRegionScreen); |
| 569 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | void Layer::finishPageFlip() |
| 574 | { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 575 | status_t err = lcblk->unlock( mFrontBufferIndex ); |
| 576 | LOGE_IF(err!=NO_ERROR, |
| 577 | "layer %p, buffer=%d wasn't locked!", |
| 578 | this, mFrontBufferIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 581 | // --------------------------------------------------------------------------- |
| 582 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 583 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
| 584 | SurfaceID id, const sp<Layer>& owner) |
| 585 | : Surface(flinger, id, owner->getIdentity(), owner) |
| 586 | { |
| 587 | } |
| 588 | |
| 589 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 590 | { |
| 591 | } |
| 592 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 593 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 594 | { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 595 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 596 | sp<Layer> owner(getOwner()); |
| 597 | if (owner != 0) { |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 598 | LOGE_IF(uint32_t(index)>=NUM_BUFFERS, |
| 599 | "getBuffer() index (%d) out of range", index); |
| 600 | if (uint32_t(index) < NUM_BUFFERS) { |
| 601 | buffer = owner->requestBuffer(index, usage); |
| 602 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 603 | } |
| 604 | return buffer; |
| 605 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | |
| 607 | // --------------------------------------------------------------------------- |
| 608 | |
| 609 | |
| 610 | }; // namespace android |