The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 | #include <EGL/android_natives.h> |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | class Rect; |
| 38 | class SurfaceComposerClient; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 39 | struct per_client_cblk_t; |
| 40 | struct layer_cblk_t; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 42 | // --------------------------------------------------------------------------- |
| 43 | |
| 44 | class SurfaceBuffer |
| 45 | : public EGLNativeBase< |
| 46 | android_native_buffer_t, |
| 47 | SurfaceBuffer, |
| 48 | LightRefBase<SurfaceBuffer> > |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | { |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 50 | public: |
| 51 | buffer_handle_t getHandle() const { |
| 52 | return handle; |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | SurfaceBuffer(); |
| 57 | SurfaceBuffer(const Parcel& reply); |
| 58 | virtual ~SurfaceBuffer(); |
| 59 | buffer_handle_t handle; |
| 60 | bool mOwner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 62 | private: |
| 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 Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 78 | class Surface; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 79 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 80 | class SurfaceControl : public RefBase |
| 81 | { |
| 82 | public: |
| 83 | static bool isValid(const sp<SurfaceControl>& surface) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 84 | return (surface != 0) && surface->isValid(); |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 85 | } |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 86 | bool isValid() { |
| 87 | return mToken>=0 && mClient!=0; |
| 88 | } |
| 89 | static bool isSameSurface( |
| 90 | const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs); |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 91 | |
| 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 Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 99 | 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 Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 112 | static status_t writeSurfaceToParcel( |
| 113 | const sp<SurfaceControl>& control, Parcel* parcel); |
| 114 | |
| 115 | sp<Surface> getSurface() const; |
| 116 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 117 | private: |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 118 | // can't be copied |
| 119 | SurfaceControl& operator = (SurfaceControl& rhs); |
| 120 | SurfaceControl(const SurfaceControl& rhs); |
| 121 | |
| 122 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 123 | 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 Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 130 | // for testing |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 131 | friend class Test; |
| 132 | const sp<ISurface>& getISurface() const { return mSurface; } |
| 133 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 134 | |
| 135 | friend class Surface; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 136 | |
| 137 | SurfaceControl( |
| 138 | const sp<SurfaceComposerClient>& client, |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 139 | const sp<ISurface>& surface, |
| 140 | const ISurfaceFlingerClient::surface_data_t& data, |
Mathias Agopian | 69d6209 | 2009-04-16 20:30:22 -0700 | [diff] [blame^] | 141 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 142 | |
| 143 | ~SurfaceControl(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 144 | |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 145 | 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 Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 154 | mutable Mutex mLock; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 155 | |
| 156 | mutable sp<Surface> mSurfaceData; |
Mathias Agopian | 6d2c0bc | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | // --------------------------------------------------------------------------- |
| 160 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 161 | class Surface |
| 162 | : public EGLNativeBase<android_native_window_t, Surface, RefBase> |
| 163 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | public: |
| 165 | struct SurfaceInfo { |
| 166 | uint32_t w; |
| 167 | uint32_t h; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 168 | uint32_t s; |
| 169 | uint32_t usage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | PixelFormat format; |
| 171 | void* bits; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | uint32_t reserved[2]; |
| 173 | }; |
| 174 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 175 | Surface(const Parcel& data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 177 | 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 Agopian | 402c346 | 2009-04-14 18:21:47 -0700 | [diff] [blame] | 189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
| 194 | // setSwapRectangle() is mainly used by EGL |
| 195 | void setSwapRectangle(const Rect& r); |
| 196 | const Rect& swapRectangle() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | private: |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 199 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | friend class SurfaceComposerClient; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 208 | friend class SurfaceControl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
| 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 Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 218 | status_t getBufferLocked(int index); |
| 219 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 220 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | Region dirtyRegion() const; |
| 223 | void setDirtyRegion(const Region& region) const; |
| 224 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 225 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 229 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | sp<SurfaceComposerClient> mClient; |
| 246 | sp<ISurface> mSurface; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 247 | sp<SurfaceBuffer> mBuffers[2]; |
| 248 | android_native_buffer_t* mLockedBuffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | SurfaceID mToken; |
| 250 | uint32_t mIdentity; |
| 251 | PixelFormat mFormat; |
| 252 | uint32_t mFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | mutable Region mDirtyRegion; |
| 254 | mutable Rect mSwapRectangle; |
| 255 | mutable uint8_t mBackbufferIndex; |
| 256 | mutable Mutex mSurfaceLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | }; // namespace android |
| 260 | |
| 261 | #endif // ANDROID_UI_SURFACE_H |
| 262 | |