xref: /aosp_15_r20/external/skia/tools/testrunners/common/surface_manager/SurfaceManager.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SurfaceManager_DEFINED
9 #define SurfaceManager_DEFINED
10 
11 #include "include/core/SkSurface.h"
12 
13 #include <functional>
14 #include <map>
15 #include <memory>
16 #include <string>
17 
18 struct GrContextOptions;
19 
20 namespace skgpu::graphite {
21 struct ContextOptions;
22 }
23 
24 namespace sk_gpu_test {
25 class ContextInfo;
26 };
27 
28 // Gathers the parameters needed by SurfaceManager::FromConfig to construct a surface for a given
29 // configuration.
30 struct SurfaceOptions {
31     int width;
32     int height;
33 
34     std::function<void(GrContextOptions*)> modifyGrContextOptions = nullptr;
35     std::function<void(skgpu::graphite::ContextOptions*)> modifyGraphiteContextOptions = nullptr;
36 };
37 
38 // Abstract class to create and manage surfaces used by various kinds of tests (GMs, Benchmarks,
39 // etc.).
40 class SurfaceManager {
41 public:
42     enum class CpuOrGpu { kCPU, kGPU };
43 
44     // Constructs a SurfaceManager for the given config name (e.g. "8888", "565", "gles"). It
45     // returns nullptr if the config is unknown, and it aborts execution if the config is known but
46     // we weren't able to construct the surface for any reason.
47     static std::unique_ptr<SurfaceManager> FromConfig(std::string config,
48                                                       SurfaceOptions surfaceOptions);
49 
50     // Returns the surface created from the given config. All calls return the same surface.
51     virtual sk_sp<SkSurface> getSurface() = 0;
52 
53     // Flushes the surface. This method should be called after the test is done drawing. This
54     // ensures that all commands are flushed to the GPU in the case of Ganesh-backed surfaces.
55     // Failing to do so may lead to blank pixmaps.
flush()56     virtual void flush() {}
57 
58     // Returns the subset of Gold key/value pairs that are determined by the surface config. The
59     // returned map includes keys "cpu_or_gpu" and "cpu_or_gpu_value", which are populated based
60     // on the cpuName and gpuName arguments, and whether the surface config is CPU or GPU bound.
61     // The returned map also includes various keys pertaining to color, which are generated from
62     // the SkColorInfo passed to this class' constructor.
63     std::map<std::string, std::string> getGoldKeyValuePairs(std::string cpuName,
64                                                             std::string gpuName) const;
65 
66     // Returns the subset of Perf key/value pairs that are determined by the surface config. The
67     // returned map includes keys "cpu_or_gpu" and "cpu_or_gpu_value", which are populated based
68     // on the cpuName and gpuName arguments, and whether the surface config is CPU or GPU bound.
69     std::map<std::string, std::string> getPerfKeyValuePairs(std::string cpuName,
70                                                             std::string gpuName) const;
71 
72     // Returns an enum indicating whether the surface is CPU or GPU bound.
73     CpuOrGpu isCpuOrGpuBound() const;
74 
75     // Returns the Ganesh ContextInfo on SurfaceManager implementations that support it.
getGaneshContextInfo()76     virtual sk_gpu_test::ContextInfo* getGaneshContextInfo() {
77         SK_ABORT("This SurfaceManager implementation does not support the requested operation.");
78     }
79 
80     virtual ~SurfaceManager() = default;
81 
82 protected:
83     // Takes the config name passed to FromConfig(), and the SkColorInfo used by FromConfig() to
84     // create the surface.
SurfaceManager(std::string config,SkColorInfo colorInfo,CpuOrGpu cpuOrGpu)85     SurfaceManager(std::string config, SkColorInfo colorInfo, CpuOrGpu cpuOrGpu)
86             : fConfig(config), fColorInfo(colorInfo), fCpuOrGpu(cpuOrGpu) {}
87 
88 private:
89     std::string fConfig;
90     SkColorInfo fColorInfo;
91     CpuOrGpu fCpuOrGpu;
92 
93     std::map<std::string, std::string> getCpuOrGpuKeyValuePairs(std::string cpuName,
94                                                                 std::string gpuName) const;
95 };
96 
97 #endif  // SurfaceManager_DEFINED
98