Surface.cpp revision a67932fe6864ac346e7f78b86df11cf6c5344137
1/*
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 <errno.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
24#include <utils/CallStack.h>
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/threads.h>
28
29#include <binder/IMemory.h>
30#include <binder/IPCThreadState.h>
31
32#include <gui/SurfaceTextureClient.h>
33
34#include <ui/DisplayInfo.h>
35#include <ui/GraphicBuffer.h>
36#include <ui/GraphicBufferMapper.h>
37#include <ui/GraphicLog.h>
38#include <ui/Rect.h>
39
40#include <surfaceflinger/ISurface.h>
41#include <surfaceflinger/ISurfaceComposer.h>
42#include <surfaceflinger/Surface.h>
43#include <surfaceflinger/SurfaceComposerClient.h>
44
45#include <private/surfaceflinger/LayerState.h>
46
47namespace android {
48
49// ----------------------------------------------------------------------
50
51static status_t copyBlt(
52        const sp<GraphicBuffer>& dst,
53        const sp<GraphicBuffer>& src,
54        const Region& reg)
55{
56    // src and dst with, height and format must be identical. no verification
57    // is done here.
58    status_t err;
59    uint8_t const * src_bits = NULL;
60    err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
61    LOGE_IF(err, "error locking src buffer %s", strerror(-err));
62
63    uint8_t* dst_bits = NULL;
64    err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
65    LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
66
67    Region::const_iterator head(reg.begin());
68    Region::const_iterator tail(reg.end());
69    if (head != tail && src_bits && dst_bits) {
70        const size_t bpp = bytesPerPixel(src->format);
71        const size_t dbpr = dst->stride * bpp;
72        const size_t sbpr = src->stride * bpp;
73
74        while (head != tail) {
75            const Rect& r(*head++);
76            ssize_t h = r.height();
77            if (h <= 0) continue;
78            size_t size = r.width() * bpp;
79            uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
80            uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
81            if (dbpr==sbpr && size==sbpr) {
82                size *= h;
83                h = 1;
84            }
85            do {
86                memcpy(d, s, size);
87                d += dbpr;
88                s += sbpr;
89            } while (--h > 0);
90        }
91    }
92
93    if (src_bits)
94        src->unlock();
95
96    if (dst_bits)
97        dst->unlock();
98
99    return err;
100}
101
102// ============================================================================
103//  SurfaceControl
104// ============================================================================
105
106SurfaceControl::SurfaceControl(
107        const sp<SurfaceComposerClient>& client,
108        const sp<ISurface>& surface,
109        const ISurfaceComposerClient::surface_data_t& data,
110        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
111    : mClient(client), mSurface(surface),
112      mToken(data.token), mIdentity(data.identity),
113      mWidth(data.width), mHeight(data.height), mFormat(data.format),
114      mFlags(flags)
115{
116}
117
118SurfaceControl::~SurfaceControl()
119{
120    destroy();
121}
122
123void SurfaceControl::destroy()
124{
125    if (isValid()) {
126        mClient->destroySurface(mToken);
127    }
128
129    // clear all references and trigger an IPC now, to make sure things
130    // happen without delay, since these resources are quite heavy.
131    mClient.clear();
132    mSurface.clear();
133    IPCThreadState::self()->flushCommands();
134}
135
136void SurfaceControl::clear()
137{
138    // here, the window manager tells us explicitly that we should destroy
139    // the surface's resource. Soon after this call, it will also release
140    // its last reference (which will call the dtor); however, it is possible
141    // that a client living in the same process still holds references which
142    // would delay the call to the dtor -- that is why we need this explicit
143    // "clear()" call.
144    destroy();
145}
146
147bool SurfaceControl::isSameSurface(
148        const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
149{
150    if (lhs == 0 || rhs == 0)
151        return false;
152    return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
153}
154
155status_t SurfaceControl::setLayer(int32_t layer) {
156    status_t err = validate();
157    if (err < 0) return err;
158    const sp<SurfaceComposerClient>& client(mClient);
159    return client->setLayer(mToken, layer);
160}
161status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
162    status_t err = validate();
163    if (err < 0) return err;
164    const sp<SurfaceComposerClient>& client(mClient);
165    return client->setPosition(mToken, x, y);
166}
167status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
168    status_t err = validate();
169    if (err < 0) return err;
170    const sp<SurfaceComposerClient>& client(mClient);
171    return client->setSize(mToken, w, h);
172}
173status_t SurfaceControl::hide() {
174    status_t err = validate();
175    if (err < 0) return err;
176    const sp<SurfaceComposerClient>& client(mClient);
177    return client->hide(mToken);
178}
179status_t SurfaceControl::show(int32_t layer) {
180    status_t err = validate();
181    if (err < 0) return err;
182    const sp<SurfaceComposerClient>& client(mClient);
183    return client->show(mToken, layer);
184}
185status_t SurfaceControl::freeze() {
186    status_t err = validate();
187    if (err < 0) return err;
188    const sp<SurfaceComposerClient>& client(mClient);
189    return client->freeze(mToken);
190}
191status_t SurfaceControl::unfreeze() {
192    status_t err = validate();
193    if (err < 0) return err;
194    const sp<SurfaceComposerClient>& client(mClient);
195    return client->unfreeze(mToken);
196}
197status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
198    status_t err = validate();
199    if (err < 0) return err;
200    const sp<SurfaceComposerClient>& client(mClient);
201    return client->setFlags(mToken, flags, mask);
202}
203status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
204    status_t err = validate();
205    if (err < 0) return err;
206    const sp<SurfaceComposerClient>& client(mClient);
207    return client->setTransparentRegionHint(mToken, transparent);
208}
209status_t SurfaceControl::setAlpha(float alpha) {
210    status_t err = validate();
211    if (err < 0) return err;
212    const sp<SurfaceComposerClient>& client(mClient);
213    return client->setAlpha(mToken, alpha);
214}
215status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
216    status_t err = validate();
217    if (err < 0) return err;
218    const sp<SurfaceComposerClient>& client(mClient);
219    return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
220}
221status_t SurfaceControl::setFreezeTint(uint32_t tint) {
222    status_t err = validate();
223    if (err < 0) return err;
224    const sp<SurfaceComposerClient>& client(mClient);
225    return client->setFreezeTint(mToken, tint);
226}
227
228status_t SurfaceControl::validate() const
229{
230    if (mToken<0 || mClient==0) {
231        LOGE("invalid token (%d, identity=%u) or client (%p)",
232                mToken, mIdentity, mClient.get());
233        return NO_INIT;
234    }
235    return NO_ERROR;
236}
237
238status_t SurfaceControl::writeSurfaceToParcel(
239        const sp<SurfaceControl>& control, Parcel* parcel)
240{
241    sp<ISurface> sur;
242    uint32_t identity = 0;
243    uint32_t width = 0;
244    uint32_t height = 0;
245    uint32_t format = 0;
246    uint32_t flags = 0;
247    if (SurfaceControl::isValid(control)) {
248        sur      = control->mSurface;
249        identity = control->mIdentity;
250        width    = control->mWidth;
251        height   = control->mHeight;
252        format   = control->mFormat;
253        flags    = control->mFlags;
254    }
255    parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
256    parcel->writeInt32(identity);
257    parcel->writeInt32(width);
258    parcel->writeInt32(height);
259    parcel->writeInt32(format);
260    parcel->writeInt32(flags);
261    return NO_ERROR;
262}
263
264sp<Surface> SurfaceControl::getSurface() const
265{
266    Mutex::Autolock _l(mLock);
267    if (mSurfaceData == 0) {
268        mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
269    }
270    return mSurfaceData;
271}
272
273// ============================================================================
274//  Surface
275// ============================================================================
276
277// ---------------------------------------------------------------------------
278
279Surface::Surface(const sp<SurfaceControl>& surface)
280    : mInitCheck(NO_INIT),
281      mSurface(surface->mSurface),
282      mIdentity(surface->mIdentity),
283      mFormat(surface->mFormat), mFlags(surface->mFlags),
284      mWidth(surface->mWidth), mHeight(surface->mHeight)
285{
286    init();
287}
288
289Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
290    : mInitCheck(NO_INIT)
291{
292    mSurface    = interface_cast<ISurface>(ref);
293    mIdentity   = parcel.readInt32();
294    mWidth      = parcel.readInt32();
295    mHeight     = parcel.readInt32();
296    mFormat     = parcel.readInt32();
297    mFlags      = parcel.readInt32();
298    init();
299}
300
301status_t Surface::writeToParcel(
302        const sp<Surface>& surface, Parcel* parcel)
303{
304    sp<ISurface> sur;
305    uint32_t identity = 0;
306    uint32_t width = 0;
307    uint32_t height = 0;
308    uint32_t format = 0;
309    uint32_t flags = 0;
310    if (Surface::isValid(surface)) {
311        sur      = surface->mSurface;
312        identity = surface->mIdentity;
313        width    = surface->mWidth;
314        height   = surface->mHeight;
315        format   = surface->mFormat;
316        flags    = surface->mFlags;
317    } else if (surface != 0 && surface->mSurface != 0) {
318        LOGW("Parceling invalid surface with non-NULL ISurface as NULL: "
319             "mSurface = %p, mIdentity = %d, mWidth = %d, mHeight = %d, "
320             "mFormat = %d, mFlags = 0x%08x, mInitCheck = %d",
321             surface->mSurface.get(), surface->mIdentity, surface->mWidth,
322             surface->mHeight, surface->mFormat, surface->mFlags,
323             surface->mInitCheck);
324    }
325    parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
326    parcel->writeInt32(identity);
327    parcel->writeInt32(width);
328    parcel->writeInt32(height);
329    parcel->writeInt32(format);
330    parcel->writeInt32(flags);
331    return NO_ERROR;
332
333}
334
335Mutex Surface::sCachedSurfacesLock;
336DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
337
338sp<Surface> Surface::readFromParcel(const Parcel& data) {
339    Mutex::Autolock _l(sCachedSurfacesLock);
340    sp<IBinder> binder(data.readStrongBinder());
341    sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
342    if (surface == 0) {
343       surface = new Surface(data, binder);
344       sCachedSurfaces.add(binder, surface);
345    }
346    if (surface->mSurface == 0) {
347      surface = 0;
348    }
349    cleanCachedSurfacesLocked();
350    return surface;
351}
352
353// Remove the stale entries from the surface cache.  This should only be called
354// with sCachedSurfacesLock held.
355void Surface::cleanCachedSurfacesLocked() {
356    for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
357        wp<Surface> s(sCachedSurfaces.valueAt(i));
358        if (s == 0 || s.promote() == 0) {
359            sCachedSurfaces.removeItemsAt(i);
360        }
361    }
362}
363
364void Surface::init()
365{
366    ANativeWindow::setSwapInterval  = setSwapInterval;
367    ANativeWindow::dequeueBuffer    = dequeueBuffer;
368    ANativeWindow::cancelBuffer     = cancelBuffer;
369    ANativeWindow::lockBuffer       = lockBuffer;
370    ANativeWindow::queueBuffer      = queueBuffer;
371    ANativeWindow::query            = query;
372    ANativeWindow::perform          = perform;
373
374    if (mSurface != NULL) {
375        sp<ISurfaceTexture> surfaceTexture(mSurface->getSurfaceTexture());
376        LOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface");
377        if (surfaceTexture != NULL) {
378            mSurfaceTextureClient = new SurfaceTextureClient(surfaceTexture);
379            mSurfaceTextureClient->setUsage(GraphicBuffer::USAGE_HW_RENDER);
380        }
381
382        DisplayInfo dinfo;
383        SurfaceComposerClient::getDisplayInfo(0, &dinfo);
384        const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
385        const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
386
387        const_cast<int&>(ANativeWindow::minSwapInterval) =
388                mSurfaceTextureClient->minSwapInterval;
389
390        const_cast<int&>(ANativeWindow::maxSwapInterval) =
391                mSurfaceTextureClient->maxSwapInterval;
392
393        const_cast<uint32_t&>(ANativeWindow::flags) = 0;
394
395        if (mSurfaceTextureClient != 0) {
396            mInitCheck = NO_ERROR;
397        }
398    }
399}
400
401Surface::~Surface()
402{
403    // clear all references and trigger an IPC now, to make sure things
404    // happen without delay, since these resources are quite heavy.
405    mSurfaceTextureClient.clear();
406    mSurface.clear();
407    IPCThreadState::self()->flushCommands();
408}
409
410bool Surface::isValid() {
411    return mInitCheck == NO_ERROR;
412}
413
414status_t Surface::validate(bool inCancelBuffer) const
415{
416    // check that we initialized ourself properly
417    if (mInitCheck != NO_ERROR) {
418        LOGE("invalid token (identity=%u)", mIdentity);
419        return mInitCheck;
420    }
421    return NO_ERROR;
422}
423
424sp<IBinder> Surface::asBinder() const {
425    return mSurface!=0 ? mSurface->asBinder() : 0;
426}
427
428// ----------------------------------------------------------------------------
429
430int Surface::setSwapInterval(ANativeWindow* window, int interval) {
431    Surface* self = getSelf(window);
432    return self->setSwapInterval(interval);
433}
434
435int Surface::dequeueBuffer(ANativeWindow* window,
436        ANativeWindowBuffer** buffer) {
437    Surface* self = getSelf(window);
438    return self->dequeueBuffer(buffer);
439}
440
441int Surface::cancelBuffer(ANativeWindow* window,
442        ANativeWindowBuffer* buffer) {
443    Surface* self = getSelf(window);
444    return self->cancelBuffer(buffer);
445}
446
447int Surface::lockBuffer(ANativeWindow* window,
448        ANativeWindowBuffer* buffer) {
449    Surface* self = getSelf(window);
450    return self->lockBuffer(buffer);
451}
452
453int Surface::queueBuffer(ANativeWindow* window,
454        ANativeWindowBuffer* buffer) {
455    Surface* self = getSelf(window);
456    return self->queueBuffer(buffer);
457}
458
459int Surface::query(const ANativeWindow* window,
460        int what, int* value) {
461    const Surface* self = getSelf(window);
462    return self->query(what, value);
463}
464
465int Surface::perform(ANativeWindow* window,
466        int operation, ...) {
467    va_list args;
468    va_start(args, operation);
469    Surface* self = getSelf(window);
470    int res = self->perform(operation, args);
471    va_end(args);
472    return res;
473}
474
475// ----------------------------------------------------------------------------
476
477int Surface::setSwapInterval(int interval) {
478    return mSurfaceTextureClient->setSwapInterval(interval);
479}
480
481int Surface::dequeueBuffer(ANativeWindowBuffer** buffer) {
482    status_t err = mSurfaceTextureClient->dequeueBuffer(buffer);
483    if (err == NO_ERROR) {
484        mDirtyRegion.set(buffer[0]->width, buffer[0]->height);
485    }
486    return err;
487}
488
489int Surface::cancelBuffer(ANativeWindowBuffer* buffer) {
490    return mSurfaceTextureClient->cancelBuffer(buffer);
491}
492
493int Surface::lockBuffer(ANativeWindowBuffer* buffer) {
494    return mSurfaceTextureClient->lockBuffer(buffer);
495}
496
497int Surface::queueBuffer(ANativeWindowBuffer* buffer) {
498    return mSurfaceTextureClient->queueBuffer(buffer);
499}
500
501int Surface::query(int what, int* value) const {
502    switch (what) {
503    case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
504        // TODO: this is not needed anymore
505        *value = 1;
506        return NO_ERROR;
507    case NATIVE_WINDOW_CONCRETE_TYPE:
508        // TODO: this is not needed anymore
509        *value = NATIVE_WINDOW_SURFACE;
510        return NO_ERROR;
511    }
512    return mSurfaceTextureClient->query(what, value);
513}
514
515int Surface::perform(int operation, va_list args) {
516    return mSurfaceTextureClient->perform(operation, args);
517}
518
519// ----------------------------------------------------------------------------
520
521int Surface::getConnectedApi() const {
522    return mSurfaceTextureClient->getConnectedApi();
523}
524
525// ----------------------------------------------------------------------------
526
527status_t Surface::lock(SurfaceInfo* info, bool blocking) {
528    return Surface::lock(info, NULL, blocking);
529}
530
531status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
532{
533    if (getConnectedApi()) {
534        LOGE("Surface::lock(%p) failed. Already connected to another API",
535                (ANativeWindow*)this);
536        CallStack stack;
537        stack.update();
538        stack.dump("");
539        return INVALID_OPERATION;
540    }
541
542    if (mApiLock.tryLock() != NO_ERROR) {
543        LOGE("calling Surface::lock from different threads!");
544        CallStack stack;
545        stack.update();
546        stack.dump("");
547        return WOULD_BLOCK;
548    }
549
550    /* Here we're holding mApiLock */
551
552    if (mLockedBuffer != 0) {
553        LOGE("Surface::lock failed, already locked");
554        mApiLock.unlock();
555        return INVALID_OPERATION;
556    }
557
558    // we're intending to do software rendering from this point
559    mSurfaceTextureClient->setUsage(
560            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
561
562    ANativeWindowBuffer* out;
563    status_t err = mSurfaceTextureClient->dequeueBuffer(&out);
564    LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
565    if (err == NO_ERROR) {
566        sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
567        err = mSurfaceTextureClient->lockBuffer(backBuffer.get());
568        LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
569                backBuffer->handle, strerror(-err));
570        if (err == NO_ERROR) {
571            const Rect bounds(backBuffer->width, backBuffer->height);
572            const Region boundsRegion(bounds);
573            Region scratch(boundsRegion);
574            Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
575            newDirtyRegion &= boundsRegion;
576
577            // figure out if we can copy the frontbuffer back
578            const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
579            const bool canCopyBack = (frontBuffer != 0 &&
580                    backBuffer->width  == frontBuffer->width &&
581                    backBuffer->height == frontBuffer->height &&
582                    backBuffer->format == frontBuffer->format &&
583                    !(mFlags & ISurfaceComposer::eDestroyBackbuffer));
584
585            // the dirty region we report to surfaceflinger is the one
586            // given by the user (as opposed to the one *we* return to the
587            // user).
588            mDirtyRegion = newDirtyRegion;
589
590            if (canCopyBack) {
591                // copy the area that is invalid and not repainted this round
592                const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
593                if (!copyback.isEmpty())
594                    copyBlt(backBuffer, frontBuffer, copyback);
595            } else {
596                // if we can't copy-back anything, modify the user's dirty
597                // region to make sure they redraw the whole buffer
598                newDirtyRegion = boundsRegion;
599            }
600
601            // keep track of the are of the buffer that is "clean"
602            // (ie: that will be redrawn)
603            mOldDirtyRegion = newDirtyRegion;
604
605            void* vaddr;
606            status_t res = backBuffer->lock(
607                    GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
608                    newDirtyRegion.bounds(), &vaddr);
609
610            LOGW_IF(res, "failed locking buffer (handle = %p)",
611                    backBuffer->handle);
612
613            mLockedBuffer = backBuffer;
614            other->w      = backBuffer->width;
615            other->h      = backBuffer->height;
616            other->s      = backBuffer->stride;
617            other->usage  = backBuffer->usage;
618            other->format = backBuffer->format;
619            other->bits   = vaddr;
620        }
621    }
622    mApiLock.unlock();
623    return err;
624}
625
626status_t Surface::unlockAndPost()
627{
628    if (mLockedBuffer == 0) {
629        LOGE("Surface::unlockAndPost failed, no locked buffer");
630        return INVALID_OPERATION;
631    }
632
633    status_t err = mLockedBuffer->unlock();
634    LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
635
636    err = mSurfaceTextureClient->queueBuffer(mLockedBuffer.get());
637    LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
638            mLockedBuffer->handle, strerror(-err));
639
640    mPostedBuffer = mLockedBuffer;
641    mLockedBuffer = 0;
642    return err;
643}
644
645// ----------------------------------------------------------------------------
646}; // namespace android
647