blob: 5dd75c32135f02b931677cfbeec5913f9c058e67 [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 Agopian55fa2512010-03-11 15:06:54 -0800356 mConnected = 0;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700357 mNeedFullUpdate = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358}
359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360Surface::~Surface()
361{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700362 // this is a client-side operation, the surface is destroyed, unmap
363 // its buffers in this process.
364 for (int i=0 ; i<2 ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700365 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700366 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700367 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700369
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700370 // clear all references and trigger an IPC now, to make sure things
371 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 mClient.clear();
373 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700374 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 IPCThreadState::self()->flushCommands();
376}
377
Mathias Agopianba5972f2009-08-14 18:52:17 -0700378sp<SurfaceComposerClient> Surface::getClient() const {
379 return mClient;
380}
381
382sp<ISurface> Surface::getISurface() const {
383 return mSurface;
384}
385
386bool Surface::isValid() {
387 return mToken>=0 && mClient!=0;
388}
389
Mathias Agopian963abad2009-11-13 15:26:29 -0800390status_t Surface::validate() const
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700391{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700392 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700393 if (mToken<0 || mClient==0) {
394 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopianba5972f2009-08-14 18:52:17 -0700395 mToken, mIdentity, client.get());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700396 return NO_INIT;
397 }
Mathias Agopian963abad2009-11-13 15:26:29 -0800398 SharedClient const* cblk = mClient->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700399 if (cblk == 0) {
400 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
401 return NO_INIT;
402 }
403 status_t err = cblk->validate(mToken);
404 if (err != NO_ERROR) {
405 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
406 mToken, mIdentity, err, strerror(-err));
407 return err;
408 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700409 uint32_t identity = cblk->getIdentity(mToken);
410 if (mIdentity != identity) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700411 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700412 mToken, mIdentity, identity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700413 return NO_INIT;
414 }
415 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416}
417
Mathias Agopian01b76682009-04-16 20:04:08 -0700418
419bool Surface::isSameSurface(
420 const sp<Surface>& lhs, const sp<Surface>& rhs)
421{
422 if (lhs == 0 || rhs == 0)
423 return false;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700424
Mathias Agopian01b76682009-04-16 20:04:08 -0700425 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
426}
427
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428// ----------------------------------------------------------------------------
429
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700430int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700431 return 0;
432}
433
434int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700435 android_native_buffer_t** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700436 Surface* self = getSelf(window);
437 return self->dequeueBuffer(buffer);
438}
439
440int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700441 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700442 Surface* self = getSelf(window);
443 return self->lockBuffer(buffer);
444}
445
446int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700447 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700448 Surface* self = getSelf(window);
449 return self->queueBuffer(buffer);
450}
451
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700452int Surface::query(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700453 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700454 Surface* self = getSelf(window);
455 return self->query(what, value);
456}
457
Mathias Agopian52212712009-08-11 22:34:02 -0700458int Surface::perform(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700459 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700460 va_list args;
461 va_start(args, operation);
462 Surface* self = getSelf(window);
463 int res = self->perform(operation, args);
464 va_end(args);
465 return res;
466}
467
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700468// ----------------------------------------------------------------------------
469
Mathias Agopian3330b202009-10-05 17:07:12 -0700470status_t Surface::dequeueBuffer(sp<GraphicBuffer>* buffer) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700471 android_native_buffer_t* out;
472 status_t err = dequeueBuffer(&out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700473 if (err == NO_ERROR) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700474 *buffer = GraphicBuffer::getSelf(out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700475 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700476 return err;
477}
478
Mathias Agopian0926f502009-05-04 14:17:04 -0700479// ----------------------------------------------------------------------------
480
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700481
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700482int Surface::dequeueBuffer(android_native_buffer_t** buffer)
483{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700484 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800485 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486 if (err != NO_ERROR)
487 return err;
488
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700489 ssize_t bufIdx = mSharedBufferClient->dequeue();
490 if (bufIdx < 0) {
491 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
492 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700493 }
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700494
495 // below we make sure we AT LEAST have the usage flags we want
496 const uint32_t usage(getUsage());
Mathias Agopian3330b202009-10-05 17:07:12 -0700497 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700498 if (backBuffer == 0 ||
499 ((uint32_t(backBuffer->usage) & usage) != usage) ||
500 mSharedBufferClient->needNewBuffer(bufIdx))
501 {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700502 err = getBufferLocked(bufIdx, usage);
503 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
504 bufIdx, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700505 if (err == NO_ERROR) {
506 // reset the width/height with the what we get from the buffer
Mathias Agopian50517542009-08-19 17:10:18 -0700507 mWidth = uint32_t(backBuffer->width);
508 mHeight = uint32_t(backBuffer->height);
509 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510 }
511
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700512 // if we still don't have a buffer here, we probably ran out of memory
513 if (!err && backBuffer==0) {
514 err = NO_MEMORY;
515 }
516
Mathias Agopiancf81c842009-07-31 14:47:00 -0700517 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700518 mDirtyRegion.set(backBuffer->width, backBuffer->height);
519 *buffer = backBuffer.get();
520 } else {
521 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700522 }
Mathias Agopian50517542009-08-19 17:10:18 -0700523
Mathias Agopiancf81c842009-07-31 14:47:00 -0700524 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700525}
526
527int Surface::lockBuffer(android_native_buffer_t* buffer)
528{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700529 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800530 status_t err = validate();
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700531 if (err != NO_ERROR)
532 return err;
533
Mathias Agopian3330b202009-10-05 17:07:12 -0700534 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700535 err = mSharedBufferClient->lock(bufIdx);
536 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
537 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538}
539
540int Surface::queueBuffer(android_native_buffer_t* buffer)
541{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700542 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian963abad2009-11-13 15:26:29 -0800543 status_t err = validate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700544 if (err != NO_ERROR)
545 return err;
546
Mathias Agopian0926f502009-05-04 14:17:04 -0700547 if (mSwapRectangle.isValid()) {
548 mDirtyRegion.set(mSwapRectangle);
549 }
550
Mathias Agopian3330b202009-10-05 17:07:12 -0700551 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700552 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
553 err = mSharedBufferClient->queue(bufIdx);
554 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700555
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700556 if (err == NO_ERROR) {
557 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopianba5972f2009-08-14 18:52:17 -0700558 client->signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700559 }
560 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700561}
562
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700563int Surface::query(int what, int* value)
564{
565 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700566 case NATIVE_WINDOW_WIDTH:
567 *value = int(mWidth);
568 return NO_ERROR;
569 case NATIVE_WINDOW_HEIGHT:
570 *value = int(mHeight);
571 return NO_ERROR;
572 case NATIVE_WINDOW_FORMAT:
573 *value = int(mFormat);
574 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700575 }
576 return BAD_VALUE;
577}
578
Mathias Agopian52212712009-08-11 22:34:02 -0700579int Surface::perform(int operation, va_list args)
580{
581 int res = NO_ERROR;
582 switch (operation) {
Mathias Agopian55fa2512010-03-11 15:06:54 -0800583 case NATIVE_WINDOW_SET_USAGE:
584 dispatch_setUsage( args );
585 break;
586 case NATIVE_WINDOW_CONNECT:
587 res = dispatch_connect( args );
588 break;
589 case NATIVE_WINDOW_DISCONNECT:
590 res = dispatch_disconnect( args );
591 break;
592 default:
593 res = NAME_NOT_FOUND;
594 break;
Mathias Agopian52212712009-08-11 22:34:02 -0700595 }
596 return res;
597}
598
Mathias Agopian55fa2512010-03-11 15:06:54 -0800599void Surface::dispatch_setUsage(va_list args) {
600 int usage = va_arg(args, int);
601 setUsage( usage );
602}
603int Surface::dispatch_connect(va_list args) {
604 int api = va_arg(args, int);
605 return connect( api );
606}
607int Surface::dispatch_disconnect(va_list args) {
608 int api = va_arg(args, int);
609 return disconnect( api );
610}
611
612
Mathias Agopianba5972f2009-08-14 18:52:17 -0700613void Surface::setUsage(uint32_t reqUsage)
614{
615 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700616 mUsage = reqUsage;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700617}
618
Mathias Agopian55fa2512010-03-11 15:06:54 -0800619int Surface::connect(int api)
620{
621 Mutex::Autolock _l(mSurfaceLock);
622 int err = NO_ERROR;
623 switch (api) {
624 case NATIVE_WINDOW_API_EGL:
625 if (mConnected) {
626 err = -EINVAL;
627 } else {
628 mConnected = api;
629 }
630 break;
631 default:
632 err = -EINVAL;
633 break;
634 }
635 return err;
636}
637
638int Surface::disconnect(int api)
639{
640 Mutex::Autolock _l(mSurfaceLock);
641 int err = NO_ERROR;
642 switch (api) {
643 case NATIVE_WINDOW_API_EGL:
644 if (mConnected == api) {
645 mConnected = 0;
646 } else {
647 err = -EINVAL;
648 }
649 break;
650 default:
651 err = -EINVAL;
652 break;
653 }
654 return err;
655}
656
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700657uint32_t Surface::getUsage() const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700658{
659 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700660 return mUsage;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700661}
662
Mathias Agopian55fa2512010-03-11 15:06:54 -0800663int Surface::getConnectedApi() const
664{
665 Mutex::Autolock _l(mSurfaceLock);
666 return mConnected;
667}
668
669
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670// ----------------------------------------------------------------------------
671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672status_t Surface::lock(SurfaceInfo* info, bool blocking) {
673 return Surface::lock(info, NULL, blocking);
674}
675
Mathias Agopian0926f502009-05-04 14:17:04 -0700676status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700677{
Mathias Agopian55fa2512010-03-11 15:06:54 -0800678 if (getConnectedApi()) {
679 LOGE("Surface::lock(%p) failed. Already connected to another API",
680 (android_native_window_t*)this);
681 CallStack stack;
682 stack.update();
683 stack.dump("");
684 return INVALID_OPERATION;
685 }
686
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700687 if (mApiLock.tryLock() != NO_ERROR) {
Mathias Agopian90147262010-01-22 11:47:55 -0800688 LOGE("calling Surface::lock from different threads!");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700689 CallStack stack;
690 stack.update();
Mathias Agopian55fa2512010-03-11 15:06:54 -0800691 stack.dump("");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700692 return WOULD_BLOCK;
693 }
Mathias Agopian90147262010-01-22 11:47:55 -0800694
695 /* Here we're holding mApiLock */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700696
Mathias Agopian90147262010-01-22 11:47:55 -0800697 if (mLockedBuffer != 0) {
698 LOGE("Surface::lock failed, already locked");
699 mApiLock.unlock();
700 return INVALID_OPERATION;
701 }
702
Mathias Agopian52212712009-08-11 22:34:02 -0700703 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700704 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
705
Mathias Agopian3330b202009-10-05 17:07:12 -0700706 sp<GraphicBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700707 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700708 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700709 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700710 err = lockBuffer(backBuffer.get());
711 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
712 backBuffer->getIndex(), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700713 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700714 // we handle copy-back here...
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700715
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700717 Region scratch(bounds);
718 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700719
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700720 if (mNeedFullUpdate) {
721 // reset newDirtyRegion to bounds when a buffer is reallocated
722 // it would be better if this information was associated with
723 // the buffer and made available to outside of Surface.
724 // This will do for now though.
725 mNeedFullUpdate = false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700726 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700727 } else {
728 newDirtyRegion.andSelf(bounds);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700729 }
730
Mathias Agopian3330b202009-10-05 17:07:12 -0700731 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700732 if (frontBuffer !=0 &&
733 backBuffer->width == frontBuffer->width &&
734 backBuffer->height == frontBuffer->height &&
735 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
736 {
737 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
738 if (!copyback.isEmpty() && frontBuffer!=0) {
739 // copy front to back
740 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700741 }
742 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700743
Mathias Agopian0926f502009-05-04 14:17:04 -0700744 mDirtyRegion = newDirtyRegion;
745 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700746
Mathias Agopiane71212b2009-05-05 00:37:46 -0700747 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700748 status_t res = backBuffer->lock(
749 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700750 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700751
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700752 LOGW_IF(res, "failed locking buffer (handle = %p)",
753 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700754
755 mLockedBuffer = backBuffer;
756 other->w = backBuffer->width;
757 other->h = backBuffer->height;
758 other->s = backBuffer->stride;
759 other->usage = backBuffer->usage;
760 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700761 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700762 }
763 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700764 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700765 return err;
766}
767
768status_t Surface::unlockAndPost()
769{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700770 if (mLockedBuffer == 0) {
Mathias Agopian90147262010-01-22 11:47:55 -0800771 LOGE("Surface::unlockAndPost failed, no locked buffer");
772 return INVALID_OPERATION;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700773 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700775 status_t err = mLockedBuffer->unlock();
776 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700777
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700778 err = queueBuffer(mLockedBuffer.get());
779 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
780 mLockedBuffer->getIndex(), strerror(-err));
781
782 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700783 mLockedBuffer = 0;
784 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785}
786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700788 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 mSwapRectangle = r;
790}
791
Mathias Agopian52212712009-08-11 22:34:02 -0700792status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700794 sp<ISurface> s(mSurface);
795 if (s == 0) return NO_INIT;
796
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700797 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700798
799 // free the current buffer
Mathias Agopian3330b202009-10-05 17:07:12 -0700800 sp<GraphicBuffer>& currentBuffer(mBuffers[index]);
Mathias Agopian50517542009-08-19 17:10:18 -0700801 if (currentBuffer != 0) {
802 getBufferMapper().unregisterBuffer(currentBuffer->handle);
803 currentBuffer.clear();
804 }
805
Mathias Agopian3330b202009-10-05 17:07:12 -0700806 sp<GraphicBuffer> buffer = s->requestBuffer(index, usage);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700807 LOGE_IF(buffer==0,
808 "ISurface::getBuffer(%d, %08x) returned NULL",
809 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700810 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700811 LOGE_IF(buffer->handle == NULL,
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700812 "Surface (identity=%d) requestBuffer(%d, %08x) returned"
813 "a buffer with a null handle", mIdentity, index, usage);
814 err = mSharedBufferClient->getStatus();
815 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
816 if (!err && buffer->handle != NULL) {
Mathias Agopian50517542009-08-19 17:10:18 -0700817 err = getBufferMapper().registerBuffer(buffer->handle);
818 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
819 err, strerror(-err));
820 if (err == NO_ERROR) {
821 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700822 currentBuffer->setIndex(index);
823 mNeedFullUpdate = true;
Mathias Agopian50517542009-08-19 17:10:18 -0700824 }
Mathias Agopiand3144be2009-10-06 15:58:44 -0700825 } else {
826 err = err<0 ? err : NO_MEMORY;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 }
828 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700829 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830}
831
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832}; // namespace android
833