blob: 7ab3a0014480d65db79ef4c8a5ce03015150a7af [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>
31#include <surfaceflinger/ISurfaceFlingerClient.h>
32
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 Agopian3330b202009-10-05 17:07:12 -070039class GraphicBufferMapper;
Andreas Hubere1864312009-08-07 12:01:29 -070040class IOMX;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070042class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043class SurfaceComposerClient;
Mathias Agopiancbb288b2009-09-07 16:32:45 -070044class SharedClient;
45class SharedBufferClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047// ---------------------------------------------------------------------------
48
Mathias Agopian62185b72009-04-16 16:19:50 -070049class SurfaceControl : public RefBase
50{
51public:
52 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070053 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070054 }
Mathias Agopian01b76682009-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 Agopian62185b72009-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 Agopian62185b72009-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 Agopian01b76682009-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 Agopian62185b72009-04-16 16:19:50 -070086private:
Mathias Agopian01b76682009-04-16 20:04:08 -070087 // can't be copied
88 SurfaceControl& operator = (SurfaceControl& rhs);
89 SurfaceControl(const SurfaceControl& rhs);
90
91
Mathias Agopian62185b72009-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 Agopian01b76682009-04-16 20:04:08 -070099 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -0700100 friend class Test;
101 const sp<ISurface>& getISurface() const { return mSurface; }
102
Mathias Agopian62185b72009-04-16 16:19:50 -0700103
104 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700105
106 SurfaceControl(
107 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700108 const sp<ISurface>& surface,
109 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700110 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700111
112 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700113
Mathias Agopian963abad2009-11-13 15:26:29 -0800114 status_t validate() const;
Mathias Agopian62185b72009-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 Agopiancb6b9042009-07-30 18:14:56 -0700121 uint32_t mWidth;
122 uint32_t mHeight;
Mathias Agopian62185b72009-04-16 16:19:50 -0700123 PixelFormat mFormat;
124 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700125 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700126
127 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700128};
129
130// ---------------------------------------------------------------------------
131
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132class Surface
133 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
134{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135public:
136 struct SurfaceInfo {
137 uint32_t w;
138 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 uint32_t s;
140 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 PixelFormat format;
142 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 uint32_t reserved[2];
144 };
145
Mathias Agopian01b76682009-04-16 20:04:08 -0700146 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147
Mathias Agopian01b76682009-04-16 20:04:08 -0700148 static bool isValid(const sp<Surface>& surface) {
149 return (surface != 0) && surface->isValid();
150 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700151
Mathias Agopian01b76682009-04-16 20:04:08 -0700152 static bool isSameSurface(
153 const sp<Surface>& lhs, const sp<Surface>& rhs);
Mathias Agopianba5972f2009-08-14 18:52:17 -0700154
155 bool isValid();
156 SurfaceID ID() const { return mToken; }
157 uint32_t getFlags() const { return mFlags; }
Mathias Agopian01b76682009-04-16 20:04:08 -0700158 uint32_t getIdentity() const { return mIdentity; }
159
Mathias Agopianba5972f2009-08-14 18:52:17 -0700160 // the lock/unlock APIs must be used from the same thread
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -0800164
Mathias Agopian0926f502009-05-04 14:17:04 -0700165 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
Mathias Agopianb2965332010-04-27 16:41:19 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169private:
Mathias Agopianb2965332010-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 Projectedbf3b62009-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 Agopianb2965332010-04-27 16:41:19 -0700177 // MediaPlayer needs access to ISurface for display
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 friend class MediaPlayer;
Andreas Hubere1864312009-08-07 12:01:29 -0700179 friend class IOMX;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700180 // this is just to be able to write some unit tests
181 friend class Test;
182
Mathias Agopianb2965332010-04-27 16:41:19 -0700183private:
184 friend class SurfaceComposerClient;
185 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186
Mathias Agopianb2965332010-04-27 16:41:19 -0700187 // can't be copied
188 Surface& operator = (Surface& rhs);
189 Surface(const Surface& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190
Mathias Agopianb2965332010-04-27 16:41:19 -0700191 Surface(const sp<SurfaceControl>& control);
192 ~Surface();
193
194
195 /*
196 * android_native_window_t hooks
197 */
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700198 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-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 Agopiancb6b9042009-07-30 18:14:56 -0700202 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700203 static int perform(android_native_window_t* window, int operation, ...);
Mathias Agopian076b1cc2009-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 Agopiancb6b9042009-07-30 18:14:56 -0700208 int query(int what, int* value);
Mathias Agopian52212712009-08-11 22:34:02 -0700209 int perform(int operation, va_list args);
Mathias Agopian0926f502009-05-04 14:17:04 -0700210
Mathias Agopian55fa2512010-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 Agopiancc08e682010-04-15 18:48:26 -0700214 int dispatch_crop(va_list args);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215
Mathias Agopianba5972f2009-08-14 18:52:17 -0700216 void setUsage(uint32_t reqUsage);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800217 int connect(int api);
218 int disconnect(int api);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700219 int crop(Rect const* rect);
Mathias Agopian55fa2512010-03-11 15:06:54 -0800220
Mathias Agopianb2965332010-04-27 16:41:19 -0700221 /*
222 * private stuff...
223 */
224 void init();
225 status_t validate() const;
226 sp<SurfaceComposerClient> getClient() const;
227 sp<ISurface> getISurface() const;
228
229 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
230 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
231
232 status_t getBufferLocked(int index, int usage);
233 int getBufferIndex(const sp<GraphicBuffer>& buffer) const;
234
Mathias Agopian68a6afe2009-09-15 19:10:47 -0700235 uint32_t getUsage() const;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800236 int getConnectedApi() const;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700237
238 // constants
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 sp<SurfaceComposerClient> mClient;
240 sp<ISurface> mSurface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 SurfaceID mToken;
242 uint32_t mIdentity;
243 PixelFormat mFormat;
244 uint32_t mFlags;
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 GraphicBufferMapper& mBufferMapper;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700246 SharedBufferClient* mSharedBufferClient;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700247
248 // protected by mSurfaceLock
249 Rect mSwapRectangle;
250 uint32_t mUsage;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800251 int mConnected;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700252 Rect mNextBufferCrop;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700253
254 // protected by mSurfaceLock. These are also used from lock/unlock
255 // but in that case, they must be called form the same thread.
Mathias Agopian3330b202009-10-05 17:07:12 -0700256 sp<GraphicBuffer> mBuffers[2];
Mathias Agopianba5972f2009-08-14 18:52:17 -0700257 mutable Region mDirtyRegion;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700258
259 // must be used from the lock/unlock thread
Mathias Agopian3330b202009-10-05 17:07:12 -0700260 sp<GraphicBuffer> mLockedBuffer;
261 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700262 mutable Region mOldDirtyRegion;
Mathias Agopian245e4d72010-04-21 15:24:11 -0700263 bool mReserved;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700264
265 // query() must be called from dequeueBuffer() thread
266 uint32_t mWidth;
267 uint32_t mHeight;
268
269 // Inherently thread-safe
270 mutable Mutex mSurfaceLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700271 mutable Mutex mApiLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272};
273
274}; // namespace android
275
Mathias Agopian9cce3252010-02-09 17:46:37 -0800276#endif // ANDROID_SF_SURFACE_H
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277