blob: 80aef5fcc2c559c129093ae6ddae48d8164e4190 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian3330b202009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070038#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40
41#define DEBUG_RESIZE 0
42
43
44namespace android {
45
Mathias Agopianca99fb82010-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 Projectedbf3b62009-03-03 19:31:44 -080050// ---------------------------------------------------------------------------
51
Mathias Agopian96f08192010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian401c2572009-09-23 19:16:27 -070056 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070057 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070058 mSecure(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070059 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070060 mBufferManager(mTextureManager),
Mathias Agopian733189d2010-12-02 21:32:29 -080061 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063}
64
65Layer::~Layer()
66{
Mathias Agopianbb641242010-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 Agopianb7e930d2010-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 Agopiand606de62010-05-10 20:06:11 -070077}
78
Mathias Agopianb7e930d2010-06-01 15:12:58 -070079status_t Layer::setToken(const sp<UserClient>& userClient,
80 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070081{
Mathias Agopian579b3f82010-06-08 19:54:15 -070082 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070083 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070084 getIdentity());
85
Mathias Agopianb7e930d2010-06-01 15:12:58 -070086 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-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 Agopianb7e930d2010-06-01 15:12:58 -070094 }
95
96 return err;
97}
98
99int32_t Layer::getToken() const
100{
101 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700102}
103
Mathias Agopian579b3f82010-06-08 19:54:15 -0700104sp<UserClient> Layer::getClient() const
105{
106 return mUserClientRef.getClient();
107}
108
Mathias Agopiand606de62010-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 Agopianb7e930d2010-06-01 15:12:58 -0700113 ClientRef::Access sharedClient(mUserClientRef);
114 SharedBufferServer* lcblk(sharedClient.get());
115 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700116 // wake up the condition
117 lcblk->setStatus(NO_INIT);
118 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700119}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700120
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
123 return mSurface;
124}
125
Mathias Agopian9a112062009-04-17 19:36:26 -0700126status_t Layer::ditch()
127{
Mathias Agopianbb641242010-05-18 17:06:55 -0700128 // NOTE: Called from the main UI thread
129
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700130 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800131 mFreezeLock.clear();
Mathias Agopianbb641242010-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 Agopian9a112062009-04-17 19:36:26 -0700139 return NO_ERROR;
140}
141
Mathias Agopianf9d93272009-06-19 17:00:27 -0700142status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 PixelFormat format, uint32_t flags)
144{
Mathias Agopian401c2572009-09-23 19:16:27 -0700145 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 PixelFormatInfo info;
147 status_t err = getPixelFormatInfo(format, &info);
148 if (err) return err;
149
Mathias Agopian401c2572009-09-23 19:16:27 -0700150 // the display's pixel format
151 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-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 Agopian401c2572009-09-23 19:16:27 -0700161 PixelFormatInfo displayInfo;
162 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700163 const uint32_t hwFlags = hw.getFlags();
164
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700165 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700166 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700167 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700168
169 mReqFormat = format;
170 mReqWidth = w;
171 mReqHeight = h;
172
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700174 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
175 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700176
Mathias Agopian401c2572009-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 Agopian96f08192010-06-02 23:28:45 -0700182 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 return NO_ERROR;
184}
185
Mathias Agopiana350ff92010-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
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800207 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
208 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700209
210 if (needsBlending()) {
211 hwcl->blending = mPremultipliedAlpha ?
212 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
213 }
214
215 hwcl->displayFrame.left = mTransformedBounds.left;
216 hwcl->displayFrame.top = mTransformedBounds.top;
217 hwcl->displayFrame.right = mTransformedBounds.right;
218 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
219
220 hwcl->visibleRegionScreen.rects =
221 reinterpret_cast<hwc_rect_t const *>(
222 visibleRegionScreen.getArray(
223 &hwcl->visibleRegionScreen.numRects));
224}
225
226void Layer::setPerFrameData(hwc_layer_t* hwcl) {
227 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
228 if (buffer == NULL) {
229 // this situation can happen if we ran out of memory for instance.
230 // not much we can do. continue to use whatever texture was bound
231 // to this context.
232 hwcl->handle = NULL;
233 return;
234 }
Louis Huemiller04048142010-12-01 12:29:36 -0800235 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800236
237 if (!mBufferCrop.isEmpty()) {
238 hwcl->sourceCrop.left = mBufferCrop.left;
239 hwcl->sourceCrop.top = mBufferCrop.top;
240 hwcl->sourceCrop.right = mBufferCrop.right;
241 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
242 } else {
243 hwcl->sourceCrop.left = 0;
244 hwcl->sourceCrop.top = 0;
245 hwcl->sourceCrop.right = buffer->width;
246 hwcl->sourceCrop.bottom = buffer->height;
247 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700248}
249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250void Layer::reloadTexture(const Region& dirty)
251{
Mathias Agopiand606de62010-05-10 20:06:11 -0700252 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800253 if (buffer == NULL) {
254 // this situation can happen if we ran out of memory for instance.
255 // not much we can do. continue to use whatever texture was bound
256 // to this context.
257 return;
258 }
259
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700260 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700261 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
262 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
263 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700264 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700266 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800267slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700268 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700269 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
270 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
271 LOGE_IF(res, "error %d (%s) locking buffer %p",
272 res, strerror(res), buffer.get());
273 if (res == NO_ERROR) {
274 mBufferManager.loadTexture(dirty, t);
275 buffer->unlock();
276 }
277 } else {
278 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281}
282
Mathias Agopian74c40c02010-09-29 13:02:36 -0700283void Layer::drawForSreenShot() const
284{
Mathias Agopian733189d2010-12-02 21:32:29 -0800285 const bool currentFiltering = mNeedsFiltering;
286 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700287 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800288 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700289}
290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291void Layer::onDraw(const Region& clip) const
292{
Mathias Agopiand606de62010-05-10 20:06:11 -0700293 Texture tex(mBufferManager.getActiveTexture());
294 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700296 // in fact never been drawn into. This happens frequently with
297 // SurfaceView because the WindowManager can't know when the client
298 // has drawn the first time.
299
300 // If there is nothing under us, we paint the screen in black, otherwise
301 // we just skip this update.
302
303 // figure out if there is something below us
304 Region under;
305 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
306 const size_t count = drawingLayers.size();
307 for (size_t i=0 ; i<count ; ++i) {
308 const sp<LayerBase>& layer(drawingLayers[i]);
309 if (layer.get() == static_cast<LayerBase const*>(this))
310 break;
311 under.orSelf(layer->visibleRegionScreen);
312 }
313 // if not everything below us is covered, we plug the holes!
314 Region holes(clip.subtract(under));
315 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700316 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 return;
319 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700320 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
Mathias Agopiana7f66922010-05-26 22:08:52 -0700323bool Layer::needsFiltering() const
324{
325 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800326 // if our buffer is not the same size than ourselves,
327 // we need filtering.
328 Mutex::Autolock _l(mLock);
329 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700330 return true;
331 }
332 return LayerBase::needsFiltering();
333}
334
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700335
336status_t Layer::setBufferCount(int bufferCount)
337{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700338 ClientRef::Access sharedClient(mUserClientRef);
339 SharedBufferServer* lcblk(sharedClient.get());
340 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700341 // oops, the client is already gone
342 return DEAD_OBJECT;
343 }
344
Mathias Agopianbb641242010-05-18 17:06:55 -0700345 // NOTE: lcblk->resize() is protected by an internal lock
346 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700347 if (err == NO_ERROR) {
348 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
349 mBufferManager.resize(bufferCount, mFlinger, dpy);
350 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700351
352 return err;
353}
354
Mathias Agopiana138f892010-05-21 17:24:35 -0700355sp<GraphicBuffer> Layer::requestBuffer(int index,
356 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
357 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358{
Mathias Agopian3330b202009-10-05 17:07:12 -0700359 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700360
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700361 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700362 return buffer;
363
364 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
365 return buffer;
366
Mathias Agopian48d819a2009-09-10 19:41:18 -0700367 // this ensures our client doesn't go away while we're accessing
368 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700369 ClientRef::Access sharedClient(mUserClientRef);
370 SharedBufferServer* lcblk(sharedClient.get());
371 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700372 // oops, the client is already gone
373 return buffer;
374 }
375
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700376 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700377 * This is called from the client's Surface::dequeue(). This can happen
378 * at any time, especially while we're in the middle of using the
379 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700380 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381
Mathias Agopian208cb072010-07-27 20:11:35 -0700382 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700383 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700384 { // scope for the lock
385 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700386
387 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700388 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700389 if (!reqFormat) reqFormat = mFormat;
390 if (!reqWidth) reqWidth = mWidth;
391 if (!reqHeight) reqHeight = mHeight;
392
393 w = reqWidth;
394 h = reqHeight;
395 f = reqFormat;
396
397 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
398 (reqFormat != mReqFormat)) {
399 mReqWidth = reqWidth;
400 mReqHeight = reqHeight;
401 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700402 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800403 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700404
Mathias Agopiana138f892010-05-21 17:24:35 -0700405 lcblk->reallocateAllExcept(index);
406 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700407 }
408
Mathias Agopian208cb072010-07-27 20:11:35 -0700409 // here we have to reallocate a new buffer because the buffer could be
410 // used as the front buffer, or by a client in our process
411 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700412 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700413 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
414 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700415
416 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800417 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700418 LOGE_IF(err || buffer->handle == 0,
419 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
420 this, index, w, h, strerror(-err));
421 } else {
422 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700423 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
424 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700425 }
426
427 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700428 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700429 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700430 }
431 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
433
Mathias Agopian3330b202009-10-05 17:07:12 -0700434uint32_t Layer::getEffectiveUsage(uint32_t usage) const
435{
436 /*
437 * buffers used for software rendering, but h/w composition
438 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
439 *
440 * buffers used for h/w rendering and h/w composition
441 * are allocated with HW_RENDER | HW_TEXTURE
442 *
443 * buffers used with h/w rendering and either NPOT or no egl_image_ext
444 * are allocated with SW_READ_RARELY | HW_RENDER
445 *
446 */
447
448 if (mSecure) {
449 // secure buffer, don't store it into the GPU
450 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
451 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
452 } else {
453 // it's allowed to modify the usage flags here, but generally
454 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700455 // request EGLImage for all buffers
456 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700457 }
458 return usage;
459}
460
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461uint32_t Layer::doTransaction(uint32_t flags)
462{
463 const Layer::State& front(drawingState());
464 const Layer::State& temp(currentState());
465
Mathias Agopiana138f892010-05-21 17:24:35 -0700466 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
467 (front.requested_h != temp.requested_h);
468
469 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700470 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700472 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
473 this,
474 int(temp.requested_w), int(temp.requested_h),
475 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476
Mathias Agopiana138f892010-05-21 17:24:35 -0700477 if (!isFixedSize()) {
478 // we're being resized and there is a freeze display request,
479 // acquire a freeze lock, so that the screen stays put
480 // until we've redrawn at the new size; this is to avoid
481 // glitches upon orientation changes.
482 if (mFlinger->hasFreezeRequest()) {
483 // if the surface is hidden, don't try to acquire the
484 // freeze lock, since hidden surfaces may never redraw
485 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
486 mFreezeLock = mFlinger->getFreezeLock();
487 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700489
490 // this will make sure LayerBase::doTransaction doesn't update
491 // the drawing state's size
492 Layer::State& editDraw(mDrawingState);
493 editDraw.requested_w = temp.requested_w;
494 editDraw.requested_h = temp.requested_h;
495
496 // record the new size, form this point on, when the client request
497 // a buffer, it'll get the new size.
498 setBufferSize(temp.requested_w, temp.requested_h);
499
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700500 ClientRef::Access sharedClient(mUserClientRef);
501 SharedBufferServer* lcblk(sharedClient.get());
502 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700503 // all buffers need reallocation
504 lcblk->reallocateAll();
505 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700506 } else {
507 // record the new size
508 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 }
510 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700511
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 if (temp.sequence != front.sequence) {
513 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
514 // this surface is now hidden, so it shouldn't hold a freeze lock
515 // (it may never redraw, which is fine if it is hidden)
516 mFreezeLock.clear();
517 }
518 }
519
520 return LayerBase::doTransaction(flags);
521}
522
Mathias Agopiana138f892010-05-21 17:24:35 -0700523void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700524 Mutex::Autolock _l(mLock);
525 mWidth = w;
526 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800527 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528}
529
Mathias Agopiana138f892010-05-21 17:24:35 -0700530bool Layer::isFixedSize() const {
531 Mutex::Autolock _l(mLock);
532 return mFixedSize;
533}
534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535// ----------------------------------------------------------------------------
536// pageflip handling...
537// ----------------------------------------------------------------------------
538
539void Layer::lockPageFlip(bool& recomputeVisibleRegions)
540{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700541 ClientRef::Access sharedClient(mUserClientRef);
542 SharedBufferServer* lcblk(sharedClient.get());
543 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700544 // client died
545 recomputeVisibleRegions = true;
546 return;
547 }
548
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700549 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700550 if (buf == NOT_ENOUGH_DATA) {
551 // NOTE: This is not an error, it simply means there is nothing to
552 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700553 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 return;
555 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700556
Mathias Agopiand606de62010-05-10 20:06:11 -0700557 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700558 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700559 mPostedDirtyRegion.clear();
560 return;
561 }
562
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700563 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700564 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700565 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700566 mPostedDirtyRegion.clear();
567 return;
568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569
Mathias Agopian3330b202009-10-05 17:07:12 -0700570 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700571 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700572 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700573 // compute the posted region
574 const Region dirty(lcblk->getDirtyRegion(buf));
575 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700576
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700577 // update the layer size and release freeze-lock
578 const Layer::State& front(drawingState());
579 if (newFrontBuffer->getWidth() == front.requested_w &&
580 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700581 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700582 if ((front.w != front.requested_w) ||
583 (front.h != front.requested_h))
584 {
585 // Here we pretend the transaction happened by updating the
586 // current and drawing states. Drawing state is only accessed
587 // in this thread, no need to have it locked
588 Layer::State& editDraw(mDrawingState);
589 editDraw.w = editDraw.requested_w;
590 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700591
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700592 // We also need to update the current state so that we don't
593 // end-up doing too much work during the next transaction.
594 // NOTE: We actually don't need hold the transaction lock here
595 // because State::w and State::h are only accessed from
596 // this thread
597 Layer::State& editTemp(currentState());
598 editTemp.w = editDraw.w;
599 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700600
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700601 // recompute visible region
602 recomputeVisibleRegions = true;
603 }
604
605 // we now have the correct size, unfreeze the screen
606 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700607 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700608
609 // get the crop region
610 setBufferCrop( lcblk->getCrop(buf) );
611
612 // get the transformation
613 setBufferTransform( lcblk->getTransform(buf) );
614
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700615 } else {
616 // this should not happen unless we ran out of memory while
617 // allocating the buffer. we're hoping that things will get back
618 // to normal the next time the app tries to draw into this buffer.
619 // meanwhile, pretend the screen didn't update.
620 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700621 }
622
Mathias Agopiane7005012009-10-07 16:44:10 -0700623 if (lcblk->getQueuedCount()) {
624 // signal an event if we have more buffers waiting
625 mFlinger->signalEvent();
626 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopian245e4d72010-04-21 15:24:11 -0700628 /* a buffer was posted, so we need to call reloadTexture(), which
629 * will update our internal data structures (eg: EGLImageKHR or
630 * texture names). we need to do this even if mPostedDirtyRegion is
631 * empty -- it's orthogonal to the fact that a new buffer was posted,
632 * for instance, a degenerate case could be that the user did an empty
633 * update but repainted the buffer with appropriate content (after a
634 * resize for instance).
635 */
636 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637}
638
639void Layer::unlockPageFlip(
640 const Transform& planeTransform, Region& outDirtyRegion)
641{
642 Region dirtyRegion(mPostedDirtyRegion);
643 if (!dirtyRegion.isEmpty()) {
644 mPostedDirtyRegion.clear();
645 // The dirty region is given in the layer's coordinate space
646 // transform the dirty region by the surface's transformation
647 // and the global transformation.
648 const Layer::State& s(drawingState());
649 const Transform tr(planeTransform * s.transform);
650 dirtyRegion = tr.transform(dirtyRegion);
651
652 // At this point, the dirty region is in screen space.
653 // Make sure it's constrained by the visible region (which
654 // is in screen space as well).
655 dirtyRegion.andSelf(visibleRegionScreen);
656 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800658 if (visibleRegionScreen.isEmpty()) {
659 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700660 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800661 mFreezeLock.clear();
662 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663}
664
665void Layer::finishPageFlip()
666{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700667 ClientRef::Access sharedClient(mUserClientRef);
668 SharedBufferServer* lcblk(sharedClient.get());
669 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700670 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700671 if (buf >= 0) {
672 status_t err = lcblk->unlock( buf );
673 LOGE_IF(err!=NO_ERROR,
674 "layer %p, buffer=%d wasn't locked!",
675 this, buf);
676 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700677 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678}
679
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700680
681void Layer::dump(String8& result, char* buffer, size_t SIZE) const
682{
683 LayerBaseClient::dump(result, buffer, SIZE);
684
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700685 ClientRef::Access sharedClient(mUserClientRef);
686 SharedBufferServer* lcblk(sharedClient.get());
687 uint32_t totalTime = 0;
688 if (lcblk) {
689 SharedBufferStack::Statistics stats = lcblk->getStats();
690 totalTime= stats.totalTime;
691 result.append( lcblk->dump(" ") );
692 }
693
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700694 sp<const GraphicBuffer> buf0(getBuffer(0));
695 sp<const GraphicBuffer> buf1(getBuffer(1));
696 uint32_t w0=0, h0=0, s0=0;
697 uint32_t w1=0, h1=0, s1=0;
698 if (buf0 != 0) {
699 w0 = buf0->getWidth();
700 h0 = buf0->getHeight();
701 s0 = buf0->getStride();
702 }
703 if (buf1 != 0) {
704 w1 = buf1->getWidth();
705 h1 = buf1->getHeight();
706 s1 = buf1->getStride();
707 }
708 snprintf(buffer, SIZE,
709 " "
710 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
711 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700712 mFormat, w0, h0, s0, w1, h1, s1,
713 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700714
715 result.append(buffer);
716}
717
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700718// ---------------------------------------------------------------------------
719
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700720Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700721 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700722}
723
724Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700725}
726
727int32_t Layer::ClientRef::getToken() const {
728 Mutex::Autolock _l(mLock);
729 return mToken;
730}
731
Mathias Agopian579b3f82010-06-08 19:54:15 -0700732sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700733 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700734 return mUserClient.promote();
735}
736
737status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
738 const sp<SharedBufferServer>& sharedClient, int32_t token) {
739 Mutex::Autolock _l(mLock);
740
741 { // scope for strong mUserClient reference
742 sp<UserClient> userClient(mUserClient.promote());
743 if (mUserClient != 0 && mControlBlock != 0) {
744 mControlBlock->setStatus(NO_INIT);
745 }
746 }
747
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700748 mUserClient = uc;
749 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700750 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700751 return NO_ERROR;
752}
753
754sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
755 return mUserClient.promote();
756}
757
758// this class gives us access to SharedBufferServer safely
759// it makes sure the UserClient (and its associated shared memory)
760// won't go away while we're accessing it.
761Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700762 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700763{
764 Mutex::Autolock _l(ref.mLock);
765 mUserClientStrongRef = ref.mUserClient.promote();
766 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700767 mControlBlock = ref.mControlBlock;
768}
769
770Layer::ClientRef::Access::~Access()
771{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700772}
773
774// ---------------------------------------------------------------------------
775
Mathias Agopiand606de62010-05-10 20:06:11 -0700776Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700777 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700778 mActiveBuffer(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700779{
780}
781
Mathias Agopianbb641242010-05-18 17:06:55 -0700782Layer::BufferManager::~BufferManager()
783{
784}
785
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700786status_t Layer::BufferManager::resize(size_t size,
787 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700788{
789 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700790
791 if (size < mNumBuffers) {
792 // Move the active texture into slot 0
793 BufferData activeBufferData = mBufferData[mActiveBuffer];
794 mBufferData[mActiveBuffer] = mBufferData[0];
795 mBufferData[0] = activeBufferData;
796 mActiveBuffer = 0;
797
798 // Free the buffers that are no longer needed.
799 for (size_t i = size; i < mNumBuffers; i++) {
800 mBufferData[i].buffer = 0;
801
802 // Create a message to destroy the textures on SurfaceFlinger's GL
803 // thread.
804 class MessageDestroyTexture : public MessageBase {
805 Image mTexture;
806 EGLDisplay mDpy;
807 public:
808 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
809 : mTexture(texture), mDpy(dpy) { }
810 virtual bool handler() {
811 status_t err = Layer::BufferManager::destroyTexture(
812 &mTexture, mDpy);
813 LOGE_IF(err<0, "error destroying texture: %d (%s)",
814 mTexture.name, strerror(-err));
815 return true; // XXX: err == 0; ????
816 }
817 };
818
819 MessageDestroyTexture *msg = new MessageDestroyTexture(
820 mBufferData[i].texture, dpy);
821
822 // Don't allow this texture to be cleaned up by
823 // BufferManager::destroy.
824 mBufferData[i].texture.name = -1U;
825 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
826
827 // Post the message to the SurfaceFlinger object.
828 flinger->postMessageAsync(msg);
829 }
830 }
831
Mathias Agopianbb641242010-05-18 17:06:55 -0700832 mNumBuffers = size;
833 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700834}
835
836// only for debugging
837sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
838 return mBufferData[index].buffer;
839}
840
841status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700842 mActiveBuffer = index;
843 return NO_ERROR;
844}
845
846size_t Layer::BufferManager::getActiveBufferIndex() const {
847 return mActiveBuffer;
848}
849
850Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700851 Texture res;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700852 if (mFailover || mActiveBuffer<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700853 res = mFailoverTexture;
854 } else {
855 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
856 }
857 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700858}
859
860sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700861 sp<GraphicBuffer> result;
862 const ssize_t activeBuffer = mActiveBuffer;
863 if (activeBuffer >= 0) {
864 BufferData const * const buffers = mBufferData;
865 Mutex::Autolock _l(mLock);
866 result = buffers[activeBuffer].buffer;
867 }
868 return result;
Mathias Agopiand606de62010-05-10 20:06:11 -0700869}
870
871sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
872{
Mathias Agopianbb641242010-05-18 17:06:55 -0700873 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700874 sp<GraphicBuffer> buffer;
875 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700876 buffer = buffers[index].buffer;
877 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700878 return buffer;
879}
880
881status_t Layer::BufferManager::attachBuffer(size_t index,
882 const sp<GraphicBuffer>& buffer)
883{
Mathias Agopianbb641242010-05-18 17:06:55 -0700884 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700885 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700886 buffers[index].buffer = buffer;
887 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700888 return NO_ERROR;
889}
890
891status_t Layer::BufferManager::destroy(EGLDisplay dpy)
892{
Mathias Agopianbb641242010-05-18 17:06:55 -0700893 BufferData* const buffers = mBufferData;
894 size_t num;
895 { // scope for the lock
896 Mutex::Autolock _l(mLock);
897 num = mNumBuffers;
898 for (size_t i=0 ; i<num ; i++) {
899 buffers[i].buffer = 0;
900 }
901 }
902 for (size_t i=0 ; i<num ; i++) {
903 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700904 }
905 destroyTexture(&mFailoverTexture, dpy);
906 return NO_ERROR;
907}
908
909status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
910 const sp<GraphicBuffer>& buffer)
911{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700912 status_t err = NO_INIT;
913 ssize_t index = mActiveBuffer;
914 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700915 if (!mFailover) {
916 Image& texture(mBufferData[index].texture);
917 err = mTextureManager.initEglImage(&texture, dpy, buffer);
918 // if EGLImage fails, we switch to regular texture mode, and we
919 // free all resources associated with using EGLImages.
920 if (err == NO_ERROR) {
921 mFailover = false;
922 destroyTexture(&mFailoverTexture, dpy);
923 } else {
924 mFailover = true;
925 const size_t num = mNumBuffers;
926 for (size_t i=0 ; i<num ; i++) {
927 destroyTexture(&mBufferData[i].texture, dpy);
928 }
Andreas Hubere049a952010-06-25 09:25:19 -0700929 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700930 } else {
931 // we failed once, don't try again
932 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700933 }
934 }
935 return err;
936}
937
938status_t Layer::BufferManager::loadTexture(
939 const Region& dirty, const GGLSurface& t)
940{
941 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
942}
943
Mathias Agopianbb641242010-05-18 17:06:55 -0700944status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
945{
946 if (tex->name != -1U) {
947 glDeleteTextures(1, &tex->name);
948 tex->name = -1U;
949 }
950 if (tex->image != EGL_NO_IMAGE_KHR) {
951 eglDestroyImageKHR(dpy, tex->image);
952 tex->image = EGL_NO_IMAGE_KHR;
953 }
954 return NO_ERROR;
955}
956
Mathias Agopiand606de62010-05-10 20:06:11 -0700957// ---------------------------------------------------------------------------
958
Mathias Agopian9a112062009-04-17 19:36:26 -0700959Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700960 const sp<Layer>& owner)
961 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700962{
963}
964
965Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700966{
967}
968
Mathias Agopiana138f892010-05-21 17:24:35 -0700969sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
970 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700971{
Mathias Agopian3330b202009-10-05 17:07:12 -0700972 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700973 sp<Layer> owner(getOwner());
974 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700975 /*
976 * requestBuffer() cannot be called from the main thread
977 * as it could cause a dead-lock, since it may have to wait
978 * on conditions updated my the main thread.
979 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700980 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700981 }
982 return buffer;
983}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700985status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
986{
987 status_t err = DEAD_OBJECT;
988 sp<Layer> owner(getOwner());
989 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700990 /*
991 * setBufferCount() cannot be called from the main thread
992 * as it could cause a dead-lock, since it may have to wait
993 * on conditions updated my the main thread.
994 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700995 err = owner->setBufferCount(bufferCount);
996 }
997 return err;
998}
999
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000// ---------------------------------------------------------------------------
1001
1002
1003}; // namespace android