blob: 39938e8a85fcbae0720847ec8e90e9150248ce22 [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
17#define LOG_TAG "Surface"
18
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopiancbb288b2009-09-07 16:32:45 -070028#include <utils/CallStack.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080029#include <utils/Log.h>
30
31#include <pixelflinger/pixelflinger.h>
32
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/IPCThreadState.h>
34#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <ui/DisplayInfo.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
38#include <ui/GraphicBufferMapper.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/Rect.h>
40
Mathias Agopian9cce3252010-02-09 17:46:37 -080041#include <surfaceflinger/Surface.h>
42#include <surfaceflinger/ISurface.h>
43#include <surfaceflinger/ISurfaceComposer.h>
44#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045
Mathias Agopian9cce3252010-02-09 17:46:37 -080046#include <private/surfaceflinger/SharedBufferStack.h>
47#include <private/surfaceflinger/LayerState.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049namespace android {
50
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051// ----------------------------------------------------------------------
52
Mathias Agopian14998592009-07-13 18:29:59 -070053static status_t copyBlt(
Mathias Agopian3330b202009-10-05 17:07:12 -070054 const sp<GraphicBuffer>& dst,
55 const sp<GraphicBuffer>& src,
Mathias Agopian0926f502009-05-04 14:17:04 -070056 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057{
Mathias Agopian14998592009-07-13 18:29:59 -070058 status_t err;
59 uint8_t const * src_bits = NULL;
60 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
61 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -070062
Mathias Agopian14998592009-07-13 18:29:59 -070063 uint8_t* dst_bits = NULL;
64 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
65 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
66
67 Region::const_iterator head(reg.begin());
68 Region::const_iterator tail(reg.end());
69 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 const size_t bpp = bytesPerPixel(src->format);
72 const size_t dbpr = dst->stride * bpp;
73 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -070074
Mathias Agopian14998592009-07-13 18:29:59 -070075 while (head != tail) {
76 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -070077 ssize_t h = r.height();
78 if (h <= 0) continue;
79 size_t size = r.width() * bpp;
80 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
81 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
82 if (dbpr==sbpr && size==sbpr) {
83 size *= h;
84 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 }
Mathias Agopian0926f502009-05-04 14:17:04 -070086 do {
87 memcpy(d, s, size);
88 d += dbpr;
89 s += sbpr;
90 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 }
92 }
Mathias Agopian0926f502009-05-04 14:17:04 -070093
Mathias Agopian14998592009-07-13 18:29:59 -070094 if (src_bits)
95 src->unlock();
96
97 if (dst_bits)
98 dst->unlock();
99
100 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101}
102
Mathias Agopian62185b72009-04-16 16:19:50 -0700103// ============================================================================
104// SurfaceControl
105// ============================================================================
106
Mathias Agopian01b76682009-04-16 20:04:08 -0700107SurfaceControl::SurfaceControl(
108 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700109 const sp<ISurface>& surface,
110 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700111 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700112 : mClient(client), mSurface(surface),
113 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700114 mWidth(data.width), mHeight(data.height), mFormat(data.format),
115 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700116{
117}
Mathias Agopian18d84462009-04-16 20:30:22 -0700118
Mathias Agopian62185b72009-04-16 16:19:50 -0700119SurfaceControl::~SurfaceControl()
120{
121 destroy();
122}
123
124void SurfaceControl::destroy()
125{
Mathias Agopian18d84462009-04-16 20:30:22 -0700126 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700127 mClient->destroySurface(mToken);
128 }
129
130 // clear all references and trigger an IPC now, to make sure things
131 // happen without delay, since these resources are quite heavy.
132 mClient.clear();
133 mSurface.clear();
134 IPCThreadState::self()->flushCommands();
135}
136
137void SurfaceControl::clear()
138{
139 // here, the window manager tells us explicitly that we should destroy
140 // the surface's resource. Soon after this call, it will also release
141 // its last reference (which will call the dtor); however, it is possible
142 // that a client living in the same process still holds references which
143 // would delay the call to the dtor -- that is why we need this explicit
144 // "clear()" call.
145 destroy();
146}
147
Mathias Agopian62185b72009-04-16 16:19:50 -0700148bool SurfaceControl::isSameSurface(
149 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
150{
151 if (lhs == 0 || rhs == 0)
152 return false;
153 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
154}
155
Mathias Agopian01b76682009-04-16 20:04:08 -0700156status_t SurfaceControl::setLayer(int32_t layer) {
157 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800158 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700159 if (err < 0) return err;
160 return client->setLayer(mToken, layer);
161}
162status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
163 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800164 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700165 if (err < 0) return err;
166 return client->setPosition(mToken, x, y);
167}
168status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
169 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800170 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700171 if (err < 0) return err;
172 return client->setSize(mToken, w, h);
173}
174status_t SurfaceControl::hide() {
175 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800176 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700177 if (err < 0) return err;
178 return client->hide(mToken);
179}
180status_t SurfaceControl::show(int32_t layer) {
181 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800182 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700183 if (err < 0) return err;
184 return client->show(mToken, layer);
185}
186status_t SurfaceControl::freeze() {
187 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800188 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700189 if (err < 0) return err;
190 return client->freeze(mToken);
191}
192status_t SurfaceControl::unfreeze() {
193 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800194 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700195 if (err < 0) return err;
196 return client->unfreeze(mToken);
197}
198status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
199 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800200 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700201 if (err < 0) return err;
202 return client->setFlags(mToken, flags, mask);
203}
204status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
205 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800206 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700207 if (err < 0) return err;
208 return client->setTransparentRegionHint(mToken, transparent);
209}
210status_t SurfaceControl::setAlpha(float alpha) {
211 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800212 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700213 if (err < 0) return err;
214 return client->setAlpha(mToken, alpha);
215}
216status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
217 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800218 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700219 if (err < 0) return err;
220 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
221}
222status_t SurfaceControl::setFreezeTint(uint32_t tint) {
223 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800224 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700225 if (err < 0) return err;
226 return client->setFreezeTint(mToken, tint);
227}
Mathias Agopian62185b72009-04-16 16:19:50 -0700228
Mathias Agopian963abad2009-11-13 15:26:29 -0800229status_t SurfaceControl::validate() const
Mathias Agopian62185b72009-04-16 16:19:50 -0700230{
231 if (mToken<0 || mClient==0) {
232 LOGE("invalid token (%d, identity=%u) or client (%p)",
233 mToken, mIdentity, mClient.get());
234 return NO_INIT;
235 }
Mathias Agopian963abad2009-11-13 15:26:29 -0800236 SharedClient const* cblk = mClient->mControl;
Mathias Agopian62185b72009-04-16 16:19:50 -0700237 if (cblk == 0) {
238 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
239 return NO_INIT;
240 }
241 status_t err = cblk->validate(mToken);
242 if (err != NO_ERROR) {
243 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
244 mToken, mIdentity, err, strerror(-err));
245 return err;
246 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700247 uint32_t identity = cblk->getIdentity(mToken);
248 if (mIdentity != identity) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700249 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700250 mToken, mIdentity, identity);
Mathias Agopian62185b72009-04-16 16:19:50 -0700251 return NO_INIT;
252 }
253 return NO_ERROR;
254}
255
Mathias Agopian01b76682009-04-16 20:04:08 -0700256status_t SurfaceControl::writeSurfaceToParcel(
257 const sp<SurfaceControl>& control, Parcel* parcel)
258{
259 uint32_t flags = 0;
260 uint32_t format = 0;
261 SurfaceID token = -1;
262 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700263 uint32_t width = 0;
264 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700265 sp<SurfaceComposerClient> client;
266 sp<ISurface> sur;
267 if (SurfaceControl::isValid(control)) {
268 token = control->mToken;
269 identity = control->mIdentity;
270 client = control->mClient;
271 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700272 width = control->mWidth;
273 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700274 format = control->mFormat;
275 flags = control->mFlags;
276 }
277 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
278 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
279 parcel->writeInt32(token);
280 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700281 parcel->writeInt32(width);
282 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700283 parcel->writeInt32(format);
284 parcel->writeInt32(flags);
285 return NO_ERROR;
286}
287
288sp<Surface> SurfaceControl::getSurface() const
289{
290 Mutex::Autolock _l(mLock);
291 if (mSurfaceData == 0) {
292 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
293 }
294 return mSurfaceData;
295}
296
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297// ============================================================================
298// Surface
299// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
Mathias Agopian01b76682009-04-16 20:04:08 -0700301Surface::Surface(const sp<SurfaceControl>& surface)
302 : mClient(surface->mClient), mSurface(surface->mSurface),
303 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700304 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian3330b202009-10-05 17:07:12 -0700305 mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700306 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700308 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700309 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700310
Mathias Agopian01b76682009-04-16 20:04:08 -0700311 init();
312}
Mathias Agopian62185b72009-04-16 16:19:50 -0700313
Mathias Agopian01b76682009-04-16 20:04:08 -0700314Surface::Surface(const Parcel& parcel)
Mathias Agopian3330b202009-10-05 17:07:12 -0700315 : mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL)
Mathias Agopian01b76682009-04-16 20:04:08 -0700316{
317 sp<IBinder> clientBinder = parcel.readStrongBinder();
318 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
319 mToken = parcel.readInt32();
320 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700321 mWidth = parcel.readInt32();
322 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700323 mFormat = parcel.readInt32();
324 mFlags = parcel.readInt32();
325
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700326 // FIXME: what does that mean if clientBinder is NULL here?
327 if (clientBinder != NULL) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700328 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
329
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700330 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700331 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700332 }
333
Mathias Agopian01b76682009-04-16 20:04:08 -0700334 init();
335}
336
337void Surface::init()
338{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340 android_native_window_t::dequeueBuffer = dequeueBuffer;
341 android_native_window_t::lockBuffer = lockBuffer;
342 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700343 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700344 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 DisplayInfo dinfo;
347 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
348 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
349 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
350 // FIXME: set real values here
351 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
352 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
353 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian52212712009-08-11 22:34:02 -0700354 // be default we request a hardware surface
355 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800356 mConnected = 0;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700357 mNeedFullUpdate = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358}
359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360Surface::~Surface()
361{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700362 // this is a client-side operation, the surface is destroyed, unmap
363 // its buffers in this process.
364 for (int i=0 ; i<2 ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700365 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700366 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700367 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700369
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700370 // clear all references and trigger an IPC now, to make sure things
371 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 mClient.clear();
373 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700374 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 IPCThreadState::self()->flushCommands();
376}
377
Mathias Agopianba5972f2009-08-14 18:52:17 -0700378sp<SurfaceComposerClient> Surface::getClient() const {
379 return mClient;
380}
381
382sp<ISurface> Surface::getISurface() const {
383 return mSurface;
384}
385
386bool Surface::isValid() {
387 return mToken>=0 && mClient!=0;
388}
389
Mathias Agopian963abad2009-11-13 15:26:29 -0800390status_t Surface::validate() const
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700391{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700392 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700393 if (mToken<0 || mClient==0) {
394 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopianba5972f2009-08-14 18:52:17 -0700395 mToken, mIdentity, client.get());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700396 return NO_INIT;
397 }
Mathias Agopian963abad2009-11-13 15:26:29 -0800398 SharedClient const* cblk = mClient->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700399 if (cblk == 0) {
400 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
401 return NO_INIT;
402 }
403 status_t err = cblk->validate(mToken);
404 if (err != NO_ERROR) {
405 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
406 mToken, mIdentity, err, strerror(-err));
407 return err;
408 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700409 uint32_t identity = cblk->getIdentity(mToken);
410 if (mIdentity != identity) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700411 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700412 mToken, mIdentity, identity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700413 return NO_INIT;
414 }
415 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416}
417
Mathias Agopian01b76682009-04-16 20:04:08 -0700418
419bool Surface::isSameSurface(
420 const sp<Surface>& lhs, const sp<Surface>& rhs)
421{
422 if (lhs == 0 || rhs == 0)
423 return false;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700424
Mathias Agopian01b76682009-04-16 20:04:08 -0700425 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
426}
427
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428// ----------------------------------------------------------------------------
429
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700430int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700431 return 0;
432}
433
434int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700435 android_native_buffer_t** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700436 Surface* self = getSelf(window);
437 return self->dequeueBuffer(buffer);
438}
439
440int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700441 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700442 Surface* self = getSelf(window);
443 return self->lockBuffer(buffer);
444}
445
446int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700447 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700448 Surface* self = getSelf(window);
449 return self->queueBuffer(buffer);
450}
451
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700452int Surface::query(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700453 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700454 Surface* self = getSelf(window);
455 return self->query(what, value);
456}
457
Mathias Agopian52212712009-08-11 22:34:02 -0700458int Surface::perform(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700459 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700460 va_list args;
461 va_start(args, operation);
462 Surface* self = getSelf(window);
463 int res = self->perform(operation, args);
464 va_end(args);
465 return res;
466}
467
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700468// ----------------------------------------------------------------------------
469
Mathias Agopian3330b202009-10-05 17:07:12 -0700470status_t Surface::dequeueBuffer(sp<GraphicBuffer>* buffer) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700471 android_native_buffer_t* out;
472 status_t err = dequeueBuffer(&out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700473 if (err == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700474 *buffer = GraphicBuffer::getSelf(out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700475 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700476 return err;
477}
478
Mathias Agopian0926f502009-05-04 14:17:04 -0700479// ----------------------------------------------------------------------------
480
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700481
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700482int Surface::dequeueBuffer(android_native_buffer_t** buffer)
483{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700484 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800485 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486 if (err != NO_ERROR)
487 return err;
488
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700489 ssize_t bufIdx = mSharedBufferClient->dequeue();
490 if (bufIdx < 0) {
491 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
492 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700493 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700494
495 // below we make sure we AT LEAST have the usage flags we want
496 const uint32_t usage(getUsage());
Mathias Agopian3330b202009-10-05 17:07:12 -0700497 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian3a806952010-04-08 18:34:07 -0700498
499 // Always call needNewBuffer(), since it clears the needed buffers flags
500 bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700501 if (backBuffer == 0 ||
502 ((uint32_t(backBuffer->usage) & usage) != usage) ||
Mathias Agopian3a806952010-04-08 18:34:07 -0700503 needNewBuffer)
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700504 {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700505 err = getBufferLocked(bufIdx, usage);
506 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
507 bufIdx, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700508 if (err == NO_ERROR) {
509 // reset the width/height with the what we get from the buffer
Mathias Agopian50517542009-08-19 17:10:18 -0700510 mWidth = uint32_t(backBuffer->width);
511 mHeight = uint32_t(backBuffer->height);
512 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700513 }
514
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700515 // if we still don't have a buffer here, we probably ran out of memory
516 if (!err && backBuffer==0) {
517 err = NO_MEMORY;
518 }
519
Mathias Agopiancf81c842009-07-31 14:47:00 -0700520 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700521 mDirtyRegion.set(backBuffer->width, backBuffer->height);
522 *buffer = backBuffer.get();
523 } else {
524 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700525 }
Mathias Agopian50517542009-08-19 17:10:18 -0700526
Mathias Agopiancf81c842009-07-31 14:47:00 -0700527 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700528}
529
530int Surface::lockBuffer(android_native_buffer_t* buffer)
531{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700532 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800533 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700534 if (err != NO_ERROR)
535 return err;
536
Mathias Agopian3330b202009-10-05 17:07:12 -0700537 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700538 err = mSharedBufferClient->lock(bufIdx);
539 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
540 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700541}
542
543int Surface::queueBuffer(android_native_buffer_t* buffer)
544{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700545 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800546 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547 if (err != NO_ERROR)
548 return err;
549
Mathias Agopian0926f502009-05-04 14:17:04 -0700550 if (mSwapRectangle.isValid()) {
551 mDirtyRegion.set(mSwapRectangle);
552 }
553
Mathias Agopian3330b202009-10-05 17:07:12 -0700554 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopiancc08e682010-04-15 18:48:26 -0700555 mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700556 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
557 err = mSharedBufferClient->queue(bufIdx);
558 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700560 if (err == NO_ERROR) {
561 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopianba5972f2009-08-14 18:52:17 -0700562 client->signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700563 }
564 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565}
566
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700567int Surface::query(int what, int* value)
568{
569 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700570 case NATIVE_WINDOW_WIDTH:
571 *value = int(mWidth);
572 return NO_ERROR;
573 case NATIVE_WINDOW_HEIGHT:
574 *value = int(mHeight);
575 return NO_ERROR;
576 case NATIVE_WINDOW_FORMAT:
577 *value = int(mFormat);
578 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700579 }
580 return BAD_VALUE;
581}
582
Mathias Agopian52212712009-08-11 22:34:02 -0700583int Surface::perform(int operation, va_list args)
584{
Mathias Agopiancc08e682010-04-15 18:48:26 -0700585 status_t err = validate();
586 if (err != NO_ERROR)
587 return err;
588
Mathias Agopian52212712009-08-11 22:34:02 -0700589 int res = NO_ERROR;
590 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800591 case NATIVE_WINDOW_SET_USAGE:
592 dispatch_setUsage( args );
593 break;
594 case NATIVE_WINDOW_CONNECT:
595 res = dispatch_connect( args );
596 break;
597 case NATIVE_WINDOW_DISCONNECT:
598 res = dispatch_disconnect( args );
599 break;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700600 case NATIVE_WINDOW_SET_CROP:
601 res = dispatch_crop( args );
602 break;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800603 default:
604 res = NAME_NOT_FOUND;
605 break;
Mathias Agopian52212712009-08-11 22:34:02 -0700606 }
607 return res;
608}
609
Mathias Agopian55fa2512010-03-11 15:06:54 -0800610void Surface::dispatch_setUsage(va_list args) {
611 int usage = va_arg(args, int);
612 setUsage( usage );
613}
614int Surface::dispatch_connect(va_list args) {
615 int api = va_arg(args, int);
616 return connect( api );
617}
618int Surface::dispatch_disconnect(va_list args) {
619 int api = va_arg(args, int);
620 return disconnect( api );
621}
Mathias Agopiancc08e682010-04-15 18:48:26 -0700622int Surface::dispatch_crop(va_list args) {
623 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
624 return crop( reinterpret_cast<Rect const*>(rect) );
625}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800626
627
Mathias Agopianba5972f2009-08-14 18:52:17 -0700628void Surface::setUsage(uint32_t reqUsage)
629{
630 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700631 mUsage = reqUsage;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700632}
633
Mathias Agopian55fa2512010-03-11 15:06:54 -0800634int Surface::connect(int api)
635{
636 Mutex::Autolock _l(mSurfaceLock);
637 int err = NO_ERROR;
638 switch (api) {
639 case NATIVE_WINDOW_API_EGL:
640 if (mConnected) {
641 err = -EINVAL;
642 } else {
643 mConnected = api;
644 }
645 break;
646 default:
647 err = -EINVAL;
648 break;
649 }
650 return err;
651}
652
653int Surface::disconnect(int api)
654{
655 Mutex::Autolock _l(mSurfaceLock);
656 int err = NO_ERROR;
657 switch (api) {
658 case NATIVE_WINDOW_API_EGL:
659 if (mConnected == api) {
660 mConnected = 0;
661 } else {
662 err = -EINVAL;
663 }
664 break;
665 default:
666 err = -EINVAL;
667 break;
668 }
669 return err;
670}
671
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700672uint32_t Surface::getUsage() const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700673{
674 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700675 return mUsage;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700676}
677
Mathias Agopian55fa2512010-03-11 15:06:54 -0800678int Surface::getConnectedApi() const
679{
680 Mutex::Autolock _l(mSurfaceLock);
681 return mConnected;
682}
683
Mathias Agopiancc08e682010-04-15 18:48:26 -0700684int Surface::crop(Rect const* rect)
685{
686 Mutex::Autolock _l(mSurfaceLock);
687 // TODO: validate rect size
688 mNextBufferCrop = *rect;
689 return NO_ERROR;
690}
691
Mathias Agopian55fa2512010-03-11 15:06:54 -0800692
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700693// ----------------------------------------------------------------------------
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695status_t Surface::lock(SurfaceInfo* info, bool blocking) {
696 return Surface::lock(info, NULL, blocking);
697}
698
Mathias Agopian0926f502009-05-04 14:17:04 -0700699status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700700{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800701 if (getConnectedApi()) {
702 LOGE("Surface::lock(%p) failed. Already connected to another API",
703 (android_native_window_t*)this);
704 CallStack stack;
705 stack.update();
706 stack.dump("");
707 return INVALID_OPERATION;
708 }
709
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700710 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800711 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700712 CallStack stack;
713 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800714 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700715 return WOULD_BLOCK;
716 }
Mathias Agopian90147262010-01-22 11:47:55 -0800717
718 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700719
Mathias Agopian90147262010-01-22 11:47:55 -0800720 if (mLockedBuffer != 0) {
721 LOGE("Surface::lock failed, already locked");
722 mApiLock.unlock();
723 return INVALID_OPERATION;
724 }
725
Mathias Agopian52212712009-08-11 22:34:02 -0700726 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700727 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
728
Mathias Agopian3330b202009-10-05 17:07:12 -0700729 sp<GraphicBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700730 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700731 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700732 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700733 err = lockBuffer(backBuffer.get());
734 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
735 backBuffer->getIndex(), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700736 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700737 // we handle copy-back here...
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700738
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700739 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700740 Region scratch(bounds);
741 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742
Mathias Agopian3a806952010-04-08 18:34:07 -0700743 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700744 if (mNeedFullUpdate) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700745 mNeedFullUpdate = false;
Mathias Agopian3a806952010-04-08 18:34:07 -0700746 Region uninitialized(bounds);
747 uninitialized.subtractSelf(copyback | newDirtyRegion);
748 // reset newDirtyRegion to bounds when a buffer is reallocated
749 // and we have nothing to copy back to it
750 if (!uninitialized.isEmpty())
751 newDirtyRegion.set(bounds);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700752 }
Mathias Agopian3a806952010-04-08 18:34:07 -0700753 newDirtyRegion.andSelf(bounds);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700754
Mathias Agopian3330b202009-10-05 17:07:12 -0700755 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian3a806952010-04-08 18:34:07 -0700756 if (frontBuffer != 0 &&
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700757 backBuffer->width == frontBuffer->width &&
758 backBuffer->height == frontBuffer->height &&
759 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
760 {
Mathias Agopian3a806952010-04-08 18:34:07 -0700761 if (!copyback.isEmpty()) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700762 // copy front to back
763 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700764 }
765 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700766
Mathias Agopian0926f502009-05-04 14:17:04 -0700767 mDirtyRegion = newDirtyRegion;
768 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700769
Mathias Agopiane71212b2009-05-05 00:37:46 -0700770 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700771 status_t res = backBuffer->lock(
772 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700773 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700774
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700775 LOGW_IF(res, "failed locking buffer (handle = %p)",
776 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700777
778 mLockedBuffer = backBuffer;
779 other->w = backBuffer->width;
780 other->h = backBuffer->height;
781 other->s = backBuffer->stride;
782 other->usage = backBuffer->usage;
783 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700784 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700785 }
786 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700787 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700788 return err;
789}
790
791status_t Surface::unlockAndPost()
792{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700793 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800794 LOGE("Surface::unlockAndPost failed, no locked buffer");
795 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700796 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700797
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700798 status_t err = mLockedBuffer->unlock();
799 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700800
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700801 err = queueBuffer(mLockedBuffer.get());
802 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
803 mLockedBuffer->getIndex(), strerror(-err));
804
805 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700806 mLockedBuffer = 0;
807 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808}
809
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700811 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 mSwapRectangle = r;
813}
814
Mathias Agopian52212712009-08-11 22:34:02 -0700815status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700817 sp<ISurface> s(mSurface);
818 if (s == 0) return NO_INIT;
819
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700820 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700821
822 // free the current buffer
Mathias Agopian3330b202009-10-05 17:07:12 -0700823 sp<GraphicBuffer>& currentBuffer(mBuffers[index]);
Mathias Agopian50517542009-08-19 17:10:18 -0700824 if (currentBuffer != 0) {
825 getBufferMapper().unregisterBuffer(currentBuffer->handle);
826 currentBuffer.clear();
827 }
828
Mathias Agopian3330b202009-10-05 17:07:12 -0700829 sp<GraphicBuffer> buffer = s->requestBuffer(index, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700830 LOGE_IF(buffer==0,
831 "ISurface::getBuffer(%d, %08x) returned NULL",
832 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700833 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700834 LOGE_IF(buffer->handle == NULL,
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700835 "Surface (identity=%d) requestBuffer(%d, %08x) returned"
836 "a buffer with a null handle", mIdentity, index, usage);
837 err = mSharedBufferClient->getStatus();
838 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
839 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -0700840 err = getBufferMapper().registerBuffer(buffer->handle);
841 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
842 err, strerror(-err));
843 if (err == NO_ERROR) {
844 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700845 currentBuffer->setIndex(index);
846 mNeedFullUpdate = true;
Mathias Agopian50517542009-08-19 17:10:18 -0700847 }
Mathias Agopiand3144be2009-10-06 15:58:44 -0700848 } else {
849 err = err<0 ? err : NO_MEMORY;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 }
851 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700852 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853}
854
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855}; // namespace android
856