blob: 4d1d923a4262677700bdad9b59f7aa133815010a [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
Mathias Agopiancbb288b2009-09-07 16:32:45 -070024#include <utils/CallStack.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070025#include <utils/Errors.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080026#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <utils/threads.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/IMemory.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <binder/IPCThreadState.h>
31
32#include <gui/SurfaceTextureClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034#include <ui/DisplayInfo.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070035#include <ui/GraphicBuffer.h>
36#include <ui/GraphicBufferMapper.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070037#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#include <ui/Rect.h>
39
Mathias Agopian9cce3252010-02-09 17:46:37 -080040#include <surfaceflinger/ISurface.h>
41#include <surfaceflinger/ISurfaceComposer.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070042#include <surfaceflinger/Surface.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080043#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044
Mathias Agopian9cce3252010-02-09 17:46:37 -080045#include <private/surfaceflinger/LayerState.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047namespace android {
48
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049// ----------------------------------------------------------------------
50
Mathias Agopian14998592009-07-13 18:29:59 -070051static status_t copyBlt(
Mathias Agopian3330b202009-10-05 17:07:12 -070052 const sp<GraphicBuffer>& dst,
53 const sp<GraphicBuffer>& src,
Mathias Agopian0926f502009-05-04 14:17:04 -070054 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055{
Mathias Agopian245e4d72010-04-21 15:24:11 -070056 // src and dst with, height and format must be identical. no verification
57 // is done here.
Mathias Agopian14998592009-07-13 18:29:59 -070058 status_t err;
59 uint8_t const * src_bits = NULL;
60 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
61 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -070062
Mathias Agopian14998592009-07-13 18:29:59 -070063 uint8_t* dst_bits = NULL;
64 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
65 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
66
67 Region::const_iterator head(reg.begin());
68 Region::const_iterator tail(reg.end());
69 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 const size_t bpp = bytesPerPixel(src->format);
71 const size_t dbpr = dst->stride * bpp;
72 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -070073
Mathias Agopian14998592009-07-13 18:29:59 -070074 while (head != tail) {
75 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -070076 ssize_t h = r.height();
77 if (h <= 0) continue;
78 size_t size = r.width() * bpp;
79 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
80 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
81 if (dbpr==sbpr && size==sbpr) {
82 size *= h;
83 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 }
Mathias Agopian0926f502009-05-04 14:17:04 -070085 do {
86 memcpy(d, s, size);
87 d += dbpr;
88 s += sbpr;
89 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 }
91 }
Mathias Agopian0926f502009-05-04 14:17:04 -070092
Mathias Agopian14998592009-07-13 18:29:59 -070093 if (src_bits)
94 src->unlock();
95
96 if (dst_bits)
97 dst->unlock();
98
99 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100}
101
Mathias Agopian62185b72009-04-16 16:19:50 -0700102// ============================================================================
103// SurfaceControl
104// ============================================================================
105
Mathias Agopian01b76682009-04-16 20:04:08 -0700106SurfaceControl::SurfaceControl(
107 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700108 const sp<ISurface>& surface,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700109 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700110 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700111 : mClient(client), mSurface(surface),
112 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700113 mWidth(data.width), mHeight(data.height), mFormat(data.format),
114 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700115{
116}
Mathias Agopian18d84462009-04-16 20:30:22 -0700117
Mathias Agopian62185b72009-04-16 16:19:50 -0700118SurfaceControl::~SurfaceControl()
119{
120 destroy();
121}
122
123void SurfaceControl::destroy()
124{
Mathias Agopian18d84462009-04-16 20:30:22 -0700125 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700126 mClient->destroySurface(mToken);
127 }
128
129 // clear all references and trigger an IPC now, to make sure things
130 // happen without delay, since these resources are quite heavy.
131 mClient.clear();
132 mSurface.clear();
133 IPCThreadState::self()->flushCommands();
134}
135
136void SurfaceControl::clear()
137{
138 // here, the window manager tells us explicitly that we should destroy
139 // the surface's resource. Soon after this call, it will also release
140 // its last reference (which will call the dtor); however, it is possible
141 // that a client living in the same process still holds references which
142 // would delay the call to the dtor -- that is why we need this explicit
143 // "clear()" call.
144 destroy();
145}
146
Mathias Agopian62185b72009-04-16 16:19:50 -0700147bool SurfaceControl::isSameSurface(
148 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
149{
150 if (lhs == 0 || rhs == 0)
151 return false;
152 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
153}
154
Mathias Agopian01b76682009-04-16 20:04:08 -0700155status_t SurfaceControl::setLayer(int32_t layer) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800156 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700157 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700158 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700159 return client->setLayer(mToken, layer);
160}
161status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800162 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700163 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700164 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700165 return client->setPosition(mToken, x, y);
166}
167status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800168 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700169 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700170 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700171 return client->setSize(mToken, w, h);
172}
173status_t SurfaceControl::hide() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800174 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700175 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700176 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700177 return client->hide(mToken);
178}
179status_t SurfaceControl::show(int32_t layer) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800180 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700181 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700182 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700183 return client->show(mToken, layer);
184}
185status_t SurfaceControl::freeze() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800186 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700187 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700188 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700189 return client->freeze(mToken);
190}
191status_t SurfaceControl::unfreeze() {
Mathias Agopian963abad2009-11-13 15:26:29 -0800192 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700193 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700194 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700195 return client->unfreeze(mToken);
196}
197status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800198 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700199 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700200 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700201 return client->setFlags(mToken, flags, mask);
202}
203status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800204 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700205 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700206 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700207 return client->setTransparentRegionHint(mToken, transparent);
208}
209status_t SurfaceControl::setAlpha(float alpha) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800210 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700211 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700212 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700213 return client->setAlpha(mToken, alpha);
214}
215status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800216 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700217 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700218 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700219 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
220}
221status_t SurfaceControl::setFreezeTint(uint32_t tint) {
Mathias Agopian963abad2009-11-13 15:26:29 -0800222 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700223 if (err < 0) return err;
Mathias Agopian631f3582010-05-25 17:51:34 -0700224 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian01b76682009-04-16 20:04:08 -0700225 return client->setFreezeTint(mToken, tint);
226}
Mathias Agopian62185b72009-04-16 16:19:50 -0700227
Mathias Agopian963abad2009-11-13 15:26:29 -0800228status_t SurfaceControl::validate() const
Mathias Agopian62185b72009-04-16 16:19:50 -0700229{
230 if (mToken<0 || mClient==0) {
231 LOGE("invalid token (%d, identity=%u) or client (%p)",
232 mToken, mIdentity, mClient.get());
233 return NO_INIT;
234 }
Mathias Agopian62185b72009-04-16 16:19:50 -0700235 return NO_ERROR;
236}
237
Mathias Agopian01b76682009-04-16 20:04:08 -0700238status_t SurfaceControl::writeSurfaceToParcel(
239 const sp<SurfaceControl>& control, Parcel* parcel)
240{
Mathias Agopian579b3f82010-06-08 19:54:15 -0700241 sp<ISurface> sur;
Mathias Agopian01b76682009-04-16 20:04:08 -0700242 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700243 uint32_t width = 0;
244 uint32_t height = 0;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700245 uint32_t format = 0;
246 uint32_t flags = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700247 if (SurfaceControl::isValid(control)) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700248 sur = control->mSurface;
Mathias Agopian579b3f82010-06-08 19:54:15 -0700249 identity = control->mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700250 width = control->mWidth;
251 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700252 format = control->mFormat;
253 flags = control->mFlags;
254 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700255 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
Mathias Agopian01b76682009-04-16 20:04:08 -0700256 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700257 parcel->writeInt32(width);
258 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700259 parcel->writeInt32(format);
260 parcel->writeInt32(flags);
261 return NO_ERROR;
262}
263
264sp<Surface> SurfaceControl::getSurface() const
265{
266 Mutex::Autolock _l(mLock);
267 if (mSurfaceData == 0) {
268 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
269 }
270 return mSurfaceData;
271}
272
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700273// ============================================================================
274// Surface
275// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700277// ---------------------------------------------------------------------------
278
Mathias Agopian01b76682009-04-16 20:04:08 -0700279Surface::Surface(const sp<SurfaceControl>& surface)
Mathias Agopiana67932f2011-04-20 14:20:59 -0700280 : mInitCheck(NO_INIT),
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700281 mSurface(surface->mSurface),
282 mIdentity(surface->mIdentity),
283 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700284 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285{
Mathias Agopian01b76682009-04-16 20:04:08 -0700286 init();
287}
Mathias Agopian62185b72009-04-16 16:19:50 -0700288
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700289Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
Mathias Agopiana67932f2011-04-20 14:20:59 -0700290 : mInitCheck(NO_INIT)
Mathias Agopian01b76682009-04-16 20:04:08 -0700291{
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700292 mSurface = interface_cast<ISurface>(ref);
Mathias Agopian01b76682009-04-16 20:04:08 -0700293 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700294 mWidth = parcel.readInt32();
295 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700296 mFormat = parcel.readInt32();
297 mFlags = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700298 init();
299}
300
Mathias Agopian579b3f82010-06-08 19:54:15 -0700301status_t Surface::writeToParcel(
302 const sp<Surface>& surface, Parcel* parcel)
303{
304 sp<ISurface> sur;
305 uint32_t identity = 0;
306 uint32_t width = 0;
307 uint32_t height = 0;
308 uint32_t format = 0;
309 uint32_t flags = 0;
310 if (Surface::isValid(surface)) {
311 sur = surface->mSurface;
312 identity = surface->mIdentity;
313 width = surface->mWidth;
314 height = surface->mHeight;
315 format = surface->mFormat;
316 flags = surface->mFlags;
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700317 } else if (surface != 0 && surface->mSurface != 0) {
318 LOGW("Parceling invalid surface with non-NULL ISurface as NULL: "
319 "mSurface = %p, mIdentity = %d, mWidth = %d, mHeight = %d, "
320 "mFormat = %d, mFlags = 0x%08x, mInitCheck = %d",
321 surface->mSurface.get(), surface->mIdentity, surface->mWidth,
322 surface->mHeight, surface->mFormat, surface->mFlags,
323 surface->mInitCheck);
Mathias Agopian579b3f82010-06-08 19:54:15 -0700324 }
325 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
326 parcel->writeInt32(identity);
327 parcel->writeInt32(width);
328 parcel->writeInt32(height);
329 parcel->writeInt32(format);
330 parcel->writeInt32(flags);
331 return NO_ERROR;
332
333}
334
Jamie Gennisaca4e222010-07-15 17:29:15 -0700335Mutex Surface::sCachedSurfacesLock;
Mathias Agopian455d18d2010-12-13 16:47:31 -0800336DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700337
338sp<Surface> Surface::readFromParcel(const Parcel& data) {
339 Mutex::Autolock _l(sCachedSurfacesLock);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700340 sp<IBinder> binder(data.readStrongBinder());
Jamie Gennisaca4e222010-07-15 17:29:15 -0700341 sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
342 if (surface == 0) {
343 surface = new Surface(data, binder);
344 sCachedSurfaces.add(binder, surface);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700345 }
Jamie Gennisaca4e222010-07-15 17:29:15 -0700346 if (surface->mSurface == 0) {
347 surface = 0;
348 }
Mathias Agopian455d18d2010-12-13 16:47:31 -0800349 cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700350 return surface;
351}
352
353// Remove the stale entries from the surface cache. This should only be called
354// with sCachedSurfacesLock held.
Mathias Agopian455d18d2010-12-13 16:47:31 -0800355void Surface::cleanCachedSurfacesLocked() {
Jamie Gennisaca4e222010-07-15 17:29:15 -0700356 for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
357 wp<Surface> s(sCachedSurfaces.valueAt(i));
358 if (s == 0 || s.promote() == 0) {
359 sCachedSurfaces.removeItemsAt(i);
360 }
361 }
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700362}
363
Mathias Agopian01b76682009-04-16 20:04:08 -0700364void Surface::init()
365{
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700366 ANativeWindow::setSwapInterval = setSwapInterval;
367 ANativeWindow::dequeueBuffer = dequeueBuffer;
Mathias Agopian19957552010-10-01 16:22:41 -0700368 ANativeWindow::cancelBuffer = cancelBuffer;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700369 ANativeWindow::lockBuffer = lockBuffer;
370 ANativeWindow::queueBuffer = queueBuffer;
371 ANativeWindow::query = query;
372 ANativeWindow::perform = perform;
Mathias Agopian631f3582010-05-25 17:51:34 -0700373
Mathias Agopiana67932f2011-04-20 14:20:59 -0700374 if (mSurface != NULL) {
375 sp<ISurfaceTexture> surfaceTexture(mSurface->getSurfaceTexture());
376 LOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface");
377 if (surfaceTexture != NULL) {
378 mSurfaceTextureClient = new SurfaceTextureClient(surfaceTexture);
379 mSurfaceTextureClient->setUsage(GraphicBuffer::USAGE_HW_RENDER);
380 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700381
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382 DisplayInfo dinfo;
383 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
384 const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
385 const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
Mathias Agopian631f3582010-05-25 17:51:34 -0700386
Mathias Agopiana67932f2011-04-20 14:20:59 -0700387 const_cast<int&>(ANativeWindow::minSwapInterval) =
388 mSurfaceTextureClient->minSwapInterval;
389
390 const_cast<int&>(ANativeWindow::maxSwapInterval) =
391 mSurfaceTextureClient->maxSwapInterval;
392
393 const_cast<uint32_t&>(ANativeWindow::flags) = 0;
394
395 if (mSurfaceTextureClient != 0) {
396 mInitCheck = NO_ERROR;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700397 }
Mathias Agopian631f3582010-05-25 17:51:34 -0700398 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399}
400
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401Surface::~Surface()
402{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700403 // clear all references and trigger an IPC now, to make sure things
404 // happen without delay, since these resources are quite heavy.
Mathias Agopiana67932f2011-04-20 14:20:59 -0700405 mSurfaceTextureClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 IPCThreadState::self()->flushCommands();
408}
409
Mathias Agopian631f3582010-05-25 17:51:34 -0700410bool Surface::isValid() {
411 return mInitCheck == NO_ERROR;
412}
413
Mathias Agopiana317f1b2011-01-14 11:04:34 -0800414status_t Surface::validate(bool inCancelBuffer) const
Mathias Agopian631f3582010-05-25 17:51:34 -0700415{
416 // check that we initialized ourself properly
417 if (mInitCheck != NO_ERROR) {
Mathias Agopian579b3f82010-06-08 19:54:15 -0700418 LOGE("invalid token (identity=%u)", mIdentity);
Mathias Agopian631f3582010-05-25 17:51:34 -0700419 return mInitCheck;
420 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700421 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
423
Mathias Agopian47d87302011-04-05 15:44:20 -0700424sp<IBinder> Surface::asBinder() const {
425 return mSurface!=0 ? mSurface->asBinder() : 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700426}
427
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428// ----------------------------------------------------------------------------
429
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700430int Surface::setSwapInterval(ANativeWindow* window, int interval) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700431 Surface* self = getSelf(window);
432 return self->setSwapInterval(interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433}
434
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700435int Surface::dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700436 ANativeWindowBuffer** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700437 Surface* self = getSelf(window);
438 return self->dequeueBuffer(buffer);
439}
440
Mathias Agopian19957552010-10-01 16:22:41 -0700441int Surface::cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700442 ANativeWindowBuffer* buffer) {
Mathias Agopian19957552010-10-01 16:22:41 -0700443 Surface* self = getSelf(window);
444 return self->cancelBuffer(buffer);
445}
446
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700447int Surface::lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700448 ANativeWindowBuffer* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700449 Surface* self = getSelf(window);
450 return self->lockBuffer(buffer);
451}
452
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700453int Surface::queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700454 ANativeWindowBuffer* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700455 Surface* self = getSelf(window);
456 return self->queueBuffer(buffer);
457}
458
Iliyan Malchev41abd672011-04-14 16:54:38 -0700459int Surface::query(const ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700460 int what, int* value) {
Iliyan Malchev41abd672011-04-14 16:54:38 -0700461 const Surface* self = getSelf(window);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700462 return self->query(what, value);
463}
464
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700465int Surface::perform(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700466 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700467 va_list args;
468 va_start(args, operation);
469 Surface* self = getSelf(window);
470 int res = self->perform(operation, args);
471 va_end(args);
472 return res;
473}
474
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700475// ----------------------------------------------------------------------------
476
Mathias Agopiana67932f2011-04-20 14:20:59 -0700477int Surface::setSwapInterval(int interval) {
478 return mSurfaceTextureClient->setSwapInterval(interval);
Mathias Agopiana138f892010-05-21 17:24:35 -0700479}
480
Mathias Agopiana67932f2011-04-20 14:20:59 -0700481int Surface::dequeueBuffer(ANativeWindowBuffer** buffer) {
482 status_t err = mSurfaceTextureClient->dequeueBuffer(buffer);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700483 if (err == NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700484 mDirtyRegion.set(buffer[0]->width, buffer[0]->height);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700485 }
486 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700487}
488
Mathias Agopiana67932f2011-04-20 14:20:59 -0700489int Surface::cancelBuffer(ANativeWindowBuffer* buffer) {
490 return mSurfaceTextureClient->cancelBuffer(buffer);
491}
492
493int Surface::lockBuffer(ANativeWindowBuffer* buffer) {
494 return mSurfaceTextureClient->lockBuffer(buffer);
495}
496
497int Surface::queueBuffer(ANativeWindowBuffer* buffer) {
498 return mSurfaceTextureClient->queueBuffer(buffer);
499}
500
501int Surface::query(int what, int* value) const {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700502 switch (what) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700503 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
504 // TODO: this is not needed anymore
505 *value = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700506 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700507 case NATIVE_WINDOW_CONCRETE_TYPE:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700508 // TODO: this is not needed anymore
Jamie Gennis391bbe22011-03-14 15:00:06 -0700509 *value = NATIVE_WINDOW_SURFACE;
510 return NO_ERROR;
511 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700512 return mSurfaceTextureClient->query(what, value);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700513}
514
Mathias Agopiana67932f2011-04-20 14:20:59 -0700515int Surface::perform(int operation, va_list args) {
516 return mSurfaceTextureClient->perform(operation, args);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800517}
518
Mathias Agopiana138f892010-05-21 17:24:35 -0700519// ----------------------------------------------------------------------------
520
Mathias Agopiana67932f2011-04-20 14:20:59 -0700521int Surface::getConnectedApi() const {
522 return mSurfaceTextureClient->getConnectedApi();
Mathias Agopiana138f892010-05-21 17:24:35 -0700523}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800524
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700525// ----------------------------------------------------------------------------
526
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527status_t Surface::lock(SurfaceInfo* info, bool blocking) {
528 return Surface::lock(info, NULL, blocking);
529}
530
Mathias Agopian0926f502009-05-04 14:17:04 -0700531status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700532{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800533 if (getConnectedApi()) {
534 LOGE("Surface::lock(%p) failed. Already connected to another API",
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700535 (ANativeWindow*)this);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800536 CallStack stack;
537 stack.update();
538 stack.dump("");
539 return INVALID_OPERATION;
540 }
541
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700542 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800543 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700544 CallStack stack;
545 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800546 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700547 return WOULD_BLOCK;
548 }
Mathias Agopian90147262010-01-22 11:47:55 -0800549
550 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700551
Mathias Agopian90147262010-01-22 11:47:55 -0800552 if (mLockedBuffer != 0) {
553 LOGE("Surface::lock failed, already locked");
554 mApiLock.unlock();
555 return INVALID_OPERATION;
556 }
557
Mathias Agopian52212712009-08-11 22:34:02 -0700558 // we're intending to do software rendering from this point
Mathias Agopiana67932f2011-04-20 14:20:59 -0700559 mSurfaceTextureClient->setUsage(
560 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700561
Iliyan Malchev697526b2011-05-01 11:33:26 -0700562 ANativeWindowBuffer* out;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700563 status_t err = mSurfaceTextureClient->dequeueBuffer(&out);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700564 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 if (err == NO_ERROR) {
Mathias Agopianb2965332010-04-27 16:41:19 -0700566 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiana67932f2011-04-20 14:20:59 -0700567 err = mSurfaceTextureClient->lockBuffer(backBuffer.get());
568 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
569 backBuffer->handle, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700572 const Region boundsRegion(bounds);
573 Region scratch(boundsRegion);
Mathias Agopian0926f502009-05-04 14:17:04 -0700574 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700575 newDirtyRegion &= boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700576
Mathias Agopian245e4d72010-04-21 15:24:11 -0700577 // figure out if we can copy the frontbuffer back
Mathias Agopian3330b202009-10-05 17:07:12 -0700578 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700579 const bool canCopyBack = (frontBuffer != 0 &&
580 backBuffer->width == frontBuffer->width &&
581 backBuffer->height == frontBuffer->height &&
582 backBuffer->format == frontBuffer->format &&
583 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
584
585 // the dirty region we report to surfaceflinger is the one
586 // given by the user (as opposed to the one *we* return to the
587 // user).
588 mDirtyRegion = newDirtyRegion;
589
590 if (canCopyBack) {
591 // copy the area that is invalid and not repainted this round
592 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
593 if (!copyback.isEmpty())
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700594 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700595 } else {
596 // if we can't copy-back anything, modify the user's dirty
597 // region to make sure they redraw the whole buffer
598 newDirtyRegion = boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700600
Mathias Agopian245e4d72010-04-21 15:24:11 -0700601 // keep track of the are of the buffer that is "clean"
602 // (ie: that will be redrawn)
Mathias Agopian0926f502009-05-04 14:17:04 -0700603 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604
Mathias Agopiane71212b2009-05-05 00:37:46 -0700605 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700606 status_t res = backBuffer->lock(
607 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700608 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700609
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700610 LOGW_IF(res, "failed locking buffer (handle = %p)",
611 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700612
613 mLockedBuffer = backBuffer;
614 other->w = backBuffer->width;
615 other->h = backBuffer->height;
616 other->s = backBuffer->stride;
617 other->usage = backBuffer->usage;
618 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700619 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620 }
621 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700622 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700623 return err;
624}
625
626status_t Surface::unlockAndPost()
627{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700628 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800629 LOGE("Surface::unlockAndPost failed, no locked buffer");
630 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700631 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700632
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700633 status_t err = mLockedBuffer->unlock();
634 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700635
Mathias Agopiana67932f2011-04-20 14:20:59 -0700636 err = mSurfaceTextureClient->queueBuffer(mLockedBuffer.get());
637 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
638 mLockedBuffer->handle, strerror(-err));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700639
640 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641 mLockedBuffer = 0;
642 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643}
644
Mathias Agopiana138f892010-05-21 17:24:35 -0700645// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646}; // namespace android