blob: ac01ce57c267823fef38695151821b29df4be2e6 [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
Mathias Agopian9cce3252010-02-09 17:46:37 -080017#ifndef ANDROID_SF_SURFACE_H
18#define ANDROID_SF_SURFACE_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/RefBase.h>
24#include <utils/threads.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <ui/PixelFormat.h>
27#include <ui/Region.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070028#include <ui/egl/android_natives.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029
Mathias Agopian9cce3252010-02-09 17:46:37 -080030#include <surfaceflinger/ISurface.h>
Mathias Agopian7e27f052010-05-28 14:22:23 -070031#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080032
Mathias Agopiane4e8cf12010-04-12 16:22:15 -070033#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
34
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035namespace android {
36
37// ---------------------------------------------------------------------------
38
Mathias Agopiana138f892010-05-21 17:24:35 -070039class GraphicBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070040class GraphicBufferMapper;
Andreas Hubere1864312009-08-07 12:01:29 -070041class IOMX;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070043class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044class SurfaceComposerClient;
Mathias Agopiancbb288b2009-09-07 16:32:45 -070045class SharedClient;
46class SharedBufferClient;
Mathias Agopiand4784a32010-05-27 19:41:15 -070047class SurfaceClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049// ---------------------------------------------------------------------------
50
Mathias Agopian62185b72009-04-16 16:19:50 -070051class SurfaceControl : public RefBase
52{
53public:
54 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070055 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070056 }
Mathias Agopian01b76682009-04-16 20:04:08 -070057 bool isValid() {
58 return mToken>=0 && mClient!=0;
59 }
60 static bool isSameSurface(
61 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070062
63 SurfaceID ID() const { return mToken; }
64 uint32_t getFlags() const { return mFlags; }
65 uint32_t getIdentity() const { return mIdentity; }
66
67 // release surface data from java
68 void clear();
69
Mathias Agopian62185b72009-04-16 16:19:50 -070070 status_t setLayer(int32_t layer);
71 status_t setPosition(int32_t x, int32_t y);
72 status_t setSize(uint32_t w, uint32_t h);
73 status_t hide();
74 status_t show(int32_t layer = -1);
75 status_t freeze();
76 status_t unfreeze();
77 status_t setFlags(uint32_t flags, uint32_t mask);
78 status_t setTransparentRegionHint(const Region& transparent);
79 status_t setAlpha(float alpha=1.0f);
80 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
81 status_t setFreezeTint(uint32_t tint);
82
Mathias Agopian01b76682009-04-16 20:04:08 -070083 static status_t writeSurfaceToParcel(
84 const sp<SurfaceControl>& control, Parcel* parcel);
85
86 sp<Surface> getSurface() const;
87
Mathias Agopian62185b72009-04-16 16:19:50 -070088private:
Mathias Agopian01b76682009-04-16 20:04:08 -070089 // can't be copied
90 SurfaceControl& operator = (SurfaceControl& rhs);
91 SurfaceControl(const SurfaceControl& rhs);
92
93
Mathias Agopian62185b72009-04-16 16:19:50 -070094 friend class SurfaceComposerClient;
95
96 // camera and camcorder need access to the ISurface binder interface for preview
97 friend class Camera;
98 friend class MediaRecorder;
99 // mediaplayer needs access to ISurface for display
100 friend class MediaPlayer;
Mathias Agopian01b76682009-04-16 20:04:08 -0700101 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -0700102 friend class Test;
103 const sp<ISurface>& getISurface() const { return mSurface; }
104
Mathias Agopian62185b72009-04-16 16:19:50 -0700105
106 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700107
108 SurfaceControl(
109 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700110 const sp<ISurface>& surface,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700111 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700112 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700113
114 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700115
Mathias Agopian963abad2009-11-13 15:26:29 -0800116 status_t validate() const;
Mathias Agopian62185b72009-04-16 16:19:50 -0700117 void destroy();
118
119 sp<SurfaceComposerClient> mClient;
120 sp<ISurface> mSurface;
121 SurfaceID mToken;
122 uint32_t mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700123 uint32_t mWidth;
124 uint32_t mHeight;
Mathias Agopian62185b72009-04-16 16:19:50 -0700125 PixelFormat mFormat;
126 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700127 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700128
129 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700130};
131
132// ---------------------------------------------------------------------------
133
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134class Surface
135 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
136{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137public:
138 struct SurfaceInfo {
139 uint32_t w;
140 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 uint32_t s;
142 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 PixelFormat format;
144 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 uint32_t reserved[2];
146 };
147
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700148 static sp<Surface> readFromParcel(
149 const Parcel& data, const sp<Surface>& other);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150
Mathias Agopian01b76682009-04-16 20:04:08 -0700151 static bool isValid(const sp<Surface>& surface) {
152 return (surface != 0) && surface->isValid();
153 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700154
Mathias Agopianba5972f2009-08-14 18:52:17 -0700155 bool isValid();
156 SurfaceID ID() const { return mToken; }
157 uint32_t getFlags() const { return mFlags; }
Mathias Agopian01b76682009-04-16 20:04:08 -0700158 uint32_t getIdentity() const { return mIdentity; }
159
Mathias Agopianba5972f2009-08-14 18:52:17 -0700160 // the lock/unlock APIs must be used from the same thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 status_t lock(SurfaceInfo* info, bool blocking = true);
162 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
163 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
Mathias Agopian0926f502009-05-04 14:17:04 -0700165 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
Mathias Agopianb2965332010-04-27 16:41:19 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169private:
Mathias Agopianb2965332010-04-27 16:41:19 -0700170 /*
171 * Android frameworks friends
172 * (eventually this should go away and be replaced by proper APIs)
173 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 // camera and camcorder need access to the ISurface binder interface for preview
175 friend class Camera;
176 friend class MediaRecorder;
Mathias Agopianb2965332010-04-27 16:41:19 -0700177 // MediaPlayer needs access to ISurface for display
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 friend class MediaPlayer;
Andreas Hubere1864312009-08-07 12:01:29 -0700179 friend class IOMX;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700180 // this is just to be able to write some unit tests
181 friend class Test;
182
Mathias Agopianb2965332010-04-27 16:41:19 -0700183private:
184 friend class SurfaceComposerClient;
185 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186
Mathias Agopianb2965332010-04-27 16:41:19 -0700187 // can't be copied
188 Surface& operator = (Surface& rhs);
189 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190
Mathias Agopianb2965332010-04-27 16:41:19 -0700191 Surface(const sp<SurfaceControl>& control);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700192 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700193 ~Surface();
194
195
196 /*
197 * android_native_window_t hooks
198 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
201 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
202 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700203 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700204 static int perform(android_native_window_t* window, int operation, ...);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205
206 int dequeueBuffer(android_native_buffer_t** buffer);
207 int lockBuffer(android_native_buffer_t* buffer);
208 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700209 int query(int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700210 int perform(int operation, va_list args);
Mathias Agopian0926f502009-05-04 14:17:04 -0700211
Mathias Agopian55fa2512010-03-11 15:06:54 -0800212 void dispatch_setUsage(va_list args);
213 int dispatch_connect(va_list args);
214 int dispatch_disconnect(va_list args);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700215 int dispatch_crop(va_list args);
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700216 int dispatch_set_buffer_count(va_list args);
Mathias Agopiana138f892010-05-21 17:24:35 -0700217 int dispatch_set_buffers_geometry(va_list args);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218
Mathias Agopianba5972f2009-08-14 18:52:17 -0700219 void setUsage(uint32_t reqUsage);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800220 int connect(int api);
221 int disconnect(int api);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700222 int crop(Rect const* rect);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700223 int setBufferCount(int bufferCount);
Mathias Agopiana138f892010-05-21 17:24:35 -0700224 int setBuffersGeometry(int w, int h, int format);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800225
Mathias Agopianb2965332010-04-27 16:41:19 -0700226 /*
227 * private stuff...
228 */
229 void init();
230 status_t validate() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700231 sp<ISurface> getISurface() const;
232
233 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
234 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
235
Mathias Agopiana138f892010-05-21 17:24:35 -0700236 status_t getBufferLocked(int index,
237 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb2965332010-04-27 16:41:19 -0700238 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
239
Mathias Agopiana138f892010-05-21 17:24:35 -0700240 int getConnectedApi() const;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700241
Mathias Agopiana138f892010-05-21 17:24:35 -0700242 bool needNewBuffer(int bufIdx,
243 uint32_t *pWidth, uint32_t *pHeight,
244 uint32_t *pFormat, uint32_t *pUsage) const;
245
246 class BufferInfo {
247 uint32_t mWidth;
248 uint32_t mHeight;
249 uint32_t mFormat;
250 uint32_t mUsage;
251 mutable uint32_t mDirty;
252 enum {
253 GEOMETRY = 0x01
254 };
255 public:
256 BufferInfo();
257 void set(uint32_t w, uint32_t h, uint32_t format);
258 void set(uint32_t usage);
259 void get(uint32_t *pWidth, uint32_t *pHeight,
260 uint32_t *pFormat, uint32_t *pUsage) const;
261 bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
262 };
263
Mathias Agopianba5972f2009-08-14 18:52:17 -0700264 // constants
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700265 GraphicBufferMapper& mBufferMapper;
266 SurfaceClient& mClient;
267 SharedBufferClient* mSharedBufferClient;
268 status_t mInitCheck;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 SurfaceID mToken;
271 uint32_t mIdentity;
272 PixelFormat mFormat;
273 uint32_t mFlags;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700274
275 // protected by mSurfaceLock
276 Rect mSwapRectangle;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800277 int mConnected;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700278 Rect mNextBufferCrop;
Mathias Agopiana138f892010-05-21 17:24:35 -0700279 BufferInfo mBufferInfo;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700280
281 // protected by mSurfaceLock. These are also used from lock/unlock
282 // but in that case, they must be called form the same thread.
Mathias Agopianba5972f2009-08-14 18:52:17 -0700283 mutable Region mDirtyRegion;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700284
285 // must be used from the lock/unlock thread
Mathias Agopian3330b202009-10-05 17:07:12 -0700286 sp<GraphicBuffer> mLockedBuffer;
287 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700288 mutable Region mOldDirtyRegion;
Mathias Agopian245e4d72010-04-21 15:24:11 -0700289 bool mReserved;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700290
Mathias Agopiana138f892010-05-21 17:24:35 -0700291 // only used from dequeueBuffer()
292 Vector< sp<GraphicBuffer> > mBuffers;
293
Mathias Agopianba5972f2009-08-14 18:52:17 -0700294 // query() must be called from dequeueBuffer() thread
295 uint32_t mWidth;
296 uint32_t mHeight;
297
298 // Inherently thread-safe
299 mutable Mutex mSurfaceLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700300 mutable Mutex mApiLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301};
302
303}; // namespace android
304
Mathias Agopian9cce3252010-02-09 17:46:37 -0800305#endif // ANDROID_SF_SURFACE_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306