blob: d5e525273c77ad3660a0c6ae4ddb4334481fc900 [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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
59typedef int32_t ClientID;
60
61#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
62#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
63
64// ---------------------------------------------------------------------------
65
66class Client
67{
68public:
69 Client(ClientID cid, const sp<SurfaceFlinger>& flinger);
70 ~Client();
71
72 int32_t generateId(int pid);
73 void free(int32_t id);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 status_t bindLayer(const sp<LayerBaseClient>& layer, int32_t id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
76 inline bool isValid(int32_t i) const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077 sp<LayerBaseClient> getLayerUser(int32_t i) const;
78 const Vector< wp<LayerBaseClient> >& getLayers() const { return mLayers; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 const sp<IMemory>& controlBlockMemory() const { return mCblkMemory; }
80 void dump(const char* what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
82 // pointer to this client's control block
83 per_client_cblk_t* ctrlblk;
84 ClientID cid;
85
86
87private:
88 int getClientPid() const { return mPid; }
89
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 int mPid;
91 uint32_t mBitmap;
92 SortedVector<uint8_t> mInUse;
93 Vector< wp<LayerBaseClient> > mLayers;
94 sp<MemoryDealer> mCblkHeap;
95 sp<SurfaceFlinger> mFlinger;
96 sp<IMemory> mCblkMemory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097};
98
99// ---------------------------------------------------------------------------
100
101class GraphicPlane
102{
103public:
104 static status_t orientationToTransfrom(int orientation, int w, int h,
105 Transform* tr);
106
107 GraphicPlane();
108 ~GraphicPlane();
109
110 bool initialized() const;
111
112 void setDisplayHardware(DisplayHardware *);
113 void setTransform(const Transform& tr);
114 status_t setOrientation(int orientation);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700115 int getOrientation() const { return mOrientation; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
117 const DisplayHardware& displayHardware() const;
118 const Transform& transform() const;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119 EGLDisplay getEGLDisplay() const;
120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121private:
122 GraphicPlane(const GraphicPlane&);
123 GraphicPlane operator = (const GraphicPlane&);
124
125 DisplayHardware* mHw;
126 Transform mTransform;
127 Transform mOrientationTransform;
128 Transform mGlobalTransform;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700129 int mOrientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130};
131
132// ---------------------------------------------------------------------------
133
134enum {
135 eTransactionNeeded = 0x01,
136 eTraversalNeeded = 0x02
137};
138
139class SurfaceFlinger : public BnSurfaceComposer, protected Thread
140{
141public:
142 static void instantiate();
143 static void shutdown();
144
145 SurfaceFlinger();
146 virtual ~SurfaceFlinger();
147 void init();
148
149 virtual status_t onTransact(
150 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
151
152 virtual status_t dump(int fd, const Vector<String16>& args);
153
154 // ISurfaceComposer interface
155 virtual sp<ISurfaceFlingerClient> createConnection();
156 virtual sp<IMemory> getCblk() const;
157 virtual void bootFinished();
158 virtual void openGlobalTransaction();
159 virtual void closeGlobalTransaction();
160 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags);
161 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags);
Mathias Agopianc08731e2009-03-27 18:11:38 -0700162 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 virtual void signal() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
165 void screenReleased(DisplayID dpy);
166 void screenAcquired(DisplayID dpy);
167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 overlay_control_device_t* getOverlayEngine() const;
169
170
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 status_t removeLayer(const sp<LayerBase>& layer);
172 status_t addLayer(const sp<LayerBase>& layer);
173 status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174
175private:
176 friend class BClient;
177 friend class LayerBase;
178 friend class LayerBuffer;
179 friend class LayerBaseClient;
180 friend class Layer;
181 friend class LayerBlur;
182
183 sp<ISurface> createSurface(ClientID client, int pid,
184 ISurfaceFlingerClient::surface_data_t* params,
185 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
186 uint32_t flags);
187
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700188 sp<LayerBaseClient> createNormalSurfaceLocked(
189 Client* client, DisplayID display,
190 int32_t id, uint32_t w, uint32_t h,
191 PixelFormat format, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 sp<LayerBaseClient> createBlurSurfaceLocked(
194 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
196
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 sp<LayerBaseClient> createDimSurfaceLocked(
198 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
200
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 sp<LayerBaseClient> createPushBuffersSurfaceLocked(
202 Client* client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 int32_t id, uint32_t w, uint32_t h, uint32_t flags);
204
Mathias Agopian9a112062009-04-17 19:36:26 -0700205 status_t removeSurface(SurfaceID surface_id);
206 status_t destroySurface(const sp<LayerBaseClient>& layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207 status_t setClientState(ClientID cid, int32_t count, const layer_state_t* states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
209
210 class LayerVector {
211 public:
212 inline LayerVector() { }
213 LayerVector(const LayerVector&);
214 inline size_t size() const { return layers.size(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 inline sp<LayerBase> const* array() const { return layers.array(); }
216 ssize_t add(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
217 ssize_t remove(const sp<LayerBase>&);
218 ssize_t reorder(const sp<LayerBase>&, Vector< sp<LayerBase> >::compar_t);
219 ssize_t indexOf(const sp<LayerBase>& key, size_t guess=0) const;
220 inline sp<LayerBase> operator [] (size_t i) const { return layers[i]; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 private:
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700222 KeyedVector< sp<LayerBase> , size_t> lookup;
223 Vector< sp<LayerBase> > layers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 };
225
226 struct State {
227 State() {
228 orientation = ISurfaceComposer::eOrientationDefault;
229 freezeDisplay = 0;
230 }
231 LayerVector layersSortedByZ;
232 uint8_t orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700233 uint8_t orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 uint8_t freezeDisplay;
235 };
236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 virtual bool threadLoop();
238 virtual status_t readyToRun();
239 virtual void onFirstRef();
240
241 const GraphicPlane& graphicPlane(int dpy) const;
242 GraphicPlane& graphicPlane(int dpy);
243
244 void waitForEvent();
245 void signalEvent();
246 void signalDelayedEvent(nsecs_t delay);
247
248 void handleConsoleEvents();
249 void handleTransaction(uint32_t transactionFlags);
250
251 void computeVisibleRegions(
252 LayerVector& currentLayers,
253 Region& dirtyRegion,
254 Region& wormholeRegion);
255
256 void handlePageFlip();
257 bool lockPageFlip(const LayerVector& currentLayers);
258 void unlockPageFlip(const LayerVector& currentLayers);
259 void handleRepaint();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 void scheduleBroadcast(Client* client);
261 void executeScheduledBroadcasts();
262 void postFramebuffer();
263 void composeSurfaces(const Region& dirty);
264 void unlockClients();
265
266
267 void destroyConnection(ClientID cid);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700268 sp<LayerBaseClient> getLayerUser_l(SurfaceID index) const;
269 status_t addLayer_l(const sp<LayerBase>& layer);
270 status_t removeLayer_l(const sp<LayerBase>& layer);
Mathias Agopian9a112062009-04-17 19:36:26 -0700271 status_t purgatorizeLayer_l(const sp<LayerBase>& layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 void free_resources_l();
273
274 uint32_t getTransactionFlags(uint32_t flags);
275 uint32_t setTransactionFlags(uint32_t flags, nsecs_t delay = 0);
276 void commitTransaction();
277
278
279 friend class FreezeLock;
280 sp<FreezeLock> getFreezeLock() const;
281 inline void incFreezeCount() { mFreezeCount++; }
282 inline void decFreezeCount() { if (mFreezeCount > 0) mFreezeCount--; }
283 inline bool hasFreezeRequest() const { return mFreezeDisplay; }
284 inline bool isFrozen() const {
285 return mFreezeDisplay || mFreezeCount>0;
286 }
287
288
289 void debugFlashRegions();
290 void debugShowFPS() const;
291 void drawWormhole() const;
292
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700293
294 mutable MessageQueue mEventQueue;
295
296
297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 // access must be protected by mStateLock
299 mutable Mutex mStateLock;
300 State mCurrentState;
301 State mDrawingState;
302 volatile int32_t mTransactionFlags;
303 volatile int32_t mTransactionCount;
304 Condition mTransactionCV;
Mathias Agopian9a112062009-04-17 19:36:26 -0700305 SortedVector< sp<LayerBase> > mLayerPurgatory;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
Mathias Agopian9a112062009-04-17 19:36:26 -0700307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 // protected by mStateLock (but we could use another lock)
309 Tokenizer mTokens;
310 DefaultKeyedVector<ClientID, Client*> mClientsMap;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700311 DefaultKeyedVector<SurfaceID, sp<LayerBaseClient> > mLayerMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 GraphicPlane mGraphicPlanes[1];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700313 bool mLayersRemoved;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 Vector<Client*> mDisconnectedClients;
315
316 // constant members (no synchronization needed for access)
317 sp<MemoryDealer> mServerHeap;
318 sp<IMemory> mServerCblkMemory;
319 surface_flinger_cblk_t* mServerCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 GLuint mWormholeTexName;
321 sp<BootAnimation> mBootAnimation;
322 nsecs_t mBootTime;
323
324 // Can only accessed from the main thread, these members
325 // don't need synchronization
326 Region mDirtyRegion;
327 Region mInvalidRegion;
328 Region mWormholeRegion;
329 Client* mLastScheduledBroadcast;
330 SortedVector<Client*> mScheduledBroadcasts;
331 bool mVisibleRegionsDirty;
332 bool mDeferReleaseConsole;
333 bool mFreezeDisplay;
334 int32_t mFreezeCount;
335 nsecs_t mFreezeDisplayTime;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 // don't use a lock for these, we don't care
338 int mDebugRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 int mDebugBackground;
340 int mDebugNoBootAnimation;
341
342 // these are thread safe
343 mutable Barrier mReadyToRunBarrier;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344
345 // atomic variables
346 enum {
347 eConsoleReleased = 1,
348 eConsoleAcquired = 2
349 };
350 volatile int32_t mConsoleSignals;
351
352 // only written in the main thread, only read in other threads
353 volatile int32_t mSecureFrameBuffer;
354};
355
356// ---------------------------------------------------------------------------
357
358class FreezeLock : public LightRefBase<FreezeLock> {
359 SurfaceFlinger* mFlinger;
360public:
361 FreezeLock(SurfaceFlinger* flinger)
362 : mFlinger(flinger) {
363 mFlinger->incFreezeCount();
364 }
365 ~FreezeLock() {
366 mFlinger->decFreezeCount();
367 }
368};
369
370// ---------------------------------------------------------------------------
371
372class BClient : public BnSurfaceFlingerClient
373{
374public:
375 BClient(SurfaceFlinger *flinger, ClientID cid,
376 const sp<IMemory>& cblk);
377 ~BClient();
378
379 // ISurfaceFlingerClient interface
380 virtual void getControlBlocks(sp<IMemory>* ctrl) const;
381
382 virtual sp<ISurface> createSurface(
383 surface_data_t* params, int pid,
384 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
385 uint32_t flags);
386
387 virtual status_t destroySurface(SurfaceID surfaceId);
388 virtual status_t setState(int32_t count, const layer_state_t* states);
389
390private:
391 ClientID mId;
392 SurfaceFlinger* mFlinger;
393 sp<IMemory> mCblk;
394};
395
396// ---------------------------------------------------------------------------
397}; // namespace android
398
399#endif // ANDROID_SURFACE_FLINGER_H