Flutter Engine
The Flutter Engine
ImageNewShaderTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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
15#include "include/core/SkRect.h"
27#include "tests/Test.h"
28#include "tools/DecodeUtils.h"
29#include "tools/Resources.h"
30
31#include <cstring>
32
34struct GrContextOptions;
35
38 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
39}
40
41static void paint_source(SkSurface* sourceSurface) {
42 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
43 sourceCanvas->clear(0xFFDEDEDE);
44
45 SkPaint paintColor;
46 paintColor.setColor(0xFFFF0000);
48
53 SkIntToScalar(sourceSurface->height()));
54
55 sourceCanvas->drawRect(rect, paintColor);
56}
57
59 SkSurface* destinationSurface, SkImageInfo& info) {
60 paint_source(sourceSurface);
61
62 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
63 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
65
67 paint.setShader(sourceShader);
68
69 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
70 destinationCanvas->clear(SK_ColorTRANSPARENT);
71 destinationCanvas->drawPaint(paint);
72
73 SkBitmap bmOrig;
74 bmOrig.allocN32Pixels(info.width(), info.height());
75 sourceSurface->readPixels(bmOrig, 0, 0);
76
77
78 SkBitmap bm;
79 bm.allocN32Pixels(info.width(), info.height());
80 destinationSurface->readPixels(bm, 0, 0);
81
82 test_bitmap_equality(reporter, bmOrig, bm);
83
84 // Test with a translated shader
86 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
87
88 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
92
93 destinationCanvas->clear(SK_ColorTRANSPARENT);
94
95 SkPaint paintTranslated;
96 paintTranslated.setShader(sourceShaderTranslated);
97
98 destinationCanvas->drawPaint(paintTranslated);
99
100 SkBitmap bmt;
101 bmt.allocN32Pixels(info.width(), info.height());
102 destinationSurface->readPixels(bmt, 0, 0);
103
104 // Test correctness
105 {
106 for (int y = 0; y < info.height(); y++) {
107 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
108
109 for (int x = 1; x < info.width(); x++) {
110 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
111 }
112 }
113 }
114}
115
116DEF_TEST(ImageNewShader, reporter) {
118
119 auto sourceSurface(SkSurfaces::Raster(info));
120 auto destinationSurface(SkSurfaces::Raster(info));
121
122 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
123}
124
127
128 auto sourceSurface(SkSurfaces::RenderTarget(rContext, skgpu::Budgeted::kNo, info));
129 auto destinationSurface(SkSurfaces::RenderTarget(rContext, skgpu::Budgeted::kNo, info));
130
131 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
132}
133
136
137 auto sourceSurface(SkSurfaces::Raster(info));
138 auto destinationSurface(SkSurfaces::RenderTarget(rContext, skgpu::Budgeted::kNo, info));
139
140 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
141}
142
144 reporter,
145 ctxInfo,
147 auto dContext = ctxInfo.directContext();
148
149 // GPU -> GPU
150 gpu_to_gpu(reporter, dContext);
151
152 // GPU -> RASTER not currently supported
153
154 // RASTER -> GPU
155 raster_to_gpu(reporter, dContext);
156}
157
158DEF_TEST(ImageRawShader, reporter) {
159 auto image = ToolUtils::GetResourceAsImage("images/mandrill_32.png");
161
162 // We should be able to turn this into a "raw" image shader:
164
165 // ... but not if we request cubic filtering
167}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
reporter
Definition: FontMgrTest.cpp:39
static void run_shader_test(skiatest::Reporter *reporter, SkSurface *sourceSurface, SkSurface *destinationSurface, SkImageInfo &info)
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
static void gpu_to_gpu(skiatest::Reporter *reporter, GrRecordingContext *rContext)
static void paint_source(SkSurface *sourceSurface)
static void test_bitmap_equality(skiatest::Reporter *reporter, SkBitmap &bm1, SkBitmap &bm2)
static void raster_to_gpu(skiatest::Reporter *reporter, GrRecordingContext *rContext)
DEF_TEST(ImageNewShader, reporter)
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
SkColor getColor(int x, int y) const
Definition: SkBitmap.h:874
size_t computeByteSize() const
Definition: SkBitmap.h:293
void * getPixels() const
Definition: SkBitmap.h:283
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
void clear(SkColor color)
Definition: SkCanvas.h:1199
sk_sp< SkShader > makeRawShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:207
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
void setShader(sk_sp< SkShader > shader)
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
int height() const
Definition: SkSurface.h:184
bool readPixels(const SkPixmap &dst, int srcX, int srcY)
Definition: SkSurface.cpp:125
const Paint & paint
Definition: color_source.cc:38
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
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)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr SkCubicResampler Mitchell()
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659