blob: 76e40a1222e2ce3cb8169fd430305f9430dfb76f [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include "Barrier.h"
39#include "BootAnimation.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include "Layer.h"
41#include "Tokenizer.h"
42
Mathias Agopianf1d8e872009-04-20 19:39:12 -070043#include "MessageQueue.h"
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045struct copybit_device_t;
46struct overlay_device_t;
47
48namespace android {
49
50// ---------------------------------------------------------------------------
51
52class Client;
53class BClient;
54class DisplayHardware;
55class FreezeLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056class Layer;
57class LayerBuffer;
58class LayerOrientationAnim;
59class OrientationAnimation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61typedef int32_t ClientID;
62
63#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
64#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
65
66// ---------------------------------------------------------------------------
67
68class Client
69{
70public:
71 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
72 ~Client();
73
74 int32_t generateId(int pid);
75 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
78 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 sp<LayerBaseClient> getLayerUser(int32_t i) const;
80 const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
82 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083
84 // pointer to this client's control block
85 per_client_cblk_t* ctrlblk;
86 ClientID cid;
87
88
89private:
90 int getClientPid() const { return mPid; }
91
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092 int mPid;
93 uint32_t mBitmap;
94 SortedVector<uint8_t> mInUse;
95 Vector< wp<LayerBaseClient> > mLayers;
96 sp<MemoryDealer> mCblkHeap;
97 sp<SurfaceFlinger> mFlinger;
98 sp<IMemory> mCblkMemory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099};
100
101// ---------------------------------------------------------------------------
102
103class GraphicPlane
104{
105public:
106 static status_t orientationToTransfrom(int orientation, int w, int h,
107 Transform* tr);
108
109 GraphicPlane();
110 ~GraphicPlane();
111
112 bool initialized() const;
113
114 void setDisplayHardware(DisplayHardware *);
115 void setTransform(const Transform& tr);
116 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700117 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 const DisplayHardware& displayHardware() const;
120 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 EGLDisplay getEGLDisplay() const;
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123private:
124 GraphicPlane(const GraphicPlane&);
125 GraphicPlane operator = (const GraphicPlane&);
126
127 DisplayHardware* mHw;
128 Transform mTransform;
129 Transform mOrientationTransform;
130 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700131 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132};
133
134// ---------------------------------------------------------------------------
135
136enum {
137 eTransactionNeeded = 0x01,
138 eTraversalNeeded = 0x02
139};
140
141class SurfaceFlinger : public BnSurfaceComposer, protected Thread
142{
143public:
144 static void instantiate();
145 static void shutdown();
146
147 SurfaceFlinger();
148 virtual ~SurfaceFlinger();
149 void init();
150
151 virtual status_t onTransact(
152 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
153
154 virtual status_t dump(int fd, const Vector<String16>& args);
155
156 // ISurfaceComposer interface
157 virtual sp<ISurfaceFlingerClient> createConnection();
158 virtual sp<IMemory> getCblk() const;
159 virtual void bootFinished();
160 virtual void openGlobalTransaction();
161 virtual void closeGlobalTransaction();
162 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
163 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700164 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
167 void screenReleased(DisplayID dpy);
168 void screenAcquired(DisplayID dpy);
169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 overlay_control_device_t* getOverlayEngine() const;
171
172
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173 status_t removeLayer(const sp<LayerBase>& layer);
174 status_t addLayer(const sp<LayerBase>& layer);
175 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
177private:
178 friend class BClient;
179 friend class LayerBase;
180 friend class LayerBuffer;
181 friend class LayerBaseClient;
182 friend class Layer;
183 friend class LayerBlur;
184
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(
191 Client* client, DisplayID display,
192 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(
196 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(
200 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(
204 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);
252
253 void computeVisibleRegions(
254 LayerVector& currentLayers,
255 Region& dirtyRegion,
256 Region& wormholeRegion);
257
258 void handlePageFlip();
259 bool lockPageFlip(const LayerVector& currentLayers);
260 void unlockPageFlip(const LayerVector& currentLayers);
261 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 void scheduleBroadcast(Client* client);
263 void executeScheduledBroadcasts();
264 void postFramebuffer();
265 void composeSurfaces(const Region& dirty);
266 void unlockClients();
267
268
269 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700270 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
271 status_t addLayer_l(const sp<LayerBase>& layer);
272 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700273 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 void free_resources_l();
275
276 uint32_t getTransactionFlags(uint32_t flags);
277 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
278 void commitTransaction();
279
280
281 friend class FreezeLock;
282 sp<FreezeLock> getFreezeLock() const;
283 inline void incFreezeCount() { mFreezeCount++; }
284 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
285 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
286 inline bool isFrozen() const {
287 return mFreezeDisplay || mFreezeCount>0;
288 }
289
290
291 void debugFlashRegions();
292 void debugShowFPS() const;
293 void drawWormhole() const;
294
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700295
296 mutable MessageQueue mEventQueue;
297
298
299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 // access must be protected by mStateLock
301 mutable Mutex mStateLock;
302 State mCurrentState;
303 State mDrawingState;
304 volatile int32_t mTransactionFlags;
305 volatile int32_t mTransactionCount;
306 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700307 SortedVector< sp<LayerBase> > mLayerPurgatory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
Mathias Agopian9a112062009-04-17 19:36:26 -0700309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 // protected by mStateLock (but we could use another lock)
311 Tokenizer mTokens;
312 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 Vector<Client*> mDisconnectedClients;
317
318 // constant members (no synchronization needed for access)
319 sp<MemoryDealer> mServerHeap;
320 sp<IMemory> mServerCblkMemory;
321 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 GLuint mWormholeTexName;
323 sp<BootAnimation> mBootAnimation;
324 nsecs_t mBootTime;
325
326 // Can only accessed from the main thread, these members
327 // don't need synchronization
328 Region mDirtyRegion;
329 Region mInvalidRegion;
330 Region mWormholeRegion;
331 Client* mLastScheduledBroadcast;
332 SortedVector<Client*> mScheduledBroadcasts;
333 bool mVisibleRegionsDirty;
334 bool mDeferReleaseConsole;
335 bool mFreezeDisplay;
336 int32_t mFreezeCount;
337 nsecs_t mFreezeDisplayTime;
338 friend class OrientationAnimation;
339 OrientationAnimation* mOrientationAnimation;
340
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 // don't use a lock for these, we don't care
342 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 int mDebugFps;
344 int mDebugBackground;
345 int mDebugNoBootAnimation;
346
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