blob: 84584aa8f4413b30b44879d19afc6c6903f9bc46 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian3330b202009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include "clz.h"
34#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
38
39#define DEBUG_RESIZE 0
40
41
42namespace android {
43
Mathias Agopianca99fb82010-04-14 16:43:44 -070044template <typename T> inline T min(T a, T b) {
45 return a<b ? a : b;
46}
47
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048// ---------------------------------------------------------------------------
49
Mathias Agopiancbb288b2009-09-07 16:32:45 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopiand606de62010-05-10 20:06:11 -070051 const sp<Client>& client, int32_t i)
52 : LayerBaseClient(flinger, display, client, i),
53 lcblk(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 mSecure(false),
Mathias Agopian401c2572009-09-23 19:16:27 -070055 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070056 mNeedsDithering(false),
57 mTextureManager(mFlags),
Mathias Agopiana138f892010-05-21 17:24:35 -070058 mBufferManager(mTextureManager),
59 mWidth(0), mHeight(0), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060{
61 // no OpenGL operation is possible here, since we might not be
62 // in the OpenGL thread.
Mathias Agopiand606de62010-05-10 20:06:11 -070063 lcblk = new SharedBufferServer(
Mathias Agopianbb641242010-05-18 17:06:55 -070064 client->ctrlblk, i, mBufferManager.getDefaultBufferCount(),
Mathias Agopiand606de62010-05-10 20:06:11 -070065 getIdentity());
66
67 mBufferManager.setActiveBufferIndex( lcblk->getFrontBuffer() );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068}
69
70Layer::~Layer()
71{
Mathias Agopianbb641242010-05-18 17:06:55 -070072 // FIXME: must be called from the main UI thread
73 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
74 mBufferManager.destroy(dpy);
75
Mathias Agopian0aa758d2009-04-22 15:23:34 -070076 // the actual buffers will be destroyed here
Mathias Agopiand606de62010-05-10 20:06:11 -070077 delete lcblk;
78}
79
80// called with SurfaceFlinger::mStateLock as soon as the layer is entered
81// in the purgatory list
82void Layer::onRemoved()
83{
84 // wake up the condition
85 lcblk->setStatus(NO_INIT);
Mathias Agopian48d819a2009-09-10 19:41:18 -070086}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070087
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
90 return mSurface;
91}
92
Mathias Agopian9a112062009-04-17 19:36:26 -070093status_t Layer::ditch()
94{
Mathias Agopianbb641242010-05-18 17:06:55 -070095 // NOTE: Called from the main UI thread
96
Mathias Agopian0aa758d2009-04-22 15:23:34 -070097 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -080098 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -070099
100 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
101 mBufferManager.destroy(dpy);
102 mSurface.clear();
103
104 Mutex::Autolock _l(mLock);
105 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700106 return NO_ERROR;
107}
108
Mathias Agopianf9d93272009-06-19 17:00:27 -0700109status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 PixelFormat format, uint32_t flags)
111{
Mathias Agopian401c2572009-09-23 19:16:27 -0700112 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 PixelFormatInfo info;
114 status_t err = getPixelFormatInfo(format, &info);
115 if (err) return err;
116
Mathias Agopian401c2572009-09-23 19:16:27 -0700117 // the display's pixel format
118 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700119 uint32_t const maxSurfaceDims = min(
120 hw.getMaxTextureSize(), hw.getMaxViewportDims());
121
122 // never allow a surface larger than what our underlying GL implementation
123 // can handle.
124 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
125 return BAD_VALUE;
126 }
127
Mathias Agopian401c2572009-09-23 19:16:27 -0700128 PixelFormatInfo displayInfo;
129 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700130 const uint32_t hwFlags = hw.getFlags();
131
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700132 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700133 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700134 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700135 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700137
Mathias Agopian401c2572009-09-23 19:16:27 -0700138 // we use the red index
139 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
140 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
141 mNeedsDithering = layerRedsize > displayRedSize;
142
Mathias Agopian9a112062009-04-17 19:36:26 -0700143 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 return NO_ERROR;
145}
146
147void Layer::reloadTexture(const Region& dirty)
148{
Mathias Agopiand606de62010-05-10 20:06:11 -0700149 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800150 if (buffer == NULL) {
151 // this situation can happen if we ran out of memory for instance.
152 // not much we can do. continue to use whatever texture was bound
153 // to this context.
154 return;
155 }
156
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700157#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700158 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700159 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
160 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
161 // not sure what we can do here...
162 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
163 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700164 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700165 } else
166#endif
167 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800168slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700170 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700171 LOGE_IF(res, "error %d (%s) locking buffer %p",
172 res, strerror(res), buffer.get());
173 if (res == NO_ERROR) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700174 mBufferManager.loadTexture(dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700175 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180void Layer::onDraw(const Region& clip) const
181{
Mathias Agopiand606de62010-05-10 20:06:11 -0700182 Texture tex(mBufferManager.getActiveTexture());
183 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700185 // in fact never been drawn into. This happens frequently with
186 // SurfaceView because the WindowManager can't know when the client
187 // has drawn the first time.
188
189 // If there is nothing under us, we paint the screen in black, otherwise
190 // we just skip this update.
191
192 // figure out if there is something below us
193 Region under;
194 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
195 const size_t count = drawingLayers.size();
196 for (size_t i=0 ; i<count ; ++i) {
197 const sp<LayerBase>& layer(drawingLayers[i]);
198 if (layer.get() == static_cast<LayerBase const*>(this))
199 break;
200 under.orSelf(layer->visibleRegionScreen);
201 }
202 // if not everything below us is covered, we plug the holes!
203 Region holes(clip.subtract(under));
204 if (!holes.isEmpty()) {
205 clearWithOpenGL(holes);
206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return;
208 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700209 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700212
213status_t Layer::setBufferCount(int bufferCount)
214{
Mathias Agopianbb641242010-05-18 17:06:55 -0700215 // Ensures our client doesn't go away while we're accessing
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700216 // the shared area.
217 sp<Client> ourClient(client.promote());
218 if (ourClient == 0) {
219 // oops, the client is already gone
220 return DEAD_OBJECT;
221 }
222
Mathias Agopianbb641242010-05-18 17:06:55 -0700223 // NOTE: lcblk->resize() is protected by an internal lock
224 status_t err = lcblk->resize(bufferCount);
225 if (err == NO_ERROR)
226 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700227
228 return err;
229}
230
Mathias Agopiana138f892010-05-21 17:24:35 -0700231sp<GraphicBuffer> Layer::requestBuffer(int index,
232 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
233 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234{
Mathias Agopian3330b202009-10-05 17:07:12 -0700235 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700236
Mathias Agopiana138f892010-05-21 17:24:35 -0700237 if ((reqWidth | reqHeight | reqFormat) < 0)
238 return buffer;
239
240 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
241 return buffer;
242
Mathias Agopian48d819a2009-09-10 19:41:18 -0700243 // this ensures our client doesn't go away while we're accessing
244 // the shared area.
245 sp<Client> ourClient(client.promote());
246 if (ourClient == 0) {
247 // oops, the client is already gone
248 return buffer;
249 }
250
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700252 * This is called from the client's Surface::dequeue(). This can happen
253 * at any time, especially while we're in the middle of using the
254 * buffer 'index' as our front buffer.
Mathias Agopianbb641242010-05-18 17:06:55 -0700255 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700256 * Make sure the buffer we're resizing is not the front buffer and has been
257 * dequeued. Once this condition is asserted, we are guaranteed that this
258 * buffer cannot become the front buffer under our feet, since we're called
259 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700261 status_t err = lcblk->assertReallocate(index);
262 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700263 if (err != NO_ERROR) {
264 // the surface may have died
265 return buffer;
266 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267
Mathias Agopiana138f892010-05-21 17:24:35 -0700268 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700269 { // scope for the lock
270 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700271 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
272 const bool formatChanged = mReqFormat != reqFormat;
273 mReqWidth = reqWidth;
274 mReqHeight = reqHeight;
275 mReqFormat = reqFormat;
276 mFixedSize = reqWidth && reqHeight;
277 w = reqWidth ? reqWidth : mWidth;
278 h = reqHeight ? reqHeight : mHeight;
279 f = reqFormat ? reqFormat : mFormat;
Mathias Agopiand606de62010-05-10 20:06:11 -0700280 buffer = mBufferManager.detachBuffer(index);
Mathias Agopiana138f892010-05-21 17:24:35 -0700281 if (fixedSizeChanged || formatChanged) {
282 lcblk->reallocateAllExcept(index);
283 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700284 }
285
Mathias Agopian3330b202009-10-05 17:07:12 -0700286 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700287 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700288 err = buffer->reallocate(w, h, f, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700290 // here we have to reallocate a new buffer because we could have a
291 // client in our process with a reference to it (eg: status bar),
292 // and we can't release the handle under its feet.
293 buffer.clear();
Mathias Agopiana138f892010-05-21 17:24:35 -0700294 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700295 err = buffer->initCheck();
296 }
297
298 if (err || buffer->handle == 0) {
299 LOGE_IF(err || buffer->handle == 0,
300 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
301 this, index, w, h, strerror(-err));
302 } else {
303 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700304 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
305 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700306 }
307
308 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700309 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700310 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700311 }
312 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313}
314
Mathias Agopian3330b202009-10-05 17:07:12 -0700315uint32_t Layer::getEffectiveUsage(uint32_t usage) const
316{
317 /*
318 * buffers used for software rendering, but h/w composition
319 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
320 *
321 * buffers used for h/w rendering and h/w composition
322 * are allocated with HW_RENDER | HW_TEXTURE
323 *
324 * buffers used with h/w rendering and either NPOT or no egl_image_ext
325 * are allocated with SW_READ_RARELY | HW_RENDER
326 *
327 */
328
329 if (mSecure) {
330 // secure buffer, don't store it into the GPU
331 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
332 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
333 } else {
334 // it's allowed to modify the usage flags here, but generally
335 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700336 // request EGLImage for all buffers
337 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700338 }
339 return usage;
340}
341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342uint32_t Layer::doTransaction(uint32_t flags)
343{
344 const Layer::State& front(drawingState());
345 const Layer::State& temp(currentState());
346
Mathias Agopiana138f892010-05-21 17:24:35 -0700347 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
348 (front.requested_h != temp.requested_h);
349
350 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700351 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700353 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
354 this,
355 int(temp.requested_w), int(temp.requested_h),
356 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357
Mathias Agopiana138f892010-05-21 17:24:35 -0700358 if (!isFixedSize()) {
359 // we're being resized and there is a freeze display request,
360 // acquire a freeze lock, so that the screen stays put
361 // until we've redrawn at the new size; this is to avoid
362 // glitches upon orientation changes.
363 if (mFlinger->hasFreezeRequest()) {
364 // if the surface is hidden, don't try to acquire the
365 // freeze lock, since hidden surfaces may never redraw
366 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
367 mFreezeLock = mFlinger->getFreezeLock();
368 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700370
371 // this will make sure LayerBase::doTransaction doesn't update
372 // the drawing state's size
373 Layer::State& editDraw(mDrawingState);
374 editDraw.requested_w = temp.requested_w;
375 editDraw.requested_h = temp.requested_h;
376
377 // record the new size, form this point on, when the client request
378 // a buffer, it'll get the new size.
379 setBufferSize(temp.requested_w, temp.requested_h);
380
381 // all buffers need reallocation
382 lcblk->reallocateAll();
383 } else {
384 // record the new size
385 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 }
387 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 if (temp.sequence != front.sequence) {
390 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
391 // this surface is now hidden, so it shouldn't hold a freeze lock
392 // (it may never redraw, which is fine if it is hidden)
393 mFreezeLock.clear();
394 }
395 }
396
397 return LayerBase::doTransaction(flags);
398}
399
Mathias Agopiana138f892010-05-21 17:24:35 -0700400void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700401 Mutex::Autolock _l(mLock);
402 mWidth = w;
403 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404}
405
Mathias Agopiana138f892010-05-21 17:24:35 -0700406bool Layer::isFixedSize() const {
407 Mutex::Autolock _l(mLock);
408 return mFixedSize;
409}
410
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411// ----------------------------------------------------------------------------
412// pageflip handling...
413// ----------------------------------------------------------------------------
414
415void Layer::lockPageFlip(bool& recomputeVisibleRegions)
416{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700417 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700418 if (buf == NOT_ENOUGH_DATA) {
419 // NOTE: This is not an error, it simply means there is nothing to
420 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700421 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 return;
423 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700424
Mathias Agopiand606de62010-05-10 20:06:11 -0700425 if (buf < NO_ERROR) {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700426 LOGE("retireAndLock() buffer index (%d) out of range", buf);
427 mPostedDirtyRegion.clear();
428 return;
429 }
430
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700431 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700432 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
433 LOGE("retireAndLock() buffer index (%d) out of range", buf);
434 mPostedDirtyRegion.clear();
435 return;
436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700438 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700439 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700440 if (newFrontBuffer != NULL) {
441 // compute the posted region
442 const Region dirty(lcblk->getDirtyRegion(buf));
443 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700444
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700445 // update the layer size and release freeze-lock
446 const Layer::State& front(drawingState());
447 if (newFrontBuffer->getWidth() == front.requested_w &&
448 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700449 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700450 if ((front.w != front.requested_w) ||
451 (front.h != front.requested_h))
452 {
453 // Here we pretend the transaction happened by updating the
454 // current and drawing states. Drawing state is only accessed
455 // in this thread, no need to have it locked
456 Layer::State& editDraw(mDrawingState);
457 editDraw.w = editDraw.requested_w;
458 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700459
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700460 // We also need to update the current state so that we don't
461 // end-up doing too much work during the next transaction.
462 // NOTE: We actually don't need hold the transaction lock here
463 // because State::w and State::h are only accessed from
464 // this thread
465 Layer::State& editTemp(currentState());
466 editTemp.w = editDraw.w;
467 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700468
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700469 // recompute visible region
470 recomputeVisibleRegions = true;
471 }
472
473 // we now have the correct size, unfreeze the screen
474 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700475 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700476 } else {
477 // this should not happen unless we ran out of memory while
478 // allocating the buffer. we're hoping that things will get back
479 // to normal the next time the app tries to draw into this buffer.
480 // meanwhile, pretend the screen didn't update.
481 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700482 }
483
Mathias Agopiane7005012009-10-07 16:44:10 -0700484 if (lcblk->getQueuedCount()) {
485 // signal an event if we have more buffers waiting
486 mFlinger->signalEvent();
487 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488
Mathias Agopian245e4d72010-04-21 15:24:11 -0700489 /* a buffer was posted, so we need to call reloadTexture(), which
490 * will update our internal data structures (eg: EGLImageKHR or
491 * texture names). we need to do this even if mPostedDirtyRegion is
492 * empty -- it's orthogonal to the fact that a new buffer was posted,
493 * for instance, a degenerate case could be that the user did an empty
494 * update but repainted the buffer with appropriate content (after a
495 * resize for instance).
496 */
497 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498}
499
500void Layer::unlockPageFlip(
501 const Transform& planeTransform, Region& outDirtyRegion)
502{
503 Region dirtyRegion(mPostedDirtyRegion);
504 if (!dirtyRegion.isEmpty()) {
505 mPostedDirtyRegion.clear();
506 // The dirty region is given in the layer's coordinate space
507 // transform the dirty region by the surface's transformation
508 // and the global transformation.
509 const Layer::State& s(drawingState());
510 const Transform tr(planeTransform * s.transform);
511 dirtyRegion = tr.transform(dirtyRegion);
512
513 // At this point, the dirty region is in screen space.
514 // Make sure it's constrained by the visible region (which
515 // is in screen space as well).
516 dirtyRegion.andSelf(visibleRegionScreen);
517 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800519 if (visibleRegionScreen.isEmpty()) {
520 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700521 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800522 mFreezeLock.clear();
523 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524}
525
526void Layer::finishPageFlip()
527{
Mathias Agopiand606de62010-05-10 20:06:11 -0700528 int buf = mBufferManager.getActiveBufferIndex();
529 status_t err = lcblk->unlock( buf );
530 LOGE_IF(err!=NO_ERROR, "layer %p, buffer=%d wasn't locked!", this, buf);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531}
532
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700533
534void Layer::dump(String8& result, char* buffer, size_t SIZE) const
535{
536 LayerBaseClient::dump(result, buffer, SIZE);
537
538 SharedBufferStack::Statistics stats = lcblk->getStats();
539 result.append( lcblk->dump(" ") );
540 sp<const GraphicBuffer> buf0(getBuffer(0));
541 sp<const GraphicBuffer> buf1(getBuffer(1));
542 uint32_t w0=0, h0=0, s0=0;
543 uint32_t w1=0, h1=0, s1=0;
544 if (buf0 != 0) {
545 w0 = buf0->getWidth();
546 h0 = buf0->getHeight();
547 s0 = buf0->getStride();
548 }
549 if (buf1 != 0) {
550 w1 = buf1->getWidth();
551 h1 = buf1->getHeight();
552 s1 = buf1->getStride();
553 }
554 snprintf(buffer, SIZE,
555 " "
556 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
557 " freezeLock=%p, dq-q-time=%u us\n",
558 pixelFormat(),
559 w0, h0, s0, w1, h1, s1,
560 getFreezeLock().get(), stats.totalTime);
561
562 result.append(buffer);
563}
564
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565// ---------------------------------------------------------------------------
566
Mathias Agopiand606de62010-05-10 20:06:11 -0700567Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700568 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
569 mActiveBuffer(0), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700570{
571}
572
Mathias Agopianbb641242010-05-18 17:06:55 -0700573Layer::BufferManager::~BufferManager()
574{
575}
576
577status_t Layer::BufferManager::resize(size_t size)
578{
579 Mutex::Autolock _l(mLock);
580 mNumBuffers = size;
581 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700582}
583
584// only for debugging
585sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
586 return mBufferData[index].buffer;
587}
588
589status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
590 // TODO: need to validate 'index'
591 mActiveBuffer = index;
592 return NO_ERROR;
593}
594
595size_t Layer::BufferManager::getActiveBufferIndex() const {
596 return mActiveBuffer;
597}
598
599Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700600 Texture res;
601 if (mFailover) {
602 res = mFailoverTexture;
603 } else {
604 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
605 }
606 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700607}
608
609sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700610 const size_t activeBuffer = mActiveBuffer;
611 BufferData const * const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700612 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700613 return buffers[activeBuffer].buffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700614}
615
616sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
617{
Mathias Agopianbb641242010-05-18 17:06:55 -0700618 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700619 sp<GraphicBuffer> buffer;
620 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700621 buffer = buffers[index].buffer;
622 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700623 return buffer;
624}
625
626status_t Layer::BufferManager::attachBuffer(size_t index,
627 const sp<GraphicBuffer>& buffer)
628{
Mathias Agopianbb641242010-05-18 17:06:55 -0700629 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700630 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700631 buffers[index].buffer = buffer;
632 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700633 return NO_ERROR;
634}
635
636status_t Layer::BufferManager::destroy(EGLDisplay dpy)
637{
Mathias Agopianbb641242010-05-18 17:06:55 -0700638 BufferData* const buffers = mBufferData;
639 size_t num;
640 { // scope for the lock
641 Mutex::Autolock _l(mLock);
642 num = mNumBuffers;
643 for (size_t i=0 ; i<num ; i++) {
644 buffers[i].buffer = 0;
645 }
646 }
647 for (size_t i=0 ; i<num ; i++) {
648 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700649 }
650 destroyTexture(&mFailoverTexture, dpy);
651 return NO_ERROR;
652}
653
654status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
655 const sp<GraphicBuffer>& buffer)
656{
657 size_t index = mActiveBuffer;
Mathias Agopianbb641242010-05-18 17:06:55 -0700658 Image& texture(mBufferData[index].texture);
Mathias Agopiand606de62010-05-10 20:06:11 -0700659 status_t err = mTextureManager.initEglImage(&texture, dpy, buffer);
660 // if EGLImage fails, we switch to regular texture mode, and we
661 // free all resources associated with using EGLImages.
662 if (err == NO_ERROR) {
663 mFailover = false;
664 destroyTexture(&mFailoverTexture, dpy);
665 } else {
666 mFailover = true;
Mathias Agopianbb641242010-05-18 17:06:55 -0700667 const size_t num = mNumBuffers;
668 for (size_t i=0 ; i<num ; i++) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700669 destroyTexture(&mBufferData[i].texture, dpy);
670 }
671 }
672 return err;
673}
674
675status_t Layer::BufferManager::loadTexture(
676 const Region& dirty, const GGLSurface& t)
677{
678 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
679}
680
Mathias Agopianbb641242010-05-18 17:06:55 -0700681status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
682{
683 if (tex->name != -1U) {
684 glDeleteTextures(1, &tex->name);
685 tex->name = -1U;
686 }
687 if (tex->image != EGL_NO_IMAGE_KHR) {
688 eglDestroyImageKHR(dpy, tex->image);
689 tex->image = EGL_NO_IMAGE_KHR;
690 }
691 return NO_ERROR;
692}
693
Mathias Agopiand606de62010-05-10 20:06:11 -0700694// ---------------------------------------------------------------------------
695
Mathias Agopian9a112062009-04-17 19:36:26 -0700696Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
697 SurfaceID id, const sp<Layer>& owner)
698 : Surface(flinger, id, owner->getIdentity(), owner)
699{
700}
701
702Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700703{
704}
705
Mathias Agopiana138f892010-05-21 17:24:35 -0700706sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
707 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708{
Mathias Agopian3330b202009-10-05 17:07:12 -0700709 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700710 sp<Layer> owner(getOwner());
711 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700712 /*
713 * requestBuffer() cannot be called from the main thread
714 * as it could cause a dead-lock, since it may have to wait
715 * on conditions updated my the main thread.
716 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700717 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700718 }
719 return buffer;
720}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700722status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
723{
724 status_t err = DEAD_OBJECT;
725 sp<Layer> owner(getOwner());
726 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700727 /*
728 * setBufferCount() cannot be called from the main thread
729 * as it could cause a dead-lock, since it may have to wait
730 * on conditions updated my the main thread.
731 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700732 err = owner->setBufferCount(bufferCount);
733 }
734 return err;
735}
736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737// ---------------------------------------------------------------------------
738
739
740}; // namespace android