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