HWC2.h revision 7d7ae7345abfb91be55c4aed6c9be9d09a144e4c
1/* 2 * Copyright 2015 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_HWC2_H 18#define ANDROID_SF_HWC2_H 19 20#define HWC2_INCLUDE_STRINGIFICATION 21#define HWC2_USE_CPP11 22#include <hardware/hwcomposer2.h> 23#undef HWC2_INCLUDE_STRINGIFICATION 24#undef HWC2_USE_CPP11 25 26#include <ui/HdrCapabilities.h> 27 28#include <utils/Log.h> 29#include <utils/StrongPointer.h> 30#include <utils/Timers.h> 31 32#include <functional> 33#include <string> 34#include <unordered_map> 35#include <vector> 36 37namespace android { 38 class Fence; 39 class FloatRect; 40 class GraphicBuffer; 41 class Rect; 42 class Region; 43} 44 45namespace HWC2 { 46 47class Display; 48class Layer; 49 50typedef std::function<void(std::shared_ptr<Display>, Connection)> 51 HotplugCallback; 52typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback; 53typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback; 54 55class Device 56{ 57public: 58 Device(hwc2_device_t* device); 59 ~Device(); 60 61 friend class HWC2::Display; 62 friend class HWC2::Layer; 63 64 // Required by HWC2 65 66 std::string dump() const; 67 68 const std::vector<Capability>& getCapabilities() const { 69 return mCapabilities; 70 }; 71 72 uint32_t getMaxVirtualDisplayCount() const; 73 Error createVirtualDisplay(uint32_t width, uint32_t height, 74 std::shared_ptr<Display>* outDisplay); 75 76 void registerHotplugCallback(HotplugCallback hotplug); 77 void registerRefreshCallback(RefreshCallback refresh); 78 void registerVsyncCallback(VsyncCallback vsync); 79 80 // For use by callbacks 81 82 void callHotplug(std::shared_ptr<Display> display, Connection connected); 83 void callRefresh(std::shared_ptr<Display> display); 84 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp); 85 86 // Other Device methods 87 88 // This will create a Display if one is not found, but it will not be marked 89 // as connected 90 std::shared_ptr<Display> getDisplayById(hwc2_display_t id); 91 92private: 93 // Initialization methods 94 95 template <typename PFN> 96 [[clang::warn_unused_result]] bool loadFunctionPointer( 97 FunctionDescriptor desc, PFN& outPFN) { 98 auto intDesc = static_cast<int32_t>(desc); 99 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc); 100 if (pfn != nullptr) { 101 outPFN = reinterpret_cast<PFN>(pfn); 102 return true; 103 } else { 104 ALOGE("Failed to load function %s", to_string(desc).c_str()); 105 return false; 106 } 107 } 108 109 template <typename PFN, typename HOOK> 110 void registerCallback(Callback callback, HOOK hook) { 111 static_assert(std::is_same<PFN, HOOK>::value, 112 "Incompatible function pointer"); 113 auto intCallback = static_cast<int32_t>(callback); 114 auto callbackData = static_cast<hwc2_callback_data_t>(this); 115 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook); 116 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn); 117 } 118 119 void loadCapabilities(); 120 void loadFunctionPointers(); 121 void registerCallbacks(); 122 123 // For use by Display 124 125 void destroyVirtualDisplay(hwc2_display_t display); 126 127 // Member variables 128 129 hwc2_device_t* mHwcDevice; 130 131 // Device function pointers 132 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay; 133 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay; 134 HWC2_PFN_DUMP mDump; 135 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount; 136 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback; 137 138 // Display function pointers 139 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges; 140 HWC2_PFN_CREATE_LAYER mCreateLayer; 141 HWC2_PFN_DESTROY_LAYER mDestroyLayer; 142 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig; 143 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes; 144 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute; 145 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs; 146 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName; 147 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests; 148 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType; 149 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport; 150 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities; 151 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences; 152 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay; 153 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig; 154 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget; 155 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer; 156 HWC2_PFN_SET_POWER_MODE mSetPowerMode; 157 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled; 158 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay; 159 160 // Layer function pointers 161 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition; 162 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer; 163 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage; 164 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode; 165 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor; 166 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType; 167 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame; 168 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha; 169 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream; 170 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop; 171 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform; 172 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion; 173 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder; 174 175 std::vector<Capability> mCapabilities; 176 std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays; 177 178 HotplugCallback mHotplug; 179 std::vector<std::pair<std::shared_ptr<Display>, Connection>> 180 mPendingHotplugs; 181 RefreshCallback mRefresh; 182 std::vector<std::shared_ptr<Display>> mPendingRefreshes; 183 VsyncCallback mVsync; 184 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs; 185}; 186 187class Display : public std::enable_shared_from_this<Display> 188{ 189public: 190 Display(Device& device, hwc2_display_t id); 191 ~Display(); 192 193 friend class HWC2::Device; 194 friend class HWC2::Layer; 195 196 class Config 197 { 198 public: 199 class Builder 200 { 201 public: 202 Builder(Display& display, hwc2_config_t id); 203 204 std::shared_ptr<const Config> build() { 205 return std::const_pointer_cast<const Config>( 206 std::move(mConfig)); 207 } 208 209 Builder& setWidth(int32_t width) { 210 mConfig->mWidth = width; 211 return *this; 212 } 213 Builder& setHeight(int32_t height) { 214 mConfig->mHeight = height; 215 return *this; 216 } 217 Builder& setVsyncPeriod(int32_t vsyncPeriod) { 218 mConfig->mVsyncPeriod = vsyncPeriod; 219 return *this; 220 } 221 Builder& setDpiX(int32_t dpiX) { 222 if (dpiX == -1) { 223 mConfig->mDpiX = getDefaultDensity(); 224 } else { 225 mConfig->mDpiX = dpiX / 1000.0f; 226 } 227 return *this; 228 } 229 Builder& setDpiY(int32_t dpiY) { 230 if (dpiY == -1) { 231 mConfig->mDpiY = getDefaultDensity(); 232 } else { 233 mConfig->mDpiY = dpiY / 1000.0f; 234 } 235 return *this; 236 } 237 238 private: 239 float getDefaultDensity(); 240 std::shared_ptr<Config> mConfig; 241 }; 242 243 hwc2_display_t getDisplayId() const { return mDisplay.getId(); } 244 hwc2_config_t getId() const { return mId; } 245 246 int32_t getWidth() const { return mWidth; } 247 int32_t getHeight() const { return mHeight; } 248 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; } 249 float getDpiX() const { return mDpiX; } 250 float getDpiY() const { return mDpiY; } 251 252 private: 253 Config(Display& display, hwc2_config_t id); 254 255 Display& mDisplay; 256 hwc2_config_t mId; 257 258 int32_t mWidth; 259 int32_t mHeight; 260 nsecs_t mVsyncPeriod; 261 float mDpiX; 262 float mDpiY; 263 }; 264 265 // Required by HWC2 266 267 [[clang::warn_unused_result]] Error acceptChanges(); 268 [[clang::warn_unused_result]] Error createLayer( 269 std::shared_ptr<Layer>* outLayer); 270 [[clang::warn_unused_result]] Error getActiveConfig( 271 std::shared_ptr<const Config>* outConfig) const; 272 [[clang::warn_unused_result]] Error getChangedCompositionTypes( 273 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes); 274 275 // Doesn't call into the HWC2 device, so no errors are possible 276 std::vector<std::shared_ptr<const Config>> getConfigs() const; 277 278 [[clang::warn_unused_result]] Error getName(std::string* outName) const; 279 [[clang::warn_unused_result]] Error getRequests( 280 DisplayRequest* outDisplayRequests, 281 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>* 282 outLayerRequests); 283 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const; 284 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const; 285 [[clang::warn_unused_result]] Error getHdrCapabilities( 286 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const; 287 [[clang::warn_unused_result]] Error getReleaseFences( 288 std::unordered_map<std::shared_ptr<Layer>, 289 android::sp<android::Fence>>* outFences) const; 290 [[clang::warn_unused_result]] Error present( 291 android::sp<android::Fence>* outRetireFence); 292 [[clang::warn_unused_result]] Error setActiveConfig( 293 const std::shared_ptr<const Config>& config); 294 [[clang::warn_unused_result]] Error setClientTarget( 295 buffer_handle_t target, 296 const android::sp<android::Fence>& acquireFence, 297 android_dataspace_t dataspace); 298 [[clang::warn_unused_result]] Error setOutputBuffer( 299 const android::sp<android::GraphicBuffer>& buffer, 300 const android::sp<android::Fence>& releaseFence); 301 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode); 302 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled); 303 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes, 304 uint32_t* outNumRequests); 305 306 // Other Display methods 307 308 Device& getDevice() const { return mDevice; } 309 hwc2_display_t getId() const { return mId; } 310 bool isConnected() const { return mIsConnected; } 311 312private: 313 // For use by Device 314 315 // Virtual displays are always connected 316 void setVirtual() { 317 mIsVirtual = true; 318 mIsConnected = true; 319 } 320 321 void setConnected(bool connected) { mIsConnected = connected; } 322 int32_t getAttribute(hwc2_config_t configId, Attribute attribute); 323 void loadConfig(hwc2_config_t configId); 324 void loadConfigs(); 325 326 // For use by Layer 327 void destroyLayer(hwc2_layer_t layerId); 328 329 // This may fail (and return a null pointer) if no layer with this ID exists 330 // on this display 331 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const; 332 333 // Member variables 334 335 Device& mDevice; 336 hwc2_display_t mId; 337 bool mIsConnected; 338 bool mIsVirtual; 339 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers; 340 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs; 341}; 342 343class Layer 344{ 345public: 346 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id); 347 ~Layer(); 348 349 bool isAbandoned() const { return mDisplay.expired(); } 350 hwc2_layer_t getId() const { return mId; } 351 352 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y); 353 [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer, 354 const android::sp<android::Fence>& acquireFence); 355 [[clang::warn_unused_result]] Error setSurfaceDamage( 356 const android::Region& damage); 357 358 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode); 359 [[clang::warn_unused_result]] Error setColor(hwc_color_t color); 360 [[clang::warn_unused_result]] Error setCompositionType(Composition type); 361 [[clang::warn_unused_result]] Error setDisplayFrame( 362 const android::Rect& frame); 363 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha); 364 [[clang::warn_unused_result]] Error setSidebandStream( 365 const native_handle_t* stream); 366 [[clang::warn_unused_result]] Error setSourceCrop( 367 const android::FloatRect& crop); 368 [[clang::warn_unused_result]] Error setTransform(Transform transform); 369 [[clang::warn_unused_result]] Error setVisibleRegion( 370 const android::Region& region); 371 [[clang::warn_unused_result]] Error setZOrder(uint32_t z); 372 373private: 374 std::weak_ptr<Display> mDisplay; 375 hwc2_display_t mDisplayId; 376 Device& mDevice; 377 hwc2_layer_t mId; 378}; 379 380} // namespace HWC2 381 382#endif // ANDROID_SF_HWC2_H 383