blob: 6bbb21ff5c9c9f1c705db452e6570c58767b8d96 [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 Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include "Barrier.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include "Layer.h"
40#include "Tokenizer.h"
41
Mathias Agopianf1d8e872009-04-20 19:39:12 -070042#include "MessageQueue.h"
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044struct 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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58typedef int32_t ClientID;
59
60#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
61#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
62
63// ---------------------------------------------------------------------------
64
65class Client
66{
67public:
68 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
69 ~Client();
70
71 int32_t generateId(int pid);
72 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
75 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 sp<LayerBaseClient> getLayerUser(int32_t i) const;
77 const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
79 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
81 // pointer to this client's control block
82 per_client_cblk_t* ctrlblk;
83 ClientID cid;
84
85
86private:
87 int getClientPid() const { return mPid; }
88
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089 int mPid;
90 uint32_t mBitmap;
91 SortedVector<uint8_t> mInUse;
92 Vector< wp<LayerBaseClient> > mLayers;
93 sp<MemoryDealer> mCblkHeap;
94 sp<SurfaceFlinger> mFlinger;
95 sp<IMemory> mCblkMemory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096};
97
98// ---------------------------------------------------------------------------
99
100class GraphicPlane
101{
102public:
103 static status_t orientationToTransfrom(int orientation, int w, int h,
104 Transform* tr);
105
106 GraphicPlane();
107 ~GraphicPlane();
108
109 bool initialized() const;
110
111 void setDisplayHardware(DisplayHardware *);
112 void setTransform(const Transform& tr);
113 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700114 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115
116 const DisplayHardware& displayHardware() const;
117 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118 EGLDisplay getEGLDisplay() const;
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120private:
121 GraphicPlane(const GraphicPlane&);
122 GraphicPlane operator = (const GraphicPlane&);
123
124 DisplayHardware* mHw;
125 Transform mTransform;
126 Transform mOrientationTransform;
127 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700128 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129};
130
131// ---------------------------------------------------------------------------
132
133enum {
134 eTransactionNeeded = 0x01,
135 eTraversalNeeded = 0x02
136};
137
138class SurfaceFlinger : public BnSurfaceComposer, protected Thread
139{
140public:
141 static void instantiate();
142 static void shutdown();
143
144 SurfaceFlinger();
145 virtual ~SurfaceFlinger();
146 void init();
147
148 virtual status_t onTransact(
149 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
150
151 virtual status_t dump(int fd, const Vector<String16>& args);
152
153 // ISurfaceComposer interface
154 virtual sp<ISurfaceFlingerClient> createConnection();
155 virtual sp<IMemory> getCblk() const;
156 virtual void bootFinished();
157 virtual void openGlobalTransaction();
158 virtual void closeGlobalTransaction();
159 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
160 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700161 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
164 void screenReleased(DisplayID dpy);
165 void screenAcquired(DisplayID dpy);
166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 overlay_control_device_t* getOverlayEngine() const;
168
169
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170 status_t removeLayer(const sp<LayerBase>& layer);
171 status_t addLayer(const sp<LayerBase>& layer);
172 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
174private:
175 friend class BClient;
176 friend class LayerBase;
177 friend class LayerBuffer;
178 friend class LayerBaseClient;
179 friend class Layer;
180 friend class LayerBlur;
181
182 sp<ISurface> createSurface(ClientID client, int pid,
183 ISurfaceFlingerClient::surface_data_t* params,
184 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
185 uint32_t flags);
186
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 sp<LayerBaseClient> createNormalSurfaceLocked(
188 Client* client, DisplayID display,
189 int32_t id, uint32_t w, uint32_t h,
190 PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 sp<LayerBaseClient> createBlurSurfaceLocked(
193 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
195
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 sp<LayerBaseClient> createDimSurfaceLocked(
197 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
199
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
201 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 Agopian9a112062009-04-17 19:36:26 -0700204 status_t removeSurface(SurfaceID surface_id);
205 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
208
209 class LayerVector {
210 public:
211 inline LayerVector() { }
212 LayerVector(const LayerVector&);
213 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214 inline sp<LayerBase> const* array() const { return layers.array(); }
215 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
216 ssize_t remove(const sp<LayerBase>&);
217 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
218 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
219 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 KeyedVector< sp<LayerBase> , size_t> lookup;
222 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 };
224
225 struct State {
226 State() {
227 orientation = ISurfaceComposer::eOrientationDefault;
228 freezeDisplay = 0;
229 }
230 LayerVector layersSortedByZ;
231 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700232 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 uint8_t freezeDisplay;
234 };
235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 virtual bool threadLoop();
237 virtual status_t readyToRun();
238 virtual void onFirstRef();
239
240 const GraphicPlane& graphicPlane(int dpy) const;
241 GraphicPlane& graphicPlane(int dpy);
242
243 void waitForEvent();
244 void signalEvent();
245 void signalDelayedEvent(nsecs_t delay);
246
247 void handleConsoleEvents();
248 void handleTransaction(uint32_t transactionFlags);
249
250 void computeVisibleRegions(
251 LayerVector& currentLayers,
252 Region& dirtyRegion,
253 Region& wormholeRegion);
254
255 void handlePageFlip();
256 bool lockPageFlip(const LayerVector& currentLayers);
257 void unlockPageFlip(const LayerVector& currentLayers);
258 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 void scheduleBroadcast(Client* client);
260 void executeScheduledBroadcasts();
261 void postFramebuffer();
262 void composeSurfaces(const Region& dirty);
263 void unlockClients();
264
265
266 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700267 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
268 status_t addLayer_l(const sp<LayerBase>& layer);
269 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700270 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 void free_resources_l();
272
273 uint32_t getTransactionFlags(uint32_t flags);
274 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
275 void commitTransaction();
276
277
278 friend class FreezeLock;
279 sp<FreezeLock> getFreezeLock() const;
280 inline void incFreezeCount() { mFreezeCount++; }
281 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
282 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
283 inline bool isFrozen() const {
284 return mFreezeDisplay || mFreezeCount>0;
285 }
286
287
288 void debugFlashRegions();
289 void debugShowFPS() const;
290 void drawWormhole() const;
291
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700292
293 mutable MessageQueue mEventQueue;
294
295
296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 // access must be protected by mStateLock
298 mutable Mutex mStateLock;
299 State mCurrentState;
300 State mDrawingState;
301 volatile int32_t mTransactionFlags;
302 volatile int32_t mTransactionCount;
303 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700304 SortedVector< sp<LayerBase> > mLayerPurgatory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
Mathias Agopian9a112062009-04-17 19:36:26 -0700306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 // protected by mStateLock (but we could use another lock)
308 Tokenizer mTokens;
309 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700310 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 Vector<Client*> mDisconnectedClients;
314
315 // constant members (no synchronization needed for access)
316 sp<MemoryDealer> mServerHeap;
317 sp<IMemory> mServerCblkMemory;
318 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 GLuint mWormholeTexName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 nsecs_t mBootTime;
321
322 // Can only accessed from the main thread, these members
323 // don't need synchronization
324 Region mDirtyRegion;
325 Region mInvalidRegion;
326 Region mWormholeRegion;
327 Client* mLastScheduledBroadcast;
328 SortedVector<Client*> mScheduledBroadcasts;
329 bool mVisibleRegionsDirty;
330 bool mDeferReleaseConsole;
331 bool mFreezeDisplay;
332 int32_t mFreezeCount;
333 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 // don't use a lock for these, we don't care
336 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 int mDebugBackground;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338
339 // these are thread safe
340 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341
342 // atomic variables
343 enum {
344 eConsoleReleased = 1,
345 eConsoleAcquired = 2
346 };
347 volatile int32_t mConsoleSignals;
348
349 // only written in the main thread, only read in other threads
350 volatile int32_t mSecureFrameBuffer;
351};
352
353// ---------------------------------------------------------------------------
354
355class FreezeLock : public LightRefBase<FreezeLock> {
356 SurfaceFlinger* mFlinger;
357public:
358 FreezeLock(SurfaceFlinger* flinger)
359 : mFlinger(flinger) {
360 mFlinger->incFreezeCount();
361 }
362 ~FreezeLock() {
363 mFlinger->decFreezeCount();
364 }
365};
366
367// ---------------------------------------------------------------------------
368
369class BClient : public BnSurfaceFlingerClient
370{
371public:
372 BClient(SurfaceFlinger *flinger, ClientID cid,
373 const sp<IMemory>& cblk);
374 ~BClient();
375
376 // ISurfaceFlingerClient interface
377 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
378
379 virtual sp<ISurface> createSurface(
380 surface_data_t* params, int pid,
381 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
382 uint32_t flags);
383
384 virtual status_t destroySurface(SurfaceID surfaceId);
385 virtual status_t setState(int32_t count, const layer_state_t* states);
386
387private:
388 ClientID mId;
389 SurfaceFlinger* mFlinger;
390 sp<IMemory> mCblk;
391};
392
393// ---------------------------------------------------------------------------
394}; // namespace android
395
396#endif // ANDROID_SURFACE_FLINGER_H