1*0a9764feSAndroid Build Coastguard Worker /* 2*0a9764feSAndroid Build Coastguard Worker * Copyright (C) 2021 The Android Open Source Project 3*0a9764feSAndroid Build Coastguard Worker * 4*0a9764feSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*0a9764feSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*0a9764feSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*0a9764feSAndroid Build Coastguard Worker * 8*0a9764feSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*0a9764feSAndroid Build Coastguard Worker * 10*0a9764feSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*0a9764feSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*0a9764feSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*0a9764feSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*0a9764feSAndroid Build Coastguard Worker * limitations under the License. 15*0a9764feSAndroid Build Coastguard Worker */ 16*0a9764feSAndroid Build Coastguard Worker 17*0a9764feSAndroid Build Coastguard Worker #pragma once 18*0a9764feSAndroid Build Coastguard Worker 19*0a9764feSAndroid Build Coastguard Worker #include <drm/drm_fourcc.h> 20*0a9764feSAndroid Build Coastguard Worker #include <hardware/gralloc.h> 21*0a9764feSAndroid Build Coastguard Worker 22*0a9764feSAndroid Build Coastguard Worker #include <array> 23*0a9764feSAndroid Build Coastguard Worker #include <map> 24*0a9764feSAndroid Build Coastguard Worker 25*0a9764feSAndroid Build Coastguard Worker #include "bufferinfo/BufferInfo.h" 26*0a9764feSAndroid Build Coastguard Worker #include "drm/DrmDevice.h" 27*0a9764feSAndroid Build Coastguard Worker #include "utils/fd.h" 28*0a9764feSAndroid Build Coastguard Worker 29*0a9764feSAndroid Build Coastguard Worker #ifndef DRM_FORMAT_INVALID 30*0a9764feSAndroid Build Coastguard Worker #define DRM_FORMAT_INVALID 0 31*0a9764feSAndroid Build Coastguard Worker #endif 32*0a9764feSAndroid Build Coastguard Worker 33*0a9764feSAndroid Build Coastguard Worker using GemHandle = uint32_t; 34*0a9764feSAndroid Build Coastguard Worker 35*0a9764feSAndroid Build Coastguard Worker namespace android { 36*0a9764feSAndroid Build Coastguard Worker 37*0a9764feSAndroid Build Coastguard Worker class DrmFbIdHandle { 38*0a9764feSAndroid Build Coastguard Worker public: 39*0a9764feSAndroid Build Coastguard Worker static auto CreateInstance(BufferInfo *bo, GemHandle first_gem_handle, 40*0a9764feSAndroid Build Coastguard Worker DrmDevice &drm) -> std::shared_ptr<DrmFbIdHandle>; 41*0a9764feSAndroid Build Coastguard Worker 42*0a9764feSAndroid Build Coastguard Worker ~DrmFbIdHandle(); 43*0a9764feSAndroid Build Coastguard Worker DrmFbIdHandle(DrmFbIdHandle &&) = delete; 44*0a9764feSAndroid Build Coastguard Worker DrmFbIdHandle(const DrmFbIdHandle &) = delete; 45*0a9764feSAndroid Build Coastguard Worker auto operator=(const DrmFbIdHandle &) = delete; 46*0a9764feSAndroid Build Coastguard Worker auto operator=(DrmFbIdHandle &&) = delete; 47*0a9764feSAndroid Build Coastguard Worker 48*0a9764feSAndroid Build Coastguard Worker auto GetFbId [[nodiscard]] () const -> uint32_t { 49*0a9764feSAndroid Build Coastguard Worker return fb_id_; 50*0a9764feSAndroid Build Coastguard Worker } 51*0a9764feSAndroid Build Coastguard Worker 52*0a9764feSAndroid Build Coastguard Worker private: DrmFbIdHandle(DrmDevice & drm)53*0a9764feSAndroid Build Coastguard Worker explicit DrmFbIdHandle(DrmDevice &drm) : drm_fd_(drm.GetFd()) {}; 54*0a9764feSAndroid Build Coastguard Worker 55*0a9764feSAndroid Build Coastguard Worker SharedFd drm_fd_; 56*0a9764feSAndroid Build Coastguard Worker 57*0a9764feSAndroid Build Coastguard Worker uint32_t fb_id_{}; 58*0a9764feSAndroid Build Coastguard Worker std::array<GemHandle, kBufferMaxPlanes> gem_handles_{}; 59*0a9764feSAndroid Build Coastguard Worker }; 60*0a9764feSAndroid Build Coastguard Worker 61*0a9764feSAndroid Build Coastguard Worker class DrmFbImporter { 62*0a9764feSAndroid Build Coastguard Worker public: DrmFbImporter(DrmDevice & drm)63*0a9764feSAndroid Build Coastguard Worker explicit DrmFbImporter(DrmDevice &drm) : drm_(&drm){}; 64*0a9764feSAndroid Build Coastguard Worker ~DrmFbImporter() = default; 65*0a9764feSAndroid Build Coastguard Worker DrmFbImporter(const DrmFbImporter &) = delete; 66*0a9764feSAndroid Build Coastguard Worker DrmFbImporter(DrmFbImporter &&) = delete; 67*0a9764feSAndroid Build Coastguard Worker auto operator=(const DrmFbImporter &) = delete; 68*0a9764feSAndroid Build Coastguard Worker auto operator=(DrmFbImporter &&) = delete; 69*0a9764feSAndroid Build Coastguard Worker 70*0a9764feSAndroid Build Coastguard Worker auto GetOrCreateFbId(BufferInfo *bo) -> std::shared_ptr<DrmFbIdHandle>; 71*0a9764feSAndroid Build Coastguard Worker 72*0a9764feSAndroid Build Coastguard Worker private: CleanupEmptyCacheElements()73*0a9764feSAndroid Build Coastguard Worker void CleanupEmptyCacheElements() { 74*0a9764feSAndroid Build Coastguard Worker for (auto it = drm_fb_id_handle_cache_.begin(); 75*0a9764feSAndroid Build Coastguard Worker it != drm_fb_id_handle_cache_.end();) { 76*0a9764feSAndroid Build Coastguard Worker if (it->second.expired()) { 77*0a9764feSAndroid Build Coastguard Worker it = drm_fb_id_handle_cache_.erase(it); 78*0a9764feSAndroid Build Coastguard Worker } else { 79*0a9764feSAndroid Build Coastguard Worker ++it; 80*0a9764feSAndroid Build Coastguard Worker } 81*0a9764feSAndroid Build Coastguard Worker } 82*0a9764feSAndroid Build Coastguard Worker } 83*0a9764feSAndroid Build Coastguard Worker 84*0a9764feSAndroid Build Coastguard Worker DrmDevice *const drm_; 85*0a9764feSAndroid Build Coastguard Worker SharedFd drm_fd_; 86*0a9764feSAndroid Build Coastguard Worker 87*0a9764feSAndroid Build Coastguard Worker std::map<GemHandle, std::weak_ptr<DrmFbIdHandle>> drm_fb_id_handle_cache_; 88*0a9764feSAndroid Build Coastguard Worker }; 89*0a9764feSAndroid Build Coastguard Worker 90*0a9764feSAndroid Build Coastguard Worker } // namespace android 91