blob: d783caff2a7f15bbe2cd7e4286fe25b7576291c8 [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
Jamie Gennisaca4e222010-07-15 17:29:15 -070023#include <utils/KeyedVector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/RefBase.h>
25#include <utils/threads.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
28#include <ui/Region.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070029#include <ui/egl/android_natives.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030
Mathias Agopian9cce3252010-02-09 17:46:37 -080031#include <surfaceflinger/ISurface.h>
Mathias Agopian7e27f052010-05-28 14:22:23 -070032#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080033
Mathias Agopiane4e8cf12010-04-12 16:22:15 -070034#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036namespace android {
37
38// ---------------------------------------------------------------------------
39
Mathias Agopiana138f892010-05-21 17:24:35 -070040class GraphicBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070041class GraphicBufferMapper;
Andreas Hubere1864312009-08-07 12:01:29 -070042class IOMX;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070044class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class SurfaceComposerClient;
Mathias Agopiancbb288b2009-09-07 16:32:45 -070046class SharedClient;
47class SharedBufferClient;
Mathias Agopiand4784a32010-05-27 19:41:15 -070048class SurfaceClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050// ---------------------------------------------------------------------------
51
Mathias Agopian62185b72009-04-16 16:19:50 -070052class SurfaceControl : public RefBase
53{
54public:
55 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070056 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070057 }
Mathias Agopian01b76682009-04-16 20:04:08 -070058 bool isValid() {
59 return mToken>=0 && mClient!=0;
60 }
61 static bool isSameSurface(
62 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070063
Mathias Agopian62185b72009-04-16 16:19:50 -070064 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
Jamie Gennis89c2dd22010-08-10 16:37:53 -070097 friend class CameraService;
Mathias Agopian62185b72009-04-16 16:19:50 -070098 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;
Dharmaray Kundargib6783162011-01-16 16:43:20 -0800103 // videoEditor preview classes
104 friend class VideoEditorPreviewController;
105
Mathias Agopian62185b72009-04-16 16:19:50 -0700106 const sp<ISurface>& getISurface() const { return mSurface; }
107
Mathias Agopian62185b72009-04-16 16:19:50 -0700108
109 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700110
111 SurfaceControl(
112 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700113 const sp<ISurface>& surface,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700114 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700115 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700116
117 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700118
Mathias Agopian963abad2009-11-13 15:26:29 -0800119 status_t validate() const;
Mathias Agopian62185b72009-04-16 16:19:50 -0700120 void destroy();
121
122 sp<SurfaceComposerClient> mClient;
123 sp<ISurface> mSurface;
124 SurfaceID mToken;
125 uint32_t mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700126 uint32_t mWidth;
127 uint32_t mHeight;
Mathias Agopian62185b72009-04-16 16:19:50 -0700128 PixelFormat mFormat;
129 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700130 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700131
132 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700133};
134
135// ---------------------------------------------------------------------------
136
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137class Surface
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700138 : public EGLNativeBase<ANativeWindow, Surface, RefBase>
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140public:
141 struct SurfaceInfo {
142 uint32_t w;
143 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700144 uint32_t s;
145 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 PixelFormat format;
147 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 uint32_t reserved[2];
149 };
150
Mathias Agopian579b3f82010-06-08 19:54:15 -0700151 static status_t writeToParcel(
152 const sp<Surface>& control, Parcel* parcel);
153
Jamie Gennisaca4e222010-07-15 17:29:15 -0700154 static sp<Surface> readFromParcel(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155
Mathias Agopian01b76682009-04-16 20:04:08 -0700156 static bool isValid(const sp<Surface>& surface) {
157 return (surface != 0) && surface->isValid();
158 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700159
Mathias Agopianba5972f2009-08-14 18:52:17 -0700160 bool isValid();
Mathias Agopianba5972f2009-08-14 18:52:17 -0700161 uint32_t getFlags() const { return mFlags; }
Mathias Agopian01b76682009-04-16 20:04:08 -0700162 uint32_t getIdentity() const { return mIdentity; }
163
Mathias Agopianba5972f2009-08-14 18:52:17 -0700164 // the lock/unlock APIs must be used from the same thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 status_t lock(SurfaceInfo* info, bool blocking = true);
166 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
167 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168
Mathias Agopian0926f502009-05-04 14:17:04 -0700169 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171
Mathias Agopianb2965332010-04-27 16:41:19 -0700172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173private:
Mathias Agopianb2965332010-04-27 16:41:19 -0700174 /*
175 * Android frameworks friends
176 * (eventually this should go away and be replaced by proper APIs)
177 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 // camera and camcorder need access to the ISurface binder interface for preview
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700179 friend class CameraService;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 friend class MediaRecorder;
Mathias Agopianb2965332010-04-27 16:41:19 -0700181 // MediaPlayer needs access to ISurface for display
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 friend class MediaPlayer;
Andreas Hubere1864312009-08-07 12:01:29 -0700183 friend class IOMX;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700184 friend class SoftwareRenderer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700185 // this is just to be able to write some unit tests
186 friend class Test;
Dharmaray Kundargib6783162011-01-16 16:43:20 -0800187 // videoEditor preview classes
188 friend class VideoEditorPreviewController;
189 friend class PreviewRenderer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700190
Mathias Agopianb2965332010-04-27 16:41:19 -0700191private:
192 friend class SurfaceComposerClient;
193 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194
Mathias Agopianb2965332010-04-27 16:41:19 -0700195 // can't be copied
196 Surface& operator = (Surface& rhs);
197 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198
Mathias Agopianb2965332010-04-27 16:41:19 -0700199 Surface(const sp<SurfaceControl>& control);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700200 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700201 ~Surface();
202
203
204 /*
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700205 * ANativeWindow hooks
Mathias Agopianb2965332010-04-27 16:41:19 -0700206 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700207 static int setSwapInterval(ANativeWindow* window, int interval);
208 static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
Mathias Agopian19957552010-10-01 16:22:41 -0700209 static int cancelBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700210 static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
211 static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
212 static int query(ANativeWindow* window, int what, int* value);
213 static int perform(ANativeWindow* window, int operation, ...);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214
215 int dequeueBuffer(android_native_buffer_t** buffer);
216 int lockBuffer(android_native_buffer_t* buffer);
217 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian19957552010-10-01 16:22:41 -0700218 int cancelBuffer(android_native_buffer_t* buffer);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700219 int query(int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700220 int perform(int operation, va_list args);
Mathias Agopian0926f502009-05-04 14:17:04 -0700221
Mathias Agopian55fa2512010-03-11 15:06:54 -0800222 void dispatch_setUsage(va_list args);
223 int dispatch_connect(va_list args);
224 int dispatch_disconnect(va_list args);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700225 int dispatch_crop(va_list args);
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700226 int dispatch_set_buffer_count(va_list args);
Mathias Agopiana138f892010-05-21 17:24:35 -0700227 int dispatch_set_buffers_geometry(va_list args);
Mathias Agopianb661d662010-08-19 17:01:19 -0700228 int dispatch_set_buffers_transform(va_list args);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229
Mathias Agopianba5972f2009-08-14 18:52:17 -0700230 void setUsage(uint32_t reqUsage);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800231 int connect(int api);
232 int disconnect(int api);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700233 int crop(Rect const* rect);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700234 int setBufferCount(int bufferCount);
Mathias Agopiana138f892010-05-21 17:24:35 -0700235 int setBuffersGeometry(int w, int h, int format);
Mathias Agopianb661d662010-08-19 17:01:19 -0700236 int setBuffersTransform(int transform);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800237
Mathias Agopianb2965332010-04-27 16:41:19 -0700238 /*
239 * private stuff...
240 */
241 void init();
Mathias Agopiana317f1b2011-01-14 11:04:34 -0800242 status_t validate(bool inCancelBuffer = false) const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700243 sp<ISurface> getISurface() const;
244
245 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
246 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
247
Mathias Agopiana138f892010-05-21 17:24:35 -0700248 status_t getBufferLocked(int index,
249 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb2965332010-04-27 16:41:19 -0700250 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
251
Mathias Agopiana138f892010-05-21 17:24:35 -0700252 int getConnectedApi() const;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700253
Mathias Agopiana138f892010-05-21 17:24:35 -0700254 bool needNewBuffer(int bufIdx,
255 uint32_t *pWidth, uint32_t *pHeight,
256 uint32_t *pFormat, uint32_t *pUsage) const;
257
Mathias Agopian455d18d2010-12-13 16:47:31 -0800258 static void cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700259
Mathias Agopiana138f892010-05-21 17:24:35 -0700260 class BufferInfo {
261 uint32_t mWidth;
262 uint32_t mHeight;
263 uint32_t mFormat;
264 uint32_t mUsage;
265 mutable uint32_t mDirty;
266 enum {
267 GEOMETRY = 0x01
268 };
269 public:
270 BufferInfo();
271 void set(uint32_t w, uint32_t h, uint32_t format);
272 void set(uint32_t usage);
273 void get(uint32_t *pWidth, uint32_t *pHeight,
274 uint32_t *pFormat, uint32_t *pUsage) const;
275 bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
276 };
277
Mathias Agopianba5972f2009-08-14 18:52:17 -0700278 // constants
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700279 GraphicBufferMapper& mBufferMapper;
280 SurfaceClient& mClient;
281 SharedBufferClient* mSharedBufferClient;
282 status_t mInitCheck;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 uint32_t mIdentity;
285 PixelFormat mFormat;
286 uint32_t mFlags;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700287
288 // protected by mSurfaceLock
289 Rect mSwapRectangle;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800290 int mConnected;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700291 Rect mNextBufferCrop;
Mathias Agopianb661d662010-08-19 17:01:19 -0700292 uint32_t mNextBufferTransform;
Mathias Agopiana138f892010-05-21 17:24:35 -0700293 BufferInfo mBufferInfo;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700294
295 // protected by mSurfaceLock. These are also used from lock/unlock
296 // but in that case, they must be called form the same thread.
Mathias Agopianba5972f2009-08-14 18:52:17 -0700297 mutable Region mDirtyRegion;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700298
299 // must be used from the lock/unlock thread
Mathias Agopian3330b202009-10-05 17:07:12 -0700300 sp<GraphicBuffer> mLockedBuffer;
301 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700302 mutable Region mOldDirtyRegion;
Mathias Agopian245e4d72010-04-21 15:24:11 -0700303 bool mReserved;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700304
Mathias Agopiana138f892010-05-21 17:24:35 -0700305 // only used from dequeueBuffer()
306 Vector< sp<GraphicBuffer> > mBuffers;
307
Mathias Agopianba5972f2009-08-14 18:52:17 -0700308 // query() must be called from dequeueBuffer() thread
309 uint32_t mWidth;
310 uint32_t mHeight;
311
312 // Inherently thread-safe
313 mutable Mutex mSurfaceLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700314 mutable Mutex mApiLock;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700315
316 // A cache of Surface objects that have been deserialized into this process.
317 static Mutex sCachedSurfacesLock;
318 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319};
320
321}; // namespace android
322
Mathias Agopian9cce3252010-02-09 17:46:37 -0800323#endif // ANDROID_SF_SURFACE_H