blob: 8df2b9217d0c86a7b31e0c6a41488efa22596fe0 [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 Agopian5bf3abe2011-03-11 17:01:07 -080055 mFormat(PIXEL_FORMAT_NONE),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070056 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian401c2572009-09-23 19:16:27 -070057 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070058 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070059 mSecure(false),
Glenn Kasten16f04532011-01-19 15:27:27 -080060 mProtectedByApp(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070062 mBufferManager(mTextureManager),
Mathias Agopian5bf3abe2011-03-11 17:01:07 -080063 mWidth(0), mHeight(0),
Eric Hassold7ffe3802011-03-11 12:24:23 -080064 mNeedsScaling(false), mFixedSize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066}
67
68Layer::~Layer()
69{
Mathias Agopianbb641242010-05-18 17:06:55 -070070 // FIXME: must be called from the main UI thread
71 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
72 mBufferManager.destroy(dpy);
73
Mathias Agopianb7e930d2010-06-01 15:12:58 -070074 // we can use getUserClientUnsafe here because we know we're
75 // single-threaded at that point.
76 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
77 if (ourClient != 0) {
78 ourClient->detachLayer(this);
79 }
Mathias Agopiand606de62010-05-10 20:06:11 -070080}
81
Mathias Agopianca4d3602011-05-19 15:38:14 -070082void Layer::destroy() const {
83 mFlinger->destroyLayer(this);
84}
85
Mathias Agopianb7e930d2010-06-01 15:12:58 -070086status_t Layer::setToken(const sp<UserClient>& userClient,
87 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070088{
Mathias Agopian579b3f82010-06-08 19:54:15 -070089 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070090 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070091 getIdentity());
92
Mathias Agopian579b3f82010-06-08 19:54:15 -070093
Mathias Agopiandd17b3e2010-12-13 16:49:05 -080094 sp<UserClient> ourClient(mUserClientRef.getClient());
95
96 /*
97 * Here it is guaranteed that userClient != ourClient
98 * (see UserClient::getTokenForSurface()).
99 *
100 * We release the token used by this surface in ourClient below.
101 * This should be safe to do so now, since this layer won't be attached
102 * to this client, it should be okay to reuse that id.
103 *
104 * If this causes problems, an other solution would be to keep a list
105 * of all the {UserClient, token} ever used and release them when the
106 * Layer is destroyed.
107 *
108 */
109
110 if (ourClient != 0) {
111 ourClient->detachLayer(this);
112 }
113
114 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700115 LOGE_IF(err != NO_ERROR,
116 "ClientRef::setToken(%p, %p, %u) failed",
117 userClient.get(), lcblk.get(), token);
118
119 if (err == NO_ERROR) {
120 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700121 }
122
123 return err;
124}
125
126int32_t Layer::getToken() const
127{
128 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian579b3f82010-06-08 19:54:15 -0700131sp<UserClient> Layer::getClient() const
132{
133 return mUserClientRef.getClient();
134}
135
Mathias Agopiand606de62010-05-10 20:06:11 -0700136// called with SurfaceFlinger::mStateLock as soon as the layer is entered
137// in the purgatory list
138void Layer::onRemoved()
139{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700140 ClientRef::Access sharedClient(mUserClientRef);
141 SharedBufferServer* lcblk(sharedClient.get());
142 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700143 // wake up the condition
144 lcblk->setStatus(NO_INIT);
145 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700146}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700147
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149{
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800150 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
151 return sur;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}
153
Mathias Agopianf9d93272009-06-19 17:00:27 -0700154status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 PixelFormat format, uint32_t flags)
156{
Mathias Agopian401c2572009-09-23 19:16:27 -0700157 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 PixelFormatInfo info;
159 status_t err = getPixelFormatInfo(format, &info);
160 if (err) return err;
161
Mathias Agopian401c2572009-09-23 19:16:27 -0700162 // the display's pixel format
163 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700164 uint32_t const maxSurfaceDims = min(
165 hw.getMaxTextureSize(), hw.getMaxViewportDims());
166
167 // never allow a surface larger than what our underlying GL implementation
168 // can handle.
169 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
170 return BAD_VALUE;
171 }
172
Mathias Agopian401c2572009-09-23 19:16:27 -0700173 PixelFormatInfo displayInfo;
174 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700175 const uint32_t hwFlags = hw.getFlags();
176
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700177 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700178 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700179 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700180
181 mReqFormat = format;
182 mReqWidth = w;
183 mReqHeight = h;
184
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kasten16f04532011-01-19 15:27:27 -0800186 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
Romain Guy3b996c92010-10-10 13:33:22 -0700187 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
188 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700189
Mathias Agopian401c2572009-09-23 19:16:27 -0700190 // we use the red index
191 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
192 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
193 mNeedsDithering = layerRedsize > displayRedSize;
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 return NO_ERROR;
196}
197
Mathias Agopiana350ff92010-08-10 17:14:02 -0700198void Layer::setGeometry(hwc_layer_t* hwcl)
199{
200 hwcl->compositionType = HWC_FRAMEBUFFER;
201 hwcl->hints = 0;
202 hwcl->flags = 0;
203 hwcl->transform = 0;
204 hwcl->blending = HWC_BLENDING_NONE;
205
206 // we can't do alpha-fade with the hwc HAL
207 const State& s(drawingState());
208 if (s.alpha < 0xFF) {
209 hwcl->flags = HWC_SKIP_LAYER;
210 return;
211 }
212
213 // we can only handle simple transformation
214 if (mOrientation & Transform::ROT_INVALID) {
215 hwcl->flags = HWC_SKIP_LAYER;
216 return;
217 }
218
Mathias Agopian86bdb2f2010-12-08 17:23:18 -0800219 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
220 hwcl->transform = tr.getOrientation();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700221
222 if (needsBlending()) {
223 hwcl->blending = mPremultipliedAlpha ?
224 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
225 }
226
227 hwcl->displayFrame.left = mTransformedBounds.left;
228 hwcl->displayFrame.top = mTransformedBounds.top;
229 hwcl->displayFrame.right = mTransformedBounds.right;
230 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
231
232 hwcl->visibleRegionScreen.rects =
233 reinterpret_cast<hwc_rect_t const *>(
234 visibleRegionScreen.getArray(
235 &hwcl->visibleRegionScreen.numRects));
236}
237
238void Layer::setPerFrameData(hwc_layer_t* hwcl) {
239 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
240 if (buffer == NULL) {
Mathias Agopianda9584d2010-12-13 18:51:59 -0800241 // this can happen if the client never drew into this layer yet,
242 // or if we ran out of memory. In that case, don't let
243 // HWC handle it.
244 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700245 hwcl->handle = NULL;
246 return;
247 }
Louis Huemiller04048142010-12-01 12:29:36 -0800248 hwcl->handle = buffer->handle;
Mathias Agopianf3450692010-12-08 17:40:28 -0800249
250 if (!mBufferCrop.isEmpty()) {
251 hwcl->sourceCrop.left = mBufferCrop.left;
252 hwcl->sourceCrop.top = mBufferCrop.top;
253 hwcl->sourceCrop.right = mBufferCrop.right;
254 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
255 } else {
256 hwcl->sourceCrop.left = 0;
257 hwcl->sourceCrop.top = 0;
258 hwcl->sourceCrop.right = buffer->width;
259 hwcl->sourceCrop.bottom = buffer->height;
260 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700261}
262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263void Layer::reloadTexture(const Region& dirty)
264{
Mathias Agopiand606de62010-05-10 20:06:11 -0700265 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800266 if (buffer == NULL) {
267 // this situation can happen if we ran out of memory for instance.
268 // not much we can do. continue to use whatever texture was bound
269 // to this context.
270 return;
271 }
272
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700273 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700274 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
275 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
276 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700277 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700279 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800280slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700282 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
283 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
284 LOGE_IF(res, "error %d (%s) locking buffer %p",
285 res, strerror(res), buffer.get());
286 if (res == NO_ERROR) {
287 mBufferManager.loadTexture(dirty, t);
288 buffer->unlock();
289 }
290 } else {
291 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700292 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294}
295
Mathias Agopian74c40c02010-09-29 13:02:36 -0700296void Layer::drawForSreenShot() const
297{
Mathias Agopian733189d2010-12-02 21:32:29 -0800298 const bool currentFiltering = mNeedsFiltering;
299 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700300 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800301 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700302}
303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304void Layer::onDraw(const Region& clip) const
305{
Mathias Agopiand606de62010-05-10 20:06:11 -0700306 Texture tex(mBufferManager.getActiveTexture());
307 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700309 // in fact never been drawn into. This happens frequently with
310 // SurfaceView because the WindowManager can't know when the client
311 // has drawn the first time.
312
313 // If there is nothing under us, we paint the screen in black, otherwise
314 // we just skip this update.
315
316 // figure out if there is something below us
317 Region under;
318 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
319 const size_t count = drawingLayers.size();
320 for (size_t i=0 ; i<count ; ++i) {
321 const sp<LayerBase>& layer(drawingLayers[i]);
322 if (layer.get() == static_cast<LayerBase const*>(this))
323 break;
324 under.orSelf(layer->visibleRegionScreen);
325 }
326 // if not everything below us is covered, we plug the holes!
327 Region holes(clip.subtract(under));
328 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700329 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700330 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 return;
332 }
Mathias Agopiand606de62010-05-10 20:06:11 -0700333 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334}
335
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800336// As documented in libhardware header, formats in the range
337// 0x100 - 0x1FF are specific to the HAL implementation, and
338// are known to have no alpha channel
339// TODO: move definition for device-specific range into
340// hardware.h, instead of using hard-coded values here.
341#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
342
343bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
344{
345 // If buffers where set with eOpaque flag, all buffers are known to
346 // be opaque without having to check their actual format
347 if (mNeedsBlending && buffer != NULL) {
348 PixelFormat format = buffer->getPixelFormat();
349
350 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
351 return false;
352 }
353
354 PixelFormatInfo info;
355 status_t err = getPixelFormatInfo(format, &info);
356 if (!err && info.h_alpha <= info.l_alpha) {
357 return false;
358 }
359 }
360
361 // Return opacity as determined from flags and format options
362 // passed to setBuffers()
363 return mNeedsBlending;
364}
365
366bool Layer::needsBlending() const
367{
368 if (mBufferManager.hasActiveBuffer()) {
369 return needsBlending(mBufferManager.getActiveBuffer());
370 }
371
372 return mNeedsBlending;
373}
374
Mathias Agopiana7f66922010-05-26 22:08:52 -0700375bool Layer::needsFiltering() const
376{
377 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800378 // if our buffer is not the same size than ourselves,
379 // we need filtering.
380 Mutex::Autolock _l(mLock);
381 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700382 return true;
383 }
384 return LayerBase::needsFiltering();
385}
386
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800387bool Layer::isProtected() const
388{
389 sp<GraphicBuffer> activeBuffer(mBufferManager.getActiveBuffer());
390 return (activeBuffer != 0) &&
391 (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
392}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700393
394status_t Layer::setBufferCount(int bufferCount)
395{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700396 ClientRef::Access sharedClient(mUserClientRef);
397 SharedBufferServer* lcblk(sharedClient.get());
398 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700399 // oops, the client is already gone
400 return DEAD_OBJECT;
401 }
402
Mathias Agopianbb641242010-05-18 17:06:55 -0700403 // NOTE: lcblk->resize() is protected by an internal lock
404 status_t err = lcblk->resize(bufferCount);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700405 if (err == NO_ERROR) {
406 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
407 mBufferManager.resize(bufferCount, mFlinger, dpy);
408 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700409
410 return err;
411}
412
Mathias Agopiana138f892010-05-21 17:24:35 -0700413sp<GraphicBuffer> Layer::requestBuffer(int index,
414 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
415 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416{
Mathias Agopian3330b202009-10-05 17:07:12 -0700417 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700418
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700419 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700420 return buffer;
421
422 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
423 return buffer;
424
Mathias Agopian48d819a2009-09-10 19:41:18 -0700425 // this ensures our client doesn't go away while we're accessing
426 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700427 ClientRef::Access sharedClient(mUserClientRef);
428 SharedBufferServer* lcblk(sharedClient.get());
429 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700430 // oops, the client is already gone
431 return buffer;
432 }
433
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700434 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700435 * This is called from the client's Surface::dequeue(). This can happen
436 * at any time, especially while we're in the middle of using the
437 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700438 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439
Mathias Agopian208cb072010-07-27 20:11:35 -0700440 status_t err = NO_ERROR;
Mathias Agopiana138f892010-05-21 17:24:35 -0700441 uint32_t w, h, f;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700442 { // scope for the lock
443 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700444
445 // zero means default
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700446 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700447 if (!reqFormat) reqFormat = mFormat;
448 if (!reqWidth) reqWidth = mWidth;
449 if (!reqHeight) reqHeight = mHeight;
450
451 w = reqWidth;
452 h = reqHeight;
453 f = reqFormat;
454
455 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
456 (reqFormat != mReqFormat)) {
457 mReqWidth = reqWidth;
458 mReqHeight = reqHeight;
459 mReqFormat = reqFormat;
Mathias Agopiane44d21a2010-09-21 10:52:42 -0700460 mFixedSize = fixedSize;
Mathias Agopian733189d2010-12-02 21:32:29 -0800461 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700462
Mathias Agopiana138f892010-05-21 17:24:35 -0700463 lcblk->reallocateAllExcept(index);
464 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700465 }
466
Mathias Agopian208cb072010-07-27 20:11:35 -0700467 // here we have to reallocate a new buffer because the buffer could be
468 // used as the front buffer, or by a client in our process
469 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian3330b202009-10-05 17:07:12 -0700470 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian208cb072010-07-27 20:11:35 -0700471 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
472 err = buffer->initCheck();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700473
474 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800475 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700476 LOGE_IF(err || buffer->handle == 0,
477 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
478 this, index, w, h, strerror(-err));
479 } else {
480 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700481 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
482 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700483 }
484
485 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700486 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700487 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700488 }
489 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490}
491
Mathias Agopian3330b202009-10-05 17:07:12 -0700492uint32_t Layer::getEffectiveUsage(uint32_t usage) const
493{
494 /*
495 * buffers used for software rendering, but h/w composition
496 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
497 *
498 * buffers used for h/w rendering and h/w composition
499 * are allocated with HW_RENDER | HW_TEXTURE
500 *
501 * buffers used with h/w rendering and either NPOT or no egl_image_ext
502 * are allocated with SW_READ_RARELY | HW_RENDER
503 *
504 */
505
506 if (mSecure) {
507 // secure buffer, don't store it into the GPU
508 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
509 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
510 } else {
511 // it's allowed to modify the usage flags here, but generally
512 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700513 // request EGLImage for all buffers
514 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700515 }
Jamie Gennis7a4d0df2011-03-09 17:05:02 -0800516 if (mProtectedByApp) {
Glenn Kasten16f04532011-01-19 15:27:27 -0800517 // need a hardware-protected path to external video sink
518 usage |= GraphicBuffer::USAGE_PROTECTED;
519 }
Mathias Agopian3330b202009-10-05 17:07:12 -0700520 return usage;
521}
522
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523uint32_t Layer::doTransaction(uint32_t flags)
524{
525 const Layer::State& front(drawingState());
526 const Layer::State& temp(currentState());
527
Mathias Agopiana138f892010-05-21 17:24:35 -0700528 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
529 (front.requested_h != temp.requested_h);
530
531 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700532 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700534 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
535 this,
536 int(temp.requested_w), int(temp.requested_h),
537 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538
Mathias Agopiana138f892010-05-21 17:24:35 -0700539 if (!isFixedSize()) {
540 // we're being resized and there is a freeze display request,
541 // acquire a freeze lock, so that the screen stays put
542 // until we've redrawn at the new size; this is to avoid
543 // glitches upon orientation changes.
544 if (mFlinger->hasFreezeRequest()) {
545 // if the surface is hidden, don't try to acquire the
546 // freeze lock, since hidden surfaces may never redraw
547 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
548 mFreezeLock = mFlinger->getFreezeLock();
549 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700551
552 // this will make sure LayerBase::doTransaction doesn't update
553 // the drawing state's size
554 Layer::State& editDraw(mDrawingState);
555 editDraw.requested_w = temp.requested_w;
556 editDraw.requested_h = temp.requested_h;
557
558 // record the new size, form this point on, when the client request
559 // a buffer, it'll get the new size.
560 setBufferSize(temp.requested_w, temp.requested_h);
561
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700562 ClientRef::Access sharedClient(mUserClientRef);
563 SharedBufferServer* lcblk(sharedClient.get());
564 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700565 // all buffers need reallocation
566 lcblk->reallocateAll();
567 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700568 } else {
569 // record the new size
570 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
572 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700573
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574 if (temp.sequence != front.sequence) {
575 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
576 // this surface is now hidden, so it shouldn't hold a freeze lock
577 // (it may never redraw, which is fine if it is hidden)
578 mFreezeLock.clear();
579 }
580 }
581
582 return LayerBase::doTransaction(flags);
583}
584
Mathias Agopiana138f892010-05-21 17:24:35 -0700585void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700586 Mutex::Autolock _l(mLock);
587 mWidth = w;
588 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800589 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590}
591
Mathias Agopiana138f892010-05-21 17:24:35 -0700592bool Layer::isFixedSize() const {
593 Mutex::Autolock _l(mLock);
594 return mFixedSize;
595}
596
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597// ----------------------------------------------------------------------------
598// pageflip handling...
599// ----------------------------------------------------------------------------
600
601void Layer::lockPageFlip(bool& recomputeVisibleRegions)
602{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700603 ClientRef::Access sharedClient(mUserClientRef);
604 SharedBufferServer* lcblk(sharedClient.get());
605 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700606 // client died
607 recomputeVisibleRegions = true;
608 return;
609 }
610
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700611 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700612 if (buf == NOT_ENOUGH_DATA) {
613 // NOTE: This is not an error, it simply means there is nothing to
614 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700615 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 return;
617 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700618
Mathias Agopiand606de62010-05-10 20:06:11 -0700619 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700620 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700621 mPostedDirtyRegion.clear();
622 return;
623 }
624
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700625 // we retired a buffer, which becomes the new front buffer
Mathias Agopianda9584d2010-12-13 18:51:59 -0800626
627 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800628 const bool activeBlending =
629 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
630
Mathias Agopiand606de62010-05-10 20:06:11 -0700631 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700632 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700633 mPostedDirtyRegion.clear();
634 return;
635 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636
Mathias Agopianda9584d2010-12-13 18:51:59 -0800637 if (noActiveBuffer) {
638 // we didn't have an active buffer, we need to recompute
639 // our visible region
640 recomputeVisibleRegions = true;
641 }
642
Mathias Agopian3330b202009-10-05 17:07:12 -0700643 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700644 if (newFrontBuffer != NULL) {
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800645 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
646 // new buffer has different opacity than previous active buffer, need
647 // to recompute visible regions accordingly
648 recomputeVisibleRegions = true;
649 }
650
Mathias Agopianb661d662010-08-19 17:01:19 -0700651 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700652 // compute the posted region
653 const Region dirty(lcblk->getDirtyRegion(buf));
654 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700655
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700656 // update the layer size and release freeze-lock
657 const Layer::State& front(drawingState());
Jamie Gennis3629d7f2011-05-16 16:55:03 -0700658 if ((newFrontBuffer->getWidth() == front.requested_w &&
659 newFrontBuffer->getHeight() == front.requested_h) ||
660 isFixedSize())
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700661 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700662 if ((front.w != front.requested_w) ||
663 (front.h != front.requested_h))
664 {
665 // Here we pretend the transaction happened by updating the
666 // current and drawing states. Drawing state is only accessed
667 // in this thread, no need to have it locked
668 Layer::State& editDraw(mDrawingState);
669 editDraw.w = editDraw.requested_w;
670 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700671
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700672 // We also need to update the current state so that we don't
673 // end-up doing too much work during the next transaction.
674 // NOTE: We actually don't need hold the transaction lock here
675 // because State::w and State::h are only accessed from
676 // this thread
677 Layer::State& editTemp(currentState());
678 editTemp.w = editDraw.w;
679 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700680
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700681 // recompute visible region
682 recomputeVisibleRegions = true;
683 }
684
685 // we now have the correct size, unfreeze the screen
686 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700687 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700688
689 // get the crop region
690 setBufferCrop( lcblk->getCrop(buf) );
691
692 // get the transformation
693 setBufferTransform( lcblk->getTransform(buf) );
694
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700695 } else {
696 // this should not happen unless we ran out of memory while
697 // allocating the buffer. we're hoping that things will get back
698 // to normal the next time the app tries to draw into this buffer.
699 // meanwhile, pretend the screen didn't update.
700 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700701 }
702
Mathias Agopiane7005012009-10-07 16:44:10 -0700703 if (lcblk->getQueuedCount()) {
704 // signal an event if we have more buffers waiting
705 mFlinger->signalEvent();
706 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707
Mathias Agopian245e4d72010-04-21 15:24:11 -0700708 /* a buffer was posted, so we need to call reloadTexture(), which
709 * will update our internal data structures (eg: EGLImageKHR or
710 * texture names). we need to do this even if mPostedDirtyRegion is
711 * empty -- it's orthogonal to the fact that a new buffer was posted,
712 * for instance, a degenerate case could be that the user did an empty
713 * update but repainted the buffer with appropriate content (after a
714 * resize for instance).
715 */
716 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717}
718
719void Layer::unlockPageFlip(
720 const Transform& planeTransform, Region& outDirtyRegion)
721{
722 Region dirtyRegion(mPostedDirtyRegion);
723 if (!dirtyRegion.isEmpty()) {
724 mPostedDirtyRegion.clear();
725 // The dirty region is given in the layer's coordinate space
726 // transform the dirty region by the surface's transformation
727 // and the global transformation.
728 const Layer::State& s(drawingState());
729 const Transform tr(planeTransform * s.transform);
730 dirtyRegion = tr.transform(dirtyRegion);
731
732 // At this point, the dirty region is in screen space.
733 // Make sure it's constrained by the visible region (which
734 // is in screen space as well).
735 dirtyRegion.andSelf(visibleRegionScreen);
736 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800738 if (visibleRegionScreen.isEmpty()) {
739 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700740 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800741 mFreezeLock.clear();
742 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743}
744
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700745void Layer::dump(String8& result, char* buffer, size_t SIZE) const
746{
747 LayerBaseClient::dump(result, buffer, SIZE);
748
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700749 ClientRef::Access sharedClient(mUserClientRef);
750 SharedBufferServer* lcblk(sharedClient.get());
751 uint32_t totalTime = 0;
752 if (lcblk) {
753 SharedBufferStack::Statistics stats = lcblk->getStats();
754 totalTime= stats.totalTime;
755 result.append( lcblk->dump(" ") );
756 }
757
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700758 sp<const GraphicBuffer> buf0(getBuffer(0));
759 sp<const GraphicBuffer> buf1(getBuffer(1));
760 uint32_t w0=0, h0=0, s0=0;
761 uint32_t w1=0, h1=0, s1=0;
762 if (buf0 != 0) {
763 w0 = buf0->getWidth();
764 h0 = buf0->getHeight();
765 s0 = buf0->getStride();
766 }
767 if (buf1 != 0) {
768 w1 = buf1->getWidth();
769 h1 = buf1->getHeight();
770 s1 = buf1->getStride();
771 }
772 snprintf(buffer, SIZE,
773 " "
774 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
775 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776 mFormat, w0, h0, s0, w1, h1, s1,
777 getFreezeLock().get(), totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700778
779 result.append(buffer);
780}
781
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700782// ---------------------------------------------------------------------------
783
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700784Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700785 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700786}
787
788Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700789}
790
791int32_t Layer::ClientRef::getToken() const {
792 Mutex::Autolock _l(mLock);
793 return mToken;
794}
795
Mathias Agopian579b3f82010-06-08 19:54:15 -0700796sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700797 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700798 return mUserClient.promote();
799}
800
801status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
802 const sp<SharedBufferServer>& sharedClient, int32_t token) {
803 Mutex::Autolock _l(mLock);
804
805 { // scope for strong mUserClient reference
806 sp<UserClient> userClient(mUserClient.promote());
Jamie Gennis5fd799d2011-03-18 16:35:13 -0700807 if (userClient != 0 && mControlBlock != 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700808 mControlBlock->setStatus(NO_INIT);
809 }
810 }
811
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700812 mUserClient = uc;
813 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700814 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700815 return NO_ERROR;
816}
817
818sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
819 return mUserClient.promote();
820}
821
822// this class gives us access to SharedBufferServer safely
823// it makes sure the UserClient (and its associated shared memory)
824// won't go away while we're accessing it.
825Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700826 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700827{
828 Mutex::Autolock _l(ref.mLock);
829 mUserClientStrongRef = ref.mUserClient.promote();
830 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700831 mControlBlock = ref.mControlBlock;
832}
833
834Layer::ClientRef::Access::~Access()
835{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700836}
837
838// ---------------------------------------------------------------------------
839
Mathias Agopiand606de62010-05-10 20:06:11 -0700840Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700841 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian420a2832010-12-14 20:30:37 -0800842 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700843{
844}
845
Mathias Agopianbb641242010-05-18 17:06:55 -0700846Layer::BufferManager::~BufferManager()
847{
848}
849
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700850status_t Layer::BufferManager::resize(size_t size,
851 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopianbb641242010-05-18 17:06:55 -0700852{
853 Mutex::Autolock _l(mLock);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700854
855 if (size < mNumBuffers) {
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700856 // If there is an active texture, move it into slot 0 if needed
857 if (mActiveBufferIndex > 0) {
858 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
859 mBufferData[mActiveBufferIndex] = mBufferData[0];
860 mBufferData[0] = activeBufferData;
861 mActiveBufferIndex = 0;
862 }
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700863
864 // Free the buffers that are no longer needed.
865 for (size_t i = size; i < mNumBuffers; i++) {
866 mBufferData[i].buffer = 0;
867
868 // Create a message to destroy the textures on SurfaceFlinger's GL
869 // thread.
870 class MessageDestroyTexture : public MessageBase {
871 Image mTexture;
872 EGLDisplay mDpy;
873 public:
874 MessageDestroyTexture(const Image& texture, EGLDisplay dpy)
875 : mTexture(texture), mDpy(dpy) { }
876 virtual bool handler() {
877 status_t err = Layer::BufferManager::destroyTexture(
878 &mTexture, mDpy);
879 LOGE_IF(err<0, "error destroying texture: %d (%s)",
880 mTexture.name, strerror(-err));
881 return true; // XXX: err == 0; ????
882 }
883 };
884
885 MessageDestroyTexture *msg = new MessageDestroyTexture(
886 mBufferData[i].texture, dpy);
887
888 // Don't allow this texture to be cleaned up by
889 // BufferManager::destroy.
890 mBufferData[i].texture.name = -1U;
891 mBufferData[i].texture.image = EGL_NO_IMAGE_KHR;
892
893 // Post the message to the SurfaceFlinger object.
894 flinger->postMessageAsync(msg);
895 }
896 }
897
Mathias Agopianbb641242010-05-18 17:06:55 -0700898 mNumBuffers = size;
899 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700900}
901
902// only for debugging
903sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
904 return mBufferData[index].buffer;
905}
906
907status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopian420a2832010-12-14 20:30:37 -0800908 BufferData const * const buffers = mBufferData;
909 Mutex::Autolock _l(mLock);
910 mActiveBuffer = buffers[index].buffer;
911 mActiveBufferIndex = index;
Mathias Agopiand606de62010-05-10 20:06:11 -0700912 return NO_ERROR;
913}
914
915size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800916 return mActiveBufferIndex;
Mathias Agopiand606de62010-05-10 20:06:11 -0700917}
918
919Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700920 Texture res;
Mathias Agopian420a2832010-12-14 20:30:37 -0800921 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700922 res = mFailoverTexture;
923 } else {
Mathias Agopian420a2832010-12-14 20:30:37 -0800924 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopianbb641242010-05-18 17:06:55 -0700925 }
926 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700927}
928
929sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800930 return mActiveBuffer;
Mathias Agopiand606de62010-05-10 20:06:11 -0700931}
932
Mathias Agopianda9584d2010-12-13 18:51:59 -0800933bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian420a2832010-12-14 20:30:37 -0800934 return mActiveBufferIndex >= 0;
Mathias Agopianda9584d2010-12-13 18:51:59 -0800935}
936
Mathias Agopiand606de62010-05-10 20:06:11 -0700937sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
938{
Mathias Agopianbb641242010-05-18 17:06:55 -0700939 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700940 sp<GraphicBuffer> buffer;
941 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700942 buffer = buffers[index].buffer;
943 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700944 return buffer;
945}
946
947status_t Layer::BufferManager::attachBuffer(size_t index,
948 const sp<GraphicBuffer>& buffer)
949{
Mathias Agopianbb641242010-05-18 17:06:55 -0700950 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700951 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700952 buffers[index].buffer = buffer;
953 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700954 return NO_ERROR;
955}
956
957status_t Layer::BufferManager::destroy(EGLDisplay dpy)
958{
Mathias Agopianbb641242010-05-18 17:06:55 -0700959 BufferData* const buffers = mBufferData;
960 size_t num;
961 { // scope for the lock
962 Mutex::Autolock _l(mLock);
963 num = mNumBuffers;
964 for (size_t i=0 ; i<num ; i++) {
965 buffers[i].buffer = 0;
966 }
967 }
968 for (size_t i=0 ; i<num ; i++) {
969 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700970 }
971 destroyTexture(&mFailoverTexture, dpy);
972 return NO_ERROR;
973}
974
975status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
976 const sp<GraphicBuffer>& buffer)
977{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700978 status_t err = NO_INIT;
Mathias Agopian420a2832010-12-14 20:30:37 -0800979 ssize_t index = mActiveBufferIndex;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700980 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700981 if (!mFailover) {
Kobi Cohen Arazi0d11baf2011-04-15 10:38:33 -0700982 {
983 // Without that lock, there is a chance of race condition
984 // where while composing a specific index, requestBuf
985 // with the same index can be executed and touch the same data
986 // that is being used in initEglImage.
987 // (e.g. dirty flag in texture)
988 Mutex::Autolock _l(mLock);
989 Image& texture(mBufferData[index].texture);
990 err = mTextureManager.initEglImage(&texture, dpy, buffer);
991 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700992 // if EGLImage fails, we switch to regular texture mode, and we
993 // free all resources associated with using EGLImages.
994 if (err == NO_ERROR) {
995 mFailover = false;
996 destroyTexture(&mFailoverTexture, dpy);
997 } else {
998 mFailover = true;
999 const size_t num = mNumBuffers;
1000 for (size_t i=0 ; i<num ; i++) {
1001 destroyTexture(&mBufferData[i].texture, dpy);
1002 }
Andreas Hubere049a952010-06-25 09:25:19 -07001003 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001004 } else {
1005 // we failed once, don't try again
1006 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -07001007 }
1008 }
1009 return err;
1010}
1011
1012status_t Layer::BufferManager::loadTexture(
1013 const Region& dirty, const GGLSurface& t)
1014{
1015 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1016}
1017
Mathias Agopianbb641242010-05-18 17:06:55 -07001018status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1019{
1020 if (tex->name != -1U) {
1021 glDeleteTextures(1, &tex->name);
1022 tex->name = -1U;
1023 }
1024 if (tex->image != EGL_NO_IMAGE_KHR) {
1025 eglDestroyImageKHR(dpy, tex->image);
1026 tex->image = EGL_NO_IMAGE_KHR;
1027 }
1028 return NO_ERROR;
1029}
1030
Mathias Agopiand606de62010-05-10 20:06:11 -07001031// ---------------------------------------------------------------------------
1032
Mathias Agopian9a112062009-04-17 19:36:26 -07001033Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -07001034 const sp<Layer>& owner)
1035 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -07001036{
1037}
1038
1039Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001040{
1041}
1042
Mathias Agopiana138f892010-05-21 17:24:35 -07001043sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1044 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001045{
Mathias Agopian3330b202009-10-05 17:07:12 -07001046 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001047 sp<Layer> owner(getOwner());
1048 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001049 /*
1050 * requestBuffer() 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 Agopiana138f892010-05-21 17:24:35 -07001054 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001055 }
1056 return buffer;
1057}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001059status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1060{
1061 status_t err = DEAD_OBJECT;
1062 sp<Layer> owner(getOwner());
1063 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -07001064 /*
1065 * setBufferCount() cannot be called from the main thread
1066 * as it could cause a dead-lock, since it may have to wait
1067 * on conditions updated my the main thread.
1068 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001069 err = owner->setBufferCount(bufferCount);
1070 }
1071 return err;
1072}
1073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074// ---------------------------------------------------------------------------
1075
1076
1077}; // namespace android