blob: 6f3c66d74c4de3e26e1de9c4ece6e3419c3e8e53 [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 Agopian3330b202009-10-05 17:07:12 -0700199 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700200 LOGE_IF(res, "error %d (%s) locking buffer %p",
201 res, strerror(res), buffer.get());
202 if (res == NO_ERROR) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700203 mBufferManager.loadTexture(dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700204 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209void Layer::onDraw(const Region& clip) const
210{
Mathias Agopiand606de62010-05-10 20:06:11 -0700211 Texture tex(mBufferManager.getActiveTexture());
212 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700214 // in fact never been drawn into. This happens frequently with
215 // SurfaceView because the WindowManager can't know when the client
216 // has drawn the first time.
217
218 // If there is nothing under us, we paint the screen in black, otherwise
219 // we just skip this update.
220
221 // figure out if there is something below us
222 Region under;
223 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
224 const size_t count = drawingLayers.size();
225 for (size_t i=0 ; i<count ; ++i) {
226 const sp<LayerBase>& layer(drawingLayers[i]);
227 if (layer.get() == static_cast<LayerBase const*>(this))
228 break;
229 under.orSelf(layer->visibleRegionScreen);
230 }
231 // if not everything below us is covered, we plug the holes!
232 Region holes(clip.subtract(under));
233 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700234 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 return;
237 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700238 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239}
240
Mathias Agopiana7f66922010-05-26 22:08:52 -0700241bool Layer::needsFiltering() const
242{
243 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
244 // NOTE: there is a race here, because mFixedSize is updated in a
245 // binder transaction. however, it doesn't really matter since it is
246 // evaluated each time we draw. To be perfectly correct, this flag
247 // would have to be associated with a buffer.
248 if (mFixedSize)
249 return true;
250 }
251 return LayerBase::needsFiltering();
252}
253
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700254
255status_t Layer::setBufferCount(int bufferCount)
256{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700257 ClientRef::Access sharedClient(mUserClientRef);
258 SharedBufferServer* lcblk(sharedClient.get());
259 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700260 // oops, the client is already gone
261 return DEAD_OBJECT;
262 }
263
Mathias Agopianbb641242010-05-18 17:06:55 -0700264 // NOTE: lcblk->resize() is protected by an internal lock
265 status_t err = lcblk->resize(bufferCount);
266 if (err == NO_ERROR)
267 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700268
269 return err;
270}
271
Mathias Agopiana138f892010-05-21 17:24:35 -0700272sp<GraphicBuffer> Layer::requestBuffer(int index,
273 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
274 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275{
Mathias Agopian3330b202009-10-05 17:07:12 -0700276 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700277
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700278 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700279 return buffer;
280
281 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
282 return buffer;
283
Mathias Agopian48d819a2009-09-10 19:41:18 -0700284 // this ensures our client doesn't go away while we're accessing
285 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700286 ClientRef::Access sharedClient(mUserClientRef);
287 SharedBufferServer* lcblk(sharedClient.get());
288 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700289 // oops, the client is already gone
290 return buffer;
291 }
292
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700294 * This is called from the client's Surface::dequeue(). This can happen
295 * at any time, especially while we're in the middle of using the
296 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
Mathias Agopian208cb072010-07-27 20:11:35 -0700299 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700300 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700301 { // scope for the lock
302 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700303 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
304 const bool formatChanged = mReqFormat != reqFormat;
305 mReqWidth = reqWidth;
306 mReqHeight = reqHeight;
307 mReqFormat = reqFormat;
308 mFixedSize = reqWidth && reqHeight;
309 w = reqWidth ? reqWidth : mWidth;
310 h = reqHeight ? reqHeight : mHeight;
311 f = reqFormat ? reqFormat : mFormat;
Mathias Agopiana138f892010-05-21 17:24:35 -0700312 if (fixedSizeChanged || formatChanged) {
313 lcblk->reallocateAllExcept(index);
314 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700315 }
316
Mathias Agopian208cb072010-07-27 20:11:35 -0700317 // here we have to reallocate a new buffer because the buffer could be
318 // used as the front buffer, or by a client in our process
319 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700320 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700321 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
322 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700323
324 if (err || buffer->handle == 0) {
325 LOGE_IF(err || buffer->handle == 0,
326 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
327 this, index, w, h, strerror(-err));
328 } else {
329 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700330 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
331 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700332 }
333
334 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700335 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700336 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700337 }
338 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339}
340
Mathias Agopian3330b202009-10-05 17:07:12 -0700341uint32_t Layer::getEffectiveUsage(uint32_t usage) const
342{
343 /*
344 * buffers used for software rendering, but h/w composition
345 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
346 *
347 * buffers used for h/w rendering and h/w composition
348 * are allocated with HW_RENDER | HW_TEXTURE
349 *
350 * buffers used with h/w rendering and either NPOT or no egl_image_ext
351 * are allocated with SW_READ_RARELY | HW_RENDER
352 *
353 */
354
355 if (mSecure) {
356 // secure buffer, don't store it into the GPU
357 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
358 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
359 } else {
360 // it's allowed to modify the usage flags here, but generally
361 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700362 // request EGLImage for all buffers
363 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700364 }
365 return usage;
366}
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368uint32_t Layer::doTransaction(uint32_t flags)
369{
370 const Layer::State& front(drawingState());
371 const Layer::State& temp(currentState());
372
Mathias Agopiana138f892010-05-21 17:24:35 -0700373 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
374 (front.requested_h != temp.requested_h);
375
376 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700377 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700379 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
380 this,
381 int(temp.requested_w), int(temp.requested_h),
382 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383
Mathias Agopiana138f892010-05-21 17:24:35 -0700384 if (!isFixedSize()) {
385 // we're being resized and there is a freeze display request,
386 // acquire a freeze lock, so that the screen stays put
387 // until we've redrawn at the new size; this is to avoid
388 // glitches upon orientation changes.
389 if (mFlinger->hasFreezeRequest()) {
390 // if the surface is hidden, don't try to acquire the
391 // freeze lock, since hidden surfaces may never redraw
392 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
393 mFreezeLock = mFlinger->getFreezeLock();
394 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700396
397 // this will make sure LayerBase::doTransaction doesn't update
398 // the drawing state's size
399 Layer::State& editDraw(mDrawingState);
400 editDraw.requested_w = temp.requested_w;
401 editDraw.requested_h = temp.requested_h;
402
403 // record the new size, form this point on, when the client request
404 // a buffer, it'll get the new size.
405 setBufferSize(temp.requested_w, temp.requested_h);
406
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700407 ClientRef::Access sharedClient(mUserClientRef);
408 SharedBufferServer* lcblk(sharedClient.get());
409 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700410 // all buffers need reallocation
411 lcblk->reallocateAll();
412 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700413 } else {
414 // record the new size
415 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 }
417 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 if (temp.sequence != front.sequence) {
420 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
421 // this surface is now hidden, so it shouldn't hold a freeze lock
422 // (it may never redraw, which is fine if it is hidden)
423 mFreezeLock.clear();
424 }
425 }
426
427 return LayerBase::doTransaction(flags);
428}
429
Mathias Agopiana138f892010-05-21 17:24:35 -0700430void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700431 Mutex::Autolock _l(mLock);
432 mWidth = w;
433 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
Mathias Agopiana138f892010-05-21 17:24:35 -0700436bool Layer::isFixedSize() const {
437 Mutex::Autolock _l(mLock);
438 return mFixedSize;
439}
440
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441// ----------------------------------------------------------------------------
442// pageflip handling...
443// ----------------------------------------------------------------------------
444
445void Layer::lockPageFlip(bool& recomputeVisibleRegions)
446{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700447 ClientRef::Access sharedClient(mUserClientRef);
448 SharedBufferServer* lcblk(sharedClient.get());
449 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700450 // client died
451 recomputeVisibleRegions = true;
452 return;
453 }
454
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700455 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700456 if (buf == NOT_ENOUGH_DATA) {
457 // NOTE: This is not an error, it simply means there is nothing to
458 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700459 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 return;
461 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700462
Mathias Agopiand606de62010-05-10 20:06:11 -0700463 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700464 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700465 mPostedDirtyRegion.clear();
466 return;
467 }
468
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700469 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700470 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700471 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700472 mPostedDirtyRegion.clear();
473 return;
474 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475
Mathias Agopian3330b202009-10-05 17:07:12 -0700476 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700477 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700478 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700479 // compute the posted region
480 const Region dirty(lcblk->getDirtyRegion(buf));
481 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700482
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700483 // update the layer size and release freeze-lock
484 const Layer::State& front(drawingState());
485 if (newFrontBuffer->getWidth() == front.requested_w &&
486 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700487 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700488 if ((front.w != front.requested_w) ||
489 (front.h != front.requested_h))
490 {
491 // Here we pretend the transaction happened by updating the
492 // current and drawing states. Drawing state is only accessed
493 // in this thread, no need to have it locked
494 Layer::State& editDraw(mDrawingState);
495 editDraw.w = editDraw.requested_w;
496 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700497
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700498 // We also need to update the current state so that we don't
499 // end-up doing too much work during the next transaction.
500 // NOTE: We actually don't need hold the transaction lock here
501 // because State::w and State::h are only accessed from
502 // this thread
503 Layer::State& editTemp(currentState());
504 editTemp.w = editDraw.w;
505 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700506
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700507 // recompute visible region
508 recomputeVisibleRegions = true;
509 }
510
511 // we now have the correct size, unfreeze the screen
512 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700513 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700514
515 // get the crop region
516 setBufferCrop( lcblk->getCrop(buf) );
517
518 // get the transformation
519 setBufferTransform( lcblk->getTransform(buf) );
520
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700521 } else {
522 // this should not happen unless we ran out of memory while
523 // allocating the buffer. we're hoping that things will get back
524 // to normal the next time the app tries to draw into this buffer.
525 // meanwhile, pretend the screen didn't update.
526 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700527 }
528
Mathias Agopiane7005012009-10-07 16:44:10 -0700529 if (lcblk->getQueuedCount()) {
530 // signal an event if we have more buffers waiting
531 mFlinger->signalEvent();
532 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533
Mathias Agopian245e4d72010-04-21 15:24:11 -0700534 /* a buffer was posted, so we need to call reloadTexture(), which
535 * will update our internal data structures (eg: EGLImageKHR or
536 * texture names). we need to do this even if mPostedDirtyRegion is
537 * empty -- it's orthogonal to the fact that a new buffer was posted,
538 * for instance, a degenerate case could be that the user did an empty
539 * update but repainted the buffer with appropriate content (after a
540 * resize for instance).
541 */
542 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543}
544
545void Layer::unlockPageFlip(
546 const Transform& planeTransform, Region& outDirtyRegion)
547{
548 Region dirtyRegion(mPostedDirtyRegion);
549 if (!dirtyRegion.isEmpty()) {
550 mPostedDirtyRegion.clear();
551 // The dirty region is given in the layer's coordinate space
552 // transform the dirty region by the surface's transformation
553 // and the global transformation.
554 const Layer::State& s(drawingState());
555 const Transform tr(planeTransform * s.transform);
556 dirtyRegion = tr.transform(dirtyRegion);
557
558 // At this point, the dirty region is in screen space.
559 // Make sure it's constrained by the visible region (which
560 // is in screen space as well).
561 dirtyRegion.andSelf(visibleRegionScreen);
562 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800564 if (visibleRegionScreen.isEmpty()) {
565 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700566 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800567 mFreezeLock.clear();
568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569}
570
571void Layer::finishPageFlip()
572{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700573 ClientRef::Access sharedClient(mUserClientRef);
574 SharedBufferServer* lcblk(sharedClient.get());
575 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700576 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700577 if (buf >= 0) {
578 status_t err = lcblk->unlock( buf );
579 LOGE_IF(err!=NO_ERROR,
580 "layer %p, buffer=%d wasn't locked!",
581 this, buf);
582 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700583 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584}
585
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700586
587void Layer::dump(String8& result, char* buffer, size_t SIZE) const
588{
589 LayerBaseClient::dump(result, buffer, SIZE);
590
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700591 ClientRef::Access sharedClient(mUserClientRef);
592 SharedBufferServer* lcblk(sharedClient.get());
593 uint32_t totalTime = 0;
594 if (lcblk) {
595 SharedBufferStack::Statistics stats = lcblk->getStats();
596 totalTime= stats.totalTime;
597 result.append( lcblk->dump(" ") );
598 }
599
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700600 sp<const GraphicBuffer> buf0(getBuffer(0));
601 sp<const GraphicBuffer> buf1(getBuffer(1));
602 uint32_t w0=0, h0=0, s0=0;
603 uint32_t w1=0, h1=0, s1=0;
604 if (buf0 != 0) {
605 w0 = buf0->getWidth();
606 h0 = buf0->getHeight();
607 s0 = buf0->getStride();
608 }
609 if (buf1 != 0) {
610 w1 = buf1->getWidth();
611 h1 = buf1->getHeight();
612 s1 = buf1->getStride();
613 }
614 snprintf(buffer, SIZE,
615 " "
616 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
617 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700618 mFormat, w0, h0, s0, w1, h1, s1,
619 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700620
621 result.append(buffer);
622}
623
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624// ---------------------------------------------------------------------------
625
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700626Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700627 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700628}
629
630Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700631}
632
633int32_t Layer::ClientRef::getToken() const {
634 Mutex::Autolock _l(mLock);
635 return mToken;
636}
637
Mathias Agopian579b3f82010-06-08 19:54:15 -0700638sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700639 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700640 return mUserClient.promote();
641}
642
643status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
644 const sp<SharedBufferServer>& sharedClient, int32_t token) {
645 Mutex::Autolock _l(mLock);
646
647 { // scope for strong mUserClient reference
648 sp<UserClient> userClient(mUserClient.promote());
649 if (mUserClient != 0 && mControlBlock != 0) {
650 mControlBlock->setStatus(NO_INIT);
651 }
652 }
653
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700654 mUserClient = uc;
655 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700656 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700657 return NO_ERROR;
658}
659
660sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
661 return mUserClient.promote();
662}
663
664// this class gives us access to SharedBufferServer safely
665// it makes sure the UserClient (and its associated shared memory)
666// won't go away while we're accessing it.
667Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700668 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700669{
670 Mutex::Autolock _l(ref.mLock);
671 mUserClientStrongRef = ref.mUserClient.promote();
672 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700673 mControlBlock = ref.mControlBlock;
674}
675
676Layer::ClientRef::Access::~Access()
677{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700678}
679
680// ---------------------------------------------------------------------------
681
Mathias Agopiand606de62010-05-10 20:06:11 -0700682Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700683 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700684 mActiveBuffer(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700685{
686}
687
Mathias Agopianbb641242010-05-18 17:06:55 -0700688Layer::BufferManager::~BufferManager()
689{
690}
691
692status_t Layer::BufferManager::resize(size_t size)
693{
694 Mutex::Autolock _l(mLock);
695 mNumBuffers = size;
696 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700697}
698
699// only for debugging
700sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
701 return mBufferData[index].buffer;
702}
703
704status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700705 mActiveBuffer = index;
706 return NO_ERROR;
707}
708
709size_t Layer::BufferManager::getActiveBufferIndex() const {
710 return mActiveBuffer;
711}
712
713Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700714 Texture res;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700715 if (mFailover || mActiveBuffer<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700716 res = mFailoverTexture;
717 } else {
718 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
719 }
720 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700721}
722
723sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700724 sp<GraphicBuffer> result;
725 const ssize_t activeBuffer = mActiveBuffer;
726 if (activeBuffer >= 0) {
727 BufferData const * const buffers = mBufferData;
728 Mutex::Autolock _l(mLock);
729 result = buffers[activeBuffer].buffer;
730 }
731 return result;
Mathias Agopiand606de62010-05-10 20:06:11 -0700732}
733
734sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
735{
Mathias Agopianbb641242010-05-18 17:06:55 -0700736 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700737 sp<GraphicBuffer> buffer;
738 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700739 buffer = buffers[index].buffer;
740 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700741 return buffer;
742}
743
744status_t Layer::BufferManager::attachBuffer(size_t index,
745 const sp<GraphicBuffer>& buffer)
746{
Mathias Agopianbb641242010-05-18 17:06:55 -0700747 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700748 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700749 buffers[index].buffer = buffer;
750 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700751 return NO_ERROR;
752}
753
754status_t Layer::BufferManager::destroy(EGLDisplay dpy)
755{
Mathias Agopianbb641242010-05-18 17:06:55 -0700756 BufferData* const buffers = mBufferData;
757 size_t num;
758 { // scope for the lock
759 Mutex::Autolock _l(mLock);
760 num = mNumBuffers;
761 for (size_t i=0 ; i<num ; i++) {
762 buffers[i].buffer = 0;
763 }
764 }
765 for (size_t i=0 ; i<num ; i++) {
766 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700767 }
768 destroyTexture(&mFailoverTexture, dpy);
769 return NO_ERROR;
770}
771
772status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
773 const sp<GraphicBuffer>& buffer)
774{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700775 status_t err = NO_INIT;
776 ssize_t index = mActiveBuffer;
777 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700778 if (!mFailover) {
779 Image& texture(mBufferData[index].texture);
780 err = mTextureManager.initEglImage(&texture, dpy, buffer);
781 // if EGLImage fails, we switch to regular texture mode, and we
782 // free all resources associated with using EGLImages.
783 if (err == NO_ERROR) {
784 mFailover = false;
785 destroyTexture(&mFailoverTexture, dpy);
786 } else {
787 mFailover = true;
788 const size_t num = mNumBuffers;
789 for (size_t i=0 ; i<num ; i++) {
790 destroyTexture(&mBufferData[i].texture, dpy);
791 }
Andreas Hubere049a952010-06-25 09:25:19 -0700792 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700793 } else {
794 // we failed once, don't try again
795 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700796 }
797 }
798 return err;
799}
800
801status_t Layer::BufferManager::loadTexture(
802 const Region& dirty, const GGLSurface& t)
803{
804 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
805}
806
Mathias Agopianbb641242010-05-18 17:06:55 -0700807status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
808{
809 if (tex->name != -1U) {
810 glDeleteTextures(1, &tex->name);
811 tex->name = -1U;
812 }
813 if (tex->image != EGL_NO_IMAGE_KHR) {
814 eglDestroyImageKHR(dpy, tex->image);
815 tex->image = EGL_NO_IMAGE_KHR;
816 }
817 return NO_ERROR;
818}
819
Mathias Agopiand606de62010-05-10 20:06:11 -0700820// ---------------------------------------------------------------------------
821
Mathias Agopian9a112062009-04-17 19:36:26 -0700822Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700823 const sp<Layer>& owner)
824 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700825{
826}
827
828Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700829{
830}
831
Mathias Agopiana138f892010-05-21 17:24:35 -0700832sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
833 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700834{
Mathias Agopian3330b202009-10-05 17:07:12 -0700835 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700836 sp<Layer> owner(getOwner());
837 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700838 /*
839 * requestBuffer() cannot be called from the main thread
840 * as it could cause a dead-lock, since it may have to wait
841 * on conditions updated my the main thread.
842 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700843 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700844 }
845 return buffer;
846}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700848status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
849{
850 status_t err = DEAD_OBJECT;
851 sp<Layer> owner(getOwner());
852 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700853 /*
854 * setBufferCount() cannot be called from the main thread
855 * as it could cause a dead-lock, since it may have to wait
856 * on conditions updated my the main thread.
857 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700858 err = owner->setBufferCount(bufferCount);
859 }
860 return err;
861}
862
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863// ---------------------------------------------------------------------------
864
865
866}; // namespace android