blob: 85149f5f5a451ac30c88f19e4052d29ddca6f4a8 [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 Agopian96f08192010-06-02 23:28:45 -070050Layer::Layer(SurfaceFlinger* flinger,
51 DisplayID display, const sp<Client>& client)
52 : LayerBaseClient(flinger, display, client),
Mathias Agopiand606de62010-05-10 20:06:11 -070053 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{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061}
62
63Layer::~Layer()
64{
Mathias Agopianbb641242010-05-18 17:06:55 -070065 // FIXME: must be called from the main UI thread
66 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
67 mBufferManager.destroy(dpy);
68
Mathias Agopian0aa758d2009-04-22 15:23:34 -070069 // the actual buffers will be destroyed here
Mathias Agopiand606de62010-05-10 20:06:11 -070070 delete lcblk;
71}
72
Mathias Agopian96f08192010-06-02 23:28:45 -070073// TODO: get rid of this
74void Layer::setToken(int32_t token)
75{
76 sp<Client> ourClient(client.promote());
77
78 mToken = token;
79
80 // no OpenGL operation is possible here, since we might not be
81 // in the OpenGL thread.
82 lcblk = new SharedBufferServer(
83 ourClient->ctrlblk, token, mBufferManager.getDefaultBufferCount(),
84 getIdentity());
85
86 mBufferManager.setActiveBufferIndex( lcblk->getFrontBuffer() );
87}
88
Mathias Agopiand606de62010-05-10 20:06:11 -070089// called with SurfaceFlinger::mStateLock as soon as the layer is entered
90// in the purgatory list
91void Layer::onRemoved()
92{
Mathias Agopian96f08192010-06-02 23:28:45 -070093 sp<Client> ourClient(client.promote());
94 if (ourClient != 0) {
95 // wake up the condition
96 lcblk->setStatus(NO_INIT);
97 }
Mathias Agopian48d819a2009-09-10 19:41:18 -070098}
Mathias Agopiancbb288b2009-09-07 16:32:45 -070099
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101{
102 return mSurface;
103}
104
Mathias Agopian9a112062009-04-17 19:36:26 -0700105status_t Layer::ditch()
106{
Mathias Agopianbb641242010-05-18 17:06:55 -0700107 // NOTE: Called from the main UI thread
108
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700109 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800110 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700111
112 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
113 mBufferManager.destroy(dpy);
114 mSurface.clear();
115
116 Mutex::Autolock _l(mLock);
117 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700118 return NO_ERROR;
119}
120
Mathias Agopianf9d93272009-06-19 17:00:27 -0700121status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 PixelFormat format, uint32_t flags)
123{
Mathias Agopian401c2572009-09-23 19:16:27 -0700124 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 PixelFormatInfo info;
126 status_t err = getPixelFormatInfo(format, &info);
127 if (err) return err;
128
Mathias Agopian401c2572009-09-23 19:16:27 -0700129 // the display's pixel format
130 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700131 uint32_t const maxSurfaceDims = min(
132 hw.getMaxTextureSize(), hw.getMaxViewportDims());
133
134 // never allow a surface larger than what our underlying GL implementation
135 // can handle.
136 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
137 return BAD_VALUE;
138 }
139
Mathias Agopian401c2572009-09-23 19:16:27 -0700140 PixelFormatInfo displayInfo;
141 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700142 const uint32_t hwFlags = hw.getFlags();
143
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700144 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700145 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700146 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700147 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700149
Mathias Agopian401c2572009-09-23 19:16:27 -0700150 // we use the red index
151 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
152 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
153 mNeedsDithering = layerRedsize > displayRedSize;
154
Mathias Agopian96f08192010-06-02 23:28:45 -0700155 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 return NO_ERROR;
157}
158
159void Layer::reloadTexture(const Region& dirty)
160{
Mathias Agopiand606de62010-05-10 20:06:11 -0700161 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800162 if (buffer == NULL) {
163 // this situation can happen if we ran out of memory for instance.
164 // not much we can do. continue to use whatever texture was bound
165 // to this context.
166 return;
167 }
168
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700169#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700170 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700171 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
172 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
173 // not sure what we can do here...
174 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
175 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700177 } else
178#endif
179 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800180slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700181 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700183 LOGE_IF(res, "error %d (%s) locking buffer %p",
184 res, strerror(res), buffer.get());
185 if (res == NO_ERROR) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700186 mBufferManager.loadTexture(dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700187 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190}
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192void Layer::onDraw(const Region& clip) const
193{
Mathias Agopiand606de62010-05-10 20:06:11 -0700194 Texture tex(mBufferManager.getActiveTexture());
195 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700197 // in fact never been drawn into. This happens frequently with
198 // SurfaceView because the WindowManager can't know when the client
199 // has drawn the first time.
200
201 // If there is nothing under us, we paint the screen in black, otherwise
202 // we just skip this update.
203
204 // figure out if there is something below us
205 Region under;
206 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
207 const size_t count = drawingLayers.size();
208 for (size_t i=0 ; i<count ; ++i) {
209 const sp<LayerBase>& layer(drawingLayers[i]);
210 if (layer.get() == static_cast<LayerBase const*>(this))
211 break;
212 under.orSelf(layer->visibleRegionScreen);
213 }
214 // if not everything below us is covered, we plug the holes!
215 Region holes(clip.subtract(under));
216 if (!holes.isEmpty()) {
217 clearWithOpenGL(holes);
218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 return;
220 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700221 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222}
223
Mathias Agopiana7f66922010-05-26 22:08:52 -0700224bool Layer::needsFiltering() const
225{
226 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
227 // NOTE: there is a race here, because mFixedSize is updated in a
228 // binder transaction. however, it doesn't really matter since it is
229 // evaluated each time we draw. To be perfectly correct, this flag
230 // would have to be associated with a buffer.
231 if (mFixedSize)
232 return true;
233 }
234 return LayerBase::needsFiltering();
235}
236
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700237
238status_t Layer::setBufferCount(int bufferCount)
239{
Mathias Agopianbb641242010-05-18 17:06:55 -0700240 // Ensures our client doesn't go away while we're accessing
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700241 // the shared area.
242 sp<Client> ourClient(client.promote());
243 if (ourClient == 0) {
244 // oops, the client is already gone
245 return DEAD_OBJECT;
246 }
247
Mathias Agopianbb641242010-05-18 17:06:55 -0700248 // NOTE: lcblk->resize() is protected by an internal lock
249 status_t err = lcblk->resize(bufferCount);
250 if (err == NO_ERROR)
251 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700252
253 return err;
254}
255
Mathias Agopiana138f892010-05-21 17:24:35 -0700256sp<GraphicBuffer> Layer::requestBuffer(int index,
257 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
258 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259{
Mathias Agopian3330b202009-10-05 17:07:12 -0700260 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700261
Mathias Agopiana138f892010-05-21 17:24:35 -0700262 if ((reqWidth | reqHeight | reqFormat) < 0)
263 return buffer;
264
265 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
266 return buffer;
267
Mathias Agopian48d819a2009-09-10 19:41:18 -0700268 // this ensures our client doesn't go away while we're accessing
269 // the shared area.
270 sp<Client> ourClient(client.promote());
271 if (ourClient == 0) {
272 // oops, the client is already gone
273 return buffer;
274 }
275
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700276 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700277 * This is called from the client's Surface::dequeue(). This can happen
278 * at any time, especially while we're in the middle of using the
279 * buffer 'index' as our front buffer.
Mathias Agopianbb641242010-05-18 17:06:55 -0700280 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700281 * Make sure the buffer we're resizing is not the front buffer and has been
282 * dequeued. Once this condition is asserted, we are guaranteed that this
283 * buffer cannot become the front buffer under our feet, since we're called
284 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700286 status_t err = lcblk->assertReallocate(index);
287 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700288 if (err != NO_ERROR) {
289 // the surface may have died
290 return buffer;
291 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
Mathias Agopiana138f892010-05-21 17:24:35 -0700293 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700294 { // scope for the lock
295 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700296 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
297 const bool formatChanged = mReqFormat != reqFormat;
298 mReqWidth = reqWidth;
299 mReqHeight = reqHeight;
300 mReqFormat = reqFormat;
301 mFixedSize = reqWidth && reqHeight;
302 w = reqWidth ? reqWidth : mWidth;
303 h = reqHeight ? reqHeight : mHeight;
304 f = reqFormat ? reqFormat : mFormat;
Mathias Agopiand606de62010-05-10 20:06:11 -0700305 buffer = mBufferManager.detachBuffer(index);
Mathias Agopiana138f892010-05-21 17:24:35 -0700306 if (fixedSizeChanged || formatChanged) {
307 lcblk->reallocateAllExcept(index);
308 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700309 }
310
Mathias Agopian3330b202009-10-05 17:07:12 -0700311 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700312 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700313 err = buffer->reallocate(w, h, f, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700315 // here we have to reallocate a new buffer because we could have a
316 // client in our process with a reference to it (eg: status bar),
317 // and we can't release the handle under its feet.
318 buffer.clear();
Mathias Agopiana138f892010-05-21 17:24:35 -0700319 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700320 err = buffer->initCheck();
321 }
322
323 if (err || buffer->handle == 0) {
324 LOGE_IF(err || buffer->handle == 0,
325 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
326 this, index, w, h, strerror(-err));
327 } else {
328 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700329 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
330 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700331 }
332
333 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700334 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700335 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700336 }
337 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338}
339
Mathias Agopian3330b202009-10-05 17:07:12 -0700340uint32_t Layer::getEffectiveUsage(uint32_t usage) const
341{
342 /*
343 * buffers used for software rendering, but h/w composition
344 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
345 *
346 * buffers used for h/w rendering and h/w composition
347 * are allocated with HW_RENDER | HW_TEXTURE
348 *
349 * buffers used with h/w rendering and either NPOT or no egl_image_ext
350 * are allocated with SW_READ_RARELY | HW_RENDER
351 *
352 */
353
354 if (mSecure) {
355 // secure buffer, don't store it into the GPU
356 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
357 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
358 } else {
359 // it's allowed to modify the usage flags here, but generally
360 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700361 // request EGLImage for all buffers
362 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700363 }
364 return usage;
365}
366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367uint32_t Layer::doTransaction(uint32_t flags)
368{
369 const Layer::State& front(drawingState());
370 const Layer::State& temp(currentState());
371
Mathias Agopiana138f892010-05-21 17:24:35 -0700372 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
373 (front.requested_h != temp.requested_h);
374
375 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700376 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700378 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
379 this,
380 int(temp.requested_w), int(temp.requested_h),
381 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382
Mathias Agopiana138f892010-05-21 17:24:35 -0700383 if (!isFixedSize()) {
384 // we're being resized and there is a freeze display request,
385 // acquire a freeze lock, so that the screen stays put
386 // until we've redrawn at the new size; this is to avoid
387 // glitches upon orientation changes.
388 if (mFlinger->hasFreezeRequest()) {
389 // if the surface is hidden, don't try to acquire the
390 // freeze lock, since hidden surfaces may never redraw
391 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
392 mFreezeLock = mFlinger->getFreezeLock();
393 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700395
396 // this will make sure LayerBase::doTransaction doesn't update
397 // the drawing state's size
398 Layer::State& editDraw(mDrawingState);
399 editDraw.requested_w = temp.requested_w;
400 editDraw.requested_h = temp.requested_h;
401
402 // record the new size, form this point on, when the client request
403 // a buffer, it'll get the new size.
404 setBufferSize(temp.requested_w, temp.requested_h);
405
Mathias Agopian96f08192010-06-02 23:28:45 -0700406 sp<Client> ourClient(client.promote());
407 if (ourClient != 0) {
408 // all buffers need reallocation
409 lcblk->reallocateAll();
410 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700411 } else {
412 // record the new size
413 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 }
415 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 if (temp.sequence != front.sequence) {
418 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
419 // this surface is now hidden, so it shouldn't hold a freeze lock
420 // (it may never redraw, which is fine if it is hidden)
421 mFreezeLock.clear();
422 }
423 }
424
425 return LayerBase::doTransaction(flags);
426}
427
Mathias Agopiana138f892010-05-21 17:24:35 -0700428void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700429 Mutex::Autolock _l(mLock);
430 mWidth = w;
431 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
433
Mathias Agopiana138f892010-05-21 17:24:35 -0700434bool Layer::isFixedSize() const {
435 Mutex::Autolock _l(mLock);
436 return mFixedSize;
437}
438
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439// ----------------------------------------------------------------------------
440// pageflip handling...
441// ----------------------------------------------------------------------------
442
443void Layer::lockPageFlip(bool& recomputeVisibleRegions)
444{
Mathias Agopian96f08192010-06-02 23:28:45 -0700445 sp<Client> ourClient(client.promote());
446 if (ourClient == 0) {
447 // client died
448 recomputeVisibleRegions = true;
449 return;
450 }
451
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700452 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700453 if (buf == NOT_ENOUGH_DATA) {
454 // NOTE: This is not an error, it simply means there is nothing to
455 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700456 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 return;
458 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700459
Mathias Agopiand606de62010-05-10 20:06:11 -0700460 if (buf < NO_ERROR) {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700461 LOGE("retireAndLock() buffer index (%d) out of range", buf);
462 mPostedDirtyRegion.clear();
463 return;
464 }
465
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700466 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700467 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
468 LOGE("retireAndLock() buffer index (%d) out of range", buf);
469 mPostedDirtyRegion.clear();
470 return;
471 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700473 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700474 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700475 if (newFrontBuffer != NULL) {
476 // compute the posted region
477 const Region dirty(lcblk->getDirtyRegion(buf));
478 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700479
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700480 // update the layer size and release freeze-lock
481 const Layer::State& front(drawingState());
482 if (newFrontBuffer->getWidth() == front.requested_w &&
483 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700484 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700485 if ((front.w != front.requested_w) ||
486 (front.h != front.requested_h))
487 {
488 // Here we pretend the transaction happened by updating the
489 // current and drawing states. Drawing state is only accessed
490 // in this thread, no need to have it locked
491 Layer::State& editDraw(mDrawingState);
492 editDraw.w = editDraw.requested_w;
493 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700494
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700495 // We also need to update the current state so that we don't
496 // end-up doing too much work during the next transaction.
497 // NOTE: We actually don't need hold the transaction lock here
498 // because State::w and State::h are only accessed from
499 // this thread
500 Layer::State& editTemp(currentState());
501 editTemp.w = editDraw.w;
502 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700503
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700504 // recompute visible region
505 recomputeVisibleRegions = true;
506 }
507
508 // we now have the correct size, unfreeze the screen
509 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700510 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700511 } else {
512 // this should not happen unless we ran out of memory while
513 // allocating the buffer. we're hoping that things will get back
514 // to normal the next time the app tries to draw into this buffer.
515 // meanwhile, pretend the screen didn't update.
516 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700517 }
518
Mathias Agopiane7005012009-10-07 16:44:10 -0700519 if (lcblk->getQueuedCount()) {
520 // signal an event if we have more buffers waiting
521 mFlinger->signalEvent();
522 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523
Mathias Agopian245e4d72010-04-21 15:24:11 -0700524 /* a buffer was posted, so we need to call reloadTexture(), which
525 * will update our internal data structures (eg: EGLImageKHR or
526 * texture names). we need to do this even if mPostedDirtyRegion is
527 * empty -- it's orthogonal to the fact that a new buffer was posted,
528 * for instance, a degenerate case could be that the user did an empty
529 * update but repainted the buffer with appropriate content (after a
530 * resize for instance).
531 */
532 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533}
534
535void Layer::unlockPageFlip(
536 const Transform& planeTransform, Region& outDirtyRegion)
537{
538 Region dirtyRegion(mPostedDirtyRegion);
539 if (!dirtyRegion.isEmpty()) {
540 mPostedDirtyRegion.clear();
541 // The dirty region is given in the layer's coordinate space
542 // transform the dirty region by the surface's transformation
543 // and the global transformation.
544 const Layer::State& s(drawingState());
545 const Transform tr(planeTransform * s.transform);
546 dirtyRegion = tr.transform(dirtyRegion);
547
548 // At this point, the dirty region is in screen space.
549 // Make sure it's constrained by the visible region (which
550 // is in screen space as well).
551 dirtyRegion.andSelf(visibleRegionScreen);
552 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800554 if (visibleRegionScreen.isEmpty()) {
555 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700556 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800557 mFreezeLock.clear();
558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559}
560
561void Layer::finishPageFlip()
562{
Mathias Agopian96f08192010-06-02 23:28:45 -0700563 sp<Client> ourClient(client.promote());
564 if (ourClient != 0) {
565 int buf = mBufferManager.getActiveBufferIndex();
566 status_t err = lcblk->unlock( buf );
567 LOGE_IF(err!=NO_ERROR, "layer %p, buffer=%d wasn't locked!", this, buf);
568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569}
570
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700571
572void Layer::dump(String8& result, char* buffer, size_t SIZE) const
573{
574 LayerBaseClient::dump(result, buffer, SIZE);
575
576 SharedBufferStack::Statistics stats = lcblk->getStats();
577 result.append( lcblk->dump(" ") );
578 sp<const GraphicBuffer> buf0(getBuffer(0));
579 sp<const GraphicBuffer> buf1(getBuffer(1));
580 uint32_t w0=0, h0=0, s0=0;
581 uint32_t w1=0, h1=0, s1=0;
582 if (buf0 != 0) {
583 w0 = buf0->getWidth();
584 h0 = buf0->getHeight();
585 s0 = buf0->getStride();
586 }
587 if (buf1 != 0) {
588 w1 = buf1->getWidth();
589 h1 = buf1->getHeight();
590 s1 = buf1->getStride();
591 }
592 snprintf(buffer, SIZE,
593 " "
594 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
595 " freezeLock=%p, dq-q-time=%u us\n",
596 pixelFormat(),
597 w0, h0, s0, w1, h1, s1,
598 getFreezeLock().get(), stats.totalTime);
599
600 result.append(buffer);
601}
602
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603// ---------------------------------------------------------------------------
604
Mathias Agopiand606de62010-05-10 20:06:11 -0700605Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700606 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
607 mActiveBuffer(0), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700608{
609}
610
Mathias Agopianbb641242010-05-18 17:06:55 -0700611Layer::BufferManager::~BufferManager()
612{
613}
614
615status_t Layer::BufferManager::resize(size_t size)
616{
617 Mutex::Autolock _l(mLock);
618 mNumBuffers = size;
619 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700620}
621
622// only for debugging
623sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
624 return mBufferData[index].buffer;
625}
626
627status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
628 // TODO: need to validate 'index'
629 mActiveBuffer = index;
630 return NO_ERROR;
631}
632
633size_t Layer::BufferManager::getActiveBufferIndex() const {
634 return mActiveBuffer;
635}
636
637Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700638 Texture res;
639 if (mFailover) {
640 res = mFailoverTexture;
641 } else {
642 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
643 }
644 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700645}
646
647sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700648 const size_t activeBuffer = mActiveBuffer;
649 BufferData const * const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700650 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700651 return buffers[activeBuffer].buffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700652}
653
654sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
655{
Mathias Agopianbb641242010-05-18 17:06:55 -0700656 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700657 sp<GraphicBuffer> buffer;
658 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700659 buffer = buffers[index].buffer;
660 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700661 return buffer;
662}
663
664status_t Layer::BufferManager::attachBuffer(size_t index,
665 const sp<GraphicBuffer>& buffer)
666{
Mathias Agopianbb641242010-05-18 17:06:55 -0700667 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700668 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700669 buffers[index].buffer = buffer;
670 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700671 return NO_ERROR;
672}
673
674status_t Layer::BufferManager::destroy(EGLDisplay dpy)
675{
Mathias Agopianbb641242010-05-18 17:06:55 -0700676 BufferData* const buffers = mBufferData;
677 size_t num;
678 { // scope for the lock
679 Mutex::Autolock _l(mLock);
680 num = mNumBuffers;
681 for (size_t i=0 ; i<num ; i++) {
682 buffers[i].buffer = 0;
683 }
684 }
685 for (size_t i=0 ; i<num ; i++) {
686 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700687 }
688 destroyTexture(&mFailoverTexture, dpy);
689 return NO_ERROR;
690}
691
692status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
693 const sp<GraphicBuffer>& buffer)
694{
695 size_t index = mActiveBuffer;
Mathias Agopianbb641242010-05-18 17:06:55 -0700696 Image& texture(mBufferData[index].texture);
Mathias Agopiand606de62010-05-10 20:06:11 -0700697 status_t err = mTextureManager.initEglImage(&texture, dpy, buffer);
698 // if EGLImage fails, we switch to regular texture mode, and we
699 // free all resources associated with using EGLImages.
700 if (err == NO_ERROR) {
701 mFailover = false;
702 destroyTexture(&mFailoverTexture, dpy);
703 } else {
704 mFailover = true;
Mathias Agopianbb641242010-05-18 17:06:55 -0700705 const size_t num = mNumBuffers;
706 for (size_t i=0 ; i<num ; i++) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700707 destroyTexture(&mBufferData[i].texture, dpy);
708 }
709 }
710 return err;
711}
712
713status_t Layer::BufferManager::loadTexture(
714 const Region& dirty, const GGLSurface& t)
715{
716 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
717}
718
Mathias Agopianbb641242010-05-18 17:06:55 -0700719status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
720{
721 if (tex->name != -1U) {
722 glDeleteTextures(1, &tex->name);
723 tex->name = -1U;
724 }
725 if (tex->image != EGL_NO_IMAGE_KHR) {
726 eglDestroyImageKHR(dpy, tex->image);
727 tex->image = EGL_NO_IMAGE_KHR;
728 }
729 return NO_ERROR;
730}
731
Mathias Agopiand606de62010-05-10 20:06:11 -0700732// ---------------------------------------------------------------------------
733
Mathias Agopian9a112062009-04-17 19:36:26 -0700734Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700735 const sp<Layer>& owner)
736 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700737{
738}
739
740Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700741{
742}
743
Mathias Agopiana138f892010-05-21 17:24:35 -0700744sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
745 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700746{
Mathias Agopian3330b202009-10-05 17:07:12 -0700747 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700748 sp<Layer> owner(getOwner());
749 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700750 /*
751 * requestBuffer() cannot be called from the main thread
752 * as it could cause a dead-lock, since it may have to wait
753 * on conditions updated my the main thread.
754 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700755 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700756 }
757 return buffer;
758}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700760status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
761{
762 status_t err = DEAD_OBJECT;
763 sp<Layer> owner(getOwner());
764 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700765 /*
766 * setBufferCount() cannot be called from the main thread
767 * as it could cause a dead-lock, since it may have to wait
768 * on conditions updated my the main thread.
769 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700770 err = owner->setBufferCount(bufferCount);
771 }
772 return err;
773}
774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775// ---------------------------------------------------------------------------
776
777
778}; // namespace android