blob: ebb0cc9b8a6e1f5b83bb1e451d26eeafe86fac9b [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>
Mathias Agopian35b48d12010-09-13 22:57:58 -070035#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <ui/Rect.h>
37
Mathias Agopian9cce3252010-02-09 17:46:37 -080038#include <surfaceflinger/Surface.h>
39#include <surfaceflinger/ISurface.h>
40#include <surfaceflinger/ISurfaceComposer.h>
41#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042
Mathias Agopian9cce3252010-02-09 17:46:37 -080043#include <private/surfaceflinger/SharedBufferStack.h>
44#include <private/surfaceflinger/LayerState.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046namespace android {
47
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048// ----------------------------------------------------------------------
49
Mathias Agopian14998592009-07-13 18:29:59 -070050static status_t copyBlt(
Mathias Agopian3330b202009-10-05 17:07:12 -070051 const sp<GraphicBuffer>& dst,
52 const sp<GraphicBuffer>& src,
Mathias Agopian0926f502009-05-04 14:17:04 -070053 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054{
Mathias Agopian245e4d72010-04-21 15:24:11 -070055 // src and dst with, height and format must be identical. no verification
56 // is done here.
Mathias Agopian14998592009-07-13 18:29:59 -070057 status_t err;
58 uint8_t const * src_bits = NULL;
59 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
60 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -070061
Mathias Agopian14998592009-07-13 18:29:59 -070062 uint8_t* dst_bits = NULL;
63 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
64 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
65
66 Region::const_iterator head(reg.begin());
67 Region::const_iterator tail(reg.end());
68 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 const size_t bpp = bytesPerPixel(src->format);
70 const size_t dbpr = dst->stride * bpp;
71 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -070072
Mathias Agopian14998592009-07-13 18:29:59 -070073 while (head != tail) {
74 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -070075 ssize_t h = r.height();
76 if (h <= 0) continue;
77 size_t size = r.width() * bpp;
78 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
79 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
80 if (dbpr==sbpr && size==sbpr) {
81 size *= h;
82 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 }
Mathias Agopian0926f502009-05-04 14:17:04 -070084 do {
85 memcpy(d, s, size);
86 d += dbpr;
87 s += sbpr;
88 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089 }
90 }
Mathias Agopian0926f502009-05-04 14:17:04 -070091
Mathias Agopian14998592009-07-13 18:29:59 -070092 if (src_bits)
93 src->unlock();
94
95 if (dst_bits)
96 dst->unlock();
97
98 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099}
100
Mathias Agopian62185b72009-04-16 16:19:50 -0700101// ============================================================================
102// SurfaceControl
103// ============================================================================
104
Mathias Agopian01b76682009-04-16 20:04:08 -0700105SurfaceControl::SurfaceControl(
106 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700107 const sp<ISurface>& surface,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700108 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700109 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700110 : mClient(client), mSurface(surface),
111 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700112 mWidth(data.width), mHeight(data.height), mFormat(data.format),
113 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700114{
115}
Mathias Agopian18d84462009-04-16 20:30:22 -0700116
Mathias Agopian62185b72009-04-16 16:19:50 -0700117SurfaceControl::~SurfaceControl()
118{
119 destroy();
120}
121
122void SurfaceControl::destroy()
123{
Mathias Agopian18d84462009-04-16 20:30:22 -0700124 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700125 mClient->destroySurface(mToken);
126 }
127
128 // clear all references and trigger an IPC now, to make sure things
129 // happen without delay, since these resources are quite heavy.
130 mClient.clear();
131 mSurface.clear();
132 IPCThreadState::self()->flushCommands();
133}
134
135void SurfaceControl::clear()
136{
137 // here, the window manager tells us explicitly that we should destroy
138 // the surface's resource. Soon after this call, it will also release
139 // its last reference (which will call the dtor); however, it is possible
140 // that a client living in the same process still holds references which
141 // would delay the call to the dtor -- that is why we need this explicit
142 // "clear()" call.
143 destroy();
144}
145
Mathias Agopian62185b72009-04-16 16:19:50 -0700146bool SurfaceControl::isSameSurface(
147 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
148{
149 if (lhs == 0 || rhs == 0)
150 return false;
151 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
152}
153
Mathias Agopian01b76682009-04-16 20:04:08 -0700154status_t SurfaceControl::setLayer(int32_t layer) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700157 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700158 return client->setLayer(mToken, layer);
159}
160status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700163 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700164 return client->setPosition(mToken, x, y);
165}
166status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700169 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700170 return client->setSize(mToken, w, h);
171}
172status_t SurfaceControl::hide() {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700175 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700176 return client->hide(mToken);
177}
178status_t SurfaceControl::show(int32_t layer) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700181 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700182 return client->show(mToken, layer);
183}
184status_t SurfaceControl::freeze() {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700187 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700188 return client->freeze(mToken);
189}
190status_t SurfaceControl::unfreeze() {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700193 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700194 return client->unfreeze(mToken);
195}
196status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700199 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700200 return client->setFlags(mToken, flags, mask);
201}
202status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700205 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700206 return client->setTransparentRegionHint(mToken, transparent);
207}
208status_t SurfaceControl::setAlpha(float alpha) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700211 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700212 return client->setAlpha(mToken, alpha);
213}
214status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700217 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700218 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
219}
220status_t SurfaceControl::setFreezeTint(uint32_t tint) {
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;
Mathias Agopian631f3582010-05-25 17:51:34 -0700223 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700224 return client->setFreezeTint(mToken, tint);
225}
Mathias Agopian62185b72009-04-16 16:19:50 -0700226
Mathias Agopian963abad2009-11-13 15:26:29 -0800227status_t SurfaceControl::validate() const
Mathias Agopian62185b72009-04-16 16:19:50 -0700228{
229 if (mToken<0 || mClient==0) {
230 LOGE("invalid token (%d, identity=%u) or client (%p)",
231 mToken, mIdentity, mClient.get());
232 return NO_INIT;
233 }
Mathias Agopian62185b72009-04-16 16:19:50 -0700234 return NO_ERROR;
235}
236
Mathias Agopian01b76682009-04-16 20:04:08 -0700237status_t SurfaceControl::writeSurfaceToParcel(
238 const sp<SurfaceControl>& control, Parcel* parcel)
239{
Mathias Agopian579b3f82010-06-08 19:54:15 -0700240 sp<ISurface> sur;
Mathias Agopian01b76682009-04-16 20:04:08 -0700241 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700242 uint32_t width = 0;
243 uint32_t height = 0;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700244 uint32_t format = 0;
245 uint32_t flags = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700246 if (SurfaceControl::isValid(control)) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700247 sur = control->mSurface;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700248 identity = control->mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700249 width = control->mWidth;
250 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700251 format = control->mFormat;
252 flags = control->mFlags;
253 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700254 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
Mathias Agopian01b76682009-04-16 20:04:08 -0700255 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700256 parcel->writeInt32(width);
257 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700258 parcel->writeInt32(format);
259 parcel->writeInt32(flags);
260 return NO_ERROR;
261}
262
263sp<Surface> SurfaceControl::getSurface() const
264{
265 Mutex::Autolock _l(mLock);
266 if (mSurfaceData == 0) {
267 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
268 }
269 return mSurfaceData;
270}
271
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272// ============================================================================
273// Surface
274// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700276class SurfaceClient : public Singleton<SurfaceClient>
277{
278 // all these attributes are constants
279 sp<ISurfaceComposer> mComposerService;
280 sp<ISurfaceComposerClient> mClient;
281 status_t mStatus;
282 SharedClient* mControl;
283 sp<IMemoryHeap> mControlMemory;
284
285 SurfaceClient()
286 : Singleton<SurfaceClient>(), mStatus(NO_INIT)
287 {
288 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
289 mComposerService = sf;
290 mClient = sf->createClientConnection();
291 if (mClient != NULL) {
292 mControlMemory = mClient->getControlBlock();
293 if (mControlMemory != NULL) {
294 mControl = static_cast<SharedClient *>(
295 mControlMemory->getBase());
296 if (mControl) {
297 mStatus = NO_ERROR;
298 }
299 }
300 }
301 }
302 friend class Singleton<SurfaceClient>;
303public:
304 status_t initCheck() const {
305 return mStatus;
306 }
307 SharedClient* getSharedClient() const {
308 return mControl;
309 }
310 ssize_t getTokenForSurface(const sp<ISurface>& sur) const {
311 // TODO: we could cache a few tokens here to avoid an IPC
312 return mClient->getTokenForSurface(sur);
313 }
314 void signalServer() const {
315 mComposerService->signal();
316 }
317};
318
319ANDROID_SINGLETON_STATIC_INSTANCE(SurfaceClient);
320
321// ---------------------------------------------------------------------------
322
Mathias Agopian01b76682009-04-16 20:04:08 -0700323Surface::Surface(const sp<SurfaceControl>& surface)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700324 : mBufferMapper(GraphicBufferMapper::get()),
325 mClient(SurfaceClient::getInstance()),
326 mSharedBufferClient(NULL),
Mathias Agopian631f3582010-05-25 17:51:34 -0700327 mInitCheck(NO_INIT),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700328 mSurface(surface->mSurface),
329 mIdentity(surface->mIdentity),
330 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700331 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332{
Mathias Agopian01b76682009-04-16 20:04:08 -0700333 init();
334}
Mathias Agopian62185b72009-04-16 16:19:50 -0700335
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700336Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700337 : mBufferMapper(GraphicBufferMapper::get()),
338 mClient(SurfaceClient::getInstance()),
339 mSharedBufferClient(NULL),
340 mInitCheck(NO_INIT)
Mathias Agopian01b76682009-04-16 20:04:08 -0700341{
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700342 mSurface = interface_cast<ISurface>(ref);
Mathias Agopian01b76682009-04-16 20:04:08 -0700343 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700344 mWidth = parcel.readInt32();
345 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700346 mFormat = parcel.readInt32();
347 mFlags = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700348 init();
349}
350
Mathias Agopian579b3f82010-06-08 19:54:15 -0700351status_t Surface::writeToParcel(
352 const sp<Surface>& surface, Parcel* parcel)
353{
354 sp<ISurface> sur;
355 uint32_t identity = 0;
356 uint32_t width = 0;
357 uint32_t height = 0;
358 uint32_t format = 0;
359 uint32_t flags = 0;
360 if (Surface::isValid(surface)) {
361 sur = surface->mSurface;
362 identity = surface->mIdentity;
363 width = surface->mWidth;
364 height = surface->mHeight;
365 format = surface->mFormat;
366 flags = surface->mFlags;
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700367 } else if (surface != 0 && surface->mSurface != 0) {
368 LOGW("Parceling invalid surface with non-NULL ISurface as NULL: "
369 "mSurface = %p, mIdentity = %d, mWidth = %d, mHeight = %d, "
370 "mFormat = %d, mFlags = 0x%08x, mInitCheck = %d",
371 surface->mSurface.get(), surface->mIdentity, surface->mWidth,
372 surface->mHeight, surface->mFormat, surface->mFlags,
373 surface->mInitCheck);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700374 }
375 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
376 parcel->writeInt32(identity);
377 parcel->writeInt32(width);
378 parcel->writeInt32(height);
379 parcel->writeInt32(format);
380 parcel->writeInt32(flags);
381 return NO_ERROR;
382
383}
384
Jamie Gennisaca4e222010-07-15 17:29:15 -0700385
386Mutex Surface::sCachedSurfacesLock;
387DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces(wp<Surface>(0));
388
389sp<Surface> Surface::readFromParcel(const Parcel& data) {
390 Mutex::Autolock _l(sCachedSurfacesLock);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700391 sp<IBinder> binder(data.readStrongBinder());
Jamie Gennisaca4e222010-07-15 17:29:15 -0700392 sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
393 if (surface == 0) {
394 surface = new Surface(data, binder);
395 sCachedSurfaces.add(binder, surface);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700396 }
Jamie Gennisaca4e222010-07-15 17:29:15 -0700397 if (surface->mSurface == 0) {
398 surface = 0;
399 }
400 cleanCachedSurfaces();
401 return surface;
402}
403
404// Remove the stale entries from the surface cache. This should only be called
405// with sCachedSurfacesLock held.
406void Surface::cleanCachedSurfaces() {
407 for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
408 wp<Surface> s(sCachedSurfaces.valueAt(i));
409 if (s == 0 || s.promote() == 0) {
410 sCachedSurfaces.removeItemsAt(i);
411 }
412 }
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700413}
414
Mathias Agopian01b76682009-04-16 20:04:08 -0700415void Surface::init()
416{
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700417 ANativeWindow::setSwapInterval = setSwapInterval;
418 ANativeWindow::dequeueBuffer = dequeueBuffer;
Mathias Agopian19957552010-10-01 16:22:41 -0700419 ANativeWindow::cancelBuffer = cancelBuffer;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700420 ANativeWindow::lockBuffer = lockBuffer;
421 ANativeWindow::queueBuffer = queueBuffer;
422 ANativeWindow::query = query;
423 ANativeWindow::perform = perform;
Mathias Agopian631f3582010-05-25 17:51:34 -0700424
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700425 DisplayInfo dinfo;
426 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700427 const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
428 const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700429 // FIXME: set real values here
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700430 const_cast<int&>(ANativeWindow::minSwapInterval) = 1;
431 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
432 const_cast<uint32_t&>(ANativeWindow::flags) = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700433
Mathias Agopianb661d662010-08-19 17:01:19 -0700434 mNextBufferTransform = 0;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800435 mConnected = 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700436 mSwapRectangle.makeInvalid();
Mathias Agopianb661d662010-08-19 17:01:19 -0700437 mNextBufferCrop = Rect(0,0);
Mathias Agopiana138f892010-05-21 17:24:35 -0700438 // two buffers by default
439 mBuffers.setCapacity(2);
440 mBuffers.insertAt(0, 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700441
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700442 if (mSurface != 0 && mClient.initCheck() == NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700443 int32_t token = mClient.getTokenForSurface(mSurface);
444 if (token >= 0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700445 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian579b3f82010-06-08 19:54:15 -0700446 mClient.getSharedClient(), token, 2, mIdentity);
447 mInitCheck = mClient.getSharedClient()->validate(token);
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700448 } else {
449 LOGW("Not initializing the shared buffer client because token = %d",
450 token);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700451 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700452 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455Surface::~Surface()
456{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700457 // this is a client-side operation, the surface is destroyed, unmap
458 // its buffers in this process.
Mathias Agopiana138f892010-05-21 17:24:35 -0700459 size_t size = mBuffers.size();
460 for (size_t i=0 ; i<size ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700461 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700462 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700463 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700465
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700466 // clear all references and trigger an IPC now, to make sure things
467 // happen without delay, since these resources are quite heavy.
Mathias Agopiana138f892010-05-21 17:24:35 -0700468 mBuffers.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700470 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 IPCThreadState::self()->flushCommands();
472}
473
Mathias Agopian631f3582010-05-25 17:51:34 -0700474bool Surface::isValid() {
475 return mInitCheck == NO_ERROR;
476}
477
478status_t Surface::validate() const
479{
480 // check that we initialized ourself properly
481 if (mInitCheck != NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700482 LOGE("invalid token (identity=%u)", mIdentity);
Mathias Agopian631f3582010-05-25 17:51:34 -0700483 return mInitCheck;
484 }
485
486 // verify the identity of this surface
Mathias Agopian7e27f052010-05-28 14:22:23 -0700487 uint32_t identity = mSharedBufferClient->getIdentity();
Mathias Agopian631f3582010-05-25 17:51:34 -0700488
489 // this is a bit of a (temporary) special case, identity==0 means that
490 // no operation are allowed from the client (eg: dequeue/queue), this
491 // is used with PUSH_BUFFER surfaces for instance
492 if (identity == 0) {
493 LOGE("[Surface] invalid operation (identity=%u)", mIdentity);
494 return INVALID_OPERATION;
495 }
496
497 if (mIdentity != identity) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700498 LOGE("[Surface] using an invalid surface, "
Mathias Agopian631f3582010-05-25 17:51:34 -0700499 "identity=%u should be %d",
Mathias Agopian579b3f82010-06-08 19:54:15 -0700500 mIdentity, identity);
Mathias Agopian631f3582010-05-25 17:51:34 -0700501 return NO_INIT;
502 }
503
504 // check the surface didn't become invalid
Mathias Agopian7e27f052010-05-28 14:22:23 -0700505 status_t err = mSharedBufferClient->getStatus();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700506 if (err != NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700507 LOGE("surface (identity=%u) is invalid, err=%d (%s)",
508 mIdentity, err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700509 return err;
510 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700511
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700512 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513}
514
Mathias Agopian631f3582010-05-25 17:51:34 -0700515sp<ISurface> Surface::getISurface() const {
516 return mSurface;
517}
518
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700519// ----------------------------------------------------------------------------
520
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700521int Surface::setSwapInterval(ANativeWindow* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700522 return 0;
523}
524
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700525int Surface::dequeueBuffer(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->dequeueBuffer(buffer);
529}
530
Mathias Agopian19957552010-10-01 16:22:41 -0700531int Surface::cancelBuffer(ANativeWindow* window,
532 android_native_buffer_t* buffer) {
533 Surface* self = getSelf(window);
534 return self->cancelBuffer(buffer);
535}
536
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700537int Surface::lockBuffer(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700538 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539 Surface* self = getSelf(window);
540 return self->lockBuffer(buffer);
541}
542
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700543int Surface::queueBuffer(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700544 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545 Surface* self = getSelf(window);
546 return self->queueBuffer(buffer);
547}
548
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700549int Surface::query(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700550 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700551 Surface* self = getSelf(window);
552 return self->query(what, value);
553}
554
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700555int Surface::perform(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700556 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700557 va_list args;
558 va_start(args, operation);
559 Surface* self = getSelf(window);
560 int res = self->perform(operation, args);
561 va_end(args);
562 return res;
563}
564
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565// ----------------------------------------------------------------------------
566
Mathias Agopiana138f892010-05-21 17:24:35 -0700567bool Surface::needNewBuffer(int bufIdx,
568 uint32_t *pWidth, uint32_t *pHeight,
569 uint32_t *pFormat, uint32_t *pUsage) const
570{
571 Mutex::Autolock _l(mSurfaceLock);
572
573 // Always call needNewBuffer(), since it clears the needed buffers flags
574 bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx);
575 bool validBuffer = mBufferInfo.validateBuffer(mBuffers[bufIdx]);
576 bool newNeewBuffer = needNewBuffer || !validBuffer;
577 if (newNeewBuffer) {
578 mBufferInfo.get(pWidth, pHeight, pFormat, pUsage);
579 }
580 return newNeewBuffer;
581}
582
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700583int Surface::dequeueBuffer(android_native_buffer_t** buffer)
584{
Mathias Agopian963abad2009-11-13 15:26:29 -0800585 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 if (err != NO_ERROR)
587 return err;
588
Mathias Agopian35b48d12010-09-13 22:57:58 -0700589 GraphicLog& logger(GraphicLog::getInstance());
590 logger.log(GraphicLog::SF_APP_DEQUEUE_BEFORE, mIdentity, -1);
591
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700592 ssize_t bufIdx = mSharedBufferClient->dequeue();
Mathias Agopian35b48d12010-09-13 22:57:58 -0700593
594 logger.log(GraphicLog::SF_APP_DEQUEUE_AFTER, mIdentity, bufIdx);
595
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700596 if (bufIdx < 0) {
597 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
598 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700599 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700600
Mathias Agopiana138f892010-05-21 17:24:35 -0700601 // grow the buffer array if needed
602 const size_t size = mBuffers.size();
603 const size_t needed = bufIdx+1;
604 if (size < needed) {
605 mBuffers.insertAt(size, needed-size);
606 }
Mathias Agopian3a806952010-04-08 18:34:07 -0700607
Mathias Agopiana138f892010-05-21 17:24:35 -0700608 uint32_t w, h, format, usage;
609 if (needNewBuffer(bufIdx, &w, &h, &format, &usage)) {
610 err = getBufferLocked(bufIdx, w, h, format, usage);
611 LOGE_IF(err, "getBufferLocked(%ld, %u, %u, %u, %08x) failed (%s)",
612 bufIdx, w, h, format, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700613 if (err == NO_ERROR) {
614 // reset the width/height with the what we get from the buffer
Mathias Agopiana138f892010-05-21 17:24:35 -0700615 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian50517542009-08-19 17:10:18 -0700616 mWidth = uint32_t(backBuffer->width);
617 mHeight = uint32_t(backBuffer->height);
618 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700619 }
620
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700621 // if we still don't have a buffer here, we probably ran out of memory
Mathias Agopiana138f892010-05-21 17:24:35 -0700622 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700623 if (!err && backBuffer==0) {
624 err = NO_MEMORY;
625 }
626
Mathias Agopiancf81c842009-07-31 14:47:00 -0700627 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700628 mDirtyRegion.set(backBuffer->width, backBuffer->height);
629 *buffer = backBuffer.get();
630 } else {
631 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700632 }
Mathias Agopian50517542009-08-19 17:10:18 -0700633
Mathias Agopiancf81c842009-07-31 14:47:00 -0700634 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700635}
636
Mathias Agopian19957552010-10-01 16:22:41 -0700637int Surface::cancelBuffer(android_native_buffer_t* buffer)
638{
639 status_t err = validate();
640 switch (err) {
641 case NO_ERROR:
642 // no error, common case
643 break;
644 case INVALID_OPERATION:
645 // legitimate errors here
646 return err;
647 default:
648 // other errors happen because the surface is now invalid,
649 // for instance because it has been destroyed. In this case,
650 // we just fail silently (canceling a buffer is not technically
651 // an error at this point)
652 return NO_ERROR;
653 }
654
655 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
656
657 err = mSharedBufferClient->cancel(bufIdx);
658
659 LOGE_IF(err, "error canceling buffer %d (%s)", bufIdx, strerror(-err));
660 return err;
661}
662
663
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700664int Surface::lockBuffer(android_native_buffer_t* buffer)
665{
Mathias Agopian963abad2009-11-13 15:26:29 -0800666 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700667 if (err != NO_ERROR)
668 return err;
669
Mathias Agopianb2965332010-04-27 16:41:19 -0700670 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopian35b48d12010-09-13 22:57:58 -0700671
672 GraphicLog& logger(GraphicLog::getInstance());
673 logger.log(GraphicLog::SF_APP_LOCK_BEFORE, mIdentity, bufIdx);
674
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700675 err = mSharedBufferClient->lock(bufIdx);
Mathias Agopian35b48d12010-09-13 22:57:58 -0700676
677 logger.log(GraphicLog::SF_APP_LOCK_AFTER, mIdentity, bufIdx);
678
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700679 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
680 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700681}
682
683int Surface::queueBuffer(android_native_buffer_t* buffer)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700684{
Mathias Agopian963abad2009-11-13 15:26:29 -0800685 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700686 if (err != NO_ERROR)
687 return err;
688
Mathias Agopian0926f502009-05-04 14:17:04 -0700689 if (mSwapRectangle.isValid()) {
690 mDirtyRegion.set(mSwapRectangle);
691 }
692
Mathias Agopianb2965332010-04-27 16:41:19 -0700693 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopian35b48d12010-09-13 22:57:58 -0700694
695 GraphicLog::getInstance().log(GraphicLog::SF_APP_QUEUE, mIdentity, bufIdx);
696
Mathias Agopianb661d662010-08-19 17:01:19 -0700697 mSharedBufferClient->setTransform(bufIdx, mNextBufferTransform);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700698 mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700699 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
700 err = mSharedBufferClient->queue(bufIdx);
701 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700702
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700703 if (err == NO_ERROR) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700704 // TODO: can we avoid this IPC if we know there is one pending?
705 mClient.signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700706 }
707 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708}
709
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700710int Surface::query(int what, int* value)
711{
712 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700713 case NATIVE_WINDOW_WIDTH:
714 *value = int(mWidth);
715 return NO_ERROR;
716 case NATIVE_WINDOW_HEIGHT:
717 *value = int(mHeight);
718 return NO_ERROR;
719 case NATIVE_WINDOW_FORMAT:
720 *value = int(mFormat);
721 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700722 }
723 return BAD_VALUE;
724}
725
Mathias Agopian52212712009-08-11 22:34:02 -0700726int Surface::perform(int operation, va_list args)
727{
Mathias Agopiancc08e682010-04-15 18:48:26 -0700728 status_t err = validate();
729 if (err != NO_ERROR)
730 return err;
731
Mathias Agopian52212712009-08-11 22:34:02 -0700732 int res = NO_ERROR;
733 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800734 case NATIVE_WINDOW_SET_USAGE:
735 dispatch_setUsage( args );
736 break;
737 case NATIVE_WINDOW_CONNECT:
738 res = dispatch_connect( args );
739 break;
740 case NATIVE_WINDOW_DISCONNECT:
741 res = dispatch_disconnect( args );
742 break;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700743 case NATIVE_WINDOW_SET_CROP:
744 res = dispatch_crop( args );
745 break;
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700746 case NATIVE_WINDOW_SET_BUFFER_COUNT:
747 res = dispatch_set_buffer_count( args );
748 break;
Mathias Agopian38ece272010-05-26 21:31:09 -0700749 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
750 res = dispatch_set_buffers_geometry( args );
751 break;
Mathias Agopianb661d662010-08-19 17:01:19 -0700752 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
753 res = dispatch_set_buffers_transform( args );
754 break;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800755 default:
756 res = NAME_NOT_FOUND;
757 break;
Mathias Agopian52212712009-08-11 22:34:02 -0700758 }
759 return res;
760}
761
Mathias Agopian55fa2512010-03-11 15:06:54 -0800762void Surface::dispatch_setUsage(va_list args) {
763 int usage = va_arg(args, int);
764 setUsage( usage );
765}
766int Surface::dispatch_connect(va_list args) {
767 int api = va_arg(args, int);
768 return connect( api );
769}
770int Surface::dispatch_disconnect(va_list args) {
771 int api = va_arg(args, int);
772 return disconnect( api );
773}
Mathias Agopiancc08e682010-04-15 18:48:26 -0700774int Surface::dispatch_crop(va_list args) {
775 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
776 return crop( reinterpret_cast<Rect const*>(rect) );
777}
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700778int Surface::dispatch_set_buffer_count(va_list args) {
779 size_t bufferCount = va_arg(args, size_t);
780 return setBufferCount(bufferCount);
781}
Mathias Agopiana138f892010-05-21 17:24:35 -0700782int Surface::dispatch_set_buffers_geometry(va_list args) {
783 int w = va_arg(args, int);
784 int h = va_arg(args, int);
785 int f = va_arg(args, int);
786 return setBuffersGeometry(w, h, f);
787}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800788
Mathias Agopianb661d662010-08-19 17:01:19 -0700789int Surface::dispatch_set_buffers_transform(va_list args) {
790 int transform = va_arg(args, int);
791 return setBuffersTransform(transform);
792}
793
Mathias Agopianba5972f2009-08-14 18:52:17 -0700794void Surface::setUsage(uint32_t reqUsage)
795{
796 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiana138f892010-05-21 17:24:35 -0700797 mBufferInfo.set(reqUsage);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700798}
799
Mathias Agopian55fa2512010-03-11 15:06:54 -0800800int Surface::connect(int api)
801{
802 Mutex::Autolock _l(mSurfaceLock);
803 int err = NO_ERROR;
804 switch (api) {
805 case NATIVE_WINDOW_API_EGL:
806 if (mConnected) {
807 err = -EINVAL;
808 } else {
809 mConnected = api;
810 }
811 break;
812 default:
813 err = -EINVAL;
814 break;
815 }
816 return err;
817}
818
819int Surface::disconnect(int api)
820{
821 Mutex::Autolock _l(mSurfaceLock);
822 int err = NO_ERROR;
823 switch (api) {
824 case NATIVE_WINDOW_API_EGL:
825 if (mConnected == api) {
826 mConnected = 0;
827 } else {
828 err = -EINVAL;
829 }
830 break;
831 default:
832 err = -EINVAL;
833 break;
834 }
835 return err;
836}
837
Mathias Agopiancc08e682010-04-15 18:48:26 -0700838int Surface::crop(Rect const* rect)
839{
Mathias Agopianb661d662010-08-19 17:01:19 -0700840 // empty/invalid rects are not allowed
841 if (rect->isEmpty())
842 return BAD_VALUE;
843
Mathias Agopiancc08e682010-04-15 18:48:26 -0700844 Mutex::Autolock _l(mSurfaceLock);
845 // TODO: validate rect size
846 mNextBufferCrop = *rect;
847 return NO_ERROR;
848}
849
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700850int Surface::setBufferCount(int bufferCount)
851{
852 sp<ISurface> s(mSurface);
853 if (s == 0) return NO_INIT;
854
Mathias Agopianbb641242010-05-18 17:06:55 -0700855 class SetBufferCountIPC : public SharedBufferClient::SetBufferCountCallback {
856 sp<ISurface> surface;
857 virtual status_t operator()(int bufferCount) const {
858 return surface->setBufferCount(bufferCount);
859 }
860 public:
861 SetBufferCountIPC(const sp<ISurface>& surface) : surface(surface) { }
862 } ipc(s);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700863
Mathias Agopianbb641242010-05-18 17:06:55 -0700864 status_t err = mSharedBufferClient->setBufferCount(bufferCount, ipc);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700865 LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s",
866 bufferCount, strerror(-err));
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700867 return err;
868}
869
Mathias Agopiana138f892010-05-21 17:24:35 -0700870int Surface::setBuffersGeometry(int w, int h, int format)
871{
872 if (w<0 || h<0 || format<0)
873 return BAD_VALUE;
874
875 if ((w && !h) || (!w && h))
876 return BAD_VALUE;
877
878 Mutex::Autolock _l(mSurfaceLock);
879 mBufferInfo.set(w, h, format);
880 return NO_ERROR;
881}
882
Mathias Agopianb661d662010-08-19 17:01:19 -0700883int Surface::setBuffersTransform(int transform)
884{
885 Mutex::Autolock _l(mSurfaceLock);
886 mNextBufferTransform = transform;
887 return NO_ERROR;
888}
889
Mathias Agopiana138f892010-05-21 17:24:35 -0700890// ----------------------------------------------------------------------------
891
892int Surface::getConnectedApi() const
893{
894 Mutex::Autolock _l(mSurfaceLock);
895 return mConnected;
896}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800897
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700898// ----------------------------------------------------------------------------
899
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900status_t Surface::lock(SurfaceInfo* info, bool blocking) {
901 return Surface::lock(info, NULL, blocking);
902}
903
Mathias Agopian0926f502009-05-04 14:17:04 -0700904status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700905{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800906 if (getConnectedApi()) {
907 LOGE("Surface::lock(%p) failed. Already connected to another API",
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700908 (ANativeWindow*)this);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800909 CallStack stack;
910 stack.update();
911 stack.dump("");
912 return INVALID_OPERATION;
913 }
914
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700915 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800916 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700917 CallStack stack;
918 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800919 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700920 return WOULD_BLOCK;
921 }
Mathias Agopian90147262010-01-22 11:47:55 -0800922
923 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700924
Mathias Agopian90147262010-01-22 11:47:55 -0800925 if (mLockedBuffer != 0) {
926 LOGE("Surface::lock failed, already locked");
927 mApiLock.unlock();
928 return INVALID_OPERATION;
929 }
930
Mathias Agopian52212712009-08-11 22:34:02 -0700931 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700932 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
933
Mathias Agopianb2965332010-04-27 16:41:19 -0700934 android_native_buffer_t* out;
935 status_t err = dequeueBuffer(&out);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700936 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700937 if (err == NO_ERROR) {
Mathias Agopianb2965332010-04-27 16:41:19 -0700938 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700939 err = lockBuffer(backBuffer.get());
940 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -0700941 getBufferIndex(backBuffer), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700942 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700943 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700944 const Region boundsRegion(bounds);
945 Region scratch(boundsRegion);
Mathias Agopian0926f502009-05-04 14:17:04 -0700946 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700947 newDirtyRegion &= boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700948
Mathias Agopian245e4d72010-04-21 15:24:11 -0700949 // figure out if we can copy the frontbuffer back
Mathias Agopian3330b202009-10-05 17:07:12 -0700950 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700951 const bool canCopyBack = (frontBuffer != 0 &&
952 backBuffer->width == frontBuffer->width &&
953 backBuffer->height == frontBuffer->height &&
954 backBuffer->format == frontBuffer->format &&
955 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
956
957 // the dirty region we report to surfaceflinger is the one
958 // given by the user (as opposed to the one *we* return to the
959 // user).
960 mDirtyRegion = newDirtyRegion;
961
962 if (canCopyBack) {
963 // copy the area that is invalid and not repainted this round
964 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
965 if (!copyback.isEmpty())
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700966 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700967 } else {
968 // if we can't copy-back anything, modify the user's dirty
969 // region to make sure they redraw the whole buffer
970 newDirtyRegion = boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700971 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700972
Mathias Agopian245e4d72010-04-21 15:24:11 -0700973 // keep track of the are of the buffer that is "clean"
974 // (ie: that will be redrawn)
Mathias Agopian0926f502009-05-04 14:17:04 -0700975 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700976
Mathias Agopiane71212b2009-05-05 00:37:46 -0700977 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700978 status_t res = backBuffer->lock(
979 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700980 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700981
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700982 LOGW_IF(res, "failed locking buffer (handle = %p)",
983 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700984
985 mLockedBuffer = backBuffer;
986 other->w = backBuffer->width;
987 other->h = backBuffer->height;
988 other->s = backBuffer->stride;
989 other->usage = backBuffer->usage;
990 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700991 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700992 }
993 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700994 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700995 return err;
996}
997
998status_t Surface::unlockAndPost()
999{
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001000 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -08001001 LOGE("Surface::unlockAndPost failed, no locked buffer");
1002 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001003 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001004
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001005 status_t err = mLockedBuffer->unlock();
1006 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -07001007
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001008 err = queueBuffer(mLockedBuffer.get());
1009 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
Mathias Agopianb2965332010-04-27 16:41:19 -07001010 getBufferIndex(mLockedBuffer), strerror(-err));
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001011
1012 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001013 mLockedBuffer = 0;
1014 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015}
1016
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -07001018 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 mSwapRectangle = r;
1020}
1021
Mathias Agopianb2965332010-04-27 16:41:19 -07001022int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const
1023{
1024 return buffer->getIndex();
1025}
1026
Mathias Agopiana138f892010-05-21 17:24:35 -07001027status_t Surface::getBufferLocked(int index,
1028 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029{
Mathias Agopianba5972f2009-08-14 18:52:17 -07001030 sp<ISurface> s(mSurface);
1031 if (s == 0) return NO_INIT;
1032
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001033 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -07001034
1035 // free the current buffer
Mathias Agopiana138f892010-05-21 17:24:35 -07001036 sp<GraphicBuffer>& currentBuffer(mBuffers.editItemAt(index));
Mathias Agopian50517542009-08-19 17:10:18 -07001037 if (currentBuffer != 0) {
1038 getBufferMapper().unregisterBuffer(currentBuffer->handle);
1039 currentBuffer.clear();
1040 }
1041
Mathias Agopiana138f892010-05-21 17:24:35 -07001042 sp<GraphicBuffer> buffer = s->requestBuffer(index, w, h, format, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001043 LOGE_IF(buffer==0,
1044 "ISurface::getBuffer(%d, %08x) returned NULL",
1045 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -07001046 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001047 LOGE_IF(buffer->handle == NULL,
Mathias Agopiana138f892010-05-21 17:24:35 -07001048 "Surface (identity=%d) requestBuffer(%d, %u, %u, %u, %08x) "
1049 "returned a buffer with a null handle",
1050 mIdentity, index, w, h, format, usage);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001051 err = mSharedBufferClient->getStatus();
1052 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
1053 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -07001054 err = getBufferMapper().registerBuffer(buffer->handle);
1055 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
1056 err, strerror(-err));
1057 if (err == NO_ERROR) {
1058 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001059 currentBuffer->setIndex(index);
Mathias Agopian50517542009-08-19 17:10:18 -07001060 }
Mathias Agopiand3144be2009-10-06 15:58:44 -07001061 } else {
Mathias Agopianf10d7fd2010-05-21 14:19:50 -07001062 err = err<0 ? err : status_t(NO_MEMORY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 }
1064 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001065 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066}
1067
Mathias Agopiana138f892010-05-21 17:24:35 -07001068// ----------------------------------------------------------------------------
1069Surface::BufferInfo::BufferInfo()
1070 : mWidth(0), mHeight(0), mFormat(0),
1071 mUsage(GRALLOC_USAGE_HW_RENDER), mDirty(0)
1072{
1073}
1074
1075void Surface::BufferInfo::set(uint32_t w, uint32_t h, uint32_t format) {
1076 if ((mWidth != w) || (mHeight != h) || (mFormat != format)) {
1077 mWidth = w;
1078 mHeight = h;
1079 mFormat = format;
1080 mDirty |= GEOMETRY;
1081 }
1082}
1083
1084void Surface::BufferInfo::set(uint32_t usage) {
1085 mUsage = usage;
1086}
1087
1088void Surface::BufferInfo::get(uint32_t *pWidth, uint32_t *pHeight,
1089 uint32_t *pFormat, uint32_t *pUsage) const {
1090 *pWidth = mWidth;
1091 *pHeight = mHeight;
1092 *pFormat = mFormat;
1093 *pUsage = mUsage;
1094}
1095
1096bool Surface::BufferInfo::validateBuffer(const sp<GraphicBuffer>& buffer) const {
1097 // make sure we AT LEAST have the usage flags we want
1098 if (mDirty || buffer==0 ||
1099 ((buffer->usage & mUsage) != mUsage)) {
1100 mDirty = 0;
1101 return false;
1102 }
1103 return true;
1104}
1105
1106// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107}; // namespace android