blob: a94bc1e821e03fb876cb8f3d7cc2b9fd01607415 [file] [log] [blame]
Fabien Sanglard9d96de42016-10-11 00:15:18 +00001/*
2 * Copyright (C) 2010 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_SF_HWCOMPOSER_HWC1_H
18#define ANDROID_SF_HWCOMPOSER_HWC1_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <hardware/hwcomposer_defs.h>
24
25#include <system/graphics.h>
26
27#include <ui/Fence.h>
28
29#include <utils/BitSet.h>
30#include <utils/Condition.h>
31#include <utils/Mutex.h>
32#include <utils/StrongPointer.h>
33#include <utils/Thread.h>
34#include <utils/Timers.h>
35#include <utils/Vector.h>
36
37extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
38 const struct timespec *request,
39 struct timespec *remain);
40
41struct hwc_composer_device_1;
42struct hwc_display_contents_1;
43struct hwc_layer_1;
44struct hwc_procs;
45struct framebuffer_device_t;
46
47namespace android {
48// ---------------------------------------------------------------------------
49
50class Fence;
Fabien Sanglard9d96de42016-10-11 00:15:18 +000051class GraphicBuffer;
52class NativeHandle;
53class Region;
54class String8;
55class SurfaceFlinger;
Dan Stoza71bded52016-10-19 11:10:33 -070056namespace gfx {
57 class FloatRect;
58}
Fabien Sanglard9d96de42016-10-11 00:15:18 +000059
60class HWComposer
61{
62public:
63 class EventHandler {
64 friend class HWComposer;
Steven Thomas3cfac282017-02-06 12:29:30 -080065 virtual void onVSyncReceived(
66 HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +000067 virtual void onHotplugReceived(int disp, bool connected) = 0;
Steven Thomas3cfac282017-02-06 12:29:30 -080068 virtual void onInvalidateReceived(HWComposer* composer) = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +000069 protected:
70 virtual ~EventHandler() {}
71 };
72
73 enum {
74 NUM_BUILTIN_DISPLAYS = HWC_NUM_PHYSICAL_DISPLAY_TYPES,
75 MAX_HWC_DISPLAYS = HWC_NUM_DISPLAY_TYPES,
76 VIRTUAL_DISPLAY_ID_BASE = HWC_DISPLAY_VIRTUAL,
77 };
78
79 HWComposer(
80 const sp<SurfaceFlinger>& flinger,
81 EventHandler& handler);
82
83 ~HWComposer();
84
85 status_t initCheck() const;
86
87 // Returns a display ID starting at VIRTUAL_DISPLAY_ID_BASE, this ID is to
88 // be used with createWorkList (and all other methods requiring an ID
89 // below).
90 // IDs below NUM_BUILTIN_DISPLAYS are pre-defined and therefore are
91 // always valid.
92 // Returns -1 if an ID cannot be allocated
93 int32_t allocateDisplayId();
94
95 // Recycles the given virtual display ID and frees the associated worklist.
96 // IDs below NUM_BUILTIN_DISPLAYS are not recycled.
97 status_t freeDisplayId(int32_t id);
98
99
100 // Asks the HAL what it can do
101 status_t prepare();
102
103 // commits the list
104 status_t commit();
105
106 // set power mode
107 status_t setPowerMode(int disp, int mode);
108
109 // set active config
110 status_t setActiveConfig(int disp, int mode);
111
112 // reset state when an external, non-virtual display is disconnected
113 void disconnectDisplay(int disp);
114
115 // create a work list for numLayers layer. sets HWC_GEOMETRY_CHANGED.
116 status_t createWorkList(int32_t id, size_t numLayers);
117
118 bool supportsFramebufferTarget() const;
119
120 // does this display have layers handled by HWC
121 bool hasHwcComposition(int32_t id) const;
122
123 // does this display have layers handled by GLES
124 bool hasGlesComposition(int32_t id) const;
125
126 // get the releaseFence file descriptor for a display's framebuffer layer.
127 // the release fence is only valid after commit()
128 sp<Fence> getAndResetReleaseFence(int32_t id);
129
130 // needed forward declarations
131 class LayerListIterator;
132
133 // return the visual id to be used to find a suitable EGLConfig for
134 // *ALL* displays.
135 int getVisualID() const;
136
137 // Forwarding to FB HAL for pre-HWC-1.1 code (see FramebufferSurface).
138 int fbPost(int32_t id, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
139 int fbCompositionComplete();
140 void fbDump(String8& result);
141
142 // Set the output buffer and acquire fence for a virtual display.
143 // Returns INVALID_OPERATION if id is not a virtual display.
144 status_t setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
145 const sp<GraphicBuffer>& buf);
146
147 // Get the retire fence for the last committed frame. This fence will
148 // signal when the h/w composer is completely finished with the frame.
149 // For physical displays, it is no longer being displayed. For virtual
150 // displays, writes to the output buffer are complete.
151 sp<Fence> getLastRetireFence(int32_t id) const;
152
153 status_t setCursorPositionAsync(int32_t id, const Rect &pos);
154
155 /*
156 * Interface to hardware composer's layers functionality.
157 * This abstracts the HAL interface to layers which can evolve in
158 * incompatible ways from one release to another.
159 * The idea is that we could extend this interface as we add
160 * features to h/w composer.
161 */
162 class HWCLayerInterface {
163 protected:
164 virtual ~HWCLayerInterface() { }
165 public:
166 virtual int32_t getCompositionType() const = 0;
167 virtual uint32_t getHints() const = 0;
168 virtual sp<Fence> getAndResetReleaseFence() = 0;
169 virtual void setDefaultState() = 0;
170 virtual void setSkip(bool skip) = 0;
171 virtual void setIsCursorLayerHint(bool isCursor = true) = 0;
172 virtual void setBlending(uint32_t blending) = 0;
173 virtual void setTransform(uint32_t transform) = 0;
174 virtual void setFrame(const Rect& frame) = 0;
Dan Stoza71bded52016-10-19 11:10:33 -0700175 virtual void setCrop(const gfx::FloatRect& crop) = 0;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000176 virtual void setVisibleRegionScreen(const Region& reg) = 0;
177 virtual void setSurfaceDamage(const Region& reg) = 0;
178 virtual void setSidebandStream(const sp<NativeHandle>& stream) = 0;
179 virtual void setBuffer(const sp<GraphicBuffer>& buffer) = 0;
180 virtual void setAcquireFenceFd(int fenceFd) = 0;
181 virtual void setPlaneAlpha(uint8_t alpha) = 0;
182 virtual void onDisplayed() = 0;
183 };
184
185 /*
186 * Interface used to implement an iterator to a list
187 * of HWCLayer.
188 */
189 class HWCLayer : public HWCLayerInterface {
190 friend class LayerListIterator;
191 // select the layer at the given index
192 virtual status_t setLayer(size_t index) = 0;
193 virtual HWCLayer* dup() = 0;
194 static HWCLayer* copy(HWCLayer *rhs) {
195 return rhs ? rhs->dup() : NULL;
196 }
197 protected:
198 virtual ~HWCLayer() { }
199 };
200
201 /*
202 * Iterator through a HWCLayer list.
203 * This behaves more or less like a forward iterator.
204 */
205 class LayerListIterator {
206 friend class HWComposer;
207 HWCLayer* const mLayerList;
208 size_t mIndex;
209
210 LayerListIterator() : mLayerList(NULL), mIndex(0) { }
211
212 LayerListIterator(HWCLayer* layer, size_t index)
213 : mLayerList(layer), mIndex(index) { }
214
215 // we don't allow assignment, because we don't need it for now
216 LayerListIterator& operator = (const LayerListIterator& rhs);
217
218 public:
219 // copy operators
220 LayerListIterator(const LayerListIterator& rhs)
221 : mLayerList(HWCLayer::copy(rhs.mLayerList)), mIndex(rhs.mIndex) {
222 }
223
224 ~LayerListIterator() { delete mLayerList; }
225
226 // pre-increment
227 LayerListIterator& operator++() {
228 mLayerList->setLayer(++mIndex);
229 return *this;
230 }
231
232 // dereference
233 HWCLayerInterface& operator * () { return *mLayerList; }
234 HWCLayerInterface* operator -> () { return mLayerList; }
235
236 // comparison
237 bool operator == (const LayerListIterator& rhs) const {
238 return mIndex == rhs.mIndex;
239 }
240 bool operator != (const LayerListIterator& rhs) const {
241 return !operator==(rhs);
242 }
243 };
244
245 // Returns an iterator to the beginning of the layer list
246 LayerListIterator begin(int32_t id);
247
248 // Returns an iterator to the end of the layer list
249 LayerListIterator end(int32_t id);
250
251
252 // Events handling ---------------------------------------------------------
253
254 enum {
255 EVENT_VSYNC = HWC_EVENT_VSYNC
256 };
257
258 void eventControl(int disp, int event, int enabled);
259
260 struct DisplayConfig {
261 uint32_t width;
262 uint32_t height;
263 float xdpi;
264 float ydpi;
265 nsecs_t refresh;
266 android_color_mode_t colorMode;
267 bool operator==(const DisplayConfig& rhs) const {
268 return width == rhs.width &&
269 height == rhs.height &&
270 xdpi == rhs.xdpi &&
271 ydpi == rhs.ydpi &&
272 refresh == rhs.refresh &&
273 colorMode == rhs.colorMode;
274 }
275 };
276
277 // Query display parameters. Pass in a display index (e.g.
278 // HWC_DISPLAY_PRIMARY).
279 nsecs_t getRefreshTimestamp(int disp) const;
280 sp<Fence> getDisplayFence(int disp) const;
281 uint32_t getFormat(int disp) const;
282 bool isConnected(int disp) const;
283
284 // These return the values for the current config of a given display index.
285 // To get the values for all configs, use getConfigs below.
286 uint32_t getWidth(int disp) const;
287 uint32_t getHeight(int disp) const;
288 float getDpiX(int disp) const;
289 float getDpiY(int disp) const;
290 nsecs_t getRefreshPeriod(int disp) const;
291 android_color_mode_t getColorMode(int disp) const;
292
293 const Vector<DisplayConfig>& getConfigs(int disp) const;
294 size_t getCurrentConfig(int disp) const;
295
296 status_t setVirtualDisplayProperties(int32_t id, uint32_t w, uint32_t h,
297 uint32_t format);
298
299 // this class is only used to fake the VSync event on systems that don't
300 // have it.
301 class VSyncThread : public Thread {
302 HWComposer& mHwc;
303 mutable Mutex mLock;
304 Condition mCondition;
305 bool mEnabled;
306 mutable nsecs_t mNextFakeVSync;
307 nsecs_t mRefreshPeriod;
308 virtual void onFirstRef();
309 virtual bool threadLoop();
310 public:
311 VSyncThread(HWComposer& hwc);
312 void setEnabled(bool enabled);
313 };
314
315 friend class VSyncThread;
316
317 // for debugging ----------------------------------------------------------
318 void dump(String8& out) const;
319
320private:
321 void loadHwcModule();
322 int loadFbHalModule();
323
324 LayerListIterator getLayerIterator(int32_t id, size_t index);
325
326 struct cb_context;
327
328 static void hook_invalidate(const struct hwc_procs* procs);
329 static void hook_vsync(const struct hwc_procs* procs, int disp,
330 int64_t timestamp);
331 static void hook_hotplug(const struct hwc_procs* procs, int disp,
332 int connected);
333
334 inline void invalidate();
335 inline void vsync(int disp, int64_t timestamp);
336 inline void hotplug(int disp, int connected);
337
338 status_t queryDisplayProperties(int disp);
339
340 status_t setFramebufferTarget(int32_t id,
341 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf);
342
343 struct DisplayData {
344 DisplayData();
345 ~DisplayData();
346 Vector<DisplayConfig> configs;
347 size_t currentConfig;
348 uint32_t format; // pixel format from FB hal, for pre-hwc-1.1
349 bool connected;
350 bool hasFbComp;
351 bool hasOvComp;
352 size_t capacity;
353 hwc_display_contents_1* list;
354 hwc_layer_1* framebufferTarget;
355 buffer_handle_t fbTargetHandle;
356 sp<Fence> lastRetireFence; // signals when the last set op retires
357 sp<Fence> lastDisplayFence; // signals when the last set op takes
358 // effect on screen
359 buffer_handle_t outbufHandle;
360 sp<Fence> outbufAcquireFence;
361
362 // protected by mEventControlLock
363 int32_t events;
364
365 // We need to hold "copies" of these for memory management purposes. The
366 // actual hwc_layer_1_t holds pointers to the memory within. Vector<>
367 // internally doesn't copy the memory unless one of the copies is
368 // modified.
369 Vector<Region> visibleRegions;
370 Vector<Region> surfaceDamageRegions;
371 };
372
373 sp<SurfaceFlinger> mFlinger;
374 framebuffer_device_t* mFbDev;
375 struct hwc_composer_device_1* mHwc;
376 // invariant: mLists[0] != NULL iff mHwc != NULL
377 // mLists[i>0] can be NULL. that display is to be ignored
378 struct hwc_display_contents_1* mLists[MAX_HWC_DISPLAYS];
379 DisplayData mDisplayData[MAX_HWC_DISPLAYS];
380 // protect mDisplayData from races between prepare and dump
381 mutable Mutex mDisplayLock;
382 size_t mNumDisplays;
383
384 cb_context* mCBContext;
385 EventHandler& mEventHandler;
386 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
387 sp<VSyncThread> mVSyncThread;
388 bool mDebugForceFakeVSync;
389 BitSet32 mAllocatedDisplayIDs;
390
391 // protected by mLock
392 mutable Mutex mLock;
393 mutable nsecs_t mLastHwVSync[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
394
395 // thread-safe
396 mutable Mutex mEventControlLock;
397};
398
399// ---------------------------------------------------------------------------
400}; // namespace android
401
402#endif // ANDROID_SF_HWCOMPOSER_H