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