blob: 6d262d2c7fe48ba749e392f48670b73f741db48c [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 Reedb612b6c2020-12-08 21:58:35 -050044 SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
piotaixrcef04f82014-07-14 07:48:04 -070045
46 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070047 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070048
piotaixr76d5b472014-07-22 15:02:05 -070049 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
50 destinationCanvas->clear(SK_ColorTRANSPARENT);
51 destinationCanvas->drawPaint(paint);
52
piotaixrcef04f82014-07-14 07:48:04 -070053 SkBitmap bmOrig;
Mike Reed12e946b2017-04-17 10:53:29 -040054 bmOrig.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040055 sourceSurface->readPixels(bmOrig, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070056
piotaixrcef04f82014-07-14 07:48:04 -070057
58 SkBitmap bm;
Mike Reed12e946b2017-04-17 10:53:29 -040059 bm.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040060 destinationSurface->readPixels(bm, 0, 0);
piotaixrcef04f82014-07-14 07:48:04 -070061
Robert Phillips67c18d62017-01-20 12:44:06 -050062 test_bitmap_equality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070063
64 // Test with a translated shader
65 SkMatrix matrix;
66 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
67
reed5671c5b2016-03-09 14:47:34 -080068 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
Mike Reedfae8fce2019-04-03 10:27:45 -040069 SkTileMode::kRepeat,
70 SkTileMode::kRepeat,
Mike Reedb612b6c2020-12-08 21:58:35 -050071 SkSamplingOptions(), &matrix);
piotaixr76d5b472014-07-22 15:02:05 -070072
73 destinationCanvas->clear(SK_ColorTRANSPARENT);
74
75 SkPaint paintTranslated;
76 paintTranslated.setShader(sourceShaderTranslated);
77
78 destinationCanvas->drawPaint(paintTranslated);
79
80 SkBitmap bmt;
Mike Reed12e946b2017-04-17 10:53:29 -040081 bmt.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040082 destinationSurface->readPixels(bmt, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070083
84 // Test correctness
85 {
piotaixr76d5b472014-07-22 15:02:05 -070086 for (int y = 0; y < info.height(); y++) {
87 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
88
89 for (int x = 1; x < info.width(); x++) {
90 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
91 }
92 }
93 }
piotaixrcef04f82014-07-14 07:48:04 -070094}
95
96DEF_TEST(ImageNewShader, reporter) {
97 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
98
reede8f30622016-03-23 18:59:25 -070099 auto sourceSurface(SkSurface::MakeRaster(info));
100 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700101
Robert Phillips67c18d62017-01-20 12:44:06 -0500102 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700103}
104
Robert Phillipsb25e0652020-07-22 09:35:49 -0400105static void gpu_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
piotaixrcef04f82014-07-14 07:48:04 -0700106 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
107
Robert Phillipsb25e0652020-07-22 09:35:49 -0400108 auto sourceSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
109 auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700110
Robert Phillips67c18d62017-01-20 12:44:06 -0500111 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700112}
113
Robert Phillipsb25e0652020-07-22 09:35:49 -0400114static void raster_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
piotaixrcef04f82014-07-14 07:48:04 -0700115 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
116
reede8f30622016-03-23 18:59:25 -0700117 auto sourceSurface(SkSurface::MakeRaster(info));
Robert Phillipsb25e0652020-07-22 09:35:49 -0400118 auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700119
Robert Phillips67c18d62017-01-20 12:44:06 -0500120 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700121}
122
bsalomon68d91342016-04-12 09:59:58 -0700123DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400124 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -0400125
kkinnunen15302832015-12-01 04:35:26 -0800126 // GPU -> GPU
Robert Phillipsb25e0652020-07-22 09:35:49 -0400127 gpu_to_gpu(reporter, dContext);
piotaixrcef04f82014-07-14 07:48:04 -0700128
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400129 // GPU -> RASTER not currently supported
piotaixrcef04f82014-07-14 07:48:04 -0700130
kkinnunen15302832015-12-01 04:35:26 -0800131 // RASTER -> GPU
Robert Phillipsb25e0652020-07-22 09:35:49 -0400132 raster_to_gpu(reporter, dContext);
piotaixrcef04f82014-07-14 07:48:04 -0700133}