blob: 731d82b67083e5e9a9e081f2c10bba8380730712 [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 Agopianf9d93272009-06-19 17:00:27 -0700148status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 PixelFormat format, uint32_t flags)
150{
Mathias Agopian401c2572009-09-23 19:16:27 -0700151 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 PixelFormatInfo info;
153 status_t err = getPixelFormatInfo(format, &info);
154 if (err) return err;
155
Mathias Agopian401c2572009-09-23 19:16:27 -0700156 // the display's pixel format
157 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700158 uint32_t const maxSurfaceDims = min(
159 hw.getMaxTextureSize(), hw.getMaxViewportDims());
160
161 // never allow a surface larger than what our underlying GL implementation
162 // can handle.
163 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
164 return BAD_VALUE;
165 }
166
Mathias Agopian401c2572009-09-23 19:16:27 -0700167 PixelFormatInfo displayInfo;
168 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700169 const uint32_t hwFlags = hw.getFlags();
170
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700171 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700172 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700173 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700174
175 mReqFormat = format;
176 mReqWidth = w;
177 mReqHeight = h;
178
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800180 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700181 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
182 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700183
Mathias Agopian401c2572009-09-23 19:16:27 -0700184 // we use the red index
185 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
186 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
187 mNeedsDithering = layerRedsize > displayRedSize;
188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 return NO_ERROR;
190}
191
Mathias Agopiana350ff92010-08-10 17:14:02 -0700192void Layer::setGeometry(hwc_layer_t* hwcl)
193{
194 hwcl->compositionType = HWC_FRAMEBUFFER;
195 hwcl->hints = 0;
196 hwcl->flags = 0;
197 hwcl->transform = 0;
198 hwcl->blending = HWC_BLENDING_NONE;
199
200 // we can't do alpha-fade with the hwc HAL
201 const State& s(drawingState());
202 if (s.alpha < 0xFF) {
203 hwcl->flags = HWC_SKIP_LAYER;
204 return;
205 }
206
207 // we can only handle simple transformation
208 if (mOrientation & Transform::ROT_INVALID) {
209 hwcl->flags = HWC_SKIP_LAYER;
210 return;
211 }
212
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800213 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
214 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700215
216 if (needsBlending()) {
217 hwcl->blending = mPremultipliedAlpha ?
218 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
219 }
220
221 hwcl->displayFrame.left = mTransformedBounds.left;
222 hwcl->displayFrame.top = mTransformedBounds.top;
223 hwcl->displayFrame.right = mTransformedBounds.right;
224 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
225
226 hwcl->visibleRegionScreen.rects =
227 reinterpret_cast<hwc_rect_t const *>(
228 visibleRegionScreen.getArray(
229 &hwcl->visibleRegionScreen.numRects));
230}
231
232void Layer::setPerFrameData(hwc_layer_t* hwcl) {
233 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
234 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800235 // this can happen if the client never drew into this layer yet,
236 // or if we ran out of memory. In that case, don't let
237 // HWC handle it.
238 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700239 hwcl->handle = NULL;
240 return;
241 }
Louis Huemiller04048142010-12-01 12:29:36 -0800242 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800243
244 if (!mBufferCrop.isEmpty()) {
245 hwcl->sourceCrop.left = mBufferCrop.left;
246 hwcl->sourceCrop.top = mBufferCrop.top;
247 hwcl->sourceCrop.right = mBufferCrop.right;
248 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
249 } else {
250 hwcl->sourceCrop.left = 0;
251 hwcl->sourceCrop.top = 0;
252 hwcl->sourceCrop.right = buffer->width;
253 hwcl->sourceCrop.bottom = buffer->height;
254 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700255}
256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257void Layer::reloadTexture(const Region& dirty)
258{
Mathias Agopiand606de62010-05-10 20:06:11 -0700259 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800260 if (buffer == NULL) {
261 // this situation can happen if we ran out of memory for instance.
262 // not much we can do. continue to use whatever texture was bound
263 // to this context.
264 return;
265 }
266
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700267 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700268 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
269 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
270 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700271 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700273 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800274slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700276 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
277 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
278 LOGE_IF(res, "error %d (%s) locking buffer %p",
279 res, strerror(res), buffer.get());
280 if (res == NO_ERROR) {
281 mBufferManager.loadTexture(dirty, t);
282 buffer->unlock();
283 }
284 } else {
285 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288}
289
Mathias Agopian74c40c02010-09-29 13:02:36 -0700290void Layer::drawForSreenShot() const
291{
Mathias Agopian733189d2010-12-02 21:32:29 -0800292 const bool currentFiltering = mNeedsFiltering;
293 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700294 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800295 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700296}
297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298void Layer::onDraw(const Region& clip) const
299{
Mathias Agopiand606de62010-05-10 20:06:11 -0700300 Texture tex(mBufferManager.getActiveTexture());
301 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700303 // in fact never been drawn into. This happens frequently with
304 // SurfaceView because the WindowManager can't know when the client
305 // has drawn the first time.
306
307 // If there is nothing under us, we paint the screen in black, otherwise
308 // we just skip this update.
309
310 // figure out if there is something below us
311 Region under;
312 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
313 const size_t count = drawingLayers.size();
314 for (size_t i=0 ; i<count ; ++i) {
315 const sp<LayerBase>& layer(drawingLayers[i]);
316 if (layer.get() == static_cast<LayerBase const*>(this))
317 break;
318 under.orSelf(layer->visibleRegionScreen);
319 }
320 // if not everything below us is covered, we plug the holes!
321 Region holes(clip.subtract(under));
322 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700323 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700324 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 return;
326 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700327 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800330// As documented in libhardware header, formats in the range
331// 0x100 - 0x1FF are specific to the HAL implementation, and
332// are known to have no alpha channel
333// TODO: move definition for device-specific range into
334// hardware.h, instead of using hard-coded values here.
335#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
336
337bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
338{
339 // If buffers where set with eOpaque flag, all buffers are known to
340 // be opaque without having to check their actual format
341 if (mNeedsBlending && buffer != NULL) {
342 PixelFormat format = buffer->getPixelFormat();
343
344 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
345 return false;
346 }
347
348 PixelFormatInfo info;
349 status_t err = getPixelFormatInfo(format, &info);
350 if (!err && info.h_alpha <= info.l_alpha) {
351 return false;
352 }
353 }
354
355 // Return opacity as determined from flags and format options
356 // passed to setBuffers()
357 return mNeedsBlending;
358}
359
360bool Layer::needsBlending() const
361{
362 if (mBufferManager.hasActiveBuffer()) {
363 return needsBlending(mBufferManager.getActiveBuffer());
364 }
365
366 return mNeedsBlending;
367}
368
Mathias Agopiana7f66922010-05-26 22:08:52 -0700369bool Layer::needsFiltering() const
370{
371 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800372 // if our buffer is not the same size than ourselves,
373 // we need filtering.
374 Mutex::Autolock _l(mLock);
375 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700376 return true;
377 }
378 return LayerBase::needsFiltering();
379}
380
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800381bool Layer::isProtected() const
382{
383 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
384 return (activeBuffer != 0) &&
385 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
386}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700387
388status_t Layer::setBufferCount(int bufferCount)
389{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700390 ClientRef::Access sharedClient(mUserClientRef);
391 SharedBufferServer* lcblk(sharedClient.get());
392 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700393 // oops, the client is already gone
394 return DEAD_OBJECT;
395 }
396
Mathias Agopianbb641242010-05-18 17:06:55 -0700397 // NOTE: lcblk->resize() is protected by an internal lock
398 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700399 if (err == NO_ERROR) {
400 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
401 mBufferManager.resize(bufferCount, mFlinger, dpy);
402 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700403
404 return err;
405}
406
Mathias Agopiana138f892010-05-21 17:24:35 -0700407sp<GraphicBuffer> Layer::requestBuffer(int index,
408 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
409 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410{
Mathias Agopian3330b202009-10-05 17:07:12 -0700411 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700412
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700413 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700414 return buffer;
415
416 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
417 return buffer;
418
Mathias Agopian48d819a2009-09-10 19:41:18 -0700419 // this ensures our client doesn't go away while we're accessing
420 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700421 ClientRef::Access sharedClient(mUserClientRef);
422 SharedBufferServer* lcblk(sharedClient.get());
423 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700424 // oops, the client is already gone
425 return buffer;
426 }
427
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700429 * This is called from the client's Surface::dequeue(). This can happen
430 * at any time, especially while we're in the middle of using the
431 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700432 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433
Mathias Agopian208cb072010-07-27 20:11:35 -0700434 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700435 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700436 { // scope for the lock
437 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700438
439 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700440 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700441 if (!reqFormat) reqFormat = mFormat;
442 if (!reqWidth) reqWidth = mWidth;
443 if (!reqHeight) reqHeight = mHeight;
444
445 w = reqWidth;
446 h = reqHeight;
447 f = reqFormat;
448
449 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
450 (reqFormat != mReqFormat)) {
451 mReqWidth = reqWidth;
452 mReqHeight = reqHeight;
453 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700454 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800455 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700456
Mathias Agopiana138f892010-05-21 17:24:35 -0700457 lcblk->reallocateAllExcept(index);
458 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700459 }
460
Mathias Agopian208cb072010-07-27 20:11:35 -0700461 // here we have to reallocate a new buffer because the buffer could be
462 // used as the front buffer, or by a client in our process
463 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700464 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700465 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
466 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700467
468 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800469 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700470 LOGE_IF(err || buffer->handle == 0,
471 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
472 this, index, w, h, strerror(-err));
473 } else {
474 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700475 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
476 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700477 }
478
479 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700480 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700481 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700482 }
483 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484}
485
Mathias Agopian3330b202009-10-05 17:07:12 -0700486uint32_t Layer::getEffectiveUsage(uint32_t usage) const
487{
488 /*
489 * buffers used for software rendering, but h/w composition
490 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
491 *
492 * buffers used for h/w rendering and h/w composition
493 * are allocated with HW_RENDER | HW_TEXTURE
494 *
495 * buffers used with h/w rendering and either NPOT or no egl_image_ext
496 * are allocated with SW_READ_RARELY | HW_RENDER
497 *
498 */
499
500 if (mSecure) {
501 // secure buffer, don't store it into the GPU
502 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
503 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
504 } else {
505 // it's allowed to modify the usage flags here, but generally
506 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700507 // request EGLImage for all buffers
508 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700509 }
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800510 if (mProtectedByApp) {
Glenn Kasten16f04532011-01-19 15:27:27 -0800511 // need a hardware-protected path to external video sink
512 usage |= GraphicBuffer::USAGE_PROTECTED;
513 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700514 return usage;
515}
516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517uint32_t Layer::doTransaction(uint32_t flags)
518{
519 const Layer::State& front(drawingState());
520 const Layer::State& temp(currentState());
521
Mathias Agopiana138f892010-05-21 17:24:35 -0700522 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
523 (front.requested_h != temp.requested_h);
524
525 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700526 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700528 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
529 this,
530 int(temp.requested_w), int(temp.requested_h),
531 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532
Mathias Agopiana138f892010-05-21 17:24:35 -0700533 if (!isFixedSize()) {
534 // we're being resized and there is a freeze display request,
535 // acquire a freeze lock, so that the screen stays put
536 // until we've redrawn at the new size; this is to avoid
537 // glitches upon orientation changes.
538 if (mFlinger->hasFreezeRequest()) {
539 // if the surface is hidden, don't try to acquire the
540 // freeze lock, since hidden surfaces may never redraw
541 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
542 mFreezeLock = mFlinger->getFreezeLock();
543 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700545
546 // this will make sure LayerBase::doTransaction doesn't update
547 // the drawing state's size
548 Layer::State& editDraw(mDrawingState);
549 editDraw.requested_w = temp.requested_w;
550 editDraw.requested_h = temp.requested_h;
551
552 // record the new size, form this point on, when the client request
553 // a buffer, it'll get the new size.
554 setBufferSize(temp.requested_w, temp.requested_h);
555
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700556 ClientRef::Access sharedClient(mUserClientRef);
557 SharedBufferServer* lcblk(sharedClient.get());
558 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700559 // all buffers need reallocation
560 lcblk->reallocateAll();
561 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700562 } else {
563 // record the new size
564 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
566 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 if (temp.sequence != front.sequence) {
569 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
570 // this surface is now hidden, so it shouldn't hold a freeze lock
571 // (it may never redraw, which is fine if it is hidden)
572 mFreezeLock.clear();
573 }
574 }
575
576 return LayerBase::doTransaction(flags);
577}
578
Mathias Agopiana138f892010-05-21 17:24:35 -0700579void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700580 Mutex::Autolock _l(mLock);
581 mWidth = w;
582 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800583 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584}
585
Mathias Agopiana138f892010-05-21 17:24:35 -0700586bool Layer::isFixedSize() const {
587 Mutex::Autolock _l(mLock);
588 return mFixedSize;
589}
590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591// ----------------------------------------------------------------------------
592// pageflip handling...
593// ----------------------------------------------------------------------------
594
595void Layer::lockPageFlip(bool& recomputeVisibleRegions)
596{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700597 ClientRef::Access sharedClient(mUserClientRef);
598 SharedBufferServer* lcblk(sharedClient.get());
599 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700600 // client died
601 recomputeVisibleRegions = true;
602 return;
603 }
604
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700605 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700606 if (buf == NOT_ENOUGH_DATA) {
607 // NOTE: This is not an error, it simply means there is nothing to
608 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700609 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 return;
611 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700612
Mathias Agopiand606de62010-05-10 20:06:11 -0700613 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700614 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700615 mPostedDirtyRegion.clear();
616 return;
617 }
618
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700619 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800620
621 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800622 const bool activeBlending =
623 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
624
Mathias Agopiand606de62010-05-10 20:06:11 -0700625 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700626 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700627 mPostedDirtyRegion.clear();
628 return;
629 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630
Mathias Agopianda9584d2010-12-13 18:51:59 -0800631 if (noActiveBuffer) {
632 // we didn't have an active buffer, we need to recompute
633 // our visible region
634 recomputeVisibleRegions = true;
635 }
636
Mathias Agopian3330b202009-10-05 17:07:12 -0700637 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700638 if (newFrontBuffer != NULL) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800639 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
640 // new buffer has different opacity than previous active buffer, need
641 // to recompute visible regions accordingly
642 recomputeVisibleRegions = true;
643 }
644
Mathias Agopianb661d662010-08-19 17:01:19 -0700645 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700646 // compute the posted region
647 const Region dirty(lcblk->getDirtyRegion(buf));
648 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700650 // update the layer size and release freeze-lock
651 const Layer::State& front(drawingState());
652 if (newFrontBuffer->getWidth() == front.requested_w &&
653 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700654 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700655 if ((front.w != front.requested_w) ||
656 (front.h != front.requested_h))
657 {
658 // Here we pretend the transaction happened by updating the
659 // current and drawing states. Drawing state is only accessed
660 // in this thread, no need to have it locked
661 Layer::State& editDraw(mDrawingState);
662 editDraw.w = editDraw.requested_w;
663 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700664
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700665 // We also need to update the current state so that we don't
666 // end-up doing too much work during the next transaction.
667 // NOTE: We actually don't need hold the transaction lock here
668 // because State::w and State::h are only accessed from
669 // this thread
670 Layer::State& editTemp(currentState());
671 editTemp.w = editDraw.w;
672 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700673
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700674 // recompute visible region
675 recomputeVisibleRegions = true;
676 }
677
678 // we now have the correct size, unfreeze the screen
679 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700680 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700681
682 // get the crop region
683 setBufferCrop( lcblk->getCrop(buf) );
684
685 // get the transformation
686 setBufferTransform( lcblk->getTransform(buf) );
687
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700688 } else {
689 // this should not happen unless we ran out of memory while
690 // allocating the buffer. we're hoping that things will get back
691 // to normal the next time the app tries to draw into this buffer.
692 // meanwhile, pretend the screen didn't update.
693 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700694 }
695
Mathias Agopiane7005012009-10-07 16:44:10 -0700696 if (lcblk->getQueuedCount()) {
697 // signal an event if we have more buffers waiting
698 mFlinger->signalEvent();
699 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700
Mathias Agopian245e4d72010-04-21 15:24:11 -0700701 /* a buffer was posted, so we need to call reloadTexture(), which
702 * will update our internal data structures (eg: EGLImageKHR or
703 * texture names). we need to do this even if mPostedDirtyRegion is
704 * empty -- it's orthogonal to the fact that a new buffer was posted,
705 * for instance, a degenerate case could be that the user did an empty
706 * update but repainted the buffer with appropriate content (after a
707 * resize for instance).
708 */
709 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710}
711
712void Layer::unlockPageFlip(
713 const Transform& planeTransform, Region& outDirtyRegion)
714{
715 Region dirtyRegion(mPostedDirtyRegion);
716 if (!dirtyRegion.isEmpty()) {
717 mPostedDirtyRegion.clear();
718 // The dirty region is given in the layer's coordinate space
719 // transform the dirty region by the surface's transformation
720 // and the global transformation.
721 const Layer::State& s(drawingState());
722 const Transform tr(planeTransform * s.transform);
723 dirtyRegion = tr.transform(dirtyRegion);
724
725 // At this point, the dirty region is in screen space.
726 // Make sure it's constrained by the visible region (which
727 // is in screen space as well).
728 dirtyRegion.andSelf(visibleRegionScreen);
729 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800731 if (visibleRegionScreen.isEmpty()) {
732 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700733 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800734 mFreezeLock.clear();
735 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736}
737
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700738void Layer::dump(String8& result, char* buffer, size_t SIZE) const
739{
740 LayerBaseClient::dump(result, buffer, SIZE);
741
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700742 ClientRef::Access sharedClient(mUserClientRef);
743 SharedBufferServer* lcblk(sharedClient.get());
744 uint32_t totalTime = 0;
745 if (lcblk) {
746 SharedBufferStack::Statistics stats = lcblk->getStats();
747 totalTime= stats.totalTime;
748 result.append( lcblk->dump(" ") );
749 }
750
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700751 sp<const GraphicBuffer> buf0(getBuffer(0));
752 sp<const GraphicBuffer> buf1(getBuffer(1));
753 uint32_t w0=0, h0=0, s0=0;
754 uint32_t w1=0, h1=0, s1=0;
755 if (buf0 != 0) {
756 w0 = buf0->getWidth();
757 h0 = buf0->getHeight();
758 s0 = buf0->getStride();
759 }
760 if (buf1 != 0) {
761 w1 = buf1->getWidth();
762 h1 = buf1->getHeight();
763 s1 = buf1->getStride();
764 }
765 snprintf(buffer, SIZE,
766 " "
767 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
768 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700769 mFormat, w0, h0, s0, w1, h1, s1,
770 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700771
772 result.append(buffer);
773}
774
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700775// ---------------------------------------------------------------------------
776
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700777Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700778 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700779}
780
781Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700782}
783
784int32_t Layer::ClientRef::getToken() const {
785 Mutex::Autolock _l(mLock);
786 return mToken;
787}
788
Mathias Agopian579b3f82010-06-08 19:54:15 -0700789sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700790 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700791 return mUserClient.promote();
792}
793
794status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
795 const sp<SharedBufferServer>& sharedClient, int32_t token) {
796 Mutex::Autolock _l(mLock);
797
798 { // scope for strong mUserClient reference
799 sp<UserClient> userClient(mUserClient.promote());
Jamie Gennis5fd799d2011-03-18 16:35:13 -0700800 if (userClient != 0 && mControlBlock != 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700801 mControlBlock->setStatus(NO_INIT);
802 }
803 }
804
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700805 mUserClient = uc;
806 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700807 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700808 return NO_ERROR;
809}
810
811sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
812 return mUserClient.promote();
813}
814
815// this class gives us access to SharedBufferServer safely
816// it makes sure the UserClient (and its associated shared memory)
817// won't go away while we're accessing it.
818Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700819 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700820{
821 Mutex::Autolock _l(ref.mLock);
822 mUserClientStrongRef = ref.mUserClient.promote();
823 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700824 mControlBlock = ref.mControlBlock;
825}
826
827Layer::ClientRef::Access::~Access()
828{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700829}
830
831// ---------------------------------------------------------------------------
832
Mathias Agopiand606de62010-05-10 20:06:11 -0700833Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700834 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800835 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700836{
837}
838
Mathias Agopianbb641242010-05-18 17:06:55 -0700839Layer::BufferManager::~BufferManager()
840{
841}
842
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700843status_t Layer::BufferManager::resize(size_t size,
844 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700845{
846 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700847
848 if (size < mNumBuffers) {
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700849 // If there is an active texture, move it into slot 0 if needed
850 if (mActiveBufferIndex > 0) {
851 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
852 mBufferData[mActiveBufferIndex] = mBufferData[0];
853 mBufferData[0] = activeBufferData;
854 mActiveBufferIndex = 0;
855 }
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700856
857 // Free the buffers that are no longer needed.
858 for (size_t i = size; i < mNumBuffers; i++) {
859 mBufferData[i].buffer = 0;
860
861 // Create a message to destroy the textures on SurfaceFlinger's GL
862 // thread.
863 class MessageDestroyTexture : public MessageBase {
864 Image mTexture;
865 EGLDisplay mDpy;
866 public:
867 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
868 : mTexture(texture), mDpy(dpy) { }
869 virtual bool handler() {
870 status_t err = Layer::BufferManager::destroyTexture(
871 &mTexture, mDpy);
872 LOGE_IF(err<0, "error destroying texture: %d (%s)",
873 mTexture.name, strerror(-err));
874 return true; // XXX: err == 0; ????
875 }
876 };
877
878 MessageDestroyTexture *msg = new MessageDestroyTexture(
879 mBufferData[i].texture, dpy);
880
881 // Don't allow this texture to be cleaned up by
882 // BufferManager::destroy.
883 mBufferData[i].texture.name = -1U;
884 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
885
886 // Post the message to the SurfaceFlinger object.
887 flinger->postMessageAsync(msg);
888 }
889 }
890
Mathias Agopianbb641242010-05-18 17:06:55 -0700891 mNumBuffers = size;
892 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700893}
894
895// only for debugging
896sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
897 return mBufferData[index].buffer;
898}
899
900status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800901 BufferData const * const buffers = mBufferData;
902 Mutex::Autolock _l(mLock);
903 mActiveBuffer = buffers[index].buffer;
904 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700905 return NO_ERROR;
906}
907
908size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800909 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700910}
911
912Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700913 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800914 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700915 res = mFailoverTexture;
916 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800917 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700918 }
919 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700920}
921
922sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800923 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700924}
925
Mathias Agopianda9584d2010-12-13 18:51:59 -0800926bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800927 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800928}
929
Mathias Agopiand606de62010-05-10 20:06:11 -0700930sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
931{
Mathias Agopianbb641242010-05-18 17:06:55 -0700932 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700933 sp<GraphicBuffer> buffer;
934 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700935 buffer = buffers[index].buffer;
936 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700937 return buffer;
938}
939
940status_t Layer::BufferManager::attachBuffer(size_t index,
941 const sp<GraphicBuffer>& buffer)
942{
Mathias Agopianbb641242010-05-18 17:06:55 -0700943 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700944 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700945 buffers[index].buffer = buffer;
946 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700947 return NO_ERROR;
948}
949
950status_t Layer::BufferManager::destroy(EGLDisplay dpy)
951{
Mathias Agopianbb641242010-05-18 17:06:55 -0700952 BufferData* const buffers = mBufferData;
953 size_t num;
954 { // scope for the lock
955 Mutex::Autolock _l(mLock);
956 num = mNumBuffers;
957 for (size_t i=0 ; i<num ; i++) {
958 buffers[i].buffer = 0;
959 }
960 }
961 for (size_t i=0 ; i<num ; i++) {
962 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700963 }
964 destroyTexture(&mFailoverTexture, dpy);
965 return NO_ERROR;
966}
967
968status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
969 const sp<GraphicBuffer>& buffer)
970{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700971 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800972 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700973 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700974 if (!mFailover) {
975 Image& texture(mBufferData[index].texture);
976 err = mTextureManager.initEglImage(&texture, dpy, buffer);
977 // if EGLImage fails, we switch to regular texture mode, and we
978 // free all resources associated with using EGLImages.
979 if (err == NO_ERROR) {
980 mFailover = false;
981 destroyTexture(&mFailoverTexture, dpy);
982 } else {
983 mFailover = true;
984 const size_t num = mNumBuffers;
985 for (size_t i=0 ; i<num ; i++) {
986 destroyTexture(&mBufferData[i].texture, dpy);
987 }
Andreas Hubere049a952010-06-25 09:25:19 -0700988 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700989 } else {
990 // we failed once, don't try again
991 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700992 }
993 }
994 return err;
995}
996
997status_t Layer::BufferManager::loadTexture(
998 const Region& dirty, const GGLSurface& t)
999{
1000 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1001}
1002
Mathias Agopianbb641242010-05-18 17:06:55 -07001003status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1004{
1005 if (tex->name != -1U) {
1006 glDeleteTextures(1, &tex->name);
1007 tex->name = -1U;
1008 }
1009 if (tex->image != EGL_NO_IMAGE_KHR) {
1010 eglDestroyImageKHR(dpy, tex->image);
1011 tex->image = EGL_NO_IMAGE_KHR;
1012 }
1013 return NO_ERROR;
1014}
1015
Mathias Agopiand606de62010-05-10 20:06:11 -07001016// ---------------------------------------------------------------------------
1017
Mathias Agopian9a112062009-04-17 19:36:26 -07001018Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -07001019 const sp<Layer>& owner)
1020 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -07001021{
1022}
1023
1024Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001025{
1026}
1027
Mathias Agopiana138f892010-05-21 17:24:35 -07001028sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1029 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001030{
Mathias Agopian3330b202009-10-05 17:07:12 -07001031 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001032 sp<Layer> owner(getOwner());
1033 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001034 /*
1035 * requestBuffer() cannot be called from the main thread
1036 * as it could cause a dead-lock, since it may have to wait
1037 * on conditions updated my the main thread.
1038 */
Mathias Agopiana138f892010-05-21 17:24:35 -07001039 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001040 }
1041 return buffer;
1042}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001044status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1045{
1046 status_t err = DEAD_OBJECT;
1047 sp<Layer> owner(getOwner());
1048 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001049 /*
1050 * setBufferCount() cannot be called from the main thread
1051 * as it could cause a dead-lock, since it may have to wait
1052 * on conditions updated my the main thread.
1053 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001054 err = owner->setBufferCount(bufferCount);
1055 }
1056 return err;
1057}
1058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059// ---------------------------------------------------------------------------
1060
1061
1062}; // namespace android