xref: /aosp_15_r20/external/skia/tools/testrunners/common/surface_manager/RasterSurfaceManager.cpp (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 #include "include/core/SkColorSpace.h"
9 #include "include/core/SkColorType.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkSurface.h"
12 #include "include/private/base/SkAssert.h"
13 #include "tools/testrunners/common/surface_manager/SurfaceManager.h"
14 
15 #include <string>
16 
17 class RasterSurfaceManager : public SurfaceManager {
18 public:
RasterSurfaceManager(sk_sp<SkSurface> surface,std::string config,SkColorInfo colorInfo)19     RasterSurfaceManager(sk_sp<SkSurface> surface, std::string config, SkColorInfo colorInfo)
20             : SurfaceManager(config, colorInfo, CpuOrGpu::kCPU), fSurface(surface) {}
21 
getSurface()22     sk_sp<SkSurface> getSurface() override { return fSurface; }
23 
24 private:
25     sk_sp<SkSurface> fSurface;
26 };
27 
FromConfig(std::string config,SurfaceOptions surfaceOptions)28 std::unique_ptr<SurfaceManager> SurfaceManager::FromConfig(std::string config,
29                                                            SurfaceOptions surfaceOptions) {
30     // This config is based on nanobench's "nonrendering" config:
31     // https://skia.googlesource.com/skia/+/a063eaeaf1e09e4d6f42e0f44a5723622a46d21c/bench/nanobench.cpp#663.
32     // It is placed here because RasterSurfaceManager is the SurfaceManager implementation used
33     // when no GPU backend is specified via Bazel's --config flag, which should be the case for all
34     // nonrendering benchmarks.
35     if (config == "nonrendering") {
36         // The surface and color info are never used by nonrendering benchmarks, so their values do
37         // not matter.
38         return std::make_unique<RasterSurfaceManager>(nullptr, config, SkColorInfo());
39     }
40 
41     // These configs are based on the RasterSink configs here:
42     // https://skia.googlesource.com/skia/+/faaa8393a68b518ec1f204a60c7c3393e1da2fa2/dm/DM.cpp#1046.
43     if (config == "8888") {
44         SkColorInfo colorInfo(kN32_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
45         sk_sp<SkSurface> surface = SkSurfaces::Raster(
46                 SkImageInfo::Make({surfaceOptions.width, surfaceOptions.height}, colorInfo));
47         SkASSERT_RELEASE(surface);
48         return std::make_unique<RasterSurfaceManager>(surface, config, colorInfo);
49     }
50     if (config == "565") {
51         SkColorInfo colorInfo(kRGB_565_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
52         sk_sp<SkSurface> surface = SkSurfaces::Raster(
53                 SkImageInfo::Make({surfaceOptions.width, surfaceOptions.height}, colorInfo));
54         SkASSERT_RELEASE(surface);
55         return std::make_unique<RasterSurfaceManager>(surface, config, colorInfo);
56     }
57     return nullptr;
58 }
59