blob: 99d904df809ac8fe6f7db8aa324757f546901bd9 [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),
Mathias Agopian1989af22010-12-02 21:32:29 -080062 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064}
65
66Layer::~Layer()
67{
Mathias Agopian898c4c92010-05-18 17:06:55 -070068 // FIXME: must be called from the main UI thread
69 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
70 mBufferManager.destroy(dpy);
71
Mathias Agopian7623da42010-06-01 15:12:58 -070072 // we can use getUserClientUnsafe here because we know we're
73 // single-threaded at that point.
74 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
75 if (ourClient != 0) {
76 ourClient->detachLayer(this);
77 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070078}
79
Mathias Agopian7623da42010-06-01 15:12:58 -070080status_t Layer::setToken(const sp<UserClient>& userClient,
81 SharedClient* sharedClient, int32_t token)
Mathias Agopian593c05c2010-06-02 23:28:45 -070082{
Mathias Agopian5e140102010-06-08 19:54:15 -070083 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopian7623da42010-06-01 15:12:58 -070084 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian593c05c2010-06-02 23:28:45 -070085 getIdentity());
86
Mathias Agopian5e140102010-06-08 19:54:15 -070087
Mathias Agopian5747bbc2010-12-13 16:49:05 -080088 sp<UserClient> ourClient(mUserClientRef.getClient());
89
90 /*
91 * Here it is guaranteed that userClient != ourClient
92 * (see UserClient::getTokenForSurface()).
93 *
94 * We release the token used by this surface in ourClient below.
95 * This should be safe to do so now, since this layer won't be attached
96 * to this client, it should be okay to reuse that id.
97 *
98 * If this causes problems, an other solution would be to keep a list
99 * of all the {UserClient, token} ever used and release them when the
100 * Layer is destroyed.
101 *
102 */
103
104 if (ourClient != 0) {
105 ourClient->detachLayer(this);
106 }
107
108 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian5e140102010-06-08 19:54:15 -0700109 LOGE_IF(err != NO_ERROR,
110 "ClientRef::setToken(%p, %p, %u) failed",
111 userClient.get(), lcblk.get(), token);
112
113 if (err == NO_ERROR) {
114 // we need to free the buffers associated with this surface
Mathias Agopian7623da42010-06-01 15:12:58 -0700115 }
116
117 return err;
118}
119
120int32_t Layer::getToken() const
121{
122 return mUserClientRef.getToken();
Mathias Agopian593c05c2010-06-02 23:28:45 -0700123}
124
Mathias Agopian5e140102010-06-08 19:54:15 -0700125sp<UserClient> Layer::getClient() const
126{
127 return mUserClientRef.getClient();
128}
129
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700130// called with SurfaceFlinger::mStateLock as soon as the layer is entered
131// in the purgatory list
132void Layer::onRemoved()
133{
Mathias Agopian7623da42010-06-01 15:12:58 -0700134 ClientRef::Access sharedClient(mUserClientRef);
135 SharedBufferServer* lcblk(sharedClient.get());
136 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700137 // wake up the condition
138 lcblk->setStatus(NO_INIT);
139 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700140}
Mathias Agopian9779b222009-09-07 16:32:45 -0700141
Mathias Agopian1473f462009-04-10 14:24:30 -0700142sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143{
Mathias Agopian1b0114f2011-02-15 19:01:06 -0800144 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
145 return sur;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146}
147
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700148status_t Layer::ditch()
149{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700150 // NOTE: Called from the main UI thread
151
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700152 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -0800153 mFreezeLock.clear();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700154
Mathias Agopian898c4c92010-05-18 17:06:55 -0700155 Mutex::Autolock _l(mLock);
156 mWidth = mHeight = 0;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700157 return NO_ERROR;
158}
159
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700160status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 PixelFormat format, uint32_t flags)
162{
Mathias Agopiancc934762009-09-23 19:16:27 -0700163 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 PixelFormatInfo info;
165 status_t err = getPixelFormatInfo(format, &info);
166 if (err) return err;
167
Mathias Agopiancc934762009-09-23 19:16:27 -0700168 // the display's pixel format
169 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-04-14 16:43:44 -0700170 uint32_t const maxSurfaceDims = min(
171 hw.getMaxTextureSize(), hw.getMaxViewportDims());
172
173 // never allow a surface larger than what our underlying GL implementation
174 // can handle.
175 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
176 return BAD_VALUE;
177 }
178
Mathias Agopiancc934762009-09-23 19:16:27 -0700179 PixelFormatInfo displayInfo;
180 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700181 const uint32_t hwFlags = hw.getFlags();
182
Mathias Agopian9779b222009-09-07 16:32:45 -0700183 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700184 mWidth = w;
Mathias Agopian9779b222009-09-07 16:32:45 -0700185 mHeight = h;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700186
187 mReqFormat = format;
188 mReqWidth = w;
189 mReqHeight = h;
190
Mathias Agopian6950e422009-10-05 17:07:12 -0700191 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800192 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guyd10cd572010-10-10 13:33:22 -0700193 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
194 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700195
Mathias Agopiancc934762009-09-23 19:16:27 -0700196 // we use the red index
197 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
198 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
199 mNeedsDithering = layerRedsize > displayRedSize;
200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 return NO_ERROR;
202}
203
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700204void Layer::setGeometry(hwc_layer_t* hwcl)
205{
206 hwcl->compositionType = HWC_FRAMEBUFFER;
207 hwcl->hints = 0;
208 hwcl->flags = 0;
209 hwcl->transform = 0;
210 hwcl->blending = HWC_BLENDING_NONE;
211
212 // we can't do alpha-fade with the hwc HAL
213 const State& s(drawingState());
214 if (s.alpha < 0xFF) {
215 hwcl->flags = HWC_SKIP_LAYER;
216 return;
217 }
218
219 // we can only handle simple transformation
220 if (mOrientation & Transform::ROT_INVALID) {
221 hwcl->flags = HWC_SKIP_LAYER;
222 return;
223 }
224
Mathias Agopian8128ee82010-12-08 17:23:18 -0800225 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
226 hwcl->transform = tr.getOrientation();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700227
228 if (needsBlending()) {
229 hwcl->blending = mPremultipliedAlpha ?
230 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
231 }
232
233 hwcl->displayFrame.left = mTransformedBounds.left;
234 hwcl->displayFrame.top = mTransformedBounds.top;
235 hwcl->displayFrame.right = mTransformedBounds.right;
236 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
237
238 hwcl->visibleRegionScreen.rects =
239 reinterpret_cast<hwc_rect_t const *>(
240 visibleRegionScreen.getArray(
241 &hwcl->visibleRegionScreen.numRects));
242}
243
244void Layer::setPerFrameData(hwc_layer_t* hwcl) {
245 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
246 if (buffer == NULL) {
Mathias Agopian575eaf52010-12-13 18:51:59 -0800247 // this can happen if the client never drew into this layer yet,
248 // or if we ran out of memory. In that case, don't let
249 // HWC handle it.
250 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700251 hwcl->handle = NULL;
252 return;
253 }
Louis Huemiller2dd198b2010-12-01 12:29:36 -0800254 hwcl->handle = buffer->handle;
Mathias Agopianac843f22010-12-08 17:40:28 -0800255
256 if (!mBufferCrop.isEmpty()) {
257 hwcl->sourceCrop.left = mBufferCrop.left;
258 hwcl->sourceCrop.top = mBufferCrop.top;
259 hwcl->sourceCrop.right = mBufferCrop.right;
260 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
261 } else {
262 hwcl->sourceCrop.left = 0;
263 hwcl->sourceCrop.top = 0;
264 hwcl->sourceCrop.right = buffer->width;
265 hwcl->sourceCrop.bottom = buffer->height;
266 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700267}
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269void Layer::reloadTexture(const Region& dirty)
270{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700271 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-12-10 15:52:29 -0800272 if (buffer == NULL) {
273 // this situation can happen if we ran out of memory for instance.
274 // not much we can do. continue to use whatever texture was bound
275 // to this context.
276 return;
277 }
278
Mathias Agopian781953d2010-06-25 18:02:21 -0700279 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700280 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
281 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
282 // not sure what we can do here...
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700283 goto slowpath;
Mathias Agopian1473f462009-04-10 14:24:30 -0700284 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700285 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800286slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700287 GGLSurface t;
Mathias Agopianc817b222010-08-20 15:59:53 -0700288 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
289 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
290 LOGE_IF(res, "error %d (%s) locking buffer %p",
291 res, strerror(res), buffer.get());
292 if (res == NO_ERROR) {
293 mBufferManager.loadTexture(dirty, t);
294 buffer->unlock();
295 }
296 } else {
297 // we can't do anything
Mathias Agopian1473f462009-04-10 14:24:30 -0700298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300}
301
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700302void Layer::drawForSreenShot() const
303{
Mathias Agopian1989af22010-12-02 21:32:29 -0800304 const bool currentFiltering = mNeedsFiltering;
305 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700306 LayerBase::drawForSreenShot();
Mathias Agopian1989af22010-12-02 21:32:29 -0800307 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700308}
309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310void Layer::onDraw(const Region& clip) const
311{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700312 Texture tex(mBufferManager.getActiveTexture());
313 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700315 // in fact never been drawn into. This happens frequently with
316 // SurfaceView because the WindowManager can't know when the client
317 // has drawn the first time.
318
319 // If there is nothing under us, we paint the screen in black, otherwise
320 // we just skip this update.
321
322 // figure out if there is something below us
323 Region under;
324 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
325 const size_t count = drawingLayers.size();
326 for (size_t i=0 ; i<count ; ++i) {
327 const sp<LayerBase>& layer(drawingLayers[i]);
328 if (layer.get() == static_cast<LayerBase const*>(this))
329 break;
330 under.orSelf(layer->visibleRegionScreen);
331 }
332 // if not everything below us is covered, we plug the holes!
333 Region holes(clip.subtract(under));
334 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700335 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 return;
338 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700339 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340}
341
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800342// As documented in libhardware header, formats in the range
343// 0x100 - 0x1FF are specific to the HAL implementation, and
344// are known to have no alpha channel
345// TODO: move definition for device-specific range into
346// hardware.h, instead of using hard-coded values here.
347#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
348
349bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
350{
351 // If buffers where set with eOpaque flag, all buffers are known to
352 // be opaque without having to check their actual format
353 if (mNeedsBlending && buffer != NULL) {
354 PixelFormat format = buffer->getPixelFormat();
355
356 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
357 return false;
358 }
359
360 PixelFormatInfo info;
361 status_t err = getPixelFormatInfo(format, &info);
362 if (!err && info.h_alpha <= info.l_alpha) {
363 return false;
364 }
365 }
366
367 // Return opacity as determined from flags and format options
368 // passed to setBuffers()
369 return mNeedsBlending;
370}
371
372bool Layer::needsBlending() const
373{
374 if (mBufferManager.hasActiveBuffer()) {
375 return needsBlending(mBufferManager.getActiveBuffer());
376 }
377
378 return mNeedsBlending;
379}
380
Mathias Agopian92377032010-05-26 22:08:52 -0700381bool Layer::needsFiltering() const
382{
383 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian1989af22010-12-02 21:32:29 -0800384 // if our buffer is not the same size than ourselves,
385 // we need filtering.
386 Mutex::Autolock _l(mLock);
387 if (mNeedsScaling)
Mathias Agopian92377032010-05-26 22:08:52 -0700388 return true;
389 }
390 return LayerBase::needsFiltering();
391}
392
Jamie Gennisf72606c2011-03-09 17:05:02 -0800393bool Layer::isProtected() const
394{
395 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
396 return (activeBuffer != 0) &&
397 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
398}
Mathias Agopian59751db2010-05-07 15:58:44 -0700399
400status_t Layer::setBufferCount(int bufferCount)
401{
Mathias Agopian7623da42010-06-01 15:12:58 -0700402 ClientRef::Access sharedClient(mUserClientRef);
403 SharedBufferServer* lcblk(sharedClient.get());
404 if (!lcblk) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700405 // oops, the client is already gone
406 return DEAD_OBJECT;
407 }
408
Mathias Agopian898c4c92010-05-18 17:06:55 -0700409 // NOTE: lcblk->resize() is protected by an internal lock
410 status_t err = lcblk->resize(bufferCount);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700411 if (err == NO_ERROR) {
412 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
413 mBufferManager.resize(bufferCount, mFlinger, dpy);
414 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700415
416 return err;
417}
418
Mathias Agopian2be352a2010-05-21 17:24:35 -0700419sp<GraphicBuffer> Layer::requestBuffer(int index,
420 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
421 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422{
Mathias Agopian6950e422009-10-05 17:07:12 -0700423 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700424
Mathias Agopian7623da42010-06-01 15:12:58 -0700425 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopian2be352a2010-05-21 17:24:35 -0700426 return buffer;
427
428 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
429 return buffer;
430
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700431 // this ensures our client doesn't go away while we're accessing
432 // the shared area.
Mathias Agopian7623da42010-06-01 15:12:58 -0700433 ClientRef::Access sharedClient(mUserClientRef);
434 SharedBufferServer* lcblk(sharedClient.get());
435 if (!lcblk) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700436 // oops, the client is already gone
437 return buffer;
438 }
439
Mathias Agopian1473f462009-04-10 14:24:30 -0700440 /*
Mathias Agopian9779b222009-09-07 16:32:45 -0700441 * This is called from the client's Surface::dequeue(). This can happen
442 * at any time, especially while we're in the middle of using the
443 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700444 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
Mathias Agopian51c70e32010-07-27 20:11:35 -0700446 status_t err = NO_ERROR;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700447 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700448 { // scope for the lock
449 Mutex::Autolock _l(mLock);
Mathias Agopianc51114f2010-08-25 14:59:15 -0700450
451 // zero means default
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700452 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700453 if (!reqFormat) reqFormat = mFormat;
454 if (!reqWidth) reqWidth = mWidth;
455 if (!reqHeight) reqHeight = mHeight;
456
457 w = reqWidth;
458 h = reqHeight;
459 f = reqFormat;
460
461 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
462 (reqFormat != mReqFormat)) {
463 mReqWidth = reqWidth;
464 mReqHeight = reqHeight;
465 mReqFormat = reqFormat;
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700466 mFixedSize = fixedSize;
Mathias Agopian1989af22010-12-02 21:32:29 -0800467 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700468
Mathias Agopian2be352a2010-05-21 17:24:35 -0700469 lcblk->reallocateAllExcept(index);
470 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700471 }
472
Mathias Agopian51c70e32010-07-27 20:11:35 -0700473 // here we have to reallocate a new buffer because the buffer could be
474 // used as the front buffer, or by a client in our process
475 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian6950e422009-10-05 17:07:12 -0700476 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian51c70e32010-07-27 20:11:35 -0700477 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
478 err = buffer->initCheck();
Mathias Agopian9779b222009-09-07 16:32:45 -0700479
480 if (err || buffer->handle == 0) {
Mathias Agopiane869aee2010-12-03 17:33:09 -0800481 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopian9779b222009-09-07 16:32:45 -0700482 LOGE_IF(err || buffer->handle == 0,
483 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
484 this, index, w, h, strerror(-err));
485 } else {
486 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700487 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
488 this, index, w, h, buffer->handle);
Mathias Agopian9779b222009-09-07 16:32:45 -0700489 }
490
491 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700492 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700493 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700494 }
495 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496}
497
Mathias Agopian6950e422009-10-05 17:07:12 -0700498uint32_t Layer::getEffectiveUsage(uint32_t usage) const
499{
500 /*
501 * buffers used for software rendering, but h/w composition
502 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
503 *
504 * buffers used for h/w rendering and h/w composition
505 * are allocated with HW_RENDER | HW_TEXTURE
506 *
507 * buffers used with h/w rendering and either NPOT or no egl_image_ext
508 * are allocated with SW_READ_RARELY | HW_RENDER
509 *
510 */
511
512 if (mSecure) {
513 // secure buffer, don't store it into the GPU
514 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
515 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
516 } else {
517 // it's allowed to modify the usage flags here, but generally
518 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700519 // request EGLImage for all buffers
520 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700521 }
Jamie Gennisf72606c2011-03-09 17:05:02 -0800522 if (mProtectedByApp) {
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800523 // need a hardware-protected path to external video sink
524 usage |= GraphicBuffer::USAGE_PROTECTED;
525 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700526 return usage;
527}
528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529uint32_t Layer::doTransaction(uint32_t flags)
530{
531 const Layer::State& front(drawingState());
532 const Layer::State& temp(currentState());
533
Mathias Agopian2be352a2010-05-21 17:24:35 -0700534 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
535 (front.requested_h != temp.requested_h);
536
537 if (sizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700538 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700540 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
541 this,
542 int(temp.requested_w), int(temp.requested_h),
543 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
Mathias Agopian2be352a2010-05-21 17:24:35 -0700545 if (!isFixedSize()) {
546 // we're being resized and there is a freeze display request,
547 // acquire a freeze lock, so that the screen stays put
548 // until we've redrawn at the new size; this is to avoid
549 // glitches upon orientation changes.
550 if (mFlinger->hasFreezeRequest()) {
551 // if the surface is hidden, don't try to acquire the
552 // freeze lock, since hidden surfaces may never redraw
553 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
554 mFreezeLock = mFlinger->getFreezeLock();
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700557
558 // this will make sure LayerBase::doTransaction doesn't update
559 // the drawing state's size
560 Layer::State& editDraw(mDrawingState);
561 editDraw.requested_w = temp.requested_w;
562 editDraw.requested_h = temp.requested_h;
563
564 // record the new size, form this point on, when the client request
565 // a buffer, it'll get the new size.
566 setBufferSize(temp.requested_w, temp.requested_h);
567
Mathias Agopian7623da42010-06-01 15:12:58 -0700568 ClientRef::Access sharedClient(mUserClientRef);
569 SharedBufferServer* lcblk(sharedClient.get());
570 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700571 // all buffers need reallocation
572 lcblk->reallocateAll();
573 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700574 } else {
575 // record the new size
576 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (temp.sequence != front.sequence) {
581 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
582 // this surface is now hidden, so it shouldn't hold a freeze lock
583 // (it may never redraw, which is fine if it is hidden)
584 mFreezeLock.clear();
585 }
586 }
587
588 return LayerBase::doTransaction(flags);
589}
590
Mathias Agopian2be352a2010-05-21 17:24:35 -0700591void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700592 Mutex::Autolock _l(mLock);
593 mWidth = w;
594 mHeight = h;
Mathias Agopian1989af22010-12-02 21:32:29 -0800595 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596}
597
Mathias Agopian2be352a2010-05-21 17:24:35 -0700598bool Layer::isFixedSize() const {
599 Mutex::Autolock _l(mLock);
600 return mFixedSize;
601}
602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603// ----------------------------------------------------------------------------
604// pageflip handling...
605// ----------------------------------------------------------------------------
606
607void Layer::lockPageFlip(bool& recomputeVisibleRegions)
608{
Mathias Agopian7623da42010-06-01 15:12:58 -0700609 ClientRef::Access sharedClient(mUserClientRef);
610 SharedBufferServer* lcblk(sharedClient.get());
611 if (!lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700612 // client died
613 recomputeVisibleRegions = true;
614 return;
615 }
616
Mathias Agopian9779b222009-09-07 16:32:45 -0700617 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700618 if (buf == NOT_ENOUGH_DATA) {
619 // NOTE: This is not an error, it simply means there is nothing to
620 // retire. The buffer is locked because we will use it
Mathias Agopian9779b222009-09-07 16:32:45 -0700621 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 return;
623 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700624
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700625 if (buf < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700626 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700627 mPostedDirtyRegion.clear();
628 return;
629 }
630
Mathias Agopian9779b222009-09-07 16:32:45 -0700631 // we retired a buffer, which becomes the new front buffer
Mathias Agopian575eaf52010-12-13 18:51:59 -0800632
633 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800634 const bool activeBlending =
635 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
636
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700637 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700638 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700639 mPostedDirtyRegion.clear();
640 return;
641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642
Mathias Agopian575eaf52010-12-13 18:51:59 -0800643 if (noActiveBuffer) {
644 // we didn't have an active buffer, we need to recompute
645 // our visible region
646 recomputeVisibleRegions = true;
647 }
648
Mathias Agopian6950e422009-10-05 17:07:12 -0700649 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700650 if (newFrontBuffer != NULL) {
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800651 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
652 // new buffer has different opacity than previous active buffer, need
653 // to recompute visible regions accordingly
654 recomputeVisibleRegions = true;
655 }
656
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700657 // get the dirty region
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700658 // compute the posted region
659 const Region dirty(lcblk->getDirtyRegion(buf));
660 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700661
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700662 // update the layer size and release freeze-lock
663 const Layer::State& front(drawingState());
664 if (newFrontBuffer->getWidth() == front.requested_w &&
665 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700666 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700667 if ((front.w != front.requested_w) ||
668 (front.h != front.requested_h))
669 {
670 // Here we pretend the transaction happened by updating the
671 // current and drawing states. Drawing state is only accessed
672 // in this thread, no need to have it locked
673 Layer::State& editDraw(mDrawingState);
674 editDraw.w = editDraw.requested_w;
675 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700676
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700677 // We also need to update the current state so that we don't
678 // end-up doing too much work during the next transaction.
679 // NOTE: We actually don't need hold the transaction lock here
680 // because State::w and State::h are only accessed from
681 // this thread
682 Layer::State& editTemp(currentState());
683 editTemp.w = editDraw.w;
684 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700685
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700686 // recompute visible region
687 recomputeVisibleRegions = true;
688 }
689
690 // we now have the correct size, unfreeze the screen
691 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700692 }
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700693
694 // get the crop region
695 setBufferCrop( lcblk->getCrop(buf) );
696
697 // get the transformation
698 setBufferTransform( lcblk->getTransform(buf) );
699
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700700 } else {
701 // this should not happen unless we ran out of memory while
702 // allocating the buffer. we're hoping that things will get back
703 // to normal the next time the app tries to draw into this buffer.
704 // meanwhile, pretend the screen didn't update.
705 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700706 }
707
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700708 if (lcblk->getQueuedCount()) {
709 // signal an event if we have more buffers waiting
710 mFlinger->signalEvent();
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700713 /* a buffer was posted, so we need to call reloadTexture(), which
714 * will update our internal data structures (eg: EGLImageKHR or
715 * texture names). we need to do this even if mPostedDirtyRegion is
716 * empty -- it's orthogonal to the fact that a new buffer was posted,
717 * for instance, a degenerate case could be that the user did an empty
718 * update but repainted the buffer with appropriate content (after a
719 * resize for instance).
720 */
721 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722}
723
724void Layer::unlockPageFlip(
725 const Transform& planeTransform, Region& outDirtyRegion)
726{
727 Region dirtyRegion(mPostedDirtyRegion);
728 if (!dirtyRegion.isEmpty()) {
729 mPostedDirtyRegion.clear();
730 // The dirty region is given in the layer's coordinate space
731 // transform the dirty region by the surface's transformation
732 // and the global transformation.
733 const Layer::State& s(drawingState());
734 const Transform tr(planeTransform * s.transform);
735 dirtyRegion = tr.transform(dirtyRegion);
736
737 // At this point, the dirty region is in screen space.
738 // Make sure it's constrained by the visible region (which
739 // is in screen space as well).
740 dirtyRegion.andSelf(visibleRegionScreen);
741 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800743 if (visibleRegionScreen.isEmpty()) {
744 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700745 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800746 mFreezeLock.clear();
747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748}
749
Mathias Agopian9bce8732010-04-20 17:55:49 -0700750void Layer::dump(String8& result, char* buffer, size_t SIZE) const
751{
752 LayerBaseClient::dump(result, buffer, SIZE);
753
Mathias Agopian7623da42010-06-01 15:12:58 -0700754 ClientRef::Access sharedClient(mUserClientRef);
755 SharedBufferServer* lcblk(sharedClient.get());
756 uint32_t totalTime = 0;
757 if (lcblk) {
758 SharedBufferStack::Statistics stats = lcblk->getStats();
759 totalTime= stats.totalTime;
760 result.append( lcblk->dump(" ") );
761 }
762
Mathias Agopian9bce8732010-04-20 17:55:49 -0700763 sp<const GraphicBuffer> buf0(getBuffer(0));
764 sp<const GraphicBuffer> buf1(getBuffer(1));
765 uint32_t w0=0, h0=0, s0=0;
766 uint32_t w1=0, h1=0, s1=0;
767 if (buf0 != 0) {
768 w0 = buf0->getWidth();
769 h0 = buf0->getHeight();
770 s0 = buf0->getStride();
771 }
772 if (buf1 != 0) {
773 w1 = buf1->getWidth();
774 h1 = buf1->getHeight();
775 s1 = buf1->getStride();
776 }
777 snprintf(buffer, SIZE,
778 " "
779 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
780 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopian7623da42010-06-01 15:12:58 -0700781 mFormat, w0, h0, s0, w1, h1, s1,
782 getFreezeLock().get(), totalTime);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700783
784 result.append(buffer);
785}
786
Mathias Agopian1473f462009-04-10 14:24:30 -0700787// ---------------------------------------------------------------------------
788
Mathias Agopian7623da42010-06-01 15:12:58 -0700789Layer::ClientRef::ClientRef()
Mathias Agopian5e140102010-06-08 19:54:15 -0700790 : mControlBlock(0), mToken(-1) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700791}
792
793Layer::ClientRef::~ClientRef() {
Mathias Agopian7623da42010-06-01 15:12:58 -0700794}
795
796int32_t Layer::ClientRef::getToken() const {
797 Mutex::Autolock _l(mLock);
798 return mToken;
799}
800
Mathias Agopian5e140102010-06-08 19:54:15 -0700801sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700802 Mutex::Autolock _l(mLock);
Mathias Agopian5e140102010-06-08 19:54:15 -0700803 return mUserClient.promote();
804}
805
806status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
807 const sp<SharedBufferServer>& sharedClient, int32_t token) {
808 Mutex::Autolock _l(mLock);
809
810 { // scope for strong mUserClient reference
811 sp<UserClient> userClient(mUserClient.promote());
Jamie Gennis6912daf2011-03-18 16:35:13 -0700812 if (userClient != 0 && mControlBlock != 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -0700813 mControlBlock->setStatus(NO_INIT);
814 }
815 }
816
Mathias Agopian7623da42010-06-01 15:12:58 -0700817 mUserClient = uc;
818 mToken = token;
Mathias Agopian5e140102010-06-08 19:54:15 -0700819 mControlBlock = sharedClient;
Mathias Agopian7623da42010-06-01 15:12:58 -0700820 return NO_ERROR;
821}
822
823sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
824 return mUserClient.promote();
825}
826
827// this class gives us access to SharedBufferServer safely
828// it makes sure the UserClient (and its associated shared memory)
829// won't go away while we're accessing it.
830Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian5e140102010-06-08 19:54:15 -0700831 : mControlBlock(0)
Mathias Agopian7623da42010-06-01 15:12:58 -0700832{
833 Mutex::Autolock _l(ref.mLock);
834 mUserClientStrongRef = ref.mUserClient.promote();
835 if (mUserClientStrongRef != 0)
Mathias Agopian5e140102010-06-08 19:54:15 -0700836 mControlBlock = ref.mControlBlock;
837}
838
839Layer::ClientRef::Access::~Access()
840{
Mathias Agopian7623da42010-06-01 15:12:58 -0700841}
842
843// ---------------------------------------------------------------------------
844
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700845Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700846 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800847 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700848{
849}
850
Mathias Agopian898c4c92010-05-18 17:06:55 -0700851Layer::BufferManager::~BufferManager()
852{
853}
854
Jamie Gennis6c925d02010-11-02 11:51:32 -0700855status_t Layer::BufferManager::resize(size_t size,
856 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700857{
858 Mutex::Autolock _l(mLock);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700859
860 if (size < mNumBuffers) {
Mathias Agopianf40e6382011-03-16 23:18:07 -0700861 // If there is an active texture, move it into slot 0 if needed
862 if (mActiveBufferIndex > 0) {
863 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
864 mBufferData[mActiveBufferIndex] = mBufferData[0];
865 mBufferData[0] = activeBufferData;
866 mActiveBufferIndex = 0;
867 }
Jamie Gennis6c925d02010-11-02 11:51:32 -0700868
869 // Free the buffers that are no longer needed.
870 for (size_t i = size; i < mNumBuffers; i++) {
871 mBufferData[i].buffer = 0;
872
873 // Create a message to destroy the textures on SurfaceFlinger's GL
874 // thread.
875 class MessageDestroyTexture : public MessageBase {
876 Image mTexture;
877 EGLDisplay mDpy;
878 public:
879 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
880 : mTexture(texture), mDpy(dpy) { }
881 virtual bool handler() {
882 status_t err = Layer::BufferManager::destroyTexture(
883 &mTexture, mDpy);
884 LOGE_IF(err<0, "error destroying texture: %d (%s)",
885 mTexture.name, strerror(-err));
886 return true; // XXX: err == 0; ????
887 }
888 };
889
890 MessageDestroyTexture *msg = new MessageDestroyTexture(
891 mBufferData[i].texture, dpy);
892
893 // Don't allow this texture to be cleaned up by
894 // BufferManager::destroy.
895 mBufferData[i].texture.name = -1U;
896 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
897
898 // Post the message to the SurfaceFlinger object.
899 flinger->postMessageAsync(msg);
900 }
901 }
902
Mathias Agopian898c4c92010-05-18 17:06:55 -0700903 mNumBuffers = size;
904 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700905}
906
907// only for debugging
908sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
909 return mBufferData[index].buffer;
910}
911
912status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800913 BufferData const * const buffers = mBufferData;
914 Mutex::Autolock _l(mLock);
915 mActiveBuffer = buffers[index].buffer;
916 mActiveBufferIndex = index;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700917 return NO_ERROR;
918}
919
920size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800921 return mActiveBufferIndex;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700922}
923
924Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700925 Texture res;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800926 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700927 res = mFailoverTexture;
928 } else {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800929 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700930 }
931 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700932}
933
934sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800935 return mActiveBuffer;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700936}
937
Mathias Agopian575eaf52010-12-13 18:51:59 -0800938bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800939 return mActiveBufferIndex >= 0;
Mathias Agopian575eaf52010-12-13 18:51:59 -0800940}
941
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700942sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
943{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700944 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700945 sp<GraphicBuffer> buffer;
946 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700947 buffer = buffers[index].buffer;
948 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700949 return buffer;
950}
951
952status_t Layer::BufferManager::attachBuffer(size_t index,
953 const sp<GraphicBuffer>& buffer)
954{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700955 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700956 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700957 buffers[index].buffer = buffer;
958 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700959 return NO_ERROR;
960}
961
962status_t Layer::BufferManager::destroy(EGLDisplay dpy)
963{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700964 BufferData* const buffers = mBufferData;
965 size_t num;
966 { // scope for the lock
967 Mutex::Autolock _l(mLock);
968 num = mNumBuffers;
969 for (size_t i=0 ; i<num ; i++) {
970 buffers[i].buffer = 0;
971 }
972 }
973 for (size_t i=0 ; i<num ; i++) {
974 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700975 }
976 destroyTexture(&mFailoverTexture, dpy);
977 return NO_ERROR;
978}
979
980status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
981 const sp<GraphicBuffer>& buffer)
982{
Mathias Agopian7623da42010-06-01 15:12:58 -0700983 status_t err = NO_INIT;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800984 ssize_t index = mActiveBufferIndex;
Mathias Agopian7623da42010-06-01 15:12:58 -0700985 if (index >= 0) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700986 if (!mFailover) {
Kobi Cohen Arazi8ecc90d02011-04-15 10:38:33 -0700987 {
988 // Without that lock, there is a chance of race condition
989 // where while composing a specific index, requestBuf
990 // with the same index can be executed and touch the same data
991 // that is being used in initEglImage.
992 // (e.g. dirty flag in texture)
993 Mutex::Autolock _l(mLock);
994 Image& texture(mBufferData[index].texture);
995 err = mTextureManager.initEglImage(&texture, dpy, buffer);
996 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700997 // if EGLImage fails, we switch to regular texture mode, and we
998 // free all resources associated with using EGLImages.
999 if (err == NO_ERROR) {
1000 mFailover = false;
1001 destroyTexture(&mFailoverTexture, dpy);
1002 } else {
1003 mFailover = true;
1004 const size_t num = mNumBuffers;
1005 for (size_t i=0 ; i<num ; i++) {
1006 destroyTexture(&mBufferData[i].texture, dpy);
1007 }
Andreas Huber330dd302010-06-25 09:25:19 -07001008 }
Mathias Agopian781953d2010-06-25 18:02:21 -07001009 } else {
1010 // we failed once, don't try again
1011 err = BAD_VALUE;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001012 }
1013 }
1014 return err;
1015}
1016
1017status_t Layer::BufferManager::loadTexture(
1018 const Region& dirty, const GGLSurface& t)
1019{
1020 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1021}
1022
Mathias Agopian898c4c92010-05-18 17:06:55 -07001023status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1024{
1025 if (tex->name != -1U) {
1026 glDeleteTextures(1, &tex->name);
1027 tex->name = -1U;
1028 }
1029 if (tex->image != EGL_NO_IMAGE_KHR) {
1030 eglDestroyImageKHR(dpy, tex->image);
1031 tex->image = EGL_NO_IMAGE_KHR;
1032 }
1033 return NO_ERROR;
1034}
1035
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001036// ---------------------------------------------------------------------------
1037
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001038Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001039 const sp<Layer>& owner)
1040 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001041{
1042}
1043
1044Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -07001045{
1046}
1047
Mathias Agopian2be352a2010-05-21 17:24:35 -07001048sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1049 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -07001050{
Mathias Agopian6950e422009-10-05 17:07:12 -07001051 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -07001052 sp<Layer> owner(getOwner());
1053 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001054 /*
1055 * requestBuffer() cannot be called from the main thread
1056 * as it could cause a dead-lock, since it may have to wait
1057 * on conditions updated my the main thread.
1058 */
Mathias Agopian2be352a2010-05-21 17:24:35 -07001059 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -07001060 }
1061 return buffer;
1062}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063
Mathias Agopian59751db2010-05-07 15:58:44 -07001064status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1065{
1066 status_t err = DEAD_OBJECT;
1067 sp<Layer> owner(getOwner());
1068 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001069 /*
1070 * setBufferCount() cannot be called from the main thread
1071 * as it could cause a dead-lock, since it may have to wait
1072 * on conditions updated my the main thread.
1073 */
Mathias Agopian59751db2010-05-07 15:58:44 -07001074 err = owner->setBufferCount(bufferCount);
1075 }
1076 return err;
1077}
1078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079// ---------------------------------------------------------------------------
1080
1081
1082}; // namespace android