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); |
Mathias Agopian | 4c29c94 | 2009-11-19 14:46:26 -0800 | [diff] [blame] | 121 | uint32_t res = LayerBase::doTransaction(flags); |
| 122 | // we always want filtering for these surfaces |
Mathias Agopian | b548dbb | 2009-12-08 19:29:38 -0800 | [diff] [blame] | 123 | mUseLinearFiltering = !(mFlags & DisplayHardware::SLOW_CONFIG); |
Mathias Agopian | 4c29c94 | 2009-11-19 14:46:26 -0800 | [diff] [blame] | 124 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void LayerBuffer::unlockPageFlip(const Transform& planeTransform, |
| 128 | Region& outDirtyRegion) |
| 129 | { |
| 130 | // this code-path must be as tight as possible, it's called each time |
| 131 | // the screen is composited. |
| 132 | sp<Source> source(getSource()); |
| 133 | if (source != 0) |
| 134 | source->onVisibilityResolved(planeTransform); |
| 135 | LayerBase::unlockPageFlip(planeTransform, outDirtyRegion); |
| 136 | } |
| 137 | |
| 138 | void LayerBuffer::onDraw(const Region& clip) const |
| 139 | { |
| 140 | sp<Source> source(getSource()); |
| 141 | if (LIKELY(source != 0)) { |
| 142 | source->onDraw(clip); |
| 143 | } else { |
| 144 | clearWithOpenGL(clip); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | bool LayerBuffer::transformed() const |
| 149 | { |
| 150 | sp<Source> source(getSource()); |
| 151 | if (LIKELY(source != 0)) |
| 152 | return source->transformed(); |
| 153 | return false; |
| 154 | } |
| 155 | |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 156 | void LayerBuffer::serverDestroy() |
| 157 | { |
| 158 | sp<Source> source(clearSource()); |
| 159 | if (source != 0) { |
| 160 | source->destroy(); |
| 161 | } |
| 162 | } |
| 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | /** |
| 165 | * This creates a "buffer" source for this surface |
| 166 | */ |
| 167 | status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers) |
| 168 | { |
| 169 | Mutex::Autolock _l(mLock); |
| 170 | if (mSource != 0) |
| 171 | return INVALID_OPERATION; |
| 172 | |
| 173 | sp<BufferSource> source = new BufferSource(*this, buffers); |
| 174 | |
| 175 | status_t result = source->getStatus(); |
| 176 | if (result == NO_ERROR) { |
| 177 | mSource = source; |
| 178 | } |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * This creates an "overlay" source for this surface |
| 184 | */ |
| 185 | sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f) |
| 186 | { |
| 187 | sp<OverlayRef> result; |
| 188 | Mutex::Autolock _l(mLock); |
| 189 | if (mSource != 0) |
| 190 | return result; |
| 191 | |
| 192 | sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f); |
| 193 | if (result != 0) { |
| 194 | mSource = source; |
| 195 | } |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | sp<LayerBuffer::Source> LayerBuffer::getSource() const { |
| 200 | Mutex::Autolock _l(mLock); |
| 201 | return mSource; |
| 202 | } |
| 203 | |
| 204 | sp<LayerBuffer::Source> LayerBuffer::clearSource() { |
| 205 | sp<Source> source; |
| 206 | Mutex::Autolock _l(mLock); |
| 207 | source = mSource; |
| 208 | mSource.clear(); |
| 209 | return source; |
| 210 | } |
| 211 | |
| 212 | // ============================================================================ |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 213 | // LayerBuffer::SurfaceLayerBuffer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | // ============================================================================ |
| 215 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 216 | LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger, |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 217 | SurfaceID id, const sp<LayerBuffer>& owner) |
| 218 | : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | { |
| 220 | } |
| 221 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 222 | LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | { |
| 224 | unregisterBuffers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 227 | status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers( |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 228 | const ISurface::BufferHeap& buffers) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 230 | sp<LayerBuffer> owner(getOwner()); |
| 231 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | return owner->registerBuffers(buffers); |
| 233 | return NO_INIT; |
| 234 | } |
| 235 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 236 | void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 238 | sp<LayerBuffer> owner(getOwner()); |
| 239 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | owner->postBuffer(offset); |
| 241 | } |
| 242 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 243 | void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 245 | sp<LayerBuffer> owner(getOwner()); |
| 246 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | owner->unregisterBuffers(); |
| 248 | } |
| 249 | |
Mathias Agopian | 9779b22 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 250 | sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | uint32_t w, uint32_t h, int32_t format) { |
| 252 | sp<OverlayRef> result; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 253 | sp<LayerBuffer> owner(getOwner()); |
| 254 | if (owner != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | result = owner->createOverlay(w, h, format); |
| 256 | return result; |
| 257 | } |
| 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | // ============================================================================ |
| 260 | // LayerBuffer::Buffer |
| 261 | // ============================================================================ |
| 262 | |
| 263 | LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset) |
Mathias Agopian | 7323e54 | 2010-01-20 13:24:14 -0800 | [diff] [blame] | 264 | : mBufferHeap(buffers), mSupportsCopybit(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | { |
| 266 | NativeBuffer& src(mNativeBuffer); |
Mathias Agopian | d86beb9 | 2009-12-02 16:23:11 -0800 | [diff] [blame] | 267 | src.crop.l = 0; |
| 268 | src.crop.t = 0; |
| 269 | src.crop.r = buffers.w; |
| 270 | src.crop.b = buffers.h; |
| 271 | |
| 272 | src.img.w = buffers.hor_stride ?: buffers.w; |
| 273 | src.img.h = buffers.ver_stride ?: buffers.h; |
| 274 | src.img.format = buffers.format; |
| 275 | src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset); |
| 276 | src.img.handle = 0; |
Mathias Agopian | 2eab9d8 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 277 | |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame] | 278 | gralloc_module_t const * module = LayerBuffer::getGrallocModule(); |
| 279 | if (module && module->perform) { |
| 280 | int err = module->perform(module, |
| 281 | GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER, |
| 282 | buffers.heap->heapID(), buffers.heap->getSize(), |
| 283 | offset, buffers.heap->base(), |
| 284 | &src.img.handle); |
| 285 | |
Mathias Agopian | 7323e54 | 2010-01-20 13:24:14 -0800 | [diff] [blame] | 286 | // we can fail here is the passed buffer is purely software |
| 287 | mSupportsCopybit = (err == NO_ERROR); |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame] | 288 | } |
Mathias Agopian | d86beb9 | 2009-12-02 16:23:11 -0800 | [diff] [blame] | 289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
| 291 | LayerBuffer::Buffer::~Buffer() |
| 292 | { |
Mathias Agopian | 2eab9d8 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 293 | NativeBuffer& src(mNativeBuffer); |
Mathias Agopian | 0b0722f | 2009-10-29 18:29:30 -0700 | [diff] [blame] | 294 | if (src.img.handle) { |
| 295 | native_handle_delete(src.img.handle); |
| 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // ============================================================================ |
| 300 | // LayerBuffer::Source |
| 301 | // LayerBuffer::BufferSource |
| 302 | // LayerBuffer::OverlaySource |
| 303 | // ============================================================================ |
| 304 | |
| 305 | LayerBuffer::Source::Source(LayerBuffer& layer) |
| 306 | : mLayer(layer) |
| 307 | { |
| 308 | } |
| 309 | LayerBuffer::Source::~Source() { |
| 310 | } |
| 311 | void LayerBuffer::Source::onDraw(const Region& clip) const { |
| 312 | } |
| 313 | void LayerBuffer::Source::onTransaction(uint32_t flags) { |
| 314 | } |
| 315 | void LayerBuffer::Source::onVisibilityResolved( |
| 316 | const Transform& planeTransform) { |
| 317 | } |
| 318 | void LayerBuffer::Source::postBuffer(ssize_t offset) { |
| 319 | } |
| 320 | void LayerBuffer::Source::unregisterBuffers() { |
| 321 | } |
| 322 | bool LayerBuffer::Source::transformed() const { |
| 323 | return mLayer.mTransformed; |
| 324 | } |
| 325 | |
| 326 | // --------------------------------------------------------------------------- |
| 327 | |
| 328 | LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer, |
| 329 | const ISurface::BufferHeap& buffers) |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 330 | : Source(layer), mStatus(NO_ERROR), mBufferSize(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | { |
| 332 | if (buffers.heap == NULL) { |
| 333 | // this is allowed, but in this case, it is illegal to receive |
| 334 | // postBuffer(). The surface just erases the framebuffer with |
| 335 | // fully transparent pixels. |
| 336 | mBufferHeap = buffers; |
| 337 | mLayer.setNeedsBlending(false); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT; |
| 342 | if (err != NO_ERROR) { |
| 343 | LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err)); |
| 344 | mStatus = err; |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | PixelFormatInfo info; |
| 349 | err = getPixelFormatInfo(buffers.format, &info); |
| 350 | if (err != NO_ERROR) { |
| 351 | LOGE("LayerBuffer::BufferSource: invalid format %d (%s)", |
| 352 | buffers.format, strerror(err)); |
| 353 | mStatus = err; |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if (buffers.hor_stride<0 || buffers.ver_stride<0) { |
| 358 | LOGE("LayerBuffer::BufferSource: invalid parameters " |
| 359 | "(w=%d, h=%d, xs=%d, ys=%d)", |
| 360 | buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride); |
| 361 | mStatus = BAD_VALUE; |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | mBufferHeap = buffers; |
| 366 | mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0); |
| 367 | mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride; |
| 368 | mLayer.forceVisibilityTransaction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | LayerBuffer::BufferSource::~BufferSource() |
| 372 | { |
Mathias Agopian | 2d41cb9 | 2010-01-21 16:04:56 -0800 | [diff] [blame^] | 373 | class MessageDestroyTexture : public MessageBase { |
| 374 | SurfaceFlinger* flinger; |
| 375 | GLuint name; |
| 376 | public: |
| 377 | MessageDestroyTexture( |
| 378 | SurfaceFlinger* flinger, GLuint name) |
| 379 | : flinger(flinger), name(name) { } |
| 380 | virtual bool handler() { |
| 381 | glDeleteTextures(1, &name); |
| 382 | return true; |
| 383 | } |
| 384 | }; |
| 385 | |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 386 | if (mTexture.name != -1U) { |
Mathias Agopian | 2d41cb9 | 2010-01-21 16:04:56 -0800 | [diff] [blame^] | 387 | // GL textures can only be destroyed from the GL thread |
| 388 | mLayer.mFlinger->mEventQueue.postMessage( |
| 389 | new MessageDestroyTexture(mLayer.mFlinger.get(), mTexture.name) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 391 | if (mTexture.image != EGL_NO_IMAGE_KHR) { |
| 392 | EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay()); |
| 393 | eglDestroyImageKHR(dpy, mTexture.image); |
Mathias Agopian | 68eeb80 | 2009-06-25 15:39:25 -0700 | [diff] [blame] | 394 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void LayerBuffer::BufferSource::postBuffer(ssize_t offset) |
| 398 | { |
| 399 | ISurface::BufferHeap buffers; |
| 400 | { // scope for the lock |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 401 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | buffers = mBufferHeap; |
| 403 | if (buffers.heap != 0) { |
| 404 | const size_t memorySize = buffers.heap->getSize(); |
| 405 | if ((size_t(offset) + mBufferSize) > memorySize) { |
| 406 | LOGE("LayerBuffer::BufferSource::postBuffer() " |
| 407 | "invalid buffer (offset=%d, size=%d, heap-size=%d", |
| 408 | int(offset), int(mBufferSize), int(memorySize)); |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | sp<Buffer> buffer; |
| 415 | if (buffers.heap != 0) { |
| 416 | buffer = new LayerBuffer::Buffer(buffers, offset); |
| 417 | if (buffer->getStatus() != NO_ERROR) |
| 418 | buffer.clear(); |
| 419 | setBuffer(buffer); |
| 420 | mLayer.invalidate(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void LayerBuffer::BufferSource::unregisterBuffers() |
| 425 | { |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 426 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | mBufferHeap.heap.clear(); |
| 428 | mBuffer.clear(); |
| 429 | mLayer.invalidate(); |
| 430 | } |
| 431 | |
| 432 | sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const |
| 433 | { |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 434 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | return mBuffer; |
| 436 | } |
| 437 | |
| 438 | void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer) |
| 439 | { |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 440 | Mutex::Autolock _l(mBufferSourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | mBuffer = buffer; |
| 442 | } |
| 443 | |
| 444 | bool LayerBuffer::BufferSource::transformed() const |
| 445 | { |
| 446 | return mBufferHeap.transform ? true : Source::transformed(); |
| 447 | } |
| 448 | |
| 449 | void LayerBuffer::BufferSource::onDraw(const Region& clip) const |
| 450 | { |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 451 | sp<Buffer> ourBuffer(getBuffer()); |
| 452 | if (UNLIKELY(ourBuffer == 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | // nothing to do, we don't have a buffer |
| 454 | mLayer.clearWithOpenGL(clip); |
| 455 | return; |
| 456 | } |
| 457 | |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 458 | status_t err = NO_ERROR; |
| 459 | NativeBuffer src(ourBuffer->getBuffer()); |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 460 | const Rect transformedBounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | |
Mathias Agopian | a1a1798 | 2010-01-21 16:27:30 -0800 | [diff] [blame] | 462 | if (UNLIKELY(mTexture.name == -1LU)) { |
| 463 | mTexture.name = mLayer.createTexture(); |
| 464 | } |
| 465 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 466 | #if defined(EGL_ANDROID_image_native_buffer) |
| 467 | if (mLayer.mFlags & DisplayHardware::DIRECT_TEXTURE) { |
Mathias Agopian | 90daccf | 2009-11-05 23:08:00 -0800 | [diff] [blame] | 468 | copybit_device_t* copybit = mLayer.mBlitEngine; |
Mathias Agopian | 7323e54 | 2010-01-20 13:24:14 -0800 | [diff] [blame] | 469 | if (copybit && ourBuffer->supportsCopybit()) { |
Mathias Agopian | 90daccf | 2009-11-05 23:08:00 -0800 | [diff] [blame] | 470 | // create our EGLImageKHR the first time |
Mathias Agopian | 76169da | 2009-12-09 14:32:56 -0800 | [diff] [blame] | 471 | err = initTempBuffer(); |
Mathias Agopian | 90daccf | 2009-11-05 23:08:00 -0800 | [diff] [blame] | 472 | if (err == NO_ERROR) { |
| 473 | // NOTE: Assume the buffer is allocated with the proper USAGE flags |
| 474 | const NativeBuffer& dst(mTempBuffer); |
| 475 | region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b))); |
| 476 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0); |
| 477 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF); |
| 478 | copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE); |
| 479 | err = copybit->stretch(copybit, &dst.img, &src.img, |
| 480 | &dst.crop, &src.crop, &clip); |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 481 | if (err != NO_ERROR) { |
| 482 | clearTempBufferImage(); |
| 483 | } |
Mathias Agopian | 90daccf | 2009-11-05 23:08:00 -0800 | [diff] [blame] | 484 | } |
| 485 | } else { |
| 486 | err = INVALID_OPERATION; |
| 487 | } |
Mathias Agopian | cbc4c9f | 2009-06-23 21:11:43 -0700 | [diff] [blame] | 488 | } |
Mathias Agopian | 26c28b1 | 2009-06-24 22:39:26 -0700 | [diff] [blame] | 489 | #endif |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 490 | else { |
| 491 | err = INVALID_OPERATION; |
| 492 | } |
| 493 | |
| 494 | if (err != NO_ERROR) { |
Mathias Agopian | f007a2f | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 495 | // slower fallback |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 496 | GGLSurface t; |
| 497 | t.version = sizeof(GGLSurface); |
| 498 | t.width = src.crop.r; |
| 499 | t.height = src.crop.b; |
| 500 | t.stride = src.img.w; |
| 501 | t.vstride= src.img.h; |
| 502 | t.format = src.img.format; |
Mathias Agopian | 2eab9d8 | 2009-06-24 16:55:59 -0700 | [diff] [blame] | 503 | t.data = (GGLubyte*)src.img.base; |
Mathias Agopian | 999543b | 2009-06-23 18:08:22 -0700 | [diff] [blame] | 504 | const Region dirty(Rect(t.width, t.height)); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 505 | mLayer.loadTexture(&mTexture, dirty, t); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 506 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 508 | mTexture.transform = mBufferHeap.transform; |
| 509 | mLayer.drawWithOpenGL(clip, mTexture); |
| 510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
Mathias Agopian | 76169da | 2009-12-09 14:32:56 -0800 | [diff] [blame] | 512 | status_t LayerBuffer::BufferSource::initTempBuffer() const |
| 513 | { |
| 514 | // figure out the size we need now |
| 515 | const ISurface::BufferHeap& buffers(mBufferHeap); |
| 516 | uint32_t w = mLayer.mTransformedBounds.width(); |
| 517 | uint32_t h = mLayer.mTransformedBounds.height(); |
| 518 | if (buffers.w * h != buffers.h * w) { |
| 519 | int t = w; w = h; h = t; |
| 520 | } |
| 521 | |
Mathias Agopian | d23fa27 | 2010-01-20 14:31:53 -0800 | [diff] [blame] | 522 | // we're in the copybit case, so make sure we can handle this blit |
| 523 | // we don't have to keep the aspect ratio here |
| 524 | copybit_device_t* copybit = mLayer.mBlitEngine; |
| 525 | const int down = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT); |
| 526 | const int up = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT); |
| 527 | if (buffers.w > w*down) w = buffers.w / down; |
| 528 | else if (w > buffers.w*up) w = buffers.w*up; |
| 529 | if (buffers.h > h*down) h = buffers.h / down; |
| 530 | else if (h > buffers.h*up) h = buffers.h*up; |
| 531 | |
Mathias Agopian | 76169da | 2009-12-09 14:32:56 -0800 | [diff] [blame] | 532 | if (mTexture.image != EGL_NO_IMAGE_KHR) { |
| 533 | // we have an EGLImage, make sure the needed size didn't change |
| 534 | if (w!=mTexture.width || h!= mTexture.height) { |
| 535 | // delete the EGLImage and texture |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 536 | clearTempBufferImage(); |
Mathias Agopian | 76169da | 2009-12-09 14:32:56 -0800 | [diff] [blame] | 537 | } else { |
| 538 | // we're good, we have an EGLImageKHR and it's (still) the |
| 539 | // right size |
| 540 | return NO_ERROR; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // figure out if we need linear filtering |
| 545 | if (buffers.w * h == buffers.h * w) { |
| 546 | // same pixel area, don't use filtering |
| 547 | mLayer.mUseLinearFiltering = false; |
| 548 | } |
| 549 | |
| 550 | // Allocate a temporary buffer and create the corresponding EGLImageKHR |
| 551 | |
| 552 | status_t err; |
| 553 | mTempGraphicBuffer.clear(); |
| 554 | mTempGraphicBuffer = new GraphicBuffer( |
| 555 | w, h, HAL_PIXEL_FORMAT_RGB_565, |
| 556 | GraphicBuffer::USAGE_HW_TEXTURE | |
| 557 | GraphicBuffer::USAGE_HW_2D); |
| 558 | |
| 559 | err = mTempGraphicBuffer->initCheck(); |
| 560 | if (err == NO_ERROR) { |
| 561 | NativeBuffer& dst(mTempBuffer); |
| 562 | dst.img.w = mTempGraphicBuffer->getStride(); |
| 563 | dst.img.h = h; |
| 564 | dst.img.format = mTempGraphicBuffer->getPixelFormat(); |
| 565 | dst.img.handle = (native_handle_t *)mTempGraphicBuffer->handle; |
| 566 | dst.img.base = 0; |
| 567 | dst.crop.l = 0; |
| 568 | dst.crop.t = 0; |
| 569 | dst.crop.r = w; |
| 570 | dst.crop.b = h; |
| 571 | |
| 572 | err = mLayer.initializeEglImage( |
| 573 | mTempGraphicBuffer, &mTexture); |
| 574 | // once the EGLImage has been created (whether it fails |
| 575 | // or not) we don't need the graphic buffer reference |
| 576 | // anymore. |
| 577 | mTempGraphicBuffer.clear(); |
| 578 | } |
| 579 | |
| 580 | return err; |
| 581 | } |
| 582 | |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 583 | void LayerBuffer::BufferSource::clearTempBufferImage() const |
| 584 | { |
Mathias Agopian | a1a1798 | 2010-01-21 16:27:30 -0800 | [diff] [blame] | 585 | // delete the image |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 586 | EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay()); |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 587 | eglDestroyImageKHR(dpy, mTexture.image); |
Mathias Agopian | a1a1798 | 2010-01-21 16:27:30 -0800 | [diff] [blame] | 588 | |
| 589 | // and the associated texture (recreate a name) |
| 590 | glDeleteTextures(1, &mTexture.name); |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 591 | Texture defaultTexture; |
| 592 | mTexture = defaultTexture; |
Mathias Agopian | a1a1798 | 2010-01-21 16:27:30 -0800 | [diff] [blame] | 593 | mTexture.name = mLayer.createTexture(); |
| 594 | |
| 595 | // and the associated buffer |
Mathias Agopian | d2832fb4 | 2010-01-20 13:56:07 -0800 | [diff] [blame] | 596 | mTempGraphicBuffer.clear(); |
| 597 | } |
| 598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | // --------------------------------------------------------------------------- |
| 600 | |
| 601 | LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer, |
| 602 | sp<OverlayRef>* overlayRef, |
| 603 | uint32_t w, uint32_t h, int32_t format) |
| 604 | : Source(layer), mVisibilityChanged(false), |
| 605 | mOverlay(0), mOverlayHandle(0), mOverlayDevice(0) |
| 606 | { |
| 607 | overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine(); |
| 608 | if (overlay_dev == NULL) { |
| 609 | // overlays not supported |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | mOverlayDevice = overlay_dev; |
| 614 | overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format); |
| 615 | if (overlay == NULL) { |
| 616 | // couldn't create the overlay (no memory? no more overlays?) |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | // enable dithering... |
| 621 | overlay_dev->setParameter(overlay_dev, overlay, |
| 622 | OVERLAY_DITHER, OVERLAY_ENABLE); |
| 623 | |
| 624 | mOverlay = overlay; |
| 625 | mWidth = overlay->w; |
| 626 | mHeight = overlay->h; |
| 627 | mFormat = overlay->format; |
| 628 | mWidthStride = overlay->w_stride; |
| 629 | mHeightStride = overlay->h_stride; |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 630 | mInitialized = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | |
| 632 | mOverlayHandle = overlay->getHandleRef(overlay); |
| 633 | |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 634 | sp<OverlayChannel> channel = new OverlayChannel( &layer ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | |
| 636 | *overlayRef = new OverlayRef(mOverlayHandle, channel, |
| 637 | mWidth, mHeight, mFormat, mWidthStride, mHeightStride); |
Benny Wong | e38212a | 2009-09-16 14:48:52 -0500 | [diff] [blame] | 638 | mLayer.mFlinger->signalEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | LayerBuffer::OverlaySource::~OverlaySource() |
| 642 | { |
| 643 | if (mOverlay && mOverlayDevice) { |
| 644 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 645 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 646 | } |
| 647 | } |
| 648 | |
Rebecca Schultz Zavin | 7ac5e69 | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 649 | void LayerBuffer::OverlaySource::onDraw(const Region& clip) const |
| 650 | { |
Mathias Agopian | 8ae0384 | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 651 | // 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] | 652 | GLclampx red = 0; |
| 653 | GLclampx green = 0; |
Mathias Agopian | 8ae0384 | 2009-09-15 19:31:28 -0700 | [diff] [blame] | 654 | GLclampx blue = 0; |
Rebecca Schultz Zavin | c854678 | 2009-09-01 23:06:45 -0700 | [diff] [blame] | 655 | mLayer.clearWithOpenGL(clip, red, green, blue, 0); |
Rebecca Schultz Zavin | 7ac5e69 | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 656 | } |
| 657 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | void LayerBuffer::OverlaySource::onTransaction(uint32_t flags) |
| 659 | { |
| 660 | const Layer::State& front(mLayer.drawingState()); |
| 661 | const Layer::State& temp(mLayer.currentState()); |
| 662 | if (temp.sequence != front.sequence) { |
| 663 | mVisibilityChanged = true; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void LayerBuffer::OverlaySource::onVisibilityResolved( |
| 668 | const Transform& planeTransform) |
| 669 | { |
| 670 | // this code-path must be as tight as possible, it's called each time |
| 671 | // the screen is composited. |
| 672 | if (UNLIKELY(mOverlay != 0)) { |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 673 | if (mVisibilityChanged || !mInitialized) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | mVisibilityChanged = false; |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 675 | mInitialized = true; |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 676 | const Rect bounds(mLayer.getTransformedBounds()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | int x = bounds.left; |
| 678 | int y = bounds.top; |
| 679 | int w = bounds.width(); |
| 680 | int h = bounds.height(); |
| 681 | |
| 682 | // we need a lock here to protect "destroy" |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 683 | Mutex::Autolock _l(mOverlaySourceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | if (mOverlay) { |
| 685 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 686 | overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h); |
Rebecca Schultz Zavin | 43ab763 | 2009-07-21 16:17:59 -0700 | [diff] [blame] | 687 | overlay_dev->setParameter(overlay_dev, mOverlay, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | OVERLAY_TRANSFORM, mLayer.getOrientation()); |
Rebecca Schultz Zavin | 7ac5e69 | 2009-07-20 21:18:04 -0700 | [diff] [blame] | 689 | overlay_dev->commit(overlay_dev, mOverlay); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 695 | void LayerBuffer::OverlaySource::destroy() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | { |
| 697 | // we need a lock here to protect "onVisibilityResolved" |
Mathias Agopian | b34d143 | 2009-09-08 20:02:47 -0700 | [diff] [blame] | 698 | Mutex::Autolock _l(mOverlaySourceLock); |
Mathias Agopian | a280496 | 2009-09-08 23:52:08 -0700 | [diff] [blame] | 699 | if (mOverlay && mOverlayDevice) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | overlay_control_device_t* overlay_dev = mOverlayDevice; |
| 701 | overlay_dev->destroyOverlay(overlay_dev, mOverlay); |
| 702 | mOverlay = 0; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | // --------------------------------------------------------------------------- |
| 707 | }; // namespace android |