blob: bac544ad6e4470236211fd086bf94a4813bd4278 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopian07952722009-05-19 19:08:10 -070028#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31#include <ui/PixelFormat.h>
32#include <ui/EGLDisplaySurface.h>
33
34#include "LayerBuffer.h"
35#include "SurfaceFlinger.h"
36#include "VRamHeap.h"
37#include "DisplayHardware/DisplayHardware.h"
38
39
40namespace android {
41
42// ---------------------------------------------------------------------------
43
44const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20;
45const char* const LayerBuffer::typeID = "LayerBuffer";
46
47// ---------------------------------------------------------------------------
48
49LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display,
50 Client* client, int32_t i)
51 : LayerBaseClient(flinger, display, client, i),
52 mNeedsBlending(false)
53{
54}
55
56LayerBuffer::~LayerBuffer()
57{
58 sp<SurfaceBuffer> s(getClientSurface());
59 if (s != 0) {
60 s->disown();
61 mClientSurface.clear();
62 }
63}
64
65sp<LayerBuffer::SurfaceBuffer> LayerBuffer::getClientSurface() const
66{
67 Mutex::Autolock _l(mLock);
68 return mClientSurface.promote();
69}
70
71sp<LayerBaseClient::Surface> LayerBuffer::getSurface() const
72{
73 sp<SurfaceBuffer> s;
74 Mutex::Autolock _l(mLock);
75 s = mClientSurface.promote();
76 if (s == 0) {
77 s = new SurfaceBuffer(clientIndex(),
78 const_cast<LayerBuffer *>(this));
79 mClientSurface = s;
80 }
81 return s;
82}
83
84bool LayerBuffer::needsBlending() const {
85 return mNeedsBlending;
86}
87
88void LayerBuffer::setNeedsBlending(bool blending) {
89 mNeedsBlending = blending;
90}
91
92void LayerBuffer::postBuffer(ssize_t offset)
93{
94 sp<Source> source(getSource());
95 if (source != 0)
96 source->postBuffer(offset);
97}
98
99void LayerBuffer::unregisterBuffers()
100{
101 sp<Source> source(clearSource());
102 if (source != 0)
103 source->unregisterBuffers();
104}
105
106uint32_t LayerBuffer::doTransaction(uint32_t flags)
107{
108 sp<Source> source(getSource());
109 if (source != 0)
110 source->onTransaction(flags);
111 return LayerBase::doTransaction(flags);
112}
113
114void LayerBuffer::unlockPageFlip(const Transform& planeTransform,
115 Region& outDirtyRegion)
116{
117 // this code-path must be as tight as possible, it's called each time
118 // the screen is composited.
119 sp<Source> source(getSource());
120 if (source != 0)
121 source->onVisibilityResolved(planeTransform);
122 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
123}
124
125void LayerBuffer::onDraw(const Region& clip) const
126{
127 sp<Source> source(getSource());
128 if (LIKELY(source != 0)) {
129 source->onDraw(clip);
130 } else {
131 clearWithOpenGL(clip);
132 }
133}
134
135bool LayerBuffer::transformed() const
136{
137 sp<Source> source(getSource());
138 if (LIKELY(source != 0))
139 return source->transformed();
140 return false;
141}
142
143/**
144 * This creates a "buffer" source for this surface
145 */
146status_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 */
164sp<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
178sp<LayerBuffer::Source> LayerBuffer::getSource() const {
179 Mutex::Autolock _l(mLock);
180 return mSource;
181}
182
183sp<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// ============================================================================
192// LayerBuffer::SurfaceBuffer
193// ============================================================================
194
195LayerBuffer::SurfaceBuffer::SurfaceBuffer(SurfaceID id, LayerBuffer* owner)
196: LayerBaseClient::Surface(id, owner->getIdentity()), mOwner(owner)
197{
198}
199
200LayerBuffer::SurfaceBuffer::~SurfaceBuffer()
201{
202 unregisterBuffers();
203 mOwner = 0;
204}
205
206status_t LayerBuffer::SurfaceBuffer::onTransact(
207 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
208{
209 switch (code) {
210 case REGISTER_BUFFERS:
211 case UNREGISTER_BUFFERS:
212 case CREATE_OVERLAY:
213 {
214 // codes that require permission check
215 IPCThreadState* ipc = IPCThreadState::self();
216 const int pid = ipc->getCallingPid();
217 const int self_pid = getpid();
218 if (LIKELY(pid != self_pid)) {
219 // we're called from a different process, do the real check
220 if (!checkCallingPermission(
221 String16("android.permission.ACCESS_SURFACE_FLINGER")))
222 {
223 const int uid = ipc->getCallingUid();
224 LOGE("Permission Denial: "
225 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
226 return PERMISSION_DENIED;
227 }
228 }
229 }
230 }
231 return LayerBaseClient::Surface::onTransact(code, data, reply, flags);
232}
233
234status_t LayerBuffer::SurfaceBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
235{
236 LayerBuffer* owner(getOwner());
237 if (owner)
238 return owner->registerBuffers(buffers);
239 return NO_INIT;
240}
241
242void LayerBuffer::SurfaceBuffer::postBuffer(ssize_t offset)
243{
244 LayerBuffer* owner(getOwner());
245 if (owner)
246 owner->postBuffer(offset);
247}
248
249void LayerBuffer::SurfaceBuffer::unregisterBuffers()
250{
251 LayerBuffer* owner(getOwner());
252 if (owner)
253 owner->unregisterBuffers();
254}
255
256sp<OverlayRef> LayerBuffer::SurfaceBuffer::createOverlay(
257 uint32_t w, uint32_t h, int32_t format) {
258 sp<OverlayRef> result;
259 LayerBuffer* owner(getOwner());
260 if (owner)
261 result = owner->createOverlay(w, h, format);
262 return result;
263}
264
265void LayerBuffer::SurfaceBuffer::disown()
266{
267 Mutex::Autolock _l(mLock);
268 mOwner = 0;
269}
270
271// ============================================================================
272// LayerBuffer::Buffer
273// ============================================================================
274
275LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset)
276 : mBufferHeap(buffers)
277{
278 NativeBuffer& src(mNativeBuffer);
279 src.crop.l = 0;
280 src.crop.t = 0;
281 src.crop.r = buffers.w;
282 src.crop.b = buffers.h;
283 src.img.w = buffers.hor_stride ?: buffers.w;
284 src.img.h = buffers.ver_stride ?: buffers.h;
285 src.img.format = buffers.format;
286 src.img.offset = offset;
287 src.img.base = buffers.heap->base();
288 src.img.fd = buffers.heap->heapID();
289}
290
291LayerBuffer::Buffer::~Buffer()
292{
293}
294
295// ============================================================================
296// LayerBuffer::Source
297// LayerBuffer::BufferSource
298// LayerBuffer::OverlaySource
299// ============================================================================
300
301LayerBuffer::Source::Source(LayerBuffer& layer)
302 : mLayer(layer)
303{
304}
305LayerBuffer::Source::~Source() {
306}
307void LayerBuffer::Source::onDraw(const Region& clip) const {
308}
309void LayerBuffer::Source::onTransaction(uint32_t flags) {
310}
311void LayerBuffer::Source::onVisibilityResolved(
312 const Transform& planeTransform) {
313}
314void LayerBuffer::Source::postBuffer(ssize_t offset) {
315}
316void LayerBuffer::Source::unregisterBuffers() {
317}
318bool LayerBuffer::Source::transformed() const {
319 return mLayer.mTransformed;
320}
321
322// ---------------------------------------------------------------------------
323
324LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
325 const ISurface::BufferHeap& buffers)
326 : Source(layer), mStatus(NO_ERROR),
327 mBufferSize(0), mTextureName(-1U)
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
362 mBufferHeap = buffers;
363 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
364 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
365 mLayer.forceVisibilityTransaction();
366
367}
368
369LayerBuffer::BufferSource::~BufferSource()
370{
371 if (mTextureName != -1U) {
372 LayerBase::deletedTextures.add(mTextureName);
373 }
374}
375
376void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
377{
378 ISurface::BufferHeap buffers;
379 { // scope for the lock
380 Mutex::Autolock _l(mLock);
381 buffers = mBufferHeap;
382 if (buffers.heap != 0) {
383 const size_t memorySize = buffers.heap->getSize();
384 if ((size_t(offset) + mBufferSize) > memorySize) {
385 LOGE("LayerBuffer::BufferSource::postBuffer() "
386 "invalid buffer (offset=%d, size=%d, heap-size=%d",
387 int(offset), int(mBufferSize), int(memorySize));
388 return;
389 }
390 }
391 }
392
393 sp<Buffer> buffer;
394 if (buffers.heap != 0) {
395 buffer = new LayerBuffer::Buffer(buffers, offset);
396 if (buffer->getStatus() != NO_ERROR)
397 buffer.clear();
398 setBuffer(buffer);
399 mLayer.invalidate();
400 }
401}
402
403void LayerBuffer::BufferSource::unregisterBuffers()
404{
405 Mutex::Autolock _l(mLock);
406 mBufferHeap.heap.clear();
407 mBuffer.clear();
408 mLayer.invalidate();
409}
410
411sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
412{
413 Mutex::Autolock _l(mLock);
414 return mBuffer;
415}
416
417void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
418{
419 Mutex::Autolock _l(mLock);
420 mBuffer = buffer;
421}
422
423bool LayerBuffer::BufferSource::transformed() const
424{
425 return mBufferHeap.transform ? true : Source::transformed();
426}
427
428void LayerBuffer::BufferSource::onDraw(const Region& clip) const
429{
430 sp<Buffer> buffer(getBuffer());
431 if (UNLIKELY(buffer == 0)) {
432 // nothing to do, we don't have a buffer
433 mLayer.clearWithOpenGL(clip);
434 return;
435 }
436
437 status_t err = NO_ERROR;
438 NativeBuffer src(buffer->getBuffer());
439 const Rect& transformedBounds = mLayer.getTransformedBounds();
440 const int can_use_copybit = mLayer.canUseCopybit();
441
442 if (can_use_copybit) {
443 const int src_width = src.crop.r - src.crop.l;
444 const int src_height = src.crop.b - src.crop.t;
445 int W = transformedBounds.width();
446 int H = transformedBounds.height();
447 if (mLayer.getOrientation() & Transform::ROT_90) {
448 int t(W); W=H; H=t;
449 }
450
451 /* With LayerBuffer, it is likely that we'll have to rescale the
452 * surface, because this is often used for video playback or
453 * camera-preview. Since we want these operation as fast as possible
454 * we make sure we can use the 2D H/W even if it doesn't support
455 * the requested scale factor, in which case we perform the scaling
456 * in several passes. */
457
458 copybit_device_t* copybit = mLayer.mFlinger->getBlitEngine();
459 const float min = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
460 const float mag = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
461
462 float xscale = 1.0f;
463 if (src_width > W*min) xscale = 1.0f / min;
464 else if (src_width*mag < W) xscale = mag;
465
466 float yscale = 1.0f;
467 if (src_height > H*min) yscale = 1.0f / min;
468 else if (src_height*mag < H) yscale = mag;
469
470 if (UNLIKELY(xscale!=1.0f || yscale!=1.0f)) {
471 if (UNLIKELY(mTemporaryDealer == 0)) {
472 // allocate a memory-dealer for this the first time
473 mTemporaryDealer = mLayer.mFlinger->getSurfaceHeapManager()
474 ->createHeap(ISurfaceComposer::eHardware);
475 mTempBitmap.init(mTemporaryDealer);
476 }
477
478 const int tmp_w = floorf(src_width * xscale);
479 const int tmp_h = floorf(src_height * yscale);
480 err = mTempBitmap.setBits(tmp_w, tmp_h, 1, src.img.format);
481
482 if (LIKELY(err == NO_ERROR)) {
483 NativeBuffer tmp;
484 mTempBitmap.getBitmapSurface(&tmp.img);
485 tmp.crop.l = 0;
486 tmp.crop.t = 0;
487 tmp.crop.r = tmp.img.w;
488 tmp.crop.b = tmp.img.h;
489
490 region_iterator tmp_it(Region(Rect(tmp.crop.r, tmp.crop.b)));
491 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
492 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
493 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
494 err = copybit->stretch(copybit,
495 &tmp.img, &src.img, &tmp.crop, &src.crop, &tmp_it);
496 src = tmp;
497 }
498 }
499
500 const DisplayHardware& hw(mLayer.graphicPlane(0).displayHardware());
501 copybit_image_t dst;
502 hw.getDisplaySurface(&dst);
503 const copybit_rect_t& drect
504 = reinterpret_cast<const copybit_rect_t&>(transformedBounds);
505 const State& s(mLayer.drawingState());
506 region_iterator it(clip);
507
508 // pick the right orientation for this buffer
509 int orientation = mLayer.getOrientation();
510 if (UNLIKELY(mBufferHeap.transform)) {
511 Transform rot90;
512 GraphicPlane::orientationToTransfrom(
513 ISurfaceComposer::eOrientation90, 0, 0, &rot90);
514 const Transform& planeTransform(mLayer.graphicPlane(0).transform());
515 const Layer::State& s(mLayer.drawingState());
516 Transform tr(planeTransform * s.transform * rot90);
517 orientation = tr.getOrientation();
518 }
519
520 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, orientation);
521 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, s.alpha);
522 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
523
524 err = copybit->stretch(copybit,
525 &dst, &src.img, &drect, &src.crop, &it);
526 if (err != NO_ERROR) {
527 LOGE("copybit failed (%s)", strerror(err));
528 }
529 }
530
531 if (!can_use_copybit || err) {
532 if (UNLIKELY(mTextureName == -1LU)) {
533 mTextureName = mLayer.createTexture();
534 }
535 GLuint w = 0;
536 GLuint h = 0;
537 GGLSurface t;
538 t.version = sizeof(GGLSurface);
539 t.width = src.crop.r;
540 t.height = src.crop.b;
541 t.stride = src.img.w;
542 t.vstride= src.img.h;
543 t.format = src.img.format;
544 t.data = (GGLubyte*)(intptr_t(src.img.base) + src.img.offset);
545 const Region dirty(Rect(t.width, t.height));
546 mLayer.loadTexture(dirty, mTextureName, t, w, h);
547 mLayer.drawWithOpenGL(clip, mTextureName, t, mBufferHeap.transform);
548 }
549}
550
551
552// ---------------------------------------------------------------------------
553
554LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
555 sp<OverlayRef>* overlayRef,
556 uint32_t w, uint32_t h, int32_t format)
557 : Source(layer), mVisibilityChanged(false),
558 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0)
559{
560 overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
561 if (overlay_dev == NULL) {
562 // overlays not supported
563 return;
564 }
565
566 mOverlayDevice = overlay_dev;
567 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
568 if (overlay == NULL) {
569 // couldn't create the overlay (no memory? no more overlays?)
570 return;
571 }
572
573 // enable dithering...
574 overlay_dev->setParameter(overlay_dev, overlay,
575 OVERLAY_DITHER, OVERLAY_ENABLE);
576
577 mOverlay = overlay;
578 mWidth = overlay->w;
579 mHeight = overlay->h;
580 mFormat = overlay->format;
581 mWidthStride = overlay->w_stride;
582 mHeightStride = overlay->h_stride;
583
584 mOverlayHandle = overlay->getHandleRef(overlay);
585
586 // NOTE: here it's okay to acquire a reference to "this"m as long as
587 // the reference is not released before we leave the ctor.
588 sp<OverlayChannel> channel = new OverlayChannel(this);
589
590 *overlayRef = new OverlayRef(mOverlayHandle, channel,
591 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
592}
593
594LayerBuffer::OverlaySource::~OverlaySource()
595{
596 if (mOverlay && mOverlayDevice) {
597 overlay_control_device_t* overlay_dev = mOverlayDevice;
598 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
599 }
600}
601
602void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
603{
604 const Layer::State& front(mLayer.drawingState());
605 const Layer::State& temp(mLayer.currentState());
606 if (temp.sequence != front.sequence) {
607 mVisibilityChanged = true;
608 }
609}
610
611void LayerBuffer::OverlaySource::onVisibilityResolved(
612 const Transform& planeTransform)
613{
614 // this code-path must be as tight as possible, it's called each time
615 // the screen is composited.
616 if (UNLIKELY(mOverlay != 0)) {
617 if (mVisibilityChanged) {
618 mVisibilityChanged = false;
619 const Rect& bounds = mLayer.getTransformedBounds();
620 int x = bounds.left;
621 int y = bounds.top;
622 int w = bounds.width();
623 int h = bounds.height();
624
625 // we need a lock here to protect "destroy"
626 Mutex::Autolock _l(mLock);
627 if (mOverlay) {
628 overlay_control_device_t* overlay_dev = mOverlayDevice;
629 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
630 overlay_dev->setParameter(overlay_dev, mOverlay,
631 OVERLAY_TRANSFORM, mLayer.getOrientation());
632 }
633 }
634 }
635}
636
637void LayerBuffer::OverlaySource::serverDestroy()
638{
639 mLayer.clearSource();
640 destroyOverlay();
641}
642
643void LayerBuffer::OverlaySource::destroyOverlay()
644{
645 // we need a lock here to protect "onVisibilityResolved"
646 Mutex::Autolock _l(mLock);
647 if (mOverlay) {
648 overlay_control_device_t* overlay_dev = mOverlayDevice;
649 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
650 mOverlay = 0;
651 }
652}
653
654// ---------------------------------------------------------------------------
655}; // namespace android