blob: 0fe28d462fb04dad29fca20d3b9ea59aa869760d [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 Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/IPCThreadState.h>
29#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <utils/Log.h>
31
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
33#include <ui/BufferMapper.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <ui/ISurface.h>
35#include <ui/Surface.h>
36#include <ui/SurfaceComposerClient.h>
37#include <ui/Rect.h>
38
Mathias Agopian7189c002009-05-05 18:11:11 -070039#include <pixelflinger/pixelflinger.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <private/ui/SharedState.h>
42#include <private/ui/LayerState.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070043#include <private/ui/SurfaceBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047// ============================================================================
48// SurfaceBuffer
49// ============================================================================
50
Mathias Agopian9f88afb2009-04-17 14:15:18 -070051ANDROID_SINGLETON_STATIC_INSTANCE( SurfaceBuffer )
Mathias Agopian4243e662009-04-15 18:34:24 -070052
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053SurfaceBuffer::SurfaceBuffer()
Mathias Agopian21c59d02009-05-05 00:59:23 -070054 : BASE(), mOwner(false), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055{
56 width =
57 height =
58 stride =
59 format =
60 usage = 0;
Mathias Agopian21c59d02009-05-05 00:59:23 -070061 handle = NULL;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070062}
63
64SurfaceBuffer::SurfaceBuffer(const Parcel& data)
Mathias Agopian21c59d02009-05-05 00:59:23 -070065 : BASE(), mOwner(true), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066{
67 // we own the handle in this case
68 width = data.readInt32();
69 height = data.readInt32();
70 stride = data.readInt32();
71 format = data.readInt32();
72 usage = data.readInt32();
73 handle = data.readNativeHandle();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074}
75
76SurfaceBuffer::~SurfaceBuffer()
77{
78 if (handle && mOwner) {
79 native_handle_close(handle);
80 native_handle_delete(const_cast<native_handle*>(handle));
81 }
82}
83
Mathias Agopiane71212b2009-05-05 00:37:46 -070084status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070085{
86 const Rect lockBounds(width, height);
Mathias Agopiane71212b2009-05-05 00:37:46 -070087 status_t res = lock(usage, lockBounds, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070088 return res;
89}
90
Mathias Agopiane71212b2009-05-05 00:37:46 -070091status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070092{
Mathias Agopiane71212b2009-05-05 00:37:46 -070093 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070094 return res;
95}
96
97status_t SurfaceBuffer::unlock()
98{
99 status_t res = getBufferMapper().unlock(handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700100 return res;
101}
102
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103status_t SurfaceBuffer::writeToParcel(Parcel* reply,
104 android_native_buffer_t const* buffer)
105{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106 reply->writeInt32(buffer->width);
107 reply->writeInt32(buffer->height);
108 reply->writeInt32(buffer->stride);
109 reply->writeInt32(buffer->format);
110 reply->writeInt32(buffer->usage);
Mathias Agopian21c59d02009-05-05 00:59:23 -0700111 reply->writeNativeHandle(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 return NO_ERROR;
113}
114
115// ----------------------------------------------------------------------
116
Mathias Agopian0926f502009-05-04 14:17:04 -0700117static void copyBlt(
118 const sp<SurfaceBuffer>& dst,
119 const sp<SurfaceBuffer>& src,
120 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700122 uint8_t const * src_bits;
123 src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Mathias Agopian0926f502009-05-04 14:17:04 -0700124
Mathias Agopiane71212b2009-05-05 00:37:46 -0700125 uint8_t* dst_bits;
126 dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Mathias Agopian0926f502009-05-04 14:17:04 -0700127
Mathias Agopian20f68782009-05-11 00:03:41 -0700128 size_t c;
129 Rect const* const rects = reg.getArray(&c);
130
131 if (c) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 const size_t bpp = bytesPerPixel(src->format);
134 const size_t dbpr = dst->stride * bpp;
135 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -0700136
Mathias Agopian20f68782009-05-11 00:03:41 -0700137 for (size_t i=0 ; i<c ; i++) {
138 const Rect& r = rects[i];
Mathias Agopian0926f502009-05-04 14:17:04 -0700139 ssize_t h = r.height();
140 if (h <= 0) continue;
141 size_t size = r.width() * bpp;
142 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
143 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
144 if (dbpr==sbpr && size==sbpr) {
145 size *= h;
146 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700147 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700148 do {
149 memcpy(d, s, size);
150 d += dbpr;
151 s += sbpr;
152 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153 }
154 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700155
156 src->unlock();
157 dst->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700158}
159
Mathias Agopian62185b72009-04-16 16:19:50 -0700160// ============================================================================
161// SurfaceControl
162// ============================================================================
163
Mathias Agopian01b76682009-04-16 20:04:08 -0700164SurfaceControl::SurfaceControl(
165 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700166 const sp<ISurface>& surface,
167 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700168 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700169 : mClient(client), mSurface(surface),
170 mToken(data.token), mIdentity(data.identity),
Mathias Agopian18d84462009-04-16 20:30:22 -0700171 mFormat(format), mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700172{
173}
Mathias Agopian18d84462009-04-16 20:30:22 -0700174
Mathias Agopian62185b72009-04-16 16:19:50 -0700175SurfaceControl::~SurfaceControl()
176{
177 destroy();
178}
179
180void SurfaceControl::destroy()
181{
Mathias Agopian18d84462009-04-16 20:30:22 -0700182 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700183 mClient->destroySurface(mToken);
184 }
185
186 // clear all references and trigger an IPC now, to make sure things
187 // happen without delay, since these resources are quite heavy.
188 mClient.clear();
189 mSurface.clear();
190 IPCThreadState::self()->flushCommands();
191}
192
193void SurfaceControl::clear()
194{
195 // here, the window manager tells us explicitly that we should destroy
196 // the surface's resource. Soon after this call, it will also release
197 // its last reference (which will call the dtor); however, it is possible
198 // that a client living in the same process still holds references which
199 // would delay the call to the dtor -- that is why we need this explicit
200 // "clear()" call.
201 destroy();
202}
203
Mathias Agopian62185b72009-04-16 16:19:50 -0700204bool SurfaceControl::isSameSurface(
205 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
206{
207 if (lhs == 0 || rhs == 0)
208 return false;
209 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
210}
211
Mathias Agopian01b76682009-04-16 20:04:08 -0700212status_t SurfaceControl::setLayer(int32_t layer) {
213 const sp<SurfaceComposerClient>& client(mClient);
214 if (client == 0) return NO_INIT;
215 status_t err = validate(client->mControl);
216 if (err < 0) return err;
217 return client->setLayer(mToken, layer);
218}
219status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
220 const sp<SurfaceComposerClient>& client(mClient);
221 if (client == 0) return NO_INIT;
222 status_t err = validate(client->mControl);
223 if (err < 0) return err;
224 return client->setPosition(mToken, x, y);
225}
226status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
227 const sp<SurfaceComposerClient>& client(mClient);
228 if (client == 0) return NO_INIT;
229 status_t err = validate(client->mControl);
230 if (err < 0) return err;
231 return client->setSize(mToken, w, h);
232}
233status_t SurfaceControl::hide() {
234 const sp<SurfaceComposerClient>& client(mClient);
235 if (client == 0) return NO_INIT;
236 status_t err = validate(client->mControl);
237 if (err < 0) return err;
238 return client->hide(mToken);
239}
240status_t SurfaceControl::show(int32_t layer) {
241 const sp<SurfaceComposerClient>& client(mClient);
242 if (client == 0) return NO_INIT;
243 status_t err = validate(client->mControl);
244 if (err < 0) return err;
245 return client->show(mToken, layer);
246}
247status_t SurfaceControl::freeze() {
248 const sp<SurfaceComposerClient>& client(mClient);
249 if (client == 0) return NO_INIT;
250 status_t err = validate(client->mControl);
251 if (err < 0) return err;
252 return client->freeze(mToken);
253}
254status_t SurfaceControl::unfreeze() {
255 const sp<SurfaceComposerClient>& client(mClient);
256 if (client == 0) return NO_INIT;
257 status_t err = validate(client->mControl);
258 if (err < 0) return err;
259 return client->unfreeze(mToken);
260}
261status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
262 const sp<SurfaceComposerClient>& client(mClient);
263 if (client == 0) return NO_INIT;
264 status_t err = validate(client->mControl);
265 if (err < 0) return err;
266 return client->setFlags(mToken, flags, mask);
267}
268status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
269 const sp<SurfaceComposerClient>& client(mClient);
270 if (client == 0) return NO_INIT;
271 status_t err = validate(client->mControl);
272 if (err < 0) return err;
273 return client->setTransparentRegionHint(mToken, transparent);
274}
275status_t SurfaceControl::setAlpha(float alpha) {
276 const sp<SurfaceComposerClient>& client(mClient);
277 if (client == 0) return NO_INIT;
278 status_t err = validate(client->mControl);
279 if (err < 0) return err;
280 return client->setAlpha(mToken, alpha);
281}
282status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
283 const sp<SurfaceComposerClient>& client(mClient);
284 if (client == 0) return NO_INIT;
285 status_t err = validate(client->mControl);
286 if (err < 0) return err;
287 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
288}
289status_t SurfaceControl::setFreezeTint(uint32_t tint) {
290 const sp<SurfaceComposerClient>& client(mClient);
291 if (client == 0) return NO_INIT;
292 status_t err = validate(client->mControl);
293 if (err < 0) return err;
294 return client->setFreezeTint(mToken, tint);
295}
Mathias Agopian62185b72009-04-16 16:19:50 -0700296
297status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const
298{
299 if (mToken<0 || mClient==0) {
300 LOGE("invalid token (%d, identity=%u) or client (%p)",
301 mToken, mIdentity, mClient.get());
302 return NO_INIT;
303 }
304 if (cblk == 0) {
305 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
306 return NO_INIT;
307 }
308 status_t err = cblk->validate(mToken);
309 if (err != NO_ERROR) {
310 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
311 mToken, mIdentity, err, strerror(-err));
312 return err;
313 }
314 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
315 LOGE("using an invalid surface id=%d, identity=%u should be %d",
316 mToken, mIdentity, cblk->layers[mToken].identity);
317 return NO_INIT;
318 }
319 return NO_ERROR;
320}
321
Mathias Agopian01b76682009-04-16 20:04:08 -0700322status_t SurfaceControl::writeSurfaceToParcel(
323 const sp<SurfaceControl>& control, Parcel* parcel)
324{
325 uint32_t flags = 0;
326 uint32_t format = 0;
327 SurfaceID token = -1;
328 uint32_t identity = 0;
329 sp<SurfaceComposerClient> client;
330 sp<ISurface> sur;
331 if (SurfaceControl::isValid(control)) {
332 token = control->mToken;
333 identity = control->mIdentity;
334 client = control->mClient;
335 sur = control->mSurface;
336 format = control->mFormat;
337 flags = control->mFlags;
338 }
339 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
340 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
341 parcel->writeInt32(token);
342 parcel->writeInt32(identity);
343 parcel->writeInt32(format);
344 parcel->writeInt32(flags);
345 return NO_ERROR;
346}
347
348sp<Surface> SurfaceControl::getSurface() const
349{
350 Mutex::Autolock _l(mLock);
351 if (mSurfaceData == 0) {
352 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
353 }
354 return mSurfaceData;
355}
356
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700357// ============================================================================
358// Surface
359// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360
Mathias Agopian01b76682009-04-16 20:04:08 -0700361Surface::Surface(const sp<SurfaceControl>& surface)
362 : mClient(surface->mClient), mSurface(surface->mSurface),
363 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700364 mFormat(surface->mFormat), mFlags(surface->mFlags),
365 mBufferMapper(BufferMapper::get())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366{
Mathias Agopian01b76682009-04-16 20:04:08 -0700367 init();
368}
Mathias Agopian62185b72009-04-16 16:19:50 -0700369
Mathias Agopian01b76682009-04-16 20:04:08 -0700370Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700371 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-04-16 20:04:08 -0700372{
373 sp<IBinder> clientBinder = parcel.readStrongBinder();
374 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
375 mToken = parcel.readInt32();
376 mIdentity = parcel.readInt32();
377 mFormat = parcel.readInt32();
378 mFlags = parcel.readInt32();
379
380 if (clientBinder != NULL)
381 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
382
383 init();
384}
385
386void Surface::init()
387{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700388 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700389 android_native_window_t::dequeueBuffer = dequeueBuffer;
390 android_native_window_t::lockBuffer = lockBuffer;
391 android_native_window_t::queueBuffer = queueBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700393 DisplayInfo dinfo;
394 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
395 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
396 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
397 // FIXME: set real values here
398 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
399 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
400 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401}
402
Mathias Agopian01b76682009-04-16 20:04:08 -0700403
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404Surface::~Surface()
405{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700406 // this is a client-side operation, the surface is destroyed, unmap
407 // its buffers in this process.
408 for (int i=0 ; i<2 ; i++) {
409 if (mBuffers[i] != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700410 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700411 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700413
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700414 // clear all references and trigger an IPC now, to make sure things
415 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 mClient.clear();
417 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 IPCThreadState::self()->flushCommands();
419}
420
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700421status_t Surface::validate(per_client_cblk_t const* cblk) const
422{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700423 if (mToken<0 || mClient==0) {
424 LOGE("invalid token (%d, identity=%u) or client (%p)",
425 mToken, mIdentity, mClient.get());
426 return NO_INIT;
427 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428 if (cblk == 0) {
429 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
430 return NO_INIT;
431 }
432 status_t err = cblk->validate(mToken);
433 if (err != NO_ERROR) {
434 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
435 mToken, mIdentity, err, strerror(-err));
436 return err;
437 }
438 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
439 LOGE("using an invalid surface id=%d, identity=%u should be %d",
440 mToken, mIdentity, cblk->layers[mToken].identity);
441 return NO_INIT;
442 }
443 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444}
445
Mathias Agopian01b76682009-04-16 20:04:08 -0700446
447bool Surface::isSameSurface(
448 const sp<Surface>& lhs, const sp<Surface>& rhs)
449{
450 if (lhs == 0 || rhs == 0)
451 return false;
452 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
453}
454
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700455// ----------------------------------------------------------------------------
456
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700457int Surface::setSwapInterval(android_native_window_t* window, int interval)
458{
459 return 0;
460}
461
462int Surface::dequeueBuffer(android_native_window_t* window,
463 android_native_buffer_t** buffer)
464{
465 Surface* self = getSelf(window);
466 return self->dequeueBuffer(buffer);
467}
468
469int Surface::lockBuffer(android_native_window_t* window,
470 android_native_buffer_t* buffer)
471{
472 Surface* self = getSelf(window);
473 return self->lockBuffer(buffer);
474}
475
476int Surface::queueBuffer(android_native_window_t* window,
477 android_native_buffer_t* buffer)
478{
479 Surface* self = getSelf(window);
480 return self->queueBuffer(buffer);
481}
482
483// ----------------------------------------------------------------------------
484
Mathias Agopian0926f502009-05-04 14:17:04 -0700485status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
486{
487 android_native_buffer_t* out;
488 status_t err = dequeueBuffer(&out);
489 *buffer = SurfaceBuffer::getSelf(out);
490 return err;
491}
492
493status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
494{
495 return lockBuffer(buffer.get());
496}
497
498status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
499{
500 return queueBuffer(buffer.get());
501}
502
503// ----------------------------------------------------------------------------
504
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700505int Surface::dequeueBuffer(android_native_buffer_t** buffer)
506{
507 // FIXME: dequeueBuffer() needs proper implementation
508
509 Mutex::Autolock _l(mSurfaceLock);
510
511 per_client_cblk_t* const cblk = mClient->mControl;
512 status_t err = validate(cblk);
513 if (err != NO_ERROR)
514 return err;
515
516 SurfaceID index(mToken);
517
518 int32_t backIdx = cblk->lock_layer(size_t(index),
519 per_client_cblk_t::BLOCKING);
520
521 if (backIdx < 0)
522 return status_t(backIdx);
523
524 mBackbufferIndex = backIdx;
525 layer_cblk_t* const lcblk = &(cblk->layers[index]);
526
527 volatile const surface_info_t* const back = lcblk->surface + backIdx;
528 if (back->flags & surface_info_t::eNeedNewBuffer) {
529 getBufferLocked(backIdx);
530 }
531
532 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700533 mDirtyRegion.set(backBuffer->width, backBuffer->height);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534 *buffer = backBuffer.get();
Mathias Agopian0926f502009-05-04 14:17:04 -0700535
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536 return NO_ERROR;
537}
538
539int Surface::lockBuffer(android_native_buffer_t* buffer)
540{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700541 Mutex::Autolock _l(mSurfaceLock);
542
543 per_client_cblk_t* const cblk = mClient->mControl;
544 status_t err = validate(cblk);
545 if (err != NO_ERROR)
546 return err;
547
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 // FIXME: lockBuffer() needs proper implementation
549 return 0;
550}
551
552int Surface::queueBuffer(android_native_buffer_t* buffer)
553{
554 Mutex::Autolock _l(mSurfaceLock);
555
556 per_client_cblk_t* const cblk = mClient->mControl;
557 status_t err = validate(cblk);
558 if (err != NO_ERROR)
559 return err;
560
Mathias Agopian0926f502009-05-04 14:17:04 -0700561 if (mSwapRectangle.isValid()) {
562 mDirtyRegion.set(mSwapRectangle);
563 }
564
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700566 SurfaceID index(mToken);
567 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700568 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700569
570 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
571 if (!(newstate & eNextFlipPending))
572 mClient->signalServer();
573
574 return NO_ERROR;
575}
576
577// ----------------------------------------------------------------------------
578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579status_t Surface::lock(SurfaceInfo* info, bool blocking) {
580 return Surface::lock(info, NULL, blocking);
581}
582
Mathias Agopian0926f502009-05-04 14:17:04 -0700583status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700584{
585 // FIXME: needs some locking here
Mathias Agopian0926f502009-05-04 14:17:04 -0700586
587 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588 status_t err = dequeueBuffer(&backBuffer);
589 if (err == NO_ERROR) {
590 err = lockBuffer(backBuffer);
591 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700592 // we handle copy-back here...
593
594 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700595 Region scratch(bounds);
596 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597
598 per_client_cblk_t* const cblk = mClient->mControl;
599 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
600 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
601 if (back->flags & surface_info_t::eBufferDirty) {
602 // content is meaningless in this case and the whole surface
603 // needs to be redrawn.
604 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700605 } else {
606 newDirtyRegion.andSelf(bounds);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607 if (!(lcblk->flags & eNoCopyBack)) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700608 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
609 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
610 if (!copyback.isEmpty() && frontBuffer!=0) {
611 // copy front to back
612 copyBlt(backBuffer, frontBuffer, copyback);
613 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 }
615 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700616 mDirtyRegion = newDirtyRegion;
617 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700618
Mathias Agopiane71212b2009-05-05 00:37:46 -0700619 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700620 status_t res = backBuffer->lock(
621 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700622 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700623
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700625 mBackbufferIndex, backBuffer->handle);
626
627 mLockedBuffer = backBuffer;
628 other->w = backBuffer->width;
629 other->h = backBuffer->height;
630 other->s = backBuffer->stride;
631 other->usage = backBuffer->usage;
632 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700633 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700634 }
635 }
636 return err;
637}
638
639status_t Surface::unlockAndPost()
640{
641 // FIXME: needs some locking here
642
643 if (mLockedBuffer == 0)
644 return BAD_VALUE;
645
Mathias Agopian0926f502009-05-04 14:17:04 -0700646 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700648 mBackbufferIndex, mLockedBuffer->handle);
649
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700650 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700651 mLockedBuffer = 0;
652 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653}
654
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700655void Surface::_send_dirty_region(
656 layer_cblk_t* lcblk, const Region& dirty)
657{
658 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
659 flat_region_t* flat_region = lcblk->region + index;
660 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
661 if (err < NO_ERROR) {
662 // region doesn't fit, use the bounds
663 const Region reg(dirty.bounds());
664 reg.write(flat_region, sizeof(flat_region_t));
665 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666}
667
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668void Surface::setSwapRectangle(const Rect& r) {
669 mSwapRectangle = r;
670}
671
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700672status_t Surface::getBufferLocked(int index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700674 status_t err = NO_MEMORY;
675 sp<SurfaceBuffer> buffer = mSurface->getBuffer();
676 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
677 if (buffer != 0) {
678 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
679 if (currentBuffer != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700680 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700681 currentBuffer.clear();
682 }
Mathias Agopian21c59d02009-05-05 00:59:23 -0700683 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684 LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
685 if (err == NO_ERROR) {
686 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 }
688 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700689 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690}
691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692}; // namespace android
693