blob: a35811def507a821497ec4583cd45c3bb457ac3e [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{
Mathias Agopian867740d2011-06-12 18:05:53 -070064 setDestroyer(this);
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 Agopian867740d2011-06-12 18:05:53 -070081void Layer::destroy(RefBase const* base) {
82 mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
Mathias Agopianf3d939c2011-06-06 09:55:15 -070083}
84
Mathias Agopianb7e930d2010-06-01 15:12:58 -070085status_t Layer::setToken(const sp<UserClient>& userClient,
86 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070087{
Mathias Agopian579b3f82010-06-08 19:54:15 -070088 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070089 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070090 getIdentity());
91
Mathias Agopian579b3f82010-06-08 19:54:15 -070092
Mathias Agopiandd17b3e2010-12-13 16:49:05 -080093 sp<UserClient> ourClient(mUserClientRef.getClient());
94
95 /*
96 * Here it is guaranteed that userClient != ourClient
97 * (see UserClient::getTokenForSurface()).
98 *
99 * We release the token used by this surface in ourClient below.
100 * This should be safe to do so now, since this layer won't be attached
101 * to this client, it should be okay to reuse that id.
102 *
103 * If this causes problems, an other solution would be to keep a list
104 * of all the {UserClient, token} ever used and release them when the
105 * Layer is destroyed.
106 *
107 */
108
109 if (ourClient != 0) {
110 ourClient->detachLayer(this);
111 }
112
113 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700114 LOGE_IF(err != NO_ERROR,
115 "ClientRef::setToken(%p, %p, %u) failed",
116 userClient.get(), lcblk.get(), token);
117
118 if (err == NO_ERROR) {
119 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120 }
121
122 return err;
123}
124
125int32_t Layer::getToken() const
126{
127 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700128}
129
Mathias Agopian579b3f82010-06-08 19:54:15 -0700130sp<UserClient> Layer::getClient() const
131{
132 return mUserClientRef.getClient();
133}
134
Mathias Agopiand606de62010-05-10 20:06:11 -0700135// called with SurfaceFlinger::mStateLock as soon as the layer is entered
136// in the purgatory list
137void Layer::onRemoved()
138{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700139 ClientRef::Access sharedClient(mUserClientRef);
140 SharedBufferServer* lcblk(sharedClient.get());
141 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700142 // wake up the condition
143 lcblk->setStatus(NO_INIT);
144 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700145}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700146
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700147sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148{
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800149 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
150 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151}
152
Mathias Agopianf9d93272009-06-19 17:00:27 -0700153status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 PixelFormat format, uint32_t flags)
155{
Mathias Agopian401c2572009-09-23 19:16:27 -0700156 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 PixelFormatInfo info;
158 status_t err = getPixelFormatInfo(format, &info);
159 if (err) return err;
160
Mathias Agopian401c2572009-09-23 19:16:27 -0700161 // the display's pixel format
162 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700163 uint32_t const maxSurfaceDims = min(
164 hw.getMaxTextureSize(), hw.getMaxViewportDims());
165
166 // never allow a surface larger than what our underlying GL implementation
167 // can handle.
168 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
169 return BAD_VALUE;
170 }
171
Mathias Agopian401c2572009-09-23 19:16:27 -0700172 PixelFormatInfo displayInfo;
173 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700174 const uint32_t hwFlags = hw.getFlags();
175
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700176 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700177 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700178 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700179
180 mReqFormat = format;
181 mReqWidth = w;
182 mReqHeight = h;
183
Mathias Agopian3330b202009-10-05 17:07:12 -0700184 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800185 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700186 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
187 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700188
Mathias Agopian401c2572009-09-23 19:16:27 -0700189 // we use the red index
190 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
191 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
192 mNeedsDithering = layerRedsize > displayRedSize;
193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 return NO_ERROR;
195}
196
Mathias Agopiana350ff92010-08-10 17:14:02 -0700197void Layer::setGeometry(hwc_layer_t* hwcl)
198{
199 hwcl->compositionType = HWC_FRAMEBUFFER;
200 hwcl->hints = 0;
201 hwcl->flags = 0;
202 hwcl->transform = 0;
203 hwcl->blending = HWC_BLENDING_NONE;
204
205 // we can't do alpha-fade with the hwc HAL
206 const State& s(drawingState());
207 if (s.alpha < 0xFF) {
208 hwcl->flags = HWC_SKIP_LAYER;
209 return;
210 }
211
212 // we can only handle simple transformation
213 if (mOrientation & Transform::ROT_INVALID) {
214 hwcl->flags = HWC_SKIP_LAYER;
215 return;
216 }
217
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800218 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
219 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700220
221 if (needsBlending()) {
222 hwcl->blending = mPremultipliedAlpha ?
223 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
224 }
225
226 hwcl->displayFrame.left = mTransformedBounds.left;
227 hwcl->displayFrame.top = mTransformedBounds.top;
228 hwcl->displayFrame.right = mTransformedBounds.right;
229 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
230
231 hwcl->visibleRegionScreen.rects =
232 reinterpret_cast<hwc_rect_t const *>(
233 visibleRegionScreen.getArray(
234 &hwcl->visibleRegionScreen.numRects));
235}
236
237void Layer::setPerFrameData(hwc_layer_t* hwcl) {
238 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
239 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800240 // this can happen if the client never drew into this layer yet,
241 // or if we ran out of memory. In that case, don't let
242 // HWC handle it.
243 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700244 hwcl->handle = NULL;
245 return;
246 }
Louis Huemiller04048142010-12-01 12:29:36 -0800247 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800248
249 if (!mBufferCrop.isEmpty()) {
250 hwcl->sourceCrop.left = mBufferCrop.left;
251 hwcl->sourceCrop.top = mBufferCrop.top;
252 hwcl->sourceCrop.right = mBufferCrop.right;
253 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
254 } else {
255 hwcl->sourceCrop.left = 0;
256 hwcl->sourceCrop.top = 0;
257 hwcl->sourceCrop.right = buffer->width;
258 hwcl->sourceCrop.bottom = buffer->height;
259 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700260}
261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262void Layer::reloadTexture(const Region& dirty)
263{
Mathias Agopiand606de62010-05-10 20:06:11 -0700264 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800265 if (buffer == NULL) {
266 // this situation can happen if we ran out of memory for instance.
267 // not much we can do. continue to use whatever texture was bound
268 // to this context.
269 return;
270 }
271
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700272 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700273 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
274 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
275 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700276 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700278 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800279slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700281 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
282 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
283 LOGE_IF(res, "error %d (%s) locking buffer %p",
284 res, strerror(res), buffer.get());
285 if (res == NO_ERROR) {
286 mBufferManager.loadTexture(dirty, t);
287 buffer->unlock();
288 }
289 } else {
290 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700291 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293}
294
Mathias Agopian74c40c02010-09-29 13:02:36 -0700295void Layer::drawForSreenShot() const
296{
Mathias Agopian733189d2010-12-02 21:32:29 -0800297 const bool currentFiltering = mNeedsFiltering;
298 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700299 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800300 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700301}
302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303void Layer::onDraw(const Region& clip) const
304{
Mathias Agopiand606de62010-05-10 20:06:11 -0700305 Texture tex(mBufferManager.getActiveTexture());
306 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700308 // in fact never been drawn into. This happens frequently with
309 // SurfaceView because the WindowManager can't know when the client
310 // has drawn the first time.
311
312 // If there is nothing under us, we paint the screen in black, otherwise
313 // we just skip this update.
314
315 // figure out if there is something below us
316 Region under;
317 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
318 const size_t count = drawingLayers.size();
319 for (size_t i=0 ; i<count ; ++i) {
320 const sp<LayerBase>& layer(drawingLayers[i]);
321 if (layer.get() == static_cast<LayerBase const*>(this))
322 break;
323 under.orSelf(layer->visibleRegionScreen);
324 }
325 // if not everything below us is covered, we plug the holes!
326 Region holes(clip.subtract(under));
327 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700328 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700329 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 return;
331 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700332 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333}
334
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800335// As documented in libhardware header, formats in the range
336// 0x100 - 0x1FF are specific to the HAL implementation, and
337// are known to have no alpha channel
338// TODO: move definition for device-specific range into
339// hardware.h, instead of using hard-coded values here.
340#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
341
342bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
343{
344 // If buffers where set with eOpaque flag, all buffers are known to
345 // be opaque without having to check their actual format
346 if (mNeedsBlending && buffer != NULL) {
347 PixelFormat format = buffer->getPixelFormat();
348
349 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
350 return false;
351 }
352
353 PixelFormatInfo info;
354 status_t err = getPixelFormatInfo(format, &info);
355 if (!err && info.h_alpha <= info.l_alpha) {
356 return false;
357 }
358 }
359
360 // Return opacity as determined from flags and format options
361 // passed to setBuffers()
362 return mNeedsBlending;
363}
364
365bool Layer::needsBlending() const
366{
367 if (mBufferManager.hasActiveBuffer()) {
368 return needsBlending(mBufferManager.getActiveBuffer());
369 }
370
371 return mNeedsBlending;
372}
373
Mathias Agopiana7f66922010-05-26 22:08:52 -0700374bool Layer::needsFiltering() const
375{
376 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800377 // if our buffer is not the same size than ourselves,
378 // we need filtering.
379 Mutex::Autolock _l(mLock);
380 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700381 return true;
382 }
383 return LayerBase::needsFiltering();
384}
385
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800386bool Layer::isProtected() const
387{
388 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
389 return (activeBuffer != 0) &&
390 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
391}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700392
393status_t Layer::setBufferCount(int bufferCount)
394{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700395 ClientRef::Access sharedClient(mUserClientRef);
396 SharedBufferServer* lcblk(sharedClient.get());
397 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700398 // oops, the client is already gone
399 return DEAD_OBJECT;
400 }
401
Mathias Agopianbb641242010-05-18 17:06:55 -0700402 // NOTE: lcblk->resize() is protected by an internal lock
403 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700404 if (err == NO_ERROR) {
405 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
406 mBufferManager.resize(bufferCount, mFlinger, dpy);
407 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700408
409 return err;
410}
411
Mathias Agopiana138f892010-05-21 17:24:35 -0700412sp<GraphicBuffer> Layer::requestBuffer(int index,
413 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
414 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415{
Mathias Agopian3330b202009-10-05 17:07:12 -0700416 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700417
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700418 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700419 return buffer;
420
421 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
422 return buffer;
423
Mathias Agopian48d819a2009-09-10 19:41:18 -0700424 // this ensures our client doesn't go away while we're accessing
425 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700426 ClientRef::Access sharedClient(mUserClientRef);
427 SharedBufferServer* lcblk(sharedClient.get());
428 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700429 // oops, the client is already gone
430 return buffer;
431 }
432
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700434 * This is called from the client's Surface::dequeue(). This can happen
435 * at any time, especially while we're in the middle of using the
436 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700437 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438
Mathias Agopian208cb072010-07-27 20:11:35 -0700439 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700440 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700441 { // scope for the lock
442 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700443
444 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700445 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700446 if (!reqFormat) reqFormat = mFormat;
447 if (!reqWidth) reqWidth = mWidth;
448 if (!reqHeight) reqHeight = mHeight;
449
450 w = reqWidth;
451 h = reqHeight;
452 f = reqFormat;
453
454 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
455 (reqFormat != mReqFormat)) {
456 mReqWidth = reqWidth;
457 mReqHeight = reqHeight;
458 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700459 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800460 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700461
Mathias Agopiana138f892010-05-21 17:24:35 -0700462 lcblk->reallocateAllExcept(index);
463 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700464 }
465
Mathias Agopian208cb072010-07-27 20:11:35 -0700466 // here we have to reallocate a new buffer because the buffer could be
467 // used as the front buffer, or by a client in our process
468 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700469 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700470 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
471 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700472
473 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800474 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700475 LOGE_IF(err || buffer->handle == 0,
476 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
477 this, index, w, h, strerror(-err));
478 } else {
479 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700480 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
481 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700482 }
483
484 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700485 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700486 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700487 }
488 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489}
490
Mathias Agopian3330b202009-10-05 17:07:12 -0700491uint32_t Layer::getEffectiveUsage(uint32_t usage) const
492{
493 /*
494 * buffers used for software rendering, but h/w composition
495 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
496 *
497 * buffers used for h/w rendering and h/w composition
498 * are allocated with HW_RENDER | HW_TEXTURE
499 *
500 * buffers used with h/w rendering and either NPOT or no egl_image_ext
501 * are allocated with SW_READ_RARELY | HW_RENDER
502 *
503 */
504
505 if (mSecure) {
506 // secure buffer, don't store it into the GPU
507 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
508 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
509 } else {
510 // it's allowed to modify the usage flags here, but generally
511 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700512 // request EGLImage for all buffers
513 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700514 }
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800515 if (mProtectedByApp) {
Glenn Kasten16f04532011-01-19 15:27:27 -0800516 // need a hardware-protected path to external video sink
517 usage |= GraphicBuffer::USAGE_PROTECTED;
518 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700519 return usage;
520}
521
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522uint32_t Layer::doTransaction(uint32_t flags)
523{
524 const Layer::State& front(drawingState());
525 const Layer::State& temp(currentState());
526
Mathias Agopiana138f892010-05-21 17:24:35 -0700527 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
528 (front.requested_h != temp.requested_h);
529
530 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700531 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700533 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
534 this,
535 int(temp.requested_w), int(temp.requested_h),
536 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537
Mathias Agopiana138f892010-05-21 17:24:35 -0700538 if (!isFixedSize()) {
539 // we're being resized and there is a freeze display request,
540 // acquire a freeze lock, so that the screen stays put
541 // until we've redrawn at the new size; this is to avoid
542 // glitches upon orientation changes.
543 if (mFlinger->hasFreezeRequest()) {
544 // if the surface is hidden, don't try to acquire the
545 // freeze lock, since hidden surfaces may never redraw
546 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
547 mFreezeLock = mFlinger->getFreezeLock();
548 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700550
551 // this will make sure LayerBase::doTransaction doesn't update
552 // the drawing state's size
553 Layer::State& editDraw(mDrawingState);
554 editDraw.requested_w = temp.requested_w;
555 editDraw.requested_h = temp.requested_h;
556
557 // record the new size, form this point on, when the client request
558 // a buffer, it'll get the new size.
559 setBufferSize(temp.requested_w, temp.requested_h);
560
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700561 ClientRef::Access sharedClient(mUserClientRef);
562 SharedBufferServer* lcblk(sharedClient.get());
563 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700564 // all buffers need reallocation
565 lcblk->reallocateAll();
566 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700567 } else {
568 // record the new size
569 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 }
571 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 if (temp.sequence != front.sequence) {
574 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
575 // this surface is now hidden, so it shouldn't hold a freeze lock
576 // (it may never redraw, which is fine if it is hidden)
577 mFreezeLock.clear();
578 }
579 }
580
581 return LayerBase::doTransaction(flags);
582}
583
Mathias Agopiana138f892010-05-21 17:24:35 -0700584void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700585 Mutex::Autolock _l(mLock);
586 mWidth = w;
587 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800588 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589}
590
Mathias Agopiana138f892010-05-21 17:24:35 -0700591bool Layer::isFixedSize() const {
592 Mutex::Autolock _l(mLock);
593 return mFixedSize;
594}
595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596// ----------------------------------------------------------------------------
597// pageflip handling...
598// ----------------------------------------------------------------------------
599
600void Layer::lockPageFlip(bool& recomputeVisibleRegions)
601{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700602 ClientRef::Access sharedClient(mUserClientRef);
603 SharedBufferServer* lcblk(sharedClient.get());
604 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700605 // client died
606 recomputeVisibleRegions = true;
607 return;
608 }
609
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700610 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700611 if (buf == NOT_ENOUGH_DATA) {
612 // NOTE: This is not an error, it simply means there is nothing to
613 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700614 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 return;
616 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700617
Mathias Agopiand606de62010-05-10 20:06:11 -0700618 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700619 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700620 mPostedDirtyRegion.clear();
621 return;
622 }
623
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700624 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800625
626 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800627 const bool activeBlending =
628 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
629
Mathias Agopiand606de62010-05-10 20:06:11 -0700630 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700631 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700632 mPostedDirtyRegion.clear();
633 return;
634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635
Mathias Agopianda9584d2010-12-13 18:51:59 -0800636 if (noActiveBuffer) {
637 // we didn't have an active buffer, we need to recompute
638 // our visible region
639 recomputeVisibleRegions = true;
640 }
641
Mathias Agopian3330b202009-10-05 17:07:12 -0700642 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700643 if (newFrontBuffer != NULL) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800644 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
645 // new buffer has different opacity than previous active buffer, need
646 // to recompute visible regions accordingly
647 recomputeVisibleRegions = true;
648 }
649
Mathias Agopianb661d662010-08-19 17:01:19 -0700650 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700651 // compute the posted region
652 const Region dirty(lcblk->getDirtyRegion(buf));
653 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700655 // update the layer size and release freeze-lock
656 const Layer::State& front(drawingState());
657 if (newFrontBuffer->getWidth() == front.requested_w &&
658 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700659 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700660 if ((front.w != front.requested_w) ||
661 (front.h != front.requested_h))
662 {
663 // Here we pretend the transaction happened by updating the
664 // current and drawing states. Drawing state is only accessed
665 // in this thread, no need to have it locked
666 Layer::State& editDraw(mDrawingState);
667 editDraw.w = editDraw.requested_w;
668 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700669
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700670 // We also need to update the current state so that we don't
671 // end-up doing too much work during the next transaction.
672 // NOTE: We actually don't need hold the transaction lock here
673 // because State::w and State::h are only accessed from
674 // this thread
675 Layer::State& editTemp(currentState());
676 editTemp.w = editDraw.w;
677 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700678
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700679 // recompute visible region
680 recomputeVisibleRegions = true;
681 }
682
683 // we now have the correct size, unfreeze the screen
684 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700685 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700686
687 // get the crop region
688 setBufferCrop( lcblk->getCrop(buf) );
689
690 // get the transformation
691 setBufferTransform( lcblk->getTransform(buf) );
692
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700693 } else {
694 // this should not happen unless we ran out of memory while
695 // allocating the buffer. we're hoping that things will get back
696 // to normal the next time the app tries to draw into this buffer.
697 // meanwhile, pretend the screen didn't update.
698 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700699 }
700
Mathias Agopiane7005012009-10-07 16:44:10 -0700701 if (lcblk->getQueuedCount()) {
702 // signal an event if we have more buffers waiting
703 mFlinger->signalEvent();
704 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705
Mathias Agopian245e4d72010-04-21 15:24:11 -0700706 /* a buffer was posted, so we need to call reloadTexture(), which
707 * will update our internal data structures (eg: EGLImageKHR or
708 * texture names). we need to do this even if mPostedDirtyRegion is
709 * empty -- it's orthogonal to the fact that a new buffer was posted,
710 * for instance, a degenerate case could be that the user did an empty
711 * update but repainted the buffer with appropriate content (after a
712 * resize for instance).
713 */
714 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715}
716
717void Layer::unlockPageFlip(
718 const Transform& planeTransform, Region& outDirtyRegion)
719{
720 Region dirtyRegion(mPostedDirtyRegion);
721 if (!dirtyRegion.isEmpty()) {
722 mPostedDirtyRegion.clear();
723 // The dirty region is given in the layer's coordinate space
724 // transform the dirty region by the surface's transformation
725 // and the global transformation.
726 const Layer::State& s(drawingState());
727 const Transform tr(planeTransform * s.transform);
728 dirtyRegion = tr.transform(dirtyRegion);
729
730 // At this point, the dirty region is in screen space.
731 // Make sure it's constrained by the visible region (which
732 // is in screen space as well).
733 dirtyRegion.andSelf(visibleRegionScreen);
734 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800736 if (visibleRegionScreen.isEmpty()) {
737 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700738 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800739 mFreezeLock.clear();
740 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741}
742
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700743void Layer::dump(String8& result, char* buffer, size_t SIZE) const
744{
745 LayerBaseClient::dump(result, buffer, SIZE);
746
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700747 ClientRef::Access sharedClient(mUserClientRef);
748 SharedBufferServer* lcblk(sharedClient.get());
749 uint32_t totalTime = 0;
750 if (lcblk) {
751 SharedBufferStack::Statistics stats = lcblk->getStats();
752 totalTime= stats.totalTime;
753 result.append( lcblk->dump(" ") );
754 }
755
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700756 sp<const GraphicBuffer> buf0(getBuffer(0));
757 sp<const GraphicBuffer> buf1(getBuffer(1));
758 uint32_t w0=0, h0=0, s0=0;
759 uint32_t w1=0, h1=0, s1=0;
760 if (buf0 != 0) {
761 w0 = buf0->getWidth();
762 h0 = buf0->getHeight();
763 s0 = buf0->getStride();
764 }
765 if (buf1 != 0) {
766 w1 = buf1->getWidth();
767 h1 = buf1->getHeight();
768 s1 = buf1->getStride();
769 }
770 snprintf(buffer, SIZE,
771 " "
772 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
773 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700774 mFormat, w0, h0, s0, w1, h1, s1,
775 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700776
777 result.append(buffer);
778}
779
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700780// ---------------------------------------------------------------------------
781
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700782Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700783 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700784}
785
786Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700787}
788
789int32_t Layer::ClientRef::getToken() const {
790 Mutex::Autolock _l(mLock);
791 return mToken;
792}
793
Mathias Agopian579b3f82010-06-08 19:54:15 -0700794sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700795 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700796 return mUserClient.promote();
797}
798
799status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
800 const sp<SharedBufferServer>& sharedClient, int32_t token) {
801 Mutex::Autolock _l(mLock);
802
803 { // scope for strong mUserClient reference
804 sp<UserClient> userClient(mUserClient.promote());
Jamie Gennis5fd799d2011-03-18 16:35:13 -0700805 if (userClient != 0 && mControlBlock != 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700806 mControlBlock->setStatus(NO_INIT);
807 }
808 }
809
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700810 mUserClient = uc;
811 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700812 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700813 return NO_ERROR;
814}
815
816sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
817 return mUserClient.promote();
818}
819
820// this class gives us access to SharedBufferServer safely
821// it makes sure the UserClient (and its associated shared memory)
822// won't go away while we're accessing it.
823Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700824 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700825{
826 Mutex::Autolock _l(ref.mLock);
827 mUserClientStrongRef = ref.mUserClient.promote();
828 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700829 mControlBlock = ref.mControlBlock;
830}
831
832Layer::ClientRef::Access::~Access()
833{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700834}
835
836// ---------------------------------------------------------------------------
837
Mathias Agopiand606de62010-05-10 20:06:11 -0700838Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700839 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800840 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700841{
842}
843
Mathias Agopianbb641242010-05-18 17:06:55 -0700844Layer::BufferManager::~BufferManager()
845{
846}
847
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700848status_t Layer::BufferManager::resize(size_t size,
849 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700850{
851 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700852
853 if (size < mNumBuffers) {
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700854 // If there is an active texture, move it into slot 0 if needed
855 if (mActiveBufferIndex > 0) {
856 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
857 mBufferData[mActiveBufferIndex] = mBufferData[0];
858 mBufferData[0] = activeBufferData;
859 mActiveBufferIndex = 0;
860 }
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700861
862 // Free the buffers that are no longer needed.
863 for (size_t i = size; i < mNumBuffers; i++) {
864 mBufferData[i].buffer = 0;
865
866 // Create a message to destroy the textures on SurfaceFlinger's GL
867 // thread.
868 class MessageDestroyTexture : public MessageBase {
869 Image mTexture;
870 EGLDisplay mDpy;
871 public:
872 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
873 : mTexture(texture), mDpy(dpy) { }
874 virtual bool handler() {
875 status_t err = Layer::BufferManager::destroyTexture(
876 &mTexture, mDpy);
877 LOGE_IF(err<0, "error destroying texture: %d (%s)",
878 mTexture.name, strerror(-err));
879 return true; // XXX: err == 0; ????
880 }
881 };
882
883 MessageDestroyTexture *msg = new MessageDestroyTexture(
884 mBufferData[i].texture, dpy);
885
886 // Don't allow this texture to be cleaned up by
887 // BufferManager::destroy.
888 mBufferData[i].texture.name = -1U;
889 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
890
891 // Post the message to the SurfaceFlinger object.
892 flinger->postMessageAsync(msg);
893 }
894 }
895
Mathias Agopianbb641242010-05-18 17:06:55 -0700896 mNumBuffers = size;
897 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700898}
899
900// only for debugging
901sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
902 return mBufferData[index].buffer;
903}
904
905status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800906 BufferData const * const buffers = mBufferData;
907 Mutex::Autolock _l(mLock);
908 mActiveBuffer = buffers[index].buffer;
909 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700910 return NO_ERROR;
911}
912
913size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800914 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700915}
916
917Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700918 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800919 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700920 res = mFailoverTexture;
921 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800922 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700923 }
924 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700925}
926
927sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800928 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700929}
930
Mathias Agopianda9584d2010-12-13 18:51:59 -0800931bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800932 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800933}
934
Mathias Agopiand606de62010-05-10 20:06:11 -0700935sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
936{
Mathias Agopianbb641242010-05-18 17:06:55 -0700937 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700938 sp<GraphicBuffer> buffer;
939 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700940 buffer = buffers[index].buffer;
941 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700942 return buffer;
943}
944
945status_t Layer::BufferManager::attachBuffer(size_t index,
946 const sp<GraphicBuffer>& buffer)
947{
Mathias Agopianbb641242010-05-18 17:06:55 -0700948 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700949 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700950 buffers[index].buffer = buffer;
951 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700952 return NO_ERROR;
953}
954
955status_t Layer::BufferManager::destroy(EGLDisplay dpy)
956{
Mathias Agopianbb641242010-05-18 17:06:55 -0700957 BufferData* const buffers = mBufferData;
958 size_t num;
959 { // scope for the lock
960 Mutex::Autolock _l(mLock);
961 num = mNumBuffers;
962 for (size_t i=0 ; i<num ; i++) {
963 buffers[i].buffer = 0;
964 }
965 }
966 for (size_t i=0 ; i<num ; i++) {
967 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700968 }
969 destroyTexture(&mFailoverTexture, dpy);
970 return NO_ERROR;
971}
972
973status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
974 const sp<GraphicBuffer>& buffer)
975{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700976 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800977 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700978 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700979 if (!mFailover) {
980 Image& texture(mBufferData[index].texture);
981 err = mTextureManager.initEglImage(&texture, dpy, buffer);
982 // if EGLImage fails, we switch to regular texture mode, and we
983 // free all resources associated with using EGLImages.
984 if (err == NO_ERROR) {
985 mFailover = false;
986 destroyTexture(&mFailoverTexture, dpy);
987 } else {
988 mFailover = true;
989 const size_t num = mNumBuffers;
990 for (size_t i=0 ; i<num ; i++) {
991 destroyTexture(&mBufferData[i].texture, dpy);
992 }
Andreas Hubere049a952010-06-25 09:25:19 -0700993 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700994 } else {
995 // we failed once, don't try again
996 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700997 }
998 }
999 return err;
1000}
1001
1002status_t Layer::BufferManager::loadTexture(
1003 const Region& dirty, const GGLSurface& t)
1004{
1005 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1006}
1007
Mathias Agopianbb641242010-05-18 17:06:55 -07001008status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1009{
1010 if (tex->name != -1U) {
1011 glDeleteTextures(1, &tex->name);
1012 tex->name = -1U;
1013 }
1014 if (tex->image != EGL_NO_IMAGE_KHR) {
1015 eglDestroyImageKHR(dpy, tex->image);
1016 tex->image = EGL_NO_IMAGE_KHR;
1017 }
1018 return NO_ERROR;
1019}
1020
Mathias Agopiand606de62010-05-10 20:06:11 -07001021// ---------------------------------------------------------------------------
1022
Mathias Agopian9a112062009-04-17 19:36:26 -07001023Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -07001024 const sp<Layer>& owner)
1025 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -07001026{
1027}
1028
1029Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001030{
1031}
1032
Mathias Agopiana138f892010-05-21 17:24:35 -07001033sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1034 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001035{
Mathias Agopian3330b202009-10-05 17:07:12 -07001036 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001037 sp<Layer> owner(getOwner());
1038 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001039 /*
1040 * requestBuffer() cannot be called from the main thread
1041 * as it could cause a dead-lock, since it may have to wait
1042 * on conditions updated my the main thread.
1043 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001044 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001045 }
1046 return buffer;
1047}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001049status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1050{
1051 status_t err = DEAD_OBJECT;
1052 sp<Layer> owner(getOwner());
1053 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001054 /*
1055 * setBufferCount() cannot be called from the main thread
1056 * as it could cause a dead-lock, since it may have to wait
1057 * on conditions updated my the main thread.
1058 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001059 err = owner->setBufferCount(bufferCount);
1060 }
1061 return err;
1062}
1063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064// ---------------------------------------------------------------------------
1065
1066
1067}; // namespace android