blob: 64522fb6c374d41c8cff160d4d076be9cda15c0b [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 Agopian9779b2212009-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>
34#include <ui/BufferMapper.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <ui/ISurface.h>
36#include <ui/Surface.h>
37#include <ui/SurfaceComposerClient.h>
38#include <ui/Rect.h>
39
Mathias Agopianac2523b2009-05-05 18:11:11 -070040#include <pixelflinger/pixelflinger.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070041
Mathias Agopian9779b2212009-09-07 16:32:45 -070042#include <private/ui/SharedBufferStack.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <private/ui/LayerState.h>
Mathias Agopianac2523b2009-05-05 18:11:11 -070044#include <private/ui/SurfaceBuffer.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 Agopiandff8e582009-05-04 14:17:04 -070051 const sp<SurfaceBuffer>& dst,
52 const sp<SurfaceBuffer>& src,
53 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);
155 if (client == 0) return NO_INIT;
156 status_t err = validate(client->mControl);
157 if (err < 0) return err;
158 return client->setLayer(mToken, layer);
159}
160status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
161 const sp<SurfaceComposerClient>& client(mClient);
162 if (client == 0) return NO_INIT;
163 status_t err = validate(client->mControl);
164 if (err < 0) return err;
165 return client->setPosition(mToken, x, y);
166}
167status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
168 const sp<SurfaceComposerClient>& client(mClient);
169 if (client == 0) return NO_INIT;
170 status_t err = validate(client->mControl);
171 if (err < 0) return err;
172 return client->setSize(mToken, w, h);
173}
174status_t SurfaceControl::hide() {
175 const sp<SurfaceComposerClient>& client(mClient);
176 if (client == 0) return NO_INIT;
177 status_t err = validate(client->mControl);
178 if (err < 0) return err;
179 return client->hide(mToken);
180}
181status_t SurfaceControl::show(int32_t layer) {
182 const sp<SurfaceComposerClient>& client(mClient);
183 if (client == 0) return NO_INIT;
184 status_t err = validate(client->mControl);
185 if (err < 0) return err;
186 return client->show(mToken, layer);
187}
188status_t SurfaceControl::freeze() {
189 const sp<SurfaceComposerClient>& client(mClient);
190 if (client == 0) return NO_INIT;
191 status_t err = validate(client->mControl);
192 if (err < 0) return err;
193 return client->freeze(mToken);
194}
195status_t SurfaceControl::unfreeze() {
196 const sp<SurfaceComposerClient>& client(mClient);
197 if (client == 0) return NO_INIT;
198 status_t err = validate(client->mControl);
199 if (err < 0) return err;
200 return client->unfreeze(mToken);
201}
202status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
203 const sp<SurfaceComposerClient>& client(mClient);
204 if (client == 0) return NO_INIT;
205 status_t err = validate(client->mControl);
206 if (err < 0) return err;
207 return client->setFlags(mToken, flags, mask);
208}
209status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
210 const sp<SurfaceComposerClient>& client(mClient);
211 if (client == 0) return NO_INIT;
212 status_t err = validate(client->mControl);
213 if (err < 0) return err;
214 return client->setTransparentRegionHint(mToken, transparent);
215}
216status_t SurfaceControl::setAlpha(float alpha) {
217 const sp<SurfaceComposerClient>& client(mClient);
218 if (client == 0) return NO_INIT;
219 status_t err = validate(client->mControl);
220 if (err < 0) return err;
221 return client->setAlpha(mToken, alpha);
222}
223status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
224 const sp<SurfaceComposerClient>& client(mClient);
225 if (client == 0) return NO_INIT;
226 status_t err = validate(client->mControl);
227 if (err < 0) return err;
228 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
229}
230status_t SurfaceControl::setFreezeTint(uint32_t tint) {
231 const sp<SurfaceComposerClient>& client(mClient);
232 if (client == 0) return NO_INIT;
233 status_t err = validate(client->mControl);
234 if (err < 0) return err;
235 return client->setFreezeTint(mToken, tint);
236}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700237
Mathias Agopian9779b2212009-09-07 16:32:45 -0700238status_t SurfaceControl::validate(SharedClient const* cblk) const
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700239{
240 if (mToken<0 || mClient==0) {
241 LOGE("invalid token (%d, identity=%u) or client (%p)",
242 mToken, mIdentity, mClient.get());
243 return NO_INIT;
244 }
245 if (cblk == 0) {
246 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
247 return NO_INIT;
248 }
249 status_t err = cblk->validate(mToken);
250 if (err != NO_ERROR) {
251 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
252 mToken, mIdentity, err, strerror(-err));
253 return err;
254 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700255 uint32_t identity = cblk->getIdentity(mToken);
256 if (mIdentity != identity) {
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700257 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopian9779b2212009-09-07 16:32:45 -0700258 mToken, mIdentity, identity);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700259 return NO_INIT;
260 }
261 return NO_ERROR;
262}
263
Mathias Agopian17f638b2009-04-16 20:04:08 -0700264status_t SurfaceControl::writeSurfaceToParcel(
265 const sp<SurfaceControl>& control, Parcel* parcel)
266{
267 uint32_t flags = 0;
268 uint32_t format = 0;
269 SurfaceID token = -1;
270 uint32_t identity = 0;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700271 uint32_t width = 0;
272 uint32_t height = 0;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700273 sp<SurfaceComposerClient> client;
274 sp<ISurface> sur;
275 if (SurfaceControl::isValid(control)) {
276 token = control->mToken;
277 identity = control->mIdentity;
278 client = control->mClient;
279 sur = control->mSurface;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700280 width = control->mWidth;
281 height = control->mHeight;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700282 format = control->mFormat;
283 flags = control->mFlags;
284 }
285 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
286 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
287 parcel->writeInt32(token);
288 parcel->writeInt32(identity);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700289 parcel->writeInt32(width);
290 parcel->writeInt32(height);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700291 parcel->writeInt32(format);
292 parcel->writeInt32(flags);
293 return NO_ERROR;
294}
295
296sp<Surface> SurfaceControl::getSurface() const
297{
298 Mutex::Autolock _l(mLock);
299 if (mSurfaceData == 0) {
300 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
301 }
302 return mSurfaceData;
303}
304
Mathias Agopian1473f462009-04-10 14:24:30 -0700305// ============================================================================
306// Surface
307// ============================================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Mathias Agopian17f638b2009-04-16 20:04:08 -0700309Surface::Surface(const sp<SurfaceControl>& surface)
310 : mClient(surface->mClient), mSurface(surface->mSurface),
311 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopiandff8e582009-05-04 14:17:04 -0700312 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian9779b2212009-09-07 16:32:45 -0700313 mBufferMapper(BufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopian321abdb2009-08-14 18:52:17 -0700314 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315{
Mathias Agopian9779b2212009-09-07 16:32:45 -0700316 mSharedBufferClient = new SharedBufferClient(
317 mClient->mControl, mToken, 2);
318
Mathias Agopian17f638b2009-04-16 20:04:08 -0700319 init();
320}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700321
Mathias Agopian17f638b2009-04-16 20:04:08 -0700322Surface::Surface(const Parcel& parcel)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700323 : mBufferMapper(BufferMapper::get()), mSharedBufferClient(NULL)
Mathias Agopian17f638b2009-04-16 20:04:08 -0700324{
325 sp<IBinder> clientBinder = parcel.readStrongBinder();
326 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
327 mToken = parcel.readInt32();
328 mIdentity = parcel.readInt32();
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700329 mWidth = parcel.readInt32();
330 mHeight = parcel.readInt32();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700331 mFormat = parcel.readInt32();
332 mFlags = parcel.readInt32();
333
Mathias Agopian9779b2212009-09-07 16:32:45 -0700334 // FIXME: what does that mean if clientBinder is NULL here?
335 if (clientBinder != NULL) {
Mathias Agopian17f638b2009-04-16 20:04:08 -0700336 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
337
Mathias Agopian9779b2212009-09-07 16:32:45 -0700338 mSharedBufferClient = new SharedBufferClient(
339 mClient->mControl, mToken, 2);
340 }
341
Mathias Agopian17f638b2009-04-16 20:04:08 -0700342 init();
343}
344
345void Surface::init()
346{
Mathias Agopian1473f462009-04-10 14:24:30 -0700347 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian1473f462009-04-10 14:24:30 -0700348 android_native_window_t::dequeueBuffer = dequeueBuffer;
349 android_native_window_t::lockBuffer = lockBuffer;
350 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700351 android_native_window_t::query = query;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700352 android_native_window_t::perform = perform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 mSwapRectangle.makeInvalid();
Mathias Agopian1473f462009-04-10 14:24:30 -0700354 DisplayInfo dinfo;
355 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
356 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
357 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
358 // FIXME: set real values here
359 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
360 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
361 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700362 // be default we request a hardware surface
363 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700364 mNeedFullUpdate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365}
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367Surface::~Surface()
368{
Mathias Agopian402c3462009-04-14 18:21:47 -0700369 // this is a client-side operation, the surface is destroyed, unmap
370 // its buffers in this process.
371 for (int i=0 ; i<2 ; i++) {
Mathias Agopianb2f84502009-08-19 17:10:18 -0700372 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopiane633f932009-05-05 00:59:23 -0700373 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian1473f462009-04-10 14:24:30 -0700374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 }
Mathias Agopian402c3462009-04-14 18:21:47 -0700376
Mathias Agopian402c3462009-04-14 18:21:47 -0700377 // clear all references and trigger an IPC now, to make sure things
378 // happen without delay, since these resources are quite heavy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 mClient.clear();
380 mSurface.clear();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700381 delete mSharedBufferClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 IPCThreadState::self()->flushCommands();
383}
384
Mathias Agopian321abdb2009-08-14 18:52:17 -0700385sp<SurfaceComposerClient> Surface::getClient() const {
386 return mClient;
387}
388
389sp<ISurface> Surface::getISurface() const {
390 return mSurface;
391}
392
393bool Surface::isValid() {
394 return mToken>=0 && mClient!=0;
395}
396
Mathias Agopian9779b2212009-09-07 16:32:45 -0700397status_t Surface::validate(SharedClient const* cblk) const
Mathias Agopian1473f462009-04-10 14:24:30 -0700398{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700399 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian402c3462009-04-14 18:21:47 -0700400 if (mToken<0 || mClient==0) {
401 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopian321abdb2009-08-14 18:52:17 -0700402 mToken, mIdentity, client.get());
Mathias Agopian402c3462009-04-14 18:21:47 -0700403 return NO_INIT;
404 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700405 if (cblk == 0) {
406 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
407 return NO_INIT;
408 }
409 status_t err = cblk->validate(mToken);
410 if (err != NO_ERROR) {
411 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
412 mToken, mIdentity, err, strerror(-err));
413 return err;
414 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700415 uint32_t identity = cblk->getIdentity(mToken);
416 if (mIdentity != identity) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700417 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopian9779b2212009-09-07 16:32:45 -0700418 mToken, mIdentity, identity);
Mathias Agopian1473f462009-04-10 14:24:30 -0700419 return NO_INIT;
420 }
421 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422}
423
Mathias Agopian17f638b2009-04-16 20:04:08 -0700424
425bool Surface::isSameSurface(
426 const sp<Surface>& lhs, const sp<Surface>& rhs)
427{
428 if (lhs == 0 || rhs == 0)
429 return false;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700430
Mathias Agopian17f638b2009-04-16 20:04:08 -0700431 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
432}
433
Mathias Agopian1473f462009-04-10 14:24:30 -0700434// ----------------------------------------------------------------------------
435
Mathias Agopian9779b2212009-09-07 16:32:45 -0700436int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700437 return 0;
438}
439
440int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700441 android_native_buffer_t** buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700442 Surface* self = getSelf(window);
443 return self->dequeueBuffer(buffer);
444}
445
446int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700447 android_native_buffer_t* buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700448 Surface* self = getSelf(window);
449 return self->lockBuffer(buffer);
450}
451
452int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700453 android_native_buffer_t* buffer) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700454 Surface* self = getSelf(window);
455 return self->queueBuffer(buffer);
456}
457
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700458int Surface::query(android_native_window_t* window,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700459 int what, int* value) {
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700460 Surface* self = getSelf(window);
461 return self->query(what, value);
462}
463
Mathias Agopian5cec4742009-08-11 22:34:02 -0700464int Surface::perform(android_native_window_t* window,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700465 int operation, ...) {
Mathias Agopian5cec4742009-08-11 22:34:02 -0700466 va_list args;
467 va_start(args, operation);
468 Surface* self = getSelf(window);
469 int res = self->perform(operation, args);
470 va_end(args);
471 return res;
472}
473
Mathias Agopian1473f462009-04-10 14:24:30 -0700474// ----------------------------------------------------------------------------
475
Mathias Agopian9779b2212009-09-07 16:32:45 -0700476status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer) {
Mathias Agopiandff8e582009-05-04 14:17:04 -0700477 android_native_buffer_t* out;
478 status_t err = dequeueBuffer(&out);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700479 if (err == NO_ERROR) {
480 *buffer = SurfaceBuffer::getSelf(out);
481 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700482 return err;
483}
484
Mathias Agopiandff8e582009-05-04 14:17:04 -0700485// ----------------------------------------------------------------------------
486
Mathias Agopian9779b2212009-09-07 16:32:45 -0700487
Mathias Agopian1473f462009-04-10 14:24:30 -0700488int Surface::dequeueBuffer(android_native_buffer_t** buffer)
489{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700490 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700491 status_t err = validate(client->mControl);
Mathias Agopian1473f462009-04-10 14:24:30 -0700492 if (err != NO_ERROR)
493 return err;
494
Mathias Agopian9779b2212009-09-07 16:32:45 -0700495 ssize_t bufIdx = mSharedBufferClient->dequeue();
496 if (bufIdx < 0) {
497 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
498 return bufIdx;
Mathias Agopian9e2be202009-08-21 15:44:17 -0700499 }
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700500
501 // below we make sure we AT LEAST have the usage flags we want
502 const uint32_t usage(getUsage());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700503 const sp<SurfaceBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700504 if (backBuffer == 0 ||
505 ((uint32_t(backBuffer->usage) & usage) != usage) ||
506 mSharedBufferClient->needNewBuffer(bufIdx))
507 {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700508 err = getBufferLocked(bufIdx, usage);
509 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
510 bufIdx, usage, strerror(-err));
Mathias Agopianb2f84502009-08-19 17:10:18 -0700511 if (err == NO_ERROR) {
512 // reset the width/height with the what we get from the buffer
Mathias Agopianb2f84502009-08-19 17:10:18 -0700513 mWidth = uint32_t(backBuffer->width);
514 mHeight = uint32_t(backBuffer->height);
515 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700516 }
517
Mathias Agopian9779b2212009-09-07 16:32:45 -0700518 // if we still don't have a buffer here, we probably ran out of memory
519 if (!err && backBuffer==0) {
520 err = NO_MEMORY;
521 }
522
Mathias Agopianabac0102009-07-31 14:47:00 -0700523 if (err == NO_ERROR) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700524 mDirtyRegion.set(backBuffer->width, backBuffer->height);
525 *buffer = backBuffer.get();
526 } else {
527 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopianabac0102009-07-31 14:47:00 -0700528 }
Mathias Agopianb2f84502009-08-19 17:10:18 -0700529
Mathias Agopianabac0102009-07-31 14:47:00 -0700530 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700531}
532
533int Surface::lockBuffer(android_native_buffer_t* buffer)
534{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700535 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700536 status_t err = validate(client->mControl);
Mathias Agopian402c3462009-04-14 18:21:47 -0700537 if (err != NO_ERROR)
538 return err;
539
Mathias Agopian9779b2212009-09-07 16:32:45 -0700540 int32_t bufIdx = SurfaceBuffer::getSelf(buffer)->getIndex();
541 err = mSharedBufferClient->lock(bufIdx);
542 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
543 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700544}
545
546int Surface::queueBuffer(android_native_buffer_t* buffer)
547{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700548 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700549 status_t err = validate(client->mControl);
Mathias Agopian1473f462009-04-10 14:24:30 -0700550 if (err != NO_ERROR)
551 return err;
552
Mathias Agopiandff8e582009-05-04 14:17:04 -0700553 if (mSwapRectangle.isValid()) {
554 mDirtyRegion.set(mSwapRectangle);
555 }
556
Mathias Agopian9779b2212009-09-07 16:32:45 -0700557 int32_t bufIdx = SurfaceBuffer::getSelf(buffer)->getIndex();
558 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
559 err = mSharedBufferClient->queue(bufIdx);
560 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700561
Mathias Agopian9779b2212009-09-07 16:32:45 -0700562 if (err == NO_ERROR) {
563 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopian321abdb2009-08-14 18:52:17 -0700564 client->signalServer();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700565 }
566 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700567}
568
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700569int Surface::query(int what, int* value)
570{
571 switch (what) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700572 case NATIVE_WINDOW_WIDTH:
573 *value = int(mWidth);
574 return NO_ERROR;
575 case NATIVE_WINDOW_HEIGHT:
576 *value = int(mHeight);
577 return NO_ERROR;
578 case NATIVE_WINDOW_FORMAT:
579 *value = int(mFormat);
580 return NO_ERROR;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700581 }
582 return BAD_VALUE;
583}
584
Mathias Agopian5cec4742009-08-11 22:34:02 -0700585int Surface::perform(int operation, va_list args)
586{
587 int res = NO_ERROR;
588 switch (operation) {
589 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian321abdb2009-08-14 18:52:17 -0700590 setUsage( va_arg(args, int) );
Mathias Agopian5cec4742009-08-11 22:34:02 -0700591 break;
592 default:
593 res = NAME_NOT_FOUND;
594 break;
595 }
596 return res;
597}
598
Mathias Agopian321abdb2009-08-14 18:52:17 -0700599void Surface::setUsage(uint32_t reqUsage)
600{
601 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700602 mUsage = reqUsage;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700603}
604
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700605uint32_t Surface::getUsage() const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700606{
607 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700608 return mUsage;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700609}
610
Mathias Agopian1473f462009-04-10 14:24:30 -0700611// ----------------------------------------------------------------------------
612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613status_t Surface::lock(SurfaceInfo* info, bool blocking) {
614 return Surface::lock(info, NULL, blocking);
615}
616
Mathias Agopiandff8e582009-05-04 14:17:04 -0700617status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian1473f462009-04-10 14:24:30 -0700618{
Mathias Agopian9779b2212009-09-07 16:32:45 -0700619 if (mApiLock.tryLock() != NO_ERROR) {
620 LOGE("calling Surface::lock() from different threads!");
621 CallStack stack;
622 stack.update();
623 stack.dump("Surface::lock called from different threads");
624 return WOULD_BLOCK;
625 }
626
Mathias Agopian5cec4742009-08-11 22:34:02 -0700627 // we're intending to do software rendering from this point
Mathias Agopian321abdb2009-08-14 18:52:17 -0700628 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
629
Mathias Agopiandff8e582009-05-04 14:17:04 -0700630 sp<SurfaceBuffer> backBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700631 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700632 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700633 if (err == NO_ERROR) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700634 err = lockBuffer(backBuffer.get());
635 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
636 backBuffer->getIndex(), strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700637 if (err == NO_ERROR) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700638 // we handle copy-back here...
Mathias Agopian9779b2212009-09-07 16:32:45 -0700639
Mathias Agopian1473f462009-04-10 14:24:30 -0700640 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700641 Region scratch(bounds);
642 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian1473f462009-04-10 14:24:30 -0700643
Mathias Agopian9779b2212009-09-07 16:32:45 -0700644 if (mNeedFullUpdate) {
645 // reset newDirtyRegion to bounds when a buffer is reallocated
646 // it would be better if this information was associated with
647 // the buffer and made available to outside of Surface.
648 // This will do for now though.
649 mNeedFullUpdate = false;
Mathias Agopian1473f462009-04-10 14:24:30 -0700650 newDirtyRegion.set(bounds);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700651 } else {
652 newDirtyRegion.andSelf(bounds);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700653 }
654
655 const sp<SurfaceBuffer>& frontBuffer(mPostedBuffer);
656 if (frontBuffer !=0 &&
657 backBuffer->width == frontBuffer->width &&
658 backBuffer->height == frontBuffer->height &&
659 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
660 {
661 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
662 if (!copyback.isEmpty() && frontBuffer!=0) {
663 // copy front to back
664 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian1473f462009-04-10 14:24:30 -0700665 }
666 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700667
Mathias Agopiandff8e582009-05-04 14:17:04 -0700668 mDirtyRegion = newDirtyRegion;
669 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -0700670
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700671 void* vaddr;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700672 status_t res = backBuffer->lock(
673 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700674 newDirtyRegion.bounds(), &vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700675
Mathias Agopian9779b2212009-09-07 16:32:45 -0700676 LOGW_IF(res, "failed locking buffer (handle = %p)",
677 backBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700678
679 mLockedBuffer = backBuffer;
680 other->w = backBuffer->width;
681 other->h = backBuffer->height;
682 other->s = backBuffer->stride;
683 other->usage = backBuffer->usage;
684 other->format = backBuffer->format;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700685 other->bits = vaddr;
Mathias Agopian1473f462009-04-10 14:24:30 -0700686 }
687 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700688 mApiLock.unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700689 return err;
690}
691
692status_t Surface::unlockAndPost()
693{
Mathias Agopian9779b2212009-09-07 16:32:45 -0700694 if (mLockedBuffer == 0) {
695 LOGE("unlockAndPost failed, no locked buffer");
Mathias Agopian1473f462009-04-10 14:24:30 -0700696 return BAD_VALUE;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700697 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700698
Mathias Agopian9779b2212009-09-07 16:32:45 -0700699 status_t err = mLockedBuffer->unlock();
700 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700701
Mathias Agopian9779b2212009-09-07 16:32:45 -0700702 err = queueBuffer(mLockedBuffer.get());
703 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
704 mLockedBuffer->getIndex(), strerror(-err));
705
706 mPostedBuffer = mLockedBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700707 mLockedBuffer = 0;
708 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709}
710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopian321abdb2009-08-14 18:52:17 -0700712 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 mSwapRectangle = r;
714}
715
Mathias Agopian5cec4742009-08-11 22:34:02 -0700716status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700718 sp<ISurface> s(mSurface);
719 if (s == 0) return NO_INIT;
720
Mathias Agopian1473f462009-04-10 14:24:30 -0700721 status_t err = NO_MEMORY;
Mathias Agopianb2f84502009-08-19 17:10:18 -0700722
723 // free the current buffer
724 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
725 if (currentBuffer != 0) {
726 getBufferMapper().unregisterBuffer(currentBuffer->handle);
727 currentBuffer.clear();
728 }
729
Mathias Agopian9779b2212009-09-07 16:32:45 -0700730 sp<SurfaceBuffer> buffer = s->requestBuffer(index, usage);
731 LOGE_IF(buffer==0,
732 "ISurface::getBuffer(%d, %08x) returned NULL",
733 index, usage);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700734 if (buffer != 0) { // this should never happen by construction
Mathias Agopian9779b2212009-09-07 16:32:45 -0700735 LOGE_IF(buffer->handle == NULL,
736 "requestBuffer(%d, %08x) returned a buffer with a null handle",
737 index, usage);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700738 if (buffer->handle != NULL) {
739 err = getBufferMapper().registerBuffer(buffer->handle);
740 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
741 err, strerror(-err));
742 if (err == NO_ERROR) {
743 currentBuffer = buffer;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700744 currentBuffer->setIndex(index);
745 mNeedFullUpdate = true;
Mathias Agopianb2f84502009-08-19 17:10:18 -0700746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700749 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750}
751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752}; // namespace android
753