blob: e7247bd90ae9059b945f425c168cc39a29c8a2d2 [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 Agopian401c2572009-09-23 19:16:27 -070053 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070054 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070055 mSecure(false),
Mathias Agopiand606de62010-05-10 20:06:11 -070056 mTextureManager(mFlags),
Mathias Agopiana138f892010-05-21 17:24:35 -070057 mBufferManager(mTextureManager),
58 mWidth(0), mHeight(0), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
62Layer::~Layer()
63{
Mathias Agopianbb641242010-05-18 17:06:55 -070064 // FIXME: must be called from the main UI thread
65 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
66 mBufferManager.destroy(dpy);
67
Mathias Agopianb7e930d2010-06-01 15:12:58 -070068 // we can use getUserClientUnsafe here because we know we're
69 // single-threaded at that point.
70 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
71 if (ourClient != 0) {
72 ourClient->detachLayer(this);
73 }
Mathias Agopiand606de62010-05-10 20:06:11 -070074}
75
Mathias Agopianb7e930d2010-06-01 15:12:58 -070076status_t Layer::setToken(const sp<UserClient>& userClient,
77 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070078{
Mathias Agopian579b3f82010-06-08 19:54:15 -070079 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070080 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070081 getIdentity());
82
Mathias Agopianb7e930d2010-06-01 15:12:58 -070083 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -070084
85 LOGE_IF(err != NO_ERROR,
86 "ClientRef::setToken(%p, %p, %u) failed",
87 userClient.get(), lcblk.get(), token);
88
89 if (err == NO_ERROR) {
90 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -070091 }
92
93 return err;
94}
95
96int32_t Layer::getToken() const
97{
98 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -070099}
100
Mathias Agopian579b3f82010-06-08 19:54:15 -0700101sp<UserClient> Layer::getClient() const
102{
103 return mUserClientRef.getClient();
104}
105
Mathias Agopiand606de62010-05-10 20:06:11 -0700106// called with SurfaceFlinger::mStateLock as soon as the layer is entered
107// in the purgatory list
108void Layer::onRemoved()
109{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700110 ClientRef::Access sharedClient(mUserClientRef);
111 SharedBufferServer* lcblk(sharedClient.get());
112 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700113 // wake up the condition
114 lcblk->setStatus(NO_INIT);
115 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700116}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700117
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119{
120 return mSurface;
121}
122
Mathias Agopian9a112062009-04-17 19:36:26 -0700123status_t Layer::ditch()
124{
Mathias Agopianbb641242010-05-18 17:06:55 -0700125 // NOTE: Called from the main UI thread
126
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700127 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800128 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700129
130 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
131 mBufferManager.destroy(dpy);
132 mSurface.clear();
133
134 Mutex::Autolock _l(mLock);
135 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700136 return NO_ERROR;
137}
138
Mathias Agopianf9d93272009-06-19 17:00:27 -0700139status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 PixelFormat format, uint32_t flags)
141{
Mathias Agopian401c2572009-09-23 19:16:27 -0700142 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 PixelFormatInfo info;
144 status_t err = getPixelFormatInfo(format, &info);
145 if (err) return err;
146
Mathias Agopian401c2572009-09-23 19:16:27 -0700147 // the display's pixel format
148 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700149 uint32_t const maxSurfaceDims = min(
150 hw.getMaxTextureSize(), hw.getMaxViewportDims());
151
152 // never allow a surface larger than what our underlying GL implementation
153 // can handle.
154 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
155 return BAD_VALUE;
156 }
157
Mathias Agopian401c2572009-09-23 19:16:27 -0700158 PixelFormatInfo displayInfo;
159 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700160 const uint32_t hwFlags = hw.getFlags();
161
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700162 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700163 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700164 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700165 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700167
Mathias Agopian401c2572009-09-23 19:16:27 -0700168 // we use the red index
169 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
170 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
171 mNeedsDithering = layerRedsize > displayRedSize;
172
Mathias Agopian96f08192010-06-02 23:28:45 -0700173 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 return NO_ERROR;
175}
176
177void Layer::reloadTexture(const Region& dirty)
178{
Mathias Agopiand606de62010-05-10 20:06:11 -0700179 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800180 if (buffer == NULL) {
181 // this situation can happen if we ran out of memory for instance.
182 // not much we can do. continue to use whatever texture was bound
183 // to this context.
184 return;
185 }
186
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700187#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700188 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700189 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
190 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
191 // not sure what we can do here...
192 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
193 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700195 } else
196#endif
197 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800198slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700200 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700201 LOGE_IF(res, "error %d (%s) locking buffer %p",
202 res, strerror(res), buffer.get());
203 if (res == NO_ERROR) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700204 mBufferManager.loadTexture(dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700205 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210void Layer::onDraw(const Region& clip) const
211{
Mathias Agopiand606de62010-05-10 20:06:11 -0700212 Texture tex(mBufferManager.getActiveTexture());
213 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700215 // in fact never been drawn into. This happens frequently with
216 // SurfaceView because the WindowManager can't know when the client
217 // has drawn the first time.
218
219 // If there is nothing under us, we paint the screen in black, otherwise
220 // we just skip this update.
221
222 // figure out if there is something below us
223 Region under;
224 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
225 const size_t count = drawingLayers.size();
226 for (size_t i=0 ; i<count ; ++i) {
227 const sp<LayerBase>& layer(drawingLayers[i]);
228 if (layer.get() == static_cast<LayerBase const*>(this))
229 break;
230 under.orSelf(layer->visibleRegionScreen);
231 }
232 // if not everything below us is covered, we plug the holes!
233 Region holes(clip.subtract(under));
234 if (!holes.isEmpty()) {
235 clearWithOpenGL(holes);
236 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 return;
238 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700239 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240}
241
Mathias Agopiana7f66922010-05-26 22:08:52 -0700242bool Layer::needsFiltering() const
243{
244 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
245 // NOTE: there is a race here, because mFixedSize is updated in a
246 // binder transaction. however, it doesn't really matter since it is
247 // evaluated each time we draw. To be perfectly correct, this flag
248 // would have to be associated with a buffer.
249 if (mFixedSize)
250 return true;
251 }
252 return LayerBase::needsFiltering();
253}
254
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700255
256status_t Layer::setBufferCount(int bufferCount)
257{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700258 ClientRef::Access sharedClient(mUserClientRef);
259 SharedBufferServer* lcblk(sharedClient.get());
260 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700261 // oops, the client is already gone
262 return DEAD_OBJECT;
263 }
264
Mathias Agopianbb641242010-05-18 17:06:55 -0700265 // NOTE: lcblk->resize() is protected by an internal lock
266 status_t err = lcblk->resize(bufferCount);
267 if (err == NO_ERROR)
268 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700269
270 return err;
271}
272
Mathias Agopiana138f892010-05-21 17:24:35 -0700273sp<GraphicBuffer> Layer::requestBuffer(int index,
274 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
275 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276{
Mathias Agopian3330b202009-10-05 17:07:12 -0700277 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700278
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700279 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700280 return buffer;
281
282 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
283 return buffer;
284
Mathias Agopian48d819a2009-09-10 19:41:18 -0700285 // this ensures our client doesn't go away while we're accessing
286 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700287 ClientRef::Access sharedClient(mUserClientRef);
288 SharedBufferServer* lcblk(sharedClient.get());
289 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700290 // oops, the client is already gone
291 return buffer;
292 }
293
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700294 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700295 * This is called from the client's Surface::dequeue(). This can happen
296 * at any time, especially while we're in the middle of using the
297 * buffer 'index' as our front buffer.
Mathias Agopianbb641242010-05-18 17:06:55 -0700298 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700299 * Make sure the buffer we're resizing is not the front buffer and has been
300 * dequeued. Once this condition is asserted, we are guaranteed that this
301 * buffer cannot become the front buffer under our feet, since we're called
302 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700304 status_t err = lcblk->assertReallocate(index);
305 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700306 if (err != NO_ERROR) {
307 // the surface may have died
308 return buffer;
309 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Mathias Agopiana138f892010-05-21 17:24:35 -0700311 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700312 { // scope for the lock
313 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700314 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
315 const bool formatChanged = mReqFormat != reqFormat;
316 mReqWidth = reqWidth;
317 mReqHeight = reqHeight;
318 mReqFormat = reqFormat;
319 mFixedSize = reqWidth && reqHeight;
320 w = reqWidth ? reqWidth : mWidth;
321 h = reqHeight ? reqHeight : mHeight;
322 f = reqFormat ? reqFormat : mFormat;
Mathias Agopiand606de62010-05-10 20:06:11 -0700323 buffer = mBufferManager.detachBuffer(index);
Mathias Agopiana138f892010-05-21 17:24:35 -0700324 if (fixedSizeChanged || formatChanged) {
325 lcblk->reallocateAllExcept(index);
326 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700327 }
328
Mathias Agopian3330b202009-10-05 17:07:12 -0700329 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700330 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700331 err = buffer->reallocate(w, h, f, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700333 // here we have to reallocate a new buffer because we could have a
334 // client in our process with a reference to it (eg: status bar),
335 // and we can't release the handle under its feet.
336 buffer.clear();
Mathias Agopiana138f892010-05-21 17:24:35 -0700337 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700338 err = buffer->initCheck();
339 }
340
341 if (err || buffer->handle == 0) {
342 LOGE_IF(err || buffer->handle == 0,
343 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
344 this, index, w, h, strerror(-err));
345 } else {
346 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700347 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
348 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700349 }
350
351 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700352 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700353 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700354 }
355 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356}
357
Mathias Agopian3330b202009-10-05 17:07:12 -0700358uint32_t Layer::getEffectiveUsage(uint32_t usage) const
359{
360 /*
361 * buffers used for software rendering, but h/w composition
362 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
363 *
364 * buffers used for h/w rendering and h/w composition
365 * are allocated with HW_RENDER | HW_TEXTURE
366 *
367 * buffers used with h/w rendering and either NPOT or no egl_image_ext
368 * are allocated with SW_READ_RARELY | HW_RENDER
369 *
370 */
371
372 if (mSecure) {
373 // secure buffer, don't store it into the GPU
374 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
375 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
376 } else {
377 // it's allowed to modify the usage flags here, but generally
378 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700379 // request EGLImage for all buffers
380 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700381 }
382 return usage;
383}
384
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385uint32_t Layer::doTransaction(uint32_t flags)
386{
387 const Layer::State& front(drawingState());
388 const Layer::State& temp(currentState());
389
Mathias Agopiana138f892010-05-21 17:24:35 -0700390 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
391 (front.requested_h != temp.requested_h);
392
393 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700394 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700396 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
397 this,
398 int(temp.requested_w), int(temp.requested_h),
399 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400
Mathias Agopiana138f892010-05-21 17:24:35 -0700401 if (!isFixedSize()) {
402 // we're being resized and there is a freeze display request,
403 // acquire a freeze lock, so that the screen stays put
404 // until we've redrawn at the new size; this is to avoid
405 // glitches upon orientation changes.
406 if (mFlinger->hasFreezeRequest()) {
407 // if the surface is hidden, don't try to acquire the
408 // freeze lock, since hidden surfaces may never redraw
409 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
410 mFreezeLock = mFlinger->getFreezeLock();
411 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700413
414 // this will make sure LayerBase::doTransaction doesn't update
415 // the drawing state's size
416 Layer::State& editDraw(mDrawingState);
417 editDraw.requested_w = temp.requested_w;
418 editDraw.requested_h = temp.requested_h;
419
420 // record the new size, form this point on, when the client request
421 // a buffer, it'll get the new size.
422 setBufferSize(temp.requested_w, temp.requested_h);
423
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700424 ClientRef::Access sharedClient(mUserClientRef);
425 SharedBufferServer* lcblk(sharedClient.get());
426 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700427 // all buffers need reallocation
428 lcblk->reallocateAll();
429 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700430 } else {
431 // record the new size
432 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 }
434 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 if (temp.sequence != front.sequence) {
437 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
438 // this surface is now hidden, so it shouldn't hold a freeze lock
439 // (it may never redraw, which is fine if it is hidden)
440 mFreezeLock.clear();
441 }
442 }
443
444 return LayerBase::doTransaction(flags);
445}
446
Mathias Agopiana138f892010-05-21 17:24:35 -0700447void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700448 Mutex::Autolock _l(mLock);
449 mWidth = w;
450 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451}
452
Mathias Agopiana138f892010-05-21 17:24:35 -0700453bool Layer::isFixedSize() const {
454 Mutex::Autolock _l(mLock);
455 return mFixedSize;
456}
457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458// ----------------------------------------------------------------------------
459// pageflip handling...
460// ----------------------------------------------------------------------------
461
462void Layer::lockPageFlip(bool& recomputeVisibleRegions)
463{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700464 ClientRef::Access sharedClient(mUserClientRef);
465 SharedBufferServer* lcblk(sharedClient.get());
466 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700467 // client died
468 recomputeVisibleRegions = true;
469 return;
470 }
471
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700472 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700473 if (buf == NOT_ENOUGH_DATA) {
474 // NOTE: This is not an error, it simply means there is nothing to
475 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700476 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 return;
478 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700479
Mathias Agopiand606de62010-05-10 20:06:11 -0700480 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700481 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700482 mPostedDirtyRegion.clear();
483 return;
484 }
485
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700486 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700487 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700488 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700489 mPostedDirtyRegion.clear();
490 return;
491 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700493 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700494 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700495 if (newFrontBuffer != NULL) {
496 // compute the posted region
497 const Region dirty(lcblk->getDirtyRegion(buf));
498 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700499
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700500 // update the layer size and release freeze-lock
501 const Layer::State& front(drawingState());
502 if (newFrontBuffer->getWidth() == front.requested_w &&
503 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700504 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700505 if ((front.w != front.requested_w) ||
506 (front.h != front.requested_h))
507 {
508 // Here we pretend the transaction happened by updating the
509 // current and drawing states. Drawing state is only accessed
510 // in this thread, no need to have it locked
511 Layer::State& editDraw(mDrawingState);
512 editDraw.w = editDraw.requested_w;
513 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700514
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700515 // We also need to update the current state so that we don't
516 // end-up doing too much work during the next transaction.
517 // NOTE: We actually don't need hold the transaction lock here
518 // because State::w and State::h are only accessed from
519 // this thread
520 Layer::State& editTemp(currentState());
521 editTemp.w = editDraw.w;
522 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700523
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700524 // recompute visible region
525 recomputeVisibleRegions = true;
526 }
527
528 // we now have the correct size, unfreeze the screen
529 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700530 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700531 } else {
532 // this should not happen unless we ran out of memory while
533 // allocating the buffer. we're hoping that things will get back
534 // to normal the next time the app tries to draw into this buffer.
535 // meanwhile, pretend the screen didn't update.
536 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700537 }
538
Mathias Agopiane7005012009-10-07 16:44:10 -0700539 if (lcblk->getQueuedCount()) {
540 // signal an event if we have more buffers waiting
541 mFlinger->signalEvent();
542 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
Mathias Agopian245e4d72010-04-21 15:24:11 -0700544 /* a buffer was posted, so we need to call reloadTexture(), which
545 * will update our internal data structures (eg: EGLImageKHR or
546 * texture names). we need to do this even if mPostedDirtyRegion is
547 * empty -- it's orthogonal to the fact that a new buffer was posted,
548 * for instance, a degenerate case could be that the user did an empty
549 * update but repainted the buffer with appropriate content (after a
550 * resize for instance).
551 */
552 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553}
554
555void Layer::unlockPageFlip(
556 const Transform& planeTransform, Region& outDirtyRegion)
557{
558 Region dirtyRegion(mPostedDirtyRegion);
559 if (!dirtyRegion.isEmpty()) {
560 mPostedDirtyRegion.clear();
561 // The dirty region is given in the layer's coordinate space
562 // transform the dirty region by the surface's transformation
563 // and the global transformation.
564 const Layer::State& s(drawingState());
565 const Transform tr(planeTransform * s.transform);
566 dirtyRegion = tr.transform(dirtyRegion);
567
568 // At this point, the dirty region is in screen space.
569 // Make sure it's constrained by the visible region (which
570 // is in screen space as well).
571 dirtyRegion.andSelf(visibleRegionScreen);
572 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800574 if (visibleRegionScreen.isEmpty()) {
575 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700576 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800577 mFreezeLock.clear();
578 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579}
580
581void Layer::finishPageFlip()
582{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700583 ClientRef::Access sharedClient(mUserClientRef);
584 SharedBufferServer* lcblk(sharedClient.get());
585 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700586 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700587 if (buf >= 0) {
588 status_t err = lcblk->unlock( buf );
589 LOGE_IF(err!=NO_ERROR,
590 "layer %p, buffer=%d wasn't locked!",
591 this, buf);
592 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594}
595
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700596
597void Layer::dump(String8& result, char* buffer, size_t SIZE) const
598{
599 LayerBaseClient::dump(result, buffer, SIZE);
600
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700601 ClientRef::Access sharedClient(mUserClientRef);
602 SharedBufferServer* lcblk(sharedClient.get());
603 uint32_t totalTime = 0;
604 if (lcblk) {
605 SharedBufferStack::Statistics stats = lcblk->getStats();
606 totalTime= stats.totalTime;
607 result.append( lcblk->dump(" ") );
608 }
609
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700610 sp<const GraphicBuffer> buf0(getBuffer(0));
611 sp<const GraphicBuffer> buf1(getBuffer(1));
612 uint32_t w0=0, h0=0, s0=0;
613 uint32_t w1=0, h1=0, s1=0;
614 if (buf0 != 0) {
615 w0 = buf0->getWidth();
616 h0 = buf0->getHeight();
617 s0 = buf0->getStride();
618 }
619 if (buf1 != 0) {
620 w1 = buf1->getWidth();
621 h1 = buf1->getHeight();
622 s1 = buf1->getStride();
623 }
624 snprintf(buffer, SIZE,
625 " "
626 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
627 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700628 mFormat, w0, h0, s0, w1, h1, s1,
629 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700630
631 result.append(buffer);
632}
633
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700634// ---------------------------------------------------------------------------
635
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700636Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700637 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700638}
639
640Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700641}
642
643int32_t Layer::ClientRef::getToken() const {
644 Mutex::Autolock _l(mLock);
645 return mToken;
646}
647
Mathias Agopian579b3f82010-06-08 19:54:15 -0700648sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700649 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700650 return mUserClient.promote();
651}
652
653status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
654 const sp<SharedBufferServer>& sharedClient, int32_t token) {
655 Mutex::Autolock _l(mLock);
656
657 { // scope for strong mUserClient reference
658 sp<UserClient> userClient(mUserClient.promote());
659 if (mUserClient != 0 && mControlBlock != 0) {
660 mControlBlock->setStatus(NO_INIT);
661 }
662 }
663
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700664 mUserClient = uc;
665 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700666 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700667 return NO_ERROR;
668}
669
670sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
671 return mUserClient.promote();
672}
673
674// this class gives us access to SharedBufferServer safely
675// it makes sure the UserClient (and its associated shared memory)
676// won't go away while we're accessing it.
677Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700678 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700679{
680 Mutex::Autolock _l(ref.mLock);
681 mUserClientStrongRef = ref.mUserClient.promote();
682 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700683 mControlBlock = ref.mControlBlock;
684}
685
686Layer::ClientRef::Access::~Access()
687{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700688}
689
690// ---------------------------------------------------------------------------
691
Mathias Agopiand606de62010-05-10 20:06:11 -0700692Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700693 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700694 mActiveBuffer(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700695{
696}
697
Mathias Agopianbb641242010-05-18 17:06:55 -0700698Layer::BufferManager::~BufferManager()
699{
700}
701
702status_t Layer::BufferManager::resize(size_t size)
703{
704 Mutex::Autolock _l(mLock);
705 mNumBuffers = size;
706 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700707}
708
709// only for debugging
710sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
711 return mBufferData[index].buffer;
712}
713
714status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700715 mActiveBuffer = index;
716 return NO_ERROR;
717}
718
719size_t Layer::BufferManager::getActiveBufferIndex() const {
720 return mActiveBuffer;
721}
722
723Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700724 Texture res;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700725 if (mFailover || mActiveBuffer<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700726 res = mFailoverTexture;
727 } else {
728 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
729 }
730 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700731}
732
733sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700734 sp<GraphicBuffer> result;
735 const ssize_t activeBuffer = mActiveBuffer;
736 if (activeBuffer >= 0) {
737 BufferData const * const buffers = mBufferData;
738 Mutex::Autolock _l(mLock);
739 result = buffers[activeBuffer].buffer;
740 }
741 return result;
Mathias Agopiand606de62010-05-10 20:06:11 -0700742}
743
744sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
745{
Mathias Agopianbb641242010-05-18 17:06:55 -0700746 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700747 sp<GraphicBuffer> buffer;
748 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700749 buffer = buffers[index].buffer;
750 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700751 return buffer;
752}
753
754status_t Layer::BufferManager::attachBuffer(size_t index,
755 const sp<GraphicBuffer>& buffer)
756{
Mathias Agopianbb641242010-05-18 17:06:55 -0700757 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700758 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700759 buffers[index].buffer = buffer;
760 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700761 return NO_ERROR;
762}
763
764status_t Layer::BufferManager::destroy(EGLDisplay dpy)
765{
Mathias Agopianbb641242010-05-18 17:06:55 -0700766 BufferData* const buffers = mBufferData;
767 size_t num;
768 { // scope for the lock
769 Mutex::Autolock _l(mLock);
770 num = mNumBuffers;
771 for (size_t i=0 ; i<num ; i++) {
772 buffers[i].buffer = 0;
773 }
774 }
775 for (size_t i=0 ; i<num ; i++) {
776 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700777 }
778 destroyTexture(&mFailoverTexture, dpy);
779 return NO_ERROR;
780}
781
782status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
783 const sp<GraphicBuffer>& buffer)
784{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700785 status_t err = NO_INIT;
786 ssize_t index = mActiveBuffer;
787 if (index >= 0) {
788 Image& texture(mBufferData[index].texture);
789 err = mTextureManager.initEglImage(&texture, dpy, buffer);
790 // if EGLImage fails, we switch to regular texture mode, and we
791 // free all resources associated with using EGLImages.
792 if (err == NO_ERROR) {
793 mFailover = false;
794 destroyTexture(&mFailoverTexture, dpy);
795 } else {
796 mFailover = true;
797 const size_t num = mNumBuffers;
798 for (size_t i=0 ; i<num ; i++) {
799 destroyTexture(&mBufferData[i].texture, dpy);
800 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700801 }
802 }
803 return err;
804}
805
806status_t Layer::BufferManager::loadTexture(
807 const Region& dirty, const GGLSurface& t)
808{
809 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
810}
811
Mathias Agopianbb641242010-05-18 17:06:55 -0700812status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
813{
814 if (tex->name != -1U) {
815 glDeleteTextures(1, &tex->name);
816 tex->name = -1U;
817 }
818 if (tex->image != EGL_NO_IMAGE_KHR) {
819 eglDestroyImageKHR(dpy, tex->image);
820 tex->image = EGL_NO_IMAGE_KHR;
821 }
822 return NO_ERROR;
823}
824
Mathias Agopiand606de62010-05-10 20:06:11 -0700825// ---------------------------------------------------------------------------
826
Mathias Agopian9a112062009-04-17 19:36:26 -0700827Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700828 const sp<Layer>& owner)
829 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700830{
831}
832
833Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700834{
835}
836
Mathias Agopiana138f892010-05-21 17:24:35 -0700837sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
838 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700839{
Mathias Agopian3330b202009-10-05 17:07:12 -0700840 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700841 sp<Layer> owner(getOwner());
842 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700843 /*
844 * requestBuffer() cannot be called from the main thread
845 * as it could cause a dead-lock, since it may have to wait
846 * on conditions updated my the main thread.
847 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700848 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700849 }
850 return buffer;
851}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700853status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
854{
855 status_t err = DEAD_OBJECT;
856 sp<Layer> owner(getOwner());
857 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700858 /*
859 * setBufferCount() cannot be called from the main thread
860 * as it could cause a dead-lock, since it may have to wait
861 * on conditions updated my the main thread.
862 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700863 err = owner->setBufferCount(bufferCount);
864 }
865 return err;
866}
867
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868// ---------------------------------------------------------------------------
869
870
871}; // namespace android