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