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
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSamplingOptions.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkTileMode.h"
22 #include "include/core/SkTypes.h"
23 #include "include/gpu/GpuTypes.h"
24 #include "include/gpu/GrDirectContext.h"
25 #include "tests/CtsEnforcement.h"
26 #include "tests/Test.h"
27 #include "tools/Resources.h"
28
29 #include <cstring>
30
31 class GrRecordingContext;
32 struct GrContextOptions;
33
test_bitmap_equality(skiatest::Reporter * reporter,SkBitmap & bm1,SkBitmap & bm2)34 static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
35 REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
36 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
37 }
38
paint_source(SkSurface * sourceSurface)39 static void paint_source(SkSurface* sourceSurface) {
40 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
41 sourceCanvas->clear(0xFFDEDEDE);
42
43 SkPaint paintColor;
44 paintColor.setColor(0xFFFF0000);
45 paintColor.setStyle(SkPaint::kFill_Style);
46
47 SkRect rect = SkRect::MakeXYWH(
48 SkIntToScalar(1),
49 SkIntToScalar(0),
50 SkIntToScalar(1),
51 SkIntToScalar(sourceSurface->height()));
52
53 sourceCanvas->drawRect(rect, paintColor);
54 }
55
run_shader_test(skiatest::Reporter * reporter,SkSurface * sourceSurface,SkSurface * destinationSurface,SkImageInfo & info)56 static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
57 SkSurface* destinationSurface, SkImageInfo& info) {
58 paint_source(sourceSurface);
59
60 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
61 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
62 SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
63
64 SkPaint paint;
65 paint.setShader(sourceShader);
66
67 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
68 destinationCanvas->clear(SK_ColorTRANSPARENT);
69 destinationCanvas->drawPaint(paint);
70
71 SkBitmap bmOrig;
72 bmOrig.allocN32Pixels(info.width(), info.height());
73 sourceSurface->readPixels(bmOrig, 0, 0);
74
75
76 SkBitmap bm;
77 bm.allocN32Pixels(info.width(), info.height());
78 destinationSurface->readPixels(bm, 0, 0);
79
80 test_bitmap_equality(reporter, bmOrig, bm);
81
82 // Test with a translated shader
83 SkMatrix matrix;
84 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
85
86 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
87 SkTileMode::kRepeat,
88 SkTileMode::kRepeat,
89 SkSamplingOptions(), &matrix);
90
91 destinationCanvas->clear(SK_ColorTRANSPARENT);
92
93 SkPaint paintTranslated;
94 paintTranslated.setShader(sourceShaderTranslated);
95
96 destinationCanvas->drawPaint(paintTranslated);
97
98 SkBitmap bmt;
99 bmt.allocN32Pixels(info.width(), info.height());
100 destinationSurface->readPixels(bmt, 0, 0);
101
102 // Test correctness
103 {
104 for (int y = 0; y < info.height(); y++) {
105 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
106
107 for (int x = 1; x < info.width(); x++) {
108 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
109 }
110 }
111 }
112 }
113
DEF_TEST(ImageNewShader,reporter)114 DEF_TEST(ImageNewShader, reporter) {
115 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
116
117 auto sourceSurface(SkSurface::MakeRaster(info));
118 auto destinationSurface(SkSurface::MakeRaster(info));
119
120 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
121 }
122
gpu_to_gpu(skiatest::Reporter * reporter,GrRecordingContext * rContext)123 static void gpu_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
124 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
125
126 auto sourceSurface(SkSurface::MakeRenderTarget(rContext, skgpu::Budgeted::kNo, info));
127 auto destinationSurface(SkSurface::MakeRenderTarget(rContext, skgpu::Budgeted::kNo, info));
128
129 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
130 }
131
raster_to_gpu(skiatest::Reporter * reporter,GrRecordingContext * rContext)132 static void raster_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
133 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
134
135 auto sourceSurface(SkSurface::MakeRaster(info));
136 auto destinationSurface(SkSurface::MakeRenderTarget(rContext, skgpu::Budgeted::kNo, info));
137
138 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
139 }
140
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)141 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU,
142 reporter,
143 ctxInfo,
144 CtsEnforcement::kApiLevel_T) {
145 auto dContext = ctxInfo.directContext();
146
147 // GPU -> GPU
148 gpu_to_gpu(reporter, dContext);
149
150 // GPU -> RASTER not currently supported
151
152 // RASTER -> GPU
153 raster_to_gpu(reporter, dContext);
154 }
155
DEF_TEST(ImageRawShader,reporter)156 DEF_TEST(ImageRawShader, reporter) {
157 auto image = GetResourceAsImage("images/mandrill_32.png");
158 REPORTER_ASSERT(reporter, image);
159
160 // We should be able to turn this into a "raw" image shader:
161 REPORTER_ASSERT(reporter, image->makeRawShader(SkSamplingOptions{}));
162
163 // ... but not if we request cubic filtering
164 REPORTER_ASSERT(reporter,
165 !image->makeRawShader(SkSamplingOptions{SkCubicResampler::Mitchell()}));
166 }
167