blob: 6eb06ae65f3566a9e35d634500009af9b7bc1416 [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
17#ifndef ANDROID_UI_SURFACE_H
18#define ANDROID_UI_SURFACE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/RefBase.h>
24#include <utils/threads.h>
25
26#include <ui/ISurface.h>
27#include <ui/PixelFormat.h>
28#include <ui/Region.h>
29#include <ui/ISurfaceFlingerClient.h>
30
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <EGL/android_natives.h>
32
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
Mathias Agopian0926f502009-05-04 14:17:04 -070037class BufferMapper;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070039class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040class SurfaceComposerClient;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041struct per_client_cblk_t;
42struct layer_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044// ---------------------------------------------------------------------------
45
46class SurfaceBuffer
47 : public EGLNativeBase<
48 android_native_buffer_t,
49 SurfaceBuffer,
50 LightRefBase<SurfaceBuffer> >
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052public:
53 buffer_handle_t getHandle() const {
54 return handle;
55 }
56
Mathias Agopian0926f502009-05-04 14:17:04 -070057 status_t lock(uint32_t usage);
58 status_t lock(uint32_t usage, const Rect& rect);
59 status_t unlock();
60
Mathias Agopian076b1cc2009-04-10 14:24:30 -070061protected:
62 SurfaceBuffer();
63 SurfaceBuffer(const Parcel& reply);
64 virtual ~SurfaceBuffer();
65 buffer_handle_t handle;
66 bool mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopian0926f502009-05-04 14:17:04 -070068 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
69 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
70
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071private:
Mathias Agopian0926f502009-05-04 14:17:04 -070072 friend class Surface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 friend class BpSurface;
74 friend class BnSurface;
75 friend class LightRefBase<SurfaceBuffer>;
76
77 SurfaceBuffer& operator = (const SurfaceBuffer& rhs);
78 const SurfaceBuffer& operator = (const SurfaceBuffer& rhs) const;
79
80 static status_t writeToParcel(Parcel* reply,
81 android_native_buffer_t const* buffer);
82
83 static int getHandle(android_native_buffer_t const * base,
84 buffer_handle_t* handle);
Mathias Agopian0926f502009-05-04 14:17:04 -070085
86 BufferMapper& mBufferMapper;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087};
88
89// ---------------------------------------------------------------------------
Mathias Agopian01b76682009-04-16 20:04:08 -070090class Surface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091
Mathias Agopian62185b72009-04-16 16:19:50 -070092class SurfaceControl : public RefBase
93{
94public:
95 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070096 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070097 }
Mathias Agopian01b76682009-04-16 20:04:08 -070098 bool isValid() {
99 return mToken>=0 && mClient!=0;
100 }
101 static bool isSameSurface(
102 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -0700103
104 SurfaceID ID() const { return mToken; }
105 uint32_t getFlags() const { return mFlags; }
106 uint32_t getIdentity() const { return mIdentity; }
107
108 // release surface data from java
109 void clear();
110
Mathias Agopian62185b72009-04-16 16:19:50 -0700111 status_t setLayer(int32_t layer);
112 status_t setPosition(int32_t x, int32_t y);
113 status_t setSize(uint32_t w, uint32_t h);
114 status_t hide();
115 status_t show(int32_t layer = -1);
116 status_t freeze();
117 status_t unfreeze();
118 status_t setFlags(uint32_t flags, uint32_t mask);
119 status_t setTransparentRegionHint(const Region& transparent);
120 status_t setAlpha(float alpha=1.0f);
121 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
122 status_t setFreezeTint(uint32_t tint);
123
Mathias Agopian01b76682009-04-16 20:04:08 -0700124 static status_t writeSurfaceToParcel(
125 const sp<SurfaceControl>& control, Parcel* parcel);
126
127 sp<Surface> getSurface() const;
128
Mathias Agopian62185b72009-04-16 16:19:50 -0700129private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700130 // can't be copied
131 SurfaceControl& operator = (SurfaceControl& rhs);
132 SurfaceControl(const SurfaceControl& rhs);
133
134
Mathias Agopian62185b72009-04-16 16:19:50 -0700135 friend class SurfaceComposerClient;
136
137 // camera and camcorder need access to the ISurface binder interface for preview
138 friend class Camera;
139 friend class MediaRecorder;
140 // mediaplayer needs access to ISurface for display
141 friend class MediaPlayer;
Mathias Agopian01b76682009-04-16 20:04:08 -0700142 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -0700143 friend class Test;
144 const sp<ISurface>& getISurface() const { return mSurface; }
145
Mathias Agopian62185b72009-04-16 16:19:50 -0700146
147 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700148
149 SurfaceControl(
150 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700151 const sp<ISurface>& surface,
152 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700153 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700154
155 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700156
Mathias Agopian62185b72009-04-16 16:19:50 -0700157 status_t validate(per_client_cblk_t const* cblk) const;
158 void destroy();
159
160 sp<SurfaceComposerClient> mClient;
161 sp<ISurface> mSurface;
162 SurfaceID mToken;
163 uint32_t mIdentity;
164 PixelFormat mFormat;
165 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700166 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700167
168 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700169};
170
171// ---------------------------------------------------------------------------
172
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173class Surface
174 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
175{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176public:
177 struct SurfaceInfo {
178 uint32_t w;
179 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180 uint32_t s;
181 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 PixelFormat format;
183 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 uint32_t reserved[2];
185 };
186
Mathias Agopian01b76682009-04-16 20:04:08 -0700187 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
Mathias Agopian01b76682009-04-16 20:04:08 -0700189 static bool isValid(const sp<Surface>& surface) {
190 return (surface != 0) && surface->isValid();
191 }
192 bool isValid() {
193 return mToken>=0 && mClient!=0;
194 }
195 static bool isSameSurface(
196 const sp<Surface>& lhs, const sp<Surface>& rhs);
197 SurfaceID ID() const { return mToken; }
198 uint32_t getFlags() const { return mFlags; }
199 uint32_t getIdentity() const { return mIdentity; }
200
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 status_t lock(SurfaceInfo* info, bool blocking = true);
203 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
204 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205
Mathias Agopian0926f502009-05-04 14:17:04 -0700206 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700210 // can't be copied
211 Surface& operator = (Surface& rhs);
212 Surface(const Surface& rhs);
213
214 Surface(const sp<SurfaceControl>& control);
215 void init();
216 ~Surface();
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 friend class SurfaceComposerClient;
Mathias Agopian01b76682009-04-16 20:04:08 -0700219 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220
221 // camera and camcorder need access to the ISurface binder interface for preview
222 friend class Camera;
223 friend class MediaRecorder;
224 // mediaplayer needs access to ISurface for display
225 friend class MediaPlayer;
226 friend class Test;
227 const sp<ISurface>& getISurface() const { return mSurface; }
228
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229 status_t getBufferLocked(int index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230
231 status_t validate(per_client_cblk_t const* cblk) const;
232 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
Mathias Agopian0926f502009-05-04 14:17:04 -0700234 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
235 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700236
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700237 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
239 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
240 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
241
242 int dequeueBuffer(android_native_buffer_t** buffer);
243 int lockBuffer(android_native_buffer_t* buffer);
244 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian0926f502009-05-04 14:17:04 -0700245
246 status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
247 status_t lockBuffer(const sp<SurfaceBuffer>& buffer);
248 status_t queueBuffer(const sp<SurfaceBuffer>& buffer);
249
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250
251 alloc_device_t* mAllocDevice;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 sp<SurfaceComposerClient> mClient;
253 sp<ISurface> mSurface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254 sp<SurfaceBuffer> mBuffers[2];
Mathias Agopian0926f502009-05-04 14:17:04 -0700255 sp<SurfaceBuffer> mLockedBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 SurfaceID mToken;
257 uint32_t mIdentity;
258 PixelFormat mFormat;
259 uint32_t mFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 mutable Region mDirtyRegion;
Mathias Agopian0926f502009-05-04 14:17:04 -0700261 mutable Region mOldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 mutable uint8_t mBackbufferIndex;
263 mutable Mutex mSurfaceLock;
Mathias Agopian0926f502009-05-04 14:17:04 -0700264 Rect mSwapRectangle;
265 BufferMapper& mBufferMapper;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266};
267
268}; // namespace android
269
270#endif // ANDROID_UI_SURFACE_H
271