Surface.h revision 2adaf04fab35cf47c824d74d901b54094e01ccd3
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#ifndef ANDROID_GUI_SURFACE_H
18#define ANDROID_GUI_SURFACE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/KeyedVector.h>
24#include <utils/RefBase.h>
25#include <utils/threads.h>
26
27#include <ui/PixelFormat.h>
28#include <ui/Region.h>
29
30#include <gui/SurfaceTextureClient.h>
31#include <gui/ISurface.h>
32#include <gui/ISurfaceComposerClient.h>
33
34#define ANDROID_VIEW_SURFACE_JNI_ID    "mNativeSurface"
35
36namespace android {
37
38// ---------------------------------------------------------------------------
39
40class IGraphicBufferProducer;
41class Surface;
42class SurfaceComposerClient;
43
44// ---------------------------------------------------------------------------
45
46class SurfaceControl : public RefBase
47{
48public:
49    static bool isValid(const sp<SurfaceControl>& surface) {
50        return (surface != 0) && surface->isValid();
51    }
52    bool isValid() {
53        return mToken>=0 && mClient!=0;
54    }
55    static bool isSameSurface(
56            const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
57
58    uint32_t    getIdentity() const { return mIdentity; }
59
60    // release surface data from java
61    void        clear();
62
63    status_t    setLayerStack(int32_t layerStack);
64    status_t    setLayer(int32_t layer);
65    status_t    setPosition(int32_t x, int32_t y);
66    status_t    setSize(uint32_t w, uint32_t h);
67    status_t    hide();
68    status_t    show();
69    status_t    setFlags(uint32_t flags, uint32_t mask);
70    status_t    setTransparentRegionHint(const Region& transparent);
71    status_t    setAlpha(float alpha=1.0f);
72    status_t    setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
73    status_t    setCrop(const Rect& crop);
74
75    static status_t writeSurfaceToParcel(
76            const sp<SurfaceControl>& control, Parcel* parcel);
77
78    sp<Surface> getSurface() const;
79
80private:
81    // can't be copied
82    SurfaceControl& operator = (SurfaceControl& rhs);
83    SurfaceControl(const SurfaceControl& rhs);
84
85    friend class SurfaceComposerClient;
86    friend class Surface;
87
88    SurfaceControl(
89            const sp<SurfaceComposerClient>& client,
90            const sp<ISurface>& surface,
91            const ISurfaceComposerClient::surface_data_t& data);
92
93    ~SurfaceControl();
94
95    status_t validate() const;
96    void destroy();
97
98    sp<SurfaceComposerClient>   mClient;
99    sp<ISurface>                mSurface;
100    SurfaceID                   mToken;
101    uint32_t                    mIdentity;
102    mutable Mutex               mLock;
103
104    mutable sp<Surface>         mSurfaceData;
105};
106
107// ---------------------------------------------------------------------------
108
109/*
110 * This is a small wrapper around SurfaceTextureClient that provides some
111 * helper classes for Binder interaction.
112 *
113 * TODO: rename to SurfaceJniHelper.  May want to move SurfaceInfo and
114 * the associated lock() / unlockAndPost() calls to STC.
115 */
116class Surface : public SurfaceTextureClient
117{
118public:
119    struct SurfaceInfo {
120        uint32_t    w;
121        uint32_t    h;
122        uint32_t    s;
123        uint32_t    usage;
124        PixelFormat format;
125        void*       bits;
126        uint32_t    reserved[2];
127    };
128
129    explicit Surface(const sp<IGraphicBufferProducer>& bp);
130
131    static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
132
133    static sp<Surface> readFromParcel(const Parcel& data);
134    static bool isValid(const sp<Surface>& surface) {
135        return (surface != 0) && surface->isValid();
136    }
137
138    bool        isValid();
139    uint32_t    getIdentity() const { return mIdentity; }
140    sp<IGraphicBufferProducer> getSurfaceTexture();     // TODO: rename this
141
142    // the lock/unlock APIs must be used from the same thread
143    status_t    lock(SurfaceInfo* info, Region* dirty = NULL);
144    status_t    unlockAndPost();
145
146    sp<IBinder> asBinder() const;
147
148private:
149    // this is just to be able to write some unit tests
150    friend class Test;
151    friend class SurfaceControl;
152
153    // can't be copied
154    Surface& operator = (Surface& rhs);
155    Surface(const Surface& rhs);
156
157    explicit Surface(const sp<SurfaceControl>& control);
158    Surface(const Parcel& data, const sp<IBinder>& ref);
159    ~Surface();
160
161    /*
162     *  private stuff...
163     */
164    void init(const sp<IGraphicBufferProducer>& bufferProducer);
165
166    static void cleanCachedSurfacesLocked();
167
168    virtual int query(int what, int* value) const;
169
170    // constants
171    sp<ISurface>                mSurface;
172    uint32_t                    mIdentity;
173
174    // A cache of Surface objects that have been deserialized into this process.
175    static Mutex sCachedSurfacesLock;
176    static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
177};
178
179}; // namespace android
180
181#endif // ANDROID_GUI_SURFACE_H
182