blob: 16d2edb8e223af85b7238da3f0f52495fd3dab95 [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
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 Agopian1473f462009-04-10 14:24:30 -070031#include <EGL/android_natives.h>
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
37class Rect;
38class SurfaceComposerClient;
Mathias Agopian1473f462009-04-10 14:24:30 -070039struct per_client_cblk_t;
40struct layer_cblk_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Mathias Agopian1473f462009-04-10 14:24:30 -070042// ---------------------------------------------------------------------------
43
44class SurfaceBuffer
45 : public EGLNativeBase<
46 android_native_buffer_t,
47 SurfaceBuffer,
48 LightRefBase<SurfaceBuffer> >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049{
Mathias Agopian1473f462009-04-10 14:24:30 -070050public:
51 buffer_handle_t getHandle() const {
52 return handle;
53 }
54
55protected:
56 SurfaceBuffer();
57 SurfaceBuffer(const Parcel& reply);
58 virtual ~SurfaceBuffer();
59 buffer_handle_t handle;
60 bool mOwner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
Mathias Agopian1473f462009-04-10 14:24:30 -070062private:
63 friend class BpSurface;
64 friend class BnSurface;
65 friend class LightRefBase<SurfaceBuffer>;
66
67 SurfaceBuffer& operator = (const SurfaceBuffer& rhs);
68 const SurfaceBuffer& operator = (const SurfaceBuffer& rhs) const;
69
70 static status_t writeToParcel(Parcel* reply,
71 android_native_buffer_t const* buffer);
72
73 static int getHandle(android_native_buffer_t const * base,
74 buffer_handle_t* handle);
75};
76
77// ---------------------------------------------------------------------------
Mathias Agopian17f638b2009-04-16 20:04:08 -070078class Surface;
Mathias Agopian1473f462009-04-10 14:24:30 -070079
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070080class SurfaceControl : public RefBase
81{
82public:
83 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070084 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070085 }
Mathias Agopian17f638b2009-04-16 20:04:08 -070086 bool isValid() {
87 return mToken>=0 && mClient!=0;
88 }
89 static bool isSameSurface(
90 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070091
92 SurfaceID ID() const { return mToken; }
93 uint32_t getFlags() const { return mFlags; }
94 uint32_t getIdentity() const { return mIdentity; }
95
96 // release surface data from java
97 void clear();
98
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070099 status_t setLayer(int32_t layer);
100 status_t setPosition(int32_t x, int32_t y);
101 status_t setSize(uint32_t w, uint32_t h);
102 status_t hide();
103 status_t show(int32_t layer = -1);
104 status_t freeze();
105 status_t unfreeze();
106 status_t setFlags(uint32_t flags, uint32_t mask);
107 status_t setTransparentRegionHint(const Region& transparent);
108 status_t setAlpha(float alpha=1.0f);
109 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
110 status_t setFreezeTint(uint32_t tint);
111
Mathias Agopian17f638b2009-04-16 20:04:08 -0700112 static status_t writeSurfaceToParcel(
113 const sp<SurfaceControl>& control, Parcel* parcel);
114
115 sp<Surface> getSurface() const;
116
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700117private:
Mathias Agopian17f638b2009-04-16 20:04:08 -0700118 // can't be copied
119 SurfaceControl& operator = (SurfaceControl& rhs);
120 SurfaceControl(const SurfaceControl& rhs);
121
122
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700123 friend class SurfaceComposerClient;
124
125 // camera and camcorder need access to the ISurface binder interface for preview
126 friend class Camera;
127 friend class MediaRecorder;
128 // mediaplayer needs access to ISurface for display
129 friend class MediaPlayer;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700130 // for testing
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700131 friend class Test;
132 const sp<ISurface>& getISurface() const { return mSurface; }
133
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700134
135 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700136
137 SurfaceControl(
138 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700139 const sp<ISurface>& surface,
140 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700141 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700142
143 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700144
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700145 status_t validate(per_client_cblk_t const* cblk) const;
146 void destroy();
147
148 sp<SurfaceComposerClient> mClient;
149 sp<ISurface> mSurface;
150 SurfaceID mToken;
151 uint32_t mIdentity;
152 PixelFormat mFormat;
153 uint32_t mFlags;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700154 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700155
156 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700157};
158
159// ---------------------------------------------------------------------------
160
Mathias Agopian1473f462009-04-10 14:24:30 -0700161class Surface
162 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
163{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164public:
165 struct SurfaceInfo {
166 uint32_t w;
167 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700168 uint32_t s;
169 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 PixelFormat format;
171 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 uint32_t reserved[2];
173 };
174
Mathias Agopian17f638b2009-04-16 20:04:08 -0700175 Surface(const Parcel& data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Mathias Agopian17f638b2009-04-16 20:04:08 -0700177 static bool isValid(const sp<Surface>& surface) {
178 return (surface != 0) && surface->isValid();
179 }
180 bool isValid() {
181 return mToken>=0 && mClient!=0;
182 }
183 static bool isSameSurface(
184 const sp<Surface>& lhs, const sp<Surface>& rhs);
185 SurfaceID ID() const { return mToken; }
186 uint32_t getFlags() const { return mFlags; }
187 uint32_t getIdentity() const { return mIdentity; }
188
Mathias Agopian402c3462009-04-14 18:21:47 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 status_t lock(SurfaceInfo* info, bool blocking = true);
191 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
192 status_t unlockAndPost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194 // setSwapRectangle() is mainly used by EGL
195 void setSwapRectangle(const Rect& r);
196 const Rect& swapRectangle() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198private:
Mathias Agopian17f638b2009-04-16 20:04:08 -0700199 // can't be copied
200 Surface& operator = (Surface& rhs);
201 Surface(const Surface& rhs);
202
203 Surface(const sp<SurfaceControl>& control);
204 void init();
205 ~Surface();
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 friend class SurfaceComposerClient;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700208 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
210 // camera and camcorder need access to the ISurface binder interface for preview
211 friend class Camera;
212 friend class MediaRecorder;
213 // mediaplayer needs access to ISurface for display
214 friend class MediaPlayer;
215 friend class Test;
216 const sp<ISurface>& getISurface() const { return mSurface; }
217
Mathias Agopian1473f462009-04-10 14:24:30 -0700218 status_t getBufferLocked(int index);
219
Mathias Agopian1473f462009-04-10 14:24:30 -0700220
Mathias Agopian17f638b2009-04-16 20:04:08 -0700221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 Region dirtyRegion() const;
223 void setDirtyRegion(const Region& region) const;
224
Mathias Agopian1473f462009-04-10 14:24:30 -0700225
226 status_t validate(per_client_cblk_t const* cblk) const;
227 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Mathias Agopian1473f462009-04-10 14:24:30 -0700229
230 static void connect(android_native_window_t* window);
231 static void disconnect(android_native_window_t* window);
232 static int setSwapInterval(android_native_window_t* window, int interval);
233 static int setSwapRectangle(android_native_window_t* window,
234 int l, int t, int w, int h);
235 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
236 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
237 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
238
239 int dequeueBuffer(android_native_buffer_t** buffer);
240 int lockBuffer(android_native_buffer_t* buffer);
241 int queueBuffer(android_native_buffer_t* buffer);
242
243
244 alloc_device_t* mAllocDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 sp<SurfaceComposerClient> mClient;
246 sp<ISurface> mSurface;
Mathias Agopian1473f462009-04-10 14:24:30 -0700247 sp<SurfaceBuffer> mBuffers[2];
248 android_native_buffer_t* mLockedBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 SurfaceID mToken;
250 uint32_t mIdentity;
251 PixelFormat mFormat;
252 uint32_t mFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 mutable Region mDirtyRegion;
254 mutable Rect mSwapRectangle;
255 mutable uint8_t mBackbufferIndex;
256 mutable Mutex mSurfaceLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257};
258
259}; // namespace android
260
261#endif // ANDROID_UI_SURFACE_H
262