blob: 55d859dc1b392d4188f481ddd3ae0c067ee41986 [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) {
Ichitaro Koharafbce6ac2011-02-17 12:31:23 +010096 if (mNeedsBlending != blending) {
97 mFlinger->invalidateLayerVisibility(this);
98 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 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 Agopian92377032010-05-26 22:08:52 -0700123 mNeedsFiltering = !(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
Mathias Agopianc57b15c2010-10-24 19:03:58 -0700138void LayerBuffer::validateVisibility(const Transform& globalTransform)
139{
140 sp<Source> source(getSource());
141 if (source != 0)
142 source->onvalidateVisibility(globalTransform);
143 LayerBase::validateVisibility(globalTransform);
144}
145
Mathias Agopian597c7f62010-09-29 13:02:36 -0700146void LayerBuffer::drawForSreenShot() const
147{
148 const DisplayHardware& hw(graphicPlane(0).displayHardware());
149 clearWithOpenGL( Region(hw.bounds()) );
150}
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152void LayerBuffer::onDraw(const Region& clip) const
153{
154 sp<Source> source(getSource());
155 if (LIKELY(source != 0)) {
156 source->onDraw(clip);
157 } else {
158 clearWithOpenGL(clip);
159 }
160}
161
Mathias Agopiana2804962009-09-08 23:52:08 -0700162void LayerBuffer::serverDestroy()
163{
164 sp<Source> source(clearSource());
165 if (source != 0) {
166 source->destroy();
167 }
168}
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170/**
171 * This creates a "buffer" source for this surface
172 */
173status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
174{
175 Mutex::Autolock _l(mLock);
176 if (mSource != 0)
177 return INVALID_OPERATION;
178
179 sp<BufferSource> source = new BufferSource(*this, buffers);
180
181 status_t result = source->getStatus();
182 if (result == NO_ERROR) {
183 mSource = source;
184 }
185 return result;
186}
187
188/**
189 * This creates an "overlay" source for this surface
190 */
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800191sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f,
192 int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193{
194 sp<OverlayRef> result;
195 Mutex::Autolock _l(mLock);
196 if (mSource != 0)
197 return result;
198
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800199 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 if (result != 0) {
201 mSource = source;
202 }
203 return result;
204}
205
206sp<LayerBuffer::Source> LayerBuffer::getSource() const {
207 Mutex::Autolock _l(mLock);
208 return mSource;
209}
210
211sp<LayerBuffer::Source> LayerBuffer::clearSource() {
212 sp<Source> source;
213 Mutex::Autolock _l(mLock);
214 source = mSource;
215 mSource.clear();
216 return source;
217}
218
219// ============================================================================
Mathias Agopian9779b2212009-09-07 16:32:45 -0700220// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221// ============================================================================
222
Mathias Agopian593c05c2010-06-02 23:28:45 -0700223LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(
224 const sp<SurfaceFlinger>& flinger, const sp<LayerBuffer>& owner)
225 : LayerBaseClient::Surface(flinger, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226{
227}
228
Mathias Agopian9779b2212009-09-07 16:32:45 -0700229LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230{
231 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232}
233
Mathias Agopian9779b2212009-09-07 16:32:45 -0700234status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700235 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236{
Mathias Agopian1473f462009-04-10 14:24:30 -0700237 sp<LayerBuffer> owner(getOwner());
238 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 return owner->registerBuffers(buffers);
240 return NO_INIT;
241}
242
Mathias Agopian9779b2212009-09-07 16:32:45 -0700243void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
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->postBuffer(offset);
248}
249
Mathias Agopian9779b2212009-09-07 16:32:45 -0700250void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251{
Mathias Agopian1473f462009-04-10 14:24:30 -0700252 sp<LayerBuffer> owner(getOwner());
253 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 owner->unregisterBuffers();
255}
256
Mathias Agopian9779b2212009-09-07 16:32:45 -0700257sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800258 uint32_t w, uint32_t h, int32_t format, int32_t orientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700260 sp<LayerBuffer> owner(getOwner());
261 if (owner != 0)
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800262 result = owner->createOverlay(w, h, format, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return result;
264}
265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266// ============================================================================
267// LayerBuffer::Buffer
268// ============================================================================
269
Mathias Agopian1d211f82010-03-08 11:14:20 -0800270LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers,
271 ssize_t offset, size_t bufferSize)
Mathias Agopian7323e542010-01-20 13:24:14 -0800272 : mBufferHeap(buffers), mSupportsCopybit(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273{
274 NativeBuffer& src(mNativeBuffer);
Mathias Agopiand86beb92009-12-02 16:23:11 -0800275 src.crop.l = 0;
276 src.crop.t = 0;
277 src.crop.r = buffers.w;
278 src.crop.b = buffers.h;
279
280 src.img.w = buffers.hor_stride ?: buffers.w;
281 src.img.h = buffers.ver_stride ?: buffers.h;
282 src.img.format = buffers.format;
283 src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset);
284 src.img.handle = 0;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700285
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700286 gralloc_module_t const * module = LayerBuffer::getGrallocModule();
287 if (module && module->perform) {
288 int err = module->perform(module,
289 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER,
Mathias Agopian1d211f82010-03-08 11:14:20 -0800290 buffers.heap->heapID(), bufferSize,
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700291 offset, buffers.heap->base(),
292 &src.img.handle);
293
Mathias Agopian7323e542010-01-20 13:24:14 -0800294 // we can fail here is the passed buffer is purely software
295 mSupportsCopybit = (err == NO_ERROR);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700296 }
Mathias Agopiand86beb92009-12-02 16:23:11 -0800297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
299LayerBuffer::Buffer::~Buffer()
300{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700301 NativeBuffer& src(mNativeBuffer);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700302 if (src.img.handle) {
303 native_handle_delete(src.img.handle);
304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305}
306
307// ============================================================================
308// LayerBuffer::Source
309// LayerBuffer::BufferSource
310// LayerBuffer::OverlaySource
311// ============================================================================
312
313LayerBuffer::Source::Source(LayerBuffer& layer)
314 : mLayer(layer)
315{
316}
317LayerBuffer::Source::~Source() {
318}
319void LayerBuffer::Source::onDraw(const Region& clip) const {
320}
321void LayerBuffer::Source::onTransaction(uint32_t flags) {
322}
323void LayerBuffer::Source::onVisibilityResolved(
324 const Transform& planeTransform) {
325}
326void LayerBuffer::Source::postBuffer(ssize_t offset) {
327}
328void LayerBuffer::Source::unregisterBuffers() {
329}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
331// ---------------------------------------------------------------------------
332
333LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
334 const ISurface::BufferHeap& buffers)
Mathias Agopian781953d2010-06-25 18:02:21 -0700335 : Source(layer), mStatus(NO_ERROR), mBufferSize(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336{
337 if (buffers.heap == NULL) {
338 // this is allowed, but in this case, it is illegal to receive
339 // postBuffer(). The surface just erases the framebuffer with
340 // fully transparent pixels.
341 mBufferHeap = buffers;
342 mLayer.setNeedsBlending(false);
343 return;
344 }
345
346 status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
347 if (err != NO_ERROR) {
348 LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
349 mStatus = err;
350 return;
351 }
352
353 PixelFormatInfo info;
354 err = getPixelFormatInfo(buffers.format, &info);
355 if (err != NO_ERROR) {
356 LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
357 buffers.format, strerror(err));
358 mStatus = err;
359 return;
360 }
361
362 if (buffers.hor_stride<0 || buffers.ver_stride<0) {
363 LOGE("LayerBuffer::BufferSource: invalid parameters "
364 "(w=%d, h=%d, xs=%d, ys=%d)",
365 buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
366 mStatus = BAD_VALUE;
367 return;
368 }
369
370 mBufferHeap = buffers;
371 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
372 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
373 mLayer.forceVisibilityTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374}
375
376LayerBuffer::BufferSource::~BufferSource()
377{
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800378 class MessageDestroyTexture : public MessageBase {
379 SurfaceFlinger* flinger;
380 GLuint name;
381 public:
382 MessageDestroyTexture(
383 SurfaceFlinger* flinger, GLuint name)
384 : flinger(flinger), name(name) { }
385 virtual bool handler() {
386 glDeleteTextures(1, &name);
387 return true;
388 }
389 };
390
Mathias Agopian999543b2009-06-23 18:08:22 -0700391 if (mTexture.name != -1U) {
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800392 // GL textures can only be destroyed from the GL thread
Mathias Agopian781953d2010-06-25 18:02:21 -0700393 getFlinger()->mEventQueue.postMessage(
394 new MessageDestroyTexture(getFlinger(), mTexture.name) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700396 if (mTexture.image != EGL_NO_IMAGE_KHR) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700397 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopian9042b452009-10-26 20:12:37 -0700398 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400}
401
402void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
403{
404 ISurface::BufferHeap buffers;
405 { // scope for the lock
Mathias Agopianb34d1432009-09-08 20:02:47 -0700406 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 buffers = mBufferHeap;
408 if (buffers.heap != 0) {
409 const size_t memorySize = buffers.heap->getSize();
410 if ((size_t(offset) + mBufferSize) > memorySize) {
411 LOGE("LayerBuffer::BufferSource::postBuffer() "
412 "invalid buffer (offset=%d, size=%d, heap-size=%d",
413 int(offset), int(mBufferSize), int(memorySize));
414 return;
415 }
416 }
417 }
418
419 sp<Buffer> buffer;
420 if (buffers.heap != 0) {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800421 buffer = new LayerBuffer::Buffer(buffers, offset, mBufferSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 if (buffer->getStatus() != NO_ERROR)
423 buffer.clear();
424 setBuffer(buffer);
425 mLayer.invalidate();
426 }
427}
428
429void LayerBuffer::BufferSource::unregisterBuffers()
430{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700431 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 mBufferHeap.heap.clear();
433 mBuffer.clear();
434 mLayer.invalidate();
435}
436
437sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
438{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700439 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 return mBuffer;
441}
442
443void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
444{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700445 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 mBuffer = buffer;
447}
448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449void LayerBuffer::BufferSource::onDraw(const Region& clip) const
450{
Mathias Agopian999543b2009-06-23 18:08:22 -0700451 sp<Buffer> ourBuffer(getBuffer());
452 if (UNLIKELY(ourBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 // nothing to do, we don't have a buffer
454 mLayer.clearWithOpenGL(clip);
455 return;
456 }
457
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700458 status_t err = NO_ERROR;
459 NativeBuffer src(ourBuffer->getBuffer());
Mathias Agopiana2804962009-09-08 23:52:08 -0700460 const Rect transformedBounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461
Mathias Agopian9042b452009-10-26 20:12:37 -0700462#if defined(EGL_ANDROID_image_native_buffer)
Mathias Agopian781953d2010-06-25 18:02:21 -0700463 if (GLExtensions::getInstance().haveDirectTexture()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800464 err = INVALID_OPERATION;
465 if (ourBuffer->supportsCopybit()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800466 copybit_device_t* copybit = mLayer.mBlitEngine;
467 if (copybit && err != NO_ERROR) {
468 // create our EGLImageKHR the first time
469 err = initTempBuffer();
470 if (err == NO_ERROR) {
471 // NOTE: Assume the buffer is allocated with the proper USAGE flags
472 const NativeBuffer& dst(mTempBuffer);
473 region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b)));
474 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
475 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
476 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
477 err = copybit->stretch(copybit, &dst.img, &src.img,
478 &dst.crop, &src.crop, &clip);
479 if (err != NO_ERROR) {
480 clearTempBufferImage();
481 }
482 }
483 }
Mathias Agopian90daccf2009-11-05 23:08:00 -0800484 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700485 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700486#endif
Mathias Agopian9042b452009-10-26 20:12:37 -0700487 else {
488 err = INVALID_OPERATION;
489 }
490
491 if (err != NO_ERROR) {
Mathias Agopianf007a2f2009-10-28 21:00:29 -0700492 // slower fallback
Mathias Agopian999543b2009-06-23 18:08:22 -0700493 GGLSurface t;
494 t.version = sizeof(GGLSurface);
495 t.width = src.crop.r;
496 t.height = src.crop.b;
497 t.stride = src.img.w;
498 t.vstride= src.img.h;
499 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700500 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700501 const Region dirty(Rect(t.width, t.height));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700502 mTextureManager.loadTexture(&mTexture, dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700505 mLayer.setBufferTransform(mBufferHeap.transform);
Mathias Agopian9042b452009-10-26 20:12:37 -0700506 mLayer.drawWithOpenGL(clip, mTexture);
507}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
Mathias Agopian76169da2009-12-09 14:32:56 -0800509status_t LayerBuffer::BufferSource::initTempBuffer() const
510{
511 // figure out the size we need now
512 const ISurface::BufferHeap& buffers(mBufferHeap);
513 uint32_t w = mLayer.mTransformedBounds.width();
514 uint32_t h = mLayer.mTransformedBounds.height();
Omprakash Dhyade92bd90d2010-08-05 16:28:37 -0700515 if (mLayer.getOrientation() & (Transform::ROT_90 | Transform::ROT_270)) {
Mathias Agopian76169da2009-12-09 14:32:56 -0800516 int t = w; w = h; h = t;
517 }
518
Mathias Agopiand23fa272010-01-20 14:31:53 -0800519 // we're in the copybit case, so make sure we can handle this blit
520 // we don't have to keep the aspect ratio here
521 copybit_device_t* copybit = mLayer.mBlitEngine;
522 const int down = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
523 const int up = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
524 if (buffers.w > w*down) w = buffers.w / down;
525 else if (w > buffers.w*up) w = buffers.w*up;
526 if (buffers.h > h*down) h = buffers.h / down;
527 else if (h > buffers.h*up) h = buffers.h*up;
528
Mathias Agopian76169da2009-12-09 14:32:56 -0800529 if (mTexture.image != EGL_NO_IMAGE_KHR) {
530 // we have an EGLImage, make sure the needed size didn't change
531 if (w!=mTexture.width || h!= mTexture.height) {
532 // delete the EGLImage and texture
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800533 clearTempBufferImage();
Mathias Agopian76169da2009-12-09 14:32:56 -0800534 } else {
535 // we're good, we have an EGLImageKHR and it's (still) the
536 // right size
537 return NO_ERROR;
538 }
539 }
540
541 // figure out if we need linear filtering
542 if (buffers.w * h == buffers.h * w) {
543 // same pixel area, don't use filtering
Mathias Agopian92377032010-05-26 22:08:52 -0700544 mLayer.mNeedsFiltering = false;
Mathias Agopian76169da2009-12-09 14:32:56 -0800545 }
546
547 // Allocate a temporary buffer and create the corresponding EGLImageKHR
Mathias Agopian08956f02010-02-12 18:50:28 -0800548 // once the EGLImage has been created we don't need the
549 // graphic buffer reference anymore.
550 sp<GraphicBuffer> buffer = new GraphicBuffer(
Mathias Agopian76169da2009-12-09 14:32:56 -0800551 w, h, HAL_PIXEL_FORMAT_RGB_565,
552 GraphicBuffer::USAGE_HW_TEXTURE |
553 GraphicBuffer::USAGE_HW_2D);
554
Mathias Agopian08956f02010-02-12 18:50:28 -0800555 status_t err = buffer->initCheck();
Mathias Agopian76169da2009-12-09 14:32:56 -0800556 if (err == NO_ERROR) {
557 NativeBuffer& dst(mTempBuffer);
Mathias Agopian08956f02010-02-12 18:50:28 -0800558 dst.img.w = buffer->getStride();
Mathias Agopian76169da2009-12-09 14:32:56 -0800559 dst.img.h = h;
Mathias Agopian08956f02010-02-12 18:50:28 -0800560 dst.img.format = buffer->getPixelFormat();
561 dst.img.handle = (native_handle_t *)buffer->handle;
Mathias Agopian76169da2009-12-09 14:32:56 -0800562 dst.img.base = 0;
563 dst.crop.l = 0;
564 dst.crop.t = 0;
565 dst.crop.r = w;
566 dst.crop.b = h;
567
Mathias Agopian781953d2010-06-25 18:02:21 -0700568 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700569 err = mTextureManager.initEglImage(&mTexture, dpy, buffer);
Mathias Agopian76169da2009-12-09 14:32:56 -0800570 }
571
572 return err;
573}
574
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800575void LayerBuffer::BufferSource::clearTempBufferImage() const
576{
Mathias Agopiana1a17982010-01-21 16:27:30 -0800577 // delete the image
Mathias Agopian781953d2010-06-25 18:02:21 -0700578 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800579 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopiana1a17982010-01-21 16:27:30 -0800580
581 // and the associated texture (recreate a name)
582 glDeleteTextures(1, &mTexture.name);
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800583 Texture defaultTexture;
584 mTexture = defaultTexture;
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800585}
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587// ---------------------------------------------------------------------------
588
589LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
590 sp<OverlayRef>* overlayRef,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800591 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 : Source(layer), mVisibilityChanged(false),
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800593 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0), mOrientation(orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594{
Mathias Agopian781953d2010-06-25 18:02:21 -0700595 overlay_control_device_t* overlay_dev = getFlinger()->getOverlayEngine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 if (overlay_dev == NULL) {
597 // overlays not supported
598 return;
599 }
600
601 mOverlayDevice = overlay_dev;
602 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
603 if (overlay == NULL) {
604 // couldn't create the overlay (no memory? no more overlays?)
605 return;
606 }
607
608 // enable dithering...
609 overlay_dev->setParameter(overlay_dev, overlay,
610 OVERLAY_DITHER, OVERLAY_ENABLE);
611
612 mOverlay = overlay;
613 mWidth = overlay->w;
614 mHeight = overlay->h;
615 mFormat = overlay->format;
616 mWidthStride = overlay->w_stride;
617 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700618 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
620 mOverlayHandle = overlay->getHandleRef(overlay);
621
Mathias Agopiana2804962009-09-08 23:52:08 -0700622 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 *overlayRef = new OverlayRef(mOverlayHandle, channel,
625 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
Mathias Agopian781953d2010-06-25 18:02:21 -0700626 getFlinger()->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627}
628
629LayerBuffer::OverlaySource::~OverlaySource()
630{
631 if (mOverlay && mOverlayDevice) {
632 overlay_control_device_t* overlay_dev = mOverlayDevice;
633 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
634 }
635}
636
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700637void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
638{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700639 // this would be where the color-key would be set, should we need it.
Mathias Agopian0d3c0062010-05-26 22:26:12 -0700640 GLclampf red = 0;
641 GLclampf green = 0;
642 GLclampf blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700643 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700644}
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
647{
648 const Layer::State& front(mLayer.drawingState());
649 const Layer::State& temp(mLayer.currentState());
650 if (temp.sequence != front.sequence) {
651 mVisibilityChanged = true;
652 }
653}
654
Mathias Agopianc57b15c2010-10-24 19:03:58 -0700655void LayerBuffer::OverlaySource::onvalidateVisibility(const Transform&)
656{
657 mVisibilityChanged = true;
658}
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660void LayerBuffer::OverlaySource::onVisibilityResolved(
661 const Transform& planeTransform)
662{
663 // this code-path must be as tight as possible, it's called each time
664 // the screen is composited.
665 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700666 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700668 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700669 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 int x = bounds.left;
671 int y = bounds.top;
672 int w = bounds.width();
673 int h = bounds.height();
674
675 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700676 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 if (mOverlay) {
678 overlay_control_device_t* overlay_dev = mOverlayDevice;
679 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800680 // we need to combine the layer orientation and the
681 // user-requested orientation.
Mathias Agopiane031ba82010-10-25 18:29:35 -0700682 Transform finalTransform(Transform(mLayer.getOrientation()) *
683 Transform(mOrientation));
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700684 overlay_dev->setParameter(overlay_dev, mOverlay,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800685 OVERLAY_TRANSFORM, finalTransform.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700686 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688 }
689 }
690}
691
Mathias Agopiana2804962009-09-08 23:52:08 -0700692void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693{
694 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700695 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700696 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 overlay_control_device_t* overlay_dev = mOverlayDevice;
698 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
699 mOverlay = 0;
700 }
701}
702
703// ---------------------------------------------------------------------------
704}; // namespace android