blob: 5f2138e8d6ca84891346001a65f7b16aa582d657 [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>
28#include <utils/IPCThreadState.h>
29#include <utils/IMemory.h>
30#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 Agopian076b1cc2009-04-10 14:24:30 -070039#include <EGL/android_natives.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <private/ui/SharedState.h>
42#include <private/ui/LayerState.h>
43
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044#include <pixelflinger/pixelflinger.h>
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046namespace android {
47
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048// ============================================================================
49// SurfaceBuffer
50// ============================================================================
51
Mathias Agopian9f88afb2009-04-17 14:15:18 -070052ANDROID_SINGLETON_STATIC_INSTANCE( SurfaceBuffer )
Mathias Agopian4243e662009-04-15 18:34:24 -070053
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054SurfaceBuffer::SurfaceBuffer()
Mathias Agopian0926f502009-05-04 14:17:04 -070055 : BASE(), handle(0), mOwner(false), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056{
57 width =
58 height =
59 stride =
60 format =
61 usage = 0;
62 android_native_buffer_t::getHandle = getHandle;
63}
64
65SurfaceBuffer::SurfaceBuffer(const Parcel& data)
Mathias Agopian0926f502009-05-04 14:17:04 -070066 : BASE(), handle(0), mOwner(true), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070067{
68 // we own the handle in this case
69 width = data.readInt32();
70 height = data.readInt32();
71 stride = data.readInt32();
72 format = data.readInt32();
73 usage = data.readInt32();
74 handle = data.readNativeHandle();
75 android_native_buffer_t::getHandle = getHandle;
76}
77
78SurfaceBuffer::~SurfaceBuffer()
79{
80 if (handle && mOwner) {
81 native_handle_close(handle);
82 native_handle_delete(const_cast<native_handle*>(handle));
83 }
84}
85
86int SurfaceBuffer::getHandle(android_native_buffer_t const * base,
87 buffer_handle_t* handle)
88{
89 *handle = getSelf(base)->handle;
90 return 0;
91}
92
Mathias Agopiane71212b2009-05-05 00:37:46 -070093status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070094{
95 const Rect lockBounds(width, height);
Mathias Agopiane71212b2009-05-05 00:37:46 -070096 status_t res = lock(usage, lockBounds, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070097 return res;
98}
99
Mathias Agopiane71212b2009-05-05 00:37:46 -0700100status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700101{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700102 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700103 return res;
104}
105
106status_t SurfaceBuffer::unlock()
107{
108 status_t res = getBufferMapper().unlock(handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700109 return res;
110}
111
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112status_t SurfaceBuffer::writeToParcel(Parcel* reply,
113 android_native_buffer_t const* buffer)
114{
115 buffer_handle_t handle;
116 status_t err = buffer->getHandle(buffer, &handle);
117 if (err < 0) {
118 return err;
119 }
120 reply->writeInt32(buffer->width);
121 reply->writeInt32(buffer->height);
122 reply->writeInt32(buffer->stride);
123 reply->writeInt32(buffer->format);
124 reply->writeInt32(buffer->usage);
125 reply->writeNativeHandle(handle);
126 return NO_ERROR;
127}
128
129// ----------------------------------------------------------------------
130
Mathias Agopian0926f502009-05-04 14:17:04 -0700131static void copyBlt(
132 const sp<SurfaceBuffer>& dst,
133 const sp<SurfaceBuffer>& src,
134 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700136 uint8_t const * src_bits;
137 src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Mathias Agopian0926f502009-05-04 14:17:04 -0700138
Mathias Agopiane71212b2009-05-05 00:37:46 -0700139 uint8_t* dst_bits;
140 dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Mathias Agopian0926f502009-05-04 14:17:04 -0700141
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142 Region::iterator iterator(reg);
143 if (iterator) {
144 // NOTE: dst and src must be the same format
145 Rect r;
146 const size_t bpp = bytesPerPixel(src->format);
147 const size_t dbpr = dst->stride * bpp;
148 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -0700149
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700150 while (iterator.iterate(&r)) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700151 ssize_t h = r.height();
152 if (h <= 0) continue;
153 size_t size = r.width() * bpp;
154 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
155 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
156 if (dbpr==sbpr && size==sbpr) {
157 size *= h;
158 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700160 do {
161 memcpy(d, s, size);
162 d += dbpr;
163 s += sbpr;
164 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700165 }
166 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700167
168 src->unlock();
169 dst->unlock();
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 Agopian0926f502009-05-04 14:17:04 -0700422 getBufferMapper().unregisterBuffer(mBuffers[i]->getHandle());
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 Agopian076b1cc2009-04-10 14:24:30 -0700619 if (!(lcblk->flags & eNoCopyBack)) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700620 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
621 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
622 if (!copyback.isEmpty() && frontBuffer!=0) {
623 // copy front to back
624 copyBlt(backBuffer, frontBuffer, copyback);
625 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700626 }
627 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700628 mDirtyRegion = newDirtyRegion;
629 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700630
Mathias Agopiane71212b2009-05-05 00:37:46 -0700631 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700632 status_t res = backBuffer->lock(
633 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700634 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700635
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700637 mBackbufferIndex, backBuffer->handle);
638
639 mLockedBuffer = backBuffer;
640 other->w = backBuffer->width;
641 other->h = backBuffer->height;
642 other->s = backBuffer->stride;
643 other->usage = backBuffer->usage;
644 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700645 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646 }
647 }
648 return err;
649}
650
651status_t Surface::unlockAndPost()
652{
653 // FIXME: needs some locking here
654
655 if (mLockedBuffer == 0)
656 return BAD_VALUE;
657
Mathias Agopian0926f502009-05-04 14:17:04 -0700658 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700660 mBackbufferIndex, mLockedBuffer->handle);
661
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700663 mLockedBuffer = 0;
664 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665}
666
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700667void Surface::_send_dirty_region(
668 layer_cblk_t* lcblk, const Region& dirty)
669{
670 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
671 flat_region_t* flat_region = lcblk->region + index;
672 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
673 if (err < NO_ERROR) {
674 // region doesn't fit, use the bounds
675 const Region reg(dirty.bounds());
676 reg.write(flat_region, sizeof(flat_region_t));
677 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678}
679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680void Surface::setSwapRectangle(const Rect& r) {
681 mSwapRectangle = r;
682}
683
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684status_t Surface::getBufferLocked(int index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700686 status_t err = NO_MEMORY;
687 sp<SurfaceBuffer> buffer = mSurface->getBuffer();
688 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
689 if (buffer != 0) {
690 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
691 if (currentBuffer != 0) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700692 getBufferMapper().unregisterBuffer(currentBuffer->getHandle());
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700693 currentBuffer.clear();
694 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700695 err = getBufferMapper().registerBuffer(buffer->getHandle());
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700696 LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
697 if (err == NO_ERROR) {
698 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 }
700 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700701 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702}
703
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704}; // namespace android
705