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(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | LayerBuffer::BufferSource::~BufferSource() |
| 345 | { |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 346 | if (mTexture.name != -1U) { |
| 347 | glDeleteTextures(1, &mTexture.name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 349 | if (mTexture.image != EGL_NO_IMAGE_KHR) { |
| 350 | EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay()); |
| 351 | eglDestroyImageKHR(dpy, mTexture.image); |
Mathias Agopian | 240c9fe | 2009-06-25 15:39:25 -0700 | [diff] [blame] | 352 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void LayerBuffer::BufferSource::postBuffer(ssize_t offset) |
| 356 | { |
| 357 | ISurface::BufferHeap buffers; |
| 358 | { // scope for the lock |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 359 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | buffers = mBufferHeap; |
| 361 | if (buffers.heap != 0) { |
| 362 | const size_t memorySize = buffers.heap->getSize(); |
| 363 | if ((size_t(offset) + mBufferSize) > memorySize) { |
| 364 | LOGE("LayerBuffer::BufferSource::postBuffer() " |
| 365 | "invalid buffer (offset=%d, size=%d, heap-size=%d", |
| 366 | int(offset), int(mBufferSize), int(memorySize)); |
| 367 | return; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | sp<Buffer> buffer; |
| 373 | if (buffers.heap != 0) { |
| 374 | buffer = new LayerBuffer::Buffer(buffers, offset); |
| 375 | if (buffer->getStatus() != NO_ERROR) |
| 376 | buffer.clear(); |
| 377 | setBuffer(buffer); |
| 378 | mLayer.invalidate(); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void LayerBuffer::BufferSource::unregisterBuffers() |
| 383 | { |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 384 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | mBufferHeap.heap.clear(); |
| 386 | mBuffer.clear(); |
| 387 | mLayer.invalidate(); |
| 388 | } |
| 389 | |
| 390 | sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const |
| 391 | { |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 392 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | return mBuffer; |
| 394 | } |
| 395 | |
| 396 | void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer) |
| 397 | { |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 398 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | mBuffer = buffer; |
| 400 | } |
| 401 | |
| 402 | bool LayerBuffer::BufferSource::transformed() const |
| 403 | { |
| 404 | return mBufferHeap.transform ? true : Source::transformed(); |
| 405 | } |
| 406 | |
| 407 | void LayerBuffer::BufferSource::onDraw(const Region& clip) const |
| 408 | { |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 409 | sp<Buffer> ourBuffer(getBuffer()); |
| 410 | if (UNLIKELY(ourBuffer == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | // nothing to do, we don't have a buffer |
| 412 | mLayer.clearWithOpenGL(clip); |
| 413 | return; |
| 414 | } |
| 415 | |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 416 | status_t err = NO_ERROR; |
| 417 | NativeBuffer src(ourBuffer->getBuffer()); |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 418 | const Rect transformedBounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 420 | if (UNLIKELY(mTexture.name == -1LU)) { |
| 421 | mTexture.name = mLayer.createTexture(); |
| 422 | } |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 423 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 424 | #if defined(EGL_ANDROID_image_native_buffer) |
| 425 | if (mLayer.mFlags & DisplayHardware::DIRECT_TEXTURE) { |
| 426 | // NOTE: Assume the buffer is allocated with the proper USAGE flags |
| 427 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer( |
| 428 | src.crop.r, src.crop.b, src.img.format, |
| 429 | GraphicBuffer::USAGE_HW_TEXTURE, |
| 430 | src.img.w, src.img.handle, false); |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 431 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 432 | err = mLayer.initializeEglImage(graphicBuffer, &mTexture); |
Mathias Agopian | 69029eb | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 433 | } |
Mathias Agopian | 2ab55a4 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 434 | #endif |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 435 | else { |
| 436 | err = INVALID_OPERATION; |
| 437 | } |
| 438 | |
| 439 | if (err != NO_ERROR) { |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 440 | // OpenGL fall-back |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 441 | GLuint w = 0; |
| 442 | GLuint h = 0; |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 443 | GGLSurface t; |
| 444 | t.version = sizeof(GGLSurface); |
| 445 | t.width = src.crop.r; |
| 446 | t.height = src.crop.b; |
| 447 | t.stride = src.img.w; |
| 448 | t.vstride= src.img.h; |
| 449 | t.format = src.img.format; |
Mathias Agopian | 5911aa9 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 450 | t.data = (GGLubyte*)src.img.base; |
Mathias Agopian | 1fed11c | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 451 | const Region dirty(Rect(t.width, t.height)); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 452 | mLayer.loadTexture(&mTexture, dirty, t); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 453 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 455 | mTexture.transform = mBufferHeap.transform; |
| 456 | mLayer.drawWithOpenGL(clip, mTexture); |
| 457 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | |
| 459 | // --------------------------------------------------------------------------- |
| 460 | |
| 461 | LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer, |
| 462 | sp<OverlayRef>* overlayRef, |
| 463 | uint32_t w, uint32_t h, int32_t format) |
| 464 | : Source(layer), mVisibilityChanged(false), |
| 465 | mOverlay(0), mOverlayHandle(0), mOverlayDevice(0) |
| 466 | { |
| 467 | overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine(); |
| 468 | if (overlay_dev == NULL) { |
| 469 | // overlays not supported |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | mOverlayDevice = overlay_dev; |
| 474 | overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format); |
| 475 | if (overlay == NULL) { |
| 476 | // couldn't create the overlay (no memory? no more overlays?) |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | // enable dithering... |
| 481 | overlay_dev->setParameter(overlay_dev, overlay, |
| 482 | OVERLAY_DITHER, OVERLAY_ENABLE); |
| 483 | |
| 484 | mOverlay = overlay; |
| 485 | mWidth = overlay->w; |
| 486 | mHeight = overlay->h; |
| 487 | mFormat = overlay->format; |
| 488 | mWidthStride = overlay->w_stride; |
| 489 | mHeightStride = overlay->h_stride; |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 490 | mInitialized = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | |
| 492 | mOverlayHandle = overlay->getHandleRef(overlay); |
| 493 | |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 494 | sp<OverlayChannel> channel = new OverlayChannel( &layer ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | |
| 496 | *overlayRef = new OverlayRef(mOverlayHandle, channel, |
| 497 | mWidth, mHeight, mFormat, mWidthStride, mHeightStride); |
Benny Wong | 5643261 | 2009-09-16 14:48:52 -0500 | [diff] [blame] | 498 | mLayer.mFlinger->signalEvent(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | LayerBuffer::OverlaySource::~OverlaySource() |
| 502 | { |
| 503 | if (mOverlay && mOverlayDevice) { |
| 504 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 505 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 506 | } |
| 507 | } |
| 508 | |
Rebecca Schultz Zavin | 96df49b | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 509 | void LayerBuffer::OverlaySource::onDraw(const Region& clip) const |
| 510 | { |
Mathias Agopian | b8cfc0f | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 511 | // 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] | 512 | GLclampx red = 0; |
| 513 | GLclampx green = 0; |
Mathias Agopian | b8cfc0f | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 514 | GLclampx blue = 0; |
Rebecca Schultz Zavin | 29aa74c | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 515 | mLayer.clearWithOpenGL(clip, red, green, blue, 0); |
Rebecca Schultz Zavin | 96df49b | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 516 | } |
| 517 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | void LayerBuffer::OverlaySource::onTransaction(uint32_t flags) |
| 519 | { |
| 520 | const Layer::State& front(mLayer.drawingState()); |
| 521 | const Layer::State& temp(mLayer.currentState()); |
| 522 | if (temp.sequence != front.sequence) { |
| 523 | mVisibilityChanged = true; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void LayerBuffer::OverlaySource::onVisibilityResolved( |
| 528 | const Transform& planeTransform) |
| 529 | { |
| 530 | // this code-path must be as tight as possible, it's called each time |
| 531 | // the screen is composited. |
| 532 | if (UNLIKELY(mOverlay != 0)) { |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 533 | if (mVisibilityChanged || !mInitialized) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | mVisibilityChanged = false; |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 535 | mInitialized = true; |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 536 | const Rect bounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | int x = bounds.left; |
| 538 | int y = bounds.top; |
| 539 | int w = bounds.width(); |
| 540 | int h = bounds.height(); |
| 541 | |
| 542 | // we need a lock here to protect "destroy" |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 543 | Mutex::Autolock _l(mOverlaySourceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | if (mOverlay) { |
| 545 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 546 | overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h); |
Rebecca Schultz Zavin | 1000170 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 547 | overlay_dev->setParameter(overlay_dev, mOverlay, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | OVERLAY_TRANSFORM, mLayer.getOrientation()); |
Rebecca Schultz Zavin | 96df49b | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 549 | overlay_dev->commit(overlay_dev, mOverlay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 555 | void LayerBuffer::OverlaySource::destroy() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | { |
| 557 | // we need a lock here to protect "onVisibilityResolved" |
Mathias Agopian | 4d2de2c | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 558 | Mutex::Autolock _l(mOverlaySourceLock); |
Mathias Agopian | 454ea68 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 559 | if (mOverlay && mOverlayDevice) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 561 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 562 | mOverlay = 0; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // --------------------------------------------------------------------------- |
| 567 | }; // namespace android |