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 | 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()); |
| 110 | PixelFormatInfo displayInfo; |
| 111 | getPixelFormatInfo(hw.getFormat(), &displayInfo); |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame^] | 112 | const uint32_t hwFlags = hw.getFlags(); |
| 113 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 114 | mFormat = format; |
| 115 | mWidth = w; |
| 116 | mHeight = h; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 117 | mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame^] | 119 | mNoEGLImageForSwBuffers = !(hwFlags & DisplayHardware::CACHED_BUFFERS); |
| 120 | |
Mathias Agopian | 401c257 | 2009-09-23 19:16:27 -0700 | [diff] [blame] | 121 | // we use the red index |
| 122 | int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 123 | int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); |
| 124 | mNeedsDithering = layerRedsize > displayRedSize; |
| 125 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 126 | for (size_t i=0 ; i<NUM_BUFFERS ; i++) { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 127 | mBuffers[i] = new GraphicBuffer(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 129 | mSurface = new SurfaceLayer(mFlinger, clientIndex(), this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | return NO_ERROR; |
| 131 | } |
| 132 | |
| 133 | void Layer::reloadTexture(const Region& dirty) |
| 134 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 135 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 136 | sp<GraphicBuffer> buffer(getFrontBuffer()); |
| 137 | if (LIKELY((mFlags & DisplayHardware::DIRECT_TEXTURE) && |
| 138 | (buffer->usage & GRALLOC_USAGE_HW_TEXTURE))) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 139 | int index = mFrontBufferIndex; |
| 140 | if (LIKELY(!mTextures[index].dirty)) { |
| 141 | glBindTexture(GL_TEXTURE_2D, mTextures[index].name); |
| 142 | } else { |
| 143 | // we need to recreate the texture |
| 144 | EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); |
| 145 | |
| 146 | // create the new texture name if needed |
| 147 | if (UNLIKELY(mTextures[index].name == -1U)) { |
| 148 | mTextures[index].name = createTexture(); |
| 149 | } else { |
| 150 | glBindTexture(GL_TEXTURE_2D, mTextures[index].name); |
| 151 | } |
| 152 | |
| 153 | // free the previous image |
| 154 | if (mTextures[index].image != EGL_NO_IMAGE_KHR) { |
| 155 | eglDestroyImageKHR(dpy, mTextures[index].image); |
| 156 | mTextures[index].image = EGL_NO_IMAGE_KHR; |
| 157 | } |
| 158 | |
| 159 | // construct an EGL_NATIVE_BUFFER_ANDROID |
| 160 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 161 | |
| 162 | // create the new EGLImageKHR |
| 163 | const EGLint attrs[] = { |
| 164 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 165 | EGL_NONE, EGL_NONE |
| 166 | }; |
| 167 | mTextures[index].image = eglCreateImageKHR( |
| 168 | dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 169 | (EGLClientBuffer)clientBuf, attrs); |
| 170 | |
| 171 | LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR, |
| 172 | "eglCreateImageKHR() failed. err=0x%4x", |
| 173 | eglGetError()); |
| 174 | |
| 175 | if (mTextures[index].image != EGL_NO_IMAGE_KHR) { |
| 176 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, |
| 177 | (GLeglImageOES)mTextures[index].image); |
| 178 | GLint error = glGetError(); |
| 179 | if (UNLIKELY(error != GL_NO_ERROR)) { |
| 180 | // this failed, for instance, because we don't support |
| 181 | // NPOT. |
| 182 | // FIXME: do something! |
Mathias Agopian | 6d9f698 | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 183 | LOGD("layer=%p, glEGLImageTargetTexture2DOES(%p) " |
Mathias Agopian | 816d7d0 | 2009-09-14 18:10:30 -0700 | [diff] [blame] | 184 | "failed err=0x%04x", |
| 185 | this, mTextures[index].image, error); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 186 | mFlags &= ~DisplayHardware::DIRECT_TEXTURE; |
| 187 | } else { |
| 188 | // Everything went okay! |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 189 | mTextures[index].NPOTAdjust = false; |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 190 | mTextures[index].dirty = false; |
| 191 | mTextures[index].width = clientBuf->width; |
| 192 | mTextures[index].height = clientBuf->height; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | } else { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 197 | for (int i=0 ; i<NUM_BUFFERS ; i++) |
| 198 | mTextures[i].image = EGL_NO_IMAGE_KHR; |
| 199 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 200 | GGLSurface t; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 201 | status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 202 | LOGE_IF(res, "error %d (%s) locking buffer %p", |
| 203 | res, strerror(res), buffer.get()); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 204 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 205 | if (res == NO_ERROR) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 206 | if (UNLIKELY(mTextures[0].name == -1U)) { |
| 207 | mTextures[0].name = createTexture(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 208 | mTextures[0].width = 0; |
| 209 | mTextures[0].height = 0; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 210 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 211 | loadTexture(&mTextures[0], dirty, t); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 212 | buffer->unlock(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 213 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | void Layer::onDraw(const Region& clip) const |
| 219 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 220 | int index = mFrontBufferIndex; |
| 221 | if (mTextures[index].image == EGL_NO_IMAGE_KHR) |
| 222 | index = 0; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 223 | GLuint textureName = mTextures[index].name; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 224 | if (UNLIKELY(textureName == -1LU)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // the texture has not been created yet, this Layer has |
| 226 | // in fact never been drawn into. this happens frequently with |
| 227 | // SurfaceView. |
| 228 | clearWithOpenGL(clip); |
| 229 | return; |
| 230 | } |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 231 | drawWithOpenGL(clip, mTextures[index]); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 234 | sp<GraphicBuffer> Layer::requestBuffer(int index, int usage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 236 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 237 | |
| 238 | // this ensures our client doesn't go away while we're accessing |
| 239 | // the shared area. |
| 240 | sp<Client> ourClient(client.promote()); |
| 241 | if (ourClient == 0) { |
| 242 | // oops, the client is already gone |
| 243 | return buffer; |
| 244 | } |
| 245 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 246 | /* |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 247 | * This is called from the client's Surface::dequeue(). This can happen |
| 248 | * at any time, especially while we're in the middle of using the |
| 249 | * buffer 'index' as our front buffer. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 250 | * |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 251 | * Make sure the buffer we're resizing is not the front buffer and has been |
| 252 | * dequeued. Once this condition is asserted, we are guaranteed that this |
| 253 | * buffer cannot become the front buffer under our feet, since we're called |
| 254 | * from Surface::dequeue() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 255 | */ |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 256 | status_t err = lcblk->assertReallocate(index); |
| 257 | LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 258 | if (err != NO_ERROR) { |
| 259 | // the surface may have died |
| 260 | return buffer; |
| 261 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 263 | uint32_t w, h; |
| 264 | { // scope for the lock |
| 265 | Mutex::Autolock _l(mLock); |
| 266 | w = mWidth; |
| 267 | h = mHeight; |
| 268 | buffer = mBuffers[index]; |
Mathias Agopian | 6d9f698 | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 269 | |
| 270 | // destroy() could have been called before we get here, we log it |
| 271 | // because it's uncommon, and the code below should handle it |
| 272 | LOGW_IF(buffer==0, |
| 273 | "mBuffers[%d] is null (mWidth=%d, mHeight=%d)", |
| 274 | index, w, h); |
| 275 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 276 | mBuffers[index].clear(); |
| 277 | } |
| 278 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 279 | const uint32_t effectiveUsage = getEffectiveUsage(usage); |
Mathias Agopian | 6d9f698 | 2009-09-17 19:19:08 -0700 | [diff] [blame] | 280 | if (buffer!=0 && buffer->getStrongCount() == 1) { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 281 | err = buffer->reallocate(w, h, mFormat, effectiveUsage); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 282 | } else { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 283 | // here we have to reallocate a new buffer because we could have a |
| 284 | // client in our process with a reference to it (eg: status bar), |
| 285 | // and we can't release the handle under its feet. |
| 286 | buffer.clear(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 287 | buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 288 | err = buffer->initCheck(); |
| 289 | } |
| 290 | |
| 291 | if (err || buffer->handle == 0) { |
| 292 | LOGE_IF(err || buffer->handle == 0, |
| 293 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", |
| 294 | this, index, w, h, strerror(-err)); |
| 295 | } else { |
| 296 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 297 | "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", |
| 298 | this, index, w, h, buffer->handle); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if (err == NO_ERROR && buffer->handle != 0) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 302 | Mutex::Autolock _l(mLock); |
| 303 | if (mWidth && mHeight) { |
| 304 | // and we have new buffer |
| 305 | mBuffers[index] = buffer; |
| 306 | // texture is now dirty... |
| 307 | mTextures[index].dirty = true; |
| 308 | } else { |
| 309 | // oops we got killed while we were allocating the buffer |
| 310 | buffer.clear(); |
| 311 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 312 | } |
| 313 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 316 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
| 317 | { |
| 318 | /* |
| 319 | * buffers used for software rendering, but h/w composition |
| 320 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 321 | * |
| 322 | * buffers used for h/w rendering and h/w composition |
| 323 | * are allocated with HW_RENDER | HW_TEXTURE |
| 324 | * |
| 325 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 326 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 327 | * |
| 328 | */ |
| 329 | |
| 330 | if (mSecure) { |
| 331 | // secure buffer, don't store it into the GPU |
| 332 | usage = GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 333 | GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 334 | } else { |
| 335 | // it's allowed to modify the usage flags here, but generally |
| 336 | // the requested flags should be honored. |
Mathias Agopian | a4b740e | 2009-10-05 18:20:39 -0700 | [diff] [blame^] | 337 | if (mNoEGLImageForSwBuffers) { |
| 338 | if (usage & GraphicBuffer::USAGE_HW_MASK) { |
| 339 | // request EGLImage for h/w buffers only |
| 340 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 341 | } |
| 342 | } else { |
| 343 | // request EGLImage for all buffers |
| 344 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 345 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 346 | } |
| 347 | return usage; |
| 348 | } |
| 349 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | uint32_t Layer::doTransaction(uint32_t flags) |
| 351 | { |
| 352 | const Layer::State& front(drawingState()); |
| 353 | const Layer::State& temp(currentState()); |
| 354 | |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 355 | if ((front.requested_w != temp.requested_w) || |
| 356 | (front.requested_h != temp.requested_h)) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 357 | // 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] | 358 | LOGD_IF(DEBUG_RESIZE, |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 359 | "resize (layer=%p), requested (%dx%d), " |
| 360 | "drawing (%d,%d), (%dx%d), (%dx%d)", |
Mathias Agopian | 7e4a587 | 2009-09-29 22:39:22 -0700 | [diff] [blame] | 361 | this, |
| 362 | int(temp.requested_w), int(temp.requested_h), |
| 363 | int(front.requested_w), int(front.requested_h), |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 364 | int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()), |
| 365 | int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 367 | // we're being resized and there is a freeze display request, |
| 368 | // acquire a freeze lock, so that the screen stays put |
| 369 | // until we've redrawn at the new size; this is to avoid |
| 370 | // glitches upon orientation changes. |
| 371 | if (mFlinger->hasFreezeRequest()) { |
| 372 | // if the surface is hidden, don't try to acquire the |
| 373 | // freeze lock, since hidden surfaces may never redraw |
| 374 | if (!(front.flags & ISurfaceComposer::eLayerHidden)) { |
| 375 | mFreezeLock = mFlinger->getFreezeLock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 378 | |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 379 | // this will make sure LayerBase::doTransaction doesn't update |
| 380 | // the drawing state's size |
| 381 | Layer::State& editDraw(mDrawingState); |
| 382 | editDraw.requested_w = temp.requested_w; |
| 383 | editDraw.requested_h = temp.requested_h; |
| 384 | |
Mathias Agopian | 6656dbc | 2009-09-30 12:48:47 -0700 | [diff] [blame] | 385 | // record the new size, form this point on, when the client request a |
| 386 | // buffer, it'll get the new size. |
| 387 | setDrawingSize(temp.requested_w, temp.requested_h); |
| 388 | |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 389 | // all buffers need reallocation |
| 390 | lcblk->reallocate(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 392 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | if (temp.sequence != front.sequence) { |
| 394 | if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { |
| 395 | // this surface is now hidden, so it shouldn't hold a freeze lock |
| 396 | // (it may never redraw, which is fine if it is hidden) |
| 397 | mFreezeLock.clear(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return LayerBase::doTransaction(flags); |
| 402 | } |
| 403 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 404 | void Layer::setDrawingSize(uint32_t w, uint32_t h) { |
| 405 | Mutex::Autolock _l(mLock); |
| 406 | mWidth = w; |
| 407 | mHeight = h; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // ---------------------------------------------------------------------------- |
| 411 | // pageflip handling... |
| 412 | // ---------------------------------------------------------------------------- |
| 413 | |
| 414 | void Layer::lockPageFlip(bool& recomputeVisibleRegions) |
| 415 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 416 | ssize_t buf = lcblk->retireAndLock(); |
| 417 | if (buf < NO_ERROR) { |
| 418 | //LOGW("nothing to retire (%s)", strerror(-buf)); |
| 419 | // NOTE: here the buffer is locked because we will used |
| 420 | // for composition later in the loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return; |
| 422 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 423 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 424 | // we retired a buffer, which becomes the new front buffer |
| 425 | mFrontBufferIndex = buf; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 427 | // get the dirty region |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 428 | sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 429 | const Region dirty(lcblk->getDirtyRegion(buf)); |
| 430 | mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 431 | |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 432 | const Layer::State& front(drawingState()); |
Mathias Agopian | df3e0b9 | 2009-09-30 14:07:22 -0700 | [diff] [blame] | 433 | if (newFrontBuffer->getWidth() == front.requested_w && |
| 434 | newFrontBuffer->getHeight() == front.requested_h) |
| 435 | { |
| 436 | if ((front.w != front.requested_w) || |
| 437 | (front.h != front.requested_h)) |
| 438 | { |
| 439 | // Here we pretend the transaction happened by updating the |
| 440 | // current and drawing states. Drawing state is only accessed |
| 441 | // in this thread, no need to have it locked |
| 442 | Layer::State& editDraw(mDrawingState); |
| 443 | editDraw.w = editDraw.requested_w; |
| 444 | editDraw.h = editDraw.requested_h; |
| 445 | |
| 446 | // We also need to update the current state so that we don't |
| 447 | // end-up doing too much work during the next transaction. |
| 448 | // NOTE: We actually don't need hold the transaction lock here |
| 449 | // because State::w and State::h are only accessed from |
| 450 | // this thread |
| 451 | Layer::State& editTemp(currentState()); |
| 452 | editTemp.w = editDraw.w; |
| 453 | editTemp.h = editDraw.h; |
| 454 | |
| 455 | // recompute visible region |
| 456 | recomputeVisibleRegions = true; |
| 457 | |
| 458 | // we now have the correct size, unfreeze the screen |
| 459 | mFreezeLock.clear(); |
| 460 | } |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 463 | // FIXME: signal an event if we have more buffers waiting |
| 464 | // mFlinger->signalEvent(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 466 | if (!mPostedDirtyRegion.isEmpty()) { |
| 467 | reloadTexture( mPostedDirtyRegion ); |
| 468 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void Layer::unlockPageFlip( |
| 472 | const Transform& planeTransform, Region& outDirtyRegion) |
| 473 | { |
| 474 | Region dirtyRegion(mPostedDirtyRegion); |
| 475 | if (!dirtyRegion.isEmpty()) { |
| 476 | mPostedDirtyRegion.clear(); |
| 477 | // The dirty region is given in the layer's coordinate space |
| 478 | // transform the dirty region by the surface's transformation |
| 479 | // and the global transformation. |
| 480 | const Layer::State& s(drawingState()); |
| 481 | const Transform tr(planeTransform * s.transform); |
| 482 | dirtyRegion = tr.transform(dirtyRegion); |
| 483 | |
| 484 | // At this point, the dirty region is in screen space. |
| 485 | // Make sure it's constrained by the visible region (which |
| 486 | // is in screen space as well). |
| 487 | dirtyRegion.andSelf(visibleRegionScreen); |
| 488 | outDirtyRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | void Layer::finishPageFlip() |
| 493 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 494 | status_t err = lcblk->unlock( mFrontBufferIndex ); |
| 495 | LOGE_IF(err!=NO_ERROR, |
| 496 | "layer %p, buffer=%d wasn't locked!", |
| 497 | this, mFrontBufferIndex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 500 | // --------------------------------------------------------------------------- |
| 501 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 502 | Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, |
| 503 | SurfaceID id, const sp<Layer>& owner) |
| 504 | : Surface(flinger, id, owner->getIdentity(), owner) |
| 505 | { |
| 506 | } |
| 507 | |
| 508 | Layer::SurfaceLayer::~SurfaceLayer() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 509 | { |
| 510 | } |
| 511 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 512 | sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 513 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 514 | sp<GraphicBuffer> buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 515 | sp<Layer> owner(getOwner()); |
| 516 | if (owner != 0) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 517 | LOGE_IF(uint32_t(index)>=NUM_BUFFERS, |
| 518 | "getBuffer() index (%d) out of range", index); |
| 519 | if (uint32_t(index) < NUM_BUFFERS) { |
| 520 | buffer = owner->requestBuffer(index, usage); |
| 521 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 522 | } |
| 523 | return buffer; |
| 524 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | |
| 526 | // --------------------------------------------------------------------------- |
| 527 | |
| 528 | |
| 529 | }; // namespace android |