blob: d1ead1d22ad77d2dc2eca63ac087d304c1003706 [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
37class Rect;
38class SurfaceComposerClient;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039struct per_client_cblk_t;
40struct layer_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080049{
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopian076b1cc2009-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 Agopian01b76682009-04-16 20:04:08 -070078class Surface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079
Mathias Agopian62185b72009-04-16 16:19:50 -070080class SurfaceControl : public RefBase
81{
82public:
83 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070084 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070085 }
Mathias Agopian01b76682009-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 Agopian62185b72009-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 Agopian62185b72009-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 Agopian01b76682009-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 Agopian62185b72009-04-16 16:19:50 -0700117private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700118 // can't be copied
119 SurfaceControl& operator = (SurfaceControl& rhs);
120 SurfaceControl(const SurfaceControl& rhs);
121
122
Mathias Agopian62185b72009-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 Agopian01b76682009-04-16 20:04:08 -0700130 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -0700131 friend class Test;
132 const sp<ISurface>& getISurface() const { return mSurface; }
133
Mathias Agopian62185b72009-04-16 16:19:50 -0700134
135 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700136
137 SurfaceControl(
138 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700139 const sp<ISurface>& surface,
140 const ISurfaceFlingerClient::surface_data_t& data,
141 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
142 bool owner = true);
143
144 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700145
Mathias Agopian62185b72009-04-16 16:19:50 -0700146 status_t validate(per_client_cblk_t const* cblk) const;
147 void destroy();
148
149 sp<SurfaceComposerClient> mClient;
150 sp<ISurface> mSurface;
151 SurfaceID mToken;
152 uint32_t mIdentity;
153 PixelFormat mFormat;
154 uint32_t mFlags;
155 const bool mOwner;
156 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700157
158 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700159};
160
161// ---------------------------------------------------------------------------
162
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700163class Surface
164 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
165{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166public:
167 struct SurfaceInfo {
168 uint32_t w;
169 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170 uint32_t s;
171 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 PixelFormat format;
173 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 uint32_t reserved[2];
175 };
176
Mathias Agopian01b76682009-04-16 20:04:08 -0700177 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178
Mathias Agopian01b76682009-04-16 20:04:08 -0700179 static bool isValid(const sp<Surface>& surface) {
180 return (surface != 0) && surface->isValid();
181 }
182 bool isValid() {
183 return mToken>=0 && mClient!=0;
184 }
185 static bool isSameSurface(
186 const sp<Surface>& lhs, const sp<Surface>& rhs);
187 SurfaceID ID() const { return mToken; }
188 uint32_t getFlags() const { return mFlags; }
189 uint32_t getIdentity() const { return mIdentity; }
190
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 status_t lock(SurfaceInfo* info, bool blocking = true);
193 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
194 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195
196 // setSwapRectangle() is mainly used by EGL
197 void setSwapRectangle(const Rect& r);
198 const Rect& swapRectangle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700201 // can't be copied
202 Surface& operator = (Surface& rhs);
203 Surface(const Surface& rhs);
204
205 Surface(const sp<SurfaceControl>& control);
206 void init();
207 ~Surface();
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 friend class SurfaceComposerClient;
Mathias Agopian01b76682009-04-16 20:04:08 -0700210 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211
212 // camera and camcorder need access to the ISurface binder interface for preview
213 friend class Camera;
214 friend class MediaRecorder;
215 // mediaplayer needs access to ISurface for display
216 friend class MediaPlayer;
217 friend class Test;
218 const sp<ISurface>& getISurface() const { return mSurface; }
219
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 status_t getBufferLocked(int index);
221
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222
Mathias Agopian01b76682009-04-16 20:04:08 -0700223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 Region dirtyRegion() const;
225 void setDirtyRegion(const Region& region) const;
226
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227
228 status_t validate(per_client_cblk_t const* cblk) const;
229 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700231
232 static void connect(android_native_window_t* window);
233 static void disconnect(android_native_window_t* window);
234 static int setSwapInterval(android_native_window_t* window, int interval);
235 static int setSwapRectangle(android_native_window_t* window,
236 int l, int t, int w, int h);
237 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
238 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
239 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
240
241 int dequeueBuffer(android_native_buffer_t** buffer);
242 int lockBuffer(android_native_buffer_t* buffer);
243 int queueBuffer(android_native_buffer_t* buffer);
244
245
246 alloc_device_t* mAllocDevice;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 sp<SurfaceComposerClient> mClient;
248 sp<ISurface> mSurface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 sp<SurfaceBuffer> mBuffers[2];
250 android_native_buffer_t* mLockedBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 SurfaceID mToken;
252 uint32_t mIdentity;
253 PixelFormat mFormat;
254 uint32_t mFlags;
255 const bool mOwner;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 mutable Region mDirtyRegion;
257 mutable Rect mSwapRectangle;
258 mutable uint8_t mBackbufferIndex;
259 mutable Mutex mSurfaceLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260};
261
262}; // namespace android
263
264#endif // ANDROID_UI_SURFACE_H
265