blob: ebe315a450f7496a12d46cf21b7b97760fb40afe [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>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mathias Agopian375f5632009-06-15 18:24:59 -070030#include <binder/MemoryDealer.h>
31#include <binder/Permission.h>
32
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <ui/PixelFormat.h>
34#include <ui/ISurfaceComposer.h>
35#include <ui/ISurfaceFlingerClient.h>
36
37#include <private/ui/SharedState.h>
38#include <private/ui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40#include "Barrier.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include "Layer.h"
42#include "Tokenizer.h"
43
Mathias Agopianf1d8e872009-04-20 19:39:12 -070044#include "MessageQueue.h"
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046struct copybit_device_t;
47struct overlay_device_t;
48
49namespace android {
50
51// ---------------------------------------------------------------------------
52
53class Client;
54class BClient;
55class DisplayHardware;
56class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057class Layer;
58class LayerBuffer;
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 virtual bool threadLoop();
239 virtual status_t readyToRun();
240 virtual void onFirstRef();
241
242 const GraphicPlane& graphicPlane(int dpy) const;
243 GraphicPlane& graphicPlane(int dpy);
244
245 void waitForEvent();
246 void signalEvent();
247 void signalDelayedEvent(nsecs_t delay);
248
249 void handleConsoleEvents();
250 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700251 void handleTransactionLocked(
252 uint32_t transactionFlags,
253 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254
255 void computeVisibleRegions(
256 LayerVector& currentLayers,
257 Region& dirtyRegion,
258 Region& wormholeRegion);
259
260 void handlePageFlip();
261 bool lockPageFlip(const LayerVector& currentLayers);
262 void unlockPageFlip(const LayerVector& currentLayers);
263 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 void scheduleBroadcast(Client* client);
265 void executeScheduledBroadcasts();
266 void postFramebuffer();
267 void composeSurfaces(const Region& dirty);
268 void unlockClients();
269
270
271 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
273 status_t addLayer_l(const sp<LayerBase>& layer);
274 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700275 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 void free_resources_l();
277
278 uint32_t getTransactionFlags(uint32_t flags);
279 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
280 void commitTransaction();
281
282
283 friend class FreezeLock;
284 sp<FreezeLock> getFreezeLock() const;
285 inline void incFreezeCount() { mFreezeCount++; }
286 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
287 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
288 inline bool isFrozen() const {
289 return mFreezeDisplay || mFreezeCount>0;
290 }
291
292
293 void debugFlashRegions();
294 void debugShowFPS() const;
295 void drawWormhole() const;
296
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700297
298 mutable MessageQueue mEventQueue;
299
300
301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 // access must be protected by mStateLock
303 mutable Mutex mStateLock;
304 State mCurrentState;
305 State mDrawingState;
306 volatile int32_t mTransactionFlags;
307 volatile int32_t mTransactionCount;
308 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700309 SortedVector< sp<LayerBase> > mLayerPurgatory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Mathias Agopian9a112062009-04-17 19:36:26 -0700311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 // protected by mStateLock (but we could use another lock)
313 Tokenizer mTokens;
314 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 Vector<Client*> mDisconnectedClients;
319
320 // constant members (no synchronization needed for access)
321 sp<MemoryDealer> mServerHeap;
322 sp<IMemory> mServerCblkMemory;
323 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 nsecs_t mBootTime;
Mathias Agopian375f5632009-06-15 18:24:59 -0700326 Permission mHardwareTest;
327 Permission mAccessSurfaceFlinger;
328 Permission mDump;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329
330 // Can only accessed from the main thread, these members
331 // don't need synchronization
332 Region mDirtyRegion;
333 Region mInvalidRegion;
334 Region mWormholeRegion;
335 Client* mLastScheduledBroadcast;
336 SortedVector<Client*> mScheduledBroadcasts;
337 bool mVisibleRegionsDirty;
338 bool mDeferReleaseConsole;
339 bool mFreezeDisplay;
340 int32_t mFreezeCount;
341 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 // don't use a lock for these, we don't care
344 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 int mDebugBackground;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346
347 // these are thread safe
348 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
350 // atomic variables
351 enum {
352 eConsoleReleased = 1,
353 eConsoleAcquired = 2
354 };
355 volatile int32_t mConsoleSignals;
356
357 // only written in the main thread, only read in other threads
358 volatile int32_t mSecureFrameBuffer;
359};
360
361// ---------------------------------------------------------------------------
362
363class FreezeLock : public LightRefBase<FreezeLock> {
364 SurfaceFlinger* mFlinger;
365public:
366 FreezeLock(SurfaceFlinger* flinger)
367 : mFlinger(flinger) {
368 mFlinger->incFreezeCount();
369 }
370 ~FreezeLock() {
371 mFlinger->decFreezeCount();
372 }
373};
374
375// ---------------------------------------------------------------------------
376
377class BClient : public BnSurfaceFlingerClient
378{
379public:
380 BClient(SurfaceFlinger *flinger, ClientID cid,
381 const sp<IMemory>& cblk);
382 ~BClient();
383
384 // ISurfaceFlingerClient interface
385 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
386
387 virtual sp<ISurface> createSurface(
388 surface_data_t* params, int pid,
389 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
390 uint32_t flags);
391
392 virtual status_t destroySurface(SurfaceID surfaceId);
393 virtual status_t setState(int32_t count, const layer_state_t* states);
394
395private:
396 ClientID mId;
397 SurfaceFlinger* mFlinger;
398 sp<IMemory> mCblk;
399};
400
401// ---------------------------------------------------------------------------
402}; // namespace android
403
404#endif // ANDROID_SURFACE_FLINGER_H