blob: 2ff61673df67e25fe2aabeeac173c1a9ad06c357 [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
Mathias Agopian6950e422009-10-05 17:07:12 -070026#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -070028#include <ui/FramebufferNativeWindow.h>
Mathias Agopian90daccf2009-11-05 23:08:00 -080029#include <ui/Rect.h>
30#include <ui/Region.h>
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -070031
32#include <hardware/copybit.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34#include "LayerBuffer.h"
35#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038namespace android {
39
40// ---------------------------------------------------------------------------
41
42const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20;
43const char* const LayerBuffer::typeID = "LayerBuffer";
Mathias Agopian0b0722f2009-10-29 18:29:30 -070044gralloc_module_t const* LayerBuffer::sGrallocModule = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
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),
Mathias Agopian90daccf2009-11-05 23:08:00 -080051 mNeedsBlending(false), mBlitEngine(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052{
53}
54
55LayerBuffer::~LayerBuffer()
56{
Mathias Agopian90daccf2009-11-05 23:08:00 -080057 if (mBlitEngine) {
58 copybit_close(mBlitEngine);
59 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060}
61
Mathias Agopian6cf0db22009-04-17 19:36:26 -070062void LayerBuffer::onFirstRef()
63{
Mathias Agopianc6603952009-06-23 20:06:46 -070064 LayerBaseClient::onFirstRef();
Mathias Agopian9779b222009-09-07 16:32:45 -070065 mSurface = new SurfaceLayerBuffer(mFlinger, clientIndex(),
Mathias Agopian6cf0db22009-04-17 19:36:26 -070066 const_cast<LayerBuffer *>(this));
Mathias Agopian0b0722f2009-10-29 18:29:30 -070067
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 Agopian90daccf2009-11-05 23:08:00 -080077
78 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
79 copybit_open(module, &mBlitEngine);
80 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -070081}
82
Mathias Agopian1473f462009-04-10 14:24:30 -070083sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084{
Mathias Agopian6cf0db22009-04-17 19:36:26 -070085 return mSurface;
86}
87
88status_t LayerBuffer::ditch()
89{
90 mSurface.clear();
91 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092}
93
94bool LayerBuffer::needsBlending() const {
95 return mNeedsBlending;
96}
97
98void LayerBuffer::setNeedsBlending(bool blending) {
99 mNeedsBlending = blending;
100}
101
102void LayerBuffer::postBuffer(ssize_t offset)
103{
104 sp<Source> source(getSource());
105 if (source != 0)
106 source->postBuffer(offset);
107}
108
109void LayerBuffer::unregisterBuffers()
110{
111 sp<Source> source(clearSource());
112 if (source != 0)
113 source->unregisterBuffers();
114}
115
116uint32_t LayerBuffer::doTransaction(uint32_t flags)
117{
118 sp<Source> source(getSource());
119 if (source != 0)
120 source->onTransaction(flags);
Mathias Agopian4c29c942009-11-19 14:46:26 -0800121 uint32_t res = LayerBase::doTransaction(flags);
122 // we always want filtering for these surfaces
Mathias Agopianb548dbb2009-12-08 19:29:38 -0800123 mUseLinearFiltering = !(mFlags & DisplayHardware::SLOW_CONFIG);
Mathias Agopian4c29c942009-11-19 14:46:26 -0800124 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125}
126
127void 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
138void 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
148bool LayerBuffer::transformed() const
149{
150 sp<Source> source(getSource());
151 if (LIKELY(source != 0))
152 return source->transformed();
153 return false;
154}
155
Mathias Agopiana2804962009-09-08 23:52:08 -0700156void LayerBuffer::serverDestroy()
157{
158 sp<Source> source(clearSource());
159 if (source != 0) {
160 source->destroy();
161 }
162}
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164/**
165 * This creates a "buffer" source for this surface
166 */
167status_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 */
185sp<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
199sp<LayerBuffer::Source> LayerBuffer::getSource() const {
200 Mutex::Autolock _l(mLock);
201 return mSource;
202}
203
204sp<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 Agopian9779b222009-09-07 16:32:45 -0700213// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214// ============================================================================
215
Mathias Agopian9779b222009-09-07 16:32:45 -0700216LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700217 SurfaceID id, const sp<LayerBuffer>& owner)
218 : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219{
220}
221
Mathias Agopian9779b222009-09-07 16:32:45 -0700222LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223{
224 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225}
226
Mathias Agopian9779b222009-09-07 16:32:45 -0700227status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700228 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229{
Mathias Agopian1473f462009-04-10 14:24:30 -0700230 sp<LayerBuffer> owner(getOwner());
231 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 return owner->registerBuffers(buffers);
233 return NO_INIT;
234}
235
Mathias Agopian9779b222009-09-07 16:32:45 -0700236void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237{
Mathias Agopian1473f462009-04-10 14:24:30 -0700238 sp<LayerBuffer> owner(getOwner());
239 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 owner->postBuffer(offset);
241}
242
Mathias Agopian9779b222009-09-07 16:32:45 -0700243void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244{
Mathias Agopian1473f462009-04-10 14:24:30 -0700245 sp<LayerBuffer> owner(getOwner());
246 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 owner->unregisterBuffers();
248}
249
Mathias Agopian9779b222009-09-07 16:32:45 -0700250sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 uint32_t w, uint32_t h, int32_t format) {
252 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700253 sp<LayerBuffer> owner(getOwner());
254 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 result = owner->createOverlay(w, h, format);
256 return result;
257}
258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259// ============================================================================
260// LayerBuffer::Buffer
261// ============================================================================
262
263LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset)
264 : mBufferHeap(buffers)
265{
266 NativeBuffer& src(mNativeBuffer);
Mathias Agopiand86beb92009-12-02 16:23:11 -0800267 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 Agopian2eab9d82009-06-24 16:55:59 -0700277
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700278 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 Agopiand86beb92009-12-02 16:23:11 -0800286 LOGE_IF(err, "CREATE_HANDLE_FROM_BUFFER (heapId=%d, size=%d, "
287 "offset=%ld, base=%p) failed (%s)",
288 buffers.heap->heapID(), buffers.heap->getSize(),
289 offset, buffers.heap->base(), strerror(-err));
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700290 }
Mathias Agopiand86beb92009-12-02 16:23:11 -0800291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293LayerBuffer::Buffer::~Buffer()
294{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700295 NativeBuffer& src(mNativeBuffer);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700296 if (src.img.handle) {
297 native_handle_delete(src.img.handle);
298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299}
300
301// ============================================================================
302// LayerBuffer::Source
303// LayerBuffer::BufferSource
304// LayerBuffer::OverlaySource
305// ============================================================================
306
307LayerBuffer::Source::Source(LayerBuffer& layer)
308 : mLayer(layer)
309{
310}
311LayerBuffer::Source::~Source() {
312}
313void LayerBuffer::Source::onDraw(const Region& clip) const {
314}
315void LayerBuffer::Source::onTransaction(uint32_t flags) {
316}
317void LayerBuffer::Source::onVisibilityResolved(
318 const Transform& planeTransform) {
319}
320void LayerBuffer::Source::postBuffer(ssize_t offset) {
321}
322void LayerBuffer::Source::unregisterBuffers() {
323}
324bool LayerBuffer::Source::transformed() const {
325 return mLayer.mTransformed;
326}
327
328// ---------------------------------------------------------------------------
329
330LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
331 const ISurface::BufferHeap& buffers)
Mathias Agopian999543b2009-06-23 18:08:22 -0700332 : Source(layer), mStatus(NO_ERROR), mBufferSize(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333{
334 if (buffers.heap == NULL) {
335 // this is allowed, but in this case, it is illegal to receive
336 // postBuffer(). The surface just erases the framebuffer with
337 // fully transparent pixels.
338 mBufferHeap = buffers;
339 mLayer.setNeedsBlending(false);
340 return;
341 }
342
343 status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
344 if (err != NO_ERROR) {
345 LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
346 mStatus = err;
347 return;
348 }
349
350 PixelFormatInfo info;
351 err = getPixelFormatInfo(buffers.format, &info);
352 if (err != NO_ERROR) {
353 LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
354 buffers.format, strerror(err));
355 mStatus = err;
356 return;
357 }
358
359 if (buffers.hor_stride<0 || buffers.ver_stride<0) {
360 LOGE("LayerBuffer::BufferSource: invalid parameters "
361 "(w=%d, h=%d, xs=%d, ys=%d)",
362 buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
363 mStatus = BAD_VALUE;
364 return;
365 }
366
367 mBufferHeap = buffers;
368 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
369 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
370 mLayer.forceVisibilityTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371}
372
373LayerBuffer::BufferSource::~BufferSource()
374{
Mathias Agopian999543b2009-06-23 18:08:22 -0700375 if (mTexture.name != -1U) {
376 glDeleteTextures(1, &mTexture.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700378 if (mTexture.image != EGL_NO_IMAGE_KHR) {
379 EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
380 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382}
383
384void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
385{
386 ISurface::BufferHeap buffers;
387 { // scope for the lock
Mathias Agopianb34d1432009-09-08 20:02:47 -0700388 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 buffers = mBufferHeap;
390 if (buffers.heap != 0) {
391 const size_t memorySize = buffers.heap->getSize();
392 if ((size_t(offset) + mBufferSize) > memorySize) {
393 LOGE("LayerBuffer::BufferSource::postBuffer() "
394 "invalid buffer (offset=%d, size=%d, heap-size=%d",
395 int(offset), int(mBufferSize), int(memorySize));
396 return;
397 }
398 }
399 }
400
401 sp<Buffer> buffer;
402 if (buffers.heap != 0) {
403 buffer = new LayerBuffer::Buffer(buffers, offset);
404 if (buffer->getStatus() != NO_ERROR)
405 buffer.clear();
406 setBuffer(buffer);
407 mLayer.invalidate();
408 }
409}
410
411void LayerBuffer::BufferSource::unregisterBuffers()
412{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700413 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 mBufferHeap.heap.clear();
415 mBuffer.clear();
416 mLayer.invalidate();
417}
418
419sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
420{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700421 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 return mBuffer;
423}
424
425void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
426{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700427 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 mBuffer = buffer;
429}
430
431bool LayerBuffer::BufferSource::transformed() const
432{
433 return mBufferHeap.transform ? true : Source::transformed();
434}
435
436void LayerBuffer::BufferSource::onDraw(const Region& clip) const
437{
Mathias Agopian999543b2009-06-23 18:08:22 -0700438 sp<Buffer> ourBuffer(getBuffer());
439 if (UNLIKELY(ourBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 // nothing to do, we don't have a buffer
441 mLayer.clearWithOpenGL(clip);
442 return;
443 }
444
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700445 status_t err = NO_ERROR;
446 NativeBuffer src(ourBuffer->getBuffer());
Mathias Agopiana2804962009-09-08 23:52:08 -0700447 const Rect transformedBounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448
Mathias Agopian9042b452009-10-26 20:12:37 -0700449 if (UNLIKELY(mTexture.name == -1LU)) {
450 mTexture.name = mLayer.createTexture();
451 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700452
Mathias Agopian9042b452009-10-26 20:12:37 -0700453#if defined(EGL_ANDROID_image_native_buffer)
454 if (mLayer.mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopian90daccf2009-11-05 23:08:00 -0800455 copybit_device_t* copybit = mLayer.mBlitEngine;
456 if (copybit) {
457 // create our EGLImageKHR the first time
Mathias Agopian76169da2009-12-09 14:32:56 -0800458 err = initTempBuffer();
Mathias Agopian90daccf2009-11-05 23:08:00 -0800459 if (err == NO_ERROR) {
460 // NOTE: Assume the buffer is allocated with the proper USAGE flags
461 const NativeBuffer& dst(mTempBuffer);
462 region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b)));
463 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
464 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
465 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
466 err = copybit->stretch(copybit, &dst.img, &src.img,
467 &dst.crop, &src.crop, &clip);
Mathias Agopian36d01842009-11-02 17:48:33 -0800468
Mathias Agopian90daccf2009-11-05 23:08:00 -0800469 }
470 } else {
471 err = INVALID_OPERATION;
472 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700473 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700474#endif
Mathias Agopian9042b452009-10-26 20:12:37 -0700475 else {
476 err = INVALID_OPERATION;
477 }
478
479 if (err != NO_ERROR) {
Mathias Agopianf007a2f2009-10-28 21:00:29 -0700480 // slower fallback
Mathias Agopian999543b2009-06-23 18:08:22 -0700481 GGLSurface t;
482 t.version = sizeof(GGLSurface);
483 t.width = src.crop.r;
484 t.height = src.crop.b;
485 t.stride = src.img.w;
486 t.vstride= src.img.h;
487 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700488 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700489 const Region dirty(Rect(t.width, t.height));
Mathias Agopian6950e422009-10-05 17:07:12 -0700490 mLayer.loadTexture(&mTexture, dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
Mathias Agopian9042b452009-10-26 20:12:37 -0700493 mTexture.transform = mBufferHeap.transform;
494 mLayer.drawWithOpenGL(clip, mTexture);
495}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
Mathias Agopian76169da2009-12-09 14:32:56 -0800497status_t LayerBuffer::BufferSource::initTempBuffer() const
498{
499 // figure out the size we need now
500 const ISurface::BufferHeap& buffers(mBufferHeap);
501 uint32_t w = mLayer.mTransformedBounds.width();
502 uint32_t h = mLayer.mTransformedBounds.height();
503 if (buffers.w * h != buffers.h * w) {
504 int t = w; w = h; h = t;
505 }
506
507 if (mTexture.image != EGL_NO_IMAGE_KHR) {
508 // we have an EGLImage, make sure the needed size didn't change
509 if (w!=mTexture.width || h!= mTexture.height) {
510 // delete the EGLImage and texture
511 EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
512 glDeleteTextures(1, &mTexture.name);
513 eglDestroyImageKHR(dpy, mTexture.image);
514 Texture defaultTexture;
515 mTexture = defaultTexture;
516 mTempGraphicBuffer.clear();
517 } else {
518 // we're good, we have an EGLImageKHR and it's (still) the
519 // right size
520 return NO_ERROR;
521 }
522 }
523
524 // figure out if we need linear filtering
525 if (buffers.w * h == buffers.h * w) {
526 // same pixel area, don't use filtering
527 mLayer.mUseLinearFiltering = false;
528 }
529
530 // Allocate a temporary buffer and create the corresponding EGLImageKHR
531
532 status_t err;
533 mTempGraphicBuffer.clear();
534 mTempGraphicBuffer = new GraphicBuffer(
535 w, h, HAL_PIXEL_FORMAT_RGB_565,
536 GraphicBuffer::USAGE_HW_TEXTURE |
537 GraphicBuffer::USAGE_HW_2D);
538
539 err = mTempGraphicBuffer->initCheck();
540 if (err == NO_ERROR) {
541 NativeBuffer& dst(mTempBuffer);
542 dst.img.w = mTempGraphicBuffer->getStride();
543 dst.img.h = h;
544 dst.img.format = mTempGraphicBuffer->getPixelFormat();
545 dst.img.handle = (native_handle_t *)mTempGraphicBuffer->handle;
546 dst.img.base = 0;
547 dst.crop.l = 0;
548 dst.crop.t = 0;
549 dst.crop.r = w;
550 dst.crop.b = h;
551
552 err = mLayer.initializeEglImage(
553 mTempGraphicBuffer, &mTexture);
554 // once the EGLImage has been created (whether it fails
555 // or not) we don't need the graphic buffer reference
556 // anymore.
557 mTempGraphicBuffer.clear();
558 }
559
560 return err;
561}
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563// ---------------------------------------------------------------------------
564
565LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
566 sp<OverlayRef>* overlayRef,
567 uint32_t w, uint32_t h, int32_t format)
568 : Source(layer), mVisibilityChanged(false),
569 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0)
570{
571 overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
572 if (overlay_dev == NULL) {
573 // overlays not supported
574 return;
575 }
576
577 mOverlayDevice = overlay_dev;
578 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
579 if (overlay == NULL) {
580 // couldn't create the overlay (no memory? no more overlays?)
581 return;
582 }
583
584 // enable dithering...
585 overlay_dev->setParameter(overlay_dev, overlay,
586 OVERLAY_DITHER, OVERLAY_ENABLE);
587
588 mOverlay = overlay;
589 mWidth = overlay->w;
590 mHeight = overlay->h;
591 mFormat = overlay->format;
592 mWidthStride = overlay->w_stride;
593 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700594 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
596 mOverlayHandle = overlay->getHandleRef(overlay);
597
Mathias Agopiana2804962009-09-08 23:52:08 -0700598 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599
600 *overlayRef = new OverlayRef(mOverlayHandle, channel,
601 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
Benny Wonge38212a2009-09-16 14:48:52 -0500602 mLayer.mFlinger->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603}
604
605LayerBuffer::OverlaySource::~OverlaySource()
606{
607 if (mOverlay && mOverlayDevice) {
608 overlay_control_device_t* overlay_dev = mOverlayDevice;
609 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
610 }
611}
612
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700613void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
614{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700615 // this would be where the color-key would be set, should we need it.
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700616 GLclampx red = 0;
617 GLclampx green = 0;
Mathias Agopian8ae03842009-09-15 19:31:28 -0700618 GLclampx blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700619 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700620}
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
623{
624 const Layer::State& front(mLayer.drawingState());
625 const Layer::State& temp(mLayer.currentState());
626 if (temp.sequence != front.sequence) {
627 mVisibilityChanged = true;
628 }
629}
630
631void LayerBuffer::OverlaySource::onVisibilityResolved(
632 const Transform& planeTransform)
633{
634 // this code-path must be as tight as possible, it's called each time
635 // the screen is composited.
636 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700637 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700639 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700640 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 int x = bounds.left;
642 int y = bounds.top;
643 int w = bounds.width();
644 int h = bounds.height();
645
646 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700647 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 if (mOverlay) {
649 overlay_control_device_t* overlay_dev = mOverlayDevice;
650 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700651 overlay_dev->setParameter(overlay_dev, mOverlay,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 OVERLAY_TRANSFORM, mLayer.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700653 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655 }
656 }
657}
658
Mathias Agopiana2804962009-09-08 23:52:08 -0700659void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660{
661 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700662 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700663 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 overlay_control_device_t* overlay_dev = mOverlayDevice;
665 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
666 mOverlay = 0;
667 }
668}
669
670// ---------------------------------------------------------------------------
671}; // namespace android