blob: 57c6ca8503a7a51261c025a755197da1b8f6f0c8 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <ui/PixelFormat.h>
32#include <ui/ISurfaceComposer.h>
33#include <ui/ISurfaceFlingerClient.h>
34
35#include <private/ui/SharedState.h>
36#include <private/ui/LayerState.h>
37#include <private/ui/SurfaceFlingerSynchro.h>
38
39#include "Barrier.h"
40#include "BootAnimation.h"
41#include "CPUGauge.h"
42#include "Layer.h"
43#include "Tokenizer.h"
44
45struct copybit_device_t;
46struct overlay_device_t;
47
48namespace android {
49
50// ---------------------------------------------------------------------------
51
52class Client;
53class BClient;
54class DisplayHardware;
55class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056class Layer;
57class LayerBuffer;
58class LayerOrientationAnim;
59class OrientationAnimation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
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);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
78 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 sp<LayerBaseClient> getLayerUser(int32_t i) const;
80 const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
82 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083
84 // pointer to this client's control block
85 per_client_cblk_t* ctrlblk;
86 ClientID cid;
87
88
89private:
90 int getClientPid() const { return mPid; }
91
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092 int mPid;
93 uint32_t mBitmap;
94 SortedVector<uint8_t> mInUse;
95 Vector< wp<LayerBaseClient> > mLayers;
96 sp<MemoryDealer> mCblkHeap;
97 sp<SurfaceFlinger> mFlinger;
98 sp<IMemory> mCblkMemory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099};
100
101// ---------------------------------------------------------------------------
102
103class GraphicPlane
104{
105public:
106 static status_t orientationToTransfrom(int orientation, int w, int h,
107 Transform* tr);
108
109 GraphicPlane();
110 ~GraphicPlane();
111
112 bool initialized() const;
113
114 void setDisplayHardware(DisplayHardware *);
115 void setTransform(const Transform& tr);
116 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700117 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 const DisplayHardware& displayHardware() const;
120 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 EGLDisplay getEGLDisplay() const;
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123private:
124 GraphicPlane(const GraphicPlane&);
125 GraphicPlane operator = (const GraphicPlane&);
126
127 DisplayHardware* mHw;
128 Transform mTransform;
129 Transform mOrientationTransform;
130 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700131 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132};
133
134// ---------------------------------------------------------------------------
135
136enum {
137 eTransactionNeeded = 0x01,
138 eTraversalNeeded = 0x02
139};
140
141class SurfaceFlinger : public BnSurfaceComposer, protected Thread
142{
143public:
144 static void instantiate();
145 static void shutdown();
146
147 SurfaceFlinger();
148 virtual ~SurfaceFlinger();
149 void init();
150
151 virtual status_t onTransact(
152 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
153
154 virtual status_t dump(int fd, const Vector<String16>& args);
155
156 // ISurfaceComposer interface
157 virtual sp<ISurfaceFlingerClient> createConnection();
158 virtual sp<IMemory> getCblk() const;
159 virtual void bootFinished();
160 virtual void openGlobalTransaction();
161 virtual void closeGlobalTransaction();
162 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
163 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700164 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
167 void screenReleased(DisplayID dpy);
168 void screenAcquired(DisplayID dpy);
169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 overlay_control_device_t* getOverlayEngine() const;
171
172
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173 status_t removeLayer(const sp<LayerBase>& layer);
174 status_t addLayer(const sp<LayerBase>& layer);
175 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
177private:
178 friend class BClient;
179 friend class LayerBase;
180 friend class LayerBuffer;
181 friend class LayerBaseClient;
182 friend class Layer;
183 friend class LayerBlur;
184
185 sp<ISurface> createSurface(ClientID client, int pid,
186 ISurfaceFlingerClient::surface_data_t* params,
187 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
188 uint32_t flags);
189
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700190 sp<LayerBaseClient> createNormalSurfaceLocked(
191 Client* client, DisplayID display,
192 int32_t id, uint32_t w, uint32_t h,
193 PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700195 sp<LayerBaseClient> createBlurSurfaceLocked(
196 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
198
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199 sp<LayerBaseClient> createDimSurfaceLocked(
200 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
202
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
204 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
206
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207 status_t destroySurface(SurfaceID surface_id);
208 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209
210
211 class LayerVector {
212 public:
213 inline LayerVector() { }
214 LayerVector(const LayerVector&);
215 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216 inline sp<LayerBase> const* array() const { return layers.array(); }
217 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
218 ssize_t remove(const sp<LayerBase>&);
219 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
220 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
221 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223 KeyedVector< sp<LayerBase> , size_t> lookup;
224 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 };
226
227 struct State {
228 State() {
229 orientation = ISurfaceComposer::eOrientationDefault;
230 freezeDisplay = 0;
231 }
232 LayerVector layersSortedByZ;
233 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700234 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 uint8_t freezeDisplay;
236 };
237
238 class DelayedTransaction : public Thread
239 {
240 friend class SurfaceFlinger;
241 sp<SurfaceFlinger> mFlinger;
242 nsecs_t mDelay;
243 public:
244 DelayedTransaction(const sp<SurfaceFlinger>& flinger, nsecs_t delay)
245 : Thread(false), mFlinger(flinger), mDelay(delay) {
246 }
247 virtual bool threadLoop() {
248 usleep(mDelay / 1000);
249 if (android_atomic_and(~1,
250 &mFlinger->mDeplayedTransactionPending) == 1) {
251 mFlinger->signalEvent();
252 }
253 return false;
254 }
255 };
256
257 virtual bool threadLoop();
258 virtual status_t readyToRun();
259 virtual void onFirstRef();
260
261 const GraphicPlane& graphicPlane(int dpy) const;
262 GraphicPlane& graphicPlane(int dpy);
263
264 void waitForEvent();
265 void signalEvent();
266 void signalDelayedEvent(nsecs_t delay);
267
268 void handleConsoleEvents();
269 void handleTransaction(uint32_t transactionFlags);
270
271 void computeVisibleRegions(
272 LayerVector& currentLayers,
273 Region& dirtyRegion,
274 Region& wormholeRegion);
275
276 void handlePageFlip();
277 bool lockPageFlip(const LayerVector& currentLayers);
278 void unlockPageFlip(const LayerVector& currentLayers);
279 void handleRepaint();
280 void handleDebugCpu();
281 void scheduleBroadcast(Client* client);
282 void executeScheduledBroadcasts();
283 void postFramebuffer();
284 void composeSurfaces(const Region& dirty);
285 void unlockClients();
286
287
288 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
290 status_t addLayer_l(const sp<LayerBase>& layer);
291 status_t removeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 void free_resources_l();
293
294 uint32_t getTransactionFlags(uint32_t flags);
295 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
296 void commitTransaction();
297
298
299 friend class FreezeLock;
300 sp<FreezeLock> getFreezeLock() const;
301 inline void incFreezeCount() { mFreezeCount++; }
302 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
303 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
304 inline bool isFrozen() const {
305 return mFreezeDisplay || mFreezeCount>0;
306 }
307
308
309 void debugFlashRegions();
310 void debugShowFPS() const;
311 void drawWormhole() const;
312
313 // access must be protected by mStateLock
314 mutable Mutex mStateLock;
315 State mCurrentState;
316 State mDrawingState;
317 volatile int32_t mTransactionFlags;
318 volatile int32_t mTransactionCount;
319 Condition mTransactionCV;
320
321 // protected by mStateLock (but we could use another lock)
322 Tokenizer mTokens;
323 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 Vector<Client*> mDisconnectedClients;
328
329 // constant members (no synchronization needed for access)
330 sp<MemoryDealer> mServerHeap;
331 sp<IMemory> mServerCblkMemory;
332 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 GLuint mWormholeTexName;
334 sp<BootAnimation> mBootAnimation;
335 nsecs_t mBootTime;
336
337 // Can only accessed from the main thread, these members
338 // don't need synchronization
339 Region mDirtyRegion;
340 Region mInvalidRegion;
341 Region mWormholeRegion;
342 Client* mLastScheduledBroadcast;
343 SortedVector<Client*> mScheduledBroadcasts;
344 bool mVisibleRegionsDirty;
345 bool mDeferReleaseConsole;
346 bool mFreezeDisplay;
347 int32_t mFreezeCount;
348 nsecs_t mFreezeDisplayTime;
349 friend class OrientationAnimation;
350 OrientationAnimation* mOrientationAnimation;
351
352 // access protected by mDebugLock
353 mutable Mutex mDebugLock;
354 sp<CPUGauge> mCpuGauge;
355
356 // don't use a lock for these, we don't care
357 int mDebugRegion;
358 int mDebugCpu;
359 int mDebugFps;
360 int mDebugBackground;
361 int mDebugNoBootAnimation;
362
363 // these are thread safe
364 mutable Barrier mReadyToRunBarrier;
365 mutable SurfaceFlingerSynchro mSyncObject;
366 volatile int32_t mDeplayedTransactionPending;
367
368 // atomic variables
369 enum {
370 eConsoleReleased = 1,
371 eConsoleAcquired = 2
372 };
373 volatile int32_t mConsoleSignals;
374
375 // only written in the main thread, only read in other threads
376 volatile int32_t mSecureFrameBuffer;
377};
378
379// ---------------------------------------------------------------------------
380
381class FreezeLock : public LightRefBase<FreezeLock> {
382 SurfaceFlinger* mFlinger;
383public:
384 FreezeLock(SurfaceFlinger* flinger)
385 : mFlinger(flinger) {
386 mFlinger->incFreezeCount();
387 }
388 ~FreezeLock() {
389 mFlinger->decFreezeCount();
390 }
391};
392
393// ---------------------------------------------------------------------------
394
395class BClient : public BnSurfaceFlingerClient
396{
397public:
398 BClient(SurfaceFlinger *flinger, ClientID cid,
399 const sp<IMemory>& cblk);
400 ~BClient();
401
402 // ISurfaceFlingerClient interface
403 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
404
405 virtual sp<ISurface> createSurface(
406 surface_data_t* params, int pid,
407 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
408 uint32_t flags);
409
410 virtual status_t destroySurface(SurfaceID surfaceId);
411 virtual status_t setState(int32_t count, const layer_state_t* states);
412
413private:
414 ClientID mId;
415 SurfaceFlinger* mFlinger;
416 sp<IMemory> mCblk;
417};
418
419// ---------------------------------------------------------------------------
420}; // namespace android
421
422#endif // ANDROID_SURFACE_FLINGER_H