blob: 1fc982c2141b7087376d2eeb4c1aef80f655625f [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 Agopian6edf5af2009-06-19 17:00:27 -070047 const sp<Client>& client, int32_t i)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 : LayerBaseClient(flinger, display, client, i),
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 Agopian9779b222009-09-07 16:32:45 -070063 mSurface = new SurfaceLayerBuffer(mFlinger, clientIndex(),
Mathias Agopian6cf0db22009-04-17 19:36:26 -070064 const_cast<LayerBuffer *>(this));
Mathias Agopian0b0722f2009-10-29 18:29:30 -070065
66 hw_module_t const* module = (hw_module_t const*)sGrallocModule;
67 if (!module) {
68 // NOTE: technically there is a race here, but it shouldn't
69 // cause any problem since hw_get_module() always returns
70 // the same value.
71 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
72 sGrallocModule = (gralloc_module_t const *)module;
73 }
74 }
Mathias Agopian90daccf2009-11-05 23:08:00 -080075
76 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
77 copybit_open(module, &mBlitEngine);
78 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -070079}
80
Mathias Agopian1473f462009-04-10 14:24:30 -070081sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082{
Mathias Agopian6cf0db22009-04-17 19:36:26 -070083 return mSurface;
84}
85
86status_t LayerBuffer::ditch()
87{
88 mSurface.clear();
89 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090}
91
92bool LayerBuffer::needsBlending() const {
93 return mNeedsBlending;
94}
95
96void LayerBuffer::setNeedsBlending(bool blending) {
97 mNeedsBlending = blending;
98}
99
100void LayerBuffer::postBuffer(ssize_t offset)
101{
102 sp<Source> source(getSource());
103 if (source != 0)
104 source->postBuffer(offset);
105}
106
107void LayerBuffer::unregisterBuffers()
108{
109 sp<Source> source(clearSource());
110 if (source != 0)
111 source->unregisterBuffers();
112}
113
114uint32_t LayerBuffer::doTransaction(uint32_t flags)
115{
116 sp<Source> source(getSource());
117 if (source != 0)
118 source->onTransaction(flags);
Mathias Agopian4c29c942009-11-19 14:46:26 -0800119 uint32_t res = LayerBase::doTransaction(flags);
120 // we always want filtering for these surfaces
Mathias Agopian92377032010-05-26 22:08:52 -0700121 mNeedsFiltering = !(mFlags & DisplayHardware::SLOW_CONFIG);
Mathias Agopian4c29c942009-11-19 14:46:26 -0800122 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
125void LayerBuffer::unlockPageFlip(const Transform& planeTransform,
126 Region& outDirtyRegion)
127{
128 // this code-path must be as tight as possible, it's called each time
129 // the screen is composited.
130 sp<Source> source(getSource());
131 if (source != 0)
132 source->onVisibilityResolved(planeTransform);
133 LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
134}
135
136void LayerBuffer::onDraw(const Region& clip) const
137{
138 sp<Source> source(getSource());
139 if (LIKELY(source != 0)) {
140 source->onDraw(clip);
141 } else {
142 clearWithOpenGL(clip);
143 }
144}
145
146bool LayerBuffer::transformed() const
147{
148 sp<Source> source(getSource());
149 if (LIKELY(source != 0))
150 return source->transformed();
151 return false;
152}
153
Mathias Agopiana2804962009-09-08 23:52:08 -0700154void LayerBuffer::serverDestroy()
155{
156 sp<Source> source(clearSource());
157 if (source != 0) {
158 source->destroy();
159 }
160}
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162/**
163 * This creates a "buffer" source for this surface
164 */
165status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
166{
167 Mutex::Autolock _l(mLock);
168 if (mSource != 0)
169 return INVALID_OPERATION;
170
171 sp<BufferSource> source = new BufferSource(*this, buffers);
172
173 status_t result = source->getStatus();
174 if (result == NO_ERROR) {
175 mSource = source;
176 }
177 return result;
178}
179
180/**
181 * This creates an "overlay" source for this surface
182 */
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800183sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f,
184 int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185{
186 sp<OverlayRef> result;
187 Mutex::Autolock _l(mLock);
188 if (mSource != 0)
189 return result;
190
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800191 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 if (result != 0) {
193 mSource = source;
194 }
195 return result;
196}
197
198sp<LayerBuffer::Source> LayerBuffer::getSource() const {
199 Mutex::Autolock _l(mLock);
200 return mSource;
201}
202
203sp<LayerBuffer::Source> LayerBuffer::clearSource() {
204 sp<Source> source;
205 Mutex::Autolock _l(mLock);
206 source = mSource;
207 mSource.clear();
208 return source;
209}
210
211// ============================================================================
Mathias Agopian9779b222009-09-07 16:32:45 -0700212// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213// ============================================================================
214
Mathias Agopian9779b222009-09-07 16:32:45 -0700215LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700216 SurfaceID id, const sp<LayerBuffer>& owner)
217 : LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218{
219}
220
Mathias Agopian9779b222009-09-07 16:32:45 -0700221LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222{
223 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224}
225
Mathias Agopian9779b222009-09-07 16:32:45 -0700226status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700227 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228{
Mathias Agopian1473f462009-04-10 14:24:30 -0700229 sp<LayerBuffer> owner(getOwner());
230 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 return owner->registerBuffers(buffers);
232 return NO_INIT;
233}
234
Mathias Agopian9779b222009-09-07 16:32:45 -0700235void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
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 owner->postBuffer(offset);
240}
241
Mathias Agopian9779b222009-09-07 16:32:45 -0700242void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243{
Mathias Agopian1473f462009-04-10 14:24:30 -0700244 sp<LayerBuffer> owner(getOwner());
245 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 owner->unregisterBuffers();
247}
248
Mathias Agopian9779b222009-09-07 16:32:45 -0700249sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800250 uint32_t w, uint32_t h, int32_t format, int32_t orientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700252 sp<LayerBuffer> owner(getOwner());
253 if (owner != 0)
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800254 result = owner->createOverlay(w, h, format, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 return result;
256}
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258// ============================================================================
259// LayerBuffer::Buffer
260// ============================================================================
261
Mathias Agopian1d211f82010-03-08 11:14:20 -0800262LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers,
263 ssize_t offset, size_t bufferSize)
Mathias Agopian7323e542010-01-20 13:24:14 -0800264 : mBufferHeap(buffers), mSupportsCopybit(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265{
266 NativeBuffer& src(mNativeBuffer);
Mathias Agopiand86beb92009-12-02 16:23:11 -0800267 src.crop.l = 0;
268 src.crop.t = 0;
269 src.crop.r = buffers.w;
270 src.crop.b = buffers.h;
271
272 src.img.w = buffers.hor_stride ?: buffers.w;
273 src.img.h = buffers.ver_stride ?: buffers.h;
274 src.img.format = buffers.format;
275 src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset);
276 src.img.handle = 0;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700277
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700278 gralloc_module_t const * module = LayerBuffer::getGrallocModule();
279 if (module && module->perform) {
280 int err = module->perform(module,
281 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER,
Mathias Agopian1d211f82010-03-08 11:14:20 -0800282 buffers.heap->heapID(), bufferSize,
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700283 offset, buffers.heap->base(),
284 &src.img.handle);
285
Mathias Agopian7323e542010-01-20 13:24:14 -0800286 // we can fail here is the passed buffer is purely software
287 mSupportsCopybit = (err == NO_ERROR);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700288 }
Mathias Agopiand86beb92009-12-02 16:23:11 -0800289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
291LayerBuffer::Buffer::~Buffer()
292{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700293 NativeBuffer& src(mNativeBuffer);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700294 if (src.img.handle) {
295 native_handle_delete(src.img.handle);
296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297}
298
299// ============================================================================
300// LayerBuffer::Source
301// LayerBuffer::BufferSource
302// LayerBuffer::OverlaySource
303// ============================================================================
304
305LayerBuffer::Source::Source(LayerBuffer& layer)
306 : mLayer(layer)
307{
308}
309LayerBuffer::Source::~Source() {
310}
311void LayerBuffer::Source::onDraw(const Region& clip) const {
312}
313void LayerBuffer::Source::onTransaction(uint32_t flags) {
314}
315void LayerBuffer::Source::onVisibilityResolved(
316 const Transform& planeTransform) {
317}
318void LayerBuffer::Source::postBuffer(ssize_t offset) {
319}
320void LayerBuffer::Source::unregisterBuffers() {
321}
322bool LayerBuffer::Source::transformed() const {
323 return mLayer.mTransformed;
324}
325
326// ---------------------------------------------------------------------------
327
328LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
329 const ISurface::BufferHeap& buffers)
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800330 : Source(layer), mStatus(NO_ERROR), mBufferSize(0),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700331 mTextureManager(layer.mFlags)
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) {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800417 buffer = new LayerBuffer::Buffer(buffers, offset, mBufferSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 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 Agopian9042b452009-10-26 20:12:37 -0700463#if defined(EGL_ANDROID_image_native_buffer)
464 if (mLayer.mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800465 err = INVALID_OPERATION;
466 if (ourBuffer->supportsCopybit()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800467 copybit_device_t* copybit = mLayer.mBlitEngine;
468 if (copybit && err != NO_ERROR) {
469 // create our EGLImageKHR the first time
470 err = initTempBuffer();
471 if (err == NO_ERROR) {
472 // NOTE: Assume the buffer is allocated with the proper USAGE flags
473 const NativeBuffer& dst(mTempBuffer);
474 region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b)));
475 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
476 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
477 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
478 err = copybit->stretch(copybit, &dst.img, &src.img,
479 &dst.crop, &src.crop, &clip);
480 if (err != NO_ERROR) {
481 clearTempBufferImage();
482 }
483 }
484 }
Mathias Agopian90daccf2009-11-05 23:08:00 -0800485 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700486 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700487#endif
Mathias Agopian9042b452009-10-26 20:12:37 -0700488 else {
489 err = INVALID_OPERATION;
490 }
491
492 if (err != NO_ERROR) {
Mathias Agopianf007a2f2009-10-28 21:00:29 -0700493 // slower fallback
Mathias Agopian999543b2009-06-23 18:08:22 -0700494 GGLSurface t;
495 t.version = sizeof(GGLSurface);
496 t.width = src.crop.r;
497 t.height = src.crop.b;
498 t.stride = src.img.w;
499 t.vstride= src.img.h;
500 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700501 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700502 const Region dirty(Rect(t.width, t.height));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700503 mTextureManager.loadTexture(&mTexture, dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
Mathias Agopian9042b452009-10-26 20:12:37 -0700506 mTexture.transform = mBufferHeap.transform;
507 mLayer.drawWithOpenGL(clip, mTexture);
508}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509
Mathias Agopian76169da2009-12-09 14:32:56 -0800510status_t LayerBuffer::BufferSource::initTempBuffer() const
511{
512 // figure out the size we need now
513 const ISurface::BufferHeap& buffers(mBufferHeap);
514 uint32_t w = mLayer.mTransformedBounds.width();
515 uint32_t h = mLayer.mTransformedBounds.height();
516 if (buffers.w * h != buffers.h * w) {
517 int t = w; w = h; h = t;
518 }
519
Mathias Agopiand23fa272010-01-20 14:31:53 -0800520 // we're in the copybit case, so make sure we can handle this blit
521 // we don't have to keep the aspect ratio here
522 copybit_device_t* copybit = mLayer.mBlitEngine;
523 const int down = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
524 const int up = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
525 if (buffers.w > w*down) w = buffers.w / down;
526 else if (w > buffers.w*up) w = buffers.w*up;
527 if (buffers.h > h*down) h = buffers.h / down;
528 else if (h > buffers.h*up) h = buffers.h*up;
529
Mathias Agopian76169da2009-12-09 14:32:56 -0800530 if (mTexture.image != EGL_NO_IMAGE_KHR) {
531 // we have an EGLImage, make sure the needed size didn't change
532 if (w!=mTexture.width || h!= mTexture.height) {
533 // delete the EGLImage and texture
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800534 clearTempBufferImage();
Mathias Agopian76169da2009-12-09 14:32:56 -0800535 } else {
536 // we're good, we have an EGLImageKHR and it's (still) the
537 // right size
538 return NO_ERROR;
539 }
540 }
541
542 // figure out if we need linear filtering
543 if (buffers.w * h == buffers.h * w) {
544 // same pixel area, don't use filtering
Mathias Agopian92377032010-05-26 22:08:52 -0700545 mLayer.mNeedsFiltering = false;
Mathias Agopian76169da2009-12-09 14:32:56 -0800546 }
547
548 // Allocate a temporary buffer and create the corresponding EGLImageKHR
Mathias Agopian08956f02010-02-12 18:50:28 -0800549 // once the EGLImage has been created we don't need the
550 // graphic buffer reference anymore.
551 sp<GraphicBuffer> buffer = new GraphicBuffer(
Mathias Agopian76169da2009-12-09 14:32:56 -0800552 w, h, HAL_PIXEL_FORMAT_RGB_565,
553 GraphicBuffer::USAGE_HW_TEXTURE |
554 GraphicBuffer::USAGE_HW_2D);
555
Mathias Agopian08956f02010-02-12 18:50:28 -0800556 status_t err = buffer->initCheck();
Mathias Agopian76169da2009-12-09 14:32:56 -0800557 if (err == NO_ERROR) {
558 NativeBuffer& dst(mTempBuffer);
Mathias Agopian08956f02010-02-12 18:50:28 -0800559 dst.img.w = buffer->getStride();
Mathias Agopian76169da2009-12-09 14:32:56 -0800560 dst.img.h = h;
Mathias Agopian08956f02010-02-12 18:50:28 -0800561 dst.img.format = buffer->getPixelFormat();
562 dst.img.handle = (native_handle_t *)buffer->handle;
Mathias Agopian76169da2009-12-09 14:32:56 -0800563 dst.img.base = 0;
564 dst.crop.l = 0;
565 dst.crop.t = 0;
566 dst.crop.r = w;
567 dst.crop.b = h;
568
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700569 EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
570 err = mTextureManager.initEglImage(&mTexture, dpy, buffer);
Mathias Agopian76169da2009-12-09 14:32:56 -0800571 }
572
573 return err;
574}
575
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800576void LayerBuffer::BufferSource::clearTempBufferImage() const
577{
Mathias Agopiana1a17982010-01-21 16:27:30 -0800578 // delete the image
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800579 EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800580 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopiana1a17982010-01-21 16:27:30 -0800581
582 // and the associated texture (recreate a name)
583 glDeleteTextures(1, &mTexture.name);
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800584 Texture defaultTexture;
585 mTexture = defaultTexture;
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800586}
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588// ---------------------------------------------------------------------------
589
590LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
591 sp<OverlayRef>* overlayRef,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800592 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 : Source(layer), mVisibilityChanged(false),
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800594 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0), mOrientation(orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595{
596 overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
597 if (overlay_dev == NULL) {
598 // overlays not supported
599 return;
600 }
601
602 mOverlayDevice = overlay_dev;
603 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
604 if (overlay == NULL) {
605 // couldn't create the overlay (no memory? no more overlays?)
606 return;
607 }
608
609 // enable dithering...
610 overlay_dev->setParameter(overlay_dev, overlay,
611 OVERLAY_DITHER, OVERLAY_ENABLE);
612
613 mOverlay = overlay;
614 mWidth = overlay->w;
615 mHeight = overlay->h;
616 mFormat = overlay->format;
617 mWidthStride = overlay->w_stride;
618 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700619 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 mOverlayHandle = overlay->getHandleRef(overlay);
622
Mathias Agopiana2804962009-09-08 23:52:08 -0700623 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
625 *overlayRef = new OverlayRef(mOverlayHandle, channel,
626 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
Benny Wonge38212a2009-09-16 14:48:52 -0500627 mLayer.mFlinger->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628}
629
630LayerBuffer::OverlaySource::~OverlaySource()
631{
632 if (mOverlay && mOverlayDevice) {
633 overlay_control_device_t* overlay_dev = mOverlayDevice;
634 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
635 }
636}
637
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700638void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
639{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700640 // this would be where the color-key would be set, should we need it.
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700641 GLclampx red = 0;
642 GLclampx green = 0;
Mathias Agopian8ae03842009-09-15 19:31:28 -0700643 GLclampx blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700644 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700645}
646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
648{
649 const Layer::State& front(mLayer.drawingState());
650 const Layer::State& temp(mLayer.currentState());
651 if (temp.sequence != front.sequence) {
652 mVisibilityChanged = true;
653 }
654}
655
656void LayerBuffer::OverlaySource::onVisibilityResolved(
657 const Transform& planeTransform)
658{
659 // this code-path must be as tight as possible, it's called each time
660 // the screen is composited.
661 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700662 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700664 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700665 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 int x = bounds.left;
667 int y = bounds.top;
668 int w = bounds.width();
669 int h = bounds.height();
670
671 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700672 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 if (mOverlay) {
674 overlay_control_device_t* overlay_dev = mOverlayDevice;
675 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800676 // we need to combine the layer orientation and the
677 // user-requested orientation.
678 Transform finalTransform = Transform(mOrientation) *
679 Transform(mLayer.getOrientation());
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700680 overlay_dev->setParameter(overlay_dev, mOverlay,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800681 OVERLAY_TRANSFORM, finalTransform.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700682 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684 }
685 }
686}
687
Mathias Agopiana2804962009-09-08 23:52:08 -0700688void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689{
690 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700691 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700692 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 overlay_control_device_t* overlay_dev = mOverlayDevice;
694 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
695 mOverlay = 0;
696 }
697}
698
699// ---------------------------------------------------------------------------
700}; // namespace android