blob: 188686d7739ad0dec27debaf784a7796803da2ef [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),
Glenn Kasten16f04532011-01-19 15:27:27 -080059 mProtectedByApp(false),
60 mProtectedByDRM(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070062 mBufferManager(mTextureManager),
Mathias Agopian733189d2010-12-02 21:32:29 -080063 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065}
66
67Layer::~Layer()
68{
Mathias Agopianbb641242010-05-18 17:06:55 -070069 // FIXME: must be called from the main UI thread
70 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
71 mBufferManager.destroy(dpy);
72
Mathias Agopianb7e930d2010-06-01 15:12:58 -070073 // we can use getUserClientUnsafe here because we know we're
74 // single-threaded at that point.
75 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
76 if (ourClient != 0) {
77 ourClient->detachLayer(this);
78 }
Mathias Agopiand606de62010-05-10 20:06:11 -070079}
80
Mathias Agopianb7e930d2010-06-01 15:12:58 -070081status_t Layer::setToken(const sp<UserClient>& userClient,
82 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070083{
Mathias Agopian579b3f82010-06-08 19:54:15 -070084 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070085 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070086 getIdentity());
87
Mathias Agopian579b3f82010-06-08 19:54:15 -070088
Mathias Agopiandd17b3e2010-12-13 16:49:05 -080089 sp<UserClient> ourClient(mUserClientRef.getClient());
90
91 /*
92 * Here it is guaranteed that userClient != ourClient
93 * (see UserClient::getTokenForSurface()).
94 *
95 * We release the token used by this surface in ourClient below.
96 * This should be safe to do so now, since this layer won't be attached
97 * to this client, it should be okay to reuse that id.
98 *
99 * If this causes problems, an other solution would be to keep a list
100 * of all the {UserClient, token} ever used and release them when the
101 * Layer is destroyed.
102 *
103 */
104
105 if (ourClient != 0) {
106 ourClient->detachLayer(this);
107 }
108
109 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700110 LOGE_IF(err != NO_ERROR,
111 "ClientRef::setToken(%p, %p, %u) failed",
112 userClient.get(), lcblk.get(), token);
113
114 if (err == NO_ERROR) {
115 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700116 }
117
118 return err;
119}
120
121int32_t Layer::getToken() const
122{
123 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700124}
125
Mathias Agopian579b3f82010-06-08 19:54:15 -0700126sp<UserClient> Layer::getClient() const
127{
128 return mUserClientRef.getClient();
129}
130
Mathias Agopiand606de62010-05-10 20:06:11 -0700131// called with SurfaceFlinger::mStateLock as soon as the layer is entered
132// in the purgatory list
133void Layer::onRemoved()
134{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700135 ClientRef::Access sharedClient(mUserClientRef);
136 SharedBufferServer* lcblk(sharedClient.get());
137 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700138 // wake up the condition
139 lcblk->setStatus(NO_INIT);
140 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700141}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700142
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144{
145 return mSurface;
146}
147
Mathias Agopian9a112062009-04-17 19:36:26 -0700148status_t Layer::ditch()
149{
Mathias Agopianbb641242010-05-18 17:06:55 -0700150 // NOTE: Called from the main UI thread
151
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700152 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800153 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700154
155 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
156 mBufferManager.destroy(dpy);
157 mSurface.clear();
158
159 Mutex::Autolock _l(mLock);
160 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700161 return NO_ERROR;
162}
163
Mathias Agopianf9d93272009-06-19 17:00:27 -0700164status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 PixelFormat format, uint32_t flags)
166{
Mathias Agopian401c2572009-09-23 19:16:27 -0700167 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 PixelFormatInfo info;
169 status_t err = getPixelFormatInfo(format, &info);
170 if (err) return err;
171
Mathias Agopian401c2572009-09-23 19:16:27 -0700172 // the display's pixel format
173 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700174 uint32_t const maxSurfaceDims = min(
175 hw.getMaxTextureSize(), hw.getMaxViewportDims());
176
177 // never allow a surface larger than what our underlying GL implementation
178 // can handle.
179 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
180 return BAD_VALUE;
181 }
182
Mathias Agopian401c2572009-09-23 19:16:27 -0700183 PixelFormatInfo displayInfo;
184 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700185 const uint32_t hwFlags = hw.getFlags();
186
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700187 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700188 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700189 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700190
191 mReqFormat = format;
192 mReqWidth = w;
193 mReqHeight = h;
194
Mathias Agopian3330b202009-10-05 17:07:12 -0700195 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800196 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
197 mProtectedByDRM = (flags & ISurfaceComposer::eProtectedByDRM) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700198 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
199 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700200
Mathias Agopian401c2572009-09-23 19:16:27 -0700201 // we use the red index
202 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
203 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
204 mNeedsDithering = layerRedsize > displayRedSize;
205
Mathias Agopian96f08192010-06-02 23:28:45 -0700206 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return NO_ERROR;
208}
209
Mathias Agopiana350ff92010-08-10 17:14:02 -0700210void Layer::setGeometry(hwc_layer_t* hwcl)
211{
212 hwcl->compositionType = HWC_FRAMEBUFFER;
213 hwcl->hints = 0;
214 hwcl->flags = 0;
215 hwcl->transform = 0;
216 hwcl->blending = HWC_BLENDING_NONE;
217
218 // we can't do alpha-fade with the hwc HAL
219 const State& s(drawingState());
220 if (s.alpha < 0xFF) {
221 hwcl->flags = HWC_SKIP_LAYER;
222 return;
223 }
224
225 // we can only handle simple transformation
226 if (mOrientation & Transform::ROT_INVALID) {
227 hwcl->flags = HWC_SKIP_LAYER;
228 return;
229 }
230
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800231 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
232 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700233
234 if (needsBlending()) {
235 hwcl->blending = mPremultipliedAlpha ?
236 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
237 }
238
239 hwcl->displayFrame.left = mTransformedBounds.left;
240 hwcl->displayFrame.top = mTransformedBounds.top;
241 hwcl->displayFrame.right = mTransformedBounds.right;
242 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
243
244 hwcl->visibleRegionScreen.rects =
245 reinterpret_cast<hwc_rect_t const *>(
246 visibleRegionScreen.getArray(
247 &hwcl->visibleRegionScreen.numRects));
248}
249
250void Layer::setPerFrameData(hwc_layer_t* hwcl) {
251 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
252 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800253 // this can happen if the client never drew into this layer yet,
254 // or if we ran out of memory. In that case, don't let
255 // HWC handle it.
256 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700257 hwcl->handle = NULL;
258 return;
259 }
Louis Huemiller04048142010-12-01 12:29:36 -0800260 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800261
262 if (!mBufferCrop.isEmpty()) {
263 hwcl->sourceCrop.left = mBufferCrop.left;
264 hwcl->sourceCrop.top = mBufferCrop.top;
265 hwcl->sourceCrop.right = mBufferCrop.right;
266 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
267 } else {
268 hwcl->sourceCrop.left = 0;
269 hwcl->sourceCrop.top = 0;
270 hwcl->sourceCrop.right = buffer->width;
271 hwcl->sourceCrop.bottom = buffer->height;
272 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700273}
274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275void Layer::reloadTexture(const Region& dirty)
276{
Mathias Agopiand606de62010-05-10 20:06:11 -0700277 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800278 if (buffer == NULL) {
279 // this situation can happen if we ran out of memory for instance.
280 // not much we can do. continue to use whatever texture was bound
281 // to this context.
282 return;
283 }
284
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700285 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700286 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
287 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
288 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700289 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700291 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800292slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700294 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
295 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
296 LOGE_IF(res, "error %d (%s) locking buffer %p",
297 res, strerror(res), buffer.get());
298 if (res == NO_ERROR) {
299 mBufferManager.loadTexture(dirty, t);
300 buffer->unlock();
301 }
302 } else {
303 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306}
307
Mathias Agopian74c40c02010-09-29 13:02:36 -0700308void Layer::drawForSreenShot() const
309{
Mathias Agopian733189d2010-12-02 21:32:29 -0800310 const bool currentFiltering = mNeedsFiltering;
311 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700312 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800313 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700314}
315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316void Layer::onDraw(const Region& clip) const
317{
Mathias Agopiand606de62010-05-10 20:06:11 -0700318 Texture tex(mBufferManager.getActiveTexture());
319 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700321 // in fact never been drawn into. This happens frequently with
322 // SurfaceView because the WindowManager can't know when the client
323 // has drawn the first time.
324
325 // If there is nothing under us, we paint the screen in black, otherwise
326 // we just skip this update.
327
328 // figure out if there is something below us
329 Region under;
330 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
331 const size_t count = drawingLayers.size();
332 for (size_t i=0 ; i<count ; ++i) {
333 const sp<LayerBase>& layer(drawingLayers[i]);
334 if (layer.get() == static_cast<LayerBase const*>(this))
335 break;
336 under.orSelf(layer->visibleRegionScreen);
337 }
338 // if not everything below us is covered, we plug the holes!
339 Region holes(clip.subtract(under));
340 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700341 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700342 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 return;
344 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700345 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346}
347
Mathias Agopiana7f66922010-05-26 22:08:52 -0700348bool Layer::needsFiltering() const
349{
350 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800351 // if our buffer is not the same size than ourselves,
352 // we need filtering.
353 Mutex::Autolock _l(mLock);
354 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700355 return true;
356 }
357 return LayerBase::needsFiltering();
358}
359
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700360
361status_t Layer::setBufferCount(int bufferCount)
362{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700363 ClientRef::Access sharedClient(mUserClientRef);
364 SharedBufferServer* lcblk(sharedClient.get());
365 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700366 // oops, the client is already gone
367 return DEAD_OBJECT;
368 }
369
Mathias Agopianbb641242010-05-18 17:06:55 -0700370 // NOTE: lcblk->resize() is protected by an internal lock
371 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700372 if (err == NO_ERROR) {
373 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
374 mBufferManager.resize(bufferCount, mFlinger, dpy);
375 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700376
377 return err;
378}
379
Mathias Agopiana138f892010-05-21 17:24:35 -0700380sp<GraphicBuffer> Layer::requestBuffer(int index,
381 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
382 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383{
Mathias Agopian3330b202009-10-05 17:07:12 -0700384 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700385
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700386 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700387 return buffer;
388
389 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
390 return buffer;
391
Mathias Agopian48d819a2009-09-10 19:41:18 -0700392 // this ensures our client doesn't go away while we're accessing
393 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700394 ClientRef::Access sharedClient(mUserClientRef);
395 SharedBufferServer* lcblk(sharedClient.get());
396 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700397 // oops, the client is already gone
398 return buffer;
399 }
400
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700401 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700402 * This is called from the client's Surface::dequeue(). This can happen
403 * at any time, especially while we're in the middle of using the
404 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700405 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406
Mathias Agopian208cb072010-07-27 20:11:35 -0700407 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700408 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700409 { // scope for the lock
410 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700411
412 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700413 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700414 if (!reqFormat) reqFormat = mFormat;
415 if (!reqWidth) reqWidth = mWidth;
416 if (!reqHeight) reqHeight = mHeight;
417
418 w = reqWidth;
419 h = reqHeight;
420 f = reqFormat;
421
422 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
423 (reqFormat != mReqFormat)) {
424 mReqWidth = reqWidth;
425 mReqHeight = reqHeight;
426 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700427 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800428 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700429
Mathias Agopiana138f892010-05-21 17:24:35 -0700430 lcblk->reallocateAllExcept(index);
431 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700432 }
433
Mathias Agopian208cb072010-07-27 20:11:35 -0700434 // here we have to reallocate a new buffer because the buffer could be
435 // used as the front buffer, or by a client in our process
436 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700437 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700438 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
439 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700440
441 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800442 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700443 LOGE_IF(err || buffer->handle == 0,
444 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
445 this, index, w, h, strerror(-err));
446 } else {
447 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700448 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
449 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700450 }
451
452 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700453 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700454 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700455 }
456 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457}
458
Mathias Agopian3330b202009-10-05 17:07:12 -0700459uint32_t Layer::getEffectiveUsage(uint32_t usage) const
460{
461 /*
462 * buffers used for software rendering, but h/w composition
463 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
464 *
465 * buffers used for h/w rendering and h/w composition
466 * are allocated with HW_RENDER | HW_TEXTURE
467 *
468 * buffers used with h/w rendering and either NPOT or no egl_image_ext
469 * are allocated with SW_READ_RARELY | HW_RENDER
470 *
471 */
472
473 if (mSecure) {
474 // secure buffer, don't store it into the GPU
475 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
476 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
477 } else {
478 // it's allowed to modify the usage flags here, but generally
479 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700480 // request EGLImage for all buffers
481 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700482 }
Glenn Kasten16f04532011-01-19 15:27:27 -0800483 if (mProtectedByApp || mProtectedByDRM) {
484 // need a hardware-protected path to external video sink
485 usage |= GraphicBuffer::USAGE_PROTECTED;
486 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700487 return usage;
488}
489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490uint32_t Layer::doTransaction(uint32_t flags)
491{
492 const Layer::State& front(drawingState());
493 const Layer::State& temp(currentState());
494
Mathias Agopiana138f892010-05-21 17:24:35 -0700495 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
496 (front.requested_h != temp.requested_h);
497
498 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700499 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700501 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
502 this,
503 int(temp.requested_w), int(temp.requested_h),
504 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505
Mathias Agopiana138f892010-05-21 17:24:35 -0700506 if (!isFixedSize()) {
507 // we're being resized and there is a freeze display request,
508 // acquire a freeze lock, so that the screen stays put
509 // until we've redrawn at the new size; this is to avoid
510 // glitches upon orientation changes.
511 if (mFlinger->hasFreezeRequest()) {
512 // if the surface is hidden, don't try to acquire the
513 // freeze lock, since hidden surfaces may never redraw
514 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
515 mFreezeLock = mFlinger->getFreezeLock();
516 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700518
519 // this will make sure LayerBase::doTransaction doesn't update
520 // the drawing state's size
521 Layer::State& editDraw(mDrawingState);
522 editDraw.requested_w = temp.requested_w;
523 editDraw.requested_h = temp.requested_h;
524
525 // record the new size, form this point on, when the client request
526 // a buffer, it'll get the new size.
527 setBufferSize(temp.requested_w, temp.requested_h);
528
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700529 ClientRef::Access sharedClient(mUserClientRef);
530 SharedBufferServer* lcblk(sharedClient.get());
531 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700532 // all buffers need reallocation
533 lcblk->reallocateAll();
534 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700535 } else {
536 // record the new size
537 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 }
539 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700540
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 if (temp.sequence != front.sequence) {
542 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
543 // this surface is now hidden, so it shouldn't hold a freeze lock
544 // (it may never redraw, which is fine if it is hidden)
545 mFreezeLock.clear();
546 }
547 }
548
549 return LayerBase::doTransaction(flags);
550}
551
Mathias Agopiana138f892010-05-21 17:24:35 -0700552void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700553 Mutex::Autolock _l(mLock);
554 mWidth = w;
555 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800556 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557}
558
Mathias Agopiana138f892010-05-21 17:24:35 -0700559bool Layer::isFixedSize() const {
560 Mutex::Autolock _l(mLock);
561 return mFixedSize;
562}
563
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564// ----------------------------------------------------------------------------
565// pageflip handling...
566// ----------------------------------------------------------------------------
567
568void Layer::lockPageFlip(bool& recomputeVisibleRegions)
569{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700570 ClientRef::Access sharedClient(mUserClientRef);
571 SharedBufferServer* lcblk(sharedClient.get());
572 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700573 // client died
574 recomputeVisibleRegions = true;
575 return;
576 }
577
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700578 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700579 if (buf == NOT_ENOUGH_DATA) {
580 // NOTE: This is not an error, it simply means there is nothing to
581 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700582 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 return;
584 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700585
Mathias Agopiand606de62010-05-10 20:06:11 -0700586 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700587 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700588 mPostedDirtyRegion.clear();
589 return;
590 }
591
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700592 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800593
594 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Mathias Agopiand606de62010-05-10 20:06:11 -0700595 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700596 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700597 mPostedDirtyRegion.clear();
598 return;
599 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600
Mathias Agopianda9584d2010-12-13 18:51:59 -0800601 if (noActiveBuffer) {
602 // we didn't have an active buffer, we need to recompute
603 // our visible region
604 recomputeVisibleRegions = true;
605 }
606
Mathias Agopian3330b202009-10-05 17:07:12 -0700607 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700608 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700609 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700610 // compute the posted region
611 const Region dirty(lcblk->getDirtyRegion(buf));
612 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700614 // update the layer size and release freeze-lock
615 const Layer::State& front(drawingState());
616 if (newFrontBuffer->getWidth() == front.requested_w &&
617 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700618 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700619 if ((front.w != front.requested_w) ||
620 (front.h != front.requested_h))
621 {
622 // Here we pretend the transaction happened by updating the
623 // current and drawing states. Drawing state is only accessed
624 // in this thread, no need to have it locked
625 Layer::State& editDraw(mDrawingState);
626 editDraw.w = editDraw.requested_w;
627 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700628
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700629 // We also need to update the current state so that we don't
630 // end-up doing too much work during the next transaction.
631 // NOTE: We actually don't need hold the transaction lock here
632 // because State::w and State::h are only accessed from
633 // this thread
634 Layer::State& editTemp(currentState());
635 editTemp.w = editDraw.w;
636 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700637
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700638 // recompute visible region
639 recomputeVisibleRegions = true;
640 }
641
642 // we now have the correct size, unfreeze the screen
643 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700644 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700645
646 // get the crop region
647 setBufferCrop( lcblk->getCrop(buf) );
648
649 // get the transformation
650 setBufferTransform( lcblk->getTransform(buf) );
651
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700652 } else {
653 // this should not happen unless we ran out of memory while
654 // allocating the buffer. we're hoping that things will get back
655 // to normal the next time the app tries to draw into this buffer.
656 // meanwhile, pretend the screen didn't update.
657 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700658 }
659
Mathias Agopiane7005012009-10-07 16:44:10 -0700660 if (lcblk->getQueuedCount()) {
661 // signal an event if we have more buffers waiting
662 mFlinger->signalEvent();
663 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664
Mathias Agopian245e4d72010-04-21 15:24:11 -0700665 /* a buffer was posted, so we need to call reloadTexture(), which
666 * will update our internal data structures (eg: EGLImageKHR or
667 * texture names). we need to do this even if mPostedDirtyRegion is
668 * empty -- it's orthogonal to the fact that a new buffer was posted,
669 * for instance, a degenerate case could be that the user did an empty
670 * update but repainted the buffer with appropriate content (after a
671 * resize for instance).
672 */
673 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674}
675
676void Layer::unlockPageFlip(
677 const Transform& planeTransform, Region& outDirtyRegion)
678{
679 Region dirtyRegion(mPostedDirtyRegion);
680 if (!dirtyRegion.isEmpty()) {
681 mPostedDirtyRegion.clear();
682 // The dirty region is given in the layer's coordinate space
683 // transform the dirty region by the surface's transformation
684 // and the global transformation.
685 const Layer::State& s(drawingState());
686 const Transform tr(planeTransform * s.transform);
687 dirtyRegion = tr.transform(dirtyRegion);
688
689 // At this point, the dirty region is in screen space.
690 // Make sure it's constrained by the visible region (which
691 // is in screen space as well).
692 dirtyRegion.andSelf(visibleRegionScreen);
693 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800695 if (visibleRegionScreen.isEmpty()) {
696 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700697 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800698 mFreezeLock.clear();
699 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700}
701
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700702void Layer::dump(String8& result, char* buffer, size_t SIZE) const
703{
704 LayerBaseClient::dump(result, buffer, SIZE);
705
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700706 ClientRef::Access sharedClient(mUserClientRef);
707 SharedBufferServer* lcblk(sharedClient.get());
708 uint32_t totalTime = 0;
709 if (lcblk) {
710 SharedBufferStack::Statistics stats = lcblk->getStats();
711 totalTime= stats.totalTime;
712 result.append( lcblk->dump(" ") );
713 }
714
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700715 sp<const GraphicBuffer> buf0(getBuffer(0));
716 sp<const GraphicBuffer> buf1(getBuffer(1));
717 uint32_t w0=0, h0=0, s0=0;
718 uint32_t w1=0, h1=0, s1=0;
719 if (buf0 != 0) {
720 w0 = buf0->getWidth();
721 h0 = buf0->getHeight();
722 s0 = buf0->getStride();
723 }
724 if (buf1 != 0) {
725 w1 = buf1->getWidth();
726 h1 = buf1->getHeight();
727 s1 = buf1->getStride();
728 }
729 snprintf(buffer, SIZE,
730 " "
731 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
732 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700733 mFormat, w0, h0, s0, w1, h1, s1,
734 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700735
736 result.append(buffer);
737}
738
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700739// ---------------------------------------------------------------------------
740
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700741Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700742 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700743}
744
745Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700746}
747
748int32_t Layer::ClientRef::getToken() const {
749 Mutex::Autolock _l(mLock);
750 return mToken;
751}
752
Mathias Agopian579b3f82010-06-08 19:54:15 -0700753sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700754 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700755 return mUserClient.promote();
756}
757
758status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
759 const sp<SharedBufferServer>& sharedClient, int32_t token) {
760 Mutex::Autolock _l(mLock);
761
762 { // scope for strong mUserClient reference
763 sp<UserClient> userClient(mUserClient.promote());
764 if (mUserClient != 0 && mControlBlock != 0) {
765 mControlBlock->setStatus(NO_INIT);
766 }
767 }
768
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700769 mUserClient = uc;
770 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700771 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700772 return NO_ERROR;
773}
774
775sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
776 return mUserClient.promote();
777}
778
779// this class gives us access to SharedBufferServer safely
780// it makes sure the UserClient (and its associated shared memory)
781// won't go away while we're accessing it.
782Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700783 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700784{
785 Mutex::Autolock _l(ref.mLock);
786 mUserClientStrongRef = ref.mUserClient.promote();
787 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700788 mControlBlock = ref.mControlBlock;
789}
790
791Layer::ClientRef::Access::~Access()
792{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700793}
794
795// ---------------------------------------------------------------------------
796
Mathias Agopiand606de62010-05-10 20:06:11 -0700797Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700798 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800799 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700800{
801}
802
Mathias Agopianbb641242010-05-18 17:06:55 -0700803Layer::BufferManager::~BufferManager()
804{
805}
806
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700807status_t Layer::BufferManager::resize(size_t size,
808 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700809{
810 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700811
812 if (size < mNumBuffers) {
813 // Move the active texture into slot 0
Mathias Agopian420a2832010-12-14 20:30:37 -0800814 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
815 mBufferData[mActiveBufferIndex] = mBufferData[0];
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700816 mBufferData[0] = activeBufferData;
Mathias Agopian420a2832010-12-14 20:30:37 -0800817 mActiveBufferIndex = 0;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700818
819 // Free the buffers that are no longer needed.
820 for (size_t i = size; i < mNumBuffers; i++) {
821 mBufferData[i].buffer = 0;
822
823 // Create a message to destroy the textures on SurfaceFlinger's GL
824 // thread.
825 class MessageDestroyTexture : public MessageBase {
826 Image mTexture;
827 EGLDisplay mDpy;
828 public:
829 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
830 : mTexture(texture), mDpy(dpy) { }
831 virtual bool handler() {
832 status_t err = Layer::BufferManager::destroyTexture(
833 &mTexture, mDpy);
834 LOGE_IF(err<0, "error destroying texture: %d (%s)",
835 mTexture.name, strerror(-err));
836 return true; // XXX: err == 0; ????
837 }
838 };
839
840 MessageDestroyTexture *msg = new MessageDestroyTexture(
841 mBufferData[i].texture, dpy);
842
843 // Don't allow this texture to be cleaned up by
844 // BufferManager::destroy.
845 mBufferData[i].texture.name = -1U;
846 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
847
848 // Post the message to the SurfaceFlinger object.
849 flinger->postMessageAsync(msg);
850 }
851 }
852
Mathias Agopianbb641242010-05-18 17:06:55 -0700853 mNumBuffers = size;
854 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700855}
856
857// only for debugging
858sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
859 return mBufferData[index].buffer;
860}
861
862status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800863 BufferData const * const buffers = mBufferData;
864 Mutex::Autolock _l(mLock);
865 mActiveBuffer = buffers[index].buffer;
866 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700867 return NO_ERROR;
868}
869
870size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800871 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700872}
873
874Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700875 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800876 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700877 res = mFailoverTexture;
878 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800879 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700880 }
881 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700882}
883
884sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800885 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700886}
887
Mathias Agopianda9584d2010-12-13 18:51:59 -0800888bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800889 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800890}
891
Mathias Agopiand606de62010-05-10 20:06:11 -0700892sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
893{
Mathias Agopianbb641242010-05-18 17:06:55 -0700894 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700895 sp<GraphicBuffer> buffer;
896 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700897 buffer = buffers[index].buffer;
898 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700899 return buffer;
900}
901
902status_t Layer::BufferManager::attachBuffer(size_t index,
903 const sp<GraphicBuffer>& buffer)
904{
Mathias Agopianbb641242010-05-18 17:06:55 -0700905 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700906 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700907 buffers[index].buffer = buffer;
908 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700909 return NO_ERROR;
910}
911
912status_t Layer::BufferManager::destroy(EGLDisplay dpy)
913{
Mathias Agopianbb641242010-05-18 17:06:55 -0700914 BufferData* const buffers = mBufferData;
915 size_t num;
916 { // scope for the lock
917 Mutex::Autolock _l(mLock);
918 num = mNumBuffers;
919 for (size_t i=0 ; i<num ; i++) {
920 buffers[i].buffer = 0;
921 }
922 }
923 for (size_t i=0 ; i<num ; i++) {
924 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700925 }
926 destroyTexture(&mFailoverTexture, dpy);
927 return NO_ERROR;
928}
929
930status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
931 const sp<GraphicBuffer>& buffer)
932{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700933 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800934 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700935 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700936 if (!mFailover) {
937 Image& texture(mBufferData[index].texture);
938 err = mTextureManager.initEglImage(&texture, dpy, buffer);
939 // if EGLImage fails, we switch to regular texture mode, and we
940 // free all resources associated with using EGLImages.
941 if (err == NO_ERROR) {
942 mFailover = false;
943 destroyTexture(&mFailoverTexture, dpy);
944 } else {
945 mFailover = true;
946 const size_t num = mNumBuffers;
947 for (size_t i=0 ; i<num ; i++) {
948 destroyTexture(&mBufferData[i].texture, dpy);
949 }
Andreas Hubere049a952010-06-25 09:25:19 -0700950 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700951 } else {
952 // we failed once, don't try again
953 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700954 }
955 }
956 return err;
957}
958
959status_t Layer::BufferManager::loadTexture(
960 const Region& dirty, const GGLSurface& t)
961{
962 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
963}
964
Mathias Agopianbb641242010-05-18 17:06:55 -0700965status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
966{
967 if (tex->name != -1U) {
968 glDeleteTextures(1, &tex->name);
969 tex->name = -1U;
970 }
971 if (tex->image != EGL_NO_IMAGE_KHR) {
972 eglDestroyImageKHR(dpy, tex->image);
973 tex->image = EGL_NO_IMAGE_KHR;
974 }
975 return NO_ERROR;
976}
977
Mathias Agopiand606de62010-05-10 20:06:11 -0700978// ---------------------------------------------------------------------------
979
Mathias Agopian9a112062009-04-17 19:36:26 -0700980Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700981 const sp<Layer>& owner)
982 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700983{
984}
985
986Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700987{
988}
989
Mathias Agopiana138f892010-05-21 17:24:35 -0700990sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
991 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700992{
Mathias Agopian3330b202009-10-05 17:07:12 -0700993 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700994 sp<Layer> owner(getOwner());
995 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700996 /*
997 * requestBuffer() cannot be called from the main thread
998 * as it could cause a dead-lock, since it may have to wait
999 * on conditions updated my the main thread.
1000 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001001 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001002 }
1003 return buffer;
1004}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001006status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1007{
1008 status_t err = DEAD_OBJECT;
1009 sp<Layer> owner(getOwner());
1010 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001011 /*
1012 * setBufferCount() cannot be called from the main thread
1013 * as it could cause a dead-lock, since it may have to wait
1014 * on conditions updated my the main thread.
1015 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001016 err = owner->setBufferCount(bufferCount);
1017 }
1018 return err;
1019}
1020
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021// ---------------------------------------------------------------------------
1022
1023
1024}; // namespace android