blob: 2b6905f33fc253fa14904ee0c9f56532f165cdf3 [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;
Mathias Agopian52212712009-08-11 22:34:02 -0700417 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-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 Agopian52212712009-08-11 22:34:02 -0700427 // be default we request a hardware surface
428 mUsage = GRALLOC_USAGE_HW_RENDER;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429}
430
Mathias Agopian01b76682009-04-16 20:04:08 -0700431
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432Surface::~Surface()
433{
Mathias Agopian40b7f6e2009-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 Agopian21c59d02009-05-05 00:59:23 -0700438 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700439 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700441
Mathias Agopian40b7f6e2009-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 Projectedbf3b62009-03-03 19:31:44 -0800444 mClient.clear();
445 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 IPCThreadState::self()->flushCommands();
447}
448
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700449status_t Surface::validate(per_client_cblk_t const* cblk) const
450{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700451 if (mToken<0 || mClient==0) {
452 LOGE("invalid token (%d, identity=%u) or client (%p)",
453 mToken, mIdentity, mClient.get());
454 return NO_INIT;
455 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700456 if (cblk == 0) {
457 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
458 return NO_INIT;
459 }
460 status_t err = cblk->validate(mToken);
461 if (err != NO_ERROR) {
462 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
463 mToken, mIdentity, err, strerror(-err));
464 return err;
465 }
466 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
467 LOGE("using an invalid surface id=%d, identity=%u should be %d",
468 mToken, mIdentity, cblk->layers[mToken].identity);
469 return NO_INIT;
470 }
471 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
Mathias Agopian01b76682009-04-16 20:04:08 -0700474
475bool Surface::isSameSurface(
476 const sp<Surface>& lhs, const sp<Surface>& rhs)
477{
478 if (lhs == 0 || rhs == 0)
479 return false;
480 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
481}
482
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700483// ----------------------------------------------------------------------------
484
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700485int Surface::setSwapInterval(android_native_window_t* window, int interval)
486{
487 return 0;
488}
489
490int Surface::dequeueBuffer(android_native_window_t* window,
491 android_native_buffer_t** buffer)
492{
493 Surface* self = getSelf(window);
494 return self->dequeueBuffer(buffer);
495}
496
497int Surface::lockBuffer(android_native_window_t* window,
498 android_native_buffer_t* buffer)
499{
500 Surface* self = getSelf(window);
501 return self->lockBuffer(buffer);
502}
503
504int Surface::queueBuffer(android_native_window_t* window,
505 android_native_buffer_t* buffer)
506{
507 Surface* self = getSelf(window);
508 return self->queueBuffer(buffer);
509}
510
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700511int Surface::query(android_native_window_t* window,
512 int what, int* value)
513{
514 Surface* self = getSelf(window);
515 return self->query(what, value);
516}
517
Mathias Agopian52212712009-08-11 22:34:02 -0700518int Surface::perform(android_native_window_t* window,
519 int operation, ...)
520{
521 va_list args;
522 va_start(args, operation);
523 Surface* self = getSelf(window);
524 int res = self->perform(operation, args);
525 va_end(args);
526 return res;
527}
528
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700529// ----------------------------------------------------------------------------
530
Mathias Agopian0926f502009-05-04 14:17:04 -0700531status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
532{
533 android_native_buffer_t* out;
534 status_t err = dequeueBuffer(&out);
535 *buffer = SurfaceBuffer::getSelf(out);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700536 // reset the width/height with the what we get from the buffer
537 mWidth = uint32_t(out->width);
538 mHeight = uint32_t(out->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700539 return err;
540}
541
542status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
543{
544 return lockBuffer(buffer.get());
545}
546
547status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
548{
549 return queueBuffer(buffer.get());
550}
551
552// ----------------------------------------------------------------------------
553
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554int Surface::dequeueBuffer(android_native_buffer_t** buffer)
555{
556 // FIXME: dequeueBuffer() needs proper implementation
557
558 Mutex::Autolock _l(mSurfaceLock);
559
560 per_client_cblk_t* const cblk = mClient->mControl;
561 status_t err = validate(cblk);
562 if (err != NO_ERROR)
563 return err;
564
565 SurfaceID index(mToken);
566
567 int32_t backIdx = cblk->lock_layer(size_t(index),
568 per_client_cblk_t::BLOCKING);
569
570 if (backIdx < 0)
571 return status_t(backIdx);
572
573 mBackbufferIndex = backIdx;
574 layer_cblk_t* const lcblk = &(cblk->layers[index]);
575
576 volatile const surface_info_t* const back = lcblk->surface + backIdx;
577 if (back->flags & surface_info_t::eNeedNewBuffer) {
Mathias Agopian52212712009-08-11 22:34:02 -0700578 err = getBufferLocked(backIdx, mUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 }
580
Mathias Agopiancf81c842009-07-31 14:47:00 -0700581 if (err == NO_ERROR) {
582 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
583 mDirtyRegion.set(backBuffer->width, backBuffer->height);
584 *buffer = backBuffer.get();
585 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700586
Mathias Agopiancf81c842009-07-31 14:47:00 -0700587 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588}
589
590int Surface::lockBuffer(android_native_buffer_t* buffer)
591{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700592 Mutex::Autolock _l(mSurfaceLock);
593
594 per_client_cblk_t* const cblk = mClient->mControl;
595 status_t err = validate(cblk);
596 if (err != NO_ERROR)
597 return err;
598
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 // FIXME: lockBuffer() needs proper implementation
600 return 0;
601}
602
603int Surface::queueBuffer(android_native_buffer_t* buffer)
604{
605 Mutex::Autolock _l(mSurfaceLock);
606
607 per_client_cblk_t* const cblk = mClient->mControl;
608 status_t err = validate(cblk);
609 if (err != NO_ERROR)
610 return err;
611
Mathias Agopian0926f502009-05-04 14:17:04 -0700612 if (mSwapRectangle.isValid()) {
613 mDirtyRegion.set(mSwapRectangle);
614 }
615
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700616 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700617 SurfaceID index(mToken);
618 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700619 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620
621 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
622 if (!(newstate & eNextFlipPending))
623 mClient->signalServer();
624
625 return NO_ERROR;
626}
627
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700628int Surface::query(int what, int* value)
629{
630 switch (what) {
631 case NATIVE_WINDOW_WIDTH:
632 *value = int(mWidth);
633 return NO_ERROR;
634 case NATIVE_WINDOW_HEIGHT:
635 *value = int(mHeight);
636 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700637 case NATIVE_WINDOW_FORMAT:
638 *value = int(mFormat);
639 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700640 }
641 return BAD_VALUE;
642}
643
Mathias Agopian52212712009-08-11 22:34:02 -0700644int Surface::perform(int operation, va_list args)
645{
646 int res = NO_ERROR;
647 switch (operation) {
648 case NATIVE_WINDOW_SET_USAGE:
649 mUsage = va_arg(args, int);
650 break;
651 default:
652 res = NAME_NOT_FOUND;
653 break;
654 }
655 return res;
656}
657
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658// ----------------------------------------------------------------------------
659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660status_t Surface::lock(SurfaceInfo* info, bool blocking) {
661 return Surface::lock(info, NULL, blocking);
662}
663
Mathias Agopian0926f502009-05-04 14:17:04 -0700664status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700665{
666 // FIXME: needs some locking here
Mathias Agopian52212712009-08-11 22:34:02 -0700667
668 // we're intending to do software rendering from this point
669 mUsage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
Mathias Agopian0926f502009-05-04 14:17:04 -0700670
671 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700672 status_t err = dequeueBuffer(&backBuffer);
673 if (err == NO_ERROR) {
674 err = lockBuffer(backBuffer);
675 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700676 // we handle copy-back here...
677
678 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700679 Region scratch(bounds);
680 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700681
682 per_client_cblk_t* const cblk = mClient->mControl;
683 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
684 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
685 if (back->flags & surface_info_t::eBufferDirty) {
686 // content is meaningless in this case and the whole surface
687 // needs to be redrawn.
688 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700689 } else {
690 newDirtyRegion.andSelf(bounds);
Mathias Agopian14998592009-07-13 18:29:59 -0700691 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
692 if (backBuffer->width == frontBuffer->width &&
693 backBuffer->height == frontBuffer->height &&
694 !(lcblk->flags & eNoCopyBack))
695 {
Mathias Agopian0926f502009-05-04 14:17:04 -0700696 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
697 if (!copyback.isEmpty() && frontBuffer!=0) {
698 // copy front to back
699 copyBlt(backBuffer, frontBuffer, copyback);
700 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700701 }
702 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700703 mDirtyRegion = newDirtyRegion;
704 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700705
Mathias Agopiane71212b2009-05-05 00:37:46 -0700706 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700707 status_t res = backBuffer->lock(
708 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700709 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700710
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700711 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700712 mBackbufferIndex, backBuffer->handle);
713
714 mLockedBuffer = backBuffer;
715 other->w = backBuffer->width;
716 other->h = backBuffer->height;
717 other->s = backBuffer->stride;
718 other->usage = backBuffer->usage;
719 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700720 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700721 }
722 }
723 return err;
724}
725
726status_t Surface::unlockAndPost()
727{
728 // FIXME: needs some locking here
729
730 if (mLockedBuffer == 0)
731 return BAD_VALUE;
732
Mathias Agopian0926f502009-05-04 14:17:04 -0700733 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700734 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700735 mBackbufferIndex, mLockedBuffer->handle);
736
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700737 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700738 mLockedBuffer = 0;
739 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742void Surface::_send_dirty_region(
743 layer_cblk_t* lcblk, const Region& dirty)
744{
745 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
746 flat_region_t* flat_region = lcblk->region + index;
747 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
748 if (err < NO_ERROR) {
749 // region doesn't fit, use the bounds
750 const Region reg(dirty.bounds());
751 reg.write(flat_region, sizeof(flat_region_t));
752 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755void Surface::setSwapRectangle(const Rect& r) {
756 mSwapRectangle = r;
757}
758
Mathias Agopian52212712009-08-11 22:34:02 -0700759status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700761 status_t err = NO_MEMORY;
Mathias Agopian52212712009-08-11 22:34:02 -0700762 sp<SurfaceBuffer> buffer = mSurface->getBuffer(usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700763 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
764 if (buffer != 0) {
765 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
766 if (currentBuffer != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700767 getBufferMapper().unregisterBuffer(currentBuffer->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700768 currentBuffer.clear();
769 }
Mathias Agopian21c59d02009-05-05 00:59:23 -0700770 err = getBufferMapper().registerBuffer(buffer->handle);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700771 LOGW_IF(err, "registerBuffer(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700772 if (err == NO_ERROR) {
773 currentBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 }
775 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700776 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777}
778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779}; // namespace android
780