The Android Open Source Project | 9066cfe | 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 | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <math.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/StopWatch.h> |
| 25 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 26 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <ui/PixelFormat.h> |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 28 | #include <ui/FramebufferNativeWindow.h> |
| 29 | |
| 30 | #include <hardware/copybit.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | #include "LayerBuffer.h" |
| 33 | #include "SurfaceFlinger.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include "DisplayHardware/DisplayHardware.h" |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | // --------------------------------------------------------------------------- |
| 39 | |
| 40 | const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20; |
| 41 | const char* const LayerBuffer::typeID = "LayerBuffer"; |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame^] | 42 | gralloc_module_t const* LayerBuffer::sGrallocModule = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | // --------------------------------------------------------------------------- |
| 45 | |
| 46 | LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | 6edf5af | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 47 | const sp<Client>& client, int32_t i) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | : LayerBaseClient(flinger, display, client, i), |
| 49 | mNeedsBlending(false) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | LayerBuffer::~LayerBuffer() |
| 54 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 57 | void LayerBuffer::onFirstRef() |
| 58 | { |
Mathias Agopian | c660395 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 59 | LayerBaseClient::onFirstRef(); |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 60 | mSurface = new SurfaceLayerBuffer(mFlinger, clientIndex(), |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 61 | const_cast<LayerBuffer *>(this)); |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame^] | 62 | |
| 63 | hw_module_t const* module = (hw_module_t const*)sGrallocModule; |
| 64 | if (!module) { |
| 65 | // NOTE: technically there is a race here, but it shouldn't |
| 66 | // cause any problem since hw_get_module() always returns |
| 67 | // the same value. |
| 68 | if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) { |
| 69 | sGrallocModule = (gralloc_module_t const *)module; |
| 70 | } |
| 71 | } |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 74 | sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | { |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 76 | return mSurface; |
| 77 | } |
| 78 | |
| 79 | status_t LayerBuffer::ditch() |
| 80 | { |
| 81 | mSurface.clear(); |
| 82 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool LayerBuffer::needsBlending() const { |
| 86 | return mNeedsBlending; |
| 87 | } |
| 88 | |
| 89 | void LayerBuffer::setNeedsBlending(bool blending) { |
| 90 | mNeedsBlending = blending; |
| 91 | } |
| 92 | |
| 93 | void LayerBuffer::postBuffer(ssize_t offset) |
| 94 | { |
| 95 | sp<Source> source(getSource()); |
| 96 | if (source != 0) |
| 97 | source->postBuffer(offset); |
| 98 | } |
| 99 | |
| 100 | void LayerBuffer::unregisterBuffers() |
| 101 | { |
| 102 | sp<Source> source(clearSource()); |
| 103 | if (source != 0) |
| 104 | source->unregisterBuffers(); |
| 105 | } |
| 106 | |
| 107 | uint32_t LayerBuffer::doTransaction(uint32_t flags) |
| 108 | { |
| 109 | sp<Source> source(getSource()); |
| 110 | if (source != 0) |
| 111 | source->onTransaction(flags); |
| 112 | return LayerBase::doTransaction(flags); |
| 113 | } |
| 114 | |
| 115 | void LayerBuffer::unlockPageFlip(const Transform& planeTransform, |
| 116 | Region& outDirtyRegion) |
| 117 | { |
| 118 | // this code-path must be as tight as possible, it's called each time |
| 119 | // the screen is composited. |
| 120 | sp<Source> source(getSource()); |
| 121 | if (source != 0) |
| 122 | source->onVisibilityResolved(planeTransform); |
| 123 | LayerBase::unlockPageFlip(planeTransform, outDirtyRegion); |
| 124 | } |
| 125 | |
| 126 | void LayerBuffer::onDraw(const Region& clip) const |
| 127 | { |
| 128 | sp<Source> source(getSource()); |
| 129 | if (LIKELY(source != 0)) { |
| 130 | source->onDraw(clip); |
| 131 | } else { |
| 132 | clearWithOpenGL(clip); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool LayerBuffer::transformed() const |
| 137 | { |
| 138 | sp<Source> source(getSource()); |
| 139 | if (LIKELY(source != 0)) |
| 140 | return source->transformed(); |
| 141 | return false; |
| 142 | } |
| 143 | |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 144 | void LayerBuffer::serverDestroy() |
| 145 | { |
| 146 | sp<Source> source(clearSource()); |
| 147 | if (source != 0) { |
| 148 | source->destroy(); |
| 149 | } |
| 150 | } |
| 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | /** |
| 153 | * This creates a "buffer" source for this surface |
| 154 | */ |
| 155 | status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers) |
| 156 | { |
| 157 | Mutex::Autolock _l(mLock); |
| 158 | if (mSource != 0) |
| 159 | return INVALID_OPERATION; |
| 160 | |
| 161 | sp<BufferSource> source = new BufferSource(*this, buffers); |
| 162 | |
| 163 | status_t result = source->getStatus(); |
| 164 | if (result == NO_ERROR) { |
| 165 | mSource = source; |
| 166 | } |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * This creates an "overlay" source for this surface |
| 172 | */ |
| 173 | sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f) |
| 174 | { |
| 175 | sp<OverlayRef> result; |
| 176 | Mutex::Autolock _l(mLock); |
| 177 | if (mSource != 0) |
| 178 | return result; |
| 179 | |
| 180 | sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f); |
| 181 | if (result != 0) { |
| 182 | mSource = source; |
| 183 | } |
| 184 | return result; |
| 185 | } |
| 186 | |
| 187 | sp<LayerBuffer::Source> LayerBuffer::getSource() const { |
| 188 | Mutex::Autolock _l(mLock); |
| 189 | return mSource; |
| 190 | } |
| 191 | |
| 192 | sp<LayerBuffer::Source> LayerBuffer::clearSource() { |
| 193 | sp<Source> source; |
| 194 | Mutex::Autolock _l(mLock); |
| 195 | source = mSource; |
| 196 | mSource.clear(); |
| 197 | return source; |
| 198 | } |
| 199 | |
| 200 | // ============================================================================ |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 201 | // LayerBuffer::SurfaceLayerBuffer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | // ============================================================================ |
| 203 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 204 | LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 205 | SurfaceID id, const sp<LayerBuffer>& owner) |
| 206 | : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | { |
| 208 | } |
| 209 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 210 | LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | { |
| 212 | unregisterBuffers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 215 | status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers( |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 216 | const ISurface::BufferHeap& buffers) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 218 | sp<LayerBuffer> owner(getOwner()); |
| 219 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | return owner->registerBuffers(buffers); |
| 221 | return NO_INIT; |
| 222 | } |
| 223 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 224 | void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 226 | sp<LayerBuffer> owner(getOwner()); |
| 227 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | owner->postBuffer(offset); |
| 229 | } |
| 230 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 231 | void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 233 | sp<LayerBuffer> owner(getOwner()); |
| 234 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | owner->unregisterBuffers(); |
| 236 | } |
| 237 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 238 | sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | uint32_t w, uint32_t h, int32_t format) { |
| 240 | sp<OverlayRef> result; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 241 | sp<LayerBuffer> owner(getOwner()); |
| 242 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | result = owner->createOverlay(w, h, format); |
| 244 | return result; |
| 245 | } |
| 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | // ============================================================================ |
| 248 | // LayerBuffer::Buffer |
| 249 | // ============================================================================ |
| 250 | |
| 251 | LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset) |
| 252 | : mBufferHeap(buffers) |
| 253 | { |
| 254 | NativeBuffer& src(mNativeBuffer); |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame^] | 255 | src.img.handle = 0; |
Mathias Agopian | 2eab9d8 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 256 | |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame^] | 257 | gralloc_module_t const * module = LayerBuffer::getGrallocModule(); |
| 258 | if (module && module->perform) { |
| 259 | int err = module->perform(module, |
| 260 | GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER, |
| 261 | buffers.heap->heapID(), buffers.heap->getSize(), |
| 262 | offset, buffers.heap->base(), |
| 263 | &src.img.handle); |
| 264 | |
| 265 | if (err == NO_ERROR) { |
| 266 | src.crop.l = 0; |
| 267 | src.crop.t = 0; |
| 268 | src.crop.r = buffers.w; |
| 269 | src.crop.b = buffers.h; |
| 270 | |
| 271 | src.img.w = buffers.hor_stride ?: buffers.w; |
| 272 | src.img.h = buffers.ver_stride ?: buffers.h; |
| 273 | src.img.format = buffers.format; |
| 274 | src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset); |
| 275 | } |
| 276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | LayerBuffer::Buffer::~Buffer() |
| 280 | { |
Mathias Agopian | 2eab9d8 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 281 | NativeBuffer& src(mNativeBuffer); |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame^] | 282 | if (src.img.handle) { |
| 283 | native_handle_delete(src.img.handle); |
| 284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // ============================================================================ |
| 288 | // LayerBuffer::Source |
| 289 | // LayerBuffer::BufferSource |
| 290 | // LayerBuffer::OverlaySource |
| 291 | // ============================================================================ |
| 292 | |
| 293 | LayerBuffer::Source::Source(LayerBuffer& layer) |
| 294 | : mLayer(layer) |
| 295 | { |
| 296 | } |
| 297 | LayerBuffer::Source::~Source() { |
| 298 | } |
| 299 | void LayerBuffer::Source::onDraw(const Region& clip) const { |
| 300 | } |
| 301 | void LayerBuffer::Source::onTransaction(uint32_t flags) { |
| 302 | } |
| 303 | void LayerBuffer::Source::onVisibilityResolved( |
| 304 | const Transform& planeTransform) { |
| 305 | } |
| 306 | void LayerBuffer::Source::postBuffer(ssize_t offset) { |
| 307 | } |
| 308 | void LayerBuffer::Source::unregisterBuffers() { |
| 309 | } |
| 310 | bool LayerBuffer::Source::transformed() const { |
| 311 | return mLayer.mTransformed; |
| 312 | } |
| 313 | |
| 314 | // --------------------------------------------------------------------------- |
| 315 | |
| 316 | LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer, |
| 317 | const ISurface::BufferHeap& buffers) |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 318 | : Source(layer), mStatus(NO_ERROR), mBufferSize(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | { |
| 320 | if (buffers.heap == NULL) { |
| 321 | // this is allowed, but in this case, it is illegal to receive |
| 322 | // postBuffer(). The surface just erases the framebuffer with |
| 323 | // fully transparent pixels. |
| 324 | mBufferHeap = buffers; |
| 325 | mLayer.setNeedsBlending(false); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT; |
| 330 | if (err != NO_ERROR) { |
| 331 | LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err)); |
| 332 | mStatus = err; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | PixelFormatInfo info; |
| 337 | err = getPixelFormatInfo(buffers.format, &info); |
| 338 | if (err != NO_ERROR) { |
| 339 | LOGE("LayerBuffer::BufferSource: invalid format %d (%s)", |
| 340 | buffers.format, strerror(err)); |
| 341 | mStatus = err; |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (buffers.hor_stride<0 || buffers.ver_stride<0) { |
| 346 | LOGE("LayerBuffer::BufferSource: invalid parameters " |
| 347 | "(w=%d, h=%d, xs=%d, ys=%d)", |
| 348 | buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride); |
| 349 | mStatus = BAD_VALUE; |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | mBufferHeap = buffers; |
| 354 | mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0); |
| 355 | mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride; |
| 356 | mLayer.forceVisibilityTransaction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | LayerBuffer::BufferSource::~BufferSource() |
| 360 | { |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 361 | if (mTexture.name != -1U) { |
| 362 | glDeleteTextures(1, &mTexture.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 364 | if (mTexture.image != EGL_NO_IMAGE_KHR) { |
| 365 | EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay()); |
| 366 | eglDestroyImageKHR(dpy, mTexture.image); |
Mathias Agopian | 68eeb80 | 2009-06-25 15:39:25 -0700 | [diff] [blame] | 367 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void LayerBuffer::BufferSource::postBuffer(ssize_t offset) |
| 371 | { |
| 372 | ISurface::BufferHeap buffers; |
| 373 | { // scope for the lock |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 374 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | buffers = mBufferHeap; |
| 376 | if (buffers.heap != 0) { |
| 377 | const size_t memorySize = buffers.heap->getSize(); |
| 378 | if ((size_t(offset) + mBufferSize) > memorySize) { |
| 379 | LOGE("LayerBuffer::BufferSource::postBuffer() " |
| 380 | "invalid buffer (offset=%d, size=%d, heap-size=%d", |
| 381 | int(offset), int(mBufferSize), int(memorySize)); |
| 382 | return; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | sp<Buffer> buffer; |
| 388 | if (buffers.heap != 0) { |
| 389 | buffer = new LayerBuffer::Buffer(buffers, offset); |
| 390 | if (buffer->getStatus() != NO_ERROR) |
| 391 | buffer.clear(); |
| 392 | setBuffer(buffer); |
| 393 | mLayer.invalidate(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void LayerBuffer::BufferSource::unregisterBuffers() |
| 398 | { |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 399 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | mBufferHeap.heap.clear(); |
| 401 | mBuffer.clear(); |
| 402 | mLayer.invalidate(); |
| 403 | } |
| 404 | |
| 405 | sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const |
| 406 | { |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 407 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | return mBuffer; |
| 409 | } |
| 410 | |
| 411 | void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer) |
| 412 | { |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 413 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | mBuffer = buffer; |
| 415 | } |
| 416 | |
| 417 | bool LayerBuffer::BufferSource::transformed() const |
| 418 | { |
| 419 | return mBufferHeap.transform ? true : Source::transformed(); |
| 420 | } |
| 421 | |
| 422 | void LayerBuffer::BufferSource::onDraw(const Region& clip) const |
| 423 | { |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 424 | sp<Buffer> ourBuffer(getBuffer()); |
| 425 | if (UNLIKELY(ourBuffer == 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | // nothing to do, we don't have a buffer |
| 427 | mLayer.clearWithOpenGL(clip); |
| 428 | return; |
| 429 | } |
| 430 | |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 431 | status_t err = NO_ERROR; |
| 432 | NativeBuffer src(ourBuffer->getBuffer()); |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 433 | const Rect transformedBounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 435 | if (UNLIKELY(mTexture.name == -1LU)) { |
| 436 | mTexture.name = mLayer.createTexture(); |
| 437 | } |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 438 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 439 | #if defined(EGL_ANDROID_image_native_buffer) |
| 440 | if (mLayer.mFlags & DisplayHardware::DIRECT_TEXTURE) { |
| 441 | // NOTE: Assume the buffer is allocated with the proper USAGE flags |
| 442 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer( |
| 443 | src.crop.r, src.crop.b, src.img.format, |
| 444 | GraphicBuffer::USAGE_HW_TEXTURE, |
| 445 | src.img.w, src.img.handle, false); |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 446 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 447 | err = mLayer.initializeEglImage(graphicBuffer, &mTexture); |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 448 | } |
Mathias Agopian | 26c28b1 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 449 | #endif |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 450 | else { |
| 451 | err = INVALID_OPERATION; |
| 452 | } |
| 453 | |
| 454 | if (err != NO_ERROR) { |
Mathias Agopian | f007a2f | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 455 | // slower fallback |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 456 | GGLSurface t; |
| 457 | t.version = sizeof(GGLSurface); |
| 458 | t.width = src.crop.r; |
| 459 | t.height = src.crop.b; |
| 460 | t.stride = src.img.w; |
| 461 | t.vstride= src.img.h; |
| 462 | t.format = src.img.format; |
Mathias Agopian | 2eab9d8 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 463 | t.data = (GGLubyte*)src.img.base; |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 464 | const Region dirty(Rect(t.width, t.height)); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 465 | mLayer.loadTexture(&mTexture, dirty, t); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 466 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 468 | mTexture.transform = mBufferHeap.transform; |
| 469 | mLayer.drawWithOpenGL(clip, mTexture); |
| 470 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | |
| 472 | // --------------------------------------------------------------------------- |
| 473 | |
| 474 | LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer, |
| 475 | sp<OverlayRef>* overlayRef, |
| 476 | uint32_t w, uint32_t h, int32_t format) |
| 477 | : Source(layer), mVisibilityChanged(false), |
| 478 | mOverlay(0), mOverlayHandle(0), mOverlayDevice(0) |
| 479 | { |
| 480 | overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine(); |
| 481 | if (overlay_dev == NULL) { |
| 482 | // overlays not supported |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | mOverlayDevice = overlay_dev; |
| 487 | overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format); |
| 488 | if (overlay == NULL) { |
| 489 | // couldn't create the overlay (no memory? no more overlays?) |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | // enable dithering... |
| 494 | overlay_dev->setParameter(overlay_dev, overlay, |
| 495 | OVERLAY_DITHER, OVERLAY_ENABLE); |
| 496 | |
| 497 | mOverlay = overlay; |
| 498 | mWidth = overlay->w; |
| 499 | mHeight = overlay->h; |
| 500 | mFormat = overlay->format; |
| 501 | mWidthStride = overlay->w_stride; |
| 502 | mHeightStride = overlay->h_stride; |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 503 | mInitialized = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | |
| 505 | mOverlayHandle = overlay->getHandleRef(overlay); |
| 506 | |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 507 | sp<OverlayChannel> channel = new OverlayChannel( &layer ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | |
| 509 | *overlayRef = new OverlayRef(mOverlayHandle, channel, |
| 510 | mWidth, mHeight, mFormat, mWidthStride, mHeightStride); |
Benny Wong | e38212a | 2009-09-16 14:48:52 -0500 | [diff] [blame] | 511 | mLayer.mFlinger->signalEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | LayerBuffer::OverlaySource::~OverlaySource() |
| 515 | { |
| 516 | if (mOverlay && mOverlayDevice) { |
| 517 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 518 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 519 | } |
| 520 | } |
| 521 | |
Rebecca Schultz Zavin | 7ac5e69 | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 522 | void LayerBuffer::OverlaySource::onDraw(const Region& clip) const |
| 523 | { |
Mathias Agopian | 8ae0384 | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 524 | // this would be where the color-key would be set, should we need it. |
Rebecca Schultz Zavin | c854678 | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 525 | GLclampx red = 0; |
| 526 | GLclampx green = 0; |
Mathias Agopian | 8ae0384 | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 527 | GLclampx blue = 0; |
Rebecca Schultz Zavin | c854678 | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 528 | mLayer.clearWithOpenGL(clip, red, green, blue, 0); |
Rebecca Schultz Zavin | 7ac5e69 | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 529 | } |
| 530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | void LayerBuffer::OverlaySource::onTransaction(uint32_t flags) |
| 532 | { |
| 533 | const Layer::State& front(mLayer.drawingState()); |
| 534 | const Layer::State& temp(mLayer.currentState()); |
| 535 | if (temp.sequence != front.sequence) { |
| 536 | mVisibilityChanged = true; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | void LayerBuffer::OverlaySource::onVisibilityResolved( |
| 541 | const Transform& planeTransform) |
| 542 | { |
| 543 | // this code-path must be as tight as possible, it's called each time |
| 544 | // the screen is composited. |
| 545 | if (UNLIKELY(mOverlay != 0)) { |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 546 | if (mVisibilityChanged || !mInitialized) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | mVisibilityChanged = false; |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 548 | mInitialized = true; |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 549 | const Rect bounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | int x = bounds.left; |
| 551 | int y = bounds.top; |
| 552 | int w = bounds.width(); |
| 553 | int h = bounds.height(); |
| 554 | |
| 555 | // we need a lock here to protect "destroy" |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 556 | Mutex::Autolock _l(mOverlaySourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | if (mOverlay) { |
| 558 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 559 | overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h); |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 560 | overlay_dev->setParameter(overlay_dev, mOverlay, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | OVERLAY_TRANSFORM, mLayer.getOrientation()); |
Rebecca Schultz Zavin | 7ac5e69 | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 562 | overlay_dev->commit(overlay_dev, mOverlay); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 568 | void LayerBuffer::OverlaySource::destroy() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | { |
| 570 | // we need a lock here to protect "onVisibilityResolved" |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 571 | Mutex::Autolock _l(mOverlaySourceLock); |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 572 | if (mOverlay && mOverlayDevice) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 574 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 575 | mOverlay = 0; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // --------------------------------------------------------------------------- |
| 580 | }; // namespace android |