blob: a94fdd4e6db6057a444b5548117692c3bf3ed40c [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 Agopianb7e930d2010-06-01 15:12:58 -070079 SharedBufferServer* lcblk = new SharedBufferServer(
80 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);
84 if (err != NO_ERROR) {
85 LOGE("ClientRef::setToken(%p, %p, %u) failed",
86 userClient.get(), lcblk, token);
87 delete lcblk;
88 }
89
90 return err;
91}
92
93int32_t Layer::getToken() const
94{
95 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -070096}
97
Mathias Agopiand606de62010-05-10 20:06:11 -070098// called with SurfaceFlinger::mStateLock as soon as the layer is entered
99// in the purgatory list
100void Layer::onRemoved()
101{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700102 ClientRef::Access sharedClient(mUserClientRef);
103 SharedBufferServer* lcblk(sharedClient.get());
104 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700105 // wake up the condition
106 lcblk->setStatus(NO_INIT);
107 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700108}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700109
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111{
112 return mSurface;
113}
114
Mathias Agopian9a112062009-04-17 19:36:26 -0700115status_t Layer::ditch()
116{
Mathias Agopianbb641242010-05-18 17:06:55 -0700117 // NOTE: Called from the main UI thread
118
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700119 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800120 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700121
122 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
123 mBufferManager.destroy(dpy);
124 mSurface.clear();
125
126 Mutex::Autolock _l(mLock);
127 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700128 return NO_ERROR;
129}
130
Mathias Agopianf9d93272009-06-19 17:00:27 -0700131status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 PixelFormat format, uint32_t flags)
133{
Mathias Agopian401c2572009-09-23 19:16:27 -0700134 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 PixelFormatInfo info;
136 status_t err = getPixelFormatInfo(format, &info);
137 if (err) return err;
138
Mathias Agopian401c2572009-09-23 19:16:27 -0700139 // the display's pixel format
140 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700141 uint32_t const maxSurfaceDims = min(
142 hw.getMaxTextureSize(), hw.getMaxViewportDims());
143
144 // never allow a surface larger than what our underlying GL implementation
145 // can handle.
146 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
147 return BAD_VALUE;
148 }
149
Mathias Agopian401c2572009-09-23 19:16:27 -0700150 PixelFormatInfo displayInfo;
151 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700152 const uint32_t hwFlags = hw.getFlags();
153
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700154 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700155 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700156 mHeight = h;
Mathias Agopian3330b202009-10-05 17:07:12 -0700157 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700159
Mathias Agopian401c2572009-09-23 19:16:27 -0700160 // we use the red index
161 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
162 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
163 mNeedsDithering = layerRedsize > displayRedSize;
164
Mathias Agopian96f08192010-06-02 23:28:45 -0700165 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 return NO_ERROR;
167}
168
169void Layer::reloadTexture(const Region& dirty)
170{
Mathias Agopiand606de62010-05-10 20:06:11 -0700171 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800172 if (buffer == NULL) {
173 // this situation can happen if we ran out of memory for instance.
174 // not much we can do. continue to use whatever texture was bound
175 // to this context.
176 return;
177 }
178
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700179#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian57720c32009-10-21 16:27:21 -0700180 if (mFlags & DisplayHardware::DIRECT_TEXTURE) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700181 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
182 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
183 // not sure what we can do here...
184 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
185 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700186 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700187 } else
188#endif
189 {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800190slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700191 GGLSurface t;
Mathias Agopian3330b202009-10-05 17:07:12 -0700192 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
Mathias Agopian0926f502009-05-04 14:17:04 -0700193 LOGE_IF(res, "error %d (%s) locking buffer %p",
194 res, strerror(res), buffer.get());
195 if (res == NO_ERROR) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700196 mBufferManager.loadTexture(dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700197 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700198 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202void Layer::onDraw(const Region& clip) const
203{
Mathias Agopiand606de62010-05-10 20:06:11 -0700204 Texture tex(mBufferManager.getActiveTexture());
205 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700207 // in fact never been drawn into. This happens frequently with
208 // SurfaceView because the WindowManager can't know when the client
209 // has drawn the first time.
210
211 // If there is nothing under us, we paint the screen in black, otherwise
212 // we just skip this update.
213
214 // figure out if there is something below us
215 Region under;
216 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
217 const size_t count = drawingLayers.size();
218 for (size_t i=0 ; i<count ; ++i) {
219 const sp<LayerBase>& layer(drawingLayers[i]);
220 if (layer.get() == static_cast<LayerBase const*>(this))
221 break;
222 under.orSelf(layer->visibleRegionScreen);
223 }
224 // if not everything below us is covered, we plug the holes!
225 Region holes(clip.subtract(under));
226 if (!holes.isEmpty()) {
227 clearWithOpenGL(holes);
228 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 return;
230 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700231 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232}
233
Mathias Agopiana7f66922010-05-26 22:08:52 -0700234bool Layer::needsFiltering() const
235{
236 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
237 // NOTE: there is a race here, because mFixedSize is updated in a
238 // binder transaction. however, it doesn't really matter since it is
239 // evaluated each time we draw. To be perfectly correct, this flag
240 // would have to be associated with a buffer.
241 if (mFixedSize)
242 return true;
243 }
244 return LayerBase::needsFiltering();
245}
246
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700247
248status_t Layer::setBufferCount(int bufferCount)
249{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700250 ClientRef::Access sharedClient(mUserClientRef);
251 SharedBufferServer* lcblk(sharedClient.get());
252 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700253 // oops, the client is already gone
254 return DEAD_OBJECT;
255 }
256
Mathias Agopianbb641242010-05-18 17:06:55 -0700257 // NOTE: lcblk->resize() is protected by an internal lock
258 status_t err = lcblk->resize(bufferCount);
259 if (err == NO_ERROR)
260 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700261
262 return err;
263}
264
Mathias Agopiana138f892010-05-21 17:24:35 -0700265sp<GraphicBuffer> Layer::requestBuffer(int index,
266 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
267 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268{
Mathias Agopian3330b202009-10-05 17:07:12 -0700269 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700270
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700271 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700272 return buffer;
273
274 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
275 return buffer;
276
Mathias Agopian48d819a2009-09-10 19:41:18 -0700277 // this ensures our client doesn't go away while we're accessing
278 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700279 ClientRef::Access sharedClient(mUserClientRef);
280 SharedBufferServer* lcblk(sharedClient.get());
281 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700282 // oops, the client is already gone
283 return buffer;
284 }
285
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700287 * This is called from the client's Surface::dequeue(). This can happen
288 * at any time, especially while we're in the middle of using the
289 * buffer 'index' as our front buffer.
Mathias Agopianbb641242010-05-18 17:06:55 -0700290 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700291 * Make sure the buffer we're resizing is not the front buffer and has been
292 * dequeued. Once this condition is asserted, we are guaranteed that this
293 * buffer cannot become the front buffer under our feet, since we're called
294 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700295 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700296 status_t err = lcblk->assertReallocate(index);
297 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
Mathias Agopian48d819a2009-09-10 19:41:18 -0700298 if (err != NO_ERROR) {
299 // the surface may have died
300 return buffer;
301 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopiana138f892010-05-21 17:24:35 -0700303 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700304 { // scope for the lock
305 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700306 const bool fixedSizeChanged = mFixedSize != (reqWidth && reqHeight);
307 const bool formatChanged = mReqFormat != reqFormat;
308 mReqWidth = reqWidth;
309 mReqHeight = reqHeight;
310 mReqFormat = reqFormat;
311 mFixedSize = reqWidth && reqHeight;
312 w = reqWidth ? reqWidth : mWidth;
313 h = reqHeight ? reqHeight : mHeight;
314 f = reqFormat ? reqFormat : mFormat;
Mathias Agopiand606de62010-05-10 20:06:11 -0700315 buffer = mBufferManager.detachBuffer(index);
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 Agopian3330b202009-10-05 17:07:12 -0700321 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian6d9f6982009-09-17 19:19:08 -0700322 if (buffer!=0 && buffer->getStrongCount() == 1) {
Mathias Agopiana138f892010-05-21 17:24:35 -0700323 err = buffer->reallocate(w, h, f, effectiveUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700325 // here we have to reallocate a new buffer because we could have a
326 // client in our process with a reference to it (eg: status bar),
327 // and we can't release the handle under its feet.
328 buffer.clear();
Mathias Agopiana138f892010-05-21 17:24:35 -0700329 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700330 err = buffer->initCheck();
331 }
332
333 if (err || buffer->handle == 0) {
334 LOGE_IF(err || buffer->handle == 0,
335 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
336 this, index, w, h, strerror(-err));
337 } else {
338 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700339 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
340 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700341 }
342
343 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700344 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700345 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 }
347 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348}
349
Mathias Agopian3330b202009-10-05 17:07:12 -0700350uint32_t Layer::getEffectiveUsage(uint32_t usage) const
351{
352 /*
353 * buffers used for software rendering, but h/w composition
354 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
355 *
356 * buffers used for h/w rendering and h/w composition
357 * are allocated with HW_RENDER | HW_TEXTURE
358 *
359 * buffers used with h/w rendering and either NPOT or no egl_image_ext
360 * are allocated with SW_READ_RARELY | HW_RENDER
361 *
362 */
363
364 if (mSecure) {
365 // secure buffer, don't store it into the GPU
366 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
367 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
368 } else {
369 // it's allowed to modify the usage flags here, but generally
370 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700371 // request EGLImage for all buffers
372 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700373 }
374 return usage;
375}
376
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377uint32_t Layer::doTransaction(uint32_t flags)
378{
379 const Layer::State& front(drawingState());
380 const Layer::State& temp(currentState());
381
Mathias Agopiana138f892010-05-21 17:24:35 -0700382 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
383 (front.requested_h != temp.requested_h);
384
385 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700386 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700388 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
389 this,
390 int(temp.requested_w), int(temp.requested_h),
391 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392
Mathias Agopiana138f892010-05-21 17:24:35 -0700393 if (!isFixedSize()) {
394 // we're being resized and there is a freeze display request,
395 // acquire a freeze lock, so that the screen stays put
396 // until we've redrawn at the new size; this is to avoid
397 // glitches upon orientation changes.
398 if (mFlinger->hasFreezeRequest()) {
399 // if the surface is hidden, don't try to acquire the
400 // freeze lock, since hidden surfaces may never redraw
401 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
402 mFreezeLock = mFlinger->getFreezeLock();
403 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700405
406 // this will make sure LayerBase::doTransaction doesn't update
407 // the drawing state's size
408 Layer::State& editDraw(mDrawingState);
409 editDraw.requested_w = temp.requested_w;
410 editDraw.requested_h = temp.requested_h;
411
412 // record the new size, form this point on, when the client request
413 // a buffer, it'll get the new size.
414 setBufferSize(temp.requested_w, temp.requested_h);
415
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700416 ClientRef::Access sharedClient(mUserClientRef);
417 SharedBufferServer* lcblk(sharedClient.get());
418 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700419 // all buffers need reallocation
420 lcblk->reallocateAll();
421 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700422 } else {
423 // record the new size
424 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 }
426 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 if (temp.sequence != front.sequence) {
429 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
430 // this surface is now hidden, so it shouldn't hold a freeze lock
431 // (it may never redraw, which is fine if it is hidden)
432 mFreezeLock.clear();
433 }
434 }
435
436 return LayerBase::doTransaction(flags);
437}
438
Mathias Agopiana138f892010-05-21 17:24:35 -0700439void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700440 Mutex::Autolock _l(mLock);
441 mWidth = w;
442 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443}
444
Mathias Agopiana138f892010-05-21 17:24:35 -0700445bool Layer::isFixedSize() const {
446 Mutex::Autolock _l(mLock);
447 return mFixedSize;
448}
449
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450// ----------------------------------------------------------------------------
451// pageflip handling...
452// ----------------------------------------------------------------------------
453
454void Layer::lockPageFlip(bool& recomputeVisibleRegions)
455{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700456 ClientRef::Access sharedClient(mUserClientRef);
457 SharedBufferServer* lcblk(sharedClient.get());
458 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700459 // client died
460 recomputeVisibleRegions = true;
461 return;
462 }
463
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700464 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700465 if (buf == NOT_ENOUGH_DATA) {
466 // NOTE: This is not an error, it simply means there is nothing to
467 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700468 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 return;
470 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700471
Mathias Agopiand606de62010-05-10 20:06:11 -0700472 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700473 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700474 mPostedDirtyRegion.clear();
475 return;
476 }
477
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700478 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700479 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700480 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700481 mPostedDirtyRegion.clear();
482 return;
483 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700485 // get the dirty region
Mathias Agopian3330b202009-10-05 17:07:12 -0700486 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700487 if (newFrontBuffer != NULL) {
488 // compute the posted region
489 const Region dirty(lcblk->getDirtyRegion(buf));
490 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700491
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700492 // update the layer size and release freeze-lock
493 const Layer::State& front(drawingState());
494 if (newFrontBuffer->getWidth() == front.requested_w &&
495 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700496 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700497 if ((front.w != front.requested_w) ||
498 (front.h != front.requested_h))
499 {
500 // Here we pretend the transaction happened by updating the
501 // current and drawing states. Drawing state is only accessed
502 // in this thread, no need to have it locked
503 Layer::State& editDraw(mDrawingState);
504 editDraw.w = editDraw.requested_w;
505 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700506
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700507 // We also need to update the current state so that we don't
508 // end-up doing too much work during the next transaction.
509 // NOTE: We actually don't need hold the transaction lock here
510 // because State::w and State::h are only accessed from
511 // this thread
512 Layer::State& editTemp(currentState());
513 editTemp.w = editDraw.w;
514 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700515
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700516 // recompute visible region
517 recomputeVisibleRegions = true;
518 }
519
520 // we now have the correct size, unfreeze the screen
521 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700522 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700523 } else {
524 // this should not happen unless we ran out of memory while
525 // allocating the buffer. we're hoping that things will get back
526 // to normal the next time the app tries to draw into this buffer.
527 // meanwhile, pretend the screen didn't update.
528 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700529 }
530
Mathias Agopiane7005012009-10-07 16:44:10 -0700531 if (lcblk->getQueuedCount()) {
532 // signal an event if we have more buffers waiting
533 mFlinger->signalEvent();
534 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535
Mathias Agopian245e4d72010-04-21 15:24:11 -0700536 /* a buffer was posted, so we need to call reloadTexture(), which
537 * will update our internal data structures (eg: EGLImageKHR or
538 * texture names). we need to do this even if mPostedDirtyRegion is
539 * empty -- it's orthogonal to the fact that a new buffer was posted,
540 * for instance, a degenerate case could be that the user did an empty
541 * update but repainted the buffer with appropriate content (after a
542 * resize for instance).
543 */
544 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545}
546
547void Layer::unlockPageFlip(
548 const Transform& planeTransform, Region& outDirtyRegion)
549{
550 Region dirtyRegion(mPostedDirtyRegion);
551 if (!dirtyRegion.isEmpty()) {
552 mPostedDirtyRegion.clear();
553 // The dirty region is given in the layer's coordinate space
554 // transform the dirty region by the surface's transformation
555 // and the global transformation.
556 const Layer::State& s(drawingState());
557 const Transform tr(planeTransform * s.transform);
558 dirtyRegion = tr.transform(dirtyRegion);
559
560 // At this point, the dirty region is in screen space.
561 // Make sure it's constrained by the visible region (which
562 // is in screen space as well).
563 dirtyRegion.andSelf(visibleRegionScreen);
564 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800566 if (visibleRegionScreen.isEmpty()) {
567 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700568 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800569 mFreezeLock.clear();
570 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571}
572
573void Layer::finishPageFlip()
574{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700575 ClientRef::Access sharedClient(mUserClientRef);
576 SharedBufferServer* lcblk(sharedClient.get());
577 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700578 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700579 if (buf >= 0) {
580 status_t err = lcblk->unlock( buf );
581 LOGE_IF(err!=NO_ERROR,
582 "layer %p, buffer=%d wasn't locked!",
583 this, buf);
584 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700585 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586}
587
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700588
589void Layer::dump(String8& result, char* buffer, size_t SIZE) const
590{
591 LayerBaseClient::dump(result, buffer, SIZE);
592
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700593 ClientRef::Access sharedClient(mUserClientRef);
594 SharedBufferServer* lcblk(sharedClient.get());
595 uint32_t totalTime = 0;
596 if (lcblk) {
597 SharedBufferStack::Statistics stats = lcblk->getStats();
598 totalTime= stats.totalTime;
599 result.append( lcblk->dump(" ") );
600 }
601
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700602 sp<const GraphicBuffer> buf0(getBuffer(0));
603 sp<const GraphicBuffer> buf1(getBuffer(1));
604 uint32_t w0=0, h0=0, s0=0;
605 uint32_t w1=0, h1=0, s1=0;
606 if (buf0 != 0) {
607 w0 = buf0->getWidth();
608 h0 = buf0->getHeight();
609 s0 = buf0->getStride();
610 }
611 if (buf1 != 0) {
612 w1 = buf1->getWidth();
613 h1 = buf1->getHeight();
614 s1 = buf1->getStride();
615 }
616 snprintf(buffer, SIZE,
617 " "
618 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
619 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700620 mFormat, w0, h0, s0, w1, h1, s1,
621 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700622
623 result.append(buffer);
624}
625
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700626// ---------------------------------------------------------------------------
627
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700628Layer::ClientRef::ClientRef()
629 : mToken(-1) {
630}
631
632Layer::ClientRef::~ClientRef() {
633 delete lcblk;
634}
635
636int32_t Layer::ClientRef::getToken() const {
637 Mutex::Autolock _l(mLock);
638 return mToken;
639}
640
641status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
642 SharedBufferServer* sharedClient, int32_t token) {
643 Mutex::Autolock _l(mLock);
644 if (mToken >= 0)
645 return INVALID_OPERATION;
646 mUserClient = uc;
647 mToken = token;
648 lcblk = sharedClient;
649 return NO_ERROR;
650}
651
652sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
653 return mUserClient.promote();
654}
655
656// this class gives us access to SharedBufferServer safely
657// it makes sure the UserClient (and its associated shared memory)
658// won't go away while we're accessing it.
659Layer::ClientRef::Access::Access(const ClientRef& ref)
660 : lcblk(0)
661{
662 Mutex::Autolock _l(ref.mLock);
663 mUserClientStrongRef = ref.mUserClient.promote();
664 if (mUserClientStrongRef != 0)
665 lcblk = ref.lcblk;
666}
667
668// ---------------------------------------------------------------------------
669
Mathias Agopiand606de62010-05-10 20:06:11 -0700670Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700671 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700672 mActiveBuffer(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700673{
674}
675
Mathias Agopianbb641242010-05-18 17:06:55 -0700676Layer::BufferManager::~BufferManager()
677{
678}
679
680status_t Layer::BufferManager::resize(size_t size)
681{
682 Mutex::Autolock _l(mLock);
683 mNumBuffers = size;
684 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700685}
686
687// only for debugging
688sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
689 return mBufferData[index].buffer;
690}
691
692status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700693 mActiveBuffer = index;
694 return NO_ERROR;
695}
696
697size_t Layer::BufferManager::getActiveBufferIndex() const {
698 return mActiveBuffer;
699}
700
701Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700702 Texture res;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700703 if (mFailover || mActiveBuffer<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700704 res = mFailoverTexture;
705 } else {
706 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
707 }
708 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700709}
710
711sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700712 sp<GraphicBuffer> result;
713 const ssize_t activeBuffer = mActiveBuffer;
714 if (activeBuffer >= 0) {
715 BufferData const * const buffers = mBufferData;
716 Mutex::Autolock _l(mLock);
717 result = buffers[activeBuffer].buffer;
718 }
719 return result;
Mathias Agopiand606de62010-05-10 20:06:11 -0700720}
721
722sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
723{
Mathias Agopianbb641242010-05-18 17:06:55 -0700724 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700725 sp<GraphicBuffer> buffer;
726 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700727 buffer = buffers[index].buffer;
728 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700729 return buffer;
730}
731
732status_t Layer::BufferManager::attachBuffer(size_t index,
733 const sp<GraphicBuffer>& buffer)
734{
Mathias Agopianbb641242010-05-18 17:06:55 -0700735 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700736 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700737 buffers[index].buffer = buffer;
738 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700739 return NO_ERROR;
740}
741
742status_t Layer::BufferManager::destroy(EGLDisplay dpy)
743{
Mathias Agopianbb641242010-05-18 17:06:55 -0700744 BufferData* const buffers = mBufferData;
745 size_t num;
746 { // scope for the lock
747 Mutex::Autolock _l(mLock);
748 num = mNumBuffers;
749 for (size_t i=0 ; i<num ; i++) {
750 buffers[i].buffer = 0;
751 }
752 }
753 for (size_t i=0 ; i<num ; i++) {
754 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700755 }
756 destroyTexture(&mFailoverTexture, dpy);
757 return NO_ERROR;
758}
759
760status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
761 const sp<GraphicBuffer>& buffer)
762{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700763 status_t err = NO_INIT;
764 ssize_t index = mActiveBuffer;
765 if (index >= 0) {
766 Image& texture(mBufferData[index].texture);
767 err = mTextureManager.initEglImage(&texture, dpy, buffer);
768 // if EGLImage fails, we switch to regular texture mode, and we
769 // free all resources associated with using EGLImages.
770 if (err == NO_ERROR) {
771 mFailover = false;
772 destroyTexture(&mFailoverTexture, dpy);
773 } else {
774 mFailover = true;
775 const size_t num = mNumBuffers;
776 for (size_t i=0 ; i<num ; i++) {
777 destroyTexture(&mBufferData[i].texture, dpy);
778 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700779 }
780 }
781 return err;
782}
783
784status_t Layer::BufferManager::loadTexture(
785 const Region& dirty, const GGLSurface& t)
786{
787 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
788}
789
Mathias Agopianbb641242010-05-18 17:06:55 -0700790status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
791{
792 if (tex->name != -1U) {
793 glDeleteTextures(1, &tex->name);
794 tex->name = -1U;
795 }
796 if (tex->image != EGL_NO_IMAGE_KHR) {
797 eglDestroyImageKHR(dpy, tex->image);
798 tex->image = EGL_NO_IMAGE_KHR;
799 }
800 return NO_ERROR;
801}
802
Mathias Agopiand606de62010-05-10 20:06:11 -0700803// ---------------------------------------------------------------------------
804
Mathias Agopian9a112062009-04-17 19:36:26 -0700805Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700806 const sp<Layer>& owner)
807 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700808{
809}
810
811Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700812{
813}
814
Mathias Agopiana138f892010-05-21 17:24:35 -0700815sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
816 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700817{
Mathias Agopian3330b202009-10-05 17:07:12 -0700818 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700819 sp<Layer> owner(getOwner());
820 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700821 /*
822 * requestBuffer() cannot be called from the main thread
823 * as it could cause a dead-lock, since it may have to wait
824 * on conditions updated my the main thread.
825 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700826 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700827 }
828 return buffer;
829}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700831status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
832{
833 status_t err = DEAD_OBJECT;
834 sp<Layer> owner(getOwner());
835 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700836 /*
837 * setBufferCount() cannot be called from the main thread
838 * as it could cause a dead-lock, since it may have to wait
839 * on conditions updated my the main thread.
840 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700841 err = owner->setBufferCount(bufferCount);
842 }
843 return err;
844}
845
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846// ---------------------------------------------------------------------------
847
848
849}; // namespace android