blob: a4710aafb4959eebc03d700b9b119dfe17e97340 [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 Agopiancb6b9042009-07-30 18:14:56 -0700183 mWidth(w), mHeight(h), 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;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700341 uint32_t width = 0;
342 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700343 sp<SurfaceComposerClient> client;
344 sp<ISurface> sur;
345 if (SurfaceControl::isValid(control)) {
346 token = control->mToken;
347 identity = control->mIdentity;
348 client = control->mClient;
349 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700350 width = control->mWidth;
351 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700352 format = control->mFormat;
353 flags = control->mFlags;
354 }
355 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
356 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
357 parcel->writeInt32(token);
358 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700359 parcel->writeInt32(width);
360 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700361 parcel->writeInt32(format);
362 parcel->writeInt32(flags);
363 return NO_ERROR;
364}
365
366sp<Surface> SurfaceControl::getSurface() const
367{
368 Mutex::Autolock _l(mLock);
369 if (mSurfaceData == 0) {
370 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
371 }
372 return mSurfaceData;
373}
374
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700375// ============================================================================
376// Surface
377// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378
Mathias Agopian01b76682009-04-16 20:04:08 -0700379Surface::Surface(const sp<SurfaceControl>& surface)
380 : mClient(surface->mClient), mSurface(surface->mSurface),
381 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700382 mWidth(surface->mWidth), mHeight(surface->mHeight),
Mathias Agopian0926f502009-05-04 14:17:04 -0700383 mFormat(surface->mFormat), mFlags(surface->mFlags),
384 mBufferMapper(BufferMapper::get())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385{
Mathias Agopian01b76682009-04-16 20:04:08 -0700386 init();
387}
Mathias Agopian62185b72009-04-16 16:19:50 -0700388
Mathias Agopian01b76682009-04-16 20:04:08 -0700389Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700390 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-04-16 20:04:08 -0700391{
392 sp<IBinder> clientBinder = parcel.readStrongBinder();
393 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
394 mToken = parcel.readInt32();
395 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700396 mWidth = parcel.readInt32();
397 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700398 mFormat = parcel.readInt32();
399 mFlags = parcel.readInt32();
400
401 if (clientBinder != NULL)
402 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
403
404 init();
405}
406
407void Surface::init()
408{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700409 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700410 android_native_window_t::dequeueBuffer = dequeueBuffer;
411 android_native_window_t::lockBuffer = lockBuffer;
412 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700413 android_native_window_t::query = query;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700415 DisplayInfo dinfo;
416 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
417 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
418 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
419 // FIXME: set real values here
420 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
421 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
422 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
Mathias Agopian01b76682009-04-16 20:04:08 -0700425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426Surface::~Surface()
427{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700428 // this is a client-side operation, the surface is destroyed, unmap
429 // its buffers in this process.
430 for (int i=0 ; i<2 ; i++) {
431 if (mBuffers[i] != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700432 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700435
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700436 // clear all references and trigger an IPC now, to make sure things
437 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 mClient.clear();
439 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 IPCThreadState::self()->flushCommands();
441}
442
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700443status_t Surface::validate(per_client_cblk_t const* cblk) const
444{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700445 if (mToken<0 || mClient==0) {
446 LOGE("invalid token (%d, identity=%u) or client (%p)",
447 mToken, mIdentity, mClient.get());
448 return NO_INIT;
449 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700450 if (cblk == 0) {
451 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
452 return NO_INIT;
453 }
454 status_t err = cblk->validate(mToken);
455 if (err != NO_ERROR) {
456 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
457 mToken, mIdentity, err, strerror(-err));
458 return err;
459 }
460 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
461 LOGE("using an invalid surface id=%d, identity=%u should be %d",
462 mToken, mIdentity, cblk->layers[mToken].identity);
463 return NO_INIT;
464 }
465 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466}
467
Mathias Agopian01b76682009-04-16 20:04:08 -0700468
469bool Surface::isSameSurface(
470 const sp<Surface>& lhs, const sp<Surface>& rhs)
471{
472 if (lhs == 0 || rhs == 0)
473 return false;
474 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
475}
476
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700477// ----------------------------------------------------------------------------
478
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700479int Surface::setSwapInterval(android_native_window_t* window, int interval)
480{
481 return 0;
482}
483
484int Surface::dequeueBuffer(android_native_window_t* window,
485 android_native_buffer_t** buffer)
486{
487 Surface* self = getSelf(window);
488 return self->dequeueBuffer(buffer);
489}
490
491int Surface::lockBuffer(android_native_window_t* window,
492 android_native_buffer_t* buffer)
493{
494 Surface* self = getSelf(window);
495 return self->lockBuffer(buffer);
496}
497
498int Surface::queueBuffer(android_native_window_t* window,
499 android_native_buffer_t* buffer)
500{
501 Surface* self = getSelf(window);
502 return self->queueBuffer(buffer);
503}
504
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700505int Surface::query(android_native_window_t* window,
506 int what, int* value)
507{
508 Surface* self = getSelf(window);
509 return self->query(what, value);
510}
511
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700512// ----------------------------------------------------------------------------
513
Mathias Agopian0926f502009-05-04 14:17:04 -0700514status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
515{
516 android_native_buffer_t* out;
517 status_t err = dequeueBuffer(&out);
518 *buffer = SurfaceBuffer::getSelf(out);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700519 // reset the width/height with the what we get from the buffer
520 mWidth = uint32_t(out->width);
521 mHeight = uint32_t(out->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700522 return err;
523}
524
525status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
526{
527 return lockBuffer(buffer.get());
528}
529
530status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
531{
532 return queueBuffer(buffer.get());
533}
534
535// ----------------------------------------------------------------------------
536
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537int Surface::dequeueBuffer(android_native_buffer_t** buffer)
538{
539 // FIXME: dequeueBuffer() needs proper implementation
540
541 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
548 SurfaceID index(mToken);
549
550 int32_t backIdx = cblk->lock_layer(size_t(index),
551 per_client_cblk_t::BLOCKING);
552
553 if (backIdx < 0)
554 return status_t(backIdx);
555
556 mBackbufferIndex = backIdx;
557 layer_cblk_t* const lcblk = &(cblk->layers[index]);
558
559 volatile const surface_info_t* const back = lcblk->surface + backIdx;
560 if (back->flags & surface_info_t::eNeedNewBuffer) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700561 err = getBufferLocked(backIdx);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700562 }
563
Mathias Agopiancf81c842009-07-31 14:47:00 -0700564 if (err == NO_ERROR) {
565 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
566 mDirtyRegion.set(backBuffer->width, backBuffer->height);
567 *buffer = backBuffer.get();
568 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700569
Mathias Agopiancf81c842009-07-31 14:47:00 -0700570 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571}
572
573int Surface::lockBuffer(android_native_buffer_t* buffer)
574{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700575 Mutex::Autolock _l(mSurfaceLock);
576
577 per_client_cblk_t* const cblk = mClient->mControl;
578 status_t err = validate(cblk);
579 if (err != NO_ERROR)
580 return err;
581
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700582 // FIXME: lockBuffer() needs proper implementation
583 return 0;
584}
585
586int Surface::queueBuffer(android_native_buffer_t* buffer)
587{
588 Mutex::Autolock _l(mSurfaceLock);
589
590 per_client_cblk_t* const cblk = mClient->mControl;
591 status_t err = validate(cblk);
592 if (err != NO_ERROR)
593 return err;
594
Mathias Agopian0926f502009-05-04 14:17:04 -0700595 if (mSwapRectangle.isValid()) {
596 mDirtyRegion.set(mSwapRectangle);
597 }
598
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600 SurfaceID index(mToken);
601 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700602 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603
604 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
605 if (!(newstate & eNextFlipPending))
606 mClient->signalServer();
607
608 return NO_ERROR;
609}
610
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700611int Surface::query(int what, int* value)
612{
613 switch (what) {
614 case NATIVE_WINDOW_WIDTH:
615 *value = int(mWidth);
616 return NO_ERROR;
617 case NATIVE_WINDOW_HEIGHT:
618 *value = int(mHeight);
619 return NO_ERROR;
620 }
621 return BAD_VALUE;
622}
623
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624// ----------------------------------------------------------------------------
625
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626status_t Surface::lock(SurfaceInfo* info, bool blocking) {
627 return Surface::lock(info, NULL, blocking);
628}
629
Mathias Agopian0926f502009-05-04 14:17:04 -0700630status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700631{
632 // FIXME: needs some locking here
Mathias Agopian0926f502009-05-04 14:17:04 -0700633
634 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700635 status_t err = dequeueBuffer(&backBuffer);
636 if (err == NO_ERROR) {
637 err = lockBuffer(backBuffer);
638 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 // we handle copy-back here...
640
641 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700642 Region scratch(bounds);
643 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644
645 per_client_cblk_t* const cblk = mClient->mControl;
646 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
647 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
648 if (back->flags & surface_info_t::eBufferDirty) {
649 // content is meaningless in this case and the whole surface
650 // needs to be redrawn.
651 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700652 } else {
653 newDirtyRegion.andSelf(bounds);
Mathias Agopian14998592009-07-13 18:29:59 -0700654 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
655 if (backBuffer->width == frontBuffer->width &&
656 backBuffer->height == frontBuffer->height &&
657 !(lcblk->flags & eNoCopyBack))
658 {
Mathias Agopian0926f502009-05-04 14:17:04 -0700659 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
660 if (!copyback.isEmpty() && frontBuffer!=0) {
661 // copy front to back
662 copyBlt(backBuffer, frontBuffer, copyback);
663 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700664 }
665 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700666 mDirtyRegion = newDirtyRegion;
667 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700668
Mathias Agopiane71212b2009-05-05 00:37:46 -0700669 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700670 status_t res = backBuffer->lock(
671 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700672 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700673
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700674 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700675 mBackbufferIndex, backBuffer->handle);
676
677 mLockedBuffer = backBuffer;
678 other->w = backBuffer->width;
679 other->h = backBuffer->height;
680 other->s = backBuffer->stride;
681 other->usage = backBuffer->usage;
682 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700683 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684 }
685 }
686 return err;
687}
688
689status_t Surface::unlockAndPost()
690{
691 // FIXME: needs some locking here
692
693 if (mLockedBuffer == 0)
694 return BAD_VALUE;
695
Mathias Agopian0926f502009-05-04 14:17:04 -0700696 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700697 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700698 mBackbufferIndex, mLockedBuffer->handle);
699
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700700 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700701 mLockedBuffer = 0;
702 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703}
704
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700705void Surface::_send_dirty_region(
706 layer_cblk_t* lcblk, const Region& dirty)
707{
708 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
709 flat_region_t* flat_region = lcblk->region + index;
710 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
711 if (err < NO_ERROR) {
712 // region doesn't fit, use the bounds
713 const Region reg(dirty.bounds());
714 reg.write(flat_region, sizeof(flat_region_t));
715 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716}
717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718void Surface::setSwapRectangle(const Rect& r) {
719 mSwapRectangle = r;
720}
721
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700722status_t Surface::getBufferLocked(int index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700724 status_t err = NO_MEMORY;
725 sp<SurfaceBuffer> buffer = mSurface->getBuffer();
726 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
727 if (buffer != 0) {
728 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
729 if (currentBuffer != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700730 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700731 currentBuffer.clear();
732 }
Mathias Agopian21c59d02009-05-05 00:59:23 -0700733 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700734 LOGW_IF(err, "registerBuffer(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700735 if (err == NO_ERROR) {
736 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 }
738 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700739 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742}; // namespace android
743