Surface.h revision 000479f9e325b4e426a67033abd92d47da412725
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_SF_SURFACE_H
18#define ANDROID_SF_SURFACE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/RefBase.h>
24#include <utils/threads.h>
25
26#include <ui/PixelFormat.h>
27#include <ui/Region.h>
28#include <ui/egl/android_natives.h>
29
30#include <surfaceflinger/ISurface.h>
31#include <surfaceflinger/ISurfaceFlingerClient.h>
32
33namespace android {
34
35// ---------------------------------------------------------------------------
36
37class GraphicBufferMapper;
38class IOMX;
39class Rect;
40class Surface;
41class SurfaceComposerClient;
42class SharedClient;
43class SharedBufferClient;
44
45// ---------------------------------------------------------------------------
46
47class SurfaceControl : public RefBase
48{
49public:
50    static bool isValid(const sp<SurfaceControl>& surface) {
51        return (surface != 0) && surface->isValid();
52    }
53    bool isValid() {
54        return mToken>=0 && mClient!=0;
55    }
56    static bool isSameSurface(
57            const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
58
59    SurfaceID   ID() const      { return mToken; }
60    uint32_t    getFlags() const { return mFlags; }
61    uint32_t    getIdentity() const { return mIdentity; }
62
63    // release surface data from java
64    void        clear();
65
66    status_t    setLayer(int32_t layer);
67    status_t    setPosition(int32_t x, int32_t y);
68    status_t    setSize(uint32_t w, uint32_t h);
69    status_t    hide();
70    status_t    show(int32_t layer = -1);
71    status_t    freeze();
72    status_t    unfreeze();
73    status_t    setFlags(uint32_t flags, uint32_t mask);
74    status_t    setTransparentRegionHint(const Region& transparent);
75    status_t    setAlpha(float alpha=1.0f);
76    status_t    setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
77    status_t    setFreezeTint(uint32_t tint);
78
79    static status_t writeSurfaceToParcel(
80            const sp<SurfaceControl>& control, Parcel* parcel);
81
82    sp<Surface> getSurface() const;
83
84private:
85    // can't be copied
86    SurfaceControl& operator = (SurfaceControl& rhs);
87    SurfaceControl(const SurfaceControl& rhs);
88
89
90    friend class SurfaceComposerClient;
91
92    // camera and camcorder need access to the ISurface binder interface for preview
93    friend class Camera;
94    friend class MediaRecorder;
95    // mediaplayer needs access to ISurface for display
96    friend class MediaPlayer;
97    // for testing
98    friend class Test;
99    const sp<ISurface>& getISurface() const { return mSurface; }
100
101
102    friend class Surface;
103
104    SurfaceControl(
105            const sp<SurfaceComposerClient>& client,
106            const sp<ISurface>& surface,
107            const ISurfaceFlingerClient::surface_data_t& data,
108            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
109
110    ~SurfaceControl();
111
112    status_t validate() const;
113    void destroy();
114
115    sp<SurfaceComposerClient>   mClient;
116    sp<ISurface>                mSurface;
117    SurfaceID                   mToken;
118    uint32_t                    mIdentity;
119    uint32_t                    mWidth;
120    uint32_t                    mHeight;
121    PixelFormat                 mFormat;
122    uint32_t                    mFlags;
123    mutable Mutex               mLock;
124
125    mutable sp<Surface>         mSurfaceData;
126};
127
128// ---------------------------------------------------------------------------
129
130class Surface
131    : public EGLNativeBase<android_native_window_t, Surface, RefBase>
132{
133public:
134    struct SurfaceInfo {
135        uint32_t    w;
136        uint32_t    h;
137        uint32_t    s;
138        uint32_t    usage;
139        PixelFormat format;
140        void*       bits;
141        uint32_t    reserved[2];
142    };
143
144    Surface(const Parcel& data);
145
146    static bool isValid(const sp<Surface>& surface) {
147        return (surface != 0) && surface->isValid();
148    }
149
150    static bool isSameSurface(
151            const sp<Surface>& lhs, const sp<Surface>& rhs);
152
153    bool        isValid();
154    SurfaceID   ID() const          { return mToken; }
155    uint32_t    getFlags() const    { return mFlags; }
156    uint32_t    getIdentity() const { return mIdentity; }
157
158    // the lock/unlock APIs must be used from the same thread
159    status_t    lock(SurfaceInfo* info, bool blocking = true);
160    status_t    lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
161    status_t    unlockAndPost();
162
163    // setSwapRectangle() is intended to be used by GL ES clients
164    void        setSwapRectangle(const Rect& r);
165
166private:
167    // can't be copied
168    Surface& operator = (Surface& rhs);
169    Surface(const Surface& rhs);
170
171    Surface(const sp<SurfaceControl>& control);
172    void init();
173     ~Surface();
174
175    friend class SurfaceComposerClient;
176    friend class SurfaceControl;
177
178
179    // camera and camcorder need access to the ISurface binder interface for preview
180    friend class Camera;
181    friend class MediaRecorder;
182    // mediaplayer needs access to ISurface for display
183    friend class MediaPlayer;
184    friend class IOMX;
185    // this is just to be able to write some unit tests
186    friend class Test;
187
188    sp<SurfaceComposerClient> getClient() const;
189    sp<ISurface> getISurface() const;
190
191    status_t getBufferLocked(int index, int usage);
192
193           status_t validate() const;
194
195    inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
196    inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
197
198    static int setSwapInterval(android_native_window_t* window, int interval);
199    static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
200    static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
201    static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
202    static int query(android_native_window_t* window, int what, int* value);
203    static int perform(android_native_window_t* window, int operation, ...);
204
205    int dequeueBuffer(android_native_buffer_t** buffer);
206    int lockBuffer(android_native_buffer_t* buffer);
207    int queueBuffer(android_native_buffer_t* buffer);
208    int query(int what, int* value);
209    int perform(int operation, va_list args);
210
211    status_t dequeueBuffer(sp<GraphicBuffer>* buffer);
212
213
214    void setUsage(uint32_t reqUsage);
215    uint32_t getUsage() const;
216
217    // constants
218    sp<SurfaceComposerClient>   mClient;
219    sp<ISurface>                mSurface;
220    SurfaceID                   mToken;
221    uint32_t                    mIdentity;
222    PixelFormat                 mFormat;
223    uint32_t                    mFlags;
224    GraphicBufferMapper&        mBufferMapper;
225    SharedBufferClient*         mSharedBufferClient;
226
227    // protected by mSurfaceLock
228    Rect                        mSwapRectangle;
229    uint32_t                    mUsage;
230
231    // protected by mSurfaceLock. These are also used from lock/unlock
232    // but in that case, they must be called form the same thread.
233    sp<GraphicBuffer>           mBuffers[2];
234    mutable Region              mDirtyRegion;
235
236    // must be used from the lock/unlock thread
237    sp<GraphicBuffer>           mLockedBuffer;
238    sp<GraphicBuffer>           mPostedBuffer;
239    mutable Region              mOldDirtyRegion;
240    bool                        mNeedFullUpdate;
241
242    // query() must be called from dequeueBuffer() thread
243    uint32_t                    mWidth;
244    uint32_t                    mHeight;
245
246    // Inherently thread-safe
247    mutable Mutex               mSurfaceLock;
248    mutable Mutex               mApiLock;
249};
250
251}; // namespace android
252
253#endif // ANDROID_SF_SURFACE_H
254
255