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