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