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