blob: d5dad31d00b6fdabb0a1169b4b8424ac8b6825a4 [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 Agopian7189c002009-05-05 18:11:11 -070031#include <ui/egl/android_natives.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
Mathias Agopian0926f502009-05-04 14:17:04 -070037class BufferMapper;
Andreas Hubere1864312009-08-07 12:01:29 -070038class IOMX;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039class Rect;
Mathias Agopian0926f502009-05-04 14:17:04 -070040class Surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041class SurfaceComposerClient;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042struct per_client_cblk_t;
43struct layer_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045// ---------------------------------------------------------------------------
46
Mathias Agopian62185b72009-04-16 16:19:50 -070047class SurfaceControl : public RefBase
48{
49public:
50 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070051 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070052 }
Mathias Agopian01b76682009-04-16 20:04:08 -070053 bool isValid() {
54 return mToken>=0 && mClient!=0;
55 }
56 static bool isSameSurface(
57 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070058
59 SurfaceID ID() const { return mToken; }
60 uint32_t getFlags() const { return mFlags; }
61 uint32_t getIdentity() const { return mIdentity; }
62
63 // release surface data from java
64 void clear();
65
Mathias Agopian62185b72009-04-16 16:19:50 -070066 status_t setLayer(int32_t layer);
67 status_t setPosition(int32_t x, int32_t y);
68 status_t setSize(uint32_t w, uint32_t h);
69 status_t hide();
70 status_t show(int32_t layer = -1);
71 status_t freeze();
72 status_t unfreeze();
73 status_t setFlags(uint32_t flags, uint32_t mask);
74 status_t setTransparentRegionHint(const Region& transparent);
75 status_t setAlpha(float alpha=1.0f);
76 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
77 status_t setFreezeTint(uint32_t tint);
78
Mathias Agopian01b76682009-04-16 20:04:08 -070079 static status_t writeSurfaceToParcel(
80 const sp<SurfaceControl>& control, Parcel* parcel);
81
82 sp<Surface> getSurface() const;
83
Mathias Agopian62185b72009-04-16 16:19:50 -070084private:
Mathias Agopian01b76682009-04-16 20:04:08 -070085 // can't be copied
86 SurfaceControl& operator = (SurfaceControl& rhs);
87 SurfaceControl(const SurfaceControl& rhs);
88
89
Mathias Agopian62185b72009-04-16 16:19:50 -070090 friend class SurfaceComposerClient;
91
92 // camera and camcorder need access to the ISurface binder interface for preview
93 friend class Camera;
94 friend class MediaRecorder;
95 // mediaplayer needs access to ISurface for display
96 friend class MediaPlayer;
Mathias Agopian01b76682009-04-16 20:04:08 -070097 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -070098 friend class Test;
99 const sp<ISurface>& getISurface() const { return mSurface; }
100
Mathias Agopian62185b72009-04-16 16:19:50 -0700101
102 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700103
104 SurfaceControl(
105 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700106 const sp<ISurface>& surface,
107 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700109
110 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700111
Mathias Agopian62185b72009-04-16 16:19:50 -0700112 status_t validate(per_client_cblk_t const* cblk) const;
113 void destroy();
114
115 sp<SurfaceComposerClient> mClient;
116 sp<ISurface> mSurface;
117 SurfaceID mToken;
118 uint32_t mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700119 uint32_t mWidth;
120 uint32_t mHeight;
Mathias Agopian62185b72009-04-16 16:19:50 -0700121 PixelFormat mFormat;
122 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700123 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700124
125 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700126};
127
128// ---------------------------------------------------------------------------
129
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130class Surface
131 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
132{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133public:
134 struct SurfaceInfo {
135 uint32_t w;
136 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 uint32_t s;
138 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 PixelFormat format;
140 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 uint32_t reserved[2];
142 };
143
Mathias Agopian01b76682009-04-16 20:04:08 -0700144 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
Mathias Agopian01b76682009-04-16 20:04:08 -0700146 static bool isValid(const sp<Surface>& surface) {
147 return (surface != 0) && surface->isValid();
148 }
149 bool isValid() {
150 return mToken>=0 && mClient!=0;
151 }
152 static bool isSameSurface(
153 const sp<Surface>& lhs, const sp<Surface>& rhs);
154 SurfaceID ID() const { return mToken; }
155 uint32_t getFlags() const { return mFlags; }
156 uint32_t getIdentity() const { return mIdentity; }
157
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700158
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 Agopian0926f502009-05-04 14:17:04 -0700163 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700167 // can't be copied
168 Surface& operator = (Surface& rhs);
169 Surface(const Surface& rhs);
170
171 Surface(const sp<SurfaceControl>& control);
172 void init();
173 ~Surface();
174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 friend class SurfaceComposerClient;
Mathias Agopian01b76682009-04-16 20:04:08 -0700176 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177
178 // camera and camcorder need access to the ISurface binder interface for preview
179 friend class Camera;
180 friend class MediaRecorder;
181 // mediaplayer needs access to ISurface for display
182 friend class MediaPlayer;
183 friend class Test;
Andreas Hubere1864312009-08-07 12:01:29 -0700184 friend class IOMX;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 const sp<ISurface>& getISurface() const { return mSurface; }
186
Fred Quintanab2fd4662009-08-11 20:49:35 -0700187 status_t getBufferLocked(int index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700188
189 status_t validate(per_client_cblk_t const* cblk) const;
190 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191
Mathias Agopian0926f502009-05-04 14:17:04 -0700192 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
193 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700195 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
197 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
198 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700199 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200
201 int dequeueBuffer(android_native_buffer_t** buffer);
202 int lockBuffer(android_native_buffer_t* buffer);
203 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700204 int query(int what, int* value);
Mathias Agopian0926f502009-05-04 14:17:04 -0700205
206 status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
207 status_t lockBuffer(const sp<SurfaceBuffer>& buffer);
208 status_t queueBuffer(const sp<SurfaceBuffer>& buffer);
209
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210
211 alloc_device_t* mAllocDevice;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 sp<SurfaceComposerClient> mClient;
213 sp<ISurface> mSurface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 sp<SurfaceBuffer> mBuffers[2];
Mathias Agopian0926f502009-05-04 14:17:04 -0700215 sp<SurfaceBuffer> mLockedBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 SurfaceID mToken;
217 uint32_t mIdentity;
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700218 uint32_t mWidth;
219 uint32_t mHeight;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 PixelFormat mFormat;
221 uint32_t mFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 mutable Region mDirtyRegion;
Mathias Agopian0926f502009-05-04 14:17:04 -0700223 mutable Region mOldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 mutable uint8_t mBackbufferIndex;
225 mutable Mutex mSurfaceLock;
Mathias Agopian0926f502009-05-04 14:17:04 -0700226 Rect mSwapRectangle;
227 BufferMapper& mBufferMapper;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228};
229
230}; // namespace android
231
232#endif // ANDROID_UI_SURFACE_H
233