blob: 0ba0a6574181967207391af188e108deb7222919 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Surface"
18
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/threads.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/IPCThreadState.h>
29#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <utils/Log.h>
31
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
33#include <ui/BufferMapper.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <ui/ISurface.h>
35#include <ui/Surface.h>
36#include <ui/SurfaceComposerClient.h>
37#include <ui/Rect.h>
38
Mathias Agopian7189c002009-05-05 18:11:11 -070039#include <pixelflinger/pixelflinger.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <private/ui/SharedState.h>
42#include <private/ui/LayerState.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070043#include <private/ui/SurfaceBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047// ============================================================================
48// SurfaceBuffer
49// ============================================================================
50
51SurfaceBuffer::SurfaceBuffer()
Mathias Agopian21c59d02009-05-05 00:59:23 -070052 : BASE(), mOwner(false), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053{
54 width =
55 height =
56 stride =
57 format =
58 usage = 0;
Mathias Agopian21c59d02009-05-05 00:59:23 -070059 handle = NULL;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060}
61
62SurfaceBuffer::SurfaceBuffer(const Parcel& data)
Mathias Agopian21c59d02009-05-05 00:59:23 -070063 : BASE(), mOwner(true), mBufferMapper(BufferMapper::get())
Mathias Agopian076b1cc2009-04-10 14:24:30 -070064{
65 // we own the handle in this case
66 width = data.readInt32();
Mathias Agopian50517542009-08-19 17:10:18 -070067 if (width < 0) {
68 width = height = stride = format = usage = 0;
69 handle = 0;
70 } else {
71 height = data.readInt32();
72 stride = data.readInt32();
73 format = data.readInt32();
74 usage = data.readInt32();
75 handle = data.readNativeHandle();
76 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077}
78
79SurfaceBuffer::~SurfaceBuffer()
80{
81 if (handle && mOwner) {
82 native_handle_close(handle);
83 native_handle_delete(const_cast<native_handle*>(handle));
84 }
85}
86
Mathias Agopiane71212b2009-05-05 00:37:46 -070087status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070088{
89 const Rect lockBounds(width, height);
Mathias Agopiane71212b2009-05-05 00:37:46 -070090 status_t res = lock(usage, lockBounds, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070091 return res;
92}
93
Mathias Agopiane71212b2009-05-05 00:37:46 -070094status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -070095{
Mathias Agopian14998592009-07-13 18:29:59 -070096 if (rect.left < 0 || rect.right > this->width ||
97 rect.top < 0 || rect.bottom > this->height) {
98 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
99 rect.left, rect.top, rect.right, rect.bottom,
100 this->width, this->height);
101 return BAD_VALUE;
102 }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700103 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700104 return res;
105}
106
107status_t SurfaceBuffer::unlock()
108{
109 status_t res = getBufferMapper().unlock(handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700110 return res;
111}
112
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113status_t SurfaceBuffer::writeToParcel(Parcel* reply,
114 android_native_buffer_t const* buffer)
115{
Mathias Agopian50517542009-08-19 17:10:18 -0700116 if (buffer == NULL)
Mathias Agopian3eded942009-08-04 20:53:59 -0700117 return BAD_VALUE;
Mathias Agopian50517542009-08-19 17:10:18 -0700118
119 if (buffer->width < 0 || buffer->height < 0)
120 return BAD_VALUE;
121
122 status_t err = NO_ERROR;
123 if (buffer->handle == NULL) {
124 // this buffer doesn't have a handle
125 reply->writeInt32(NO_MEMORY);
126 } else {
127 reply->writeInt32(buffer->width);
128 reply->writeInt32(buffer->height);
129 reply->writeInt32(buffer->stride);
130 reply->writeInt32(buffer->format);
131 reply->writeInt32(buffer->usage);
132 err = reply->writeNativeHandle(buffer->handle);
Mathias Agopian3eded942009-08-04 20:53:59 -0700133 }
Mathias Agopian50517542009-08-19 17:10:18 -0700134 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135}
136
137// ----------------------------------------------------------------------
138
Mathias Agopian14998592009-07-13 18:29:59 -0700139static status_t copyBlt(
Mathias Agopian0926f502009-05-04 14:17:04 -0700140 const sp<SurfaceBuffer>& dst,
141 const sp<SurfaceBuffer>& src,
142 const Region& reg)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143{
Mathias Agopian14998592009-07-13 18:29:59 -0700144 status_t err;
145 uint8_t const * src_bits = NULL;
146 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
147 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian0926f502009-05-04 14:17:04 -0700148
Mathias Agopian14998592009-07-13 18:29:59 -0700149 uint8_t* dst_bits = NULL;
150 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
151 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
152
153 Region::const_iterator head(reg.begin());
154 Region::const_iterator tail(reg.end());
155 if (head != tail && src_bits && dst_bits) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700156 // NOTE: dst and src must be the same format
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157 const size_t bpp = bytesPerPixel(src->format);
158 const size_t dbpr = dst->stride * bpp;
159 const size_t sbpr = src->stride * bpp;
Mathias Agopian0926f502009-05-04 14:17:04 -0700160
Mathias Agopian14998592009-07-13 18:29:59 -0700161 while (head != tail) {
162 const Rect& r(*head++);
Mathias Agopian0926f502009-05-04 14:17:04 -0700163 ssize_t h = r.height();
164 if (h <= 0) continue;
165 size_t size = r.width() * bpp;
166 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
167 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
168 if (dbpr==sbpr && size==sbpr) {
169 size *= h;
170 h = 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700172 do {
173 memcpy(d, s, size);
174 d += dbpr;
175 s += sbpr;
176 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 }
178 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700179
Mathias Agopian14998592009-07-13 18:29:59 -0700180 if (src_bits)
181 src->unlock();
182
183 if (dst_bits)
184 dst->unlock();
185
186 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187}
188
Mathias Agopian62185b72009-04-16 16:19:50 -0700189// ============================================================================
190// SurfaceControl
191// ============================================================================
192
Mathias Agopian01b76682009-04-16 20:04:08 -0700193SurfaceControl::SurfaceControl(
194 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700195 const sp<ISurface>& surface,
196 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700197 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700198 : mClient(client), mSurface(surface),
199 mToken(data.token), mIdentity(data.identity),
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700200 mWidth(w), mHeight(h), mFormat(format), mFlags(flags)
Mathias Agopian62185b72009-04-16 16:19:50 -0700201{
202}
Mathias Agopian18d84462009-04-16 20:30:22 -0700203
Mathias Agopian62185b72009-04-16 16:19:50 -0700204SurfaceControl::~SurfaceControl()
205{
206 destroy();
207}
208
209void SurfaceControl::destroy()
210{
Mathias Agopian18d84462009-04-16 20:30:22 -0700211 if (isValid()) {
Mathias Agopian62185b72009-04-16 16:19:50 -0700212 mClient->destroySurface(mToken);
213 }
214
215 // clear all references and trigger an IPC now, to make sure things
216 // happen without delay, since these resources are quite heavy.
217 mClient.clear();
218 mSurface.clear();
219 IPCThreadState::self()->flushCommands();
220}
221
222void SurfaceControl::clear()
223{
224 // here, the window manager tells us explicitly that we should destroy
225 // the surface's resource. Soon after this call, it will also release
226 // its last reference (which will call the dtor); however, it is possible
227 // that a client living in the same process still holds references which
228 // would delay the call to the dtor -- that is why we need this explicit
229 // "clear()" call.
230 destroy();
231}
232
Mathias Agopian62185b72009-04-16 16:19:50 -0700233bool SurfaceControl::isSameSurface(
234 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
235{
236 if (lhs == 0 || rhs == 0)
237 return false;
238 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
239}
240
Mathias Agopian01b76682009-04-16 20:04:08 -0700241status_t SurfaceControl::setLayer(int32_t layer) {
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->setLayer(mToken, layer);
247}
248status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
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->setPosition(mToken, x, y);
254}
255status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
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->setSize(mToken, w, h);
261}
262status_t SurfaceControl::hide() {
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->hide(mToken);
268}
269status_t SurfaceControl::show(int32_t layer) {
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->show(mToken, layer);
275}
276status_t SurfaceControl::freeze() {
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->freeze(mToken);
282}
283status_t SurfaceControl::unfreeze() {
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->unfreeze(mToken);
289}
290status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
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->setFlags(mToken, flags, mask);
296}
297status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
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->setTransparentRegionHint(mToken, transparent);
303}
304status_t SurfaceControl::setAlpha(float alpha) {
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->setAlpha(mToken, alpha);
310}
311status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
312 const sp<SurfaceComposerClient>& client(mClient);
313 if (client == 0) return NO_INIT;
314 status_t err = validate(client->mControl);
315 if (err < 0) return err;
316 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
317}
318status_t SurfaceControl::setFreezeTint(uint32_t tint) {
319 const sp<SurfaceComposerClient>& client(mClient);
320 if (client == 0) return NO_INIT;
321 status_t err = validate(client->mControl);
322 if (err < 0) return err;
323 return client->setFreezeTint(mToken, tint);
324}
Mathias Agopian62185b72009-04-16 16:19:50 -0700325
326status_t SurfaceControl::validate(per_client_cblk_t const* cblk) const
327{
328 if (mToken<0 || mClient==0) {
329 LOGE("invalid token (%d, identity=%u) or client (%p)",
330 mToken, mIdentity, mClient.get());
331 return NO_INIT;
332 }
333 if (cblk == 0) {
334 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
335 return NO_INIT;
336 }
337 status_t err = cblk->validate(mToken);
338 if (err != NO_ERROR) {
339 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
340 mToken, mIdentity, err, strerror(-err));
341 return err;
342 }
343 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
344 LOGE("using an invalid surface id=%d, identity=%u should be %d",
345 mToken, mIdentity, cblk->layers[mToken].identity);
346 return NO_INIT;
347 }
348 return NO_ERROR;
349}
350
Mathias Agopian01b76682009-04-16 20:04:08 -0700351status_t SurfaceControl::writeSurfaceToParcel(
352 const sp<SurfaceControl>& control, Parcel* parcel)
353{
354 uint32_t flags = 0;
355 uint32_t format = 0;
356 SurfaceID token = -1;
357 uint32_t identity = 0;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700358 uint32_t width = 0;
359 uint32_t height = 0;
Mathias Agopian01b76682009-04-16 20:04:08 -0700360 sp<SurfaceComposerClient> client;
361 sp<ISurface> sur;
362 if (SurfaceControl::isValid(control)) {
363 token = control->mToken;
364 identity = control->mIdentity;
365 client = control->mClient;
366 sur = control->mSurface;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700367 width = control->mWidth;
368 height = control->mHeight;
Mathias Agopian01b76682009-04-16 20:04:08 -0700369 format = control->mFormat;
370 flags = control->mFlags;
371 }
372 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
373 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
374 parcel->writeInt32(token);
375 parcel->writeInt32(identity);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700376 parcel->writeInt32(width);
377 parcel->writeInt32(height);
Mathias Agopian01b76682009-04-16 20:04:08 -0700378 parcel->writeInt32(format);
379 parcel->writeInt32(flags);
380 return NO_ERROR;
381}
382
383sp<Surface> SurfaceControl::getSurface() const
384{
385 Mutex::Autolock _l(mLock);
386 if (mSurfaceData == 0) {
387 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
388 }
389 return mSurfaceData;
390}
391
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700392// ============================================================================
393// Surface
394// ============================================================================
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395
Mathias Agopian01b76682009-04-16 20:04:08 -0700396Surface::Surface(const sp<SurfaceControl>& surface)
397 : mClient(surface->mClient), mSurface(surface->mSurface),
398 mToken(surface->mToken), mIdentity(surface->mIdentity),
Mathias Agopian0926f502009-05-04 14:17:04 -0700399 mFormat(surface->mFormat), mFlags(surface->mFlags),
Mathias Agopianba5972f2009-08-14 18:52:17 -0700400 mBufferMapper(BufferMapper::get()),
401 mWidth(surface->mWidth), mHeight(surface->mHeight)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402{
Mathias Agopian01b76682009-04-16 20:04:08 -0700403 init();
404}
Mathias Agopian62185b72009-04-16 16:19:50 -0700405
Mathias Agopian01b76682009-04-16 20:04:08 -0700406Surface::Surface(const Parcel& parcel)
Mathias Agopian0926f502009-05-04 14:17:04 -0700407 : mBufferMapper(BufferMapper::get())
Mathias Agopian01b76682009-04-16 20:04:08 -0700408{
409 sp<IBinder> clientBinder = parcel.readStrongBinder();
410 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
411 mToken = parcel.readInt32();
412 mIdentity = parcel.readInt32();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700413 mWidth = parcel.readInt32();
414 mHeight = parcel.readInt32();
Mathias Agopian01b76682009-04-16 20:04:08 -0700415 mFormat = parcel.readInt32();
416 mFlags = parcel.readInt32();
417
418 if (clientBinder != NULL)
419 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
420
421 init();
422}
423
424void Surface::init()
425{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700426 android_native_window_t::setSwapInterval = setSwapInterval;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700427 android_native_window_t::dequeueBuffer = dequeueBuffer;
428 android_native_window_t::lockBuffer = lockBuffer;
429 android_native_window_t::queueBuffer = queueBuffer;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700430 android_native_window_t::query = query;
Mathias Agopian52212712009-08-11 22:34:02 -0700431 android_native_window_t::perform = perform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 mSwapRectangle.makeInvalid();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700433 DisplayInfo dinfo;
434 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
435 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
436 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
437 // FIXME: set real values here
438 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
439 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
440 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
Mathias Agopian52212712009-08-11 22:34:02 -0700441 // be default we request a hardware surface
442 mUsage = GRALLOC_USAGE_HW_RENDER;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700443 mUsageChanged = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444}
445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446Surface::~Surface()
447{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700448 // this is a client-side operation, the surface is destroyed, unmap
449 // its buffers in this process.
450 for (int i=0 ; i<2 ; i++) {
Mathias Agopian50517542009-08-19 17:10:18 -0700451 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
Mathias Agopian21c59d02009-05-05 00:59:23 -0700452 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700453 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 }
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700455
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700456 // clear all references and trigger an IPC now, to make sure things
457 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 mClient.clear();
459 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 IPCThreadState::self()->flushCommands();
461}
462
Mathias Agopianba5972f2009-08-14 18:52:17 -0700463sp<SurfaceComposerClient> Surface::getClient() const {
464 return mClient;
465}
466
467sp<ISurface> Surface::getISurface() const {
468 return mSurface;
469}
470
471bool Surface::isValid() {
472 return mToken>=0 && mClient!=0;
473}
474
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700475status_t Surface::validate(per_client_cblk_t const* cblk) const
476{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700477 sp<SurfaceComposerClient> client(getClient());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700478 if (mToken<0 || mClient==0) {
479 LOGE("invalid token (%d, identity=%u) or client (%p)",
Mathias Agopianba5972f2009-08-14 18:52:17 -0700480 mToken, mIdentity, client.get());
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700481 return NO_INIT;
482 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700483 if (cblk == 0) {
484 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
485 return NO_INIT;
486 }
487 status_t err = cblk->validate(mToken);
488 if (err != NO_ERROR) {
489 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
490 mToken, mIdentity, err, strerror(-err));
491 return err;
492 }
493 if (mIdentity != uint32_t(cblk->layers[mToken].identity)) {
494 LOGE("using an invalid surface id=%d, identity=%u should be %d",
495 mToken, mIdentity, cblk->layers[mToken].identity);
496 return NO_INIT;
497 }
498 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499}
500
Mathias Agopian01b76682009-04-16 20:04:08 -0700501
502bool Surface::isSameSurface(
503 const sp<Surface>& lhs, const sp<Surface>& rhs)
504{
505 if (lhs == 0 || rhs == 0)
506 return false;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700507
Mathias Agopian01b76682009-04-16 20:04:08 -0700508 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
509}
510
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511// ----------------------------------------------------------------------------
512
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700513int Surface::setSwapInterval(android_native_window_t* window, int interval)
514{
515 return 0;
516}
517
518int Surface::dequeueBuffer(android_native_window_t* window,
519 android_native_buffer_t** buffer)
520{
521 Surface* self = getSelf(window);
522 return self->dequeueBuffer(buffer);
523}
524
525int Surface::lockBuffer(android_native_window_t* window,
526 android_native_buffer_t* buffer)
527{
528 Surface* self = getSelf(window);
529 return self->lockBuffer(buffer);
530}
531
532int Surface::queueBuffer(android_native_window_t* window,
533 android_native_buffer_t* buffer)
534{
535 Surface* self = getSelf(window);
536 return self->queueBuffer(buffer);
537}
538
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700539int Surface::query(android_native_window_t* window,
540 int what, int* value)
541{
542 Surface* self = getSelf(window);
543 return self->query(what, value);
544}
545
Mathias Agopian52212712009-08-11 22:34:02 -0700546int Surface::perform(android_native_window_t* window,
547 int operation, ...)
548{
549 va_list args;
550 va_start(args, operation);
551 Surface* self = getSelf(window);
552 int res = self->perform(operation, args);
553 va_end(args);
554 return res;
555}
556
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700557// ----------------------------------------------------------------------------
558
Mathias Agopian0926f502009-05-04 14:17:04 -0700559status_t Surface::dequeueBuffer(sp<SurfaceBuffer>* buffer)
560{
561 android_native_buffer_t* out;
562 status_t err = dequeueBuffer(&out);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700563 if (err == NO_ERROR) {
564 *buffer = SurfaceBuffer::getSelf(out);
565 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700566 return err;
567}
568
569status_t Surface::lockBuffer(const sp<SurfaceBuffer>& buffer)
570{
571 return lockBuffer(buffer.get());
572}
573
574status_t Surface::queueBuffer(const sp<SurfaceBuffer>& buffer)
575{
576 return queueBuffer(buffer.get());
577}
578
579// ----------------------------------------------------------------------------
580
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581int Surface::dequeueBuffer(android_native_buffer_t** buffer)
582{
583 // FIXME: dequeueBuffer() needs proper implementation
584
585 Mutex::Autolock _l(mSurfaceLock);
586
Mathias Agopianba5972f2009-08-14 18:52:17 -0700587 sp<SurfaceComposerClient> client(getClient());
588 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700589 status_t err = validate(cblk);
590 if (err != NO_ERROR)
591 return err;
592
593 SurfaceID index(mToken);
594
595 int32_t backIdx = cblk->lock_layer(size_t(index),
596 per_client_cblk_t::BLOCKING);
597
598 if (backIdx < 0)
599 return status_t(backIdx);
600
601 mBackbufferIndex = backIdx;
602 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 volatile const surface_info_t* const back = lcblk->surface + backIdx;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700604 if ((back->flags & surface_info_t::eNeedNewBuffer) || mUsageChanged) {
605 mUsageChanged = false;
Mathias Agopian52212712009-08-11 22:34:02 -0700606 err = getBufferLocked(backIdx, mUsage);
Mathias Agopian50517542009-08-19 17:10:18 -0700607 if (err == NO_ERROR) {
608 // reset the width/height with the what we get from the buffer
609 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
610 mWidth = uint32_t(backBuffer->width);
611 mHeight = uint32_t(backBuffer->height);
612 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613 }
614
Mathias Agopiancf81c842009-07-31 14:47:00 -0700615 if (err == NO_ERROR) {
616 const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
Mathias Agopian50517542009-08-19 17:10:18 -0700617 if (backBuffer != 0) {
618 mDirtyRegion.set(backBuffer->width, backBuffer->height);
619 *buffer = backBuffer.get();
620 } else {
621 err = NO_MEMORY;
622 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700623 }
Mathias Agopian50517542009-08-19 17:10:18 -0700624
Mathias Agopiancf81c842009-07-31 14:47:00 -0700625 return err;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700626}
627
628int Surface::lockBuffer(android_native_buffer_t* buffer)
629{
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700630 Mutex::Autolock _l(mSurfaceLock);
631
Mathias Agopianba5972f2009-08-14 18:52:17 -0700632 sp<SurfaceComposerClient> client(getClient());
633 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700634 status_t err = validate(cblk);
635 if (err != NO_ERROR)
636 return err;
637
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700638 // FIXME: lockBuffer() needs proper implementation
639 return 0;
640}
641
642int Surface::queueBuffer(android_native_buffer_t* buffer)
643{
644 Mutex::Autolock _l(mSurfaceLock);
645
Mathias Agopianba5972f2009-08-14 18:52:17 -0700646 sp<SurfaceComposerClient> client(getClient());
647 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700648 status_t err = validate(cblk);
649 if (err != NO_ERROR)
650 return err;
651
Mathias Agopian0926f502009-05-04 14:17:04 -0700652 if (mSwapRectangle.isValid()) {
653 mDirtyRegion.set(mSwapRectangle);
654 }
655
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700656 // transmit the dirty region
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700657 SurfaceID index(mToken);
658 layer_cblk_t* const lcblk = &(cblk->layers[index]);
Mathias Agopian0926f502009-05-04 14:17:04 -0700659 _send_dirty_region(lcblk, mDirtyRegion);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660
661 uint32_t newstate = cblk->unlock_layer_and_post(size_t(index));
662 if (!(newstate & eNextFlipPending))
Mathias Agopianba5972f2009-08-14 18:52:17 -0700663 client->signalServer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700664
665 return NO_ERROR;
666}
667
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700668int Surface::query(int what, int* value)
669{
670 switch (what) {
671 case NATIVE_WINDOW_WIDTH:
672 *value = int(mWidth);
673 return NO_ERROR;
674 case NATIVE_WINDOW_HEIGHT:
675 *value = int(mHeight);
676 return NO_ERROR;
Mathias Agopian6b1f4102009-08-06 16:04:29 -0700677 case NATIVE_WINDOW_FORMAT:
678 *value = int(mFormat);
679 return NO_ERROR;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700680 }
681 return BAD_VALUE;
682}
683
Mathias Agopian52212712009-08-11 22:34:02 -0700684int Surface::perform(int operation, va_list args)
685{
686 int res = NO_ERROR;
687 switch (operation) {
688 case NATIVE_WINDOW_SET_USAGE:
Mathias Agopianba5972f2009-08-14 18:52:17 -0700689 setUsage( va_arg(args, int) );
Mathias Agopian52212712009-08-11 22:34:02 -0700690 break;
691 default:
692 res = NAME_NOT_FOUND;
693 break;
694 }
695 return res;
696}
697
Mathias Agopianba5972f2009-08-14 18:52:17 -0700698void Surface::setUsage(uint32_t reqUsage)
699{
700 Mutex::Autolock _l(mSurfaceLock);
701 if (mUsage != reqUsage) {
702 mUsageChanged = true;
703 mUsage = reqUsage;
704 }
705}
706
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700707// ----------------------------------------------------------------------------
708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709status_t Surface::lock(SurfaceInfo* info, bool blocking) {
710 return Surface::lock(info, NULL, blocking);
711}
712
Mathias Agopian0926f502009-05-04 14:17:04 -0700713status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700714{
Mathias Agopian52212712009-08-11 22:34:02 -0700715 // we're intending to do software rendering from this point
Mathias Agopianba5972f2009-08-14 18:52:17 -0700716 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
717
Mathias Agopian0926f502009-05-04 14:17:04 -0700718 sp<SurfaceBuffer> backBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700719 status_t err = dequeueBuffer(&backBuffer);
720 if (err == NO_ERROR) {
721 err = lockBuffer(backBuffer);
722 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700723 // we handle copy-back here...
724
725 const Rect bounds(backBuffer->width, backBuffer->height);
Mathias Agopian0926f502009-05-04 14:17:04 -0700726 Region scratch(bounds);
727 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700728
Mathias Agopianba5972f2009-08-14 18:52:17 -0700729 sp<SurfaceComposerClient> client(getClient());
730 per_client_cblk_t* const cblk = client->mControl;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700731 layer_cblk_t* const lcblk = &(cblk->layers[SurfaceID(mToken)]);
732 volatile const surface_info_t* const back = lcblk->surface + mBackbufferIndex;
733 if (back->flags & surface_info_t::eBufferDirty) {
734 // content is meaningless in this case and the whole surface
735 // needs to be redrawn.
736 newDirtyRegion.set(bounds);
Mathias Agopian0926f502009-05-04 14:17:04 -0700737 } else {
738 newDirtyRegion.andSelf(bounds);
Mathias Agopian14998592009-07-13 18:29:59 -0700739 const sp<SurfaceBuffer>& frontBuffer(mBuffers[1-mBackbufferIndex]);
Mathias Agopian50517542009-08-19 17:10:18 -0700740 if (frontBuffer !=0 &&
741 backBuffer->width == frontBuffer->width &&
Mathias Agopian14998592009-07-13 18:29:59 -0700742 backBuffer->height == frontBuffer->height &&
743 !(lcblk->flags & eNoCopyBack))
744 {
Mathias Agopian0926f502009-05-04 14:17:04 -0700745 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
746 if (!copyback.isEmpty() && frontBuffer!=0) {
747 // copy front to back
748 copyBlt(backBuffer, frontBuffer, copyback);
749 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700750 }
751 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700752 mDirtyRegion = newDirtyRegion;
753 mOldDirtyRegion = newDirtyRegion;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700754
Mathias Agopiane71212b2009-05-05 00:37:46 -0700755 void* vaddr;
Mathias Agopian0926f502009-05-04 14:17:04 -0700756 status_t res = backBuffer->lock(
757 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Mathias Agopiane71212b2009-05-05 00:37:46 -0700758 newDirtyRegion.bounds(), &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700759
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700760 LOGW_IF(res, "failed locking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700761 mBackbufferIndex, backBuffer->handle);
762
763 mLockedBuffer = backBuffer;
764 other->w = backBuffer->width;
765 other->h = backBuffer->height;
766 other->s = backBuffer->stride;
767 other->usage = backBuffer->usage;
768 other->format = backBuffer->format;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700769 other->bits = vaddr;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700770 }
771 }
772 return err;
773}
774
775status_t Surface::unlockAndPost()
776{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700777 if (mLockedBuffer == 0)
778 return BAD_VALUE;
779
Mathias Agopian0926f502009-05-04 14:17:04 -0700780 status_t res = mLockedBuffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700781 LOGW_IF(res, "failed unlocking buffer %d (%p)",
Mathias Agopian0926f502009-05-04 14:17:04 -0700782 mBackbufferIndex, mLockedBuffer->handle);
783
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700784 status_t err = queueBuffer(mLockedBuffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700785 mLockedBuffer = 0;
786 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787}
788
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700789void Surface::_send_dirty_region(
790 layer_cblk_t* lcblk, const Region& dirty)
791{
792 const int32_t index = (lcblk->flags & eBufferIndex) >> eBufferIndexShift;
793 flat_region_t* flat_region = lcblk->region + index;
794 status_t err = dirty.write(flat_region, sizeof(flat_region_t));
795 if (err < NO_ERROR) {
796 // region doesn't fit, use the bounds
797 const Region reg(dirty.bounds());
798 reg.write(flat_region, sizeof(flat_region_t));
799 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800}
801
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802void Surface::setSwapRectangle(const Rect& r) {
Mathias Agopianba5972f2009-08-14 18:52:17 -0700803 Mutex::Autolock _l(mSurfaceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 mSwapRectangle = r;
805}
806
Mathias Agopian52212712009-08-11 22:34:02 -0700807status_t Surface::getBufferLocked(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808{
Mathias Agopianba5972f2009-08-14 18:52:17 -0700809 sp<ISurface> s(mSurface);
810 if (s == 0) return NO_INIT;
811
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700812 status_t err = NO_MEMORY;
Mathias Agopian50517542009-08-19 17:10:18 -0700813
814 // free the current buffer
815 sp<SurfaceBuffer>& currentBuffer(mBuffers[index]);
816 if (currentBuffer != 0) {
817 getBufferMapper().unregisterBuffer(currentBuffer->handle);
818 currentBuffer.clear();
819 }
820
Mathias Agopianba5972f2009-08-14 18:52:17 -0700821 sp<SurfaceBuffer> buffer = s->getBuffer(usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700822 LOGE_IF(buffer==0, "ISurface::getBuffer() returned NULL");
Mathias Agopian50517542009-08-19 17:10:18 -0700823 if (buffer != 0) { // this should never happen by construction
824 if (buffer->handle != NULL) {
825 err = getBufferMapper().registerBuffer(buffer->handle);
826 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
827 err, strerror(-err));
828 if (err == NO_ERROR) {
829 currentBuffer = buffer;
830 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 }
832 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700833 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834}
835
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836}; // namespace android
837