1 /* 2 * Copyright 2018 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_HW_EMU_HWC2_H 18 #define ANDROID_HW_EMU_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 #include <utils/Thread.h> 26 27 #include <atomic> 28 #include <map> 29 #include <mutex> 30 #include <vector> 31 #include <unordered_set> 32 #include <unordered_map> 33 #include <set> 34 35 #include "gralloc_cb.h" 36 #include "MiniFence.h" 37 #include "HostConnection.h" 38 39 namespace android { 40 41 class EmuHWC2 : public hwc2_device_t { 42 public: 43 EmuHWC2(); 44 int populatePrimary(); 45 46 private: getHWC2(hwc2_device_t * device)47 static inline EmuHWC2* getHWC2(hwc2_device_t* device) { 48 return static_cast<EmuHWC2*>(device); 49 } 50 closeHook(hw_device_t * device)51 static int closeHook(hw_device_t* device) { 52 EmuHWC2 *ctx = reinterpret_cast<EmuHWC2*>(device); 53 delete ctx; 54 return 0; 55 } 56 57 // getCapabilities 58 void doGetCapabilities(uint32_t* outCount, int32_t* outCapabilities); getCapabilitiesHook(hwc2_device_t * device,uint32_t * outCount,int32_t * outCapabilities)59 static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount, 60 int32_t* outCapabilities) { 61 getHWC2(device)->doGetCapabilities(outCount, outCapabilities); 62 } 63 64 // getFunction 65 hwc2_function_pointer_t doGetFunction(HWC2::FunctionDescriptor descriptor); getFunctionHook(hwc2_device_t * device,int32_t desc)66 static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device, 67 int32_t desc) { 68 auto descriptor = static_cast<HWC2::FunctionDescriptor>(desc); 69 return getHWC2(device)->doGetFunction(descriptor); 70 } 71 72 // Device functions 73 HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height, 74 int32_t* format, hwc2_display_t* outDisplay); createVirtualDisplayHook(hwc2_device_t * device,uint32_t width,uint32_t height,int32_t * format,hwc2_display_t * outDisplay)75 static int32_t createVirtualDisplayHook(hwc2_device_t* device, 76 uint32_t width, uint32_t height, int32_t* format, 77 hwc2_display_t* outDisplay) { 78 auto error = getHWC2(device)->createVirtualDisplay(width, height, 79 format, outDisplay); 80 return static_cast<int32_t>(error); 81 } 82 83 HWC2::Error destroyVirtualDisplay(hwc2_display_t display); destroyVirtualDisplayHook(hwc2_device_t * device,hwc2_display_t display)84 static int32_t destroyVirtualDisplayHook(hwc2_device_t* device, 85 hwc2_display_t display) { 86 auto error = getHWC2(device)->destroyVirtualDisplay(display); 87 return static_cast<int32_t>(error); 88 } 89 90 std::string mDumpString; 91 void dump(uint32_t* outSize, char* outBuffer); dumpHook(hwc2_device_t * device,uint32_t * outSize,char * outBuffer)92 static void dumpHook(hwc2_device_t* device, uint32_t* outSize, 93 char* outBuffer) { 94 getHWC2(device)->dump(outSize, outBuffer); 95 } 96 97 uint32_t getMaxVirtualDisplayCount(); getMaxVirtualDisplayCountHook(hwc2_device_t * device)98 static uint32_t getMaxVirtualDisplayCountHook(hwc2_device_t* device) { 99 return getHWC2(device)->getMaxVirtualDisplayCount(); 100 } 101 102 HWC2::Error registerCallback(HWC2::Callback descriptor, 103 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer); registerCallbackHook(hwc2_device_t * device,int32_t intDesc,hwc2_callback_data_t callbackData,hwc2_function_pointer_t pointer)104 static int32_t registerCallbackHook(hwc2_device_t* device, 105 int32_t intDesc, hwc2_callback_data_t callbackData, 106 hwc2_function_pointer_t pointer) { 107 auto descriptor = static_cast<HWC2::Callback>(intDesc); 108 auto error = getHWC2(device)->registerCallback(descriptor, 109 callbackData, pointer); 110 return static_cast<int32_t>(error); 111 } 112 113 class Layer; 114 class SortLayersByZ { 115 public: 116 bool operator()(const std::shared_ptr<Layer>& lhs, 117 const std::shared_ptr<Layer>& rhs) const; 118 }; 119 120 // SurfaceFlinger sets the ColorBuffer and its Fence handler for each 121 // layer. This class is a container for these two. 122 class FencedBuffer { 123 public: FencedBuffer()124 FencedBuffer() : mBuffer(nullptr), mFence(MiniFence::NO_FENCE) {} 125 setBuffer(buffer_handle_t buffer)126 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; } setFence(int fenceFd)127 void setFence(int fenceFd) { mFence = new MiniFence(fenceFd); } 128 getBuffer()129 buffer_handle_t getBuffer() const { return mBuffer; } getFence()130 int getFence() const { return mFence->dup(); } 131 132 private: 133 buffer_handle_t mBuffer; 134 sp<MiniFence> mFence; 135 }; 136 137 class GrallocModule { 138 public: 139 GrallocModule(); 140 ~GrallocModule(); getFb()141 framebuffer_device_t* getFb() { return mFbDev; } 142 uint32_t getTargetCb(); 143 private: 144 const hw_module_t* mHw = nullptr; 145 const gralloc_module_t* mGralloc = nullptr; 146 alloc_device_t* mAllocDev = nullptr; 147 framebuffer_device_t* mFbDev = nullptr; 148 buffer_handle_t mHandle = nullptr; 149 }; 150 151 typedef struct compose_layer { 152 uint32_t cbHandle; 153 hwc2_composition_t composeMode; 154 hwc_rect_t displayFrame; 155 hwc_frect_t crop; 156 int32_t blendMode; 157 float alpha; 158 hwc_color_t color; 159 hwc_transform_t transform; 160 } ComposeLayer; 161 typedef struct compose_device { 162 uint32_t version; 163 uint32_t targetHandle; 164 uint32_t numLayers; 165 struct compose_layer layer[0]; 166 } ComposeDevice; 167 168 class ComposeMsg { 169 public: 170 ComposeMsg(uint32_t layerCnt = 0) : 171 mData(sizeof(ComposeDevice) + layerCnt * sizeof(ComposeLayer)) 172 { 173 mComposeDevice = reinterpret_cast<ComposeDevice*>(mData.data()); 174 mLayerCnt = layerCnt; 175 } 176 get()177 ComposeDevice* get() { return mComposeDevice; } 178 getLayerCnt()179 uint32_t getLayerCnt() { return mLayerCnt; } 180 181 private: 182 std::vector<uint8_t> mData; 183 uint32_t mLayerCnt; 184 ComposeDevice* mComposeDevice; 185 }; 186 187 class Display { 188 public: 189 Display(EmuHWC2& device, HWC2::DisplayType type); getId()190 hwc2_display_t getId() const {return mId;} 191 192 // HWC2 Display functions 193 HWC2::Error acceptChanges(); 194 HWC2::Error createLayer(hwc2_layer_t* outLayerId); 195 HWC2::Error destroyLayer(hwc2_layer_t layerId); 196 HWC2::Error getActiveConfig(hwc2_config_t* outConfigId); 197 HWC2::Error getDisplayAttribute(hwc2_config_t configId, 198 int32_t attribute, int32_t* outValue); 199 HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements, 200 hwc2_layer_t* outLayers, int32_t* outTypes); 201 HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes); 202 HWC2::Error getConfigs(uint32_t* outNumConfigs, 203 hwc2_config_t* outConfigIds); 204 HWC2::Error getDozeSupport(int32_t* outSupport); 205 HWC2::Error getHdrCapabilities(uint32_t* outNumTypes, 206 int32_t* outTypes, float* outMaxLuminance, 207 float* outMaxAverageLuminance, float* outMinLuminance); 208 HWC2::Error getName(uint32_t* outSize, char* outName); 209 HWC2::Error getReleaseFences(uint32_t* outNumElements, 210 hwc2_layer_t* outLayers, int32_t* outFences); 211 HWC2::Error getRequests(int32_t* outDisplayRequests, 212 uint32_t* outNumElements, hwc2_layer_t* outLayers, 213 int32_t* outLayerRequests); 214 HWC2::Error getType(int32_t* outType); 215 HWC2::Error present(int32_t* outRetireFence); 216 HWC2::Error setActiveConfig(hwc2_config_t configId); 217 HWC2::Error setClientTarget(buffer_handle_t target, 218 int32_t acquireFence, int32_t dataspace, 219 hwc_region_t damage); 220 HWC2::Error setColorMode(int32_t mode); 221 HWC2::Error setColorTransform(const float* matrix, 222 int32_t hint); 223 HWC2::Error setOutputBuffer(buffer_handle_t buffer, 224 int32_t releaseFence); 225 HWC2::Error setPowerMode(int32_t mode); 226 HWC2::Error setVsyncEnabled(int32_t enabled); 227 HWC2::Error validate(uint32_t* outNumTypes, 228 uint32_t* outNumRequests); 229 HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z); 230 HWC2::Error getClientTargetSupport(uint32_t width, uint32_t height, 231 int32_t format, int32_t dataspace); 232 233 // Read configs from PRIMARY Display 234 int populatePrimaryConfigs(); 235 236 private: 237 class Config { 238 public: Config(Display & display)239 Config(Display& display) 240 : mDisplay(display), 241 mId(0), 242 mAttributes() {} 243 isOnDisplay(const Display & display)244 bool isOnDisplay(const Display& display) const { 245 return display.getId() == mDisplay.getId(); 246 } 247 void setAttribute(HWC2::Attribute attribute, int32_t value); 248 int32_t getAttribute(HWC2::Attribute attribute) const; setId(hwc2_config_t id)249 void setId(hwc2_config_t id) {mId = id; } getId()250 hwc2_config_t getId() const {return mId; } 251 std::string toString() const; 252 253 private: 254 Display& mDisplay; 255 hwc2_config_t mId; 256 std::unordered_map<HWC2::Attribute, int32_t> mAttributes; 257 }; 258 259 // Stores changes requested from the device upon calling prepare(). 260 // Handles change request to: 261 // - Layer composition type. 262 // - Layer hints. 263 class Changes { 264 public: getNumTypes()265 uint32_t getNumTypes() const { 266 return static_cast<uint32_t>(mTypeChanges.size()); 267 } 268 getNumLayerRequests()269 uint32_t getNumLayerRequests() const { 270 return static_cast<uint32_t>(mLayerRequests.size()); 271 } 272 273 const std::unordered_map<hwc2_layer_t, HWC2::Composition>& getTypeChanges()274 getTypeChanges() const { 275 return mTypeChanges; 276 } 277 278 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>& getLayerRequests()279 getLayerRequests() const { 280 return mLayerRequests; 281 } 282 addTypeChange(hwc2_layer_t layerId,HWC2::Composition type)283 void addTypeChange(hwc2_layer_t layerId, 284 HWC2::Composition type) { 285 mTypeChanges.insert({layerId, type}); 286 } 287 clearTypeChanges()288 void clearTypeChanges() { mTypeChanges.clear(); } 289 addLayerRequest(hwc2_layer_t layerId,HWC2::LayerRequest request)290 void addLayerRequest(hwc2_layer_t layerId, 291 HWC2::LayerRequest request) { 292 mLayerRequests.insert({layerId, request}); 293 } 294 295 private: 296 std::unordered_map<hwc2_layer_t, HWC2::Composition> 297 mTypeChanges; 298 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest> 299 mLayerRequests; 300 }; 301 302 // Generate sw vsync signal 303 class VsyncThread : public Thread { 304 public: VsyncThread(Display & display)305 VsyncThread(Display& display) 306 : mDisplay(display) {} ~VsyncThread()307 virtual ~VsyncThread() {} 308 private: 309 Display& mDisplay; 310 bool threadLoop() final; 311 }; 312 313 private: 314 EmuHWC2& mDevice; 315 // Display ID generator. 316 static std::atomic<hwc2_display_t> sNextId; 317 const hwc2_display_t mId; 318 std::string mName; 319 HWC2::DisplayType mType; 320 HWC2::PowerMode mPowerMode; 321 HWC2::Vsync mVsyncEnabled; 322 uint32_t mVsyncPeriod; 323 VsyncThread mVsyncThread; 324 FencedBuffer mClientTarget; 325 // Will only be non-null after the Display has been validated and 326 // before it has been presented 327 std::unique_ptr<Changes> mChanges; 328 // All layers this Display is aware of. 329 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers; 330 std::vector<hwc2_display_t> mReleaseLayerIds; 331 std::vector<int32_t> mReleaseFences; 332 std::vector<std::shared_ptr<Config>> mConfigs; 333 std::shared_ptr<const Config> mActiveConfig; 334 std::set<android_color_mode_t> mColorModes; 335 android_color_mode_t mActiveColorMode; 336 bool mSetColorTransform; 337 // The state of this display should only be modified from 338 // SurfaceFlinger's main loop, with the exception of when dump is 339 // called. To prevent a bad state from crashing us during a dump 340 // call, all public calls into Display must acquire this mutex. 341 mutable std::mutex mStateMutex; 342 std::unique_ptr<GrallocModule> mGralloc; 343 std::unique_ptr<ComposeMsg> mComposeMsg; 344 int mSyncDeviceFd; 345 346 }; 347 348 template<typename MF, MF memFunc, typename ...Args> displayHook(hwc2_device_t * device,hwc2_display_t displayId,Args...args)349 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId, 350 Args... args) { 351 auto display = getHWC2(device)->getDisplay(displayId); 352 if (!display) { 353 return static_cast<int32_t>(HWC2::Error::BadDisplay); 354 } 355 auto error = ((*display).*memFunc)(std::forward<Args>(args)...); 356 return static_cast<int32_t>(error); 357 } 358 359 class Layer { 360 public: 361 explicit Layer(Display& display); getDisplay()362 Display& getDisplay() const {return mDisplay;} getId()363 hwc2_layer_t getId() const {return mId;} 364 bool operator==(const Layer& other) { return mId == other.mId; } 365 bool operator!=(const Layer& other) { return !(*this == other); } 366 367 // HWC2 Layer functions 368 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence); 369 HWC2::Error setCursorPosition(int32_t x, int32_t y); 370 HWC2::Error setSurfaceDamage(hwc_region_t damage); 371 372 // HWC2 Layer state functions 373 HWC2::Error setBlendMode(int32_t mode); 374 HWC2::Error setColor(hwc_color_t color); 375 HWC2::Error setCompositionType(int32_t type); 376 HWC2::Error setDataspace(int32_t dataspace); 377 HWC2::Error setDisplayFrame(hwc_rect_t frame); 378 HWC2::Error setPlaneAlpha(float alpha); 379 HWC2::Error setSidebandStream(const native_handle_t* stream); 380 HWC2::Error setSourceCrop(hwc_frect_t crop); 381 HWC2::Error setTransform(int32_t transform); 382 HWC2::Error setVisibleRegion(hwc_region_t visible); 383 HWC2::Error setZ(uint32_t z); 384 getCompositionType()385 HWC2::Composition getCompositionType() const { 386 return mCompositionType; 387 } getColor()388 hwc_color_t getColor() {return mColor; } getZ()389 uint32_t getZ() {return mZ; } getNumVisibleRegions()390 std::size_t getNumVisibleRegions() {return mVisibleRegion.size(); } getLayerBuffer()391 FencedBuffer& getLayerBuffer() {return mBuffer; } getBlendMode()392 int32_t getBlendMode() {return (int32_t)mBlendMode; } getPlaneAlpha()393 float getPlaneAlpha() {return mPlaneAlpha; } getSourceCrop()394 hwc_frect_t getSourceCrop() {return mSourceCrop; } getDisplayFrame()395 hwc_rect_t getDisplayFrame() {return mDisplayFrame; } getTransform()396 hwc_transform_t getTransform() {return (hwc_transform_t)mTransform; } 397 private: 398 static std::atomic<hwc2_layer_t> sNextId; 399 const hwc2_layer_t mId; 400 Display& mDisplay; 401 FencedBuffer mBuffer; 402 std::vector<hwc_rect_t> mSurfaceDamage; 403 404 HWC2::BlendMode mBlendMode; 405 hwc_color_t mColor; 406 HWC2::Composition mCompositionType; 407 hwc_rect_t mDisplayFrame; 408 float mPlaneAlpha; 409 const native_handle_t* mSidebandStream; 410 hwc_frect_t mSourceCrop; 411 HWC2::Transform mTransform; 412 std::vector<hwc_rect_t> mVisibleRegion; 413 uint32_t mZ; 414 }; 415 416 template <typename MF, MF memFunc, typename ...Args> layerHook(hwc2_device_t * device,hwc2_display_t displayId,hwc2_layer_t layerId,Args...args)417 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId, 418 hwc2_layer_t layerId, Args... args) { 419 auto result = getHWC2(device)->getLayer(displayId, layerId); 420 auto error = std::get<HWC2::Error>(result); 421 if (error == HWC2::Error::None) { 422 auto layer = std::get<Layer*>(result); 423 error = ((*layer).*memFunc)(std::forward<Args>(args)...); 424 } 425 return static_cast<int32_t>(error); 426 } 427 428 // helpers 429 void populateCapabilities(); 430 Display* getDisplay(hwc2_display_t id); 431 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId, 432 hwc2_layer_t layerId); 433 434 std::unordered_set<HWC2::Capability> mCapabilities; 435 436 // These are potentially accessed from multiple threads, and are protected 437 // by this mutex. 438 std::mutex mStateMutex; 439 440 struct CallbackInfo { 441 hwc2_callback_data_t data; 442 hwc2_function_pointer_t pointer; 443 }; 444 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks; 445 446 std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays; 447 std::unordered_map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers; 448 449 }; 450 451 } 452 #endif 453