blob: 695cbfa0a80d6779cd0f68efe670ac43466f4881 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian6950e422009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
38
39
40#define DEBUG_RESIZE 0
41
42
43namespace android {
44
Mathias Agopian967dce32010-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 Project9066cfe2009-03-03 19:31:44 -080049// ---------------------------------------------------------------------------
50
Mathias Agopian593c05c2010-06-02 23:28:45 -070051Layer::Layer(SurfaceFlinger* flinger,
52 DisplayID display, const sp<Client>& client)
53 : LayerBaseClient(flinger, display, client),
Mathias Agopian781953d2010-06-25 18:02:21 -070054 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiancc934762009-09-23 19:16:27 -070055 mNeedsBlending(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070056 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070057 mSecure(false),
Mathias Agopian781953d2010-06-25 18:02:21 -070058 mTextureManager(),
Mathias Agopian2be352a2010-05-21 17:24:35 -070059 mBufferManager(mTextureManager),
60 mWidth(0), mHeight(0), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062}
63
64Layer::~Layer()
65{
Mathias Agopian898c4c92010-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 Agopian7623da42010-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 Agopian9f2c4fd2010-05-10 20:06:11 -070076}
77
Mathias Agopian7623da42010-06-01 15:12:58 -070078status_t Layer::setToken(const sp<UserClient>& userClient,
79 SharedClient* sharedClient, int32_t token)
Mathias Agopian593c05c2010-06-02 23:28:45 -070080{
Mathias Agopian5e140102010-06-08 19:54:15 -070081 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopian7623da42010-06-01 15:12:58 -070082 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian593c05c2010-06-02 23:28:45 -070083 getIdentity());
84
Mathias Agopian7623da42010-06-01 15:12:58 -070085 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian5e140102010-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 Agopian7623da42010-06-01 15:12:58 -070093 }
94
95 return err;
96}
97
98int32_t Layer::getToken() const
99{
100 return mUserClientRef.getToken();
Mathias Agopian593c05c2010-06-02 23:28:45 -0700101}
102
Mathias Agopian5e140102010-06-08 19:54:15 -0700103sp<UserClient> Layer::getClient() const
104{
105 return mUserClientRef.getClient();
106}
107
Mathias Agopian9f2c4fd2010-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 Agopian7623da42010-06-01 15:12:58 -0700112 ClientRef::Access sharedClient(mUserClientRef);
113 SharedBufferServer* lcblk(sharedClient.get());
114 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700115 // wake up the condition
116 lcblk->setStatus(NO_INIT);
117 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700118}
Mathias Agopian9779b222009-09-07 16:32:45 -0700119
Mathias Agopian1473f462009-04-10 14:24:30 -0700120sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
122 return mSurface;
123}
124
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700125status_t Layer::ditch()
126{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700127 // NOTE: Called from the main UI thread
128
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700129 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -0800130 mFreezeLock.clear();
Mathias Agopian898c4c92010-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 Agopian6cf0db22009-04-17 19:36:26 -0700138 return NO_ERROR;
139}
140
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700141status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 PixelFormat format, uint32_t flags)
143{
Mathias Agopiancc934762009-09-23 19:16:27 -0700144 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 PixelFormatInfo info;
146 status_t err = getPixelFormatInfo(format, &info);
147 if (err) return err;
148
Mathias Agopiancc934762009-09-23 19:16:27 -0700149 // the display's pixel format
150 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-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 Agopiancc934762009-09-23 19:16:27 -0700160 PixelFormatInfo displayInfo;
161 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700162 const uint32_t hwFlags = hw.getFlags();
163
Mathias Agopian9779b222009-09-07 16:32:45 -0700164 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700165 mWidth = w;
Mathias Agopian9779b222009-09-07 16:32:45 -0700166 mHeight = h;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700167
168 mReqFormat = format;
169 mReqWidth = w;
170 mReqHeight = h;
171
Mathias Agopian6950e422009-10-05 17:07:12 -0700172 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700174
Mathias Agopiancc934762009-09-23 19:16:27 -0700175 // we use the red index
176 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
177 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
178 mNeedsDithering = layerRedsize > displayRedSize;
179
Mathias Agopian593c05c2010-06-02 23:28:45 -0700180 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 return NO_ERROR;
182}
183
184void Layer::reloadTexture(const Region& dirty)
185{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700186 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-12-10 15:52:29 -0800187 if (buffer == NULL) {
188 // this situation can happen if we ran out of memory for instance.
189 // not much we can do. continue to use whatever texture was bound
190 // to this context.
191 return;
192 }
193
Mathias Agopian781953d2010-06-25 18:02:21 -0700194 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700195 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
196 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
197 // not sure what we can do here...
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700198 goto slowpath;
Mathias Agopian1473f462009-04-10 14:24:30 -0700199 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700200 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800201slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700202 GGLSurface t;
Mathias Agopianc817b222010-08-20 15:59:53 -0700203 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
204 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
205 LOGE_IF(res, "error %d (%s) locking buffer %p",
206 res, strerror(res), buffer.get());
207 if (res == NO_ERROR) {
208 mBufferManager.loadTexture(dirty, t);
209 buffer->unlock();
210 }
211 } else {
212 // we can't do anything
Mathias Agopian1473f462009-04-10 14:24:30 -0700213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215}
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217void Layer::onDraw(const Region& clip) const
218{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700219 Texture tex(mBufferManager.getActiveTexture());
220 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700222 // in fact never been drawn into. This happens frequently with
223 // SurfaceView because the WindowManager can't know when the client
224 // has drawn the first time.
225
226 // If there is nothing under us, we paint the screen in black, otherwise
227 // we just skip this update.
228
229 // figure out if there is something below us
230 Region under;
231 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
232 const size_t count = drawingLayers.size();
233 for (size_t i=0 ; i<count ; ++i) {
234 const sp<LayerBase>& layer(drawingLayers[i]);
235 if (layer.get() == static_cast<LayerBase const*>(this))
236 break;
237 under.orSelf(layer->visibleRegionScreen);
238 }
239 // if not everything below us is covered, we plug the holes!
240 Region holes(clip.subtract(under));
241 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700242 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 return;
245 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700246 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247}
248
Mathias Agopian92377032010-05-26 22:08:52 -0700249bool Layer::needsFiltering() const
250{
251 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
252 // NOTE: there is a race here, because mFixedSize is updated in a
253 // binder transaction. however, it doesn't really matter since it is
254 // evaluated each time we draw. To be perfectly correct, this flag
255 // would have to be associated with a buffer.
256 if (mFixedSize)
257 return true;
258 }
259 return LayerBase::needsFiltering();
260}
261
Mathias Agopian59751db2010-05-07 15:58:44 -0700262
263status_t Layer::setBufferCount(int bufferCount)
264{
Mathias Agopian7623da42010-06-01 15:12:58 -0700265 ClientRef::Access sharedClient(mUserClientRef);
266 SharedBufferServer* lcblk(sharedClient.get());
267 if (!lcblk) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700268 // oops, the client is already gone
269 return DEAD_OBJECT;
270 }
271
Mathias Agopian898c4c92010-05-18 17:06:55 -0700272 // NOTE: lcblk->resize() is protected by an internal lock
273 status_t err = lcblk->resize(bufferCount);
274 if (err == NO_ERROR)
275 mBufferManager.resize(bufferCount);
Mathias Agopian59751db2010-05-07 15:58:44 -0700276
277 return err;
278}
279
Mathias Agopian2be352a2010-05-21 17:24:35 -0700280sp<GraphicBuffer> Layer::requestBuffer(int index,
281 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
282 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283{
Mathias Agopian6950e422009-10-05 17:07:12 -0700284 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700285
Mathias Agopian7623da42010-06-01 15:12:58 -0700286 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopian2be352a2010-05-21 17:24:35 -0700287 return buffer;
288
289 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
290 return buffer;
291
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700292 // this ensures our client doesn't go away while we're accessing
293 // the shared area.
Mathias Agopian7623da42010-06-01 15:12:58 -0700294 ClientRef::Access sharedClient(mUserClientRef);
295 SharedBufferServer* lcblk(sharedClient.get());
296 if (!lcblk) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700297 // oops, the client is already gone
298 return buffer;
299 }
300
Mathias Agopian1473f462009-04-10 14:24:30 -0700301 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700302 * This is called from the client's Surface::dequeue(). This can happen
303 * at any time, especially while we're in the middle of using the
304 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700305 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
Mathias Agopian51c70e32010-07-27 20:11:35 -0700307 status_t err = NO_ERROR;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700308 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700309 { // scope for the lock
310 Mutex::Autolock _l(mLock);
Mathias Agopianc51114f2010-08-25 14:59:15 -0700311
312 // zero means default
313 if (!reqFormat) reqFormat = mFormat;
314 if (!reqWidth) reqWidth = mWidth;
315 if (!reqHeight) reqHeight = mHeight;
316
317 w = reqWidth;
318 h = reqHeight;
319 f = reqFormat;
320
321 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
322 (reqFormat != mReqFormat)) {
323 mReqWidth = reqWidth;
324 mReqHeight = reqHeight;
325 mReqFormat = reqFormat;
326
Mathias Agopian2be352a2010-05-21 17:24:35 -0700327 lcblk->reallocateAllExcept(index);
328 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700329 }
330
Mathias Agopian51c70e32010-07-27 20:11:35 -0700331 // here we have to reallocate a new buffer because the buffer could be
332 // used as the front buffer, or by a client in our process
333 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian6950e422009-10-05 17:07:12 -0700334 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian51c70e32010-07-27 20:11:35 -0700335 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
336 err = buffer->initCheck();
Mathias Agopian9779b222009-09-07 16:32:45 -0700337
338 if (err || buffer->handle == 0) {
339 LOGE_IF(err || buffer->handle == 0,
340 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
341 this, index, w, h, strerror(-err));
342 } else {
343 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700344 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
345 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700346 }
347
348 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700349 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700350 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700351 }
352 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353}
354
Mathias Agopian6950e422009-10-05 17:07:12 -0700355uint32_t Layer::getEffectiveUsage(uint32_t usage) const
356{
357 /*
358 * buffers used for software rendering, but h/w composition
359 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
360 *
361 * buffers used for h/w rendering and h/w composition
362 * are allocated with HW_RENDER | HW_TEXTURE
363 *
364 * buffers used with h/w rendering and either NPOT or no egl_image_ext
365 * are allocated with SW_READ_RARELY | HW_RENDER
366 *
367 */
368
369 if (mSecure) {
370 // secure buffer, don't store it into the GPU
371 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
372 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
373 } else {
374 // it's allowed to modify the usage flags here, but generally
375 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700376 // request EGLImage for all buffers
377 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700378 }
379 return usage;
380}
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382uint32_t Layer::doTransaction(uint32_t flags)
383{
384 const Layer::State& front(drawingState());
385 const Layer::State& temp(currentState());
386
Mathias Agopian2be352a2010-05-21 17:24:35 -0700387 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
388 (front.requested_h != temp.requested_h);
389
390 if (sizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700391 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700393 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
394 this,
395 int(temp.requested_w), int(temp.requested_h),
396 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397
Mathias Agopian2be352a2010-05-21 17:24:35 -0700398 if (!isFixedSize()) {
399 // we're being resized and there is a freeze display request,
400 // acquire a freeze lock, so that the screen stays put
401 // until we've redrawn at the new size; this is to avoid
402 // glitches upon orientation changes.
403 if (mFlinger->hasFreezeRequest()) {
404 // if the surface is hidden, don't try to acquire the
405 // freeze lock, since hidden surfaces may never redraw
406 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
407 mFreezeLock = mFlinger->getFreezeLock();
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700410
411 // this will make sure LayerBase::doTransaction doesn't update
412 // the drawing state's size
413 Layer::State& editDraw(mDrawingState);
414 editDraw.requested_w = temp.requested_w;
415 editDraw.requested_h = temp.requested_h;
416
417 // record the new size, form this point on, when the client request
418 // a buffer, it'll get the new size.
419 setBufferSize(temp.requested_w, temp.requested_h);
420
Mathias Agopian7623da42010-06-01 15:12:58 -0700421 ClientRef::Access sharedClient(mUserClientRef);
422 SharedBufferServer* lcblk(sharedClient.get());
423 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700424 // all buffers need reallocation
425 lcblk->reallocateAll();
426 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700427 } else {
428 // record the new size
429 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
431 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 if (temp.sequence != front.sequence) {
434 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
435 // this surface is now hidden, so it shouldn't hold a freeze lock
436 // (it may never redraw, which is fine if it is hidden)
437 mFreezeLock.clear();
438 }
439 }
440
441 return LayerBase::doTransaction(flags);
442}
443
Mathias Agopian2be352a2010-05-21 17:24:35 -0700444void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700445 Mutex::Autolock _l(mLock);
446 mWidth = w;
447 mHeight = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448}
449
Mathias Agopian2be352a2010-05-21 17:24:35 -0700450bool Layer::isFixedSize() const {
451 Mutex::Autolock _l(mLock);
452 return mFixedSize;
453}
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455// ----------------------------------------------------------------------------
456// pageflip handling...
457// ----------------------------------------------------------------------------
458
459void Layer::lockPageFlip(bool& recomputeVisibleRegions)
460{
Mathias Agopian7623da42010-06-01 15:12:58 -0700461 ClientRef::Access sharedClient(mUserClientRef);
462 SharedBufferServer* lcblk(sharedClient.get());
463 if (!lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700464 // client died
465 recomputeVisibleRegions = true;
466 return;
467 }
468
Mathias Agopian9779b222009-09-07 16:32:45 -0700469 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700470 if (buf == NOT_ENOUGH_DATA) {
471 // NOTE: This is not an error, it simply means there is nothing to
472 // retire. The buffer is locked because we will use it
Mathias Agopian9779b222009-09-07 16:32:45 -0700473 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 return;
475 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700476
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700477 if (buf < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700478 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700479 mPostedDirtyRegion.clear();
480 return;
481 }
482
Mathias Agopian9779b222009-09-07 16:32:45 -0700483 // we retired a buffer, which becomes the new front buffer
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700484 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700485 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700486 mPostedDirtyRegion.clear();
487 return;
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489
Mathias Agopian6950e422009-10-05 17:07:12 -0700490 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700491 if (newFrontBuffer != NULL) {
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700492 // get the dirty region
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700493 // compute the posted region
494 const Region dirty(lcblk->getDirtyRegion(buf));
495 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700496
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700497 // update the layer size and release freeze-lock
498 const Layer::State& front(drawingState());
499 if (newFrontBuffer->getWidth() == front.requested_w &&
500 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700501 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700502 if ((front.w != front.requested_w) ||
503 (front.h != front.requested_h))
504 {
505 // Here we pretend the transaction happened by updating the
506 // current and drawing states. Drawing state is only accessed
507 // in this thread, no need to have it locked
508 Layer::State& editDraw(mDrawingState);
509 editDraw.w = editDraw.requested_w;
510 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700511
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700512 // We also need to update the current state so that we don't
513 // end-up doing too much work during the next transaction.
514 // NOTE: We actually don't need hold the transaction lock here
515 // because State::w and State::h are only accessed from
516 // this thread
517 Layer::State& editTemp(currentState());
518 editTemp.w = editDraw.w;
519 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700520
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700521 // recompute visible region
522 recomputeVisibleRegions = true;
523 }
524
525 // we now have the correct size, unfreeze the screen
526 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700527 }
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700528
529 // get the crop region
530 setBufferCrop( lcblk->getCrop(buf) );
531
532 // get the transformation
533 setBufferTransform( lcblk->getTransform(buf) );
534
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700535 } else {
536 // this should not happen unless we ran out of memory while
537 // allocating the buffer. we're hoping that things will get back
538 // to normal the next time the app tries to draw into this buffer.
539 // meanwhile, pretend the screen didn't update.
540 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700541 }
542
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700543 if (lcblk->getQueuedCount()) {
544 // signal an event if we have more buffers waiting
545 mFlinger->signalEvent();
546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700548 /* a buffer was posted, so we need to call reloadTexture(), which
549 * will update our internal data structures (eg: EGLImageKHR or
550 * texture names). we need to do this even if mPostedDirtyRegion is
551 * empty -- it's orthogonal to the fact that a new buffer was posted,
552 * for instance, a degenerate case could be that the user did an empty
553 * update but repainted the buffer with appropriate content (after a
554 * resize for instance).
555 */
556 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557}
558
559void Layer::unlockPageFlip(
560 const Transform& planeTransform, Region& outDirtyRegion)
561{
562 Region dirtyRegion(mPostedDirtyRegion);
563 if (!dirtyRegion.isEmpty()) {
564 mPostedDirtyRegion.clear();
565 // The dirty region is given in the layer's coordinate space
566 // transform the dirty region by the surface's transformation
567 // and the global transformation.
568 const Layer::State& s(drawingState());
569 const Transform tr(planeTransform * s.transform);
570 dirtyRegion = tr.transform(dirtyRegion);
571
572 // At this point, the dirty region is in screen space.
573 // Make sure it's constrained by the visible region (which
574 // is in screen space as well).
575 dirtyRegion.andSelf(visibleRegionScreen);
576 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800578 if (visibleRegionScreen.isEmpty()) {
579 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700580 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800581 mFreezeLock.clear();
582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583}
584
585void Layer::finishPageFlip()
586{
Mathias Agopian7623da42010-06-01 15:12:58 -0700587 ClientRef::Access sharedClient(mUserClientRef);
588 SharedBufferServer* lcblk(sharedClient.get());
589 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700590 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopian7623da42010-06-01 15:12:58 -0700591 if (buf >= 0) {
592 status_t err = lcblk->unlock( buf );
593 LOGE_IF(err!=NO_ERROR,
594 "layer %p, buffer=%d wasn't locked!",
595 this, buf);
596 }
Mathias Agopian593c05c2010-06-02 23:28:45 -0700597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598}
599
Mathias Agopian9bce8732010-04-20 17:55:49 -0700600
601void Layer::dump(String8& result, char* buffer, size_t SIZE) const
602{
603 LayerBaseClient::dump(result, buffer, SIZE);
604
Mathias Agopian7623da42010-06-01 15:12:58 -0700605 ClientRef::Access sharedClient(mUserClientRef);
606 SharedBufferServer* lcblk(sharedClient.get());
607 uint32_t totalTime = 0;
608 if (lcblk) {
609 SharedBufferStack::Statistics stats = lcblk->getStats();
610 totalTime= stats.totalTime;
611 result.append( lcblk->dump(" ") );
612 }
613
Mathias Agopian9bce8732010-04-20 17:55:49 -0700614 sp<const GraphicBuffer> buf0(getBuffer(0));
615 sp<const GraphicBuffer> buf1(getBuffer(1));
616 uint32_t w0=0, h0=0, s0=0;
617 uint32_t w1=0, h1=0, s1=0;
618 if (buf0 != 0) {
619 w0 = buf0->getWidth();
620 h0 = buf0->getHeight();
621 s0 = buf0->getStride();
622 }
623 if (buf1 != 0) {
624 w1 = buf1->getWidth();
625 h1 = buf1->getHeight();
626 s1 = buf1->getStride();
627 }
628 snprintf(buffer, SIZE,
629 " "
630 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
631 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopian7623da42010-06-01 15:12:58 -0700632 mFormat, w0, h0, s0, w1, h1, s1,
633 getFreezeLock().get(), totalTime);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700634
635 result.append(buffer);
636}
637
Mathias Agopian1473f462009-04-10 14:24:30 -0700638// ---------------------------------------------------------------------------
639
Mathias Agopian7623da42010-06-01 15:12:58 -0700640Layer::ClientRef::ClientRef()
Mathias Agopian5e140102010-06-08 19:54:15 -0700641 : mControlBlock(0), mToken(-1) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700642}
643
644Layer::ClientRef::~ClientRef() {
Mathias Agopian7623da42010-06-01 15:12:58 -0700645}
646
647int32_t Layer::ClientRef::getToken() const {
648 Mutex::Autolock _l(mLock);
649 return mToken;
650}
651
Mathias Agopian5e140102010-06-08 19:54:15 -0700652sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700653 Mutex::Autolock _l(mLock);
Mathias Agopian5e140102010-06-08 19:54:15 -0700654 return mUserClient.promote();
655}
656
657status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
658 const sp<SharedBufferServer>& sharedClient, int32_t token) {
659 Mutex::Autolock _l(mLock);
660
661 { // scope for strong mUserClient reference
662 sp<UserClient> userClient(mUserClient.promote());
663 if (mUserClient != 0 && mControlBlock != 0) {
664 mControlBlock->setStatus(NO_INIT);
665 }
666 }
667
Mathias Agopian7623da42010-06-01 15:12:58 -0700668 mUserClient = uc;
669 mToken = token;
Mathias Agopian5e140102010-06-08 19:54:15 -0700670 mControlBlock = sharedClient;
Mathias Agopian7623da42010-06-01 15:12:58 -0700671 return NO_ERROR;
672}
673
674sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
675 return mUserClient.promote();
676}
677
678// this class gives us access to SharedBufferServer safely
679// it makes sure the UserClient (and its associated shared memory)
680// won't go away while we're accessing it.
681Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian5e140102010-06-08 19:54:15 -0700682 : mControlBlock(0)
Mathias Agopian7623da42010-06-01 15:12:58 -0700683{
684 Mutex::Autolock _l(ref.mLock);
685 mUserClientStrongRef = ref.mUserClient.promote();
686 if (mUserClientStrongRef != 0)
Mathias Agopian5e140102010-06-08 19:54:15 -0700687 mControlBlock = ref.mControlBlock;
688}
689
690Layer::ClientRef::Access::~Access()
691{
Mathias Agopian7623da42010-06-01 15:12:58 -0700692}
693
694// ---------------------------------------------------------------------------
695
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700696Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700697 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian7623da42010-06-01 15:12:58 -0700698 mActiveBuffer(-1), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700699{
700}
701
Mathias Agopian898c4c92010-05-18 17:06:55 -0700702Layer::BufferManager::~BufferManager()
703{
704}
705
706status_t Layer::BufferManager::resize(size_t size)
707{
708 Mutex::Autolock _l(mLock);
709 mNumBuffers = size;
710 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700711}
712
713// only for debugging
714sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
715 return mBufferData[index].buffer;
716}
717
718status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700719 mActiveBuffer = index;
720 return NO_ERROR;
721}
722
723size_t Layer::BufferManager::getActiveBufferIndex() const {
724 return mActiveBuffer;
725}
726
727Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700728 Texture res;
Mathias Agopian7623da42010-06-01 15:12:58 -0700729 if (mFailover || mActiveBuffer<0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700730 res = mFailoverTexture;
731 } else {
732 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
733 }
734 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700735}
736
737sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700738 sp<GraphicBuffer> result;
739 const ssize_t activeBuffer = mActiveBuffer;
740 if (activeBuffer >= 0) {
741 BufferData const * const buffers = mBufferData;
742 Mutex::Autolock _l(mLock);
743 result = buffers[activeBuffer].buffer;
744 }
745 return result;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700746}
747
748sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
749{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700750 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700751 sp<GraphicBuffer> buffer;
752 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700753 buffer = buffers[index].buffer;
754 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700755 return buffer;
756}
757
758status_t Layer::BufferManager::attachBuffer(size_t index,
759 const sp<GraphicBuffer>& buffer)
760{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700761 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700762 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700763 buffers[index].buffer = buffer;
764 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700765 return NO_ERROR;
766}
767
768status_t Layer::BufferManager::destroy(EGLDisplay dpy)
769{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700770 BufferData* const buffers = mBufferData;
771 size_t num;
772 { // scope for the lock
773 Mutex::Autolock _l(mLock);
774 num = mNumBuffers;
775 for (size_t i=0 ; i<num ; i++) {
776 buffers[i].buffer = 0;
777 }
778 }
779 for (size_t i=0 ; i<num ; i++) {
780 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700781 }
782 destroyTexture(&mFailoverTexture, dpy);
783 return NO_ERROR;
784}
785
786status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
787 const sp<GraphicBuffer>& buffer)
788{
Mathias Agopian7623da42010-06-01 15:12:58 -0700789 status_t err = NO_INIT;
790 ssize_t index = mActiveBuffer;
791 if (index >= 0) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700792 if (!mFailover) {
793 Image& texture(mBufferData[index].texture);
794 err = mTextureManager.initEglImage(&texture, dpy, buffer);
795 // if EGLImage fails, we switch to regular texture mode, and we
796 // free all resources associated with using EGLImages.
797 if (err == NO_ERROR) {
798 mFailover = false;
799 destroyTexture(&mFailoverTexture, dpy);
800 } else {
801 mFailover = true;
802 const size_t num = mNumBuffers;
803 for (size_t i=0 ; i<num ; i++) {
804 destroyTexture(&mBufferData[i].texture, dpy);
805 }
Andreas Huber330dd302010-06-25 09:25:19 -0700806 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700807 } else {
808 // we failed once, don't try again
809 err = BAD_VALUE;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700810 }
811 }
812 return err;
813}
814
815status_t Layer::BufferManager::loadTexture(
816 const Region& dirty, const GGLSurface& t)
817{
818 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
819}
820
Mathias Agopian898c4c92010-05-18 17:06:55 -0700821status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
822{
823 if (tex->name != -1U) {
824 glDeleteTextures(1, &tex->name);
825 tex->name = -1U;
826 }
827 if (tex->image != EGL_NO_IMAGE_KHR) {
828 eglDestroyImageKHR(dpy, tex->image);
829 tex->image = EGL_NO_IMAGE_KHR;
830 }
831 return NO_ERROR;
832}
833
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700834// ---------------------------------------------------------------------------
835
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700836Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian593c05c2010-06-02 23:28:45 -0700837 const sp<Layer>& owner)
838 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700839{
840}
841
842Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -0700843{
844}
845
Mathias Agopian2be352a2010-05-21 17:24:35 -0700846sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
847 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700848{
Mathias Agopian6950e422009-10-05 17:07:12 -0700849 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700850 sp<Layer> owner(getOwner());
851 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700852 /*
853 * requestBuffer() cannot be called from the main thread
854 * as it could cause a dead-lock, since it may have to wait
855 * on conditions updated my the main thread.
856 */
Mathias Agopian2be352a2010-05-21 17:24:35 -0700857 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700858 }
859 return buffer;
860}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861
Mathias Agopian59751db2010-05-07 15:58:44 -0700862status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
863{
864 status_t err = DEAD_OBJECT;
865 sp<Layer> owner(getOwner());
866 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700867 /*
868 * setBufferCount() cannot be called from the main thread
869 * as it could cause a dead-lock, since it may have to wait
870 * on conditions updated my the main thread.
871 */
Mathias Agopian59751db2010-05-07 15:58:44 -0700872 err = owner->setBufferCount(bufferCount);
873 }
874 return err;
875}
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877// ---------------------------------------------------------------------------
878
879
880}; // namespace android