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