blob: a242f1aa01862198409585156b4bb223fdf1067b [file] [log] [blame]
The Android Open Source Project7c1b96a2008-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
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080044struct copybit_device_t;
45struct overlay_device_t;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046
47namespace android {
48
49// ---------------------------------------------------------------------------
50
51class BClient;
52class Client;
53class DisplayHardware;
54class GPUHardwareInterface;
55class IGPUCallback;
56class Layer;
57class LayerBuffer;
58class RFBServer;
59class SurfaceHeapManager;
60class FreezeLock;
61
62typedef int32_t ClientID;
63
64#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
65#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
66
67// ---------------------------------------------------------------------------
68
69class Client
70{
71public:
72 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
73 ~Client();
74
75 int32_t generateId(int pid);
76 void free(int32_t id);
77 status_t bindLayer(LayerBaseClient* layer, int32_t id);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080078 sp<MemoryDealer> createAllocator(uint32_t memory_type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079
80 inline bool isValid(int32_t i) const;
81 inline const uint8_t* inUseArray() const;
82 inline size_t numActiveLayers() const;
83 LayerBaseClient* getLayerUser(int32_t i) const;
84 const Vector<LayerBaseClient*>& getLayers() const { return mLayers; }
85 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
86 void dump(const char* what);
87 const sp<SurfaceHeapManager>& getSurfaceHeapManager() const;
88
89 // pointer to this client's control block
90 per_client_cblk_t* ctrlblk;
91 ClientID cid;
92
93
94private:
95 int getClientPid() const { return mPid; }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096
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
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800182 copybit_device_t* getBlitEngine() const;
The Android Open Source Project27629322009-01-09 17:51:23 -0800183 overlay_control_device_t* getOverlayEngine() const;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184
185private:
186 friend class BClient;
187 friend class LayerBase;
188 friend class LayerBuffer;
189 friend class LayerBaseClient;
190 friend class Layer;
191 friend class LayerBlur;
192
193 sp<ISurface> createSurface(ClientID client, int pid,
194 ISurfaceFlingerClient::surface_data_t* params,
195 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
196 uint32_t flags);
197
198 LayerBaseClient* createNormalSurfaceLocked(Client* client, DisplayID display,
199 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
200
201 LayerBaseClient* createBlurSurfaceLocked(Client* client, DisplayID display,
202 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
203
204 LayerBaseClient* createDimSurfaceLocked(Client* client, DisplayID display,
205 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
206
207 LayerBaseClient* createPushBuffersSurfaceLocked(Client* client, DisplayID display,
208 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
209
210 status_t destroySurface(SurfaceID surface_id);
211 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
212
213
214 class LayerVector {
215 public:
216 inline LayerVector() { }
217 LayerVector(const LayerVector&);
218 inline size_t size() const { return layers.size(); }
219 inline LayerBase*const* array() const { return layers.array(); }
220 ssize_t add(LayerBase*, Vector<LayerBase*>::compar_t);
221 ssize_t remove(LayerBase*);
222 ssize_t reorder(LayerBase*, Vector<LayerBase*>::compar_t);
223 ssize_t indexOf(LayerBase* key, size_t guess=0) const;
224 inline LayerBase* operator [] (size_t i) const { return layers[i]; }
225 private:
226 KeyedVector<LayerBase*, size_t> lookup;
227 Vector<LayerBase*> layers;
228 };
229
230 struct State {
231 State() {
232 orientation = ISurfaceComposer::eOrientationDefault;
233 freezeDisplay = 0;
234 }
235 LayerVector layersSortedByZ;
236 uint8_t orientation;
237 uint8_t freezeDisplay;
238 };
239
240 class DelayedTransaction : public Thread
241 {
242 friend class SurfaceFlinger;
243 sp<SurfaceFlinger> mFlinger;
244 nsecs_t mDelay;
245 public:
246 DelayedTransaction(const sp<SurfaceFlinger>& flinger, nsecs_t delay)
247 : Thread(false), mFlinger(flinger), mDelay(delay) {
248 }
249 virtual bool threadLoop() {
250 usleep(mDelay / 1000);
251 if (android_atomic_and(~1,
252 &mFlinger->mDeplayedTransactionPending) == 1) {
253 mFlinger->signalEvent();
254 }
255 return false;
256 }
257 };
258
259 virtual bool threadLoop();
260 virtual status_t readyToRun();
261 virtual void onFirstRef();
262
263 const GraphicPlane& graphicPlane(int dpy) const;
264 GraphicPlane& graphicPlane(int dpy);
265
266 void waitForEvent();
267 void signalEvent();
268 void signalDelayedEvent(nsecs_t delay);
269
270 void handleConsoleEvents();
271 void handleTransaction(uint32_t transactionFlags);
272
273 void computeVisibleRegions(
274 LayerVector& currentLayers,
275 Region& dirtyRegion,
276 Region& wormholeRegion);
277
278 void handlePageFlip();
279 bool lockPageFlip(const LayerVector& currentLayers);
280 void unlockPageFlip(const LayerVector& currentLayers);
281 void handleRepaint();
282 void handleDebugCpu();
283 void scheduleBroadcast(Client* client);
284 void executeScheduledBroadcasts();
285 void postFramebuffer();
286 void composeSurfaces(const Region& dirty);
287 void unlockClients();
288
289
290 void destroyConnection(ClientID cid);
291 LayerBaseClient* getLayerUser_l(SurfaceID index) const;
292 status_t addLayer_l(LayerBase* layer);
293 status_t removeLayer_l(LayerBase* layer);
294 void destroy_all_removed_layers_l();
295 void free_resources_l();
296
297 uint32_t getTransactionFlags(uint32_t flags);
298 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
299 void commitTransaction();
300
301
302 friend class FreezeLock;
303 sp<FreezeLock> getFreezeLock() const;
304 inline void incFreezeCount() { mFreezeCount++; }
305 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
306 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
307 inline bool isFrozen() const {
308 return mFreezeDisplay || mFreezeCount>0;
309 }
310
311
312 void debugFlashRegions();
313 void debugShowFPS() const;
314 void drawWormhole() const;
315
316 // access must be protected by mStateLock
317 mutable Mutex mStateLock;
318 State mCurrentState;
319 State mDrawingState;
320 volatile int32_t mTransactionFlags;
321 volatile int32_t mTransactionCount;
322 Condition mTransactionCV;
323
324 // protected by mStateLock (but we could use another lock)
325 Tokenizer mTokens;
326 DefaultKeyedVector<ClientID, Client*> mClientsMap;
327 DefaultKeyedVector<SurfaceID, LayerBaseClient*> mLayerMap;
328 GraphicPlane mGraphicPlanes[1];
329 SortedVector<LayerBase*> mRemovedLayers;
330 Vector<Client*> mDisconnectedClients;
331
332 // constant members (no synchronization needed for access)
333 sp<MemoryDealer> mServerHeap;
334 sp<IMemory> mServerCblkMemory;
335 surface_flinger_cblk_t* mServerCblk;
336 sp<SurfaceHeapManager> mSurfaceHeapManager;
337 sp<GPUHardwareInterface> mGPU;
338 GLuint mWormholeTexName;
339 sp<BootAnimation> mBootAnimation;
340 sp<RFBServer> mRFBServer;
341 nsecs_t mBootTime;
342
343 // Can only accessed from the main thread, these members
344 // don't need synchronization
345 Region mDirtyRegion;
346 Region mInvalidRegion;
347 Region mWormholeRegion;
348 Client* mLastScheduledBroadcast;
349 SortedVector<Client*> mScheduledBroadcasts;
350 bool mVisibleRegionsDirty;
351 bool mDeferReleaseConsole;
352 bool mFreezeDisplay;
353 int32_t mFreezeCount;
354 nsecs_t mFreezeDisplayTime;
355
356 // access protected by mDebugLock
357 mutable Mutex mDebugLock;
358 sp<CPUGauge> mCpuGauge;
359
360 // don't use a lock for these, we don't care
361 int mDebugRegion;
362 int mDebugCpu;
363 int mDebugFps;
364 int mDebugBackground;
365 int mDebugNoBootAnimation;
366
367 // these are thread safe
368 mutable Barrier mReadyToRunBarrier;
369 mutable SurfaceFlingerSynchro mSyncObject;
370 volatile int32_t mDeplayedTransactionPending;
371
372 // atomic variables
373 enum {
374 eConsoleReleased = 1,
375 eConsoleAcquired = 2
376 };
377 volatile int32_t mConsoleSignals;
378
379 // only written in the main thread, only read in other threads
380 volatile int32_t mSecureFrameBuffer;
381};
382
383// ---------------------------------------------------------------------------
384
The Android Open Source Project27629322009-01-09 17:51:23 -0800385class FreezeLock : public LightRefBase<FreezeLock> {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700386 SurfaceFlinger* mFlinger;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387public:
388 FreezeLock(SurfaceFlinger* flinger)
The Android Open Source Project27629322009-01-09 17:51:23 -0800389 : mFlinger(flinger) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700390 mFlinger->incFreezeCount();
391 }
392 ~FreezeLock() {
393 mFlinger->decFreezeCount();
394 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395};
396
397// ---------------------------------------------------------------------------
398
399class BClient : public BnSurfaceFlingerClient
400{
401public:
402 BClient(SurfaceFlinger *flinger, ClientID cid,
403 const sp<IMemory>& cblk);
404 ~BClient();
405
406 // ISurfaceFlingerClient interface
407 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
408
409 virtual sp<ISurface> createSurface(
410 surface_data_t* params, int pid,
411 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
412 uint32_t flags);
413
414 virtual status_t destroySurface(SurfaceID surfaceId);
415 virtual status_t setState(int32_t count, const layer_state_t* states);
416
417private:
418 ClientID mId;
419 SurfaceFlinger* mFlinger;
420 sp<IMemory> mCblk;
421};
422
423// ---------------------------------------------------------------------------
424}; // namespace android
425
426#endif // ANDROID_SURFACE_FLINGER_H