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 <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 | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 26 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <ui/PixelFormat.h> |
Mathias Agopian | 69029eb | 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 | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | #include "LayerBuffer.h" |
| 33 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include "DisplayHardware/DisplayHardware.h" |
| 35 | |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 36 | #include "gralloc_priv.h" // needed for msm / copybit |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | // --------------------------------------------------------------------------- |
| 41 | |
| 42 | const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20; |
| 43 | const char* const LayerBuffer::typeID = "LayerBuffer"; |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 48 | const sp<Client>& client, int32_t i) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | : LayerBaseClient(flinger, display, client, i), |
| 50 | mNeedsBlending(false) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | LayerBuffer::~LayerBuffer() |
| 55 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 58 | void LayerBuffer::onFirstRef() |
| 59 | { |
Mathias Agopian | 2e12324 | 2009-06-23 20:06:46 -0700 | [diff] [blame] | 60 | LayerBaseClient::onFirstRef(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 61 | mSurface = new SurfaceLayerBuffer(mFlinger, clientIndex(), |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 62 | const_cast<LayerBuffer *>(this)); |
| 63 | } |
| 64 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 65 | sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | { |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 67 | return mSurface; |
| 68 | } |
| 69 | |
| 70 | status_t LayerBuffer::ditch() |
| 71 | { |
| 72 | mSurface.clear(); |
| 73 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool LayerBuffer::needsBlending() const { |
| 77 | return mNeedsBlending; |
| 78 | } |
| 79 | |
| 80 | void LayerBuffer::setNeedsBlending(bool blending) { |
| 81 | mNeedsBlending = blending; |
| 82 | } |
| 83 | |
| 84 | void LayerBuffer::postBuffer(ssize_t offset) |
| 85 | { |
| 86 | sp<Source> source(getSource()); |
| 87 | if (source != 0) |
| 88 | source->postBuffer(offset); |
| 89 | } |
| 90 | |
| 91 | void LayerBuffer::unregisterBuffers() |
| 92 | { |
| 93 | sp<Source> source(clearSource()); |
| 94 | if (source != 0) |
| 95 | source->unregisterBuffers(); |
| 96 | } |
| 97 | |
| 98 | uint32_t LayerBuffer::doTransaction(uint32_t flags) |
| 99 | { |
| 100 | sp<Source> source(getSource()); |
| 101 | if (source != 0) |
| 102 | source->onTransaction(flags); |
| 103 | return LayerBase::doTransaction(flags); |
| 104 | } |
| 105 | |
| 106 | void LayerBuffer::unlockPageFlip(const Transform& planeTransform, |
| 107 | Region& outDirtyRegion) |
| 108 | { |
| 109 | // this code-path must be as tight as possible, it's called each time |
| 110 | // the screen is composited. |
| 111 | sp<Source> source(getSource()); |
| 112 | if (source != 0) |
| 113 | source->onVisibilityResolved(planeTransform); |
| 114 | LayerBase::unlockPageFlip(planeTransform, outDirtyRegion); |
| 115 | } |
| 116 | |
| 117 | void LayerBuffer::onDraw(const Region& clip) const |
| 118 | { |
| 119 | sp<Source> source(getSource()); |
| 120 | if (LIKELY(source != 0)) { |
| 121 | source->onDraw(clip); |
| 122 | } else { |
| 123 | clearWithOpenGL(clip); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | bool LayerBuffer::transformed() const |
| 128 | { |
| 129 | sp<Source> source(getSource()); |
| 130 | if (LIKELY(source != 0)) |
| 131 | return source->transformed(); |
| 132 | return false; |
| 133 | } |
| 134 | |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 135 | void LayerBuffer::serverDestroy() |
| 136 | { |
| 137 | sp<Source> source(clearSource()); |
| 138 | if (source != 0) { |
| 139 | source->destroy(); |
| 140 | } |
| 141 | } |
| 142 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | /** |
| 144 | * This creates a "buffer" source for this surface |
| 145 | */ |
| 146 | status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers) |
| 147 | { |
| 148 | Mutex::Autolock _l(mLock); |
| 149 | if (mSource != 0) |
| 150 | return INVALID_OPERATION; |
| 151 | |
| 152 | sp<BufferSource> source = new BufferSource(*this, buffers); |
| 153 | |
| 154 | status_t result = source->getStatus(); |
| 155 | if (result == NO_ERROR) { |
| 156 | mSource = source; |
| 157 | } |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * This creates an "overlay" source for this surface |
| 163 | */ |
| 164 | sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f) |
| 165 | { |
| 166 | sp<OverlayRef> result; |
| 167 | Mutex::Autolock _l(mLock); |
| 168 | if (mSource != 0) |
| 169 | return result; |
| 170 | |
| 171 | sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f); |
| 172 | if (result != 0) { |
| 173 | mSource = source; |
| 174 | } |
| 175 | return result; |
| 176 | } |
| 177 | |
| 178 | sp<LayerBuffer::Source> LayerBuffer::getSource() const { |
| 179 | Mutex::Autolock _l(mLock); |
| 180 | return mSource; |
| 181 | } |
| 182 | |
| 183 | sp<LayerBuffer::Source> LayerBuffer::clearSource() { |
| 184 | sp<Source> source; |
| 185 | Mutex::Autolock _l(mLock); |
| 186 | source = mSource; |
| 187 | mSource.clear(); |
| 188 | return source; |
| 189 | } |
| 190 | |
| 191 | // ============================================================================ |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 192 | // LayerBuffer::SurfaceLayerBuffer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | // ============================================================================ |
| 194 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 195 | LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 196 | SurfaceID id, const sp<LayerBuffer>& owner) |
| 197 | : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | { |
| 199 | } |
| 200 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 201 | LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | { |
| 203 | unregisterBuffers(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 206 | status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers( |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 207 | const ISurface::BufferHeap& buffers) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 209 | sp<LayerBuffer> owner(getOwner()); |
| 210 | if (owner != 0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | return owner->registerBuffers(buffers); |
| 212 | return NO_INIT; |
| 213 | } |
| 214 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 215 | void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 217 | sp<LayerBuffer> owner(getOwner()); |
| 218 | if (owner != 0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | owner->postBuffer(offset); |
| 220 | } |
| 221 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 222 | void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 224 | sp<LayerBuffer> owner(getOwner()); |
| 225 | if (owner != 0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | owner->unregisterBuffers(); |
| 227 | } |
| 228 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 229 | sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay( |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | uint32_t w, uint32_t h, int32_t format) { |
| 231 | sp<OverlayRef> result; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 232 | sp<LayerBuffer> owner(getOwner()); |
| 233 | if (owner != 0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | result = owner->createOverlay(w, h, format); |
| 235 | return result; |
| 236 | } |
| 237 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | // ============================================================================ |
| 239 | // LayerBuffer::Buffer |
| 240 | // ============================================================================ |
| 241 | |
| 242 | LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset) |
| 243 | : mBufferHeap(buffers) |
| 244 | { |
| 245 | NativeBuffer& src(mNativeBuffer); |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 246 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | src.crop.l = 0; |
| 248 | src.crop.t = 0; |
| 249 | src.crop.r = buffers.w; |
| 250 | src.crop.b = buffers.h; |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 251 | |
| 252 | src.img.w = buffers.hor_stride ?: buffers.w; |
| 253 | src.img.h = buffers.ver_stride ?: buffers.h; |
| 254 | src.img.format = buffers.format; |
| 255 | src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset); |
| 256 | |
Mathias Agopian | d512f23 | 2009-06-25 17:41:12 -0700 | [diff] [blame] | 257 | // FIXME: gross hack, we should never access private_handle_t from here, |
| 258 | // but this is needed by msm drivers |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 259 | private_handle_t* hnd = new private_handle_t( |
| 260 | buffers.heap->heapID(), buffers.heap->getSize(), 0); |
| 261 | hnd->offset = offset; |
| 262 | src.img.handle = hnd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | LayerBuffer::Buffer::~Buffer() |
| 266 | { |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 267 | NativeBuffer& src(mNativeBuffer); |
| 268 | if (src.img.handle) |
| 269 | delete (private_handle_t*)src.img.handle; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // ============================================================================ |
| 273 | // LayerBuffer::Source |
| 274 | // LayerBuffer::BufferSource |
| 275 | // LayerBuffer::OverlaySource |
| 276 | // ============================================================================ |
| 277 | |
| 278 | LayerBuffer::Source::Source(LayerBuffer& layer) |
| 279 | : mLayer(layer) |
| 280 | { |
| 281 | } |
| 282 | LayerBuffer::Source::~Source() { |
| 283 | } |
| 284 | void LayerBuffer::Source::onDraw(const Region& clip) const { |
| 285 | } |
| 286 | void LayerBuffer::Source::onTransaction(uint32_t flags) { |
| 287 | } |
| 288 | void LayerBuffer::Source::onVisibilityResolved( |
| 289 | const Transform& planeTransform) { |
| 290 | } |
| 291 | void LayerBuffer::Source::postBuffer(ssize_t offset) { |
| 292 | } |
| 293 | void LayerBuffer::Source::unregisterBuffers() { |
| 294 | } |
| 295 | bool LayerBuffer::Source::transformed() const { |
| 296 | return mLayer.mTransformed; |
| 297 | } |
| 298 | |
| 299 | // --------------------------------------------------------------------------- |
| 300 | |
| 301 | LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer, |
| 302 | const ISurface::BufferHeap& buffers) |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 303 | : Source(layer), mStatus(NO_ERROR), mBufferSize(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | { |
| 305 | if (buffers.heap == NULL) { |
| 306 | // this is allowed, but in this case, it is illegal to receive |
| 307 | // postBuffer(). The surface just erases the framebuffer with |
| 308 | // fully transparent pixels. |
| 309 | mBufferHeap = buffers; |
| 310 | mLayer.setNeedsBlending(false); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT; |
| 315 | if (err != NO_ERROR) { |
| 316 | LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err)); |
| 317 | mStatus = err; |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | PixelFormatInfo info; |
| 322 | err = getPixelFormatInfo(buffers.format, &info); |
| 323 | if (err != NO_ERROR) { |
| 324 | LOGE("LayerBuffer::BufferSource: invalid format %d (%s)", |
| 325 | buffers.format, strerror(err)); |
| 326 | mStatus = err; |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | if (buffers.hor_stride<0 || buffers.ver_stride<0) { |
| 331 | LOGE("LayerBuffer::BufferSource: invalid parameters " |
| 332 | "(w=%d, h=%d, xs=%d, ys=%d)", |
| 333 | buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride); |
| 334 | mStatus = BAD_VALUE; |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | mBufferHeap = buffers; |
| 339 | mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0); |
| 340 | mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride; |
| 341 | mLayer.forceVisibilityTransaction(); |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 342 | |
| 343 | hw_module_t const* module; |
| 344 | mBlitEngine = NULL; |
| 345 | if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) { |
| 346 | copybit_open(module, &mBlitEngine); |
| 347 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | LayerBuffer::BufferSource::~BufferSource() |
| 351 | { |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 352 | if (mTexture.name != -1U) { |
| 353 | glDeleteTextures(1, &mTexture.name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | } |
Mathias Agopian | 240c9fe | 2009-06-25 15:39:25 -0700 | [diff] [blame] | 355 | if (mBlitEngine) { |
| 356 | copybit_close(mBlitEngine); |
| 357 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void LayerBuffer::BufferSource::postBuffer(ssize_t offset) |
| 361 | { |
| 362 | ISurface::BufferHeap buffers; |
| 363 | { // scope for the lock |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 364 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | buffers = mBufferHeap; |
| 366 | if (buffers.heap != 0) { |
| 367 | const size_t memorySize = buffers.heap->getSize(); |
| 368 | if ((size_t(offset) + mBufferSize) > memorySize) { |
| 369 | LOGE("LayerBuffer::BufferSource::postBuffer() " |
| 370 | "invalid buffer (offset=%d, size=%d, heap-size=%d", |
| 371 | int(offset), int(mBufferSize), int(memorySize)); |
| 372 | return; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | sp<Buffer> buffer; |
| 378 | if (buffers.heap != 0) { |
| 379 | buffer = new LayerBuffer::Buffer(buffers, offset); |
| 380 | if (buffer->getStatus() != NO_ERROR) |
| 381 | buffer.clear(); |
| 382 | setBuffer(buffer); |
| 383 | mLayer.invalidate(); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void LayerBuffer::BufferSource::unregisterBuffers() |
| 388 | { |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 389 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | mBufferHeap.heap.clear(); |
| 391 | mBuffer.clear(); |
| 392 | mLayer.invalidate(); |
| 393 | } |
| 394 | |
| 395 | sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const |
| 396 | { |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 397 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | return mBuffer; |
| 399 | } |
| 400 | |
| 401 | void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer) |
| 402 | { |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 403 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | mBuffer = buffer; |
| 405 | } |
| 406 | |
| 407 | bool LayerBuffer::BufferSource::transformed() const |
| 408 | { |
| 409 | return mBufferHeap.transform ? true : Source::transformed(); |
| 410 | } |
| 411 | |
| 412 | void LayerBuffer::BufferSource::onDraw(const Region& clip) const |
| 413 | { |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 414 | sp<Buffer> ourBuffer(getBuffer()); |
| 415 | if (UNLIKELY(ourBuffer == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | // nothing to do, we don't have a buffer |
| 417 | mLayer.clearWithOpenGL(clip); |
| 418 | return; |
| 419 | } |
| 420 | |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 421 | status_t err = NO_ERROR; |
| 422 | NativeBuffer src(ourBuffer->getBuffer()); |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 423 | const Rect transformedBounds(mLayer.getTransformedBounds()); |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 424 | copybit_device_t* copybit = mBlitEngine; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 426 | if (copybit) { |
| 427 | const int src_width = src.crop.r - src.crop.l; |
| 428 | const int src_height = src.crop.b - src.crop.t; |
| 429 | int W = transformedBounds.width(); |
| 430 | int H = transformedBounds.height(); |
| 431 | if (mLayer.getOrientation() & Transform::ROT_90) { |
| 432 | int t(W); W=H; H=t; |
| 433 | } |
| 434 | |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 435 | #ifdef EGL_ANDROID_get_render_buffer |
| 436 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 437 | EGLSurface draw = eglGetCurrentSurface(EGL_DRAW); |
| 438 | EGLClientBuffer clientBuf = eglGetRenderBufferANDROID(dpy, draw); |
| 439 | android_native_buffer_t* nb = (android_native_buffer_t*)clientBuf; |
| 440 | if (nb == 0) { |
| 441 | err = BAD_VALUE; |
| 442 | } else { |
| 443 | copybit_image_t dst; |
| 444 | dst.w = nb->width; |
| 445 | dst.h = nb->height; |
| 446 | dst.format = nb->format; |
| 447 | dst.base = NULL; // unused by copybit on msm7k |
| 448 | dst.handle = (native_handle_t *)nb->handle; |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 449 | |
Mathias Agopian | d512f23 | 2009-06-25 17:41:12 -0700 | [diff] [blame] | 450 | /* With LayerBuffer, it is likely that we'll have to rescale the |
| 451 | * surface, because this is often used for video playback or |
| 452 | * camera-preview. Since we want these operation as fast as possible |
| 453 | * we make sure we can use the 2D H/W even if it doesn't support |
| 454 | * the requested scale factor, in which case we perform the scaling |
| 455 | * in several passes. */ |
| 456 | |
| 457 | const float min = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT); |
| 458 | const float mag = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT); |
| 459 | |
| 460 | float xscale = 1.0f; |
| 461 | if (src_width > W*min) xscale = 1.0f / min; |
| 462 | else if (src_width*mag < W) xscale = mag; |
| 463 | |
| 464 | float yscale = 1.0f; |
| 465 | if (src_height > H*min) yscale = 1.0f / min; |
| 466 | else if (src_height*mag < H) yscale = mag; |
| 467 | |
| 468 | if (UNLIKELY(xscale!=1.0f || yscale!=1.0f)) { |
| 469 | const int tmp_w = floorf(src_width * xscale); |
| 470 | const int tmp_h = floorf(src_height * yscale); |
| 471 | |
| 472 | if (mTempBitmap==0 || |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 473 | mTempBitmap->getWidth() < size_t(tmp_w) || |
| 474 | mTempBitmap->getHeight() < size_t(tmp_h)) { |
Mathias Agopian | d512f23 | 2009-06-25 17:41:12 -0700 | [diff] [blame] | 475 | mTempBitmap.clear(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 476 | mTempBitmap = new GraphicBuffer( |
| 477 | tmp_w, tmp_h, src.img.format, |
| 478 | GraphicBuffer::USAGE_HW_2D); |
Mathias Agopian | d512f23 | 2009-06-25 17:41:12 -0700 | [diff] [blame] | 479 | err = mTempBitmap->initCheck(); |
| 480 | } |
| 481 | |
| 482 | if (LIKELY(err == NO_ERROR)) { |
| 483 | NativeBuffer tmp; |
| 484 | tmp.img.w = tmp_w; |
| 485 | tmp.img.h = tmp_h; |
| 486 | tmp.img.format = src.img.format; |
| 487 | tmp.img.handle = (native_handle_t*)mTempBitmap->getNativeBuffer()->handle; |
| 488 | tmp.crop.l = 0; |
| 489 | tmp.crop.t = 0; |
| 490 | tmp.crop.r = tmp.img.w; |
| 491 | tmp.crop.b = tmp.img.h; |
| 492 | |
| 493 | region_iterator tmp_it(Region(Rect(tmp.crop.r, tmp.crop.b))); |
| 494 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0); |
| 495 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF); |
| 496 | copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE); |
| 497 | err = copybit->stretch(copybit, |
| 498 | &tmp.img, &src.img, &tmp.crop, &src.crop, &tmp_it); |
| 499 | src = tmp; |
| 500 | } |
| 501 | } |
| 502 | |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 503 | const Rect transformedBounds(mLayer.getTransformedBounds()); |
Mathias Agopian | d512f23 | 2009-06-25 17:41:12 -0700 | [diff] [blame] | 504 | const copybit_rect_t& drect = |
| 505 | reinterpret_cast<const copybit_rect_t&>(transformedBounds); |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 506 | const State& s(mLayer.drawingState()); |
| 507 | region_iterator it(clip); |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 508 | |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 509 | // pick the right orientation for this buffer |
| 510 | int orientation = mLayer.getOrientation(); |
| 511 | if (UNLIKELY(mBufferHeap.transform)) { |
| 512 | Transform rot90; |
| 513 | GraphicPlane::orientationToTransfrom( |
| 514 | ISurfaceComposer::eOrientation90, 0, 0, &rot90); |
| 515 | const Transform& planeTransform(mLayer.graphicPlane(0).transform()); |
| 516 | const Layer::State& s(mLayer.drawingState()); |
| 517 | Transform tr(planeTransform * s.transform * rot90); |
| 518 | orientation = tr.getOrientation(); |
| 519 | } |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 520 | |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 521 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, orientation); |
| 522 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, s.alpha); |
| 523 | copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE); |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 524 | |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 525 | err = copybit->stretch(copybit, |
| 526 | &dst, &src.img, &drect, &src.crop, &it); |
| 527 | if (err != NO_ERROR) { |
| 528 | LOGE("copybit failed (%s)", strerror(err)); |
| 529 | } |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 532 | #endif |
| 533 | |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 534 | if (!copybit || err) |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 535 | { |
| 536 | // OpenGL fall-back |
| 537 | if (UNLIKELY(mTexture.name == -1LU)) { |
| 538 | mTexture.name = mLayer.createTexture(); |
| 539 | } |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 540 | GLuint w = 0; |
| 541 | GLuint h = 0; |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 542 | GGLSurface t; |
| 543 | t.version = sizeof(GGLSurface); |
| 544 | t.width = src.crop.r; |
| 545 | t.height = src.crop.b; |
| 546 | t.stride = src.img.w; |
| 547 | t.vstride= src.img.h; |
| 548 | t.format = src.img.format; |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 549 | t.data = (GGLubyte*)src.img.base; |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 550 | const Region dirty(Rect(t.width, t.height)); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 551 | mLayer.loadTexture(&mTexture, dirty, t); |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 552 | mTexture.transform = mBufferHeap.transform; |
| 553 | mLayer.drawWithOpenGL(clip, mTexture); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 554 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | // --------------------------------------------------------------------------- |
| 559 | |
| 560 | LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer, |
| 561 | sp<OverlayRef>* overlayRef, |
| 562 | uint32_t w, uint32_t h, int32_t format) |
| 563 | : Source(layer), mVisibilityChanged(false), |
| 564 | mOverlay(0), mOverlayHandle(0), mOverlayDevice(0) |
| 565 | { |
| 566 | overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine(); |
| 567 | if (overlay_dev == NULL) { |
| 568 | // overlays not supported |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | mOverlayDevice = overlay_dev; |
| 573 | overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format); |
| 574 | if (overlay == NULL) { |
| 575 | // couldn't create the overlay (no memory? no more overlays?) |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | // enable dithering... |
| 580 | overlay_dev->setParameter(overlay_dev, overlay, |
| 581 | OVERLAY_DITHER, OVERLAY_ENABLE); |
| 582 | |
| 583 | mOverlay = overlay; |
| 584 | mWidth = overlay->w; |
| 585 | mHeight = overlay->h; |
| 586 | mFormat = overlay->format; |
| 587 | mWidthStride = overlay->w_stride; |
| 588 | mHeightStride = overlay->h_stride; |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 589 | mInitialized = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
| 591 | mOverlayHandle = overlay->getHandleRef(overlay); |
| 592 | |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 593 | sp<OverlayChannel> channel = new OverlayChannel( &layer ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | |
| 595 | *overlayRef = new OverlayRef(mOverlayHandle, channel, |
| 596 | mWidth, mHeight, mFormat, mWidthStride, mHeightStride); |
Benny Wong | 5643261 | 2009-09-16 14:48:52 -0500 | [diff] [blame] | 597 | mLayer.mFlinger->signalEvent(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | LayerBuffer::OverlaySource::~OverlaySource() |
| 601 | { |
| 602 | if (mOverlay && mOverlayDevice) { |
| 603 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 604 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 605 | } |
| 606 | } |
| 607 | |
Rebecca Schultz Zavin | 96df49b | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 608 | void LayerBuffer::OverlaySource::onDraw(const Region& clip) const |
| 609 | { |
Mathias Agopian | b8cfc0f | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 610 | // this would be where the color-key would be set, should we need it. |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 611 | GLclampx red = 0; |
| 612 | GLclampx green = 0; |
Mathias Agopian | b8cfc0f | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 613 | GLclampx blue = 0; |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 614 | mLayer.clearWithOpenGL(clip, red, green, blue, 0); |
Rebecca Schultz Zavin | 96df49b | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 615 | } |
| 616 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | void LayerBuffer::OverlaySource::onTransaction(uint32_t flags) |
| 618 | { |
| 619 | const Layer::State& front(mLayer.drawingState()); |
| 620 | const Layer::State& temp(mLayer.currentState()); |
| 621 | if (temp.sequence != front.sequence) { |
| 622 | mVisibilityChanged = true; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | void LayerBuffer::OverlaySource::onVisibilityResolved( |
| 627 | const Transform& planeTransform) |
| 628 | { |
| 629 | // this code-path must be as tight as possible, it's called each time |
| 630 | // the screen is composited. |
| 631 | if (UNLIKELY(mOverlay != 0)) { |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 632 | if (mVisibilityChanged || !mInitialized) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | mVisibilityChanged = false; |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 634 | mInitialized = true; |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 635 | const Rect bounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | int x = bounds.left; |
| 637 | int y = bounds.top; |
| 638 | int w = bounds.width(); |
| 639 | int h = bounds.height(); |
| 640 | |
| 641 | // we need a lock here to protect "destroy" |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 642 | Mutex::Autolock _l(mOverlaySourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | if (mOverlay) { |
| 644 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 645 | overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h); |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 646 | overlay_dev->setParameter(overlay_dev, mOverlay, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | OVERLAY_TRANSFORM, mLayer.getOrientation()); |
Rebecca Schultz Zavin | 96df49b | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 648 | overlay_dev->commit(overlay_dev, mOverlay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 654 | void LayerBuffer::OverlaySource::destroy() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | { |
| 656 | // we need a lock here to protect "onVisibilityResolved" |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 657 | Mutex::Autolock _l(mOverlaySourceLock); |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 658 | if (mOverlay && mOverlayDevice) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 660 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 661 | mOverlay = 0; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // --------------------------------------------------------------------------- |
| 666 | }; // namespace android |