blob: d0095a2bdfeb0dd071a6fa1d7f496261cc5398a7 [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 Agopian7303c6b2009-07-02 18:11:53 -070030#include <binder/IMemory.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070031#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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
Mathias Agopian7303c6b2009-07-02 18:11:53 -070081 const Vector< wp<LayerBaseClient> >& getLayers() const {
82 return mLayers;
83 }
84
85 const sp<IMemoryHeap>& getControlBlockMemory() const {
86 return mCblkHeap;
87 }
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 // pointer to this client's control block
90 per_client_cblk_t* ctrlblk;
91 ClientID cid;
92
93
94private:
Mathias Agopian7303c6b2009-07-02 18:11:53 -070095 int getClientPid() const { return mPid; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097 int mPid;
98 uint32_t mBitmap;
99 SortedVector<uint8_t> mInUse;
100 Vector< wp<LayerBaseClient> > mLayers;
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700101 sp<IMemoryHeap> mCblkHeap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 sp<SurfaceFlinger> mFlinger;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103};
104
105// ---------------------------------------------------------------------------
106
107class GraphicPlane
108{
109public:
110 static status_t orientationToTransfrom(int orientation, int w, int h,
111 Transform* tr);
112
113 GraphicPlane();
114 ~GraphicPlane();
115
116 bool initialized() const;
117
118 void setDisplayHardware(DisplayHardware *);
119 void setTransform(const Transform& tr);
120 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700121 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122
123 const DisplayHardware& displayHardware() const;
124 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700125 EGLDisplay getEGLDisplay() const;
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127private:
128 GraphicPlane(const GraphicPlane&);
129 GraphicPlane operator = (const GraphicPlane&);
130
131 DisplayHardware* mHw;
132 Transform mTransform;
133 Transform mOrientationTransform;
134 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700135 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136};
137
138// ---------------------------------------------------------------------------
139
140enum {
141 eTransactionNeeded = 0x01,
142 eTraversalNeeded = 0x02
143};
144
145class SurfaceFlinger : public BnSurfaceComposer, protected Thread
146{
147public:
148 static void instantiate();
149 static void shutdown();
150
151 SurfaceFlinger();
152 virtual ~SurfaceFlinger();
153 void init();
154
155 virtual status_t onTransact(
156 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
157
158 virtual status_t dump(int fd, const Vector<String16>& args);
159
160 // ISurfaceComposer interface
161 virtual sp<ISurfaceFlingerClient> createConnection();
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700162 virtual sp<IMemoryHeap> getCblk() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 virtual void bootFinished();
164 virtual void openGlobalTransaction();
165 virtual void closeGlobalTransaction();
166 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
167 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700168 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170
171 void screenReleased(DisplayID dpy);
172 void screenAcquired(DisplayID dpy);
173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 overlay_control_device_t* getOverlayEngine() const;
175
176
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 status_t removeLayer(const sp<LayerBase>& layer);
178 status_t addLayer(const sp<LayerBase>& layer);
179 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180
181private:
182 friend class BClient;
183 friend class LayerBase;
184 friend class LayerBuffer;
185 friend class LayerBaseClient;
186 friend class Layer;
187 friend class LayerBlur;
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700188 friend class LayerDim;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189
190 sp<ISurface> createSurface(ClientID client, int pid,
191 ISurfaceFlingerClient::surface_data_t* params,
192 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
193 uint32_t flags);
194
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700195 sp<LayerBaseClient> createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700196 const sp<Client>& client, DisplayID display,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 int32_t id, uint32_t w, uint32_t h,
198 PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 sp<LayerBaseClient> createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700201 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
203
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 sp<LayerBaseClient> createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700205 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
207
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -0700209 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
211
Mathias Agopian9a112062009-04-17 19:36:26 -0700212 status_t removeSurface(SurfaceID surface_id);
213 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215
216
217 class LayerVector {
218 public:
219 inline LayerVector() { }
220 LayerVector(const LayerVector&);
221 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222 inline sp<LayerBase> const* array() const { return layers.array(); }
223 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
224 ssize_t remove(const sp<LayerBase>&);
225 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
226 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
227 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229 KeyedVector< sp<LayerBase> , size_t> lookup;
230 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 };
232
233 struct State {
234 State() {
235 orientation = ISurfaceComposer::eOrientationDefault;
236 freezeDisplay = 0;
237 }
238 LayerVector layersSortedByZ;
239 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700240 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 uint8_t freezeDisplay;
242 };
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 virtual bool threadLoop();
245 virtual status_t readyToRun();
246 virtual void onFirstRef();
247
248 const GraphicPlane& graphicPlane(int dpy) const;
249 GraphicPlane& graphicPlane(int dpy);
250
251 void waitForEvent();
252 void signalEvent();
253 void signalDelayedEvent(nsecs_t delay);
254
255 void handleConsoleEvents();
256 void handleTransaction(uint32_t transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700257 void handleTransactionLocked(
258 uint32_t transactionFlags,
259 Vector< sp<LayerBase> >& ditchedLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260
261 void computeVisibleRegions(
262 LayerVector& currentLayers,
263 Region& dirtyRegion,
264 Region& wormholeRegion);
265
266 void handlePageFlip();
267 bool lockPageFlip(const LayerVector& currentLayers);
268 void unlockPageFlip(const LayerVector& currentLayers);
269 void handleRepaint();
Mathias Agopianf9d93272009-06-19 17:00:27 -0700270 void scheduleBroadcast(const sp<Client>& client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 void executeScheduledBroadcasts();
272 void postFramebuffer();
273 void composeSurfaces(const Region& dirty);
274 void unlockClients();
275
276
277 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
279 status_t addLayer_l(const sp<LayerBase>& layer);
280 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700281 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 void free_resources_l();
283
284 uint32_t getTransactionFlags(uint32_t flags);
285 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
286 void commitTransaction();
287
288
289 friend class FreezeLock;
290 sp<FreezeLock> getFreezeLock() const;
291 inline void incFreezeCount() { mFreezeCount++; }
292 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
293 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
294 inline bool isFrozen() const {
295 return mFreezeDisplay || mFreezeCount>0;
296 }
297
298
299 void debugFlashRegions();
300 void debugShowFPS() const;
301 void drawWormhole() const;
302
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700303
304 mutable MessageQueue mEventQueue;
305
306
307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 // access must be protected by mStateLock
309 mutable Mutex mStateLock;
310 State mCurrentState;
311 State mDrawingState;
312 volatile int32_t mTransactionFlags;
313 volatile int32_t mTransactionCount;
314 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 // protected by mStateLock (but we could use another lock)
317 Tokenizer mTokens;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700318 DefaultKeyedVector<ClientID, sp<Client> > mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700321 bool mLayersRemoved;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700322 Vector< sp<Client> > mDisconnectedClients;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323
324 // constant members (no synchronization needed for access)
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700325 sp<IMemoryHeap> mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 nsecs_t mBootTime;
Mathias Agopian375f5632009-06-15 18:24:59 -0700329 Permission mHardwareTest;
330 Permission mAccessSurfaceFlinger;
331 Permission mDump;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332
333 // Can only accessed from the main thread, these members
334 // don't need synchronization
335 Region mDirtyRegion;
336 Region mInvalidRegion;
337 Region mWormholeRegion;
Mathias Agopianf9d93272009-06-19 17:00:27 -0700338 wp<Client> mLastScheduledBroadcast;
339 SortedVector< wp<Client> > mScheduledBroadcasts;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 bool mVisibleRegionsDirty;
341 bool mDeferReleaseConsole;
342 bool mFreezeDisplay;
343 int32_t mFreezeCount;
344 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 // don't use a lock for these, we don't care
347 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 int mDebugBackground;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
350 // these are thread safe
351 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352
353 // atomic variables
354 enum {
355 eConsoleReleased = 1,
356 eConsoleAcquired = 2
357 };
358 volatile int32_t mConsoleSignals;
359
360 // only written in the main thread, only read in other threads
361 volatile int32_t mSecureFrameBuffer;
362};
363
364// ---------------------------------------------------------------------------
365
366class FreezeLock : public LightRefBase<FreezeLock> {
367 SurfaceFlinger* mFlinger;
368public:
369 FreezeLock(SurfaceFlinger* flinger)
370 : mFlinger(flinger) {
371 mFlinger->incFreezeCount();
372 }
373 ~FreezeLock() {
374 mFlinger->decFreezeCount();
375 }
376};
377
378// ---------------------------------------------------------------------------
379
380class BClient : public BnSurfaceFlingerClient
381{
382public:
383 BClient(SurfaceFlinger *flinger, ClientID cid,
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700384 const sp<IMemoryHeap>& cblk);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 ~BClient();
386
387 // ISurfaceFlingerClient interface
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700388 virtual sp<IMemoryHeap> getControlBlock() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389
390 virtual sp<ISurface> createSurface(
391 surface_data_t* params, int pid,
392 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
393 uint32_t flags);
394
395 virtual status_t destroySurface(SurfaceID surfaceId);
396 virtual status_t setState(int32_t count, const layer_state_t* states);
397
398private:
399 ClientID mId;
400 SurfaceFlinger* mFlinger;
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700401 sp<IMemoryHeap> mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402};
403
404// ---------------------------------------------------------------------------
405}; // namespace android
406
407#endif // ANDROID_SURFACE_FLINGER_H