blob: 534a54e6a4220802918eb956d105bdd8793b223d [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/Errors.h>
25#include <utils/threads.h>
Mathias Agopiancbb288b2009-09-07 16:32:45 -070026#include <utils/CallStack.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080027#include <utils/Log.h>
28
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/IPCThreadState.h>
30#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070033#include <ui/GraphicBuffer.h>
34#include <ui/GraphicBufferMapper.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <ui/Rect.h>
36
Mathias Agopian9cce3252010-02-09 17:46:37 -080037#include <surfaceflinger/Surface.h>
38#include <surfaceflinger/ISurface.h>
39#include <surfaceflinger/ISurfaceComposer.h>
40#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
Mathias Agopian9cce3252010-02-09 17:46:37 -080042#include <private/surfaceflinger/SharedBufferStack.h>
43#include <private/surfaceflinger/LayerState.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047// ----------------------------------------------------------------------
48
Mathias Agopian14998592009-07-13 18:29:59 -070049static status_t copyBlt(
Mathias Agopian3330b202009-10-05 17:07:12 -070050 const sp<GraphicBuffer>& dst,
51 const sp<GraphicBuffer>& src,
Mathias Agopian0926f502009-05-04 14:17:04 -070052 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053{
Mathias Agopian245e4d72010-04-21 15:24:11 -070054 // src and dst with, height and format must be identical. no verification
55 // is done here.
Mathias Agopian14998592009-07-13 18:29:59 -070056 status_t err;
57 uint8_t const * src_bits = NULL;
58 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
59 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -070060
Mathias Agopian14998592009-07-13 18:29:59 -070061 uint8_t* dst_bits = NULL;
62 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
63 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
64
65 Region::const_iterator head(reg.begin());
66 Region::const_iterator tail(reg.end());
67 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068 const size_t bpp = bytesPerPixel(src->format);
69 const size_t dbpr = dst->stride * bpp;
70 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -070071
Mathias Agopian14998592009-07-13 18:29:59 -070072 while (head != tail) {
73 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -070074 ssize_t h = r.height();
75 if (h <= 0) continue;
76 size_t size = r.width() * bpp;
77 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
78 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
79 if (dbpr==sbpr && size==sbpr) {
80 size *= h;
81 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082 }
Mathias Agopian0926f502009-05-04 14:17:04 -070083 do {
84 memcpy(d, s, size);
85 d += dbpr;
86 s += sbpr;
87 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 }
89 }
Mathias Agopian0926f502009-05-04 14:17:04 -070090
Mathias Agopian14998592009-07-13 18:29:59 -070091 if (src_bits)
92 src->unlock();
93
94 if (dst_bits)
95 dst->unlock();
96
97 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098}
99
Mathias Agopian62185b72009-04-16 16:19:50 -0700100// ============================================================================
101// SurfaceControl
102// ============================================================================
103
Mathias Agopian01b76682009-04-16 20:04:08 -0700104SurfaceControl::SurfaceControl(
105 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700106 const sp<ISurface>& surface,
107 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700109 : mClient(client), mSurface(surface),
110 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700111 mWidth(data.width), mHeight(data.height), mFormat(data.format),
112 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700113{
114}
Mathias Agopian18d84462009-04-16 20:30:22 -0700115
Mathias Agopian62185b72009-04-16 16:19:50 -0700116SurfaceControl::~SurfaceControl()
117{
118 destroy();
119}
120
121void SurfaceControl::destroy()
122{
Mathias Agopian18d84462009-04-16 20:30:22 -0700123 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700124 mClient->destroySurface(mToken);
125 }
126
127 // clear all references and trigger an IPC now, to make sure things
128 // happen without delay, since these resources are quite heavy.
129 mClient.clear();
130 mSurface.clear();
131 IPCThreadState::self()->flushCommands();
132}
133
134void SurfaceControl::clear()
135{
136 // here, the window manager tells us explicitly that we should destroy
137 // the surface's resource. Soon after this call, it will also release
138 // its last reference (which will call the dtor); however, it is possible
139 // that a client living in the same process still holds references which
140 // would delay the call to the dtor -- that is why we need this explicit
141 // "clear()" call.
142 destroy();
143}
144
Mathias Agopian62185b72009-04-16 16:19:50 -0700145bool SurfaceControl::isSameSurface(
146 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
147{
148 if (lhs == 0 || rhs == 0)
149 return false;
150 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
151}
152
Mathias Agopian01b76682009-04-16 20:04:08 -0700153status_t SurfaceControl::setLayer(int32_t layer) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800154 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700155 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700156 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700157 return client->setLayer(mToken, layer);
158}
159status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800160 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700161 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700162 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700163 return client->setPosition(mToken, x, y);
164}
165status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800166 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700167 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700168 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700169 return client->setSize(mToken, w, h);
170}
171status_t SurfaceControl::hide() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800172 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700173 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700174 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700175 return client->hide(mToken);
176}
177status_t SurfaceControl::show(int32_t layer) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800178 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700179 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700180 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700181 return client->show(mToken, layer);
182}
183status_t SurfaceControl::freeze() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800184 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700185 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700186 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700187 return client->freeze(mToken);
188}
189status_t SurfaceControl::unfreeze() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800190 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700191 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700192 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700193 return client->unfreeze(mToken);
194}
195status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800196 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700197 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700198 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700199 return client->setFlags(mToken, flags, mask);
200}
201status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800202 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700203 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700204 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700205 return client->setTransparentRegionHint(mToken, transparent);
206}
207status_t SurfaceControl::setAlpha(float alpha) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800208 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700209 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700210 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700211 return client->setAlpha(mToken, alpha);
212}
213status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800214 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700215 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700216 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700217 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
218}
219status_t SurfaceControl::setFreezeTint(uint32_t tint) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800220 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700221 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700222 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700223 return client->setFreezeTint(mToken, tint);
224}
Mathias Agopian62185b72009-04-16 16:19:50 -0700225
Mathias Agopian963abad2009-11-13 15:26:29 -0800226status_t SurfaceControl::validate() const
Mathias Agopian62185b72009-04-16 16:19:50 -0700227{
228 if (mToken<0 || mClient==0) {
229 LOGE("invalid token (%d, identity=%u) or client (%p)",
230 mToken, mIdentity, mClient.get());
231 return NO_INIT;
232 }
Mathias Agopian62185b72009-04-16 16:19:50 -0700233 return NO_ERROR;
234}
235
Mathias Agopian01b76682009-04-16 20:04:08 -0700236status_t SurfaceControl::writeSurfaceToParcel(
237 const sp<SurfaceControl>& control, Parcel* parcel)
238{
239 uint32_t flags = 0;
240 uint32_t format = 0;
241 SurfaceID token = -1;
242 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700243 uint32_t width = 0;
244 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700245 sp<SurfaceComposerClient> client;
246 sp<ISurface> sur;
247 if (SurfaceControl::isValid(control)) {
248 token = control->mToken;
249 identity = control->mIdentity;
250 client = control->mClient;
251 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700252 width = control->mWidth;
253 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700254 format = control->mFormat;
255 flags = control->mFlags;
256 }
257 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
258 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
259 parcel->writeInt32(token);
260 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700261 parcel->writeInt32(width);
262 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700263 parcel->writeInt32(format);
264 parcel->writeInt32(flags);
265 return NO_ERROR;
266}
267
268sp<Surface> SurfaceControl::getSurface() const
269{
270 Mutex::Autolock _l(mLock);
271 if (mSurfaceData == 0) {
272 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
273 }
274 return mSurfaceData;
275}
276
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277// ============================================================================
278// Surface
279// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280
Mathias Agopian01b76682009-04-16 20:04:08 -0700281Surface::Surface(const sp<SurfaceControl>& surface)
282 : mClient(surface->mClient), mSurface(surface->mSurface),
283 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700284 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian3330b202009-10-05 17:07:12 -0700285 mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopian631f3582010-05-25 17:51:34 -0700286 mInitCheck(NO_INIT),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700287 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288{
Mathias Agopian01b76682009-04-16 20:04:08 -0700289 init();
290}
Mathias Agopian62185b72009-04-16 16:19:50 -0700291
Mathias Agopian01b76682009-04-16 20:04:08 -0700292Surface::Surface(const Parcel& parcel)
Mathias Agopian631f3582010-05-25 17:51:34 -0700293 : mBufferMapper(GraphicBufferMapper::get()),
294 mSharedBufferClient(NULL), mInitCheck(NO_INIT)
Mathias Agopian01b76682009-04-16 20:04:08 -0700295{
296 sp<IBinder> clientBinder = parcel.readStrongBinder();
297 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
298 mToken = parcel.readInt32();
299 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700300 mWidth = parcel.readInt32();
301 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700302 mFormat = parcel.readInt32();
303 mFlags = parcel.readInt32();
304
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700305 // FIXME: what does that mean if clientBinder is NULL here?
306 if (clientBinder != NULL) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700307 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700308 }
309
Mathias Agopian01b76682009-04-16 20:04:08 -0700310 init();
311}
312
313void Surface::init()
314{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316 android_native_window_t::dequeueBuffer = dequeueBuffer;
317 android_native_window_t::lockBuffer = lockBuffer;
318 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700319 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700320 android_native_window_t::perform = perform;
Mathias Agopian631f3582010-05-25 17:51:34 -0700321
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 DisplayInfo dinfo;
323 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
324 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
325 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
326 // FIXME: set real values here
327 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
328 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
329 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700330
Mathias Agopian55fa2512010-03-11 15:06:54 -0800331 mConnected = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700332 mSwapRectangle.makeInvalid();
Mathias Agopiana138f892010-05-21 17:24:35 -0700333 // two buffers by default
334 mBuffers.setCapacity(2);
335 mBuffers.insertAt(0, 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700336
337 if (mClient != 0) {
338 mSharedBufferClient = new SharedBufferClient(
339 mClient->getSharedClient(), mToken, 2, mIdentity);
340 }
341
342 mInitCheck = initCheck();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343}
344
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345Surface::~Surface()
346{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700347 // this is a client-side operation, the surface is destroyed, unmap
348 // its buffers in this process.
Mathias Agopiana138f892010-05-21 17:24:35 -0700349 size_t size = mBuffers.size();
350 for (size_t i=0 ; i<size ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700351 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700352 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700353 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700355
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700356 // clear all references and trigger an IPC now, to make sure things
357 // happen without delay, since these resources are quite heavy.
Mathias Agopiana138f892010-05-21 17:24:35 -0700358 mBuffers.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 mClient.clear();
360 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700361 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 IPCThreadState::self()->flushCommands();
363}
364
Mathias Agopian631f3582010-05-25 17:51:34 -0700365status_t Surface::initCheck() const
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700366{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700367 if (mToken<0 || mClient==0) {
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700368 return NO_INIT;
369 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700370 SharedClient const* cblk = mClient->getSharedClient();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700371 if (cblk == 0) {
372 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
373 return NO_INIT;
374 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700375 return NO_ERROR;
376}
377
378bool Surface::isValid() {
379 return mInitCheck == NO_ERROR;
380}
381
382status_t Surface::validate() const
383{
384 // check that we initialized ourself properly
385 if (mInitCheck != NO_ERROR) {
386 LOGE("invalid token (%d, identity=%u) or client (%p)",
387 mToken, mIdentity, mClient.get());
388 return mInitCheck;
389 }
390
391 // verify the identity of this surface
392 SharedClient const* cblk = mClient->getSharedClient();
393
394 uint32_t identity = cblk->getIdentity(mToken);
395
396 // this is a bit of a (temporary) special case, identity==0 means that
397 // no operation are allowed from the client (eg: dequeue/queue), this
398 // is used with PUSH_BUFFER surfaces for instance
399 if (identity == 0) {
400 LOGE("[Surface] invalid operation (identity=%u)", mIdentity);
401 return INVALID_OPERATION;
402 }
403
404 if (mIdentity != identity) {
405 LOGE("[Surface] using an invalid surface id=%d, "
406 "identity=%u should be %d",
407 mToken, mIdentity, identity);
408 return NO_INIT;
409 }
410
411 // check the surface didn't become invalid
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700412 status_t err = cblk->validate(mToken);
413 if (err != NO_ERROR) {
414 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
415 mToken, mIdentity, err, strerror(-err));
416 return err;
417 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700418
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700419 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420}
421
Mathias Agopian631f3582010-05-25 17:51:34 -0700422bool Surface::isSameSurface(const sp<Surface>& lhs, const sp<Surface>& rhs) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700423 if (lhs == 0 || rhs == 0)
424 return false;
425 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
426}
427
Mathias Agopian631f3582010-05-25 17:51:34 -0700428sp<ISurface> Surface::getISurface() const {
429 return mSurface;
430}
431
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700432// ----------------------------------------------------------------------------
433
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700434int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700435 return 0;
436}
437
438int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700439 android_native_buffer_t** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700440 Surface* self = getSelf(window);
441 return self->dequeueBuffer(buffer);
442}
443
444int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700445 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700446 Surface* self = getSelf(window);
447 return self->lockBuffer(buffer);
448}
449
450int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700451 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700452 Surface* self = getSelf(window);
453 return self->queueBuffer(buffer);
454}
455
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700456int Surface::query(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700457 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700458 Surface* self = getSelf(window);
459 return self->query(what, value);
460}
461
Mathias Agopian52212712009-08-11 22:34:02 -0700462int Surface::perform(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700463 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700464 va_list args;
465 va_start(args, operation);
466 Surface* self = getSelf(window);
467 int res = self->perform(operation, args);
468 va_end(args);
469 return res;
470}
471
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700472// ----------------------------------------------------------------------------
473
Mathias Agopiana138f892010-05-21 17:24:35 -0700474bool Surface::needNewBuffer(int bufIdx,
475 uint32_t *pWidth, uint32_t *pHeight,
476 uint32_t *pFormat, uint32_t *pUsage) const
477{
478 Mutex::Autolock _l(mSurfaceLock);
479
480 // Always call needNewBuffer(), since it clears the needed buffers flags
481 bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx);
482 bool validBuffer = mBufferInfo.validateBuffer(mBuffers[bufIdx]);
483 bool newNeewBuffer = needNewBuffer || !validBuffer;
484 if (newNeewBuffer) {
485 mBufferInfo.get(pWidth, pHeight, pFormat, pUsage);
486 }
487 return newNeewBuffer;
488}
489
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700490int Surface::dequeueBuffer(android_native_buffer_t** buffer)
491{
Mathias Agopian963abad2009-11-13 15:26:29 -0800492 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700493 if (err != NO_ERROR)
494 return err;
495
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700496 ssize_t bufIdx = mSharedBufferClient->dequeue();
497 if (bufIdx < 0) {
498 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
499 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700500 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700501
Mathias Agopiana138f892010-05-21 17:24:35 -0700502 // grow the buffer array if needed
503 const size_t size = mBuffers.size();
504 const size_t needed = bufIdx+1;
505 if (size < needed) {
506 mBuffers.insertAt(size, needed-size);
507 }
Mathias Agopian3a806952010-04-08 18:34:07 -0700508
Mathias Agopiana138f892010-05-21 17:24:35 -0700509 uint32_t w, h, format, usage;
510 if (needNewBuffer(bufIdx, &w, &h, &format, &usage)) {
511 err = getBufferLocked(bufIdx, w, h, format, usage);
512 LOGE_IF(err, "getBufferLocked(%ld, %u, %u, %u, %08x) failed (%s)",
513 bufIdx, w, h, format, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700514 if (err == NO_ERROR) {
515 // reset the width/height with the what we get from the buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700516 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian50517542009-08-19 17:10:18 -0700517 mWidth = uint32_t(backBuffer->width);
518 mHeight = uint32_t(backBuffer->height);
519 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700520 }
521
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700522 // if we still don't have a buffer here, we probably ran out of memory
Mathias Agopiana138f892010-05-21 17:24:35 -0700523 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700524 if (!err && backBuffer==0) {
525 err = NO_MEMORY;
526 }
527
Mathias Agopiancf81c842009-07-31 14:47:00 -0700528 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700529 mDirtyRegion.set(backBuffer->width, backBuffer->height);
530 *buffer = backBuffer.get();
531 } else {
532 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700533 }
Mathias Agopian50517542009-08-19 17:10:18 -0700534
Mathias Agopiancf81c842009-07-31 14:47:00 -0700535 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536}
537
538int Surface::lockBuffer(android_native_buffer_t* buffer)
539{
Mathias Agopian963abad2009-11-13 15:26:29 -0800540 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700541 if (err != NO_ERROR)
542 return err;
543
Mathias Agopianb2965332010-04-27 16:41:19 -0700544 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700545 err = mSharedBufferClient->lock(bufIdx);
546 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
547 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548}
549
550int Surface::queueBuffer(android_native_buffer_t* buffer)
551{
Mathias Agopian963abad2009-11-13 15:26:29 -0800552 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 if (err != NO_ERROR)
554 return err;
555
Mathias Agopian0926f502009-05-04 14:17:04 -0700556 if (mSwapRectangle.isValid()) {
557 mDirtyRegion.set(mSwapRectangle);
558 }
559
Mathias Agopianb2965332010-04-27 16:41:19 -0700560 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopiancc08e682010-04-15 18:48:26 -0700561 mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700562 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
563 err = mSharedBufferClient->queue(bufIdx);
564 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700566 if (err == NO_ERROR) {
567 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopian631f3582010-05-25 17:51:34 -0700568 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700569 client->signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700570 }
571 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572}
573
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700574int Surface::query(int what, int* value)
575{
576 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700577 case NATIVE_WINDOW_WIDTH:
578 *value = int(mWidth);
579 return NO_ERROR;
580 case NATIVE_WINDOW_HEIGHT:
581 *value = int(mHeight);
582 return NO_ERROR;
583 case NATIVE_WINDOW_FORMAT:
584 *value = int(mFormat);
585 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700586 }
587 return BAD_VALUE;
588}
589
Mathias Agopian52212712009-08-11 22:34:02 -0700590int Surface::perform(int operation, va_list args)
591{
Mathias Agopiancc08e682010-04-15 18:48:26 -0700592 status_t err = validate();
593 if (err != NO_ERROR)
594 return err;
595
Mathias Agopian52212712009-08-11 22:34:02 -0700596 int res = NO_ERROR;
597 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800598 case NATIVE_WINDOW_SET_USAGE:
599 dispatch_setUsage( args );
600 break;
601 case NATIVE_WINDOW_CONNECT:
602 res = dispatch_connect( args );
603 break;
604 case NATIVE_WINDOW_DISCONNECT:
605 res = dispatch_disconnect( args );
606 break;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700607 case NATIVE_WINDOW_SET_CROP:
608 res = dispatch_crop( args );
609 break;
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700610 case NATIVE_WINDOW_SET_BUFFER_COUNT:
611 res = dispatch_set_buffer_count( args );
612 break;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800613 default:
614 res = NAME_NOT_FOUND;
615 break;
Mathias Agopian52212712009-08-11 22:34:02 -0700616 }
617 return res;
618}
619
Mathias Agopian55fa2512010-03-11 15:06:54 -0800620void Surface::dispatch_setUsage(va_list args) {
621 int usage = va_arg(args, int);
622 setUsage( usage );
623}
624int Surface::dispatch_connect(va_list args) {
625 int api = va_arg(args, int);
626 return connect( api );
627}
628int Surface::dispatch_disconnect(va_list args) {
629 int api = va_arg(args, int);
630 return disconnect( api );
631}
Mathias Agopiancc08e682010-04-15 18:48:26 -0700632int Surface::dispatch_crop(va_list args) {
633 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
634 return crop( reinterpret_cast<Rect const*>(rect) );
635}
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700636int Surface::dispatch_set_buffer_count(va_list args) {
637 size_t bufferCount = va_arg(args, size_t);
638 return setBufferCount(bufferCount);
639}
Mathias Agopiana138f892010-05-21 17:24:35 -0700640int Surface::dispatch_set_buffers_geometry(va_list args) {
641 int w = va_arg(args, int);
642 int h = va_arg(args, int);
643 int f = va_arg(args, int);
644 return setBuffersGeometry(w, h, f);
645}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800646
Mathias Agopianba5972f2009-08-14 18:52:17 -0700647void Surface::setUsage(uint32_t reqUsage)
648{
649 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700650 mBufferInfo.set(reqUsage);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700651}
652
Mathias Agopian55fa2512010-03-11 15:06:54 -0800653int Surface::connect(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) {
660 err = -EINVAL;
661 } else {
662 mConnected = api;
663 }
664 break;
665 default:
666 err = -EINVAL;
667 break;
668 }
669 return err;
670}
671
672int Surface::disconnect(int api)
673{
674 Mutex::Autolock _l(mSurfaceLock);
675 int err = NO_ERROR;
676 switch (api) {
677 case NATIVE_WINDOW_API_EGL:
678 if (mConnected == api) {
679 mConnected = 0;
680 } else {
681 err = -EINVAL;
682 }
683 break;
684 default:
685 err = -EINVAL;
686 break;
687 }
688 return err;
689}
690
Mathias Agopiancc08e682010-04-15 18:48:26 -0700691int Surface::crop(Rect const* rect)
692{
693 Mutex::Autolock _l(mSurfaceLock);
694 // TODO: validate rect size
695 mNextBufferCrop = *rect;
696 return NO_ERROR;
697}
698
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700699int Surface::setBufferCount(int bufferCount)
700{
701 sp<ISurface> s(mSurface);
702 if (s == 0) return NO_INIT;
703
Mathias Agopianbb641242010-05-18 17:06:55 -0700704 class SetBufferCountIPC : public SharedBufferClient::SetBufferCountCallback {
705 sp<ISurface> surface;
706 virtual status_t operator()(int bufferCount) const {
707 return surface->setBufferCount(bufferCount);
708 }
709 public:
710 SetBufferCountIPC(const sp<ISurface>& surface) : surface(surface) { }
711 } ipc(s);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700712
Mathias Agopianbb641242010-05-18 17:06:55 -0700713 status_t err = mSharedBufferClient->setBufferCount(bufferCount, ipc);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700714 LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s",
715 bufferCount, strerror(-err));
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700716 return err;
717}
718
Mathias Agopiana138f892010-05-21 17:24:35 -0700719int Surface::setBuffersGeometry(int w, int h, int format)
720{
721 if (w<0 || h<0 || format<0)
722 return BAD_VALUE;
723
724 if ((w && !h) || (!w && h))
725 return BAD_VALUE;
726
727 Mutex::Autolock _l(mSurfaceLock);
728 mBufferInfo.set(w, h, format);
729 return NO_ERROR;
730}
731
732// ----------------------------------------------------------------------------
733
734int Surface::getConnectedApi() const
735{
736 Mutex::Autolock _l(mSurfaceLock);
737 return mConnected;
738}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800739
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700740// ----------------------------------------------------------------------------
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742status_t Surface::lock(SurfaceInfo* info, bool blocking) {
743 return Surface::lock(info, NULL, blocking);
744}
745
Mathias Agopian0926f502009-05-04 14:17:04 -0700746status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700747{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800748 if (getConnectedApi()) {
749 LOGE("Surface::lock(%p) failed. Already connected to another API",
750 (android_native_window_t*)this);
751 CallStack stack;
752 stack.update();
753 stack.dump("");
754 return INVALID_OPERATION;
755 }
756
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700757 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800758 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700759 CallStack stack;
760 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800761 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700762 return WOULD_BLOCK;
763 }
Mathias Agopian90147262010-01-22 11:47:55 -0800764
765 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700766
Mathias Agopian90147262010-01-22 11:47:55 -0800767 if (mLockedBuffer != 0) {
768 LOGE("Surface::lock failed, already locked");
769 mApiLock.unlock();
770 return INVALID_OPERATION;
771 }
772
Mathias Agopian52212712009-08-11 22:34:02 -0700773 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700774 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
775
Mathias Agopianb2965332010-04-27 16:41:19 -0700776 android_native_buffer_t* out;
777 status_t err = dequeueBuffer(&out);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700778 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700779 if (err == NO_ERROR) {
Mathias Agopianb2965332010-04-27 16:41:19 -0700780 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700781 err = lockBuffer(backBuffer.get());
782 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700783 getBufferIndex(backBuffer), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700784 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700785 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700786 const Region boundsRegion(bounds);
787 Region scratch(boundsRegion);
Mathias Agopian0926f502009-05-04 14:17:04 -0700788 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700789 newDirtyRegion &= boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790
Mathias Agopian245e4d72010-04-21 15:24:11 -0700791 // figure out if we can copy the frontbuffer back
Mathias Agopian3330b202009-10-05 17:07:12 -0700792 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700793 const bool canCopyBack = (frontBuffer != 0 &&
794 backBuffer->width == frontBuffer->width &&
795 backBuffer->height == frontBuffer->height &&
796 backBuffer->format == frontBuffer->format &&
797 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
798
799 // the dirty region we report to surfaceflinger is the one
800 // given by the user (as opposed to the one *we* return to the
801 // user).
802 mDirtyRegion = newDirtyRegion;
803
804 if (canCopyBack) {
805 // copy the area that is invalid and not repainted this round
806 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
807 if (!copyback.isEmpty())
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700808 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700809 } else {
810 // if we can't copy-back anything, modify the user's dirty
811 // region to make sure they redraw the whole buffer
812 newDirtyRegion = boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700813 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700814
Mathias Agopian245e4d72010-04-21 15:24:11 -0700815 // keep track of the are of the buffer that is "clean"
816 // (ie: that will be redrawn)
Mathias Agopian0926f502009-05-04 14:17:04 -0700817 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700818
Mathias Agopiane71212b2009-05-05 00:37:46 -0700819 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700820 status_t res = backBuffer->lock(
821 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700822 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700823
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700824 LOGW_IF(res, "failed locking buffer (handle = %p)",
825 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700826
827 mLockedBuffer = backBuffer;
828 other->w = backBuffer->width;
829 other->h = backBuffer->height;
830 other->s = backBuffer->stride;
831 other->usage = backBuffer->usage;
832 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700833 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700834 }
835 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700836 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700837 return err;
838}
839
840status_t Surface::unlockAndPost()
841{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700842 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800843 LOGE("Surface::unlockAndPost failed, no locked buffer");
844 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700845 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700846
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700847 status_t err = mLockedBuffer->unlock();
848 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700849
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700850 err = queueBuffer(mLockedBuffer.get());
851 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700852 getBufferIndex(mLockedBuffer), strerror(-err));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700853
854 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700855 mLockedBuffer = 0;
856 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857}
858
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700860 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 mSwapRectangle = r;
862}
863
Mathias Agopianb2965332010-04-27 16:41:19 -0700864int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const
865{
866 return buffer->getIndex();
867}
868
Mathias Agopiana138f892010-05-21 17:24:35 -0700869status_t Surface::getBufferLocked(int index,
870 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700872 sp<ISurface> s(mSurface);
873 if (s == 0) return NO_INIT;
874
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700875 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700876
877 // free the current buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700878 sp<GraphicBuffer>& currentBuffer(mBuffers.editItemAt(index));
Mathias Agopian50517542009-08-19 17:10:18 -0700879 if (currentBuffer != 0) {
880 getBufferMapper().unregisterBuffer(currentBuffer->handle);
881 currentBuffer.clear();
882 }
883
Mathias Agopiana138f892010-05-21 17:24:35 -0700884 sp<GraphicBuffer> buffer = s->requestBuffer(index, w, h, format, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700885 LOGE_IF(buffer==0,
886 "ISurface::getBuffer(%d, %08x) returned NULL",
887 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700888 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700889 LOGE_IF(buffer->handle == NULL,
Mathias Agopiana138f892010-05-21 17:24:35 -0700890 "Surface (identity=%d) requestBuffer(%d, %u, %u, %u, %08x) "
891 "returned a buffer with a null handle",
892 mIdentity, index, w, h, format, usage);
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700893 err = mSharedBufferClient->getStatus();
894 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
895 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -0700896 err = getBufferMapper().registerBuffer(buffer->handle);
897 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
898 err, strerror(-err));
899 if (err == NO_ERROR) {
900 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700901 currentBuffer->setIndex(index);
Mathias Agopian50517542009-08-19 17:10:18 -0700902 }
Mathias Agopiand3144be2009-10-06 15:58:44 -0700903 } else {
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700904 err = err<0 ? err : status_t(NO_MEMORY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 }
906 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700907 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908}
909
Mathias Agopiana138f892010-05-21 17:24:35 -0700910// ----------------------------------------------------------------------------
911Surface::BufferInfo::BufferInfo()
912 : mWidth(0), mHeight(0), mFormat(0),
913 mUsage(GRALLOC_USAGE_HW_RENDER), mDirty(0)
914{
915}
916
917void Surface::BufferInfo::set(uint32_t w, uint32_t h, uint32_t format) {
918 if ((mWidth != w) || (mHeight != h) || (mFormat != format)) {
919 mWidth = w;
920 mHeight = h;
921 mFormat = format;
922 mDirty |= GEOMETRY;
923 }
924}
925
926void Surface::BufferInfo::set(uint32_t usage) {
927 mUsage = usage;
928}
929
930void Surface::BufferInfo::get(uint32_t *pWidth, uint32_t *pHeight,
931 uint32_t *pFormat, uint32_t *pUsage) const {
932 *pWidth = mWidth;
933 *pHeight = mHeight;
934 *pFormat = mFormat;
935 *pUsage = mUsage;
936}
937
938bool Surface::BufferInfo::validateBuffer(const sp<GraphicBuffer>& buffer) const {
939 // make sure we AT LEAST have the usage flags we want
940 if (mDirty || buffer==0 ||
941 ((buffer->usage & mUsage) != mUsage)) {
942 mDirty = 0;
943 return false;
944 }
945 return true;
946}
947
948// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949}; // namespace android
950