blob: 15913f2318f7245855e8ad995e72f5b1cd32582f [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"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include "CPUGauge.h"
40#include "Layer.h"
41#include "Tokenizer.h"
42
43struct copybit_device_t;
44struct overlay_device_t;
45
46namespace android {
47
48// ---------------------------------------------------------------------------
49
50class Client;
51class BClient;
52class DisplayHardware;
53class FreezeLock;
54class GPUHardwareInterface;
55class IGPUCallback;
56class Layer;
57class LayerBuffer;
58class LayerOrientationAnim;
59class OrientationAnimation;
60class SurfaceHeapManager;
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);
78 sp<MemoryDealer> createAllocator(uint32_t memory_type);
79
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; }
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 static status_t orientationToTransfrom(int orientation, int w, int h,
114 Transform* tr);
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);
Mathias Agopian89a18722009-03-27 15:36:09 -0700124 int getOrientation() const { return mOrientation; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
126 const DisplayHardware& displayHardware() const;
127 const Transform& transform() const;
128private:
129 GraphicPlane(const GraphicPlane&);
130 GraphicPlane operator = (const GraphicPlane&);
131
132 DisplayHardware* mHw;
133 Transform mTransform;
134 Transform mOrientationTransform;
135 Transform mGlobalTransform;
Mathias Agopian89a18722009-03-27 15:36:09 -0700136 int mOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137};
138
139// ---------------------------------------------------------------------------
140
141enum {
142 eTransactionNeeded = 0x01,
143 eTraversalNeeded = 0x02
144};
145
146class SurfaceFlinger : public BnSurfaceComposer, protected Thread
147{
148public:
149 static void instantiate();
150 static void shutdown();
151
152 SurfaceFlinger();
153 virtual ~SurfaceFlinger();
154 void init();
155
156 virtual status_t onTransact(
157 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
158
159 virtual status_t dump(int fd, const Vector<String16>& args);
160
161 // ISurfaceComposer interface
162 virtual sp<ISurfaceFlingerClient> createConnection();
163 virtual sp<IMemory> getCblk() const;
164 virtual void bootFinished();
165 virtual void openGlobalTransaction();
166 virtual void closeGlobalTransaction();
167 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
168 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopian66b8ec92009-03-27 16:10:37 -0700169 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 virtual void signal() const;
171 virtual status_t requestGPU(const sp<IGPUCallback>& callback,
172 gpu_info_t* gpu);
173 virtual status_t revokeGPU();
174
175 void screenReleased(DisplayID dpy);
176 void screenAcquired(DisplayID dpy);
177
178 const sp<SurfaceHeapManager>& getSurfaceHeapManager() const {
179 return mSurfaceHeapManager;
180 }
181
182 const sp<GPUHardwareInterface>& getGPU() const {
183 return mGPU;
184 }
185
186 copybit_device_t* getBlitEngine() const;
187 overlay_control_device_t* getOverlayEngine() const;
188
189
190 status_t removeLayer(LayerBase* layer);
191 status_t addLayer(LayerBase* layer);
192 status_t invalidateLayerVisibility(LayerBase* layer);
193
194private:
195 friend class BClient;
196 friend class LayerBase;
197 friend class LayerBuffer;
198 friend class LayerBaseClient;
199 friend class Layer;
200 friend class LayerBlur;
201
202 sp<ISurface> createSurface(ClientID client, int pid,
203 ISurfaceFlingerClient::surface_data_t* params,
204 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
205 uint32_t flags);
206
207 LayerBaseClient* createNormalSurfaceLocked(Client* client, DisplayID display,
208 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
209
210 LayerBaseClient* createBlurSurfaceLocked(Client* client, DisplayID display,
211 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
212
213 LayerBaseClient* createDimSurfaceLocked(Client* client, DisplayID display,
214 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
215
216 LayerBaseClient* createPushBuffersSurfaceLocked(Client* client, DisplayID display,
217 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
218
219 status_t destroySurface(SurfaceID surface_id);
220 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
221
222
223 class LayerVector {
224 public:
225 inline LayerVector() { }
226 LayerVector(const LayerVector&);
227 inline size_t size() const { return layers.size(); }
228 inline LayerBase*const* array() const { return layers.array(); }
229 ssize_t add(LayerBase*, Vector<LayerBase*>::compar_t);
230 ssize_t remove(LayerBase*);
231 ssize_t reorder(LayerBase*, Vector<LayerBase*>::compar_t);
232 ssize_t indexOf(LayerBase* key, size_t guess=0) const;
233 inline LayerBase* operator [] (size_t i) const { return layers[i]; }
234 private:
235 KeyedVector<LayerBase*, size_t> lookup;
236 Vector<LayerBase*> layers;
237 };
238
239 struct State {
240 State() {
241 orientation = ISurfaceComposer::eOrientationDefault;
242 freezeDisplay = 0;
243 }
244 LayerVector layersSortedByZ;
245 uint8_t orientation;
Mathias Agopian66b8ec92009-03-27 16:10:37 -0700246 uint8_t orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 nsecs_t mBootTime;
350
351 // Can only accessed from the main thread, these members
352 // don't need synchronization
353 Region mDirtyRegion;
354 Region mInvalidRegion;
355 Region mWormholeRegion;
356 Client* mLastScheduledBroadcast;
357 SortedVector<Client*> mScheduledBroadcasts;
358 bool mVisibleRegionsDirty;
359 bool mDeferReleaseConsole;
360 bool mFreezeDisplay;
361 int32_t mFreezeCount;
362 nsecs_t mFreezeDisplayTime;
363 friend class OrientationAnimation;
364 OrientationAnimation* mOrientationAnimation;
365
366 // access protected by mDebugLock
367 mutable Mutex mDebugLock;
368 sp<CPUGauge> mCpuGauge;
369
370 // don't use a lock for these, we don't care
371 int mDebugRegion;
372 int mDebugCpu;
373 int mDebugFps;
374 int mDebugBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
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
394class FreezeLock : public LightRefBase<FreezeLock> {
395 SurfaceFlinger* mFlinger;
396public:
397 FreezeLock(SurfaceFlinger* flinger)
398 : mFlinger(flinger) {
399 mFlinger->incFreezeCount();
400 }
401 ~FreezeLock() {
402 mFlinger->decFreezeCount();
403 }
404};
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