blob: dfee803bdc46304df21297de7221df0beb7da3ee [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"
38
39
40#define DEBUG_RESIZE 0
41
42
43namespace android {
44
Mathias Agopianca99fb82010-04-14 16:43:44 -070045template <typename T> inline T min(T a, T b) {
46 return a<b ? a : b;
47}
48
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049// ---------------------------------------------------------------------------
50
Mathias Agopian96f08192010-06-02 23:28:45 -070051Layer::Layer(SurfaceFlinger* flinger,
52 DisplayID display, const sp<Client>& client)
53 : LayerBaseClient(flinger, display, client),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054 mGLExtensions(GLExtensions::getInstance()),
Mathias Agopian401c2572009-09-23 19:16:27 -070055 mNeedsBlending(true),
Mathias Agopiand606de62010-05-10 20:06:11 -070056 mNeedsDithering(false),
Mathias Agopianb7e930d2010-06-01 15:12:58 -070057 mSecure(false),
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058 mTextureManager(),
Mathias Agopiana138f892010-05-21 17:24:35 -070059 mBufferManager(mTextureManager),
Mathias Agopian22c67842010-11-01 23:32:18 -070060 mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false),
61 mBypassState(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063}
64
65Layer::~Layer()
66{
Mathias Agopianbb641242010-05-18 17:06:55 -070067 // FIXME: must be called from the main UI thread
68 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
69 mBufferManager.destroy(dpy);
70
Mathias Agopianb7e930d2010-06-01 15:12:58 -070071 // we can use getUserClientUnsafe here because we know we're
72 // single-threaded at that point.
73 sp<UserClient> ourClient(mUserClientRef.getUserClientUnsafe());
74 if (ourClient != 0) {
75 ourClient->detachLayer(this);
76 }
Mathias Agopiand606de62010-05-10 20:06:11 -070077}
78
Mathias Agopianb7e930d2010-06-01 15:12:58 -070079status_t Layer::setToken(const sp<UserClient>& userClient,
80 SharedClient* sharedClient, int32_t token)
Mathias Agopian96f08192010-06-02 23:28:45 -070081{
Mathias Agopian579b3f82010-06-08 19:54:15 -070082 sp<SharedBufferServer> lcblk = new SharedBufferServer(
Mathias Agopianb7e930d2010-06-01 15:12:58 -070083 sharedClient, token, mBufferManager.getDefaultBufferCount(),
Mathias Agopian96f08192010-06-02 23:28:45 -070084 getIdentity());
85
Mathias Agopianb7e930d2010-06-01 15:12:58 -070086 status_t err = mUserClientRef.setToken(userClient, lcblk, token);
Mathias Agopian579b3f82010-06-08 19:54:15 -070087
88 LOGE_IF(err != NO_ERROR,
89 "ClientRef::setToken(%p, %p, %u) failed",
90 userClient.get(), lcblk.get(), token);
91
92 if (err == NO_ERROR) {
93 // we need to free the buffers associated with this surface
Mathias Agopianb7e930d2010-06-01 15:12:58 -070094 }
95
96 return err;
97}
98
99int32_t Layer::getToken() const
100{
101 return mUserClientRef.getToken();
Mathias Agopian96f08192010-06-02 23:28:45 -0700102}
103
Mathias Agopian579b3f82010-06-08 19:54:15 -0700104sp<UserClient> Layer::getClient() const
105{
106 return mUserClientRef.getClient();
107}
108
Mathias Agopiand606de62010-05-10 20:06:11 -0700109// called with SurfaceFlinger::mStateLock as soon as the layer is entered
110// in the purgatory list
111void Layer::onRemoved()
112{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700113 ClientRef::Access sharedClient(mUserClientRef);
114 SharedBufferServer* lcblk(sharedClient.get());
115 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700116 // wake up the condition
117 lcblk->setStatus(NO_INIT);
118 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700119}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700120
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
123 return mSurface;
124}
125
Mathias Agopian9a112062009-04-17 19:36:26 -0700126status_t Layer::ditch()
127{
Mathias Agopianbb641242010-05-18 17:06:55 -0700128 // NOTE: Called from the main UI thread
129
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700130 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopianf5430db2009-12-11 00:56:10 -0800131 mFreezeLock.clear();
Mathias Agopianbb641242010-05-18 17:06:55 -0700132
133 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
134 mBufferManager.destroy(dpy);
135 mSurface.clear();
136
137 Mutex::Autolock _l(mLock);
138 mWidth = mHeight = 0;
Mathias Agopian9a112062009-04-17 19:36:26 -0700139 return NO_ERROR;
140}
141
Mathias Agopianf9d93272009-06-19 17:00:27 -0700142status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 PixelFormat format, uint32_t flags)
144{
Mathias Agopian401c2572009-09-23 19:16:27 -0700145 // this surfaces pixel format
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 PixelFormatInfo info;
147 status_t err = getPixelFormatInfo(format, &info);
148 if (err) return err;
149
Mathias Agopian401c2572009-09-23 19:16:27 -0700150 // the display's pixel format
151 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopianca99fb82010-04-14 16:43:44 -0700152 uint32_t const maxSurfaceDims = min(
153 hw.getMaxTextureSize(), hw.getMaxViewportDims());
154
155 // never allow a surface larger than what our underlying GL implementation
156 // can handle.
157 if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
158 return BAD_VALUE;
159 }
160
Mathias Agopian401c2572009-09-23 19:16:27 -0700161 PixelFormatInfo displayInfo;
162 getPixelFormatInfo(hw.getFormat(), &displayInfo);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700163 const uint32_t hwFlags = hw.getFlags();
164
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700165 mFormat = format;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700166 mWidth = w;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700167 mHeight = h;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700168
169 mReqFormat = format;
170 mReqWidth = w;
171 mReqHeight = h;
172
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopianca99fb82010-04-14 16:43:44 -0700175
Mathias Agopian401c2572009-09-23 19:16:27 -0700176 // we use the red index
177 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
178 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
179 mNeedsDithering = layerRedsize > displayRedSize;
180
Mathias Agopian96f08192010-06-02 23:28:45 -0700181 mSurface = new SurfaceLayer(mFlinger, this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 return NO_ERROR;
183}
184
185void Layer::reloadTexture(const Region& dirty)
186{
Mathias Agopiand606de62010-05-10 20:06:11 -0700187 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
Mathias Agopian8f03b472009-12-10 15:52:29 -0800188 if (buffer == NULL) {
189 // this situation can happen if we ran out of memory for instance.
190 // not much we can do. continue to use whatever texture was bound
191 // to this context.
192 return;
193 }
194
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700195 if (mGLExtensions.haveDirectTexture()) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700196 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
197 if (mBufferManager.initEglImage(dpy, buffer) != NO_ERROR) {
198 // not sure what we can do here...
Mathias Agopiand606de62010-05-10 20:06:11 -0700199 goto slowpath;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700201 } else {
Mathias Agopianfcfeb4b2010-03-08 11:14:20 -0800202slowpath:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203 GGLSurface t;
Mathias Agopianf1b38242010-08-20 15:59:53 -0700204 if (buffer->usage & GRALLOC_USAGE_SW_READ_MASK) {
205 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN);
206 LOGE_IF(res, "error %d (%s) locking buffer %p",
207 res, strerror(res), buffer.get());
208 if (res == NO_ERROR) {
209 mBufferManager.loadTexture(dirty, t);
210 buffer->unlock();
211 }
212 } else {
213 // we can't do anything
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
Mathias Agopiandf85c452010-09-29 13:02:36 -0700218void Layer::drawForSreenShot() const
219{
Mathias Agopian733189d2010-12-02 21:32:29 -0800220 const bool currentFiltering = mNeedsFiltering;
221 const_cast<Layer*>(this)->mNeedsFiltering = true;
Mathias Agopiandf85c452010-09-29 13:02:36 -0700222 LayerBase::drawForSreenShot();
Mathias Agopian733189d2010-12-02 21:32:29 -0800223 const_cast<Layer*>(this)->mNeedsFiltering = currentFiltering;
Mathias Agopiandf85c452010-09-29 13:02:36 -0700224}
225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226void Layer::onDraw(const Region& clip) const
227{
Mathias Agopiand606de62010-05-10 20:06:11 -0700228 Texture tex(mBufferManager.getActiveTexture());
229 if (tex.name == -1LU) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 // the texture has not been created yet, this Layer has
Mathias Agopian179169e2010-05-06 20:21:45 -0700231 // in fact never been drawn into. This happens frequently with
232 // SurfaceView because the WindowManager can't know when the client
233 // has drawn the first time.
234
235 // If there is nothing under us, we paint the screen in black, otherwise
236 // we just skip this update.
237
238 // figure out if there is something below us
239 Region under;
240 const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
241 const size_t count = drawingLayers.size();
242 for (size_t i=0 ; i<count ; ++i) {
243 const sp<LayerBase>& layer(drawingLayers[i]);
244 if (layer.get() == static_cast<LayerBase const*>(this))
245 break;
246 under.orSelf(layer->visibleRegionScreen);
247 }
248 // if not everything below us is covered, we plug the holes!
249 Region holes(clip.subtract(under));
250 if (!holes.isEmpty()) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700251 clearWithOpenGL(holes, 0, 0, 0, 1);
Mathias Agopian179169e2010-05-06 20:21:45 -0700252 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 return;
254 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700255
256#ifdef USE_COMPOSITION_BYPASS
257 sp<GraphicBuffer> buffer(mBufferManager.getActiveBuffer());
258 if ((buffer != NULL) && (buffer->transform)) {
259 // Here we have a "bypass" buffer, but we need to composite it
260 // most likely because it's not fullscreen anymore.
261 // Since the buffer may have a transformation applied by the client
262 // we need to inverse this transformation here.
263
264 // calculate the inverse of the buffer transform
265 const uint32_t mask = HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
266 const uint32_t bufferTransformInverse = buffer->transform ^ mask;
267
268 // To accomplish the inverse transform, we use "mBufferTransform"
269 // which is not used by Layer.cpp
270 const_cast<Layer*>(this)->mBufferTransform = bufferTransformInverse;
271 drawWithOpenGL(clip, tex);
272 // reset to "no transfrom"
273 const_cast<Layer*>(this)->mBufferTransform = 0;
274 return;
275 }
276#endif
277
Mathias Agopiand606de62010-05-10 20:06:11 -0700278 drawWithOpenGL(clip, tex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279}
280
Mathias Agopiana7f66922010-05-26 22:08:52 -0700281bool Layer::needsFiltering() const
282{
283 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
Mathias Agopian733189d2010-12-02 21:32:29 -0800284 // if our buffer is not the same size than ourselves,
285 // we need filtering.
286 Mutex::Autolock _l(mLock);
287 if (mNeedsScaling)
Mathias Agopiana7f66922010-05-26 22:08:52 -0700288 return true;
289 }
290 return LayerBase::needsFiltering();
291}
292
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700293
294status_t Layer::setBufferCount(int bufferCount)
295{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700296 ClientRef::Access sharedClient(mUserClientRef);
297 SharedBufferServer* lcblk(sharedClient.get());
298 if (!lcblk) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700299 // oops, the client is already gone
300 return DEAD_OBJECT;
301 }
302
Mathias Agopianbb641242010-05-18 17:06:55 -0700303 // NOTE: lcblk->resize() is protected by an internal lock
304 status_t err = lcblk->resize(bufferCount);
305 if (err == NO_ERROR)
306 mBufferManager.resize(bufferCount);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700307
308 return err;
309}
310
Mathias Agopiana138f892010-05-21 17:24:35 -0700311sp<GraphicBuffer> Layer::requestBuffer(int index,
312 uint32_t reqWidth, uint32_t reqHeight, uint32_t reqFormat,
313 uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314{
Mathias Agopian3330b202009-10-05 17:07:12 -0700315 sp<GraphicBuffer> buffer;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700316
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700317 if (int32_t(reqWidth | reqHeight | reqFormat) < 0)
Mathias Agopiana138f892010-05-21 17:24:35 -0700318 return buffer;
319
320 if ((!reqWidth && reqHeight) || (reqWidth && !reqHeight))
321 return buffer;
322
Mathias Agopian48d819a2009-09-10 19:41:18 -0700323 // this ensures our client doesn't go away while we're accessing
324 // the shared area.
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700325 ClientRef::Access sharedClient(mUserClientRef);
326 SharedBufferServer* lcblk(sharedClient.get());
327 if (!lcblk) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700328 // oops, the client is already gone
329 return buffer;
330 }
331
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700333 * This is called from the client's Surface::dequeue(). This can happen
334 * at any time, especially while we're in the middle of using the
335 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700336 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337
Mathias Agopian22c67842010-11-01 23:32:18 -0700338 uint32_t w, h, f, bypass;
Mathias Agopian48d819a2009-09-10 19:41:18 -0700339 { // scope for the lock
340 Mutex::Autolock _l(mLock);
Mathias Agopianeff062c2010-08-25 14:59:15 -0700341
Mathias Agopian22c67842010-11-01 23:32:18 -0700342 bypass = mBypassState;
343
Mathias Agopianeff062c2010-08-25 14:59:15 -0700344 // zero means default
Mathias Agopian733189d2010-12-02 21:32:29 -0800345 mFixedSize = reqWidth && reqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700346 if (!reqFormat) reqFormat = mFormat;
347 if (!reqWidth) reqWidth = mWidth;
348 if (!reqHeight) reqHeight = mHeight;
349
350 w = reqWidth;
351 h = reqHeight;
352 f = reqFormat;
353
354 if ((reqWidth != mReqWidth) || (reqHeight != mReqHeight) ||
355 (reqFormat != mReqFormat)) {
356 mReqWidth = reqWidth;
357 mReqHeight = reqHeight;
358 mReqFormat = reqFormat;
Mathias Agopian733189d2010-12-02 21:32:29 -0800359 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
Mathias Agopianeff062c2010-08-25 14:59:15 -0700360
Mathias Agopiana138f892010-05-21 17:24:35 -0700361 lcblk->reallocateAllExcept(index);
362 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700363 }
364
Mathias Agopian208cb072010-07-27 20:11:35 -0700365 // here we have to reallocate a new buffer because the buffer could be
366 // used as the front buffer, or by a client in our process
367 // (eg: status bar), and we can't release the handle under its feet.
Mathias Agopian22c67842010-11-01 23:32:18 -0700368 uint32_t effectiveUsage = getEffectiveUsage(usage);
369
370 status_t err = NO_MEMORY;
371
372#ifdef USE_COMPOSITION_BYPASS
373 if (!mSecure && bypass && (effectiveUsage & GRALLOC_USAGE_HW_RENDER)) {
374 // always allocate a buffer matching the screen size. the size
375 // may be different from (w,h) if the buffer is rotated.
376 const DisplayHardware& hw(graphicPlane(0).displayHardware());
377 int32_t w = hw.getWidth();
378 int32_t h = hw.getHeight();
379 int32_t f = hw.getFormat();
380
381 buffer = new GraphicBuffer(w, h, f, effectiveUsage | GRALLOC_USAGE_HW_FB);
382 err = buffer->initCheck();
383 buffer->transform = uint8_t(getOrientation());
384
385 if (err != NO_ERROR) {
386 // allocation didn't succeed, probably because an older bypass
387 // window hasn't released all its resources yet.
388 ClientRef::Access sharedClient(mUserClientRef);
389 SharedBufferServer* lcblk(sharedClient.get());
390 if (lcblk) {
391 // all buffers need reallocation
392 lcblk->reallocateAll();
393 }
394 }
395 }
396#endif
397
398 if (err != NO_ERROR) {
399 buffer = new GraphicBuffer(w, h, f, effectiveUsage);
400 err = buffer->initCheck();
401 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700402
403 if (err || buffer->handle == 0) {
Mathias Agopian678bdd62010-12-03 17:33:09 -0800404 GraphicBuffer::dumpAllocationsToSystemLog();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700405 LOGE_IF(err || buffer->handle == 0,
406 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
407 this, index, w, h, strerror(-err));
408 } else {
409 LOGD_IF(DEBUG_RESIZE,
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700410 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p",
411 this, index, w, h, buffer->handle);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700412 }
413
414 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700415 Mutex::Autolock _l(mLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700416 mBufferManager.attachBuffer(index, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700417 }
418 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419}
420
Mathias Agopian3330b202009-10-05 17:07:12 -0700421uint32_t Layer::getEffectiveUsage(uint32_t usage) const
422{
423 /*
424 * buffers used for software rendering, but h/w composition
425 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
426 *
427 * buffers used for h/w rendering and h/w composition
428 * are allocated with HW_RENDER | HW_TEXTURE
429 *
430 * buffers used with h/w rendering and either NPOT or no egl_image_ext
431 * are allocated with SW_READ_RARELY | HW_RENDER
432 *
433 */
434
435 if (mSecure) {
436 // secure buffer, don't store it into the GPU
437 usage = GraphicBuffer::USAGE_SW_READ_OFTEN |
438 GraphicBuffer::USAGE_SW_WRITE_OFTEN;
439 } else {
440 // it's allowed to modify the usage flags here, but generally
441 // the requested flags should be honored.
Mathias Agopian89141f92010-05-10 20:10:10 -0700442 // request EGLImage for all buffers
443 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopian3330b202009-10-05 17:07:12 -0700444 }
445 return usage;
446}
447
Mathias Agopian22c67842010-11-01 23:32:18 -0700448bool Layer::setBypass(bool enable)
449{
450 Mutex::Autolock _l(mLock);
451
452 if (mNeedsScaling || mNeedsFiltering) {
453 return false;
454 }
455
456 if (mBypassState != enable) {
457 mBypassState = enable;
458 ClientRef::Access sharedClient(mUserClientRef);
459 SharedBufferServer* lcblk(sharedClient.get());
460 if (lcblk) {
461 // all buffers need reallocation
462 lcblk->reallocateAll();
463 }
464 }
465
466 return true;
467}
468
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800469void Layer::updateBuffersOrientation()
470{
471 sp<GraphicBuffer> buffer(getBypassBuffer());
472 if (buffer != NULL && mOrientation != buffer->transform) {
473 ClientRef::Access sharedClient(mUserClientRef);
474 SharedBufferServer* lcblk(sharedClient.get());
475 if (lcblk) { // all buffers need reallocation
476 lcblk->reallocateAll();
477 }
478 }
479}
480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481uint32_t Layer::doTransaction(uint32_t flags)
482{
483 const Layer::State& front(drawingState());
484 const Layer::State& temp(currentState());
485
Mathias Agopiana138f892010-05-21 17:24:35 -0700486 const bool sizeChanged = (front.requested_w != temp.requested_w) ||
487 (front.requested_h != temp.requested_h);
488
489 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700490 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiana138f892010-05-21 17:24:35 -0700492 "resize (layer=%p), requested (%dx%d), drawing (%d,%d)",
493 this,
494 int(temp.requested_w), int(temp.requested_h),
495 int(front.requested_w), int(front.requested_h));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496
Mathias Agopiana138f892010-05-21 17:24:35 -0700497 if (!isFixedSize()) {
498 // we're being resized and there is a freeze display request,
499 // acquire a freeze lock, so that the screen stays put
500 // until we've redrawn at the new size; this is to avoid
501 // glitches upon orientation changes.
502 if (mFlinger->hasFreezeRequest()) {
503 // if the surface is hidden, don't try to acquire the
504 // freeze lock, since hidden surfaces may never redraw
505 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
506 mFreezeLock = mFlinger->getFreezeLock();
507 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700509
510 // this will make sure LayerBase::doTransaction doesn't update
511 // the drawing state's size
512 Layer::State& editDraw(mDrawingState);
513 editDraw.requested_w = temp.requested_w;
514 editDraw.requested_h = temp.requested_h;
515
516 // record the new size, form this point on, when the client request
517 // a buffer, it'll get the new size.
518 setBufferSize(temp.requested_w, temp.requested_h);
519
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700520 ClientRef::Access sharedClient(mUserClientRef);
521 SharedBufferServer* lcblk(sharedClient.get());
522 if (lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700523 // all buffers need reallocation
524 lcblk->reallocateAll();
525 }
Mathias Agopiana138f892010-05-21 17:24:35 -0700526 } else {
527 // record the new size
528 setBufferSize(temp.requested_w, temp.requested_h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 }
530 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700531
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 if (temp.sequence != front.sequence) {
533 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
534 // this surface is now hidden, so it shouldn't hold a freeze lock
535 // (it may never redraw, which is fine if it is hidden)
536 mFreezeLock.clear();
537 }
538 }
539
540 return LayerBase::doTransaction(flags);
541}
542
Mathias Agopiana138f892010-05-21 17:24:35 -0700543void Layer::setBufferSize(uint32_t w, uint32_t h) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700544 Mutex::Autolock _l(mLock);
545 mWidth = w;
546 mHeight = h;
Mathias Agopian733189d2010-12-02 21:32:29 -0800547 mNeedsScaling = mWidth != mReqWidth || mHeight != mReqHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548}
549
Mathias Agopiana138f892010-05-21 17:24:35 -0700550bool Layer::isFixedSize() const {
551 Mutex::Autolock _l(mLock);
552 return mFixedSize;
553}
554
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555// ----------------------------------------------------------------------------
556// pageflip handling...
557// ----------------------------------------------------------------------------
558
559void Layer::lockPageFlip(bool& recomputeVisibleRegions)
560{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700561 ClientRef::Access sharedClient(mUserClientRef);
562 SharedBufferServer* lcblk(sharedClient.get());
563 if (!lcblk) {
Mathias Agopian96f08192010-06-02 23:28:45 -0700564 // client died
565 recomputeVisibleRegions = true;
566 return;
567 }
568
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700569 ssize_t buf = lcblk->retireAndLock();
Mathias Agopiand606de62010-05-10 20:06:11 -0700570 if (buf == NOT_ENOUGH_DATA) {
571 // NOTE: This is not an error, it simply means there is nothing to
572 // retire. The buffer is locked because we will use it
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700573 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574 return;
575 }
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700576
Mathias Agopiand606de62010-05-10 20:06:11 -0700577 if (buf < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700578 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700579 mPostedDirtyRegion.clear();
580 return;
581 }
582
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700583 // we retired a buffer, which becomes the new front buffer
Mathias Agopiand606de62010-05-10 20:06:11 -0700584 if (mBufferManager.setActiveBufferIndex(buf) < NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700585 LOGE("retireAndLock() buffer index (%d) out of range", int(buf));
Mathias Agopiand606de62010-05-10 20:06:11 -0700586 mPostedDirtyRegion.clear();
587 return;
588 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589
Mathias Agopian3330b202009-10-05 17:07:12 -0700590 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf));
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700591 if (newFrontBuffer != NULL) {
Mathias Agopianb661d662010-08-19 17:01:19 -0700592 // get the dirty region
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700593 // compute the posted region
594 const Region dirty(lcblk->getDirtyRegion(buf));
595 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700596
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700597 // update the layer size and release freeze-lock
598 const Layer::State& front(drawingState());
599 if (newFrontBuffer->getWidth() == front.requested_w &&
600 newFrontBuffer->getHeight() == front.requested_h)
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700601 {
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700602 if ((front.w != front.requested_w) ||
603 (front.h != front.requested_h))
604 {
605 // Here we pretend the transaction happened by updating the
606 // current and drawing states. Drawing state is only accessed
607 // in this thread, no need to have it locked
608 Layer::State& editDraw(mDrawingState);
609 editDraw.w = editDraw.requested_w;
610 editDraw.h = editDraw.requested_h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700611
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700612 // We also need to update the current state so that we don't
613 // end-up doing too much work during the next transaction.
614 // NOTE: We actually don't need hold the transaction lock here
615 // because State::w and State::h are only accessed from
616 // this thread
617 Layer::State& editTemp(currentState());
618 editTemp.w = editDraw.w;
619 editTemp.h = editDraw.h;
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700620
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700621 // recompute visible region
622 recomputeVisibleRegions = true;
623 }
624
625 // we now have the correct size, unfreeze the screen
626 mFreezeLock.clear();
Mathias Agopiandf3e0b92009-09-30 14:07:22 -0700627 }
Mathias Agopianb661d662010-08-19 17:01:19 -0700628
629 // get the crop region
630 setBufferCrop( lcblk->getCrop(buf) );
631
632 // get the transformation
633 setBufferTransform( lcblk->getTransform(buf) );
634
Mathias Agopiand343e3d2010-03-15 18:15:20 -0700635 } else {
636 // this should not happen unless we ran out of memory while
637 // allocating the buffer. we're hoping that things will get back
638 // to normal the next time the app tries to draw into this buffer.
639 // meanwhile, pretend the screen didn't update.
640 mPostedDirtyRegion.clear();
Mathias Agopiancaa600c2009-09-16 18:27:24 -0700641 }
642
Mathias Agopiane7005012009-10-07 16:44:10 -0700643 if (lcblk->getQueuedCount()) {
644 // signal an event if we have more buffers waiting
645 mFlinger->signalEvent();
646 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647
Mathias Agopian245e4d72010-04-21 15:24:11 -0700648 /* a buffer was posted, so we need to call reloadTexture(), which
649 * will update our internal data structures (eg: EGLImageKHR or
650 * texture names). we need to do this even if mPostedDirtyRegion is
651 * empty -- it's orthogonal to the fact that a new buffer was posted,
652 * for instance, a degenerate case could be that the user did an empty
653 * update but repainted the buffer with appropriate content (after a
654 * resize for instance).
655 */
656 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657}
658
659void Layer::unlockPageFlip(
660 const Transform& planeTransform, Region& outDirtyRegion)
661{
662 Region dirtyRegion(mPostedDirtyRegion);
663 if (!dirtyRegion.isEmpty()) {
664 mPostedDirtyRegion.clear();
665 // The dirty region is given in the layer's coordinate space
666 // transform the dirty region by the surface's transformation
667 // and the global transformation.
668 const Layer::State& s(drawingState());
669 const Transform tr(planeTransform * s.transform);
670 dirtyRegion = tr.transform(dirtyRegion);
671
672 // At this point, the dirty region is in screen space.
673 // Make sure it's constrained by the visible region (which
674 // is in screen space as well).
675 dirtyRegion.andSelf(visibleRegionScreen);
676 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 }
Mathias Agopianc61de172009-11-30 11:15:41 -0800678 if (visibleRegionScreen.isEmpty()) {
679 // an invisible layer should not hold a freeze-lock
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700680 // (because it may never be updated and therefore never release it)
Mathias Agopianc61de172009-11-30 11:15:41 -0800681 mFreezeLock.clear();
682 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683}
684
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700685void Layer::dump(String8& result, char* buffer, size_t SIZE) const
686{
687 LayerBaseClient::dump(result, buffer, SIZE);
688
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700689 ClientRef::Access sharedClient(mUserClientRef);
690 SharedBufferServer* lcblk(sharedClient.get());
691 uint32_t totalTime = 0;
692 if (lcblk) {
693 SharedBufferStack::Statistics stats = lcblk->getStats();
694 totalTime= stats.totalTime;
695 result.append( lcblk->dump(" ") );
696 }
697
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700698 sp<const GraphicBuffer> buf0(getBuffer(0));
699 sp<const GraphicBuffer> buf1(getBuffer(1));
700 uint32_t w0=0, h0=0, s0=0;
701 uint32_t w1=0, h1=0, s1=0;
702 if (buf0 != 0) {
703 w0 = buf0->getWidth();
704 h0 = buf0->getHeight();
705 s0 = buf0->getStride();
706 }
707 if (buf1 != 0) {
708 w1 = buf1->getWidth();
709 h1 = buf1->getHeight();
710 s1 = buf1->getStride();
711 }
712 snprintf(buffer, SIZE,
713 " "
714 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian22c67842010-11-01 23:32:18 -0700715 " freezeLock=%p, bypass=%d, dq-q-time=%u us\n",
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700716 mFormat, w0, h0, s0, w1, h1, s1,
Mathias Agopian22c67842010-11-01 23:32:18 -0700717 getFreezeLock().get(), mBypassState, totalTime);
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700718
719 result.append(buffer);
720}
721
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700722// ---------------------------------------------------------------------------
723
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700724Layer::ClientRef::ClientRef()
Mathias Agopian579b3f82010-06-08 19:54:15 -0700725 : mControlBlock(0), mToken(-1) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700726}
727
728Layer::ClientRef::~ClientRef() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700729}
730
731int32_t Layer::ClientRef::getToken() const {
732 Mutex::Autolock _l(mLock);
733 return mToken;
734}
735
Mathias Agopian579b3f82010-06-08 19:54:15 -0700736sp<UserClient> Layer::ClientRef::getClient() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700737 Mutex::Autolock _l(mLock);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700738 return mUserClient.promote();
739}
740
741status_t Layer::ClientRef::setToken(const sp<UserClient>& uc,
742 const sp<SharedBufferServer>& sharedClient, int32_t token) {
743 Mutex::Autolock _l(mLock);
744
745 { // scope for strong mUserClient reference
746 sp<UserClient> userClient(mUserClient.promote());
747 if (mUserClient != 0 && mControlBlock != 0) {
748 mControlBlock->setStatus(NO_INIT);
749 }
750 }
751
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700752 mUserClient = uc;
753 mToken = token;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700754 mControlBlock = sharedClient;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700755 return NO_ERROR;
756}
757
758sp<UserClient> Layer::ClientRef::getUserClientUnsafe() const {
759 return mUserClient.promote();
760}
761
762// this class gives us access to SharedBufferServer safely
763// it makes sure the UserClient (and its associated shared memory)
764// won't go away while we're accessing it.
765Layer::ClientRef::Access::Access(const ClientRef& ref)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700766 : mControlBlock(0)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700767{
768 Mutex::Autolock _l(ref.mLock);
769 mUserClientStrongRef = ref.mUserClient.promote();
770 if (mUserClientStrongRef != 0)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700771 mControlBlock = ref.mControlBlock;
772}
773
774Layer::ClientRef::Access::~Access()
775{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776}
777
778// ---------------------------------------------------------------------------
779
Mathias Agopiand606de62010-05-10 20:06:11 -0700780Layer::BufferManager::BufferManager(TextureManager& tm)
Mathias Agopianbb641242010-05-18 17:06:55 -0700781 : mNumBuffers(NUM_BUFFERS), mTextureManager(tm),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700782 mActiveBuffer(-1), mFailover(false)
Mathias Agopiand606de62010-05-10 20:06:11 -0700783{
784}
785
Mathias Agopianbb641242010-05-18 17:06:55 -0700786Layer::BufferManager::~BufferManager()
787{
788}
789
790status_t Layer::BufferManager::resize(size_t size)
791{
792 Mutex::Autolock _l(mLock);
793 mNumBuffers = size;
794 return NO_ERROR;
Mathias Agopiand606de62010-05-10 20:06:11 -0700795}
796
797// only for debugging
798sp<GraphicBuffer> Layer::BufferManager::getBuffer(size_t index) const {
799 return mBufferData[index].buffer;
800}
801
802status_t Layer::BufferManager::setActiveBufferIndex(size_t index) {
Mathias Agopiand606de62010-05-10 20:06:11 -0700803 mActiveBuffer = index;
804 return NO_ERROR;
805}
806
807size_t Layer::BufferManager::getActiveBufferIndex() const {
808 return mActiveBuffer;
809}
810
811Texture Layer::BufferManager::getActiveTexture() const {
Mathias Agopianbb641242010-05-18 17:06:55 -0700812 Texture res;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700813 if (mFailover || mActiveBuffer<0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700814 res = mFailoverTexture;
815 } else {
816 static_cast<Image&>(res) = mBufferData[mActiveBuffer].texture;
817 }
818 return res;
Mathias Agopiand606de62010-05-10 20:06:11 -0700819}
820
821sp<GraphicBuffer> Layer::BufferManager::getActiveBuffer() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700822 sp<GraphicBuffer> result;
823 const ssize_t activeBuffer = mActiveBuffer;
824 if (activeBuffer >= 0) {
825 BufferData const * const buffers = mBufferData;
826 Mutex::Autolock _l(mLock);
827 result = buffers[activeBuffer].buffer;
828 }
829 return result;
Mathias Agopiand606de62010-05-10 20:06:11 -0700830}
831
832sp<GraphicBuffer> Layer::BufferManager::detachBuffer(size_t index)
833{
Mathias Agopianbb641242010-05-18 17:06:55 -0700834 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700835 sp<GraphicBuffer> buffer;
836 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700837 buffer = buffers[index].buffer;
838 buffers[index].buffer = 0;
Mathias Agopiand606de62010-05-10 20:06:11 -0700839 return buffer;
840}
841
842status_t Layer::BufferManager::attachBuffer(size_t index,
843 const sp<GraphicBuffer>& buffer)
844{
Mathias Agopianbb641242010-05-18 17:06:55 -0700845 BufferData* const buffers = mBufferData;
Mathias Agopiand606de62010-05-10 20:06:11 -0700846 Mutex::Autolock _l(mLock);
Mathias Agopianbb641242010-05-18 17:06:55 -0700847 buffers[index].buffer = buffer;
848 buffers[index].texture.dirty = true;
Mathias Agopiand606de62010-05-10 20:06:11 -0700849 return NO_ERROR;
850}
851
852status_t Layer::BufferManager::destroy(EGLDisplay dpy)
853{
Mathias Agopianbb641242010-05-18 17:06:55 -0700854 BufferData* const buffers = mBufferData;
855 size_t num;
856 { // scope for the lock
857 Mutex::Autolock _l(mLock);
858 num = mNumBuffers;
859 for (size_t i=0 ; i<num ; i++) {
860 buffers[i].buffer = 0;
861 }
862 }
863 for (size_t i=0 ; i<num ; i++) {
864 destroyTexture(&buffers[i].texture, dpy);
Mathias Agopiand606de62010-05-10 20:06:11 -0700865 }
866 destroyTexture(&mFailoverTexture, dpy);
867 return NO_ERROR;
868}
869
870status_t Layer::BufferManager::initEglImage(EGLDisplay dpy,
871 const sp<GraphicBuffer>& buffer)
872{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700873 status_t err = NO_INIT;
874 ssize_t index = mActiveBuffer;
875 if (index >= 0) {
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700876 if (!mFailover) {
Kobi Cohen Arazi0d11baf2011-04-15 10:38:33 -0700877 {
878 // Without that lock, there is a chance of race condition
879 // where while composing a specific index, requestBuf
880 // with the same index can be executed and touch the same data
881 // that is being used in initEglImage.
882 // (e.g. dirty flag in texture)
883 Mutex::Autolock _l(mLock);
884 Image& texture(mBufferData[index].texture);
885 err = mTextureManager.initEglImage(&texture, dpy, buffer);
886 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700887 // if EGLImage fails, we switch to regular texture mode, and we
888 // free all resources associated with using EGLImages.
889 if (err == NO_ERROR) {
890 mFailover = false;
891 destroyTexture(&mFailoverTexture, dpy);
892 } else {
893 mFailover = true;
894 const size_t num = mNumBuffers;
895 for (size_t i=0 ; i<num ; i++) {
896 destroyTexture(&mBufferData[i].texture, dpy);
897 }
Andreas Hubere049a952010-06-25 09:25:19 -0700898 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700899 } else {
900 // we failed once, don't try again
901 err = BAD_VALUE;
Mathias Agopiand606de62010-05-10 20:06:11 -0700902 }
903 }
904 return err;
905}
906
907status_t Layer::BufferManager::loadTexture(
908 const Region& dirty, const GGLSurface& t)
909{
910 return mTextureManager.loadTexture(&mFailoverTexture, dirty, t);
911}
912
Mathias Agopianbb641242010-05-18 17:06:55 -0700913status_t Layer::BufferManager::destroyTexture(Image* tex, EGLDisplay dpy)
914{
915 if (tex->name != -1U) {
916 glDeleteTextures(1, &tex->name);
917 tex->name = -1U;
918 }
919 if (tex->image != EGL_NO_IMAGE_KHR) {
920 eglDestroyImageKHR(dpy, tex->image);
921 tex->image = EGL_NO_IMAGE_KHR;
922 }
923 return NO_ERROR;
924}
925
Mathias Agopiand606de62010-05-10 20:06:11 -0700926// ---------------------------------------------------------------------------
927
Mathias Agopian9a112062009-04-17 19:36:26 -0700928Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700929 const sp<Layer>& owner)
930 : Surface(flinger, owner->getIdentity(), owner)
Mathias Agopian9a112062009-04-17 19:36:26 -0700931{
932}
933
934Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700935{
936}
937
Mathias Agopiana138f892010-05-21 17:24:35 -0700938sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index,
939 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700940{
Mathias Agopian3330b202009-10-05 17:07:12 -0700941 sp<GraphicBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700942 sp<Layer> owner(getOwner());
943 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700944 /*
945 * requestBuffer() cannot be called from the main thread
946 * as it could cause a dead-lock, since it may have to wait
947 * on conditions updated my the main thread.
948 */
Mathias Agopiana138f892010-05-21 17:24:35 -0700949 buffer = owner->requestBuffer(index, w, h, format, usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700950 }
951 return buffer;
952}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700954status_t Layer::SurfaceLayer::setBufferCount(int bufferCount)
955{
956 status_t err = DEAD_OBJECT;
957 sp<Layer> owner(getOwner());
958 if (owner != 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700959 /*
960 * setBufferCount() cannot be called from the main thread
961 * as it could cause a dead-lock, since it may have to wait
962 * on conditions updated my the main thread.
963 */
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700964 err = owner->setBufferCount(bufferCount);
965 }
966 return err;
967}
968
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969// ---------------------------------------------------------------------------
970
971
972}; // namespace android