blob: f6792c45959768732714f5ac2dd8d94b4849cf63 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopian07952722009-05-19 19:08:10 -070028#include <binder/IPCThreadState.h>
29#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <utils/Log.h>
31
Mathias Agopian1473f462009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
33#include <ui/BufferMapper.h>
The Android Open Source Project9066cfe2009-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 Agopianac2523b2009-05-05 18:11:11 -070039#include <pixelflinger/pixelflinger.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041#include <private/ui/SharedState.h>
42#include <private/ui/LayerState.h>
Mathias Agopianac2523b2009-05-05 18:11:11 -070043#include <private/ui/SurfaceBuffer.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045namespace android {
46
Mathias Agopian1473f462009-04-10 14:24:30 -070047// ============================================================================
48// SurfaceBuffer
49// ============================================================================
50
51SurfaceBuffer::SurfaceBuffer()
Mathias Agopiane633f932009-05-05 00:59:23 -070052 : BASE(), mOwner(false), mBufferMapper(BufferMapper::get())
Mathias Agopian1473f462009-04-10 14:24:30 -070053{
54 width =
55 height =
56 stride =
57 format =
58 usage = 0;
Mathias Agopiane633f932009-05-05 00:59:23 -070059 handle = NULL;
Mathias Agopian1473f462009-04-10 14:24:30 -070060}
61
62SurfaceBuffer::SurfaceBuffer(const Parcel& data)
Mathias Agopiane633f932009-05-05 00:59:23 -070063 : BASE(), mOwner(true), mBufferMapper(BufferMapper::get())
Mathias Agopian1473f462009-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 Agopian1473f462009-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 Agopian430f2ed2009-05-05 00:37:46 -070082status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
Mathias Agopiandff8e582009-05-04 14:17:04 -070083{
84 const Rect lockBounds(width, height);
Mathias Agopian430f2ed2009-05-05 00:37:46 -070085 status_t res = lock(usage, lockBounds, vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -070086 return res;
87}
88
Mathias Agopian430f2ed2009-05-05 00:37:46 -070089status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
Mathias Agopiandff8e582009-05-04 14:17:04 -070090{
Mathias Agopian8d9a5eff2009-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 Agopian430f2ed2009-05-05 00:37:46 -070098 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -070099 return res;
100}
101
102status_t SurfaceBuffer::unlock()
103{
104 status_t res = getBufferMapper().unlock(handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700105 return res;
106}
107
Mathias Agopian1473f462009-04-10 14:24:30 -0700108status_t SurfaceBuffer::writeToParcel(Parcel* reply,
109 android_native_buffer_t const* buffer)
110{
Mathias Agopian4e0df8e2009-08-04 20:53:59 -0700111 if (buffer == NULL) {
112 return BAD_VALUE;
113 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700114 reply->writeInt32(buffer->width);
115 reply->writeInt32(buffer->height);
116 reply->writeInt32(buffer->stride);
117 reply->writeInt32(buffer->format);
118 reply->writeInt32(buffer->usage);
Mathias Agopiane633f932009-05-05 00:59:23 -0700119 reply->writeNativeHandle(buffer->handle);
Mathias Agopian1473f462009-04-10 14:24:30 -0700120 return NO_ERROR;
121}
122
123// ----------------------------------------------------------------------
124
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -0700125static status_t copyBlt(
Mathias Agopiandff8e582009-05-04 14:17:04 -0700126 const sp<SurfaceBuffer>& dst,
127 const sp<SurfaceBuffer>& src,
128 const Region& reg)
Mathias Agopian1473f462009-04-10 14:24:30 -0700129{
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -0700130 status_t err;
131 uint8_t const * src_bits = NULL;
132 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
133 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopiandff8e582009-05-04 14:17:04 -0700134
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -0700135 uint8_t* dst_bits = NULL;
136 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
137 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
138
139 Region::const_iterator head(reg.begin());
140 Region::const_iterator tail(reg.end());
141 if (head != tail && src_bits && dst_bits) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700142 // NOTE: dst and src must be the same format
Mathias Agopian1473f462009-04-10 14:24:30 -0700143 const size_t bpp = bytesPerPixel(src->format);
144 const size_t dbpr = dst->stride * bpp;
145 const size_t sbpr = src->stride * bpp;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700146
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -0700147 while (head != tail) {
148 const Rect& r(*head++);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700149 ssize_t h = r.height();
150 if (h <= 0) continue;
151 size_t size = r.width() * bpp;
152 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
153 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
154 if (dbpr==sbpr && size==sbpr) {
155 size *= h;
156 h = 1;
Mathias Agopian1473f462009-04-10 14:24:30 -0700157 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700158 do {
159 memcpy(d, s, size);
160 d += dbpr;
161 s += sbpr;
162 } while (--h > 0);
Mathias Agopian1473f462009-04-10 14:24:30 -0700163 }
164 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700165
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -0700166 if (src_bits)
167 src->unlock();
168
169 if (dst_bits)
170 dst->unlock();
171
172 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700173}
174
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700175// ============================================================================
176// SurfaceControl
177// ============================================================================
178
Mathias Agopian17f638b2009-04-16 20:04:08 -0700179SurfaceControl::SurfaceControl(
180 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700181 const sp<ISurface>& surface,
182 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700183 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700184 : mClient(client), mSurface(surface),
185 mToken(data.token), mIdentity(data.identity),
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700186 mWidth(w), mHeight(h), mFormat(format), mFlags(flags)
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700187{
188}
Mathias Agopian69d62092009-04-16 20:30:22 -0700189
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700190SurfaceControl::~SurfaceControl()
191{
192 destroy();
193}
194
195void SurfaceControl::destroy()
196{
Mathias Agopian69d62092009-04-16 20:30:22 -0700197 if (isValid()) {
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700198 mClient->destroySurface(mToken);
199 }
200
201 // clear all references and trigger an IPC now, to make sure things
202 // happen without delay, since these resources are quite heavy.
203 mClient.clear();
204 mSurface.clear();
205 IPCThreadState::self()->flushCommands();
206}
207
208void SurfaceControl::clear()
209{
210 // here, the window manager tells us explicitly that we should destroy
211 // the surface's resource. Soon after this call, it will also release
212 // its last reference (which will call the dtor); however, it is possible
213 // that a client living in the same process still holds references which
214 // would delay the call to the dtor -- that is why we need this explicit
215 // "clear()" call.
216 destroy();
217}
218
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700219bool SurfaceControl::isSameSurface(
220 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
221{
222 if (lhs == 0 || rhs == 0)
223 return false;
224 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
225}
226
Mathias Agopian17f638b2009-04-16 20:04:08 -0700227status_t SurfaceControl::setLayer(int32_t layer) {
228 const sp<SurfaceComposerClient>& client(mClient);
229 if (client == 0) return NO_INIT;
230 status_t err = validate(client->mControl);
231 if (err < 0) return err;
232 return client->setLayer(mToken, layer);
233}
234status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
235 const sp<SurfaceComposerClient>& client(mClient);
236 if (client == 0) return NO_INIT;
237 status_t err = validate(client->mControl);
238 if (err < 0) return err;
239 return client->setPosition(mToken, x, y);
240}
241status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
242 const sp<SurfaceComposerClient>& client(mClient);
243 if (client == 0) return NO_INIT;
244 status_t err = validate(client->mControl);
245 if (err < 0) return err;
246 return client->setSize(mToken, w, h);
247}
248status_t SurfaceControl::hide() {
249 const sp<SurfaceComposerClient>& client(mClient);
250 if (client == 0) return NO_INIT;
251 status_t err = validate(client->mControl);
252 if (err < 0) return err;
253 return client->hide(mToken);
254}
255status_t SurfaceControl::show(int32_t layer) {
256 const sp<SurfaceComposerClient>& client(mClient);
257 if (client == 0) return NO_INIT;
258 status_t err = validate(client->mControl);
259 if (err < 0) return err;
260 return client->show(mToken, layer);
261}
262status_t SurfaceControl::freeze() {
263 const sp<SurfaceComposerClient>& client(mClient);
264 if (client == 0) return NO_INIT;
265 status_t err = validate(client->mControl);
266 if (err < 0) return err;
267 return client->freeze(mToken);
268}
269status_t SurfaceControl::unfreeze() {
270 const sp<SurfaceComposerClient>& client(mClient);
271 if (client == 0) return NO_INIT;
272 status_t err = validate(client->mControl);
273 if (err < 0) return err;
274 return client->unfreeze(mToken);
275}
276status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
277 const sp<SurfaceComposerClient>& client(mClient);
278 if (client == 0) return NO_INIT;
279 status_t err = validate(client->mControl);
280 if (err < 0) return err;
281 return client->setFlags(mToken, flags, mask);
282}
283status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
284 const sp<SurfaceComposerClient>& client(mClient);
285 if (client == 0) return NO_INIT;
286 status_t err = validate(client->mControl);
287 if (err < 0) return err;
288 return client->setTransparentRegionHint(mToken, transparent);
289}
290status_t SurfaceControl::setAlpha(float alpha) {
291 const sp<SurfaceComposerClient>& client(mClient);
292 if (client == 0) return NO_INIT;
293 status_t err = validate(client->mControl);
294 if (err < 0) return err;
295 return client->setAlpha(mToken, alpha);
296}
297status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
298 const sp<SurfaceComposerClient>& client(mClient);
299 if (client == 0) return NO_INIT;
300 status_t err = validate(client->mControl);
301 if (err < 0) return err;
302 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
303}
304status_t SurfaceControl::setFreezeTint(uint32_t tint) {
305 const sp<SurfaceComposerClient>& client(mClient);
306 if (client == 0) return NO_INIT;
307 status_t err = validate(client->mControl);
308 if (err < 0) return err;
309 return client->setFreezeTint(mToken, tint);
310}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700311
312status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const
313{
314 if (mToken<0 || mClient==0) {
315 LOGE("invalid token (%d, identity=%u) or client (%p)",
316 mToken, mIdentity, mClient.get());
317 return NO_INIT;
318 }
319 if (cblk == 0) {
320 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
321 return NO_INIT;
322 }
323 status_t err = cblk->validate(mToken);
324 if (err != NO_ERROR) {
325 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
326 mToken, mIdentity, err, strerror(-err));
327 return err;
328 }
329 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
330 LOGE("using an invalid surface id=%d, identity=%u should be %d",
331 mToken, mIdentity, cblk->layers[mToken].identity);
332 return NO_INIT;
333 }
334 return NO_ERROR;
335}
336
Mathias Agopian17f638b2009-04-16 20:04:08 -0700337status_t SurfaceControl::writeSurfaceToParcel(
338 const sp<SurfaceControl>& control, Parcel* parcel)
339{
340 uint32_t flags = 0;
341 uint32_t format = 0;
342 SurfaceID token = -1;
343 uint32_t identity = 0;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700344 uint32_t width = 0;
345 uint32_t height = 0;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700346 sp<SurfaceComposerClient> client;
347 sp<ISurface> sur;
348 if (SurfaceControl::isValid(control)) {
349 token = control->mToken;
350 identity = control->mIdentity;
351 client = control->mClient;
352 sur = control->mSurface;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700353 width = control->mWidth;
354 height = control->mHeight;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700355 format = control->mFormat;
356 flags = control->mFlags;
357 }
358 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
359 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
360 parcel->writeInt32(token);
361 parcel->writeInt32(identity);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700362 parcel->writeInt32(width);
363 parcel->writeInt32(height);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700364 parcel->writeInt32(format);
365 parcel->writeInt32(flags);
366 return NO_ERROR;
367}
368
369sp<Surface> SurfaceControl::getSurface() const
370{
371 Mutex::Autolock _l(mLock);
372 if (mSurfaceData == 0) {
373 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
374 }
375 return mSurfaceData;
376}
377
Mathias Agopian1473f462009-04-10 14:24:30 -0700378// ============================================================================
379// Surface
380// ============================================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381
Mathias Agopian17f638b2009-04-16 20:04:08 -0700382Surface::Surface(const sp<SurfaceControl>& surface)
383 : mClient(surface->mClient), mSurface(surface->mSurface),
384 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopiandff8e582009-05-04 14:17:04 -0700385 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopian321abdb2009-08-14 18:52:17 -0700386 mBufferMapper(BufferMapper::get()),
387 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388{
Mathias Agopian17f638b2009-04-16 20:04:08 -0700389 init();
390}
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700391
Mathias Agopian17f638b2009-04-16 20:04:08 -0700392Surface::Surface(const Parcel& parcel)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700393 : mBufferMapper(BufferMapper::get())
Mathias Agopian17f638b2009-04-16 20:04:08 -0700394{
395 sp<IBinder> clientBinder = parcel.readStrongBinder();
396 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
397 mToken = parcel.readInt32();
398 mIdentity = parcel.readInt32();
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700399 mWidth = parcel.readInt32();
400 mHeight = parcel.readInt32();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700401 mFormat = parcel.readInt32();
402 mFlags = parcel.readInt32();
403
404 if (clientBinder != NULL)
405 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
406
407 init();
408}
409
410void Surface::init()
411{
Mathias Agopian1473f462009-04-10 14:24:30 -0700412 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian1473f462009-04-10 14:24:30 -0700413 android_native_window_t::dequeueBuffer = dequeueBuffer;
414 android_native_window_t::lockBuffer = lockBuffer;
415 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700416 android_native_window_t::query = query;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700417 android_native_window_t::perform = perform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mSwapRectangle.makeInvalid();
Mathias Agopian1473f462009-04-10 14:24:30 -0700419 DisplayInfo dinfo;
420 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
421 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
422 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
423 // FIXME: set real values here
424 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
425 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
426 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700427 // be default we request a hardware surface
428 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700429 mUsageChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430}
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432Surface::~Surface()
433{
Mathias Agopian402c3462009-04-14 18:21:47 -0700434 // this is a client-side operation, the surface is destroyed, unmap
435 // its buffers in this process.
436 for (int i=0 ; i<2 ; i++) {
437 if (mBuffers[i] != 0) {
Mathias Agopiane633f932009-05-05 00:59:23 -0700438 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian1473f462009-04-10 14:24:30 -0700439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
Mathias Agopian402c3462009-04-14 18:21:47 -0700441
Mathias Agopian402c3462009-04-14 18:21:47 -0700442 // clear all references and trigger an IPC now, to make sure things
443 // happen without delay, since these resources are quite heavy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 mClient.clear();
445 mSurface.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 IPCThreadState::self()->flushCommands();
447}
448
Mathias Agopian321abdb2009-08-14 18:52:17 -0700449sp<SurfaceComposerClient> Surface::getClient() const {
450 return mClient;
451}
452
453sp<ISurface> Surface::getISurface() const {
454 return mSurface;
455}
456
457bool Surface::isValid() {
458 return mToken>=0 && mClient!=0;
459}
460
Mathias Agopian1473f462009-04-10 14:24:30 -0700461status_t Surface::validate(per_client_cblk_t const* cblk) const
462{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700463 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian402c3462009-04-14 18:21:47 -0700464 if (mToken<0 || mClient==0) {
465 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopian321abdb2009-08-14 18:52:17 -0700466 mToken, mIdentity, client.get());
Mathias Agopian402c3462009-04-14 18:21:47 -0700467 return NO_INIT;
468 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700469 if (cblk == 0) {
470 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
471 return NO_INIT;
472 }
473 status_t err = cblk->validate(mToken);
474 if (err != NO_ERROR) {
475 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
476 mToken, mIdentity, err, strerror(-err));
477 return err;
478 }
479 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
480 LOGE("using an invalid surface id=%d, identity=%u should be %d",
481 mToken, mIdentity, cblk->layers[mToken].identity);
482 return NO_INIT;
483 }
484 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485}
486
Mathias Agopian17f638b2009-04-16 20:04:08 -0700487
488bool Surface::isSameSurface(
489 const sp<Surface>& lhs, const sp<Surface>& rhs)
490{
491 if (lhs == 0 || rhs == 0)
492 return false;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700493
Mathias Agopian17f638b2009-04-16 20:04:08 -0700494 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
495}
496
Mathias Agopian1473f462009-04-10 14:24:30 -0700497// ----------------------------------------------------------------------------
498
Mathias Agopian1473f462009-04-10 14:24:30 -0700499int Surface::setSwapInterval(android_native_window_t* window, int interval)
500{
501 return 0;
502}
503
504int Surface::dequeueBuffer(android_native_window_t* window,
505 android_native_buffer_t** buffer)
506{
507 Surface* self = getSelf(window);
508 return self->dequeueBuffer(buffer);
509}
510
511int Surface::lockBuffer(android_native_window_t* window,
512 android_native_buffer_t* buffer)
513{
514 Surface* self = getSelf(window);
515 return self->lockBuffer(buffer);
516}
517
518int Surface::queueBuffer(android_native_window_t* window,
519 android_native_buffer_t* buffer)
520{
521 Surface* self = getSelf(window);
522 return self->queueBuffer(buffer);
523}
524
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700525int Surface::query(android_native_window_t* window,
526 int what, int* value)
527{
528 Surface* self = getSelf(window);
529 return self->query(what, value);
530}
531
Mathias Agopian5cec4742009-08-11 22:34:02 -0700532int Surface::perform(android_native_window_t* window,
533 int operation, ...)
534{
535 va_list args;
536 va_start(args, operation);
537 Surface* self = getSelf(window);
538 int res = self->perform(operation, args);
539 va_end(args);
540 return res;
541}
542
Mathias Agopian1473f462009-04-10 14:24:30 -0700543// ----------------------------------------------------------------------------
544
Mathias Agopiandff8e582009-05-04 14:17:04 -0700545status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
546{
547 android_native_buffer_t* out;
548 status_t err = dequeueBuffer(&out);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700549 if (err == NO_ERROR) {
550 *buffer = SurfaceBuffer::getSelf(out);
551 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700552 return err;
553}
554
555status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
556{
557 return lockBuffer(buffer.get());
558}
559
560status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
561{
562 return queueBuffer(buffer.get());
563}
564
565// ----------------------------------------------------------------------------
566
Mathias Agopian1473f462009-04-10 14:24:30 -0700567int Surface::dequeueBuffer(android_native_buffer_t** buffer)
568{
569 // FIXME: dequeueBuffer() needs proper implementation
570
571 Mutex::Autolock _l(mSurfaceLock);
572
Mathias Agopian321abdb2009-08-14 18:52:17 -0700573 sp<SurfaceComposerClient> client(getClient());
574 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian1473f462009-04-10 14:24:30 -0700575 status_t err = validate(cblk);
576 if (err != NO_ERROR)
577 return err;
578
579 SurfaceID index(mToken);
580
581 int32_t backIdx = cblk->lock_layer(size_t(index),
582 per_client_cblk_t::BLOCKING);
583
584 if (backIdx < 0)
585 return status_t(backIdx);
586
587 mBackbufferIndex = backIdx;
588 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian1473f462009-04-10 14:24:30 -0700589 volatile const surface_info_t* const back = lcblk->surface + backIdx;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700590 if ((back->flags & surface_info_t::eNeedNewBuffer) || mUsageChanged) {
591 mUsageChanged = false;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700592 err = getBufferLocked(backIdx, mUsage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700593 }
594
Mathias Agopianabac0102009-07-31 14:47:00 -0700595 if (err == NO_ERROR) {
596 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700597 // reset the width/height with the what we get from the buffer
598 mWidth = uint32_t(backBuffer->width);
599 mHeight = uint32_t(backBuffer->height);
Mathias Agopianabac0102009-07-31 14:47:00 -0700600 mDirtyRegion.set(backBuffer->width, backBuffer->height);
601 *buffer = backBuffer.get();
602 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700603
Mathias Agopianabac0102009-07-31 14:47:00 -0700604 return err;
Mathias Agopian1473f462009-04-10 14:24:30 -0700605}
606
607int Surface::lockBuffer(android_native_buffer_t* buffer)
608{
Mathias Agopian402c3462009-04-14 18:21:47 -0700609 Mutex::Autolock _l(mSurfaceLock);
610
Mathias Agopian321abdb2009-08-14 18:52:17 -0700611 sp<SurfaceComposerClient> client(getClient());
612 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian402c3462009-04-14 18:21:47 -0700613 status_t err = validate(cblk);
614 if (err != NO_ERROR)
615 return err;
616
Mathias Agopian1473f462009-04-10 14:24:30 -0700617 // FIXME: lockBuffer() needs proper implementation
618 return 0;
619}
620
621int Surface::queueBuffer(android_native_buffer_t* buffer)
622{
623 Mutex::Autolock _l(mSurfaceLock);
624
Mathias Agopian321abdb2009-08-14 18:52:17 -0700625 sp<SurfaceComposerClient> client(getClient());
626 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian1473f462009-04-10 14:24:30 -0700627 status_t err = validate(cblk);
628 if (err != NO_ERROR)
629 return err;
630
Mathias Agopiandff8e582009-05-04 14:17:04 -0700631 if (mSwapRectangle.isValid()) {
632 mDirtyRegion.set(mSwapRectangle);
633 }
634
Mathias Agopian1473f462009-04-10 14:24:30 -0700635 // transmit the dirty region
Mathias Agopian1473f462009-04-10 14:24:30 -0700636 SurfaceID index(mToken);
637 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700638 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian1473f462009-04-10 14:24:30 -0700639
640 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
641 if (!(newstate & eNextFlipPending))
Mathias Agopian321abdb2009-08-14 18:52:17 -0700642 client->signalServer();
Mathias Agopian1473f462009-04-10 14:24:30 -0700643
644 return NO_ERROR;
645}
646
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700647int Surface::query(int what, int* value)
648{
649 switch (what) {
650 case NATIVE_WINDOW_WIDTH:
651 *value = int(mWidth);
652 return NO_ERROR;
653 case NATIVE_WINDOW_HEIGHT:
654 *value = int(mHeight);
655 return NO_ERROR;
Mathias Agopian25ec00f2009-08-06 16:04:29 -0700656 case NATIVE_WINDOW_FORMAT:
657 *value = int(mFormat);
658 return NO_ERROR;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700659 }
660 return BAD_VALUE;
661}
662
Mathias Agopian5cec4742009-08-11 22:34:02 -0700663int Surface::perform(int operation, va_list args)
664{
665 int res = NO_ERROR;
666 switch (operation) {
667 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopian321abdb2009-08-14 18:52:17 -0700668 setUsage( va_arg(args, int) );
Mathias Agopian5cec4742009-08-11 22:34:02 -0700669 break;
670 default:
671 res = NAME_NOT_FOUND;
672 break;
673 }
674 return res;
675}
676
Mathias Agopian321abdb2009-08-14 18:52:17 -0700677void Surface::setUsage(uint32_t reqUsage)
678{
679 Mutex::Autolock _l(mSurfaceLock);
680 if (mUsage != reqUsage) {
681 mUsageChanged = true;
682 mUsage = reqUsage;
683 }
684}
685
Mathias Agopian1473f462009-04-10 14:24:30 -0700686// ----------------------------------------------------------------------------
687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688status_t Surface::lock(SurfaceInfo* info, bool blocking) {
689 return Surface::lock(info, NULL, blocking);
690}
691
Mathias Agopiandff8e582009-05-04 14:17:04 -0700692status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian1473f462009-04-10 14:24:30 -0700693{
Mathias Agopian5cec4742009-08-11 22:34:02 -0700694 // we're intending to do software rendering from this point
Mathias Agopian321abdb2009-08-14 18:52:17 -0700695 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
696
Mathias Agopiandff8e582009-05-04 14:17:04 -0700697 sp<SurfaceBuffer> backBuffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700698 status_t err = dequeueBuffer(&backBuffer);
699 if (err == NO_ERROR) {
700 err = lockBuffer(backBuffer);
701 if (err == NO_ERROR) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700702 // we handle copy-back here...
703
704 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700705 Region scratch(bounds);
706 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian1473f462009-04-10 14:24:30 -0700707
Mathias Agopian321abdb2009-08-14 18:52:17 -0700708 sp<SurfaceComposerClient> client(getClient());
709 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian1473f462009-04-10 14:24:30 -0700710 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
711 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
712 if (back->flags & surface_info_t::eBufferDirty) {
713 // content is meaningless in this case and the whole surface
714 // needs to be redrawn.
715 newDirtyRegion.set(bounds);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700716 } else {
717 newDirtyRegion.andSelf(bounds);
Mathias Agopian8d9a5eff2009-07-13 18:29:59 -0700718 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
719 if (backBuffer->width == frontBuffer->width &&
720 backBuffer->height == frontBuffer->height &&
721 !(lcblk->flags & eNoCopyBack))
722 {
Mathias Agopiandff8e582009-05-04 14:17:04 -0700723 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
724 if (!copyback.isEmpty() && frontBuffer!=0) {
725 // copy front to back
726 copyBlt(backBuffer, frontBuffer, copyback);
727 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700728 }
729 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700730 mDirtyRegion = newDirtyRegion;
731 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian1473f462009-04-10 14:24:30 -0700732
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700733 void* vaddr;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700734 status_t res = backBuffer->lock(
735 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700736 newDirtyRegion.bounds(), &vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700737
Mathias Agopian1473f462009-04-10 14:24:30 -0700738 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopiandff8e582009-05-04 14:17:04 -0700739 mBackbufferIndex, backBuffer->handle);
740
741 mLockedBuffer = backBuffer;
742 other->w = backBuffer->width;
743 other->h = backBuffer->height;
744 other->s = backBuffer->stride;
745 other->usage = backBuffer->usage;
746 other->format = backBuffer->format;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700747 other->bits = vaddr;
Mathias Agopian1473f462009-04-10 14:24:30 -0700748 }
749 }
750 return err;
751}
752
753status_t Surface::unlockAndPost()
754{
Mathias Agopian1473f462009-04-10 14:24:30 -0700755 if (mLockedBuffer == 0)
756 return BAD_VALUE;
757
Mathias Agopiandff8e582009-05-04 14:17:04 -0700758 status_t res = mLockedBuffer->unlock();
Mathias Agopian1473f462009-04-10 14:24:30 -0700759 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopiandff8e582009-05-04 14:17:04 -0700760 mBackbufferIndex, mLockedBuffer->handle);
761
Mathias Agopian1473f462009-04-10 14:24:30 -0700762 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700763 mLockedBuffer = 0;
764 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765}
766
Mathias Agopian1473f462009-04-10 14:24:30 -0700767void Surface::_send_dirty_region(
768 layer_cblk_t* lcblk, const Region& dirty)
769{
770 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
771 flat_region_t* flat_region = lcblk->region + index;
772 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
773 if (err < NO_ERROR) {
774 // region doesn't fit, use the bounds
775 const Region reg(dirty.bounds());
776 reg.write(flat_region, sizeof(flat_region_t));
777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778}
779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopian321abdb2009-08-14 18:52:17 -0700781 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 mSwapRectangle = r;
783}
784
Mathias Agopian5cec4742009-08-11 22:34:02 -0700785status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786{
Mathias Agopian321abdb2009-08-14 18:52:17 -0700787 sp<ISurface> s(mSurface);
788 if (s == 0) return NO_INIT;
789
Mathias Agopian1473f462009-04-10 14:24:30 -0700790 status_t err = NO_MEMORY;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700791 sp<SurfaceBuffer> buffer = s->getBuffer(usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700792 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
793 if (buffer != 0) {
794 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
795 if (currentBuffer != 0) {
Mathias Agopiane633f932009-05-05 00:59:23 -0700796 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian1473f462009-04-10 14:24:30 -0700797 currentBuffer.clear();
798 }
Mathias Agopiane633f932009-05-05 00:59:23 -0700799 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopianabac0102009-07-31 14:47:00 -0700800 LOGW_IF(err, "registerBuffer(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700801 if (err == NO_ERROR) {
802 currentBuffer = buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700805 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806}
807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808}; // namespace android
809