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