Surface.h 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#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 ISurfaceTexture;
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    setLayer(int32_t layer);
64    status_t    setPosition(int32_t x, int32_t y);
65    status_t    setSize(uint32_t w, uint32_t h);
66    status_t    hide();
67    status_t    show(int32_t layer = -1);
68    status_t    freeze();
69    status_t    unfreeze();
70    status_t    setFlags(uint32_t flags, uint32_t mask);
71    status_t    setTransparentRegionHint(const Region& transparent);
72    status_t    setAlpha(float alpha=1.0f);
73    status_t    setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
74    status_t    setFreezeTint(uint32_t tint);
75    status_t    setCrop(const Rect& crop);
76
77    static status_t writeSurfaceToParcel(
78            const sp<SurfaceControl>& control, Parcel* parcel);
79
80    sp<Surface> getSurface() const;
81
82private:
83    // can't be copied
84    SurfaceControl& operator = (SurfaceControl& rhs);
85    SurfaceControl(const SurfaceControl& rhs);
86
87    friend class SurfaceComposerClient;
88    friend class Surface;
89
90    SurfaceControl(
91            const sp<SurfaceComposerClient>& client,
92            const sp<ISurface>& surface,
93            const ISurfaceComposerClient::surface_data_t& data);
94
95    ~SurfaceControl();
96
97    status_t validate() const;
98    void destroy();
99
100    sp<SurfaceComposerClient>   mClient;
101    sp<ISurface>                mSurface;
102    SurfaceID                   mToken;
103    uint32_t                    mIdentity;
104    mutable Mutex               mLock;
105
106    mutable sp<Surface>         mSurfaceData;
107};
108
109// ---------------------------------------------------------------------------
110
111class Surface : public SurfaceTextureClient
112{
113public:
114    struct SurfaceInfo {
115        uint32_t    w;
116        uint32_t    h;
117        uint32_t    s;
118        uint32_t    usage;
119        PixelFormat format;
120        void*       bits;
121        uint32_t    reserved[2];
122    };
123
124    explicit Surface(const sp<ISurfaceTexture>& st);
125
126    static status_t writeToParcel(const sp<Surface>& control, Parcel* parcel);
127
128    static sp<Surface> readFromParcel(const Parcel& data);
129    static bool isValid(const sp<Surface>& surface) {
130        return (surface != 0) && surface->isValid();
131    }
132
133    bool        isValid();
134    uint32_t    getIdentity() const { return mIdentity; }
135    sp<ISurfaceTexture> getSurfaceTexture();
136
137    // the lock/unlock APIs must be used from the same thread
138    status_t    lock(SurfaceInfo* info, Region* dirty = NULL);
139    status_t    unlockAndPost();
140
141    sp<IBinder> asBinder() const;
142
143private:
144    // this is just to be able to write some unit tests
145    friend class Test;
146    friend class SurfaceControl;
147
148    // can't be copied
149    Surface& operator = (Surface& rhs);
150    Surface(const Surface& rhs);
151
152    explicit Surface(const sp<SurfaceControl>& control);
153    Surface(const Parcel& data, const sp<IBinder>& ref);
154    ~Surface();
155
156    /*
157     *  private stuff...
158     */
159    void init(const sp<ISurfaceTexture>& surfaceTexture);
160
161    static void cleanCachedSurfacesLocked();
162
163    virtual int query(int what, int* value) const;
164
165    // constants
166    sp<ISurface>                mSurface;
167    uint32_t                    mIdentity;
168
169    // A cache of Surface objects that have been deserialized into this process.
170    static Mutex sCachedSurfacesLock;
171    static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
172};
173
174}; // namespace android
175
176#endif // ANDROID_GUI_SURFACE_H
177