Flutter Engine
The Flutter Engine
RectangleTextureTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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 "tests/Test.h"
9
10#ifdef SK_GL
18#include "include/core/SkRect.h"
25#include "include/gpu/GrTypes.h"
30#include "src/gpu/Swizzle.h"
43#include "src/gpu/ganesh/SkGr.h"
49#include "tests/TestUtils.h"
51
52#include <cstdint>
53#include <initializer_list>
54#include <memory>
55#include <utility>
56
57using namespace skia_private;
58
59struct GrContextOptions;
60
61// skbug.com/5932
62static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrDirectContext* dContext,
64 SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
65 auto sfc = dContext->priv().makeSFC(
66 {colorType, kPremul_SkAlphaType, nullptr, rectView.dimensions()}, /*label=*/{});
69 sfc->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA));
70 auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm);
71 sfc->fillWithFP(std::move(fp));
72 TestReadPixels(reporter, dContext, sfc.get(), expectedPixelValues,
73 "RectangleTexture-basic-draw");
74 }
75 }
76}
77
78static void test_clear(skiatest::Reporter* reporter,
79 GrDirectContext* dContext,
80 skgpu::ganesh::SurfaceContext* rectContext) {
81 if (auto sfc = rectContext->asFillContext()) {
82 // Clear the whole thing.
83 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
84 sfc->clear(SkPMColor4f::FromBytes_RGBA(color0));
85
86 int w = sfc->width();
87 int h = sfc->height();
88 int pixelCnt = w * h;
89 AutoTMalloc<uint32_t> expectedPixels(pixelCnt);
90
91 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
92 uint32_t expectedColor0 = 0;
93 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
94 expectedBytes0[0] = GrColorUnpackR(color0);
95 expectedBytes0[1] = GrColorUnpackG(color0);
96 expectedBytes0[2] = GrColorUnpackB(color0);
97 expectedBytes0[3] = GrColorUnpackA(color0);
98 for (int i = 0; i < sfc->width() * sfc->height(); ++i) {
99 expectedPixels.get()[i] = expectedColor0;
100 }
101
102 // Clear the the top to a different color.
103 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
105 sfc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1));
106
107 uint32_t expectedColor1 = 0;
108 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
109 expectedBytes1[0] = GrColorUnpackR(color1);
110 expectedBytes1[1] = GrColorUnpackG(color1);
111 expectedBytes1[2] = GrColorUnpackB(color1);
112 expectedBytes1[3] = GrColorUnpackA(color1);
113
114 for (int y = 0; y < h/2; ++y) {
115 for (int x = 0; x < w; ++x) {
116 expectedPixels.get()[y * h + x] = expectedColor1;
117 }
118 }
119
120 TestReadPixels(reporter, dContext, sfc, expectedPixels.get(), "RectangleTexture-clear");
121 }
122}
123
124static void test_copy_to_surface(skiatest::Reporter* reporter,
125 GrDirectContext* dContext,
127 const char* testName) {
128 int pixelCnt = dstContext->width() * dstContext->height();
129 AutoTMalloc<uint32_t> pixels(pixelCnt);
130 for (int y = 0; y < dstContext->width(); ++y) {
131 for (int x = 0; x < dstContext->height(); ++x) {
132 pixels.get()[y * dstContext->width() + x] =
134 }
135 }
136
137 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
138 auto origin = dstContext->origin();
141 nullptr,
142 dstContext->dimensions());
143 GrCPixmap pixmap(info, pixels.get(), dstContext->width()*sizeof(uint32_t));
144 auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
145 renderable,
146 origin,
147 pixmap);
148 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
149 // be more restrictive.
150 SkAssertResult(dstContext->testCopy(srcView.refProxy()));
151 TestReadPixels(reporter, dContext, dstContext, pixels.get(), testName);
152 }
153}
154
156 auto dContext = ctxInfo.directContext();
157
158 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
159 static const int kWidth = 16;
160 static const int kHeight = 16;
161
162 uint32_t pixels[kWidth * kHeight];
163 for (int y = 0; y < kHeight; ++y) {
164 for (int x = 0; x < kWidth; ++x) {
165 pixels[y * kWidth + x] = y * kWidth + x;
166 }
167 }
169 SkPixmap pm(ii, pixels, sizeof(uint32_t)*kWidth);
170
173 GrBackendTexture rectangleTex = dContext->createBackendTexture(
175 if (!rectangleTex.isValid()) {
176 continue;
177 }
178
179 if (!dContext->updateBackendTexture(rectangleTex, &pm, 1, origin, nullptr, nullptr)) {
180 continue;
181 }
182
183 GrColor refPixels[kWidth * kHeight];
184 for (int y = 0; y < kHeight; ++y) {
185 for (int x = 0; x < kWidth; ++x) {
186 refPixels[y * kWidth + x] = pixels[y * kWidth + x];
187 }
188 }
189
190 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
192
193 if (!rectProxy) {
194 dContext->deleteBackendTexture(rectangleTex);
195 continue;
196 }
197
200
203 SkASSERT(rectProxy->hasRestrictedSampling());
205
206 GrImageInfo grII = ii;
207 skgpu::Swizzle swizzle = dContext->priv().caps()->getReadSwizzle(
208 rectangleTex.getBackendFormat(), grII.colorType());
209 GrSurfaceProxyView view(rectProxy, origin, swizzle);
210
211 test_basic_draw_as_src(reporter, dContext, view, grII.colorType(), kPremul_SkAlphaType,
212 refPixels);
213
214 // Test copy to both a texture and RT
215 TestCopyFromSurface(reporter, dContext, rectProxy, origin, grII.colorType(), refPixels,
216 "RectangleTexture-copy-from");
217
218 auto rectContext = dContext->priv().makeSC(std::move(view), grII.colorInfo());
219 SkASSERT(rectContext);
220
221 TestReadPixels(reporter, dContext, rectContext.get(), refPixels, "RectangleTexture-read");
222
223 test_copy_to_surface(reporter, dContext, rectContext.get(), "RectangleTexture-copy-to");
224
225 TestWritePixels(reporter, dContext, rectContext.get(), true, "RectangleTexture-write");
226
227 test_clear(reporter, dContext, rectContext.get());
228
229 dContext->deleteBackendTexture(rectangleTex);
230 }
231}
232#endif
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
reporter
Definition: FontMgrTest.cpp:39
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
#define GrColorUnpackA(color)
Definition: GrColor.h:62
static GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a)
Definition: GrColor.h:46
#define GrColorUnpackR(color)
Definition: GrColor.h:59
#define GrColorUnpackG(color)
Definition: GrColor.h:60
#define GrColorUnpackB(color)
Definition: GrColor.h:61
uint32_t GrColor
Definition: GrColor.h:25
#define GR_GL_RGBA8
Definition: GrGLDefines.h:524
#define GR_GL_TEXTURE_RECTANGLE
Definition: GrGLDefines.h:1056
@ kRW_GrIOType
Definition: GrTypesPriv.h:405
@ kBorrow_GrWrapOwnership
Definition: GrTypesPriv.h:79
GrColorType
Definition: GrTypesPriv.h:540
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
SkAlphaType
Definition: SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
static GrColor SkColorToPremulGrColor(SkColor c)
Definition: SkGr.h:51
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
void TestReadPixels(skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *srcContext, uint32_t expectedPixelValues[], const char *testName)
Definition: TestUtils.cpp:39
void TestWritePixels(skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *dstContext, bool expectedToWork, const char *testName)
Definition: TestUtils.cpp:66
void TestCopyFromSurface(skiatest::Reporter *reporter, GrDirectContext *dContext, sk_sp< GrSurfaceProxy > proxy, GrSurfaceOrigin origin, GrColorType colorType, uint32_t expectedPixelValues[], const char *testName)
Definition: TestUtils.cpp:98
#define DEF_GANESH_TEST_FOR_GL_CONTEXT(name, reporter, context_info, ctsEnforcement)
Definition: Test.h:442
GrBackendFormat getBackendFormat() const
bool isValid() const
const GrCaps * caps() const
skgpu::Swizzle getReadSwizzle(const GrBackendFormat &format, GrColorType colorType) const
Definition: GrCaps.cpp:443
bool updateBackendTexture(const GrBackendTexture &, const SkColor4f &color, GrGpuFinishedProc finishedProc, GrGpuFinishedContext finishedContext)
void deleteBackendTexture(const GrBackendTexture &)
GrBackendTexture createBackendTexture(int width, int height, const GrBackendFormat &, skgpu::Mipmapped, GrRenderable, GrProtected=GrProtected::kNo, std::string_view label={})
GrDirectContextPriv priv()
const GrColorInfo & colorInfo() const
Definition: GrImageInfo.h:42
GrColorType colorType() const
Definition: GrImageInfo.h:44
sk_sp< GrTextureProxy > wrapBackendTexture(const GrBackendTexture &, GrWrapOwnership, GrWrapCacheable, GrIOType, sk_sp< skgpu::RefCntedCallback >=nullptr)
GrProxyProvider * proxyProvider()
std::unique_ptr< skgpu::ganesh::SurfaceContext > makeSC(GrSurfaceProxyView readView, const GrColorInfo &)
std::unique_ptr< skgpu::ganesh::SurfaceFillContext > makeSFC(GrImageInfo, std::string_view label, SkBackingFit=SkBackingFit::kExact, int sampleCount=1, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Protected=skgpu::Protected::kNo, GrSurfaceOrigin=kTopLeft_GrSurfaceOrigin, skgpu::Budgeted=skgpu::Budgeted::kYes)
SkISize dimensions() const
GrTexture * peekTexture() const
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView, SkAlphaType, const SkMatrix &=SkMatrix::I(), GrSamplerState::Filter=GrSamplerState::Filter::kNearest, GrSamplerState::MipmapMode mipmapMode=GrSamplerState::MipmapMode::kNone)
GrTextureType textureType() const
bool hasRestrictedSampling() const
skgpu::Mipmapped mipmapped() const
skgpu::Mipmapped mipmapped() const
Definition: GrTexture.h:62
bool hasRestrictedSampling() const
Definition: GrTexture.h:56
GrTextureType textureType() const
Definition: GrTexture.h:55
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
virtual SurfaceFillContext * asFillContext()
GrSurfaceOrigin origin() const
uint32_t uint32_t * format
double y
double x
SK_API GrBackendFormat MakeGL(GrGLenum format, GrGLenum target)
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
const uint32_t fp
GrSurfaceProxyView MakeTextureProxyViewFromData(GrDirectContext *dContext, GrRenderable renderable, GrSurfaceOrigin origin, GrCPixmap pixmap)
Definition: ProxyUtils.cpp:56
SkScalar w
SkScalar h
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRGBA4f FromBytes_RGBA(uint32_t color)
constexpr size_t kHeight
constexpr size_t kWidth