blob: e9bb1b3f473d5333525b856be389ef07a0df9154 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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_UI_SURFACE_H
18#define ANDROID_UI_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/ISurface.h>
27#include <ui/PixelFormat.h>
28#include <ui/Region.h>
29#include <ui/ISurfaceFlingerClient.h>
30
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <EGL/android_natives.h>
32
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
Mathias Agopian0926f502009-05-04 14:17:04 -070037class BufferMapper;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070039class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040class SurfaceComposerClient;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041struct per_client_cblk_t;
42struct layer_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044// ---------------------------------------------------------------------------
45
46class SurfaceBuffer
47 : public EGLNativeBase<
48 android_native_buffer_t,
49 SurfaceBuffer,
50 LightRefBase<SurfaceBuffer> >
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052public:
Mathias Agopiane71212b2009-05-05 00:37:46 -070053 status_t lock(uint32_t usage, void** vaddr);
54 status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -070055 status_t unlock();
56
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057protected:
58 SurfaceBuffer();
59 SurfaceBuffer(const Parcel& reply);
60 virtual ~SurfaceBuffer();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070061 bool mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopian0926f502009-05-04 14:17:04 -070063 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
64 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
65
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066private:
Mathias Agopian0926f502009-05-04 14:17:04 -070067 friend class Surface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068 friend class BpSurface;
69 friend class BnSurface;
70 friend class LightRefBase<SurfaceBuffer>;
71
72 SurfaceBuffer& operator = (const SurfaceBuffer& rhs);
73 const SurfaceBuffer& operator = (const SurfaceBuffer& rhs) const;
74
75 static status_t writeToParcel(Parcel* reply,
76 android_native_buffer_t const* buffer);
77
Mathias Agopian0926f502009-05-04 14:17:04 -070078 BufferMapper& mBufferMapper;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079};
80
81// ---------------------------------------------------------------------------
Mathias Agopian01b76682009-04-16 20:04:08 -070082class Surface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083
Mathias Agopian62185b72009-04-16 16:19:50 -070084class SurfaceControl : public RefBase
85{
86public:
87 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070088 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070089 }
Mathias Agopian01b76682009-04-16 20:04:08 -070090 bool isValid() {
91 return mToken>=0 && mClient!=0;
92 }
93 static bool isSameSurface(
94 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070095
96 SurfaceID ID() const { return mToken; }
97 uint32_t getFlags() const { return mFlags; }
98 uint32_t getIdentity() const { return mIdentity; }
99
100 // release surface data from java
101 void clear();
102
Mathias Agopian62185b72009-04-16 16:19:50 -0700103 status_t setLayer(int32_t layer);
104 status_t setPosition(int32_t x, int32_t y);
105 status_t setSize(uint32_t w, uint32_t h);
106 status_t hide();
107 status_t show(int32_t layer = -1);
108 status_t freeze();
109 status_t unfreeze();
110 status_t setFlags(uint32_t flags, uint32_t mask);
111 status_t setTransparentRegionHint(const Region& transparent);
112 status_t setAlpha(float alpha=1.0f);
113 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
114 status_t setFreezeTint(uint32_t tint);
115
Mathias Agopian01b76682009-04-16 20:04:08 -0700116 static status_t writeSurfaceToParcel(
117 const sp<SurfaceControl>& control, Parcel* parcel);
118
119 sp<Surface> getSurface() const;
120
Mathias Agopian62185b72009-04-16 16:19:50 -0700121private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700122 // can't be copied
123 SurfaceControl& operator = (SurfaceControl& rhs);
124 SurfaceControl(const SurfaceControl& rhs);
125
126
Mathias Agopian62185b72009-04-16 16:19:50 -0700127 friend class SurfaceComposerClient;
128
129 // camera and camcorder need access to the ISurface binder interface for preview
130 friend class Camera;
131 friend class MediaRecorder;
132 // mediaplayer needs access to ISurface for display
133 friend class MediaPlayer;
Mathias Agopian01b76682009-04-16 20:04:08 -0700134 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -0700135 friend class Test;
136 const sp<ISurface>& getISurface() const { return mSurface; }
137
Mathias Agopian62185b72009-04-16 16:19:50 -0700138
139 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700140
141 SurfaceControl(
142 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700143 const sp<ISurface>& surface,
144 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700145 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700146
147 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700148
Mathias Agopian62185b72009-04-16 16:19:50 -0700149 status_t validate(per_client_cblk_t const* cblk) const;
150 void destroy();
151
152 sp<SurfaceComposerClient> mClient;
153 sp<ISurface> mSurface;
154 SurfaceID mToken;
155 uint32_t mIdentity;
156 PixelFormat mFormat;
157 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700158 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700159
160 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700161};
162
163// ---------------------------------------------------------------------------
164
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700165class Surface
166 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
167{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168public:
169 struct SurfaceInfo {
170 uint32_t w;
171 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700172 uint32_t s;
173 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 PixelFormat format;
175 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 uint32_t reserved[2];
177 };
178
Mathias Agopian01b76682009-04-16 20:04:08 -0700179 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180
Mathias Agopian01b76682009-04-16 20:04:08 -0700181 static bool isValid(const sp<Surface>& surface) {
182 return (surface != 0) && surface->isValid();
183 }
184 bool isValid() {
185 return mToken>=0 && mClient!=0;
186 }
187 static bool isSameSurface(
188 const sp<Surface>& lhs, const sp<Surface>& rhs);
189 SurfaceID ID() const { return mToken; }
190 uint32_t getFlags() const { return mFlags; }
191 uint32_t getIdentity() const { return mIdentity; }
192
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 status_t lock(SurfaceInfo* info, bool blocking = true);
195 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
196 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197
Mathias Agopian0926f502009-05-04 14:17:04 -0700198 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700202 // can't be copied
203 Surface& operator = (Surface& rhs);
204 Surface(const Surface& rhs);
205
206 Surface(const sp<SurfaceControl>& control);
207 void init();
208 ~Surface();
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 friend class SurfaceComposerClient;
Mathias Agopian01b76682009-04-16 20:04:08 -0700211 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212
213 // camera and camcorder need access to the ISurface binder interface for preview
214 friend class Camera;
215 friend class MediaRecorder;
216 // mediaplayer needs access to ISurface for display
217 friend class MediaPlayer;
218 friend class Test;
219 const sp<ISurface>& getISurface() const { return mSurface; }
220
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 status_t getBufferLocked(int index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222
223 status_t validate(per_client_cblk_t const* cblk) const;
224 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225
Mathias Agopian0926f502009-05-04 14:17:04 -0700226 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
227 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
231 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
232 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
233
234 int dequeueBuffer(android_native_buffer_t** buffer);
235 int lockBuffer(android_native_buffer_t* buffer);
236 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian0926f502009-05-04 14:17:04 -0700237
238 status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
239 status_t lockBuffer(const sp<SurfaceBuffer>& buffer);
240 status_t queueBuffer(const sp<SurfaceBuffer>& buffer);
241
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700242
243 alloc_device_t* mAllocDevice;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 sp<SurfaceComposerClient> mClient;
245 sp<ISurface> mSurface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246 sp<SurfaceBuffer> mBuffers[2];
Mathias Agopian0926f502009-05-04 14:17:04 -0700247 sp<SurfaceBuffer> mLockedBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 SurfaceID mToken;
249 uint32_t mIdentity;
250 PixelFormat mFormat;
251 uint32_t mFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 mutable Region mDirtyRegion;
Mathias Agopian0926f502009-05-04 14:17:04 -0700253 mutable Region mOldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 mutable uint8_t mBackbufferIndex;
255 mutable Mutex mSurfaceLock;
Mathias Agopian0926f502009-05-04 14:17:04 -0700256 Rect mSwapRectangle;
257 BufferMapper& mBufferMapper;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258};
259
260}; // namespace android
261
262#endif // ANDROID_UI_SURFACE_H
263