blob: aef47fdad06571c2d110edf8b5c654caa453cbc8 [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 Agopian14998592009-07-13 18:29:59 -070091 if (rect.left < 0 || rect.right > this->width ||
92 rect.top < 0 || rect.bottom > this->height) {
93 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
94 rect.left, rect.top, rect.right, rect.bottom,
95 this->width, this->height);
96 return BAD_VALUE;
97 }
Mathias Agopiane71212b2009-05-05 00:37:46 -070098 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070099 return res;
100}
101
102status_t SurfaceBuffer::unlock()
103{
104 status_t res = getBufferMapper().unlock(handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700105 return res;
106}
107
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108status_t SurfaceBuffer::writeToParcel(Parcel* reply,
109 android_native_buffer_t const* buffer)
110{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111 reply->writeInt32(buffer->width);
112 reply->writeInt32(buffer->height);
113 reply->writeInt32(buffer->stride);
114 reply->writeInt32(buffer->format);
115 reply->writeInt32(buffer->usage);
Mathias Agopian21c59d02009-05-05 00:59:23 -0700116 reply->writeNativeHandle(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700117 return NO_ERROR;
118}
119
120// ----------------------------------------------------------------------
121
Mathias Agopian14998592009-07-13 18:29:59 -0700122static status_t copyBlt(
Mathias Agopian0926f502009-05-04 14:17:04 -0700123 const sp<SurfaceBuffer>& dst,
124 const sp<SurfaceBuffer>& src,
125 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700126{
Mathias Agopian14998592009-07-13 18:29:59 -0700127 status_t err;
128 uint8_t const * src_bits = NULL;
129 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
130 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -0700131
Mathias Agopian14998592009-07-13 18:29:59 -0700132 uint8_t* dst_bits = NULL;
133 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
134 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
135
136 Region::const_iterator head(reg.begin());
137 Region::const_iterator tail(reg.end());
138 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 const size_t bpp = bytesPerPixel(src->format);
141 const size_t dbpr = dst->stride * bpp;
142 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -0700143
Mathias Agopian14998592009-07-13 18:29:59 -0700144 while (head != tail) {
145 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -0700146 ssize_t h = r.height();
147 if (h <= 0) continue;
148 size_t size = r.width() * bpp;
149 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
150 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
151 if (dbpr==sbpr && size==sbpr) {
152 size *= h;
153 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700154 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700155 do {
156 memcpy(d, s, size);
157 d += dbpr;
158 s += sbpr;
159 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160 }
161 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700162
Mathias Agopian14998592009-07-13 18:29:59 -0700163 if (src_bits)
164 src->unlock();
165
166 if (dst_bits)
167 dst->unlock();
168
169 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170}
171
Mathias Agopian62185b72009-04-16 16:19:50 -0700172// ============================================================================
173// SurfaceControl
174// ============================================================================
175
Mathias Agopian01b76682009-04-16 20:04:08 -0700176SurfaceControl::SurfaceControl(
177 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700178 const sp<ISurface>& surface,
179 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700180 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700181 : mClient(client), mSurface(surface),
182 mToken(data.token), mIdentity(data.identity),
Mathias Agopian18d84462009-04-16 20:30:22 -0700183 mFormat(format), mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700184{
185}
Mathias Agopian18d84462009-04-16 20:30:22 -0700186
Mathias Agopian62185b72009-04-16 16:19:50 -0700187SurfaceControl::~SurfaceControl()
188{
189 destroy();
190}
191
192void SurfaceControl::destroy()
193{
Mathias Agopian18d84462009-04-16 20:30:22 -0700194 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700195 mClient->destroySurface(mToken);
196 }
197
198 // clear all references and trigger an IPC now, to make sure things
199 // happen without delay, since these resources are quite heavy.
200 mClient.clear();
201 mSurface.clear();
202 IPCThreadState::self()->flushCommands();
203}
204
205void SurfaceControl::clear()
206{
207 // here, the window manager tells us explicitly that we should destroy
208 // the surface's resource. Soon after this call, it will also release
209 // its last reference (which will call the dtor); however, it is possible
210 // that a client living in the same process still holds references which
211 // would delay the call to the dtor -- that is why we need this explicit
212 // "clear()" call.
213 destroy();
214}
215
Mathias Agopian62185b72009-04-16 16:19:50 -0700216bool SurfaceControl::isSameSurface(
217 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
218{
219 if (lhs == 0 || rhs == 0)
220 return false;
221 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
222}
223
Mathias Agopian01b76682009-04-16 20:04:08 -0700224status_t SurfaceControl::setLayer(int32_t layer) {
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->setLayer(mToken, layer);
230}
231status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
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->setPosition(mToken, x, y);
237}
238status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
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->setSize(mToken, w, h);
244}
245status_t SurfaceControl::hide() {
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->hide(mToken);
251}
252status_t SurfaceControl::show(int32_t layer) {
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->show(mToken, layer);
258}
259status_t SurfaceControl::freeze() {
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->freeze(mToken);
265}
266status_t SurfaceControl::unfreeze() {
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->unfreeze(mToken);
272}
273status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
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->setFlags(mToken, flags, mask);
279}
280status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
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->setTransparentRegionHint(mToken, transparent);
286}
287status_t SurfaceControl::setAlpha(float alpha) {
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->setAlpha(mToken, alpha);
293}
294status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
295 const sp<SurfaceComposerClient>& client(mClient);
296 if (client == 0) return NO_INIT;
297 status_t err = validate(client->mControl);
298 if (err < 0) return err;
299 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
300}
301status_t SurfaceControl::setFreezeTint(uint32_t tint) {
302 const sp<SurfaceComposerClient>& client(mClient);
303 if (client == 0) return NO_INIT;
304 status_t err = validate(client->mControl);
305 if (err < 0) return err;
306 return client->setFreezeTint(mToken, tint);
307}
Mathias Agopian62185b72009-04-16 16:19:50 -0700308
309status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const
310{
311 if (mToken<0 || mClient==0) {
312 LOGE("invalid token (%d, identity=%u) or client (%p)",
313 mToken, mIdentity, mClient.get());
314 return NO_INIT;
315 }
316 if (cblk == 0) {
317 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
318 return NO_INIT;
319 }
320 status_t err = cblk->validate(mToken);
321 if (err != NO_ERROR) {
322 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
323 mToken, mIdentity, err, strerror(-err));
324 return err;
325 }
326 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
327 LOGE("using an invalid surface id=%d, identity=%u should be %d",
328 mToken, mIdentity, cblk->layers[mToken].identity);
329 return NO_INIT;
330 }
331 return NO_ERROR;
332}
333
Mathias Agopian01b76682009-04-16 20:04:08 -0700334status_t SurfaceControl::writeSurfaceToParcel(
335 const sp<SurfaceControl>& control, Parcel* parcel)
336{
337 uint32_t flags = 0;
338 uint32_t format = 0;
339 SurfaceID token = -1;
340 uint32_t identity = 0;
341 sp<SurfaceComposerClient> client;
342 sp<ISurface> sur;
343 if (SurfaceControl::isValid(control)) {
344 token = control->mToken;
345 identity = control->mIdentity;
346 client = control->mClient;
347 sur = control->mSurface;
348 format = control->mFormat;
349 flags = control->mFlags;
350 }
351 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
352 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
353 parcel->writeInt32(token);
354 parcel->writeInt32(identity);
355 parcel->writeInt32(format);
356 parcel->writeInt32(flags);
357 return NO_ERROR;
358}
359
360sp<Surface> SurfaceControl::getSurface() const
361{
362 Mutex::Autolock _l(mLock);
363 if (mSurfaceData == 0) {
364 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
365 }
366 return mSurfaceData;
367}
368
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700369// ============================================================================
370// Surface
371// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372
Mathias Agopian01b76682009-04-16 20:04:08 -0700373Surface::Surface(const sp<SurfaceControl>& surface)
374 : mClient(surface->mClient), mSurface(surface->mSurface),
375 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700376 mFormat(surface->mFormat), mFlags(surface->mFlags),
377 mBufferMapper(BufferMapper::get())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378{
Mathias Agopian01b76682009-04-16 20:04:08 -0700379 init();
380}
Mathias Agopian62185b72009-04-16 16:19:50 -0700381
Mathias Agopian01b76682009-04-16 20:04:08 -0700382Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700383 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-04-16 20:04:08 -0700384{
385 sp<IBinder> clientBinder = parcel.readStrongBinder();
386 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
387 mToken = parcel.readInt32();
388 mIdentity = parcel.readInt32();
389 mFormat = parcel.readInt32();
390 mFlags = parcel.readInt32();
391
392 if (clientBinder != NULL)
393 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
394
395 init();
396}
397
398void Surface::init()
399{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700400 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700401 android_native_window_t::dequeueBuffer = dequeueBuffer;
402 android_native_window_t::lockBuffer = lockBuffer;
403 android_native_window_t::queueBuffer = queueBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700405 DisplayInfo dinfo;
406 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
407 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
408 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
409 // FIXME: set real values here
410 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
411 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
412 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413}
414
Mathias Agopian01b76682009-04-16 20:04:08 -0700415
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416Surface::~Surface()
417{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700418 // this is a client-side operation, the surface is destroyed, unmap
419 // its buffers in this process.
420 for (int i=0 ; i<2 ; i++) {
421 if (mBuffers[i] != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700422 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700423 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700425
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700426 // clear all references and trigger an IPC now, to make sure things
427 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 mClient.clear();
429 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 IPCThreadState::self()->flushCommands();
431}
432
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433status_t Surface::validate(per_client_cblk_t const* cblk) const
434{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700435 if (mToken<0 || mClient==0) {
436 LOGE("invalid token (%d, identity=%u) or client (%p)",
437 mToken, mIdentity, mClient.get());
438 return NO_INIT;
439 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700440 if (cblk == 0) {
441 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
442 return NO_INIT;
443 }
444 status_t err = cblk->validate(mToken);
445 if (err != NO_ERROR) {
446 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
447 mToken, mIdentity, err, strerror(-err));
448 return err;
449 }
450 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
451 LOGE("using an invalid surface id=%d, identity=%u should be %d",
452 mToken, mIdentity, cblk->layers[mToken].identity);
453 return NO_INIT;
454 }
455 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456}
457
Mathias Agopian01b76682009-04-16 20:04:08 -0700458
459bool Surface::isSameSurface(
460 const sp<Surface>& lhs, const sp<Surface>& rhs)
461{
462 if (lhs == 0 || rhs == 0)
463 return false;
464 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
465}
466
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700467// ----------------------------------------------------------------------------
468
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700469int Surface::setSwapInterval(android_native_window_t* window, int interval)
470{
471 return 0;
472}
473
474int Surface::dequeueBuffer(android_native_window_t* window,
475 android_native_buffer_t** buffer)
476{
477 Surface* self = getSelf(window);
478 return self->dequeueBuffer(buffer);
479}
480
481int Surface::lockBuffer(android_native_window_t* window,
482 android_native_buffer_t* buffer)
483{
484 Surface* self = getSelf(window);
485 return self->lockBuffer(buffer);
486}
487
488int Surface::queueBuffer(android_native_window_t* window,
489 android_native_buffer_t* buffer)
490{
491 Surface* self = getSelf(window);
492 return self->queueBuffer(buffer);
493}
494
495// ----------------------------------------------------------------------------
496
Mathias Agopian0926f502009-05-04 14:17:04 -0700497status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
498{
499 android_native_buffer_t* out;
500 status_t err = dequeueBuffer(&out);
501 *buffer = SurfaceBuffer::getSelf(out);
502 return err;
503}
504
505status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
506{
507 return lockBuffer(buffer.get());
508}
509
510status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
511{
512 return queueBuffer(buffer.get());
513}
514
515// ----------------------------------------------------------------------------
516
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517int Surface::dequeueBuffer(android_native_buffer_t** buffer)
518{
519 // FIXME: dequeueBuffer() needs proper implementation
520
521 Mutex::Autolock _l(mSurfaceLock);
522
523 per_client_cblk_t* const cblk = mClient->mControl;
524 status_t err = validate(cblk);
525 if (err != NO_ERROR)
526 return err;
527
528 SurfaceID index(mToken);
529
530 int32_t backIdx = cblk->lock_layer(size_t(index),
531 per_client_cblk_t::BLOCKING);
532
533 if (backIdx < 0)
534 return status_t(backIdx);
535
536 mBackbufferIndex = backIdx;
537 layer_cblk_t* const lcblk = &(cblk->layers[index]);
538
539 volatile const surface_info_t* const back = lcblk->surface + backIdx;
540 if (back->flags & surface_info_t::eNeedNewBuffer) {
541 getBufferLocked(backIdx);
542 }
543
544 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700545 mDirtyRegion.set(backBuffer->width, backBuffer->height);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 *buffer = backBuffer.get();
Mathias Agopian0926f502009-05-04 14:17:04 -0700547
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700548 return NO_ERROR;
549}
550
551int Surface::lockBuffer(android_native_buffer_t* buffer)
552{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700553 Mutex::Autolock _l(mSurfaceLock);
554
555 per_client_cblk_t* const cblk = mClient->mControl;
556 status_t err = validate(cblk);
557 if (err != NO_ERROR)
558 return err;
559
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560 // FIXME: lockBuffer() needs proper implementation
561 return 0;
562}
563
564int Surface::queueBuffer(android_native_buffer_t* buffer)
565{
566 Mutex::Autolock _l(mSurfaceLock);
567
568 per_client_cblk_t* const cblk = mClient->mControl;
569 status_t err = validate(cblk);
570 if (err != NO_ERROR)
571 return err;
572
Mathias Agopian0926f502009-05-04 14:17:04 -0700573 if (mSwapRectangle.isValid()) {
574 mDirtyRegion.set(mSwapRectangle);
575 }
576
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578 SurfaceID index(mToken);
579 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700580 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581
582 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
583 if (!(newstate & eNextFlipPending))
584 mClient->signalServer();
585
586 return NO_ERROR;
587}
588
589// ----------------------------------------------------------------------------
590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591status_t Surface::lock(SurfaceInfo* info, bool blocking) {
592 return Surface::lock(info, NULL, blocking);
593}
594
Mathias Agopian0926f502009-05-04 14:17:04 -0700595status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700596{
597 // FIXME: needs some locking here
Mathias Agopian0926f502009-05-04 14:17:04 -0700598
599 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600 status_t err = dequeueBuffer(&backBuffer);
601 if (err == NO_ERROR) {
602 err = lockBuffer(backBuffer);
603 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 // we handle copy-back here...
605
606 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700607 Region scratch(bounds);
608 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700609
610 per_client_cblk_t* const cblk = mClient->mControl;
611 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
612 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
613 if (back->flags & surface_info_t::eBufferDirty) {
614 // content is meaningless in this case and the whole surface
615 // needs to be redrawn.
616 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700617 } else {
618 newDirtyRegion.andSelf(bounds);
Mathias Agopian14998592009-07-13 18:29:59 -0700619 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
620 if (backBuffer->width == frontBuffer->width &&
621 backBuffer->height == frontBuffer->height &&
622 !(lcblk->flags & eNoCopyBack))
623 {
Mathias Agopian0926f502009-05-04 14:17:04 -0700624 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
625 if (!copyback.isEmpty() && frontBuffer!=0) {
626 // copy front to back
627 copyBlt(backBuffer, frontBuffer, copyback);
628 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700629 }
630 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700631 mDirtyRegion = newDirtyRegion;
632 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700633
Mathias Agopiane71212b2009-05-05 00:37:46 -0700634 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700635 status_t res = backBuffer->lock(
636 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700637 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700638
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700640 mBackbufferIndex, backBuffer->handle);
641
642 mLockedBuffer = backBuffer;
643 other->w = backBuffer->width;
644 other->h = backBuffer->height;
645 other->s = backBuffer->stride;
646 other->usage = backBuffer->usage;
647 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700648 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 }
650 }
651 return err;
652}
653
654status_t Surface::unlockAndPost()
655{
656 // FIXME: needs some locking here
657
658 if (mLockedBuffer == 0)
659 return BAD_VALUE;
660
Mathias Agopian0926f502009-05-04 14:17:04 -0700661 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700663 mBackbufferIndex, mLockedBuffer->handle);
664
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700665 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700666 mLockedBuffer = 0;
667 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668}
669
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670void Surface::_send_dirty_region(
671 layer_cblk_t* lcblk, const Region& dirty)
672{
673 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
674 flat_region_t* flat_region = lcblk->region + index;
675 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
676 if (err < NO_ERROR) {
677 // region doesn't fit, use the bounds
678 const Region reg(dirty.bounds());
679 reg.write(flat_region, sizeof(flat_region_t));
680 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681}
682
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683void Surface::setSwapRectangle(const Rect& r) {
684 mSwapRectangle = r;
685}
686
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700687status_t Surface::getBufferLocked(int index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700689 status_t err = NO_MEMORY;
690 sp<SurfaceBuffer> buffer = mSurface->getBuffer();
691 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
692 if (buffer != 0) {
693 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
694 if (currentBuffer != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700695 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700696 currentBuffer.clear();
697 }
Mathias Agopian21c59d02009-05-05 00:59:23 -0700698 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700699 LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
700 if (err == NO_ERROR) {
701 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 }
703 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700704 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705}
706
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707}; // namespace android
708