blob: fde68f6e07f664a57e29d75024efd11b8ba686e2 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian3330b202009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070038#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40
41#define DEBUG_RESIZE 0
42
43
44namespace android {
45
Mathias Agopianca99fb82010-04-14 16:43:44 -070046template <typename T> inline T min(T a, T b) {
47 return a<b ? a : b;
48}
49
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050// ---------------------------------------------------------------------------
51
Mathias Agopian96f08192010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian401c2572009-09-23 19:16:27 -070056 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070057 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070058 mSecure(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070059 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070060 mBufferManager(mTextureManager),
Mathias Agopian733189d2010-12-02 21:32:29 -080061 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063}
64
65Layer::~Layer()
66{
Mathias Agopianbb641242010-05-18 17:06:55 -070067 // FIXME: must be called from the main UI thread
68 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
69 mBufferManager.destroy(dpy);
70
Mathias Agopianb7e930d2010-06-01 15:12:58 -070071 // we can use getUserClientUnsafe here because we know we're
72 // single-threaded at that point.
73 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
74 if (ourClient != 0) {
75 ourClient->detachLayer(this);
76 }
Mathias Agopiand606de62010-05-10 20:06:11 -070077}
78
Mathias Agopianb7e930d2010-06-01 15:12:58 -070079status_t Layer::setToken(const sp<UserClient>& userClient,
80 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070081{
Mathias Agopian579b3f82010-06-08 19:54:15 -070082 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070083 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070084 getIdentity());
85
Mathias Agopian579b3f82010-06-08 19:54:15 -070086
Mathias Agopiandd17b3e2010-12-13 16:49:05 -080087 sp<UserClient> ourClient(mUserClientRef.getClient());
88
89 /*
90 * Here it is guaranteed that userClient != ourClient
91 * (see UserClient::getTokenForSurface()).
92 *
93 * We release the token used by this surface in ourClient below.
94 * This should be safe to do so now, since this layer won't be attached
95 * to this client, it should be okay to reuse that id.
96 *
97 * If this causes problems, an other solution would be to keep a list
98 * of all the {UserClient, token} ever used and release them when the
99 * Layer is destroyed.
100 *
101 */
102
103 if (ourClient != 0) {
104 ourClient->detachLayer(this);
105 }
106
107 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700108 LOGE_IF(err != NO_ERROR,
109 "ClientRef::setToken(%p, %p, %u) failed",
110 userClient.get(), lcblk.get(), token);
111
112 if (err == NO_ERROR) {
113 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700114 }
115
116 return err;
117}
118
119int32_t Layer::getToken() const
120{
121 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700122}
123
Mathias Agopian579b3f82010-06-08 19:54:15 -0700124sp<UserClient> Layer::getClient() const
125{
126 return mUserClientRef.getClient();
127}
128
Mathias Agopiand606de62010-05-10 20:06:11 -0700129// called with SurfaceFlinger::mStateLock as soon as the layer is entered
130// in the purgatory list
131void Layer::onRemoved()
132{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700133 ClientRef::Access sharedClient(mUserClientRef);
134 SharedBufferServer* lcblk(sharedClient.get());
135 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700136 // wake up the condition
137 lcblk->setStatus(NO_INIT);
138 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700139}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700140
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142{
143 return mSurface;
144}
145
Mathias Agopian9a112062009-04-17 19:36:26 -0700146status_t Layer::ditch()
147{
Mathias Agopianbb641242010-05-18 17:06:55 -0700148 // NOTE: Called from the main UI thread
149
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700150 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800151 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700152
153 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
154 mBufferManager.destroy(dpy);
155 mSurface.clear();
156
157 Mutex::Autolock _l(mLock);
158 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700159 return NO_ERROR;
160}
161
Mathias Agopianf9d93272009-06-19 17:00:27 -0700162status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 PixelFormat format, uint32_t flags)
164{
Mathias Agopian401c2572009-09-23 19:16:27 -0700165 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 PixelFormatInfo info;
167 status_t err = getPixelFormatInfo(format, &info);
168 if (err) return err;
169
Mathias Agopian401c2572009-09-23 19:16:27 -0700170 // the display's pixel format
171 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700172 uint32_t const maxSurfaceDims = min(
173 hw.getMaxTextureSize(), hw.getMaxViewportDims());
174
175 // never allow a surface larger than what our underlying GL implementation
176 // can handle.
177 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
178 return BAD_VALUE;
179 }
180
Mathias Agopian401c2572009-09-23 19:16:27 -0700181 PixelFormatInfo displayInfo;
182 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700183 const uint32_t hwFlags = hw.getFlags();
184
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700185 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700186 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700187 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700188
189 mReqFormat = format;
190 mReqWidth = w;
191 mReqHeight = h;
192
Mathias Agopian3330b202009-10-05 17:07:12 -0700193 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700194 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
195 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700196
Mathias Agopian401c2572009-09-23 19:16:27 -0700197 // we use the red index
198 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
199 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
200 mNeedsDithering = layerRedsize > displayRedSize;
201
Mathias Agopian96f08192010-06-02 23:28:45 -0700202 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 return NO_ERROR;
204}
205
Mathias Agopiana350ff92010-08-10 17:14:02 -0700206void Layer::setGeometry(hwc_layer_t* hwcl)
207{
208 hwcl->compositionType = HWC_FRAMEBUFFER;
209 hwcl->hints = 0;
210 hwcl->flags = 0;
211 hwcl->transform = 0;
212 hwcl->blending = HWC_BLENDING_NONE;
213
214 // we can't do alpha-fade with the hwc HAL
215 const State& s(drawingState());
216 if (s.alpha < 0xFF) {
217 hwcl->flags = HWC_SKIP_LAYER;
218 return;
219 }
220
221 // we can only handle simple transformation
222 if (mOrientation & Transform::ROT_INVALID) {
223 hwcl->flags = HWC_SKIP_LAYER;
224 return;
225 }
226
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800227 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
228 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229
230 if (needsBlending()) {
231 hwcl->blending = mPremultipliedAlpha ?
232 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
233 }
234
235 hwcl->displayFrame.left = mTransformedBounds.left;
236 hwcl->displayFrame.top = mTransformedBounds.top;
237 hwcl->displayFrame.right = mTransformedBounds.right;
238 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
239
240 hwcl->visibleRegionScreen.rects =
241 reinterpret_cast<hwc_rect_t const *>(
242 visibleRegionScreen.getArray(
243 &hwcl->visibleRegionScreen.numRects));
244}
245
246void Layer::setPerFrameData(hwc_layer_t* hwcl) {
247 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
248 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800249 // this can happen if the client never drew into this layer yet,
250 // or if we ran out of memory. In that case, don't let
251 // HWC handle it.
252 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253 hwcl->handle = NULL;
254 return;
255 }
Louis Huemiller04048142010-12-01 12:29:36 -0800256 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800257
258 if (!mBufferCrop.isEmpty()) {
259 hwcl->sourceCrop.left = mBufferCrop.left;
260 hwcl->sourceCrop.top = mBufferCrop.top;
261 hwcl->sourceCrop.right = mBufferCrop.right;
262 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
263 } else {
264 hwcl->sourceCrop.left = 0;
265 hwcl->sourceCrop.top = 0;
266 hwcl->sourceCrop.right = buffer->width;
267 hwcl->sourceCrop.bottom = buffer->height;
268 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700269}
270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271void Layer::reloadTexture(const Region& dirty)
272{
Mathias Agopiand606de62010-05-10 20:06:11 -0700273 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800274 if (buffer == NULL) {
275 // this situation can happen if we ran out of memory for instance.
276 // not much we can do. continue to use whatever texture was bound
277 // to this context.
278 return;
279 }
280
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700281 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700282 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
283 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
284 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700285 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700287 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800288slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700290 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
291 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
292 LOGE_IF(res, "error %d (%s) locking buffer %p",
293 res, strerror(res), buffer.get());
294 if (res == NO_ERROR) {
295 mBufferManager.loadTexture(dirty, t);
296 buffer->unlock();
297 }
298 } else {
299 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700300 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302}
303
Mathias Agopian74c40c02010-09-29 13:02:36 -0700304void Layer::drawForSreenShot() const
305{
Mathias Agopian733189d2010-12-02 21:32:29 -0800306 const bool currentFiltering = mNeedsFiltering;
307 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700308 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800309 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700310}
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312void Layer::onDraw(const Region& clip) const
313{
Mathias Agopiand606de62010-05-10 20:06:11 -0700314 Texture tex(mBufferManager.getActiveTexture());
315 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700317 // in fact never been drawn into. This happens frequently with
318 // SurfaceView because the WindowManager can't know when the client
319 // has drawn the first time.
320
321 // If there is nothing under us, we paint the screen in black, otherwise
322 // we just skip this update.
323
324 // figure out if there is something below us
325 Region under;
326 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
327 const size_t count = drawingLayers.size();
328 for (size_t i=0 ; i<count ; ++i) {
329 const sp<LayerBase>& layer(drawingLayers[i]);
330 if (layer.get() == static_cast<LayerBase const*>(this))
331 break;
332 under.orSelf(layer->visibleRegionScreen);
333 }
334 // if not everything below us is covered, we plug the holes!
335 Region holes(clip.subtract(under));
336 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700337 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700338 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 return;
340 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700341 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342}
343
Mathias Agopiana7f66922010-05-26 22:08:52 -0700344bool Layer::needsFiltering() const
345{
346 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800347 // if our buffer is not the same size than ourselves,
348 // we need filtering.
349 Mutex::Autolock _l(mLock);
350 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700351 return true;
352 }
353 return LayerBase::needsFiltering();
354}
355
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700356
357status_t Layer::setBufferCount(int bufferCount)
358{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700359 ClientRef::Access sharedClient(mUserClientRef);
360 SharedBufferServer* lcblk(sharedClient.get());
361 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700362 // oops, the client is already gone
363 return DEAD_OBJECT;
364 }
365
Mathias Agopianbb641242010-05-18 17:06:55 -0700366 // NOTE: lcblk->resize() is protected by an internal lock
367 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700368 if (err == NO_ERROR) {
369 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
370 mBufferManager.resize(bufferCount, mFlinger, dpy);
371 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700372
373 return err;
374}
375
Mathias Agopiana138f892010-05-21 17:24:35 -0700376sp<GraphicBuffer> Layer::requestBuffer(int index,
377 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
378 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379{
Mathias Agopian3330b202009-10-05 17:07:12 -0700380 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700381
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700382 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700383 return buffer;
384
385 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
386 return buffer;
387
Mathias Agopian48d819a2009-09-10 19:41:18 -0700388 // this ensures our client doesn't go away while we're accessing
389 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700390 ClientRef::Access sharedClient(mUserClientRef);
391 SharedBufferServer* lcblk(sharedClient.get());
392 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700393 // oops, the client is already gone
394 return buffer;
395 }
396
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700397 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398 * This is called from the client's Surface::dequeue(). This can happen
399 * at any time, especially while we're in the middle of using the
400 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700401 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402
Mathias Agopian208cb072010-07-27 20:11:35 -0700403 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700404 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700405 { // scope for the lock
406 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700407
408 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700409 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700410 if (!reqFormat) reqFormat = mFormat;
411 if (!reqWidth) reqWidth = mWidth;
412 if (!reqHeight) reqHeight = mHeight;
413
414 w = reqWidth;
415 h = reqHeight;
416 f = reqFormat;
417
418 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
419 (reqFormat != mReqFormat)) {
420 mReqWidth = reqWidth;
421 mReqHeight = reqHeight;
422 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700423 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800424 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700425
Mathias Agopiana138f892010-05-21 17:24:35 -0700426 lcblk->reallocateAllExcept(index);
427 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700428 }
429
Mathias Agopian208cb072010-07-27 20:11:35 -0700430 // here we have to reallocate a new buffer because the buffer could be
431 // used as the front buffer, or by a client in our process
432 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700433 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700434 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
435 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700436
437 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800438 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700439 LOGE_IF(err || buffer->handle == 0,
440 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
441 this, index, w, h, strerror(-err));
442 } else {
443 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700444 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
445 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700446 }
447
448 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700449 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700450 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700451 }
452 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
Mathias Agopian3330b202009-10-05 17:07:12 -0700455uint32_t Layer::getEffectiveUsage(uint32_t usage) const
456{
457 /*
458 * buffers used for software rendering, but h/w composition
459 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
460 *
461 * buffers used for h/w rendering and h/w composition
462 * are allocated with HW_RENDER | HW_TEXTURE
463 *
464 * buffers used with h/w rendering and either NPOT or no egl_image_ext
465 * are allocated with SW_READ_RARELY | HW_RENDER
466 *
467 */
468
469 if (mSecure) {
470 // secure buffer, don't store it into the GPU
471 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
472 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
473 } else {
474 // it's allowed to modify the usage flags here, but generally
475 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700476 // request EGLImage for all buffers
477 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700478 }
479 return usage;
480}
481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482uint32_t Layer::doTransaction(uint32_t flags)
483{
484 const Layer::State& front(drawingState());
485 const Layer::State& temp(currentState());
486
Mathias Agopiana138f892010-05-21 17:24:35 -0700487 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
488 (front.requested_h != temp.requested_h);
489
490 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700491 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700493 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
494 this,
495 int(temp.requested_w), int(temp.requested_h),
496 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497
Mathias Agopiana138f892010-05-21 17:24:35 -0700498 if (!isFixedSize()) {
499 // we're being resized and there is a freeze display request,
500 // acquire a freeze lock, so that the screen stays put
501 // until we've redrawn at the new size; this is to avoid
502 // glitches upon orientation changes.
503 if (mFlinger->hasFreezeRequest()) {
504 // if the surface is hidden, don't try to acquire the
505 // freeze lock, since hidden surfaces may never redraw
506 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
507 mFreezeLock = mFlinger->getFreezeLock();
508 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700510
511 // this will make sure LayerBase::doTransaction doesn't update
512 // the drawing state's size
513 Layer::State& editDraw(mDrawingState);
514 editDraw.requested_w = temp.requested_w;
515 editDraw.requested_h = temp.requested_h;
516
517 // record the new size, form this point on, when the client request
518 // a buffer, it'll get the new size.
519 setBufferSize(temp.requested_w, temp.requested_h);
520
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700521 ClientRef::Access sharedClient(mUserClientRef);
522 SharedBufferServer* lcblk(sharedClient.get());
523 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700524 // all buffers need reallocation
525 lcblk->reallocateAll();
526 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700527 } else {
528 // record the new size
529 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530 }
531 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 if (temp.sequence != front.sequence) {
534 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
535 // this surface is now hidden, so it shouldn't hold a freeze lock
536 // (it may never redraw, which is fine if it is hidden)
537 mFreezeLock.clear();
538 }
539 }
540
541 return LayerBase::doTransaction(flags);
542}
543
Mathias Agopiana138f892010-05-21 17:24:35 -0700544void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700545 Mutex::Autolock _l(mLock);
546 mWidth = w;
547 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800548 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549}
550
Mathias Agopiana138f892010-05-21 17:24:35 -0700551bool Layer::isFixedSize() const {
552 Mutex::Autolock _l(mLock);
553 return mFixedSize;
554}
555
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556// ----------------------------------------------------------------------------
557// pageflip handling...
558// ----------------------------------------------------------------------------
559
560void Layer::lockPageFlip(bool& recomputeVisibleRegions)
561{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700562 ClientRef::Access sharedClient(mUserClientRef);
563 SharedBufferServer* lcblk(sharedClient.get());
564 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700565 // client died
566 recomputeVisibleRegions = true;
567 return;
568 }
569
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700570 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700571 if (buf == NOT_ENOUGH_DATA) {
572 // NOTE: This is not an error, it simply means there is nothing to
573 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700574 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 return;
576 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700577
Mathias Agopiand606de62010-05-10 20:06:11 -0700578 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700579 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700580 mPostedDirtyRegion.clear();
581 return;
582 }
583
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700584 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800585
586 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Mathias Agopiand606de62010-05-10 20:06:11 -0700587 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700588 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700589 mPostedDirtyRegion.clear();
590 return;
591 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592
Mathias Agopianda9584d2010-12-13 18:51:59 -0800593 if (noActiveBuffer) {
594 // we didn't have an active buffer, we need to recompute
595 // our visible region
596 recomputeVisibleRegions = true;
597 }
598
Mathias Agopian3330b202009-10-05 17:07:12 -0700599 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700600 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700601 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700602 // compute the posted region
603 const Region dirty(lcblk->getDirtyRegion(buf));
604 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700606 // update the layer size and release freeze-lock
607 const Layer::State& front(drawingState());
608 if (newFrontBuffer->getWidth() == front.requested_w &&
609 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700610 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700611 if ((front.w != front.requested_w) ||
612 (front.h != front.requested_h))
613 {
614 // Here we pretend the transaction happened by updating the
615 // current and drawing states. Drawing state is only accessed
616 // in this thread, no need to have it locked
617 Layer::State& editDraw(mDrawingState);
618 editDraw.w = editDraw.requested_w;
619 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700620
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700621 // We also need to update the current state so that we don't
622 // end-up doing too much work during the next transaction.
623 // NOTE: We actually don't need hold the transaction lock here
624 // because State::w and State::h are only accessed from
625 // this thread
626 Layer::State& editTemp(currentState());
627 editTemp.w = editDraw.w;
628 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700629
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700630 // recompute visible region
631 recomputeVisibleRegions = true;
632 }
633
634 // we now have the correct size, unfreeze the screen
635 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700636 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700637
638 // get the crop region
639 setBufferCrop( lcblk->getCrop(buf) );
640
641 // get the transformation
642 setBufferTransform( lcblk->getTransform(buf) );
643
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700644 } else {
645 // this should not happen unless we ran out of memory while
646 // allocating the buffer. we're hoping that things will get back
647 // to normal the next time the app tries to draw into this buffer.
648 // meanwhile, pretend the screen didn't update.
649 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700650 }
651
Mathias Agopiane7005012009-10-07 16:44:10 -0700652 if (lcblk->getQueuedCount()) {
653 // signal an event if we have more buffers waiting
654 mFlinger->signalEvent();
655 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656
Mathias Agopian245e4d72010-04-21 15:24:11 -0700657 /* a buffer was posted, so we need to call reloadTexture(), which
658 * will update our internal data structures (eg: EGLImageKHR or
659 * texture names). we need to do this even if mPostedDirtyRegion is
660 * empty -- it's orthogonal to the fact that a new buffer was posted,
661 * for instance, a degenerate case could be that the user did an empty
662 * update but repainted the buffer with appropriate content (after a
663 * resize for instance).
664 */
665 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666}
667
668void Layer::unlockPageFlip(
669 const Transform& planeTransform, Region& outDirtyRegion)
670{
671 Region dirtyRegion(mPostedDirtyRegion);
672 if (!dirtyRegion.isEmpty()) {
673 mPostedDirtyRegion.clear();
674 // The dirty region is given in the layer's coordinate space
675 // transform the dirty region by the surface's transformation
676 // and the global transformation.
677 const Layer::State& s(drawingState());
678 const Transform tr(planeTransform * s.transform);
679 dirtyRegion = tr.transform(dirtyRegion);
680
681 // At this point, the dirty region is in screen space.
682 // Make sure it's constrained by the visible region (which
683 // is in screen space as well).
684 dirtyRegion.andSelf(visibleRegionScreen);
685 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800687 if (visibleRegionScreen.isEmpty()) {
688 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700689 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800690 mFreezeLock.clear();
691 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692}
693
694void Layer::finishPageFlip()
695{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700696 ClientRef::Access sharedClient(mUserClientRef);
697 SharedBufferServer* lcblk(sharedClient.get());
698 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700699 int buf = mBufferManager.getActiveBufferIndex();
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700700 if (buf >= 0) {
701 status_t err = lcblk->unlock( buf );
702 LOGE_IF(err!=NO_ERROR,
703 "layer %p, buffer=%d wasn't locked!",
704 this, buf);
705 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700706 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707}
708
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700709
710void Layer::dump(String8& result, char* buffer, size_t SIZE) const
711{
712 LayerBaseClient::dump(result, buffer, SIZE);
713
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700714 ClientRef::Access sharedClient(mUserClientRef);
715 SharedBufferServer* lcblk(sharedClient.get());
716 uint32_t totalTime = 0;
717 if (lcblk) {
718 SharedBufferStack::Statistics stats = lcblk->getStats();
719 totalTime= stats.totalTime;
720 result.append( lcblk->dump(" ") );
721 }
722
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700723 sp<const GraphicBuffer> buf0(getBuffer(0));
724 sp<const GraphicBuffer> buf1(getBuffer(1));
725 uint32_t w0=0, h0=0, s0=0;
726 uint32_t w1=0, h1=0, s1=0;
727 if (buf0 != 0) {
728 w0 = buf0->getWidth();
729 h0 = buf0->getHeight();
730 s0 = buf0->getStride();
731 }
732 if (buf1 != 0) {
733 w1 = buf1->getWidth();
734 h1 = buf1->getHeight();
735 s1 = buf1->getStride();
736 }
737 snprintf(buffer, SIZE,
738 " "
739 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
740 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700741 mFormat, w0, h0, s0, w1, h1, s1,
742 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700743
744 result.append(buffer);
745}
746
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700747// ---------------------------------------------------------------------------
748
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700749Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700750 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700751}
752
753Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700754}
755
756int32_t Layer::ClientRef::getToken() const {
757 Mutex::Autolock _l(mLock);
758 return mToken;
759}
760
Mathias Agopian579b3f82010-06-08 19:54:15 -0700761sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700762 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700763 return mUserClient.promote();
764}
765
766status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
767 const sp<SharedBufferServer>& sharedClient, int32_t token) {
768 Mutex::Autolock _l(mLock);
769
770 { // scope for strong mUserClient reference
771 sp<UserClient> userClient(mUserClient.promote());
772 if (mUserClient != 0 && mControlBlock != 0) {
773 mControlBlock->setStatus(NO_INIT);
774 }
775 }
776
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700777 mUserClient = uc;
778 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700779 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700780 return NO_ERROR;
781}
782
783sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
784 return mUserClient.promote();
785}
786
787// this class gives us access to SharedBufferServer safely
788// it makes sure the UserClient (and its associated shared memory)
789// won't go away while we're accessing it.
790Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700791 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700792{
793 Mutex::Autolock _l(ref.mLock);
794 mUserClientStrongRef = ref.mUserClient.promote();
795 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700796 mControlBlock = ref.mControlBlock;
797}
798
799Layer::ClientRef::Access::~Access()
800{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700801}
802
803// ---------------------------------------------------------------------------
804
Mathias Agopiand606de62010-05-10 20:06:11 -0700805Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700806 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800807 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700808{
809}
810
Mathias Agopianbb641242010-05-18 17:06:55 -0700811Layer::BufferManager::~BufferManager()
812{
813}
814
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700815status_t Layer::BufferManager::resize(size_t size,
816 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700817{
818 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700819
820 if (size < mNumBuffers) {
821 // Move the active texture into slot 0
Mathias Agopian420a2832010-12-14 20:30:37 -0800822 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
823 mBufferData[mActiveBufferIndex] = mBufferData[0];
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700824 mBufferData[0] = activeBufferData;
Mathias Agopian420a2832010-12-14 20:30:37 -0800825 mActiveBufferIndex = 0;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700826
827 // Free the buffers that are no longer needed.
828 for (size_t i = size; i < mNumBuffers; i++) {
829 mBufferData[i].buffer = 0;
830
831 // Create a message to destroy the textures on SurfaceFlinger's GL
832 // thread.
833 class MessageDestroyTexture : public MessageBase {
834 Image mTexture;
835 EGLDisplay mDpy;
836 public:
837 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
838 : mTexture(texture), mDpy(dpy) { }
839 virtual bool handler() {
840 status_t err = Layer::BufferManager::destroyTexture(
841 &mTexture, mDpy);
842 LOGE_IF(err<0, "error destroying texture: %d (%s)",
843 mTexture.name, strerror(-err));
844 return true; // XXX: err == 0; ????
845 }
846 };
847
848 MessageDestroyTexture *msg = new MessageDestroyTexture(
849 mBufferData[i].texture, dpy);
850
851 // Don't allow this texture to be cleaned up by
852 // BufferManager::destroy.
853 mBufferData[i].texture.name = -1U;
854 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
855
856 // Post the message to the SurfaceFlinger object.
857 flinger->postMessageAsync(msg);
858 }
859 }
860
Mathias Agopianbb641242010-05-18 17:06:55 -0700861 mNumBuffers = size;
862 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700863}
864
865// only for debugging
866sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
867 return mBufferData[index].buffer;
868}
869
870status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800871 BufferData const * const buffers = mBufferData;
872 Mutex::Autolock _l(mLock);
873 mActiveBuffer = buffers[index].buffer;
874 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700875 return NO_ERROR;
876}
877
878size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800879 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700880}
881
882Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700883 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800884 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700885 res = mFailoverTexture;
886 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800887 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700888 }
889 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700890}
891
892sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800893 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700894}
895
Mathias Agopianda9584d2010-12-13 18:51:59 -0800896bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800897 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800898}
899
Mathias Agopiand606de62010-05-10 20:06:11 -0700900sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
901{
Mathias Agopianbb641242010-05-18 17:06:55 -0700902 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700903 sp<GraphicBuffer> buffer;
904 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700905 buffer = buffers[index].buffer;
906 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700907 return buffer;
908}
909
910status_t Layer::BufferManager::attachBuffer(size_t index,
911 const sp<GraphicBuffer>& buffer)
912{
Mathias Agopianbb641242010-05-18 17:06:55 -0700913 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700914 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700915 buffers[index].buffer = buffer;
916 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700917 return NO_ERROR;
918}
919
920status_t Layer::BufferManager::destroy(EGLDisplay dpy)
921{
Mathias Agopianbb641242010-05-18 17:06:55 -0700922 BufferData* const buffers = mBufferData;
923 size_t num;
924 { // scope for the lock
925 Mutex::Autolock _l(mLock);
926 num = mNumBuffers;
927 for (size_t i=0 ; i<num ; i++) {
928 buffers[i].buffer = 0;
929 }
930 }
931 for (size_t i=0 ; i<num ; i++) {
932 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700933 }
934 destroyTexture(&mFailoverTexture, dpy);
935 return NO_ERROR;
936}
937
938status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
939 const sp<GraphicBuffer>& buffer)
940{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700941 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800942 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700943 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700944 if (!mFailover) {
945 Image& texture(mBufferData[index].texture);
946 err = mTextureManager.initEglImage(&texture, dpy, buffer);
947 // if EGLImage fails, we switch to regular texture mode, and we
948 // free all resources associated with using EGLImages.
949 if (err == NO_ERROR) {
950 mFailover = false;
951 destroyTexture(&mFailoverTexture, dpy);
952 } else {
953 mFailover = true;
954 const size_t num = mNumBuffers;
955 for (size_t i=0 ; i<num ; i++) {
956 destroyTexture(&mBufferData[i].texture, dpy);
957 }
Andreas Hubere049a952010-06-25 09:25:19 -0700958 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700959 } else {
960 // we failed once, don't try again
961 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700962 }
963 }
964 return err;
965}
966
967status_t Layer::BufferManager::loadTexture(
968 const Region& dirty, const GGLSurface& t)
969{
970 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
971}
972
Mathias Agopianbb641242010-05-18 17:06:55 -0700973status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
974{
975 if (tex->name != -1U) {
976 glDeleteTextures(1, &tex->name);
977 tex->name = -1U;
978 }
979 if (tex->image != EGL_NO_IMAGE_KHR) {
980 eglDestroyImageKHR(dpy, tex->image);
981 tex->image = EGL_NO_IMAGE_KHR;
982 }
983 return NO_ERROR;
984}
985
Mathias Agopiand606de62010-05-10 20:06:11 -0700986// ---------------------------------------------------------------------------
987
Mathias Agopian9a112062009-04-17 19:36:26 -0700988Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700989 const sp<Layer>& owner)
990 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700991{
992}
993
994Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700995{
996}
997
Mathias Agopiana138f892010-05-21 17:24:35 -0700998sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
999 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001000{
Mathias Agopian3330b202009-10-05 17:07:12 -07001001 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001002 sp<Layer> owner(getOwner());
1003 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001004 /*
1005 * requestBuffer() cannot be called from the main thread
1006 * as it could cause a dead-lock, since it may have to wait
1007 * on conditions updated my the main thread.
1008 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001009 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001010 }
1011 return buffer;
1012}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001014status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1015{
1016 status_t err = DEAD_OBJECT;
1017 sp<Layer> owner(getOwner());
1018 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001019 /*
1020 * setBufferCount() cannot be called from the main thread
1021 * as it could cause a dead-lock, since it may have to wait
1022 * on conditions updated my the main thread.
1023 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001024 err = owner->setBufferCount(bufferCount);
1025 }
1026 return err;
1027}
1028
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029// ---------------------------------------------------------------------------
1030
1031
1032}; // namespace android