blob: e4d60af7cde341f5aae0c53fbfa76ba3778d411b [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>
31#include <surfaceflinger/ISurfaceFlingerClient.h>
32
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 Agopian6950e422009-10-05 17:07:12 -070039class GraphicBufferMapper;
Andreas Huberccf8b942009-08-07 12:01:29 -070040class IOMX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041class Rect;
Mathias Agopiandff8e582009-05-04 14:17:04 -070042class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043class SurfaceComposerClient;
Mathias Agopian9779b222009-09-07 16:32:45 -070044class SharedClient;
45class SharedBufferClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Mathias Agopian1473f462009-04-10 14:24:30 -070047// ---------------------------------------------------------------------------
48
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070049class SurfaceControl : public RefBase
50{
51public:
52 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070053 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070054 }
Mathias Agopian17f638b2009-04-16 20:04:08 -070055 bool isValid() {
56 return mToken>=0 && mClient!=0;
57 }
58 static bool isSameSurface(
59 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070060
61 SurfaceID ID() const { return mToken; }
62 uint32_t getFlags() const { return mFlags; }
63 uint32_t getIdentity() const { return mIdentity; }
64
65 // release surface data from java
66 void clear();
67
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070068 status_t setLayer(int32_t layer);
69 status_t setPosition(int32_t x, int32_t y);
70 status_t setSize(uint32_t w, uint32_t h);
71 status_t hide();
72 status_t show(int32_t layer = -1);
73 status_t freeze();
74 status_t unfreeze();
75 status_t setFlags(uint32_t flags, uint32_t mask);
76 status_t setTransparentRegionHint(const Region& transparent);
77 status_t setAlpha(float alpha=1.0f);
78 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
79 status_t setFreezeTint(uint32_t tint);
80
Mathias Agopian17f638b2009-04-16 20:04:08 -070081 static status_t writeSurfaceToParcel(
82 const sp<SurfaceControl>& control, Parcel* parcel);
83
84 sp<Surface> getSurface() const;
85
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070086private:
Mathias Agopian17f638b2009-04-16 20:04:08 -070087 // can't be copied
88 SurfaceControl& operator = (SurfaceControl& rhs);
89 SurfaceControl(const SurfaceControl& rhs);
90
91
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070092 friend class SurfaceComposerClient;
93
94 // camera and camcorder need access to the ISurface binder interface for preview
95 friend class Camera;
96 friend class MediaRecorder;
97 // mediaplayer needs access to ISurface for display
98 friend class MediaPlayer;
Mathias Agopian17f638b2009-04-16 20:04:08 -070099 // for testing
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700100 friend class Test;
101 const sp<ISurface>& getISurface() const { return mSurface; }
102
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700103
104 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700105
106 SurfaceControl(
107 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700108 const sp<ISurface>& surface,
109 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700110 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700111
112 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700113
Mathias Agopian18e02602009-11-13 15:26:29 -0800114 status_t validate() const;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700115 void destroy();
116
117 sp<SurfaceComposerClient> mClient;
118 sp<ISurface> mSurface;
119 SurfaceID mToken;
120 uint32_t mIdentity;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700121 uint32_t mWidth;
122 uint32_t mHeight;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700123 PixelFormat mFormat;
124 uint32_t mFlags;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700125 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700126
127 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700128};
129
130// ---------------------------------------------------------------------------
131
Mathias Agopian1473f462009-04-10 14:24:30 -0700132class Surface
133 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
134{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135public:
136 struct SurfaceInfo {
137 uint32_t w;
138 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700139 uint32_t s;
140 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 PixelFormat format;
142 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 uint32_t reserved[2];
144 };
145
Mathias Agopian17f638b2009-04-16 20:04:08 -0700146 Surface(const Parcel& data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
Mathias Agopian17f638b2009-04-16 20:04:08 -0700148 static bool isValid(const sp<Surface>& surface) {
149 return (surface != 0) && surface->isValid();
150 }
Mathias Agopian321abdb2009-08-14 18:52:17 -0700151
Mathias Agopian17f638b2009-04-16 20:04:08 -0700152 static bool isSameSurface(
153 const sp<Surface>& lhs, const sp<Surface>& rhs);
Mathias Agopian321abdb2009-08-14 18:52:17 -0700154
155 bool isValid();
156 SurfaceID ID() const { return mToken; }
157 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 Agopianf590f702010-04-27 16:41:19 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169private:
Mathias Agopianf590f702010-04-27 16:41:19 -0700170 /*
171 * Android frameworks friends
172 * (eventually this should go away and be replaced by proper APIs)
173 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // camera and camcorder need access to the ISurface binder interface for preview
175 friend class Camera;
176 friend class MediaRecorder;
Mathias Agopianf590f702010-04-27 16:41:19 -0700177 // MediaPlayer needs access to ISurface for display
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 friend class MediaPlayer;
Andreas Huberccf8b942009-08-07 12:01:29 -0700179 friend class IOMX;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700180 // this is just to be able to write some unit tests
181 friend class Test;
182
Mathias Agopianf590f702010-04-27 16:41:19 -0700183private:
184 friend class SurfaceComposerClient;
185 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Mathias Agopianf590f702010-04-27 16:41:19 -0700187 // can't be copied
188 Surface& operator = (Surface& rhs);
189 Surface(const Surface& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Mathias Agopianf590f702010-04-27 16:41:19 -0700191 Surface(const sp<SurfaceControl>& control);
192 ~Surface();
193
194
195 /*
196 * android_native_window_t hooks
197 */
Mathias Agopian1473f462009-04-10 14:24:30 -0700198 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian1473f462009-04-10 14:24:30 -0700199 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
200 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
201 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700202 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700203 static int perform(android_native_window_t* window, int operation, ...);
Mathias Agopian1473f462009-04-10 14:24:30 -0700204
205 int dequeueBuffer(android_native_buffer_t** buffer);
206 int lockBuffer(android_native_buffer_t* buffer);
207 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700208 int query(int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700209 int perform(int operation, va_list args);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700210
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800211 void dispatch_setUsage(va_list args);
212 int dispatch_connect(va_list args);
213 int dispatch_disconnect(va_list args);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700214 int dispatch_crop(va_list args);
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700215 int dispatch_set_buffer_count(va_list args);
Mathias Agopian1473f462009-04-10 14:24:30 -0700216
Mathias Agopian321abdb2009-08-14 18:52:17 -0700217 void setUsage(uint32_t reqUsage);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800218 int connect(int api);
219 int disconnect(int api);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700220 int crop(Rect const* rect);
Mathias Agopian59751db2010-05-07 15:58:44 -0700221 int setBufferCount(int bufferCount);
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800222
Mathias Agopianf590f702010-04-27 16:41:19 -0700223 /*
224 * private stuff...
225 */
226 void init();
227 status_t validate() const;
228 sp<SurfaceComposerClient> getClient() const;
229 sp<ISurface> getISurface() const;
230
231 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
232 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
233
234 status_t getBufferLocked(int index, int usage);
235 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
236
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700237 uint32_t getUsage() const;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800238 int getConnectedApi() const;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700239
240 // constants
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 sp<SurfaceComposerClient> mClient;
242 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 SurfaceID mToken;
244 uint32_t mIdentity;
245 PixelFormat mFormat;
246 uint32_t mFlags;
Mathias Agopian6950e422009-10-05 17:07:12 -0700247 GraphicBufferMapper& mBufferMapper;
Mathias Agopian9779b222009-09-07 16:32:45 -0700248 SharedBufferClient* mSharedBufferClient;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700249
250 // protected by mSurfaceLock
251 Rect mSwapRectangle;
252 uint32_t mUsage;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800253 int mConnected;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700254 Rect mNextBufferCrop;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700255
256 // protected by mSurfaceLock. These are also used from lock/unlock
257 // but in that case, they must be called form the same thread.
Mathias Agopian6950e422009-10-05 17:07:12 -0700258 sp<GraphicBuffer> mBuffers[2];
Mathias Agopian321abdb2009-08-14 18:52:17 -0700259 mutable Region mDirtyRegion;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700260
261 // must be used from the lock/unlock thread
Mathias Agopian6950e422009-10-05 17:07:12 -0700262 sp<GraphicBuffer> mLockedBuffer;
263 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700264 mutable Region mOldDirtyRegion;
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700265 bool mReserved;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700266
267 // query() must be called from dequeueBuffer() thread
268 uint32_t mWidth;
269 uint32_t mHeight;
270
271 // Inherently thread-safe
272 mutable Mutex mSurfaceLock;
Mathias Agopian9779b222009-09-07 16:32:45 -0700273 mutable Mutex mApiLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274};
275
276}; // namespace android
277
Mathias Agopian000479f2010-02-09 17:46:37 -0800278#endif // ANDROID_SF_SURFACE_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279