blob: 7be58c6541ac5f6f7965a86fc1b192f1c906b803 [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"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070038#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40
41#define DEBUG_RESIZE 0
42
43
44namespace android {
45
Mathias Agopian967dce32010-04-14 16:43:44 -070046template <typename T> inline T min(T a, T b) {
47 return a<b ? a : b;
48}
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050// ---------------------------------------------------------------------------
51
Mathias Agopian593c05c2010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopian781953d2010-06-25 18:02:21 -070055 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiancc934762009-09-23 19:16:27 -070056 mNeedsBlending(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070057 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070058 mSecure(false),
Mathias Agopian781953d2010-06-25 18:02:21 -070059 mTextureManager(),
Mathias Agopian2be352a2010-05-21 17:24:35 -070060 mBufferManager(mTextureManager),
61 mWidth(0), mHeight(0), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063}
64
65Layer::~Layer()
66{
Mathias Agopian898c4c92010-05-18 17:06:55 -070067 // FIXME: must be called from the main UI thread
68 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
69 mBufferManager.destroy(dpy);
70
Mathias Agopian7623da42010-06-01 15:12:58 -070071 // we can use getUserClientUnsafe here because we know we're
72 // single-threaded at that point.
73 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
74 if (ourClient != 0) {
75 ourClient->detachLayer(this);
76 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070077}
78
Mathias Agopian7623da42010-06-01 15:12:58 -070079status_t Layer::setToken(const sp<UserClient>& userClient,
80 SharedClient* sharedClient, int32_t token)
Mathias Agopian593c05c2010-06-02 23:28:45 -070081{
Mathias Agopian5e140102010-06-08 19:54:15 -070082 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopian7623da42010-06-01 15:12:58 -070083 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian593c05c2010-06-02 23:28:45 -070084 getIdentity());
85
Mathias Agopian7623da42010-06-01 15:12:58 -070086 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian5e140102010-06-08 19:54:15 -070087
88 LOGE_IF(err != NO_ERROR,
89 "ClientRef::setToken(%p, %p, %u) failed",
90 userClient.get(), lcblk.get(), token);
91
92 if (err == NO_ERROR) {
93 // we need to free the buffers associated with this surface
Mathias Agopian7623da42010-06-01 15:12:58 -070094 }
95
96 return err;
97}
98
99int32_t Layer::getToken() const
100{
101 return mUserClientRef.getToken();
Mathias Agopian593c05c2010-06-02 23:28:45 -0700102}
103
Mathias Agopian5e140102010-06-08 19:54:15 -0700104sp<UserClient> Layer::getClient() const
105{
106 return mUserClientRef.getClient();
107}
108
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700109// called with SurfaceFlinger::mStateLock as soon as the layer is entered
110// in the purgatory list
111void Layer::onRemoved()
112{
Mathias Agopian7623da42010-06-01 15:12:58 -0700113 ClientRef::Access sharedClient(mUserClientRef);
114 SharedBufferServer* lcblk(sharedClient.get());
115 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700116 // wake up the condition
117 lcblk->setStatus(NO_INIT);
118 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700119}
Mathias Agopian9779b2212009-09-07 16:32:45 -0700120
Mathias Agopian1473f462009-04-10 14:24:30 -0700121sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122{
123 return mSurface;
124}
125
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700126status_t Layer::ditch()
127{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700128 // NOTE: Called from the main UI thread
129
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700130 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -0800131 mFreezeLock.clear();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700132
133 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
134 mBufferManager.destroy(dpy);
135 mSurface.clear();
136
137 Mutex::Autolock _l(mLock);
138 mWidth = mHeight = 0;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700139 return NO_ERROR;
140}
141
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700142status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 PixelFormat format, uint32_t flags)
144{
Mathias Agopiancc934762009-09-23 19:16:27 -0700145 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 PixelFormatInfo info;
147 status_t err = getPixelFormatInfo(format, &info);
148 if (err) return err;
149
Mathias Agopiancc934762009-09-23 19:16:27 -0700150 // the display's pixel format
151 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-04-14 16:43:44 -0700152 uint32_t const maxSurfaceDims = min(
153 hw.getMaxTextureSize(), hw.getMaxViewportDims());
154
155 // never allow a surface larger than what our underlying GL implementation
156 // can handle.
157 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
158 return BAD_VALUE;
159 }
160
Mathias Agopiancc934762009-09-23 19:16:27 -0700161 PixelFormatInfo displayInfo;
162 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700163 const uint32_t hwFlags = hw.getFlags();
164
Mathias Agopian9779b2212009-09-07 16:32:45 -0700165 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700166 mWidth = w;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700167 mHeight = h;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700168
169 mReqFormat = format;
170 mReqWidth = w;
171 mReqHeight = h;
172
Mathias Agopian6950e422009-10-05 17:07:12 -0700173 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Romain Guyd10cd572010-10-10 13:33:22 -0700174 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
175 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700176
Mathias Agopiancc934762009-09-23 19:16:27 -0700177 // we use the red index
178 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
179 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
180 mNeedsDithering = layerRedsize > displayRedSize;
181
Mathias Agopian593c05c2010-06-02 23:28:45 -0700182 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 return NO_ERROR;
184}
185
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700186void Layer::setGeometry(hwc_layer_t* hwcl)
187{
188 hwcl->compositionType = HWC_FRAMEBUFFER;
189 hwcl->hints = 0;
190 hwcl->flags = 0;
191 hwcl->transform = 0;
192 hwcl->blending = HWC_BLENDING_NONE;
193
194 // we can't do alpha-fade with the hwc HAL
195 const State& s(drawingState());
196 if (s.alpha < 0xFF) {
197 hwcl->flags = HWC_SKIP_LAYER;
198 return;
199 }
200
201 // we can only handle simple transformation
202 if (mOrientation & Transform::ROT_INVALID) {
203 hwcl->flags = HWC_SKIP_LAYER;
204 return;
205 }
206
207 hwcl->transform = mOrientation;
208
209 if (needsBlending()) {
210 hwcl->blending = mPremultipliedAlpha ?
211 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
212 }
213
214 hwcl->displayFrame.left = mTransformedBounds.left;
215 hwcl->displayFrame.top = mTransformedBounds.top;
216 hwcl->displayFrame.right = mTransformedBounds.right;
217 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
218
219 hwcl->visibleRegionScreen.rects =
220 reinterpret_cast<hwc_rect_t const *>(
221 visibleRegionScreen.getArray(
222 &hwcl->visibleRegionScreen.numRects));
223}
224
225void Layer::setPerFrameData(hwc_layer_t* hwcl) {
226 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
227 if (buffer == NULL) {
228 // this situation can happen if we ran out of memory for instance.
229 // not much we can do. continue to use whatever texture was bound
230 // to this context.
231 hwcl->handle = NULL;
232 return;
233 }
234 hwcl->handle = const_cast<native_handle_t*>(buffer->handle);
235 // TODO: set the crop value properly
236 hwcl->sourceCrop.left = 0;
237 hwcl->sourceCrop.top = 0;
238 hwcl->sourceCrop.right = buffer->width;
239 hwcl->sourceCrop.bottom = buffer->height;
240}
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242void Layer::reloadTexture(const Region& dirty)
243{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700244 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-12-10 15:52:29 -0800245 if (buffer == NULL) {
246 // this situation can happen if we ran out of memory for instance.
247 // not much we can do. continue to use whatever texture was bound
248 // to this context.
249 return;
250 }
251
Mathias Agopian781953d2010-06-25 18:02:21 -0700252 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700253 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
254 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
255 // not sure what we can do here...
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700256 goto slowpath;
Mathias Agopian1473f462009-04-10 14:24:30 -0700257 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700258 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800259slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700260 GGLSurface t;
Mathias Agopianc817b222010-08-20 15:59:53 -0700261 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
262 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
263 LOGE_IF(res, "error %d (%s) locking buffer %p",
264 res, strerror(res), buffer.get());
265 if (res == NO_ERROR) {
266 mBufferManager.loadTexture(dirty, t);
267 buffer->unlock();
268 }
269 } else {
270 // we can't do anything
Mathias Agopian1473f462009-04-10 14:24:30 -0700271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273}
274
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700275void Layer::drawForSreenShot() const
276{
277 bool currentFixedSize = mFixedSize;
278 bool currentBlending = mNeedsBlending;
279 const_cast<Layer*>(this)->mFixedSize = false;
280 const_cast<Layer*>(this)->mFixedSize = true;
281 LayerBase::drawForSreenShot();
282 const_cast<Layer*>(this)->mFixedSize = currentFixedSize;
283 const_cast<Layer*>(this)->mNeedsBlending = currentBlending;
284}
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286void Layer::onDraw(const Region& clip) const
287{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700288 Texture tex(mBufferManager.getActiveTexture());
289 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700291 // in fact never been drawn into. This happens frequently with
292 // SurfaceView because the WindowManager can't know when the client
293 // has drawn the first time.
294
295 // If there is nothing under us, we paint the screen in black, otherwise
296 // we just skip this update.
297
298 // figure out if there is something below us
299 Region under;
300 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
301 const size_t count = drawingLayers.size();
302 for (size_t i=0 ; i<count ; ++i) {
303 const sp<LayerBase>& layer(drawingLayers[i]);
304 if (layer.get() == static_cast<LayerBase const*>(this))
305 break;
306 under.orSelf(layer->visibleRegionScreen);
307 }
308 // if not everything below us is covered, we plug the holes!
309 Region holes(clip.subtract(under));
310 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700311 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 return;
314 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700315 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316}
317
Mathias Agopian92377032010-05-26 22:08:52 -0700318bool Layer::needsFiltering() const
319{
320 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
321 // NOTE: there is a race here, because mFixedSize is updated in a
322 // binder transaction. however, it doesn't really matter since it is
323 // evaluated each time we draw. To be perfectly correct, this flag
324 // would have to be associated with a buffer.
325 if (mFixedSize)
326 return true;
327 }
328 return LayerBase::needsFiltering();
329}
330
Mathias Agopian59751db2010-05-07 15:58:44 -0700331
332status_t Layer::setBufferCount(int bufferCount)
333{
Mathias Agopian7623da42010-06-01 15:12:58 -0700334 ClientRef::Access sharedClient(mUserClientRef);
335 SharedBufferServer* lcblk(sharedClient.get());
336 if (!lcblk) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700337 // oops, the client is already gone
338 return DEAD_OBJECT;
339 }
340
Mathias Agopian898c4c92010-05-18 17:06:55 -0700341 // NOTE: lcblk->resize() is protected by an internal lock
342 status_t err = lcblk->resize(bufferCount);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700343 if (err == NO_ERROR) {
344 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
345 mBufferManager.resize(bufferCount, mFlinger, dpy);
346 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700347
348 return err;
349}
350
Mathias Agopian2be352a2010-05-21 17:24:35 -0700351sp<GraphicBuffer> Layer::requestBuffer(int index,
352 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
353 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354{
Mathias Agopian6950e422009-10-05 17:07:12 -0700355 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700356
Mathias Agopian7623da42010-06-01 15:12:58 -0700357 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopian2be352a2010-05-21 17:24:35 -0700358 return buffer;
359
360 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
361 return buffer;
362
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700363 // this ensures our client doesn't go away while we're accessing
364 // the shared area.
Mathias Agopian7623da42010-06-01 15:12:58 -0700365 ClientRef::Access sharedClient(mUserClientRef);
366 SharedBufferServer* lcblk(sharedClient.get());
367 if (!lcblk) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700368 // oops, the client is already gone
369 return buffer;
370 }
371
Mathias Agopian1473f462009-04-10 14:24:30 -0700372 /*
Mathias Agopian9779b2212009-09-07 16:32:45 -0700373 * This is called from the client's Surface::dequeue(). This can happen
374 * at any time, especially while we're in the middle of using the
375 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700376 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Mathias Agopian51c70e32010-07-27 20:11:35 -0700378 status_t err = NO_ERROR;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700379 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700380 { // scope for the lock
381 Mutex::Autolock _l(mLock);
Mathias Agopianc51114f2010-08-25 14:59:15 -0700382
383 // zero means default
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700384 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700385 if (!reqFormat) reqFormat = mFormat;
386 if (!reqWidth) reqWidth = mWidth;
387 if (!reqHeight) reqHeight = mHeight;
388
389 w = reqWidth;
390 h = reqHeight;
391 f = reqFormat;
392
393 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
394 (reqFormat != mReqFormat)) {
395 mReqWidth = reqWidth;
396 mReqHeight = reqHeight;
397 mReqFormat = reqFormat;
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700398 mFixedSize = fixedSize;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700399
Mathias Agopian2be352a2010-05-21 17:24:35 -0700400 lcblk->reallocateAllExcept(index);
401 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700402 }
403
Mathias Agopian51c70e32010-07-27 20:11:35 -0700404 // here we have to reallocate a new buffer because the buffer could be
405 // used as the front buffer, or by a client in our process
406 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian6950e422009-10-05 17:07:12 -0700407 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian51c70e32010-07-27 20:11:35 -0700408 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
409 err = buffer->initCheck();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700410
411 if (err || buffer->handle == 0) {
412 LOGE_IF(err || buffer->handle == 0,
413 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
414 this, index, w, h, strerror(-err));
415 } else {
416 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700417 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
418 this, index, w, h, buffer->handle);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700419 }
420
421 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700422 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700423 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700424 }
425 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426}
427
Mathias Agopian6950e422009-10-05 17:07:12 -0700428uint32_t Layer::getEffectiveUsage(uint32_t usage) const
429{
430 /*
431 * buffers used for software rendering, but h/w composition
432 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
433 *
434 * buffers used for h/w rendering and h/w composition
435 * are allocated with HW_RENDER | HW_TEXTURE
436 *
437 * buffers used with h/w rendering and either NPOT or no egl_image_ext
438 * are allocated with SW_READ_RARELY | HW_RENDER
439 *
440 */
441
442 if (mSecure) {
443 // secure buffer, don't store it into the GPU
444 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
445 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
446 } else {
447 // it's allowed to modify the usage flags here, but generally
448 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700449 // request EGLImage for all buffers
450 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700451 }
452 return usage;
453}
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455uint32_t Layer::doTransaction(uint32_t flags)
456{
457 const Layer::State& front(drawingState());
458 const Layer::State& temp(currentState());
459
Mathias Agopian2be352a2010-05-21 17:24:35 -0700460 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
461 (front.requested_h != temp.requested_h);
462
463 if (sizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700464 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700466 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
467 this,
468 int(temp.requested_w), int(temp.requested_h),
469 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
Mathias Agopian2be352a2010-05-21 17:24:35 -0700471 if (!isFixedSize()) {
472 // we're being resized and there is a freeze display request,
473 // acquire a freeze lock, so that the screen stays put
474 // until we've redrawn at the new size; this is to avoid
475 // glitches upon orientation changes.
476 if (mFlinger->hasFreezeRequest()) {
477 // if the surface is hidden, don't try to acquire the
478 // freeze lock, since hidden surfaces may never redraw
479 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
480 mFreezeLock = mFlinger->getFreezeLock();
481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700483
484 // this will make sure LayerBase::doTransaction doesn't update
485 // the drawing state's size
486 Layer::State& editDraw(mDrawingState);
487 editDraw.requested_w = temp.requested_w;
488 editDraw.requested_h = temp.requested_h;
489
490 // record the new size, form this point on, when the client request
491 // a buffer, it'll get the new size.
492 setBufferSize(temp.requested_w, temp.requested_h);
493
Mathias Agopian7623da42010-06-01 15:12:58 -0700494 ClientRef::Access sharedClient(mUserClientRef);
495 SharedBufferServer* lcblk(sharedClient.get());
496 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700497 // all buffers need reallocation
498 lcblk->reallocateAll();
499 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700500 } else {
501 // record the new size
502 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 if (temp.sequence != front.sequence) {
507 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
508 // this surface is now hidden, so it shouldn't hold a freeze lock
509 // (it may never redraw, which is fine if it is hidden)
510 mFreezeLock.clear();
511 }
512 }
513
514 return LayerBase::doTransaction(flags);
515}
516
Mathias Agopian2be352a2010-05-21 17:24:35 -0700517void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700518 Mutex::Autolock _l(mLock);
519 mWidth = w;
520 mHeight = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521}
522
Mathias Agopian2be352a2010-05-21 17:24:35 -0700523bool Layer::isFixedSize() const {
524 Mutex::Autolock _l(mLock);
525 return mFixedSize;
526}
527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528// ----------------------------------------------------------------------------
529// pageflip handling...
530// ----------------------------------------------------------------------------
531
532void Layer::lockPageFlip(bool& recomputeVisibleRegions)
533{
Mathias Agopian7623da42010-06-01 15:12:58 -0700534 ClientRef::Access sharedClient(mUserClientRef);
535 SharedBufferServer* lcblk(sharedClient.get());
536 if (!lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700537 // client died
538 recomputeVisibleRegions = true;
539 return;
540 }
541
Mathias Agopian9779b2212009-09-07 16:32:45 -0700542 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700543 if (buf == NOT_ENOUGH_DATA) {
544 // NOTE: This is not an error, it simply means there is nothing to
545 // retire. The buffer is locked because we will use it
Mathias Agopian9779b2212009-09-07 16:32:45 -0700546 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 return;
548 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700549
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700550 if (buf < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700551 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700552 mPostedDirtyRegion.clear();
553 return;
554 }
555
Mathias Agopian9779b2212009-09-07 16:32:45 -0700556 // we retired a buffer, which becomes the new front buffer
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700557 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700558 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700559 mPostedDirtyRegion.clear();
560 return;
561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
Mathias Agopian6950e422009-10-05 17:07:12 -0700563 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700564 if (newFrontBuffer != NULL) {
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700565 // get the dirty region
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700566 // compute the posted region
567 const Region dirty(lcblk->getDirtyRegion(buf));
568 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700569
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700570 // update the layer size and release freeze-lock
571 const Layer::State& front(drawingState());
572 if (newFrontBuffer->getWidth() == front.requested_w &&
573 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700574 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700575 if ((front.w != front.requested_w) ||
576 (front.h != front.requested_h))
577 {
578 // Here we pretend the transaction happened by updating the
579 // current and drawing states. Drawing state is only accessed
580 // in this thread, no need to have it locked
581 Layer::State& editDraw(mDrawingState);
582 editDraw.w = editDraw.requested_w;
583 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700584
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700585 // We also need to update the current state so that we don't
586 // end-up doing too much work during the next transaction.
587 // NOTE: We actually don't need hold the transaction lock here
588 // because State::w and State::h are only accessed from
589 // this thread
590 Layer::State& editTemp(currentState());
591 editTemp.w = editDraw.w;
592 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700593
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700594 // recompute visible region
595 recomputeVisibleRegions = true;
596 }
597
598 // we now have the correct size, unfreeze the screen
599 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700600 }
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700601
602 // get the crop region
603 setBufferCrop( lcblk->getCrop(buf) );
604
605 // get the transformation
606 setBufferTransform( lcblk->getTransform(buf) );
607
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700608 } else {
609 // this should not happen unless we ran out of memory while
610 // allocating the buffer. we're hoping that things will get back
611 // to normal the next time the app tries to draw into this buffer.
612 // meanwhile, pretend the screen didn't update.
613 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700614 }
615
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700616 if (lcblk->getQueuedCount()) {
617 // signal an event if we have more buffers waiting
618 mFlinger->signalEvent();
619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700621 /* a buffer was posted, so we need to call reloadTexture(), which
622 * will update our internal data structures (eg: EGLImageKHR or
623 * texture names). we need to do this even if mPostedDirtyRegion is
624 * empty -- it's orthogonal to the fact that a new buffer was posted,
625 * for instance, a degenerate case could be that the user did an empty
626 * update but repainted the buffer with appropriate content (after a
627 * resize for instance).
628 */
629 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630}
631
632void Layer::unlockPageFlip(
633 const Transform& planeTransform, Region& outDirtyRegion)
634{
635 Region dirtyRegion(mPostedDirtyRegion);
636 if (!dirtyRegion.isEmpty()) {
637 mPostedDirtyRegion.clear();
638 // The dirty region is given in the layer's coordinate space
639 // transform the dirty region by the surface's transformation
640 // and the global transformation.
641 const Layer::State& s(drawingState());
642 const Transform tr(planeTransform * s.transform);
643 dirtyRegion = tr.transform(dirtyRegion);
644
645 // At this point, the dirty region is in screen space.
646 // Make sure it's constrained by the visible region (which
647 // is in screen space as well).
648 dirtyRegion.andSelf(visibleRegionScreen);
649 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800651 if (visibleRegionScreen.isEmpty()) {
652 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700653 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800654 mFreezeLock.clear();
655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656}
657
658void Layer::finishPageFlip()
659{
Mathias Agopian7623da42010-06-01 15:12:58 -0700660 ClientRef::Access sharedClient(mUserClientRef);
661 SharedBufferServer* lcblk(sharedClient.get());
662 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700663 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopian7623da42010-06-01 15:12:58 -0700664 if (buf >= 0) {
665 status_t err = lcblk->unlock( buf );
666 LOGE_IF(err!=NO_ERROR,
667 "layer %p, buffer=%d wasn't locked!",
668 this, buf);
669 }
Mathias Agopian593c05c2010-06-02 23:28:45 -0700670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671}
672
Mathias Agopian9bce8732010-04-20 17:55:49 -0700673
674void Layer::dump(String8& result, char* buffer, size_t SIZE) const
675{
676 LayerBaseClient::dump(result, buffer, SIZE);
677
Mathias Agopian7623da42010-06-01 15:12:58 -0700678 ClientRef::Access sharedClient(mUserClientRef);
679 SharedBufferServer* lcblk(sharedClient.get());
680 uint32_t totalTime = 0;
681 if (lcblk) {
682 SharedBufferStack::Statistics stats = lcblk->getStats();
683 totalTime= stats.totalTime;
684 result.append( lcblk->dump(" ") );
685 }
686
Mathias Agopian9bce8732010-04-20 17:55:49 -0700687 sp<const GraphicBuffer> buf0(getBuffer(0));
688 sp<const GraphicBuffer> buf1(getBuffer(1));
689 uint32_t w0=0, h0=0, s0=0;
690 uint32_t w1=0, h1=0, s1=0;
691 if (buf0 != 0) {
692 w0 = buf0->getWidth();
693 h0 = buf0->getHeight();
694 s0 = buf0->getStride();
695 }
696 if (buf1 != 0) {
697 w1 = buf1->getWidth();
698 h1 = buf1->getHeight();
699 s1 = buf1->getStride();
700 }
701 snprintf(buffer, SIZE,
702 " "
703 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
704 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopian7623da42010-06-01 15:12:58 -0700705 mFormat, w0, h0, s0, w1, h1, s1,
706 getFreezeLock().get(), totalTime);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700707
708 result.append(buffer);
709}
710
Mathias Agopian1473f462009-04-10 14:24:30 -0700711// ---------------------------------------------------------------------------
712
Mathias Agopian7623da42010-06-01 15:12:58 -0700713Layer::ClientRef::ClientRef()
Mathias Agopian5e140102010-06-08 19:54:15 -0700714 : mControlBlock(0), mToken(-1) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700715}
716
717Layer::ClientRef::~ClientRef() {
Mathias Agopian7623da42010-06-01 15:12:58 -0700718}
719
720int32_t Layer::ClientRef::getToken() const {
721 Mutex::Autolock _l(mLock);
722 return mToken;
723}
724
Mathias Agopian5e140102010-06-08 19:54:15 -0700725sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700726 Mutex::Autolock _l(mLock);
Mathias Agopian5e140102010-06-08 19:54:15 -0700727 return mUserClient.promote();
728}
729
730status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
731 const sp<SharedBufferServer>& sharedClient, int32_t token) {
732 Mutex::Autolock _l(mLock);
733
734 { // scope for strong mUserClient reference
735 sp<UserClient> userClient(mUserClient.promote());
736 if (mUserClient != 0 && mControlBlock != 0) {
737 mControlBlock->setStatus(NO_INIT);
738 }
739 }
740
Mathias Agopian7623da42010-06-01 15:12:58 -0700741 mUserClient = uc;
742 mToken = token;
Mathias Agopian5e140102010-06-08 19:54:15 -0700743 mControlBlock = sharedClient;
Mathias Agopian7623da42010-06-01 15:12:58 -0700744 return NO_ERROR;
745}
746
747sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
748 return mUserClient.promote();
749}
750
751// this class gives us access to SharedBufferServer safely
752// it makes sure the UserClient (and its associated shared memory)
753// won't go away while we're accessing it.
754Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian5e140102010-06-08 19:54:15 -0700755 : mControlBlock(0)
Mathias Agopian7623da42010-06-01 15:12:58 -0700756{
757 Mutex::Autolock _l(ref.mLock);
758 mUserClientStrongRef = ref.mUserClient.promote();
759 if (mUserClientStrongRef != 0)
Mathias Agopian5e140102010-06-08 19:54:15 -0700760 mControlBlock = ref.mControlBlock;
761}
762
763Layer::ClientRef::Access::~Access()
764{
Mathias Agopian7623da42010-06-01 15:12:58 -0700765}
766
767// ---------------------------------------------------------------------------
768
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700769Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700770 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian7623da42010-06-01 15:12:58 -0700771 mActiveBuffer(-1), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700772{
773}
774
Mathias Agopian898c4c92010-05-18 17:06:55 -0700775Layer::BufferManager::~BufferManager()
776{
777}
778
Jamie Gennis6c925d02010-11-02 11:51:32 -0700779status_t Layer::BufferManager::resize(size_t size,
780 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700781{
782 Mutex::Autolock _l(mLock);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700783
784 if (size < mNumBuffers) {
785 // Move the active texture into slot 0
786 BufferData activeBufferData = mBufferData[mActiveBuffer];
787 mBufferData[mActiveBuffer] = mBufferData[0];
788 mBufferData[0] = activeBufferData;
789 mActiveBuffer = 0;
790
791 // Free the buffers that are no longer needed.
792 for (size_t i = size; i < mNumBuffers; i++) {
793 mBufferData[i].buffer = 0;
794
795 // Create a message to destroy the textures on SurfaceFlinger's GL
796 // thread.
797 class MessageDestroyTexture : public MessageBase {
798 Image mTexture;
799 EGLDisplay mDpy;
800 public:
801 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
802 : mTexture(texture), mDpy(dpy) { }
803 virtual bool handler() {
804 status_t err = Layer::BufferManager::destroyTexture(
805 &mTexture, mDpy);
806 LOGE_IF(err<0, "error destroying texture: %d (%s)",
807 mTexture.name, strerror(-err));
808 return true; // XXX: err == 0; ????
809 }
810 };
811
812 MessageDestroyTexture *msg = new MessageDestroyTexture(
813 mBufferData[i].texture, dpy);
814
815 // Don't allow this texture to be cleaned up by
816 // BufferManager::destroy.
817 mBufferData[i].texture.name = -1U;
818 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
819
820 // Post the message to the SurfaceFlinger object.
821 flinger->postMessageAsync(msg);
822 }
823 }
824
Mathias Agopian898c4c92010-05-18 17:06:55 -0700825 mNumBuffers = size;
826 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700827}
828
829// only for debugging
830sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
831 return mBufferData[index].buffer;
832}
833
834status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700835 mActiveBuffer = index;
836 return NO_ERROR;
837}
838
839size_t Layer::BufferManager::getActiveBufferIndex() const {
840 return mActiveBuffer;
841}
842
843Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700844 Texture res;
Mathias Agopian7623da42010-06-01 15:12:58 -0700845 if (mFailover || mActiveBuffer<0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700846 res = mFailoverTexture;
847 } else {
848 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
849 }
850 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700851}
852
853sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700854 sp<GraphicBuffer> result;
855 const ssize_t activeBuffer = mActiveBuffer;
856 if (activeBuffer >= 0) {
857 BufferData const * const buffers = mBufferData;
858 Mutex::Autolock _l(mLock);
859 result = buffers[activeBuffer].buffer;
860 }
861 return result;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700862}
863
864sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
865{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700866 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700867 sp<GraphicBuffer> buffer;
868 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700869 buffer = buffers[index].buffer;
870 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700871 return buffer;
872}
873
874status_t Layer::BufferManager::attachBuffer(size_t index,
875 const sp<GraphicBuffer>& buffer)
876{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700877 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700878 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700879 buffers[index].buffer = buffer;
880 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700881 return NO_ERROR;
882}
883
884status_t Layer::BufferManager::destroy(EGLDisplay dpy)
885{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700886 BufferData* const buffers = mBufferData;
887 size_t num;
888 { // scope for the lock
889 Mutex::Autolock _l(mLock);
890 num = mNumBuffers;
891 for (size_t i=0 ; i<num ; i++) {
892 buffers[i].buffer = 0;
893 }
894 }
895 for (size_t i=0 ; i<num ; i++) {
896 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700897 }
898 destroyTexture(&mFailoverTexture, dpy);
899 return NO_ERROR;
900}
901
902status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
903 const sp<GraphicBuffer>& buffer)
904{
Mathias Agopian7623da42010-06-01 15:12:58 -0700905 status_t err = NO_INIT;
906 ssize_t index = mActiveBuffer;
907 if (index >= 0) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700908 if (!mFailover) {
909 Image& texture(mBufferData[index].texture);
910 err = mTextureManager.initEglImage(&texture, dpy, buffer);
911 // if EGLImage fails, we switch to regular texture mode, and we
912 // free all resources associated with using EGLImages.
913 if (err == NO_ERROR) {
914 mFailover = false;
915 destroyTexture(&mFailoverTexture, dpy);
916 } else {
917 mFailover = true;
918 const size_t num = mNumBuffers;
919 for (size_t i=0 ; i<num ; i++) {
920 destroyTexture(&mBufferData[i].texture, dpy);
921 }
Andreas Huber330dd302010-06-25 09:25:19 -0700922 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700923 } else {
924 // we failed once, don't try again
925 err = BAD_VALUE;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700926 }
927 }
928 return err;
929}
930
931status_t Layer::BufferManager::loadTexture(
932 const Region& dirty, const GGLSurface& t)
933{
934 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
935}
936
Mathias Agopian898c4c92010-05-18 17:06:55 -0700937status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
938{
939 if (tex->name != -1U) {
940 glDeleteTextures(1, &tex->name);
941 tex->name = -1U;
942 }
943 if (tex->image != EGL_NO_IMAGE_KHR) {
944 eglDestroyImageKHR(dpy, tex->image);
945 tex->image = EGL_NO_IMAGE_KHR;
946 }
947 return NO_ERROR;
948}
949
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700950// ---------------------------------------------------------------------------
951
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700952Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian593c05c2010-06-02 23:28:45 -0700953 const sp<Layer>& owner)
954 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700955{
956}
957
958Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -0700959{
960}
961
Mathias Agopian2be352a2010-05-21 17:24:35 -0700962sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
963 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -0700964{
Mathias Agopian6950e422009-10-05 17:07:12 -0700965 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700966 sp<Layer> owner(getOwner());
967 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700968 /*
969 * requestBuffer() cannot be called from the main thread
970 * as it could cause a dead-lock, since it may have to wait
971 * on conditions updated my the main thread.
972 */
Mathias Agopian2be352a2010-05-21 17:24:35 -0700973 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700974 }
975 return buffer;
976}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977
Mathias Agopian59751db2010-05-07 15:58:44 -0700978status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
979{
980 status_t err = DEAD_OBJECT;
981 sp<Layer> owner(getOwner());
982 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700983 /*
984 * setBufferCount() cannot be called from the main thread
985 * as it could cause a dead-lock, since it may have to wait
986 * on conditions updated my the main thread.
987 */
Mathias Agopian59751db2010-05-07 15:58:44 -0700988 err = owner->setBufferCount(bufferCount);
989 }
990 return err;
991}
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993// ---------------------------------------------------------------------------
994
995
996}; // namespace android