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