blob: 40dc467fb93bf348485256ad89ba17c902df9df0 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168private:
Mathias Agopian17f638b2009-04-16 20:04:08 -0700169 // can't be copied
170 Surface& operator = (Surface& rhs);
171 Surface(const Surface& rhs);
172
173 Surface(const sp<SurfaceControl>& control);
174 void init();
175 ~Surface();
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 friend class SurfaceComposerClient;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700178 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Mathias Agopian321abdb2009-08-14 18:52:17 -0700180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // camera and camcorder need access to the ISurface binder interface for preview
182 friend class Camera;
183 friend class MediaRecorder;
184 // mediaplayer needs access to ISurface for display
185 friend class MediaPlayer;
Andreas Huberccf8b942009-08-07 12:01:29 -0700186 friend class IOMX;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700187 // this is just to be able to write some unit tests
188 friend class Test;
189
190 sp<SurfaceComposerClient> getClient() const;
191 sp<ISurface> getISurface() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Mathias Agopian5cec4742009-08-11 22:34:02 -0700193 status_t getBufferLocked(int index, int usage);
Mathias Agopian1473f462009-04-10 14:24:30 -0700194
Mathias Agopian18e02602009-11-13 15:26:29 -0800195 status_t validate() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Mathias Agopian6950e422009-10-05 17:07:12 -0700197 inline const GraphicBufferMapper& getBufferMapper() const { return mBufferMapper; }
198 inline GraphicBufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian1473f462009-04-10 14:24:30 -0700199
Mathias Agopian1473f462009-04-10 14:24:30 -0700200 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian1473f462009-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 Agopian5b5c9142009-07-30 18:14:56 -0700204 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian5cec4742009-08-11 22:34:02 -0700205 static int perform(android_native_window_t* 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 Agopian6950e422009-10-05 17:07:12 -0700213 status_t dequeueBuffer(sp<GraphicBuffer>* buffer);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700214
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800215 void dispatch_setUsage(va_list args);
216 int dispatch_connect(va_list args);
217 int dispatch_disconnect(va_list args);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700218 int dispatch_crop(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 Agopian2f7540e2010-03-11 15:06:54 -0800224
Mathias Agopiane611a6e2009-09-15 19:10:47 -0700225 uint32_t getUsage() const;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800226 int getConnectedApi() const;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700227
228 // constants
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 sp<SurfaceComposerClient> mClient;
230 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 SurfaceID mToken;
232 uint32_t mIdentity;
233 PixelFormat mFormat;
234 uint32_t mFlags;
Mathias Agopian6950e422009-10-05 17:07:12 -0700235 GraphicBufferMapper& mBufferMapper;
Mathias Agopian9779b222009-09-07 16:32:45 -0700236 SharedBufferClient* mSharedBufferClient;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700237
238 // protected by mSurfaceLock
239 Rect mSwapRectangle;
240 uint32_t mUsage;
Mathias Agopian2f7540e2010-03-11 15:06:54 -0800241 int mConnected;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700242 Rect mNextBufferCrop;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700243
244 // protected by mSurfaceLock. These are also used from lock/unlock
245 // but in that case, they must be called form the same thread.
Mathias Agopian6950e422009-10-05 17:07:12 -0700246 sp<GraphicBuffer> mBuffers[2];
Mathias Agopian321abdb2009-08-14 18:52:17 -0700247 mutable Region mDirtyRegion;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700248
249 // must be used from the lock/unlock thread
Mathias Agopian6950e422009-10-05 17:07:12 -0700250 sp<GraphicBuffer> mLockedBuffer;
251 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700252 mutable Region mOldDirtyRegion;
Mathias Agopiana8a0aa82010-04-21 15:24:11 -0700253 bool mReserved;
Mathias Agopian321abdb2009-08-14 18:52:17 -0700254
255 // query() must be called from dequeueBuffer() thread
256 uint32_t mWidth;
257 uint32_t mHeight;
258
259 // Inherently thread-safe
260 mutable Mutex mSurfaceLock;
Mathias Agopian9779b222009-09-07 16:32:45 -0700261 mutable Mutex mApiLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262};
263
264}; // namespace android
265
Mathias Agopian000479f2010-02-09 17:46:37 -0800266#endif // ANDROID_SF_SURFACE_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267