blob: 8845dc9e1cb0e09f7e7d308d6b2f02505d600ea1 [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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070044class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class SurfaceComposerClient;
Mathias Agopiana67932f2011-04-20 14:20:59 -070046class SurfaceTextureClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048// ---------------------------------------------------------------------------
49
Mathias Agopian62185b72009-04-16 16:19:50 -070050class SurfaceControl : public RefBase
51{
52public:
53 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070054 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070055 }
Mathias Agopian01b76682009-04-16 20:04:08 -070056 bool isValid() {
57 return mToken>=0 && mClient!=0;
58 }
59 static bool isSameSurface(
60 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070061
Mathias Agopian62185b72009-04-16 16:19:50 -070062 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
Jamie Gennis89c2dd22010-08-10 16:37:53 -070095 friend class CameraService;
Mathias Agopian62185b72009-04-16 16:19:50 -070096 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;
Dharmaray Kundargib6783162011-01-16 16:43:20 -0800101 // videoEditor preview classes
102 friend class VideoEditorPreviewController;
Mathias Agopian62185b72009-04-16 16:19:50 -0700103 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700104
105 SurfaceControl(
106 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700107 const sp<ISurface>& surface,
Mathias Agopian7e27f052010-05-28 14:22:23 -0700108 const ISurfaceComposerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700109 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700110
111 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700112
Mathias Agopian963abad2009-11-13 15:26:29 -0800113 status_t validate() const;
Mathias Agopian62185b72009-04-16 16:19:50 -0700114 void destroy();
115
116 sp<SurfaceComposerClient> mClient;
117 sp<ISurface> mSurface;
118 SurfaceID mToken;
119 uint32_t mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700120 uint32_t mWidth;
121 uint32_t mHeight;
Mathias Agopian62185b72009-04-16 16:19:50 -0700122 PixelFormat mFormat;
123 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700124 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700125
126 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700127};
128
129// ---------------------------------------------------------------------------
130
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131class Surface
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700132 : public EGLNativeBase<ANativeWindow, Surface, RefBase>
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134public:
135 struct SurfaceInfo {
136 uint32_t w;
137 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 uint32_t s;
139 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 PixelFormat format;
141 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 uint32_t reserved[2];
143 };
144
Mathias Agopian579b3f82010-06-08 19:54:15 -0700145 static status_t writeToParcel(
146 const sp<Surface>& control, Parcel* parcel);
147
Jamie Gennisaca4e222010-07-15 17:29:15 -0700148 static sp<Surface> readFromParcel(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 Agopianba5972f2009-08-14 18:52:17 -0700154 bool isValid();
Mathias Agopianba5972f2009-08-14 18:52:17 -0700155 uint32_t getFlags() const { return mFlags; }
Mathias Agopian01b76682009-04-16 20:04:08 -0700156 uint32_t getIdentity() const { return mIdentity; }
157
Mathias Agopianba5972f2009-08-14 18:52:17 -0700158 // the lock/unlock APIs must be used from the same thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 status_t lock(SurfaceInfo* info, bool blocking = true);
160 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
161 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
Mathias Agopian47d87302011-04-05 15:44:20 -0700163 sp<IBinder> asBinder() const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165private:
Mathias Agopianb2965332010-04-27 16:41:19 -0700166 /*
167 * Android frameworks friends
168 * (eventually this should go away and be replaced by proper APIs)
169 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 // camera and camcorder need access to the ISurface binder interface for preview
Jamie Gennis89c2dd22010-08-10 16:37:53 -0700171 friend class CameraService;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 friend class MediaRecorder;
Mathias Agopianb2965332010-04-27 16:41:19 -0700173 // MediaPlayer needs access to ISurface for display
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 friend class MediaPlayer;
Andreas Hubere1864312009-08-07 12:01:29 -0700175 friend class IOMX;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700176 friend class SoftwareRenderer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700177 // this is just to be able to write some unit tests
178 friend class Test;
Dharmaray Kundargib6783162011-01-16 16:43:20 -0800179 // videoEditor preview classes
180 friend class VideoEditorPreviewController;
181 friend class PreviewRenderer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700182
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);
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700192 Surface(const Parcel& data, const sp<IBinder>& ref);
Mathias Agopianb2965332010-04-27 16:41:19 -0700193 ~Surface();
194
195
196 /*
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700197 * ANativeWindow hooks
Mathias Agopianb2965332010-04-27 16:41:19 -0700198 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700199 static int setSwapInterval(ANativeWindow* window, int interval);
Iliyan Malchev697526b2011-05-01 11:33:26 -0700200 static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
201 static int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
202 static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
203 static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
Iliyan Malchev41abd672011-04-14 16:54:38 -0700204 static int query(const ANativeWindow* window, int what, int* value);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700205 static int perform(ANativeWindow* window, int operation, ...);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206
Mathias Agopiana67932f2011-04-20 14:20:59 -0700207 int setSwapInterval(int interval);
Iliyan Malchev697526b2011-05-01 11:33:26 -0700208 int dequeueBuffer(ANativeWindowBuffer** buffer);
209 int lockBuffer(ANativeWindowBuffer* buffer);
210 int queueBuffer(ANativeWindowBuffer* buffer);
211 int cancelBuffer(ANativeWindowBuffer* buffer);
Iliyan Malchev41abd672011-04-14 16:54:38 -0700212 int query(int what, int* value) const;
Mathias Agopian52212712009-08-11 22:34:02 -0700213 int perform(int operation, va_list args);
Mathias Agopian0926f502009-05-04 14:17:04 -0700214
Mathias Agopianb2965332010-04-27 16:41:19 -0700215 /*
216 * private stuff...
217 */
218 void init();
Mathias Agopiana317f1b2011-01-14 11:04:34 -0800219 status_t validate(bool inCancelBuffer = false) const;
Mathias Agopianb2965332010-04-27 16:41:19 -0700220
Mathias Agopiana138f892010-05-21 17:24:35 -0700221 int getConnectedApi() const;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700222
Mathias Agopian455d18d2010-12-13 16:47:31 -0800223 static void cleanCachedSurfacesLocked();
Jamie Gennisaca4e222010-07-15 17:29:15 -0700224
Mathias Agopianba5972f2009-08-14 18:52:17 -0700225 // constants
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700226 status_t mInitCheck;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 sp<ISurface> mSurface;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700228 sp<SurfaceTextureClient> mSurfaceTextureClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 uint32_t mIdentity;
230 PixelFormat mFormat;
231 uint32_t mFlags;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700232
233 // protected by mSurfaceLock. These are also used from lock/unlock
234 // but in that case, they must be called form the same thread.
Mathias Agopianba5972f2009-08-14 18:52:17 -0700235 mutable Region mDirtyRegion;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700236
237 // must be used from the lock/unlock thread
Mathias Agopian3330b202009-10-05 17:07:12 -0700238 sp<GraphicBuffer> mLockedBuffer;
239 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700240 mutable Region mOldDirtyRegion;
Mathias Agopian245e4d72010-04-21 15:24:11 -0700241 bool mReserved;
Mathias Agopianba5972f2009-08-14 18:52:17 -0700242
243 // query() must be called from dequeueBuffer() thread
244 uint32_t mWidth;
245 uint32_t mHeight;
246
247 // Inherently thread-safe
248 mutable Mutex mSurfaceLock;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700249 mutable Mutex mApiLock;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700250
251 // A cache of Surface objects that have been deserialized into this process.
252 static Mutex sCachedSurfacesLock;
253 static DefaultKeyedVector<wp<IBinder>, wp<Surface> > sCachedSurfaces;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254};
255
256}; // namespace android
257
Mathias Agopian9cce3252010-02-09 17:46:37 -0800258#endif // ANDROID_SF_SURFACE_H