blob: 38a897d5aa150dbe4c2f803657a07489e75b7b25 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <ui/PixelFormat.h>
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -070027#include <ui/FramebufferNativeWindow.h>
28
29#include <hardware/copybit.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Mathias Agopian9779b222009-09-07 16:32:45 -070031#include "Buffer.h"
Mathias Agopian5cec4742009-08-11 22:34:02 -070032#include "BufferAllocator.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include "LayerBuffer.h"
34#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "DisplayHardware/DisplayHardware.h"
36
Mathias Agopian2eab9d82009-06-24 16:55:59 -070037#include "gralloc_priv.h" // needed for msm / copybit
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39namespace android {
40
41// ---------------------------------------------------------------------------
42
43const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20;
44const char* const LayerBuffer::typeID = "LayerBuffer";
45
46// ---------------------------------------------------------------------------
47
48LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian6edf5af2009-06-19 17:00:27 -070049 const sp<Client>& client, int32_t i)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 : LayerBaseClient(flinger, display, client, i),
51 mNeedsBlending(false)
52{
53}
54
55LayerBuffer::~LayerBuffer()
56{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057}
58
Mathias Agopian6cf0db22009-04-17 19:36:26 -070059void LayerBuffer::onFirstRef()
60{
Mathias Agopianc6603952009-06-23 20:06:46 -070061 LayerBaseClient::onFirstRef();
Mathias Agopian9779b222009-09-07 16:32:45 -070062 mSurface = new SurfaceLayerBuffer(mFlinger, clientIndex(),
Mathias Agopian6cf0db22009-04-17 19:36:26 -070063 const_cast<LayerBuffer *>(this));
64}
65
Mathias Agopian1473f462009-04-10 14:24:30 -070066sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067{
Mathias Agopian6cf0db22009-04-17 19:36:26 -070068 return mSurface;
69}
70
71status_t LayerBuffer::ditch()
72{
73 mSurface.clear();
74 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075}
76
77bool LayerBuffer::needsBlending() const {
78 return mNeedsBlending;
79}
80
81void LayerBuffer::setNeedsBlending(bool blending) {
82 mNeedsBlending = blending;
83}
84
85void LayerBuffer::postBuffer(ssize_t offset)
86{
87 sp<Source> source(getSource());
88 if (source != 0)
89 source->postBuffer(offset);
90}
91
92void LayerBuffer::unregisterBuffers()
93{
94 sp<Source> source(clearSource());
95 if (source != 0)
96 source->unregisterBuffers();
97}
98
99uint32_t LayerBuffer::doTransaction(uint32_t flags)
100{
101 sp<Source> source(getSource());
102 if (source != 0)
103 source->onTransaction(flags);
104 return LayerBase::doTransaction(flags);
105}
106
107void LayerBuffer::unlockPageFlip(const Transform& planeTransform,
108 Region& outDirtyRegion)
109{
110 // this code-path must be as tight as possible, it's called each time
111 // the screen is composited.
112 sp<Source> source(getSource());
113 if (source != 0)
114 source->onVisibilityResolved(planeTransform);
115 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
116}
117
118void LayerBuffer::onDraw(const Region& clip) const
119{
120 sp<Source> source(getSource());
121 if (LIKELY(source != 0)) {
122 source->onDraw(clip);
123 } else {
124 clearWithOpenGL(clip);
125 }
126}
127
128bool LayerBuffer::transformed() const
129{
130 sp<Source> source(getSource());
131 if (LIKELY(source != 0))
132 return source->transformed();
133 return false;
134}
135
Mathias Agopiana2804962009-09-08 23:52:08 -0700136void LayerBuffer::serverDestroy()
137{
138 sp<Source> source(clearSource());
139 if (source != 0) {
140 source->destroy();
141 }
142}
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144/**
145 * This creates a "buffer" source for this surface
146 */
147status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
148{
149 Mutex::Autolock _l(mLock);
150 if (mSource != 0)
151 return INVALID_OPERATION;
152
153 sp<BufferSource> source = new BufferSource(*this, buffers);
154
155 status_t result = source->getStatus();
156 if (result == NO_ERROR) {
157 mSource = source;
158 }
159 return result;
160}
161
162/**
163 * This creates an "overlay" source for this surface
164 */
165sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f)
166{
167 sp<OverlayRef> result;
168 Mutex::Autolock _l(mLock);
169 if (mSource != 0)
170 return result;
171
172 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f);
173 if (result != 0) {
174 mSource = source;
175 }
176 return result;
177}
178
179sp<LayerBuffer::Source> LayerBuffer::getSource() const {
180 Mutex::Autolock _l(mLock);
181 return mSource;
182}
183
184sp<LayerBuffer::Source> LayerBuffer::clearSource() {
185 sp<Source> source;
186 Mutex::Autolock _l(mLock);
187 source = mSource;
188 mSource.clear();
189 return source;
190}
191
192// ============================================================================
Mathias Agopian9779b222009-09-07 16:32:45 -0700193// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194// ============================================================================
195
Mathias Agopian9779b222009-09-07 16:32:45 -0700196LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700197 SurfaceID id, const sp<LayerBuffer>& owner)
198 : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199{
200}
201
Mathias Agopian9779b222009-09-07 16:32:45 -0700202LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203{
204 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205}
206
Mathias Agopian9779b222009-09-07 16:32:45 -0700207status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700208 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209{
Mathias Agopian1473f462009-04-10 14:24:30 -0700210 sp<LayerBuffer> owner(getOwner());
211 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 return owner->registerBuffers(buffers);
213 return NO_INIT;
214}
215
Mathias Agopian9779b222009-09-07 16:32:45 -0700216void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217{
Mathias Agopian1473f462009-04-10 14:24:30 -0700218 sp<LayerBuffer> owner(getOwner());
219 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 owner->postBuffer(offset);
221}
222
Mathias Agopian9779b222009-09-07 16:32:45 -0700223void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224{
Mathias Agopian1473f462009-04-10 14:24:30 -0700225 sp<LayerBuffer> owner(getOwner());
226 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 owner->unregisterBuffers();
228}
229
Mathias Agopian9779b222009-09-07 16:32:45 -0700230sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 uint32_t w, uint32_t h, int32_t format) {
232 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700233 sp<LayerBuffer> owner(getOwner());
234 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 result = owner->createOverlay(w, h, format);
236 return result;
237}
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239// ============================================================================
240// LayerBuffer::Buffer
241// ============================================================================
242
243LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset)
244 : mBufferHeap(buffers)
245{
246 NativeBuffer& src(mNativeBuffer);
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 src.crop.l = 0;
249 src.crop.t = 0;
250 src.crop.r = buffers.w;
251 src.crop.b = buffers.h;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700252
253 src.img.w = buffers.hor_stride ?: buffers.w;
254 src.img.h = buffers.ver_stride ?: buffers.h;
255 src.img.format = buffers.format;
256 src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset);
257
Mathias Agopian8f78faa2009-06-25 17:41:12 -0700258 // FIXME: gross hack, we should never access private_handle_t from here,
259 // but this is needed by msm drivers
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700260 private_handle_t* hnd = new private_handle_t(
261 buffers.heap->heapID(), buffers.heap->getSize(), 0);
262 hnd->offset = offset;
263 src.img.handle = hnd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264}
265
266LayerBuffer::Buffer::~Buffer()
267{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700268 NativeBuffer& src(mNativeBuffer);
269 if (src.img.handle)
270 delete (private_handle_t*)src.img.handle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271}
272
273// ============================================================================
274// LayerBuffer::Source
275// LayerBuffer::BufferSource
276// LayerBuffer::OverlaySource
277// ============================================================================
278
279LayerBuffer::Source::Source(LayerBuffer& layer)
280 : mLayer(layer)
281{
282}
283LayerBuffer::Source::~Source() {
284}
285void LayerBuffer::Source::onDraw(const Region& clip) const {
286}
287void LayerBuffer::Source::onTransaction(uint32_t flags) {
288}
289void LayerBuffer::Source::onVisibilityResolved(
290 const Transform& planeTransform) {
291}
292void LayerBuffer::Source::postBuffer(ssize_t offset) {
293}
294void LayerBuffer::Source::unregisterBuffers() {
295}
296bool LayerBuffer::Source::transformed() const {
297 return mLayer.mTransformed;
298}
299
300// ---------------------------------------------------------------------------
301
302LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
303 const ISurface::BufferHeap& buffers)
Mathias Agopian999543b2009-06-23 18:08:22 -0700304 : Source(layer), mStatus(NO_ERROR), mBufferSize(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305{
306 if (buffers.heap == NULL) {
307 // this is allowed, but in this case, it is illegal to receive
308 // postBuffer(). The surface just erases the framebuffer with
309 // fully transparent pixels.
310 mBufferHeap = buffers;
311 mLayer.setNeedsBlending(false);
312 return;
313 }
314
315 status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
316 if (err != NO_ERROR) {
317 LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
318 mStatus = err;
319 return;
320 }
321
322 PixelFormatInfo info;
323 err = getPixelFormatInfo(buffers.format, &info);
324 if (err != NO_ERROR) {
325 LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
326 buffers.format, strerror(err));
327 mStatus = err;
328 return;
329 }
330
331 if (buffers.hor_stride<0 || buffers.ver_stride<0) {
332 LOGE("LayerBuffer::BufferSource: invalid parameters "
333 "(w=%d, h=%d, xs=%d, ys=%d)",
334 buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
335 mStatus = BAD_VALUE;
336 return;
337 }
338
339 mBufferHeap = buffers;
340 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
341 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
342 mLayer.forceVisibilityTransaction();
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700343
344 hw_module_t const* module;
345 mBlitEngine = NULL;
346 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
347 copybit_open(module, &mBlitEngine);
348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349}
350
351LayerBuffer::BufferSource::~BufferSource()
352{
Mathias Agopian999543b2009-06-23 18:08:22 -0700353 if (mTexture.name != -1U) {
354 glDeleteTextures(1, &mTexture.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700356 if (mBlitEngine) {
357 copybit_close(mBlitEngine);
358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359}
360
361void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
362{
363 ISurface::BufferHeap buffers;
364 { // scope for the lock
Mathias Agopianb34d1432009-09-08 20:02:47 -0700365 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 buffers = mBufferHeap;
367 if (buffers.heap != 0) {
368 const size_t memorySize = buffers.heap->getSize();
369 if ((size_t(offset) + mBufferSize) > memorySize) {
370 LOGE("LayerBuffer::BufferSource::postBuffer() "
371 "invalid buffer (offset=%d, size=%d, heap-size=%d",
372 int(offset), int(mBufferSize), int(memorySize));
373 return;
374 }
375 }
376 }
377
378 sp<Buffer> buffer;
379 if (buffers.heap != 0) {
380 buffer = new LayerBuffer::Buffer(buffers, offset);
381 if (buffer->getStatus() != NO_ERROR)
382 buffer.clear();
383 setBuffer(buffer);
384 mLayer.invalidate();
385 }
386}
387
388void LayerBuffer::BufferSource::unregisterBuffers()
389{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700390 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 mBufferHeap.heap.clear();
392 mBuffer.clear();
393 mLayer.invalidate();
394}
395
396sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
397{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700398 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 return mBuffer;
400}
401
402void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
403{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700404 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 mBuffer = buffer;
406}
407
408bool LayerBuffer::BufferSource::transformed() const
409{
410 return mBufferHeap.transform ? true : Source::transformed();
411}
412
413void LayerBuffer::BufferSource::onDraw(const Region& clip) const
414{
Mathias Agopian999543b2009-06-23 18:08:22 -0700415 sp<Buffer> ourBuffer(getBuffer());
416 if (UNLIKELY(ourBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 // nothing to do, we don't have a buffer
418 mLayer.clearWithOpenGL(clip);
419 return;
420 }
421
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700422 status_t err = NO_ERROR;
423 NativeBuffer src(ourBuffer->getBuffer());
Mathias Agopiana2804962009-09-08 23:52:08 -0700424 const Rect transformedBounds(mLayer.getTransformedBounds());
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700425 copybit_device_t* copybit = mBlitEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700427 if (copybit) {
428 const int src_width = src.crop.r - src.crop.l;
429 const int src_height = src.crop.b - src.crop.t;
430 int W = transformedBounds.width();
431 int H = transformedBounds.height();
432 if (mLayer.getOrientation() & Transform::ROT_90) {
433 int t(W); W=H; H=t;
434 }
435
Mathias Agopian26c28b12009-06-24 22:39:26 -0700436#ifdef EGL_ANDROID_get_render_buffer
437 EGLDisplay dpy = eglGetCurrentDisplay();
438 EGLSurface draw = eglGetCurrentSurface(EGL_DRAW);
439 EGLClientBuffer clientBuf = eglGetRenderBufferANDROID(dpy, draw);
440 android_native_buffer_t* nb = (android_native_buffer_t*)clientBuf;
441 if (nb == 0) {
442 err = BAD_VALUE;
443 } else {
444 copybit_image_t dst;
445 dst.w = nb->width;
446 dst.h = nb->height;
447 dst.format = nb->format;
448 dst.base = NULL; // unused by copybit on msm7k
449 dst.handle = (native_handle_t *)nb->handle;
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700450
Mathias Agopian8f78faa2009-06-25 17:41:12 -0700451 /* 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 const float min = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
459 const float mag = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
460
461 float xscale = 1.0f;
462 if (src_width > W*min) xscale = 1.0f / min;
463 else if (src_width*mag < W) xscale = mag;
464
465 float yscale = 1.0f;
466 if (src_height > H*min) yscale = 1.0f / min;
467 else if (src_height*mag < H) yscale = mag;
468
469 if (UNLIKELY(xscale!=1.0f || yscale!=1.0f)) {
470 const int tmp_w = floorf(src_width * xscale);
471 const int tmp_h = floorf(src_height * yscale);
472
473 if (mTempBitmap==0 ||
Mathias Agopian9779b222009-09-07 16:32:45 -0700474 mTempBitmap->getWidth() < size_t(tmp_w) ||
475 mTempBitmap->getHeight() < size_t(tmp_h)) {
Mathias Agopian8f78faa2009-06-25 17:41:12 -0700476 mTempBitmap.clear();
Mathias Agopian5cec4742009-08-11 22:34:02 -0700477 mTempBitmap = new android::Buffer(
478 tmp_w, tmp_h, src.img.format,
Mathias Agopian5cec4742009-08-11 22:34:02 -0700479 BufferAllocator::USAGE_HW_2D);
Mathias Agopian8f78faa2009-06-25 17:41:12 -0700480 err = mTempBitmap->initCheck();
481 }
482
483 if (LIKELY(err == NO_ERROR)) {
484 NativeBuffer tmp;
485 tmp.img.w = tmp_w;
486 tmp.img.h = tmp_h;
487 tmp.img.format = src.img.format;
488 tmp.img.handle = (native_handle_t*)mTempBitmap->getNativeBuffer()->handle;
489 tmp.crop.l = 0;
490 tmp.crop.t = 0;
491 tmp.crop.r = tmp.img.w;
492 tmp.crop.b = tmp.img.h;
493
494 region_iterator tmp_it(Region(Rect(tmp.crop.r, tmp.crop.b)));
495 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
496 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
497 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
498 err = copybit->stretch(copybit,
499 &tmp.img, &src.img, &tmp.crop, &src.crop, &tmp_it);
500 src = tmp;
501 }
502 }
503
Mathias Agopiana2804962009-09-08 23:52:08 -0700504 const Rect transformedBounds(mLayer.getTransformedBounds());
Mathias Agopian8f78faa2009-06-25 17:41:12 -0700505 const copybit_rect_t& drect =
506 reinterpret_cast<const copybit_rect_t&>(transformedBounds);
Mathias Agopian26c28b12009-06-24 22:39:26 -0700507 const State& s(mLayer.drawingState());
508 region_iterator it(clip);
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700509
Mathias Agopian26c28b12009-06-24 22:39:26 -0700510 // pick the right orientation for this buffer
511 int orientation = mLayer.getOrientation();
512 if (UNLIKELY(mBufferHeap.transform)) {
513 Transform rot90;
514 GraphicPlane::orientationToTransfrom(
515 ISurfaceComposer::eOrientation90, 0, 0, &rot90);
516 const Transform& planeTransform(mLayer.graphicPlane(0).transform());
517 const Layer::State& s(mLayer.drawingState());
518 Transform tr(planeTransform * s.transform * rot90);
519 orientation = tr.getOrientation();
520 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700521
Mathias Agopian26c28b12009-06-24 22:39:26 -0700522 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, orientation);
523 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, s.alpha);
524 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700525
Mathias Agopian26c28b12009-06-24 22:39:26 -0700526 err = copybit->stretch(copybit,
527 &dst, &src.img, &drect, &src.crop, &it);
528 if (err != NO_ERROR) {
529 LOGE("copybit failed (%s)", strerror(err));
530 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700531 }
532 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700533#endif
534
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700535 if (!copybit || err)
Mathias Agopian999543b2009-06-23 18:08:22 -0700536 {
537 // OpenGL fall-back
538 if (UNLIKELY(mTexture.name == -1LU)) {
539 mTexture.name = mLayer.createTexture();
540 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700541 GLuint w = 0;
542 GLuint h = 0;
Mathias Agopian999543b2009-06-23 18:08:22 -0700543 GGLSurface t;
544 t.version = sizeof(GGLSurface);
545 t.width = src.crop.r;
546 t.height = src.crop.b;
547 t.stride = src.img.w;
548 t.vstride= src.img.h;
549 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700550 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700551 const Region dirty(Rect(t.width, t.height));
552 mLayer.loadTexture(&mTexture, mTexture.name, dirty, t);
553 mTexture.transform = mBufferHeap.transform;
554 mLayer.drawWithOpenGL(clip, mTexture);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556}
557
558
559// ---------------------------------------------------------------------------
560
561LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
562 sp<OverlayRef>* overlayRef,
563 uint32_t w, uint32_t h, int32_t format)
564 : Source(layer), mVisibilityChanged(false),
565 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0)
566{
567 overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
568 if (overlay_dev == NULL) {
569 // overlays not supported
570 return;
571 }
572
573 mOverlayDevice = overlay_dev;
574 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
575 if (overlay == NULL) {
576 // couldn't create the overlay (no memory? no more overlays?)
577 return;
578 }
579
580 // enable dithering...
581 overlay_dev->setParameter(overlay_dev, overlay,
582 OVERLAY_DITHER, OVERLAY_ENABLE);
583
584 mOverlay = overlay;
585 mWidth = overlay->w;
586 mHeight = overlay->h;
587 mFormat = overlay->format;
588 mWidthStride = overlay->w_stride;
589 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700590 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591
592 mOverlayHandle = overlay->getHandleRef(overlay);
593
Mathias Agopiana2804962009-09-08 23:52:08 -0700594 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
596 *overlayRef = new OverlayRef(mOverlayHandle, channel,
597 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
598}
599
600LayerBuffer::OverlaySource::~OverlaySource()
601{
602 if (mOverlay && mOverlayDevice) {
603 overlay_control_device_t* overlay_dev = mOverlayDevice;
604 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
605 }
606}
607
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700608void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
609{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700610 // this would be where the color-key would be set, should we need it.
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700611 GLclampx red = 0;
612 GLclampx green = 0;
Mathias Agopian8ae03842009-09-15 19:31:28 -0700613 GLclampx blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700614 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700615}
616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
618{
619 const Layer::State& front(mLayer.drawingState());
620 const Layer::State& temp(mLayer.currentState());
621 if (temp.sequence != front.sequence) {
622 mVisibilityChanged = true;
623 }
624}
625
626void LayerBuffer::OverlaySource::onVisibilityResolved(
627 const Transform& planeTransform)
628{
629 // this code-path must be as tight as possible, it's called each time
630 // the screen is composited.
631 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700632 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700634 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700635 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 int x = bounds.left;
637 int y = bounds.top;
638 int w = bounds.width();
639 int h = bounds.height();
640
641 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700642 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 if (mOverlay) {
644 overlay_control_device_t* overlay_dev = mOverlayDevice;
645 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700646 overlay_dev->setParameter(overlay_dev, mOverlay,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 OVERLAY_TRANSFORM, mLayer.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700648 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650 }
651 }
652}
653
Mathias Agopiana2804962009-09-08 23:52:08 -0700654void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655{
656 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700657 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700658 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 overlay_control_device_t* overlay_dev = mOverlayDevice;
660 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
661 mOverlay = 0;
662 }
663}
664
665// ---------------------------------------------------------------------------
666}; // namespace android