blob: 118fb8317b26210cfd39ee12a8dbabe860a9dcb9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopianac2523b2009-05-05 18:11:11 -070031#include <ui/egl/android_natives.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
Mathias Agopiandff8e582009-05-04 14:17:04 -070037class BufferMapper;
Andreas Huberccf8b942009-08-07 12:01:29 -070038class IOMX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039class Rect;
Mathias Agopiandff8e582009-05-04 14:17:04 -070040class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041class SurfaceComposerClient;
Mathias Agopian9779b222009-09-07 16:32:45 -070042class SharedClient;
43class SharedBufferClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Mathias Agopian1473f462009-04-10 14:24:30 -070045// ---------------------------------------------------------------------------
46
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070047class SurfaceControl : public RefBase
48{
49public:
50 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070051 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070052 }
Mathias Agopian17f638b2009-04-16 20:04:08 -070053 bool isValid() {
54 return mToken>=0 && mClient!=0;
55 }
56 static bool isSameSurface(
57 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070058
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
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070066 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
Mathias Agopian17f638b2009-04-16 20:04:08 -070079 static status_t writeSurfaceToParcel(
80 const sp<SurfaceControl>& control, Parcel* parcel);
81
82 sp<Surface> getSurface() const;
83
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070084private:
Mathias Agopian17f638b2009-04-16 20:04:08 -070085 // can't be copied
86 SurfaceControl& operator = (SurfaceControl& rhs);
87 SurfaceControl(const SurfaceControl& rhs);
88
89
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070090 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;
Mathias Agopian17f638b2009-04-16 20:04:08 -070097 // for testing
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070098 friend class Test;
99 const sp<ISurface>& getISurface() const { return mSurface; }
100
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700101
102 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700103
104 SurfaceControl(
105 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700106 const sp<ISurface>& surface,
107 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700109
110 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700111
Mathias Agopian9779b222009-09-07 16:32:45 -0700112 status_t validate(SharedClient const* cblk) const;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700113 void destroy();
114
115 sp<SurfaceComposerClient> mClient;
116 sp<ISurface> mSurface;
117 SurfaceID mToken;
118 uint32_t mIdentity;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700119 uint32_t mWidth;
120 uint32_t mHeight;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700121 PixelFormat mFormat;
122 uint32_t mFlags;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700123 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700124
125 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700126};
127
128// ---------------------------------------------------------------------------
129
Mathias Agopian1473f462009-04-10 14:24:30 -0700130class Surface
131 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
132{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133public:
134 struct SurfaceInfo {
135 uint32_t w;
136 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700137 uint32_t s;
138 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 PixelFormat format;
140 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 uint32_t reserved[2];
142 };
143
Mathias Agopian17f638b2009-04-16 20:04:08 -0700144 Surface(const Parcel& data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Mathias Agopian17f638b2009-04-16 20:04:08 -0700146 static bool isValid(const sp<Surface>& surface) {
147 return (surface != 0) && surface->isValid();
148 }
Mathias Agopian321abdb2009-08-14 18:52:17 -0700149
Mathias Agopian17f638b2009-04-16 20:04:08 -0700150 static bool isSameSurface(
151 const sp<Surface>& lhs, const sp<Surface>& rhs);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700152
153 bool isValid();
154 SurfaceID ID() const { return mToken; }
155 uint32_t getFlags() const { return mFlags; }
Mathias Agopian17f638b2009-04-16 20:04:08 -0700156 uint32_t getIdentity() const { return mIdentity; }
157
Mathias Agopian321abdb2009-08-14 18:52:17 -0700158 // the lock/unlock APIs must be used from the same thread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 status_t lock(SurfaceInfo* info, bool blocking = true);
160 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
161 status_t unlockAndPost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Mathias Agopiandff8e582009-05-04 14:17:04 -0700163 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 void setSwapRectangle(const Rect& r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166private:
Mathias Agopian17f638b2009-04-16 20:04:08 -0700167 // 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 friend class SurfaceComposerClient;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700176 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
Mathias Agopian321abdb2009-08-14 18:52:17 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 // 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;
Andreas Huberccf8b942009-08-07 12:01:29 -0700184 friend class IOMX;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700185 // 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Mathias Agopian5cec4742009-08-11 22:34:02 -0700191 status_t getBufferLocked(int index, int usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700192
Mathias Agopian9779b222009-09-07 16:32:45 -0700193 status_t validate(SharedClient const* cblk) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Mathias Agopiandff8e582009-05-04 14:17:04 -0700195 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
196 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian1473f462009-04-10 14:24:30 -0700197
Mathias Agopian1473f462009-04-10 14:24:30 -0700198 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian1473f462009-04-10 14:24:30 -0700199 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);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700202 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700203 static int perform(android_native_window_t* window, int operation, ...);
Mathias Agopian1473f462009-04-10 14:24:30 -0700204
205 int dequeueBuffer(android_native_buffer_t** buffer);
206 int lockBuffer(android_native_buffer_t* buffer);
207 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700208 int query(int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700209 int perform(int operation, va_list args);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700210
211 status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700212
Mathias Agopian1473f462009-04-10 14:24:30 -0700213
Mathias Agopian321abdb2009-08-14 18:52:17 -0700214 void setUsage(uint32_t reqUsage);
Mathias Agopian8f17a762009-09-15 18:57:06 -0700215 bool getUsage(uint32_t* usage);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700216
217 // constants
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 sp<SurfaceComposerClient> mClient;
219 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 SurfaceID mToken;
221 uint32_t mIdentity;
222 PixelFormat mFormat;
223 uint32_t mFlags;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700224 BufferMapper& mBufferMapper;
Mathias Agopian9779b222009-09-07 16:32:45 -0700225 SharedBufferClient* mSharedBufferClient;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700226
227 // protected by mSurfaceLock
228 Rect mSwapRectangle;
229 uint32_t mUsage;
Mathias Agopian8f17a762009-09-15 18:57:06 -0700230 int32_t mUsageChanged;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700231
232 // protected by mSurfaceLock. These are also used from lock/unlock
233 // but in that case, they must be called form the same thread.
234 sp<SurfaceBuffer> mBuffers[2];
235 mutable Region mDirtyRegion;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700236
237 // must be used from the lock/unlock thread
238 sp<SurfaceBuffer> mLockedBuffer;
Mathias Agopian9779b222009-09-07 16:32:45 -0700239 sp<SurfaceBuffer> mPostedBuffer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700240 mutable Region mOldDirtyRegion;
Mathias Agopian9779b222009-09-07 16:32:45 -0700241 bool mNeedFullUpdate;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700242
243 // query() must be called from dequeueBuffer() thread
244 uint32_t mWidth;
245 uint32_t mHeight;
246
247 // Inherently thread-safe
248 mutable Mutex mSurfaceLock;
Mathias Agopian9779b222009-09-07 16:32:45 -0700249 mutable Mutex mApiLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250};
251
252}; // namespace android
253
254#endif // ANDROID_UI_SURFACE_H
255