xref: /aosp_15_r20/external/skia/tests/TextureStripAtlasManagerTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 Google Inc.
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/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSamplingOptions.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTileMode.h"
20 #include "include/core/SkTypes.h"
21 #include "include/effects/SkGradientShader.h"
22 #include "include/gpu/GpuTypes.h"
23 #include "include/gpu/ganesh/GrDirectContext.h"
24 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
25 #include "tests/CtsEnforcement.h"
26 #include "tests/Test.h"
27 #include "tools/DecodeUtils.h"
28 
29 #include <cstdint>
30 #include <utility>
31 
32 class SkImage;
33 struct GrContextOptions;
34 
35 // The gradient shader will use the texture strip atlas if it has too many colors. Make sure
36 // abandoning the context works.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureStripAtlasManagerGradientTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)37 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureStripAtlasManagerGradientTest,
38                                        reporter,
39                                        ctxInfo,
40                                        CtsEnforcement::kApiLevel_T) {
41     auto context = ctxInfo.directContext();
42 
43     static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
44                                        SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW,
45                                        SK_ColorBLACK };
46     static const SkScalar gPos[] = { 0, 0.17f, 0.32f, 0.49f, 0.66f, 0.83f, 1.0f };
47 
48     SkPaint p;
49     p.setShader(SkGradientShader::MakeTwoPointConical(SkPoint::Make(0, 0),
50                                                       1.0f,
51                                                       SkPoint::Make(10.0f, 20.0f),
52                                                       2.0f,
53                                                       gColors,
54                                                       gPos,
55                                                       7,
56                                                       SkTileMode::kClamp));
57 
58     SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
59     auto surface(SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, info));
60     SkCanvas* canvas = surface->getCanvas();
61 
62     SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
63 
64     canvas->drawRect(r, p);
65 
66     context->abandonContext();
67 }
68 
69 // The table color filter uses the texture strip atlas. Make sure abandoning the context works.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureStripAtlasManagerColorFilterTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)70 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureStripAtlasManagerColorFilterTest,
71                                        reporter,
72                                        ctxInfo,
73                                        CtsEnforcement::kApiLevel_T) {
74     auto context = ctxInfo.directContext();
75 
76     sk_sp<SkImage> img = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
77 
78     uint8_t identity[256];
79     for (int i = 0; i < 256; i++) {
80         identity[i] = i;
81     }
82 
83     SkPaint p;
84     p.setColorFilter(SkColorFilters::Table(identity));
85 
86     SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
87     auto surface(SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, info));
88     SkCanvas* canvas = surface->getCanvas();
89 
90     canvas->drawImage(std::move(img), 0, 0, SkSamplingOptions(), &p);
91 
92     context->abandonContext();
93 }
94