1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2020 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include <ui/DisplayId.h> 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #include <limits> 22*38e8c45fSAndroid Build Coastguard Worker #include <optional> 23*38e8c45fSAndroid Build Coastguard Worker #include <random> 24*38e8c45fSAndroid Build Coastguard Worker #include <unordered_set> 25*38e8c45fSAndroid Build Coastguard Worker 26*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h> 27*38e8c45fSAndroid Build Coastguard Worker 28*38e8c45fSAndroid Build Coastguard Worker namespace android { 29*38e8c45fSAndroid Build Coastguard Worker 30*38e8c45fSAndroid Build Coastguard Worker // Generates pseudo-random IDs of type GpuVirtualDisplayId or HalVirtualDisplayId. 31*38e8c45fSAndroid Build Coastguard Worker template <typename Id> 32*38e8c45fSAndroid Build Coastguard Worker class DisplayIdGenerator { 33*38e8c45fSAndroid Build Coastguard Worker public: 34*38e8c45fSAndroid Build Coastguard Worker explicit DisplayIdGenerator(size_t maxIdsCount = std::numeric_limits<size_t>::max()) mMaxIdsCount(maxIdsCount)35*38e8c45fSAndroid Build Coastguard Worker : mMaxIdsCount(maxIdsCount) {} 36*38e8c45fSAndroid Build Coastguard Worker inUse()37*38e8c45fSAndroid Build Coastguard Worker bool inUse() const { return !mUsedIds.empty(); } 38*38e8c45fSAndroid Build Coastguard Worker generateId()39*38e8c45fSAndroid Build Coastguard Worker std::optional<Id> generateId() { 40*38e8c45fSAndroid Build Coastguard Worker if (mUsedIds.size() >= mMaxIdsCount) { 41*38e8c45fSAndroid Build Coastguard Worker return std::nullopt; 42*38e8c45fSAndroid Build Coastguard Worker } 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker constexpr int kMaxAttempts = 1000; 45*38e8c45fSAndroid Build Coastguard Worker 46*38e8c45fSAndroid Build Coastguard Worker for (int attempts = 0; attempts < kMaxAttempts; attempts++) { 47*38e8c45fSAndroid Build Coastguard Worker const Id id{mDistribution(mGenerator)}; 48*38e8c45fSAndroid Build Coastguard Worker if (mUsedIds.count(id) == 0) { 49*38e8c45fSAndroid Build Coastguard Worker mUsedIds.insert(id); 50*38e8c45fSAndroid Build Coastguard Worker return id; 51*38e8c45fSAndroid Build Coastguard Worker } 52*38e8c45fSAndroid Build Coastguard Worker } 53*38e8c45fSAndroid Build Coastguard Worker 54*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL("Couldn't generate ID after %d attempts", kMaxAttempts); 55*38e8c45fSAndroid Build Coastguard Worker } 56*38e8c45fSAndroid Build Coastguard Worker releaseId(Id id)57*38e8c45fSAndroid Build Coastguard Worker void releaseId(Id id) { mUsedIds.erase(id); } 58*38e8c45fSAndroid Build Coastguard Worker 59*38e8c45fSAndroid Build Coastguard Worker private: 60*38e8c45fSAndroid Build Coastguard Worker const size_t mMaxIdsCount; 61*38e8c45fSAndroid Build Coastguard Worker 62*38e8c45fSAndroid Build Coastguard Worker std::unordered_set<Id> mUsedIds; 63*38e8c45fSAndroid Build Coastguard Worker 64*38e8c45fSAndroid Build Coastguard Worker // Pseudo-random with random seed, in contrast to physical display IDs, which are stable 65*38e8c45fSAndroid Build Coastguard Worker // across reboots. The only ISurfaceComposer exposure for these IDs is a restricted API 66*38e8c45fSAndroid Build Coastguard Worker // for screencap, so there is little benefit in making them unpredictable. 67*38e8c45fSAndroid Build Coastguard Worker std::default_random_engine mGenerator{std::random_device()()}; 68*38e8c45fSAndroid Build Coastguard Worker std::uniform_int_distribution<typename Id::BaseId> mDistribution; 69*38e8c45fSAndroid Build Coastguard Worker }; 70*38e8c45fSAndroid Build Coastguard Worker 71*38e8c45fSAndroid Build Coastguard Worker } // namespace android 72