blob: 24ae27f286d4c8e1b8af730bfa21d0478443808b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Surface"
18
19#include <stdint.h>
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 Project9066cfe2009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopian9779b222009-09-07 16:32:45 -070028#include <utils/CallStack.h>
Mathias Agopian07952722009-05-19 19:08:10 -070029#include <binder/IPCThreadState.h>
30#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include <utils/Log.h>
32
Mathias Agopian1473f462009-04-10 14:24:30 -070033#include <ui/DisplayInfo.h>
Mathias Agopian6950e422009-10-05 17:07:12 -070034#include <ui/GraphicBuffer.h>
35#include <ui/GraphicBufferMapper.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <ui/ISurface.h>
37#include <ui/Surface.h>
38#include <ui/SurfaceComposerClient.h>
39#include <ui/Rect.h>
40
Mathias Agopianac2523b2009-05-05 18:11:11 -070041#include <pixelflinger/pixelflinger.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070042
Mathias Agopian9779b222009-09-07 16:32:45 -070043#include <private/ui/SharedBufferStack.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044#include <private/ui/LayerState.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046namespace android {
47
Mathias Agopian1473f462009-04-10 14:24:30 -070048// ----------------------------------------------------------------------
49
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070050static status_t copyBlt(
Mathias Agopian6950e422009-10-05 17:07:12 -070051 const sp<GraphicBuffer>& dst,
52 const sp<GraphicBuffer>& src,
Mathias Agopiandff8e582009-05-04 14:17:04 -070053 const Region& reg)
Mathias Agopian1473f462009-04-10 14:24:30 -070054{
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070055 status_t err;
56 uint8_t const * src_bits = NULL;
57 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
58 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopiandff8e582009-05-04 14:17:04 -070059
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070060 uint8_t* dst_bits = NULL;
61 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
62 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
63
64 Region::const_iterator head(reg.begin());
65 Region::const_iterator tail(reg.end());
66 if (head != tail && src_bits && dst_bits) {
Mathias Agopian1473f462009-04-10 14:24:30 -070067 // NOTE: dst and src must be the same format
Mathias Agopian1473f462009-04-10 14:24:30 -070068 const size_t bpp = bytesPerPixel(src->format);
69 const size_t dbpr = dst->stride * bpp;
70 const size_t sbpr = src->stride * bpp;
Mathias Agopiandff8e582009-05-04 14:17:04 -070071
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070072 while (head != tail) {
73 const Rect& r(*head++);
Mathias Agopiandff8e582009-05-04 14:17:04 -070074 ssize_t h = r.height();
75 if (h <= 0) continue;
76 size_t size = r.width() * bpp;
77 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
78 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
79 if (dbpr==sbpr && size==sbpr) {
80 size *= h;
81 h = 1;
Mathias Agopian1473f462009-04-10 14:24:30 -070082 }
Mathias Agopiandff8e582009-05-04 14:17:04 -070083 do {
84 memcpy(d, s, size);
85 d += dbpr;
86 s += sbpr;
87 } while (--h > 0);
Mathias Agopian1473f462009-04-10 14:24:30 -070088 }
89 }
Mathias Agopiandff8e582009-05-04 14:17:04 -070090
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -070091 if (src_bits)
92 src->unlock();
93
94 if (dst_bits)
95 dst->unlock();
96
97 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -070098}
99
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700100// ============================================================================
101// SurfaceControl
102// ============================================================================
103
Mathias Agopian17f638b2009-04-16 20:04:08 -0700104SurfaceControl::SurfaceControl(
105 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700106 const sp<ISurface>& surface,
107 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700109 : mClient(client), mSurface(surface),
110 mToken(data.token), mIdentity(data.identity),
Mathias Agopian18b6b492009-08-19 17:46:26 -0700111 mWidth(data.width), mHeight(data.height), mFormat(data.format),
112 mFlags(flags)
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700113{
114}
Mathias Agopian69d62092009-04-16 20:30:22 -0700115
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700116SurfaceControl::~SurfaceControl()
117{
118 destroy();
119}
120
121void SurfaceControl::destroy()
122{
Mathias Agopian69d62092009-04-16 20:30:22 -0700123 if (isValid()) {
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700124 mClient->destroySurface(mToken);
125 }
126
127 // clear all references and trigger an IPC now, to make sure things
128 // happen without delay, since these resources are quite heavy.
129 mClient.clear();
130 mSurface.clear();
131 IPCThreadState::self()->flushCommands();
132}
133
134void SurfaceControl::clear()
135{
136 // here, the window manager tells us explicitly that we should destroy
137 // the surface's resource. Soon after this call, it will also release
138 // its last reference (which will call the dtor); however, it is possible
139 // that a client living in the same process still holds references which
140 // would delay the call to the dtor -- that is why we need this explicit
141 // "clear()" call.
142 destroy();
143}
144
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700145bool SurfaceControl::isSameSurface(
146 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
147{
148 if (lhs == 0 || rhs == 0)
149 return false;
150 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
151}
152
Mathias Agopian17f638b2009-04-16 20:04:08 -0700153status_t SurfaceControl::setLayer(int32_t layer) {
154 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800155 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700156 if (err < 0) return err;
157 return client->setLayer(mToken, layer);
158}
159status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
160 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800161 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700162 if (err < 0) return err;
163 return client->setPosition(mToken, x, y);
164}
165status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
166 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800167 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700168 if (err < 0) return err;
169 return client->setSize(mToken, w, h);
170}
171status_t SurfaceControl::hide() {
172 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800173 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700174 if (err < 0) return err;
175 return client->hide(mToken);
176}
177status_t SurfaceControl::show(int32_t layer) {
178 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800179 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700180 if (err < 0) return err;
181 return client->show(mToken, layer);
182}
183status_t SurfaceControl::freeze() {
184 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800185 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700186 if (err < 0) return err;
187 return client->freeze(mToken);
188}
189status_t SurfaceControl::unfreeze() {
190 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800191 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700192 if (err < 0) return err;
193 return client->unfreeze(mToken);
194}
195status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
196 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800197 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700198 if (err < 0) return err;
199 return client->setFlags(mToken, flags, mask);
200}
201status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
202 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800203 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700204 if (err < 0) return err;
205 return client->setTransparentRegionHint(mToken, transparent);
206}
207status_t SurfaceControl::setAlpha(float alpha) {
208 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800209 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700210 if (err < 0) return err;
211 return client->setAlpha(mToken, alpha);
212}
213status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
214 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800215 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700216 if (err < 0) return err;
217 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
218}
219status_t SurfaceControl::setFreezeTint(uint32_t tint) {
220 const sp<SurfaceComposerClient>& client(mClient);
Mathias Agopian18e02602009-11-13 15:26:29 -0800221 status_t err = validate();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700222 if (err < 0) return err;
223 return client->setFreezeTint(mToken, tint);
224}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700225
Mathias Agopian18e02602009-11-13 15:26:29 -0800226status_t SurfaceControl::validate() const
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700227{
228 if (mToken<0 || mClient==0) {
229 LOGE("invalid token (%d, identity=%u) or client (%p)",
230 mToken, mIdentity, mClient.get());
231 return NO_INIT;
232 }
Mathias Agopian18e02602009-11-13 15:26:29 -0800233 SharedClient const* cblk = mClient->mControl;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700234 if (cblk == 0) {
235 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
236 return NO_INIT;
237 }
238 status_t err = cblk->validate(mToken);
239 if (err != NO_ERROR) {
240 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
241 mToken, mIdentity, err, strerror(-err));
242 return err;
243 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700244 uint32_t identity = cblk->getIdentity(mToken);
245 if (mIdentity != identity) {
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700246 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopian9779b222009-09-07 16:32:45 -0700247 mToken, mIdentity, identity);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700248 return NO_INIT;
249 }
250 return NO_ERROR;
251}
252
Mathias Agopian17f638b2009-04-16 20:04:08 -0700253status_t SurfaceControl::writeSurfaceToParcel(
254 const sp<SurfaceControl>& control, Parcel* parcel)
255{
256 uint32_t flags = 0;
257 uint32_t format = 0;
258 SurfaceID token = -1;
259 uint32_t identity = 0;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700260 uint32_t width = 0;
261 uint32_t height = 0;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700262 sp<SurfaceComposerClient> client;
263 sp<ISurface> sur;
264 if (SurfaceControl::isValid(control)) {
265 token = control->mToken;
266 identity = control->mIdentity;
267 client = control->mClient;
268 sur = control->mSurface;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700269 width = control->mWidth;
270 height = control->mHeight;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700271 format = control->mFormat;
272 flags = control->mFlags;
273 }
274 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
275 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
276 parcel->writeInt32(token);
277 parcel->writeInt32(identity);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700278 parcel->writeInt32(width);
279 parcel->writeInt32(height);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700280 parcel->writeInt32(format);
281 parcel->writeInt32(flags);
282 return NO_ERROR;
283}
284
285sp<Surface> SurfaceControl::getSurface() const
286{
287 Mutex::Autolock _l(mLock);
288 if (mSurfaceData == 0) {
289 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
290 }
291 return mSurfaceData;
292}
293
Mathias Agopian1473f462009-04-10 14:24:30 -0700294// ============================================================================
295// Surface
296// ============================================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Mathias Agopian17f638b2009-04-16 20:04:08 -0700298Surface::Surface(const sp<SurfaceControl>& surface)
299 : mClient(surface->mClient), mSurface(surface->mSurface),
300 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopiandff8e582009-05-04 14:17:04 -0700301 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian6950e422009-10-05 17:07:12 -0700302 mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopian321abdb2009-08-14 18:52:17 -0700303 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304{
Mathias Agopian9779b222009-09-07 16:32:45 -0700305 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian4961c952009-10-06 19:00:57 -0700306 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopian9779b222009-09-07 16:32:45 -0700307
Mathias Agopian17f638b2009-04-16 20:04:08 -0700308 init();
309}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700310
Mathias Agopian17f638b2009-04-16 20:04:08 -0700311Surface::Surface(const Parcel& parcel)
Mathias Agopian6950e422009-10-05 17:07:12 -0700312 : mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL)
Mathias Agopian17f638b2009-04-16 20:04:08 -0700313{
314 sp<IBinder> clientBinder = parcel.readStrongBinder();
315 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
316 mToken = parcel.readInt32();
317 mIdentity = parcel.readInt32();
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700318 mWidth = parcel.readInt32();
319 mHeight = parcel.readInt32();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700320 mFormat = parcel.readInt32();
321 mFlags = parcel.readInt32();
322
Mathias Agopian9779b222009-09-07 16:32:45 -0700323 // FIXME: what does that mean if clientBinder is NULL here?
324 if (clientBinder != NULL) {
Mathias Agopian17f638b2009-04-16 20:04:08 -0700325 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
326
Mathias Agopian9779b222009-09-07 16:32:45 -0700327 mSharedBufferClient = new SharedBufferClient(
Mathias Agopian4961c952009-10-06 19:00:57 -0700328 mClient->mControl, mToken, 2, mIdentity);
Mathias Agopian9779b222009-09-07 16:32:45 -0700329 }
330
Mathias Agopian17f638b2009-04-16 20:04:08 -0700331 init();
332}
333
334void Surface::init()
335{
Mathias Agopian1473f462009-04-10 14:24:30 -0700336 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian1473f462009-04-10 14:24:30 -0700337 android_native_window_t::dequeueBuffer = dequeueBuffer;
338 android_native_window_t::lockBuffer = lockBuffer;
339 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700340 android_native_window_t::query = query;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700341 android_native_window_t::perform = perform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 mSwapRectangle.makeInvalid();
Mathias Agopian1473f462009-04-10 14:24:30 -0700343 DisplayInfo dinfo;
344 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
345 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
346 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
347 // FIXME: set real values here
348 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
349 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
350 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700351 // be default we request a hardware surface
352 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopian9779b222009-09-07 16:32:45 -0700353 mNeedFullUpdate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354}
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356Surface::~Surface()
357{
Mathias Agopian402c3462009-04-14 18:21:47 -0700358 // this is a client-side operation, the surface is destroyed, unmap
359 // its buffers in this process.
360 for (int i=0 ; i<2 ; i++) {
Mathias Agopianb2f84502009-08-19 17:10:18 -0700361 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopiane633f932009-05-05 00:59:23 -0700362 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian1473f462009-04-10 14:24:30 -0700363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
Mathias Agopian402c3462009-04-14 18:21:47 -0700365
Mathias Agopian402c3462009-04-14 18:21:47 -0700366 // clear all references and trigger an IPC now, to make sure things
367 // happen without delay, since these resources are quite heavy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 mClient.clear();
369 mSurface.clear();
Mathias Agopian9779b222009-09-07 16:32:45 -0700370 delete mSharedBufferClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 IPCThreadState::self()->flushCommands();
372}
373
Mathias Agopian321abdb2009-08-14 18:52:17 -0700374sp<SurfaceComposerClient> Surface::getClient() const {
375 return mClient;
376}
377
378sp<ISurface> Surface::getISurface() const {
379 return mSurface;
380}
381
382bool Surface::isValid() {
383 return mToken>=0 && mClient!=0;
384}
385
Mathias Agopian18e02602009-11-13 15:26:29 -0800386status_t Surface::validate() const
Mathias Agopian1473f462009-04-10 14:24:30 -0700387{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700388 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian402c3462009-04-14 18:21:47 -0700389 if (mToken<0 || mClient==0) {
390 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopian321abdb2009-08-14 18:52:17 -0700391 mToken, mIdentity, client.get());
Mathias Agopian402c3462009-04-14 18:21:47 -0700392 return NO_INIT;
393 }
Mathias Agopian18e02602009-11-13 15:26:29 -0800394 SharedClient const* cblk = mClient->mControl;
Mathias Agopian1473f462009-04-10 14:24:30 -0700395 if (cblk == 0) {
396 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
397 return NO_INIT;
398 }
399 status_t err = cblk->validate(mToken);
400 if (err != NO_ERROR) {
401 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
402 mToken, mIdentity, err, strerror(-err));
403 return err;
404 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700405 uint32_t identity = cblk->getIdentity(mToken);
406 if (mIdentity != identity) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700407 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopian9779b222009-09-07 16:32:45 -0700408 mToken, mIdentity, identity);
Mathias Agopian1473f462009-04-10 14:24:30 -0700409 return NO_INIT;
410 }
411 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412}
413
Mathias Agopian17f638b2009-04-16 20:04:08 -0700414
415bool Surface::isSameSurface(
416 const sp<Surface>& lhs, const sp<Surface>& rhs)
417{
418 if (lhs == 0 || rhs == 0)
419 return false;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700420
Mathias Agopian17f638b2009-04-16 20:04:08 -0700421 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
422}
423
Mathias Agopian1473f462009-04-10 14:24:30 -0700424// ----------------------------------------------------------------------------
425
Mathias Agopian9779b222009-09-07 16:32:45 -0700426int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700427 return 0;
428}
429
430int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700431 android_native_buffer_t** buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700432 Surface* self = getSelf(window);
433 return self->dequeueBuffer(buffer);
434}
435
436int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700437 android_native_buffer_t* buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700438 Surface* self = getSelf(window);
439 return self->lockBuffer(buffer);
440}
441
442int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700443 android_native_buffer_t* buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700444 Surface* self = getSelf(window);
445 return self->queueBuffer(buffer);
446}
447
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700448int Surface::query(android_native_window_t* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700449 int what, int* value) {
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700450 Surface* self = getSelf(window);
451 return self->query(what, value);
452}
453
Mathias Agopian5cec4742009-08-11 22:34:02 -0700454int Surface::perform(android_native_window_t* window,
Mathias Agopian9779b222009-09-07 16:32:45 -0700455 int operation, ...) {
Mathias Agopian5cec4742009-08-11 22:34:02 -0700456 va_list args;
457 va_start(args, operation);
458 Surface* self = getSelf(window);
459 int res = self->perform(operation, args);
460 va_end(args);
461 return res;
462}
463
Mathias Agopian1473f462009-04-10 14:24:30 -0700464// ----------------------------------------------------------------------------
465
Mathias Agopian6950e422009-10-05 17:07:12 -0700466status_t Surface::dequeueBuffer(sp<GraphicBuffer>* buffer) {
Mathias Agopiandff8e582009-05-04 14:17:04 -0700467 android_native_buffer_t* out;
468 status_t err = dequeueBuffer(&out);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700469 if (err == NO_ERROR) {
Mathias Agopian6950e422009-10-05 17:07:12 -0700470 *buffer = GraphicBuffer::getSelf(out);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700471 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700472 return err;
473}
474
Mathias Agopiandff8e582009-05-04 14:17:04 -0700475// ----------------------------------------------------------------------------
476
Mathias Agopian9779b222009-09-07 16:32:45 -0700477
Mathias Agopian1473f462009-04-10 14:24:30 -0700478int Surface::dequeueBuffer(android_native_buffer_t** buffer)
479{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700480 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian18e02602009-11-13 15:26:29 -0800481 status_t err = validate();
Mathias Agopian1473f462009-04-10 14:24:30 -0700482 if (err != NO_ERROR)
483 return err;
484
Mathias Agopian9779b222009-09-07 16:32:45 -0700485 ssize_t bufIdx = mSharedBufferClient->dequeue();
486 if (bufIdx < 0) {
487 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
488 return bufIdx;
Mathias Agopian9e2be202009-08-21 15:44:17 -0700489 }
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700490
491 // below we make sure we AT LEAST have the usage flags we want
492 const uint32_t usage(getUsage());
Mathias Agopian6950e422009-10-05 17:07:12 -0700493 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700494 if (backBuffer == 0 ||
495 ((uint32_t(backBuffer->usage) & usage) != usage) ||
496 mSharedBufferClient->needNewBuffer(bufIdx))
497 {
Mathias Agopian9779b222009-09-07 16:32:45 -0700498 err = getBufferLocked(bufIdx, usage);
499 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
500 bufIdx, usage, strerror(-err));
Mathias Agopianb2f84502009-08-19 17:10:18 -0700501 if (err == NO_ERROR) {
502 // reset the width/height with the what we get from the buffer
Mathias Agopianb2f84502009-08-19 17:10:18 -0700503 mWidth = uint32_t(backBuffer->width);
504 mHeight = uint32_t(backBuffer->height);
505 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700506 }
507
Mathias Agopian9779b222009-09-07 16:32:45 -0700508 // if we still don't have a buffer here, we probably ran out of memory
509 if (!err && backBuffer==0) {
510 err = NO_MEMORY;
511 }
512
Mathias Agopianabac0102009-07-31 14:47:00 -0700513 if (err == NO_ERROR) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700514 mDirtyRegion.set(backBuffer->width, backBuffer->height);
515 *buffer = backBuffer.get();
516 } else {
517 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopianabac0102009-07-31 14:47:00 -0700518 }
Mathias Agopianb2f84502009-08-19 17:10:18 -0700519
Mathias Agopianabac0102009-07-31 14:47:00 -0700520 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700521}
522
523int Surface::lockBuffer(android_native_buffer_t* buffer)
524{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700525 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian18e02602009-11-13 15:26:29 -0800526 status_t err = validate();
Mathias Agopian402c3462009-04-14 18:21:47 -0700527 if (err != NO_ERROR)
528 return err;
529
Mathias Agopian6950e422009-10-05 17:07:12 -0700530 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopian9779b222009-09-07 16:32:45 -0700531 err = mSharedBufferClient->lock(bufIdx);
532 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
533 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700534}
535
536int Surface::queueBuffer(android_native_buffer_t* buffer)
537{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700538 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian18e02602009-11-13 15:26:29 -0800539 status_t err = validate();
Mathias Agopian1473f462009-04-10 14:24:30 -0700540 if (err != NO_ERROR)
541 return err;
542
Mathias Agopiandff8e582009-05-04 14:17:04 -0700543 if (mSwapRectangle.isValid()) {
544 mDirtyRegion.set(mSwapRectangle);
545 }
546
Mathias Agopian6950e422009-10-05 17:07:12 -0700547 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopian9779b222009-09-07 16:32:45 -0700548 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
549 err = mSharedBufferClient->queue(bufIdx);
550 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700551
Mathias Agopian9779b222009-09-07 16:32:45 -0700552 if (err == NO_ERROR) {
553 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopian321abdb2009-08-14 18:52:17 -0700554 client->signalServer();
Mathias Agopian9779b222009-09-07 16:32:45 -0700555 }
556 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700557}
558
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700559int Surface::query(int what, int* value)
560{
561 switch (what) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700562 case NATIVE_WINDOW_WIDTH:
563 *value = int(mWidth);
564 return NO_ERROR;
565 case NATIVE_WINDOW_HEIGHT:
566 *value = int(mHeight);
567 return NO_ERROR;
568 case NATIVE_WINDOW_FORMAT:
569 *value = int(mFormat);
570 return NO_ERROR;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700571 }
572 return BAD_VALUE;
573}
574
Mathias Agopian5cec4742009-08-11 22:34:02 -0700575int Surface::perform(int operation, va_list args)
576{
577 int res = NO_ERROR;
578 switch (operation) {
579 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian321abdb2009-08-14 18:52:17 -0700580 setUsage( va_arg(args, int) );
Mathias Agopian5cec4742009-08-11 22:34:02 -0700581 break;
582 default:
583 res = NAME_NOT_FOUND;
584 break;
585 }
586 return res;
587}
588
Mathias Agopian321abdb2009-08-14 18:52:17 -0700589void Surface::setUsage(uint32_t reqUsage)
590{
591 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700592 mUsage = reqUsage;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700593}
594
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700595uint32_t Surface::getUsage() const
Mathias Agopian9779b222009-09-07 16:32:45 -0700596{
597 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700598 return mUsage;
Mathias Agopian9779b222009-09-07 16:32:45 -0700599}
600
Mathias Agopian1473f462009-04-10 14:24:30 -0700601// ----------------------------------------------------------------------------
602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603status_t Surface::lock(SurfaceInfo* info, bool blocking) {
604 return Surface::lock(info, NULL, blocking);
605}
606
Mathias Agopiandff8e582009-05-04 14:17:04 -0700607status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian1473f462009-04-10 14:24:30 -0700608{
Mathias Agopian9779b222009-09-07 16:32:45 -0700609 if (mApiLock.tryLock() != NO_ERROR) {
610 LOGE("calling Surface::lock() from different threads!");
611 CallStack stack;
612 stack.update();
613 stack.dump("Surface::lock called from different threads");
614 return WOULD_BLOCK;
615 }
616
Mathias Agopian5cec4742009-08-11 22:34:02 -0700617 // we're intending to do software rendering from this point
Mathias Agopian321abdb2009-08-14 18:52:17 -0700618 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
619
Mathias Agopian6950e422009-10-05 17:07:12 -0700620 sp<GraphicBuffer> backBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700621 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopian9779b222009-09-07 16:32:45 -0700622 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700623 if (err == NO_ERROR) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700624 err = lockBuffer(backBuffer.get());
625 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
626 backBuffer->getIndex(), strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700627 if (err == NO_ERROR) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700628 // we handle copy-back here...
Mathias Agopian9779b222009-09-07 16:32:45 -0700629
Mathias Agopian1473f462009-04-10 14:24:30 -0700630 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700631 Region scratch(bounds);
632 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian1473f462009-04-10 14:24:30 -0700633
Mathias Agopian9779b222009-09-07 16:32:45 -0700634 if (mNeedFullUpdate) {
635 // reset newDirtyRegion to bounds when a buffer is reallocated
636 // it would be better if this information was associated with
637 // the buffer and made available to outside of Surface.
638 // This will do for now though.
639 mNeedFullUpdate = false;
Mathias Agopian1473f462009-04-10 14:24:30 -0700640 newDirtyRegion.set(bounds);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700641 } else {
642 newDirtyRegion.andSelf(bounds);
Mathias Agopian9779b222009-09-07 16:32:45 -0700643 }
644
Mathias Agopian6950e422009-10-05 17:07:12 -0700645 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopian9779b222009-09-07 16:32:45 -0700646 if (frontBuffer !=0 &&
647 backBuffer->width == frontBuffer->width &&
648 backBuffer->height == frontBuffer->height &&
649 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
650 {
651 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
652 if (!copyback.isEmpty() && frontBuffer!=0) {
653 // copy front to back
654 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian1473f462009-04-10 14:24:30 -0700655 }
656 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700657
Mathias Agopiandff8e582009-05-04 14:17:04 -0700658 mDirtyRegion = newDirtyRegion;
659 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -0700660
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700661 void* vaddr;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700662 status_t res = backBuffer->lock(
663 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700664 newDirtyRegion.bounds(), &vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700665
Mathias Agopian9779b222009-09-07 16:32:45 -0700666 LOGW_IF(res, "failed locking buffer (handle = %p)",
667 backBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700668
669 mLockedBuffer = backBuffer;
670 other->w = backBuffer->width;
671 other->h = backBuffer->height;
672 other->s = backBuffer->stride;
673 other->usage = backBuffer->usage;
674 other->format = backBuffer->format;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700675 other->bits = vaddr;
Mathias Agopian1473f462009-04-10 14:24:30 -0700676 }
677 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700678 mApiLock.unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700679 return err;
680}
681
682status_t Surface::unlockAndPost()
683{
Mathias Agopian9779b222009-09-07 16:32:45 -0700684 if (mLockedBuffer == 0) {
685 LOGE("unlockAndPost failed, no locked buffer");
Mathias Agopian1473f462009-04-10 14:24:30 -0700686 return BAD_VALUE;
Mathias Agopian9779b222009-09-07 16:32:45 -0700687 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700688
Mathias Agopian9779b222009-09-07 16:32:45 -0700689 status_t err = mLockedBuffer->unlock();
690 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700691
Mathias Agopian9779b222009-09-07 16:32:45 -0700692 err = queueBuffer(mLockedBuffer.get());
693 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
694 mLockedBuffer->getIndex(), strerror(-err));
695
696 mPostedBuffer = mLockedBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700697 mLockedBuffer = 0;
698 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699}
700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopian321abdb2009-08-14 18:52:17 -0700702 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 mSwapRectangle = r;
704}
705
Mathias Agopian5cec4742009-08-11 22:34:02 -0700706status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700708 sp<ISurface> s(mSurface);
709 if (s == 0) return NO_INIT;
710
Mathias Agopian1473f462009-04-10 14:24:30 -0700711 status_t err = NO_MEMORY;
Mathias Agopianb2f84502009-08-19 17:10:18 -0700712
713 // free the current buffer
Mathias Agopian6950e422009-10-05 17:07:12 -0700714 sp<GraphicBuffer>& currentBuffer(mBuffers[index]);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700715 if (currentBuffer != 0) {
716 getBufferMapper().unregisterBuffer(currentBuffer->handle);
717 currentBuffer.clear();
718 }
719
Mathias Agopian6950e422009-10-05 17:07:12 -0700720 sp<GraphicBuffer> buffer = s->requestBuffer(index, usage);
Mathias Agopian9779b222009-09-07 16:32:45 -0700721 LOGE_IF(buffer==0,
722 "ISurface::getBuffer(%d, %08x) returned NULL",
723 index, usage);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700724 if (buffer != 0) { // this should never happen by construction
Mathias Agopian9779b222009-09-07 16:32:45 -0700725 LOGE_IF(buffer->handle == NULL,
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700726 "Surface (identity=%d) requestBuffer(%d, %08x) returned"
727 "a buffer with a null handle", mIdentity, index, usage);
728 err = mSharedBufferClient->getStatus();
729 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
730 if (!err && buffer->handle != NULL) {
Mathias Agopianb2f84502009-08-19 17:10:18 -0700731 err = getBufferMapper().registerBuffer(buffer->handle);
732 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
733 err, strerror(-err));
734 if (err == NO_ERROR) {
735 currentBuffer = buffer;
Mathias Agopian9779b222009-09-07 16:32:45 -0700736 currentBuffer->setIndex(index);
737 mNeedFullUpdate = true;
Mathias Agopianb2f84502009-08-19 17:10:18 -0700738 }
Mathias Agopian0da41a32009-10-06 15:58:44 -0700739 } else {
740 err = err<0 ? err : NO_MEMORY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700743 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744}
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746}; // namespace android
747