blob: 4dab3a245df84b3e5340e147675ca1a084b4c60d [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 Agopian9779b2212009-09-07 16:32:45 -0700500
Mathias Agopian486aa962009-09-15 17:34:04 -0700501 const uint32_t usage(getUsage());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700502 const sp<SurfaceBuffer>& backBuffer(mBuffers[bufIdx]);
Mathias Agopian486aa962009-09-15 17:34:04 -0700503 if (backBuffer == 0 ||
504 uint32_t(backBuffer->usage) != usage ||
505 mSharedBufferClient->needNewBuffer(bufIdx))
506 {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700507 err = getBufferLocked(bufIdx, usage);
508 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
509 bufIdx, usage, strerror(-err));
Mathias Agopianb2f84502009-08-19 17:10:18 -0700510 if (err == NO_ERROR) {
511 // reset the width/height with the what we get from the buffer
Mathias Agopianb2f84502009-08-19 17:10:18 -0700512 mWidth = uint32_t(backBuffer->width);
513 mHeight = uint32_t(backBuffer->height);
514 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700515 }
516
Mathias Agopian9779b2212009-09-07 16:32:45 -0700517 // if we still don't have a buffer here, we probably ran out of memory
518 if (!err && backBuffer==0) {
519 err = NO_MEMORY;
520 }
521
Mathias Agopianabac0102009-07-31 14:47:00 -0700522 if (err == NO_ERROR) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700523 mDirtyRegion.set(backBuffer->width, backBuffer->height);
524 *buffer = backBuffer.get();
525 } else {
526 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopianabac0102009-07-31 14:47:00 -0700527 }
Mathias Agopianb2f84502009-08-19 17:10:18 -0700528
Mathias Agopianabac0102009-07-31 14:47:00 -0700529 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700530}
531
532int Surface::lockBuffer(android_native_buffer_t* buffer)
533{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700534 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700535 status_t err = validate(client->mControl);
Mathias Agopian402c3462009-04-14 18:21:47 -0700536 if (err != NO_ERROR)
537 return err;
538
Mathias Agopian9779b2212009-09-07 16:32:45 -0700539 int32_t bufIdx = SurfaceBuffer::getSelf(buffer)->getIndex();
540 err = mSharedBufferClient->lock(bufIdx);
541 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
542 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700543}
544
545int Surface::queueBuffer(android_native_buffer_t* buffer)
546{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700547 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian9779b2212009-09-07 16:32:45 -0700548 status_t err = validate(client->mControl);
Mathias Agopian1473f462009-04-10 14:24:30 -0700549 if (err != NO_ERROR)
550 return err;
551
Mathias Agopiandff8e582009-05-04 14:17:04 -0700552 if (mSwapRectangle.isValid()) {
553 mDirtyRegion.set(mSwapRectangle);
554 }
555
Mathias Agopian9779b2212009-09-07 16:32:45 -0700556 int32_t bufIdx = SurfaceBuffer::getSelf(buffer)->getIndex();
557 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
558 err = mSharedBufferClient->queue(bufIdx);
559 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700560
Mathias Agopian9779b2212009-09-07 16:32:45 -0700561 if (err == NO_ERROR) {
562 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopian321abdb2009-08-14 18:52:17 -0700563 client->signalServer();
Mathias Agopian9779b2212009-09-07 16:32:45 -0700564 }
565 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700566}
567
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700568int Surface::query(int what, int* value)
569{
570 switch (what) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700571 case NATIVE_WINDOW_WIDTH:
572 *value = int(mWidth);
573 return NO_ERROR;
574 case NATIVE_WINDOW_HEIGHT:
575 *value = int(mHeight);
576 return NO_ERROR;
577 case NATIVE_WINDOW_FORMAT:
578 *value = int(mFormat);
579 return NO_ERROR;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700580 }
581 return BAD_VALUE;
582}
583
Mathias Agopian5cec4742009-08-11 22:34:02 -0700584int Surface::perform(int operation, va_list args)
585{
586 int res = NO_ERROR;
587 switch (operation) {
588 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian321abdb2009-08-14 18:52:17 -0700589 setUsage( va_arg(args, int) );
Mathias Agopian5cec4742009-08-11 22:34:02 -0700590 break;
591 default:
592 res = NAME_NOT_FOUND;
593 break;
594 }
595 return res;
596}
597
Mathias Agopian321abdb2009-08-14 18:52:17 -0700598void Surface::setUsage(uint32_t reqUsage)
599{
600 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian486aa962009-09-15 17:34:04 -0700601 mUsage = reqUsage;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700602}
603
Mathias Agopian486aa962009-09-15 17:34:04 -0700604uint32_t Surface::getUsage() const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700605{
606 Mutex::Autolock _l(mSurfaceLock);
Mathias Agopian486aa962009-09-15 17:34:04 -0700607 return mUsage;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700608}
609
Mathias Agopian1473f462009-04-10 14:24:30 -0700610// ----------------------------------------------------------------------------
611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612status_t Surface::lock(SurfaceInfo* info, bool blocking) {
613 return Surface::lock(info, NULL, blocking);
614}
615
Mathias Agopiandff8e582009-05-04 14:17:04 -0700616status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian1473f462009-04-10 14:24:30 -0700617{
Mathias Agopian9779b2212009-09-07 16:32:45 -0700618 if (mApiLock.tryLock() != NO_ERROR) {
619 LOGE("calling Surface::lock() from different threads!");
620 CallStack stack;
621 stack.update();
622 stack.dump("Surface::lock called from different threads");
623 return WOULD_BLOCK;
624 }
625
Mathias Agopian5cec4742009-08-11 22:34:02 -0700626 // we're intending to do software rendering from this point
Mathias Agopian321abdb2009-08-14 18:52:17 -0700627 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
628
Mathias Agopiandff8e582009-05-04 14:17:04 -0700629 sp<SurfaceBuffer> backBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700630 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700631 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700632 if (err == NO_ERROR) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700633 err = lockBuffer(backBuffer.get());
634 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
635 backBuffer->getIndex(), strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700636 if (err == NO_ERROR) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700637 // we handle copy-back here...
Mathias Agopian9779b2212009-09-07 16:32:45 -0700638
Mathias Agopian1473f462009-04-10 14:24:30 -0700639 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700640 Region scratch(bounds);
641 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian1473f462009-04-10 14:24:30 -0700642
Mathias Agopian9779b2212009-09-07 16:32:45 -0700643 if (mNeedFullUpdate) {
644 // reset newDirtyRegion to bounds when a buffer is reallocated
645 // it would be better if this information was associated with
646 // the buffer and made available to outside of Surface.
647 // This will do for now though.
648 mNeedFullUpdate = false;
Mathias Agopian1473f462009-04-10 14:24:30 -0700649 newDirtyRegion.set(bounds);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700650 } else {
651 newDirtyRegion.andSelf(bounds);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700652 }
653
654 const sp<SurfaceBuffer>& frontBuffer(mPostedBuffer);
655 if (frontBuffer !=0 &&
656 backBuffer->width == frontBuffer->width &&
657 backBuffer->height == frontBuffer->height &&
658 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
659 {
660 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
661 if (!copyback.isEmpty() && frontBuffer!=0) {
662 // copy front to back
663 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian1473f462009-04-10 14:24:30 -0700664 }
665 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700666
Mathias Agopiandff8e582009-05-04 14:17:04 -0700667 mDirtyRegion = newDirtyRegion;
668 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -0700669
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700670 void* vaddr;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700671 status_t res = backBuffer->lock(
672 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700673 newDirtyRegion.bounds(), &vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700674
Mathias Agopian9779b2212009-09-07 16:32:45 -0700675 LOGW_IF(res, "failed locking buffer (handle = %p)",
676 backBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700677
678 mLockedBuffer = backBuffer;
679 other->w = backBuffer->width;
680 other->h = backBuffer->height;
681 other->s = backBuffer->stride;
682 other->usage = backBuffer->usage;
683 other->format = backBuffer->format;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700684 other->bits = vaddr;
Mathias Agopian1473f462009-04-10 14:24:30 -0700685 }
686 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700687 mApiLock.unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700688 return err;
689}
690
691status_t Surface::unlockAndPost()
692{
Mathias Agopian9779b2212009-09-07 16:32:45 -0700693 if (mLockedBuffer == 0) {
694 LOGE("unlockAndPost failed, no locked buffer");
Mathias Agopian1473f462009-04-10 14:24:30 -0700695 return BAD_VALUE;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700696 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700697
Mathias Agopian9779b2212009-09-07 16:32:45 -0700698 status_t err = mLockedBuffer->unlock();
699 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700700
Mathias Agopian9779b2212009-09-07 16:32:45 -0700701 err = queueBuffer(mLockedBuffer.get());
702 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
703 mLockedBuffer->getIndex(), strerror(-err));
704
705 mPostedBuffer = mLockedBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700706 mLockedBuffer = 0;
707 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708}
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopian321abdb2009-08-14 18:52:17 -0700711 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 mSwapRectangle = r;
713}
714
Mathias Agopian5cec4742009-08-11 22:34:02 -0700715status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700717 sp<ISurface> s(mSurface);
718 if (s == 0) return NO_INIT;
719
Mathias Agopian1473f462009-04-10 14:24:30 -0700720 status_t err = NO_MEMORY;
Mathias Agopianb2f84502009-08-19 17:10:18 -0700721
722 // free the current buffer
723 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
724 if (currentBuffer != 0) {
725 getBufferMapper().unregisterBuffer(currentBuffer->handle);
726 currentBuffer.clear();
727 }
728
Mathias Agopian9779b2212009-09-07 16:32:45 -0700729 sp<SurfaceBuffer> buffer = s->requestBuffer(index, usage);
730 LOGE_IF(buffer==0,
731 "ISurface::getBuffer(%d, %08x) returned NULL",
732 index, usage);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700733 if (buffer != 0) { // this should never happen by construction
Mathias Agopian9779b2212009-09-07 16:32:45 -0700734 LOGE_IF(buffer->handle == NULL,
735 "requestBuffer(%d, %08x) returned a buffer with a null handle",
736 index, usage);
Mathias Agopianb2f84502009-08-19 17:10:18 -0700737 if (buffer->handle != NULL) {
738 err = getBufferMapper().registerBuffer(buffer->handle);
739 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
740 err, strerror(-err));
741 if (err == NO_ERROR) {
742 currentBuffer = buffer;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700743 currentBuffer->setIndex(index);
744 mNeedFullUpdate = true;
Mathias Agopianb2f84502009-08-19 17:10:18 -0700745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700748 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749}
750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751}; // namespace android
752