blob: 4bb256f23c88f47f29d8fcc7c911346917aa9255 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian3330b202009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070038#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40
41#define DEBUG_RESIZE 0
42
43
44namespace android {
45
Mathias Agopianca99fb82010-04-14 16:43:44 -070046template <typename T> inline T min(T a, T b) {
47 return a<b ? a : b;
48}
49
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050// ---------------------------------------------------------------------------
51
Mathias Agopian96f08192010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian401c2572009-09-23 19:16:27 -070056 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070057 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070058 mSecure(false),
Glenn Kasten16f04532011-01-19 15:27:27 -080059 mProtectedByApp(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070060 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070061 mBufferManager(mTextureManager),
Mathias Agopian733189d2010-12-02 21:32:29 -080062 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064}
65
66Layer::~Layer()
67{
Mathias Agopianbb641242010-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 Agopianb7e930d2010-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 Agopiand606de62010-05-10 20:06:11 -070078}
79
Mathias Agopianb4081bb2011-05-19 15:38:14 -070080void Layer::destroy() const {
81 mFlinger->destroyLayer(this);
82}
83
Mathias Agopianb7e930d2010-06-01 15:12:58 -070084status_t Layer::setToken(const sp<UserClient>& userClient,
85 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070086{
Mathias Agopian579b3f82010-06-08 19:54:15 -070087 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070088 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070089 getIdentity());
90
Mathias Agopian579b3f82010-06-08 19:54:15 -070091
Mathias Agopiandd17b3e2010-12-13 16:49:05 -080092 sp<UserClient> ourClient(mUserClientRef.getClient());
93
94 /*
95 * Here it is guaranteed that userClient != ourClient
96 * (see UserClient::getTokenForSurface()).
97 *
98 * We release the token used by this surface in ourClient below.
99 * This should be safe to do so now, since this layer won't be attached
100 * to this client, it should be okay to reuse that id.
101 *
102 * If this causes problems, an other solution would be to keep a list
103 * of all the {UserClient, token} ever used and release them when the
104 * Layer is destroyed.
105 *
106 */
107
108 if (ourClient != 0) {
109 ourClient->detachLayer(this);
110 }
111
112 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700113 LOGE_IF(err != NO_ERROR,
114 "ClientRef::setToken(%p, %p, %u) failed",
115 userClient.get(), lcblk.get(), token);
116
117 if (err == NO_ERROR) {
118 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119 }
120
121 return err;
122}
123
124int32_t Layer::getToken() const
125{
126 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700127}
128
Mathias Agopian579b3f82010-06-08 19:54:15 -0700129sp<UserClient> Layer::getClient() const
130{
131 return mUserClientRef.getClient();
132}
133
Mathias Agopiand606de62010-05-10 20:06:11 -0700134// called with SurfaceFlinger::mStateLock as soon as the layer is entered
135// in the purgatory list
136void Layer::onRemoved()
137{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700138 ClientRef::Access sharedClient(mUserClientRef);
139 SharedBufferServer* lcblk(sharedClient.get());
140 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700141 // wake up the condition
142 lcblk->setStatus(NO_INIT);
143 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700144}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700145
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700146sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147{
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800148 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
149 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Mathias Agopianf9d93272009-06-19 17:00:27 -0700152status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 PixelFormat format, uint32_t flags)
154{
Mathias Agopian401c2572009-09-23 19:16:27 -0700155 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 PixelFormatInfo info;
157 status_t err = getPixelFormatInfo(format, &info);
158 if (err) return err;
159
Mathias Agopian401c2572009-09-23 19:16:27 -0700160 // the display's pixel format
161 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700162 uint32_t const maxSurfaceDims = min(
163 hw.getMaxTextureSize(), hw.getMaxViewportDims());
164
165 // never allow a surface larger than what our underlying GL implementation
166 // can handle.
167 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
168 return BAD_VALUE;
169 }
170
Mathias Agopian401c2572009-09-23 19:16:27 -0700171 PixelFormatInfo displayInfo;
172 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700173 const uint32_t hwFlags = hw.getFlags();
174
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700175 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700176 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700177 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700178
179 mReqFormat = format;
180 mReqWidth = w;
181 mReqHeight = h;
182
Mathias Agopian3330b202009-10-05 17:07:12 -0700183 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800184 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700185 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
186 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700187
Mathias Agopian401c2572009-09-23 19:16:27 -0700188 // we use the red index
189 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
190 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
191 mNeedsDithering = layerRedsize > displayRedSize;
192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 return NO_ERROR;
194}
195
Mathias Agopiana350ff92010-08-10 17:14:02 -0700196void Layer::setGeometry(hwc_layer_t* hwcl)
197{
198 hwcl->compositionType = HWC_FRAMEBUFFER;
199 hwcl->hints = 0;
200 hwcl->flags = 0;
201 hwcl->transform = 0;
202 hwcl->blending = HWC_BLENDING_NONE;
203
204 // we can't do alpha-fade with the hwc HAL
205 const State& s(drawingState());
206 if (s.alpha < 0xFF) {
207 hwcl->flags = HWC_SKIP_LAYER;
208 return;
209 }
210
211 // we can only handle simple transformation
212 if (mOrientation & Transform::ROT_INVALID) {
213 hwcl->flags = HWC_SKIP_LAYER;
214 return;
215 }
216
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800217 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
218 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700219
220 if (needsBlending()) {
221 hwcl->blending = mPremultipliedAlpha ?
222 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
223 }
224
225 hwcl->displayFrame.left = mTransformedBounds.left;
226 hwcl->displayFrame.top = mTransformedBounds.top;
227 hwcl->displayFrame.right = mTransformedBounds.right;
228 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
229
230 hwcl->visibleRegionScreen.rects =
231 reinterpret_cast<hwc_rect_t const *>(
232 visibleRegionScreen.getArray(
233 &hwcl->visibleRegionScreen.numRects));
234}
235
236void Layer::setPerFrameData(hwc_layer_t* hwcl) {
237 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
238 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800239 // this can happen if the client never drew into this layer yet,
240 // or if we ran out of memory. In that case, don't let
241 // HWC handle it.
242 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700243 hwcl->handle = NULL;
244 return;
245 }
Louis Huemiller04048142010-12-01 12:29:36 -0800246 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800247
248 if (!mBufferCrop.isEmpty()) {
249 hwcl->sourceCrop.left = mBufferCrop.left;
250 hwcl->sourceCrop.top = mBufferCrop.top;
251 hwcl->sourceCrop.right = mBufferCrop.right;
252 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
253 } else {
254 hwcl->sourceCrop.left = 0;
255 hwcl->sourceCrop.top = 0;
256 hwcl->sourceCrop.right = buffer->width;
257 hwcl->sourceCrop.bottom = buffer->height;
258 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700259}
260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261void Layer::reloadTexture(const Region& dirty)
262{
Mathias Agopiand606de62010-05-10 20:06:11 -0700263 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800264 if (buffer == NULL) {
265 // this situation can happen if we ran out of memory for instance.
266 // not much we can do. continue to use whatever texture was bound
267 // to this context.
268 return;
269 }
270
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700271 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700272 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
273 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
274 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700275 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700276 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700277 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800278slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700280 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
281 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
282 LOGE_IF(res, "error %d (%s) locking buffer %p",
283 res, strerror(res), buffer.get());
284 if (res == NO_ERROR) {
285 mBufferManager.loadTexture(dirty, t);
286 buffer->unlock();
287 }
288 } else {
289 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292}
293
Mathias Agopian74c40c02010-09-29 13:02:36 -0700294void Layer::drawForSreenShot() const
295{
Mathias Agopian733189d2010-12-02 21:32:29 -0800296 const bool currentFiltering = mNeedsFiltering;
297 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700298 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800299 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700300}
301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302void Layer::onDraw(const Region& clip) const
303{
Mathias Agopiand606de62010-05-10 20:06:11 -0700304 Texture tex(mBufferManager.getActiveTexture());
305 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700307 // in fact never been drawn into. This happens frequently with
308 // SurfaceView because the WindowManager can't know when the client
309 // has drawn the first time.
310
311 // If there is nothing under us, we paint the screen in black, otherwise
312 // we just skip this update.
313
314 // figure out if there is something below us
315 Region under;
316 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
317 const size_t count = drawingLayers.size();
318 for (size_t i=0 ; i<count ; ++i) {
319 const sp<LayerBase>& layer(drawingLayers[i]);
320 if (layer.get() == static_cast<LayerBase const*>(this))
321 break;
322 under.orSelf(layer->visibleRegionScreen);
323 }
324 // if not everything below us is covered, we plug the holes!
325 Region holes(clip.subtract(under));
326 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700327 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700328 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 return;
330 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700331 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332}
333
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800334// As documented in libhardware header, formats in the range
335// 0x100 - 0x1FF are specific to the HAL implementation, and
336// are known to have no alpha channel
337// TODO: move definition for device-specific range into
338// hardware.h, instead of using hard-coded values here.
339#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
340
341bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
342{
343 // If buffers where set with eOpaque flag, all buffers are known to
344 // be opaque without having to check their actual format
345 if (mNeedsBlending && buffer != NULL) {
346 PixelFormat format = buffer->getPixelFormat();
347
348 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
349 return false;
350 }
351
352 PixelFormatInfo info;
353 status_t err = getPixelFormatInfo(format, &info);
354 if (!err && info.h_alpha <= info.l_alpha) {
355 return false;
356 }
357 }
358
359 // Return opacity as determined from flags and format options
360 // passed to setBuffers()
361 return mNeedsBlending;
362}
363
364bool Layer::needsBlending() const
365{
366 if (mBufferManager.hasActiveBuffer()) {
367 return needsBlending(mBufferManager.getActiveBuffer());
368 }
369
370 return mNeedsBlending;
371}
372
Mathias Agopiana7f66922010-05-26 22:08:52 -0700373bool Layer::needsFiltering() const
374{
375 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800376 // if our buffer is not the same size than ourselves,
377 // we need filtering.
378 Mutex::Autolock _l(mLock);
379 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700380 return true;
381 }
382 return LayerBase::needsFiltering();
383}
384
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800385bool Layer::isProtected() const
386{
387 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
388 return (activeBuffer != 0) &&
389 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
390}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700391
392status_t Layer::setBufferCount(int bufferCount)
393{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700394 ClientRef::Access sharedClient(mUserClientRef);
395 SharedBufferServer* lcblk(sharedClient.get());
396 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700397 // oops, the client is already gone
398 return DEAD_OBJECT;
399 }
400
Mathias Agopianbb641242010-05-18 17:06:55 -0700401 // NOTE: lcblk->resize() is protected by an internal lock
402 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700403 if (err == NO_ERROR) {
404 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
405 mBufferManager.resize(bufferCount, mFlinger, dpy);
406 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700407
408 return err;
409}
410
Mathias Agopiana138f892010-05-21 17:24:35 -0700411sp<GraphicBuffer> Layer::requestBuffer(int index,
412 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
413 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414{
Mathias Agopian3330b202009-10-05 17:07:12 -0700415 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700416
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700417 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700418 return buffer;
419
420 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
421 return buffer;
422
Mathias Agopian48d819a2009-09-10 19:41:18 -0700423 // this ensures our client doesn't go away while we're accessing
424 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700425 ClientRef::Access sharedClient(mUserClientRef);
426 SharedBufferServer* lcblk(sharedClient.get());
427 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700428 // oops, the client is already gone
429 return buffer;
430 }
431
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700432 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700433 * This is called from the client's Surface::dequeue(). This can happen
434 * at any time, especially while we're in the middle of using the
435 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700436 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437
Mathias Agopian208cb072010-07-27 20:11:35 -0700438 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700439 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700440 { // scope for the lock
441 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700442
443 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700444 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700445 if (!reqFormat) reqFormat = mFormat;
446 if (!reqWidth) reqWidth = mWidth;
447 if (!reqHeight) reqHeight = mHeight;
448
449 w = reqWidth;
450 h = reqHeight;
451 f = reqFormat;
452
453 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
454 (reqFormat != mReqFormat)) {
455 mReqWidth = reqWidth;
456 mReqHeight = reqHeight;
457 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700458 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800459 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700460
Mathias Agopiana138f892010-05-21 17:24:35 -0700461 lcblk->reallocateAllExcept(index);
462 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700463 }
464
Mathias Agopian208cb072010-07-27 20:11:35 -0700465 // here we have to reallocate a new buffer because the buffer could be
466 // used as the front buffer, or by a client in our process
467 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700468 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700469 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
470 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700471
472 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800473 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700474 LOGE_IF(err || buffer->handle == 0,
475 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
476 this, index, w, h, strerror(-err));
477 } else {
478 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700479 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
480 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700481 }
482
483 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700484 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700485 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486 }
487 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488}
489
Mathias Agopian3330b202009-10-05 17:07:12 -0700490uint32_t Layer::getEffectiveUsage(uint32_t usage) const
491{
492 /*
493 * buffers used for software rendering, but h/w composition
494 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
495 *
496 * buffers used for h/w rendering and h/w composition
497 * are allocated with HW_RENDER | HW_TEXTURE
498 *
499 * buffers used with h/w rendering and either NPOT or no egl_image_ext
500 * are allocated with SW_READ_RARELY | HW_RENDER
501 *
502 */
503
504 if (mSecure) {
505 // secure buffer, don't store it into the GPU
506 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
507 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
508 } else {
509 // it's allowed to modify the usage flags here, but generally
510 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700511 // request EGLImage for all buffers
512 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700513 }
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800514 if (mProtectedByApp) {
Glenn Kasten16f04532011-01-19 15:27:27 -0800515 // need a hardware-protected path to external video sink
516 usage |= GraphicBuffer::USAGE_PROTECTED;
517 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700518 return usage;
519}
520
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521uint32_t Layer::doTransaction(uint32_t flags)
522{
523 const Layer::State& front(drawingState());
524 const Layer::State& temp(currentState());
525
Mathias Agopiana138f892010-05-21 17:24:35 -0700526 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
527 (front.requested_h != temp.requested_h);
528
529 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700530 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700532 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
533 this,
534 int(temp.requested_w), int(temp.requested_h),
535 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536
Mathias Agopiana138f892010-05-21 17:24:35 -0700537 if (!isFixedSize()) {
538 // we're being resized and there is a freeze display request,
539 // acquire a freeze lock, so that the screen stays put
540 // until we've redrawn at the new size; this is to avoid
541 // glitches upon orientation changes.
542 if (mFlinger->hasFreezeRequest()) {
543 // if the surface is hidden, don't try to acquire the
544 // freeze lock, since hidden surfaces may never redraw
545 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
546 mFreezeLock = mFlinger->getFreezeLock();
547 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700549
550 // this will make sure LayerBase::doTransaction doesn't update
551 // the drawing state's size
552 Layer::State& editDraw(mDrawingState);
553 editDraw.requested_w = temp.requested_w;
554 editDraw.requested_h = temp.requested_h;
555
556 // record the new size, form this point on, when the client request
557 // a buffer, it'll get the new size.
558 setBufferSize(temp.requested_w, temp.requested_h);
559
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700560 ClientRef::Access sharedClient(mUserClientRef);
561 SharedBufferServer* lcblk(sharedClient.get());
562 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700563 // all buffers need reallocation
564 lcblk->reallocateAll();
565 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700566 } else {
567 // record the new size
568 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 }
570 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 if (temp.sequence != front.sequence) {
573 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
574 // this surface is now hidden, so it shouldn't hold a freeze lock
575 // (it may never redraw, which is fine if it is hidden)
576 mFreezeLock.clear();
577 }
578 }
579
580 return LayerBase::doTransaction(flags);
581}
582
Mathias Agopiana138f892010-05-21 17:24:35 -0700583void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700584 Mutex::Autolock _l(mLock);
585 mWidth = w;
586 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800587 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588}
589
Mathias Agopiana138f892010-05-21 17:24:35 -0700590bool Layer::isFixedSize() const {
591 Mutex::Autolock _l(mLock);
592 return mFixedSize;
593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595// ----------------------------------------------------------------------------
596// pageflip handling...
597// ----------------------------------------------------------------------------
598
599void Layer::lockPageFlip(bool& recomputeVisibleRegions)
600{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700601 ClientRef::Access sharedClient(mUserClientRef);
602 SharedBufferServer* lcblk(sharedClient.get());
603 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700604 // client died
605 recomputeVisibleRegions = true;
606 return;
607 }
608
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700609 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700610 if (buf == NOT_ENOUGH_DATA) {
611 // NOTE: This is not an error, it simply means there is nothing to
612 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700613 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 return;
615 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700616
Mathias Agopiand606de62010-05-10 20:06:11 -0700617 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700618 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700619 mPostedDirtyRegion.clear();
620 return;
621 }
622
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700623 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800624
625 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800626 const bool activeBlending =
627 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
628
Mathias Agopiand606de62010-05-10 20:06:11 -0700629 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700630 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700631 mPostedDirtyRegion.clear();
632 return;
633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634
Mathias Agopianda9584d2010-12-13 18:51:59 -0800635 if (noActiveBuffer) {
636 // we didn't have an active buffer, we need to recompute
637 // our visible region
638 recomputeVisibleRegions = true;
639 }
640
Mathias Agopian3330b202009-10-05 17:07:12 -0700641 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700642 if (newFrontBuffer != NULL) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800643 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
644 // new buffer has different opacity than previous active buffer, need
645 // to recompute visible regions accordingly
646 recomputeVisibleRegions = true;
647 }
648
Mathias Agopianb661d662010-08-19 17:01:19 -0700649 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700650 // compute the posted region
651 const Region dirty(lcblk->getDirtyRegion(buf));
652 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700654 // update the layer size and release freeze-lock
655 const Layer::State& front(drawingState());
656 if (newFrontBuffer->getWidth() == front.requested_w &&
657 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700658 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700659 if ((front.w != front.requested_w) ||
660 (front.h != front.requested_h))
661 {
662 // Here we pretend the transaction happened by updating the
663 // current and drawing states. Drawing state is only accessed
664 // in this thread, no need to have it locked
665 Layer::State& editDraw(mDrawingState);
666 editDraw.w = editDraw.requested_w;
667 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700668
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700669 // We also need to update the current state so that we don't
670 // end-up doing too much work during the next transaction.
671 // NOTE: We actually don't need hold the transaction lock here
672 // because State::w and State::h are only accessed from
673 // this thread
674 Layer::State& editTemp(currentState());
675 editTemp.w = editDraw.w;
676 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700677
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700678 // recompute visible region
679 recomputeVisibleRegions = true;
680 }
681
682 // we now have the correct size, unfreeze the screen
683 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700684 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700685
686 // get the crop region
687 setBufferCrop( lcblk->getCrop(buf) );
688
689 // get the transformation
690 setBufferTransform( lcblk->getTransform(buf) );
691
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700692 } else {
693 // this should not happen unless we ran out of memory while
694 // allocating the buffer. we're hoping that things will get back
695 // to normal the next time the app tries to draw into this buffer.
696 // meanwhile, pretend the screen didn't update.
697 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700698 }
699
Mathias Agopiane7005012009-10-07 16:44:10 -0700700 if (lcblk->getQueuedCount()) {
701 // signal an event if we have more buffers waiting
702 mFlinger->signalEvent();
703 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704
Mathias Agopian245e4d72010-04-21 15:24:11 -0700705 /* a buffer was posted, so we need to call reloadTexture(), which
706 * will update our internal data structures (eg: EGLImageKHR or
707 * texture names). we need to do this even if mPostedDirtyRegion is
708 * empty -- it's orthogonal to the fact that a new buffer was posted,
709 * for instance, a degenerate case could be that the user did an empty
710 * update but repainted the buffer with appropriate content (after a
711 * resize for instance).
712 */
713 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714}
715
716void Layer::unlockPageFlip(
717 const Transform& planeTransform, Region& outDirtyRegion)
718{
719 Region dirtyRegion(mPostedDirtyRegion);
720 if (!dirtyRegion.isEmpty()) {
721 mPostedDirtyRegion.clear();
722 // The dirty region is given in the layer's coordinate space
723 // transform the dirty region by the surface's transformation
724 // and the global transformation.
725 const Layer::State& s(drawingState());
726 const Transform tr(planeTransform * s.transform);
727 dirtyRegion = tr.transform(dirtyRegion);
728
729 // At this point, the dirty region is in screen space.
730 // Make sure it's constrained by the visible region (which
731 // is in screen space as well).
732 dirtyRegion.andSelf(visibleRegionScreen);
733 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800735 if (visibleRegionScreen.isEmpty()) {
736 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700737 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800738 mFreezeLock.clear();
739 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700742void Layer::dump(String8& result, char* buffer, size_t SIZE) const
743{
744 LayerBaseClient::dump(result, buffer, SIZE);
745
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700746 ClientRef::Access sharedClient(mUserClientRef);
747 SharedBufferServer* lcblk(sharedClient.get());
748 uint32_t totalTime = 0;
749 if (lcblk) {
750 SharedBufferStack::Statistics stats = lcblk->getStats();
751 totalTime= stats.totalTime;
752 result.append( lcblk->dump(" ") );
753 }
754
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700755 sp<const GraphicBuffer> buf0(getBuffer(0));
756 sp<const GraphicBuffer> buf1(getBuffer(1));
757 uint32_t w0=0, h0=0, s0=0;
758 uint32_t w1=0, h1=0, s1=0;
759 if (buf0 != 0) {
760 w0 = buf0->getWidth();
761 h0 = buf0->getHeight();
762 s0 = buf0->getStride();
763 }
764 if (buf1 != 0) {
765 w1 = buf1->getWidth();
766 h1 = buf1->getHeight();
767 s1 = buf1->getStride();
768 }
769 snprintf(buffer, SIZE,
770 " "
771 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
772 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700773 mFormat, w0, h0, s0, w1, h1, s1,
774 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700775
776 result.append(buffer);
777}
778
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700779// ---------------------------------------------------------------------------
780
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700781Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700782 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700783}
784
785Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700786}
787
788int32_t Layer::ClientRef::getToken() const {
789 Mutex::Autolock _l(mLock);
790 return mToken;
791}
792
Mathias Agopian579b3f82010-06-08 19:54:15 -0700793sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700794 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700795 return mUserClient.promote();
796}
797
798status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
799 const sp<SharedBufferServer>& sharedClient, int32_t token) {
800 Mutex::Autolock _l(mLock);
801
802 { // scope for strong mUserClient reference
803 sp<UserClient> userClient(mUserClient.promote());
Jamie Gennis5fd799d2011-03-18 16:35:13 -0700804 if (userClient != 0 && mControlBlock != 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700805 mControlBlock->setStatus(NO_INIT);
806 }
807 }
808
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700809 mUserClient = uc;
810 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700811 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700812 return NO_ERROR;
813}
814
815sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
816 return mUserClient.promote();
817}
818
819// this class gives us access to SharedBufferServer safely
820// it makes sure the UserClient (and its associated shared memory)
821// won't go away while we're accessing it.
822Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700823 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700824{
825 Mutex::Autolock _l(ref.mLock);
826 mUserClientStrongRef = ref.mUserClient.promote();
827 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700828 mControlBlock = ref.mControlBlock;
829}
830
831Layer::ClientRef::Access::~Access()
832{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700833}
834
835// ---------------------------------------------------------------------------
836
Mathias Agopiand606de62010-05-10 20:06:11 -0700837Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700838 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800839 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700840{
841}
842
Mathias Agopianbb641242010-05-18 17:06:55 -0700843Layer::BufferManager::~BufferManager()
844{
845}
846
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700847status_t Layer::BufferManager::resize(size_t size,
848 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700849{
850 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700851
852 if (size < mNumBuffers) {
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700853 // If there is an active texture, move it into slot 0 if needed
854 if (mActiveBufferIndex > 0) {
855 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
856 mBufferData[mActiveBufferIndex] = mBufferData[0];
857 mBufferData[0] = activeBufferData;
858 mActiveBufferIndex = 0;
859 }
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700860
861 // Free the buffers that are no longer needed.
862 for (size_t i = size; i < mNumBuffers; i++) {
863 mBufferData[i].buffer = 0;
864
865 // Create a message to destroy the textures on SurfaceFlinger's GL
866 // thread.
867 class MessageDestroyTexture : public MessageBase {
868 Image mTexture;
869 EGLDisplay mDpy;
870 public:
871 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
872 : mTexture(texture), mDpy(dpy) { }
873 virtual bool handler() {
874 status_t err = Layer::BufferManager::destroyTexture(
875 &mTexture, mDpy);
876 LOGE_IF(err<0, "error destroying texture: %d (%s)",
877 mTexture.name, strerror(-err));
878 return true; // XXX: err == 0; ????
879 }
880 };
881
882 MessageDestroyTexture *msg = new MessageDestroyTexture(
883 mBufferData[i].texture, dpy);
884
885 // Don't allow this texture to be cleaned up by
886 // BufferManager::destroy.
887 mBufferData[i].texture.name = -1U;
888 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
889
890 // Post the message to the SurfaceFlinger object.
891 flinger->postMessageAsync(msg);
892 }
893 }
894
Mathias Agopianbb641242010-05-18 17:06:55 -0700895 mNumBuffers = size;
896 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700897}
898
899// only for debugging
900sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
901 return mBufferData[index].buffer;
902}
903
904status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800905 BufferData const * const buffers = mBufferData;
906 Mutex::Autolock _l(mLock);
907 mActiveBuffer = buffers[index].buffer;
908 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700909 return NO_ERROR;
910}
911
912size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800913 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700914}
915
916Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700917 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800918 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700919 res = mFailoverTexture;
920 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800921 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700922 }
923 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700924}
925
926sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800927 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700928}
929
Mathias Agopianda9584d2010-12-13 18:51:59 -0800930bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800931 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800932}
933
Mathias Agopiand606de62010-05-10 20:06:11 -0700934sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
935{
Mathias Agopianbb641242010-05-18 17:06:55 -0700936 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700937 sp<GraphicBuffer> buffer;
938 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700939 buffer = buffers[index].buffer;
940 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700941 return buffer;
942}
943
944status_t Layer::BufferManager::attachBuffer(size_t index,
945 const sp<GraphicBuffer>& buffer)
946{
Mathias Agopianbb641242010-05-18 17:06:55 -0700947 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700948 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700949 buffers[index].buffer = buffer;
950 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700951 return NO_ERROR;
952}
953
954status_t Layer::BufferManager::destroy(EGLDisplay dpy)
955{
Mathias Agopianbb641242010-05-18 17:06:55 -0700956 BufferData* const buffers = mBufferData;
957 size_t num;
958 { // scope for the lock
959 Mutex::Autolock _l(mLock);
960 num = mNumBuffers;
961 for (size_t i=0 ; i<num ; i++) {
962 buffers[i].buffer = 0;
963 }
964 }
965 for (size_t i=0 ; i<num ; i++) {
966 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700967 }
968 destroyTexture(&mFailoverTexture, dpy);
969 return NO_ERROR;
970}
971
972status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
973 const sp<GraphicBuffer>& buffer)
974{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700975 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800976 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700977 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700978 if (!mFailover) {
Kobi Cohen Arazi0d11baf2011-04-15 10:38:33 -0700979 {
980 // Without that lock, there is a chance of race condition
981 // where while composing a specific index, requestBuf
982 // with the same index can be executed and touch the same data
983 // that is being used in initEglImage.
984 // (e.g. dirty flag in texture)
985 Mutex::Autolock _l(mLock);
986 Image& texture(mBufferData[index].texture);
987 err = mTextureManager.initEglImage(&texture, dpy, buffer);
988 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700989 // if EGLImage fails, we switch to regular texture mode, and we
990 // free all resources associated with using EGLImages.
991 if (err == NO_ERROR) {
992 mFailover = false;
993 destroyTexture(&mFailoverTexture, dpy);
994 } else {
995 mFailover = true;
996 const size_t num = mNumBuffers;
997 for (size_t i=0 ; i<num ; i++) {
998 destroyTexture(&mBufferData[i].texture, dpy);
999 }
Andreas Hubere049a952010-06-25 09:25:19 -07001000 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001001 } else {
1002 // we failed once, don't try again
1003 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -07001004 }
1005 }
1006 return err;
1007}
1008
1009status_t Layer::BufferManager::loadTexture(
1010 const Region& dirty, const GGLSurface& t)
1011{
1012 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1013}
1014
Mathias Agopianbb641242010-05-18 17:06:55 -07001015status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1016{
1017 if (tex->name != -1U) {
1018 glDeleteTextures(1, &tex->name);
1019 tex->name = -1U;
1020 }
1021 if (tex->image != EGL_NO_IMAGE_KHR) {
1022 eglDestroyImageKHR(dpy, tex->image);
1023 tex->image = EGL_NO_IMAGE_KHR;
1024 }
1025 return NO_ERROR;
1026}
1027
Mathias Agopiand606de62010-05-10 20:06:11 -07001028// ---------------------------------------------------------------------------
1029
Mathias Agopian9a112062009-04-17 19:36:26 -07001030Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -07001031 const sp<Layer>& owner)
1032 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -07001033{
1034}
1035
1036Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001037{
1038}
1039
Mathias Agopiana138f892010-05-21 17:24:35 -07001040sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1041 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001042{
Mathias Agopian3330b202009-10-05 17:07:12 -07001043 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001044 sp<Layer> owner(getOwner());
1045 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001046 /*
1047 * requestBuffer() cannot be called from the main thread
1048 * as it could cause a dead-lock, since it may have to wait
1049 * on conditions updated my the main thread.
1050 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001051 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001052 }
1053 return buffer;
1054}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001056status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1057{
1058 status_t err = DEAD_OBJECT;
1059 sp<Layer> owner(getOwner());
1060 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001061 /*
1062 * setBufferCount() cannot be called from the main thread
1063 * as it could cause a dead-lock, since it may have to wait
1064 * on conditions updated my the main thread.
1065 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001066 err = owner->setBufferCount(bufferCount);
1067 }
1068 return err;
1069}
1070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071// ---------------------------------------------------------------------------
1072
1073
1074}; // namespace android