blob: ab30f45dd5418f1d0a61557b35d88c02c0a89ce6 [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
Mathias Agopian000479f2010-02-09 17:46:37 -080017#ifndef ANDROID_SF_SURFACE_H
18#define ANDROID_SF_SURFACE_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20#include <stdint.h>
21#include <sys/types.h>
22
Jamie Gennis5ee65f02010-07-15 17:29:15 -070023#include <utils/KeyedVector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/RefBase.h>
25#include <utils/threads.h>
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
28#include <ui/Region.h>
Mathias Agopianac2523b2009-05-05 18:11:11 -070029#include <ui/egl/android_natives.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070030
Mathias Agopian000479f2010-02-09 17:46:37 -080031#include <surfaceflinger/ISurface.h>
Mathias Agopian770492c2010-05-28 14:22:23 -070032#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080033
Mathias Agopian8b138322010-04-12 16:22:15 -070034#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036namespace android {
37
38// ---------------------------------------------------------------------------
39
Mathias Agopian2be352a2010-05-21 17:24:35 -070040class GraphicBuffer;
Mathias Agopian6950e422009-10-05 17:07:12 -070041class GraphicBufferMapper;
Andreas Huberccf8b942009-08-07 12:01:29 -070042class IOMX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043class Rect;
Mathias Agopiandff8e582009-05-04 14:17:04 -070044class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045class SurfaceComposerClient;
Mathias Agopian9779b222009-09-07 16:32:45 -070046class SharedClient;
47class SharedBufferClient;
Mathias Agopianc7b388c2010-05-27 19:41:15 -070048class SurfaceClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Mathias Agopian1473f462009-04-10 14:24:30 -070050// ---------------------------------------------------------------------------
51
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070052class SurfaceControl : public RefBase
53{
54public:
55 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070056 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070057 }
Mathias Agopian17f638b2009-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 Agopian6d2c0bc2009-04-16 16:19:50 -070063
Mathias Agopian6d2c0bc2009-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 Agopian6d2c0bc2009-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 Agopian17f638b2009-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 Agopian6d2c0bc2009-04-16 16:19:50 -070088private:
Mathias Agopian17f638b2009-04-16 20:04:08 -070089 // can't be copied
90 SurfaceControl& operator = (SurfaceControl& rhs);
91 SurfaceControl(const SurfaceControl& rhs);
92
93
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070094 friend class SurfaceComposerClient;
95
96 // camera and camcorder need access to the ISurface binder interface for preview
Jamie Gennis85cfdd02010-08-10 16:37:53 -070097 friend class CameraService;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070098 friend class MediaRecorder;
99 // mediaplayer needs access to ISurface for display
100 friend class MediaPlayer;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700101 // for testing
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700102 friend class Test;
Dharmaray Kundargi3b3cddc2011-01-16 16:43:20 -0800103 // videoEditor preview classes
104 friend class VideoEditorPreviewController;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700105 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700106
107 SurfaceControl(
108 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700109 const sp<ISurface>& surface,
Mathias Agopian770492c2010-05-28 14:22:23 -0700110 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700111 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700112
113 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700114
Mathias Agopian18e02602009-11-13 15:26:29 -0800115 status_t validate() const;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700116 void destroy();
117
118 sp<SurfaceComposerClient> mClient;
119 sp<ISurface> mSurface;
120 SurfaceID mToken;
121 uint32_t mIdentity;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700122 uint32_t mWidth;
123 uint32_t mHeight;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700124 PixelFormat mFormat;
125 uint32_t mFlags;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700126 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700127
128 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700129};
130
131// ---------------------------------------------------------------------------
132
Mathias Agopian1473f462009-04-10 14:24:30 -0700133class Surface
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700134 : public EGLNativeBase<ANativeWindow, Surface, RefBase>
Mathias Agopian1473f462009-04-10 14:24:30 -0700135{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136public:
137 struct SurfaceInfo {
138 uint32_t w;
139 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700140 uint32_t s;
141 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 PixelFormat format;
143 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 uint32_t reserved[2];
145 };
146
Mathias Agopian5e140102010-06-08 19:54:15 -0700147 static status_t writeToParcel(
148 const sp<Surface>& control, Parcel* parcel);
149
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700150 static sp<Surface> readFromParcel(const Parcel& data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
Mathias Agopian17f638b2009-04-16 20:04:08 -0700152 static bool isValid(const sp<Surface>& surface) {
153 return (surface != 0) && surface->isValid();
154 }
Mathias Agopian321abdb2009-08-14 18:52:17 -0700155
Mathias Agopian321abdb2009-08-14 18:52:17 -0700156 bool isValid();
Mathias Agopian321abdb2009-08-14 18:52:17 -0700157 uint32_t getFlags() const { return mFlags; }
Mathias Agopian17f638b2009-04-16 20:04:08 -0700158 uint32_t getIdentity() const { return mIdentity; }
159
Mathias Agopian321abdb2009-08-14 18:52:17 -0700160 // the lock/unlock APIs must be used from the same thread
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800164
Mathias Agopiandff8e582009-05-04 14:17:04 -0700165 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 void setSwapRectangle(const Rect& r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Mathias Agopianc8a04b52011-04-05 15:44:20 -0700168 sp<IBinder> asBinder() const;
Mathias Agopianf590f702010-04-27 16:41:19 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170private:
Mathias Agopianf590f702010-04-27 16:41:19 -0700171 /*
172 * Android frameworks friends
173 * (eventually this should go away and be replaced by proper APIs)
174 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 // camera and camcorder need access to the ISurface binder interface for preview
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700176 friend class CameraService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 friend class MediaRecorder;
Mathias Agopianf590f702010-04-27 16:41:19 -0700178 // MediaPlayer needs access to ISurface for display
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 friend class MediaPlayer;
Andreas Huberccf8b942009-08-07 12:01:29 -0700180 friend class IOMX;
Andreas Hubere3c01832010-08-16 08:49:37 -0700181 friend class SoftwareRenderer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700182 // this is just to be able to write some unit tests
183 friend class Test;
Dharmaray Kundargi3b3cddc2011-01-16 16:43:20 -0800184 // videoEditor preview classes
185 friend class VideoEditorPreviewController;
186 friend class PreviewRenderer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700187
Mathias Agopianf590f702010-04-27 16:41:19 -0700188private:
189 friend class SurfaceComposerClient;
190 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Mathias Agopianf590f702010-04-27 16:41:19 -0700192 // can't be copied
193 Surface& operator = (Surface& rhs);
194 Surface(const Surface& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
Mathias Agopianf590f702010-04-27 16:41:19 -0700196 Surface(const sp<SurfaceControl>& control);
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700197 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianf590f702010-04-27 16:41:19 -0700198 ~Surface();
199
200
201 /*
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700202 * ANativeWindow hooks
Mathias Agopianf590f702010-04-27 16:41:19 -0700203 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700204 static int setSwapInterval(ANativeWindow* window, int interval);
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700205 static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
206 static int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
207 static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
208 static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700209 static int query(const ANativeWindow* window, int what, int* value);
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700210 static int perform(ANativeWindow* window, int operation, ...);
Mathias Agopian1473f462009-04-10 14:24:30 -0700211
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700212 int dequeueBuffer(ANativeWindowBuffer** buffer);
213 int lockBuffer(ANativeWindowBuffer* buffer);
214 int queueBuffer(ANativeWindowBuffer* buffer);
215 int cancelBuffer(ANativeWindowBuffer* buffer);
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700216 int query(int what, int* value) const;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700217 int perform(int operation, va_list args);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700218
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800219 void dispatch_setUsage(va_list args);
220 int dispatch_connect(va_list args);
221 int dispatch_disconnect(va_list args);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700222 int dispatch_crop(va_list args);
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700223 int dispatch_set_buffer_count(va_list args);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700224 int dispatch_set_buffers_geometry(va_list args);
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700225 int dispatch_set_buffers_transform(va_list args);
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800226 int dispatch_set_buffers_timestamp(va_list args);
227
Mathias Agopian321abdb2009-08-14 18:52:17 -0700228 void setUsage(uint32_t reqUsage);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800229 int connect(int api);
230 int disconnect(int api);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700231 int crop(Rect const* rect);
Mathias Agopian59751db2010-05-07 15:58:44 -0700232 int setBufferCount(int bufferCount);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700233 int setBuffersGeometry(int w, int h, int format);
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700234 int setBuffersTransform(int transform);
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800235 int setBuffersTimestamp(int64_t timestamp);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800236
Mathias Agopianf590f702010-04-27 16:41:19 -0700237 /*
238 * private stuff...
239 */
240 void init();
Mathias Agopianafc724b2011-01-14 11:04:34 -0800241 status_t validate(bool inCancelBuffer = false) const;
Mathias Agopianf590f702010-04-27 16:41:19 -0700242
Jamie Gennis96dcc972011-02-27 14:10:20 -0800243 // When the buffer pool is a fixed size we want to make sure SurfaceFlinger
244 // won't stall clients, so we require an extra buffer.
245 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
246
Mathias Agopianf590f702010-04-27 16:41:19 -0700247 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
248 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
249
Mathias Agopian2be352a2010-05-21 17:24:35 -0700250 status_t getBufferLocked(int index,
251 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianf590f702010-04-27 16:41:19 -0700252 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
253
Mathias Agopian2be352a2010-05-21 17:24:35 -0700254 int getConnectedApi() const;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700255
Mathias Agopian2be352a2010-05-21 17:24:35 -0700256 bool needNewBuffer(int bufIdx,
257 uint32_t *pWidth, uint32_t *pHeight,
258 uint32_t *pFormat, uint32_t *pUsage) const;
259
Mathias Agopian06f9ebf2010-12-13 16:47:31 -0800260 static void cleanCachedSurfacesLocked();
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700261
Mathias Agopian2be352a2010-05-21 17:24:35 -0700262 class BufferInfo {
263 uint32_t mWidth;
264 uint32_t mHeight;
265 uint32_t mFormat;
266 uint32_t mUsage;
267 mutable uint32_t mDirty;
268 enum {
269 GEOMETRY = 0x01
270 };
271 public:
272 BufferInfo();
273 void set(uint32_t w, uint32_t h, uint32_t format);
274 void set(uint32_t usage);
275 void get(uint32_t *pWidth, uint32_t *pHeight,
276 uint32_t *pFormat, uint32_t *pUsage) const;
277 bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
278 };
279
Mathias Agopian321abdb2009-08-14 18:52:17 -0700280 // constants
Mathias Agopian7623da42010-06-01 15:12:58 -0700281 GraphicBufferMapper& mBufferMapper;
282 SurfaceClient& mClient;
283 SharedBufferClient* mSharedBufferClient;
284 status_t mInitCheck;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 uint32_t mIdentity;
287 PixelFormat mFormat;
288 uint32_t mFlags;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700289
290 // protected by mSurfaceLock
291 Rect mSwapRectangle;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800292 int mConnected;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700293 Rect mNextBufferCrop;
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700294 uint32_t mNextBufferTransform;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700295 BufferInfo mBufferInfo;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700296
297 // protected by mSurfaceLock. These are also used from lock/unlock
298 // but in that case, they must be called form the same thread.
Mathias Agopian321abdb2009-08-14 18:52:17 -0700299 mutable Region mDirtyRegion;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700300
301 // must be used from the lock/unlock thread
Mathias Agopian6950e422009-10-05 17:07:12 -0700302 sp<GraphicBuffer> mLockedBuffer;
303 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700304 mutable Region mOldDirtyRegion;
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700305 bool mReserved;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700306
Mathias Agopian2be352a2010-05-21 17:24:35 -0700307 // only used from dequeueBuffer()
308 Vector< sp<GraphicBuffer> > mBuffers;
309
Mathias Agopian321abdb2009-08-14 18:52:17 -0700310 // query() must be called from dequeueBuffer() thread
311 uint32_t mWidth;
312 uint32_t mHeight;
313
314 // Inherently thread-safe
315 mutable Mutex mSurfaceLock;
Mathias Agopian9779b222009-09-07 16:32:45 -0700316 mutable Mutex mApiLock;
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700317
318 // A cache of Surface objects that have been deserialized into this process.
319 static Mutex sCachedSurfacesLock;
320 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321};
322
323}; // namespace android
324
Mathias Agopian000479f2010-02-09 17:46:37 -0800325#endif // ANDROID_SF_SURFACE_H