blob: 57af001fabc8dd14d32be6b0ac4731b3f2e8edd2 [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),
60 mProtectedByDRM(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070062 mBufferManager(mTextureManager),
Mathias Agopian733189d2010-12-02 21:32:29 -080063 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065}
66
67Layer::~Layer()
68{
Mathias Agopianbb641242010-05-18 17:06:55 -070069 // FIXME: must be called from the main UI thread
70 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
71 mBufferManager.destroy(dpy);
72
Mathias Agopianb7e930d2010-06-01 15:12:58 -070073 // we can use getUserClientUnsafe here because we know we're
74 // single-threaded at that point.
75 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
76 if (ourClient != 0) {
77 ourClient->detachLayer(this);
78 }
Mathias Agopiand606de62010-05-10 20:06:11 -070079}
80
Mathias Agopianb7e930d2010-06-01 15:12:58 -070081status_t Layer::setToken(const sp<UserClient>& userClient,
82 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070083{
Mathias Agopian579b3f82010-06-08 19:54:15 -070084 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070085 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070086 getIdentity());
87
Mathias Agopian579b3f82010-06-08 19:54:15 -070088
Mathias Agopiandd17b3e2010-12-13 16:49:05 -080089 sp<UserClient> ourClient(mUserClientRef.getClient());
90
91 /*
92 * Here it is guaranteed that userClient != ourClient
93 * (see UserClient::getTokenForSurface()).
94 *
95 * We release the token used by this surface in ourClient below.
96 * This should be safe to do so now, since this layer won't be attached
97 * to this client, it should be okay to reuse that id.
98 *
99 * If this causes problems, an other solution would be to keep a list
100 * of all the {UserClient, token} ever used and release them when the
101 * Layer is destroyed.
102 *
103 */
104
105 if (ourClient != 0) {
106 ourClient->detachLayer(this);
107 }
108
109 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700110 LOGE_IF(err != NO_ERROR,
111 "ClientRef::setToken(%p, %p, %u) failed",
112 userClient.get(), lcblk.get(), token);
113
114 if (err == NO_ERROR) {
115 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700116 }
117
118 return err;
119}
120
121int32_t Layer::getToken() const
122{
123 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700124}
125
Mathias Agopian579b3f82010-06-08 19:54:15 -0700126sp<UserClient> Layer::getClient() const
127{
128 return mUserClientRef.getClient();
129}
130
Mathias Agopiand606de62010-05-10 20:06:11 -0700131// called with SurfaceFlinger::mStateLock as soon as the layer is entered
132// in the purgatory list
133void Layer::onRemoved()
134{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700135 ClientRef::Access sharedClient(mUserClientRef);
136 SharedBufferServer* lcblk(sharedClient.get());
137 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700138 // wake up the condition
139 lcblk->setStatus(NO_INIT);
140 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700141}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700142
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144{
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800145 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
146 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}
148
Mathias Agopian9a112062009-04-17 19:36:26 -0700149status_t Layer::ditch()
150{
Mathias Agopianbb641242010-05-18 17:06:55 -0700151 // NOTE: Called from the main UI thread
152
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700153 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800154 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700155
Mathias Agopianbb641242010-05-18 17:06:55 -0700156 Mutex::Autolock _l(mLock);
157 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700158 return NO_ERROR;
159}
160
Mathias Agopianf9d93272009-06-19 17:00:27 -0700161status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 PixelFormat format, uint32_t flags)
163{
Mathias Agopian401c2572009-09-23 19:16:27 -0700164 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 PixelFormatInfo info;
166 status_t err = getPixelFormatInfo(format, &info);
167 if (err) return err;
168
Mathias Agopian401c2572009-09-23 19:16:27 -0700169 // the display's pixel format
170 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700171 uint32_t const maxSurfaceDims = min(
172 hw.getMaxTextureSize(), hw.getMaxViewportDims());
173
174 // never allow a surface larger than what our underlying GL implementation
175 // can handle.
176 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
177 return BAD_VALUE;
178 }
179
Mathias Agopian401c2572009-09-23 19:16:27 -0700180 PixelFormatInfo displayInfo;
181 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700182 const uint32_t hwFlags = hw.getFlags();
183
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700184 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700185 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700186 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700187
188 mReqFormat = format;
189 mReqWidth = w;
190 mReqHeight = h;
191
Mathias Agopian3330b202009-10-05 17:07:12 -0700192 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800193 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
194 mProtectedByDRM = (flags & ISurfaceComposer::eProtectedByDRM) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700195 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
196 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700197
Mathias Agopian401c2572009-09-23 19:16:27 -0700198 // we use the red index
199 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
200 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
201 mNeedsDithering = layerRedsize > displayRedSize;
202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 return NO_ERROR;
204}
205
Mathias Agopiana350ff92010-08-10 17:14:02 -0700206void Layer::setGeometry(hwc_layer_t* hwcl)
207{
208 hwcl->compositionType = HWC_FRAMEBUFFER;
209 hwcl->hints = 0;
210 hwcl->flags = 0;
211 hwcl->transform = 0;
212 hwcl->blending = HWC_BLENDING_NONE;
213
214 // we can't do alpha-fade with the hwc HAL
215 const State& s(drawingState());
216 if (s.alpha < 0xFF) {
217 hwcl->flags = HWC_SKIP_LAYER;
218 return;
219 }
220
221 // we can only handle simple transformation
222 if (mOrientation & Transform::ROT_INVALID) {
223 hwcl->flags = HWC_SKIP_LAYER;
224 return;
225 }
226
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800227 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
228 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229
230 if (needsBlending()) {
231 hwcl->blending = mPremultipliedAlpha ?
232 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
233 }
234
235 hwcl->displayFrame.left = mTransformedBounds.left;
236 hwcl->displayFrame.top = mTransformedBounds.top;
237 hwcl->displayFrame.right = mTransformedBounds.right;
238 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
239
240 hwcl->visibleRegionScreen.rects =
241 reinterpret_cast<hwc_rect_t const *>(
242 visibleRegionScreen.getArray(
243 &hwcl->visibleRegionScreen.numRects));
244}
245
246void Layer::setPerFrameData(hwc_layer_t* hwcl) {
247 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
248 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800249 // this can happen if the client never drew into this layer yet,
250 // or if we ran out of memory. In that case, don't let
251 // HWC handle it.
252 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253 hwcl->handle = NULL;
254 return;
255 }
Louis Huemiller04048142010-12-01 12:29:36 -0800256 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800257
258 if (!mBufferCrop.isEmpty()) {
259 hwcl->sourceCrop.left = mBufferCrop.left;
260 hwcl->sourceCrop.top = mBufferCrop.top;
261 hwcl->sourceCrop.right = mBufferCrop.right;
262 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
263 } else {
264 hwcl->sourceCrop.left = 0;
265 hwcl->sourceCrop.top = 0;
266 hwcl->sourceCrop.right = buffer->width;
267 hwcl->sourceCrop.bottom = buffer->height;
268 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700269}
270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271void Layer::reloadTexture(const Region& dirty)
272{
Mathias Agopiand606de62010-05-10 20:06:11 -0700273 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800274 if (buffer == NULL) {
275 // this situation can happen if we ran out of memory for instance.
276 // not much we can do. continue to use whatever texture was bound
277 // to this context.
278 return;
279 }
280
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700281 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700282 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
283 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
284 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700285 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700287 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800288slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700290 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
291 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
292 LOGE_IF(res, "error %d (%s) locking buffer %p",
293 res, strerror(res), buffer.get());
294 if (res == NO_ERROR) {
295 mBufferManager.loadTexture(dirty, t);
296 buffer->unlock();
297 }
298 } else {
299 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700300 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302}
303
Mathias Agopian74c40c02010-09-29 13:02:36 -0700304void Layer::drawForSreenShot() const
305{
Mathias Agopian733189d2010-12-02 21:32:29 -0800306 const bool currentFiltering = mNeedsFiltering;
307 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700308 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800309 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700310}
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312void Layer::onDraw(const Region& clip) const
313{
Mathias Agopiand606de62010-05-10 20:06:11 -0700314 Texture tex(mBufferManager.getActiveTexture());
315 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700317 // in fact never been drawn into. This happens frequently with
318 // SurfaceView because the WindowManager can't know when the client
319 // has drawn the first time.
320
321 // If there is nothing under us, we paint the screen in black, otherwise
322 // we just skip this update.
323
324 // figure out if there is something below us
325 Region under;
326 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
327 const size_t count = drawingLayers.size();
328 for (size_t i=0 ; i<count ; ++i) {
329 const sp<LayerBase>& layer(drawingLayers[i]);
330 if (layer.get() == static_cast<LayerBase const*>(this))
331 break;
332 under.orSelf(layer->visibleRegionScreen);
333 }
334 // if not everything below us is covered, we plug the holes!
335 Region holes(clip.subtract(under));
336 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700337 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700338 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 return;
340 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700341 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342}
343
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800344// As documented in libhardware header, formats in the range
345// 0x100 - 0x1FF are specific to the HAL implementation, and
346// are known to have no alpha channel
347// TODO: move definition for device-specific range into
348// hardware.h, instead of using hard-coded values here.
349#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
350
351bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
352{
353 // If buffers where set with eOpaque flag, all buffers are known to
354 // be opaque without having to check their actual format
355 if (mNeedsBlending && buffer != NULL) {
356 PixelFormat format = buffer->getPixelFormat();
357
358 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
359 return false;
360 }
361
362 PixelFormatInfo info;
363 status_t err = getPixelFormatInfo(format, &info);
364 if (!err && info.h_alpha <= info.l_alpha) {
365 return false;
366 }
367 }
368
369 // Return opacity as determined from flags and format options
370 // passed to setBuffers()
371 return mNeedsBlending;
372}
373
374bool Layer::needsBlending() const
375{
376 if (mBufferManager.hasActiveBuffer()) {
377 return needsBlending(mBufferManager.getActiveBuffer());
378 }
379
380 return mNeedsBlending;
381}
382
Mathias Agopiana7f66922010-05-26 22:08:52 -0700383bool Layer::needsFiltering() const
384{
385 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800386 // if our buffer is not the same size than ourselves,
387 // we need filtering.
388 Mutex::Autolock _l(mLock);
389 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700390 return true;
391 }
392 return LayerBase::needsFiltering();
393}
394
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700395
396status_t Layer::setBufferCount(int bufferCount)
397{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700398 ClientRef::Access sharedClient(mUserClientRef);
399 SharedBufferServer* lcblk(sharedClient.get());
400 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700401 // oops, the client is already gone
402 return DEAD_OBJECT;
403 }
404
Mathias Agopianbb641242010-05-18 17:06:55 -0700405 // NOTE: lcblk->resize() is protected by an internal lock
406 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700407 if (err == NO_ERROR) {
408 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
409 mBufferManager.resize(bufferCount, mFlinger, dpy);
410 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700411
412 return err;
413}
414
Mathias Agopiana138f892010-05-21 17:24:35 -0700415sp<GraphicBuffer> Layer::requestBuffer(int index,
416 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
417 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418{
Mathias Agopian3330b202009-10-05 17:07:12 -0700419 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700420
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700421 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700422 return buffer;
423
424 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
425 return buffer;
426
Mathias Agopian48d819a2009-09-10 19:41:18 -0700427 // this ensures our client doesn't go away while we're accessing
428 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700429 ClientRef::Access sharedClient(mUserClientRef);
430 SharedBufferServer* lcblk(sharedClient.get());
431 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700432 // oops, the client is already gone
433 return buffer;
434 }
435
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700436 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700437 * This is called from the client's Surface::dequeue(). This can happen
438 * at any time, especially while we're in the middle of using the
439 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700440 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441
Mathias Agopian208cb072010-07-27 20:11:35 -0700442 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700443 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700444 { // scope for the lock
445 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700446
447 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700448 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700449 if (!reqFormat) reqFormat = mFormat;
450 if (!reqWidth) reqWidth = mWidth;
451 if (!reqHeight) reqHeight = mHeight;
452
453 w = reqWidth;
454 h = reqHeight;
455 f = reqFormat;
456
457 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
458 (reqFormat != mReqFormat)) {
459 mReqWidth = reqWidth;
460 mReqHeight = reqHeight;
461 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700462 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800463 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700464
Mathias Agopiana138f892010-05-21 17:24:35 -0700465 lcblk->reallocateAllExcept(index);
466 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700467 }
468
Mathias Agopian208cb072010-07-27 20:11:35 -0700469 // here we have to reallocate a new buffer because the buffer could be
470 // used as the front buffer, or by a client in our process
471 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700472 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700473 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
474 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700475
476 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800477 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700478 LOGE_IF(err || buffer->handle == 0,
479 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
480 this, index, w, h, strerror(-err));
481 } else {
482 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700483 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
484 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700485 }
486
487 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700488 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700489 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700490 }
491 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492}
493
Mathias Agopian3330b202009-10-05 17:07:12 -0700494uint32_t Layer::getEffectiveUsage(uint32_t usage) const
495{
496 /*
497 * buffers used for software rendering, but h/w composition
498 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
499 *
500 * buffers used for h/w rendering and h/w composition
501 * are allocated with HW_RENDER | HW_TEXTURE
502 *
503 * buffers used with h/w rendering and either NPOT or no egl_image_ext
504 * are allocated with SW_READ_RARELY | HW_RENDER
505 *
506 */
507
508 if (mSecure) {
509 // secure buffer, don't store it into the GPU
510 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
511 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
512 } else {
513 // it's allowed to modify the usage flags here, but generally
514 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700515 // request EGLImage for all buffers
516 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700517 }
Glenn Kasten16f04532011-01-19 15:27:27 -0800518 if (mProtectedByApp || mProtectedByDRM) {
519 // need a hardware-protected path to external video sink
520 usage |= GraphicBuffer::USAGE_PROTECTED;
521 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700522 return usage;
523}
524
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525uint32_t Layer::doTransaction(uint32_t flags)
526{
527 const Layer::State& front(drawingState());
528 const Layer::State& temp(currentState());
529
Mathias Agopiana138f892010-05-21 17:24:35 -0700530 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
531 (front.requested_h != temp.requested_h);
532
533 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700534 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700536 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
537 this,
538 int(temp.requested_w), int(temp.requested_h),
539 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540
Mathias Agopiana138f892010-05-21 17:24:35 -0700541 if (!isFixedSize()) {
542 // we're being resized and there is a freeze display request,
543 // acquire a freeze lock, so that the screen stays put
544 // until we've redrawn at the new size; this is to avoid
545 // glitches upon orientation changes.
546 if (mFlinger->hasFreezeRequest()) {
547 // if the surface is hidden, don't try to acquire the
548 // freeze lock, since hidden surfaces may never redraw
549 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
550 mFreezeLock = mFlinger->getFreezeLock();
551 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700553
554 // this will make sure LayerBase::doTransaction doesn't update
555 // the drawing state's size
556 Layer::State& editDraw(mDrawingState);
557 editDraw.requested_w = temp.requested_w;
558 editDraw.requested_h = temp.requested_h;
559
560 // record the new size, form this point on, when the client request
561 // a buffer, it'll get the new size.
562 setBufferSize(temp.requested_w, temp.requested_h);
563
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700564 ClientRef::Access sharedClient(mUserClientRef);
565 SharedBufferServer* lcblk(sharedClient.get());
566 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700567 // all buffers need reallocation
568 lcblk->reallocateAll();
569 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700570 } else {
571 // record the new size
572 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 }
574 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 if (temp.sequence != front.sequence) {
577 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
578 // this surface is now hidden, so it shouldn't hold a freeze lock
579 // (it may never redraw, which is fine if it is hidden)
580 mFreezeLock.clear();
581 }
582 }
583
584 return LayerBase::doTransaction(flags);
585}
586
Mathias Agopiana138f892010-05-21 17:24:35 -0700587void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700588 Mutex::Autolock _l(mLock);
589 mWidth = w;
590 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800591 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592}
593
Mathias Agopiana138f892010-05-21 17:24:35 -0700594bool Layer::isFixedSize() const {
595 Mutex::Autolock _l(mLock);
596 return mFixedSize;
597}
598
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599// ----------------------------------------------------------------------------
600// pageflip handling...
601// ----------------------------------------------------------------------------
602
603void Layer::lockPageFlip(bool& recomputeVisibleRegions)
604{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700605 ClientRef::Access sharedClient(mUserClientRef);
606 SharedBufferServer* lcblk(sharedClient.get());
607 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700608 // client died
609 recomputeVisibleRegions = true;
610 return;
611 }
612
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700613 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700614 if (buf == NOT_ENOUGH_DATA) {
615 // NOTE: This is not an error, it simply means there is nothing to
616 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700617 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 return;
619 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700620
Mathias Agopiand606de62010-05-10 20:06:11 -0700621 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700622 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700623 mPostedDirtyRegion.clear();
624 return;
625 }
626
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700627 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800628
629 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800630 const bool activeBlending =
631 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
632
Mathias Agopiand606de62010-05-10 20:06:11 -0700633 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700634 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700635 mPostedDirtyRegion.clear();
636 return;
637 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638
Mathias Agopianda9584d2010-12-13 18:51:59 -0800639 if (noActiveBuffer) {
640 // we didn't have an active buffer, we need to recompute
641 // our visible region
642 recomputeVisibleRegions = true;
643 }
644
Mathias Agopian3330b202009-10-05 17:07:12 -0700645 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700646 if (newFrontBuffer != NULL) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800647 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
648 // new buffer has different opacity than previous active buffer, need
649 // to recompute visible regions accordingly
650 recomputeVisibleRegions = true;
651 }
652
Mathias Agopianb661d662010-08-19 17:01:19 -0700653 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700654 // compute the posted region
655 const Region dirty(lcblk->getDirtyRegion(buf));
656 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700657
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700658 // update the layer size and release freeze-lock
659 const Layer::State& front(drawingState());
660 if (newFrontBuffer->getWidth() == front.requested_w &&
661 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700662 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700663 if ((front.w != front.requested_w) ||
664 (front.h != front.requested_h))
665 {
666 // Here we pretend the transaction happened by updating the
667 // current and drawing states. Drawing state is only accessed
668 // in this thread, no need to have it locked
669 Layer::State& editDraw(mDrawingState);
670 editDraw.w = editDraw.requested_w;
671 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700672
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700673 // We also need to update the current state so that we don't
674 // end-up doing too much work during the next transaction.
675 // NOTE: We actually don't need hold the transaction lock here
676 // because State::w and State::h are only accessed from
677 // this thread
678 Layer::State& editTemp(currentState());
679 editTemp.w = editDraw.w;
680 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700681
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700682 // recompute visible region
683 recomputeVisibleRegions = true;
684 }
685
686 // we now have the correct size, unfreeze the screen
687 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700688 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700689
690 // get the crop region
691 setBufferCrop( lcblk->getCrop(buf) );
692
693 // get the transformation
694 setBufferTransform( lcblk->getTransform(buf) );
695
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700696 } else {
697 // this should not happen unless we ran out of memory while
698 // allocating the buffer. we're hoping that things will get back
699 // to normal the next time the app tries to draw into this buffer.
700 // meanwhile, pretend the screen didn't update.
701 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700702 }
703
Mathias Agopiane7005012009-10-07 16:44:10 -0700704 if (lcblk->getQueuedCount()) {
705 // signal an event if we have more buffers waiting
706 mFlinger->signalEvent();
707 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708
Mathias Agopian245e4d72010-04-21 15:24:11 -0700709 /* a buffer was posted, so we need to call reloadTexture(), which
710 * will update our internal data structures (eg: EGLImageKHR or
711 * texture names). we need to do this even if mPostedDirtyRegion is
712 * empty -- it's orthogonal to the fact that a new buffer was posted,
713 * for instance, a degenerate case could be that the user did an empty
714 * update but repainted the buffer with appropriate content (after a
715 * resize for instance).
716 */
717 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718}
719
720void Layer::unlockPageFlip(
721 const Transform& planeTransform, Region& outDirtyRegion)
722{
723 Region dirtyRegion(mPostedDirtyRegion);
724 if (!dirtyRegion.isEmpty()) {
725 mPostedDirtyRegion.clear();
726 // The dirty region is given in the layer's coordinate space
727 // transform the dirty region by the surface's transformation
728 // and the global transformation.
729 const Layer::State& s(drawingState());
730 const Transform tr(planeTransform * s.transform);
731 dirtyRegion = tr.transform(dirtyRegion);
732
733 // At this point, the dirty region is in screen space.
734 // Make sure it's constrained by the visible region (which
735 // is in screen space as well).
736 dirtyRegion.andSelf(visibleRegionScreen);
737 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800739 if (visibleRegionScreen.isEmpty()) {
740 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700741 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800742 mFreezeLock.clear();
743 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744}
745
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700746void Layer::dump(String8& result, char* buffer, size_t SIZE) const
747{
748 LayerBaseClient::dump(result, buffer, SIZE);
749
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700750 ClientRef::Access sharedClient(mUserClientRef);
751 SharedBufferServer* lcblk(sharedClient.get());
752 uint32_t totalTime = 0;
753 if (lcblk) {
754 SharedBufferStack::Statistics stats = lcblk->getStats();
755 totalTime= stats.totalTime;
756 result.append( lcblk->dump(" ") );
757 }
758
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700759 sp<const GraphicBuffer> buf0(getBuffer(0));
760 sp<const GraphicBuffer> buf1(getBuffer(1));
761 uint32_t w0=0, h0=0, s0=0;
762 uint32_t w1=0, h1=0, s1=0;
763 if (buf0 != 0) {
764 w0 = buf0->getWidth();
765 h0 = buf0->getHeight();
766 s0 = buf0->getStride();
767 }
768 if (buf1 != 0) {
769 w1 = buf1->getWidth();
770 h1 = buf1->getHeight();
771 s1 = buf1->getStride();
772 }
773 snprintf(buffer, SIZE,
774 " "
775 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
776 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700777 mFormat, w0, h0, s0, w1, h1, s1,
778 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700779
780 result.append(buffer);
781}
782
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700783// ---------------------------------------------------------------------------
784
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700785Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700786 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700787}
788
789Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700790}
791
792int32_t Layer::ClientRef::getToken() const {
793 Mutex::Autolock _l(mLock);
794 return mToken;
795}
796
Mathias Agopian579b3f82010-06-08 19:54:15 -0700797sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700798 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700799 return mUserClient.promote();
800}
801
802status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
803 const sp<SharedBufferServer>& sharedClient, int32_t token) {
804 Mutex::Autolock _l(mLock);
805
806 { // scope for strong mUserClient reference
807 sp<UserClient> userClient(mUserClient.promote());
808 if (mUserClient != 0 && mControlBlock != 0) {
809 mControlBlock->setStatus(NO_INIT);
810 }
811 }
812
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700813 mUserClient = uc;
814 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700815 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700816 return NO_ERROR;
817}
818
819sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
820 return mUserClient.promote();
821}
822
823// this class gives us access to SharedBufferServer safely
824// it makes sure the UserClient (and its associated shared memory)
825// won't go away while we're accessing it.
826Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700827 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700828{
829 Mutex::Autolock _l(ref.mLock);
830 mUserClientStrongRef = ref.mUserClient.promote();
831 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700832 mControlBlock = ref.mControlBlock;
833}
834
835Layer::ClientRef::Access::~Access()
836{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700837}
838
839// ---------------------------------------------------------------------------
840
Mathias Agopiand606de62010-05-10 20:06:11 -0700841Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700842 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800843 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700844{
845}
846
Mathias Agopianbb641242010-05-18 17:06:55 -0700847Layer::BufferManager::~BufferManager()
848{
849}
850
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700851status_t Layer::BufferManager::resize(size_t size,
852 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700853{
854 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700855
856 if (size < mNumBuffers) {
857 // Move the active texture into slot 0
Mathias Agopian420a2832010-12-14 20:30:37 -0800858 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
859 mBufferData[mActiveBufferIndex] = mBufferData[0];
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700860 mBufferData[0] = activeBufferData;
Mathias Agopian420a2832010-12-14 20:30:37 -0800861 mActiveBufferIndex = 0;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700862
863 // Free the buffers that are no longer needed.
864 for (size_t i = size; i < mNumBuffers; i++) {
865 mBufferData[i].buffer = 0;
866
867 // Create a message to destroy the textures on SurfaceFlinger's GL
868 // thread.
869 class MessageDestroyTexture : public MessageBase {
870 Image mTexture;
871 EGLDisplay mDpy;
872 public:
873 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
874 : mTexture(texture), mDpy(dpy) { }
875 virtual bool handler() {
876 status_t err = Layer::BufferManager::destroyTexture(
877 &mTexture, mDpy);
878 LOGE_IF(err<0, "error destroying texture: %d (%s)",
879 mTexture.name, strerror(-err));
880 return true; // XXX: err == 0; ????
881 }
882 };
883
884 MessageDestroyTexture *msg = new MessageDestroyTexture(
885 mBufferData[i].texture, dpy);
886
887 // Don't allow this texture to be cleaned up by
888 // BufferManager::destroy.
889 mBufferData[i].texture.name = -1U;
890 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
891
892 // Post the message to the SurfaceFlinger object.
893 flinger->postMessageAsync(msg);
894 }
895 }
896
Mathias Agopianbb641242010-05-18 17:06:55 -0700897 mNumBuffers = size;
898 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700899}
900
901// only for debugging
902sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
903 return mBufferData[index].buffer;
904}
905
906status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800907 BufferData const * const buffers = mBufferData;
908 Mutex::Autolock _l(mLock);
909 mActiveBuffer = buffers[index].buffer;
910 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700911 return NO_ERROR;
912}
913
914size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800915 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700916}
917
918Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700919 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800920 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700921 res = mFailoverTexture;
922 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800923 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700924 }
925 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700926}
927
928sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800929 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700930}
931
Mathias Agopianda9584d2010-12-13 18:51:59 -0800932bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800933 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800934}
935
Mathias Agopiand606de62010-05-10 20:06:11 -0700936sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
937{
Mathias Agopianbb641242010-05-18 17:06:55 -0700938 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700939 sp<GraphicBuffer> buffer;
940 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700941 buffer = buffers[index].buffer;
942 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700943 return buffer;
944}
945
946status_t Layer::BufferManager::attachBuffer(size_t index,
947 const sp<GraphicBuffer>& buffer)
948{
Mathias Agopianbb641242010-05-18 17:06:55 -0700949 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700950 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700951 buffers[index].buffer = buffer;
952 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700953 return NO_ERROR;
954}
955
956status_t Layer::BufferManager::destroy(EGLDisplay dpy)
957{
Mathias Agopianbb641242010-05-18 17:06:55 -0700958 BufferData* const buffers = mBufferData;
959 size_t num;
960 { // scope for the lock
961 Mutex::Autolock _l(mLock);
962 num = mNumBuffers;
963 for (size_t i=0 ; i<num ; i++) {
964 buffers[i].buffer = 0;
965 }
966 }
967 for (size_t i=0 ; i<num ; i++) {
968 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700969 }
970 destroyTexture(&mFailoverTexture, dpy);
971 return NO_ERROR;
972}
973
974status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
975 const sp<GraphicBuffer>& buffer)
976{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700977 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800978 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700979 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700980 if (!mFailover) {
981 Image& texture(mBufferData[index].texture);
982 err = mTextureManager.initEglImage(&texture, dpy, buffer);
983 // if EGLImage fails, we switch to regular texture mode, and we
984 // free all resources associated with using EGLImages.
985 if (err == NO_ERROR) {
986 mFailover = false;
987 destroyTexture(&mFailoverTexture, dpy);
988 } else {
989 mFailover = true;
990 const size_t num = mNumBuffers;
991 for (size_t i=0 ; i<num ; i++) {
992 destroyTexture(&mBufferData[i].texture, dpy);
993 }
Andreas Hubere049a952010-06-25 09:25:19 -0700994 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700995 } else {
996 // we failed once, don't try again
997 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700998 }
999 }
1000 return err;
1001}
1002
1003status_t Layer::BufferManager::loadTexture(
1004 const Region& dirty, const GGLSurface& t)
1005{
1006 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1007}
1008
Mathias Agopianbb641242010-05-18 17:06:55 -07001009status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1010{
1011 if (tex->name != -1U) {
1012 glDeleteTextures(1, &tex->name);
1013 tex->name = -1U;
1014 }
1015 if (tex->image != EGL_NO_IMAGE_KHR) {
1016 eglDestroyImageKHR(dpy, tex->image);
1017 tex->image = EGL_NO_IMAGE_KHR;
1018 }
1019 return NO_ERROR;
1020}
1021
Mathias Agopiand606de62010-05-10 20:06:11 -07001022// ---------------------------------------------------------------------------
1023
Mathias Agopian9a112062009-04-17 19:36:26 -07001024Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -07001025 const sp<Layer>& owner)
1026 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -07001027{
1028}
1029
1030Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001031{
1032}
1033
Mathias Agopiana138f892010-05-21 17:24:35 -07001034sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1035 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001036{
Mathias Agopian3330b202009-10-05 17:07:12 -07001037 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001038 sp<Layer> owner(getOwner());
1039 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001040 /*
1041 * requestBuffer() cannot be called from the main thread
1042 * as it could cause a dead-lock, since it may have to wait
1043 * on conditions updated my the main thread.
1044 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001045 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001046 }
1047 return buffer;
1048}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001050status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1051{
1052 status_t err = DEAD_OBJECT;
1053 sp<Layer> owner(getOwner());
1054 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001055 /*
1056 * setBufferCount() cannot be called from the main thread
1057 * as it could cause a dead-lock, since it may have to wait
1058 * on conditions updated my the main thread.
1059 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001060 err = owner->setBufferCount(bufferCount);
1061 }
1062 return err;
1063}
1064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065// ---------------------------------------------------------------------------
1066
1067
1068}; // namespace android