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.
18 #include "HWComposer_hwc1.h"
21 #ifndef ANDROID_SF_HWCOMPOSER_H
22 #define ANDROID_SF_HWCOMPOSER_H
27 #include <sys/types.h>
31 #include <utils/BitSet.h>
32 #include <utils/Condition.h>
33 #include <utils/Mutex.h>
34 #include <utils/StrongPointer.h>
35 #include <utils/Thread.h>
36 #include <utils/Timers.h>
37 #include <utils/Vector.h>
43 extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
44 const struct timespec *request,
45 struct timespec *remain);
47 struct framebuffer_device_t;
55 // ---------------------------------------------------------------------------
69 friend class HWComposer;
70 virtual void onVSyncReceived(
71 HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0;
72 virtual void onHotplugReceived(HWComposer* composer, int32_t disp, bool connected) = 0;
73 virtual void onInvalidateReceived(HWComposer* composer) = 0;
75 virtual ~EventHandler() {}
78 // useVrComposer is passed to the composer HAL. When true, the composer HAL
79 // will use the vr composer service, otherwise it uses the real hardware
81 HWComposer(bool useVrComposer);
85 void setEventHandler(EventHandler* handler);
87 bool hasCapability(HWC2::Capability capability) const;
89 // Attempts to allocate a virtual display. If the virtual display is created
90 // on the HWC device, outId will contain its HWC ID.
91 status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
92 android_pixel_format_t* format, int32_t* outId);
94 // Attempts to create a new layer on this display
95 std::shared_ptr<HWC2::Layer> createLayer(int32_t displayId);
97 // Asks the HAL what it can do
98 status_t prepare(DisplayDevice& displayDevice);
100 status_t setClientTarget(int32_t displayId, uint32_t slot,
101 const sp<Fence>& acquireFence,
102 const sp<GraphicBuffer>& target, android_dataspace_t dataspace);
104 // Present layers to the display and read releaseFences.
105 status_t presentAndGetReleaseFences(int32_t displayId);
108 status_t setPowerMode(int32_t displayId, int mode);
111 status_t setActiveConfig(int32_t displayId, size_t configId);
113 // Sets a color transform to be applied to the result of composition
114 status_t setColorTransform(int32_t displayId, const mat4& transform);
116 // reset state when an external, non-virtual display is disconnected
117 void disconnectDisplay(int32_t displayId);
119 // does this display have layers handled by HWC
120 bool hasDeviceComposition(int32_t displayId) const;
122 // does this display have layers handled by GLES
123 bool hasClientComposition(int32_t displayId) const;
125 // get the present fence received from the last call to present.
126 sp<Fence> getPresentFence(int32_t displayId) const;
128 // Get last release fence for the given layer
129 sp<Fence> getLayerReleaseFence(int32_t displayId,
130 const std::shared_ptr<HWC2::Layer>& layer) const;
132 // Set the output buffer and acquire fence for a virtual display.
133 // Returns INVALID_OPERATION if displayId is not a virtual display.
134 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
135 const sp<GraphicBuffer>& buf);
137 // After SurfaceFlinger has retrieved the release fences for all the frames,
138 // it can call this to clear the shared pointers in the release fence map
139 void clearReleaseFences(int32_t displayId);
141 // Returns the HDR capabilities of the given display
142 std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId);
144 // Events handling ---------------------------------------------------------
146 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
148 // Query display parameters. Pass in a display index (e.g.
149 // HWC_DISPLAY_PRIMARY).
150 nsecs_t getRefreshTimestamp(int32_t displayId) const;
151 bool isConnected(int32_t displayId) const;
153 // Non-const because it can update configMap inside of mDisplayData
154 std::vector<std::shared_ptr<const HWC2::Display::Config>>
155 getConfigs(int32_t displayId) const;
157 std::shared_ptr<const HWC2::Display::Config>
158 getActiveConfig(int32_t displayId) const;
160 std::vector<android_color_mode_t> getColorModes(int32_t displayId) const;
162 status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode);
164 bool isUsingVrComposer() const;
166 // for debugging ----------------------------------------------------------
167 void dump(String8& out) const;
169 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
171 static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2;
173 void loadHwcModule(bool useVrComposer);
175 bool isValidDisplay(int32_t displayId) const;
176 static void validateChange(HWC2::Composition from, HWC2::Composition to);
180 void invalidate(const std::shared_ptr<HWC2::Display>& display);
181 void vsync(const std::shared_ptr<HWC2::Display>& display,
183 void hotplug(const std::shared_ptr<HWC2::Display>& display,
184 HWC2::Connection connected);
191 bool hasClientComposition;
192 bool hasDeviceComposition;
193 std::shared_ptr<HWC2::Display> hwcDisplay;
194 HWC2::DisplayRequest displayRequests;
195 sp<Fence> lastPresentFence; // signals when the last set op retires
196 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>>
198 buffer_handle_t outbufHandle;
199 sp<Fence> outbufAcquireFence;
200 mutable std::unordered_map<int32_t,
201 std::shared_ptr<const HWC2::Display::Config>> configMap;
203 // protected by mVsyncLock
204 HWC2::Vsync vsyncEnabled;
206 bool validateWasSkipped;
207 HWC2::Error presentError;
210 std::unique_ptr<HWC2::Device> mHwcDevice;
211 std::vector<DisplayData> mDisplayData;
212 std::set<size_t> mFreeDisplaySlots;
213 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
214 // protect mDisplayData from races between prepare and dump
215 mutable Mutex mDisplayLock;
217 cb_context* mCBContext;
218 EventHandler* mEventHandler;
219 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
220 uint32_t mRemainingHwcVirtualDisplays;
222 // protected by mLock
224 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync;
227 mutable Mutex mVsyncLock;
230 // ---------------------------------------------------------------------------
231 }; // namespace android
233 #endif // ANDROID_SF_HWCOMPOSER_H
235 #endif // #ifdef USE_HWC2