blob: 03d7a0232b0eb729c67399978c993d5e7fd67483 [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"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
38
39
40#define DEBUG_RESIZE 0
41
42
43namespace android {
44
Mathias Agopianca99fb82010-04-14 16:43:44 -070045template <typename T> inline T min(T a, T b) {
46 return a<b ? a : b;
47}
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049// ---------------------------------------------------------------------------
50
Mathias Agopian96f08192010-06-02 23:28:45 -070051Layer::Layer(SurfaceFlinger* flinger,
52 DisplayID display, const sp<Client>& client)
53 : LayerBaseClient(flinger, display, client),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian401c2572009-09-23 19:16:27 -070055 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070056 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070057 mSecure(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070059 mBufferManager(mTextureManager),
60 mWidth(0), mHeight(0), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062}
63
64Layer::~Layer()
65{
Mathias Agopianbb641242010-05-18 17:06:55 -070066 // FIXME: must be called from the main UI thread
67 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
68 mBufferManager.destroy(dpy);
69
Mathias Agopianb7e930d2010-06-01 15:12:58 -070070 // we can use getUserClientUnsafe here because we know we're
71 // single-threaded at that point.
72 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
73 if (ourClient != 0) {
74 ourClient->detachLayer(this);
75 }
Mathias Agopiand606de62010-05-10 20:06:11 -070076}
77
Mathias Agopianb7e930d2010-06-01 15:12:58 -070078status_t Layer::setToken(const sp<UserClient>& userClient,
79 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070080{
Mathias Agopian579b3f82010-06-08 19:54:15 -070081 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070082 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070083 getIdentity());
84
Mathias Agopianb7e930d2010-06-01 15:12:58 -070085 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -070086
87 LOGE_IF(err != NO_ERROR,
88 "ClientRef::setToken(%p, %p, %u) failed",
89 userClient.get(), lcblk.get(), token);
90
91 if (err == NO_ERROR) {
92 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -070093 }
94
95 return err;
96}
97
98int32_t Layer::getToken() const
99{
100 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700101}
102
Mathias Agopian579b3f82010-06-08 19:54:15 -0700103sp<UserClient> Layer::getClient() const
104{
105 return mUserClientRef.getClient();
106}
107
Mathias Agopiand606de62010-05-10 20:06:11 -0700108// called with SurfaceFlinger::mStateLock as soon as the layer is entered
109// in the purgatory list
110void Layer::onRemoved()
111{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700112 ClientRef::Access sharedClient(mUserClientRef);
113 SharedBufferServer* lcblk(sharedClient.get());
114 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700115 // wake up the condition
116 lcblk->setStatus(NO_INIT);
117 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700118}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700119
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121{
122 return mSurface;
123}
124
Mathias Agopian9a112062009-04-17 19:36:26 -0700125status_t Layer::ditch()
126{
Mathias Agopianbb641242010-05-18 17:06:55 -0700127 // NOTE: Called from the main UI thread
128
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700129 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800130 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700131
132 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
133 mBufferManager.destroy(dpy);
134 mSurface.clear();
135
136 Mutex::Autolock _l(mLock);
137 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700138 return NO_ERROR;
139}
140
Mathias Agopianf9d93272009-06-19 17:00:27 -0700141status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 PixelFormat format, uint32_t flags)
143{
Mathias Agopian401c2572009-09-23 19:16:27 -0700144 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 PixelFormatInfo info;
146 status_t err = getPixelFormatInfo(format, &info);
147 if (err) return err;
148
Mathias Agopian401c2572009-09-23 19:16:27 -0700149 // the display's pixel format
150 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700151 uint32_t const maxSurfaceDims = min(
152 hw.getMaxTextureSize(), hw.getMaxViewportDims());
153
154 // never allow a surface larger than what our underlying GL implementation
155 // can handle.
156 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
157 return BAD_VALUE;
158 }
159
Mathias Agopian401c2572009-09-23 19:16:27 -0700160 PixelFormatInfo displayInfo;
161 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700162 const uint32_t hwFlags = hw.getFlags();
163
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700164 mFormat = format;
Mathias Agopianf955be92010-06-21 15:19:26 -0700165 mReqFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700166 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700167 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700168 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700170
Mathias Agopian401c2572009-09-23 19:16:27 -0700171 // we use the red index
172 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
173 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
174 mNeedsDithering = layerRedsize > displayRedSize;
175
Mathias Agopian96f08192010-06-02 23:28:45 -0700176 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 return NO_ERROR;
178}
179
180void Layer::reloadTexture(const Region& dirty)
181{
Mathias Agopiand606de62010-05-10 20:06:11 -0700182 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800183 if (buffer == NULL) {
184 // this situation can happen if we ran out of memory for instance.
185 // not much we can do. continue to use whatever texture was bound
186 // to this context.
187 return;
188 }
189
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700190 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700191 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
192 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
193 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700194 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700195 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700196 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800197slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700198 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700199 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
200 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
201 LOGE_IF(res, "error %d (%s) locking buffer %p",
202 res, strerror(res), buffer.get());
203 if (res == NO_ERROR) {
204 mBufferManager.loadTexture(dirty, t);
205 buffer->unlock();
206 }
207 } else {
208 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213void Layer::onDraw(const Region& clip) const
214{
Mathias Agopiand606de62010-05-10 20:06:11 -0700215 Texture tex(mBufferManager.getActiveTexture());
216 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700218 // in fact never been drawn into. This happens frequently with
219 // SurfaceView because the WindowManager can't know when the client
220 // has drawn the first time.
221
222 // If there is nothing under us, we paint the screen in black, otherwise
223 // we just skip this update.
224
225 // figure out if there is something below us
226 Region under;
227 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
228 const size_t count = drawingLayers.size();
229 for (size_t i=0 ; i<count ; ++i) {
230 const sp<LayerBase>& layer(drawingLayers[i]);
231 if (layer.get() == static_cast<LayerBase const*>(this))
232 break;
233 under.orSelf(layer->visibleRegionScreen);
234 }
235 // if not everything below us is covered, we plug the holes!
236 Region holes(clip.subtract(under));
237 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700238 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700239 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 return;
241 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700242 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243}
244
Mathias Agopiana7f66922010-05-26 22:08:52 -0700245bool Layer::needsFiltering() const
246{
247 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
248 // NOTE: there is a race here, because mFixedSize is updated in a
249 // binder transaction. however, it doesn't really matter since it is
250 // evaluated each time we draw. To be perfectly correct, this flag
251 // would have to be associated with a buffer.
252 if (mFixedSize)
253 return true;
254 }
255 return LayerBase::needsFiltering();
256}
257
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700258
259status_t Layer::setBufferCount(int bufferCount)
260{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700261 ClientRef::Access sharedClient(mUserClientRef);
262 SharedBufferServer* lcblk(sharedClient.get());
263 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700264 // oops, the client is already gone
265 return DEAD_OBJECT;
266 }
267
Mathias Agopianbb641242010-05-18 17:06:55 -0700268 // NOTE: lcblk->resize() is protected by an internal lock
269 status_t err = lcblk->resize(bufferCount);
270 if (err == NO_ERROR)
271 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700272
273 return err;
274}
275
Mathias Agopiana138f892010-05-21 17:24:35 -0700276sp<GraphicBuffer> Layer::requestBuffer(int index,
277 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
278 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279{
Mathias Agopian3330b202009-10-05 17:07:12 -0700280 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700281
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700282 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700283 return buffer;
284
285 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
286 return buffer;
287
Mathias Agopian48d819a2009-09-10 19:41:18 -0700288 // this ensures our client doesn't go away while we're accessing
289 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700290 ClientRef::Access sharedClient(mUserClientRef);
291 SharedBufferServer* lcblk(sharedClient.get());
292 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700293 // oops, the client is already gone
294 return buffer;
295 }
296
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700298 * This is called from the client's Surface::dequeue(). This can happen
299 * at any time, especially while we're in the middle of using the
300 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700301 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopian208cb072010-07-27 20:11:35 -0700303 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700304 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700305 { // scope for the lock
306 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700307 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
308 const bool formatChanged = mReqFormat != reqFormat;
309 mReqWidth = reqWidth;
310 mReqHeight = reqHeight;
311 mReqFormat = reqFormat;
312 mFixedSize = reqWidth && reqHeight;
313 w = reqWidth ? reqWidth : mWidth;
314 h = reqHeight ? reqHeight : mHeight;
315 f = reqFormat ? reqFormat : mFormat;
Mathias Agopiana138f892010-05-21 17:24:35 -0700316 if (fixedSizeChanged || formatChanged) {
317 lcblk->reallocateAllExcept(index);
318 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700319 }
320
Mathias Agopian208cb072010-07-27 20:11:35 -0700321 // here we have to reallocate a new buffer because the buffer could be
322 // used as the front buffer, or by a client in our process
323 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700324 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700325 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
326 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700327
328 if (err || buffer->handle == 0) {
329 LOGE_IF(err || buffer->handle == 0,
330 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
331 this, index, w, h, strerror(-err));
332 } else {
333 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700334 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
335 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700336 }
337
338 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700339 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700340 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700341 }
342 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343}
344
Mathias Agopian3330b202009-10-05 17:07:12 -0700345uint32_t Layer::getEffectiveUsage(uint32_t usage) const
346{
347 /*
348 * buffers used for software rendering, but h/w composition
349 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
350 *
351 * buffers used for h/w rendering and h/w composition
352 * are allocated with HW_RENDER | HW_TEXTURE
353 *
354 * buffers used with h/w rendering and either NPOT or no egl_image_ext
355 * are allocated with SW_READ_RARELY | HW_RENDER
356 *
357 */
358
359 if (mSecure) {
360 // secure buffer, don't store it into the GPU
361 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
362 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
363 } else {
364 // it's allowed to modify the usage flags here, but generally
365 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700366 // request EGLImage for all buffers
367 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700368 }
369 return usage;
370}
371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372uint32_t Layer::doTransaction(uint32_t flags)
373{
374 const Layer::State& front(drawingState());
375 const Layer::State& temp(currentState());
376
Mathias Agopiana138f892010-05-21 17:24:35 -0700377 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
378 (front.requested_h != temp.requested_h);
379
380 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700381 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700383 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
384 this,
385 int(temp.requested_w), int(temp.requested_h),
386 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387
Mathias Agopiana138f892010-05-21 17:24:35 -0700388 if (!isFixedSize()) {
389 // we're being resized and there is a freeze display request,
390 // acquire a freeze lock, so that the screen stays put
391 // until we've redrawn at the new size; this is to avoid
392 // glitches upon orientation changes.
393 if (mFlinger->hasFreezeRequest()) {
394 // if the surface is hidden, don't try to acquire the
395 // freeze lock, since hidden surfaces may never redraw
396 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
397 mFreezeLock = mFlinger->getFreezeLock();
398 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700400
401 // this will make sure LayerBase::doTransaction doesn't update
402 // the drawing state's size
403 Layer::State& editDraw(mDrawingState);
404 editDraw.requested_w = temp.requested_w;
405 editDraw.requested_h = temp.requested_h;
406
407 // record the new size, form this point on, when the client request
408 // a buffer, it'll get the new size.
409 setBufferSize(temp.requested_w, temp.requested_h);
410
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700411 ClientRef::Access sharedClient(mUserClientRef);
412 SharedBufferServer* lcblk(sharedClient.get());
413 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700414 // all buffers need reallocation
415 lcblk->reallocateAll();
416 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700417 } else {
418 // record the new size
419 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 }
421 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700422
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 if (temp.sequence != front.sequence) {
424 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
425 // this surface is now hidden, so it shouldn't hold a freeze lock
426 // (it may never redraw, which is fine if it is hidden)
427 mFreezeLock.clear();
428 }
429 }
430
431 return LayerBase::doTransaction(flags);
432}
433
Mathias Agopiana138f892010-05-21 17:24:35 -0700434void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700435 Mutex::Autolock _l(mLock);
436 mWidth = w;
437 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438}
439
Mathias Agopiana138f892010-05-21 17:24:35 -0700440bool Layer::isFixedSize() const {
441 Mutex::Autolock _l(mLock);
442 return mFixedSize;
443}
444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445// ----------------------------------------------------------------------------
446// pageflip handling...
447// ----------------------------------------------------------------------------
448
449void Layer::lockPageFlip(bool& recomputeVisibleRegions)
450{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700451 ClientRef::Access sharedClient(mUserClientRef);
452 SharedBufferServer* lcblk(sharedClient.get());
453 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700454 // client died
455 recomputeVisibleRegions = true;
456 return;
457 }
458
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700459 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700460 if (buf == NOT_ENOUGH_DATA) {
461 // NOTE: This is not an error, it simply means there is nothing to
462 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700463 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 return;
465 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700466
Mathias Agopiand606de62010-05-10 20:06:11 -0700467 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700468 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700469 mPostedDirtyRegion.clear();
470 return;
471 }
472
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700473 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700474 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700475 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700476 mPostedDirtyRegion.clear();
477 return;
478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479
Mathias Agopian3330b202009-10-05 17:07:12 -0700480 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700481 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700482 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700483 // compute the posted region
484 const Region dirty(lcblk->getDirtyRegion(buf));
485 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700487 // update the layer size and release freeze-lock
488 const Layer::State& front(drawingState());
489 if (newFrontBuffer->getWidth() == front.requested_w &&
490 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700491 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700492 if ((front.w != front.requested_w) ||
493 (front.h != front.requested_h))
494 {
495 // Here we pretend the transaction happened by updating the
496 // current and drawing states. Drawing state is only accessed
497 // in this thread, no need to have it locked
498 Layer::State& editDraw(mDrawingState);
499 editDraw.w = editDraw.requested_w;
500 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700501
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700502 // We also need to update the current state so that we don't
503 // end-up doing too much work during the next transaction.
504 // NOTE: We actually don't need hold the transaction lock here
505 // because State::w and State::h are only accessed from
506 // this thread
507 Layer::State& editTemp(currentState());
508 editTemp.w = editDraw.w;
509 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700510
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700511 // recompute visible region
512 recomputeVisibleRegions = true;
513 }
514
515 // we now have the correct size, unfreeze the screen
516 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700517 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700518
519 // get the crop region
520 setBufferCrop( lcblk->getCrop(buf) );
521
522 // get the transformation
523 setBufferTransform( lcblk->getTransform(buf) );
524
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700525 } else {
526 // this should not happen unless we ran out of memory while
527 // allocating the buffer. we're hoping that things will get back
528 // to normal the next time the app tries to draw into this buffer.
529 // meanwhile, pretend the screen didn't update.
530 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700531 }
532
Mathias Agopiane7005012009-10-07 16:44:10 -0700533 if (lcblk->getQueuedCount()) {
534 // signal an event if we have more buffers waiting
535 mFlinger->signalEvent();
536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537
Mathias Agopian245e4d72010-04-21 15:24:11 -0700538 /* a buffer was posted, so we need to call reloadTexture(), which
539 * will update our internal data structures (eg: EGLImageKHR or
540 * texture names). we need to do this even if mPostedDirtyRegion is
541 * empty -- it's orthogonal to the fact that a new buffer was posted,
542 * for instance, a degenerate case could be that the user did an empty
543 * update but repainted the buffer with appropriate content (after a
544 * resize for instance).
545 */
546 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547}
548
549void Layer::unlockPageFlip(
550 const Transform& planeTransform, Region& outDirtyRegion)
551{
552 Region dirtyRegion(mPostedDirtyRegion);
553 if (!dirtyRegion.isEmpty()) {
554 mPostedDirtyRegion.clear();
555 // The dirty region is given in the layer's coordinate space
556 // transform the dirty region by the surface's transformation
557 // and the global transformation.
558 const Layer::State& s(drawingState());
559 const Transform tr(planeTransform * s.transform);
560 dirtyRegion = tr.transform(dirtyRegion);
561
562 // At this point, the dirty region is in screen space.
563 // Make sure it's constrained by the visible region (which
564 // is in screen space as well).
565 dirtyRegion.andSelf(visibleRegionScreen);
566 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800568 if (visibleRegionScreen.isEmpty()) {
569 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700570 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800571 mFreezeLock.clear();
572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573}
574
575void Layer::finishPageFlip()
576{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700577 ClientRef::Access sharedClient(mUserClientRef);
578 SharedBufferServer* lcblk(sharedClient.get());
579 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700580 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700581 if (buf >= 0) {
582 status_t err = lcblk->unlock( buf );
583 LOGE_IF(err!=NO_ERROR,
584 "layer %p, buffer=%d wasn't locked!",
585 this, buf);
586 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700587 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588}
589
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700590
591void Layer::dump(String8& result, char* buffer, size_t SIZE) const
592{
593 LayerBaseClient::dump(result, buffer, SIZE);
594
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700595 ClientRef::Access sharedClient(mUserClientRef);
596 SharedBufferServer* lcblk(sharedClient.get());
597 uint32_t totalTime = 0;
598 if (lcblk) {
599 SharedBufferStack::Statistics stats = lcblk->getStats();
600 totalTime= stats.totalTime;
601 result.append( lcblk->dump(" ") );
602 }
603
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700604 sp<const GraphicBuffer> buf0(getBuffer(0));
605 sp<const GraphicBuffer> buf1(getBuffer(1));
606 uint32_t w0=0, h0=0, s0=0;
607 uint32_t w1=0, h1=0, s1=0;
608 if (buf0 != 0) {
609 w0 = buf0->getWidth();
610 h0 = buf0->getHeight();
611 s0 = buf0->getStride();
612 }
613 if (buf1 != 0) {
614 w1 = buf1->getWidth();
615 h1 = buf1->getHeight();
616 s1 = buf1->getStride();
617 }
618 snprintf(buffer, SIZE,
619 " "
620 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
621 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700622 mFormat, w0, h0, s0, w1, h1, s1,
623 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700624
625 result.append(buffer);
626}
627
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700628// ---------------------------------------------------------------------------
629
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700630Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700631 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700632}
633
634Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700635}
636
637int32_t Layer::ClientRef::getToken() const {
638 Mutex::Autolock _l(mLock);
639 return mToken;
640}
641
Mathias Agopian579b3f82010-06-08 19:54:15 -0700642sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700643 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700644 return mUserClient.promote();
645}
646
647status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
648 const sp<SharedBufferServer>& sharedClient, int32_t token) {
649 Mutex::Autolock _l(mLock);
650
651 { // scope for strong mUserClient reference
652 sp<UserClient> userClient(mUserClient.promote());
653 if (mUserClient != 0 && mControlBlock != 0) {
654 mControlBlock->setStatus(NO_INIT);
655 }
656 }
657
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700658 mUserClient = uc;
659 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700660 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700661 return NO_ERROR;
662}
663
664sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
665 return mUserClient.promote();
666}
667
668// this class gives us access to SharedBufferServer safely
669// it makes sure the UserClient (and its associated shared memory)
670// won't go away while we're accessing it.
671Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700672 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700673{
674 Mutex::Autolock _l(ref.mLock);
675 mUserClientStrongRef = ref.mUserClient.promote();
676 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700677 mControlBlock = ref.mControlBlock;
678}
679
680Layer::ClientRef::Access::~Access()
681{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700682}
683
684// ---------------------------------------------------------------------------
685
Mathias Agopiand606de62010-05-10 20:06:11 -0700686Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700687 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700688 mActiveBuffer(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700689{
690}
691
Mathias Agopianbb641242010-05-18 17:06:55 -0700692Layer::BufferManager::~BufferManager()
693{
694}
695
696status_t Layer::BufferManager::resize(size_t size)
697{
698 Mutex::Autolock _l(mLock);
699 mNumBuffers = size;
700 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700701}
702
703// only for debugging
704sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
705 return mBufferData[index].buffer;
706}
707
708status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700709 mActiveBuffer = index;
710 return NO_ERROR;
711}
712
713size_t Layer::BufferManager::getActiveBufferIndex() const {
714 return mActiveBuffer;
715}
716
717Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700718 Texture res;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700719 if (mFailover || mActiveBuffer<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700720 res = mFailoverTexture;
721 } else {
722 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
723 }
724 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700725}
726
727sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700728 sp<GraphicBuffer> result;
729 const ssize_t activeBuffer = mActiveBuffer;
730 if (activeBuffer >= 0) {
731 BufferData const * const buffers = mBufferData;
732 Mutex::Autolock _l(mLock);
733 result = buffers[activeBuffer].buffer;
734 }
735 return result;
Mathias Agopiand606de62010-05-10 20:06:11 -0700736}
737
738sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
739{
Mathias Agopianbb641242010-05-18 17:06:55 -0700740 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700741 sp<GraphicBuffer> buffer;
742 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700743 buffer = buffers[index].buffer;
744 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700745 return buffer;
746}
747
748status_t Layer::BufferManager::attachBuffer(size_t index,
749 const sp<GraphicBuffer>& buffer)
750{
Mathias Agopianbb641242010-05-18 17:06:55 -0700751 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700752 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700753 buffers[index].buffer = buffer;
754 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700755 return NO_ERROR;
756}
757
758status_t Layer::BufferManager::destroy(EGLDisplay dpy)
759{
Mathias Agopianbb641242010-05-18 17:06:55 -0700760 BufferData* const buffers = mBufferData;
761 size_t num;
762 { // scope for the lock
763 Mutex::Autolock _l(mLock);
764 num = mNumBuffers;
765 for (size_t i=0 ; i<num ; i++) {
766 buffers[i].buffer = 0;
767 }
768 }
769 for (size_t i=0 ; i<num ; i++) {
770 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700771 }
772 destroyTexture(&mFailoverTexture, dpy);
773 return NO_ERROR;
774}
775
776status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
777 const sp<GraphicBuffer>& buffer)
778{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700779 status_t err = NO_INIT;
780 ssize_t index = mActiveBuffer;
781 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700782 if (!mFailover) {
783 Image& texture(mBufferData[index].texture);
784 err = mTextureManager.initEglImage(&texture, dpy, buffer);
785 // if EGLImage fails, we switch to regular texture mode, and we
786 // free all resources associated with using EGLImages.
787 if (err == NO_ERROR) {
788 mFailover = false;
789 destroyTexture(&mFailoverTexture, dpy);
790 } else {
791 mFailover = true;
792 const size_t num = mNumBuffers;
793 for (size_t i=0 ; i<num ; i++) {
794 destroyTexture(&mBufferData[i].texture, dpy);
795 }
Andreas Hubere049a952010-06-25 09:25:19 -0700796 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700797 } else {
798 // we failed once, don't try again
799 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700800 }
801 }
802 return err;
803}
804
805status_t Layer::BufferManager::loadTexture(
806 const Region& dirty, const GGLSurface& t)
807{
808 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
809}
810
Mathias Agopianbb641242010-05-18 17:06:55 -0700811status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
812{
813 if (tex->name != -1U) {
814 glDeleteTextures(1, &tex->name);
815 tex->name = -1U;
816 }
817 if (tex->image != EGL_NO_IMAGE_KHR) {
818 eglDestroyImageKHR(dpy, tex->image);
819 tex->image = EGL_NO_IMAGE_KHR;
820 }
821 return NO_ERROR;
822}
823
Mathias Agopiand606de62010-05-10 20:06:11 -0700824// ---------------------------------------------------------------------------
825
Mathias Agopian9a112062009-04-17 19:36:26 -0700826Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700827 const sp<Layer>& owner)
828 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700829{
830}
831
832Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700833{
834}
835
Mathias Agopiana138f892010-05-21 17:24:35 -0700836sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
837 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700838{
Mathias Agopian3330b202009-10-05 17:07:12 -0700839 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700840 sp<Layer> owner(getOwner());
841 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700842 /*
843 * requestBuffer() cannot be called from the main thread
844 * as it could cause a dead-lock, since it may have to wait
845 * on conditions updated my the main thread.
846 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700847 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700848 }
849 return buffer;
850}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700852status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
853{
854 status_t err = DEAD_OBJECT;
855 sp<Layer> owner(getOwner());
856 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700857 /*
858 * setBufferCount() cannot be called from the main thread
859 * as it could cause a dead-lock, since it may have to wait
860 * on conditions updated my the main thread.
861 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700862 err = owner->setBufferCount(bufferCount);
863 }
864 return err;
865}
866
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867// ---------------------------------------------------------------------------
868
869
870}; // namespace android