blob: 8c4f63ddfec09e9f55d1c7d0f97286c95a71f556 [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;
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
Mathias Agopian62185b72009-04-16 16:19:50 -070046class SurfaceControl : public RefBase
47{
48public:
49 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian01b76682009-04-16 20:04:08 -070050 return (surface != 0) && surface->isValid();
Mathias Agopian62185b72009-04-16 16:19:50 -070051 }
Mathias Agopian01b76682009-04-16 20:04:08 -070052 bool isValid() {
53 return mToken>=0 && mClient!=0;
54 }
55 static bool isSameSurface(
56 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs);
Mathias Agopian62185b72009-04-16 16:19:50 -070057
58 SurfaceID ID() const { return mToken; }
59 uint32_t getFlags() const { return mFlags; }
60 uint32_t getIdentity() const { return mIdentity; }
61
62 // release surface data from java
63 void clear();
64
Mathias Agopian62185b72009-04-16 16:19:50 -070065 status_t setLayer(int32_t layer);
66 status_t setPosition(int32_t x, int32_t y);
67 status_t setSize(uint32_t w, uint32_t h);
68 status_t hide();
69 status_t show(int32_t layer = -1);
70 status_t freeze();
71 status_t unfreeze();
72 status_t setFlags(uint32_t flags, uint32_t mask);
73 status_t setTransparentRegionHint(const Region& transparent);
74 status_t setAlpha(float alpha=1.0f);
75 status_t setMatrix(float dsdx, float dtdx, float dsdy, float dtdy);
76 status_t setFreezeTint(uint32_t tint);
77
Mathias Agopian01b76682009-04-16 20:04:08 -070078 static status_t writeSurfaceToParcel(
79 const sp<SurfaceControl>& control, Parcel* parcel);
80
81 sp<Surface> getSurface() const;
82
Mathias Agopian62185b72009-04-16 16:19:50 -070083private:
Mathias Agopian01b76682009-04-16 20:04:08 -070084 // can't be copied
85 SurfaceControl& operator = (SurfaceControl& rhs);
86 SurfaceControl(const SurfaceControl& rhs);
87
88
Mathias Agopian62185b72009-04-16 16:19:50 -070089 friend class SurfaceComposerClient;
90
91 // camera and camcorder need access to the ISurface binder interface for preview
92 friend class Camera;
93 friend class MediaRecorder;
94 // mediaplayer needs access to ISurface for display
95 friend class MediaPlayer;
Mathias Agopian01b76682009-04-16 20:04:08 -070096 // for testing
Mathias Agopian62185b72009-04-16 16:19:50 -070097 friend class Test;
98 const sp<ISurface>& getISurface() const { return mSurface; }
99
Mathias Agopian62185b72009-04-16 16:19:50 -0700100
101 friend class Surface;
Mathias Agopian01b76682009-04-16 20:04:08 -0700102
103 SurfaceControl(
104 const sp<SurfaceComposerClient>& client,
Mathias Agopian62185b72009-04-16 16:19:50 -0700105 const sp<ISurface>& surface,
106 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian18d84462009-04-16 20:30:22 -0700107 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian62185b72009-04-16 16:19:50 -0700108
109 ~SurfaceControl();
Mathias Agopian01b76682009-04-16 20:04:08 -0700110
Mathias Agopian62185b72009-04-16 16:19:50 -0700111 status_t validate(per_client_cblk_t const* cblk) const;
112 void destroy();
113
114 sp<SurfaceComposerClient> mClient;
115 sp<ISurface> mSurface;
116 SurfaceID mToken;
117 uint32_t mIdentity;
118 PixelFormat mFormat;
119 uint32_t mFlags;
Mathias Agopian62185b72009-04-16 16:19:50 -0700120 mutable Mutex mLock;
Mathias Agopian01b76682009-04-16 20:04:08 -0700121
122 mutable sp<Surface> mSurfaceData;
Mathias Agopian62185b72009-04-16 16:19:50 -0700123};
124
125// ---------------------------------------------------------------------------
126
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700127class Surface
128 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
129{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130public:
131 struct SurfaceInfo {
132 uint32_t w;
133 uint32_t h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 uint32_t s;
135 uint32_t usage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 PixelFormat format;
137 void* bits;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 uint32_t reserved[2];
139 };
140
Mathias Agopian01b76682009-04-16 20:04:08 -0700141 Surface(const Parcel& data);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142
Mathias Agopian01b76682009-04-16 20:04:08 -0700143 static bool isValid(const sp<Surface>& surface) {
144 return (surface != 0) && surface->isValid();
145 }
146 bool isValid() {
147 return mToken>=0 && mClient!=0;
148 }
149 static bool isSameSurface(
150 const sp<Surface>& lhs, const sp<Surface>& rhs);
151 SurfaceID ID() const { return mToken; }
152 uint32_t getFlags() const { return mFlags; }
153 uint32_t getIdentity() const { return mIdentity; }
154
Mathias Agopian40b7f6e2009-04-14 18:21:47 -0700155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 status_t lock(SurfaceInfo* info, bool blocking = true);
157 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
158 status_t unlockAndPost();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159
Mathias Agopian0926f502009-05-04 14:17:04 -0700160 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 void setSwapRectangle(const Rect& r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163private:
Mathias Agopian01b76682009-04-16 20:04:08 -0700164 // can't be copied
165 Surface& operator = (Surface& rhs);
166 Surface(const Surface& rhs);
167
168 Surface(const sp<SurfaceControl>& control);
169 void init();
170 ~Surface();
171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 friend class SurfaceComposerClient;
Mathias Agopian01b76682009-04-16 20:04:08 -0700173 friend class SurfaceControl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174
175 // camera and camcorder need access to the ISurface binder interface for preview
176 friend class Camera;
177 friend class MediaRecorder;
178 // mediaplayer needs access to ISurface for display
179 friend class MediaPlayer;
180 friend class Test;
181 const sp<ISurface>& getISurface() const { return mSurface; }
182
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 status_t getBufferLocked(int index);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184
185 status_t validate(per_client_cblk_t const* cblk) const;
186 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187
Mathias Agopian0926f502009-05-04 14:17:04 -0700188 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
189 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700190
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700191 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
193 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
194 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
195
196 int dequeueBuffer(android_native_buffer_t** buffer);
197 int lockBuffer(android_native_buffer_t* buffer);
198 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian0926f502009-05-04 14:17:04 -0700199
200 status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
201 status_t lockBuffer(const sp<SurfaceBuffer>& buffer);
202 status_t queueBuffer(const sp<SurfaceBuffer>& buffer);
203
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204
205 alloc_device_t* mAllocDevice;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 sp<SurfaceComposerClient> mClient;
207 sp<ISurface> mSurface;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208 sp<SurfaceBuffer> mBuffers[2];
Mathias Agopian0926f502009-05-04 14:17:04 -0700209 sp<SurfaceBuffer> mLockedBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 SurfaceID mToken;
211 uint32_t mIdentity;
212 PixelFormat mFormat;
213 uint32_t mFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 mutable Region mDirtyRegion;
Mathias Agopian0926f502009-05-04 14:17:04 -0700215 mutable Region mOldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 mutable uint8_t mBackbufferIndex;
217 mutable Mutex mSurfaceLock;
Mathias Agopian0926f502009-05-04 14:17:04 -0700218 Rect mSwapRectangle;
219 BufferMapper& mBufferMapper;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220};
221
222}; // namespace android
223
224#endif // ANDROID_UI_SURFACE_H
225