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