blob: cb84542f2087927fd373243369e649a0df7b49db [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055class Layer;
56class LayerBuffer;
57class LayerOrientationAnim;
58class OrientationAnimation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60typedef int32_t ClientID;
61
62#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
63#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
64
65// ---------------------------------------------------------------------------
66
67class Client
68{
69public:
70 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
71 ~Client();
72
73 int32_t generateId(int pid);
74 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
77 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 sp<LayerBaseClient> getLayerUser(int32_t i) const;
79 const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
81 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
83 // pointer to this client's control block
84 per_client_cblk_t* ctrlblk;
85 ClientID cid;
86
87
88private:
89 int getClientPid() const { return mPid; }
90
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 int mPid;
92 uint32_t mBitmap;
93 SortedVector<uint8_t> mInUse;
94 Vector< wp<LayerBaseClient> > mLayers;
95 sp<MemoryDealer> mCblkHeap;
96 sp<SurfaceFlinger> mFlinger;
97 sp<IMemory> mCblkMemory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098};
99
100// ---------------------------------------------------------------------------
101
102class GraphicPlane
103{
104public:
105 static status_t orientationToTransfrom(int orientation, int w, int h,
106 Transform* tr);
107
108 GraphicPlane();
109 ~GraphicPlane();
110
111 bool initialized() const;
112
113 void setDisplayHardware(DisplayHardware *);
114 void setTransform(const Transform& tr);
115 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700116 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
118 const DisplayHardware& displayHardware() const;
119 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 EGLDisplay getEGLDisplay() const;
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122private:
123 GraphicPlane(const GraphicPlane&);
124 GraphicPlane operator = (const GraphicPlane&);
125
126 DisplayHardware* mHw;
127 Transform mTransform;
128 Transform mOrientationTransform;
129 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700130 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131};
132
133// ---------------------------------------------------------------------------
134
135enum {
136 eTransactionNeeded = 0x01,
137 eTraversalNeeded = 0x02
138};
139
140class SurfaceFlinger : public BnSurfaceComposer, protected Thread
141{
142public:
143 static void instantiate();
144 static void shutdown();
145
146 SurfaceFlinger();
147 virtual ~SurfaceFlinger();
148 void init();
149
150 virtual status_t onTransact(
151 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
152
153 virtual status_t dump(int fd, const Vector<String16>& args);
154
155 // ISurfaceComposer interface
156 virtual sp<ISurfaceFlingerClient> createConnection();
157 virtual sp<IMemory> getCblk() const;
158 virtual void bootFinished();
159 virtual void openGlobalTransaction();
160 virtual void closeGlobalTransaction();
161 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
162 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700163 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
166 void screenReleased(DisplayID dpy);
167 void screenAcquired(DisplayID dpy);
168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 overlay_control_device_t* getOverlayEngine() const;
170
171
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700172 status_t removeLayer(const sp<LayerBase>& layer);
173 status_t addLayer(const sp<LayerBase>& layer);
174 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175
176private:
177 friend class BClient;
178 friend class LayerBase;
179 friend class LayerBuffer;
180 friend class LayerBaseClient;
181 friend class Layer;
182 friend class LayerBlur;
183
184 sp<ISurface> createSurface(ClientID client, int pid,
185 ISurfaceFlingerClient::surface_data_t* params,
186 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
187 uint32_t flags);
188
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700189 sp<LayerBaseClient> createNormalSurfaceLocked(
190 Client* client, DisplayID display,
191 int32_t id, uint32_t w, uint32_t h,
192 PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 sp<LayerBaseClient> createBlurSurfaceLocked(
195 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
197
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700198 sp<LayerBaseClient> createDimSurfaceLocked(
199 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
201
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
203 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
205
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 status_t destroySurface(SurfaceID surface_id);
207 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
209
210 class LayerVector {
211 public:
212 inline LayerVector() { }
213 LayerVector(const LayerVector&);
214 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 inline sp<LayerBase> const* array() const { return layers.array(); }
216 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
217 ssize_t remove(const sp<LayerBase>&);
218 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
219 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
220 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222 KeyedVector< sp<LayerBase> , size_t> lookup;
223 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 };
225
226 struct State {
227 State() {
228 orientation = ISurfaceComposer::eOrientationDefault;
229 freezeDisplay = 0;
230 }
231 LayerVector layersSortedByZ;
232 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700233 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 uint8_t freezeDisplay;
235 };
236
237 class DelayedTransaction : public Thread
238 {
239 friend class SurfaceFlinger;
240 sp<SurfaceFlinger> mFlinger;
241 nsecs_t mDelay;
242 public:
243 DelayedTransaction(const sp<SurfaceFlinger>& flinger, nsecs_t delay)
244 : Thread(false), mFlinger(flinger), mDelay(delay) {
245 }
246 virtual bool threadLoop() {
247 usleep(mDelay / 1000);
248 if (android_atomic_and(~1,
249 &mFlinger->mDeplayedTransactionPending) == 1) {
250 mFlinger->signalEvent();
251 }
252 return false;
253 }
254 };
255
256 virtual bool threadLoop();
257 virtual status_t readyToRun();
258 virtual void onFirstRef();
259
260 const GraphicPlane& graphicPlane(int dpy) const;
261 GraphicPlane& graphicPlane(int dpy);
262
263 void waitForEvent();
264 void signalEvent();
265 void signalDelayedEvent(nsecs_t delay);
266
267 void handleConsoleEvents();
268 void handleTransaction(uint32_t transactionFlags);
269
270 void computeVisibleRegions(
271 LayerVector& currentLayers,
272 Region& dirtyRegion,
273 Region& wormholeRegion);
274
275 void handlePageFlip();
276 bool lockPageFlip(const LayerVector& currentLayers);
277 void unlockPageFlip(const LayerVector& currentLayers);
278 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 void scheduleBroadcast(Client* client);
280 void executeScheduledBroadcasts();
281 void postFramebuffer();
282 void composeSurfaces(const Region& dirty);
283 void unlockClients();
284
285
286 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700287 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
288 status_t addLayer_l(const sp<LayerBase>& layer);
289 status_t removeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 void free_resources_l();
291
292 uint32_t getTransactionFlags(uint32_t flags);
293 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
294 void commitTransaction();
295
296
297 friend class FreezeLock;
298 sp<FreezeLock> getFreezeLock() const;
299 inline void incFreezeCount() { mFreezeCount++; }
300 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
301 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
302 inline bool isFrozen() const {
303 return mFreezeDisplay || mFreezeCount>0;
304 }
305
306
307 void debugFlashRegions();
308 void debugShowFPS() const;
309 void drawWormhole() const;
310
311 // access must be protected by mStateLock
312 mutable Mutex mStateLock;
313 State mCurrentState;
314 State mDrawingState;
315 volatile int32_t mTransactionFlags;
316 volatile int32_t mTransactionCount;
317 Condition mTransactionCV;
318
319 // protected by mStateLock (but we could use another lock)
320 Tokenizer mTokens;
321 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 Vector<Client*> mDisconnectedClients;
326
327 // constant members (no synchronization needed for access)
328 sp<MemoryDealer> mServerHeap;
329 sp<IMemory> mServerCblkMemory;
330 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 GLuint mWormholeTexName;
332 sp<BootAnimation> mBootAnimation;
333 nsecs_t mBootTime;
334
335 // Can only accessed from the main thread, these members
336 // don't need synchronization
337 Region mDirtyRegion;
338 Region mInvalidRegion;
339 Region mWormholeRegion;
340 Client* mLastScheduledBroadcast;
341 SortedVector<Client*> mScheduledBroadcasts;
342 bool mVisibleRegionsDirty;
343 bool mDeferReleaseConsole;
344 bool mFreezeDisplay;
345 int32_t mFreezeCount;
346 nsecs_t mFreezeDisplayTime;
347 friend class OrientationAnimation;
348 OrientationAnimation* mOrientationAnimation;
349
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 // don't use a lock for these, we don't care
351 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 int mDebugFps;
353 int mDebugBackground;
354 int mDebugNoBootAnimation;
355
356 // these are thread safe
357 mutable Barrier mReadyToRunBarrier;
358 mutable SurfaceFlingerSynchro mSyncObject;
359 volatile int32_t mDeplayedTransactionPending;
360
361 // atomic variables
362 enum {
363 eConsoleReleased = 1,
364 eConsoleAcquired = 2
365 };
366 volatile int32_t mConsoleSignals;
367
368 // only written in the main thread, only read in other threads
369 volatile int32_t mSecureFrameBuffer;
370};
371
372// ---------------------------------------------------------------------------
373
374class FreezeLock : public LightRefBase<FreezeLock> {
375 SurfaceFlinger* mFlinger;
376public:
377 FreezeLock(SurfaceFlinger* flinger)
378 : mFlinger(flinger) {
379 mFlinger->incFreezeCount();
380 }
381 ~FreezeLock() {
382 mFlinger->decFreezeCount();
383 }
384};
385
386// ---------------------------------------------------------------------------
387
388class BClient : public BnSurfaceFlingerClient
389{
390public:
391 BClient(SurfaceFlinger *flinger, ClientID cid,
392 const sp<IMemory>& cblk);
393 ~BClient();
394
395 // ISurfaceFlingerClient interface
396 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
397
398 virtual sp<ISurface> createSurface(
399 surface_data_t* params, int pid,
400 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
401 uint32_t flags);
402
403 virtual status_t destroySurface(SurfaceID surfaceId);
404 virtual status_t setState(int32_t count, const layer_state_t* states);
405
406private:
407 ClientID mId;
408 SurfaceFlinger* mFlinger;
409 sp<IMemory> mCblk;
410};
411
412// ---------------------------------------------------------------------------
413}; // namespace android
414
415#endif // ANDROID_SURFACE_FLINGER_H