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