2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_SF_HWCOMPOSER_H
18 #define ANDROID_SF_HWCOMPOSER_H
21 #include <sys/types.h>
23 #include <hardware/hwcomposer_defs.h>
27 #include <utils/BitSet.h>
28 #include <utils/Condition.h>
29 #include <utils/Mutex.h>
30 #include <utils/StrongPointer.h>
31 #include <utils/Thread.h>
32 #include <utils/Timers.h>
33 #include <utils/Vector.h>
35 extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
36 const struct timespec *request,
37 struct timespec *remain);
39 struct hwc_composer_device_1;
40 struct hwc_display_contents_1;
43 struct framebuffer_device_t;
46 // ---------------------------------------------------------------------------
58 friend class HWComposer;
59 virtual void onVSyncReceived(int disp, nsecs_t timestamp) = 0;
60 virtual void onHotplugReceived(int disp, bool connected) = 0;
62 virtual ~EventHandler() {}
66 MAX_DISPLAYS = HWC_NUM_DISPLAY_TYPES + 1
70 const sp<SurfaceFlinger>& flinger,
71 EventHandler& handler);
75 status_t initCheck() const;
77 // returns a display ID starting at MAX_DISPLAYS, this ID
78 // is to be used with createWorkList (and all other
79 // methods requiring an ID below).
80 // IDs below MAX_DISPLAY are pre-defined and therefore are always valid.
81 // returns a negative error code if an ID cannot be allocated
82 int32_t allocateDisplayId();
84 // recycles the given ID and frees the associated worklist.
85 // IDs below MAX_DISPLAYS are not recycled
86 status_t freeDisplayId(int32_t id);
89 // Asks the HAL what it can do
95 // release hardware resources and blank screen
96 status_t release(int disp);
98 // acquire hardware resources and unblank screen
99 status_t acquire(int disp);
101 // reset state when an external, non-virtual display is disconnected
102 void disconnectDisplay(int disp);
104 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
105 status_t createWorkList(int32_t id, size_t numLayers);
107 bool supportsFramebufferTarget() const;
109 // does this display have layers handled by HWC
110 bool hasHwcComposition(int32_t id) const;
112 // does this display have layers handled by GLES
113 bool hasGlesComposition(int32_t id) const;
115 // get the releaseFence file descriptor for a display's framebuffer layer.
116 // the release fence is only valid after commit()
117 sp<Fence> getAndResetReleaseFence(int32_t id);
119 // needed forward declarations
120 class LayerListIterator;
122 // return the visual id to be used to find a suitable EGLConfig for
124 int getVisualID() const;
126 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
127 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
128 int fbCompositionComplete();
129 void fbDump(String8& result);
131 // Set the output buffer and acquire fence for a virtual display.
132 // Returns INVALID_OPERATION if id is not a virtual display.
133 status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
134 const sp<GraphicBuffer>& buf);
136 // Get the retire fence for the last committed frame. This fence will
137 // signal when the h/w composer is completely finished with the frame.
138 // For physical displays, it is no longer being displayed. For virtual
139 // displays, writes to the output buffer are complete.
140 sp<Fence> getLastRetireFence(int32_t id);
143 * Interface to hardware composer's layers functionality.
144 * This abstracts the HAL interface to layers which can evolve in
145 * incompatible ways from one release to another.
146 * The idea is that we could extend this interface as we add
147 * features to h/w composer.
149 class HWCLayerInterface {
151 virtual ~HWCLayerInterface() { }
153 virtual int32_t getCompositionType() const = 0;
154 virtual uint32_t getHints() const = 0;
155 virtual sp<Fence> getAndResetReleaseFence() = 0;
156 virtual void setDefaultState() = 0;
157 virtual void setSkip(bool skip) = 0;
158 virtual void setBlending(uint32_t blending) = 0;
159 virtual void setTransform(uint32_t transform) = 0;
160 virtual void setFrame(const Rect& frame) = 0;
161 virtual void setCrop(const Rect& crop) = 0;
162 virtual void setVisibleRegionScreen(const Region& reg) = 0;
163 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
164 virtual void setAcquireFenceFd(int fenceFd) = 0;
165 virtual void setPlaneAlpha(uint8_t alpha) = 0;
166 virtual void onDisplayed() = 0;
170 * Interface used to implement an iterator to a list
173 class HWCLayer : public HWCLayerInterface {
174 friend class LayerListIterator;
175 // select the layer at the given index
176 virtual status_t setLayer(size_t index) = 0;
177 virtual HWCLayer* dup() = 0;
178 static HWCLayer* copy(HWCLayer *rhs) {
179 return rhs ? rhs->dup() : NULL;
182 virtual ~HWCLayer() { }
186 * Iterator through a HWCLayer list.
187 * This behaves more or less like a forward iterator.
189 class LayerListIterator {
190 friend struct HWComposer;
191 HWCLayer* const mLayerList;
194 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
196 LayerListIterator(HWCLayer* layer, size_t index)
197 : mLayerList(layer), mIndex(index) { }
199 // we don't allow assignment, because we don't need it for now
200 LayerListIterator& operator = (const LayerListIterator& rhs);
204 LayerListIterator(const LayerListIterator& rhs)
205 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
208 ~LayerListIterator() { delete mLayerList; }
211 LayerListIterator& operator++() {
212 mLayerList->setLayer(++mIndex);
217 HWCLayerInterface& operator * () { return *mLayerList; }
218 HWCLayerInterface* operator -> () { return mLayerList; }
221 bool operator == (const LayerListIterator& rhs) const {
222 return mIndex == rhs.mIndex;
224 bool operator != (const LayerListIterator& rhs) const {
225 return !operator==(rhs);
229 // Returns an iterator to the beginning of the layer list
230 LayerListIterator begin(int32_t id);
232 // Returns an iterator to the end of the layer list
233 LayerListIterator end(int32_t id);
236 // Events handling ---------------------------------------------------------
239 EVENT_VSYNC = HWC_EVENT_VSYNC
242 void eventControl(int disp, int event, int enabled);
244 // Query display parameters. Pass in a display index (e.g.
245 // HWC_DISPLAY_PRIMARY).
246 nsecs_t getRefreshPeriod(int disp) const;
247 nsecs_t getRefreshTimestamp(int disp) const;
248 sp<Fence> getDisplayFence(int disp) const;
249 uint32_t getWidth(int disp) const;
250 uint32_t getHeight(int disp) const;
251 uint32_t getFormat(int disp) const;
252 float getDpiX(int disp) const;
253 float getDpiY(int disp) const;
254 bool isConnected(int disp) const;
256 status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
259 // this class is only used to fake the VSync event on systems that don't
261 class VSyncThread : public Thread {
264 Condition mCondition;
266 mutable nsecs_t mNextFakeVSync;
267 nsecs_t mRefreshPeriod;
268 virtual void onFirstRef();
269 virtual bool threadLoop();
271 VSyncThread(HWComposer& hwc);
272 void setEnabled(bool enabled);
275 friend class VSyncThread;
277 // for debugging ----------------------------------------------------------
278 void dump(String8& out, char* scratch, size_t SIZE) const;
281 void loadHwcModule();
282 int loadFbHalModule();
284 LayerListIterator getLayerIterator(int32_t id, size_t index);
288 static void hook_invalidate(const struct hwc_procs* procs);
289 static void hook_vsync(const struct hwc_procs* procs, int disp,
291 static void hook_hotplug(const struct hwc_procs* procs, int disp,
294 inline void invalidate();
295 inline void vsync(int disp, int64_t timestamp);
296 inline void hotplug(int disp, int connected);
298 status_t queryDisplayProperties(int disp);
300 status_t setFramebufferTarget(int32_t id,
301 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
309 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
317 hwc_display_contents_1* list;
318 hwc_layer_1* framebufferTarget;
319 buffer_handle_t fbTargetHandle;
320 sp<Fence> lastRetireFence; // signals when the last set op retires
321 sp<Fence> lastDisplayFence; // signals when the last set op takes
323 buffer_handle_t outbufHandle;
324 sp<Fence> outbufAcquireFence;
326 // protected by mEventControlLock
330 sp<SurfaceFlinger> mFlinger;
331 framebuffer_device_t* mFbDev;
332 struct hwc_composer_device_1* mHwc;
333 // invariant: mLists[0] != NULL iff mHwc != NULL
334 // mLists[i>0] can be NULL. that display is to be ignored
335 struct hwc_display_contents_1* mLists[MAX_DISPLAYS];
336 DisplayData mDisplayData[MAX_DISPLAYS];
339 cb_context* mCBContext;
340 EventHandler& mEventHandler;
342 sp<VSyncThread> mVSyncThread;
343 bool mDebugForceFakeVSync;
344 BitSet32 mAllocatedDisplayIDs;
346 // protected by mLock
348 mutable nsecs_t mLastHwVSync;
351 mutable Mutex mEventControlLock;
354 // ---------------------------------------------------------------------------
355 }; // namespace android
357 #endif // ANDROID_SF_HWCOMPOSER_H