blob: b2f95cd2ac3625fa6560c499529d1923799526d1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian6950e422009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070038#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40
41#define DEBUG_RESIZE 0
42
43
44namespace android {
45
Mathias Agopian967dce32010-04-14 16:43:44 -070046template <typename T> inline T min(T a, T b) {
47 return a<b ? a : b;
48}
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050// ---------------------------------------------------------------------------
51
Mathias Agopian593c05c2010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopianccf94df2011-03-11 17:01:07 -080055 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian781953d2010-06-25 18:02:21 -070056 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiancc934762009-09-23 19:16:27 -070057 mNeedsBlending(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070058 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070059 mSecure(false),
Glenn Kastend6f5bde2011-01-19 15:27:27 -080060 mProtectedByApp(false),
Mathias Agopian781953d2010-06-25 18:02:21 -070061 mTextureManager(),
Mathias Agopian2be352a2010-05-21 17:24:35 -070062 mBufferManager(mTextureManager),
Mathias Agopianccf94df2011-03-11 17:01:07 -080063 mWidth(0), mHeight(0),
Eric Hassoldb6c0f512011-03-11 12:24:23 -080064 mNeedsScaling(false), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066}
67
68Layer::~Layer()
69{
Mathias Agopian898c4c92010-05-18 17:06:55 -070070 // FIXME: must be called from the main UI thread
71 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
72 mBufferManager.destroy(dpy);
73
Mathias Agopian7623da42010-06-01 15:12:58 -070074 // we can use getUserClientUnsafe here because we know we're
75 // single-threaded at that point.
76 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
77 if (ourClient != 0) {
78 ourClient->detachLayer(this);
79 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070080}
81
Mathias Agopian7623da42010-06-01 15:12:58 -070082status_t Layer::setToken(const sp<UserClient>& userClient,
83 SharedClient* sharedClient, int32_t token)
Mathias Agopian593c05c2010-06-02 23:28:45 -070084{
Mathias Agopian5e140102010-06-08 19:54:15 -070085 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopian7623da42010-06-01 15:12:58 -070086 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian593c05c2010-06-02 23:28:45 -070087 getIdentity());
88
Mathias Agopian5e140102010-06-08 19:54:15 -070089
Mathias Agopian5747bbc2010-12-13 16:49:05 -080090 sp<UserClient> ourClient(mUserClientRef.getClient());
91
92 /*
93 * Here it is guaranteed that userClient != ourClient
94 * (see UserClient::getTokenForSurface()).
95 *
96 * We release the token used by this surface in ourClient below.
97 * This should be safe to do so now, since this layer won't be attached
98 * to this client, it should be okay to reuse that id.
99 *
100 * If this causes problems, an other solution would be to keep a list
101 * of all the {UserClient, token} ever used and release them when the
102 * Layer is destroyed.
103 *
104 */
105
106 if (ourClient != 0) {
107 ourClient->detachLayer(this);
108 }
109
110 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian5e140102010-06-08 19:54:15 -0700111 LOGE_IF(err != NO_ERROR,
112 "ClientRef::setToken(%p, %p, %u) failed",
113 userClient.get(), lcblk.get(), token);
114
115 if (err == NO_ERROR) {
116 // we need to free the buffers associated with this surface
Mathias Agopian7623da42010-06-01 15:12:58 -0700117 }
118
119 return err;
120}
121
122int32_t Layer::getToken() const
123{
124 return mUserClientRef.getToken();
Mathias Agopian593c05c2010-06-02 23:28:45 -0700125}
126
Mathias Agopian5e140102010-06-08 19:54:15 -0700127sp<UserClient> Layer::getClient() const
128{
129 return mUserClientRef.getClient();
130}
131
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700132// called with SurfaceFlinger::mStateLock as soon as the layer is entered
133// in the purgatory list
134void Layer::onRemoved()
135{
Mathias Agopian7623da42010-06-01 15:12:58 -0700136 ClientRef::Access sharedClient(mUserClientRef);
137 SharedBufferServer* lcblk(sharedClient.get());
138 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700139 // wake up the condition
140 lcblk->setStatus(NO_INIT);
141 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700142}
Mathias Agopian9779b222009-09-07 16:32:45 -0700143
Mathias Agopian1473f462009-04-10 14:24:30 -0700144sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145{
Mathias Agopian1b0114f2011-02-15 19:01:06 -0800146 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
147 return sur;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148}
149
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700150status_t Layer::ditch()
151{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700152 // NOTE: Called from the main UI thread
153
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700154 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -0800155 mFreezeLock.clear();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700156
Mathias Agopian898c4c92010-05-18 17:06:55 -0700157 Mutex::Autolock _l(mLock);
158 mWidth = mHeight = 0;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700159 return NO_ERROR;
160}
161
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700162status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 PixelFormat format, uint32_t flags)
164{
Mathias Agopiancc934762009-09-23 19:16:27 -0700165 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 PixelFormatInfo info;
167 status_t err = getPixelFormatInfo(format, &info);
168 if (err) return err;
169
Mathias Agopiancc934762009-09-23 19:16:27 -0700170 // the display's pixel format
171 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-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 Agopiancc934762009-09-23 19:16:27 -0700181 PixelFormatInfo displayInfo;
182 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700183 const uint32_t hwFlags = hw.getFlags();
184
Mathias Agopian9779b222009-09-07 16:32:45 -0700185 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700186 mWidth = w;
Mathias Agopian9779b222009-09-07 16:32:45 -0700187 mHeight = h;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700188
189 mReqFormat = format;
190 mReqWidth = w;
191 mReqHeight = h;
192
Mathias Agopian6950e422009-10-05 17:07:12 -0700193 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800194 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guyd10cd572010-10-10 13:33:22 -0700195 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
196 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700197
Mathias Agopiancc934762009-09-23 19:16:27 -0700198 // we use the red index
199 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
200 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
201 mNeedsDithering = layerRedsize > displayRedSize;
202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 return NO_ERROR;
204}
205
Mathias Agopiane0d5f5b2010-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 Agopian8128ee82010-12-08 17:23:18 -0800227 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
228 hwcl->transform = tr.getOrientation();
Mathias Agopiane0d5f5b2010-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 Agopian575eaf52010-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 Agopiane0d5f5b2010-08-10 17:14:02 -0700253 hwcl->handle = NULL;
254 return;
255 }
Louis Huemiller2dd198b2010-12-01 12:29:36 -0800256 hwcl->handle = buffer->handle;
Mathias Agopianac843f22010-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 Agopiane0d5f5b2010-08-10 17:14:02 -0700269}
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271void Layer::reloadTexture(const Region& dirty)
272{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700273 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-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 Agopian781953d2010-06-25 18:02:21 -0700281 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopian9f2c4fd2010-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 Agopian9f2c4fd2010-05-10 20:06:11 -0700285 goto slowpath;
Mathias Agopian1473f462009-04-10 14:24:30 -0700286 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700287 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800288slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700289 GGLSurface t;
Mathias Agopianc817b222010-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 Agopian1473f462009-04-10 14:24:30 -0700300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302}
303
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700304void Layer::drawForSreenShot() const
305{
Mathias Agopian1989af22010-12-02 21:32:29 -0800306 const bool currentFiltering = mNeedsFiltering;
307 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700308 LayerBase::drawForSreenShot();
Mathias Agopian1989af22010-12-02 21:32:29 -0800309 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700310}
311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312void Layer::onDraw(const Region& clip) const
313{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700314 Texture tex(mBufferManager.getActiveTexture());
315 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-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 Agopianf8b4b442010-06-14 21:20:00 -0700337 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 return;
340 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700341 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342}
343
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800344// As documented in libhardware header, formats in the range
345// 0x100 - 0x1FF are specific to the HAL implementation, and
346// are known to have no alpha channel
347// TODO: move definition for device-specific range into
348// hardware.h, instead of using hard-coded values here.
349#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
350
351bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
352{
353 // If buffers where set with eOpaque flag, all buffers are known to
354 // be opaque without having to check their actual format
355 if (mNeedsBlending && buffer != NULL) {
356 PixelFormat format = buffer->getPixelFormat();
357
358 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
359 return false;
360 }
361
362 PixelFormatInfo info;
363 status_t err = getPixelFormatInfo(format, &info);
364 if (!err && info.h_alpha <= info.l_alpha) {
365 return false;
366 }
367 }
368
369 // Return opacity as determined from flags and format options
370 // passed to setBuffers()
371 return mNeedsBlending;
372}
373
374bool Layer::needsBlending() const
375{
376 if (mBufferManager.hasActiveBuffer()) {
377 return needsBlending(mBufferManager.getActiveBuffer());
378 }
379
380 return mNeedsBlending;
381}
382
Mathias Agopian92377032010-05-26 22:08:52 -0700383bool Layer::needsFiltering() const
384{
385 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian1989af22010-12-02 21:32:29 -0800386 // if our buffer is not the same size than ourselves,
387 // we need filtering.
388 Mutex::Autolock _l(mLock);
389 if (mNeedsScaling)
Mathias Agopian92377032010-05-26 22:08:52 -0700390 return true;
391 }
392 return LayerBase::needsFiltering();
393}
394
Jamie Gennisf72606c2011-03-09 17:05:02 -0800395bool Layer::isProtected() const
396{
397 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
398 return (activeBuffer != 0) &&
399 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
400}
Mathias Agopian59751db2010-05-07 15:58:44 -0700401
402status_t Layer::setBufferCount(int bufferCount)
403{
Mathias Agopian7623da42010-06-01 15:12:58 -0700404 ClientRef::Access sharedClient(mUserClientRef);
405 SharedBufferServer* lcblk(sharedClient.get());
406 if (!lcblk) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700407 // oops, the client is already gone
408 return DEAD_OBJECT;
409 }
410
Mathias Agopian898c4c92010-05-18 17:06:55 -0700411 // NOTE: lcblk->resize() is protected by an internal lock
412 status_t err = lcblk->resize(bufferCount);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700413 if (err == NO_ERROR) {
414 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
415 mBufferManager.resize(bufferCount, mFlinger, dpy);
416 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700417
418 return err;
419}
420
Mathias Agopian2be352a2010-05-21 17:24:35 -0700421sp<GraphicBuffer> Layer::requestBuffer(int index,
422 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
423 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424{
Mathias Agopian6950e422009-10-05 17:07:12 -0700425 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700426
Mathias Agopian7623da42010-06-01 15:12:58 -0700427 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopian2be352a2010-05-21 17:24:35 -0700428 return buffer;
429
430 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
431 return buffer;
432
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700433 // this ensures our client doesn't go away while we're accessing
434 // the shared area.
Mathias Agopian7623da42010-06-01 15:12:58 -0700435 ClientRef::Access sharedClient(mUserClientRef);
436 SharedBufferServer* lcblk(sharedClient.get());
437 if (!lcblk) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700438 // oops, the client is already gone
439 return buffer;
440 }
441
Mathias Agopian1473f462009-04-10 14:24:30 -0700442 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700443 * This is called from the client's Surface::dequeue(). This can happen
444 * at any time, especially while we're in the middle of using the
445 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700446 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447
Mathias Agopian51c70e32010-07-27 20:11:35 -0700448 status_t err = NO_ERROR;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700449 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700450 { // scope for the lock
451 Mutex::Autolock _l(mLock);
Mathias Agopianc51114f2010-08-25 14:59:15 -0700452
453 // zero means default
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700454 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700455 if (!reqFormat) reqFormat = mFormat;
456 if (!reqWidth) reqWidth = mWidth;
457 if (!reqHeight) reqHeight = mHeight;
458
459 w = reqWidth;
460 h = reqHeight;
461 f = reqFormat;
462
463 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
464 (reqFormat != mReqFormat)) {
465 mReqWidth = reqWidth;
466 mReqHeight = reqHeight;
467 mReqFormat = reqFormat;
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700468 mFixedSize = fixedSize;
Mathias Agopian1989af22010-12-02 21:32:29 -0800469 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700470
Mathias Agopian2be352a2010-05-21 17:24:35 -0700471 lcblk->reallocateAllExcept(index);
472 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700473 }
474
Mathias Agopian51c70e32010-07-27 20:11:35 -0700475 // here we have to reallocate a new buffer because the buffer could be
476 // used as the front buffer, or by a client in our process
477 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian6950e422009-10-05 17:07:12 -0700478 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian51c70e32010-07-27 20:11:35 -0700479 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
480 err = buffer->initCheck();
Mathias Agopian9779b222009-09-07 16:32:45 -0700481
482 if (err || buffer->handle == 0) {
Mathias Agopiane869aee2010-12-03 17:33:09 -0800483 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopian9779b222009-09-07 16:32:45 -0700484 LOGE_IF(err || buffer->handle == 0,
485 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
486 this, index, w, h, strerror(-err));
487 } else {
488 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700489 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
490 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700491 }
492
493 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700494 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700495 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700496 }
497 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498}
499
Mathias Agopian6950e422009-10-05 17:07:12 -0700500uint32_t Layer::getEffectiveUsage(uint32_t usage) const
501{
502 /*
503 * buffers used for software rendering, but h/w composition
504 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
505 *
506 * buffers used for h/w rendering and h/w composition
507 * are allocated with HW_RENDER | HW_TEXTURE
508 *
509 * buffers used with h/w rendering and either NPOT or no egl_image_ext
510 * are allocated with SW_READ_RARELY | HW_RENDER
511 *
512 */
513
514 if (mSecure) {
515 // secure buffer, don't store it into the GPU
516 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
517 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
518 } else {
519 // it's allowed to modify the usage flags here, but generally
520 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700521 // request EGLImage for all buffers
522 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700523 }
Jamie Gennisf72606c2011-03-09 17:05:02 -0800524 if (mProtectedByApp) {
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800525 // need a hardware-protected path to external video sink
526 usage |= GraphicBuffer::USAGE_PROTECTED;
527 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700528 return usage;
529}
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531uint32_t Layer::doTransaction(uint32_t flags)
532{
533 const Layer::State& front(drawingState());
534 const Layer::State& temp(currentState());
535
Mathias Agopian2be352a2010-05-21 17:24:35 -0700536 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
537 (front.requested_h != temp.requested_h);
538
539 if (sizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700540 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700542 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
543 this,
544 int(temp.requested_w), int(temp.requested_h),
545 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546
Mathias Agopian2be352a2010-05-21 17:24:35 -0700547 if (!isFixedSize()) {
548 // we're being resized and there is a freeze display request,
549 // acquire a freeze lock, so that the screen stays put
550 // until we've redrawn at the new size; this is to avoid
551 // glitches upon orientation changes.
552 if (mFlinger->hasFreezeRequest()) {
553 // if the surface is hidden, don't try to acquire the
554 // freeze lock, since hidden surfaces may never redraw
555 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
556 mFreezeLock = mFlinger->getFreezeLock();
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700559
560 // this will make sure LayerBase::doTransaction doesn't update
561 // the drawing state's size
562 Layer::State& editDraw(mDrawingState);
563 editDraw.requested_w = temp.requested_w;
564 editDraw.requested_h = temp.requested_h;
565
566 // record the new size, form this point on, when the client request
567 // a buffer, it'll get the new size.
568 setBufferSize(temp.requested_w, temp.requested_h);
569
Mathias Agopian7623da42010-06-01 15:12:58 -0700570 ClientRef::Access sharedClient(mUserClientRef);
571 SharedBufferServer* lcblk(sharedClient.get());
572 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700573 // all buffers need reallocation
574 lcblk->reallocateAll();
575 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700576 } else {
577 // record the new size
578 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 if (temp.sequence != front.sequence) {
583 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
584 // this surface is now hidden, so it shouldn't hold a freeze lock
585 // (it may never redraw, which is fine if it is hidden)
586 mFreezeLock.clear();
587 }
588 }
589
590 return LayerBase::doTransaction(flags);
591}
592
Mathias Agopian2be352a2010-05-21 17:24:35 -0700593void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700594 Mutex::Autolock _l(mLock);
595 mWidth = w;
596 mHeight = h;
Mathias Agopian1989af22010-12-02 21:32:29 -0800597 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598}
599
Mathias Agopian2be352a2010-05-21 17:24:35 -0700600bool Layer::isFixedSize() const {
601 Mutex::Autolock _l(mLock);
602 return mFixedSize;
603}
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605// ----------------------------------------------------------------------------
606// pageflip handling...
607// ----------------------------------------------------------------------------
608
609void Layer::lockPageFlip(bool& recomputeVisibleRegions)
610{
Mathias Agopian7623da42010-06-01 15:12:58 -0700611 ClientRef::Access sharedClient(mUserClientRef);
612 SharedBufferServer* lcblk(sharedClient.get());
613 if (!lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700614 // client died
615 recomputeVisibleRegions = true;
616 return;
617 }
618
Mathias Agopian9779b222009-09-07 16:32:45 -0700619 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700620 if (buf == NOT_ENOUGH_DATA) {
621 // NOTE: This is not an error, it simply means there is nothing to
622 // retire. The buffer is locked because we will use it
Mathias Agopian9779b222009-09-07 16:32:45 -0700623 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 return;
625 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700626
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700627 if (buf < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700628 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700629 mPostedDirtyRegion.clear();
630 return;
631 }
632
Mathias Agopian9779b222009-09-07 16:32:45 -0700633 // we retired a buffer, which becomes the new front buffer
Mathias Agopian575eaf52010-12-13 18:51:59 -0800634
635 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800636 const bool activeBlending =
637 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
638
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700639 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700640 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700641 mPostedDirtyRegion.clear();
642 return;
643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644
Mathias Agopian575eaf52010-12-13 18:51:59 -0800645 if (noActiveBuffer) {
646 // we didn't have an active buffer, we need to recompute
647 // our visible region
648 recomputeVisibleRegions = true;
649 }
650
Mathias Agopian6950e422009-10-05 17:07:12 -0700651 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700652 if (newFrontBuffer != NULL) {
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800653 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
654 // new buffer has different opacity than previous active buffer, need
655 // to recompute visible regions accordingly
656 recomputeVisibleRegions = true;
657 }
658
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700659 // get the dirty region
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700660 // compute the posted region
661 const Region dirty(lcblk->getDirtyRegion(buf));
662 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700663
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700664 // update the layer size and release freeze-lock
665 const Layer::State& front(drawingState());
666 if (newFrontBuffer->getWidth() == front.requested_w &&
667 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700668 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700669 if ((front.w != front.requested_w) ||
670 (front.h != front.requested_h))
671 {
672 // Here we pretend the transaction happened by updating the
673 // current and drawing states. Drawing state is only accessed
674 // in this thread, no need to have it locked
675 Layer::State& editDraw(mDrawingState);
676 editDraw.w = editDraw.requested_w;
677 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700678
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700679 // We also need to update the current state so that we don't
680 // end-up doing too much work during the next transaction.
681 // NOTE: We actually don't need hold the transaction lock here
682 // because State::w and State::h are only accessed from
683 // this thread
684 Layer::State& editTemp(currentState());
685 editTemp.w = editDraw.w;
686 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700687
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700688 // recompute visible region
689 recomputeVisibleRegions = true;
690 }
691
692 // we now have the correct size, unfreeze the screen
693 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700694 }
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700695
696 // get the crop region
697 setBufferCrop( lcblk->getCrop(buf) );
698
699 // get the transformation
700 setBufferTransform( lcblk->getTransform(buf) );
701
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700702 } else {
703 // this should not happen unless we ran out of memory while
704 // allocating the buffer. we're hoping that things will get back
705 // to normal the next time the app tries to draw into this buffer.
706 // meanwhile, pretend the screen didn't update.
707 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700708 }
709
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700710 if (lcblk->getQueuedCount()) {
711 // signal an event if we have more buffers waiting
712 mFlinger->signalEvent();
713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700715 /* a buffer was posted, so we need to call reloadTexture(), which
716 * will update our internal data structures (eg: EGLImageKHR or
717 * texture names). we need to do this even if mPostedDirtyRegion is
718 * empty -- it's orthogonal to the fact that a new buffer was posted,
719 * for instance, a degenerate case could be that the user did an empty
720 * update but repainted the buffer with appropriate content (after a
721 * resize for instance).
722 */
723 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724}
725
726void Layer::unlockPageFlip(
727 const Transform& planeTransform, Region& outDirtyRegion)
728{
729 Region dirtyRegion(mPostedDirtyRegion);
730 if (!dirtyRegion.isEmpty()) {
731 mPostedDirtyRegion.clear();
732 // The dirty region is given in the layer's coordinate space
733 // transform the dirty region by the surface's transformation
734 // and the global transformation.
735 const Layer::State& s(drawingState());
736 const Transform tr(planeTransform * s.transform);
737 dirtyRegion = tr.transform(dirtyRegion);
738
739 // At this point, the dirty region is in screen space.
740 // Make sure it's constrained by the visible region (which
741 // is in screen space as well).
742 dirtyRegion.andSelf(visibleRegionScreen);
743 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800745 if (visibleRegionScreen.isEmpty()) {
746 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700747 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800748 mFreezeLock.clear();
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750}
751
Mathias Agopian9bce8732010-04-20 17:55:49 -0700752void Layer::dump(String8& result, char* buffer, size_t SIZE) const
753{
754 LayerBaseClient::dump(result, buffer, SIZE);
755
Mathias Agopian7623da42010-06-01 15:12:58 -0700756 ClientRef::Access sharedClient(mUserClientRef);
757 SharedBufferServer* lcblk(sharedClient.get());
758 uint32_t totalTime = 0;
759 if (lcblk) {
760 SharedBufferStack::Statistics stats = lcblk->getStats();
761 totalTime= stats.totalTime;
762 result.append( lcblk->dump(" ") );
763 }
764
Mathias Agopian9bce8732010-04-20 17:55:49 -0700765 sp<const GraphicBuffer> buf0(getBuffer(0));
766 sp<const GraphicBuffer> buf1(getBuffer(1));
767 uint32_t w0=0, h0=0, s0=0;
768 uint32_t w1=0, h1=0, s1=0;
769 if (buf0 != 0) {
770 w0 = buf0->getWidth();
771 h0 = buf0->getHeight();
772 s0 = buf0->getStride();
773 }
774 if (buf1 != 0) {
775 w1 = buf1->getWidth();
776 h1 = buf1->getHeight();
777 s1 = buf1->getStride();
778 }
779 snprintf(buffer, SIZE,
780 " "
781 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
782 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopian7623da42010-06-01 15:12:58 -0700783 mFormat, w0, h0, s0, w1, h1, s1,
784 getFreezeLock().get(), totalTime);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700785
786 result.append(buffer);
787}
788
Mathias Agopian1473f462009-04-10 14:24:30 -0700789// ---------------------------------------------------------------------------
790
Mathias Agopian7623da42010-06-01 15:12:58 -0700791Layer::ClientRef::ClientRef()
Mathias Agopian5e140102010-06-08 19:54:15 -0700792 : mControlBlock(0), mToken(-1) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700793}
794
795Layer::ClientRef::~ClientRef() {
Mathias Agopian7623da42010-06-01 15:12:58 -0700796}
797
798int32_t Layer::ClientRef::getToken() const {
799 Mutex::Autolock _l(mLock);
800 return mToken;
801}
802
Mathias Agopian5e140102010-06-08 19:54:15 -0700803sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700804 Mutex::Autolock _l(mLock);
Mathias Agopian5e140102010-06-08 19:54:15 -0700805 return mUserClient.promote();
806}
807
808status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
809 const sp<SharedBufferServer>& sharedClient, int32_t token) {
810 Mutex::Autolock _l(mLock);
811
812 { // scope for strong mUserClient reference
813 sp<UserClient> userClient(mUserClient.promote());
Jamie Gennis6912daf2011-03-18 16:35:13 -0700814 if (userClient != 0 && mControlBlock != 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -0700815 mControlBlock->setStatus(NO_INIT);
816 }
817 }
818
Mathias Agopian7623da42010-06-01 15:12:58 -0700819 mUserClient = uc;
820 mToken = token;
Mathias Agopian5e140102010-06-08 19:54:15 -0700821 mControlBlock = sharedClient;
Mathias Agopian7623da42010-06-01 15:12:58 -0700822 return NO_ERROR;
823}
824
825sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
826 return mUserClient.promote();
827}
828
829// this class gives us access to SharedBufferServer safely
830// it makes sure the UserClient (and its associated shared memory)
831// won't go away while we're accessing it.
832Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian5e140102010-06-08 19:54:15 -0700833 : mControlBlock(0)
Mathias Agopian7623da42010-06-01 15:12:58 -0700834{
835 Mutex::Autolock _l(ref.mLock);
836 mUserClientStrongRef = ref.mUserClient.promote();
837 if (mUserClientStrongRef != 0)
Mathias Agopian5e140102010-06-08 19:54:15 -0700838 mControlBlock = ref.mControlBlock;
839}
840
841Layer::ClientRef::Access::~Access()
842{
Mathias Agopian7623da42010-06-01 15:12:58 -0700843}
844
845// ---------------------------------------------------------------------------
846
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700847Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700848 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800849 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700850{
851}
852
Mathias Agopian898c4c92010-05-18 17:06:55 -0700853Layer::BufferManager::~BufferManager()
854{
855}
856
Jamie Gennis6c925d02010-11-02 11:51:32 -0700857status_t Layer::BufferManager::resize(size_t size,
858 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700859{
860 Mutex::Autolock _l(mLock);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700861
862 if (size < mNumBuffers) {
Mathias Agopianf40e6382011-03-16 23:18:07 -0700863 // If there is an active texture, move it into slot 0 if needed
864 if (mActiveBufferIndex > 0) {
865 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
866 mBufferData[mActiveBufferIndex] = mBufferData[0];
867 mBufferData[0] = activeBufferData;
868 mActiveBufferIndex = 0;
869 }
Jamie Gennis6c925d02010-11-02 11:51:32 -0700870
871 // Free the buffers that are no longer needed.
872 for (size_t i = size; i < mNumBuffers; i++) {
873 mBufferData[i].buffer = 0;
874
875 // Create a message to destroy the textures on SurfaceFlinger's GL
876 // thread.
877 class MessageDestroyTexture : public MessageBase {
878 Image mTexture;
879 EGLDisplay mDpy;
880 public:
881 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
882 : mTexture(texture), mDpy(dpy) { }
883 virtual bool handler() {
884 status_t err = Layer::BufferManager::destroyTexture(
885 &mTexture, mDpy);
886 LOGE_IF(err<0, "error destroying texture: %d (%s)",
887 mTexture.name, strerror(-err));
888 return true; // XXX: err == 0; ????
889 }
890 };
891
892 MessageDestroyTexture *msg = new MessageDestroyTexture(
893 mBufferData[i].texture, dpy);
894
895 // Don't allow this texture to be cleaned up by
896 // BufferManager::destroy.
897 mBufferData[i].texture.name = -1U;
898 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
899
900 // Post the message to the SurfaceFlinger object.
901 flinger->postMessageAsync(msg);
902 }
903 }
904
Mathias Agopian898c4c92010-05-18 17:06:55 -0700905 mNumBuffers = size;
906 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700907}
908
909// only for debugging
910sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
911 return mBufferData[index].buffer;
912}
913
914status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800915 BufferData const * const buffers = mBufferData;
916 Mutex::Autolock _l(mLock);
917 mActiveBuffer = buffers[index].buffer;
918 mActiveBufferIndex = index;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700919 return NO_ERROR;
920}
921
922size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800923 return mActiveBufferIndex;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700924}
925
926Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700927 Texture res;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800928 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700929 res = mFailoverTexture;
930 } else {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800931 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700932 }
933 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700934}
935
936sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800937 return mActiveBuffer;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700938}
939
Mathias Agopian575eaf52010-12-13 18:51:59 -0800940bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800941 return mActiveBufferIndex >= 0;
Mathias Agopian575eaf52010-12-13 18:51:59 -0800942}
943
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700944sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
945{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700946 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700947 sp<GraphicBuffer> buffer;
948 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700949 buffer = buffers[index].buffer;
950 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700951 return buffer;
952}
953
954status_t Layer::BufferManager::attachBuffer(size_t index,
955 const sp<GraphicBuffer>& buffer)
956{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700957 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700958 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700959 buffers[index].buffer = buffer;
960 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700961 return NO_ERROR;
962}
963
964status_t Layer::BufferManager::destroy(EGLDisplay dpy)
965{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700966 BufferData* const buffers = mBufferData;
967 size_t num;
968 { // scope for the lock
969 Mutex::Autolock _l(mLock);
970 num = mNumBuffers;
971 for (size_t i=0 ; i<num ; i++) {
972 buffers[i].buffer = 0;
973 }
974 }
975 for (size_t i=0 ; i<num ; i++) {
976 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700977 }
978 destroyTexture(&mFailoverTexture, dpy);
979 return NO_ERROR;
980}
981
982status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
983 const sp<GraphicBuffer>& buffer)
984{
Mathias Agopian7623da42010-06-01 15:12:58 -0700985 status_t err = NO_INIT;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800986 ssize_t index = mActiveBufferIndex;
Mathias Agopian7623da42010-06-01 15:12:58 -0700987 if (index >= 0) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700988 if (!mFailover) {
Kobi Cohen Arazi8ecc90d02011-04-15 10:38:33 -0700989 {
990 // Without that lock, there is a chance of race condition
991 // where while composing a specific index, requestBuf
992 // with the same index can be executed and touch the same data
993 // that is being used in initEglImage.
994 // (e.g. dirty flag in texture)
995 Mutex::Autolock _l(mLock);
996 Image& texture(mBufferData[index].texture);
997 err = mTextureManager.initEglImage(&texture, dpy, buffer);
998 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700999 // if EGLImage fails, we switch to regular texture mode, and we
1000 // free all resources associated with using EGLImages.
1001 if (err == NO_ERROR) {
1002 mFailover = false;
1003 destroyTexture(&mFailoverTexture, dpy);
1004 } else {
1005 mFailover = true;
1006 const size_t num = mNumBuffers;
1007 for (size_t i=0 ; i<num ; i++) {
1008 destroyTexture(&mBufferData[i].texture, dpy);
1009 }
Andreas Huber330dd302010-06-25 09:25:19 -07001010 }
Mathias Agopian781953d2010-06-25 18:02:21 -07001011 } else {
1012 // we failed once, don't try again
1013 err = BAD_VALUE;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001014 }
1015 }
1016 return err;
1017}
1018
1019status_t Layer::BufferManager::loadTexture(
1020 const Region& dirty, const GGLSurface& t)
1021{
1022 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1023}
1024
Mathias Agopian898c4c92010-05-18 17:06:55 -07001025status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1026{
1027 if (tex->name != -1U) {
1028 glDeleteTextures(1, &tex->name);
1029 tex->name = -1U;
1030 }
1031 if (tex->image != EGL_NO_IMAGE_KHR) {
1032 eglDestroyImageKHR(dpy, tex->image);
1033 tex->image = EGL_NO_IMAGE_KHR;
1034 }
1035 return NO_ERROR;
1036}
1037
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001038// ---------------------------------------------------------------------------
1039
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001040Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001041 const sp<Layer>& owner)
1042 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001043{
1044}
1045
1046Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -07001047{
1048}
1049
Mathias Agopian2be352a2010-05-21 17:24:35 -07001050sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1051 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -07001052{
Mathias Agopian6950e422009-10-05 17:07:12 -07001053 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -07001054 sp<Layer> owner(getOwner());
1055 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001056 /*
1057 * requestBuffer() cannot be called from the main thread
1058 * as it could cause a dead-lock, since it may have to wait
1059 * on conditions updated my the main thread.
1060 */
Mathias Agopian2be352a2010-05-21 17:24:35 -07001061 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -07001062 }
1063 return buffer;
1064}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065
Mathias Agopian59751db2010-05-07 15:58:44 -07001066status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1067{
1068 status_t err = DEAD_OBJECT;
1069 sp<Layer> owner(getOwner());
1070 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001071 /*
1072 * setBufferCount() cannot be called from the main thread
1073 * as it could cause a dead-lock, since it may have to wait
1074 * on conditions updated my the main thread.
1075 */
Mathias Agopian59751db2010-05-07 15:58:44 -07001076 err = owner->setBufferCount(bufferCount);
1077 }
1078 return err;
1079}
1080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081// ---------------------------------------------------------------------------
1082
1083
1084}; // namespace android