blob: 295797066f80ee83da85e4c8bf5638da7f064465 [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 Agopian01b76682009-04-16 20:04:08 -0700148 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149
Mathias Agopian01b76682009-04-16 20:04:08 -0700150 static bool isValid(const sp<Surface>& surface) {
151 return (surface != 0) && surface->isValid();
152 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700153
Mathias Agopian01b76682009-04-16 20:04:08 -0700154 static bool isSameSurface(
155 const sp<Surface>& lhs, const sp<Surface>& rhs);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700156
157 bool isValid();
158 SurfaceID ID() const { return mToken; }
159 uint32_t getFlags() const { return mFlags; }
Mathias Agopian01b76682009-04-16 20:04:08 -0700160 uint32_t getIdentity() const { return mIdentity; }
161
Mathias Agopianba5972f2009-08-14 18:52:17 -0700162 // the lock/unlock APIs must be used from the same thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 status_t lock(SurfaceInfo* info, bool blocking = true);
164 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
165 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
Mathias Agopian0926f502009-05-04 14:17:04 -0700167 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
Mathias Agopianb2965332010-04-27 16:41:19 -0700170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171private:
Mathias Agopianb2965332010-04-27 16:41:19 -0700172 /*
173 * Android frameworks friends
174 * (eventually this should go away and be replaced by proper APIs)
175 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 // camera and camcorder need access to the ISurface binder interface for preview
177 friend class Camera;
178 friend class MediaRecorder;
Mathias Agopianb2965332010-04-27 16:41:19 -0700179 // MediaPlayer needs access to ISurface for display
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 friend class MediaPlayer;
Andreas Hubere1864312009-08-07 12:01:29 -0700181 friend class IOMX;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700182 // this is just to be able to write some unit tests
183 friend class Test;
184
Mathias Agopianb2965332010-04-27 16:41:19 -0700185private:
186 friend class SurfaceComposerClient;
187 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
Mathias Agopianb2965332010-04-27 16:41:19 -0700189 // can't be copied
190 Surface& operator = (Surface& rhs);
191 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192
Mathias Agopianb2965332010-04-27 16:41:19 -0700193 Surface(const sp<SurfaceControl>& control);
194 ~Surface();
195
196
197 /*
198 * android_native_window_t hooks
199 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
202 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
203 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700204 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700205 static int perform(android_native_window_t* window, int operation, ...);
Mathias Agopian076b1cc2009-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 Agopiancb6b9042009-07-30 18:14:56 -0700210 int query(int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700211 int perform(int operation, va_list args);
Mathias Agopian0926f502009-05-04 14:17:04 -0700212
Mathias Agopian55fa2512010-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 Agopiancc08e682010-04-15 18:48:26 -0700216 int dispatch_crop(va_list args);
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700217 int dispatch_set_buffer_count(va_list args);
Mathias Agopiana138f892010-05-21 17:24:35 -0700218 int dispatch_set_buffers_geometry(va_list args);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700219
Mathias Agopianba5972f2009-08-14 18:52:17 -0700220 void setUsage(uint32_t reqUsage);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800221 int connect(int api);
222 int disconnect(int api);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700223 int crop(Rect const* rect);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700224 int setBufferCount(int bufferCount);
Mathias Agopiana138f892010-05-21 17:24:35 -0700225 int setBuffersGeometry(int w, int h, int format);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800226
Mathias Agopianb2965332010-04-27 16:41:19 -0700227 /*
228 * private stuff...
229 */
230 void init();
231 status_t validate() const;
Mathias Agopian631f3582010-05-25 17:51:34 -0700232 status_t initCheck() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700233 sp<ISurface> getISurface() const;
234
235 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
236 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
237
Mathias Agopiana138f892010-05-21 17:24:35 -0700238 status_t getBufferLocked(int index,
239 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
Mathias Agopianb2965332010-04-27 16:41:19 -0700240 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
241
Mathias Agopiana138f892010-05-21 17:24:35 -0700242 int getConnectedApi() const;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700243
Mathias Agopiana138f892010-05-21 17:24:35 -0700244 bool needNewBuffer(int bufIdx,
245 uint32_t *pWidth, uint32_t *pHeight,
246 uint32_t *pFormat, uint32_t *pUsage) const;
247
248 class BufferInfo {
249 uint32_t mWidth;
250 uint32_t mHeight;
251 uint32_t mFormat;
252 uint32_t mUsage;
253 mutable uint32_t mDirty;
254 enum {
255 GEOMETRY = 0x01
256 };
257 public:
258 BufferInfo();
259 void set(uint32_t w, uint32_t h, uint32_t format);
260 void set(uint32_t usage);
261 void get(uint32_t *pWidth, uint32_t *pHeight,
262 uint32_t *pFormat, uint32_t *pUsage) const;
263 bool validateBuffer(const sp<GraphicBuffer>& buffer) const;
264 };
265
Mathias Agopianba5972f2009-08-14 18:52:17 -0700266 // constants
Mathias Agopiand4784a32010-05-27 19:41:15 -0700267 sp<SurfaceClient> mClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 SurfaceID mToken;
270 uint32_t mIdentity;
271 PixelFormat mFormat;
272 uint32_t mFlags;
Mathias Agopian3330b202009-10-05 17:07:12 -0700273 GraphicBufferMapper& mBufferMapper;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700274 SharedBufferClient* mSharedBufferClient;
Mathias Agopian631f3582010-05-25 17:51:34 -0700275 status_t mInitCheck;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700276
277 // protected by mSurfaceLock
278 Rect mSwapRectangle;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800279 int mConnected;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700280 Rect mNextBufferCrop;
Mathias Agopiana138f892010-05-21 17:24:35 -0700281 BufferInfo mBufferInfo;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700282
283 // protected by mSurfaceLock. These are also used from lock/unlock
284 // but in that case, they must be called form the same thread.
Mathias Agopianba5972f2009-08-14 18:52:17 -0700285 mutable Region mDirtyRegion;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700286
287 // must be used from the lock/unlock thread
Mathias Agopian3330b202009-10-05 17:07:12 -0700288 sp<GraphicBuffer> mLockedBuffer;
289 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700290 mutable Region mOldDirtyRegion;
Mathias Agopian245e4d72010-04-21 15:24:11 -0700291 bool mReserved;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700292
Mathias Agopiana138f892010-05-21 17:24:35 -0700293 // only used from dequeueBuffer()
294 Vector< sp<GraphicBuffer> > mBuffers;
295
Mathias Agopianba5972f2009-08-14 18:52:17 -0700296 // query() must be called from dequeueBuffer() thread
297 uint32_t mWidth;
298 uint32_t mHeight;
299
300 // Inherently thread-safe
301 mutable Mutex mSurfaceLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 mutable Mutex mApiLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303};
304
305}; // namespace android
306
Mathias Agopian9cce3252010-02-09 17:46:37 -0800307#endif // ANDROID_SF_SURFACE_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308