blob: 36a10cfa68f04efa0414da82f9edba17f72fd522 [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();
Mathias Agopian50517542009-08-19 17:10:18 -070067 if (width < 0) {
68 width = height = stride = format = usage = 0;
69 handle = 0;
70 } else {
71 height = data.readInt32();
72 stride = data.readInt32();
73 format = data.readInt32();
74 usage = data.readInt32();
75 handle = data.readNativeHandle();
76 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077}
78
79SurfaceBuffer::~SurfaceBuffer()
80{
81 if (handle && mOwner) {
82 native_handle_close(handle);
83 native_handle_delete(const_cast<native_handle*>(handle));
84 }
85}
86
Mathias Agopiane71212b2009-05-05 00:37:46 -070087status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070088{
89 const Rect lockBounds(width, height);
Mathias Agopiane71212b2009-05-05 00:37:46 -070090 status_t res = lock(usage, lockBounds, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070091 return res;
92}
93
Mathias Agopiane71212b2009-05-05 00:37:46 -070094status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070095{
Mathias Agopian14998592009-07-13 18:29:59 -070096 if (rect.left < 0 || rect.right > this->width ||
97 rect.top < 0 || rect.bottom > this->height) {
98 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
99 rect.left, rect.top, rect.right, rect.bottom,
100 this->width, this->height);
101 return BAD_VALUE;
102 }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700103 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700104 return res;
105}
106
107status_t SurfaceBuffer::unlock()
108{
109 status_t res = getBufferMapper().unlock(handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700110 return res;
111}
112
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113status_t SurfaceBuffer::writeToParcel(Parcel* reply,
114 android_native_buffer_t const* buffer)
115{
Mathias Agopian50517542009-08-19 17:10:18 -0700116 if (buffer == NULL)
Mathias Agopian3eded942009-08-04 20:53:59 -0700117 return BAD_VALUE;
Mathias Agopian50517542009-08-19 17:10:18 -0700118
119 if (buffer->width < 0 || buffer->height < 0)
120 return BAD_VALUE;
121
122 status_t err = NO_ERROR;
123 if (buffer->handle == NULL) {
124 // this buffer doesn't have a handle
125 reply->writeInt32(NO_MEMORY);
126 } else {
127 reply->writeInt32(buffer->width);
128 reply->writeInt32(buffer->height);
129 reply->writeInt32(buffer->stride);
130 reply->writeInt32(buffer->format);
131 reply->writeInt32(buffer->usage);
132 err = reply->writeNativeHandle(buffer->handle);
Mathias Agopian3eded942009-08-04 20:53:59 -0700133 }
Mathias Agopian50517542009-08-19 17:10:18 -0700134 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135}
136
137// ----------------------------------------------------------------------
138
Mathias Agopian14998592009-07-13 18:29:59 -0700139static status_t copyBlt(
Mathias Agopian0926f502009-05-04 14:17:04 -0700140 const sp<SurfaceBuffer>& dst,
141 const sp<SurfaceBuffer>& src,
142 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143{
Mathias Agopian14998592009-07-13 18:29:59 -0700144 status_t err;
145 uint8_t const * src_bits = NULL;
146 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
147 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -0700148
Mathias Agopian14998592009-07-13 18:29:59 -0700149 uint8_t* dst_bits = NULL;
150 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
151 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
152
153 Region::const_iterator head(reg.begin());
154 Region::const_iterator tail(reg.end());
155 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700156 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157 const size_t bpp = bytesPerPixel(src->format);
158 const size_t dbpr = dst->stride * bpp;
159 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -0700160
Mathias Agopian14998592009-07-13 18:29:59 -0700161 while (head != tail) {
162 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -0700163 ssize_t h = r.height();
164 if (h <= 0) continue;
165 size_t size = r.width() * bpp;
166 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
167 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
168 if (dbpr==sbpr && size==sbpr) {
169 size *= h;
170 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700172 do {
173 memcpy(d, s, size);
174 d += dbpr;
175 s += sbpr;
176 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 }
178 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700179
Mathias Agopian14998592009-07-13 18:29:59 -0700180 if (src_bits)
181 src->unlock();
182
183 if (dst_bits)
184 dst->unlock();
185
186 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187}
188
Mathias Agopian62185b72009-04-16 16:19:50 -0700189// ============================================================================
190// SurfaceControl
191// ============================================================================
192
Mathias Agopian01b76682009-04-16 20:04:08 -0700193SurfaceControl::SurfaceControl(
194 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700195 const sp<ISurface>& surface,
196 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700197 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700198 : mClient(client), mSurface(surface),
199 mToken(data.token), mIdentity(data.identity),
Mathias Agopian1c97d2e2009-08-19 17:46:26 -0700200 mWidth(data.width), mHeight(data.height), mFormat(data.format),
201 mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700202{
203}
Mathias Agopian18d84462009-04-16 20:30:22 -0700204
Mathias Agopian62185b72009-04-16 16:19:50 -0700205SurfaceControl::~SurfaceControl()
206{
207 destroy();
208}
209
210void SurfaceControl::destroy()
211{
Mathias Agopian18d84462009-04-16 20:30:22 -0700212 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700213 mClient->destroySurface(mToken);
214 }
215
216 // clear all references and trigger an IPC now, to make sure things
217 // happen without delay, since these resources are quite heavy.
218 mClient.clear();
219 mSurface.clear();
220 IPCThreadState::self()->flushCommands();
221}
222
223void SurfaceControl::clear()
224{
225 // here, the window manager tells us explicitly that we should destroy
226 // the surface's resource. Soon after this call, it will also release
227 // its last reference (which will call the dtor); however, it is possible
228 // that a client living in the same process still holds references which
229 // would delay the call to the dtor -- that is why we need this explicit
230 // "clear()" call.
231 destroy();
232}
233
Mathias Agopian62185b72009-04-16 16:19:50 -0700234bool SurfaceControl::isSameSurface(
235 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
236{
237 if (lhs == 0 || rhs == 0)
238 return false;
239 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
240}
241
Mathias Agopian01b76682009-04-16 20:04:08 -0700242status_t SurfaceControl::setLayer(int32_t layer) {
243 const sp<SurfaceComposerClient>& client(mClient);
244 if (client == 0) return NO_INIT;
245 status_t err = validate(client->mControl);
246 if (err < 0) return err;
247 return client->setLayer(mToken, layer);
248}
249status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
250 const sp<SurfaceComposerClient>& client(mClient);
251 if (client == 0) return NO_INIT;
252 status_t err = validate(client->mControl);
253 if (err < 0) return err;
254 return client->setPosition(mToken, x, y);
255}
256status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
257 const sp<SurfaceComposerClient>& client(mClient);
258 if (client == 0) return NO_INIT;
259 status_t err = validate(client->mControl);
260 if (err < 0) return err;
261 return client->setSize(mToken, w, h);
262}
263status_t SurfaceControl::hide() {
264 const sp<SurfaceComposerClient>& client(mClient);
265 if (client == 0) return NO_INIT;
266 status_t err = validate(client->mControl);
267 if (err < 0) return err;
268 return client->hide(mToken);
269}
270status_t SurfaceControl::show(int32_t layer) {
271 const sp<SurfaceComposerClient>& client(mClient);
272 if (client == 0) return NO_INIT;
273 status_t err = validate(client->mControl);
274 if (err < 0) return err;
275 return client->show(mToken, layer);
276}
277status_t SurfaceControl::freeze() {
278 const sp<SurfaceComposerClient>& client(mClient);
279 if (client == 0) return NO_INIT;
280 status_t err = validate(client->mControl);
281 if (err < 0) return err;
282 return client->freeze(mToken);
283}
284status_t SurfaceControl::unfreeze() {
285 const sp<SurfaceComposerClient>& client(mClient);
286 if (client == 0) return NO_INIT;
287 status_t err = validate(client->mControl);
288 if (err < 0) return err;
289 return client->unfreeze(mToken);
290}
291status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
292 const sp<SurfaceComposerClient>& client(mClient);
293 if (client == 0) return NO_INIT;
294 status_t err = validate(client->mControl);
295 if (err < 0) return err;
296 return client->setFlags(mToken, flags, mask);
297}
298status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
299 const sp<SurfaceComposerClient>& client(mClient);
300 if (client == 0) return NO_INIT;
301 status_t err = validate(client->mControl);
302 if (err < 0) return err;
303 return client->setTransparentRegionHint(mToken, transparent);
304}
305status_t SurfaceControl::setAlpha(float alpha) {
306 const sp<SurfaceComposerClient>& client(mClient);
307 if (client == 0) return NO_INIT;
308 status_t err = validate(client->mControl);
309 if (err < 0) return err;
310 return client->setAlpha(mToken, alpha);
311}
312status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
313 const sp<SurfaceComposerClient>& client(mClient);
314 if (client == 0) return NO_INIT;
315 status_t err = validate(client->mControl);
316 if (err < 0) return err;
317 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
318}
319status_t SurfaceControl::setFreezeTint(uint32_t tint) {
320 const sp<SurfaceComposerClient>& client(mClient);
321 if (client == 0) return NO_INIT;
322 status_t err = validate(client->mControl);
323 if (err < 0) return err;
324 return client->setFreezeTint(mToken, tint);
325}
Mathias Agopian62185b72009-04-16 16:19:50 -0700326
327status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const
328{
329 if (mToken<0 || mClient==0) {
330 LOGE("invalid token (%d, identity=%u) or client (%p)",
331 mToken, mIdentity, mClient.get());
332 return NO_INIT;
333 }
334 if (cblk == 0) {
335 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
336 return NO_INIT;
337 }
338 status_t err = cblk->validate(mToken);
339 if (err != NO_ERROR) {
340 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
341 mToken, mIdentity, err, strerror(-err));
342 return err;
343 }
344 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
345 LOGE("using an invalid surface id=%d, identity=%u should be %d",
346 mToken, mIdentity, cblk->layers[mToken].identity);
347 return NO_INIT;
348 }
349 return NO_ERROR;
350}
351
Mathias Agopian01b76682009-04-16 20:04:08 -0700352status_t SurfaceControl::writeSurfaceToParcel(
353 const sp<SurfaceControl>& control, Parcel* parcel)
354{
355 uint32_t flags = 0;
356 uint32_t format = 0;
357 SurfaceID token = -1;
358 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700359 uint32_t width = 0;
360 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700361 sp<SurfaceComposerClient> client;
362 sp<ISurface> sur;
363 if (SurfaceControl::isValid(control)) {
364 token = control->mToken;
365 identity = control->mIdentity;
366 client = control->mClient;
367 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700368 width = control->mWidth;
369 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700370 format = control->mFormat;
371 flags = control->mFlags;
372 }
373 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
374 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
375 parcel->writeInt32(token);
376 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700377 parcel->writeInt32(width);
378 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700379 parcel->writeInt32(format);
380 parcel->writeInt32(flags);
381 return NO_ERROR;
382}
383
384sp<Surface> SurfaceControl::getSurface() const
385{
386 Mutex::Autolock _l(mLock);
387 if (mSurfaceData == 0) {
388 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
389 }
390 return mSurfaceData;
391}
392
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700393// ============================================================================
394// Surface
395// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396
Mathias Agopian01b76682009-04-16 20:04:08 -0700397Surface::Surface(const sp<SurfaceControl>& surface)
398 : mClient(surface->mClient), mSurface(surface->mSurface),
399 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700400 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700401 mBufferMapper(BufferMapper::get()),
402 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403{
Mathias Agopian01b76682009-04-16 20:04:08 -0700404 init();
405}
Mathias Agopian62185b72009-04-16 16:19:50 -0700406
Mathias Agopian01b76682009-04-16 20:04:08 -0700407Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700408 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-04-16 20:04:08 -0700409{
410 sp<IBinder> clientBinder = parcel.readStrongBinder();
411 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
412 mToken = parcel.readInt32();
413 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700414 mWidth = parcel.readInt32();
415 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700416 mFormat = parcel.readInt32();
417 mFlags = parcel.readInt32();
418
419 if (clientBinder != NULL)
420 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
421
422 init();
423}
424
425void Surface::init()
426{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700427 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700428 android_native_window_t::dequeueBuffer = dequeueBuffer;
429 android_native_window_t::lockBuffer = lockBuffer;
430 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700431 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700432 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700434 DisplayInfo dinfo;
435 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
436 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
437 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
438 // FIXME: set real values here
439 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
440 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
441 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian52212712009-08-11 22:34:02 -0700442 // be default we request a hardware surface
443 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700444 mUsageChanged = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
446
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447Surface::~Surface()
448{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700449 // this is a client-side operation, the surface is destroyed, unmap
450 // its buffers in this process.
451 for (int i=0 ; i<2 ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700452 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700453 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700456
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700457 // clear all references and trigger an IPC now, to make sure things
458 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459 mClient.clear();
460 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 IPCThreadState::self()->flushCommands();
462}
463
Mathias Agopianba5972f2009-08-14 18:52:17 -0700464sp<SurfaceComposerClient> Surface::getClient() const {
465 return mClient;
466}
467
468sp<ISurface> Surface::getISurface() const {
469 return mSurface;
470}
471
472bool Surface::isValid() {
473 return mToken>=0 && mClient!=0;
474}
475
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700476status_t Surface::validate(per_client_cblk_t const* cblk) const
477{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700478 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700479 if (mToken<0 || mClient==0) {
480 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopianba5972f2009-08-14 18:52:17 -0700481 mToken, mIdentity, client.get());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700482 return NO_INIT;
483 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700484 if (cblk == 0) {
485 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
486 return NO_INIT;
487 }
488 status_t err = cblk->validate(mToken);
489 if (err != NO_ERROR) {
490 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
491 mToken, mIdentity, err, strerror(-err));
492 return err;
493 }
494 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
495 LOGE("using an invalid surface id=%d, identity=%u should be %d",
496 mToken, mIdentity, cblk->layers[mToken].identity);
497 return NO_INIT;
498 }
499 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500}
501
Mathias Agopian01b76682009-04-16 20:04:08 -0700502
503bool Surface::isSameSurface(
504 const sp<Surface>& lhs, const sp<Surface>& rhs)
505{
506 if (lhs == 0 || rhs == 0)
507 return false;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700508
Mathias Agopian01b76682009-04-16 20:04:08 -0700509 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
510}
511
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700512// ----------------------------------------------------------------------------
513
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700514int Surface::setSwapInterval(android_native_window_t* window, int interval)
515{
516 return 0;
517}
518
519int Surface::dequeueBuffer(android_native_window_t* window,
520 android_native_buffer_t** buffer)
521{
522 Surface* self = getSelf(window);
523 return self->dequeueBuffer(buffer);
524}
525
526int Surface::lockBuffer(android_native_window_t* window,
527 android_native_buffer_t* buffer)
528{
529 Surface* self = getSelf(window);
530 return self->lockBuffer(buffer);
531}
532
533int Surface::queueBuffer(android_native_window_t* window,
534 android_native_buffer_t* buffer)
535{
536 Surface* self = getSelf(window);
537 return self->queueBuffer(buffer);
538}
539
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700540int Surface::query(android_native_window_t* window,
541 int what, int* value)
542{
543 Surface* self = getSelf(window);
544 return self->query(what, value);
545}
546
Mathias Agopian52212712009-08-11 22:34:02 -0700547int Surface::perform(android_native_window_t* window,
548 int operation, ...)
549{
550 va_list args;
551 va_start(args, operation);
552 Surface* self = getSelf(window);
553 int res = self->perform(operation, args);
554 va_end(args);
555 return res;
556}
557
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700558// ----------------------------------------------------------------------------
559
Mathias Agopian0926f502009-05-04 14:17:04 -0700560status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
561{
562 android_native_buffer_t* out;
563 status_t err = dequeueBuffer(&out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700564 if (err == NO_ERROR) {
565 *buffer = SurfaceBuffer::getSelf(out);
566 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700567 return err;
568}
569
570status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
571{
572 return lockBuffer(buffer.get());
573}
574
575status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
576{
577 return queueBuffer(buffer.get());
578}
579
580// ----------------------------------------------------------------------------
581
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700582int Surface::dequeueBuffer(android_native_buffer_t** buffer)
583{
584 // FIXME: dequeueBuffer() needs proper implementation
585
586 Mutex::Autolock _l(mSurfaceLock);
587
Mathias Agopianba5972f2009-08-14 18:52:17 -0700588 sp<SurfaceComposerClient> client(getClient());
589 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 status_t err = validate(cblk);
591 if (err != NO_ERROR)
592 return err;
593
594 SurfaceID index(mToken);
595
596 int32_t backIdx = cblk->lock_layer(size_t(index),
597 per_client_cblk_t::BLOCKING);
598
599 if (backIdx < 0)
600 return status_t(backIdx);
601
602 mBackbufferIndex = backIdx;
603 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 volatile const surface_info_t* const back = lcblk->surface + backIdx;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700605 if ((back->flags & surface_info_t::eNeedNewBuffer) || mUsageChanged) {
606 mUsageChanged = false;
Mathias Agopian52212712009-08-11 22:34:02 -0700607 err = getBufferLocked(backIdx, mUsage);
Mathias Agopian50517542009-08-19 17:10:18 -0700608 if (err == NO_ERROR) {
609 // reset the width/height with the what we get from the buffer
610 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
611 mWidth = uint32_t(backBuffer->width);
612 mHeight = uint32_t(backBuffer->height);
613 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 }
615
Mathias Agopiancf81c842009-07-31 14:47:00 -0700616 if (err == NO_ERROR) {
617 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
Mathias Agopian50517542009-08-19 17:10:18 -0700618 if (backBuffer != 0) {
619 mDirtyRegion.set(backBuffer->width, backBuffer->height);
620 *buffer = backBuffer.get();
621 } else {
622 err = NO_MEMORY;
623 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700624 }
Mathias Agopian50517542009-08-19 17:10:18 -0700625
Mathias Agopiancf81c842009-07-31 14:47:00 -0700626 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627}
628
629int Surface::lockBuffer(android_native_buffer_t* buffer)
630{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700631 Mutex::Autolock _l(mSurfaceLock);
632
Mathias Agopianba5972f2009-08-14 18:52:17 -0700633 sp<SurfaceComposerClient> client(getClient());
634 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700635 status_t err = validate(cblk);
636 if (err != NO_ERROR)
637 return err;
638
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 // FIXME: lockBuffer() needs proper implementation
640 return 0;
641}
642
643int Surface::queueBuffer(android_native_buffer_t* buffer)
644{
645 Mutex::Autolock _l(mSurfaceLock);
646
Mathias Agopianba5972f2009-08-14 18:52:17 -0700647 sp<SurfaceComposerClient> client(getClient());
648 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 status_t err = validate(cblk);
650 if (err != NO_ERROR)
651 return err;
652
Mathias Agopian0926f502009-05-04 14:17:04 -0700653 if (mSwapRectangle.isValid()) {
654 mDirtyRegion.set(mSwapRectangle);
655 }
656
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700657 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658 SurfaceID index(mToken);
659 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700660 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661
662 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
663 if (!(newstate & eNextFlipPending))
Mathias Agopianba5972f2009-08-14 18:52:17 -0700664 client->signalServer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700665
666 return NO_ERROR;
667}
668
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700669int Surface::query(int what, int* value)
670{
671 switch (what) {
672 case NATIVE_WINDOW_WIDTH:
673 *value = int(mWidth);
674 return NO_ERROR;
675 case NATIVE_WINDOW_HEIGHT:
676 *value = int(mHeight);
677 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700678 case NATIVE_WINDOW_FORMAT:
679 *value = int(mFormat);
680 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700681 }
682 return BAD_VALUE;
683}
684
Mathias Agopian52212712009-08-11 22:34:02 -0700685int Surface::perform(int operation, va_list args)
686{
687 int res = NO_ERROR;
688 switch (operation) {
689 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700690 setUsage( va_arg(args, int) );
Mathias Agopian52212712009-08-11 22:34:02 -0700691 break;
692 default:
693 res = NAME_NOT_FOUND;
694 break;
695 }
696 return res;
697}
698
Mathias Agopianba5972f2009-08-14 18:52:17 -0700699void Surface::setUsage(uint32_t reqUsage)
700{
701 Mutex::Autolock _l(mSurfaceLock);
702 if (mUsage != reqUsage) {
703 mUsageChanged = true;
704 mUsage = reqUsage;
705 }
706}
707
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700708// ----------------------------------------------------------------------------
709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710status_t Surface::lock(SurfaceInfo* info, bool blocking) {
711 return Surface::lock(info, NULL, blocking);
712}
713
Mathias Agopian0926f502009-05-04 14:17:04 -0700714status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700715{
Mathias Agopian52212712009-08-11 22:34:02 -0700716 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700717 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
718
Mathias Agopian0926f502009-05-04 14:17:04 -0700719 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700720 status_t err = dequeueBuffer(&backBuffer);
721 if (err == NO_ERROR) {
722 err = lockBuffer(backBuffer);
723 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700724 // we handle copy-back here...
725
726 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700727 Region scratch(bounds);
728 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700729
Mathias Agopianba5972f2009-08-14 18:52:17 -0700730 sp<SurfaceComposerClient> client(getClient());
731 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700732 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
733 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
734 if (back->flags & surface_info_t::eBufferDirty) {
735 // content is meaningless in this case and the whole surface
736 // needs to be redrawn.
737 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700738 } else {
739 newDirtyRegion.andSelf(bounds);
Mathias Agopian14998592009-07-13 18:29:59 -0700740 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
Mathias Agopian50517542009-08-19 17:10:18 -0700741 if (frontBuffer !=0 &&
742 backBuffer->width == frontBuffer->width &&
Mathias Agopian14998592009-07-13 18:29:59 -0700743 backBuffer->height == frontBuffer->height &&
744 !(lcblk->flags & eNoCopyBack))
745 {
Mathias Agopian0926f502009-05-04 14:17:04 -0700746 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
747 if (!copyback.isEmpty() && frontBuffer!=0) {
748 // copy front to back
749 copyBlt(backBuffer, frontBuffer, copyback);
750 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700751 }
752 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700753 mDirtyRegion = newDirtyRegion;
754 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755
Mathias Agopiane71212b2009-05-05 00:37:46 -0700756 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700757 status_t res = backBuffer->lock(
758 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700759 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700760
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700761 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700762 mBackbufferIndex, backBuffer->handle);
763
764 mLockedBuffer = backBuffer;
765 other->w = backBuffer->width;
766 other->h = backBuffer->height;
767 other->s = backBuffer->stride;
768 other->usage = backBuffer->usage;
769 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700770 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700771 }
772 }
773 return err;
774}
775
776status_t Surface::unlockAndPost()
777{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700778 if (mLockedBuffer == 0)
779 return BAD_VALUE;
780
Mathias Agopian0926f502009-05-04 14:17:04 -0700781 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700782 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700783 mBackbufferIndex, mLockedBuffer->handle);
784
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700785 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700786 mLockedBuffer = 0;
787 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788}
789
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790void Surface::_send_dirty_region(
791 layer_cblk_t* lcblk, const Region& dirty)
792{
793 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
794 flat_region_t* flat_region = lcblk->region + index;
795 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
796 if (err < NO_ERROR) {
797 // region doesn't fit, use the bounds
798 const Region reg(dirty.bounds());
799 reg.write(flat_region, sizeof(flat_region_t));
800 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801}
802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700804 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 mSwapRectangle = r;
806}
807
Mathias Agopian52212712009-08-11 22:34:02 -0700808status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700810 sp<ISurface> s(mSurface);
811 if (s == 0) return NO_INIT;
812
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700813 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700814
815 // free the current buffer
816 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
817 if (currentBuffer != 0) {
818 getBufferMapper().unregisterBuffer(currentBuffer->handle);
819 currentBuffer.clear();
820 }
821
Mathias Agopianba5972f2009-08-14 18:52:17 -0700822 sp<SurfaceBuffer> buffer = s->getBuffer(usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700823 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
Mathias Agopian50517542009-08-19 17:10:18 -0700824 if (buffer != 0) { // this should never happen by construction
825 if (buffer->handle != NULL) {
826 err = getBufferMapper().registerBuffer(buffer->handle);
827 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
828 err, strerror(-err));
829 if (err == NO_ERROR) {
830 currentBuffer = buffer;
831 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 }
833 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700834 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835}
836
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837}; // namespace android
838