blob: 71504fa79cf9aa0bda24ca564ec89b58c8b601b4 [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) {
154 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800155 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700156 if (err < 0) return err;
157 return client->setLayer(mToken, layer);
158}
159status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
160 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800161 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700162 if (err < 0) return err;
163 return client->setPosition(mToken, x, y);
164}
165status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
166 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800167 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700168 if (err < 0) return err;
169 return client->setSize(mToken, w, h);
170}
171status_t SurfaceControl::hide() {
172 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800173 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700174 if (err < 0) return err;
175 return client->hide(mToken);
176}
177status_t SurfaceControl::show(int32_t layer) {
178 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800179 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700180 if (err < 0) return err;
181 return client->show(mToken, layer);
182}
183status_t SurfaceControl::freeze() {
184 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800185 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700186 if (err < 0) return err;
187 return client->freeze(mToken);
188}
189status_t SurfaceControl::unfreeze() {
190 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800191 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700192 if (err < 0) return err;
193 return client->unfreeze(mToken);
194}
195status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
196 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800197 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700198 if (err < 0) return err;
199 return client->setFlags(mToken, flags, mask);
200}
201status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
202 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800203 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700204 if (err < 0) return err;
205 return client->setTransparentRegionHint(mToken, transparent);
206}
207status_t SurfaceControl::setAlpha(float alpha) {
208 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800209 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700210 if (err < 0) return err;
211 return client->setAlpha(mToken, alpha);
212}
213status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
214 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800215 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700216 if (err < 0) return err;
217 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
218}
219status_t SurfaceControl::setFreezeTint(uint32_t tint) {
220 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800221 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700222 if (err < 0) return err;
223 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 Agopian963abad2009-11-13 15:26:29 -0800233 SharedClient const* cblk = mClient->mControl;
Mathias Agopian62185b72009-04-16 16:19:50 -0700234 if (cblk == 0) {
235 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
236 return NO_INIT;
237 }
238 status_t err = cblk->validate(mToken);
239 if (err != NO_ERROR) {
240 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
241 mToken, mIdentity, err, strerror(-err));
242 return err;
243 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700244 uint32_t identity = cblk->getIdentity(mToken);
245 if (mIdentity != identity) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700246 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700247 mToken, mIdentity, identity);
Mathias Agopian62185b72009-04-16 16:19:50 -0700248 return NO_INIT;
249 }
250 return NO_ERROR;
251}
252
Mathias Agopian01b76682009-04-16 20:04:08 -0700253status_t SurfaceControl::writeSurfaceToParcel(
254 const sp<SurfaceControl>& control, Parcel* parcel)
255{
256 uint32_t flags = 0;
257 uint32_t format = 0;
258 SurfaceID token = -1;
259 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700260 uint32_t width = 0;
261 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700262 sp<SurfaceComposerClient> client;
263 sp<ISurface> sur;
264 if (SurfaceControl::isValid(control)) {
265 token = control->mToken;
266 identity = control->mIdentity;
267 client = control->mClient;
268 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700269 width = control->mWidth;
270 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700271 format = control->mFormat;
272 flags = control->mFlags;
273 }
274 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
275 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
276 parcel->writeInt32(token);
277 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700278 parcel->writeInt32(width);
279 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700280 parcel->writeInt32(format);
281 parcel->writeInt32(flags);
282 return NO_ERROR;
283}
284
285sp<Surface> SurfaceControl::getSurface() const
286{
287 Mutex::Autolock _l(mLock);
288 if (mSurfaceData == 0) {
289 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
290 }
291 return mSurfaceData;
292}
293
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700294// ============================================================================
295// Surface
296// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
Mathias Agopian01b76682009-04-16 20:04:08 -0700298Surface::Surface(const sp<SurfaceControl>& surface)
299 : mClient(surface->mClient), mSurface(surface->mSurface),
300 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700301 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian3330b202009-10-05 17:07:12 -0700302 mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700303 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700305 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700306 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700307
Mathias Agopian01b76682009-04-16 20:04:08 -0700308 init();
309}
Mathias Agopian62185b72009-04-16 16:19:50 -0700310
Mathias Agopian01b76682009-04-16 20:04:08 -0700311Surface::Surface(const Parcel& parcel)
Mathias Agopian3330b202009-10-05 17:07:12 -0700312 : mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL)
Mathias Agopian01b76682009-04-16 20:04:08 -0700313{
314 sp<IBinder> clientBinder = parcel.readStrongBinder();
315 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
316 mToken = parcel.readInt32();
317 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700318 mWidth = parcel.readInt32();
319 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700320 mFormat = parcel.readInt32();
321 mFlags = parcel.readInt32();
322
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700323 // FIXME: what does that mean if clientBinder is NULL here?
324 if (clientBinder != NULL) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700325 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
326
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700327 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700328 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700329 }
330
Mathias Agopian01b76682009-04-16 20:04:08 -0700331 init();
332}
333
334void Surface::init()
335{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700336 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700337 android_native_window_t::dequeueBuffer = dequeueBuffer;
338 android_native_window_t::lockBuffer = lockBuffer;
339 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700340 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700341 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700343 DisplayInfo dinfo;
344 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
345 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
346 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
347 // FIXME: set real values here
348 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
349 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
350 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian52212712009-08-11 22:34:02 -0700351 // be default we request a hardware surface
Mathias Agopian55fa2512010-03-11 15:06:54 -0800352 mConnected = 0;
Mathias Agopiana138f892010-05-21 17:24:35 -0700353 // two buffers by default
354 mBuffers.setCapacity(2);
355 mBuffers.insertAt(0, 2);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356}
357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358Surface::~Surface()
359{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700360 // this is a client-side operation, the surface is destroyed, unmap
361 // its buffers in this process.
Mathias Agopiana138f892010-05-21 17:24:35 -0700362 size_t size = mBuffers.size();
363 for (size_t i=0 ; i<size ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700364 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700365 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700366 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700368
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700369 // clear all references and trigger an IPC now, to make sure things
370 // happen without delay, since these resources are quite heavy.
Mathias Agopiana138f892010-05-21 17:24:35 -0700371 mBuffers.clear();
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 Agopiana138f892010-05-21 17:24:35 -0700470bool Surface::needNewBuffer(int bufIdx,
471 uint32_t *pWidth, uint32_t *pHeight,
472 uint32_t *pFormat, uint32_t *pUsage) const
473{
474 Mutex::Autolock _l(mSurfaceLock);
475
476 // Always call needNewBuffer(), since it clears the needed buffers flags
477 bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx);
478 bool validBuffer = mBufferInfo.validateBuffer(mBuffers[bufIdx]);
479 bool newNeewBuffer = needNewBuffer || !validBuffer;
480 if (newNeewBuffer) {
481 mBufferInfo.get(pWidth, pHeight, pFormat, pUsage);
482 }
483 return newNeewBuffer;
484}
485
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486int Surface::dequeueBuffer(android_native_buffer_t** buffer)
487{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700488 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800489 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700490 if (err != NO_ERROR)
491 return err;
492
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700493 ssize_t bufIdx = mSharedBufferClient->dequeue();
494 if (bufIdx < 0) {
495 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
496 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700497 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700498
Mathias Agopiana138f892010-05-21 17:24:35 -0700499 // grow the buffer array if needed
500 const size_t size = mBuffers.size();
501 const size_t needed = bufIdx+1;
502 if (size < needed) {
503 mBuffers.insertAt(size, needed-size);
504 }
Mathias Agopian3a806952010-04-08 18:34:07 -0700505
Mathias Agopiana138f892010-05-21 17:24:35 -0700506 uint32_t w, h, format, usage;
507 if (needNewBuffer(bufIdx, &w, &h, &format, &usage)) {
508 err = getBufferLocked(bufIdx, w, h, format, usage);
509 LOGE_IF(err, "getBufferLocked(%ld, %u, %u, %u, %08x) failed (%s)",
510 bufIdx, w, h, format, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700511 if (err == NO_ERROR) {
512 // reset the width/height with the what we get from the buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700513 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian50517542009-08-19 17:10:18 -0700514 mWidth = uint32_t(backBuffer->width);
515 mHeight = uint32_t(backBuffer->height);
516 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517 }
518
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700519 // if we still don't have a buffer here, we probably ran out of memory
Mathias Agopiana138f892010-05-21 17:24:35 -0700520 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700521 if (!err && backBuffer==0) {
522 err = NO_MEMORY;
523 }
524
Mathias Agopiancf81c842009-07-31 14:47:00 -0700525 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700526 mDirtyRegion.set(backBuffer->width, backBuffer->height);
527 *buffer = backBuffer.get();
528 } else {
529 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700530 }
Mathias Agopian50517542009-08-19 17:10:18 -0700531
Mathias Agopiancf81c842009-07-31 14:47:00 -0700532 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700533}
534
535int Surface::lockBuffer(android_native_buffer_t* buffer)
536{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700537 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800538 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700539 if (err != NO_ERROR)
540 return err;
541
Mathias Agopianb2965332010-04-27 16:41:19 -0700542 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700543 err = mSharedBufferClient->lock(bufIdx);
544 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
545 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546}
547
548int Surface::queueBuffer(android_native_buffer_t* buffer)
549{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700550 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800551 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552 if (err != NO_ERROR)
553 return err;
554
Mathias Agopian0926f502009-05-04 14:17:04 -0700555 if (mSwapRectangle.isValid()) {
556 mDirtyRegion.set(mSwapRectangle);
557 }
558
Mathias Agopianb2965332010-04-27 16:41:19 -0700559 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopiancc08e682010-04-15 18:48:26 -0700560 mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700561 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
562 err = mSharedBufferClient->queue(bufIdx);
563 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700565 if (err == NO_ERROR) {
566 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopianba5972f2009-08-14 18:52:17 -0700567 client->signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700568 }
569 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570}
571
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700572int Surface::query(int what, int* value)
573{
574 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700575 case NATIVE_WINDOW_WIDTH:
576 *value = int(mWidth);
577 return NO_ERROR;
578 case NATIVE_WINDOW_HEIGHT:
579 *value = int(mHeight);
580 return NO_ERROR;
581 case NATIVE_WINDOW_FORMAT:
582 *value = int(mFormat);
583 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700584 }
585 return BAD_VALUE;
586}
587
Mathias Agopian52212712009-08-11 22:34:02 -0700588int Surface::perform(int operation, va_list args)
589{
Mathias Agopiancc08e682010-04-15 18:48:26 -0700590 status_t err = validate();
591 if (err != NO_ERROR)
592 return err;
593
Mathias Agopian52212712009-08-11 22:34:02 -0700594 int res = NO_ERROR;
595 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800596 case NATIVE_WINDOW_SET_USAGE:
597 dispatch_setUsage( args );
598 break;
599 case NATIVE_WINDOW_CONNECT:
600 res = dispatch_connect( args );
601 break;
602 case NATIVE_WINDOW_DISCONNECT:
603 res = dispatch_disconnect( args );
604 break;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700605 case NATIVE_WINDOW_SET_CROP:
606 res = dispatch_crop( args );
607 break;
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700608 case NATIVE_WINDOW_SET_BUFFER_COUNT:
609 res = dispatch_set_buffer_count( args );
610 break;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800611 default:
612 res = NAME_NOT_FOUND;
613 break;
Mathias Agopian52212712009-08-11 22:34:02 -0700614 }
615 return res;
616}
617
Mathias Agopian55fa2512010-03-11 15:06:54 -0800618void Surface::dispatch_setUsage(va_list args) {
619 int usage = va_arg(args, int);
620 setUsage( usage );
621}
622int Surface::dispatch_connect(va_list args) {
623 int api = va_arg(args, int);
624 return connect( api );
625}
626int Surface::dispatch_disconnect(va_list args) {
627 int api = va_arg(args, int);
628 return disconnect( api );
629}
Mathias Agopiancc08e682010-04-15 18:48:26 -0700630int Surface::dispatch_crop(va_list args) {
631 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
632 return crop( reinterpret_cast<Rect const*>(rect) );
633}
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700634int Surface::dispatch_set_buffer_count(va_list args) {
635 size_t bufferCount = va_arg(args, size_t);
636 return setBufferCount(bufferCount);
637}
Mathias Agopiana138f892010-05-21 17:24:35 -0700638int Surface::dispatch_set_buffers_geometry(va_list args) {
639 int w = va_arg(args, int);
640 int h = va_arg(args, int);
641 int f = va_arg(args, int);
642 return setBuffersGeometry(w, h, f);
643}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800644
Mathias Agopianba5972f2009-08-14 18:52:17 -0700645void Surface::setUsage(uint32_t reqUsage)
646{
647 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700648 mBufferInfo.set(reqUsage);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700649}
650
Mathias Agopian55fa2512010-03-11 15:06:54 -0800651int Surface::connect(int api)
652{
653 Mutex::Autolock _l(mSurfaceLock);
654 int err = NO_ERROR;
655 switch (api) {
656 case NATIVE_WINDOW_API_EGL:
657 if (mConnected) {
658 err = -EINVAL;
659 } else {
660 mConnected = api;
661 }
662 break;
663 default:
664 err = -EINVAL;
665 break;
666 }
667 return err;
668}
669
670int Surface::disconnect(int api)
671{
672 Mutex::Autolock _l(mSurfaceLock);
673 int err = NO_ERROR;
674 switch (api) {
675 case NATIVE_WINDOW_API_EGL:
676 if (mConnected == api) {
677 mConnected = 0;
678 } else {
679 err = -EINVAL;
680 }
681 break;
682 default:
683 err = -EINVAL;
684 break;
685 }
686 return err;
687}
688
Mathias Agopiancc08e682010-04-15 18:48:26 -0700689int Surface::crop(Rect const* rect)
690{
691 Mutex::Autolock _l(mSurfaceLock);
692 // TODO: validate rect size
693 mNextBufferCrop = *rect;
694 return NO_ERROR;
695}
696
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700697int Surface::setBufferCount(int bufferCount)
698{
699 sp<ISurface> s(mSurface);
700 if (s == 0) return NO_INIT;
701
Mathias Agopianbb641242010-05-18 17:06:55 -0700702 class SetBufferCountIPC : public SharedBufferClient::SetBufferCountCallback {
703 sp<ISurface> surface;
704 virtual status_t operator()(int bufferCount) const {
705 return surface->setBufferCount(bufferCount);
706 }
707 public:
708 SetBufferCountIPC(const sp<ISurface>& surface) : surface(surface) { }
709 } ipc(s);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700710
Mathias Agopianbb641242010-05-18 17:06:55 -0700711 status_t err = mSharedBufferClient->setBufferCount(bufferCount, ipc);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700712 LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s",
713 bufferCount, strerror(-err));
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700714 return err;
715}
716
Mathias Agopiana138f892010-05-21 17:24:35 -0700717int Surface::setBuffersGeometry(int w, int h, int format)
718{
719 if (w<0 || h<0 || format<0)
720 return BAD_VALUE;
721
722 if ((w && !h) || (!w && h))
723 return BAD_VALUE;
724
725 Mutex::Autolock _l(mSurfaceLock);
726 mBufferInfo.set(w, h, format);
727 return NO_ERROR;
728}
729
730// ----------------------------------------------------------------------------
731
732int Surface::getConnectedApi() const
733{
734 Mutex::Autolock _l(mSurfaceLock);
735 return mConnected;
736}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800737
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700738// ----------------------------------------------------------------------------
739
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740status_t Surface::lock(SurfaceInfo* info, bool blocking) {
741 return Surface::lock(info, NULL, blocking);
742}
743
Mathias Agopian0926f502009-05-04 14:17:04 -0700744status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700745{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800746 if (getConnectedApi()) {
747 LOGE("Surface::lock(%p) failed. Already connected to another API",
748 (android_native_window_t*)this);
749 CallStack stack;
750 stack.update();
751 stack.dump("");
752 return INVALID_OPERATION;
753 }
754
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700755 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800756 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700757 CallStack stack;
758 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800759 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700760 return WOULD_BLOCK;
761 }
Mathias Agopian90147262010-01-22 11:47:55 -0800762
763 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700764
Mathias Agopian90147262010-01-22 11:47:55 -0800765 if (mLockedBuffer != 0) {
766 LOGE("Surface::lock failed, already locked");
767 mApiLock.unlock();
768 return INVALID_OPERATION;
769 }
770
Mathias Agopian52212712009-08-11 22:34:02 -0700771 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700772 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
773
Mathias Agopianb2965332010-04-27 16:41:19 -0700774 android_native_buffer_t* out;
775 status_t err = dequeueBuffer(&out);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700776 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700777 if (err == NO_ERROR) {
Mathias Agopianb2965332010-04-27 16:41:19 -0700778 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700779 err = lockBuffer(backBuffer.get());
780 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700781 getBufferIndex(backBuffer), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700782 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700783 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700784 const Region boundsRegion(bounds);
785 Region scratch(boundsRegion);
Mathias Agopian0926f502009-05-04 14:17:04 -0700786 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700787 newDirtyRegion &= boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700788
Mathias Agopian245e4d72010-04-21 15:24:11 -0700789 // figure out if we can copy the frontbuffer back
Mathias Agopian3330b202009-10-05 17:07:12 -0700790 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700791 const bool canCopyBack = (frontBuffer != 0 &&
792 backBuffer->width == frontBuffer->width &&
793 backBuffer->height == frontBuffer->height &&
794 backBuffer->format == frontBuffer->format &&
795 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
796
797 // the dirty region we report to surfaceflinger is the one
798 // given by the user (as opposed to the one *we* return to the
799 // user).
800 mDirtyRegion = newDirtyRegion;
801
802 if (canCopyBack) {
803 // copy the area that is invalid and not repainted this round
804 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
805 if (!copyback.isEmpty())
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700806 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700807 } else {
808 // if we can't copy-back anything, modify the user's dirty
809 // region to make sure they redraw the whole buffer
810 newDirtyRegion = boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700811 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700812
Mathias Agopian245e4d72010-04-21 15:24:11 -0700813 // keep track of the are of the buffer that is "clean"
814 // (ie: that will be redrawn)
Mathias Agopian0926f502009-05-04 14:17:04 -0700815 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700816
Mathias Agopiane71212b2009-05-05 00:37:46 -0700817 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700818 status_t res = backBuffer->lock(
819 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700820 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700821
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700822 LOGW_IF(res, "failed locking buffer (handle = %p)",
823 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700824
825 mLockedBuffer = backBuffer;
826 other->w = backBuffer->width;
827 other->h = backBuffer->height;
828 other->s = backBuffer->stride;
829 other->usage = backBuffer->usage;
830 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700831 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700832 }
833 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700834 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700835 return err;
836}
837
838status_t Surface::unlockAndPost()
839{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700840 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800841 LOGE("Surface::unlockAndPost failed, no locked buffer");
842 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700843 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700844
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700845 status_t err = mLockedBuffer->unlock();
846 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700847
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700848 err = queueBuffer(mLockedBuffer.get());
849 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700850 getBufferIndex(mLockedBuffer), strerror(-err));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700851
852 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700853 mLockedBuffer = 0;
854 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855}
856
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700858 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 mSwapRectangle = r;
860}
861
Mathias Agopianb2965332010-04-27 16:41:19 -0700862int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const
863{
864 return buffer->getIndex();
865}
866
Mathias Agopiana138f892010-05-21 17:24:35 -0700867status_t Surface::getBufferLocked(int index,
868 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700870 sp<ISurface> s(mSurface);
871 if (s == 0) return NO_INIT;
872
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700873 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700874
875 // free the current buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700876 sp<GraphicBuffer>& currentBuffer(mBuffers.editItemAt(index));
Mathias Agopian50517542009-08-19 17:10:18 -0700877 if (currentBuffer != 0) {
878 getBufferMapper().unregisterBuffer(currentBuffer->handle);
879 currentBuffer.clear();
880 }
881
Mathias Agopiana138f892010-05-21 17:24:35 -0700882 sp<GraphicBuffer> buffer = s->requestBuffer(index, w, h, format, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700883 LOGE_IF(buffer==0,
884 "ISurface::getBuffer(%d, %08x) returned NULL",
885 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700886 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700887 LOGE_IF(buffer->handle == NULL,
Mathias Agopiana138f892010-05-21 17:24:35 -0700888 "Surface (identity=%d) requestBuffer(%d, %u, %u, %u, %08x) "
889 "returned a buffer with a null handle",
890 mIdentity, index, w, h, format, usage);
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700891 err = mSharedBufferClient->getStatus();
892 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
893 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -0700894 err = getBufferMapper().registerBuffer(buffer->handle);
895 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
896 err, strerror(-err));
897 if (err == NO_ERROR) {
898 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700899 currentBuffer->setIndex(index);
Mathias Agopian50517542009-08-19 17:10:18 -0700900 }
Mathias Agopiand3144be2009-10-06 15:58:44 -0700901 } else {
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700902 err = err<0 ? err : status_t(NO_MEMORY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 }
904 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700905 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906}
907
Mathias Agopiana138f892010-05-21 17:24:35 -0700908// ----------------------------------------------------------------------------
909Surface::BufferInfo::BufferInfo()
910 : mWidth(0), mHeight(0), mFormat(0),
911 mUsage(GRALLOC_USAGE_HW_RENDER), mDirty(0)
912{
913}
914
915void Surface::BufferInfo::set(uint32_t w, uint32_t h, uint32_t format) {
916 if ((mWidth != w) || (mHeight != h) || (mFormat != format)) {
917 mWidth = w;
918 mHeight = h;
919 mFormat = format;
920 mDirty |= GEOMETRY;
921 }
922}
923
924void Surface::BufferInfo::set(uint32_t usage) {
925 mUsage = usage;
926}
927
928void Surface::BufferInfo::get(uint32_t *pWidth, uint32_t *pHeight,
929 uint32_t *pFormat, uint32_t *pUsage) const {
930 *pWidth = mWidth;
931 *pHeight = mHeight;
932 *pFormat = mFormat;
933 *pUsage = mUsage;
934}
935
936bool Surface::BufferInfo::validateBuffer(const sp<GraphicBuffer>& buffer) const {
937 // make sure we AT LEAST have the usage flags we want
938 if (mDirty || buffer==0 ||
939 ((buffer->usage & mUsage) != mUsage)) {
940 mDirty = 0;
941 return false;
942 }
943 return true;
944}
945
946// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947}; // namespace android
948