blob: 1de3a4fb7ea1b45c5ffc13da37a88f4dd40e6562 [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,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700107 const ISurfaceComposerClient::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{
Mathias Agopian579b3f82010-06-08 19:54:15 -0700239 sp<ISurface> sur;
Mathias Agopian01b76682009-04-16 20:04:08 -0700240 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700241 uint32_t width = 0;
242 uint32_t height = 0;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700243 uint32_t format = 0;
244 uint32_t flags = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700245 if (SurfaceControl::isValid(control)) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700246 sur = control->mSurface;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700247 identity = control->mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700248 width = control->mWidth;
249 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700250 format = control->mFormat;
251 flags = control->mFlags;
252 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700253 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
Mathias Agopian01b76682009-04-16 20:04:08 -0700254 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700255 parcel->writeInt32(width);
256 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700257 parcel->writeInt32(format);
258 parcel->writeInt32(flags);
259 return NO_ERROR;
260}
261
262sp<Surface> SurfaceControl::getSurface() const
263{
264 Mutex::Autolock _l(mLock);
265 if (mSurfaceData == 0) {
266 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
267 }
268 return mSurfaceData;
269}
270
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271// ============================================================================
272// Surface
273// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700275class SurfaceClient : public Singleton<SurfaceClient>
276{
277 // all these attributes are constants
278 sp<ISurfaceComposer> mComposerService;
279 sp<ISurfaceComposerClient> mClient;
280 status_t mStatus;
281 SharedClient* mControl;
282 sp<IMemoryHeap> mControlMemory;
283
284 SurfaceClient()
285 : Singleton<SurfaceClient>(), mStatus(NO_INIT)
286 {
287 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
288 mComposerService = sf;
289 mClient = sf->createClientConnection();
290 if (mClient != NULL) {
291 mControlMemory = mClient->getControlBlock();
292 if (mControlMemory != NULL) {
293 mControl = static_cast<SharedClient *>(
294 mControlMemory->getBase());
295 if (mControl) {
296 mStatus = NO_ERROR;
297 }
298 }
299 }
300 }
301 friend class Singleton<SurfaceClient>;
302public:
303 status_t initCheck() const {
304 return mStatus;
305 }
306 SharedClient* getSharedClient() const {
307 return mControl;
308 }
309 ssize_t getTokenForSurface(const sp<ISurface>& sur) const {
310 // TODO: we could cache a few tokens here to avoid an IPC
311 return mClient->getTokenForSurface(sur);
312 }
313 void signalServer() const {
314 mComposerService->signal();
315 }
316};
317
318ANDROID_SINGLETON_STATIC_INSTANCE(SurfaceClient);
319
320// ---------------------------------------------------------------------------
321
Mathias Agopian01b76682009-04-16 20:04:08 -0700322Surface::Surface(const sp<SurfaceControl>& surface)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700323 : mBufferMapper(GraphicBufferMapper::get()),
324 mClient(SurfaceClient::getInstance()),
325 mSharedBufferClient(NULL),
Mathias Agopian631f3582010-05-25 17:51:34 -0700326 mInitCheck(NO_INIT),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700327 mSurface(surface->mSurface),
328 mIdentity(surface->mIdentity),
329 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700330 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331{
Mathias Agopian01b76682009-04-16 20:04:08 -0700332 init();
333}
Mathias Agopian62185b72009-04-16 16:19:50 -0700334
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700335Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700336 : mBufferMapper(GraphicBufferMapper::get()),
337 mClient(SurfaceClient::getInstance()),
338 mSharedBufferClient(NULL),
339 mInitCheck(NO_INIT)
Mathias Agopian01b76682009-04-16 20:04:08 -0700340{
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700341 mSurface = interface_cast<ISurface>(ref);
Mathias Agopian01b76682009-04-16 20:04:08 -0700342 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700343 mWidth = parcel.readInt32();
344 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700345 mFormat = parcel.readInt32();
346 mFlags = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700347 init();
348}
349
Mathias Agopian579b3f82010-06-08 19:54:15 -0700350status_t Surface::writeToParcel(
351 const sp<Surface>& surface, Parcel* parcel)
352{
353 sp<ISurface> sur;
354 uint32_t identity = 0;
355 uint32_t width = 0;
356 uint32_t height = 0;
357 uint32_t format = 0;
358 uint32_t flags = 0;
359 if (Surface::isValid(surface)) {
360 sur = surface->mSurface;
361 identity = surface->mIdentity;
362 width = surface->mWidth;
363 height = surface->mHeight;
364 format = surface->mFormat;
365 flags = surface->mFlags;
366 }
367 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
368 parcel->writeInt32(identity);
369 parcel->writeInt32(width);
370 parcel->writeInt32(height);
371 parcel->writeInt32(format);
372 parcel->writeInt32(flags);
373 return NO_ERROR;
374
375}
376
Jamie Gennisaca4e222010-07-15 17:29:15 -0700377
378Mutex Surface::sCachedSurfacesLock;
379DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces(wp<Surface>(0));
380
381sp<Surface> Surface::readFromParcel(const Parcel& data) {
382 Mutex::Autolock _l(sCachedSurfacesLock);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700383 sp<IBinder> binder(data.readStrongBinder());
Jamie Gennisaca4e222010-07-15 17:29:15 -0700384 sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
385 if (surface == 0) {
386 surface = new Surface(data, binder);
387 sCachedSurfaces.add(binder, surface);
388 } else {
389 LOGW("Reusing surface!");
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700390 }
Jamie Gennisaca4e222010-07-15 17:29:15 -0700391 if (surface->mSurface == 0) {
392 surface = 0;
393 }
394 cleanCachedSurfaces();
395 return surface;
396}
397
398// Remove the stale entries from the surface cache. This should only be called
399// with sCachedSurfacesLock held.
400void Surface::cleanCachedSurfaces() {
401 for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
402 wp<Surface> s(sCachedSurfaces.valueAt(i));
403 if (s == 0 || s.promote() == 0) {
404 sCachedSurfaces.removeItemsAt(i);
405 }
406 }
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700407}
408
Mathias Agopian01b76682009-04-16 20:04:08 -0700409void Surface::init()
410{
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700411 ANativeWindow::setSwapInterval = setSwapInterval;
412 ANativeWindow::dequeueBuffer = dequeueBuffer;
413 ANativeWindow::lockBuffer = lockBuffer;
414 ANativeWindow::queueBuffer = queueBuffer;
415 ANativeWindow::query = query;
416 ANativeWindow::perform = perform;
Mathias Agopian631f3582010-05-25 17:51:34 -0700417
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700418 DisplayInfo dinfo;
419 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700420 const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
421 const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700422 // FIXME: set real values here
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700423 const_cast<int&>(ANativeWindow::minSwapInterval) = 1;
424 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
425 const_cast<uint32_t&>(ANativeWindow::flags) = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700426
Mathias Agopian55fa2512010-03-11 15:06:54 -0800427 mConnected = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700428 mSwapRectangle.makeInvalid();
Mathias Agopiana138f892010-05-21 17:24:35 -0700429 // two buffers by default
430 mBuffers.setCapacity(2);
431 mBuffers.insertAt(0, 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700432
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700433 if (mSurface != 0 && mClient.initCheck() == NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700434 int32_t token = mClient.getTokenForSurface(mSurface);
435 if (token >= 0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700436 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian579b3f82010-06-08 19:54:15 -0700437 mClient.getSharedClient(), token, 2, mIdentity);
438 mInitCheck = mClient.getSharedClient()->validate(token);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700439 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700440 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441}
442
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443Surface::~Surface()
444{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700445 // this is a client-side operation, the surface is destroyed, unmap
446 // its buffers in this process.
Mathias Agopiana138f892010-05-21 17:24:35 -0700447 size_t size = mBuffers.size();
448 for (size_t i=0 ; i<size ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700449 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700450 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700451 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700453
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700454 // clear all references and trigger an IPC now, to make sure things
455 // happen without delay, since these resources are quite heavy.
Mathias Agopiana138f892010-05-21 17:24:35 -0700456 mBuffers.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700458 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459 IPCThreadState::self()->flushCommands();
460}
461
Mathias Agopian631f3582010-05-25 17:51:34 -0700462bool Surface::isValid() {
463 return mInitCheck == NO_ERROR;
464}
465
466status_t Surface::validate() const
467{
468 // check that we initialized ourself properly
469 if (mInitCheck != NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700470 LOGE("invalid token (identity=%u)", mIdentity);
Mathias Agopian631f3582010-05-25 17:51:34 -0700471 return mInitCheck;
472 }
473
474 // verify the identity of this surface
Mathias Agopian7e27f052010-05-28 14:22:23 -0700475 uint32_t identity = mSharedBufferClient->getIdentity();
Mathias Agopian631f3582010-05-25 17:51:34 -0700476
477 // this is a bit of a (temporary) special case, identity==0 means that
478 // no operation are allowed from the client (eg: dequeue/queue), this
479 // is used with PUSH_BUFFER surfaces for instance
480 if (identity == 0) {
481 LOGE("[Surface] invalid operation (identity=%u)", mIdentity);
482 return INVALID_OPERATION;
483 }
484
485 if (mIdentity != identity) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700486 LOGE("[Surface] using an invalid surface, "
Mathias Agopian631f3582010-05-25 17:51:34 -0700487 "identity=%u should be %d",
Mathias Agopian579b3f82010-06-08 19:54:15 -0700488 mIdentity, identity);
Mathias Agopian631f3582010-05-25 17:51:34 -0700489 return NO_INIT;
490 }
491
492 // check the surface didn't become invalid
Mathias Agopian7e27f052010-05-28 14:22:23 -0700493 status_t err = mSharedBufferClient->getStatus();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700494 if (err != NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700495 LOGE("surface (identity=%u) is invalid, err=%d (%s)",
496 mIdentity, err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700497 return err;
498 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700499
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700500 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501}
502
Mathias Agopian631f3582010-05-25 17:51:34 -0700503sp<ISurface> Surface::getISurface() const {
504 return mSurface;
505}
506
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700507// ----------------------------------------------------------------------------
508
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700509int Surface::setSwapInterval(ANativeWindow* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510 return 0;
511}
512
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700513int Surface::dequeueBuffer(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700514 android_native_buffer_t** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515 Surface* self = getSelf(window);
516 return self->dequeueBuffer(buffer);
517}
518
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700519int Surface::lockBuffer(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700520 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521 Surface* self = getSelf(window);
522 return self->lockBuffer(buffer);
523}
524
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700525int Surface::queueBuffer(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700526 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700527 Surface* self = getSelf(window);
528 return self->queueBuffer(buffer);
529}
530
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700531int Surface::query(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700532 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700533 Surface* self = getSelf(window);
534 return self->query(what, value);
535}
536
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700537int Surface::perform(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700538 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700539 va_list args;
540 va_start(args, operation);
541 Surface* self = getSelf(window);
542 int res = self->perform(operation, args);
543 va_end(args);
544 return res;
545}
546
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547// ----------------------------------------------------------------------------
548
Mathias Agopiana138f892010-05-21 17:24:35 -0700549bool Surface::needNewBuffer(int bufIdx,
550 uint32_t *pWidth, uint32_t *pHeight,
551 uint32_t *pFormat, uint32_t *pUsage) const
552{
553 Mutex::Autolock _l(mSurfaceLock);
554
555 // Always call needNewBuffer(), since it clears the needed buffers flags
556 bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx);
557 bool validBuffer = mBufferInfo.validateBuffer(mBuffers[bufIdx]);
558 bool newNeewBuffer = needNewBuffer || !validBuffer;
559 if (newNeewBuffer) {
560 mBufferInfo.get(pWidth, pHeight, pFormat, pUsage);
561 }
562 return newNeewBuffer;
563}
564
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565int Surface::dequeueBuffer(android_native_buffer_t** buffer)
566{
Mathias Agopian963abad2009-11-13 15:26:29 -0800567 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700568 if (err != NO_ERROR)
569 return err;
570
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700571 ssize_t bufIdx = mSharedBufferClient->dequeue();
572 if (bufIdx < 0) {
573 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
574 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700575 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700576
Mathias Agopiana138f892010-05-21 17:24:35 -0700577 // grow the buffer array if needed
578 const size_t size = mBuffers.size();
579 const size_t needed = bufIdx+1;
580 if (size < needed) {
581 mBuffers.insertAt(size, needed-size);
582 }
Mathias Agopian3a806952010-04-08 18:34:07 -0700583
Mathias Agopiana138f892010-05-21 17:24:35 -0700584 uint32_t w, h, format, usage;
585 if (needNewBuffer(bufIdx, &w, &h, &format, &usage)) {
586 err = getBufferLocked(bufIdx, w, h, format, usage);
587 LOGE_IF(err, "getBufferLocked(%ld, %u, %u, %u, %08x) failed (%s)",
588 bufIdx, w, h, format, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700589 if (err == NO_ERROR) {
590 // reset the width/height with the what we get from the buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700591 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian50517542009-08-19 17:10:18 -0700592 mWidth = uint32_t(backBuffer->width);
593 mHeight = uint32_t(backBuffer->height);
594 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595 }
596
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700597 // if we still don't have a buffer here, we probably ran out of memory
Mathias Agopiana138f892010-05-21 17:24:35 -0700598 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700599 if (!err && backBuffer==0) {
600 err = NO_MEMORY;
601 }
602
Mathias Agopiancf81c842009-07-31 14:47:00 -0700603 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700604 mDirtyRegion.set(backBuffer->width, backBuffer->height);
605 *buffer = backBuffer.get();
606 } else {
607 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700608 }
Mathias Agopian50517542009-08-19 17:10:18 -0700609
Mathias Agopiancf81c842009-07-31 14:47:00 -0700610 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611}
612
613int Surface::lockBuffer(android_native_buffer_t* buffer)
614{
Mathias Agopian963abad2009-11-13 15:26:29 -0800615 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700616 if (err != NO_ERROR)
617 return err;
618
Mathias Agopianb2965332010-04-27 16:41:19 -0700619 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700620 err = mSharedBufferClient->lock(bufIdx);
621 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
622 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700623}
624
625int Surface::queueBuffer(android_native_buffer_t* buffer)
626{
Mathias Agopian963abad2009-11-13 15:26:29 -0800627 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700628 if (err != NO_ERROR)
629 return err;
630
Mathias Agopian0926f502009-05-04 14:17:04 -0700631 if (mSwapRectangle.isValid()) {
632 mDirtyRegion.set(mSwapRectangle);
633 }
634
Mathias Agopianb2965332010-04-27 16:41:19 -0700635 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopiancc08e682010-04-15 18:48:26 -0700636 mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700637 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
638 err = mSharedBufferClient->queue(bufIdx);
639 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700640
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700641 if (err == NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700642 // TODO: can we avoid this IPC if we know there is one pending?
643 mClient.signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700644 }
645 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646}
647
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700648int Surface::query(int what, int* value)
649{
650 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700651 case NATIVE_WINDOW_WIDTH:
652 *value = int(mWidth);
653 return NO_ERROR;
654 case NATIVE_WINDOW_HEIGHT:
655 *value = int(mHeight);
656 return NO_ERROR;
657 case NATIVE_WINDOW_FORMAT:
658 *value = int(mFormat);
659 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700660 }
661 return BAD_VALUE;
662}
663
Mathias Agopian52212712009-08-11 22:34:02 -0700664int Surface::perform(int operation, va_list args)
665{
Mathias Agopiancc08e682010-04-15 18:48:26 -0700666 status_t err = validate();
667 if (err != NO_ERROR)
668 return err;
669
Mathias Agopian52212712009-08-11 22:34:02 -0700670 int res = NO_ERROR;
671 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800672 case NATIVE_WINDOW_SET_USAGE:
673 dispatch_setUsage( args );
674 break;
675 case NATIVE_WINDOW_CONNECT:
676 res = dispatch_connect( args );
677 break;
678 case NATIVE_WINDOW_DISCONNECT:
679 res = dispatch_disconnect( args );
680 break;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700681 case NATIVE_WINDOW_SET_CROP:
682 res = dispatch_crop( args );
683 break;
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700684 case NATIVE_WINDOW_SET_BUFFER_COUNT:
685 res = dispatch_set_buffer_count( args );
686 break;
Mathias Agopian38ece272010-05-26 21:31:09 -0700687 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
688 res = dispatch_set_buffers_geometry( args );
689 break;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800690 default:
691 res = NAME_NOT_FOUND;
692 break;
Mathias Agopian52212712009-08-11 22:34:02 -0700693 }
694 return res;
695}
696
Mathias Agopian55fa2512010-03-11 15:06:54 -0800697void Surface::dispatch_setUsage(va_list args) {
698 int usage = va_arg(args, int);
699 setUsage( usage );
700}
701int Surface::dispatch_connect(va_list args) {
702 int api = va_arg(args, int);
703 return connect( api );
704}
705int Surface::dispatch_disconnect(va_list args) {
706 int api = va_arg(args, int);
707 return disconnect( api );
708}
Mathias Agopiancc08e682010-04-15 18:48:26 -0700709int Surface::dispatch_crop(va_list args) {
710 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
711 return crop( reinterpret_cast<Rect const*>(rect) );
712}
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700713int Surface::dispatch_set_buffer_count(va_list args) {
714 size_t bufferCount = va_arg(args, size_t);
715 return setBufferCount(bufferCount);
716}
Mathias Agopiana138f892010-05-21 17:24:35 -0700717int Surface::dispatch_set_buffers_geometry(va_list args) {
718 int w = va_arg(args, int);
719 int h = va_arg(args, int);
720 int f = va_arg(args, int);
721 return setBuffersGeometry(w, h, f);
722}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800723
Mathias Agopianba5972f2009-08-14 18:52:17 -0700724void Surface::setUsage(uint32_t reqUsage)
725{
726 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700727 mBufferInfo.set(reqUsage);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700728}
729
Mathias Agopian55fa2512010-03-11 15:06:54 -0800730int Surface::connect(int api)
731{
732 Mutex::Autolock _l(mSurfaceLock);
733 int err = NO_ERROR;
734 switch (api) {
735 case NATIVE_WINDOW_API_EGL:
736 if (mConnected) {
737 err = -EINVAL;
738 } else {
739 mConnected = api;
740 }
741 break;
742 default:
743 err = -EINVAL;
744 break;
745 }
746 return err;
747}
748
749int Surface::disconnect(int api)
750{
751 Mutex::Autolock _l(mSurfaceLock);
752 int err = NO_ERROR;
753 switch (api) {
754 case NATIVE_WINDOW_API_EGL:
755 if (mConnected == api) {
756 mConnected = 0;
757 } else {
758 err = -EINVAL;
759 }
760 break;
761 default:
762 err = -EINVAL;
763 break;
764 }
765 return err;
766}
767
Mathias Agopiancc08e682010-04-15 18:48:26 -0700768int Surface::crop(Rect const* rect)
769{
770 Mutex::Autolock _l(mSurfaceLock);
771 // TODO: validate rect size
772 mNextBufferCrop = *rect;
773 return NO_ERROR;
774}
775
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700776int Surface::setBufferCount(int bufferCount)
777{
778 sp<ISurface> s(mSurface);
779 if (s == 0) return NO_INIT;
780
Mathias Agopianbb641242010-05-18 17:06:55 -0700781 class SetBufferCountIPC : public SharedBufferClient::SetBufferCountCallback {
782 sp<ISurface> surface;
783 virtual status_t operator()(int bufferCount) const {
784 return surface->setBufferCount(bufferCount);
785 }
786 public:
787 SetBufferCountIPC(const sp<ISurface>& surface) : surface(surface) { }
788 } ipc(s);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700789
Mathias Agopianbb641242010-05-18 17:06:55 -0700790 status_t err = mSharedBufferClient->setBufferCount(bufferCount, ipc);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700791 LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s",
792 bufferCount, strerror(-err));
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700793 return err;
794}
795
Mathias Agopiana138f892010-05-21 17:24:35 -0700796int Surface::setBuffersGeometry(int w, int h, int format)
797{
798 if (w<0 || h<0 || format<0)
799 return BAD_VALUE;
800
801 if ((w && !h) || (!w && h))
802 return BAD_VALUE;
803
804 Mutex::Autolock _l(mSurfaceLock);
805 mBufferInfo.set(w, h, format);
806 return NO_ERROR;
807}
808
809// ----------------------------------------------------------------------------
810
811int Surface::getConnectedApi() const
812{
813 Mutex::Autolock _l(mSurfaceLock);
814 return mConnected;
815}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800816
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700817// ----------------------------------------------------------------------------
818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819status_t Surface::lock(SurfaceInfo* info, bool blocking) {
820 return Surface::lock(info, NULL, blocking);
821}
822
Mathias Agopian0926f502009-05-04 14:17:04 -0700823status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700824{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800825 if (getConnectedApi()) {
826 LOGE("Surface::lock(%p) failed. Already connected to another API",
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700827 (ANativeWindow*)this);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800828 CallStack stack;
829 stack.update();
830 stack.dump("");
831 return INVALID_OPERATION;
832 }
833
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700834 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800835 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700836 CallStack stack;
837 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800838 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700839 return WOULD_BLOCK;
840 }
Mathias Agopian90147262010-01-22 11:47:55 -0800841
842 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700843
Mathias Agopian90147262010-01-22 11:47:55 -0800844 if (mLockedBuffer != 0) {
845 LOGE("Surface::lock failed, already locked");
846 mApiLock.unlock();
847 return INVALID_OPERATION;
848 }
849
Mathias Agopian52212712009-08-11 22:34:02 -0700850 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700851 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
852
Mathias Agopianb2965332010-04-27 16:41:19 -0700853 android_native_buffer_t* out;
854 status_t err = dequeueBuffer(&out);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700855 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700856 if (err == NO_ERROR) {
Mathias Agopianb2965332010-04-27 16:41:19 -0700857 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700858 err = lockBuffer(backBuffer.get());
859 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700860 getBufferIndex(backBuffer), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700861 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700862 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700863 const Region boundsRegion(bounds);
864 Region scratch(boundsRegion);
Mathias Agopian0926f502009-05-04 14:17:04 -0700865 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700866 newDirtyRegion &= boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700867
Mathias Agopian245e4d72010-04-21 15:24:11 -0700868 // figure out if we can copy the frontbuffer back
Mathias Agopian3330b202009-10-05 17:07:12 -0700869 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700870 const bool canCopyBack = (frontBuffer != 0 &&
871 backBuffer->width == frontBuffer->width &&
872 backBuffer->height == frontBuffer->height &&
873 backBuffer->format == frontBuffer->format &&
874 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
875
876 // the dirty region we report to surfaceflinger is the one
877 // given by the user (as opposed to the one *we* return to the
878 // user).
879 mDirtyRegion = newDirtyRegion;
880
881 if (canCopyBack) {
882 // copy the area that is invalid and not repainted this round
883 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
884 if (!copyback.isEmpty())
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700885 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700886 } else {
887 // if we can't copy-back anything, modify the user's dirty
888 // region to make sure they redraw the whole buffer
889 newDirtyRegion = boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700890 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700891
Mathias Agopian245e4d72010-04-21 15:24:11 -0700892 // keep track of the are of the buffer that is "clean"
893 // (ie: that will be redrawn)
Mathias Agopian0926f502009-05-04 14:17:04 -0700894 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700895
Mathias Agopiane71212b2009-05-05 00:37:46 -0700896 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700897 status_t res = backBuffer->lock(
898 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700899 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700900
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700901 LOGW_IF(res, "failed locking buffer (handle = %p)",
902 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700903
904 mLockedBuffer = backBuffer;
905 other->w = backBuffer->width;
906 other->h = backBuffer->height;
907 other->s = backBuffer->stride;
908 other->usage = backBuffer->usage;
909 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700910 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700911 }
912 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700913 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700914 return err;
915}
916
917status_t Surface::unlockAndPost()
918{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700919 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800920 LOGE("Surface::unlockAndPost failed, no locked buffer");
921 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700922 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700923
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700924 status_t err = mLockedBuffer->unlock();
925 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700926
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700927 err = queueBuffer(mLockedBuffer.get());
928 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700929 getBufferIndex(mLockedBuffer), strerror(-err));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700930
931 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700932 mLockedBuffer = 0;
933 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934}
935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700937 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 mSwapRectangle = r;
939}
940
Mathias Agopianb2965332010-04-27 16:41:19 -0700941int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const
942{
943 return buffer->getIndex();
944}
945
Mathias Agopiana138f892010-05-21 17:24:35 -0700946status_t Surface::getBufferLocked(int index,
947 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700949 sp<ISurface> s(mSurface);
950 if (s == 0) return NO_INIT;
951
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700952 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700953
954 // free the current buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700955 sp<GraphicBuffer>& currentBuffer(mBuffers.editItemAt(index));
Mathias Agopian50517542009-08-19 17:10:18 -0700956 if (currentBuffer != 0) {
957 getBufferMapper().unregisterBuffer(currentBuffer->handle);
958 currentBuffer.clear();
959 }
960
Mathias Agopiana138f892010-05-21 17:24:35 -0700961 sp<GraphicBuffer> buffer = s->requestBuffer(index, w, h, format, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700962 LOGE_IF(buffer==0,
963 "ISurface::getBuffer(%d, %08x) returned NULL",
964 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700965 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700966 LOGE_IF(buffer->handle == NULL,
Mathias Agopiana138f892010-05-21 17:24:35 -0700967 "Surface (identity=%d) requestBuffer(%d, %u, %u, %u, %08x) "
968 "returned a buffer with a null handle",
969 mIdentity, index, w, h, format, usage);
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700970 err = mSharedBufferClient->getStatus();
971 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
972 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -0700973 err = getBufferMapper().registerBuffer(buffer->handle);
974 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
975 err, strerror(-err));
976 if (err == NO_ERROR) {
977 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700978 currentBuffer->setIndex(index);
Mathias Agopian50517542009-08-19 17:10:18 -0700979 }
Mathias Agopiand3144be2009-10-06 15:58:44 -0700980 } else {
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700981 err = err<0 ? err : status_t(NO_MEMORY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 }
983 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700984 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985}
986
Mathias Agopiana138f892010-05-21 17:24:35 -0700987// ----------------------------------------------------------------------------
988Surface::BufferInfo::BufferInfo()
989 : mWidth(0), mHeight(0), mFormat(0),
990 mUsage(GRALLOC_USAGE_HW_RENDER), mDirty(0)
991{
992}
993
994void Surface::BufferInfo::set(uint32_t w, uint32_t h, uint32_t format) {
995 if ((mWidth != w) || (mHeight != h) || (mFormat != format)) {
996 mWidth = w;
997 mHeight = h;
998 mFormat = format;
999 mDirty |= GEOMETRY;
1000 }
1001}
1002
1003void Surface::BufferInfo::set(uint32_t usage) {
1004 mUsage = usage;
1005}
1006
1007void Surface::BufferInfo::get(uint32_t *pWidth, uint32_t *pHeight,
1008 uint32_t *pFormat, uint32_t *pUsage) const {
1009 *pWidth = mWidth;
1010 *pHeight = mHeight;
1011 *pFormat = mFormat;
1012 *pUsage = mUsage;
1013}
1014
1015bool Surface::BufferInfo::validateBuffer(const sp<GraphicBuffer>& buffer) const {
1016 // make sure we AT LEAST have the usage flags we want
1017 if (mDirty || buffer==0 ||
1018 ((buffer->usage & mUsage) != mUsage)) {
1019 mDirty = 0;
1020 return false;
1021 }
1022 return true;
1023}
1024
1025// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026}; // namespace android
1027