blob: ccaad519cd23864848bfc8e49e1f214104847e2d [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
Mathias Agopianc57b15c2010-10-24 19:03:58 -0700135void LayerBuffer::validateVisibility(const Transform& globalTransform)
136{
137 sp<Source> source(getSource());
138 if (source != 0)
139 source->onvalidateVisibility(globalTransform);
140 LayerBase::validateVisibility(globalTransform);
141}
142
Mathias Agopian597c7f62010-09-29 13:02:36 -0700143void LayerBuffer::drawForSreenShot() const
144{
145 const DisplayHardware& hw(graphicPlane(0).displayHardware());
146 clearWithOpenGL( Region(hw.bounds()) );
147}
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149void LayerBuffer::onDraw(const Region& clip) const
150{
151 sp<Source> source(getSource());
152 if (LIKELY(source != 0)) {
153 source->onDraw(clip);
154 } else {
155 clearWithOpenGL(clip);
156 }
157}
158
Mathias Agopiana2804962009-09-08 23:52:08 -0700159void LayerBuffer::serverDestroy()
160{
161 sp<Source> source(clearSource());
162 if (source != 0) {
163 source->destroy();
164 }
165}
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167/**
168 * This creates a "buffer" source for this surface
169 */
170status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
171{
172 Mutex::Autolock _l(mLock);
173 if (mSource != 0)
174 return INVALID_OPERATION;
175
176 sp<BufferSource> source = new BufferSource(*this, buffers);
177
178 status_t result = source->getStatus();
179 if (result == NO_ERROR) {
180 mSource = source;
181 }
182 return result;
183}
184
185/**
186 * This creates an "overlay" source for this surface
187 */
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800188sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f,
189 int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190{
191 sp<OverlayRef> result;
192 Mutex::Autolock _l(mLock);
193 if (mSource != 0)
194 return result;
195
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800196 sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 if (result != 0) {
198 mSource = source;
199 }
200 return result;
201}
202
203sp<LayerBuffer::Source> LayerBuffer::getSource() const {
204 Mutex::Autolock _l(mLock);
205 return mSource;
206}
207
208sp<LayerBuffer::Source> LayerBuffer::clearSource() {
209 sp<Source> source;
210 Mutex::Autolock _l(mLock);
211 source = mSource;
212 mSource.clear();
213 return source;
214}
215
216// ============================================================================
Mathias Agopian9779b222009-09-07 16:32:45 -0700217// LayerBuffer::SurfaceLayerBuffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218// ============================================================================
219
Mathias Agopian593c05c2010-06-02 23:28:45 -0700220LayerBuffer::SurfaceLayerBuffer::SurfaceLayerBuffer(
221 const sp<SurfaceFlinger>& flinger, const sp<LayerBuffer>& owner)
222 : LayerBaseClient::Surface(flinger, owner->getIdentity(), owner)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223{
224}
225
Mathias Agopian9779b222009-09-07 16:32:45 -0700226LayerBuffer::SurfaceLayerBuffer::~SurfaceLayerBuffer()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227{
228 unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229}
230
Mathias Agopian9779b222009-09-07 16:32:45 -0700231status_t LayerBuffer::SurfaceLayerBuffer::registerBuffers(
Mathias Agopian1473f462009-04-10 14:24:30 -0700232 const ISurface::BufferHeap& buffers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233{
Mathias Agopian1473f462009-04-10 14:24:30 -0700234 sp<LayerBuffer> owner(getOwner());
235 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 return owner->registerBuffers(buffers);
237 return NO_INIT;
238}
239
Mathias Agopian9779b222009-09-07 16:32:45 -0700240void LayerBuffer::SurfaceLayerBuffer::postBuffer(ssize_t offset)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241{
Mathias Agopian1473f462009-04-10 14:24:30 -0700242 sp<LayerBuffer> owner(getOwner());
243 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 owner->postBuffer(offset);
245}
246
Mathias Agopian9779b222009-09-07 16:32:45 -0700247void LayerBuffer::SurfaceLayerBuffer::unregisterBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248{
Mathias Agopian1473f462009-04-10 14:24:30 -0700249 sp<LayerBuffer> owner(getOwner());
250 if (owner != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 owner->unregisterBuffers();
252}
253
Mathias Agopian9779b222009-09-07 16:32:45 -0700254sp<OverlayRef> LayerBuffer::SurfaceLayerBuffer::createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800255 uint32_t w, uint32_t h, int32_t format, int32_t orientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 sp<OverlayRef> result;
Mathias Agopian1473f462009-04-10 14:24:30 -0700257 sp<LayerBuffer> owner(getOwner());
258 if (owner != 0)
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800259 result = owner->createOverlay(w, h, format, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 return result;
261}
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263// ============================================================================
264// LayerBuffer::Buffer
265// ============================================================================
266
Mathias Agopian1d211f82010-03-08 11:14:20 -0800267LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers,
268 ssize_t offset, size_t bufferSize)
Mathias Agopian7323e542010-01-20 13:24:14 -0800269 : mBufferHeap(buffers), mSupportsCopybit(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270{
271 NativeBuffer& src(mNativeBuffer);
Mathias Agopiand86beb92009-12-02 16:23:11 -0800272 src.crop.l = 0;
273 src.crop.t = 0;
274 src.crop.r = buffers.w;
275 src.crop.b = buffers.h;
276
277 src.img.w = buffers.hor_stride ?: buffers.w;
278 src.img.h = buffers.ver_stride ?: buffers.h;
279 src.img.format = buffers.format;
280 src.img.base = (void*)(intptr_t(buffers.heap->base()) + offset);
281 src.img.handle = 0;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700282
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700283 gralloc_module_t const * module = LayerBuffer::getGrallocModule();
284 if (module && module->perform) {
285 int err = module->perform(module,
286 GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER,
Mathias Agopian1d211f82010-03-08 11:14:20 -0800287 buffers.heap->heapID(), bufferSize,
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700288 offset, buffers.heap->base(),
289 &src.img.handle);
290
Mathias Agopian7323e542010-01-20 13:24:14 -0800291 // we can fail here is the passed buffer is purely software
292 mSupportsCopybit = (err == NO_ERROR);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700293 }
Mathias Agopiand86beb92009-12-02 16:23:11 -0800294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
296LayerBuffer::Buffer::~Buffer()
297{
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700298 NativeBuffer& src(mNativeBuffer);
Mathias Agopian0b0722f2009-10-29 18:29:30 -0700299 if (src.img.handle) {
300 native_handle_delete(src.img.handle);
301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302}
303
304// ============================================================================
305// LayerBuffer::Source
306// LayerBuffer::BufferSource
307// LayerBuffer::OverlaySource
308// ============================================================================
309
310LayerBuffer::Source::Source(LayerBuffer& layer)
311 : mLayer(layer)
312{
313}
314LayerBuffer::Source::~Source() {
315}
316void LayerBuffer::Source::onDraw(const Region& clip) const {
317}
318void LayerBuffer::Source::onTransaction(uint32_t flags) {
319}
320void LayerBuffer::Source::onVisibilityResolved(
321 const Transform& planeTransform) {
322}
323void LayerBuffer::Source::postBuffer(ssize_t offset) {
324}
325void LayerBuffer::Source::unregisterBuffers() {
326}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
328// ---------------------------------------------------------------------------
329
330LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
331 const ISurface::BufferHeap& buffers)
Mathias Agopian781953d2010-06-25 18:02:21 -0700332 : Source(layer), mStatus(NO_ERROR), mBufferSize(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333{
334 if (buffers.heap == NULL) {
335 // this is allowed, but in this case, it is illegal to receive
336 // postBuffer(). The surface just erases the framebuffer with
337 // fully transparent pixels.
338 mBufferHeap = buffers;
339 mLayer.setNeedsBlending(false);
340 return;
341 }
342
343 status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
344 if (err != NO_ERROR) {
345 LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
346 mStatus = err;
347 return;
348 }
349
350 PixelFormatInfo info;
351 err = getPixelFormatInfo(buffers.format, &info);
352 if (err != NO_ERROR) {
353 LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
354 buffers.format, strerror(err));
355 mStatus = err;
356 return;
357 }
358
359 if (buffers.hor_stride<0 || buffers.ver_stride<0) {
360 LOGE("LayerBuffer::BufferSource: invalid parameters "
361 "(w=%d, h=%d, xs=%d, ys=%d)",
362 buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
363 mStatus = BAD_VALUE;
364 return;
365 }
366
367 mBufferHeap = buffers;
368 mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
369 mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
370 mLayer.forceVisibilityTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371}
372
373LayerBuffer::BufferSource::~BufferSource()
374{
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800375 class MessageDestroyTexture : public MessageBase {
376 SurfaceFlinger* flinger;
377 GLuint name;
378 public:
379 MessageDestroyTexture(
380 SurfaceFlinger* flinger, GLuint name)
381 : flinger(flinger), name(name) { }
382 virtual bool handler() {
383 glDeleteTextures(1, &name);
384 return true;
385 }
386 };
387
Mathias Agopian999543b2009-06-23 18:08:22 -0700388 if (mTexture.name != -1U) {
Mathias Agopian2d41cb92010-01-21 16:04:56 -0800389 // GL textures can only be destroyed from the GL thread
Mathias Agopian781953d2010-06-25 18:02:21 -0700390 getFlinger()->mEventQueue.postMessage(
391 new MessageDestroyTexture(getFlinger(), mTexture.name) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700393 if (mTexture.image != EGL_NO_IMAGE_KHR) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700394 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopian9042b452009-10-26 20:12:37 -0700395 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397}
398
399void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
400{
401 ISurface::BufferHeap buffers;
402 { // scope for the lock
Mathias Agopianb34d1432009-09-08 20:02:47 -0700403 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 buffers = mBufferHeap;
405 if (buffers.heap != 0) {
406 const size_t memorySize = buffers.heap->getSize();
407 if ((size_t(offset) + mBufferSize) > memorySize) {
408 LOGE("LayerBuffer::BufferSource::postBuffer() "
409 "invalid buffer (offset=%d, size=%d, heap-size=%d",
410 int(offset), int(mBufferSize), int(memorySize));
411 return;
412 }
413 }
414 }
415
416 sp<Buffer> buffer;
417 if (buffers.heap != 0) {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800418 buffer = new LayerBuffer::Buffer(buffers, offset, mBufferSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 if (buffer->getStatus() != NO_ERROR)
420 buffer.clear();
421 setBuffer(buffer);
422 mLayer.invalidate();
423 }
424}
425
426void LayerBuffer::BufferSource::unregisterBuffers()
427{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700428 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 mBufferHeap.heap.clear();
430 mBuffer.clear();
431 mLayer.invalidate();
432}
433
434sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
435{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700436 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 return mBuffer;
438}
439
440void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
441{
Mathias Agopianb34d1432009-09-08 20:02:47 -0700442 Mutex::Autolock _l(mBufferSourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 mBuffer = buffer;
444}
445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446void LayerBuffer::BufferSource::onDraw(const Region& clip) const
447{
Mathias Agopian999543b2009-06-23 18:08:22 -0700448 sp<Buffer> ourBuffer(getBuffer());
449 if (UNLIKELY(ourBuffer == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 // nothing to do, we don't have a buffer
451 mLayer.clearWithOpenGL(clip);
452 return;
453 }
454
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700455 status_t err = NO_ERROR;
456 NativeBuffer src(ourBuffer->getBuffer());
Mathias Agopiana2804962009-09-08 23:52:08 -0700457 const Rect transformedBounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
Mathias Agopian9042b452009-10-26 20:12:37 -0700459#if defined(EGL_ANDROID_image_native_buffer)
Mathias Agopian781953d2010-06-25 18:02:21 -0700460 if (GLExtensions::getInstance().haveDirectTexture()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800461 err = INVALID_OPERATION;
462 if (ourBuffer->supportsCopybit()) {
Mathias Agopianfcdd3942010-02-04 17:13:06 -0800463 copybit_device_t* copybit = mLayer.mBlitEngine;
464 if (copybit && err != NO_ERROR) {
465 // create our EGLImageKHR the first time
466 err = initTempBuffer();
467 if (err == NO_ERROR) {
468 // NOTE: Assume the buffer is allocated with the proper USAGE flags
469 const NativeBuffer& dst(mTempBuffer);
470 region_iterator clip(Region(Rect(dst.crop.r, dst.crop.b)));
471 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
472 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 0xFF);
473 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
474 err = copybit->stretch(copybit, &dst.img, &src.img,
475 &dst.crop, &src.crop, &clip);
476 if (err != NO_ERROR) {
477 clearTempBufferImage();
478 }
479 }
480 }
Mathias Agopian90daccf2009-11-05 23:08:00 -0800481 }
Mathias Agopiancbc4c9f2009-06-23 21:11:43 -0700482 }
Mathias Agopian26c28b12009-06-24 22:39:26 -0700483#endif
Mathias Agopian9042b452009-10-26 20:12:37 -0700484 else {
485 err = INVALID_OPERATION;
486 }
487
488 if (err != NO_ERROR) {
Mathias Agopianf007a2f2009-10-28 21:00:29 -0700489 // slower fallback
Mathias Agopian999543b2009-06-23 18:08:22 -0700490 GGLSurface t;
491 t.version = sizeof(GGLSurface);
492 t.width = src.crop.r;
493 t.height = src.crop.b;
494 t.stride = src.img.w;
495 t.vstride= src.img.h;
496 t.format = src.img.format;
Mathias Agopian2eab9d82009-06-24 16:55:59 -0700497 t.data = (GGLubyte*)src.img.base;
Mathias Agopian999543b2009-06-23 18:08:22 -0700498 const Region dirty(Rect(t.width, t.height));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700499 mTextureManager.loadTexture(&mTexture, dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700502 mLayer.setBufferTransform(mBufferHeap.transform);
Mathias Agopian9042b452009-10-26 20:12:37 -0700503 mLayer.drawWithOpenGL(clip, mTexture);
504}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
Mathias Agopian76169da2009-12-09 14:32:56 -0800506status_t LayerBuffer::BufferSource::initTempBuffer() const
507{
508 // figure out the size we need now
509 const ISurface::BufferHeap& buffers(mBufferHeap);
510 uint32_t w = mLayer.mTransformedBounds.width();
511 uint32_t h = mLayer.mTransformedBounds.height();
512 if (buffers.w * h != buffers.h * w) {
513 int t = w; w = h; h = t;
514 }
515
Mathias Agopiand23fa272010-01-20 14:31:53 -0800516 // we're in the copybit case, so make sure we can handle this blit
517 // we don't have to keep the aspect ratio here
518 copybit_device_t* copybit = mLayer.mBlitEngine;
519 const int down = copybit->get(copybit, COPYBIT_MINIFICATION_LIMIT);
520 const int up = copybit->get(copybit, COPYBIT_MAGNIFICATION_LIMIT);
521 if (buffers.w > w*down) w = buffers.w / down;
522 else if (w > buffers.w*up) w = buffers.w*up;
523 if (buffers.h > h*down) h = buffers.h / down;
524 else if (h > buffers.h*up) h = buffers.h*up;
525
Mathias Agopian76169da2009-12-09 14:32:56 -0800526 if (mTexture.image != EGL_NO_IMAGE_KHR) {
527 // we have an EGLImage, make sure the needed size didn't change
528 if (w!=mTexture.width || h!= mTexture.height) {
529 // delete the EGLImage and texture
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800530 clearTempBufferImage();
Mathias Agopian76169da2009-12-09 14:32:56 -0800531 } else {
532 // we're good, we have an EGLImageKHR and it's (still) the
533 // right size
534 return NO_ERROR;
535 }
536 }
537
538 // figure out if we need linear filtering
539 if (buffers.w * h == buffers.h * w) {
540 // same pixel area, don't use filtering
Mathias Agopian92377032010-05-26 22:08:52 -0700541 mLayer.mNeedsFiltering = false;
Mathias Agopian76169da2009-12-09 14:32:56 -0800542 }
543
544 // Allocate a temporary buffer and create the corresponding EGLImageKHR
Mathias Agopian08956f02010-02-12 18:50:28 -0800545 // once the EGLImage has been created we don't need the
546 // graphic buffer reference anymore.
547 sp<GraphicBuffer> buffer = new GraphicBuffer(
Mathias Agopian76169da2009-12-09 14:32:56 -0800548 w, h, HAL_PIXEL_FORMAT_RGB_565,
549 GraphicBuffer::USAGE_HW_TEXTURE |
550 GraphicBuffer::USAGE_HW_2D);
551
Mathias Agopian08956f02010-02-12 18:50:28 -0800552 status_t err = buffer->initCheck();
Mathias Agopian76169da2009-12-09 14:32:56 -0800553 if (err == NO_ERROR) {
554 NativeBuffer& dst(mTempBuffer);
Mathias Agopian08956f02010-02-12 18:50:28 -0800555 dst.img.w = buffer->getStride();
Mathias Agopian76169da2009-12-09 14:32:56 -0800556 dst.img.h = h;
Mathias Agopian08956f02010-02-12 18:50:28 -0800557 dst.img.format = buffer->getPixelFormat();
558 dst.img.handle = (native_handle_t *)buffer->handle;
Mathias Agopian76169da2009-12-09 14:32:56 -0800559 dst.img.base = 0;
560 dst.crop.l = 0;
561 dst.crop.t = 0;
562 dst.crop.r = w;
563 dst.crop.b = h;
564
Mathias Agopian781953d2010-06-25 18:02:21 -0700565 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700566 err = mTextureManager.initEglImage(&mTexture, dpy, buffer);
Mathias Agopian76169da2009-12-09 14:32:56 -0800567 }
568
569 return err;
570}
571
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800572void LayerBuffer::BufferSource::clearTempBufferImage() const
573{
Mathias Agopiana1a17982010-01-21 16:27:30 -0800574 // delete the image
Mathias Agopian781953d2010-06-25 18:02:21 -0700575 EGLDisplay dpy(getFlinger()->graphicPlane(0).getEGLDisplay());
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800576 eglDestroyImageKHR(dpy, mTexture.image);
Mathias Agopiana1a17982010-01-21 16:27:30 -0800577
578 // and the associated texture (recreate a name)
579 glDeleteTextures(1, &mTexture.name);
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800580 Texture defaultTexture;
581 mTexture = defaultTexture;
Mathias Agopiand2832fb42010-01-20 13:56:07 -0800582}
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584// ---------------------------------------------------------------------------
585
586LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
587 sp<OverlayRef>* overlayRef,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800588 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 : Source(layer), mVisibilityChanged(false),
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800590 mOverlay(0), mOverlayHandle(0), mOverlayDevice(0), mOrientation(orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591{
Mathias Agopian781953d2010-06-25 18:02:21 -0700592 overlay_control_device_t* overlay_dev = getFlinger()->getOverlayEngine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 if (overlay_dev == NULL) {
594 // overlays not supported
595 return;
596 }
597
598 mOverlayDevice = overlay_dev;
599 overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
600 if (overlay == NULL) {
601 // couldn't create the overlay (no memory? no more overlays?)
602 return;
603 }
604
605 // enable dithering...
606 overlay_dev->setParameter(overlay_dev, overlay,
607 OVERLAY_DITHER, OVERLAY_ENABLE);
608
609 mOverlay = overlay;
610 mWidth = overlay->w;
611 mHeight = overlay->h;
612 mFormat = overlay->format;
613 mWidthStride = overlay->w_stride;
614 mHeightStride = overlay->h_stride;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700615 mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616
617 mOverlayHandle = overlay->getHandleRef(overlay);
618
Mathias Agopiana2804962009-09-08 23:52:08 -0700619 sp<OverlayChannel> channel = new OverlayChannel( &layer );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 *overlayRef = new OverlayRef(mOverlayHandle, channel,
622 mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
Mathias Agopian781953d2010-06-25 18:02:21 -0700623 getFlinger()->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624}
625
626LayerBuffer::OverlaySource::~OverlaySource()
627{
628 if (mOverlay && mOverlayDevice) {
629 overlay_control_device_t* overlay_dev = mOverlayDevice;
630 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
631 }
632}
633
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700634void LayerBuffer::OverlaySource::onDraw(const Region& clip) const
635{
Mathias Agopian8ae03842009-09-15 19:31:28 -0700636 // this would be where the color-key would be set, should we need it.
Mathias Agopian0d3c0062010-05-26 22:26:12 -0700637 GLclampf red = 0;
638 GLclampf green = 0;
639 GLclampf blue = 0;
Rebecca Schultz Zavinc8546782009-09-01 23:06:45 -0700640 mLayer.clearWithOpenGL(clip, red, green, blue, 0);
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700641}
642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
644{
645 const Layer::State& front(mLayer.drawingState());
646 const Layer::State& temp(mLayer.currentState());
647 if (temp.sequence != front.sequence) {
648 mVisibilityChanged = true;
649 }
650}
651
Mathias Agopianc57b15c2010-10-24 19:03:58 -0700652void LayerBuffer::OverlaySource::onvalidateVisibility(const Transform&)
653{
654 mVisibilityChanged = true;
655}
656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657void LayerBuffer::OverlaySource::onVisibilityResolved(
658 const Transform& planeTransform)
659{
660 // this code-path must be as tight as possible, it's called each time
661 // the screen is composited.
662 if (UNLIKELY(mOverlay != 0)) {
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700663 if (mVisibilityChanged || !mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 mVisibilityChanged = false;
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700665 mInitialized = true;
Mathias Agopiana2804962009-09-08 23:52:08 -0700666 const Rect bounds(mLayer.getTransformedBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 int x = bounds.left;
668 int y = bounds.top;
669 int w = bounds.width();
670 int h = bounds.height();
671
672 // we need a lock here to protect "destroy"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700673 Mutex::Autolock _l(mOverlaySourceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 if (mOverlay) {
675 overlay_control_device_t* overlay_dev = mOverlayDevice;
676 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800677 // we need to combine the layer orientation and the
678 // user-requested orientation.
679 Transform finalTransform = Transform(mOrientation) *
680 Transform(mLayer.getOrientation());
Rebecca Schultz Zavin43ab7632009-07-21 16:17:59 -0700681 overlay_dev->setParameter(overlay_dev, mOverlay,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800682 OVERLAY_TRANSFORM, finalTransform.getOrientation());
Rebecca Schultz Zavin7ac5e692009-07-20 21:18:04 -0700683 overlay_dev->commit(overlay_dev, mOverlay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 }
686 }
687}
688
Mathias Agopiana2804962009-09-08 23:52:08 -0700689void LayerBuffer::OverlaySource::destroy()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690{
691 // we need a lock here to protect "onVisibilityResolved"
Mathias Agopianb34d1432009-09-08 20:02:47 -0700692 Mutex::Autolock _l(mOverlaySourceLock);
Mathias Agopiana2804962009-09-08 23:52:08 -0700693 if (mOverlay && mOverlayDevice) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 overlay_control_device_t* overlay_dev = mOverlayDevice;
695 overlay_dev->destroyOverlay(overlay_dev, mOverlay);
696 mOverlay = 0;
697 }
698}
699
700// ---------------------------------------------------------------------------
701}; // namespace android