1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Collabora, Ltd.
3bf215546Sopenharmony_ci * Author: Antonio Caggiano <antonio.caggiano@collabora.com>
4bf215546Sopenharmony_ci * Author: Robert Beckett <bob.beckett@collabora.com>
5bf215546Sopenharmony_ci * Author: Corentin Noël <corentin.noel@collabora.com>
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * SPDX-License-Identifier: MIT
8bf215546Sopenharmony_ci */
9bf215546Sopenharmony_ci
10bf215546Sopenharmony_ci#pragma once
11bf215546Sopenharmony_ci
12bf215546Sopenharmony_ci#include <memory>
13bf215546Sopenharmony_ci#include <string>
14bf215546Sopenharmony_ci#include <unordered_map>
15bf215546Sopenharmony_ci#include <vector>
16bf215546Sopenharmony_ci
17bf215546Sopenharmony_ci#include "pps_counter.h"
18bf215546Sopenharmony_ci#include "pps_device.h"
19bf215546Sopenharmony_ci
20bf215546Sopenharmony_cinamespace pps
21bf215546Sopenharmony_ci{
22bf215546Sopenharmony_ci/// @brief Abstract Driver class
23bf215546Sopenharmony_ciclass Driver
24bf215546Sopenharmony_ci{
25bf215546Sopenharmony_ci   public:
26bf215546Sopenharmony_ci   /// @return A map of supported DRM device names and their relative pps driver
27bf215546Sopenharmony_ci   static const std::unordered_map<std::string, std::unique_ptr<Driver>> &get_supported_drivers();
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci   /// @return A list of supported DRM device names
30bf215546Sopenharmony_ci   static const std::vector<std::string> supported_device_names();
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci   /// @return A driver supporting a specific DRM device
33bf215546Sopenharmony_ci   static Driver *get_driver(DrmDevice &&drm_device);
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci   /// @return The name of a default selected PPS driver
36bf215546Sopenharmony_ci   static std::string default_driver_name();
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   /// @return The name of a driver based on the request, otherwise the default driver name
39bf215546Sopenharmony_ci   static std::string find_driver_name(const char *requested_name);
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   Driver() = default;
42bf215546Sopenharmony_ci   virtual ~Driver() = default;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   // Forbid copy
45bf215546Sopenharmony_ci   Driver(const Driver &) = delete;
46bf215546Sopenharmony_ci   Driver &operator=(const Driver &) = delete;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   /// @return The minimum sampling period for the current device
49bf215546Sopenharmony_ci   virtual uint64_t get_min_sampling_period_ns() = 0;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /// @brief Enable a counter by its ID
52bf215546Sopenharmony_ci   virtual void enable_counter(uint32_t counter_id) = 0;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   virtual void enable_all_counters() = 0;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /// @brief Initialize performance counters data such as groups and counters
57bf215546Sopenharmony_ci   /// @return Whether it was successful or not
58bf215546Sopenharmony_ci   virtual bool init_perfcnt() = 0;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   /// @brief Enables performance counters, meaning that from now on they can be sampled
61bf215546Sopenharmony_ci   virtual void enable_perfcnt(uint64_t sampling_period_ns) = 0;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   /// @brief Disables performance counters on the device
64bf215546Sopenharmony_ci   virtual void disable_perfcnt() = 0;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /// @brief Asking the GPU to dump performance counters could have different meanings
67bf215546Sopenharmony_ci   /// depending on the concrete driver. Some could just ask the GPU to dump counters to a
68bf215546Sopenharmony_ci   /// user space buffer, while some others will need to read data from a stream which was
69bf215546Sopenharmony_ci   /// written asynchronously.
70bf215546Sopenharmony_ci   /// @return Whether it was able to dump, false otherwise
71bf215546Sopenharmony_ci   virtual bool dump_perfcnt() = 0;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   /// @brief After dumping performance counters, with this function you can iterate
74bf215546Sopenharmony_ci   /// through the samples collected.
75bf215546Sopenharmony_ci   /// @return The GPU timestamp associated to current sample, or 0 if there are no more samples
76bf215546Sopenharmony_ci   virtual uint64_t next() = 0;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   /// Clock ID in which the values returned by gpu_timestamp() belong
79bf215546Sopenharmony_ci   virtual uint32_t gpu_clock_id() const = 0;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   /// Sample a timestamp from the GPU
82bf215546Sopenharmony_ci   virtual uint64_t gpu_timestamp() const = 0;
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   DrmDevice drm_device;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   /// List of counter groups
87bf215546Sopenharmony_ci   std::vector<CounterGroup> groups;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   /// List of counters exposed by the GPU
90bf215546Sopenharmony_ci   std::vector<Counter> counters;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   /// List of counters that are actually enabled
93bf215546Sopenharmony_ci   std::vector<Counter> enabled_counters;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   protected:
96bf215546Sopenharmony_ci   // Prevent object slicing by allowing move only from subclasses
97bf215546Sopenharmony_ci   Driver(Driver &&) = default;
98bf215546Sopenharmony_ci   Driver &operator=(Driver &&) = default;
99bf215546Sopenharmony_ci};
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci} // namespace pps
102