1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <drm/drm_fourcc.h> 20 #include <hardware/gralloc.h> 21 22 #include <array> 23 #include <map> 24 25 #include "bufferinfo/BufferInfo.h" 26 #include "drm/DrmDevice.h" 27 28 #ifndef DRM_FORMAT_INVALID 29 #define DRM_FORMAT_INVALID 0 30 #endif 31 32 using GemHandle = uint32_t; 33 34 namespace android { 35 36 class DrmFbIdHandle { 37 public: 38 static auto CreateInstance(BufferInfo *bo, GemHandle first_gem_handle, 39 DrmDevice &drm) -> std::shared_ptr<DrmFbIdHandle>; 40 41 ~DrmFbIdHandle(); 42 DrmFbIdHandle(DrmFbIdHandle &&) = delete; 43 DrmFbIdHandle(const DrmFbIdHandle &) = delete; 44 auto operator=(const DrmFbIdHandle &) = delete; 45 auto operator=(DrmFbIdHandle &&) = delete; 46 47 auto GetFbId [[nodiscard]] () const -> uint32_t { 48 return fb_id_; 49 } 50 51 private: DrmFbIdHandle(DrmDevice & drm)52 explicit DrmFbIdHandle(DrmDevice &drm) : drm_(&drm){}; 53 54 DrmDevice *const drm_; 55 56 uint32_t fb_id_{}; 57 std::array<GemHandle, kBufferMaxPlanes> gem_handles_{}; 58 }; 59 60 class DrmFbImporter { 61 public: DrmFbImporter(DrmDevice & drm)62 explicit DrmFbImporter(DrmDevice &drm) : drm_(&drm){}; 63 ~DrmFbImporter() = default; 64 DrmFbImporter(const DrmFbImporter &) = delete; 65 DrmFbImporter(DrmFbImporter &&) = delete; 66 auto operator=(const DrmFbImporter &) = delete; 67 auto operator=(DrmFbImporter &&) = delete; 68 69 auto GetOrCreateFbId(BufferInfo *bo) -> std::shared_ptr<DrmFbIdHandle>; 70 71 private: CleanupEmptyCacheElements()72 void CleanupEmptyCacheElements() { 73 for (auto it = drm_fb_id_handle_cache_.begin(); 74 it != drm_fb_id_handle_cache_.end();) { 75 if (it->second.expired()) { 76 it = drm_fb_id_handle_cache_.erase(it); 77 } else { 78 ++it; 79 } 80 } 81 } 82 83 DrmDevice *const drm_; 84 85 std::map<GemHandle, std::weak_ptr<DrmFbIdHandle>> drm_fb_id_handle_cache_; 86 }; 87 88 } // namespace android 89