blob: 4fd0681596c657eb14b0789fbf00671291de5fcc [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
23#include <utils/RefBase.h>
24#include <utils/threads.h>
25
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <ui/PixelFormat.h>
27#include <ui/Region.h>
Mathias Agopianac2523b2009-05-05 18:11:11 -070028#include <ui/egl/android_natives.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070029
Mathias Agopian000479f2010-02-09 17:46:37 -080030#include <surfaceflinger/ISurface.h>
Mathias Agopian770492c2010-05-28 14:22:23 -070031#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080032
Mathias Agopian8b138322010-04-12 16:22:15 -070033#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035namespace android {
36
37// ---------------------------------------------------------------------------
38
Mathias Agopian2be352a2010-05-21 17:24:35 -070039class GraphicBuffer;
Mathias Agopian6950e422009-10-05 17:07:12 -070040class GraphicBufferMapper;
Andreas Huberccf8b942009-08-07 12:01:29 -070041class IOMX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042class Rect;
Mathias Agopiandff8e582009-05-04 14:17:04 -070043class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044class SurfaceComposerClient;
Mathias Agopian9779b222009-09-07 16:32:45 -070045class SharedClient;
46class SharedBufferClient;
Mathias Agopianc7b388c2010-05-27 19:41:15 -070047class SurfaceClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Mathias Agopian1473f462009-04-10 14:24:30 -070049// ---------------------------------------------------------------------------
50
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070051class SurfaceControl : public RefBase
52{
53public:
54 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070055 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070056 }
Mathias Agopian17f638b2009-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 Agopian6d2c0bc2009-04-16 16:19:50 -070062
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070063 uint32_t getFlags() const { return mFlags; }
64 uint32_t getIdentity() const { return mIdentity; }
65
66 // release surface data from java
67 void clear();
68
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070069 status_t setLayer(int32_t layer);
70 status_t setPosition(int32_t x, int32_t y);
71 status_t setSize(uint32_t w, uint32_t h);
72 status_t hide();
73 status_t show(int32_t layer = -1);
74 status_t freeze();
75 status_t unfreeze();
76 status_t setFlags(uint32_t flags, uint32_t mask);
77 status_t setTransparentRegionHint(const Region& transparent);
78 status_t setAlpha(float alpha=1.0f);
79 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
80 status_t setFreezeTint(uint32_t tint);
81
Mathias Agopian17f638b2009-04-16 20:04:08 -070082 static status_t writeSurfaceToParcel(
83 const sp<SurfaceControl>& control, Parcel* parcel);
84
85 sp<Surface> getSurface() const;
86
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070087private:
Mathias Agopian17f638b2009-04-16 20:04:08 -070088 // can't be copied
89 SurfaceControl& operator = (SurfaceControl& rhs);
90 SurfaceControl(const SurfaceControl& rhs);
91
92
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070093 friend class SurfaceComposerClient;
94
95 // camera and camcorder need access to the ISurface binder interface for preview
96 friend class Camera;
97 friend class MediaRecorder;
98 // mediaplayer needs access to ISurface for display
99 friend class MediaPlayer;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700100 // for testing
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700101 friend class Test;
102 const sp<ISurface>& getISurface() const { return mSurface; }
103
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700104
105 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
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700150 static sp<Surface> readFromParcel(
151 const Parcel& data, const sp<Surface>& other);
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
176 friend class Camera;
177 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;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700181 // this is just to be able to write some unit tests
182 friend class Test;
183
Mathias Agopianf590f702010-04-27 16:41:19 -0700184private:
185 friend class SurfaceComposerClient;
186 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Mathias Agopianf590f702010-04-27 16:41:19 -0700188 // can't be copied
189 Surface& operator = (Surface& rhs);
190 Surface(const Surface& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Mathias Agopianf590f702010-04-27 16:41:19 -0700192 Surface(const sp<SurfaceControl>& control);
Mathias Agopianfae5cb22010-06-04 18:26:32 -0700193 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianf590f702010-04-27 16:41:19 -0700194 ~Surface();
195
196
197 /*
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700198 * ANativeWindow hooks
Mathias Agopianf590f702010-04-27 16:41:19 -0700199 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700200 static int setSwapInterval(ANativeWindow* window, int interval);
201 static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
202 static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
203 static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
204 static int query(ANativeWindow* window, int what, int* value);
205 static int perform(ANativeWindow* window, int operation, ...);
Mathias Agopian1473f462009-04-10 14:24:30 -0700206
207 int dequeueBuffer(android_native_buffer_t** buffer);
208 int lockBuffer(android_native_buffer_t* buffer);
209 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700210 int query(int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700211 int perform(int operation, va_list args);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700212
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800213 void dispatch_setUsage(va_list args);
214 int dispatch_connect(va_list args);
215 int dispatch_disconnect(va_list args);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700216 int dispatch_crop(va_list args);
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700217 int dispatch_set_buffer_count(va_list args);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700218 int dispatch_set_buffers_geometry(va_list args);
Mathias Agopian1473f462009-04-10 14:24:30 -0700219
Mathias Agopian321abdb2009-08-14 18:52:17 -0700220 void setUsage(uint32_t reqUsage);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800221 int connect(int api);
222 int disconnect(int api);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700223 int crop(Rect const* rect);
Mathias Agopian59751db2010-05-07 15:58:44 -0700224 int setBufferCount(int bufferCount);
Mathias Agopian2be352a2010-05-21 17:24:35 -0700225 int setBuffersGeometry(int w, int h, int format);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800226
Mathias Agopianf590f702010-04-27 16:41:19 -0700227 /*
228 * private stuff...
229 */
230 void init();
231 status_t validate() const;
Mathias Agopianf590f702010-04-27 16:41:19 -0700232 sp<ISurface> getISurface() const;
233
234 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
235 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
236
Mathias Agopian2be352a2010-05-21 17:24:35 -0700237 status_t getBufferLocked(int index,
238 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianf590f702010-04-27 16:41:19 -0700239 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
240
Mathias Agopian2be352a2010-05-21 17:24:35 -0700241 int getConnectedApi() const;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700242
Mathias Agopian2be352a2010-05-21 17:24:35 -0700243 bool needNewBuffer(int bufIdx,
244 uint32_t *pWidth, uint32_t *pHeight,
245 uint32_t *pFormat, uint32_t *pUsage) const;
246
247 class BufferInfo {
248 uint32_t mWidth;
249 uint32_t mHeight;
250 uint32_t mFormat;
251 uint32_t mUsage;
252 mutable uint32_t mDirty;
253 enum {
254 GEOMETRY = 0x01
255 };
256 public:
257 BufferInfo();
258 void set(uint32_t w, uint32_t h, uint32_t format);
259 void set(uint32_t usage);
260 void get(uint32_t *pWidth, uint32_t *pHeight,
261 uint32_t *pFormat, uint32_t *pUsage) const;
262 bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
263 };
264
Mathias Agopian321abdb2009-08-14 18:52:17 -0700265 // constants
Mathias Agopian7623da42010-06-01 15:12:58 -0700266 GraphicBufferMapper& mBufferMapper;
267 SurfaceClient& mClient;
268 SharedBufferClient* mSharedBufferClient;
269 status_t mInitCheck;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 uint32_t mIdentity;
272 PixelFormat mFormat;
273 uint32_t mFlags;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700274
275 // protected by mSurfaceLock
276 Rect mSwapRectangle;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800277 int mConnected;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700278 Rect mNextBufferCrop;
Mathias Agopian2be352a2010-05-21 17:24:35 -0700279 BufferInfo mBufferInfo;
Mathias Agopian321abdb2009-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 Agopian321abdb2009-08-14 18:52:17 -0700283 mutable Region mDirtyRegion;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700284
285 // must be used from the lock/unlock thread
Mathias Agopian6950e422009-10-05 17:07:12 -0700286 sp<GraphicBuffer> mLockedBuffer;
287 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700288 mutable Region mOldDirtyRegion;
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700289 bool mReserved;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700290
Mathias Agopian2be352a2010-05-21 17:24:35 -0700291 // only used from dequeueBuffer()
292 Vector< sp<GraphicBuffer> > mBuffers;
293
Mathias Agopian321abdb2009-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 Agopian9779b222009-09-07 16:32:45 -0700300 mutable Mutex mApiLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301};
302
303}; // namespace android
304
Mathias Agopian000479f2010-02-09 17:46:37 -0800305#endif // ANDROID_SF_SURFACE_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306