xref: /aosp_15_r20/external/skia/tests/TextureSizeTest.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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkRefCnt.h"
15 #include "tests/CtsEnforcement.h"
16 #include "tests/Test.h"
17 
18 #if defined(SK_GRAPHITE)
19 #include "include/gpu/graphite/Context.h"
20 #include "include/gpu/graphite/Image.h"
21 #include "include/gpu/graphite/Recorder.h"
22 #endif
23 
24 #if defined(SK_GANESH)
25 #include "include/gpu/GpuTypes.h"
26 #include "include/gpu/ganesh/GrDirectContext.h"
27 #include "include/gpu/ganesh/SkImageGanesh.h"
28 #include "src/gpu/ganesh/GrCaps.h"
29 #include "src/gpu/ganesh/GrDirectContextPriv.h"
30 struct GrContextOptions;
31 #endif
32 
33 #include <cstddef>
34 #include <functional>
35 #include <initializer_list>
36 
37 namespace {
38 
run_test(skiatest::Reporter * reporter,bool testMipmaps,std::function<sk_sp<SkImage> (SkImage *,skgpu::Mipmapped)> convert2gpu)39 void run_test(skiatest::Reporter* reporter, bool testMipmaps,
40               std::function<sk_sp<SkImage>(SkImage*, skgpu::Mipmapped)> convert2gpu) {
41 
42     for (auto mm : { skgpu::Mipmapped::kYes, skgpu::Mipmapped::kNo }) {
43         if (!testMipmaps && mm == skgpu::Mipmapped::kYes) {
44             continue;
45         }
46 
47         for (auto ct : { kRGBA_8888_SkColorType, kAlpha_8_SkColorType }) {
48             SkImageInfo ii = SkImageInfo::Make(9, 9, ct, kPremul_SkAlphaType);
49 
50             SkBitmap src;
51             src.allocPixels(ii);
52             src.eraseColor(SK_ColorWHITE);
53 
54             sk_sp<SkImage> raster = src.asImage();
55 
56             sk_sp<SkImage> gpu = convert2gpu(raster.get(), mm);
57 
58             int bytesPerPixel = SkColorTypeBytesPerPixel(ct);
59 
60             size_t expectedSize = bytesPerPixel * gpu->width() * gpu->height();
61             if (mm == skgpu::Mipmapped::kYes) {
62                 expectedSize += expectedSize/3;
63             }
64 
65             size_t actualSize = gpu->textureSize();
66 
67             REPORTER_ASSERT(reporter, actualSize == expectedSize,
68                             "Expected: %zu  Actual: %zu", expectedSize, actualSize);
69         }
70     }
71 }
72 
73 } // anonymous namespace
74 
75 #if defined(SK_GANESH)
76 
DEF_GANESH_TEST_FOR_ALL_CONTEXTS(ImageSizeTest_Ganesh,reporter,ctxInfo,CtsEnforcement::kApiLevel_V)77 DEF_GANESH_TEST_FOR_ALL_CONTEXTS(ImageSizeTest_Ganesh,
78                                  reporter,
79                                  ctxInfo,
80                                  CtsEnforcement::kApiLevel_V) {
81     auto dContext = ctxInfo.directContext();
82 
83     bool testMipmaps = dContext->priv().caps()->mipmapSupport();
84 
85     run_test(reporter, testMipmaps,
86              [&](SkImage* src, skgpu::Mipmapped mipmapped) -> sk_sp<SkImage> {
87                  return SkImages::TextureFromImage(dContext, src, mipmapped);
88              });
89 }
90 
91 #endif // SK_GANESH
92 
93 #if defined(SK_GRAPHITE)
94 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageSizeTest_Graphite,reporter,context,CtsEnforcement::kApiLevel_V)95 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageSizeTest_Graphite, reporter, context,
96                                    CtsEnforcement::kApiLevel_V) {
97     using namespace skgpu::graphite;
98 
99     std::unique_ptr<Recorder> recorder = context->makeRecorder();
100 
101     run_test(reporter, /* testMipmaps= */ true,
102              [&](SkImage* src, skgpu::Mipmapped mipmapped) -> sk_sp<SkImage> {
103                  return SkImages::TextureFromImage(recorder.get(), src,
104                                                    { mipmapped == skgpu::Mipmapped::kYes });
105              });
106 }
107 
108 #endif // SK_GRAPHITE
109