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