blob: bf1e9aabf7c1d86b5f91224947463d9bf511764f [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
Mathias Agopian6950e422009-10-05 17:07:12 -070028#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/PixelFormat.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080030
31#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070034#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070038#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40
41#define DEBUG_RESIZE 0
42
43
44namespace android {
45
Mathias Agopian967dce32010-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 Project9066cfe2009-03-03 19:31:44 -080050// ---------------------------------------------------------------------------
51
Mathias Agopian593c05c2010-06-02 23:28:45 -070052Layer::Layer(SurfaceFlinger* flinger,
53 DisplayID display, const sp<Client>& client)
54 : LayerBaseClient(flinger, display, client),
Mathias Agopian781953d2010-06-25 18:02:21 -070055 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopiancc934762009-09-23 19:16:27 -070056 mNeedsBlending(true),
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070057 mNeedsDithering(false),
Mathias Agopian7623da42010-06-01 15:12:58 -070058 mSecure(false),
Glenn Kastend6f5bde2011-01-19 15:27:27 -080059 mProtectedByApp(false),
60 mProtectedByDRM(false),
Mathias Agopian781953d2010-06-25 18:02:21 -070061 mTextureManager(),
Mathias Agopian2be352a2010-05-21 17:24:35 -070062 mBufferManager(mTextureManager),
Eric Hassoldb6c0f512011-03-11 12:24:23 -080063 mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE),
64 mNeedsScaling(false), mFixedSize(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066}
67
68Layer::~Layer()
69{
Mathias Agopian898c4c92010-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 Agopian7623da42010-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 Agopian9f2c4fd2010-05-10 20:06:11 -070080}
81
Mathias Agopian7623da42010-06-01 15:12:58 -070082status_t Layer::setToken(const sp<UserClient>& userClient,
83 SharedClient* sharedClient, int32_t token)
Mathias Agopian593c05c2010-06-02 23:28:45 -070084{
Mathias Agopian5e140102010-06-08 19:54:15 -070085 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopian7623da42010-06-01 15:12:58 -070086 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian593c05c2010-06-02 23:28:45 -070087 getIdentity());
88
Mathias Agopian5e140102010-06-08 19:54:15 -070089
Mathias Agopian5747bbc2010-12-13 16:49:05 -080090 sp<UserClient> ourClient(mUserClientRef.getClient());
91
92 /*
93 * Here it is guaranteed that userClient != ourClient
94 * (see UserClient::getTokenForSurface()).
95 *
96 * We release the token used by this surface in ourClient below.
97 * This should be safe to do so now, since this layer won't be attached
98 * to this client, it should be okay to reuse that id.
99 *
100 * If this causes problems, an other solution would be to keep a list
101 * of all the {UserClient, token} ever used and release them when the
102 * Layer is destroyed.
103 *
104 */
105
106 if (ourClient != 0) {
107 ourClient->detachLayer(this);
108 }
109
110 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian5e140102010-06-08 19:54:15 -0700111 LOGE_IF(err != NO_ERROR,
112 "ClientRef::setToken(%p, %p, %u) failed",
113 userClient.get(), lcblk.get(), token);
114
115 if (err == NO_ERROR) {
116 // we need to free the buffers associated with this surface
Mathias Agopian7623da42010-06-01 15:12:58 -0700117 }
118
119 return err;
120}
121
122int32_t Layer::getToken() const
123{
124 return mUserClientRef.getToken();
Mathias Agopian593c05c2010-06-02 23:28:45 -0700125}
126
Mathias Agopian5e140102010-06-08 19:54:15 -0700127sp<UserClient> Layer::getClient() const
128{
129 return mUserClientRef.getClient();
130}
131
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700132// called with SurfaceFlinger::mStateLock as soon as the layer is entered
133// in the purgatory list
134void Layer::onRemoved()
135{
Mathias Agopian7623da42010-06-01 15:12:58 -0700136 ClientRef::Access sharedClient(mUserClientRef);
137 SharedBufferServer* lcblk(sharedClient.get());
138 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700139 // wake up the condition
140 lcblk->setStatus(NO_INIT);
141 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700142}
Mathias Agopian9779b2212009-09-07 16:32:45 -0700143
Mathias Agopian1473f462009-04-10 14:24:30 -0700144sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145{
Mathias Agopian1b0114f2011-02-15 19:01:06 -0800146 sp<Surface> sur(new SurfaceLayer(mFlinger, const_cast<Layer *>(this)));
147 return sur;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148}
149
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700150status_t Layer::ditch()
151{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700152 // NOTE: Called from the main UI thread
153
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700154 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf9b0e822009-12-11 00:56:10 -0800155 mFreezeLock.clear();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700156
Mathias Agopian898c4c92010-05-18 17:06:55 -0700157 Mutex::Autolock _l(mLock);
158 mWidth = mHeight = 0;
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700159 return NO_ERROR;
160}
161
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700162status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 PixelFormat format, uint32_t flags)
164{
Mathias Agopiancc934762009-09-23 19:16:27 -0700165 // this surfaces pixel format
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 PixelFormatInfo info;
167 status_t err = getPixelFormatInfo(format, &info);
168 if (err) return err;
169
Mathias Agopiancc934762009-09-23 19:16:27 -0700170 // the display's pixel format
171 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian967dce32010-04-14 16:43:44 -0700172 uint32_t const maxSurfaceDims = min(
173 hw.getMaxTextureSize(), hw.getMaxViewportDims());
174
175 // never allow a surface larger than what our underlying GL implementation
176 // can handle.
177 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
178 return BAD_VALUE;
179 }
180
Mathias Agopiancc934762009-09-23 19:16:27 -0700181 PixelFormatInfo displayInfo;
182 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopian351a7072009-10-05 18:20:39 -0700183 const uint32_t hwFlags = hw.getFlags();
184
Mathias Agopian9779b2212009-09-07 16:32:45 -0700185 mFormat = format;
Mathias Agopian967dce32010-04-14 16:43:44 -0700186 mWidth = w;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700187 mHeight = h;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700188
189 mReqFormat = format;
190 mReqWidth = w;
191 mReqHeight = h;
192
Mathias Agopian6950e422009-10-05 17:07:12 -0700193 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800194 mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
195 mProtectedByDRM = (flags & ISurfaceComposer::eProtectedByDRM) ? true : false;
Romain Guyd10cd572010-10-10 13:33:22 -0700196 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0 &&
197 (flags & ISurfaceComposer::eOpaque) == 0;
Mathias Agopian967dce32010-04-14 16:43:44 -0700198
Mathias Agopiancc934762009-09-23 19:16:27 -0700199 // we use the red index
200 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
201 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
202 mNeedsDithering = layerRedsize > displayRedSize;
203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 return NO_ERROR;
205}
206
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700207void Layer::setGeometry(hwc_layer_t* hwcl)
208{
209 hwcl->compositionType = HWC_FRAMEBUFFER;
210 hwcl->hints = 0;
211 hwcl->flags = 0;
212 hwcl->transform = 0;
213 hwcl->blending = HWC_BLENDING_NONE;
214
215 // we can't do alpha-fade with the hwc HAL
216 const State& s(drawingState());
217 if (s.alpha < 0xFF) {
218 hwcl->flags = HWC_SKIP_LAYER;
219 return;
220 }
221
222 // we can only handle simple transformation
223 if (mOrientation & Transform::ROT_INVALID) {
224 hwcl->flags = HWC_SKIP_LAYER;
225 return;
226 }
227
Mathias Agopian8128ee82010-12-08 17:23:18 -0800228 Transform tr(Transform(mOrientation) * Transform(mBufferTransform));
229 hwcl->transform = tr.getOrientation();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700230
231 if (needsBlending()) {
232 hwcl->blending = mPremultipliedAlpha ?
233 HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
234 }
235
236 hwcl->displayFrame.left = mTransformedBounds.left;
237 hwcl->displayFrame.top = mTransformedBounds.top;
238 hwcl->displayFrame.right = mTransformedBounds.right;
239 hwcl->displayFrame.bottom = mTransformedBounds.bottom;
240
241 hwcl->visibleRegionScreen.rects =
242 reinterpret_cast<hwc_rect_t const *>(
243 visibleRegionScreen.getArray(
244 &hwcl->visibleRegionScreen.numRects));
245}
246
247void Layer::setPerFrameData(hwc_layer_t* hwcl) {
248 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
249 if (buffer == NULL) {
Mathias Agopian575eaf52010-12-13 18:51:59 -0800250 // this can happen if the client never drew into this layer yet,
251 // or if we ran out of memory. In that case, don't let
252 // HWC handle it.
253 hwcl->flags |= HWC_SKIP_LAYER;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700254 hwcl->handle = NULL;
255 return;
256 }
Louis Huemiller2dd198b2010-12-01 12:29:36 -0800257 hwcl->handle = buffer->handle;
Mathias Agopianac843f22010-12-08 17:40:28 -0800258
259 if (!mBufferCrop.isEmpty()) {
260 hwcl->sourceCrop.left = mBufferCrop.left;
261 hwcl->sourceCrop.top = mBufferCrop.top;
262 hwcl->sourceCrop.right = mBufferCrop.right;
263 hwcl->sourceCrop.bottom = mBufferCrop.bottom;
264 } else {
265 hwcl->sourceCrop.left = 0;
266 hwcl->sourceCrop.top = 0;
267 hwcl->sourceCrop.right = buffer->width;
268 hwcl->sourceCrop.bottom = buffer->height;
269 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700270}
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272void Layer::reloadTexture(const Region& dirty)
273{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700274 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian083a557c2009-12-10 15:52:29 -0800275 if (buffer == NULL) {
276 // this situation can happen if we ran out of memory for instance.
277 // not much we can do. continue to use whatever texture was bound
278 // to this context.
279 return;
280 }
281
Mathias Agopian781953d2010-06-25 18:02:21 -0700282 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700283 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
284 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
285 // not sure what we can do here...
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700286 goto slowpath;
Mathias Agopian1473f462009-04-10 14:24:30 -0700287 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700288 } else {
Mathias Agopian1d211f82010-03-08 11:14:20 -0800289slowpath:
Mathias Agopian1473f462009-04-10 14:24:30 -0700290 GGLSurface t;
Mathias Agopianc817b222010-08-20 15:59:53 -0700291 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
292 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
293 LOGE_IF(res, "error %d (%s) locking buffer %p",
294 res, strerror(res), buffer.get());
295 if (res == NO_ERROR) {
296 mBufferManager.loadTexture(dirty, t);
297 buffer->unlock();
298 }
299 } else {
300 // we can't do anything
Mathias Agopian1473f462009-04-10 14:24:30 -0700301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303}
304
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700305void Layer::drawForSreenShot() const
306{
Mathias Agopian1989af22010-12-02 21:32:29 -0800307 const bool currentFiltering = mNeedsFiltering;
308 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700309 LayerBase::drawForSreenShot();
Mathias Agopian1989af22010-12-02 21:32:29 -0800310 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700311}
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313void Layer::onDraw(const Region& clip) const
314{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700315 Texture tex(mBufferManager.getActiveTexture());
316 if (tex.name == -1LU) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 // the texture has not been created yet, this Layer has
Mathias Agopian2df6f512010-05-06 20:21:45 -0700318 // in fact never been drawn into. This happens frequently with
319 // SurfaceView because the WindowManager can't know when the client
320 // has drawn the first time.
321
322 // If there is nothing under us, we paint the screen in black, otherwise
323 // we just skip this update.
324
325 // figure out if there is something below us
326 Region under;
327 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
328 const size_t count = drawingLayers.size();
329 for (size_t i=0 ; i<count ; ++i) {
330 const sp<LayerBase>& layer(drawingLayers[i]);
331 if (layer.get() == static_cast<LayerBase const*>(this))
332 break;
333 under.orSelf(layer->visibleRegionScreen);
334 }
335 // if not everything below us is covered, we plug the holes!
336 Region holes(clip.subtract(under));
337 if (!holes.isEmpty()) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700338 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian2df6f512010-05-06 20:21:45 -0700339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 return;
341 }
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700342 drawWithOpenGL(clip, tex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343}
344
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800345// As documented in libhardware header, formats in the range
346// 0x100 - 0x1FF are specific to the HAL implementation, and
347// are known to have no alpha channel
348// TODO: move definition for device-specific range into
349// hardware.h, instead of using hard-coded values here.
350#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
351
352bool Layer::needsBlending(const sp<GraphicBuffer>& buffer) const
353{
354 // If buffers where set with eOpaque flag, all buffers are known to
355 // be opaque without having to check their actual format
356 if (mNeedsBlending && buffer != NULL) {
357 PixelFormat format = buffer->getPixelFormat();
358
359 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
360 return false;
361 }
362
363 PixelFormatInfo info;
364 status_t err = getPixelFormatInfo(format, &info);
365 if (!err && info.h_alpha <= info.l_alpha) {
366 return false;
367 }
368 }
369
370 // Return opacity as determined from flags and format options
371 // passed to setBuffers()
372 return mNeedsBlending;
373}
374
375bool Layer::needsBlending() const
376{
377 if (mBufferManager.hasActiveBuffer()) {
378 return needsBlending(mBufferManager.getActiveBuffer());
379 }
380
381 return mNeedsBlending;
382}
383
Mathias Agopian92377032010-05-26 22:08:52 -0700384bool Layer::needsFiltering() const
385{
386 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian1989af22010-12-02 21:32:29 -0800387 // if our buffer is not the same size than ourselves,
388 // we need filtering.
389 Mutex::Autolock _l(mLock);
390 if (mNeedsScaling)
Mathias Agopian92377032010-05-26 22:08:52 -0700391 return true;
392 }
393 return LayerBase::needsFiltering();
394}
395
Mathias Agopian59751db2010-05-07 15:58:44 -0700396
397status_t Layer::setBufferCount(int bufferCount)
398{
Mathias Agopian7623da42010-06-01 15:12:58 -0700399 ClientRef::Access sharedClient(mUserClientRef);
400 SharedBufferServer* lcblk(sharedClient.get());
401 if (!lcblk) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700402 // oops, the client is already gone
403 return DEAD_OBJECT;
404 }
405
Mathias Agopian898c4c92010-05-18 17:06:55 -0700406 // NOTE: lcblk->resize() is protected by an internal lock
407 status_t err = lcblk->resize(bufferCount);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700408 if (err == NO_ERROR) {
409 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
410 mBufferManager.resize(bufferCount, mFlinger, dpy);
411 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700412
413 return err;
414}
415
Mathias Agopian2be352a2010-05-21 17:24:35 -0700416sp<GraphicBuffer> Layer::requestBuffer(int index,
417 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
418 uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419{
Mathias Agopian6950e422009-10-05 17:07:12 -0700420 sp<GraphicBuffer> buffer;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700421
Mathias Agopian7623da42010-06-01 15:12:58 -0700422 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopian2be352a2010-05-21 17:24:35 -0700423 return buffer;
424
425 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
426 return buffer;
427
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700428 // this ensures our client doesn't go away while we're accessing
429 // the shared area.
Mathias Agopian7623da42010-06-01 15:12:58 -0700430 ClientRef::Access sharedClient(mUserClientRef);
431 SharedBufferServer* lcblk(sharedClient.get());
432 if (!lcblk) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700433 // oops, the client is already gone
434 return buffer;
435 }
436
Mathias Agopian1473f462009-04-10 14:24:30 -0700437 /*
Mathias Agopian9779b2212009-09-07 16:32:45 -0700438 * This is called from the client's Surface::dequeue(). This can happen
439 * at any time, especially while we're in the middle of using the
440 * buffer 'index' as our front buffer.
Mathias Agopian1473f462009-04-10 14:24:30 -0700441 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442
Mathias Agopian51c70e32010-07-27 20:11:35 -0700443 status_t err = NO_ERROR;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700444 uint32_t w, h, f;
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700445 { // scope for the lock
446 Mutex::Autolock _l(mLock);
Mathias Agopianc51114f2010-08-25 14:59:15 -0700447
448 // zero means default
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700449 const bool fixedSize = reqWidth && reqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700450 if (!reqFormat) reqFormat = mFormat;
451 if (!reqWidth) reqWidth = mWidth;
452 if (!reqHeight) reqHeight = mHeight;
453
454 w = reqWidth;
455 h = reqHeight;
456 f = reqFormat;
457
458 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
459 (reqFormat != mReqFormat)) {
460 mReqWidth = reqWidth;
461 mReqHeight = reqHeight;
462 mReqFormat = reqFormat;
Mathias Agopian3dfe1202010-09-21 10:52:42 -0700463 mFixedSize = fixedSize;
Mathias Agopian1989af22010-12-02 21:32:29 -0800464 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianc51114f2010-08-25 14:59:15 -0700465
Mathias Agopian2be352a2010-05-21 17:24:35 -0700466 lcblk->reallocateAllExcept(index);
467 }
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700468 }
469
Mathias Agopian51c70e32010-07-27 20:11:35 -0700470 // here we have to reallocate a new buffer because the buffer could be
471 // used as the front buffer, or by a client in our process
472 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian6950e422009-10-05 17:07:12 -0700473 const uint32_t effectiveUsage = getEffectiveUsage(usage);
Mathias Agopian51c70e32010-07-27 20:11:35 -0700474 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
475 err = buffer->initCheck();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700476
477 if (err || buffer->handle == 0) {
Mathias Agopiane869aee2010-12-03 17:33:09 -0800478 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700479 LOGE_IF(err || buffer->handle == 0,
480 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
481 this, index, w, h, strerror(-err));
482 } else {
483 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiane1b6f242009-09-29 22:39:22 -0700484 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
485 this, index, w, h, buffer->handle);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700486 }
487
488 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700489 Mutex::Autolock _l(mLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700490 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700491 }
492 return buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493}
494
Mathias Agopian6950e422009-10-05 17:07:12 -0700495uint32_t Layer::getEffectiveUsage(uint32_t usage) const
496{
497 /*
498 * buffers used for software rendering, but h/w composition
499 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
500 *
501 * buffers used for h/w rendering and h/w composition
502 * are allocated with HW_RENDER | HW_TEXTURE
503 *
504 * buffers used with h/w rendering and either NPOT or no egl_image_ext
505 * are allocated with SW_READ_RARELY | HW_RENDER
506 *
507 */
508
509 if (mSecure) {
510 // secure buffer, don't store it into the GPU
511 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
512 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
513 } else {
514 // it's allowed to modify the usage flags here, but generally
515 // the requested flags should be honored.
Mathias Agopianaca2ee82010-05-10 20:10:10 -0700516 // request EGLImage for all buffers
517 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian6950e422009-10-05 17:07:12 -0700518 }
Glenn Kastend6f5bde2011-01-19 15:27:27 -0800519 if (mProtectedByApp || mProtectedByDRM) {
520 // need a hardware-protected path to external video sink
521 usage |= GraphicBuffer::USAGE_PROTECTED;
522 }
Mathias Agopian6950e422009-10-05 17:07:12 -0700523 return usage;
524}
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526uint32_t Layer::doTransaction(uint32_t flags)
527{
528 const Layer::State& front(drawingState());
529 const Layer::State& temp(currentState());
530
Mathias Agopian2be352a2010-05-21 17:24:35 -0700531 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
532 (front.requested_h != temp.requested_h);
533
534 if (sizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700535 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700537 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
538 this,
539 int(temp.requested_w), int(temp.requested_h),
540 int(front.requested_w), int(front.requested_h));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541
Mathias Agopian2be352a2010-05-21 17:24:35 -0700542 if (!isFixedSize()) {
543 // we're being resized and there is a freeze display request,
544 // acquire a freeze lock, so that the screen stays put
545 // until we've redrawn at the new size; this is to avoid
546 // glitches upon orientation changes.
547 if (mFlinger->hasFreezeRequest()) {
548 // if the surface is hidden, don't try to acquire the
549 // freeze lock, since hidden surfaces may never redraw
550 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
551 mFreezeLock = mFlinger->getFreezeLock();
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700554
555 // this will make sure LayerBase::doTransaction doesn't update
556 // the drawing state's size
557 Layer::State& editDraw(mDrawingState);
558 editDraw.requested_w = temp.requested_w;
559 editDraw.requested_h = temp.requested_h;
560
561 // record the new size, form this point on, when the client request
562 // a buffer, it'll get the new size.
563 setBufferSize(temp.requested_w, temp.requested_h);
564
Mathias Agopian7623da42010-06-01 15:12:58 -0700565 ClientRef::Access sharedClient(mUserClientRef);
566 SharedBufferServer* lcblk(sharedClient.get());
567 if (lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700568 // all buffers need reallocation
569 lcblk->reallocateAll();
570 }
Mathias Agopian2be352a2010-05-21 17:24:35 -0700571 } else {
572 // record the new size
573 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 if (temp.sequence != front.sequence) {
578 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
579 // this surface is now hidden, so it shouldn't hold a freeze lock
580 // (it may never redraw, which is fine if it is hidden)
581 mFreezeLock.clear();
582 }
583 }
584
585 return LayerBase::doTransaction(flags);
586}
587
Mathias Agopian2be352a2010-05-21 17:24:35 -0700588void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700589 Mutex::Autolock _l(mLock);
590 mWidth = w;
591 mHeight = h;
Mathias Agopian1989af22010-12-02 21:32:29 -0800592 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593}
594
Mathias Agopian2be352a2010-05-21 17:24:35 -0700595bool Layer::isFixedSize() const {
596 Mutex::Autolock _l(mLock);
597 return mFixedSize;
598}
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600// ----------------------------------------------------------------------------
601// pageflip handling...
602// ----------------------------------------------------------------------------
603
604void Layer::lockPageFlip(bool& recomputeVisibleRegions)
605{
Mathias Agopian7623da42010-06-01 15:12:58 -0700606 ClientRef::Access sharedClient(mUserClientRef);
607 SharedBufferServer* lcblk(sharedClient.get());
608 if (!lcblk) {
Mathias Agopian593c05c2010-06-02 23:28:45 -0700609 // client died
610 recomputeVisibleRegions = true;
611 return;
612 }
613
Mathias Agopian9779b2212009-09-07 16:32:45 -0700614 ssize_t buf = lcblk->retireAndLock();
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700615 if (buf == NOT_ENOUGH_DATA) {
616 // NOTE: This is not an error, it simply means there is nothing to
617 // retire. The buffer is locked because we will use it
Mathias Agopian9779b2212009-09-07 16:32:45 -0700618 // for composition later in the loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 return;
620 }
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700621
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700622 if (buf < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700623 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700624 mPostedDirtyRegion.clear();
625 return;
626 }
627
Mathias Agopian9779b2212009-09-07 16:32:45 -0700628 // we retired a buffer, which becomes the new front buffer
Mathias Agopian575eaf52010-12-13 18:51:59 -0800629
630 const bool noActiveBuffer = !mBufferManager.hasActiveBuffer();
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800631 const bool activeBlending =
632 noActiveBuffer ? true : needsBlending(mBufferManager.getActiveBuffer());
633
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700634 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700635 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700636 mPostedDirtyRegion.clear();
637 return;
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639
Mathias Agopian575eaf52010-12-13 18:51:59 -0800640 if (noActiveBuffer) {
641 // we didn't have an active buffer, we need to recompute
642 // our visible region
643 recomputeVisibleRegions = true;
644 }
645
Mathias Agopian6950e422009-10-05 17:07:12 -0700646 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700647 if (newFrontBuffer != NULL) {
Eric Hassold2ae32bd2011-02-10 14:41:26 -0800648 if (!noActiveBuffer && activeBlending != needsBlending(newFrontBuffer)) {
649 // new buffer has different opacity than previous active buffer, need
650 // to recompute visible regions accordingly
651 recomputeVisibleRegions = true;
652 }
653
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700654 // get the dirty region
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700655 // compute the posted region
656 const Region dirty(lcblk->getDirtyRegion(buf));
657 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian1473f462009-04-10 14:24:30 -0700658
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700659 // update the layer size and release freeze-lock
660 const Layer::State& front(drawingState());
661 if (newFrontBuffer->getWidth() == front.requested_w &&
662 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopianbd23e302009-09-30 14:07:22 -0700663 {
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700664 if ((front.w != front.requested_w) ||
665 (front.h != front.requested_h))
666 {
667 // Here we pretend the transaction happened by updating the
668 // current and drawing states. Drawing state is only accessed
669 // in this thread, no need to have it locked
670 Layer::State& editDraw(mDrawingState);
671 editDraw.w = editDraw.requested_w;
672 editDraw.h = editDraw.requested_h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700673
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700674 // We also need to update the current state so that we don't
675 // end-up doing too much work during the next transaction.
676 // NOTE: We actually don't need hold the transaction lock here
677 // because State::w and State::h are only accessed from
678 // this thread
679 Layer::State& editTemp(currentState());
680 editTemp.w = editDraw.w;
681 editTemp.h = editDraw.h;
Mathias Agopianbd23e302009-09-30 14:07:22 -0700682
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700683 // recompute visible region
684 recomputeVisibleRegions = true;
685 }
686
687 // we now have the correct size, unfreeze the screen
688 mFreezeLock.clear();
Mathias Agopianbd23e302009-09-30 14:07:22 -0700689 }
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700690
691 // get the crop region
692 setBufferCrop( lcblk->getCrop(buf) );
693
694 // get the transformation
695 setBufferTransform( lcblk->getTransform(buf) );
696
Mathias Agopian9e3d6932010-03-15 18:15:20 -0700697 } else {
698 // this should not happen unless we ran out of memory while
699 // allocating the buffer. we're hoping that things will get back
700 // to normal the next time the app tries to draw into this buffer.
701 // meanwhile, pretend the screen didn't update.
702 mPostedDirtyRegion.clear();
Mathias Agopian7cf03ba2009-09-16 18:27:24 -0700703 }
704
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700705 if (lcblk->getQueuedCount()) {
706 // signal an event if we have more buffers waiting
707 mFlinger->signalEvent();
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700710 /* a buffer was posted, so we need to call reloadTexture(), which
711 * will update our internal data structures (eg: EGLImageKHR or
712 * texture names). we need to do this even if mPostedDirtyRegion is
713 * empty -- it's orthogonal to the fact that a new buffer was posted,
714 * for instance, a degenerate case could be that the user did an empty
715 * update but repainted the buffer with appropriate content (after a
716 * resize for instance).
717 */
718 reloadTexture( mPostedDirtyRegion );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719}
720
721void Layer::unlockPageFlip(
722 const Transform& planeTransform, Region& outDirtyRegion)
723{
724 Region dirtyRegion(mPostedDirtyRegion);
725 if (!dirtyRegion.isEmpty()) {
726 mPostedDirtyRegion.clear();
727 // The dirty region is given in the layer's coordinate space
728 // transform the dirty region by the surface's transformation
729 // and the global transformation.
730 const Layer::State& s(drawingState());
731 const Transform tr(planeTransform * s.transform);
732 dirtyRegion = tr.transform(dirtyRegion);
733
734 // At this point, the dirty region is in screen space.
735 // Make sure it's constrained by the visible region (which
736 // is in screen space as well).
737 dirtyRegion.andSelf(visibleRegionScreen);
738 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800740 if (visibleRegionScreen.isEmpty()) {
741 // an invisible layer should not hold a freeze-lock
Mathias Agopian9bce8732010-04-20 17:55:49 -0700742 // (because it may never be updated and therefore never release it)
Mathias Agopian5469a4a2009-11-30 11:15:41 -0800743 mFreezeLock.clear();
744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745}
746
Mathias Agopian9bce8732010-04-20 17:55:49 -0700747void Layer::dump(String8& result, char* buffer, size_t SIZE) const
748{
749 LayerBaseClient::dump(result, buffer, SIZE);
750
Mathias Agopian7623da42010-06-01 15:12:58 -0700751 ClientRef::Access sharedClient(mUserClientRef);
752 SharedBufferServer* lcblk(sharedClient.get());
753 uint32_t totalTime = 0;
754 if (lcblk) {
755 SharedBufferStack::Statistics stats = lcblk->getStats();
756 totalTime= stats.totalTime;
757 result.append( lcblk->dump(" ") );
758 }
759
Mathias Agopian9bce8732010-04-20 17:55:49 -0700760 sp<const GraphicBuffer> buf0(getBuffer(0));
761 sp<const GraphicBuffer> buf1(getBuffer(1));
762 uint32_t w0=0, h0=0, s0=0;
763 uint32_t w1=0, h1=0, s1=0;
764 if (buf0 != 0) {
765 w0 = buf0->getWidth();
766 h0 = buf0->getHeight();
767 s0 = buf0->getStride();
768 }
769 if (buf1 != 0) {
770 w1 = buf1->getWidth();
771 h1 = buf1->getHeight();
772 s1 = buf1->getStride();
773 }
774 snprintf(buffer, SIZE,
775 " "
776 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
777 " freezeLock=%p, dq-q-time=%u us\n",
Mathias Agopian7623da42010-06-01 15:12:58 -0700778 mFormat, w0, h0, s0, w1, h1, s1,
779 getFreezeLock().get(), totalTime);
Mathias Agopian9bce8732010-04-20 17:55:49 -0700780
781 result.append(buffer);
782}
783
Mathias Agopian1473f462009-04-10 14:24:30 -0700784// ---------------------------------------------------------------------------
785
Mathias Agopian7623da42010-06-01 15:12:58 -0700786Layer::ClientRef::ClientRef()
Mathias Agopian5e140102010-06-08 19:54:15 -0700787 : mControlBlock(0), mToken(-1) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700788}
789
790Layer::ClientRef::~ClientRef() {
Mathias Agopian7623da42010-06-01 15:12:58 -0700791}
792
793int32_t Layer::ClientRef::getToken() const {
794 Mutex::Autolock _l(mLock);
795 return mToken;
796}
797
Mathias Agopian5e140102010-06-08 19:54:15 -0700798sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopian7623da42010-06-01 15:12:58 -0700799 Mutex::Autolock _l(mLock);
Mathias Agopian5e140102010-06-08 19:54:15 -0700800 return mUserClient.promote();
801}
802
803status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
804 const sp<SharedBufferServer>& sharedClient, int32_t token) {
805 Mutex::Autolock _l(mLock);
806
807 { // scope for strong mUserClient reference
808 sp<UserClient> userClient(mUserClient.promote());
809 if (mUserClient != 0 && mControlBlock != 0) {
810 mControlBlock->setStatus(NO_INIT);
811 }
812 }
813
Mathias Agopian7623da42010-06-01 15:12:58 -0700814 mUserClient = uc;
815 mToken = token;
Mathias Agopian5e140102010-06-08 19:54:15 -0700816 mControlBlock = sharedClient;
Mathias Agopian7623da42010-06-01 15:12:58 -0700817 return NO_ERROR;
818}
819
820sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
821 return mUserClient.promote();
822}
823
824// this class gives us access to SharedBufferServer safely
825// it makes sure the UserClient (and its associated shared memory)
826// won't go away while we're accessing it.
827Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian5e140102010-06-08 19:54:15 -0700828 : mControlBlock(0)
Mathias Agopian7623da42010-06-01 15:12:58 -0700829{
830 Mutex::Autolock _l(ref.mLock);
831 mUserClientStrongRef = ref.mUserClient.promote();
832 if (mUserClientStrongRef != 0)
Mathias Agopian5e140102010-06-08 19:54:15 -0700833 mControlBlock = ref.mControlBlock;
834}
835
836Layer::ClientRef::Access::~Access()
837{
Mathias Agopian7623da42010-06-01 15:12:58 -0700838}
839
840// ---------------------------------------------------------------------------
841
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700842Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700843 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800844 mActiveBufferIndex(-1), mFailover(false)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700845{
846}
847
Mathias Agopian898c4c92010-05-18 17:06:55 -0700848Layer::BufferManager::~BufferManager()
849{
850}
851
Jamie Gennis6c925d02010-11-02 11:51:32 -0700852status_t Layer::BufferManager::resize(size_t size,
853 const sp<SurfaceFlinger>& flinger, EGLDisplay dpy)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700854{
855 Mutex::Autolock _l(mLock);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700856
857 if (size < mNumBuffers) {
858 // Move the active texture into slot 0
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800859 BufferData activeBufferData = mBufferData[mActiveBufferIndex];
860 mBufferData[mActiveBufferIndex] = mBufferData[0];
Jamie Gennis6c925d02010-11-02 11:51:32 -0700861 mBufferData[0] = activeBufferData;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800862 mActiveBufferIndex = 0;
Jamie Gennis6c925d02010-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 Agopian898c4c92010-05-18 17:06:55 -0700898 mNumBuffers = size;
899 return NO_ERROR;
Mathias Agopian9f2c4fd2010-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 Agopian1ebaa3a2010-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 Agopian9f2c4fd2010-05-10 20:06:11 -0700912 return NO_ERROR;
913}
914
915size_t Layer::BufferManager::getActiveBufferIndex() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800916 return mActiveBufferIndex;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700917}
918
919Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700920 Texture res;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800921 if (mFailover || mActiveBufferIndex<0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700922 res = mFailoverTexture;
923 } else {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800924 static_cast<Image&>(res) = mBufferData[mActiveBufferIndex].texture;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700925 }
926 return res;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700927}
928
929sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800930 return mActiveBuffer;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700931}
932
Mathias Agopian575eaf52010-12-13 18:51:59 -0800933bool Layer::BufferManager::hasActiveBuffer() const {
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800934 return mActiveBufferIndex >= 0;
Mathias Agopian575eaf52010-12-13 18:51:59 -0800935}
936
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700937sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
938{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700939 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700940 sp<GraphicBuffer> buffer;
941 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700942 buffer = buffers[index].buffer;
943 buffers[index].buffer = 0;
Mathias Agopian9f2c4fd2010-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 Agopian898c4c92010-05-18 17:06:55 -0700950 BufferData* const buffers = mBufferData;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700951 Mutex::Autolock _l(mLock);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700952 buffers[index].buffer = buffer;
953 buffers[index].texture.dirty = true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700954 return NO_ERROR;
955}
956
957status_t Layer::BufferManager::destroy(EGLDisplay dpy)
958{
Mathias Agopian898c4c92010-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 Agopian9f2c4fd2010-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 Agopian7623da42010-06-01 15:12:58 -0700978 status_t err = NO_INIT;
Mathias Agopian1ebaa3a2010-12-14 20:30:37 -0800979 ssize_t index = mActiveBufferIndex;
Mathias Agopian7623da42010-06-01 15:12:58 -0700980 if (index >= 0) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700981 if (!mFailover) {
982 Image& texture(mBufferData[index].texture);
983 err = mTextureManager.initEglImage(&texture, dpy, buffer);
984 // if EGLImage fails, we switch to regular texture mode, and we
985 // free all resources associated with using EGLImages.
986 if (err == NO_ERROR) {
987 mFailover = false;
988 destroyTexture(&mFailoverTexture, dpy);
989 } else {
990 mFailover = true;
991 const size_t num = mNumBuffers;
992 for (size_t i=0 ; i<num ; i++) {
993 destroyTexture(&mBufferData[i].texture, dpy);
994 }
Andreas Huber330dd302010-06-25 09:25:19 -0700995 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700996 } else {
997 // we failed once, don't try again
998 err = BAD_VALUE;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700999 }
1000 }
1001 return err;
1002}
1003
1004status_t Layer::BufferManager::loadTexture(
1005 const Region& dirty, const GGLSurface& t)
1006{
1007 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
1008}
1009
Mathias Agopian898c4c92010-05-18 17:06:55 -07001010status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
1011{
1012 if (tex->name != -1U) {
1013 glDeleteTextures(1, &tex->name);
1014 tex->name = -1U;
1015 }
1016 if (tex->image != EGL_NO_IMAGE_KHR) {
1017 eglDestroyImageKHR(dpy, tex->image);
1018 tex->image = EGL_NO_IMAGE_KHR;
1019 }
1020 return NO_ERROR;
1021}
1022
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001023// ---------------------------------------------------------------------------
1024
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001025Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001026 const sp<Layer>& owner)
1027 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001028{
1029}
1030
1031Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian1473f462009-04-10 14:24:30 -07001032{
1033}
1034
Mathias Agopian2be352a2010-05-21 17:24:35 -07001035sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
1036 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -07001037{
Mathias Agopian6950e422009-10-05 17:07:12 -07001038 sp<GraphicBuffer> buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -07001039 sp<Layer> owner(getOwner());
1040 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001041 /*
1042 * requestBuffer() cannot be called from the main thread
1043 * as it could cause a dead-lock, since it may have to wait
1044 * on conditions updated my the main thread.
1045 */
Mathias Agopian2be352a2010-05-21 17:24:35 -07001046 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian1473f462009-04-10 14:24:30 -07001047 }
1048 return buffer;
1049}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050
Mathias Agopian59751db2010-05-07 15:58:44 -07001051status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
1052{
1053 status_t err = DEAD_OBJECT;
1054 sp<Layer> owner(getOwner());
1055 if (owner != 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -07001056 /*
1057 * setBufferCount() cannot be called from the main thread
1058 * as it could cause a dead-lock, since it may have to wait
1059 * on conditions updated my the main thread.
1060 */
Mathias Agopian59751db2010-05-07 15:58:44 -07001061 err = owner->setBufferCount(bufferCount);
1062 }
1063 return err;
1064}
1065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066// ---------------------------------------------------------------------------
1067
1068
1069}; // namespace android