blob: 156d453b2d20c783a6af85598ed912d5a0ed7ed8 [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 Agopianac2523b2009-05-05 18:11:11 -070031#include <ui/egl/android_natives.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
Mathias Agopiandff8e582009-05-04 14:17:04 -070037class BufferMapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038class Rect;
Mathias Agopiandff8e582009-05-04 14:17:04 -070039class Surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040class SurfaceComposerClient;
Mathias Agopian1473f462009-04-10 14:24:30 -070041struct per_client_cblk_t;
42struct layer_cblk_t;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Mathias Agopian1473f462009-04-10 14:24:30 -070044// ---------------------------------------------------------------------------
45
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070046class SurfaceControl : public RefBase
47{
48public:
49 static bool isValid(const sp<SurfaceControl>& surface) {
Mathias Agopian17f638b2009-04-16 20:04:08 -070050 return (surface != 0) && surface->isValid();
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070051 }
Mathias Agopian17f638b2009-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 Agopian6d2c0bc2009-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 Agopian6d2c0bc2009-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 Agopian17f638b2009-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 Agopian6d2c0bc2009-04-16 16:19:50 -070083private:
Mathias Agopian17f638b2009-04-16 20:04:08 -070084 // can't be copied
85 SurfaceControl& operator = (SurfaceControl& rhs);
86 SurfaceControl(const SurfaceControl& rhs);
87
88
Mathias Agopian6d2c0bc2009-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 Agopian17f638b2009-04-16 20:04:08 -070096 // for testing
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -070097 friend class Test;
98 const sp<ISurface>& getISurface() const { return mSurface; }
99
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700100
101 friend class Surface;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700102
103 SurfaceControl(
104 const sp<SurfaceComposerClient>& client,
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700105 const sp<ISurface>& surface,
106 const ISurfaceFlingerClient::surface_data_t& data,
Mathias Agopian69d62092009-04-16 20:30:22 -0700107 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700108
109 ~SurfaceControl();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700110
Mathias Agopian6d2c0bc2009-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;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700118 uint32_t mWidth;
119 uint32_t mHeight;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700120 PixelFormat mFormat;
121 uint32_t mFlags;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700122 mutable Mutex mLock;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700123
124 mutable sp<Surface> mSurfaceData;
Mathias Agopian6d2c0bc2009-04-16 16:19:50 -0700125};
126
127// ---------------------------------------------------------------------------
128
Mathias Agopian1473f462009-04-10 14:24:30 -0700129class Surface
130 : public EGLNativeBase<android_native_window_t, Surface, RefBase>
131{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132public:
133 struct SurfaceInfo {
134 uint32_t w;
135 uint32_t h;
Mathias Agopian1473f462009-04-10 14:24:30 -0700136 uint32_t s;
137 uint32_t usage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 PixelFormat format;
139 void* bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 uint32_t reserved[2];
141 };
142
Mathias Agopian17f638b2009-04-16 20:04:08 -0700143 Surface(const Parcel& data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Mathias Agopian17f638b2009-04-16 20:04:08 -0700145 static bool isValid(const sp<Surface>& surface) {
146 return (surface != 0) && surface->isValid();
147 }
148 bool isValid() {
149 return mToken>=0 && mClient!=0;
150 }
151 static bool isSameSurface(
152 const sp<Surface>& lhs, const sp<Surface>& rhs);
153 SurfaceID ID() const { return mToken; }
154 uint32_t getFlags() const { return mFlags; }
155 uint32_t getIdentity() const { return mIdentity; }
156
Mathias Agopian402c3462009-04-14 18:21:47 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 status_t lock(SurfaceInfo* info, bool blocking = true);
159 status_t lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
160 status_t unlockAndPost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Mathias Agopiandff8e582009-05-04 14:17:04 -0700162 // setSwapRectangle() is intended to be used by GL ES clients
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 void setSwapRectangle(const Rect& r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165private:
Mathias Agopian17f638b2009-04-16 20:04:08 -0700166 // can't be copied
167 Surface& operator = (Surface& rhs);
168 Surface(const Surface& rhs);
169
170 Surface(const sp<SurfaceControl>& control);
171 void init();
172 ~Surface();
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 friend class SurfaceComposerClient;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700175 friend class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 // camera and camcorder need access to the ISurface binder interface for preview
178 friend class Camera;
179 friend class MediaRecorder;
180 // mediaplayer needs access to ISurface for display
181 friend class MediaPlayer;
182 friend class Test;
183 const sp<ISurface>& getISurface() const { return mSurface; }
184
Mathias Agopian1473f462009-04-10 14:24:30 -0700185 status_t getBufferLocked(int index);
Mathias Agopian1473f462009-04-10 14:24:30 -0700186
187 status_t validate(per_client_cblk_t const* cblk) const;
188 static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Mathias Agopiandff8e582009-05-04 14:17:04 -0700190 inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
191 inline BufferMapper& getBufferMapper() { return mBufferMapper; }
Mathias Agopian1473f462009-04-10 14:24:30 -0700192
Mathias Agopian1473f462009-04-10 14:24:30 -0700193 static int setSwapInterval(android_native_window_t* window, int interval);
Mathias Agopian1473f462009-04-10 14:24:30 -0700194 static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
195 static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
196 static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700197 static int query(android_native_window_t* window, int what, int* value);
Mathias Agopian1473f462009-04-10 14:24:30 -0700198
199 int dequeueBuffer(android_native_buffer_t** buffer);
200 int lockBuffer(android_native_buffer_t* buffer);
201 int queueBuffer(android_native_buffer_t* buffer);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700202 int query(int what, int* value);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700203
204 status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
205 status_t lockBuffer(const sp<SurfaceBuffer>& buffer);
206 status_t queueBuffer(const sp<SurfaceBuffer>& buffer);
207
Mathias Agopian1473f462009-04-10 14:24:30 -0700208
209 alloc_device_t* mAllocDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 sp<SurfaceComposerClient> mClient;
211 sp<ISurface> mSurface;
Mathias Agopian1473f462009-04-10 14:24:30 -0700212 sp<SurfaceBuffer> mBuffers[2];
Mathias Agopiandff8e582009-05-04 14:17:04 -0700213 sp<SurfaceBuffer> mLockedBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 SurfaceID mToken;
215 uint32_t mIdentity;
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700216 uint32_t mWidth;
217 uint32_t mHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 PixelFormat mFormat;
219 uint32_t mFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 mutable Region mDirtyRegion;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700221 mutable Region mOldDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 mutable uint8_t mBackbufferIndex;
223 mutable Mutex mSurfaceLock;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700224 Rect mSwapRectangle;
225 BufferMapper& mBufferMapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226};
227
228}; // namespace android
229
230#endif // ANDROID_UI_SURFACE_H
231