blob: 9185e1e9930cc80cd26e2359bf55f37d5c862930 [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
tedbo1e7fa9e2011-06-22 15:52:53 -0700424sp<ISurfaceTexture> Surface::getSurfaceTexture() {
425 return mSurface != NULL ? mSurface->getSurfaceTexture() : NULL;
426}
427
Mathias Agopian47d87302011-04-05 15:44:20 -0700428sp<IBinder> Surface::asBinder() const {
429 return mSurface!=0 ? mSurface->asBinder() : 0;
Mathias Agopian631f3582010-05-25 17:51:34 -0700430}
431
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700432// ----------------------------------------------------------------------------
433
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700434int Surface::setSwapInterval(ANativeWindow* window, int interval) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700435 Surface* self = getSelf(window);
436 return self->setSwapInterval(interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700437}
438
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700439int Surface::dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700440 ANativeWindowBuffer** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700441 Surface* self = getSelf(window);
442 return self->dequeueBuffer(buffer);
443}
444
Mathias Agopian19957552010-10-01 16:22:41 -0700445int Surface::cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700446 ANativeWindowBuffer* buffer) {
Mathias Agopian19957552010-10-01 16:22:41 -0700447 Surface* self = getSelf(window);
448 return self->cancelBuffer(buffer);
449}
450
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700451int Surface::lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700452 ANativeWindowBuffer* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700453 Surface* self = getSelf(window);
454 return self->lockBuffer(buffer);
455}
456
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700457int Surface::queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700458 ANativeWindowBuffer* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700459 Surface* self = getSelf(window);
460 return self->queueBuffer(buffer);
461}
462
Iliyan Malchev41abd672011-04-14 16:54:38 -0700463int Surface::query(const ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700464 int what, int* value) {
Iliyan Malchev41abd672011-04-14 16:54:38 -0700465 const Surface* self = getSelf(window);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700466 return self->query(what, value);
467}
468
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700469int Surface::perform(ANativeWindow* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700470 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700471 va_list args;
472 va_start(args, operation);
473 Surface* self = getSelf(window);
474 int res = self->perform(operation, args);
475 va_end(args);
476 return res;
477}
478
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700479// ----------------------------------------------------------------------------
480
Mathias Agopiana67932f2011-04-20 14:20:59 -0700481int Surface::setSwapInterval(int interval) {
482 return mSurfaceTextureClient->setSwapInterval(interval);
Mathias Agopiana138f892010-05-21 17:24:35 -0700483}
484
Mathias Agopiana67932f2011-04-20 14:20:59 -0700485int Surface::dequeueBuffer(ANativeWindowBuffer** buffer) {
486 status_t err = mSurfaceTextureClient->dequeueBuffer(buffer);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700487 if (err == NO_ERROR) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700488 mDirtyRegion.set(buffer[0]->width, buffer[0]->height);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700489 }
490 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700491}
492
Mathias Agopiana67932f2011-04-20 14:20:59 -0700493int Surface::cancelBuffer(ANativeWindowBuffer* buffer) {
494 return mSurfaceTextureClient->cancelBuffer(buffer);
495}
496
497int Surface::lockBuffer(ANativeWindowBuffer* buffer) {
498 return mSurfaceTextureClient->lockBuffer(buffer);
499}
500
501int Surface::queueBuffer(ANativeWindowBuffer* buffer) {
502 return mSurfaceTextureClient->queueBuffer(buffer);
503}
504
505int Surface::query(int what, int* value) const {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700506 switch (what) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700507 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
508 // TODO: this is not needed anymore
509 *value = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700510 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700511 case NATIVE_WINDOW_CONCRETE_TYPE:
Mathias Agopiana67932f2011-04-20 14:20:59 -0700512 // TODO: this is not needed anymore
Jamie Gennis391bbe22011-03-14 15:00:06 -0700513 *value = NATIVE_WINDOW_SURFACE;
514 return NO_ERROR;
515 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700516 return mSurfaceTextureClient->query(what, value);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700517}
518
Mathias Agopiana67932f2011-04-20 14:20:59 -0700519int Surface::perform(int operation, va_list args) {
520 return mSurfaceTextureClient->perform(operation, args);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800521}
522
Mathias Agopiana138f892010-05-21 17:24:35 -0700523// ----------------------------------------------------------------------------
524
Mathias Agopiana67932f2011-04-20 14:20:59 -0700525int Surface::getConnectedApi() const {
526 return mSurfaceTextureClient->getConnectedApi();
Mathias Agopiana138f892010-05-21 17:24:35 -0700527}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800528
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700529// ----------------------------------------------------------------------------
530
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531status_t Surface::lock(SurfaceInfo* info, bool blocking) {
532 return Surface::lock(info, NULL, blocking);
533}
534
Mathias Agopian0926f502009-05-04 14:17:04 -0700535status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800537 if (getConnectedApi()) {
538 LOGE("Surface::lock(%p) failed. Already connected to another API",
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700539 (ANativeWindow*)this);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800540 CallStack stack;
541 stack.update();
542 stack.dump("");
543 return INVALID_OPERATION;
544 }
545
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700546 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800547 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700548 CallStack stack;
549 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800550 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700551 return WOULD_BLOCK;
552 }
Mathias Agopian90147262010-01-22 11:47:55 -0800553
554 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700555
Mathias Agopian90147262010-01-22 11:47:55 -0800556 if (mLockedBuffer != 0) {
557 LOGE("Surface::lock failed, already locked");
558 mApiLock.unlock();
559 return INVALID_OPERATION;
560 }
561
Mathias Agopian52212712009-08-11 22:34:02 -0700562 // we're intending to do software rendering from this point
Mathias Agopiana67932f2011-04-20 14:20:59 -0700563 mSurfaceTextureClient->setUsage(
564 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700565
Iliyan Malchev697526b2011-05-01 11:33:26 -0700566 ANativeWindowBuffer* out;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700567 status_t err = mSurfaceTextureClient->dequeueBuffer(&out);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700568 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700569 if (err == NO_ERROR) {
Mathias Agopianb2965332010-04-27 16:41:19 -0700570 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiana67932f2011-04-20 14:20:59 -0700571 err = mSurfaceTextureClient->lockBuffer(backBuffer.get());
572 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
573 backBuffer->handle, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700576 const Region boundsRegion(bounds);
577 Region scratch(boundsRegion);
Mathias Agopian0926f502009-05-04 14:17:04 -0700578 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700579 newDirtyRegion &= boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700580
Mathias Agopian245e4d72010-04-21 15:24:11 -0700581 // figure out if we can copy the frontbuffer back
Mathias Agopian3330b202009-10-05 17:07:12 -0700582 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700583 const bool canCopyBack = (frontBuffer != 0 &&
584 backBuffer->width == frontBuffer->width &&
585 backBuffer->height == frontBuffer->height &&
586 backBuffer->format == frontBuffer->format &&
587 !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
588
589 // the dirty region we report to surfaceflinger is the one
590 // given by the user (as opposed to the one *we* return to the
591 // user).
592 mDirtyRegion = newDirtyRegion;
593
594 if (canCopyBack) {
595 // copy the area that is invalid and not repainted this round
596 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
597 if (!copyback.isEmpty())
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700598 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian245e4d72010-04-21 15:24:11 -0700599 } else {
600 // if we can't copy-back anything, modify the user's dirty
601 // region to make sure they redraw the whole buffer
602 newDirtyRegion = boundsRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700604
Mathias Agopian245e4d72010-04-21 15:24:11 -0700605 // keep track of the are of the buffer that is "clean"
606 // (ie: that will be redrawn)
Mathias Agopian0926f502009-05-04 14:17:04 -0700607 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700608
Mathias Agopiane71212b2009-05-05 00:37:46 -0700609 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700610 status_t res = backBuffer->lock(
611 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700612 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700613
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700614 LOGW_IF(res, "failed locking buffer (handle = %p)",
615 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700616
617 mLockedBuffer = backBuffer;
618 other->w = backBuffer->width;
619 other->h = backBuffer->height;
620 other->s = backBuffer->stride;
621 other->usage = backBuffer->usage;
622 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700623 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624 }
625 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700626 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627 return err;
628}
629
630status_t Surface::unlockAndPost()
631{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700632 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800633 LOGE("Surface::unlockAndPost failed, no locked buffer");
634 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700635 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700637 status_t err = mLockedBuffer->unlock();
638 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700639
Mathias Agopiana67932f2011-04-20 14:20:59 -0700640 err = mSurfaceTextureClient->queueBuffer(mLockedBuffer.get());
641 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
642 mLockedBuffer->handle, strerror(-err));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700643
644 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 mLockedBuffer = 0;
646 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647}
648
Mathias Agopiana138f892010-05-21 17:24:35 -0700649// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650}; // namespace android