blob: dc2a84562082877995ecfec070812bacf70d27ae [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
Jamie Gennisaca4e222010-07-15 17:29:15 -070023#include <utils/KeyedVector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/RefBase.h>
25#include <utils/threads.h>
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
28#include <ui/Region.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070029#include <ui/egl/android_natives.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030
Mathias Agopian9cce3252010-02-09 17:46:37 -080031#include <surfaceflinger/ISurface.h>
Mathias Agopian7e27f052010-05-28 14:22:23 -070032#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080033
Mathias Agopiane4e8cf12010-04-12 16:22:15 -070034#define ANDROID_VIEW_SURFACE_JNI_ID "mNativeSurface"
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036namespace android {
37
38// ---------------------------------------------------------------------------
39
Mathias Agopiana138f892010-05-21 17:24:35 -070040class GraphicBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070041class GraphicBufferMapper;
Andreas Hubere1864312009-08-07 12:01:29 -070042class IOMX;
tedbo1e7fa9e2011-06-22 15:52:53 -070043class ISurfaceTexture;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070045class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046class SurfaceComposerClient;
Mathias Agopiana67932f2011-04-20 14:20:59 -070047class SurfaceTextureClient;
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
Mathias Agopian62185b72009-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 Agopian62185b72009-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 Agopian01b76682009-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 Agopian62185b72009-04-16 16:19:50 -070087private:
Mathias Agopian01b76682009-04-16 20:04:08 -070088 // can't be copied
89 SurfaceControl& operator = (SurfaceControl& rhs);
90 SurfaceControl(const SurfaceControl& rhs);
91
92
Mathias Agopian62185b72009-04-16 16:19:50 -070093 friend class SurfaceComposerClient;
94
95 // camera and camcorder need access to the ISurface binder interface for preview
Jamie Gennis89c2dd22010-08-10 16:37:53 -070096 friend class CameraService;
Mathias Agopian62185b72009-04-16 16:19:50 -070097 friend class MediaRecorder;
98 // mediaplayer needs access to ISurface for display
99 friend class MediaPlayer;
Mathias Agopian01b76682009-04-16 20:04:08 -0700100 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -0700101 friend class Test;
Dharmaray Kundargib6783162011-01-16 16:43:20 -0800102 // videoEditor preview classes
103 friend class VideoEditorPreviewController;
Mathias Agopian62185b72009-04-16 16:19:50 -0700104 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,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700109 const ISurfaceComposerClient::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
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700133 : public EGLNativeBase<ANativeWindow, Surface, RefBase>
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134{
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 Agopian579b3f82010-06-08 19:54:15 -0700146 static status_t writeToParcel(
147 const sp<Surface>& control, Parcel* parcel);
148
Jamie Gennisaca4e222010-07-15 17:29:15 -0700149 static sp<Surface> readFromParcel(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150
Mathias Agopian01b76682009-04-16 20:04:08 -0700151 static bool isValid(const sp<Surface>& surface) {
152 return (surface != 0) && surface->isValid();
153 }
Mathias Agopianba5972f2009-08-14 18:52:17 -0700154
Mathias Agopianba5972f2009-08-14 18:52:17 -0700155 bool isValid();
Mathias Agopianba5972f2009-08-14 18:52:17 -0700156 uint32_t getFlags() const { return mFlags; }
Mathias Agopian01b76682009-04-16 20:04:08 -0700157 uint32_t getIdentity() const { return mIdentity; }
tedbo1e7fa9e2011-06-22 15:52:53 -0700158 sp<ISurfaceTexture> getSurfaceTexture();
Mathias Agopian01b76682009-04-16 20:04:08 -0700159
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 Agopian47d87302011-04-05 15:44:20 -0700165 sp<IBinder> asBinder() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167private:
Mathias Agopianb2965332010-04-27 16:41:19 -0700168 /*
169 * Android frameworks friends
170 * (eventually this should go away and be replaced by proper APIs)
171 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 // camera and camcorder need access to the ISurface binder interface for preview
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700173 friend class CameraService;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 friend class MediaRecorder;
Mathias Agopianb2965332010-04-27 16:41:19 -0700175 // MediaPlayer needs access to ISurface for display
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 friend class MediaPlayer;
Andreas Hubere1864312009-08-07 12:01:29 -0700177 friend class IOMX;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700178 friend class SoftwareRenderer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700179 // this is just to be able to write some unit tests
180 friend class Test;
Dharmaray Kundargib6783162011-01-16 16:43:20 -0800181 // videoEditor preview classes
182 friend class VideoEditorPreviewController;
183 friend class PreviewRenderer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700184
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);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700194 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700195 ~Surface();
196
197
198 /*
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700199 * ANativeWindow hooks
Mathias Agopianb2965332010-04-27 16:41:19 -0700200 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700201 static int setSwapInterval(ANativeWindow* window, int interval);
Iliyan Malchev697526b2011-05-01 11:33:26 -0700202 static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
203 static int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
204 static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
205 static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
Iliyan Malchev41abd672011-04-14 16:54:38 -0700206 static int query(const ANativeWindow* window, int what, int* value);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700207 static int perform(ANativeWindow* window, int operation, ...);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208
Mathias Agopiana67932f2011-04-20 14:20:59 -0700209 int setSwapInterval(int interval);
Iliyan Malchev697526b2011-05-01 11:33:26 -0700210 int dequeueBuffer(ANativeWindowBuffer** buffer);
211 int lockBuffer(ANativeWindowBuffer* buffer);
212 int queueBuffer(ANativeWindowBuffer* buffer);
213 int cancelBuffer(ANativeWindowBuffer* buffer);
Iliyan Malchev41abd672011-04-14 16:54:38 -0700214 int query(int what, int* value) const;
Mathias Agopian52212712009-08-11 22:34:02 -0700215 int perform(int operation, va_list args);
Mathias Agopian0926f502009-05-04 14:17:04 -0700216
Mathias Agopianb2965332010-04-27 16:41:19 -0700217 /*
218 * private stuff...
219 */
220 void init();
Mathias Agopiana317f1b2011-01-14 11:04:34 -0800221 status_t validate(bool inCancelBuffer = false) const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700222
Mathias Agopiana138f892010-05-21 17:24:35 -0700223 int getConnectedApi() const;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700224
Mathias Agopian455d18d2010-12-13 16:47:31 -0800225 static void cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700226
Mathias Agopianba5972f2009-08-14 18:52:17 -0700227 // constants
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700228 status_t mInitCheck;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 sp<ISurface> mSurface;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700230 sp<SurfaceTextureClient> mSurfaceTextureClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 uint32_t mIdentity;
232 PixelFormat mFormat;
233 uint32_t mFlags;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700234
235 // protected by mSurfaceLock. These are also used from lock/unlock
236 // but in that case, they must be called form the same thread.
Mathias Agopianba5972f2009-08-14 18:52:17 -0700237 mutable Region mDirtyRegion;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700238
239 // must be used from the lock/unlock thread
Mathias Agopian3330b202009-10-05 17:07:12 -0700240 sp<GraphicBuffer> mLockedBuffer;
241 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700242 mutable Region mOldDirtyRegion;
Mathias Agopian245e4d72010-04-21 15:24:11 -0700243 bool mReserved;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700244
245 // query() must be called from dequeueBuffer() thread
246 uint32_t mWidth;
247 uint32_t mHeight;
248
249 // Inherently thread-safe
250 mutable Mutex mSurfaceLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700251 mutable Mutex mApiLock;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700252
253 // A cache of Surface objects that have been deserialized into this process.
254 static Mutex sCachedSurfacesLock;
255 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256};
257
258}; // namespace android
259
Mathias Agopian9cce3252010-02-09 17:46:37 -0800260#endif // ANDROID_SF_SURFACE_H