blob: fde8e677dfcf21c401e3b9fe084dc9fbeffb41a8 [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
Mathias Agopian3cc26772011-01-28 18:23:24 -0800155 // Free our own reference to ISurface
Mathias Agopianbb641242010-05-18 17:06:55 -0700156 mSurface.clear();
157
158 Mutex::Autolock _l(mLock);
159 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700160 return NO_ERROR;
161}
162
Mathias Agopianf9d93272009-06-19 17:00:27 -0700163status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 PixelFormat format, uint32_t flags)
165{
Mathias Agopian401c2572009-09-23 19:16:27 -0700166 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 PixelFormatInfo info;
168 status_t err = getPixelFormatInfo(format, &info);
169 if (err) return err;
170
Mathias Agopian401c2572009-09-23 19:16:27 -0700171 // the display's pixel format
172 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700173 uint32_t const maxSurfaceDims = min(
174 hw.getMaxTextureSize(), hw.getMaxViewportDims());
175
176 // never allow a surface larger than what our underlying GL implementation
177 // can handle.
178 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
179 return BAD_VALUE;
180 }
181
Mathias Agopian401c2572009-09-23 19:16:27 -0700182 PixelFormatInfo displayInfo;
183 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700184 const uint32_t hwFlags = hw.getFlags();
185
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700186 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700187 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700188 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700189
190 mReqFormat = format;
191 mReqWidth = w;
192 mReqHeight = h;
193
Mathias Agopian3330b202009-10-05 17:07:12 -0700194 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800195 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
196 mProtectedByDRM = (flags & ISurfaceComposer::eProtectedByDRM) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700197 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
198 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700199
Mathias Agopian401c2572009-09-23 19:16:27 -0700200 // we use the red index
201 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
202 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
203 mNeedsDithering = layerRedsize > displayRedSize;
204
Mathias Agopian96f08192010-06-02 23:28:45 -0700205 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 return NO_ERROR;
207}
208
Mathias Agopiana350ff92010-08-10 17:14:02 -0700209void Layer::setGeometry(hwc_layer_t* hwcl)
210{
211 hwcl->compositionType = HWC_FRAMEBUFFER;
212 hwcl->hints = 0;
213 hwcl->flags = 0;
214 hwcl->transform = 0;
215 hwcl->blending = HWC_BLENDING_NONE;
216
217 // we can't do alpha-fade with the hwc HAL
218 const State& s(drawingState());
219 if (s.alpha < 0xFF) {
220 hwcl->flags = HWC_SKIP_LAYER;
221 return;
222 }
223
224 // we can only handle simple transformation
225 if (mOrientation & Transform::ROT_INVALID) {
226 hwcl->flags = HWC_SKIP_LAYER;
227 return;
228 }
229
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800230 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
231 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700232
233 if (needsBlending()) {
234 hwcl->blending = mPremultipliedAlpha ?
235 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
236 }
237
238 hwcl->displayFrame.left = mTransformedBounds.left;
239 hwcl->displayFrame.top = mTransformedBounds.top;
240 hwcl->displayFrame.right = mTransformedBounds.right;
241 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
242
243 hwcl->visibleRegionScreen.rects =
244 reinterpret_cast<hwc_rect_t const *>(
245 visibleRegionScreen.getArray(
246 &hwcl->visibleRegionScreen.numRects));
247}
248
249void Layer::setPerFrameData(hwc_layer_t* hwcl) {
250 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
251 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800252 // this can happen if the client never drew into this layer yet,
253 // or if we ran out of memory. In that case, don't let
254 // HWC handle it.
255 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700256 hwcl->handle = NULL;
257 return;
258 }
Louis Huemiller04048142010-12-01 12:29:36 -0800259 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800260
261 if (!mBufferCrop.isEmpty()) {
262 hwcl->sourceCrop.left = mBufferCrop.left;
263 hwcl->sourceCrop.top = mBufferCrop.top;
264 hwcl->sourceCrop.right = mBufferCrop.right;
265 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
266 } else {
267 hwcl->sourceCrop.left = 0;
268 hwcl->sourceCrop.top = 0;
269 hwcl->sourceCrop.right = buffer->width;
270 hwcl->sourceCrop.bottom = buffer->height;
271 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700272}
273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274void Layer::reloadTexture(const Region& dirty)
275{
Mathias Agopiand606de62010-05-10 20:06:11 -0700276 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800277 if (buffer == NULL) {
278 // this situation can happen if we ran out of memory for instance.
279 // not much we can do. continue to use whatever texture was bound
280 // to this context.
281 return;
282 }
283
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700284 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700285 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
286 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
287 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700288 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700290 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800291slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700292 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700293 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
294 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
295 LOGE_IF(res, "error %d (%s) locking buffer %p",
296 res, strerror(res), buffer.get());
297 if (res == NO_ERROR) {
298 mBufferManager.loadTexture(dirty, t);
299 buffer->unlock();
300 }
301 } else {
302 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305}
306
Mathias Agopian74c40c02010-09-29 13:02:36 -0700307void Layer::drawForSreenShot() const
308{
Mathias Agopian733189d2010-12-02 21:32:29 -0800309 const bool currentFiltering = mNeedsFiltering;
310 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700311 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800312 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700313}
314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315void Layer::onDraw(const Region& clip) const
316{
Mathias Agopiand606de62010-05-10 20:06:11 -0700317 Texture tex(mBufferManager.getActiveTexture());
318 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700320 // in fact never been drawn into. This happens frequently with
321 // SurfaceView because the WindowManager can't know when the client
322 // has drawn the first time.
323
324 // If there is nothing under us, we paint the screen in black, otherwise
325 // we just skip this update.
326
327 // figure out if there is something below us
328 Region under;
329 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
330 const size_t count = drawingLayers.size();
331 for (size_t i=0 ; i<count ; ++i) {
332 const sp<LayerBase>& layer(drawingLayers[i]);
333 if (layer.get() == static_cast<LayerBase const*>(this))
334 break;
335 under.orSelf(layer->visibleRegionScreen);
336 }
337 // if not everything below us is covered, we plug the holes!
338 Region holes(clip.subtract(under));
339 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700340 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700341 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 return;
343 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700344 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345}
346
Mathias Agopiana7f66922010-05-26 22:08:52 -0700347bool Layer::needsFiltering() const
348{
349 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800350 // if our buffer is not the same size than ourselves,
351 // we need filtering.
352 Mutex::Autolock _l(mLock);
353 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700354 return true;
355 }
356 return LayerBase::needsFiltering();
357}
358
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700359
360status_t Layer::setBufferCount(int bufferCount)
361{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700362 ClientRef::Access sharedClient(mUserClientRef);
363 SharedBufferServer* lcblk(sharedClient.get());
364 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700365 // oops, the client is already gone
366 return DEAD_OBJECT;
367 }
368
Mathias Agopianbb641242010-05-18 17:06:55 -0700369 // NOTE: lcblk->resize() is protected by an internal lock
370 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700371 if (err == NO_ERROR) {
372 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
373 mBufferManager.resize(bufferCount, mFlinger, dpy);
374 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700375
376 return err;
377}
378
Mathias Agopiana138f892010-05-21 17:24:35 -0700379sp<GraphicBuffer> Layer::requestBuffer(int index,
380 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
381 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382{
Mathias Agopian3330b202009-10-05 17:07:12 -0700383 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700384
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700385 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700386 return buffer;
387
388 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
389 return buffer;
390
Mathias Agopian48d819a2009-09-10 19:41:18 -0700391 // this ensures our client doesn't go away while we're accessing
392 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700393 ClientRef::Access sharedClient(mUserClientRef);
394 SharedBufferServer* lcblk(sharedClient.get());
395 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700396 // oops, the client is already gone
397 return buffer;
398 }
399
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700400 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700401 * This is called from the client's Surface::dequeue(). This can happen
402 * at any time, especially while we're in the middle of using the
403 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700404 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405
Mathias Agopian208cb072010-07-27 20:11:35 -0700406 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700407 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700408 { // scope for the lock
409 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700410
411 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700412 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700413 if (!reqFormat) reqFormat = mFormat;
414 if (!reqWidth) reqWidth = mWidth;
415 if (!reqHeight) reqHeight = mHeight;
416
417 w = reqWidth;
418 h = reqHeight;
419 f = reqFormat;
420
421 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
422 (reqFormat != mReqFormat)) {
423 mReqWidth = reqWidth;
424 mReqHeight = reqHeight;
425 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700426 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800427 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700428
Mathias Agopiana138f892010-05-21 17:24:35 -0700429 lcblk->reallocateAllExcept(index);
430 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700431 }
432
Mathias Agopian208cb072010-07-27 20:11:35 -0700433 // here we have to reallocate a new buffer because the buffer could be
434 // used as the front buffer, or by a client in our process
435 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700436 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700437 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
438 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700439
440 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800441 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700442 LOGE_IF(err || buffer->handle == 0,
443 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
444 this, index, w, h, strerror(-err));
445 } else {
446 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700447 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
448 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700449 }
450
451 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700452 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700453 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700454 }
455 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456}
457
Mathias Agopian3330b202009-10-05 17:07:12 -0700458uint32_t Layer::getEffectiveUsage(uint32_t usage) const
459{
460 /*
461 * buffers used for software rendering, but h/w composition
462 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
463 *
464 * buffers used for h/w rendering and h/w composition
465 * are allocated with HW_RENDER | HW_TEXTURE
466 *
467 * buffers used with h/w rendering and either NPOT or no egl_image_ext
468 * are allocated with SW_READ_RARELY | HW_RENDER
469 *
470 */
471
472 if (mSecure) {
473 // secure buffer, don't store it into the GPU
474 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
475 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
476 } else {
477 // it's allowed to modify the usage flags here, but generally
478 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700479 // request EGLImage for all buffers
480 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700481 }
Glenn Kasten16f04532011-01-19 15:27:27 -0800482 if (mProtectedByApp || mProtectedByDRM) {
483 // need a hardware-protected path to external video sink
484 usage |= GraphicBuffer::USAGE_PROTECTED;
485 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700486 return usage;
487}
488
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489uint32_t Layer::doTransaction(uint32_t flags)
490{
491 const Layer::State& front(drawingState());
492 const Layer::State& temp(currentState());
493
Mathias Agopiana138f892010-05-21 17:24:35 -0700494 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
495 (front.requested_h != temp.requested_h);
496
497 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700498 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700500 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
501 this,
502 int(temp.requested_w), int(temp.requested_h),
503 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504
Mathias Agopiana138f892010-05-21 17:24:35 -0700505 if (!isFixedSize()) {
506 // we're being resized and there is a freeze display request,
507 // acquire a freeze lock, so that the screen stays put
508 // until we've redrawn at the new size; this is to avoid
509 // glitches upon orientation changes.
510 if (mFlinger->hasFreezeRequest()) {
511 // if the surface is hidden, don't try to acquire the
512 // freeze lock, since hidden surfaces may never redraw
513 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
514 mFreezeLock = mFlinger->getFreezeLock();
515 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700517
518 // this will make sure LayerBase::doTransaction doesn't update
519 // the drawing state's size
520 Layer::State& editDraw(mDrawingState);
521 editDraw.requested_w = temp.requested_w;
522 editDraw.requested_h = temp.requested_h;
523
524 // record the new size, form this point on, when the client request
525 // a buffer, it'll get the new size.
526 setBufferSize(temp.requested_w, temp.requested_h);
527
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700528 ClientRef::Access sharedClient(mUserClientRef);
529 SharedBufferServer* lcblk(sharedClient.get());
530 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700531 // all buffers need reallocation
532 lcblk->reallocateAll();
533 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700534 } else {
535 // record the new size
536 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700539
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 if (temp.sequence != front.sequence) {
541 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
542 // this surface is now hidden, so it shouldn't hold a freeze lock
543 // (it may never redraw, which is fine if it is hidden)
544 mFreezeLock.clear();
545 }
546 }
547
548 return LayerBase::doTransaction(flags);
549}
550
Mathias Agopiana138f892010-05-21 17:24:35 -0700551void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700552 Mutex::Autolock _l(mLock);
553 mWidth = w;
554 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800555 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556}
557
Mathias Agopiana138f892010-05-21 17:24:35 -0700558bool Layer::isFixedSize() const {
559 Mutex::Autolock _l(mLock);
560 return mFixedSize;
561}
562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563// ----------------------------------------------------------------------------
564// pageflip handling...
565// ----------------------------------------------------------------------------
566
567void Layer::lockPageFlip(bool& recomputeVisibleRegions)
568{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700569 ClientRef::Access sharedClient(mUserClientRef);
570 SharedBufferServer* lcblk(sharedClient.get());
571 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700572 // client died
573 recomputeVisibleRegions = true;
574 return;
575 }
576
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700577 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700578 if (buf == NOT_ENOUGH_DATA) {
579 // NOTE: This is not an error, it simply means there is nothing to
580 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700581 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 return;
583 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700584
Mathias Agopiand606de62010-05-10 20:06:11 -0700585 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700586 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700587 mPostedDirtyRegion.clear();
588 return;
589 }
590
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700591 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800592
593 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Mathias Agopiand606de62010-05-10 20:06:11 -0700594 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700595 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700596 mPostedDirtyRegion.clear();
597 return;
598 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599
Mathias Agopianda9584d2010-12-13 18:51:59 -0800600 if (noActiveBuffer) {
601 // we didn't have an active buffer, we need to recompute
602 // our visible region
603 recomputeVisibleRegions = true;
604 }
605
Mathias Agopian3330b202009-10-05 17:07:12 -0700606 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700607 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700608 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700609 // compute the posted region
610 const Region dirty(lcblk->getDirtyRegion(buf));
611 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700613 // update the layer size and release freeze-lock
614 const Layer::State& front(drawingState());
615 if (newFrontBuffer->getWidth() == front.requested_w &&
616 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700617 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700618 if ((front.w != front.requested_w) ||
619 (front.h != front.requested_h))
620 {
621 // Here we pretend the transaction happened by updating the
622 // current and drawing states. Drawing state is only accessed
623 // in this thread, no need to have it locked
624 Layer::State& editDraw(mDrawingState);
625 editDraw.w = editDraw.requested_w;
626 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700627
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700628 // We also need to update the current state so that we don't
629 // end-up doing too much work during the next transaction.
630 // NOTE: We actually don't need hold the transaction lock here
631 // because State::w and State::h are only accessed from
632 // this thread
633 Layer::State& editTemp(currentState());
634 editTemp.w = editDraw.w;
635 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700636
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700637 // recompute visible region
638 recomputeVisibleRegions = true;
639 }
640
641 // we now have the correct size, unfreeze the screen
642 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700643 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700644
645 // get the crop region
646 setBufferCrop( lcblk->getCrop(buf) );
647
648 // get the transformation
649 setBufferTransform( lcblk->getTransform(buf) );
650
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700651 } else {
652 // this should not happen unless we ran out of memory while
653 // allocating the buffer. we're hoping that things will get back
654 // to normal the next time the app tries to draw into this buffer.
655 // meanwhile, pretend the screen didn't update.
656 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700657 }
658
Mathias Agopiane7005012009-10-07 16:44:10 -0700659 if (lcblk->getQueuedCount()) {
660 // signal an event if we have more buffers waiting
661 mFlinger->signalEvent();
662 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
Mathias Agopian245e4d72010-04-21 15:24:11 -0700664 /* a buffer was posted, so we need to call reloadTexture(), which
665 * will update our internal data structures (eg: EGLImageKHR or
666 * texture names). we need to do this even if mPostedDirtyRegion is
667 * empty -- it's orthogonal to the fact that a new buffer was posted,
668 * for instance, a degenerate case could be that the user did an empty
669 * update but repainted the buffer with appropriate content (after a
670 * resize for instance).
671 */
672 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673}
674
675void Layer::unlockPageFlip(
676 const Transform& planeTransform, Region& outDirtyRegion)
677{
678 Region dirtyRegion(mPostedDirtyRegion);
679 if (!dirtyRegion.isEmpty()) {
680 mPostedDirtyRegion.clear();
681 // The dirty region is given in the layer's coordinate space
682 // transform the dirty region by the surface's transformation
683 // and the global transformation.
684 const Layer::State& s(drawingState());
685 const Transform tr(planeTransform * s.transform);
686 dirtyRegion = tr.transform(dirtyRegion);
687
688 // At this point, the dirty region is in screen space.
689 // Make sure it's constrained by the visible region (which
690 // is in screen space as well).
691 dirtyRegion.andSelf(visibleRegionScreen);
692 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800694 if (visibleRegionScreen.isEmpty()) {
695 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700696 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800697 mFreezeLock.clear();
698 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699}
700
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700701void Layer::dump(String8& result, char* buffer, size_t SIZE) const
702{
703 LayerBaseClient::dump(result, buffer, SIZE);
704
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700705 ClientRef::Access sharedClient(mUserClientRef);
706 SharedBufferServer* lcblk(sharedClient.get());
707 uint32_t totalTime = 0;
708 if (lcblk) {
709 SharedBufferStack::Statistics stats = lcblk->getStats();
710 totalTime= stats.totalTime;
711 result.append( lcblk->dump(" ") );
712 }
713
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700714 sp<const GraphicBuffer> buf0(getBuffer(0));
715 sp<const GraphicBuffer> buf1(getBuffer(1));
716 uint32_t w0=0, h0=0, s0=0;
717 uint32_t w1=0, h1=0, s1=0;
718 if (buf0 != 0) {
719 w0 = buf0->getWidth();
720 h0 = buf0->getHeight();
721 s0 = buf0->getStride();
722 }
723 if (buf1 != 0) {
724 w1 = buf1->getWidth();
725 h1 = buf1->getHeight();
726 s1 = buf1->getStride();
727 }
728 snprintf(buffer, SIZE,
729 " "
730 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
731 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700732 mFormat, w0, h0, s0, w1, h1, s1,
733 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700734
735 result.append(buffer);
736}
737
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700738// ---------------------------------------------------------------------------
739
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700740Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700741 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700742}
743
744Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700745}
746
747int32_t Layer::ClientRef::getToken() const {
748 Mutex::Autolock _l(mLock);
749 return mToken;
750}
751
Mathias Agopian579b3f82010-06-08 19:54:15 -0700752sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700753 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700754 return mUserClient.promote();
755}
756
757status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
758 const sp<SharedBufferServer>& sharedClient, int32_t token) {
759 Mutex::Autolock _l(mLock);
760
761 { // scope for strong mUserClient reference
762 sp<UserClient> userClient(mUserClient.promote());
763 if (mUserClient != 0 && mControlBlock != 0) {
764 mControlBlock->setStatus(NO_INIT);
765 }
766 }
767
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700768 mUserClient = uc;
769 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700770 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700771 return NO_ERROR;
772}
773
774sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
775 return mUserClient.promote();
776}
777
778// this class gives us access to SharedBufferServer safely
779// it makes sure the UserClient (and its associated shared memory)
780// won't go away while we're accessing it.
781Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700782 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700783{
784 Mutex::Autolock _l(ref.mLock);
785 mUserClientStrongRef = ref.mUserClient.promote();
786 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700787 mControlBlock = ref.mControlBlock;
788}
789
790Layer::ClientRef::Access::~Access()
791{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700792}
793
794// ---------------------------------------------------------------------------
795
Mathias Agopiand606de62010-05-10 20:06:11 -0700796Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700797 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800798 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700799{
800}
801
Mathias Agopianbb641242010-05-18 17:06:55 -0700802Layer::BufferManager::~BufferManager()
803{
804}
805
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700806status_t Layer::BufferManager::resize(size_t size,
807 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700808{
809 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700810
811 if (size < mNumBuffers) {
812 // Move the active texture into slot 0
Mathias Agopian420a2832010-12-14 20:30:37 -0800813 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
814 mBufferData[mActiveBufferIndex] = mBufferData[0];
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700815 mBufferData[0] = activeBufferData;
Mathias Agopian420a2832010-12-14 20:30:37 -0800816 mActiveBufferIndex = 0;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700817
818 // Free the buffers that are no longer needed.
819 for (size_t i = size; i < mNumBuffers; i++) {
820 mBufferData[i].buffer = 0;
821
822 // Create a message to destroy the textures on SurfaceFlinger's GL
823 // thread.
824 class MessageDestroyTexture : public MessageBase {
825 Image mTexture;
826 EGLDisplay mDpy;
827 public:
828 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
829 : mTexture(texture), mDpy(dpy) { }
830 virtual bool handler() {
831 status_t err = Layer::BufferManager::destroyTexture(
832 &mTexture, mDpy);
833 LOGE_IF(err<0, "error destroying texture: %d (%s)",
834 mTexture.name, strerror(-err));
835 return true; // XXX: err == 0; ????
836 }
837 };
838
839 MessageDestroyTexture *msg = new MessageDestroyTexture(
840 mBufferData[i].texture, dpy);
841
842 // Don't allow this texture to be cleaned up by
843 // BufferManager::destroy.
844 mBufferData[i].texture.name = -1U;
845 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
846
847 // Post the message to the SurfaceFlinger object.
848 flinger->postMessageAsync(msg);
849 }
850 }
851
Mathias Agopianbb641242010-05-18 17:06:55 -0700852 mNumBuffers = size;
853 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700854}
855
856// only for debugging
857sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
858 return mBufferData[index].buffer;
859}
860
861status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800862 BufferData const * const buffers = mBufferData;
863 Mutex::Autolock _l(mLock);
864 mActiveBuffer = buffers[index].buffer;
865 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700866 return NO_ERROR;
867}
868
869size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800870 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700871}
872
873Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700874 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800875 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700876 res = mFailoverTexture;
877 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800878 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700879 }
880 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700881}
882
883sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800884 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700885}
886
Mathias Agopianda9584d2010-12-13 18:51:59 -0800887bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800888 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800889}
890
Mathias Agopiand606de62010-05-10 20:06:11 -0700891sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
892{
Mathias Agopianbb641242010-05-18 17:06:55 -0700893 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700894 sp<GraphicBuffer> buffer;
895 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700896 buffer = buffers[index].buffer;
897 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700898 return buffer;
899}
900
901status_t Layer::BufferManager::attachBuffer(size_t index,
902 const sp<GraphicBuffer>& buffer)
903{
Mathias Agopianbb641242010-05-18 17:06:55 -0700904 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700905 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700906 buffers[index].buffer = buffer;
907 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700908 return NO_ERROR;
909}
910
911status_t Layer::BufferManager::destroy(EGLDisplay dpy)
912{
Mathias Agopianbb641242010-05-18 17:06:55 -0700913 BufferData* const buffers = mBufferData;
914 size_t num;
915 { // scope for the lock
916 Mutex::Autolock _l(mLock);
917 num = mNumBuffers;
918 for (size_t i=0 ; i<num ; i++) {
919 buffers[i].buffer = 0;
920 }
921 }
922 for (size_t i=0 ; i<num ; i++) {
923 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700924 }
925 destroyTexture(&mFailoverTexture, dpy);
926 return NO_ERROR;
927}
928
929status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
930 const sp<GraphicBuffer>& buffer)
931{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700932 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800933 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700934 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700935 if (!mFailover) {
936 Image& texture(mBufferData[index].texture);
937 err = mTextureManager.initEglImage(&texture, dpy, buffer);
938 // if EGLImage fails, we switch to regular texture mode, and we
939 // free all resources associated with using EGLImages.
940 if (err == NO_ERROR) {
941 mFailover = false;
942 destroyTexture(&mFailoverTexture, dpy);
943 } else {
944 mFailover = true;
945 const size_t num = mNumBuffers;
946 for (size_t i=0 ; i<num ; i++) {
947 destroyTexture(&mBufferData[i].texture, dpy);
948 }
Andreas Hubere049a952010-06-25 09:25:19 -0700949 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700950 } else {
951 // we failed once, don't try again
952 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700953 }
954 }
955 return err;
956}
957
958status_t Layer::BufferManager::loadTexture(
959 const Region& dirty, const GGLSurface& t)
960{
961 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
962}
963
Mathias Agopianbb641242010-05-18 17:06:55 -0700964status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
965{
966 if (tex->name != -1U) {
967 glDeleteTextures(1, &tex->name);
968 tex->name = -1U;
969 }
970 if (tex->image != EGL_NO_IMAGE_KHR) {
971 eglDestroyImageKHR(dpy, tex->image);
972 tex->image = EGL_NO_IMAGE_KHR;
973 }
974 return NO_ERROR;
975}
976
Mathias Agopiand606de62010-05-10 20:06:11 -0700977// ---------------------------------------------------------------------------
978
Mathias Agopian9a112062009-04-17 19:36:26 -0700979Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700980 const sp<Layer>& owner)
981 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700982{
983}
984
985Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700986{
987}
988
Mathias Agopiana138f892010-05-21 17:24:35 -0700989sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
990 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700991{
Mathias Agopian3330b202009-10-05 17:07:12 -0700992 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700993 sp<Layer> owner(getOwner());
994 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700995 /*
996 * requestBuffer() cannot be called from the main thread
997 * as it could cause a dead-lock, since it may have to wait
998 * on conditions updated my the main thread.
999 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001000 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001001 }
1002 return buffer;
1003}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001005status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1006{
1007 status_t err = DEAD_OBJECT;
1008 sp<Layer> owner(getOwner());
1009 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001010 /*
1011 * setBufferCount() cannot be called from the main thread
1012 * as it could cause a dead-lock, since it may have to wait
1013 * on conditions updated my the main thread.
1014 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001015 err = owner->setBufferCount(bufferCount);
1016 }
1017 return err;
1018}
1019
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020// ---------------------------------------------------------------------------
1021
1022
1023}; // namespace android