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