blob: eb3457bfda07e9deec1b043c2b8b297d73eeac92 [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>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopiancbb288b2009-09-07 16:32:45 -070028#include <utils/CallStack.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080029#include <utils/Log.h>
30
31#include <pixelflinger/pixelflinger.h>
32
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/IPCThreadState.h>
34#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <ui/DisplayInfo.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBuffer.h>
38#include <ui/GraphicBufferMapper.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/Rect.h>
40
Mathias Agopian9cce3252010-02-09 17:46:37 -080041#include <surfaceflinger/Surface.h>
42#include <surfaceflinger/ISurface.h>
43#include <surfaceflinger/ISurfaceComposer.h>
44#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045
Mathias Agopian9cce3252010-02-09 17:46:37 -080046#include <private/surfaceflinger/SharedBufferStack.h>
47#include <private/surfaceflinger/LayerState.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049namespace android {
50
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051// ----------------------------------------------------------------------
52
Mathias Agopian14998592009-07-13 18:29:59 -070053static status_t copyBlt(
Mathias Agopian3330b202009-10-05 17:07:12 -070054 const sp<GraphicBuffer>& dst,
55 const sp<GraphicBuffer>& src,
Mathias Agopian0926f502009-05-04 14:17:04 -070056 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057{
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 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 const size_t bpp = bytesPerPixel(src->format);
72 const size_t dbpr = dst->stride * bpp;
73 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -070074
Mathias Agopian14998592009-07-13 18:29:59 -070075 while (head != tail) {
76 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -070077 ssize_t h = r.height();
78 if (h <= 0) continue;
79 size_t size = r.width() * bpp;
80 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
81 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
82 if (dbpr==sbpr && size==sbpr) {
83 size *= h;
84 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 }
Mathias Agopian0926f502009-05-04 14:17:04 -070086 do {
87 memcpy(d, s, size);
88 d += dbpr;
89 s += sbpr;
90 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 }
92 }
Mathias Agopian0926f502009-05-04 14:17:04 -070093
Mathias Agopian14998592009-07-13 18:29:59 -070094 if (src_bits)
95 src->unlock();
96
97 if (dst_bits)
98 dst->unlock();
99
100 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101}
102
Mathias Agopian62185b72009-04-16 16:19:50 -0700103// ============================================================================
104// SurfaceControl
105// ============================================================================
106
Mathias Agopian01b76682009-04-16 20:04:08 -0700107SurfaceControl::SurfaceControl(
108 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700109 const sp<ISurface>& surface,
110 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700111 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700112 : mClient(client), mSurface(surface),
113 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700114 mWidth(data.width), mHeight(data.height), mFormat(data.format),
115 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700116{
117}
Mathias Agopian18d84462009-04-16 20:30:22 -0700118
Mathias Agopian62185b72009-04-16 16:19:50 -0700119SurfaceControl::~SurfaceControl()
120{
121 destroy();
122}
123
124void SurfaceControl::destroy()
125{
Mathias Agopian18d84462009-04-16 20:30:22 -0700126 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700127 mClient->destroySurface(mToken);
128 }
129
130 // clear all references and trigger an IPC now, to make sure things
131 // happen without delay, since these resources are quite heavy.
132 mClient.clear();
133 mSurface.clear();
134 IPCThreadState::self()->flushCommands();
135}
136
137void SurfaceControl::clear()
138{
139 // here, the window manager tells us explicitly that we should destroy
140 // the surface's resource. Soon after this call, it will also release
141 // its last reference (which will call the dtor); however, it is possible
142 // that a client living in the same process still holds references which
143 // would delay the call to the dtor -- that is why we need this explicit
144 // "clear()" call.
145 destroy();
146}
147
Mathias Agopian62185b72009-04-16 16:19:50 -0700148bool SurfaceControl::isSameSurface(
149 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
150{
151 if (lhs == 0 || rhs == 0)
152 return false;
153 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
154}
155
Mathias Agopian01b76682009-04-16 20:04:08 -0700156status_t SurfaceControl::setLayer(int32_t layer) {
157 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800158 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700159 if (err < 0) return err;
160 return client->setLayer(mToken, layer);
161}
162status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
163 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800164 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700165 if (err < 0) return err;
166 return client->setPosition(mToken, x, y);
167}
168status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
169 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800170 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700171 if (err < 0) return err;
172 return client->setSize(mToken, w, h);
173}
174status_t SurfaceControl::hide() {
175 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800176 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700177 if (err < 0) return err;
178 return client->hide(mToken);
179}
180status_t SurfaceControl::show(int32_t layer) {
181 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800182 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700183 if (err < 0) return err;
184 return client->show(mToken, layer);
185}
186status_t SurfaceControl::freeze() {
187 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800188 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700189 if (err < 0) return err;
190 return client->freeze(mToken);
191}
192status_t SurfaceControl::unfreeze() {
193 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800194 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700195 if (err < 0) return err;
196 return client->unfreeze(mToken);
197}
198status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
199 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800200 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700201 if (err < 0) return err;
202 return client->setFlags(mToken, flags, mask);
203}
204status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
205 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800206 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700207 if (err < 0) return err;
208 return client->setTransparentRegionHint(mToken, transparent);
209}
210status_t SurfaceControl::setAlpha(float alpha) {
211 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800212 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700213 if (err < 0) return err;
214 return client->setAlpha(mToken, alpha);
215}
216status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
217 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800218 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700219 if (err < 0) return err;
220 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
221}
222status_t SurfaceControl::setFreezeTint(uint32_t tint) {
223 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian963abad2009-11-13 15:26:29 -0800224 status_t err = validate();
Mathias Agopian01b76682009-04-16 20:04:08 -0700225 if (err < 0) return err;
226 return client->setFreezeTint(mToken, tint);
227}
Mathias Agopian62185b72009-04-16 16:19:50 -0700228
Mathias Agopian963abad2009-11-13 15:26:29 -0800229status_t SurfaceControl::validate() const
Mathias Agopian62185b72009-04-16 16:19:50 -0700230{
231 if (mToken<0 || mClient==0) {
232 LOGE("invalid token (%d, identity=%u) or client (%p)",
233 mToken, mIdentity, mClient.get());
234 return NO_INIT;
235 }
Mathias Agopian963abad2009-11-13 15:26:29 -0800236 SharedClient const* cblk = mClient->mControl;
Mathias Agopian62185b72009-04-16 16:19:50 -0700237 if (cblk == 0) {
238 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
239 return NO_INIT;
240 }
241 status_t err = cblk->validate(mToken);
242 if (err != NO_ERROR) {
243 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
244 mToken, mIdentity, err, strerror(-err));
245 return err;
246 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700247 uint32_t identity = cblk->getIdentity(mToken);
248 if (mIdentity != identity) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700249 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700250 mToken, mIdentity, identity);
Mathias Agopian62185b72009-04-16 16:19:50 -0700251 return NO_INIT;
252 }
253 return NO_ERROR;
254}
255
Mathias Agopian01b76682009-04-16 20:04:08 -0700256status_t SurfaceControl::writeSurfaceToParcel(
257 const sp<SurfaceControl>& control, Parcel* parcel)
258{
259 uint32_t flags = 0;
260 uint32_t format = 0;
261 SurfaceID token = -1;
262 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700263 uint32_t width = 0;
264 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700265 sp<SurfaceComposerClient> client;
266 sp<ISurface> sur;
267 if (SurfaceControl::isValid(control)) {
268 token = control->mToken;
269 identity = control->mIdentity;
270 client = control->mClient;
271 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700272 width = control->mWidth;
273 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700274 format = control->mFormat;
275 flags = control->mFlags;
276 }
277 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
278 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
279 parcel->writeInt32(token);
280 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700281 parcel->writeInt32(width);
282 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700283 parcel->writeInt32(format);
284 parcel->writeInt32(flags);
285 return NO_ERROR;
286}
287
288sp<Surface> SurfaceControl::getSurface() const
289{
290 Mutex::Autolock _l(mLock);
291 if (mSurfaceData == 0) {
292 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
293 }
294 return mSurfaceData;
295}
296
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297// ============================================================================
298// Surface
299// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
Mathias Agopian01b76682009-04-16 20:04:08 -0700301Surface::Surface(const sp<SurfaceControl>& surface)
302 : mClient(surface->mClient), mSurface(surface->mSurface),
303 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700304 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian3330b202009-10-05 17:07:12 -0700305 mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700306 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700308 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700309 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700310
Mathias Agopian01b76682009-04-16 20:04:08 -0700311 init();
312}
Mathias Agopian62185b72009-04-16 16:19:50 -0700313
Mathias Agopian01b76682009-04-16 20:04:08 -0700314Surface::Surface(const Parcel& parcel)
Mathias Agopian3330b202009-10-05 17:07:12 -0700315 : mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL)
Mathias Agopian01b76682009-04-16 20:04:08 -0700316{
317 sp<IBinder> clientBinder = parcel.readStrongBinder();
318 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
319 mToken = parcel.readInt32();
320 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700321 mWidth = parcel.readInt32();
322 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700323 mFormat = parcel.readInt32();
324 mFlags = parcel.readInt32();
325
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700326 // FIXME: what does that mean if clientBinder is NULL here?
327 if (clientBinder != NULL) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700328 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
329
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700330 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700331 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700332 }
333
Mathias Agopian01b76682009-04-16 20:04:08 -0700334 init();
335}
336
337void Surface::init()
338{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340 android_native_window_t::dequeueBuffer = dequeueBuffer;
341 android_native_window_t::lockBuffer = lockBuffer;
342 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700343 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700344 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700346 DisplayInfo dinfo;
347 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
348 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
349 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
350 // FIXME: set real values here
351 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
352 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
353 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian52212712009-08-11 22:34:02 -0700354 // be default we request a hardware surface
355 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700356 mNeedFullUpdate = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357}
358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359Surface::~Surface()
360{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700361 // this is a client-side operation, the surface is destroyed, unmap
362 // its buffers in this process.
363 for (int i=0 ; i<2 ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700364 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700365 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700366 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700368
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700369 // clear all references and trigger an IPC now, to make sure things
370 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 mClient.clear();
372 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700373 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 IPCThreadState::self()->flushCommands();
375}
376
Mathias Agopianba5972f2009-08-14 18:52:17 -0700377sp<SurfaceComposerClient> Surface::getClient() const {
378 return mClient;
379}
380
381sp<ISurface> Surface::getISurface() const {
382 return mSurface;
383}
384
385bool Surface::isValid() {
386 return mToken>=0 && mClient!=0;
387}
388
Mathias Agopian963abad2009-11-13 15:26:29 -0800389status_t Surface::validate() const
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700390{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700391 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700392 if (mToken<0 || mClient==0) {
393 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopianba5972f2009-08-14 18:52:17 -0700394 mToken, mIdentity, client.get());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700395 return NO_INIT;
396 }
Mathias Agopian963abad2009-11-13 15:26:29 -0800397 SharedClient const* cblk = mClient->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700398 if (cblk == 0) {
399 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
400 return NO_INIT;
401 }
402 status_t err = cblk->validate(mToken);
403 if (err != NO_ERROR) {
404 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
405 mToken, mIdentity, err, strerror(-err));
406 return err;
407 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700408 uint32_t identity = cblk->getIdentity(mToken);
409 if (mIdentity != identity) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700410 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700411 mToken, mIdentity, identity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700412 return NO_INIT;
413 }
414 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415}
416
Mathias Agopian01b76682009-04-16 20:04:08 -0700417
418bool Surface::isSameSurface(
419 const sp<Surface>& lhs, const sp<Surface>& rhs)
420{
421 if (lhs == 0 || rhs == 0)
422 return false;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700423
Mathias Agopian01b76682009-04-16 20:04:08 -0700424 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
425}
426
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700427// ----------------------------------------------------------------------------
428
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700429int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700430 return 0;
431}
432
433int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700434 android_native_buffer_t** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700435 Surface* self = getSelf(window);
436 return self->dequeueBuffer(buffer);
437}
438
439int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700440 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700441 Surface* self = getSelf(window);
442 return self->lockBuffer(buffer);
443}
444
445int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700446 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700447 Surface* self = getSelf(window);
448 return self->queueBuffer(buffer);
449}
450
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700451int Surface::query(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700452 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700453 Surface* self = getSelf(window);
454 return self->query(what, value);
455}
456
Mathias Agopian52212712009-08-11 22:34:02 -0700457int Surface::perform(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700458 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700459 va_list args;
460 va_start(args, operation);
461 Surface* self = getSelf(window);
462 int res = self->perform(operation, args);
463 va_end(args);
464 return res;
465}
466
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700467// ----------------------------------------------------------------------------
468
Mathias Agopian3330b202009-10-05 17:07:12 -0700469status_t Surface::dequeueBuffer(sp<GraphicBuffer>* buffer) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700470 android_native_buffer_t* out;
471 status_t err = dequeueBuffer(&out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700472 if (err == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700473 *buffer = GraphicBuffer::getSelf(out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700474 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700475 return err;
476}
477
Mathias Agopian0926f502009-05-04 14:17:04 -0700478// ----------------------------------------------------------------------------
479
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700480
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700481int Surface::dequeueBuffer(android_native_buffer_t** buffer)
482{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700483 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800484 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700485 if (err != NO_ERROR)
486 return err;
487
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700488 ssize_t bufIdx = mSharedBufferClient->dequeue();
489 if (bufIdx < 0) {
490 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
491 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700492 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700493
494 // below we make sure we AT LEAST have the usage flags we want
495 const uint32_t usage(getUsage());
Mathias Agopian3330b202009-10-05 17:07:12 -0700496 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700497 if (backBuffer == 0 ||
498 ((uint32_t(backBuffer->usage) & usage) != usage) ||
499 mSharedBufferClient->needNewBuffer(bufIdx))
500 {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700501 err = getBufferLocked(bufIdx, usage);
502 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
503 bufIdx, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700504 if (err == NO_ERROR) {
505 // reset the width/height with the what we get from the buffer
Mathias Agopian50517542009-08-19 17:10:18 -0700506 mWidth = uint32_t(backBuffer->width);
507 mHeight = uint32_t(backBuffer->height);
508 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700509 }
510
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700511 // if we still don't have a buffer here, we probably ran out of memory
512 if (!err && backBuffer==0) {
513 err = NO_MEMORY;
514 }
515
Mathias Agopiancf81c842009-07-31 14:47:00 -0700516 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700517 mDirtyRegion.set(backBuffer->width, backBuffer->height);
518 *buffer = backBuffer.get();
519 } else {
520 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700521 }
Mathias Agopian50517542009-08-19 17:10:18 -0700522
Mathias Agopiancf81c842009-07-31 14:47:00 -0700523 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700524}
525
526int Surface::lockBuffer(android_native_buffer_t* buffer)
527{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700528 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800529 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700530 if (err != NO_ERROR)
531 return err;
532
Mathias Agopian3330b202009-10-05 17:07:12 -0700533 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700534 err = mSharedBufferClient->lock(bufIdx);
535 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
536 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537}
538
539int Surface::queueBuffer(android_native_buffer_t* buffer)
540{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700541 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800542 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543 if (err != NO_ERROR)
544 return err;
545
Mathias Agopian0926f502009-05-04 14:17:04 -0700546 if (mSwapRectangle.isValid()) {
547 mDirtyRegion.set(mSwapRectangle);
548 }
549
Mathias Agopian3330b202009-10-05 17:07:12 -0700550 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700551 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
552 err = mSharedBufferClient->queue(bufIdx);
553 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700555 if (err == NO_ERROR) {
556 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopianba5972f2009-08-14 18:52:17 -0700557 client->signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700558 }
559 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560}
561
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700562int Surface::query(int what, int* value)
563{
564 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700565 case NATIVE_WINDOW_WIDTH:
566 *value = int(mWidth);
567 return NO_ERROR;
568 case NATIVE_WINDOW_HEIGHT:
569 *value = int(mHeight);
570 return NO_ERROR;
571 case NATIVE_WINDOW_FORMAT:
572 *value = int(mFormat);
573 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700574 }
575 return BAD_VALUE;
576}
577
Mathias Agopian52212712009-08-11 22:34:02 -0700578int Surface::perform(int operation, va_list args)
579{
580 int res = NO_ERROR;
581 switch (operation) {
582 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700583 setUsage( va_arg(args, int) );
Mathias Agopian52212712009-08-11 22:34:02 -0700584 break;
585 default:
586 res = NAME_NOT_FOUND;
587 break;
588 }
589 return res;
590}
591
Mathias Agopianba5972f2009-08-14 18:52:17 -0700592void Surface::setUsage(uint32_t reqUsage)
593{
594 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700595 mUsage = reqUsage;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700596}
597
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700598uint32_t Surface::getUsage() const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700599{
600 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700601 return mUsage;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700602}
603
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604// ----------------------------------------------------------------------------
605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606status_t Surface::lock(SurfaceInfo* info, bool blocking) {
607 return Surface::lock(info, NULL, blocking);
608}
609
Mathias Agopian0926f502009-05-04 14:17:04 -0700610status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700612 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800613 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700614 CallStack stack;
615 stack.update();
616 stack.dump("Surface::lock called from different threads");
617 return WOULD_BLOCK;
618 }
Mathias Agopian90147262010-01-22 11:47:55 -0800619
620 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700621
Mathias Agopian90147262010-01-22 11:47:55 -0800622 if (mLockedBuffer != 0) {
623 LOGE("Surface::lock failed, already locked");
624 mApiLock.unlock();
625 return INVALID_OPERATION;
626 }
627
Mathias Agopian52212712009-08-11 22:34:02 -0700628 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700629 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
630
Mathias Agopian3330b202009-10-05 17:07:12 -0700631 sp<GraphicBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700632 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700633 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700634 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700635 err = lockBuffer(backBuffer.get());
636 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
637 backBuffer->getIndex(), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700638 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 // we handle copy-back here...
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700640
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700642 Region scratch(bounds);
643 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700645 if (mNeedFullUpdate) {
646 // reset newDirtyRegion to bounds when a buffer is reallocated
647 // it would be better if this information was associated with
648 // the buffer and made available to outside of Surface.
649 // This will do for now though.
650 mNeedFullUpdate = false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700651 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700652 } else {
653 newDirtyRegion.andSelf(bounds);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700654 }
655
Mathias Agopian3330b202009-10-05 17:07:12 -0700656 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700657 if (frontBuffer !=0 &&
658 backBuffer->width == frontBuffer->width &&
659 backBuffer->height == frontBuffer->height &&
660 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
661 {
662 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
663 if (!copyback.isEmpty() && frontBuffer!=0) {
664 // copy front to back
665 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700666 }
667 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700668
Mathias Agopian0926f502009-05-04 14:17:04 -0700669 mDirtyRegion = newDirtyRegion;
670 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700671
Mathias Agopiane71212b2009-05-05 00:37:46 -0700672 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700673 status_t res = backBuffer->lock(
674 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700675 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700676
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700677 LOGW_IF(res, "failed locking buffer (handle = %p)",
678 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700679
680 mLockedBuffer = backBuffer;
681 other->w = backBuffer->width;
682 other->h = backBuffer->height;
683 other->s = backBuffer->stride;
684 other->usage = backBuffer->usage;
685 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700686 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700687 }
688 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700689 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700690 return err;
691}
692
693status_t Surface::unlockAndPost()
694{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700695 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800696 LOGE("Surface::unlockAndPost failed, no locked buffer");
697 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700698 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700699
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700700 status_t err = mLockedBuffer->unlock();
701 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700702
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700703 err = queueBuffer(mLockedBuffer.get());
704 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
705 mLockedBuffer->getIndex(), strerror(-err));
706
707 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708 mLockedBuffer = 0;
709 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710}
711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700713 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 mSwapRectangle = r;
715}
716
Mathias Agopian52212712009-08-11 22:34:02 -0700717status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700719 sp<ISurface> s(mSurface);
720 if (s == 0) return NO_INIT;
721
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700722 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700723
724 // free the current buffer
Mathias Agopian3330b202009-10-05 17:07:12 -0700725 sp<GraphicBuffer>& currentBuffer(mBuffers[index]);
Mathias Agopian50517542009-08-19 17:10:18 -0700726 if (currentBuffer != 0) {
727 getBufferMapper().unregisterBuffer(currentBuffer->handle);
728 currentBuffer.clear();
729 }
730
Mathias Agopian3330b202009-10-05 17:07:12 -0700731 sp<GraphicBuffer> buffer = s->requestBuffer(index, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700732 LOGE_IF(buffer==0,
733 "ISurface::getBuffer(%d, %08x) returned NULL",
734 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700735 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700736 LOGE_IF(buffer->handle == NULL,
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700737 "Surface (identity=%d) requestBuffer(%d, %08x) returned"
738 "a buffer with a null handle", mIdentity, index, usage);
739 err = mSharedBufferClient->getStatus();
740 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
741 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -0700742 err = getBufferMapper().registerBuffer(buffer->handle);
743 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
744 err, strerror(-err));
745 if (err == NO_ERROR) {
746 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700747 currentBuffer->setIndex(index);
748 mNeedFullUpdate = true;
Mathias Agopian50517542009-08-19 17:10:18 -0700749 }
Mathias Agopiand3144be2009-10-06 15:58:44 -0700750 } else {
751 err = err<0 ? err : NO_MEMORY;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 }
753 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700754 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755}
756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757}; // namespace android
758