blob: 4abb7f62ad443ec9710e3740dc733d909febf435 [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 Agopian3eded942009-08-04 20:53:59 -0700111 if (buffer == NULL) {
112 return BAD_VALUE;
113 }
Mathias Agopian076b1cc2009-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 Agopian21c59d02009-05-05 00:59:23 -0700119 reply->writeNativeHandle(buffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 return NO_ERROR;
121}
122
123// ----------------------------------------------------------------------
124
Mathias Agopian14998592009-07-13 18:29:59 -0700125static status_t copyBlt(
Mathias Agopian0926f502009-05-04 14:17:04 -0700126 const sp<SurfaceBuffer>& dst,
127 const sp<SurfaceBuffer>& src,
128 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129{
Mathias Agopian14998592009-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 Agopian0926f502009-05-04 14:17:04 -0700134
Mathias Agopian14998592009-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 Agopian076b1cc2009-04-10 14:24:30 -0700142 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-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 Agopian0926f502009-05-04 14:17:04 -0700146
Mathias Agopian14998592009-07-13 18:29:59 -0700147 while (head != tail) {
148 const Rect& r(*head++);
Mathias Agopian0926f502009-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 Agopian076b1cc2009-04-10 14:24:30 -0700157 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700158 do {
159 memcpy(d, s, size);
160 d += dbpr;
161 s += sbpr;
162 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700163 }
164 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700165
Mathias Agopian14998592009-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 Agopian076b1cc2009-04-10 14:24:30 -0700173}
174
Mathias Agopian62185b72009-04-16 16:19:50 -0700175// ============================================================================
176// SurfaceControl
177// ============================================================================
178
Mathias Agopian01b76682009-04-16 20:04:08 -0700179SurfaceControl::SurfaceControl(
180 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700181 const sp<ISurface>& surface,
182 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700183 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700184 : mClient(client), mSurface(surface),
185 mToken(data.token), mIdentity(data.identity),
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700186 mWidth(w), mHeight(h), mFormat(format), mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700187{
188}
Mathias Agopian18d84462009-04-16 20:30:22 -0700189
Mathias Agopian62185b72009-04-16 16:19:50 -0700190SurfaceControl::~SurfaceControl()
191{
192 destroy();
193}
194
195void SurfaceControl::destroy()
196{
Mathias Agopian18d84462009-04-16 20:30:22 -0700197 if (isValid()) {
Mathias Agopian62185b72009-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 Agopian62185b72009-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 Agopian01b76682009-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 Agopian62185b72009-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 Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700344 uint32_t width = 0;
345 uint32_t height = 0;
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700353 width = control->mWidth;
354 height = control->mHeight;
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700362 parcel->writeInt32(width);
363 parcel->writeInt32(height);
Mathias Agopian01b76682009-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 Agopian076b1cc2009-04-10 14:24:30 -0700378// ============================================================================
379// Surface
380// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700385 mWidth(surface->mWidth), mHeight(surface->mHeight),
Mathias Agopian0926f502009-05-04 14:17:04 -0700386 mFormat(surface->mFormat), mFlags(surface->mFlags),
387 mBufferMapper(BufferMapper::get())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388{
Mathias Agopian01b76682009-04-16 20:04:08 -0700389 init();
390}
Mathias Agopian62185b72009-04-16 16:19:50 -0700391
Mathias Agopian01b76682009-04-16 20:04:08 -0700392Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700393 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-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 Agopiancb6b9042009-07-30 18:14:56 -0700399 mWidth = parcel.readInt32();
400 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-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 Agopian076b1cc2009-04-10 14:24:30 -0700412 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-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 Agopiancb6b9042009-07-30 18:14:56 -0700416 android_native_window_t::query = query;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700418 DisplayInfo dinfo;
419 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
420 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
421 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
422 // FIXME: set real values here
423 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
424 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
425 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426}
427
Mathias Agopian01b76682009-04-16 20:04:08 -0700428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429Surface::~Surface()
430{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700431 // this is a client-side operation, the surface is destroyed, unmap
432 // its buffers in this process.
433 for (int i=0 ; i<2 ; i++) {
434 if (mBuffers[i] != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700435 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700438
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700439 // clear all references and trigger an IPC now, to make sure things
440 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 mClient.clear();
442 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 IPCThreadState::self()->flushCommands();
444}
445
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700446status_t Surface::validate(per_client_cblk_t const* cblk) const
447{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700448 if (mToken<0 || mClient==0) {
449 LOGE("invalid token (%d, identity=%u) or client (%p)",
450 mToken, mIdentity, mClient.get());
451 return NO_INIT;
452 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700453 if (cblk == 0) {
454 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
455 return NO_INIT;
456 }
457 status_t err = cblk->validate(mToken);
458 if (err != NO_ERROR) {
459 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
460 mToken, mIdentity, err, strerror(-err));
461 return err;
462 }
463 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
464 LOGE("using an invalid surface id=%d, identity=%u should be %d",
465 mToken, mIdentity, cblk->layers[mToken].identity);
466 return NO_INIT;
467 }
468 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469}
470
Mathias Agopian01b76682009-04-16 20:04:08 -0700471
472bool Surface::isSameSurface(
473 const sp<Surface>& lhs, const sp<Surface>& rhs)
474{
475 if (lhs == 0 || rhs == 0)
476 return false;
477 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
478}
479
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700480// ----------------------------------------------------------------------------
481
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700482int Surface::setSwapInterval(android_native_window_t* window, int interval)
483{
484 return 0;
485}
486
487int Surface::dequeueBuffer(android_native_window_t* window,
488 android_native_buffer_t** buffer)
489{
490 Surface* self = getSelf(window);
491 return self->dequeueBuffer(buffer);
492}
493
494int Surface::lockBuffer(android_native_window_t* window,
495 android_native_buffer_t* buffer)
496{
497 Surface* self = getSelf(window);
498 return self->lockBuffer(buffer);
499}
500
501int Surface::queueBuffer(android_native_window_t* window,
502 android_native_buffer_t* buffer)
503{
504 Surface* self = getSelf(window);
505 return self->queueBuffer(buffer);
506}
507
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700508int Surface::query(android_native_window_t* window,
509 int what, int* value)
510{
511 Surface* self = getSelf(window);
512 return self->query(what, value);
513}
514
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515// ----------------------------------------------------------------------------
516
Mathias Agopian0926f502009-05-04 14:17:04 -0700517status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
518{
519 android_native_buffer_t* out;
520 status_t err = dequeueBuffer(&out);
521 *buffer = SurfaceBuffer::getSelf(out);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700522 // reset the width/height with the what we get from the buffer
523 mWidth = uint32_t(out->width);
524 mHeight = uint32_t(out->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700525 return err;
526}
527
528status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
529{
530 return lockBuffer(buffer.get());
531}
532
533status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
534{
535 return queueBuffer(buffer.get());
536}
537
538// ----------------------------------------------------------------------------
539
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540int Surface::dequeueBuffer(android_native_buffer_t** buffer)
541{
542 // FIXME: dequeueBuffer() needs proper implementation
543
544 Mutex::Autolock _l(mSurfaceLock);
545
546 per_client_cblk_t* const cblk = mClient->mControl;
547 status_t err = validate(cblk);
548 if (err != NO_ERROR)
549 return err;
550
551 SurfaceID index(mToken);
552
553 int32_t backIdx = cblk->lock_layer(size_t(index),
554 per_client_cblk_t::BLOCKING);
555
556 if (backIdx < 0)
557 return status_t(backIdx);
558
559 mBackbufferIndex = backIdx;
560 layer_cblk_t* const lcblk = &(cblk->layers[index]);
561
562 volatile const surface_info_t* const back = lcblk->surface + backIdx;
563 if (back->flags & surface_info_t::eNeedNewBuffer) {
Fred Quintanab2fd4662009-08-11 20:49:35 -0700564 err = getBufferLocked(backIdx);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 }
566
Mathias Agopiancf81c842009-07-31 14:47:00 -0700567 if (err == NO_ERROR) {
568 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
569 mDirtyRegion.set(backBuffer->width, backBuffer->height);
570 *buffer = backBuffer.get();
571 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700572
Mathias Agopiancf81c842009-07-31 14:47:00 -0700573 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574}
575
576int Surface::lockBuffer(android_native_buffer_t* buffer)
577{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700578 Mutex::Autolock _l(mSurfaceLock);
579
580 per_client_cblk_t* const cblk = mClient->mControl;
581 status_t err = validate(cblk);
582 if (err != NO_ERROR)
583 return err;
584
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 // FIXME: lockBuffer() needs proper implementation
586 return 0;
587}
588
589int Surface::queueBuffer(android_native_buffer_t* buffer)
590{
591 Mutex::Autolock _l(mSurfaceLock);
592
593 per_client_cblk_t* const cblk = mClient->mControl;
594 status_t err = validate(cblk);
595 if (err != NO_ERROR)
596 return err;
597
Mathias Agopian0926f502009-05-04 14:17:04 -0700598 if (mSwapRectangle.isValid()) {
599 mDirtyRegion.set(mSwapRectangle);
600 }
601
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700602 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 SurfaceID index(mToken);
604 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700605 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606
607 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
608 if (!(newstate & eNextFlipPending))
609 mClient->signalServer();
610
611 return NO_ERROR;
612}
613
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700614int Surface::query(int what, int* value)
615{
616 switch (what) {
617 case NATIVE_WINDOW_WIDTH:
618 *value = int(mWidth);
619 return NO_ERROR;
620 case NATIVE_WINDOW_HEIGHT:
621 *value = int(mHeight);
622 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700623 case NATIVE_WINDOW_FORMAT:
624 *value = int(mFormat);
625 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700626 }
627 return BAD_VALUE;
628}
629
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700630// ----------------------------------------------------------------------------
631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632status_t Surface::lock(SurfaceInfo* info, bool blocking) {
633 return Surface::lock(info, NULL, blocking);
634}
635
Mathias Agopian0926f502009-05-04 14:17:04 -0700636status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700637{
638 // FIXME: needs some locking here
Mathias Agopian0926f502009-05-04 14:17:04 -0700639
640 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641 status_t err = dequeueBuffer(&backBuffer);
642 if (err == NO_ERROR) {
643 err = lockBuffer(backBuffer);
644 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 // we handle copy-back here...
646
647 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700648 Region scratch(bounds);
649 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700650
651 per_client_cblk_t* const cblk = mClient->mControl;
652 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
653 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
654 if (back->flags & surface_info_t::eBufferDirty) {
655 // content is meaningless in this case and the whole surface
656 // needs to be redrawn.
657 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700658 } else {
659 newDirtyRegion.andSelf(bounds);
Mathias Agopian14998592009-07-13 18:29:59 -0700660 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
661 if (backBuffer->width == frontBuffer->width &&
662 backBuffer->height == frontBuffer->height &&
663 !(lcblk->flags & eNoCopyBack))
664 {
Mathias Agopian0926f502009-05-04 14:17:04 -0700665 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
666 if (!copyback.isEmpty() && frontBuffer!=0) {
667 // copy front to back
668 copyBlt(backBuffer, frontBuffer, copyback);
669 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670 }
671 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700672 mDirtyRegion = newDirtyRegion;
673 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700674
Mathias Agopiane71212b2009-05-05 00:37:46 -0700675 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700676 status_t res = backBuffer->lock(
677 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700678 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700679
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700680 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700681 mBackbufferIndex, backBuffer->handle);
682
683 mLockedBuffer = backBuffer;
684 other->w = backBuffer->width;
685 other->h = backBuffer->height;
686 other->s = backBuffer->stride;
687 other->usage = backBuffer->usage;
688 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700689 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700690 }
691 }
692 return err;
693}
694
695status_t Surface::unlockAndPost()
696{
697 // FIXME: needs some locking here
698
699 if (mLockedBuffer == 0)
700 return BAD_VALUE;
701
Mathias Agopian0926f502009-05-04 14:17:04 -0700702 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700703 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700704 mBackbufferIndex, mLockedBuffer->handle);
705
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700706 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700707 mLockedBuffer = 0;
708 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709}
710
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700711void Surface::_send_dirty_region(
712 layer_cblk_t* lcblk, const Region& dirty)
713{
714 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
715 flat_region_t* flat_region = lcblk->region + index;
716 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
717 if (err < NO_ERROR) {
718 // region doesn't fit, use the bounds
719 const Region reg(dirty.bounds());
720 reg.write(flat_region, sizeof(flat_region_t));
721 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722}
723
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724void Surface::setSwapRectangle(const Rect& r) {
725 mSwapRectangle = r;
726}
727
Fred Quintanab2fd4662009-08-11 20:49:35 -0700728status_t Surface::getBufferLocked(int index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700730 status_t err = NO_MEMORY;
Fred Quintanab2fd4662009-08-11 20:49:35 -0700731 sp<SurfaceBuffer> buffer = mSurface->getBuffer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700732 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
733 if (buffer != 0) {
734 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
735 if (currentBuffer != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700736 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700737 currentBuffer.clear();
738 }
Mathias Agopian21c59d02009-05-05 00:59:23 -0700739 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700740 LOGW_IF(err, "registerBuffer(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700741 if (err == NO_ERROR) {
742 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 }
744 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700745 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746}
747
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748}; // namespace android
749