blob: 3fbb4d386fe8f4fefbfefdee1693e442a370553c [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 <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian6950e422009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33#include "clz.h"
34#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "DisplayHardware/DisplayHardware.h"
37
38
39#define DEBUG_RESIZE 0
40
41
42namespace android {
43
Mathias Agopian967dce32010-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 Project9066cfe2009-03-03 19:31:44 -080048// ---------------------------------------------------------------------------
49
Mathias Agopian9779b222009-09-07 16:32:45 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopian9f2c4fd2010-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 Project9066cfe2009-03-03 19:31:44 -080054 mSecure(false),
Mathias Agopiancc934762009-09-23 19:16:27 -070055 mNeedsBlending(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070056 mNeedsDithering(false),
57 mTextureManager(mFlags),
Mathias Agopian2be352a2010-05-21 17:24:35 -070058 mBufferManager(mTextureManager),
59 mWidth(0), mHeight(0), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060{
61 // no OpenGL operation is possible here, since we might not be
62 // in the OpenGL thread.
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070063 lcblk = new SharedBufferServer(
Mathias Agopian898c4c92010-05-18 17:06:55 -070064 client->ctrlblk, i, mBufferManager.getDefaultBufferCount(),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070065 getIdentity());
66
67 mBufferManager.setActiveBufferIndex( lcblk->getFrontBuffer() );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
70Layer::~Layer()
71{
Mathias Agopian898c4c92010-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 Agopiana3aa6c92009-04-22 15:23:34 -070076 // the actual buffers will be destroyed here
Mathias Agopian9f2c4fd2010-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 Agopian248b5bd2009-09-10 19:41:18 -070086}
Mathias Agopian9779b222009-09-07 16:32:45 -070087
Mathias Agopian1473f462009-04-10 14:24:30 -070088sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089{
90 return mSurface;
91}
92
Mathias Agopian6cf0db22009-04-17 19:36:26 -070093status_t Layer::ditch()
94{
Mathias Agopian898c4c92010-05-18 17:06:55 -070095 // NOTE: Called from the main UI thread
96
Mathias Agopiana3aa6c92009-04-22 15:23:34 -070097 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -080098 mFreezeLock.clear();
Mathias Agopian898c4c92010-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 Agopian6cf0db22009-04-17 19:36:26 -0700106 return NO_ERROR;
107}
108
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700109status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 PixelFormat format, uint32_t flags)
111{
Mathias Agopiancc934762009-09-23 19:16:27 -0700112 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 PixelFormatInfo info;
114 status_t err = getPixelFormatInfo(format, &info);
115 if (err) return err;
116
Mathias Agopiancc934762009-09-23 19:16:27 -0700117 // the display's pixel format
118 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-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 Agopiancc934762009-09-23 19:16:27 -0700128 PixelFormatInfo displayInfo;
129 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700130 const uint32_t hwFlags = hw.getFlags();
131
Mathias Agopian9779b222009-09-07 16:32:45 -0700132 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700133 mWidth = w;
Mathias Agopian9779b222009-09-07 16:32:45 -0700134 mHeight = h;
Mathias Agopian6950e422009-10-05 17:07:12 -0700135 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700137
Mathias Agopiancc934762009-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 Agopian6cf0db22009-04-17 19:36:26 -0700143 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 return NO_ERROR;
145}
146
147void Layer::reloadTexture(const Region& dirty)
148{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700149 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-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 Agopian9042b452009-10-26 20:12:37 -0700157#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian382e17d2009-10-21 16:27:21 -0700158 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopian9f2c4fd2010-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 Agopian1473f462009-04-10 14:24:30 -0700164 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700165 } else
166#endif
167 {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800168slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700169 GGLSurface t;
Mathias Agopian6950e422009-10-05 17:07:12 -0700170 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopiandff8e582009-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 Agopian9f2c4fd2010-05-10 20:06:11 -0700174 mBufferManager.loadTexture(dirty, t);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700175 buffer->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178}
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180void Layer::onDraw(const Region& clip) const
181{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700182 Texture tex(mBufferManager.getActiveTexture());
183 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-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 Project9066cfe2009-03-03 19:31:44 -0800207 return;
208 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700209 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210}
211
Mathias Agopian92377032010-05-26 22:08:52 -0700212bool Layer::needsFiltering() const
213{
214 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
215 // NOTE: there is a race here, because mFixedSize is updated in a
216 // binder transaction. however, it doesn't really matter since it is
217 // evaluated each time we draw. To be perfectly correct, this flag
218 // would have to be associated with a buffer.
219 if (mFixedSize)
220 return true;
221 }
222 return LayerBase::needsFiltering();
223}
224
Mathias Agopian59751db2010-05-07 15:58:44 -0700225
226status_t Layer::setBufferCount(int bufferCount)
227{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700228 // Ensures our client doesn't go away while we're accessing
Mathias Agopian59751db2010-05-07 15:58:44 -0700229 // the shared area.
230 sp<Client> ourClient(client.promote());
231 if (ourClient == 0) {
232 // oops, the client is already gone
233 return DEAD_OBJECT;
234 }
235
Mathias Agopian898c4c92010-05-18 17:06:55 -0700236 // NOTE: lcblk->resize() is protected by an internal lock
237 status_t err = lcblk->resize(bufferCount);
238 if (err == NO_ERROR)
239 mBufferManager.resize(bufferCount);
Mathias Agopian59751db2010-05-07 15:58:44 -0700240
241 return err;
242}
243
Mathias Agopian2be352a2010-05-21 17:24:35 -0700244sp<GraphicBuffer> Layer::requestBuffer(int index,
245 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
246 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247{
Mathias Agopian6950e422009-10-05 17:07:12 -0700248 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700249
Mathias Agopian2be352a2010-05-21 17:24:35 -0700250 if ((reqWidth | reqHeight | reqFormat) < 0)
251 return buffer;
252
253 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
254 return buffer;
255
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700256 // this ensures our client doesn't go away while we're accessing
257 // the shared area.
258 sp<Client> ourClient(client.promote());
259 if (ourClient == 0) {
260 // oops, the client is already gone
261 return buffer;
262 }
263
Mathias Agopian1473f462009-04-10 14:24:30 -0700264 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700265 * This is called from the client's Surface::dequeue(). This can happen
266 * at any time, especially while we're in the middle of using the
267 * buffer 'index' as our front buffer.
Mathias Agopian898c4c92010-05-18 17:06:55 -0700268 *
Mathias Agopian9779b222009-09-07 16:32:45 -0700269 * Make sure the buffer we're resizing is not the front buffer and has been
270 * dequeued. Once this condition is asserted, we are guaranteed that this
271 * buffer cannot become the front buffer under our feet, since we're called
272 * from Surface::dequeue()
Mathias Agopian1473f462009-04-10 14:24:30 -0700273 */
Mathias Agopian9779b222009-09-07 16:32:45 -0700274 status_t err = lcblk->assertReallocate(index);
275 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700276 if (err != NO_ERROR) {
277 // the surface may have died
278 return buffer;
279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
Mathias Agopian2be352a2010-05-21 17:24:35 -0700281 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700282 { // scope for the lock
283 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700284 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
285 const bool formatChanged = mReqFormat != reqFormat;
286 mReqWidth = reqWidth;
287 mReqHeight = reqHeight;
288 mReqFormat = reqFormat;
289 mFixedSize = reqWidth && reqHeight;
290 w = reqWidth ? reqWidth : mWidth;
291 h = reqHeight ? reqHeight : mHeight;
292 f = reqFormat ? reqFormat : mFormat;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700293 buffer = mBufferManager.detachBuffer(index);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700294 if (fixedSizeChanged || formatChanged) {
295 lcblk->reallocateAllExcept(index);
296 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700297 }
298
Mathias Agopian6950e422009-10-05 17:07:12 -0700299 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopianac7f13b2009-09-17 19:19:08 -0700300 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopian2be352a2010-05-21 17:24:35 -0700301 err = buffer->reallocate(w, h, f, effectiveUsage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700302 } else {
Mathias Agopian9779b222009-09-07 16:32:45 -0700303 // here we have to reallocate a new buffer because we could have a
304 // client in our process with a reference to it (eg: status bar),
305 // and we can't release the handle under its feet.
306 buffer.clear();
Mathias Agopian2be352a2010-05-21 17:24:35 -0700307 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
Mathias Agopian9779b222009-09-07 16:32:45 -0700308 err = buffer->initCheck();
309 }
310
311 if (err || buffer->handle == 0) {
312 LOGE_IF(err || buffer->handle == 0,
313 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
314 this, index, w, h, strerror(-err));
315 } else {
316 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700317 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
318 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700319 }
320
321 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700322 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700323 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700324 }
325 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326}
327
Mathias Agopian6950e422009-10-05 17:07:12 -0700328uint32_t Layer::getEffectiveUsage(uint32_t usage) const
329{
330 /*
331 * buffers used for software rendering, but h/w composition
332 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
333 *
334 * buffers used for h/w rendering and h/w composition
335 * are allocated with HW_RENDER | HW_TEXTURE
336 *
337 * buffers used with h/w rendering and either NPOT or no egl_image_ext
338 * are allocated with SW_READ_RARELY | HW_RENDER
339 *
340 */
341
342 if (mSecure) {
343 // secure buffer, don't store it into the GPU
344 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
345 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
346 } else {
347 // it's allowed to modify the usage flags here, but generally
348 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700349 // request EGLImage for all buffers
350 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700351 }
352 return usage;
353}
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355uint32_t Layer::doTransaction(uint32_t flags)
356{
357 const Layer::State& front(drawingState());
358 const Layer::State& temp(currentState());
359
Mathias Agopian2be352a2010-05-21 17:24:35 -0700360 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
361 (front.requested_h != temp.requested_h);
362
363 if (sizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700364 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700366 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
367 this,
368 int(temp.requested_w), int(temp.requested_h),
369 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
Mathias Agopian2be352a2010-05-21 17:24:35 -0700371 if (!isFixedSize()) {
372 // we're being resized and there is a freeze display request,
373 // acquire a freeze lock, so that the screen stays put
374 // until we've redrawn at the new size; this is to avoid
375 // glitches upon orientation changes.
376 if (mFlinger->hasFreezeRequest()) {
377 // if the surface is hidden, don't try to acquire the
378 // freeze lock, since hidden surfaces may never redraw
379 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
380 mFreezeLock = mFlinger->getFreezeLock();
381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700383
384 // this will make sure LayerBase::doTransaction doesn't update
385 // the drawing state's size
386 Layer::State& editDraw(mDrawingState);
387 editDraw.requested_w = temp.requested_w;
388 editDraw.requested_h = temp.requested_h;
389
390 // record the new size, form this point on, when the client request
391 // a buffer, it'll get the new size.
392 setBufferSize(temp.requested_w, temp.requested_h);
393
394 // all buffers need reallocation
395 lcblk->reallocateAll();
396 } else {
397 // record the new size
398 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 if (temp.sequence != front.sequence) {
403 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
404 // this surface is now hidden, so it shouldn't hold a freeze lock
405 // (it may never redraw, which is fine if it is hidden)
406 mFreezeLock.clear();
407 }
408 }
409
410 return LayerBase::doTransaction(flags);
411}
412
Mathias Agopian2be352a2010-05-21 17:24:35 -0700413void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700414 Mutex::Autolock _l(mLock);
415 mWidth = w;
416 mHeight = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417}
418
Mathias Agopian2be352a2010-05-21 17:24:35 -0700419bool Layer::isFixedSize() const {
420 Mutex::Autolock _l(mLock);
421 return mFixedSize;
422}
423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424// ----------------------------------------------------------------------------
425// pageflip handling...
426// ----------------------------------------------------------------------------
427
428void Layer::lockPageFlip(bool& recomputeVisibleRegions)
429{
Mathias Agopian9779b222009-09-07 16:32:45 -0700430 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700431 if (buf == NOT_ENOUGH_DATA) {
432 // NOTE: This is not an error, it simply means there is nothing to
433 // retire. The buffer is locked because we will use it
Mathias Agopian9779b222009-09-07 16:32:45 -0700434 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 return;
436 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700437
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700438 if (buf < NO_ERROR) {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700439 LOGE("retireAndLock() buffer index (%d) out of range", buf);
440 mPostedDirtyRegion.clear();
441 return;
442 }
443
Mathias Agopian9779b222009-09-07 16:32:45 -0700444 // we retired a buffer, which becomes the new front buffer
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700445 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
446 LOGE("retireAndLock() buffer index (%d) out of range", buf);
447 mPostedDirtyRegion.clear();
448 return;
449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450
Mathias Agopian9779b222009-09-07 16:32:45 -0700451 // get the dirty region
Mathias Agopian6950e422009-10-05 17:07:12 -0700452 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700453 if (newFrontBuffer != NULL) {
454 // compute the posted region
455 const Region dirty(lcblk->getDirtyRegion(buf));
456 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700457
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700458 // update the layer size and release freeze-lock
459 const Layer::State& front(drawingState());
460 if (newFrontBuffer->getWidth() == front.requested_w &&
461 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700462 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700463 if ((front.w != front.requested_w) ||
464 (front.h != front.requested_h))
465 {
466 // Here we pretend the transaction happened by updating the
467 // current and drawing states. Drawing state is only accessed
468 // in this thread, no need to have it locked
469 Layer::State& editDraw(mDrawingState);
470 editDraw.w = editDraw.requested_w;
471 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700472
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700473 // We also need to update the current state so that we don't
474 // end-up doing too much work during the next transaction.
475 // NOTE: We actually don't need hold the transaction lock here
476 // because State::w and State::h are only accessed from
477 // this thread
478 Layer::State& editTemp(currentState());
479 editTemp.w = editDraw.w;
480 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700481
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700482 // recompute visible region
483 recomputeVisibleRegions = true;
484 }
485
486 // we now have the correct size, unfreeze the screen
487 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700488 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700489 } else {
490 // this should not happen unless we ran out of memory while
491 // allocating the buffer. we're hoping that things will get back
492 // to normal the next time the app tries to draw into this buffer.
493 // meanwhile, pretend the screen didn't update.
494 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700495 }
496
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700497 if (lcblk->getQueuedCount()) {
498 // signal an event if we have more buffers waiting
499 mFlinger->signalEvent();
500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700502 /* a buffer was posted, so we need to call reloadTexture(), which
503 * will update our internal data structures (eg: EGLImageKHR or
504 * texture names). we need to do this even if mPostedDirtyRegion is
505 * empty -- it's orthogonal to the fact that a new buffer was posted,
506 * for instance, a degenerate case could be that the user did an empty
507 * update but repainted the buffer with appropriate content (after a
508 * resize for instance).
509 */
510 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511}
512
513void Layer::unlockPageFlip(
514 const Transform& planeTransform, Region& outDirtyRegion)
515{
516 Region dirtyRegion(mPostedDirtyRegion);
517 if (!dirtyRegion.isEmpty()) {
518 mPostedDirtyRegion.clear();
519 // The dirty region is given in the layer's coordinate space
520 // transform the dirty region by the surface's transformation
521 // and the global transformation.
522 const Layer::State& s(drawingState());
523 const Transform tr(planeTransform * s.transform);
524 dirtyRegion = tr.transform(dirtyRegion);
525
526 // At this point, the dirty region is in screen space.
527 // Make sure it's constrained by the visible region (which
528 // is in screen space as well).
529 dirtyRegion.andSelf(visibleRegionScreen);
530 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800532 if (visibleRegionScreen.isEmpty()) {
533 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700534 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800535 mFreezeLock.clear();
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537}
538
539void Layer::finishPageFlip()
540{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700541 int buf = mBufferManager.getActiveBufferIndex();
542 status_t err = lcblk->unlock( buf );
543 LOGE_IF(err!=NO_ERROR, "layer %p, buffer=%d wasn't locked!", this, buf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544}
545
Mathias Agopian9bce8732010-04-20 17:55:49 -0700546
547void Layer::dump(String8& result, char* buffer, size_t SIZE) const
548{
549 LayerBaseClient::dump(result, buffer, SIZE);
550
551 SharedBufferStack::Statistics stats = lcblk->getStats();
552 result.append( lcblk->dump(" ") );
553 sp<const GraphicBuffer> buf0(getBuffer(0));
554 sp<const GraphicBuffer> buf1(getBuffer(1));
555 uint32_t w0=0, h0=0, s0=0;
556 uint32_t w1=0, h1=0, s1=0;
557 if (buf0 != 0) {
558 w0 = buf0->getWidth();
559 h0 = buf0->getHeight();
560 s0 = buf0->getStride();
561 }
562 if (buf1 != 0) {
563 w1 = buf1->getWidth();
564 h1 = buf1->getHeight();
565 s1 = buf1->getStride();
566 }
567 snprintf(buffer, SIZE,
568 " "
569 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
570 " freezeLock=%p, dq-q-time=%u us\n",
571 pixelFormat(),
572 w0, h0, s0, w1, h1, s1,
573 getFreezeLock().get(), stats.totalTime);
574
575 result.append(buffer);
576}
577
Mathias Agopian1473f462009-04-10 14:24:30 -0700578// ---------------------------------------------------------------------------
579
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700580Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700581 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
582 mActiveBuffer(0), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700583{
584}
585
Mathias Agopian898c4c92010-05-18 17:06:55 -0700586Layer::BufferManager::~BufferManager()
587{
588}
589
590status_t Layer::BufferManager::resize(size_t size)
591{
592 Mutex::Autolock _l(mLock);
593 mNumBuffers = size;
594 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700595}
596
597// only for debugging
598sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
599 return mBufferData[index].buffer;
600}
601
602status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
603 // TODO: need to validate 'index'
604 mActiveBuffer = index;
605 return NO_ERROR;
606}
607
608size_t Layer::BufferManager::getActiveBufferIndex() const {
609 return mActiveBuffer;
610}
611
612Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700613 Texture res;
614 if (mFailover) {
615 res = mFailoverTexture;
616 } else {
617 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
618 }
619 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700620}
621
622sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700623 const size_t activeBuffer = mActiveBuffer;
624 BufferData const * const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700625 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700626 return buffers[activeBuffer].buffer;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700627}
628
629sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
630{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700631 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700632 sp<GraphicBuffer> buffer;
633 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700634 buffer = buffers[index].buffer;
635 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700636 return buffer;
637}
638
639status_t Layer::BufferManager::attachBuffer(size_t index,
640 const sp<GraphicBuffer>& buffer)
641{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700642 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700643 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700644 buffers[index].buffer = buffer;
645 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700646 return NO_ERROR;
647}
648
649status_t Layer::BufferManager::destroy(EGLDisplay dpy)
650{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700651 BufferData* const buffers = mBufferData;
652 size_t num;
653 { // scope for the lock
654 Mutex::Autolock _l(mLock);
655 num = mNumBuffers;
656 for (size_t i=0 ; i<num ; i++) {
657 buffers[i].buffer = 0;
658 }
659 }
660 for (size_t i=0 ; i<num ; i++) {
661 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700662 }
663 destroyTexture(&mFailoverTexture, dpy);
664 return NO_ERROR;
665}
666
667status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
668 const sp<GraphicBuffer>& buffer)
669{
670 size_t index = mActiveBuffer;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700671 Image& texture(mBufferData[index].texture);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700672 status_t err = mTextureManager.initEglImage(&texture, dpy, buffer);
673 // if EGLImage fails, we switch to regular texture mode, and we
674 // free all resources associated with using EGLImages.
675 if (err == NO_ERROR) {
676 mFailover = false;
677 destroyTexture(&mFailoverTexture, dpy);
678 } else {
679 mFailover = true;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700680 const size_t num = mNumBuffers;
681 for (size_t i=0 ; i<num ; i++) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700682 destroyTexture(&mBufferData[i].texture, dpy);
683 }
684 }
685 return err;
686}
687
688status_t Layer::BufferManager::loadTexture(
689 const Region& dirty, const GGLSurface& t)
690{
691 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
692}
693
Mathias Agopian898c4c92010-05-18 17:06:55 -0700694status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
695{
696 if (tex->name != -1U) {
697 glDeleteTextures(1, &tex->name);
698 tex->name = -1U;
699 }
700 if (tex->image != EGL_NO_IMAGE_KHR) {
701 eglDestroyImageKHR(dpy, tex->image);
702 tex->image = EGL_NO_IMAGE_KHR;
703 }
704 return NO_ERROR;
705}
706
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700707// ---------------------------------------------------------------------------
708
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700709Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
710 SurfaceID id, const sp<Layer>& owner)
711 : Surface(flinger, id, owner->getIdentity(), owner)
712{
713}
714
715Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -0700716{
717}
718
Mathias Agopian2be352a2010-05-21 17:24:35 -0700719sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
720 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700721{
Mathias Agopian6950e422009-10-05 17:07:12 -0700722 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700723 sp<Layer> owner(getOwner());
724 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700725 /*
726 * requestBuffer() cannot be called from the main thread
727 * as it could cause a dead-lock, since it may have to wait
728 * on conditions updated my the main thread.
729 */
Mathias Agopian2be352a2010-05-21 17:24:35 -0700730 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700731 }
732 return buffer;
733}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
Mathias Agopian59751db2010-05-07 15:58:44 -0700735status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
736{
737 status_t err = DEAD_OBJECT;
738 sp<Layer> owner(getOwner());
739 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700740 /*
741 * setBufferCount() cannot be called from the main thread
742 * as it could cause a dead-lock, since it may have to wait
743 * on conditions updated my the main thread.
744 */
Mathias Agopian59751db2010-05-07 15:58:44 -0700745 err = owner->setBufferCount(bufferCount);
746 }
747 return err;
748}
749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750// ---------------------------------------------------------------------------
751
752
753}; // namespace android