Surface.cpp revision 6501e9944df131b3b7e293007084735dfa217f24
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::setFlags(uint32_t flags, uint32_t mask) {
126    status_t err = validate();
127    if (err < 0) return err;
128    const sp<SurfaceComposerClient>& client(mClient);
129    return client->setFlags(mToken, flags, mask);
130}
131status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
132    status_t err = validate();
133    if (err < 0) return err;
134    const sp<SurfaceComposerClient>& client(mClient);
135    return client->setTransparentRegionHint(mToken, transparent);
136}
137status_t SurfaceControl::setAlpha(float alpha) {
138    status_t err = validate();
139    if (err < 0) return err;
140    const sp<SurfaceComposerClient>& client(mClient);
141    return client->setAlpha(mToken, alpha);
142}
143status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
144    status_t err = validate();
145    if (err < 0) return err;
146    const sp<SurfaceComposerClient>& client(mClient);
147    return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
148}
149status_t SurfaceControl::setCrop(const Rect& crop) {
150    status_t err = validate();
151    if (err < 0) return err;
152    const sp<SurfaceComposerClient>& client(mClient);
153    return client->setCrop(mToken, crop);
154}
155
156status_t SurfaceControl::validate() const
157{
158    if (mToken<0 || mClient==0) {
159        ALOGE("invalid token (%d, identity=%u) or client (%p)",
160                mToken, mIdentity, mClient.get());
161        return NO_INIT;
162    }
163    return NO_ERROR;
164}
165
166status_t SurfaceControl::writeSurfaceToParcel(
167        const sp<SurfaceControl>& control, Parcel* parcel)
168{
169    sp<ISurface> sur;
170    uint32_t identity = 0;
171    if (SurfaceControl::isValid(control)) {
172        sur      = control->mSurface;
173        identity = control->mIdentity;
174    }
175    parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
176    parcel->writeStrongBinder(NULL);  // NULL ISurfaceTexture in this case.
177    parcel->writeInt32(identity);
178    return NO_ERROR;
179}
180
181sp<Surface> SurfaceControl::getSurface() const
182{
183    Mutex::Autolock _l(mLock);
184    if (mSurfaceData == 0) {
185        sp<SurfaceControl> surface_control(const_cast<SurfaceControl*>(this));
186        mSurfaceData = new Surface(surface_control);
187    }
188    return mSurfaceData;
189}
190
191// ============================================================================
192//  Surface
193// ============================================================================
194
195// ---------------------------------------------------------------------------
196
197Surface::Surface(const sp<SurfaceControl>& surface)
198    : SurfaceTextureClient(),
199      mSurface(surface->mSurface),
200      mIdentity(surface->mIdentity)
201{
202    sp<ISurfaceTexture> st;
203    if (mSurface != NULL) {
204        st = mSurface->getSurfaceTexture();
205    }
206    init(st);
207}
208
209Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
210    : SurfaceTextureClient()
211{
212    mSurface = interface_cast<ISurface>(ref);
213    sp<IBinder> st_binder(parcel.readStrongBinder());
214    sp<ISurfaceTexture> st;
215    if (st_binder != NULL) {
216        st = interface_cast<ISurfaceTexture>(st_binder);
217    } else if (mSurface != NULL) {
218        st = mSurface->getSurfaceTexture();
219    }
220
221    mIdentity   = parcel.readInt32();
222    init(st);
223}
224
225Surface::Surface(const sp<ISurfaceTexture>& st)
226    : SurfaceTextureClient(),
227      mSurface(NULL),
228      mIdentity(0)
229{
230    init(st);
231}
232
233status_t Surface::writeToParcel(
234        const sp<Surface>& surface, Parcel* parcel)
235{
236    sp<ISurface> sur;
237    sp<ISurfaceTexture> st;
238    uint32_t identity = 0;
239    if (Surface::isValid(surface)) {
240        sur      = surface->mSurface;
241        st       = surface->getISurfaceTexture();
242        identity = surface->mIdentity;
243    } else if (surface != 0 &&
244            (surface->mSurface != NULL ||
245             surface->getISurfaceTexture() != NULL)) {
246        ALOGE("Parceling invalid surface with non-NULL ISurface/ISurfaceTexture as NULL: "
247             "mSurface = %p, surfaceTexture = %p, mIdentity = %d, ",
248             surface->mSurface.get(), surface->getISurfaceTexture().get(),
249             surface->mIdentity);
250    }
251
252    parcel->writeStrongBinder(sur != NULL ? sur->asBinder() : NULL);
253    parcel->writeStrongBinder(st != NULL ? st->asBinder() : NULL);
254    parcel->writeInt32(identity);
255    return NO_ERROR;
256
257}
258
259Mutex Surface::sCachedSurfacesLock;
260DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
261
262sp<Surface> Surface::readFromParcel(const Parcel& data) {
263    Mutex::Autolock _l(sCachedSurfacesLock);
264    sp<IBinder> binder(data.readStrongBinder());
265    sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
266    if (surface == 0) {
267       surface = new Surface(data, binder);
268       sCachedSurfaces.add(binder, surface);
269    } else {
270        // The Surface was found in the cache, but we still should clear any
271        // remaining data from the parcel.
272        data.readStrongBinder();  // ISurfaceTexture
273        data.readInt32();         // identity
274    }
275    if (surface->mSurface == NULL && surface->getISurfaceTexture() == NULL) {
276        surface = 0;
277    }
278    cleanCachedSurfacesLocked();
279    return surface;
280}
281
282// Remove the stale entries from the surface cache.  This should only be called
283// with sCachedSurfacesLock held.
284void Surface::cleanCachedSurfacesLocked() {
285    for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
286        wp<Surface> s(sCachedSurfaces.valueAt(i));
287        if (s == 0 || s.promote() == 0) {
288            sCachedSurfaces.removeItemsAt(i);
289        }
290    }
291}
292
293void Surface::init(const sp<ISurfaceTexture>& surfaceTexture)
294{
295    if (mSurface != NULL || surfaceTexture != NULL) {
296        ALOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface");
297        if (surfaceTexture != NULL) {
298            setISurfaceTexture(surfaceTexture);
299            setUsage(GraphicBuffer::USAGE_HW_RENDER);
300        }
301
302        DisplayInfo dinfo;
303        SurfaceComposerClient::getDisplayInfo(0, &dinfo);
304        const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
305        const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
306        const_cast<uint32_t&>(ANativeWindow::flags) = 0;
307    }
308}
309
310Surface::~Surface()
311{
312    // clear all references and trigger an IPC now, to make sure things
313    // happen without delay, since these resources are quite heavy.
314    mSurface.clear();
315    IPCThreadState::self()->flushCommands();
316}
317
318bool Surface::isValid() {
319    return getISurfaceTexture() != NULL;
320}
321
322sp<ISurfaceTexture> Surface::getSurfaceTexture() {
323    return getISurfaceTexture();
324}
325
326sp<IBinder> Surface::asBinder() const {
327    return mSurface!=0 ? mSurface->asBinder() : 0;
328}
329
330// ----------------------------------------------------------------------------
331
332int Surface::query(int what, int* value) const {
333    switch (what) {
334    case NATIVE_WINDOW_CONCRETE_TYPE:
335        *value = NATIVE_WINDOW_SURFACE;
336        return NO_ERROR;
337    }
338    return SurfaceTextureClient::query(what, value);
339}
340
341// ----------------------------------------------------------------------------
342
343status_t Surface::lock(SurfaceInfo* other, Region* inOutDirtyRegion) {
344    ANativeWindow_Buffer outBuffer;
345
346    ARect temp;
347    ARect* inOutDirtyBounds = NULL;
348    if (inOutDirtyRegion) {
349        temp = inOutDirtyRegion->getBounds();
350        inOutDirtyBounds = &temp;
351    }
352
353    status_t err = SurfaceTextureClient::lock(&outBuffer, inOutDirtyBounds);
354
355    if (err == NO_ERROR) {
356        other->w = uint32_t(outBuffer.width);
357        other->h = uint32_t(outBuffer.height);
358        other->s = uint32_t(outBuffer.stride);
359        other->usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
360        other->format = uint32_t(outBuffer.format);
361        other->bits = outBuffer.bits;
362    }
363
364    if (inOutDirtyRegion) {
365        inOutDirtyRegion->set( static_cast<Rect const&>(temp) );
366    }
367
368    return err;
369}
370
371status_t Surface::unlockAndPost() {
372    return SurfaceTextureClient::unlockAndPost();
373}
374
375// ----------------------------------------------------------------------------
376}; // namespace android
377