blob: c3fbea271fd7249e10ec5a4079d9c641942ae774 [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 Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/IPCThreadState.h>
30#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/Log.h>
32
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <ui/DisplayInfo.h>
34#include <ui/BufferMapper.h>
The Android Open Source Projectedbf3b62009-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 Agopian7189c002009-05-05 18:11:11 -070040#include <pixelflinger/pixelflinger.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
Mathias Agopiancbb288b2009-09-07 16:32:45 -070042#include <private/ui/SharedBufferStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <private/ui/LayerState.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070044#include <private/ui/SurfaceBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046namespace android {
47
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048// ----------------------------------------------------------------------
49
Mathias Agopian14998592009-07-13 18:29:59 -070050static status_t copyBlt(
Mathias Agopian0926f502009-05-04 14:17:04 -070051 const sp<SurfaceBuffer>& dst,
52 const sp<SurfaceBuffer>& src,
53 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054{
Mathias Agopian14998592009-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 Agopian0926f502009-05-04 14:17:04 -070059
Mathias Agopian14998592009-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 Agopian076b1cc2009-04-10 14:24:30 -070067 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-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 Agopian0926f502009-05-04 14:17:04 -070071
Mathias Agopian14998592009-07-13 18:29:59 -070072 while (head != tail) {
73 const Rect& r(*head++);
Mathias Agopian0926f502009-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 Agopian076b1cc2009-04-10 14:24:30 -070082 }
Mathias Agopian0926f502009-05-04 14:17:04 -070083 do {
84 memcpy(d, s, size);
85 d += dbpr;
86 s += sbpr;
87 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 }
89 }
Mathias Agopian0926f502009-05-04 14:17:04 -070090
Mathias Agopian14998592009-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 Agopian076b1cc2009-04-10 14:24:30 -070098}
99
Mathias Agopian62185b72009-04-16 16:19:50 -0700100// ============================================================================
101// SurfaceControl
102// ============================================================================
103
Mathias Agopian01b76682009-04-16 20:04:08 -0700104SurfaceControl::SurfaceControl(
105 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700106 const sp<ISurface>& surface,
107 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700109 : mClient(client), mSurface(surface),
110 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700111 mWidth(data.width), mHeight(data.height), mFormat(data.format),
112 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700113{
114}
Mathias Agopian18d84462009-04-16 20:30:22 -0700115
Mathias Agopian62185b72009-04-16 16:19:50 -0700116SurfaceControl::~SurfaceControl()
117{
118 destroy();
119}
120
121void SurfaceControl::destroy()
122{
Mathias Agopian18d84462009-04-16 20:30:22 -0700123 if (isValid()) {
Mathias Agopian62185b72009-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 Agopian62185b72009-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 Agopian01b76682009-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 Agopian62185b72009-04-16 16:19:50 -0700237
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700238status_t SurfaceControl::validate(SharedClient const* cblk) const
Mathias Agopian62185b72009-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 Agopiancbb288b2009-09-07 16:32:45 -0700255 uint32_t identity = cblk->getIdentity(mToken);
256 if (mIdentity != identity) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700257 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700258 mToken, mIdentity, identity);
Mathias Agopian62185b72009-04-16 16:19:50 -0700259 return NO_INIT;
260 }
261 return NO_ERROR;
262}
263
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700271 uint32_t width = 0;
272 uint32_t height = 0;
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700280 width = control->mWidth;
281 height = control->mHeight;
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700289 parcel->writeInt32(width);
290 parcel->writeInt32(height);
Mathias Agopian01b76682009-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 Agopian076b1cc2009-04-10 14:24:30 -0700305// ============================================================================
306// Surface
307// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
Mathias Agopian01b76682009-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 Agopian0926f502009-05-04 14:17:04 -0700312 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700313 mBufferMapper(BufferMapper::get()), mSharedBufferClient(NULL),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700314 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700316 mSharedBufferClient = new SharedBufferClient(
317 mClient->mControl, mToken, 2);
318
Mathias Agopian01b76682009-04-16 20:04:08 -0700319 init();
320}
Mathias Agopian62185b72009-04-16 16:19:50 -0700321
Mathias Agopian01b76682009-04-16 20:04:08 -0700322Surface::Surface(const Parcel& parcel)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700323 : mBufferMapper(BufferMapper::get()), mSharedBufferClient(NULL)
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700329 mWidth = parcel.readInt32();
330 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700331 mFormat = parcel.readInt32();
332 mFlags = parcel.readInt32();
333
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700334 // FIXME: what does that mean if clientBinder is NULL here?
335 if (clientBinder != NULL) {
Mathias Agopian01b76682009-04-16 20:04:08 -0700336 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
337
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700338 mSharedBufferClient = new SharedBufferClient(
339 mClient->mControl, mToken, 2);
340 }
341
Mathias Agopian01b76682009-04-16 20:04:08 -0700342 init();
343}
344
345void Surface::init()
346{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-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 Agopiancb6b9042009-07-30 18:14:56 -0700351 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700352 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-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 Agopian52212712009-08-11 22:34:02 -0700362 // be default we request a hardware surface
363 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700364 mUsageChanged = true;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700365 mNeedFullUpdate = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366}
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368Surface::~Surface()
369{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700370 // this is a client-side operation, the surface is destroyed, unmap
371 // its buffers in this process.
372 for (int i=0 ; i<2 ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700373 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700374 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700375 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700377
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700378 // clear all references and trigger an IPC now, to make sure things
379 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 mClient.clear();
381 mSurface.clear();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700382 delete mSharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 IPCThreadState::self()->flushCommands();
384}
385
Mathias Agopianba5972f2009-08-14 18:52:17 -0700386sp<SurfaceComposerClient> Surface::getClient() const {
387 return mClient;
388}
389
390sp<ISurface> Surface::getISurface() const {
391 return mSurface;
392}
393
394bool Surface::isValid() {
395 return mToken>=0 && mClient!=0;
396}
397
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398status_t Surface::validate(SharedClient const* cblk) const
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700399{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700400 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700401 if (mToken<0 || mClient==0) {
402 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopianba5972f2009-08-14 18:52:17 -0700403 mToken, mIdentity, client.get());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700404 return NO_INIT;
405 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700406 if (cblk == 0) {
407 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
408 return NO_INIT;
409 }
410 status_t err = cblk->validate(mToken);
411 if (err != NO_ERROR) {
412 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
413 mToken, mIdentity, err, strerror(-err));
414 return err;
415 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700416 uint32_t identity = cblk->getIdentity(mToken);
417 if (mIdentity != identity) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700418 LOGE("using an invalid surface id=%d, identity=%u should be %d",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700419 mToken, mIdentity, identity);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700420 return NO_INIT;
421 }
422 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
Mathias Agopian01b76682009-04-16 20:04:08 -0700425
426bool Surface::isSameSurface(
427 const sp<Surface>& lhs, const sp<Surface>& rhs)
428{
429 if (lhs == 0 || rhs == 0)
430 return false;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700431
Mathias Agopian01b76682009-04-16 20:04:08 -0700432 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
433}
434
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700435// ----------------------------------------------------------------------------
436
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700437int Surface::setSwapInterval(android_native_window_t* window, int interval) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700438 return 0;
439}
440
441int Surface::dequeueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700442 android_native_buffer_t** buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700443 Surface* self = getSelf(window);
444 return self->dequeueBuffer(buffer);
445}
446
447int Surface::lockBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700448 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700449 Surface* self = getSelf(window);
450 return self->lockBuffer(buffer);
451}
452
453int Surface::queueBuffer(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700454 android_native_buffer_t* buffer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700455 Surface* self = getSelf(window);
456 return self->queueBuffer(buffer);
457}
458
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700459int Surface::query(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700460 int what, int* value) {
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700461 Surface* self = getSelf(window);
462 return self->query(what, value);
463}
464
Mathias Agopian52212712009-08-11 22:34:02 -0700465int Surface::perform(android_native_window_t* window,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700466 int operation, ...) {
Mathias Agopian52212712009-08-11 22:34:02 -0700467 va_list args;
468 va_start(args, operation);
469 Surface* self = getSelf(window);
470 int res = self->perform(operation, args);
471 va_end(args);
472 return res;
473}
474
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700475// ----------------------------------------------------------------------------
476
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700477status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700478 android_native_buffer_t* out;
479 status_t err = dequeueBuffer(&out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700480 if (err == NO_ERROR) {
481 *buffer = SurfaceBuffer::getSelf(out);
482 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700483 return err;
484}
485
Mathias Agopian0926f502009-05-04 14:17:04 -0700486// ----------------------------------------------------------------------------
487
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700488
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700489int Surface::dequeueBuffer(android_native_buffer_t** buffer)
490{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700491 sp<SurfaceComposerClient> client(getClient());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700492 status_t err = validate(client->mControl);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700493 if (err != NO_ERROR)
494 return err;
495
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700496 ssize_t bufIdx = mSharedBufferClient->dequeue();
497 if (bufIdx < 0) {
498 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
499 return bufIdx;
Mathias Agopian04bc12b2009-08-21 15:44:17 -0700500 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700501
502 // FIXME: in case of failure below, we need to undo the dequeue
503
504 uint32_t usage;
505 const bool usageChanged = getUsage(&usage);
506 const sp<SurfaceBuffer>& backBuffer(mBuffers[bufIdx]);
507 if ((backBuffer == 0) || usageChanged ||
508 mSharedBufferClient->needNewBuffer(bufIdx)) {
509 err = getBufferLocked(bufIdx, usage);
510 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
511 bufIdx, usage, strerror(-err));
Mathias Agopian50517542009-08-19 17:10:18 -0700512 if (err == NO_ERROR) {
513 // reset the width/height with the what we get from the buffer
Mathias Agopian50517542009-08-19 17:10:18 -0700514 mWidth = uint32_t(backBuffer->width);
515 mHeight = uint32_t(backBuffer->height);
516 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517 }
518
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700519 // if we still don't have a buffer here, we probably ran out of memory
520 if (!err && backBuffer==0) {
521 err = NO_MEMORY;
522 }
523
Mathias Agopiancf81c842009-07-31 14:47:00 -0700524 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700525 mDirtyRegion.set(backBuffer->width, backBuffer->height);
526 *buffer = backBuffer.get();
527 } else {
528 mSharedBufferClient->undoDequeue(bufIdx);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700529 }
Mathias Agopian50517542009-08-19 17:10:18 -0700530
Mathias Agopiancf81c842009-07-31 14:47:00 -0700531 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700532}
533
534int Surface::lockBuffer(android_native_buffer_t* buffer)
535{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700536 sp<SurfaceComposerClient> client(getClient());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700537 status_t err = validate(client->mControl);
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700538 if (err != NO_ERROR)
539 return err;
540
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700541 int32_t bufIdx = SurfaceBuffer::getSelf(buffer)->getIndex();
542 err = mSharedBufferClient->lock(bufIdx);
543 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
544 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545}
546
547int Surface::queueBuffer(android_native_buffer_t* buffer)
548{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700549 sp<SurfaceComposerClient> client(getClient());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700550 status_t err = validate(client->mControl);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700551 if (err != NO_ERROR)
552 return err;
553
Mathias Agopian0926f502009-05-04 14:17:04 -0700554 if (mSwapRectangle.isValid()) {
555 mDirtyRegion.set(mSwapRectangle);
556 }
557
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700558 int32_t bufIdx = SurfaceBuffer::getSelf(buffer)->getIndex();
559 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
560 err = mSharedBufferClient->queue(bufIdx);
561 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700562
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700563 if (err == NO_ERROR) {
564 // FIXME: can we avoid this IPC if we know there is one pending?
Mathias Agopianba5972f2009-08-14 18:52:17 -0700565 client->signalServer();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700566 }
567 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700568}
569
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700570int Surface::query(int what, int* value)
571{
572 switch (what) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700573 case NATIVE_WINDOW_WIDTH:
574 *value = int(mWidth);
575 return NO_ERROR;
576 case NATIVE_WINDOW_HEIGHT:
577 *value = int(mHeight);
578 return NO_ERROR;
579 case NATIVE_WINDOW_FORMAT:
580 *value = int(mFormat);
581 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700582 }
583 return BAD_VALUE;
584}
585
Mathias Agopian52212712009-08-11 22:34:02 -0700586int Surface::perform(int operation, va_list args)
587{
588 int res = NO_ERROR;
589 switch (operation) {
590 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700591 setUsage( va_arg(args, int) );
Mathias Agopian52212712009-08-11 22:34:02 -0700592 break;
593 default:
594 res = NAME_NOT_FOUND;
595 break;
596 }
597 return res;
598}
599
Mathias Agopianba5972f2009-08-14 18:52:17 -0700600void Surface::setUsage(uint32_t reqUsage)
601{
602 Mutex::Autolock _l(mSurfaceLock);
603 if (mUsage != reqUsage) {
604 mUsageChanged = true;
605 mUsage = reqUsage;
606 }
607}
608
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700609bool Surface::getUsage(uint32_t* usage)
610{
611 Mutex::Autolock _l(mSurfaceLock);
612 *usage = mUsage;
613 if (mUsageChanged) {
614 mUsageChanged = false;
615 return true;
616 }
617 return false;
618}
619
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620// ----------------------------------------------------------------------------
621
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622status_t Surface::lock(SurfaceInfo* info, bool blocking) {
623 return Surface::lock(info, NULL, blocking);
624}
625
Mathias Agopian0926f502009-05-04 14:17:04 -0700626status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700628 if (mApiLock.tryLock() != NO_ERROR) {
629 LOGE("calling Surface::lock() from different threads!");
630 CallStack stack;
631 stack.update();
632 stack.dump("Surface::lock called from different threads");
633 return WOULD_BLOCK;
634 }
635
Mathias Agopian52212712009-08-11 22:34:02 -0700636 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700637 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
638
Mathias Agopian0926f502009-05-04 14:17:04 -0700639 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700640 status_t err = dequeueBuffer(&backBuffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700641 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700642 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700643 err = lockBuffer(backBuffer.get());
644 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
645 backBuffer->getIndex(), strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647 // we handle copy-back here...
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700648
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700650 Region scratch(bounds);
651 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700652
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700653 if (mNeedFullUpdate) {
654 // reset newDirtyRegion to bounds when a buffer is reallocated
655 // it would be better if this information was associated with
656 // the buffer and made available to outside of Surface.
657 // This will do for now though.
658 mNeedFullUpdate = false;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700660 } else {
661 newDirtyRegion.andSelf(bounds);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700662 }
663
664 const sp<SurfaceBuffer>& frontBuffer(mPostedBuffer);
665 if (frontBuffer !=0 &&
666 backBuffer->width == frontBuffer->width &&
667 backBuffer->height == frontBuffer->height &&
668 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
669 {
670 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
671 if (!copyback.isEmpty() && frontBuffer!=0) {
672 // copy front to back
673 copyBlt(backBuffer, frontBuffer, copyback);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700674 }
675 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700676
Mathias Agopian0926f502009-05-04 14:17:04 -0700677 mDirtyRegion = newDirtyRegion;
678 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700679
Mathias Agopiane71212b2009-05-05 00:37:46 -0700680 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700681 status_t res = backBuffer->lock(
682 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700683 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700684
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700685 LOGW_IF(res, "failed locking buffer (handle = %p)",
686 backBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700687
688 mLockedBuffer = backBuffer;
689 other->w = backBuffer->width;
690 other->h = backBuffer->height;
691 other->s = backBuffer->stride;
692 other->usage = backBuffer->usage;
693 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700694 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700695 }
696 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700697 mApiLock.unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700698 return err;
699}
700
701status_t Surface::unlockAndPost()
702{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700703 if (mLockedBuffer == 0) {
704 LOGE("unlockAndPost failed, no locked buffer");
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700705 return BAD_VALUE;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700706 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700707
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700708 status_t err = mLockedBuffer->unlock();
709 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700710
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700711 err = queueBuffer(mLockedBuffer.get());
712 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
713 mLockedBuffer->getIndex(), strerror(-err));
714
715 mPostedBuffer = mLockedBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700716 mLockedBuffer = 0;
717 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718}
719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700721 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 mSwapRectangle = r;
723}
724
Mathias Agopian52212712009-08-11 22:34:02 -0700725status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700727 sp<ISurface> s(mSurface);
728 if (s == 0) return NO_INIT;
729
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700730 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700731
732 // free the current buffer
733 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
734 if (currentBuffer != 0) {
735 getBufferMapper().unregisterBuffer(currentBuffer->handle);
736 currentBuffer.clear();
737 }
738
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700739 sp<SurfaceBuffer> buffer = s->requestBuffer(index, usage);
740 LOGE_IF(buffer==0,
741 "ISurface::getBuffer(%d, %08x) returned NULL",
742 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700743 if (buffer != 0) { // this should never happen by construction
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700744 LOGE_IF(buffer->handle == NULL,
745 "requestBuffer(%d, %08x) returned a buffer with a null handle",
746 index, usage);
Mathias Agopian50517542009-08-19 17:10:18 -0700747 if (buffer->handle != NULL) {
748 err = getBufferMapper().registerBuffer(buffer->handle);
749 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
750 err, strerror(-err));
751 if (err == NO_ERROR) {
752 currentBuffer = buffer;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700753 currentBuffer->setIndex(index);
754 mNeedFullUpdate = true;
Mathias Agopian50517542009-08-19 17:10:18 -0700755 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 }
757 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700758 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759}
760
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761}; // namespace android
762