blob: a2108804af5e8e11caaaf5593aaa601c0468ae86 [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;
103 const sp<ISurface>& getISurface() const { return mSurface; }
104
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700105
106 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700107
108 SurfaceControl(
109 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700110 const sp<ISurface>& surface,
Mathias Agopian770492c2010-05-28 14:22:23 -0700111 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700112 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700113
114 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700115
Mathias Agopian18e02602009-11-13 15:26:29 -0800116 status_t validate() const;
Mathias Agopian6d2c0bc2009-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 Agopian5b5c9142009-07-30 18:14:56 -0700123 uint32_t mWidth;
124 uint32_t mHeight;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700125 PixelFormat mFormat;
126 uint32_t mFlags;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700127 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700128
129 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700130};
131
132// ---------------------------------------------------------------------------
133
Mathias Agopian1473f462009-04-10 14:24:30 -0700134class Surface
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700135 : public EGLNativeBase<ANativeWindow, Surface, RefBase>
Mathias Agopian1473f462009-04-10 14:24:30 -0700136{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137public:
138 struct SurfaceInfo {
139 uint32_t w;
140 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700141 uint32_t s;
142 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 PixelFormat format;
144 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 uint32_t reserved[2];
146 };
147
Mathias Agopian5e140102010-06-08 19:54:15 -0700148 static status_t writeToParcel(
149 const sp<Surface>& control, Parcel* parcel);
150
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700151 static sp<Surface> readFromParcel(const Parcel& data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
Mathias Agopian17f638b2009-04-16 20:04:08 -0700153 static bool isValid(const sp<Surface>& surface) {
154 return (surface != 0) && surface->isValid();
155 }
Mathias Agopian321abdb2009-08-14 18:52:17 -0700156
Mathias Agopian321abdb2009-08-14 18:52:17 -0700157 bool isValid();
Mathias Agopian321abdb2009-08-14 18:52:17 -0700158 uint32_t getFlags() const { return mFlags; }
Mathias Agopian17f638b2009-04-16 20:04:08 -0700159 uint32_t getIdentity() const { return mIdentity; }
160
Mathias Agopian321abdb2009-08-14 18:52:17 -0700161 // the lock/unlock APIs must be used from the same thread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 status_t lock(SurfaceInfo* info, bool blocking = true);
163 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
164 status_t unlockAndPost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Mathias Agopiandff8e582009-05-04 14:17:04 -0700166 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 void setSwapRectangle(const Rect& r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
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;
184
Mathias Agopianf590f702010-04-27 16:41:19 -0700185private:
186 friend class SurfaceComposerClient;
187 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Mathias Agopianf590f702010-04-27 16:41:19 -0700189 // can't be copied
190 Surface& operator = (Surface& rhs);
191 Surface(const Surface& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Mathias Agopianf590f702010-04-27 16:41:19 -0700193 Surface(const sp<SurfaceControl>& control);
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700194 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianf590f702010-04-27 16:41:19 -0700195 ~Surface();
196
197
198 /*
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700199 * ANativeWindow hooks
Mathias Agopianf590f702010-04-27 16:41:19 -0700200 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700201 static int setSwapInterval(ANativeWindow* window, int interval);
202 static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
203 static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
204 static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
205 static int query(ANativeWindow* window, int what, int* value);
206 static int perform(ANativeWindow* window, int operation, ...);
Mathias Agopian1473f462009-04-10 14:24:30 -0700207
208 int dequeueBuffer(android_native_buffer_t** buffer);
209 int lockBuffer(android_native_buffer_t* buffer);
210 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700211 int query(int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700212 int perform(int operation, va_list args);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700213
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800214 void dispatch_setUsage(va_list args);
215 int dispatch_connect(va_list args);
216 int dispatch_disconnect(va_list args);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700217 int dispatch_crop(va_list args);
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700218 int dispatch_set_buffer_count(va_list args);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700219 int dispatch_set_buffers_geometry(va_list args);
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700220 int dispatch_set_buffers_transform(va_list args);
Mathias Agopian1473f462009-04-10 14:24:30 -0700221
Mathias Agopian321abdb2009-08-14 18:52:17 -0700222 void setUsage(uint32_t reqUsage);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800223 int connect(int api);
224 int disconnect(int api);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700225 int crop(Rect const* rect);
Mathias Agopian59751db2010-05-07 15:58:44 -0700226 int setBufferCount(int bufferCount);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700227 int setBuffersGeometry(int w, int h, int format);
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700228 int setBuffersTransform(int transform);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800229
Mathias Agopianf590f702010-04-27 16:41:19 -0700230 /*
231 * private stuff...
232 */
233 void init();
234 status_t validate() const;
Mathias Agopianf590f702010-04-27 16:41:19 -0700235 sp<ISurface> getISurface() const;
236
237 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
238 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
239
Mathias Agopian2be352a2010-05-21 17:24:35 -0700240 status_t getBufferLocked(int index,
241 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianf590f702010-04-27 16:41:19 -0700242 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
243
Mathias Agopian2be352a2010-05-21 17:24:35 -0700244 int getConnectedApi() const;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700245
Mathias Agopian2be352a2010-05-21 17:24:35 -0700246 bool needNewBuffer(int bufIdx,
247 uint32_t *pWidth, uint32_t *pHeight,
248 uint32_t *pFormat, uint32_t *pUsage) const;
249
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700250 static void cleanCachedSurfaces();
251
Mathias Agopian2be352a2010-05-21 17:24:35 -0700252 class BufferInfo {
253 uint32_t mWidth;
254 uint32_t mHeight;
255 uint32_t mFormat;
256 uint32_t mUsage;
257 mutable uint32_t mDirty;
258 enum {
259 GEOMETRY = 0x01
260 };
261 public:
262 BufferInfo();
263 void set(uint32_t w, uint32_t h, uint32_t format);
264 void set(uint32_t usage);
265 void get(uint32_t *pWidth, uint32_t *pHeight,
266 uint32_t *pFormat, uint32_t *pUsage) const;
267 bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
268 };
269
Mathias Agopian321abdb2009-08-14 18:52:17 -0700270 // constants
Mathias Agopian7623da42010-06-01 15:12:58 -0700271 GraphicBufferMapper& mBufferMapper;
272 SurfaceClient& mClient;
273 SharedBufferClient* mSharedBufferClient;
274 status_t mInitCheck;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 uint32_t mIdentity;
277 PixelFormat mFormat;
278 uint32_t mFlags;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700279
280 // protected by mSurfaceLock
281 Rect mSwapRectangle;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800282 int mConnected;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700283 Rect mNextBufferCrop;
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700284 uint32_t mNextBufferTransform;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700285 BufferInfo mBufferInfo;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700286
287 // protected by mSurfaceLock. These are also used from lock/unlock
288 // but in that case, they must be called form the same thread.
Mathias Agopian321abdb2009-08-14 18:52:17 -0700289 mutable Region mDirtyRegion;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700290
291 // must be used from the lock/unlock thread
Mathias Agopian6950e422009-10-05 17:07:12 -0700292 sp<GraphicBuffer> mLockedBuffer;
293 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700294 mutable Region mOldDirtyRegion;
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700295 bool mReserved;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700296
Mathias Agopian2be352a2010-05-21 17:24:35 -0700297 // only used from dequeueBuffer()
298 Vector< sp<GraphicBuffer> > mBuffers;
299
Mathias Agopian321abdb2009-08-14 18:52:17 -0700300 // query() must be called from dequeueBuffer() thread
301 uint32_t mWidth;
302 uint32_t mHeight;
303
304 // Inherently thread-safe
305 mutable Mutex mSurfaceLock;
Mathias Agopian9779b222009-09-07 16:32:45 -0700306 mutable Mutex mApiLock;
Jamie Gennis5ee65f02010-07-15 17:29:15 -0700307
308 // A cache of Surface objects that have been deserialized into this process.
309 static Mutex sCachedSurfacesLock;
310 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311};
312
313}; // namespace android
314
Mathias Agopian000479f2010-02-09 17:46:37 -0800315#endif // ANDROID_SF_SURFACE_H