blob: f5c22f1e0aa07f3268019485b7b9765c847a2271 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkImage.h"
10#include "include/core/SkShader.h"
11#include "include/core/SkSurface.h"
12#include "include/core/SkTypes.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040013#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
piotaixrcef04f82014-07-14 07:48:04 -070015
Robert Phillips67c18d62017-01-20 12:44:06 -050016static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
Mike Reedf0ffb892017-10-03 14:47:21 -040017 REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
18 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
piotaixrcef04f82014-07-14 07:48:04 -070019}
20
Robert Phillips67c18d62017-01-20 12:44:06 -050021static void paint_source(SkSurface* sourceSurface) {
piotaixr76d5b472014-07-22 15:02:05 -070022 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
23 sourceCanvas->clear(0xFFDEDEDE);
piotaixrcef04f82014-07-14 07:48:04 -070024
piotaixr76d5b472014-07-22 15:02:05 -070025 SkPaint paintColor;
26 paintColor.setColor(0xFFFF0000);
27 paintColor.setStyle(SkPaint::kFill_Style);
28
29 SkRect rect = SkRect::MakeXYWH(
30 SkIntToScalar(1),
31 SkIntToScalar(0),
32 SkIntToScalar(1),
33 SkIntToScalar(sourceSurface->height()));
34
35 sourceCanvas->drawRect(rect, paintColor);
36}
37
Robert Phillips67c18d62017-01-20 12:44:06 -050038static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
39 SkSurface* destinationSurface, SkImageInfo& info) {
40 paint_source(sourceSurface);
piotaixr76d5b472014-07-22 15:02:05 -070041
reed9ce9d672016-03-17 10:51:11 -070042 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
reed5671c5b2016-03-09 14:47:34 -080043 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
Mike Reedfae8fce2019-04-03 10:27:45 -040044 SkTileMode::kRepeat,
45 SkTileMode::kRepeat);
piotaixrcef04f82014-07-14 07:48:04 -070046
47 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070048 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070049
piotaixr76d5b472014-07-22 15:02:05 -070050 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
51 destinationCanvas->clear(SK_ColorTRANSPARENT);
52 destinationCanvas->drawPaint(paint);
53
piotaixrcef04f82014-07-14 07:48:04 -070054 SkBitmap bmOrig;
Mike Reed12e946b2017-04-17 10:53:29 -040055 bmOrig.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040056 sourceSurface->readPixels(bmOrig, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070057
piotaixrcef04f82014-07-14 07:48:04 -070058
59 SkBitmap bm;
Mike Reed12e946b2017-04-17 10:53:29 -040060 bm.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040061 destinationSurface->readPixels(bm, 0, 0);
piotaixrcef04f82014-07-14 07:48:04 -070062
Robert Phillips67c18d62017-01-20 12:44:06 -050063 test_bitmap_equality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070064
65 // Test with a translated shader
66 SkMatrix matrix;
67 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
68
reed5671c5b2016-03-09 14:47:34 -080069 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
Mike Reedfae8fce2019-04-03 10:27:45 -040070 SkTileMode::kRepeat,
71 SkTileMode::kRepeat,
reed5671c5b2016-03-09 14:47:34 -080072 &matrix);
piotaixr76d5b472014-07-22 15:02:05 -070073
74 destinationCanvas->clear(SK_ColorTRANSPARENT);
75
76 SkPaint paintTranslated;
77 paintTranslated.setShader(sourceShaderTranslated);
78
79 destinationCanvas->drawPaint(paintTranslated);
80
81 SkBitmap bmt;
Mike Reed12e946b2017-04-17 10:53:29 -040082 bmt.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040083 destinationSurface->readPixels(bmt, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070084
85 // Test correctness
86 {
piotaixr76d5b472014-07-22 15:02:05 -070087 for (int y = 0; y < info.height(); y++) {
88 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
89
90 for (int x = 1; x < info.width(); x++) {
91 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
92 }
93 }
94 }
piotaixrcef04f82014-07-14 07:48:04 -070095}
96
97DEF_TEST(ImageNewShader, reporter) {
98 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
99
reede8f30622016-03-23 18:59:25 -0700100 auto sourceSurface(SkSurface::MakeRaster(info));
101 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700102
Robert Phillips67c18d62017-01-20 12:44:06 -0500103 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700104}
105
Robert Phillips67c18d62017-01-20 12:44:06 -0500106static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700107 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
108
reede8f30622016-03-23 18:59:25 -0700109 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
110 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700111
Robert Phillips67c18d62017-01-20 12:44:06 -0500112 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700113}
114
Robert Phillips67c18d62017-01-20 12:44:06 -0500115static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700116 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::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700120
Robert Phillips67c18d62017-01-20 12:44:06 -0500121 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700122}
123
Robert Phillips67c18d62017-01-20 12:44:06 -0500124static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700125 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
126
reede8f30622016-03-23 18:59:25 -0700127 auto sourceSurface(SkSurface::MakeRaster(info));
128 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700129
Robert Phillips67c18d62017-01-20 12:44:06 -0500130 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700131}
132
bsalomon68d91342016-04-12 09:59:58 -0700133DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400134 auto context = ctxInfo.directContext();
135
kkinnunen15302832015-12-01 04:35:26 -0800136 // GPU -> GPU
Robert Phillips6d344c32020-07-06 10:56:46 -0400137 gpu_to_gpu(reporter, context);
piotaixrcef04f82014-07-14 07:48:04 -0700138
kkinnunen15302832015-12-01 04:35:26 -0800139 // GPU -> RASTER
Robert Phillips6d344c32020-07-06 10:56:46 -0400140 gpu_to_raster(reporter, context);
piotaixrcef04f82014-07-14 07:48:04 -0700141
kkinnunen15302832015-12-01 04:35:26 -0800142 // RASTER -> GPU
Robert Phillips6d344c32020-07-06 10:56:46 -0400143 raster_to_gpu(reporter, context);
piotaixrcef04f82014-07-14 07:48:04 -0700144}