blob: c9cebf4c9164ae207575315c23632273795ce1ac [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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
17#define LOG_TAG "SurfaceFlinger"
18
19#include <stdlib.h>
20#include <stdint.h>
21#include <math.h>
22#include <sys/types.h>
23
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
28#include <ui/PixelFormat.h>
29#include <ui/EGLDisplaySurface.h>
30
31#include "LayerBuffer.h"
32#include "SurfaceFlinger.h"
33#include "VRamHeap.h"
34#include "DisplayHardware/DisplayHardware.h"
35
36
37namespace android {
38
39// ---------------------------------------------------------------------------
40
41const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20;
42const char* const LayerBuffer::typeID = "LayerBuffer";
43
44// ---------------------------------------------------------------------------
45
46LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display,
47 Client* client, int32_t i)
48 : LayerBaseClient(flinger, display, client, i),
The Android Open Source Project27629322009-01-09 17:51:23 -080049 mNeedsBlending(false)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050{
51}
52
53LayerBuffer::~LayerBuffer()
54{
55 sp<SurfaceBuffer> s(getClientSurface());
56 if (s != 0) {
57 s->disown();
58 mClientSurface.clear();
59 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060}
61
62sp<LayerBuffer::SurfaceBuffer> LayerBuffer::getClientSurface() const
63{
64 Mutex::Autolock _l(mLock);
65 return mClientSurface.promote();
66}
67
68sp<LayerBaseClient::Surface> LayerBuffer::getSurface() const
69{
70 sp<SurfaceBuffer> s;
71 Mutex::Autolock _l(mLock);
72 s = mClientSurface.promote();
73 if (s == 0) {
74 s = new SurfaceBuffer(clientIndex(),
75 const_cast<LayerBuffer *>(this));
76 mClientSurface = s;
77 }
78 return s;
79}
80
The Android Open Source Project27629322009-01-09 17:51:23 -080081bool LayerBuffer::needsBlending() const {
82 return mNeedsBlending;
83}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070084
The Android Open Source Project27629322009-01-09 17:51:23 -080085void LayerBuffer::setNeedsBlending(bool blending) {
86 mNeedsBlending = blending;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070087}
88
89void LayerBuffer::postBuffer(ssize_t offset)
90{
The Android Open Source Project27629322009-01-09 17:51:23 -080091 sp<Source> source(getSource());
92 if (source != 0)
93 source->postBuffer(offset);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070094}
95
96void LayerBuffer::unregisterBuffers()
97{
The Android Open Source Project8a7a6752009-01-15 16:12:10 -080098 sp<Source> source(clearSource());
The Android Open Source Project27629322009-01-09 17:51:23 -080099 if (source != 0)
100 source->unregisterBuffers();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700101}
102
The Android Open Source Project27629322009-01-09 17:51:23 -0800103uint32_t LayerBuffer::doTransaction(uint32_t flags)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800104{
The Android Open Source Project27629322009-01-09 17:51:23 -0800105 sp<Source> source(getSource());
106 if (source != 0)
107 source->onTransaction(flags);
108 return LayerBase::doTransaction(flags);
109}
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800110
The Android Open Source Project27629322009-01-09 17:51:23 -0800111void LayerBuffer::unlockPageFlip(const Transform& planeTransform,
112 Region& outDirtyRegion)
113{
114 // this code-path must be as tight as possible, it's called each time
115 // the screen is composited.
116 sp<Source> source(getSource());
117 if (source != 0)
118 source->onVisibilityResolved(planeTransform);
119 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
120}
121
122void LayerBuffer::onDraw(const Region& clip) const
123{
124 sp<Source> source(getSource());
125 if (LIKELY(source != 0)) {
126 source->onDraw(clip);
127 } else {
128 clearWithOpenGL(clip);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800129 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800130}
131
132/**
133 * This creates a "buffer" source for this surface
134 */
135status_t LayerBuffer::registerBuffers(int w, int h, int hstride, int vstride,
136 PixelFormat format, const sp<IMemoryHeap>& memoryHeap)
137{
138 Mutex::Autolock _l(mLock);
139 if (mSource != 0)
140 return INVALID_OPERATION;
141
142 sp<BufferSource> source = new BufferSource(*this, w, h,
143 hstride, vstride, format, memoryHeap);
144
145 status_t result = source->getStatus();
146 if (result == NO_ERROR) {
147 mSource = source;
148 }
149 return result;
150}
151
152/**
153 * This creates an "overlay" source for this surface
154 */
155sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f)
156{
157 sp<OverlayRef> result;
158 Mutex::Autolock _l(mLock);
159 if (mSource != 0)
160 return result;
161
162 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f);
163 if (result != 0) {
164 mSource = source;
165 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800166 return result;
167}
168
The Android Open Source Project27629322009-01-09 17:51:23 -0800169sp<LayerBuffer::Source> LayerBuffer::getSource() const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170 Mutex::Autolock _l(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800171 return mSource;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172}
173
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800174sp<LayerBuffer::Source> LayerBuffer::clearSource() {
175 sp<Source> source;
176 Mutex::Autolock _l(mLock);
177 source = mSource;
178 mSource.clear();
179 return source;
180}
181
The Android Open Source Project27629322009-01-09 17:51:23 -0800182// ============================================================================
183// LayerBuffer::SurfaceBuffer
184// ============================================================================
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185
186LayerBuffer::SurfaceBuffer::SurfaceBuffer(SurfaceID id, LayerBuffer* owner)
The Android Open Source Project27629322009-01-09 17:51:23 -0800187: LayerBaseClient::Surface(id, owner->getIdentity()), mOwner(owner)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188{
189}
190
191LayerBuffer::SurfaceBuffer::~SurfaceBuffer()
192{
193 unregisterBuffers();
194 mOwner = 0;
195}
196
197status_t LayerBuffer::SurfaceBuffer::registerBuffers(
198 int w, int h, int hs, int vs,
199 PixelFormat format, const sp<IMemoryHeap>& heap)
200{
201 LayerBuffer* owner(getOwner());
202 if (owner)
203 return owner->registerBuffers(w, h, hs, vs, format, heap);
204 return NO_INIT;
205}
206
207void LayerBuffer::SurfaceBuffer::postBuffer(ssize_t offset)
208{
209 LayerBuffer* owner(getOwner());
210 if (owner)
211 owner->postBuffer(offset);
212}
213
214void LayerBuffer::SurfaceBuffer::unregisterBuffers()
215{
216 LayerBuffer* owner(getOwner());
217 if (owner)
218 owner->unregisterBuffers();
219}
220
The Android Open Source Project27629322009-01-09 17:51:23 -0800221sp<OverlayRef> LayerBuffer::SurfaceBuffer::createOverlay(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800222 uint32_t w, uint32_t h, int32_t format) {
The Android Open Source Project27629322009-01-09 17:51:23 -0800223 sp<OverlayRef> result;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800224 LayerBuffer* owner(getOwner());
225 if (owner)
226 result = owner->createOverlay(w, h, format);
227 return result;
228}
229
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230void LayerBuffer::SurfaceBuffer::disown()
231{
232 Mutex::Autolock _l(mLock);
233 mOwner = 0;
234}
235
The Android Open Source Project27629322009-01-09 17:51:23 -0800236// ============================================================================
237// LayerBuffer::Buffer
238// ============================================================================
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239
240LayerBuffer::Buffer::Buffer(const sp<IMemoryHeap>& heap, ssize_t offset,
241 int w, int h, int hs, int vs, int f)
The Android Open Source Project27629322009-01-09 17:51:23 -0800242: mHeap(heap)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243{
244 NativeBuffer& src(mNativeBuffer);
245 src.crop.l = 0;
246 src.crop.t = 0;
247 src.crop.r = w;
248 src.crop.b = h;
249 src.img.w = hs ?: w;
250 src.img.h = vs ?: h;
251 src.img.format = f;
252 src.img.offset = offset;
253 src.img.base = heap->base();
254 src.img.fd = heap->heapID();
255 // FIXME: make sure this buffer lies within the heap, in which case, set
256 // mHeap to null
257}
258
259LayerBuffer::Buffer::~Buffer()
260{
261}
262
The Android Open Source Project27629322009-01-09 17:51:23 -0800263// ============================================================================
264// LayerBuffer::Source
265// LayerBuffer::BufferSource
266// LayerBuffer::OverlaySource
267// ============================================================================
268
269LayerBuffer::Source::Source(LayerBuffer& layer)
270 : mLayer(layer)
271{
272}
273LayerBuffer::Source::~Source() {
274}
275void LayerBuffer::Source::onDraw(const Region& clip) const {
276}
277void LayerBuffer::Source::onTransaction(uint32_t flags) {
278}
279void LayerBuffer::Source::onVisibilityResolved(
280 const Transform& planeTransform) {
281}
282void LayerBuffer::Source::postBuffer(ssize_t offset) {
283}
284void LayerBuffer::Source::unregisterBuffers() {
285}
286
287// ---------------------------------------------------------------------------
288
289LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
290 int w, int h, int hstride, int vstride,
291 PixelFormat format, const sp<IMemoryHeap>& memoryHeap)
292 : Source(layer), mStatus(NO_ERROR), mTextureName(-1U)
293{
294 if (memoryHeap == NULL) {
295 // this is allowed, but in this case, it is illegal to receive
296 // postBuffer(). The surface just erases the framebuffer with
297 // fully transparent pixels.
298 mHeap.clear();
299 mWidth = w;
300 mHeight = h;
301 mLayer.setNeedsBlending(false);
302 return;
303 }
304
305 status_t err = (memoryHeap->heapID() >= 0) ? NO_ERROR : NO_INIT;
306 if (err != NO_ERROR) {
307 mStatus = err;
308 return;
309 }
310
311 // TODO: validate format/parameters
312 mHeap = memoryHeap;
313 mWidth = w;
314 mHeight = h;
315 mHStride = hstride;
316 mVStride = vstride;
317 mFormat = format;
318 PixelFormatInfo info;
319 getPixelFormatInfo(format, &info);
320 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
321}
322
323LayerBuffer::BufferSource::~BufferSource()
324{
325 if (mTextureName != -1U) {
326 LayerBase::deletedTextures.add(mTextureName);
327 }
328}
329
330void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
331{
332 sp<IMemoryHeap> heap;
333 int w, h, hs, vs, f;
334 { // scope for the lock
335 Mutex::Autolock _l(mLock);
336 w = mWidth;
337 h = mHeight;
338 hs= mHStride;
339 vs= mVStride;
340 f = mFormat;
341 heap = mHeap;
342 }
343
344 sp<Buffer> buffer;
345 if (heap != 0) {
346 buffer = new LayerBuffer::Buffer(heap, offset, w, h, hs, vs, f);
347 if (buffer->getStatus() != NO_ERROR)
348 buffer.clear();
349 setBuffer(buffer);
350 mLayer.invalidate();
351 }
352}
353
354void LayerBuffer::BufferSource::unregisterBuffers()
355{
356 Mutex::Autolock _l(mLock);
357 mHeap.clear();
358 mBuffer.clear();
359 mLayer.invalidate();
360}
361
362sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
363{
364 Mutex::Autolock _l(mLock);
365 return mBuffer;
366}
367
368void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
369{
370 Mutex::Autolock _l(mLock);
371 mBuffer = buffer;
372}
373
374void LayerBuffer::BufferSource::onDraw(const Region& clip) const
375{
376 sp<Buffer> buffer(getBuffer());
377 if (UNLIKELY(buffer == 0)) {
378 // nothing to do, we don't have a buffer
379 mLayer.clearWithOpenGL(clip);
380 return;
381 }
382
383 status_t err = NO_ERROR;
384 NativeBuffer src(buffer->getBuffer());
385 const Rect& transformedBounds = mLayer.getTransformedBounds();
386 const int can_use_copybit = mLayer.canUseCopybit();
387
388 if (can_use_copybit) {
389 const int src_width = src.crop.r - src.crop.l;
390 const int src_height = src.crop.b - src.crop.t;
391 int W = transformedBounds.width();
392 int H = transformedBounds.height();
393 if (mLayer.getOrientation() & Transform::ROT_90) {
394 int t(W); W=H; H=t;
395 }
396
397 /* With LayerBuffer, it is likely that we'll have to rescale the
398 * surface, because this is often used for video playback or
399 * camera-preview. Since we want these operation as fast as possible
400 * we make sure we can use the 2D H/W even if it doesn't support
401 * the requested scale factor, in which case we perform the scaling
402 * in several passes. */
403
404 copybit_device_t* copybit = mLayer.mFlinger->getBlitEngine();
405 const float min = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
406 const float mag = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
407
408 float xscale = 1.0f;
409 if (src_width > W*min) xscale = 1.0f / min;
410 else if (src_width*mag < W) xscale = mag;
411
412 float yscale = 1.0f;
413 if (src_height > H*min) yscale = 1.0f / min;
414 else if (src_height*mag < H) yscale = mag;
415
416 if (UNLIKELY(xscale!=1.0f || yscale!=1.0f)) {
417 if (UNLIKELY(mTemporaryDealer == 0)) {
418 // allocate a memory-dealer for this the first time
419 mTemporaryDealer = mLayer.mFlinger->getSurfaceHeapManager()
420 ->createHeap(ISurfaceComposer::eHardware);
421 mTempBitmap.init(mTemporaryDealer);
422 }
423
424 const int tmp_w = floorf(src_width * xscale);
425 const int tmp_h = floorf(src_height * yscale);
426 err = mTempBitmap.setBits(tmp_w, tmp_h, 1, src.img.format);
427
428 if (LIKELY(err == NO_ERROR)) {
429 NativeBuffer tmp;
430 mTempBitmap.getBitmapSurface(&tmp.img);
431 tmp.crop.l = 0;
432 tmp.crop.t = 0;
433 tmp.crop.r = tmp.img.w;
434 tmp.crop.b = tmp.img.h;
435
436 region_iterator tmp_it(Region(Rect(tmp.crop.r, tmp.crop.b)));
437 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
438 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
439 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
440 err = copybit->stretch(copybit,
441 &tmp.img, &src.img, &tmp.crop, &src.crop, &tmp_it);
442 src = tmp;
443 }
444 }
445
446 const DisplayHardware& hw(mLayer.graphicPlane(0).displayHardware());
447 copybit_image_t dst;
448 hw.getDisplaySurface(&dst);
449 const copybit_rect_t& drect
450 = reinterpret_cast<const copybit_rect_t&>(transformedBounds);
451 const State& s(mLayer.drawingState());
452 region_iterator it(clip);
453 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, mLayer.getOrientation());
454 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, s.alpha);
455 copybit->set_parameter(copybit, COPYBIT_DITHER,
456 s.flags & ISurfaceComposer::eLayerDither ?
457 COPYBIT_ENABLE : COPYBIT_DISABLE);
458 err = copybit->stretch(copybit,
459 &dst, &src.img, &drect, &src.crop, &it);
460 }
461
462 if (!can_use_copybit || err) {
463 if (UNLIKELY(mTextureName == -1LU)) {
464 mTextureName = mLayer.createTexture();
465 }
466 GLuint w = 0;
467 GLuint h = 0;
468 GGLSurface t;
469 t.version = sizeof(GGLSurface);
470 t.width = src.crop.r;
471 t.height = src.crop.b;
472 t.stride = src.img.w;
473 t.vstride= src.img.h;
474 t.format = src.img.format;
475 t.data = (GGLubyte*)(intptr_t(src.img.base) + src.img.offset);
476 const Region dirty(Rect(t.width, t.height));
477 mLayer.loadTexture(dirty, mTextureName, t, w, h);
478 mLayer.drawWithOpenGL(clip, mTextureName, t);
479 }
480}
481
482// ---------------------------------------------------------------------------
483
484LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
485 sp<OverlayRef>* overlayRef,
486 uint32_t w, uint32_t h, int32_t format)
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800487 : Source(layer), mVisibilityChanged(false),
488 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0)
The Android Open Source Project27629322009-01-09 17:51:23 -0800489{
490 overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
The Android Open Source Project27629322009-01-09 17:51:23 -0800491 if (overlay_dev == NULL) {
492 // overlays not supported
493 return;
494 }
495
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800496 mOverlayDevice = overlay_dev;
The Android Open Source Project27629322009-01-09 17:51:23 -0800497 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
498 if (overlay == NULL) {
499 // couldn't create the overlay (no memory? no more overlays?)
500 return;
501 }
502
503 // enable dithering...
504 overlay_dev->setParameter(overlay_dev, overlay,
505 OVERLAY_DITHER, OVERLAY_ENABLE);
506
507 mOverlay = overlay;
508 mWidth = overlay->w;
509 mHeight = overlay->h;
510 mFormat = overlay->format;
511 mWidthStride = overlay->w_stride;
512 mHeightStride = overlay->h_stride;
513
514 mOverlayHandle = overlay->getHandleRef(overlay);
515
516 // NOTE: here it's okay to acquire a reference to "this"m as long as
517 // the reference is not released before we leave the ctor.
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800518 sp<OverlayChannel> channel = new OverlayChannel(this);
The Android Open Source Project27629322009-01-09 17:51:23 -0800519
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800520 *overlayRef = new OverlayRef(mOverlayHandle, channel,
The Android Open Source Project27629322009-01-09 17:51:23 -0800521 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
522}
523
524LayerBuffer::OverlaySource::~OverlaySource()
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800525{
526 if (mOverlay && mOverlayDevice) {
527 overlay_control_device_t* overlay_dev = mOverlayDevice;
528 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
529 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800530}
531
532void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
533{
534 const Layer::State& front(mLayer.drawingState());
535 const Layer::State& temp(mLayer.currentState());
536 if (temp.sequence != front.sequence) {
537 mVisibilityChanged = true;
538 }
539}
540
541void LayerBuffer::OverlaySource::onVisibilityResolved(
542 const Transform& planeTransform)
543{
544 // this code-path must be as tight as possible, it's called each time
545 // the screen is composited.
546 if (UNLIKELY(mOverlay != 0)) {
547 if (mVisibilityChanged) {
548 mVisibilityChanged = false;
549 const Rect& bounds = mLayer.getTransformedBounds();
550 int x = bounds.left;
551 int y = bounds.top;
552 int w = bounds.width();
553 int h = bounds.height();
554
555 // we need a lock here to protect "destroy"
556 Mutex::Autolock _l(mLock);
557 if (mOverlay) {
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800558 overlay_control_device_t* overlay_dev = mOverlayDevice;
The Android Open Source Project27629322009-01-09 17:51:23 -0800559 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
560 overlay_dev->setParameter(overlay_dev, mOverlay,
561 OVERLAY_TRANSFORM, mLayer.getOrientation());
562 }
563 }
564 }
565}
566
567void LayerBuffer::OverlaySource::serverDestroy()
568{
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800569 mLayer.clearSource();
570 destroyOverlay();
571}
572
573void LayerBuffer::OverlaySource::destroyOverlay()
574{
The Android Open Source Project27629322009-01-09 17:51:23 -0800575 // we need a lock here to protect "onVisibilityResolved"
576 Mutex::Autolock _l(mLock);
577 if (mOverlay) {
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800578 overlay_control_device_t* overlay_dev = mOverlayDevice;
The Android Open Source Project27629322009-01-09 17:51:23 -0800579 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
580 mOverlay = 0;
581 }
582}
583
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584// ---------------------------------------------------------------------------
585}; // namespace android