blob: b488ab5143f556bfae8fd7e50b4f67488290a455 [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 Agopian9a112062009-04-17 19:36:26 -0700206 status_t removeSurface(SurfaceID surface_id);
207 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208 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();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 void scheduleBroadcast(Client* client);
281 void executeScheduledBroadcasts();
282 void postFramebuffer();
283 void composeSurfaces(const Region& dirty);
284 void unlockClients();
285
286
287 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700288 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
289 status_t addLayer_l(const sp<LayerBase>& layer);
290 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700291 status_t purgatorizeLayer_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;
Mathias Agopian9a112062009-04-17 19:36:26 -0700320 SortedVector< sp<LayerBase> > mLayerPurgatory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
Mathias Agopian9a112062009-04-17 19:36:26 -0700322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 // protected by mStateLock (but we could use another lock)
324 Tokenizer mTokens;
325 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 Vector<Client*> mDisconnectedClients;
330
331 // constant members (no synchronization needed for access)
332 sp<MemoryDealer> mServerHeap;
333 sp<IMemory> mServerCblkMemory;
334 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 GLuint mWormholeTexName;
336 sp<BootAnimation> mBootAnimation;
337 nsecs_t mBootTime;
338
339 // Can only accessed from the main thread, these members
340 // don't need synchronization
341 Region mDirtyRegion;
342 Region mInvalidRegion;
343 Region mWormholeRegion;
344 Client* mLastScheduledBroadcast;
345 SortedVector<Client*> mScheduledBroadcasts;
346 bool mVisibleRegionsDirty;
347 bool mDeferReleaseConsole;
348 bool mFreezeDisplay;
349 int32_t mFreezeCount;
350 nsecs_t mFreezeDisplayTime;
351 friend class OrientationAnimation;
352 OrientationAnimation* mOrientationAnimation;
353
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 // don't use a lock for these, we don't care
355 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 int mDebugFps;
357 int mDebugBackground;
358 int mDebugNoBootAnimation;
359
360 // these are thread safe
361 mutable Barrier mReadyToRunBarrier;
362 mutable SurfaceFlingerSynchro mSyncObject;
363 volatile int32_t mDeplayedTransactionPending;
364
365 // atomic variables
366 enum {
367 eConsoleReleased = 1,
368 eConsoleAcquired = 2
369 };
370 volatile int32_t mConsoleSignals;
371
372 // only written in the main thread, only read in other threads
373 volatile int32_t mSecureFrameBuffer;
374};
375
376// ---------------------------------------------------------------------------
377
378class FreezeLock : public LightRefBase<FreezeLock> {
379 SurfaceFlinger* mFlinger;
380public:
381 FreezeLock(SurfaceFlinger* flinger)
382 : mFlinger(flinger) {
383 mFlinger->incFreezeCount();
384 }
385 ~FreezeLock() {
386 mFlinger->decFreezeCount();
387 }
388};
389
390// ---------------------------------------------------------------------------
391
392class BClient : public BnSurfaceFlingerClient
393{
394public:
395 BClient(SurfaceFlinger *flinger, ClientID cid,
396 const sp<IMemory>& cblk);
397 ~BClient();
398
399 // ISurfaceFlingerClient interface
400 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
401
402 virtual sp<ISurface> createSurface(
403 surface_data_t* params, int pid,
404 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
405 uint32_t flags);
406
407 virtual status_t destroySurface(SurfaceID surfaceId);
408 virtual status_t setState(int32_t count, const layer_state_t* states);
409
410private:
411 ClientID mId;
412 SurfaceFlinger* mFlinger;
413 sp<IMemory> mCblk;
414};
415
416// ---------------------------------------------------------------------------
417}; // namespace android
418
419#endif // ANDROID_SURFACE_FLINGER_H