blob: b8b428f9a2e3799910b48ed9fcf5a218fd369d96 [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
51SurfaceBuffer::SurfaceBuffer()
Mathias Agopian21c59d02009-05-05 00:59:23 -070052 : BASE(), mOwner(false), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053{
54 width =
55 height =
56 stride =
57 format =
58 usage = 0;
Mathias Agopian21c59d02009-05-05 00:59:23 -070059 handle = NULL;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060}
61
62SurfaceBuffer::SurfaceBuffer(const Parcel& data)
Mathias Agopian21c59d02009-05-05 00:59:23 -070063 : BASE(), mOwner(true), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070064{
65 // we own the handle in this case
66 width = data.readInt32();
67 height = data.readInt32();
68 stride = data.readInt32();
69 format = data.readInt32();
70 usage = data.readInt32();
71 handle = data.readNativeHandle();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072}
73
74SurfaceBuffer::~SurfaceBuffer()
75{
76 if (handle && mOwner) {
77 native_handle_close(handle);
78 native_handle_delete(const_cast<native_handle*>(handle));
79 }
80}
81
Mathias Agopiane71212b2009-05-05 00:37:46 -070082status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070083{
84 const Rect lockBounds(width, height);
Mathias Agopiane71212b2009-05-05 00:37:46 -070085 status_t res = lock(usage, lockBounds, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070086 return res;
87}
88
Mathias Agopiane71212b2009-05-05 00:37:46 -070089status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070090{
Mathias Agopiane71212b2009-05-05 00:37:46 -070091 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070092 return res;
93}
94
95status_t SurfaceBuffer::unlock()
96{
97 status_t res = getBufferMapper().unlock(handle);
Mathias Agopian0926f502009-05-04 14:17:04 -070098 return res;
99}
100
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101status_t SurfaceBuffer::writeToParcel(Parcel* reply,
102 android_native_buffer_t const* buffer)
103{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104 reply->writeInt32(buffer->width);
105 reply->writeInt32(buffer->height);
106 reply->writeInt32(buffer->stride);
107 reply->writeInt32(buffer->format);
108 reply->writeInt32(buffer->usage);
Mathias Agopian21c59d02009-05-05 00:59:23 -0700109 reply->writeNativeHandle(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 return NO_ERROR;
111}
112
113// ----------------------------------------------------------------------
114
Mathias Agopian0926f502009-05-04 14:17:04 -0700115static void copyBlt(
116 const sp<SurfaceBuffer>& dst,
117 const sp<SurfaceBuffer>& src,
118 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700120 uint8_t const * src_bits;
121 src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Mathias Agopian0926f502009-05-04 14:17:04 -0700122
Mathias Agopiane71212b2009-05-05 00:37:46 -0700123 uint8_t* dst_bits;
124 dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Mathias Agopian0926f502009-05-04 14:17:04 -0700125
Mathias Agopian20f68782009-05-11 00:03:41 -0700126 size_t c;
127 Rect const* const rects = reg.getArray(&c);
128
129 if (c) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 const size_t bpp = bytesPerPixel(src->format);
132 const size_t dbpr = dst->stride * bpp;
133 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -0700134
Mathias Agopian20f68782009-05-11 00:03:41 -0700135 for (size_t i=0 ; i<c ; i++) {
136 const Rect& r = rects[i];
Mathias Agopian0926f502009-05-04 14:17:04 -0700137 ssize_t h = r.height();
138 if (h <= 0) continue;
139 size_t size = r.width() * bpp;
140 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
141 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
142 if (dbpr==sbpr && size==sbpr) {
143 size *= h;
144 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700146 do {
147 memcpy(d, s, size);
148 d += dbpr;
149 s += sbpr;
150 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151 }
152 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700153
154 src->unlock();
155 dst->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700156}
157
Mathias Agopian62185b72009-04-16 16:19:50 -0700158// ============================================================================
159// SurfaceControl
160// ============================================================================
161
Mathias Agopian01b76682009-04-16 20:04:08 -0700162SurfaceControl::SurfaceControl(
163 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700164 const sp<ISurface>& surface,
165 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700166 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700167 : mClient(client), mSurface(surface),
168 mToken(data.token), mIdentity(data.identity),
Mathias Agopian18d84462009-04-16 20:30:22 -0700169 mFormat(format), mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700170{
171}
Mathias Agopian18d84462009-04-16 20:30:22 -0700172
Mathias Agopian62185b72009-04-16 16:19:50 -0700173SurfaceControl::~SurfaceControl()
174{
175 destroy();
176}
177
178void SurfaceControl::destroy()
179{
Mathias Agopian18d84462009-04-16 20:30:22 -0700180 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700181 mClient->destroySurface(mToken);
182 }
183
184 // clear all references and trigger an IPC now, to make sure things
185 // happen without delay, since these resources are quite heavy.
186 mClient.clear();
187 mSurface.clear();
188 IPCThreadState::self()->flushCommands();
189}
190
191void SurfaceControl::clear()
192{
193 // here, the window manager tells us explicitly that we should destroy
194 // the surface's resource. Soon after this call, it will also release
195 // its last reference (which will call the dtor); however, it is possible
196 // that a client living in the same process still holds references which
197 // would delay the call to the dtor -- that is why we need this explicit
198 // "clear()" call.
199 destroy();
200}
201
Mathias Agopian62185b72009-04-16 16:19:50 -0700202bool SurfaceControl::isSameSurface(
203 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
204{
205 if (lhs == 0 || rhs == 0)
206 return false;
207 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
208}
209
Mathias Agopian01b76682009-04-16 20:04:08 -0700210status_t SurfaceControl::setLayer(int32_t layer) {
211 const sp<SurfaceComposerClient>& client(mClient);
212 if (client == 0) return NO_INIT;
213 status_t err = validate(client->mControl);
214 if (err < 0) return err;
215 return client->setLayer(mToken, layer);
216}
217status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
218 const sp<SurfaceComposerClient>& client(mClient);
219 if (client == 0) return NO_INIT;
220 status_t err = validate(client->mControl);
221 if (err < 0) return err;
222 return client->setPosition(mToken, x, y);
223}
224status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
225 const sp<SurfaceComposerClient>& client(mClient);
226 if (client == 0) return NO_INIT;
227 status_t err = validate(client->mControl);
228 if (err < 0) return err;
229 return client->setSize(mToken, w, h);
230}
231status_t SurfaceControl::hide() {
232 const sp<SurfaceComposerClient>& client(mClient);
233 if (client == 0) return NO_INIT;
234 status_t err = validate(client->mControl);
235 if (err < 0) return err;
236 return client->hide(mToken);
237}
238status_t SurfaceControl::show(int32_t layer) {
239 const sp<SurfaceComposerClient>& client(mClient);
240 if (client == 0) return NO_INIT;
241 status_t err = validate(client->mControl);
242 if (err < 0) return err;
243 return client->show(mToken, layer);
244}
245status_t SurfaceControl::freeze() {
246 const sp<SurfaceComposerClient>& client(mClient);
247 if (client == 0) return NO_INIT;
248 status_t err = validate(client->mControl);
249 if (err < 0) return err;
250 return client->freeze(mToken);
251}
252status_t SurfaceControl::unfreeze() {
253 const sp<SurfaceComposerClient>& client(mClient);
254 if (client == 0) return NO_INIT;
255 status_t err = validate(client->mControl);
256 if (err < 0) return err;
257 return client->unfreeze(mToken);
258}
259status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
260 const sp<SurfaceComposerClient>& client(mClient);
261 if (client == 0) return NO_INIT;
262 status_t err = validate(client->mControl);
263 if (err < 0) return err;
264 return client->setFlags(mToken, flags, mask);
265}
266status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
267 const sp<SurfaceComposerClient>& client(mClient);
268 if (client == 0) return NO_INIT;
269 status_t err = validate(client->mControl);
270 if (err < 0) return err;
271 return client->setTransparentRegionHint(mToken, transparent);
272}
273status_t SurfaceControl::setAlpha(float alpha) {
274 const sp<SurfaceComposerClient>& client(mClient);
275 if (client == 0) return NO_INIT;
276 status_t err = validate(client->mControl);
277 if (err < 0) return err;
278 return client->setAlpha(mToken, alpha);
279}
280status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
281 const sp<SurfaceComposerClient>& client(mClient);
282 if (client == 0) return NO_INIT;
283 status_t err = validate(client->mControl);
284 if (err < 0) return err;
285 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
286}
287status_t SurfaceControl::setFreezeTint(uint32_t tint) {
288 const sp<SurfaceComposerClient>& client(mClient);
289 if (client == 0) return NO_INIT;
290 status_t err = validate(client->mControl);
291 if (err < 0) return err;
292 return client->setFreezeTint(mToken, tint);
293}
Mathias Agopian62185b72009-04-16 16:19:50 -0700294
295status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const
296{
297 if (mToken<0 || mClient==0) {
298 LOGE("invalid token (%d, identity=%u) or client (%p)",
299 mToken, mIdentity, mClient.get());
300 return NO_INIT;
301 }
302 if (cblk == 0) {
303 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
304 return NO_INIT;
305 }
306 status_t err = cblk->validate(mToken);
307 if (err != NO_ERROR) {
308 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
309 mToken, mIdentity, err, strerror(-err));
310 return err;
311 }
312 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
313 LOGE("using an invalid surface id=%d, identity=%u should be %d",
314 mToken, mIdentity, cblk->layers[mToken].identity);
315 return NO_INIT;
316 }
317 return NO_ERROR;
318}
319
Mathias Agopian01b76682009-04-16 20:04:08 -0700320status_t SurfaceControl::writeSurfaceToParcel(
321 const sp<SurfaceControl>& control, Parcel* parcel)
322{
323 uint32_t flags = 0;
324 uint32_t format = 0;
325 SurfaceID token = -1;
326 uint32_t identity = 0;
327 sp<SurfaceComposerClient> client;
328 sp<ISurface> sur;
329 if (SurfaceControl::isValid(control)) {
330 token = control->mToken;
331 identity = control->mIdentity;
332 client = control->mClient;
333 sur = control->mSurface;
334 format = control->mFormat;
335 flags = control->mFlags;
336 }
337 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
338 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
339 parcel->writeInt32(token);
340 parcel->writeInt32(identity);
341 parcel->writeInt32(format);
342 parcel->writeInt32(flags);
343 return NO_ERROR;
344}
345
346sp<Surface> SurfaceControl::getSurface() const
347{
348 Mutex::Autolock _l(mLock);
349 if (mSurfaceData == 0) {
350 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
351 }
352 return mSurfaceData;
353}
354
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700355// ============================================================================
356// Surface
357// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358
Mathias Agopian01b76682009-04-16 20:04:08 -0700359Surface::Surface(const sp<SurfaceControl>& surface)
360 : mClient(surface->mClient), mSurface(surface->mSurface),
361 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700362 mFormat(surface->mFormat), mFlags(surface->mFlags),
363 mBufferMapper(BufferMapper::get())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364{
Mathias Agopian01b76682009-04-16 20:04:08 -0700365 init();
366}
Mathias Agopian62185b72009-04-16 16:19:50 -0700367
Mathias Agopian01b76682009-04-16 20:04:08 -0700368Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700369 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-04-16 20:04:08 -0700370{
371 sp<IBinder> clientBinder = parcel.readStrongBinder();
372 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
373 mToken = parcel.readInt32();
374 mIdentity = parcel.readInt32();
375 mFormat = parcel.readInt32();
376 mFlags = parcel.readInt32();
377
378 if (clientBinder != NULL)
379 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
380
381 init();
382}
383
384void Surface::init()
385{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700386 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700387 android_native_window_t::dequeueBuffer = dequeueBuffer;
388 android_native_window_t::lockBuffer = lockBuffer;
389 android_native_window_t::queueBuffer = queueBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700391 DisplayInfo dinfo;
392 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
393 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
394 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
395 // FIXME: set real values here
396 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
397 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
398 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399}
400
Mathias Agopian01b76682009-04-16 20:04:08 -0700401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402Surface::~Surface()
403{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700404 // this is a client-side operation, the surface is destroyed, unmap
405 // its buffers in this process.
406 for (int i=0 ; i<2 ; i++) {
407 if (mBuffers[i] != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700408 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700409 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700411
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700412 // clear all references and trigger an IPC now, to make sure things
413 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 mClient.clear();
415 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 IPCThreadState::self()->flushCommands();
417}
418
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700419status_t Surface::validate(per_client_cblk_t const* cblk) const
420{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700421 if (mToken<0 || mClient==0) {
422 LOGE("invalid token (%d, identity=%u) or client (%p)",
423 mToken, mIdentity, mClient.get());
424 return NO_INIT;
425 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700426 if (cblk == 0) {
427 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
428 return NO_INIT;
429 }
430 status_t err = cblk->validate(mToken);
431 if (err != NO_ERROR) {
432 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
433 mToken, mIdentity, err, strerror(-err));
434 return err;
435 }
436 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
437 LOGE("using an invalid surface id=%d, identity=%u should be %d",
438 mToken, mIdentity, cblk->layers[mToken].identity);
439 return NO_INIT;
440 }
441 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442}
443
Mathias Agopian01b76682009-04-16 20:04:08 -0700444
445bool Surface::isSameSurface(
446 const sp<Surface>& lhs, const sp<Surface>& rhs)
447{
448 if (lhs == 0 || rhs == 0)
449 return false;
450 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
451}
452
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700453// ----------------------------------------------------------------------------
454
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700455int Surface::setSwapInterval(android_native_window_t* window, int interval)
456{
457 return 0;
458}
459
460int Surface::dequeueBuffer(android_native_window_t* window,
461 android_native_buffer_t** buffer)
462{
463 Surface* self = getSelf(window);
464 return self->dequeueBuffer(buffer);
465}
466
467int Surface::lockBuffer(android_native_window_t* window,
468 android_native_buffer_t* buffer)
469{
470 Surface* self = getSelf(window);
471 return self->lockBuffer(buffer);
472}
473
474int Surface::queueBuffer(android_native_window_t* window,
475 android_native_buffer_t* buffer)
476{
477 Surface* self = getSelf(window);
478 return self->queueBuffer(buffer);
479}
480
481// ----------------------------------------------------------------------------
482
Mathias Agopian0926f502009-05-04 14:17:04 -0700483status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
484{
485 android_native_buffer_t* out;
486 status_t err = dequeueBuffer(&out);
487 *buffer = SurfaceBuffer::getSelf(out);
488 return err;
489}
490
491status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
492{
493 return lockBuffer(buffer.get());
494}
495
496status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
497{
498 return queueBuffer(buffer.get());
499}
500
501// ----------------------------------------------------------------------------
502
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700503int Surface::dequeueBuffer(android_native_buffer_t** buffer)
504{
505 // FIXME: dequeueBuffer() needs proper implementation
506
507 Mutex::Autolock _l(mSurfaceLock);
508
509 per_client_cblk_t* const cblk = mClient->mControl;
510 status_t err = validate(cblk);
511 if (err != NO_ERROR)
512 return err;
513
514 SurfaceID index(mToken);
515
516 int32_t backIdx = cblk->lock_layer(size_t(index),
517 per_client_cblk_t::BLOCKING);
518
519 if (backIdx < 0)
520 return status_t(backIdx);
521
522 mBackbufferIndex = backIdx;
523 layer_cblk_t* const lcblk = &(cblk->layers[index]);
524
525 volatile const surface_info_t* const back = lcblk->surface + backIdx;
526 if (back->flags & surface_info_t::eNeedNewBuffer) {
527 getBufferLocked(backIdx);
528 }
529
530 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700531 mDirtyRegion.set(backBuffer->width, backBuffer->height);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700532 *buffer = backBuffer.get();
Mathias Agopian0926f502009-05-04 14:17:04 -0700533
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534 return NO_ERROR;
535}
536
537int Surface::lockBuffer(android_native_buffer_t* buffer)
538{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700539 Mutex::Autolock _l(mSurfaceLock);
540
541 per_client_cblk_t* const cblk = mClient->mControl;
542 status_t err = validate(cblk);
543 if (err != NO_ERROR)
544 return err;
545
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 // FIXME: lockBuffer() needs proper implementation
547 return 0;
548}
549
550int Surface::queueBuffer(android_native_buffer_t* buffer)
551{
552 Mutex::Autolock _l(mSurfaceLock);
553
554 per_client_cblk_t* const cblk = mClient->mControl;
555 status_t err = validate(cblk);
556 if (err != NO_ERROR)
557 return err;
558
Mathias Agopian0926f502009-05-04 14:17:04 -0700559 if (mSwapRectangle.isValid()) {
560 mDirtyRegion.set(mSwapRectangle);
561 }
562
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700563 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564 SurfaceID index(mToken);
565 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700566 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567
568 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
569 if (!(newstate & eNextFlipPending))
570 mClient->signalServer();
571
572 return NO_ERROR;
573}
574
575// ----------------------------------------------------------------------------
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577status_t Surface::lock(SurfaceInfo* info, bool blocking) {
578 return Surface::lock(info, NULL, blocking);
579}
580
Mathias Agopian0926f502009-05-04 14:17:04 -0700581status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700582{
583 // FIXME: needs some locking here
Mathias Agopian0926f502009-05-04 14:17:04 -0700584
585 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 status_t err = dequeueBuffer(&backBuffer);
587 if (err == NO_ERROR) {
588 err = lockBuffer(backBuffer);
589 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 // we handle copy-back here...
591
592 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700593 Region scratch(bounds);
594 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595
596 per_client_cblk_t* const cblk = mClient->mControl;
597 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
598 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
599 if (back->flags & surface_info_t::eBufferDirty) {
600 // content is meaningless in this case and the whole surface
601 // needs to be redrawn.
602 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700603 } else {
604 newDirtyRegion.andSelf(bounds);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 if (!(lcblk->flags & eNoCopyBack)) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700606 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
607 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
608 if (!copyback.isEmpty() && frontBuffer!=0) {
609 // copy front to back
610 copyBlt(backBuffer, frontBuffer, copyback);
611 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 }
613 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700614 mDirtyRegion = newDirtyRegion;
615 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700616
Mathias Agopiane71212b2009-05-05 00:37:46 -0700617 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700618 status_t res = backBuffer->lock(
619 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700620 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700621
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700622 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700623 mBackbufferIndex, backBuffer->handle);
624
625 mLockedBuffer = backBuffer;
626 other->w = backBuffer->width;
627 other->h = backBuffer->height;
628 other->s = backBuffer->stride;
629 other->usage = backBuffer->usage;
630 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700631 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700632 }
633 }
634 return err;
635}
636
637status_t Surface::unlockAndPost()
638{
639 // FIXME: needs some locking here
640
641 if (mLockedBuffer == 0)
642 return BAD_VALUE;
643
Mathias Agopian0926f502009-05-04 14:17:04 -0700644 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700646 mBackbufferIndex, mLockedBuffer->handle);
647
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700648 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 mLockedBuffer = 0;
650 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651}
652
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653void Surface::_send_dirty_region(
654 layer_cblk_t* lcblk, const Region& dirty)
655{
656 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
657 flat_region_t* flat_region = lcblk->region + index;
658 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
659 if (err < NO_ERROR) {
660 // region doesn't fit, use the bounds
661 const Region reg(dirty.bounds());
662 reg.write(flat_region, sizeof(flat_region_t));
663 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664}
665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666void Surface::setSwapRectangle(const Rect& r) {
667 mSwapRectangle = r;
668}
669
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670status_t Surface::getBufferLocked(int index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700672 status_t err = NO_MEMORY;
673 sp<SurfaceBuffer> buffer = mSurface->getBuffer();
674 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
675 if (buffer != 0) {
676 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
677 if (currentBuffer != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700678 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700679 currentBuffer.clear();
680 }
Mathias Agopian21c59d02009-05-05 00:59:23 -0700681 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700682 LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
683 if (err == NO_ERROR) {
684 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 }
686 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700687 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688}
689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690}; // namespace android
691