blob: 4afe90517826be6ab368601f5e6e738357387765 [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 Agopian781953d2010-06-25 18:02:21 -070055 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiancc934762009-09-23 19:16:27 -070056 mNeedsBlending(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070057 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070058 mSecure(false),
Glenn Kastend6f5bde2011-01-19 15:27:27 -080059 mProtectedByApp(false),
Mathias Agopian781953d2010-06-25 18:02:21 -070060 mTextureManager(),
Mathias Agopian2be352a2010-05-21 17:24:35 -070061 mBufferManager(mTextureManager),
Eric Hassoldb6c0f512011-03-11 12:24:23 -080062 mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE),
63 mNeedsScaling(false), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065}
66
67Layer::~Layer()
68{
Mathias Agopian898c4c92010-05-18 17:06:55 -070069 // FIXME: must be called from the main UI thread
70 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
71 mBufferManager.destroy(dpy);
72
Mathias Agopian7623da42010-06-01 15:12:58 -070073 // we can use getUserClientUnsafe here because we know we're
74 // single-threaded at that point.
75 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
76 if (ourClient != 0) {
77 ourClient->detachLayer(this);
78 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070079}
80
Mathias Agopian7623da42010-06-01 15:12:58 -070081status_t Layer::setToken(const sp<UserClient>& userClient,
82 SharedClient* sharedClient, int32_t token)
Mathias Agopian593c05c2010-06-02 23:28:45 -070083{
Mathias Agopian5e140102010-06-08 19:54:15 -070084 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopian7623da42010-06-01 15:12:58 -070085 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian593c05c2010-06-02 23:28:45 -070086 getIdentity());
87
Mathias Agopian5e140102010-06-08 19:54:15 -070088
Mathias Agopian5747bbc2010-12-13 16:49:05 -080089 sp<UserClient> ourClient(mUserClientRef.getClient());
90
91 /*
92 * Here it is guaranteed that userClient != ourClient
93 * (see UserClient::getTokenForSurface()).
94 *
95 * We release the token used by this surface in ourClient below.
96 * This should be safe to do so now, since this layer won't be attached
97 * to this client, it should be okay to reuse that id.
98 *
99 * If this causes problems, an other solution would be to keep a list
100 * of all the {UserClient, token} ever used and release them when the
101 * Layer is destroyed.
102 *
103 */
104
105 if (ourClient != 0) {
106 ourClient->detachLayer(this);
107 }
108
109 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian5e140102010-06-08 19:54:15 -0700110 LOGE_IF(err != NO_ERROR,
111 "ClientRef::setToken(%p, %p, %u) failed",
112 userClient.get(), lcblk.get(), token);
113
114 if (err == NO_ERROR) {
115 // we need to free the buffers associated with this surface
Mathias Agopian7623da42010-06-01 15:12:58 -0700116 }
117
118 return err;
119}
120
121int32_t Layer::getToken() const
122{
123 return mUserClientRef.getToken();
Mathias Agopian593c05c2010-06-02 23:28:45 -0700124}
125
Mathias Agopian5e140102010-06-08 19:54:15 -0700126sp<UserClient> Layer::getClient() const
127{
128 return mUserClientRef.getClient();
129}
130
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700131// called with SurfaceFlinger::mStateLock as soon as the layer is entered
132// in the purgatory list
133void Layer::onRemoved()
134{
Mathias Agopian7623da42010-06-01 15:12:58 -0700135 ClientRef::Access sharedClient(mUserClientRef);
136 SharedBufferServer* lcblk(sharedClient.get());
137 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700138 // wake up the condition
139 lcblk->setStatus(NO_INIT);
140 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700141}
Mathias Agopian9779b2212009-09-07 16:32:45 -0700142
Mathias Agopian1473f462009-04-10 14:24:30 -0700143sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144{
Mathias Agopian1b0114f2011-02-15 19:01:06 -0800145 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
146 return sur;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147}
148
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700149status_t Layer::ditch()
150{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700151 // NOTE: Called from the main UI thread
152
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700153 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -0800154 mFreezeLock.clear();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700155
Mathias Agopian898c4c92010-05-18 17:06:55 -0700156 Mutex::Autolock _l(mLock);
157 mWidth = mHeight = 0;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700158 return NO_ERROR;
159}
160
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700161status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 PixelFormat format, uint32_t flags)
163{
Mathias Agopiancc934762009-09-23 19:16:27 -0700164 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 PixelFormatInfo info;
166 status_t err = getPixelFormatInfo(format, &info);
167 if (err) return err;
168
Mathias Agopiancc934762009-09-23 19:16:27 -0700169 // the display's pixel format
170 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-04-14 16:43:44 -0700171 uint32_t const maxSurfaceDims = min(
172 hw.getMaxTextureSize(), hw.getMaxViewportDims());
173
174 // never allow a surface larger than what our underlying GL implementation
175 // can handle.
176 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
177 return BAD_VALUE;
178 }
179
Mathias Agopiancc934762009-09-23 19:16:27 -0700180 PixelFormatInfo displayInfo;
181 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700182 const uint32_t hwFlags = hw.getFlags();
183
Mathias Agopian9779b2212009-09-07 16:32:45 -0700184 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700185 mWidth = w;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700186 mHeight = h;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700187
188 mReqFormat = format;
189 mReqWidth = w;
190 mReqHeight = h;
191
Mathias Agopian6950e422009-10-05 17:07:12 -0700192 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800193 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guyd10cd572010-10-10 13:33:22 -0700194 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
195 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700196
Mathias Agopiancc934762009-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 return NO_ERROR;
203}
204
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700205void Layer::setGeometry(hwc_layer_t* hwcl)
206{
207 hwcl->compositionType = HWC_FRAMEBUFFER;
208 hwcl->hints = 0;
209 hwcl->flags = 0;
210 hwcl->transform = 0;
211 hwcl->blending = HWC_BLENDING_NONE;
212
213 // we can't do alpha-fade with the hwc HAL
214 const State& s(drawingState());
215 if (s.alpha < 0xFF) {
216 hwcl->flags = HWC_SKIP_LAYER;
217 return;
218 }
219
220 // we can only handle simple transformation
221 if (mOrientation & Transform::ROT_INVALID) {
222 hwcl->flags = HWC_SKIP_LAYER;
223 return;
224 }
225
Mathias Agopian8128ee82010-12-08 17:23:18 -0800226 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
227 hwcl->transform = tr.getOrientation();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700228
229 if (needsBlending()) {
230 hwcl->blending = mPremultipliedAlpha ?
231 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
232 }
233
234 hwcl->displayFrame.left = mTransformedBounds.left;
235 hwcl->displayFrame.top = mTransformedBounds.top;
236 hwcl->displayFrame.right = mTransformedBounds.right;
237 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
238
239 hwcl->visibleRegionScreen.rects =
240 reinterpret_cast<hwc_rect_t const *>(
241 visibleRegionScreen.getArray(
242 &hwcl->visibleRegionScreen.numRects));
243}
244
245void Layer::setPerFrameData(hwc_layer_t* hwcl) {
246 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
247 if (buffer == NULL) {
Mathias Agopian575eaf52010-12-13 18:51:59 -0800248 // this can happen if the client never drew into this layer yet,
249 // or if we ran out of memory. In that case, don't let
250 // HWC handle it.
251 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700252 hwcl->handle = NULL;
253 return;
254 }
Louis Huemiller2dd198b2010-12-01 12:29:36 -0800255 hwcl->handle = buffer->handle;
Mathias Agopianac843f22010-12-08 17:40:28 -0800256
257 if (!mBufferCrop.isEmpty()) {
258 hwcl->sourceCrop.left = mBufferCrop.left;
259 hwcl->sourceCrop.top = mBufferCrop.top;
260 hwcl->sourceCrop.right = mBufferCrop.right;
261 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
262 } else {
263 hwcl->sourceCrop.left = 0;
264 hwcl->sourceCrop.top = 0;
265 hwcl->sourceCrop.right = buffer->width;
266 hwcl->sourceCrop.bottom = buffer->height;
267 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700268}
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270void Layer::reloadTexture(const Region& dirty)
271{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700272 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-12-10 15:52:29 -0800273 if (buffer == NULL) {
274 // this situation can happen if we ran out of memory for instance.
275 // not much we can do. continue to use whatever texture was bound
276 // to this context.
277 return;
278 }
279
Mathias Agopian781953d2010-06-25 18:02:21 -0700280 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700281 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
282 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
283 // not sure what we can do here...
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700284 goto slowpath;
Mathias Agopian1473f462009-04-10 14:24:30 -0700285 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700286 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800287slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700288 GGLSurface t;
Mathias Agopianc817b222010-08-20 15:59:53 -0700289 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
290 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
291 LOGE_IF(res, "error %d (%s) locking buffer %p",
292 res, strerror(res), buffer.get());
293 if (res == NO_ERROR) {
294 mBufferManager.loadTexture(dirty, t);
295 buffer->unlock();
296 }
297 } else {
298 // we can't do anything
Mathias Agopian1473f462009-04-10 14:24:30 -0700299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301}
302
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700303void Layer::drawForSreenShot() const
304{
Mathias Agopian1989af22010-12-02 21:32:29 -0800305 const bool currentFiltering = mNeedsFiltering;
306 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700307 LayerBase::drawForSreenShot();
Mathias Agopian1989af22010-12-02 21:32:29 -0800308 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700309}
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311void Layer::onDraw(const Region& clip) const
312{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700313 Texture tex(mBufferManager.getActiveTexture());
314 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700316 // in fact never been drawn into. This happens frequently with
317 // SurfaceView because the WindowManager can't know when the client
318 // has drawn the first time.
319
320 // If there is nothing under us, we paint the screen in black, otherwise
321 // we just skip this update.
322
323 // figure out if there is something below us
324 Region under;
325 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
326 const size_t count = drawingLayers.size();
327 for (size_t i=0 ; i<count ; ++i) {
328 const sp<LayerBase>& layer(drawingLayers[i]);
329 if (layer.get() == static_cast<LayerBase const*>(this))
330 break;
331 under.orSelf(layer->visibleRegionScreen);
332 }
333 // if not everything below us is covered, we plug the holes!
334 Region holes(clip.subtract(under));
335 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700336 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 return;
339 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700340 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341}
342
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800343// As documented in libhardware header, formats in the range
344// 0x100 - 0x1FF are specific to the HAL implementation, and
345// are known to have no alpha channel
346// TODO: move definition for device-specific range into
347// hardware.h, instead of using hard-coded values here.
348#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
349
350bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
351{
352 // If buffers where set with eOpaque flag, all buffers are known to
353 // be opaque without having to check their actual format
354 if (mNeedsBlending && buffer != NULL) {
355 PixelFormat format = buffer->getPixelFormat();
356
357 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
358 return false;
359 }
360
361 PixelFormatInfo info;
362 status_t err = getPixelFormatInfo(format, &info);
363 if (!err && info.h_alpha <= info.l_alpha) {
364 return false;
365 }
366 }
367
368 // Return opacity as determined from flags and format options
369 // passed to setBuffers()
370 return mNeedsBlending;
371}
372
373bool Layer::needsBlending() const
374{
375 if (mBufferManager.hasActiveBuffer()) {
376 return needsBlending(mBufferManager.getActiveBuffer());
377 }
378
379 return mNeedsBlending;
380}
381
Mathias Agopian92377032010-05-26 22:08:52 -0700382bool Layer::needsFiltering() const
383{
384 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian1989af22010-12-02 21:32:29 -0800385 // if our buffer is not the same size than ourselves,
386 // we need filtering.
387 Mutex::Autolock _l(mLock);
388 if (mNeedsScaling)
Mathias Agopian92377032010-05-26 22:08:52 -0700389 return true;
390 }
391 return LayerBase::needsFiltering();
392}
393
Jamie Gennisf72606c2011-03-09 17:05:02 -0800394bool Layer::isProtected() const
395{
396 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
397 return (activeBuffer != 0) &&
398 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
399}
Mathias Agopian59751db2010-05-07 15:58:44 -0700400
401status_t Layer::setBufferCount(int bufferCount)
402{
Mathias Agopian7623da42010-06-01 15:12:58 -0700403 ClientRef::Access sharedClient(mUserClientRef);
404 SharedBufferServer* lcblk(sharedClient.get());
405 if (!lcblk) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700406 // oops, the client is already gone
407 return DEAD_OBJECT;
408 }
409
Mathias Agopian898c4c92010-05-18 17:06:55 -0700410 // NOTE: lcblk->resize() is protected by an internal lock
411 status_t err = lcblk->resize(bufferCount);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700412 if (err == NO_ERROR) {
413 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
414 mBufferManager.resize(bufferCount, mFlinger, dpy);
415 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700416
417 return err;
418}
419
Mathias Agopian2be352a2010-05-21 17:24:35 -0700420sp<GraphicBuffer> Layer::requestBuffer(int index,
421 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
422 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423{
Mathias Agopian6950e422009-10-05 17:07:12 -0700424 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700425
Mathias Agopian7623da42010-06-01 15:12:58 -0700426 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopian2be352a2010-05-21 17:24:35 -0700427 return buffer;
428
429 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
430 return buffer;
431
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700432 // this ensures our client doesn't go away while we're accessing
433 // the shared area.
Mathias Agopian7623da42010-06-01 15:12:58 -0700434 ClientRef::Access sharedClient(mUserClientRef);
435 SharedBufferServer* lcblk(sharedClient.get());
436 if (!lcblk) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700437 // oops, the client is already gone
438 return buffer;
439 }
440
Mathias Agopian1473f462009-04-10 14:24:30 -0700441 /*
Mathias Agopian9779b2212009-09-07 16:32:45 -0700442 * This is called from the client's Surface::dequeue(). This can happen
443 * at any time, especially while we're in the middle of using the
444 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700445 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
Mathias Agopian51c70e32010-07-27 20:11:35 -0700447 status_t err = NO_ERROR;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700448 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700449 { // scope for the lock
450 Mutex::Autolock _l(mLock);
Mathias Agopianc51114f2010-08-25 14:59:15 -0700451
452 // zero means default
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700453 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700454 if (!reqFormat) reqFormat = mFormat;
455 if (!reqWidth) reqWidth = mWidth;
456 if (!reqHeight) reqHeight = mHeight;
457
458 w = reqWidth;
459 h = reqHeight;
460 f = reqFormat;
461
462 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
463 (reqFormat != mReqFormat)) {
464 mReqWidth = reqWidth;
465 mReqHeight = reqHeight;
466 mReqFormat = reqFormat;
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700467 mFixedSize = fixedSize;
Mathias Agopian1989af22010-12-02 21:32:29 -0800468 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700469
Mathias Agopian2be352a2010-05-21 17:24:35 -0700470 lcblk->reallocateAllExcept(index);
471 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700472 }
473
Mathias Agopian51c70e32010-07-27 20:11:35 -0700474 // here we have to reallocate a new buffer because the buffer could be
475 // used as the front buffer, or by a client in our process
476 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian6950e422009-10-05 17:07:12 -0700477 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian51c70e32010-07-27 20:11:35 -0700478 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
479 err = buffer->initCheck();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700480
481 if (err || buffer->handle == 0) {
Mathias Agopiane869aee2010-12-03 17:33:09 -0800482 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700483 LOGE_IF(err || buffer->handle == 0,
484 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
485 this, index, w, h, strerror(-err));
486 } else {
487 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700488 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
489 this, index, w, h, buffer->handle);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700490 }
491
492 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700493 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700494 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700495 }
496 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497}
498
Mathias Agopian6950e422009-10-05 17:07:12 -0700499uint32_t Layer::getEffectiveUsage(uint32_t usage) const
500{
501 /*
502 * buffers used for software rendering, but h/w composition
503 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
504 *
505 * buffers used for h/w rendering and h/w composition
506 * are allocated with HW_RENDER | HW_TEXTURE
507 *
508 * buffers used with h/w rendering and either NPOT or no egl_image_ext
509 * are allocated with SW_READ_RARELY | HW_RENDER
510 *
511 */
512
513 if (mSecure) {
514 // secure buffer, don't store it into the GPU
515 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
516 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
517 } else {
518 // it's allowed to modify the usage flags here, but generally
519 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700520 // request EGLImage for all buffers
521 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700522 }
Jamie Gennisf72606c2011-03-09 17:05:02 -0800523 if (mProtectedByApp) {
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800524 // need a hardware-protected path to external video sink
525 usage |= GraphicBuffer::USAGE_PROTECTED;
526 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700527 return usage;
528}
529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530uint32_t Layer::doTransaction(uint32_t flags)
531{
532 const Layer::State& front(drawingState());
533 const Layer::State& temp(currentState());
534
Mathias Agopian2be352a2010-05-21 17:24:35 -0700535 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
536 (front.requested_h != temp.requested_h);
537
538 if (sizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700539 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700541 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
542 this,
543 int(temp.requested_w), int(temp.requested_h),
544 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545
Mathias Agopian2be352a2010-05-21 17:24:35 -0700546 if (!isFixedSize()) {
547 // we're being resized and there is a freeze display request,
548 // acquire a freeze lock, so that the screen stays put
549 // until we've redrawn at the new size; this is to avoid
550 // glitches upon orientation changes.
551 if (mFlinger->hasFreezeRequest()) {
552 // if the surface is hidden, don't try to acquire the
553 // freeze lock, since hidden surfaces may never redraw
554 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
555 mFreezeLock = mFlinger->getFreezeLock();
556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700558
559 // this will make sure LayerBase::doTransaction doesn't update
560 // the drawing state's size
561 Layer::State& editDraw(mDrawingState);
562 editDraw.requested_w = temp.requested_w;
563 editDraw.requested_h = temp.requested_h;
564
565 // record the new size, form this point on, when the client request
566 // a buffer, it'll get the new size.
567 setBufferSize(temp.requested_w, temp.requested_h);
568
Mathias Agopian7623da42010-06-01 15:12:58 -0700569 ClientRef::Access sharedClient(mUserClientRef);
570 SharedBufferServer* lcblk(sharedClient.get());
571 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700572 // all buffers need reallocation
573 lcblk->reallocateAll();
574 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700575 } else {
576 // record the new size
577 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 if (temp.sequence != front.sequence) {
582 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
583 // this surface is now hidden, so it shouldn't hold a freeze lock
584 // (it may never redraw, which is fine if it is hidden)
585 mFreezeLock.clear();
586 }
587 }
588
589 return LayerBase::doTransaction(flags);
590}
591
Mathias Agopian2be352a2010-05-21 17:24:35 -0700592void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700593 Mutex::Autolock _l(mLock);
594 mWidth = w;
595 mHeight = h;
Mathias Agopian1989af22010-12-02 21:32:29 -0800596 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597}
598
Mathias Agopian2be352a2010-05-21 17:24:35 -0700599bool Layer::isFixedSize() const {
600 Mutex::Autolock _l(mLock);
601 return mFixedSize;
602}
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604// ----------------------------------------------------------------------------
605// pageflip handling...
606// ----------------------------------------------------------------------------
607
608void Layer::lockPageFlip(bool& recomputeVisibleRegions)
609{
Mathias Agopian7623da42010-06-01 15:12:58 -0700610 ClientRef::Access sharedClient(mUserClientRef);
611 SharedBufferServer* lcblk(sharedClient.get());
612 if (!lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700613 // client died
614 recomputeVisibleRegions = true;
615 return;
616 }
617
Mathias Agopian9779b2212009-09-07 16:32:45 -0700618 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700619 if (buf == NOT_ENOUGH_DATA) {
620 // NOTE: This is not an error, it simply means there is nothing to
621 // retire. The buffer is locked because we will use it
Mathias Agopian9779b2212009-09-07 16:32:45 -0700622 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 return;
624 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700625
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700626 if (buf < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700627 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700628 mPostedDirtyRegion.clear();
629 return;
630 }
631
Mathias Agopian9779b2212009-09-07 16:32:45 -0700632 // we retired a buffer, which becomes the new front buffer
Mathias Agopian575eaf52010-12-13 18:51:59 -0800633
634 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800635 const bool activeBlending =
636 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
637
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700638 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700639 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700640 mPostedDirtyRegion.clear();
641 return;
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
Mathias Agopian575eaf52010-12-13 18:51:59 -0800644 if (noActiveBuffer) {
645 // we didn't have an active buffer, we need to recompute
646 // our visible region
647 recomputeVisibleRegions = true;
648 }
649
Mathias Agopian6950e422009-10-05 17:07:12 -0700650 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700651 if (newFrontBuffer != NULL) {
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800652 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
653 // new buffer has different opacity than previous active buffer, need
654 // to recompute visible regions accordingly
655 recomputeVisibleRegions = true;
656 }
657
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700658 // get the dirty region
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700659 // compute the posted region
660 const Region dirty(lcblk->getDirtyRegion(buf));
661 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700662
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700663 // update the layer size and release freeze-lock
664 const Layer::State& front(drawingState());
665 if (newFrontBuffer->getWidth() == front.requested_w &&
666 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700667 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700668 if ((front.w != front.requested_w) ||
669 (front.h != front.requested_h))
670 {
671 // Here we pretend the transaction happened by updating the
672 // current and drawing states. Drawing state is only accessed
673 // in this thread, no need to have it locked
674 Layer::State& editDraw(mDrawingState);
675 editDraw.w = editDraw.requested_w;
676 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700677
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700678 // We also need to update the current state so that we don't
679 // end-up doing too much work during the next transaction.
680 // NOTE: We actually don't need hold the transaction lock here
681 // because State::w and State::h are only accessed from
682 // this thread
683 Layer::State& editTemp(currentState());
684 editTemp.w = editDraw.w;
685 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700686
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700687 // recompute visible region
688 recomputeVisibleRegions = true;
689 }
690
691 // we now have the correct size, unfreeze the screen
692 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700693 }
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700694
695 // get the crop region
696 setBufferCrop( lcblk->getCrop(buf) );
697
698 // get the transformation
699 setBufferTransform( lcblk->getTransform(buf) );
700
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700701 } else {
702 // this should not happen unless we ran out of memory while
703 // allocating the buffer. we're hoping that things will get back
704 // to normal the next time the app tries to draw into this buffer.
705 // meanwhile, pretend the screen didn't update.
706 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700707 }
708
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700709 if (lcblk->getQueuedCount()) {
710 // signal an event if we have more buffers waiting
711 mFlinger->signalEvent();
712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700714 /* a buffer was posted, so we need to call reloadTexture(), which
715 * will update our internal data structures (eg: EGLImageKHR or
716 * texture names). we need to do this even if mPostedDirtyRegion is
717 * empty -- it's orthogonal to the fact that a new buffer was posted,
718 * for instance, a degenerate case could be that the user did an empty
719 * update but repainted the buffer with appropriate content (after a
720 * resize for instance).
721 */
722 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723}
724
725void Layer::unlockPageFlip(
726 const Transform& planeTransform, Region& outDirtyRegion)
727{
728 Region dirtyRegion(mPostedDirtyRegion);
729 if (!dirtyRegion.isEmpty()) {
730 mPostedDirtyRegion.clear();
731 // The dirty region is given in the layer's coordinate space
732 // transform the dirty region by the surface's transformation
733 // and the global transformation.
734 const Layer::State& s(drawingState());
735 const Transform tr(planeTransform * s.transform);
736 dirtyRegion = tr.transform(dirtyRegion);
737
738 // At this point, the dirty region is in screen space.
739 // Make sure it's constrained by the visible region (which
740 // is in screen space as well).
741 dirtyRegion.andSelf(visibleRegionScreen);
742 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800744 if (visibleRegionScreen.isEmpty()) {
745 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700746 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800747 mFreezeLock.clear();
748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749}
750
Mathias Agopian9bce8732010-04-20 17:55:49 -0700751void Layer::dump(String8& result, char* buffer, size_t SIZE) const
752{
753 LayerBaseClient::dump(result, buffer, SIZE);
754
Mathias Agopian7623da42010-06-01 15:12:58 -0700755 ClientRef::Access sharedClient(mUserClientRef);
756 SharedBufferServer* lcblk(sharedClient.get());
757 uint32_t totalTime = 0;
758 if (lcblk) {
759 SharedBufferStack::Statistics stats = lcblk->getStats();
760 totalTime= stats.totalTime;
761 result.append( lcblk->dump(" ") );
762 }
763
Mathias Agopian9bce8732010-04-20 17:55:49 -0700764 sp<const GraphicBuffer> buf0(getBuffer(0));
765 sp<const GraphicBuffer> buf1(getBuffer(1));
766 uint32_t w0=0, h0=0, s0=0;
767 uint32_t w1=0, h1=0, s1=0;
768 if (buf0 != 0) {
769 w0 = buf0->getWidth();
770 h0 = buf0->getHeight();
771 s0 = buf0->getStride();
772 }
773 if (buf1 != 0) {
774 w1 = buf1->getWidth();
775 h1 = buf1->getHeight();
776 s1 = buf1->getStride();
777 }
778 snprintf(buffer, SIZE,
779 " "
780 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
781 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopian7623da42010-06-01 15:12:58 -0700782 mFormat, w0, h0, s0, w1, h1, s1,
783 getFreezeLock().get(), totalTime);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700784
785 result.append(buffer);
786}
787
Mathias Agopian1473f462009-04-10 14:24:30 -0700788// ---------------------------------------------------------------------------
789
Mathias Agopian7623da42010-06-01 15:12:58 -0700790Layer::ClientRef::ClientRef()
Mathias Agopian5e140102010-06-08 19:54:15 -0700791 : mControlBlock(0), mToken(-1) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700792}
793
794Layer::ClientRef::~ClientRef() {
Mathias Agopian7623da42010-06-01 15:12:58 -0700795}
796
797int32_t Layer::ClientRef::getToken() const {
798 Mutex::Autolock _l(mLock);
799 return mToken;
800}
801
Mathias Agopian5e140102010-06-08 19:54:15 -0700802sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700803 Mutex::Autolock _l(mLock);
Mathias Agopian5e140102010-06-08 19:54:15 -0700804 return mUserClient.promote();
805}
806
807status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
808 const sp<SharedBufferServer>& sharedClient, int32_t token) {
809 Mutex::Autolock _l(mLock);
810
811 { // scope for strong mUserClient reference
812 sp<UserClient> userClient(mUserClient.promote());
813 if (mUserClient != 0 && mControlBlock != 0) {
814 mControlBlock->setStatus(NO_INIT);
815 }
816 }
817
Mathias Agopian7623da42010-06-01 15:12:58 -0700818 mUserClient = uc;
819 mToken = token;
Mathias Agopian5e140102010-06-08 19:54:15 -0700820 mControlBlock = sharedClient;
Mathias Agopian7623da42010-06-01 15:12:58 -0700821 return NO_ERROR;
822}
823
824sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
825 return mUserClient.promote();
826}
827
828// this class gives us access to SharedBufferServer safely
829// it makes sure the UserClient (and its associated shared memory)
830// won't go away while we're accessing it.
831Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian5e140102010-06-08 19:54:15 -0700832 : mControlBlock(0)
Mathias Agopian7623da42010-06-01 15:12:58 -0700833{
834 Mutex::Autolock _l(ref.mLock);
835 mUserClientStrongRef = ref.mUserClient.promote();
836 if (mUserClientStrongRef != 0)
Mathias Agopian5e140102010-06-08 19:54:15 -0700837 mControlBlock = ref.mControlBlock;
838}
839
840Layer::ClientRef::Access::~Access()
841{
Mathias Agopian7623da42010-06-01 15:12:58 -0700842}
843
844// ---------------------------------------------------------------------------
845
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700846Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700847 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800848 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700849{
850}
851
Mathias Agopian898c4c92010-05-18 17:06:55 -0700852Layer::BufferManager::~BufferManager()
853{
854}
855
Jamie Gennis6c925d02010-11-02 11:51:32 -0700856status_t Layer::BufferManager::resize(size_t size,
857 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700858{
859 Mutex::Autolock _l(mLock);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700860
861 if (size < mNumBuffers) {
862 // Move the active texture into slot 0
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800863 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
864 mBufferData[mActiveBufferIndex] = mBufferData[0];
Jamie Gennis6c925d02010-11-02 11:51:32 -0700865 mBufferData[0] = activeBufferData;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800866 mActiveBufferIndex = 0;
Jamie Gennis6c925d02010-11-02 11:51:32 -0700867
868 // Free the buffers that are no longer needed.
869 for (size_t i = size; i < mNumBuffers; i++) {
870 mBufferData[i].buffer = 0;
871
872 // Create a message to destroy the textures on SurfaceFlinger's GL
873 // thread.
874 class MessageDestroyTexture : public MessageBase {
875 Image mTexture;
876 EGLDisplay mDpy;
877 public:
878 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
879 : mTexture(texture), mDpy(dpy) { }
880 virtual bool handler() {
881 status_t err = Layer::BufferManager::destroyTexture(
882 &mTexture, mDpy);
883 LOGE_IF(err<0, "error destroying texture: %d (%s)",
884 mTexture.name, strerror(-err));
885 return true; // XXX: err == 0; ????
886 }
887 };
888
889 MessageDestroyTexture *msg = new MessageDestroyTexture(
890 mBufferData[i].texture, dpy);
891
892 // Don't allow this texture to be cleaned up by
893 // BufferManager::destroy.
894 mBufferData[i].texture.name = -1U;
895 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
896
897 // Post the message to the SurfaceFlinger object.
898 flinger->postMessageAsync(msg);
899 }
900 }
901
Mathias Agopian898c4c92010-05-18 17:06:55 -0700902 mNumBuffers = size;
903 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700904}
905
906// only for debugging
907sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
908 return mBufferData[index].buffer;
909}
910
911status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800912 BufferData const * const buffers = mBufferData;
913 Mutex::Autolock _l(mLock);
914 mActiveBuffer = buffers[index].buffer;
915 mActiveBufferIndex = index;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700916 return NO_ERROR;
917}
918
919size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800920 return mActiveBufferIndex;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700921}
922
923Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700924 Texture res;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800925 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700926 res = mFailoverTexture;
927 } else {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800928 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700929 }
930 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700931}
932
933sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800934 return mActiveBuffer;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700935}
936
Mathias Agopian575eaf52010-12-13 18:51:59 -0800937bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800938 return mActiveBufferIndex >= 0;
Mathias Agopian575eaf52010-12-13 18:51:59 -0800939}
940
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700941sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
942{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700943 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700944 sp<GraphicBuffer> buffer;
945 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700946 buffer = buffers[index].buffer;
947 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700948 return buffer;
949}
950
951status_t Layer::BufferManager::attachBuffer(size_t index,
952 const sp<GraphicBuffer>& buffer)
953{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700954 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700955 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700956 buffers[index].buffer = buffer;
957 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700958 return NO_ERROR;
959}
960
961status_t Layer::BufferManager::destroy(EGLDisplay dpy)
962{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700963 BufferData* const buffers = mBufferData;
964 size_t num;
965 { // scope for the lock
966 Mutex::Autolock _l(mLock);
967 num = mNumBuffers;
968 for (size_t i=0 ; i<num ; i++) {
969 buffers[i].buffer = 0;
970 }
971 }
972 for (size_t i=0 ; i<num ; i++) {
973 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700974 }
975 destroyTexture(&mFailoverTexture, dpy);
976 return NO_ERROR;
977}
978
979status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
980 const sp<GraphicBuffer>& buffer)
981{
Mathias Agopian7623da42010-06-01 15:12:58 -0700982 status_t err = NO_INIT;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800983 ssize_t index = mActiveBufferIndex;
Mathias Agopian7623da42010-06-01 15:12:58 -0700984 if (index >= 0) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700985 if (!mFailover) {
986 Image& texture(mBufferData[index].texture);
987 err = mTextureManager.initEglImage(&texture, dpy, buffer);
988 // if EGLImage fails, we switch to regular texture mode, and we
989 // free all resources associated with using EGLImages.
990 if (err == NO_ERROR) {
991 mFailover = false;
992 destroyTexture(&mFailoverTexture, dpy);
993 } else {
994 mFailover = true;
995 const size_t num = mNumBuffers;
996 for (size_t i=0 ; i<num ; i++) {
997 destroyTexture(&mBufferData[i].texture, dpy);
998 }
Andreas Huber330dd302010-06-25 09:25:19 -0700999 }
Mathias Agopian781953d2010-06-25 18:02:21 -07001000 } else {
1001 // we failed once, don't try again
1002 err = BAD_VALUE;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001003 }
1004 }
1005 return err;
1006}
1007
1008status_t Layer::BufferManager::loadTexture(
1009 const Region& dirty, const GGLSurface& t)
1010{
1011 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1012}
1013
Mathias Agopian898c4c92010-05-18 17:06:55 -07001014status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1015{
1016 if (tex->name != -1U) {
1017 glDeleteTextures(1, &tex->name);
1018 tex->name = -1U;
1019 }
1020 if (tex->image != EGL_NO_IMAGE_KHR) {
1021 eglDestroyImageKHR(dpy, tex->image);
1022 tex->image = EGL_NO_IMAGE_KHR;
1023 }
1024 return NO_ERROR;
1025}
1026
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001027// ---------------------------------------------------------------------------
1028
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001029Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001030 const sp<Layer>& owner)
1031 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001032{
1033}
1034
1035Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -07001036{
1037}
1038
Mathias Agopian2be352a2010-05-21 17:24:35 -07001039sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1040 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -07001041{
Mathias Agopian6950e422009-10-05 17:07:12 -07001042 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -07001043 sp<Layer> owner(getOwner());
1044 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001045 /*
1046 * requestBuffer() cannot be called from the main thread
1047 * as it could cause a dead-lock, since it may have to wait
1048 * on conditions updated my the main thread.
1049 */
Mathias Agopian2be352a2010-05-21 17:24:35 -07001050 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -07001051 }
1052 return buffer;
1053}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054
Mathias Agopian59751db2010-05-07 15:58:44 -07001055status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1056{
1057 status_t err = DEAD_OBJECT;
1058 sp<Layer> owner(getOwner());
1059 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001060 /*
1061 * setBufferCount() cannot be called from the main thread
1062 * as it could cause a dead-lock, since it may have to wait
1063 * on conditions updated my the main thread.
1064 */
Mathias Agopian59751db2010-05-07 15:58:44 -07001065 err = owner->setBufferCount(bufferCount);
1066 }
1067 return err;
1068}
1069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070// ---------------------------------------------------------------------------
1071
1072
1073}; // namespace android