Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ImageOriginTest.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
8#include "tests/Test.h"
9
12#include "include/core/SkRect.h"
21#include "tools/ToolUtils.h"
23
24namespace skgpu::graphite {
25
26namespace {
27
28using DrawFn = void (*)(sk_sp<SkImage>, SkCanvas*, SkRect /*srcRect*/, SkRect /*dstRect*/);
29
30constexpr SkColor4f kTopColor = SkColors::kRed;
31constexpr SkColor4f kBottomColor = SkColors::kBlue;
32constexpr int32_t kHalfSize = 4;
33constexpr SkISize kImageSize = {2*kHalfSize, 2*kHalfSize};
34
36 Context* context,
37 skgpu::Origin origin,
38 SkRect srcRect,
39 SkRect dstRect,
40 DrawFn drawImageFn) {
41 std::unique_ptr<Recorder> recorder = context->makeRecorder();
42
43 skgpu::Protected isProtected = skgpu::Protected(context->priv().caps()->protectedSupport());
44
47 0);
48 bitmap.eraseColor(kTopColor);
49 bitmap.erase(kBottomColor,
50 SkIRect::MakeLTRB(0, kHalfSize, kImageSize.width(), kImageSize.height()));
51
52 auto managedTexture =
53 sk_gpu_test::ManagedGraphiteTexture::MakeFromPixmap(recorder.get(),
54 bitmap.pixmap(),
55 skgpu::Mipmapped::kNo,
56 skgpu::Renderable::kNo,
57 isProtected);
58
59 REPORTER_ASSERT(reporter, managedTexture);
60 if (!managedTexture) {
61 return;
62 }
63
65 managedTexture->texture(),
68 /*colorSpace=*/nullptr,
69 origin);
70
72 if (!image) {
73 return;
74 }
75
77 recorder.get(),
79
81 if (!surface) {
82 return;
83 }
84
85 SkCanvas* canvas = surface->getCanvas();
86
87 drawImageFn(image, canvas, srcRect, dstRect);
88
89 SkPixmap pm;
90
93 bool peekPixelsSuccess = result.peekPixels(&pm);
94 REPORTER_ASSERT(reporter, peekPixelsSuccess);
95
96 bool readPixelsSuccess = surface->readPixels(pm, 0, 0);
97 REPORTER_ASSERT(reporter, readPixelsSuccess);
98
99 bool resultTopColorOnTop = origin == skgpu::Origin::kTopLeft;
100
101 for (int32_t y = 0; y < kImageSize.height(); ++y) {
102 for (int32_t x = 0; x < kImageSize.width(); ++x) {
104
105 SkColor4f expectedColor = ((y < kHalfSize) == resultTopColorOnTop) ? kTopColor
106 : kBottomColor;
108 color == expectedColor,
109 "At position {%d, %d}, "
110 "expected {%.1f, %.1f, %.1f, %.1f}, "
111 "found {%.1f, %.1f, %.1f, %.1f}",
112 x, y,
113 expectedColor.fR, expectedColor.fG, expectedColor.fB, expectedColor.fA,
114 color.fR, color.fG, color.fB, color.fA);
115 }
116 }
117}
118
119const SkRect kTestSrcRects[] = {
120 // entire thing
121 SkRect::MakeWH(kImageSize.width(), kImageSize.height()),
122 // half rect still splitting top and bottom colors
123 SkRect::MakeXYWH(2, 2, kHalfSize, kHalfSize),
124};
125
126void test_draw_fn(skiatest::Reporter* reporter,
127 Context* context,
128 DrawFn drawImageFn) {
130 for (auto srcRect: kTestSrcRects) {
132 context,
133 origin,
134 srcRect,
135 SkRect::MakeWH(kImageSize.width(), kImageSize.height()),
136 drawImageFn);
137 }
138 }
139}
140
142 SkCanvas* canvas,
143 SkRect srcRect,
144 SkRect dstRect) {
145 canvas->drawImageRect(image,
146 srcRect,
147 dstRect,
149 /*paint=*/nullptr,
151}
152
153void draw_image_with_shader(sk_sp<SkImage> image,
154 SkCanvas* canvas,
155 SkRect srcRect,
156 SkRect dstRect) {
157 SkPaint p;
158 SkMatrix srcToDst = SkMatrix::RectToRect(srcRect, dstRect);
160 std::move(image),
161 srcRect,
165 &srcToDst));
166 canvas->drawRect(dstRect, p);
167}
168
169} // anonymous namespace
170
171
172DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(ImageOriginTest_drawImage_Graphite, reporter, context,
174 test_draw_fn(reporter, context, draw_image);
175}
176
177DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(ImageOriginTest_imageShader_Graphite, reporter, context,
179 test_draw_fn(reporter, context, draw_image_with_shader);
180}
181
182} // namespace skgpu::graphite
reporter
static void draw_image(SkCanvas *canvas, SkImage *img)
SkColor4f color
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(name, reporter, graphite_context, ctsEnforcement)
Definition Test.h:377
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
void drawRect(const SkRect &rect, const SkPaint &paint)
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition SkCanvas.h:1542
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
static sk_sp< SkShader > MakeSubset(sk_sp< SkImage >, const SkRect &subset, SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix, bool clampAsIfUnpremul=false)
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
SkColor4f getColor4f(int x, int y) const
Definition SkPixmap.cpp:388
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
GAsyncResult * result
static const int kImageSize
Definition flippity.cpp:44
double y
double x
constexpr SkColor4f kRed
Definition SkColor.h:440
constexpr SkColor4f kBlue
Definition SkColor.h:442
SK_API sk_sp< SkImage > WrapTexture(skgpu::graphite::Recorder *, const skgpu::graphite::BackendTexture &, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, skgpu::Origin origin, GenerateMipmapsFromBase generateMipmapsFromBase, TextureReleaseProc=nullptr, ReleaseContext=nullptr)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
Protected
Definition GpuTypes.h:61
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition SkRect.h:91
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static void test_draw(SkCanvas *canvas, const char label[])
Definition surface.cpp:62