blob: 8e5fd885d5a5ac1144f85ff5539e030a44395a65 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
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_SURFACE_FLINGER_H
18#define ANDROID_SURFACE_FLINGER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/SortedVector.h>
24#include <utils/KeyedVector.h>
25#include <utils/threads.h>
26#include <utils/Atomic.h>
27#include <utils/Errors.h>
28#include <utils/MemoryDealer.h>
29
30#include <ui/PixelFormat.h>
31#include <ui/ISurfaceComposer.h>
32#include <ui/ISurfaceFlingerClient.h>
33
34#include <private/ui/SharedState.h>
35#include <private/ui/LayerState.h>
36#include <private/ui/SurfaceFlingerSynchro.h>
37
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080038#include "Barrier.h"
39#include "BootAnimation.h"
40#include "CPUGauge.h"
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041#include "Layer.h"
42#include "Tokenizer.h"
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070043
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080044struct copybit_device_t;
45struct overlay_device_t;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070046
47namespace android {
48
49// ---------------------------------------------------------------------------
50
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051class Client;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080052class BClient;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070053class DisplayHardware;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080054class FreezeLock;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070055class GPUHardwareInterface;
56class IGPUCallback;
57class Layer;
58class LayerBuffer;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080059class LayerOrientationAnim;
60class OrientationAnimation;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070061class RFBServer;
62class SurfaceHeapManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070063
64typedef int32_t ClientID;
65
66#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
67#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
68
69// ---------------------------------------------------------------------------
70
71class Client
72{
73public:
74 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
75 ~Client();
76
77 int32_t generateId(int pid);
78 void free(int32_t id);
79 status_t bindLayer(LayerBaseClient* layer, int32_t id);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080080 sp<MemoryDealer> createAllocator(uint32_t memory_type);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070081
82 inline bool isValid(int32_t i) const;
83 inline const uint8_t* inUseArray() const;
84 inline size_t numActiveLayers() const;
85 LayerBaseClient* getLayerUser(int32_t i) const;
86 const Vector<LayerBaseClient*>& getLayers() const { return mLayers; }
87 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
88 void dump(const char* what);
89 const sp<SurfaceHeapManager>& getSurfaceHeapManager() const;
90
91 // pointer to this client's control block
92 per_client_cblk_t* ctrlblk;
93 ClientID cid;
94
95
96private:
97 int getClientPid() const { return mPid; }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070098
99 int mPid;
100 uint32_t mBitmap;
101 SortedVector<uint8_t> mInUse;
102 Vector<LayerBaseClient*> mLayers;
103 sp<MemoryDealer> mCblkHeap;
104 sp<SurfaceFlinger> mFlinger;
105 sp<MemoryDealer> mSharedHeapAllocator;
106 sp<MemoryDealer> mPMemAllocator;
107 sp<IMemory> mCblkMemory;
108};
109
110// ---------------------------------------------------------------------------
111
112class GraphicPlane
113{
114public:
115
116 GraphicPlane();
117 ~GraphicPlane();
118
119 bool initialized() const;
120
121 void setDisplayHardware(DisplayHardware *);
122 void setTransform(const Transform& tr);
123 status_t setOrientation(int orientation);
124
125 const DisplayHardware& displayHardware() const;
126 const Transform& transform() const;
127private:
128 GraphicPlane(const GraphicPlane&);
129 GraphicPlane operator = (const GraphicPlane&);
130
131 DisplayHardware* mHw;
132 Transform mTransform;
133 Transform mOrientationTransform;
134 Transform mGlobalTransform;
135};
136
137// ---------------------------------------------------------------------------
138
139enum {
140 eTransactionNeeded = 0x01,
141 eTraversalNeeded = 0x02
142};
143
144class SurfaceFlinger : public BnSurfaceComposer, protected Thread
145{
146public:
147 static void instantiate();
148 static void shutdown();
149
150 SurfaceFlinger();
151 virtual ~SurfaceFlinger();
152 void init();
153
154 virtual status_t onTransact(
155 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
156
157 virtual status_t dump(int fd, const Vector<String16>& args);
158
159 // ISurfaceComposer interface
160 virtual sp<ISurfaceFlingerClient> createConnection();
161 virtual sp<IMemory> getCblk() const;
162 virtual void bootFinished();
163 virtual void openGlobalTransaction();
164 virtual void closeGlobalTransaction();
165 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
166 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
167 virtual int setOrientation(DisplayID dpy, int orientation);
168 virtual void signal() const;
169 virtual status_t requestGPU(const sp<IGPUCallback>& callback,
170 gpu_info_t* gpu);
171 virtual status_t revokeGPU();
172
173 void screenReleased(DisplayID dpy);
174 void screenAcquired(DisplayID dpy);
175
176 const sp<SurfaceHeapManager>& getSurfaceHeapManager() const {
177 return mSurfaceHeapManager;
178 }
179
180 const sp<GPUHardwareInterface>& getGPU() const {
181 return mGPU;
182 }
183
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800184 copybit_device_t* getBlitEngine() const;
The Android Open Source Projectb7986892009-01-09 17:51:23 -0800185 overlay_control_device_t* getOverlayEngine() const;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800186
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700187
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800188 status_t removeLayer(LayerBase* layer);
189 status_t addLayer(LayerBase* layer);
190 status_t invalidateLayerVisibility(LayerBase* layer);
191
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700192private:
193 friend class BClient;
194 friend class LayerBase;
195 friend class LayerBuffer;
196 friend class LayerBaseClient;
197 friend class Layer;
198 friend class LayerBlur;
199
200 sp<ISurface> createSurface(ClientID client, int pid,
201 ISurfaceFlingerClient::surface_data_t* params,
202 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
203 uint32_t flags);
204
205 LayerBaseClient* createNormalSurfaceLocked(Client* client, DisplayID display,
206 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
207
208 LayerBaseClient* createBlurSurfaceLocked(Client* client, DisplayID display,
209 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
210
211 LayerBaseClient* createDimSurfaceLocked(Client* client, DisplayID display,
212 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
213
214 LayerBaseClient* createPushBuffersSurfaceLocked(Client* client, DisplayID display,
215 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
216
217 status_t destroySurface(SurfaceID surface_id);
218 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
219
220
221 class LayerVector {
222 public:
223 inline LayerVector() { }
224 LayerVector(const LayerVector&);
225 inline size_t size() const { return layers.size(); }
226 inline LayerBase*const* array() const { return layers.array(); }
227 ssize_t add(LayerBase*, Vector<LayerBase*>::compar_t);
228 ssize_t remove(LayerBase*);
229 ssize_t reorder(LayerBase*, Vector<LayerBase*>::compar_t);
230 ssize_t indexOf(LayerBase* key, size_t guess=0) const;
231 inline LayerBase* operator [] (size_t i) const { return layers[i]; }
232 private:
233 KeyedVector<LayerBase*, size_t> lookup;
234 Vector<LayerBase*> layers;
235 };
236
237 struct State {
238 State() {
239 orientation = ISurfaceComposer::eOrientationDefault;
240 freezeDisplay = 0;
241 }
242 LayerVector layersSortedByZ;
243 uint8_t orientation;
244 uint8_t freezeDisplay;
245 };
246
247 class DelayedTransaction : public Thread
248 {
249 friend class SurfaceFlinger;
250 sp<SurfaceFlinger> mFlinger;
251 nsecs_t mDelay;
252 public:
253 DelayedTransaction(const sp<SurfaceFlinger>& flinger, nsecs_t delay)
254 : Thread(false), mFlinger(flinger), mDelay(delay) {
255 }
256 virtual bool threadLoop() {
257 usleep(mDelay / 1000);
258 if (android_atomic_and(~1,
259 &mFlinger->mDeplayedTransactionPending) == 1) {
260 mFlinger->signalEvent();
261 }
262 return false;
263 }
264 };
265
266 virtual bool threadLoop();
267 virtual status_t readyToRun();
268 virtual void onFirstRef();
269
270 const GraphicPlane& graphicPlane(int dpy) const;
271 GraphicPlane& graphicPlane(int dpy);
272
273 void waitForEvent();
274 void signalEvent();
275 void signalDelayedEvent(nsecs_t delay);
276
277 void handleConsoleEvents();
278 void handleTransaction(uint32_t transactionFlags);
279
280 void computeVisibleRegions(
281 LayerVector& currentLayers,
282 Region& dirtyRegion,
283 Region& wormholeRegion);
284
285 void handlePageFlip();
286 bool lockPageFlip(const LayerVector& currentLayers);
287 void unlockPageFlip(const LayerVector& currentLayers);
288 void handleRepaint();
289 void handleDebugCpu();
290 void scheduleBroadcast(Client* client);
291 void executeScheduledBroadcasts();
292 void postFramebuffer();
293 void composeSurfaces(const Region& dirty);
294 void unlockClients();
295
296
297 void destroyConnection(ClientID cid);
298 LayerBaseClient* getLayerUser_l(SurfaceID index) const;
299 status_t addLayer_l(LayerBase* layer);
300 status_t removeLayer_l(LayerBase* layer);
301 void destroy_all_removed_layers_l();
302 void free_resources_l();
303
304 uint32_t getTransactionFlags(uint32_t flags);
305 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
306 void commitTransaction();
307
308
309 friend class FreezeLock;
310 sp<FreezeLock> getFreezeLock() const;
311 inline void incFreezeCount() { mFreezeCount++; }
312 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
313 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
314 inline bool isFrozen() const {
315 return mFreezeDisplay || mFreezeCount>0;
316 }
317
318
319 void debugFlashRegions();
320 void debugShowFPS() const;
321 void drawWormhole() const;
322
323 // access must be protected by mStateLock
324 mutable Mutex mStateLock;
325 State mCurrentState;
326 State mDrawingState;
327 volatile int32_t mTransactionFlags;
328 volatile int32_t mTransactionCount;
329 Condition mTransactionCV;
330
331 // protected by mStateLock (but we could use another lock)
332 Tokenizer mTokens;
333 DefaultKeyedVector<ClientID, Client*> mClientsMap;
334 DefaultKeyedVector<SurfaceID, LayerBaseClient*> mLayerMap;
335 GraphicPlane mGraphicPlanes[1];
336 SortedVector<LayerBase*> mRemovedLayers;
337 Vector<Client*> mDisconnectedClients;
338
339 // constant members (no synchronization needed for access)
340 sp<MemoryDealer> mServerHeap;
341 sp<IMemory> mServerCblkMemory;
342 surface_flinger_cblk_t* mServerCblk;
343 sp<SurfaceHeapManager> mSurfaceHeapManager;
344 sp<GPUHardwareInterface> mGPU;
345 GLuint mWormholeTexName;
346 sp<BootAnimation> mBootAnimation;
347 sp<RFBServer> mRFBServer;
348 nsecs_t mBootTime;
349
350 // Can only accessed from the main thread, these members
351 // don't need synchronization
352 Region mDirtyRegion;
353 Region mInvalidRegion;
354 Region mWormholeRegion;
355 Client* mLastScheduledBroadcast;
356 SortedVector<Client*> mScheduledBroadcasts;
357 bool mVisibleRegionsDirty;
358 bool mDeferReleaseConsole;
359 bool mFreezeDisplay;
360 int32_t mFreezeCount;
361 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800362 friend class OrientationAnimation;
363 OrientationAnimation* mOrientationAnimation;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700364
365 // access protected by mDebugLock
366 mutable Mutex mDebugLock;
367 sp<CPUGauge> mCpuGauge;
368
369 // don't use a lock for these, we don't care
370 int mDebugRegion;
371 int mDebugCpu;
372 int mDebugFps;
373 int mDebugBackground;
374 int mDebugNoBootAnimation;
375
376 // these are thread safe
377 mutable Barrier mReadyToRunBarrier;
378 mutable SurfaceFlingerSynchro mSyncObject;
379 volatile int32_t mDeplayedTransactionPending;
380
381 // atomic variables
382 enum {
383 eConsoleReleased = 1,
384 eConsoleAcquired = 2
385 };
386 volatile int32_t mConsoleSignals;
387
388 // only written in the main thread, only read in other threads
389 volatile int32_t mSecureFrameBuffer;
390};
391
392// ---------------------------------------------------------------------------
393
The Android Open Source Projectb7986892009-01-09 17:51:23 -0800394class FreezeLock : public LightRefBase<FreezeLock> {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700395 SurfaceFlinger* mFlinger;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700396public:
397 FreezeLock(SurfaceFlinger* flinger)
The Android Open Source Projectb7986892009-01-09 17:51:23 -0800398 : mFlinger(flinger) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700399 mFlinger->incFreezeCount();
400 }
401 ~FreezeLock() {
402 mFlinger->decFreezeCount();
403 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700404};
405
406// ---------------------------------------------------------------------------
407
408class BClient : public BnSurfaceFlingerClient
409{
410public:
411 BClient(SurfaceFlinger *flinger, ClientID cid,
412 const sp<IMemory>& cblk);
413 ~BClient();
414
415 // ISurfaceFlingerClient interface
416 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
417
418 virtual sp<ISurface> createSurface(
419 surface_data_t* params, int pid,
420 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
421 uint32_t flags);
422
423 virtual status_t destroySurface(SurfaceID surfaceId);
424 virtual status_t setState(int32_t count, const layer_state_t* states);
425
426private:
427 ClientID mId;
428 SurfaceFlinger* mFlinger;
429 sp<IMemory> mCblk;
430};
431
432// ---------------------------------------------------------------------------
433}; // namespace android
434
435#endif // ANDROID_SURFACE_FLINGER_H