blob: 0c5767b8c2e9067f993abc0d4b8e32778dd4e302 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080020#include <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/Errors.h>
25#include <utils/threads.h>
Mathias Agopian9779b222009-09-07 16:32:45 -070026#include <utils/CallStack.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080027#include <utils/Log.h>
28
Mathias Agopian07952722009-05-19 19:08:10 -070029#include <binder/IPCThreadState.h>
30#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Mathias Agopian1473f462009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
Mathias Agopian6950e422009-10-05 17:07:12 -070033#include <ui/GraphicBuffer.h>
34#include <ui/GraphicBufferMapper.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070035#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <ui/Rect.h>
37
Mathias Agopian000479f2010-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 Agopian1473f462009-04-10 14:24:30 -070042
Mathias Agopian000479f2010-02-09 17:46:37 -080043#include <private/surfaceflinger/SharedBufferStack.h>
44#include <private/surfaceflinger/LayerState.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046namespace android {
47
Mathias Agopian1473f462009-04-10 14:24:30 -070048// ----------------------------------------------------------------------
49
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070050static status_t copyBlt(
Mathias Agopian6950e422009-10-05 17:07:12 -070051 const sp<GraphicBuffer>& dst,
52 const sp<GraphicBuffer>& src,
Mathias Agopiandff8e582009-05-04 14:17:04 -070053 const Region& reg)
Mathias Agopian1473f462009-04-10 14:24:30 -070054{
Mathias Agopiana8a0aa82010-04-21 15:24:11 -070055 // src and dst with, height and format must be identical. no verification
56 // is done here.
Mathias Agopian8d9a5eff2009-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 Agopiandff8e582009-05-04 14:17:04 -070061
Mathias Agopian8d9a5eff2009-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 Agopian1473f462009-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 Agopiandff8e582009-05-04 14:17:04 -070072
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070073 while (head != tail) {
74 const Rect& r(*head++);
Mathias Agopiandff8e582009-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 Agopian1473f462009-04-10 14:24:30 -070083 }
Mathias Agopiandff8e582009-05-04 14:17:04 -070084 do {
85 memcpy(d, s, size);
86 d += dbpr;
87 s += sbpr;
88 } while (--h > 0);
Mathias Agopian1473f462009-04-10 14:24:30 -070089 }
90 }
Mathias Agopiandff8e582009-05-04 14:17:04 -070091
Mathias Agopian8d9a5eff2009-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 Agopian1473f462009-04-10 14:24:30 -070099}
100
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700101// ============================================================================
102// SurfaceControl
103// ============================================================================
104
Mathias Agopian17f638b2009-04-16 20:04:08 -0700105SurfaceControl::SurfaceControl(
106 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700107 const sp<ISurface>& surface,
Mathias Agopian770492c2010-05-28 14:22:23 -0700108 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700109 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700110 : mClient(client), mSurface(surface),
111 mToken(data.token), mIdentity(data.identity),
Mathias Agopian18b6b492009-08-19 17:46:26 -0700112 mWidth(data.width), mHeight(data.height), mFormat(data.format),
113 mFlags(flags)
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700114{
115}
Mathias Agopian69d62092009-04-16 20:30:22 -0700116
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700117SurfaceControl::~SurfaceControl()
118{
119 destroy();
120}
121
122void SurfaceControl::destroy()
123{
Mathias Agopian69d62092009-04-16 20:30:22 -0700124 if (isValid()) {
Mathias Agopian6d2c0bc2009-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 Agopian6d2c0bc2009-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 Agopian17f638b2009-04-16 20:04:08 -0700154status_t SurfaceControl::setLayer(int32_t layer) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800155 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700156 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700157 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700158 return client->setLayer(mToken, layer);
159}
160status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800161 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700162 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700163 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700164 return client->setPosition(mToken, x, y);
165}
166status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800167 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700168 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700169 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700170 return client->setSize(mToken, w, h);
171}
172status_t SurfaceControl::hide() {
Mathias Agopian18e02602009-11-13 15:26:29 -0800173 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700174 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700175 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700176 return client->hide(mToken);
177}
178status_t SurfaceControl::show(int32_t layer) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800179 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700180 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700181 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700182 return client->show(mToken, layer);
183}
184status_t SurfaceControl::freeze() {
Mathias Agopian18e02602009-11-13 15:26:29 -0800185 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700186 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700187 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700188 return client->freeze(mToken);
189}
190status_t SurfaceControl::unfreeze() {
Mathias Agopian18e02602009-11-13 15:26:29 -0800191 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700192 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700193 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700194 return client->unfreeze(mToken);
195}
196status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800197 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700198 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700199 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700200 return client->setFlags(mToken, flags, mask);
201}
202status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800203 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700204 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700205 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700206 return client->setTransparentRegionHint(mToken, transparent);
207}
208status_t SurfaceControl::setAlpha(float alpha) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800209 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700210 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700211 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-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 Agopian18e02602009-11-13 15:26:29 -0800215 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700216 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700217 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700218 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
219}
220status_t SurfaceControl::setFreezeTint(uint32_t tint) {
Mathias Agopian18e02602009-11-13 15:26:29 -0800221 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700222 if (err < 0) return err;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700223 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700224 return client->setFreezeTint(mToken, tint);
225}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700226
Mathias Agopian18e02602009-11-13 15:26:29 -0800227status_t SurfaceControl::validate() const
Mathias Agopian6d2c0bc2009-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 Agopian6d2c0bc2009-04-16 16:19:50 -0700234 return NO_ERROR;
235}
236
Mathias Agopian17f638b2009-04-16 20:04:08 -0700237status_t SurfaceControl::writeSurfaceToParcel(
238 const sp<SurfaceControl>& control, Parcel* parcel)
239{
Mathias Agopian5e140102010-06-08 19:54:15 -0700240 sp<ISurface> sur;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700241 uint32_t identity = 0;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700242 uint32_t width = 0;
243 uint32_t height = 0;
Mathias Agopian5e140102010-06-08 19:54:15 -0700244 uint32_t format = 0;
245 uint32_t flags = 0;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700246 if (SurfaceControl::isValid(control)) {
Mathias Agopian17f638b2009-04-16 20:04:08 -0700247 sur = control->mSurface;
Mathias Agopian5e140102010-06-08 19:54:15 -0700248 identity = control->mIdentity;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700249 width = control->mWidth;
250 height = control->mHeight;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700251 format = control->mFormat;
252 flags = control->mFlags;
253 }
Mathias Agopian7623da42010-06-01 15:12:58 -0700254 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700255 parcel->writeInt32(identity);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700256 parcel->writeInt32(width);
257 parcel->writeInt32(height);
Mathias Agopian17f638b2009-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 Agopian1473f462009-04-10 14:24:30 -0700272// ============================================================================
273// Surface
274// ============================================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
Mathias Agopian7623da42010-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 Agopian17f638b2009-04-16 20:04:08 -0700323Surface::Surface(const sp<SurfaceControl>& surface)
Mathias Agopian7623da42010-06-01 15:12:58 -0700324 : mBufferMapper(GraphicBufferMapper::get()),
325 mClient(SurfaceClient::getInstance()),
326 mSharedBufferClient(NULL),
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700327 mInitCheck(NO_INIT),
Mathias Agopian7623da42010-06-01 15:12:58 -0700328 mSurface(surface->mSurface),
329 mIdentity(surface->mIdentity),
330 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian321abdb2009-08-14 18:52:17 -0700331 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332{
Mathias Agopian17f638b2009-04-16 20:04:08 -0700333 init();
334}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700335
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700336Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
Mathias Agopian7623da42010-06-01 15:12:58 -0700337 : mBufferMapper(GraphicBufferMapper::get()),
338 mClient(SurfaceClient::getInstance()),
339 mSharedBufferClient(NULL),
340 mInitCheck(NO_INIT)
Mathias Agopian17f638b2009-04-16 20:04:08 -0700341{
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700342 mSurface = interface_cast<ISurface>(ref);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700343 mIdentity = parcel.readInt32();
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700344 mWidth = parcel.readInt32();
345 mHeight = parcel.readInt32();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700346 mFormat = parcel.readInt32();
347 mFlags = parcel.readInt32();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700348 init();
349}
350
Mathias Agopian5e140102010-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 Gennis85cfdd02010-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 Agopian5e140102010-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 Gennis5ee65f02010-07-15 17:29:15 -0700385
386Mutex Surface::sCachedSurfacesLock;
Mathias Agopian06f9ebf2010-12-13 16:47:31 -0800387DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700388
389sp<Surface> Surface::readFromParcel(const Parcel& data) {
390 Mutex::Autolock _l(sCachedSurfacesLock);
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700391 sp<IBinder> binder(data.readStrongBinder());
Jamie Gennis5ee65f02010-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 Agopianfae5cb22010-06-04 18:26:32 -0700396 }
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700397 if (surface->mSurface == 0) {
398 surface = 0;
399 }
Mathias Agopian06f9ebf2010-12-13 16:47:31 -0800400 cleanCachedSurfacesLocked();
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700401 return surface;
402}
403
404// Remove the stale entries from the surface cache. This should only be called
405// with sCachedSurfacesLock held.
Mathias Agopian06f9ebf2010-12-13 16:47:31 -0800406void Surface::cleanCachedSurfacesLocked() {
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700407 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 Agopianfae5cb22010-06-04 18:26:32 -0700413}
414
Mathias Agopian17f638b2009-04-16 20:04:08 -0700415void Surface::init()
416{
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700417 ANativeWindow::setSwapInterval = setSwapInterval;
418 ANativeWindow::dequeueBuffer = dequeueBuffer;
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700419 ANativeWindow::cancelBuffer = cancelBuffer;
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700420 ANativeWindow::lockBuffer = lockBuffer;
421 ANativeWindow::queueBuffer = queueBuffer;
422 ANativeWindow::query = query;
423 ANativeWindow::perform = perform;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700424
Mathias Agopian1473f462009-04-10 14:24:30 -0700425 DisplayInfo dinfo;
426 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700427 const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
428 const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
Mathias Agopian1473f462009-04-10 14:24:30 -0700429 // FIXME: set real values here
Dianne Hackborn8b49bd12010-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 Agopian2ce19af2010-05-25 17:51:34 -0700433
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700434 mNextBufferTransform = 0;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800435 mConnected = 0;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700436 mSwapRectangle.makeInvalid();
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700437 mNextBufferCrop = Rect(0,0);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700438 // two buffers by default
439 mBuffers.setCapacity(2);
440 mBuffers.insertAt(0, 2);
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700441
Mathias Agopian7623da42010-06-01 15:12:58 -0700442 if (mSurface != 0 && mClient.initCheck() == NO_ERROR) {
Mathias Agopian5e140102010-06-08 19:54:15 -0700443 int32_t token = mClient.getTokenForSurface(mSurface);
444 if (token >= 0) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700445 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian5e140102010-06-08 19:54:15 -0700446 mClient.getSharedClient(), token, 2, mIdentity);
447 mInitCheck = mClient.getSharedClient()->validate(token);
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700448 } else {
449 LOGW("Not initializing the shared buffer client because token = %d",
450 token);
Mathias Agopian7623da42010-06-01 15:12:58 -0700451 }
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453}
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455Surface::~Surface()
456{
Mathias Agopian402c3462009-04-14 18:21:47 -0700457 // clear all references and trigger an IPC now, to make sure things
458 // happen without delay, since these resources are quite heavy.
Mathias Agopian2be352a2010-05-21 17:24:35 -0700459 mBuffers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 mSurface.clear();
Mathias Agopian9779b222009-09-07 16:32:45 -0700461 delete mSharedBufferClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 IPCThreadState::self()->flushCommands();
463}
464
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700465bool Surface::isValid() {
466 return mInitCheck == NO_ERROR;
467}
468
Mathias Agopianafc724b2011-01-14 11:04:34 -0800469status_t Surface::validate(bool inCancelBuffer) const
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700470{
471 // check that we initialized ourself properly
472 if (mInitCheck != NO_ERROR) {
Mathias Agopian5e140102010-06-08 19:54:15 -0700473 LOGE("invalid token (identity=%u)", mIdentity);
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700474 return mInitCheck;
475 }
476
477 // verify the identity of this surface
Mathias Agopian770492c2010-05-28 14:22:23 -0700478 uint32_t identity = mSharedBufferClient->getIdentity();
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700479 if (mIdentity != identity) {
Mathias Agopian5e140102010-06-08 19:54:15 -0700480 LOGE("[Surface] using an invalid surface, "
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700481 "identity=%u should be %d",
Mathias Agopian5e140102010-06-08 19:54:15 -0700482 mIdentity, identity);
Mathias Agopianf7d63cf2010-12-10 15:37:48 -0800483 CallStack stack;
484 stack.update();
485 stack.dump("Surface");
Mathias Agopianafc724b2011-01-14 11:04:34 -0800486 return BAD_INDEX;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700487 }
488
489 // check the surface didn't become invalid
Mathias Agopian770492c2010-05-28 14:22:23 -0700490 status_t err = mSharedBufferClient->getStatus();
Mathias Agopian1473f462009-04-10 14:24:30 -0700491 if (err != NO_ERROR) {
Mathias Agopianafc724b2011-01-14 11:04:34 -0800492 if (!inCancelBuffer) {
493 LOGE("surface (identity=%u) is invalid, err=%d (%s)",
494 mIdentity, err, strerror(-err));
495 CallStack stack;
496 stack.update();
497 stack.dump("Surface");
498 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700499 return err;
500 }
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700501
Mathias Agopian1473f462009-04-10 14:24:30 -0700502 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503}
504
Mathias Agopianc8a04b52011-04-05 15:44:20 -0700505sp<IBinder> Surface::asBinder() const {
506 return mSurface!=0 ? mSurface->asBinder() : 0;
Mathias Agopian2ce19af2010-05-25 17:51:34 -0700507}
508
Mathias Agopian1473f462009-04-10 14:24:30 -0700509// ----------------------------------------------------------------------------
510
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700511int Surface::setSwapInterval(ANativeWindow* window, int interval) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700512 return 0;
513}
514
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700515int Surface::dequeueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700516 ANativeWindowBuffer** buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700517 Surface* self = getSelf(window);
518 return self->dequeueBuffer(buffer);
519}
520
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700521int Surface::cancelBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700522 ANativeWindowBuffer* buffer) {
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700523 Surface* self = getSelf(window);
524 return self->cancelBuffer(buffer);
525}
526
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700527int Surface::lockBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700528 ANativeWindowBuffer* buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700529 Surface* self = getSelf(window);
530 return self->lockBuffer(buffer);
531}
532
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700533int Surface::queueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700534 ANativeWindowBuffer* buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700535 Surface* self = getSelf(window);
536 return self->queueBuffer(buffer);
537}
538
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700539int Surface::query(const ANativeWindow* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700540 int what, int* value) {
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700541 const Surface* self = getSelf(window);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700542 return self->query(what, value);
543}
544
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700545int Surface::perform(ANativeWindow* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700546 int operation, ...) {
Mathias Agopian5cec4742009-08-11 22:34:02 -0700547 va_list args;
548 va_start(args, operation);
549 Surface* self = getSelf(window);
550 int res = self->perform(operation, args);
551 va_end(args);
552 return res;
553}
554
Mathias Agopian1473f462009-04-10 14:24:30 -0700555// ----------------------------------------------------------------------------
556
Mathias Agopian2be352a2010-05-21 17:24:35 -0700557bool Surface::needNewBuffer(int bufIdx,
558 uint32_t *pWidth, uint32_t *pHeight,
559 uint32_t *pFormat, uint32_t *pUsage) const
560{
561 Mutex::Autolock _l(mSurfaceLock);
562
563 // Always call needNewBuffer(), since it clears the needed buffers flags
564 bool needNewBuffer = mSharedBufferClient->needNewBuffer(bufIdx);
565 bool validBuffer = mBufferInfo.validateBuffer(mBuffers[bufIdx]);
566 bool newNeewBuffer = needNewBuffer || !validBuffer;
567 if (newNeewBuffer) {
568 mBufferInfo.get(pWidth, pHeight, pFormat, pUsage);
569 }
570 return newNeewBuffer;
571}
572
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700573int Surface::dequeueBuffer(ANativeWindowBuffer** buffer)
Mathias Agopian1473f462009-04-10 14:24:30 -0700574{
Mathias Agopian18e02602009-11-13 15:26:29 -0800575 status_t err = validate();
Mathias Agopian1473f462009-04-10 14:24:30 -0700576 if (err != NO_ERROR)
577 return err;
578
Mathias Agopian04262e92010-09-13 22:57:58 -0700579 GraphicLog& logger(GraphicLog::getInstance());
580 logger.log(GraphicLog::SF_APP_DEQUEUE_BEFORE, mIdentity, -1);
581
Mathias Agopian9779b222009-09-07 16:32:45 -0700582 ssize_t bufIdx = mSharedBufferClient->dequeue();
Mathias Agopian04262e92010-09-13 22:57:58 -0700583
584 logger.log(GraphicLog::SF_APP_DEQUEUE_AFTER, mIdentity, bufIdx);
585
Mathias Agopian9779b222009-09-07 16:32:45 -0700586 if (bufIdx < 0) {
587 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
588 return bufIdx;
Mathias Agopian9e2be202009-08-21 15:44:17 -0700589 }
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700590
Mathias Agopian2be352a2010-05-21 17:24:35 -0700591 // grow the buffer array if needed
592 const size_t size = mBuffers.size();
593 const size_t needed = bufIdx+1;
594 if (size < needed) {
595 mBuffers.insertAt(size, needed-size);
596 }
Mathias Agopiandb3647f2010-04-08 18:34:07 -0700597
Mathias Agopian2be352a2010-05-21 17:24:35 -0700598 uint32_t w, h, format, usage;
599 if (needNewBuffer(bufIdx, &w, &h, &format, &usage)) {
600 err = getBufferLocked(bufIdx, w, h, format, usage);
601 LOGE_IF(err, "getBufferLocked(%ld, %u, %u, %u, %08x) failed (%s)",
602 bufIdx, w, h, format, usage, strerror(-err));
Mathias Agopianb2f84502009-08-19 17:10:18 -0700603 if (err == NO_ERROR) {
604 // reset the width/height with the what we get from the buffer
Mathias Agopian2be352a2010-05-21 17:24:35 -0700605 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700606 mWidth = uint32_t(backBuffer->width);
607 mHeight = uint32_t(backBuffer->height);
608 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700609 }
610
Mathias Agopian9779b222009-09-07 16:32:45 -0700611 // if we still don't have a buffer here, we probably ran out of memory
Mathias Agopian2be352a2010-05-21 17:24:35 -0700612 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian9779b222009-09-07 16:32:45 -0700613 if (!err && backBuffer==0) {
614 err = NO_MEMORY;
615 }
616
Mathias Agopianabac0102009-07-31 14:47:00 -0700617 if (err == NO_ERROR) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700618 mDirtyRegion.set(backBuffer->width, backBuffer->height);
619 *buffer = backBuffer.get();
620 } else {
621 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopianabac0102009-07-31 14:47:00 -0700622 }
Mathias Agopianb2f84502009-08-19 17:10:18 -0700623
Mathias Agopianabac0102009-07-31 14:47:00 -0700624 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700625}
626
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700627int Surface::cancelBuffer(ANativeWindowBuffer* buffer)
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700628{
Mathias Agopianafc724b2011-01-14 11:04:34 -0800629 status_t err = validate(true);
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700630 switch (err) {
631 case NO_ERROR:
632 // no error, common case
633 break;
Mathias Agopianafc724b2011-01-14 11:04:34 -0800634 case BAD_INDEX:
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700635 // legitimate errors here
636 return err;
637 default:
638 // other errors happen because the surface is now invalid,
639 // for instance because it has been destroyed. In this case,
640 // we just fail silently (canceling a buffer is not technically
641 // an error at this point)
642 return NO_ERROR;
643 }
644
645 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
646
647 err = mSharedBufferClient->cancel(bufIdx);
648
649 LOGE_IF(err, "error canceling buffer %d (%s)", bufIdx, strerror(-err));
650 return err;
651}
652
653
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700654int Surface::lockBuffer(ANativeWindowBuffer* buffer)
Mathias Agopian1473f462009-04-10 14:24:30 -0700655{
Mathias Agopian18e02602009-11-13 15:26:29 -0800656 status_t err = validate();
Mathias Agopian402c3462009-04-14 18:21:47 -0700657 if (err != NO_ERROR)
658 return err;
659
Mathias Agopianf590f702010-04-27 16:41:19 -0700660 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopian04262e92010-09-13 22:57:58 -0700661
662 GraphicLog& logger(GraphicLog::getInstance());
663 logger.log(GraphicLog::SF_APP_LOCK_BEFORE, mIdentity, bufIdx);
664
Mathias Agopian9779b222009-09-07 16:32:45 -0700665 err = mSharedBufferClient->lock(bufIdx);
Mathias Agopian04262e92010-09-13 22:57:58 -0700666
667 logger.log(GraphicLog::SF_APP_LOCK_AFTER, mIdentity, bufIdx);
668
Mathias Agopian9779b222009-09-07 16:32:45 -0700669 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
670 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700671}
672
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700673int Surface::queueBuffer(ANativeWindowBuffer* buffer)
Mathias Agopian04262e92010-09-13 22:57:58 -0700674{
Mathias Agopian18e02602009-11-13 15:26:29 -0800675 status_t err = validate();
Mathias Agopian1473f462009-04-10 14:24:30 -0700676 if (err != NO_ERROR)
677 return err;
678
Mathias Agopiandff8e582009-05-04 14:17:04 -0700679 if (mSwapRectangle.isValid()) {
680 mDirtyRegion.set(mSwapRectangle);
681 }
682
Mathias Agopianf590f702010-04-27 16:41:19 -0700683 int32_t bufIdx = getBufferIndex(GraphicBuffer::getSelf(buffer));
Mathias Agopian04262e92010-09-13 22:57:58 -0700684
685 GraphicLog::getInstance().log(GraphicLog::SF_APP_QUEUE, mIdentity, bufIdx);
686
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700687 mSharedBufferClient->setTransform(bufIdx, mNextBufferTransform);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700688 mSharedBufferClient->setCrop(bufIdx, mNextBufferCrop);
Mathias Agopian9779b222009-09-07 16:32:45 -0700689 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
690 err = mSharedBufferClient->queue(bufIdx);
691 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700692
Mathias Agopian9779b222009-09-07 16:32:45 -0700693 if (err == NO_ERROR) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700694 // TODO: can we avoid this IPC if we know there is one pending?
695 mClient.signalServer();
Mathias Agopian9779b222009-09-07 16:32:45 -0700696 }
697 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700698}
699
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700700int Surface::query(int what, int* value) const
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700701{
702 switch (what) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700703 case NATIVE_WINDOW_WIDTH:
704 *value = int(mWidth);
705 return NO_ERROR;
706 case NATIVE_WINDOW_HEIGHT:
707 *value = int(mHeight);
708 return NO_ERROR;
709 case NATIVE_WINDOW_FORMAT:
710 *value = int(mFormat);
711 return NO_ERROR;
Jamie Gennis96dcc972011-02-27 14:10:20 -0800712 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
713 *value = MIN_UNDEQUEUED_BUFFERS;
714 return NO_ERROR;
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700715 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800716 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
717 *value = sf->authenticateSurface(mSurface) ? 1 : 0;
718 return NO_ERROR;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700719 }
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700720 case NATIVE_WINDOW_CONCRETE_TYPE:
721 *value = NATIVE_WINDOW_SURFACE;
722 return NO_ERROR;
723 }
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700724 return BAD_VALUE;
725}
726
Mathias Agopian5cec4742009-08-11 22:34:02 -0700727int Surface::perform(int operation, va_list args)
728{
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700729 status_t err = validate();
730 if (err != NO_ERROR)
731 return err;
732
Mathias Agopian5cec4742009-08-11 22:34:02 -0700733 int res = NO_ERROR;
734 switch (operation) {
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800735 case NATIVE_WINDOW_SET_USAGE:
736 dispatch_setUsage( args );
737 break;
738 case NATIVE_WINDOW_CONNECT:
739 res = dispatch_connect( args );
740 break;
741 case NATIVE_WINDOW_DISCONNECT:
742 res = dispatch_disconnect( args );
743 break;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700744 case NATIVE_WINDOW_SET_CROP:
745 res = dispatch_crop( args );
746 break;
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700747 case NATIVE_WINDOW_SET_BUFFER_COUNT:
748 res = dispatch_set_buffer_count( args );
749 break;
Mathias Agopian663badd2010-05-26 21:31:09 -0700750 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
751 res = dispatch_set_buffers_geometry( args );
752 break;
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700753 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
754 res = dispatch_set_buffers_transform( args );
755 break;
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800756 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
757 res = dispatch_set_buffers_timestamp( args );
758 break;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800759 default:
760 res = NAME_NOT_FOUND;
761 break;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700762 }
763 return res;
764}
765
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800766void Surface::dispatch_setUsage(va_list args) {
767 int usage = va_arg(args, int);
768 setUsage( usage );
769}
770int Surface::dispatch_connect(va_list args) {
771 int api = va_arg(args, int);
772 return connect( api );
773}
774int Surface::dispatch_disconnect(va_list args) {
775 int api = va_arg(args, int);
776 return disconnect( api );
777}
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700778int Surface::dispatch_crop(va_list args) {
779 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
780 return crop( reinterpret_cast<Rect const*>(rect) );
781}
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700782int Surface::dispatch_set_buffer_count(va_list args) {
783 size_t bufferCount = va_arg(args, size_t);
784 return setBufferCount(bufferCount);
785}
Mathias Agopian2be352a2010-05-21 17:24:35 -0700786int Surface::dispatch_set_buffers_geometry(va_list args) {
787 int w = va_arg(args, int);
788 int h = va_arg(args, int);
789 int f = va_arg(args, int);
790 return setBuffersGeometry(w, h, f);
791}
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800792
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700793int Surface::dispatch_set_buffers_transform(va_list args) {
794 int transform = va_arg(args, int);
795 return setBuffersTransform(transform);
796}
797
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800798int Surface::dispatch_set_buffers_timestamp(va_list args) {
799 int64_t timestamp = va_arg(args, int64_t);
800 return setBuffersTimestamp(timestamp);
801}
802
Mathias Agopian321abdb2009-08-14 18:52:17 -0700803void Surface::setUsage(uint32_t reqUsage)
804{
805 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700806 mBufferInfo.set(reqUsage);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700807}
808
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800809int Surface::connect(int api)
810{
811 Mutex::Autolock _l(mSurfaceLock);
812 int err = NO_ERROR;
813 switch (api) {
814 case NATIVE_WINDOW_API_EGL:
815 if (mConnected) {
816 err = -EINVAL;
817 } else {
818 mConnected = api;
819 }
820 break;
821 default:
822 err = -EINVAL;
823 break;
824 }
825 return err;
826}
827
828int Surface::disconnect(int api)
829{
830 Mutex::Autolock _l(mSurfaceLock);
831 int err = NO_ERROR;
832 switch (api) {
833 case NATIVE_WINDOW_API_EGL:
834 if (mConnected == api) {
835 mConnected = 0;
836 } else {
837 err = -EINVAL;
838 }
839 break;
840 default:
841 err = -EINVAL;
842 break;
843 }
844 return err;
845}
846
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700847int Surface::crop(Rect const* rect)
848{
849 Mutex::Autolock _l(mSurfaceLock);
850 // TODO: validate rect size
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800851
852 if (rect == NULL || rect->isEmpty()) {
853 mNextBufferCrop = Rect(0,0);
854 } else {
855 mNextBufferCrop = *rect;
856 }
857
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700858 return NO_ERROR;
859}
860
Mathias Agopian59751db2010-05-07 15:58:44 -0700861int Surface::setBufferCount(int bufferCount)
862{
863 sp<ISurface> s(mSurface);
864 if (s == 0) return NO_INIT;
865
Mathias Agopian898c4c92010-05-18 17:06:55 -0700866 class SetBufferCountIPC : public SharedBufferClient::SetBufferCountCallback {
867 sp<ISurface> surface;
868 virtual status_t operator()(int bufferCount) const {
869 return surface->setBufferCount(bufferCount);
870 }
871 public:
872 SetBufferCountIPC(const sp<ISurface>& surface) : surface(surface) { }
873 } ipc(s);
Mathias Agopian59751db2010-05-07 15:58:44 -0700874
Mathias Agopian898c4c92010-05-18 17:06:55 -0700875 status_t err = mSharedBufferClient->setBufferCount(bufferCount, ipc);
Mathias Agopian59751db2010-05-07 15:58:44 -0700876 LOGE_IF(err, "ISurface::setBufferCount(%d) returned %s",
877 bufferCount, strerror(-err));
Jamie Gennis6c925d02010-11-02 11:51:32 -0700878
879 if (err == NO_ERROR) {
880 // Clear out any references to the old buffers.
881 mBuffers.clear();
882 }
883
Mathias Agopian59751db2010-05-07 15:58:44 -0700884 return err;
885}
886
Mathias Agopian2be352a2010-05-21 17:24:35 -0700887int Surface::setBuffersGeometry(int w, int h, int format)
888{
889 if (w<0 || h<0 || format<0)
890 return BAD_VALUE;
891
892 if ((w && !h) || (!w && h))
893 return BAD_VALUE;
894
895 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopianb9737a12010-10-25 16:29:24 -0700896 if (mConnected == NATIVE_WINDOW_API_EGL) {
897 return INVALID_OPERATION;
898 }
899
Mathias Agopian2be352a2010-05-21 17:24:35 -0700900 mBufferInfo.set(w, h, format);
Mathias Agopianb9737a12010-10-25 16:29:24 -0700901 if (format != 0) {
902 // we update the format of the surface as reported by query().
903 // this is to allow applications to change the format of a surface's
904 // buffer, and have it reflected in EGL; which is needed for
905 // EGLConfig validation.
906 mFormat = format;
907 }
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800908
909 mNextBufferCrop = Rect(0,0);
910
Mathias Agopian2be352a2010-05-21 17:24:35 -0700911 return NO_ERROR;
912}
913
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700914int Surface::setBuffersTransform(int transform)
915{
916 Mutex::Autolock _l(mSurfaceLock);
917 mNextBufferTransform = transform;
918 return NO_ERROR;
919}
920
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800921int Surface::setBuffersTimestamp(int64_t timestamp)
922{
923 // Surface doesn't really have anything meaningful to do with timestamps
924 // so they'll just be dropped here.
925 return NO_ERROR;
926}
927
Mathias Agopian2be352a2010-05-21 17:24:35 -0700928// ----------------------------------------------------------------------------
929
930int Surface::getConnectedApi() const
931{
932 Mutex::Autolock _l(mSurfaceLock);
933 return mConnected;
934}
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800935
Mathias Agopian1473f462009-04-10 14:24:30 -0700936// ----------------------------------------------------------------------------
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938status_t Surface::lock(SurfaceInfo* info, bool blocking) {
939 return Surface::lock(info, NULL, blocking);
940}
941
Mathias Agopiandff8e582009-05-04 14:17:04 -0700942status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian1473f462009-04-10 14:24:30 -0700943{
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800944 if (getConnectedApi()) {
945 LOGE("Surface::lock(%p) failed. Already connected to another API",
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700946 (ANativeWindow*)this);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800947 CallStack stack;
948 stack.update();
949 stack.dump("");
950 return INVALID_OPERATION;
951 }
952
Mathias Agopian9779b222009-09-07 16:32:45 -0700953 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian116e5412010-01-22 11:47:55 -0800954 LOGE("calling Surface::lock from different threads!");
Mathias Agopian9779b222009-09-07 16:32:45 -0700955 CallStack stack;
956 stack.update();
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800957 stack.dump("");
Mathias Agopian9779b222009-09-07 16:32:45 -0700958 return WOULD_BLOCK;
959 }
Mathias Agopian116e5412010-01-22 11:47:55 -0800960
961 /* Here we're holding mApiLock */
Mathias Agopian9779b222009-09-07 16:32:45 -0700962
Mathias Agopian116e5412010-01-22 11:47:55 -0800963 if (mLockedBuffer != 0) {
964 LOGE("Surface::lock failed, already locked");
965 mApiLock.unlock();
966 return INVALID_OPERATION;
967 }
968
Mathias Agopian5cec4742009-08-11 22:34:02 -0700969 // we're intending to do software rendering from this point
Mathias Agopian321abdb2009-08-14 18:52:17 -0700970 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
971
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700972 ANativeWindowBuffer* out;
Mathias Agopianf590f702010-04-27 16:41:19 -0700973 status_t err = dequeueBuffer(&out);
Mathias Agopian9779b222009-09-07 16:32:45 -0700974 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700975 if (err == NO_ERROR) {
Mathias Agopianf590f702010-04-27 16:41:19 -0700976 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopian9779b222009-09-07 16:32:45 -0700977 err = lockBuffer(backBuffer.get());
978 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
Mathias Agopianf590f702010-04-27 16:41:19 -0700979 getBufferIndex(backBuffer), strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700980 if (err == NO_ERROR) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700981 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700982 const Region boundsRegion(bounds);
983 Region scratch(boundsRegion);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700984 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700985 newDirtyRegion &= boundsRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -0700986
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700987 // figure out if we can copy the frontbuffer back
Mathias Agopian6950e422009-10-05 17:07:12 -0700988 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700989 const bool canCopyBack = (frontBuffer != 0 &&
990 backBuffer->width == frontBuffer->width &&
991 backBuffer->height == frontBuffer->height &&
992 backBuffer->format == frontBuffer->format &&
993 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
994
995 // the dirty region we report to surfaceflinger is the one
996 // given by the user (as opposed to the one *we* return to the
997 // user).
998 mDirtyRegion = newDirtyRegion;
999
1000 if (canCopyBack) {
1001 // copy the area that is invalid and not repainted this round
1002 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
1003 if (!copyback.isEmpty())
Mathias Agopian9779b222009-09-07 16:32:45 -07001004 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopiana8a0aa82010-04-21 15:24:11 -07001005 } else {
1006 // if we can't copy-back anything, modify the user's dirty
1007 // region to make sure they redraw the whole buffer
1008 newDirtyRegion = boundsRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -07001009 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001010
Mathias Agopiana8a0aa82010-04-21 15:24:11 -07001011 // keep track of the are of the buffer that is "clean"
1012 // (ie: that will be redrawn)
Mathias Agopiandff8e582009-05-04 14:17:04 -07001013 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -07001014
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001015 void* vaddr;
Mathias Agopiandff8e582009-05-04 14:17:04 -07001016 status_t res = backBuffer->lock(
1017 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001018 newDirtyRegion.bounds(), &vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001019
Mathias Agopian9779b222009-09-07 16:32:45 -07001020 LOGW_IF(res, "failed locking buffer (handle = %p)",
1021 backBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001022
1023 mLockedBuffer = backBuffer;
1024 other->w = backBuffer->width;
1025 other->h = backBuffer->height;
1026 other->s = backBuffer->stride;
1027 other->usage = backBuffer->usage;
1028 other->format = backBuffer->format;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001029 other->bits = vaddr;
Mathias Agopian1473f462009-04-10 14:24:30 -07001030 }
1031 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001032 mApiLock.unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -07001033 return err;
1034}
1035
1036status_t Surface::unlockAndPost()
1037{
Mathias Agopian9779b222009-09-07 16:32:45 -07001038 if (mLockedBuffer == 0) {
Mathias Agopian116e5412010-01-22 11:47:55 -08001039 LOGE("Surface::unlockAndPost failed, no locked buffer");
1040 return INVALID_OPERATION;
Mathias Agopian9779b222009-09-07 16:32:45 -07001041 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001042
Mathias Agopian9779b222009-09-07 16:32:45 -07001043 status_t err = mLockedBuffer->unlock();
1044 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001045
Mathias Agopian9779b222009-09-07 16:32:45 -07001046 err = queueBuffer(mLockedBuffer.get());
1047 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
Mathias Agopianf590f702010-04-27 16:41:19 -07001048 getBufferIndex(mLockedBuffer), strerror(-err));
Mathias Agopian9779b222009-09-07 16:32:45 -07001049
1050 mPostedBuffer = mLockedBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -07001051 mLockedBuffer = 0;
1052 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053}
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopian321abdb2009-08-14 18:52:17 -07001056 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mSwapRectangle = r;
1058}
1059
Mathias Agopianf590f702010-04-27 16:41:19 -07001060int Surface::getBufferIndex(const sp<GraphicBuffer>& buffer) const
1061{
Jamie Gennis1ef773f2010-10-07 13:46:55 -07001062 int idx = buffer->getIndex();
1063 if (idx < 0) {
1064 // The buffer doesn't have an index set. See if the handle the same as
1065 // one of the buffers for which we do know the index. This can happen
Iliyan Malchevb2a153a2011-05-01 11:33:26 -07001066 // e.g. if GraphicBuffer is used to wrap an ANativeWindowBuffer that
Jamie Gennis1ef773f2010-10-07 13:46:55 -07001067 // was dequeued from an ANativeWindow.
Jamie Gennis0b440fc2010-11-09 18:01:27 -08001068 for (size_t i = 0; i < mBuffers.size(); i++) {
Jamie Gennisa1c7e0e2011-02-27 18:40:09 -08001069 if (mBuffers[i] != 0 && buffer->handle == mBuffers[i]->handle) {
Jamie Gennis1ef773f2010-10-07 13:46:55 -07001070 idx = mBuffers[i]->getIndex();
1071 break;
1072 }
1073 }
1074 }
1075 return idx;
Mathias Agopianf590f702010-04-27 16:41:19 -07001076}
1077
Mathias Agopian2be352a2010-05-21 17:24:35 -07001078status_t Surface::getBufferLocked(int index,
1079 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080{
Mathias Agopian321abdb2009-08-14 18:52:17 -07001081 sp<ISurface> s(mSurface);
1082 if (s == 0) return NO_INIT;
1083
Mathias Agopian1473f462009-04-10 14:24:30 -07001084 status_t err = NO_MEMORY;
Mathias Agopianb2f84502009-08-19 17:10:18 -07001085
1086 // free the current buffer
Mathias Agopian2be352a2010-05-21 17:24:35 -07001087 sp<GraphicBuffer>& currentBuffer(mBuffers.editItemAt(index));
Mathias Agopianb2f84502009-08-19 17:10:18 -07001088 if (currentBuffer != 0) {
Mathias Agopianb2f84502009-08-19 17:10:18 -07001089 currentBuffer.clear();
1090 }
1091
Mathias Agopian2be352a2010-05-21 17:24:35 -07001092 sp<GraphicBuffer> buffer = s->requestBuffer(index, w, h, format, usage);
Mathias Agopian9779b222009-09-07 16:32:45 -07001093 LOGE_IF(buffer==0,
1094 "ISurface::getBuffer(%d, %08x) returned NULL",
1095 index, usage);
Jamie Gennis1ef773f2010-10-07 13:46:55 -07001096 if (buffer != 0) { // this should always happen by construction
Mathias Agopian9779b222009-09-07 16:32:45 -07001097 LOGE_IF(buffer->handle == NULL,
Mathias Agopian2be352a2010-05-21 17:24:35 -07001098 "Surface (identity=%d) requestBuffer(%d, %u, %u, %u, %08x) "
1099 "returned a buffer with a null handle",
1100 mIdentity, index, w, h, format, usage);
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001101 err = mSharedBufferClient->getStatus();
1102 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
1103 if (!err && buffer->handle != NULL) {
Jamie Gennis1ef773f2010-10-07 13:46:55 -07001104 currentBuffer = buffer;
1105 currentBuffer->setIndex(index);
Mathias Agopian0da41a32009-10-06 15:58:44 -07001106 } else {
Mathias Agopian25f0bda2010-05-21 14:19:50 -07001107 err = err<0 ? err : status_t(NO_MEMORY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001110 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111}
1112
Mathias Agopian2be352a2010-05-21 17:24:35 -07001113// ----------------------------------------------------------------------------
1114Surface::BufferInfo::BufferInfo()
1115 : mWidth(0), mHeight(0), mFormat(0),
1116 mUsage(GRALLOC_USAGE_HW_RENDER), mDirty(0)
1117{
1118}
1119
1120void Surface::BufferInfo::set(uint32_t w, uint32_t h, uint32_t format) {
1121 if ((mWidth != w) || (mHeight != h) || (mFormat != format)) {
1122 mWidth = w;
1123 mHeight = h;
1124 mFormat = format;
1125 mDirty |= GEOMETRY;
1126 }
1127}
1128
1129void Surface::BufferInfo::set(uint32_t usage) {
1130 mUsage = usage;
1131}
1132
1133void Surface::BufferInfo::get(uint32_t *pWidth, uint32_t *pHeight,
1134 uint32_t *pFormat, uint32_t *pUsage) const {
1135 *pWidth = mWidth;
1136 *pHeight = mHeight;
1137 *pFormat = mFormat;
1138 *pUsage = mUsage;
1139}
1140
1141bool Surface::BufferInfo::validateBuffer(const sp<GraphicBuffer>& buffer) const {
1142 // make sure we AT LEAST have the usage flags we want
1143 if (mDirty || buffer==0 ||
1144 ((buffer->usage & mUsage) != mUsage)) {
1145 mDirty = 0;
1146 return false;
1147 }
1148 return true;
1149}
1150
1151// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152}; // namespace android