blob: fdf9abc81ada324010e3e4ada05a3e0495cfd0b3 [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
Mathias Agopian0b0722f2009-10-29 18:29:30 -070042gralloc_module_t const* LayerBuffer::sGrallocModule = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44// ---------------------------------------------------------------------------
45
46LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -070047 const sp<Client>& client)
48 : LayerBaseClient(flinger, display, client),
Mathias Agopian90daccf2009-11-05 23:08:00 -080049 mNeedsBlending(false), mBlitEngine(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050{
51}
52
53LayerBuffer::~LayerBuffer()
54{
Mathias Agopian90daccf2009-11-05 23:08:00 -080055 if (mBlitEngine) {
56 copybit_close(mBlitEngine);
57 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058}
59
Mathias Agopian6cf0db22009-04-17 19:36:26 -070060void LayerBuffer::onFirstRef()
61{
Mathias Agopianc6603952009-06-23 20:06:46 -070062 LayerBaseClient::onFirstRef();
Mathias Agopian593c05c2010-06-02 23:28:45 -070063 mSurface = new SurfaceLayerBuffer(mFlinger, this);
Mathias Agopian0b0722f2009-10-29 18:29:30 -070064
65 hw_module_t const* module = (hw_module_t const*)sGrallocModule;
66 if (!module) {
67 // NOTE: technically there is a race here, but it shouldn't
68 // cause any problem since hw_get_module() always returns
69 // the same value.
70 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
71 sGrallocModule = (gralloc_module_t const *)module;
72 }
73 }
Mathias Agopian90daccf2009-11-05 23:08:00 -080074
75 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
76 copybit_open(module, &mBlitEngine);
77 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -070078}
79
Mathias Agopian1473f462009-04-10 14:24:30 -070080sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081{
Mathias Agopian6cf0db22009-04-17 19:36:26 -070082 return mSurface;
83}
84
85status_t LayerBuffer::ditch()
86{
87 mSurface.clear();
88 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089}
90
91bool LayerBuffer::needsBlending() const {
92 return mNeedsBlending;
93}
94
95void LayerBuffer::setNeedsBlending(bool blending) {
96 mNeedsBlending = blending;
97}
98
99void LayerBuffer::postBuffer(ssize_t offset)
100{
101 sp<Source> source(getSource());
102 if (source != 0)
103 source->postBuffer(offset);
104}
105
106void LayerBuffer::unregisterBuffers()
107{
108 sp<Source> source(clearSource());
109 if (source != 0)
110 source->unregisterBuffers();
111}
112
113uint32_t LayerBuffer::doTransaction(uint32_t flags)
114{
115 sp<Source> source(getSource());
116 if (source != 0)
117 source->onTransaction(flags);
Mathias Agopian4c29c942009-11-19 14:46:26 -0800118 uint32_t res = LayerBase::doTransaction(flags);
119 // we always want filtering for these surfaces
Mathias Agopian92377032010-05-26 22:08:52 -0700120 mNeedsFiltering = !(mFlags & DisplayHardware::SLOW_CONFIG);
Mathias Agopian4c29c942009-11-19 14:46:26 -0800121 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122}
123
124void LayerBuffer::unlockPageFlip(const Transform& planeTransform,
125 Region& outDirtyRegion)
126{
127 // this code-path must be as tight as possible, it's called each time
128 // the screen is composited.
129 sp<Source> source(getSource());
130 if (source != 0)
131 source->onVisibilityResolved(planeTransform);
132 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
133}
134
135void LayerBuffer::onDraw(const Region& clip) const
136{
137 sp<Source> source(getSource());
138 if (LIKELY(source != 0)) {
139 source->onDraw(clip);
140 } else {
141 clearWithOpenGL(clip);
142 }
143}
144
Mathias Agopiana2804962009-09-08 23:52:08 -0700145void LayerBuffer::serverDestroy()
146{
147 sp<Source> source(clearSource());
148 if (source != 0) {
149 source->destroy();
150 }
151}
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153/**
154 * This creates a "buffer" source for this surface
155 */
156status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
157{
158 Mutex::Autolock _l(mLock);
159 if (mSource != 0)
160 return INVALID_OPERATION;
161
162 sp<BufferSource> source = new BufferSource(*this, buffers);
163
164 status_t result = source->getStatus();
165 if (result == NO_ERROR) {
166 mSource = source;
167 }
168 return result;
169}
170
171/**
172 * This creates an "overlay" source for this surface
173 */
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800174sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f,
175 int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176{
177 sp<OverlayRef> result;
178 Mutex::Autolock _l(mLock);
179 if (mSource != 0)
180 return result;
181
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800182 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 if (result != 0) {
184 mSource = source;
185 }
186 return result;
187}
188
189sp<LayerBuffer::Source> LayerBuffer::getSource() const {
190 Mutex::Autolock _l(mLock);
191 return mSource;
192}
193
194sp<LayerBuffer::Source> LayerBuffer::clearSource() {
195 sp<Source> source;
196 Mutex::Autolock _l(mLock);
197 source = mSource;
198 mSource.clear();
199 return source;
200}
201
202// ============================================================================
Mathias Agopian9779b2212009-09-07 16:32:45 -0700203// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204// ============================================================================
205
Mathias Agopian593c05c2010-06-02 23:28:45 -0700206LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(
207 const sp<SurfaceFlinger>& flinger, const sp<LayerBuffer>& owner)
208 : LayerBaseClient::Surface(flinger, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209{
210}
211
Mathias Agopian9779b2212009-09-07 16:32:45 -0700212LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213{
214 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215}
216
Mathias Agopian9779b2212009-09-07 16:32:45 -0700217status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700218 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219{
Mathias Agopian1473f462009-04-10 14:24:30 -0700220 sp<LayerBuffer> owner(getOwner());
221 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 return owner->registerBuffers(buffers);
223 return NO_INIT;
224}
225
Mathias Agopian9779b2212009-09-07 16:32:45 -0700226void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227{
Mathias Agopian1473f462009-04-10 14:24:30 -0700228 sp<LayerBuffer> owner(getOwner());
229 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 owner->postBuffer(offset);
231}
232
Mathias Agopian9779b2212009-09-07 16:32:45 -0700233void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234{
Mathias Agopian1473f462009-04-10 14:24:30 -0700235 sp<LayerBuffer> owner(getOwner());
236 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 owner->unregisterBuffers();
238}
239
Mathias Agopian9779b2212009-09-07 16:32:45 -0700240sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800241 uint32_t w, uint32_t h, int32_t format, int32_t orientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700243 sp<LayerBuffer> owner(getOwner());
244 if (owner != 0)
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800245 result = owner->createOverlay(w, h, format, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 return result;
247}
248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249// ============================================================================
250// LayerBuffer::Buffer
251// ============================================================================
252
Mathias Agopian1d211f82010-03-08 11:14:20 -0800253LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers,
254 ssize_t offset, size_t bufferSize)
Mathias Agopian7323e542010-01-20 13:24:14 -0800255 : mBufferHeap(buffers), mSupportsCopybit(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256{
257 NativeBuffer& src(mNativeBuffer);
Mathias Agopiand86beb92009-12-02 16:23:11 -0800258 src.crop.l = 0;
259 src.crop.t = 0;
260 src.crop.r = buffers.w;
261 src.crop.b = buffers.h;
262
263 src.img.w = buffers.hor_stride ?: buffers.w;
264 src.img.h = buffers.ver_stride ?: buffers.h;
265 src.img.format = buffers.format;
266 src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset);
267 src.img.handle = 0;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700268
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700269 gralloc_module_t const * module = LayerBuffer::getGrallocModule();
270 if (module && module->perform) {
271 int err = module->perform(module,
272 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER,
Mathias Agopian1d211f82010-03-08 11:14:20 -0800273 buffers.heap->heapID(), bufferSize,
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700274 offset, buffers.heap->base(),
275 &src.img.handle);
276
Mathias Agopian7323e542010-01-20 13:24:14 -0800277 // we can fail here is the passed buffer is purely software
278 mSupportsCopybit = (err == NO_ERROR);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700279 }
Mathias Agopiand86beb92009-12-02 16:23:11 -0800280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
282LayerBuffer::Buffer::~Buffer()
283{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700284 NativeBuffer& src(mNativeBuffer);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700285 if (src.img.handle) {
286 native_handle_delete(src.img.handle);
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288}
289
290// ============================================================================
291// LayerBuffer::Source
292// LayerBuffer::BufferSource
293// LayerBuffer::OverlaySource
294// ============================================================================
295
296LayerBuffer::Source::Source(LayerBuffer& layer)
297 : mLayer(layer)
298{
299}
300LayerBuffer::Source::~Source() {
301}
302void LayerBuffer::Source::onDraw(const Region& clip) const {
303}
304void LayerBuffer::Source::onTransaction(uint32_t flags) {
305}
306void LayerBuffer::Source::onVisibilityResolved(
307 const Transform& planeTransform) {
308}
309void LayerBuffer::Source::postBuffer(ssize_t offset) {
310}
311void LayerBuffer::Source::unregisterBuffers() {
312}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314// ---------------------------------------------------------------------------
315
316LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
317 const ISurface::BufferHeap& buffers)
Mathias Agopian781953d2010-06-25 18:02:21 -0700318 : Source(layer), mStatus(NO_ERROR), mBufferSize(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319{
320 if (buffers.heap == NULL) {
321 // this is allowed, but in this case, it is illegal to receive
322 // postBuffer(). The surface just erases the framebuffer with
323 // fully transparent pixels.
324 mBufferHeap = buffers;
325 mLayer.setNeedsBlending(false);
326 return;
327 }
328
329 status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
330 if (err != NO_ERROR) {
331 LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
332 mStatus = err;
333 return;
334 }
335
336 PixelFormatInfo info;
337 err = getPixelFormatInfo(buffers.format, &info);
338 if (err != NO_ERROR) {
339 LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
340 buffers.format, strerror(err));
341 mStatus = err;
342 return;
343 }
344
345 if (buffers.hor_stride<0 || buffers.ver_stride<0) {
346 LOGE("LayerBuffer::BufferSource: invalid parameters "
347 "(w=%d, h=%d, xs=%d, ys=%d)",
348 buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
349 mStatus = BAD_VALUE;
350 return;
351 }
352
353 mBufferHeap = buffers;
354 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
355 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
356 mLayer.forceVisibilityTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357}
358
359LayerBuffer::BufferSource::~BufferSource()
360{
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800361 class MessageDestroyTexture : public MessageBase {
362 SurfaceFlinger* flinger;
363 GLuint name;
364 public:
365 MessageDestroyTexture(
366 SurfaceFlinger* flinger, GLuint name)
367 : flinger(flinger), name(name) { }
368 virtual bool handler() {
369 glDeleteTextures(1, &name);
370 return true;
371 }
372 };
373
Mathias Agopian999543b2009-06-23 18:08:22 -0700374 if (mTexture.name != -1U) {
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800375 // GL textures can only be destroyed from the GL thread
Mathias Agopian781953d2010-06-25 18:02:21 -0700376 getFlinger()->mEventQueue.postMessage(
377 new MessageDestroyTexture(getFlinger(), mTexture.name) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700379 if (mTexture.image != EGL_NO_IMAGE_KHR) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700380 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopian9042b452009-10-26 20:12:37 -0700381 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383}
384
385void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
386{
387 ISurface::BufferHeap buffers;
388 { // scope for the lock
Mathias Agopianb34d1432009-09-08 20:02:47 -0700389 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 buffers = mBufferHeap;
391 if (buffers.heap != 0) {
392 const size_t memorySize = buffers.heap->getSize();
393 if ((size_t(offset) + mBufferSize) > memorySize) {
394 LOGE("LayerBuffer::BufferSource::postBuffer() "
395 "invalid buffer (offset=%d, size=%d, heap-size=%d",
396 int(offset), int(mBufferSize), int(memorySize));
397 return;
398 }
399 }
400 }
401
402 sp<Buffer> buffer;
403 if (buffers.heap != 0) {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800404 buffer = new LayerBuffer::Buffer(buffers, offset, mBufferSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 if (buffer->getStatus() != NO_ERROR)
406 buffer.clear();
407 setBuffer(buffer);
408 mLayer.invalidate();
409 }
410}
411
412void LayerBuffer::BufferSource::unregisterBuffers()
413{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700414 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mBufferHeap.heap.clear();
416 mBuffer.clear();
417 mLayer.invalidate();
418}
419
420sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
421{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700422 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 return mBuffer;
424}
425
426void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
427{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700428 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 mBuffer = buffer;
430}
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432void LayerBuffer::BufferSource::onDraw(const Region& clip) const
433{
Mathias Agopian999543b2009-06-23 18:08:22 -0700434 sp<Buffer> ourBuffer(getBuffer());
435 if (UNLIKELY(ourBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 // nothing to do, we don't have a buffer
437 mLayer.clearWithOpenGL(clip);
438 return;
439 }
440
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700441 status_t err = NO_ERROR;
442 NativeBuffer src(ourBuffer->getBuffer());
Mathias Agopiana2804962009-09-08 23:52:08 -0700443 const Rect transformedBounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
Mathias Agopian9042b452009-10-26 20:12:37 -0700445#if defined(EGL_ANDROID_image_native_buffer)
Mathias Agopian781953d2010-06-25 18:02:21 -0700446 if (GLExtensions::getInstance().haveDirectTexture()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800447 err = INVALID_OPERATION;
448 if (ourBuffer->supportsCopybit()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800449 copybit_device_t* copybit = mLayer.mBlitEngine;
450 if (copybit && err != NO_ERROR) {
451 // create our EGLImageKHR the first time
452 err = initTempBuffer();
453 if (err == NO_ERROR) {
454 // NOTE: Assume the buffer is allocated with the proper USAGE flags
455 const NativeBuffer& dst(mTempBuffer);
456 region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b)));
457 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
458 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
459 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
460 err = copybit->stretch(copybit, &dst.img, &src.img,
461 &dst.crop, &src.crop, &clip);
462 if (err != NO_ERROR) {
463 clearTempBufferImage();
464 }
465 }
466 }
Mathias Agopian90daccf2009-11-05 23:08:00 -0800467 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700468 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700469#endif
Mathias Agopian9042b452009-10-26 20:12:37 -0700470 else {
471 err = INVALID_OPERATION;
472 }
473
474 if (err != NO_ERROR) {
Mathias Agopianf007a2f2009-10-28 21:00:29 -0700475 // slower fallback
Mathias Agopian999543b2009-06-23 18:08:22 -0700476 GGLSurface t;
477 t.version = sizeof(GGLSurface);
478 t.width = src.crop.r;
479 t.height = src.crop.b;
480 t.stride = src.img.w;
481 t.vstride= src.img.h;
482 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700483 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700484 const Region dirty(Rect(t.width, t.height));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700485 mTextureManager.loadTexture(&mTexture, dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700488 mLayer.setBufferTransform(mBufferHeap.transform);
Mathias Agopian9042b452009-10-26 20:12:37 -0700489 mLayer.drawWithOpenGL(clip, mTexture);
490}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
Mathias Agopian76169da2009-12-09 14:32:56 -0800492status_t LayerBuffer::BufferSource::initTempBuffer() const
493{
494 // figure out the size we need now
495 const ISurface::BufferHeap& buffers(mBufferHeap);
496 uint32_t w = mLayer.mTransformedBounds.width();
497 uint32_t h = mLayer.mTransformedBounds.height();
Omprakash Dhyade92bd90d2010-08-05 16:28:37 -0700498 if (mLayer.getOrientation() & (Transform::ROT_90 | Transform::ROT_270)) {
Mathias Agopian76169da2009-12-09 14:32:56 -0800499 int t = w; w = h; h = t;
500 }
501
Mathias Agopiand23fa272010-01-20 14:31:53 -0800502 // we're in the copybit case, so make sure we can handle this blit
503 // we don't have to keep the aspect ratio here
504 copybit_device_t* copybit = mLayer.mBlitEngine;
505 const int down = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
506 const int up = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
507 if (buffers.w > w*down) w = buffers.w / down;
508 else if (w > buffers.w*up) w = buffers.w*up;
509 if (buffers.h > h*down) h = buffers.h / down;
510 else if (h > buffers.h*up) h = buffers.h*up;
511
Mathias Agopian76169da2009-12-09 14:32:56 -0800512 if (mTexture.image != EGL_NO_IMAGE_KHR) {
513 // we have an EGLImage, make sure the needed size didn't change
514 if (w!=mTexture.width || h!= mTexture.height) {
515 // delete the EGLImage and texture
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800516 clearTempBufferImage();
Mathias Agopian76169da2009-12-09 14:32:56 -0800517 } 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
Mathias Agopian92377032010-05-26 22:08:52 -0700527 mLayer.mNeedsFiltering = false;
Mathias Agopian76169da2009-12-09 14:32:56 -0800528 }
529
530 // Allocate a temporary buffer and create the corresponding EGLImageKHR
Mathias Agopian08956f02010-02-12 18:50:28 -0800531 // once the EGLImage has been created we don't need the
532 // graphic buffer reference anymore.
533 sp<GraphicBuffer> buffer = new GraphicBuffer(
Mathias Agopian76169da2009-12-09 14:32:56 -0800534 w, h, HAL_PIXEL_FORMAT_RGB_565,
535 GraphicBuffer::USAGE_HW_TEXTURE |
536 GraphicBuffer::USAGE_HW_2D);
537
Mathias Agopian08956f02010-02-12 18:50:28 -0800538 status_t err = buffer->initCheck();
Mathias Agopian76169da2009-12-09 14:32:56 -0800539 if (err == NO_ERROR) {
540 NativeBuffer& dst(mTempBuffer);
Mathias Agopian08956f02010-02-12 18:50:28 -0800541 dst.img.w = buffer->getStride();
Mathias Agopian76169da2009-12-09 14:32:56 -0800542 dst.img.h = h;
Mathias Agopian08956f02010-02-12 18:50:28 -0800543 dst.img.format = buffer->getPixelFormat();
544 dst.img.handle = (native_handle_t *)buffer->handle;
Mathias Agopian76169da2009-12-09 14:32:56 -0800545 dst.img.base = 0;
546 dst.crop.l = 0;
547 dst.crop.t = 0;
548 dst.crop.r = w;
549 dst.crop.b = h;
550
Mathias Agopian781953d2010-06-25 18:02:21 -0700551 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700552 err = mTextureManager.initEglImage(&mTexture, dpy, buffer);
Mathias Agopian76169da2009-12-09 14:32:56 -0800553 }
554
555 return err;
556}
557
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800558void LayerBuffer::BufferSource::clearTempBufferImage() const
559{
Mathias Agopiana1a17982010-01-21 16:27:30 -0800560 // delete the image
Mathias Agopian781953d2010-06-25 18:02:21 -0700561 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800562 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopiana1a17982010-01-21 16:27:30 -0800563
564 // and the associated texture (recreate a name)
565 glDeleteTextures(1, &mTexture.name);
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800566 Texture defaultTexture;
567 mTexture = defaultTexture;
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800568}
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570// ---------------------------------------------------------------------------
571
572LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
573 sp<OverlayRef>* overlayRef,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800574 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 : Source(layer), mVisibilityChanged(false),
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800576 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0), mOrientation(orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577{
Mathias Agopian781953d2010-06-25 18:02:21 -0700578 overlay_control_device_t* overlay_dev = getFlinger()->getOverlayEngine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 if (overlay_dev == NULL) {
580 // overlays not supported
581 return;
582 }
583
584 mOverlayDevice = overlay_dev;
585 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
586 if (overlay == NULL) {
587 // couldn't create the overlay (no memory? no more overlays?)
588 return;
589 }
590
591 // enable dithering...
592 overlay_dev->setParameter(overlay_dev, overlay,
593 OVERLAY_DITHER, OVERLAY_ENABLE);
594
595 mOverlay = overlay;
596 mWidth = overlay->w;
597 mHeight = overlay->h;
598 mFormat = overlay->format;
599 mWidthStride = overlay->w_stride;
600 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700601 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602
603 mOverlayHandle = overlay->getHandleRef(overlay);
604
Mathias Agopiana2804962009-09-08 23:52:08 -0700605 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
607 *overlayRef = new OverlayRef(mOverlayHandle, channel,
608 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
Mathias Agopian781953d2010-06-25 18:02:21 -0700609 getFlinger()->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610}
611
612LayerBuffer::OverlaySource::~OverlaySource()
613{
614 if (mOverlay && mOverlayDevice) {
615 overlay_control_device_t* overlay_dev = mOverlayDevice;
616 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
617 }
618}
619
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700620void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
621{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700622 // this would be where the color-key would be set, should we need it.
Mathias Agopian0d3c0062010-05-26 22:26:12 -0700623 GLclampf red = 0;
624 GLclampf green = 0;
625 GLclampf blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700626 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700627}
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
630{
631 const Layer::State& front(mLayer.drawingState());
632 const Layer::State& temp(mLayer.currentState());
633 if (temp.sequence != front.sequence) {
634 mVisibilityChanged = true;
635 }
636}
637
638void LayerBuffer::OverlaySource::onVisibilityResolved(
639 const Transform& planeTransform)
640{
641 // this code-path must be as tight as possible, it's called each time
642 // the screen is composited.
643 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700644 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700646 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700647 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 int x = bounds.left;
649 int y = bounds.top;
650 int w = bounds.width();
651 int h = bounds.height();
652
653 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700654 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 if (mOverlay) {
656 overlay_control_device_t* overlay_dev = mOverlayDevice;
657 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800658 // we need to combine the layer orientation and the
659 // user-requested orientation.
660 Transform finalTransform = Transform(mOrientation) *
661 Transform(mLayer.getOrientation());
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700662 overlay_dev->setParameter(overlay_dev, mOverlay,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800663 OVERLAY_TRANSFORM, finalTransform.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700664 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 }
667 }
668}
669
Mathias Agopiana2804962009-09-08 23:52:08 -0700670void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671{
672 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700673 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700674 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 overlay_control_device_t* overlay_dev = mOverlayDevice;
676 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
677 mOverlay = 0;
678 }
679}
680
681// ---------------------------------------------------------------------------
682}; // namespace android