Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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,
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 }) {
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
reporter
@ 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)
#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
T * get() const
Definition SkRefCnt.h:303
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
void run_test(skiatest::Reporter *reporter, Context *context, SkISize surfaceSize, SkISize recordingSize, SkISize replayOffset, DrawCallback draw, const std::vector< Expectation > &expectations)
Mipmapped
Definition GpuTypes.h:53
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)