Surface.cpp revision b0e76f456afaada43ddb0968bb37145219f90cd3
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 <android/native_window.h>
25
26#include <utils/CallStack.h>
27#include <utils/Errors.h>
28#include <utils/Log.h>
29#include <utils/threads.h>
30
31#include <binder/IPCThreadState.h>
32
33#include <ui/DisplayInfo.h>
34#include <ui/GraphicBuffer.h>
35#include <ui/Rect.h>
36
37#include <gui/ISurface.h>
38#include <gui/ISurfaceComposer.h>
39#include <gui/Surface.h>
40#include <gui/SurfaceComposerClient.h>
41#include <gui/SurfaceTextureClient.h>
42
43namespace android {
44
45// ============================================================================
46//  SurfaceControl
47// ============================================================================
48
49SurfaceControl::SurfaceControl(
50        const sp<SurfaceComposerClient>& client,
51        const sp<ISurface>& surface,
52        const ISurfaceComposerClient::surface_data_t& data)
53    : mClient(client), mSurface(surface),
54      mToken(data.token), mIdentity(data.identity)
55{
56}
57
58SurfaceControl::~SurfaceControl()
59{
60    destroy();
61}
62
63void SurfaceControl::destroy()
64{
65    if (isValid()) {
66        mClient->destroySurface(mToken);
67    }
68
69    // clear all references and trigger an IPC now, to make sure things
70    // happen without delay, since these resources are quite heavy.
71    mClient.clear();
72    mSurface.clear();
73    IPCThreadState::self()->flushCommands();
74}
75
76void SurfaceControl::clear()
77{
78    // here, the window manager tells us explicitly that we should destroy
79    // the surface's resource. Soon after this call, it will also release
80    // its last reference (which will call the dtor); however, it is possible
81    // that a client living in the same process still holds references which
82    // would delay the call to the dtor -- that is why we need this explicit
83    // "clear()" call.
84    destroy();
85}
86
87bool SurfaceControl::isSameSurface(
88        const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
89{
90    if (lhs == 0 || rhs == 0)
91        return false;
92    return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
93}
94
95status_t SurfaceControl::setLayer(int32_t layer) {
96    status_t err = validate();
97    if (err < 0) return err;
98    const sp<SurfaceComposerClient>& client(mClient);
99    return client->setLayer(mToken, layer);
100}
101status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
102    status_t err = validate();
103    if (err < 0) return err;
104    const sp<SurfaceComposerClient>& client(mClient);
105    return client->setPosition(mToken, x, y);
106}
107status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
108    status_t err = validate();
109    if (err < 0) return err;
110    const sp<SurfaceComposerClient>& client(mClient);
111    return client->setSize(mToken, w, h);
112}
113status_t SurfaceControl::hide() {
114    status_t err = validate();
115    if (err < 0) return err;
116    const sp<SurfaceComposerClient>& client(mClient);
117    return client->hide(mToken);
118}
119status_t SurfaceControl::show(int32_t layer) {
120    status_t err = validate();
121    if (err < 0) return err;
122    const sp<SurfaceComposerClient>& client(mClient);
123    return client->show(mToken, layer);
124}
125status_t SurfaceControl::freeze() {
126    status_t err = validate();
127    if (err < 0) return err;
128    const sp<SurfaceComposerClient>& client(mClient);
129    return client->freeze(mToken);
130}
131status_t SurfaceControl::unfreeze() {
132    status_t err = validate();
133    if (err < 0) return err;
134    const sp<SurfaceComposerClient>& client(mClient);
135    return client->unfreeze(mToken);
136}
137status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
138    status_t err = validate();
139    if (err < 0) return err;
140    const sp<SurfaceComposerClient>& client(mClient);
141    return client->setFlags(mToken, flags, mask);
142}
143status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
144    status_t err = validate();
145    if (err < 0) return err;
146    const sp<SurfaceComposerClient>& client(mClient);
147    return client->setTransparentRegionHint(mToken, transparent);
148}
149status_t SurfaceControl::setAlpha(float alpha) {
150    status_t err = validate();
151    if (err < 0) return err;
152    const sp<SurfaceComposerClient>& client(mClient);
153    return client->setAlpha(mToken, alpha);
154}
155status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
156    status_t err = validate();
157    if (err < 0) return err;
158    const sp<SurfaceComposerClient>& client(mClient);
159    return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
160}
161status_t SurfaceControl::setFreezeTint(uint32_t tint) {
162    status_t err = validate();
163    if (err < 0) return err;
164    const sp<SurfaceComposerClient>& client(mClient);
165    return client->setFreezeTint(mToken, tint);
166}
167
168status_t SurfaceControl::validate() const
169{
170    if (mToken<0 || mClient==0) {
171        ALOGE("invalid token (%d, identity=%u) or client (%p)",
172                mToken, mIdentity, mClient.get());
173        return NO_INIT;
174    }
175    return NO_ERROR;
176}
177
178status_t SurfaceControl::writeSurfaceToParcel(
179        const sp<SurfaceControl>& control, Parcel* parcel)
180{
181    sp<ISurface> sur;
182    uint32_t identity = 0;
183    if (SurfaceControl::isValid(control)) {
184        sur      = control->mSurface;
185        identity = control->mIdentity;
186    }
187    parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
188    parcel->writeStrongBinder(NULL);  // NULL ISurfaceTexture in this case.
189    parcel->writeInt32(identity);
190    return NO_ERROR;
191}
192
193sp<Surface> SurfaceControl::getSurface() const
194{
195    Mutex::Autolock _l(mLock);
196    if (mSurfaceData == 0) {
197        sp<SurfaceControl> surface_control(const_cast<SurfaceControl*>(this));
198        mSurfaceData = new Surface(surface_control);
199    }
200    return mSurfaceData;
201}
202
203// ============================================================================
204//  Surface
205// ============================================================================
206
207// ---------------------------------------------------------------------------
208
209Surface::Surface(const sp<SurfaceControl>& surface)
210    : SurfaceTextureClient(),
211      mSurface(surface->mSurface),
212      mIdentity(surface->mIdentity)
213{
214    sp<ISurfaceTexture> st;
215    if (mSurface != NULL) {
216        st = mSurface->getSurfaceTexture();
217    }
218    init(st);
219}
220
221Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
222    : SurfaceTextureClient()
223{
224    mSurface = interface_cast<ISurface>(ref);
225    sp<IBinder> st_binder(parcel.readStrongBinder());
226    sp<ISurfaceTexture> st;
227    if (st_binder != NULL) {
228        st = interface_cast<ISurfaceTexture>(st_binder);
229    } else if (mSurface != NULL) {
230        st = mSurface->getSurfaceTexture();
231    }
232
233    mIdentity   = parcel.readInt32();
234    init(st);
235}
236
237Surface::Surface(const sp<ISurfaceTexture>& st)
238    : SurfaceTextureClient(),
239      mSurface(NULL),
240      mIdentity(0)
241{
242    init(st);
243}
244
245status_t Surface::writeToParcel(
246        const sp<Surface>& surface, Parcel* parcel)
247{
248    sp<ISurface> sur;
249    sp<ISurfaceTexture> st;
250    uint32_t identity = 0;
251    if (Surface::isValid(surface)) {
252        sur      = surface->mSurface;
253        st       = surface->getISurfaceTexture();
254        identity = surface->mIdentity;
255    } else if (surface != 0 &&
256            (surface->mSurface != NULL ||
257             surface->getISurfaceTexture() != NULL)) {
258        ALOGE("Parceling invalid surface with non-NULL ISurface/ISurfaceTexture as NULL: "
259             "mSurface = %p, surfaceTexture = %p, mIdentity = %d, ",
260             surface->mSurface.get(), surface->getISurfaceTexture().get(),
261             surface->mIdentity);
262    }
263
264    parcel->writeStrongBinder(sur != NULL ? sur->asBinder() : NULL);
265    parcel->writeStrongBinder(st != NULL ? st->asBinder() : NULL);
266    parcel->writeInt32(identity);
267    return NO_ERROR;
268
269}
270
271Mutex Surface::sCachedSurfacesLock;
272DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
273
274sp<Surface> Surface::readFromParcel(const Parcel& data) {
275    Mutex::Autolock _l(sCachedSurfacesLock);
276    sp<IBinder> binder(data.readStrongBinder());
277    sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
278    if (surface == 0) {
279       surface = new Surface(data, binder);
280       sCachedSurfaces.add(binder, surface);
281    } else {
282        // The Surface was found in the cache, but we still should clear any
283        // remaining data from the parcel.
284        data.readStrongBinder();  // ISurfaceTexture
285        data.readInt32();         // identity
286    }
287    if (surface->mSurface == NULL && surface->getISurfaceTexture() == NULL) {
288        surface = 0;
289    }
290    cleanCachedSurfacesLocked();
291    return surface;
292}
293
294// Remove the stale entries from the surface cache.  This should only be called
295// with sCachedSurfacesLock held.
296void Surface::cleanCachedSurfacesLocked() {
297    for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
298        wp<Surface> s(sCachedSurfaces.valueAt(i));
299        if (s == 0 || s.promote() == 0) {
300            sCachedSurfaces.removeItemsAt(i);
301        }
302    }
303}
304
305void Surface::init(const sp<ISurfaceTexture>& surfaceTexture)
306{
307    if (mSurface != NULL || surfaceTexture != NULL) {
308        ALOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface");
309        if (surfaceTexture != NULL) {
310            setISurfaceTexture(surfaceTexture);
311            setUsage(GraphicBuffer::USAGE_HW_RENDER);
312        }
313
314        DisplayInfo dinfo;
315        SurfaceComposerClient::getDisplayInfo(0, &dinfo);
316        const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
317        const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
318        const_cast<uint32_t&>(ANativeWindow::flags) = 0;
319    }
320}
321
322Surface::~Surface()
323{
324    // clear all references and trigger an IPC now, to make sure things
325    // happen without delay, since these resources are quite heavy.
326    mSurface.clear();
327    IPCThreadState::self()->flushCommands();
328}
329
330bool Surface::isValid() {
331    return getISurfaceTexture() != NULL;
332}
333
334sp<ISurfaceTexture> Surface::getSurfaceTexture() {
335    return getISurfaceTexture();
336}
337
338sp<IBinder> Surface::asBinder() const {
339    return mSurface!=0 ? mSurface->asBinder() : 0;
340}
341
342// ----------------------------------------------------------------------------
343
344int Surface::query(int what, int* value) const {
345    switch (what) {
346    case NATIVE_WINDOW_CONCRETE_TYPE:
347        *value = NATIVE_WINDOW_SURFACE;
348        return NO_ERROR;
349    }
350    return SurfaceTextureClient::query(what, value);
351}
352
353// ----------------------------------------------------------------------------
354
355status_t Surface::lock(SurfaceInfo* other, Region* inOutDirtyRegion) {
356    ANativeWindow_Buffer outBuffer;
357
358    ARect temp;
359    ARect* inOutDirtyBounds = NULL;
360    if (inOutDirtyRegion) {
361        temp = inOutDirtyRegion->getBounds();
362        inOutDirtyBounds = &temp;
363    }
364
365    status_t err = SurfaceTextureClient::lock(&outBuffer, inOutDirtyBounds);
366
367    if (err == NO_ERROR) {
368        other->w = uint32_t(outBuffer.width);
369        other->h = uint32_t(outBuffer.height);
370        other->s = uint32_t(outBuffer.stride);
371        other->usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
372        other->format = uint32_t(outBuffer.format);
373        other->bits = outBuffer.bits;
374    }
375
376    if (inOutDirtyRegion) {
377        inOutDirtyRegion->set( static_cast<Rect const&>(temp) );
378    }
379
380    return err;
381}
382
383status_t Surface::unlockAndPost() {
384    return SurfaceTextureClient::unlockAndPost();
385}
386
387// ----------------------------------------------------------------------------
388}; // namespace android
389