blob: a92a4be4d56aee4e78da36b440f0e86fa90db01a [file] [log] [blame]
piotaixrcef04f82014-07-14 07:48:04 -07001/*
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
piotaixrcef04f82014-07-14 07:48:04 -07008#include "SkCanvas.h"
9#include "SkImage.h"
10#include "SkShader.h"
11#include "SkSurface.h"
kkinnunen15302832015-12-01 04:35:26 -080012#include "SkTypes.h"
piotaixrcef04f82014-07-14 07:48:04 -070013#include "Test.h"
14
kkinnunen15302832015-12-01 04:35:26 -080015#if SK_SUPPORT_GPU
16#include "GrContext.h"
17#endif
18
piotaixrcef04f82014-07-14 07:48:04 -070019void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
piotaixr76d5b472014-07-22 15:02:05 -070020 SkAutoLockPixels lockBm1(bm1);
21 SkAutoLockPixels lockBm2(bm2);
piotaixrcef04f82014-07-14 07:48:04 -070022
23 REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
24 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
piotaixrcef04f82014-07-14 07:48:04 -070025}
26
piotaixr76d5b472014-07-22 15:02:05 -070027void paintSource(SkSurface* sourceSurface) {
28 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
29 sourceCanvas->clear(0xFFDEDEDE);
piotaixrcef04f82014-07-14 07:48:04 -070030
piotaixr76d5b472014-07-22 15:02:05 -070031 SkPaint paintColor;
32 paintColor.setColor(0xFFFF0000);
33 paintColor.setStyle(SkPaint::kFill_Style);
34
35 SkRect rect = SkRect::MakeXYWH(
36 SkIntToScalar(1),
37 SkIntToScalar(0),
38 SkIntToScalar(1),
39 SkIntToScalar(sourceSurface->height()));
40
41 sourceCanvas->drawRect(rect, paintColor);
42}
43
44void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSurface* destinationSurface, SkImageInfo& info) {
45 paintSource(sourceSurface);
46
reed9ce9d672016-03-17 10:51:11 -070047 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
reed5671c5b2016-03-09 14:47:34 -080048 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
piotaixrcef04f82014-07-14 07:48:04 -070049 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080050 SkShader::kRepeat_TileMode);
piotaixrcef04f82014-07-14 07:48:04 -070051
52 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070053 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070054
piotaixr76d5b472014-07-22 15:02:05 -070055 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
56 destinationCanvas->clear(SK_ColorTRANSPARENT);
57 destinationCanvas->drawPaint(paint);
58
halcanaryf622a6c2014-10-24 12:54:53 -070059 SkIRect rect = info.bounds();
piotaixrcef04f82014-07-14 07:48:04 -070060
61 SkBitmap bmOrig;
piotaixr76d5b472014-07-22 15:02:05 -070062 sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
63
piotaixrcef04f82014-07-14 07:48:04 -070064
65 SkBitmap bm;
piotaixr76d5b472014-07-22 15:02:05 -070066 destinationCanvas->readPixels(rect, &bm);
piotaixrcef04f82014-07-14 07:48:04 -070067
68 testBitmapEquality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070069
70
71
72 // Test with a translated shader
73 SkMatrix matrix;
74 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
75
reed5671c5b2016-03-09 14:47:34 -080076 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
piotaixr76d5b472014-07-22 15:02:05 -070077 SkShader::kRepeat_TileMode,
78 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080079 &matrix);
piotaixr76d5b472014-07-22 15:02:05 -070080
81 destinationCanvas->clear(SK_ColorTRANSPARENT);
82
83 SkPaint paintTranslated;
84 paintTranslated.setShader(sourceShaderTranslated);
85
86 destinationCanvas->drawPaint(paintTranslated);
87
88 SkBitmap bmt;
89 destinationCanvas->readPixels(rect, &bmt);
90
91 // Test correctness
92 {
93 SkAutoLockPixels lockBm(bmt);
94 for (int y = 0; y < info.height(); y++) {
95 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
96
97 for (int x = 1; x < info.width(); x++) {
98 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
99 }
100 }
101 }
piotaixrcef04f82014-07-14 07:48:04 -0700102}
103
104DEF_TEST(ImageNewShader, reporter) {
105 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
106
reede8f30622016-03-23 18:59:25 -0700107 auto sourceSurface(SkSurface::MakeRaster(info));
108 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700109
piotaixr76d5b472014-07-22 15:02:05 -0700110 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700111}
112
113#if SK_SUPPORT_GPU
114
115void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
116 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
117
reede8f30622016-03-23 18:59:25 -0700118 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
119 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700120
piotaixr76d5b472014-07-22 15:02:05 -0700121 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700122}
123
124void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
125 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
126
reede8f30622016-03-23 18:59:25 -0700127 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
128 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700129
piotaixr76d5b472014-07-22 15:02:05 -0700130 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700131}
132
133void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
134 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
135
reede8f30622016-03-23 18:59:25 -0700136 auto sourceSurface(SkSurface::MakeRaster(info));
137 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700138
piotaixr76d5b472014-07-22 15:02:05 -0700139 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700140}
141
bsalomon68d91342016-04-12 09:59:58 -0700142DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800143 // GPU -> GPU
bsalomon8b7451a2016-05-11 06:33:06 -0700144 gpuToGpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700145
kkinnunen15302832015-12-01 04:35:26 -0800146 // GPU -> RASTER
bsalomon8b7451a2016-05-11 06:33:06 -0700147 gpuToRaster(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700148
kkinnunen15302832015-12-01 04:35:26 -0800149 // RASTER -> GPU
bsalomon8b7451a2016-05-11 06:33:06 -0700150 rasterToGpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700151}
152
153#endif