blob: 2735aa2a31e2a7def80f2cf81d54b6b8c8d7d829 [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 */
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800185sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f,
186 int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187{
188 sp<OverlayRef> result;
189 Mutex::Autolock _l(mLock);
190 if (mSource != 0)
191 return result;
192
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800193 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 if (result != 0) {
195 mSource = source;
196 }
197 return result;
198}
199
200sp<LayerBuffer::Source> LayerBuffer::getSource() const {
201 Mutex::Autolock _l(mLock);
202 return mSource;
203}
204
205sp<LayerBuffer::Source> LayerBuffer::clearSource() {
206 sp<Source> source;
207 Mutex::Autolock _l(mLock);
208 source = mSource;
209 mSource.clear();
210 return source;
211}
212
213// ============================================================================
Mathias Agopian9779b222009-09-07 16:32:45 -0700214// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215// ============================================================================
216
Mathias Agopian9779b222009-09-07 16:32:45 -0700217LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700218 SurfaceID id, const sp<LayerBuffer>& owner)
219 : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220{
221}
222
Mathias Agopian9779b222009-09-07 16:32:45 -0700223LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224{
225 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226}
227
Mathias Agopian9779b222009-09-07 16:32:45 -0700228status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700229 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230{
Mathias Agopian1473f462009-04-10 14:24:30 -0700231 sp<LayerBuffer> owner(getOwner());
232 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 return owner->registerBuffers(buffers);
234 return NO_INIT;
235}
236
Mathias Agopian9779b222009-09-07 16:32:45 -0700237void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238{
Mathias Agopian1473f462009-04-10 14:24:30 -0700239 sp<LayerBuffer> owner(getOwner());
240 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 owner->postBuffer(offset);
242}
243
Mathias Agopian9779b222009-09-07 16:32:45 -0700244void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245{
Mathias Agopian1473f462009-04-10 14:24:30 -0700246 sp<LayerBuffer> owner(getOwner());
247 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 owner->unregisterBuffers();
249}
250
Mathias Agopian9779b222009-09-07 16:32:45 -0700251sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800252 uint32_t w, uint32_t h, int32_t format, int32_t orientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700254 sp<LayerBuffer> owner(getOwner());
255 if (owner != 0)
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800256 result = owner->createOverlay(w, h, format, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 return result;
258}
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260// ============================================================================
261// LayerBuffer::Buffer
262// ============================================================================
263
264LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset)
Mathias Agopian7323e542010-01-20 13:24:14 -0800265 : mBufferHeap(buffers), mSupportsCopybit(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266{
267 NativeBuffer& src(mNativeBuffer);
Mathias Agopiand86beb92009-12-02 16:23:11 -0800268 src.crop.l = 0;
269 src.crop.t = 0;
270 src.crop.r = buffers.w;
271 src.crop.b = buffers.h;
272
273 src.img.w = buffers.hor_stride ?: buffers.w;
274 src.img.h = buffers.ver_stride ?: buffers.h;
275 src.img.format = buffers.format;
276 src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset);
277 src.img.handle = 0;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700278
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700279 gralloc_module_t const * module = LayerBuffer::getGrallocModule();
280 if (module && module->perform) {
281 int err = module->perform(module,
282 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER,
283 buffers.heap->heapID(), buffers.heap->getSize(),
284 offset, buffers.heap->base(),
285 &src.img.handle);
286
Mathias Agopian7323e542010-01-20 13:24:14 -0800287 // we can fail here is the passed buffer is purely software
288 mSupportsCopybit = (err == NO_ERROR);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700289 }
Mathias Agopiand86beb92009-12-02 16:23:11 -0800290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
292LayerBuffer::Buffer::~Buffer()
293{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700294 NativeBuffer& src(mNativeBuffer);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700295 if (src.img.handle) {
296 native_handle_delete(src.img.handle);
297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298}
299
300// ============================================================================
301// LayerBuffer::Source
302// LayerBuffer::BufferSource
303// LayerBuffer::OverlaySource
304// ============================================================================
305
306LayerBuffer::Source::Source(LayerBuffer& layer)
307 : mLayer(layer)
308{
309}
310LayerBuffer::Source::~Source() {
311}
312void LayerBuffer::Source::onDraw(const Region& clip) const {
313}
314void LayerBuffer::Source::onTransaction(uint32_t flags) {
315}
316void LayerBuffer::Source::onVisibilityResolved(
317 const Transform& planeTransform) {
318}
319void LayerBuffer::Source::postBuffer(ssize_t offset) {
320}
321void LayerBuffer::Source::unregisterBuffers() {
322}
323bool LayerBuffer::Source::transformed() const {
324 return mLayer.mTransformed;
325}
326
327// ---------------------------------------------------------------------------
328
329LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
330 const ISurface::BufferHeap& buffers)
Mathias Agopian999543b2009-06-23 18:08:22 -0700331 : Source(layer), mStatus(NO_ERROR), mBufferSize(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332{
333 if (buffers.heap == NULL) {
334 // this is allowed, but in this case, it is illegal to receive
335 // postBuffer(). The surface just erases the framebuffer with
336 // fully transparent pixels.
337 mBufferHeap = buffers;
338 mLayer.setNeedsBlending(false);
339 return;
340 }
341
342 status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
343 if (err != NO_ERROR) {
344 LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
345 mStatus = err;
346 return;
347 }
348
349 PixelFormatInfo info;
350 err = getPixelFormatInfo(buffers.format, &info);
351 if (err != NO_ERROR) {
352 LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
353 buffers.format, strerror(err));
354 mStatus = err;
355 return;
356 }
357
358 if (buffers.hor_stride<0 || buffers.ver_stride<0) {
359 LOGE("LayerBuffer::BufferSource: invalid parameters "
360 "(w=%d, h=%d, xs=%d, ys=%d)",
361 buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
362 mStatus = BAD_VALUE;
363 return;
364 }
365
366 mBufferHeap = buffers;
367 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
368 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
369 mLayer.forceVisibilityTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370}
371
372LayerBuffer::BufferSource::~BufferSource()
373{
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800374 class MessageDestroyTexture : public MessageBase {
375 SurfaceFlinger* flinger;
376 GLuint name;
377 public:
378 MessageDestroyTexture(
379 SurfaceFlinger* flinger, GLuint name)
380 : flinger(flinger), name(name) { }
381 virtual bool handler() {
382 glDeleteTextures(1, &name);
383 return true;
384 }
385 };
386
Mathias Agopian999543b2009-06-23 18:08:22 -0700387 if (mTexture.name != -1U) {
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800388 // GL textures can only be destroyed from the GL thread
389 mLayer.mFlinger->mEventQueue.postMessage(
390 new MessageDestroyTexture(mLayer.mFlinger.get(), mTexture.name) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700392 if (mTexture.image != EGL_NO_IMAGE_KHR) {
393 EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
394 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396}
397
398void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
399{
400 ISurface::BufferHeap buffers;
401 { // scope for the lock
Mathias Agopianb34d1432009-09-08 20:02:47 -0700402 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 buffers = mBufferHeap;
404 if (buffers.heap != 0) {
405 const size_t memorySize = buffers.heap->getSize();
406 if ((size_t(offset) + mBufferSize) > memorySize) {
407 LOGE("LayerBuffer::BufferSource::postBuffer() "
408 "invalid buffer (offset=%d, size=%d, heap-size=%d",
409 int(offset), int(mBufferSize), int(memorySize));
410 return;
411 }
412 }
413 }
414
415 sp<Buffer> buffer;
416 if (buffers.heap != 0) {
417 buffer = new LayerBuffer::Buffer(buffers, offset);
418 if (buffer->getStatus() != NO_ERROR)
419 buffer.clear();
420 setBuffer(buffer);
421 mLayer.invalidate();
422 }
423}
424
425void LayerBuffer::BufferSource::unregisterBuffers()
426{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700427 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 mBufferHeap.heap.clear();
429 mBuffer.clear();
430 mLayer.invalidate();
431}
432
433sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
434{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700435 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 return mBuffer;
437}
438
439void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
440{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700441 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 mBuffer = buffer;
443}
444
445bool LayerBuffer::BufferSource::transformed() const
446{
447 return mBufferHeap.transform ? true : Source::transformed();
448}
449
450void LayerBuffer::BufferSource::onDraw(const Region& clip) const
451{
Mathias Agopian999543b2009-06-23 18:08:22 -0700452 sp<Buffer> ourBuffer(getBuffer());
453 if (UNLIKELY(ourBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 // nothing to do, we don't have a buffer
455 mLayer.clearWithOpenGL(clip);
456 return;
457 }
458
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700459 status_t err = NO_ERROR;
460 NativeBuffer src(ourBuffer->getBuffer());
Mathias Agopiana2804962009-09-08 23:52:08 -0700461 const Rect transformedBounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462
Mathias Agopiana1a17982010-01-21 16:27:30 -0800463 if (UNLIKELY(mTexture.name == -1LU)) {
464 mTexture.name = mLayer.createTexture();
465 }
466
Mathias Agopian9042b452009-10-26 20:12:37 -0700467#if defined(EGL_ANDROID_image_native_buffer)
468 if (mLayer.mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopian90daccf2009-11-05 23:08:00 -0800469 copybit_device_t* copybit = mLayer.mBlitEngine;
Mathias Agopian7323e542010-01-20 13:24:14 -0800470 if (copybit && ourBuffer->supportsCopybit()) {
Mathias Agopian90daccf2009-11-05 23:08:00 -0800471 // create our EGLImageKHR the first time
Mathias Agopian76169da2009-12-09 14:32:56 -0800472 err = initTempBuffer();
Mathias Agopian90daccf2009-11-05 23:08:00 -0800473 if (err == NO_ERROR) {
474 // NOTE: Assume the buffer is allocated with the proper USAGE flags
475 const NativeBuffer& dst(mTempBuffer);
476 region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b)));
477 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
478 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
479 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
480 err = copybit->stretch(copybit, &dst.img, &src.img,
481 &dst.crop, &src.crop, &clip);
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800482 if (err != NO_ERROR) {
483 clearTempBufferImage();
484 }
Mathias Agopian90daccf2009-11-05 23:08:00 -0800485 }
486 } else {
487 err = INVALID_OPERATION;
488 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700489 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700490#endif
Mathias Agopian9042b452009-10-26 20:12:37 -0700491 else {
492 err = INVALID_OPERATION;
493 }
494
495 if (err != NO_ERROR) {
Mathias Agopianf007a2f2009-10-28 21:00:29 -0700496 // slower fallback
Mathias Agopian999543b2009-06-23 18:08:22 -0700497 GGLSurface t;
498 t.version = sizeof(GGLSurface);
499 t.width = src.crop.r;
500 t.height = src.crop.b;
501 t.stride = src.img.w;
502 t.vstride= src.img.h;
503 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700504 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700505 const Region dirty(Rect(t.width, t.height));
Mathias Agopian6950e422009-10-05 17:07:12 -0700506 mLayer.loadTexture(&mTexture, dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
Mathias Agopian9042b452009-10-26 20:12:37 -0700509 mTexture.transform = mBufferHeap.transform;
510 mLayer.drawWithOpenGL(clip, mTexture);
511}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
Mathias Agopian76169da2009-12-09 14:32:56 -0800513status_t LayerBuffer::BufferSource::initTempBuffer() const
514{
515 // figure out the size we need now
516 const ISurface::BufferHeap& buffers(mBufferHeap);
517 uint32_t w = mLayer.mTransformedBounds.width();
518 uint32_t h = mLayer.mTransformedBounds.height();
519 if (buffers.w * h != buffers.h * w) {
520 int t = w; w = h; h = t;
521 }
522
Mathias Agopiand23fa272010-01-20 14:31:53 -0800523 // we're in the copybit case, so make sure we can handle this blit
524 // we don't have to keep the aspect ratio here
525 copybit_device_t* copybit = mLayer.mBlitEngine;
526 const int down = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
527 const int up = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
528 if (buffers.w > w*down) w = buffers.w / down;
529 else if (w > buffers.w*up) w = buffers.w*up;
530 if (buffers.h > h*down) h = buffers.h / down;
531 else if (h > buffers.h*up) h = buffers.h*up;
532
Mathias Agopian76169da2009-12-09 14:32:56 -0800533 if (mTexture.image != EGL_NO_IMAGE_KHR) {
534 // we have an EGLImage, make sure the needed size didn't change
535 if (w!=mTexture.width || h!= mTexture.height) {
536 // delete the EGLImage and texture
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800537 clearTempBufferImage();
Mathias Agopian76169da2009-12-09 14:32:56 -0800538 } else {
539 // we're good, we have an EGLImageKHR and it's (still) the
540 // right size
541 return NO_ERROR;
542 }
543 }
544
545 // figure out if we need linear filtering
546 if (buffers.w * h == buffers.h * w) {
547 // same pixel area, don't use filtering
548 mLayer.mUseLinearFiltering = false;
549 }
550
551 // Allocate a temporary buffer and create the corresponding EGLImageKHR
552
553 status_t err;
554 mTempGraphicBuffer.clear();
555 mTempGraphicBuffer = new GraphicBuffer(
556 w, h, HAL_PIXEL_FORMAT_RGB_565,
557 GraphicBuffer::USAGE_HW_TEXTURE |
558 GraphicBuffer::USAGE_HW_2D);
559
560 err = mTempGraphicBuffer->initCheck();
561 if (err == NO_ERROR) {
562 NativeBuffer& dst(mTempBuffer);
563 dst.img.w = mTempGraphicBuffer->getStride();
564 dst.img.h = h;
565 dst.img.format = mTempGraphicBuffer->getPixelFormat();
566 dst.img.handle = (native_handle_t *)mTempGraphicBuffer->handle;
567 dst.img.base = 0;
568 dst.crop.l = 0;
569 dst.crop.t = 0;
570 dst.crop.r = w;
571 dst.crop.b = h;
572
573 err = mLayer.initializeEglImage(
574 mTempGraphicBuffer, &mTexture);
575 // once the EGLImage has been created (whether it fails
576 // or not) we don't need the graphic buffer reference
577 // anymore.
578 mTempGraphicBuffer.clear();
579 }
580
581 return err;
582}
583
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800584void LayerBuffer::BufferSource::clearTempBufferImage() const
585{
Mathias Agopiana1a17982010-01-21 16:27:30 -0800586 // delete the image
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800587 EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800588 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopiana1a17982010-01-21 16:27:30 -0800589
590 // and the associated texture (recreate a name)
591 glDeleteTextures(1, &mTexture.name);
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800592 Texture defaultTexture;
593 mTexture = defaultTexture;
Mathias Agopiana1a17982010-01-21 16:27:30 -0800594 mTexture.name = mLayer.createTexture();
595
596 // and the associated buffer
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800597 mTempGraphicBuffer.clear();
598}
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600// ---------------------------------------------------------------------------
601
602LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
603 sp<OverlayRef>* overlayRef,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800604 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 : Source(layer), mVisibilityChanged(false),
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800606 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0), mOrientation(orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607{
608 overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
609 if (overlay_dev == NULL) {
610 // overlays not supported
611 return;
612 }
613
614 mOverlayDevice = overlay_dev;
615 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
616 if (overlay == NULL) {
617 // couldn't create the overlay (no memory? no more overlays?)
618 return;
619 }
620
621 // enable dithering...
622 overlay_dev->setParameter(overlay_dev, overlay,
623 OVERLAY_DITHER, OVERLAY_ENABLE);
624
625 mOverlay = overlay;
626 mWidth = overlay->w;
627 mHeight = overlay->h;
628 mFormat = overlay->format;
629 mWidthStride = overlay->w_stride;
630 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700631 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632
633 mOverlayHandle = overlay->getHandleRef(overlay);
634
Mathias Agopiana2804962009-09-08 23:52:08 -0700635 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636
637 *overlayRef = new OverlayRef(mOverlayHandle, channel,
638 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
Benny Wonge38212a2009-09-16 14:48:52 -0500639 mLayer.mFlinger->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640}
641
642LayerBuffer::OverlaySource::~OverlaySource()
643{
644 if (mOverlay && mOverlayDevice) {
645 overlay_control_device_t* overlay_dev = mOverlayDevice;
646 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
647 }
648}
649
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700650void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
651{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700652 // this would be where the color-key would be set, should we need it.
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700653 GLclampx red = 0;
654 GLclampx green = 0;
Mathias Agopian8ae03842009-09-15 19:31:28 -0700655 GLclampx blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700656 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700657}
658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
660{
661 const Layer::State& front(mLayer.drawingState());
662 const Layer::State& temp(mLayer.currentState());
663 if (temp.sequence != front.sequence) {
664 mVisibilityChanged = true;
665 }
666}
667
668void LayerBuffer::OverlaySource::onVisibilityResolved(
669 const Transform& planeTransform)
670{
671 // this code-path must be as tight as possible, it's called each time
672 // the screen is composited.
673 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700674 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700676 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700677 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 int x = bounds.left;
679 int y = bounds.top;
680 int w = bounds.width();
681 int h = bounds.height();
682
683 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700684 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 if (mOverlay) {
686 overlay_control_device_t* overlay_dev = mOverlayDevice;
687 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800688 // we need to combine the layer orientation and the
689 // user-requested orientation.
690 Transform finalTransform = Transform(mOrientation) *
691 Transform(mLayer.getOrientation());
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700692 overlay_dev->setParameter(overlay_dev, mOverlay,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800693 OVERLAY_TRANSFORM, finalTransform.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700694 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696 }
697 }
698}
699
Mathias Agopiana2804962009-09-08 23:52:08 -0700700void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701{
702 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700703 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700704 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 overlay_control_device_t* overlay_dev = mOverlayDevice;
706 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
707 mOverlay = 0;
708 }
709}
710
711// ---------------------------------------------------------------------------
712}; // namespace android