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