blob: b7b008d9ff069f0919816d8b788ff77c504fe5eb [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
Mathias Agopianf9d93272009-06-19 17:00:27 -070067class Client : public RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068{
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;
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700183 friend class LayerDim;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
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(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700191 const sp<Client>& client, DisplayID display,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 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(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700196 const sp<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(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700200 const sp<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(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700204 const sp<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 Agopian9a112062009-04-17 19:36:26 -0700207 status_t removeSurface(SurfaceID surface_id);
208 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210
211
212 class LayerVector {
213 public:
214 inline LayerVector() { }
215 LayerVector(const LayerVector&);
216 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 inline sp<LayerBase> const* array() const { return layers.array(); }
218 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
219 ssize_t remove(const sp<LayerBase>&);
220 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
221 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
222 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700224 KeyedVector< sp<LayerBase> , size_t> lookup;
225 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 };
227
228 struct State {
229 State() {
230 orientation = ISurfaceComposer::eOrientationDefault;
231 freezeDisplay = 0;
232 }
233 LayerVector layersSortedByZ;
234 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700235 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 uint8_t freezeDisplay;
237 };
238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 virtual bool threadLoop();
240 virtual status_t readyToRun();
241 virtual void onFirstRef();
242
243 const GraphicPlane& graphicPlane(int dpy) const;
244 GraphicPlane& graphicPlane(int dpy);
245
246 void waitForEvent();
247 void signalEvent();
248 void signalDelayedEvent(nsecs_t delay);
249
250 void handleConsoleEvents();
251 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700252 void handleTransactionLocked(
253 uint32_t transactionFlags,
254 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255
256 void computeVisibleRegions(
257 LayerVector& currentLayers,
258 Region& dirtyRegion,
259 Region& wormholeRegion);
260
261 void handlePageFlip();
262 bool lockPageFlip(const LayerVector& currentLayers);
263 void unlockPageFlip(const LayerVector& currentLayers);
264 void handleRepaint();
Mathias Agopianf9d93272009-06-19 17:00:27 -0700265 void scheduleBroadcast(const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 void executeScheduledBroadcasts();
267 void postFramebuffer();
268 void composeSurfaces(const Region& dirty);
269 void unlockClients();
270
271
272 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700273 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
274 status_t addLayer_l(const sp<LayerBase>& layer);
275 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700276 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 void free_resources_l();
278
279 uint32_t getTransactionFlags(uint32_t flags);
280 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
281 void commitTransaction();
282
283
284 friend class FreezeLock;
285 sp<FreezeLock> getFreezeLock() const;
286 inline void incFreezeCount() { mFreezeCount++; }
287 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
288 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
289 inline bool isFrozen() const {
290 return mFreezeDisplay || mFreezeCount>0;
291 }
292
293
294 void debugFlashRegions();
295 void debugShowFPS() const;
296 void drawWormhole() const;
297
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700298
299 mutable MessageQueue mEventQueue;
300
301
302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 // access must be protected by mStateLock
304 mutable Mutex mStateLock;
305 State mCurrentState;
306 State mDrawingState;
307 volatile int32_t mTransactionFlags;
308 volatile int32_t mTransactionCount;
309 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 // protected by mStateLock (but we could use another lock)
312 Tokenizer mTokens;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700313 DefaultKeyedVector<ClientID, sp<Client> > mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700314 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316 bool mLayersRemoved;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700317 Vector< sp<Client> > mDisconnectedClients;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318
319 // constant members (no synchronization needed for access)
320 sp<MemoryDealer> mServerHeap;
321 sp<IMemory> mServerCblkMemory;
322 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 nsecs_t mBootTime;
Mathias Agopian375f5632009-06-15 18:24:59 -0700325 Permission mHardwareTest;
326 Permission mAccessSurfaceFlinger;
327 Permission mDump;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328
329 // Can only accessed from the main thread, these members
330 // don't need synchronization
331 Region mDirtyRegion;
332 Region mInvalidRegion;
333 Region mWormholeRegion;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700334 wp<Client> mLastScheduledBroadcast;
335 SortedVector< wp<Client> > mScheduledBroadcasts;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 bool mVisibleRegionsDirty;
337 bool mDeferReleaseConsole;
338 bool mFreezeDisplay;
339 int32_t mFreezeCount;
340 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 // don't use a lock for these, we don't care
343 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 int mDebugBackground;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345
346 // these are thread safe
347 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348
349 // atomic variables
350 enum {
351 eConsoleReleased = 1,
352 eConsoleAcquired = 2
353 };
354 volatile int32_t mConsoleSignals;
355
356 // only written in the main thread, only read in other threads
357 volatile int32_t mSecureFrameBuffer;
358};
359
360// ---------------------------------------------------------------------------
361
362class FreezeLock : public LightRefBase<FreezeLock> {
363 SurfaceFlinger* mFlinger;
364public:
365 FreezeLock(SurfaceFlinger* flinger)
366 : mFlinger(flinger) {
367 mFlinger->incFreezeCount();
368 }
369 ~FreezeLock() {
370 mFlinger->decFreezeCount();
371 }
372};
373
374// ---------------------------------------------------------------------------
375
376class BClient : public BnSurfaceFlingerClient
377{
378public:
379 BClient(SurfaceFlinger *flinger, ClientID cid,
380 const sp<IMemory>& cblk);
381 ~BClient();
382
383 // ISurfaceFlingerClient interface
384 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
385
386 virtual sp<ISurface> createSurface(
387 surface_data_t* params, int pid,
388 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
389 uint32_t flags);
390
391 virtual status_t destroySurface(SurfaceID surfaceId);
392 virtual status_t setState(int32_t count, const layer_state_t* states);
393
394private:
395 ClientID mId;
396 SurfaceFlinger* mFlinger;
397 sp<IMemory> mCblk;
398};
399
400// ---------------------------------------------------------------------------
401}; // namespace android
402
403#endif // ANDROID_SURFACE_FLINGER_H