Flutter Engine
The Flutter Engine
TextureSizeTest.cpp
Go to the documentation of this file.
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
16#include "tests/Test.h"
17
18#if defined(SK_GRAPHITE)
22#endif
23
24#if defined(SK_GANESH)
30struct GrContextOptions;
31#endif
32
33#include <cstddef>
34#include <functional>
35#include <initializer_list>
36
37namespace {
38
39void run_test(skiatest::Reporter* reporter, bool testMipmaps,
41
43 if (!testMipmaps && mm == skgpu::Mipmapped::kYes) {
44 continue;
45 }
46
47 for (auto ct : { kRGBA_8888_SkColorType, kAlpha_8_SkColorType }) {
49
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
77DEF_GANESH_TEST_FOR_ALL_CONTEXTS(ImageSizeTest_Ganesh,
79 ctxInfo,
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
95DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageSizeTest_Graphite, reporter, context,
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
static void run_test(GrDirectContext *dContext, skiatest::Reporter *reporter, BulkRectTest test)
reporter
Definition: FontMgrTest.cpp:39
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
#define DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(name, reporter, graphite_ctx, ctsEnforcement)
Definition: Test.h:373
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define DEF_GANESH_TEST_FOR_ALL_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition: Test.h:431
int width() const
Definition: SkImage.h:285
virtual size_t textureSize() const =0
int height() const
Definition: SkImage.h:291
T * get() const
Definition: SkRefCnt.h:303
Dart_NativeFunction function
Definition: fuchsia.cc:51
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
Mipmapped
Definition: GpuTypes.h:53
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)