blob: 07142043154c8f9022e23cd26a5f17bd4d758a32 [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 Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#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"
Robert Phillips6d344c32020-07-06 10:56:46 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
piotaixrcef04f82014-07-14 07:48:04 -070016
Robert Phillips67c18d62017-01-20 12:44:06 -050017static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
Mike Reedf0ffb892017-10-03 14:47:21 -040018 REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
19 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
piotaixrcef04f82014-07-14 07:48:04 -070020}
21
Robert Phillips67c18d62017-01-20 12:44:06 -050022static void paint_source(SkSurface* sourceSurface) {
piotaixr76d5b472014-07-22 15:02:05 -070023 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
24 sourceCanvas->clear(0xFFDEDEDE);
piotaixrcef04f82014-07-14 07:48:04 -070025
piotaixr76d5b472014-07-22 15:02:05 -070026 SkPaint paintColor;
27 paintColor.setColor(0xFFFF0000);
28 paintColor.setStyle(SkPaint::kFill_Style);
29
30 SkRect rect = SkRect::MakeXYWH(
31 SkIntToScalar(1),
32 SkIntToScalar(0),
33 SkIntToScalar(1),
34 SkIntToScalar(sourceSurface->height()));
35
36 sourceCanvas->drawRect(rect, paintColor);
37}
38
Robert Phillips67c18d62017-01-20 12:44:06 -050039static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
40 SkSurface* destinationSurface, SkImageInfo& info) {
41 paint_source(sourceSurface);
piotaixr76d5b472014-07-22 15:02:05 -070042
reed9ce9d672016-03-17 10:51:11 -070043 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
reed5671c5b2016-03-09 14:47:34 -080044 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
Mike Reedb612b6c2020-12-08 21:58:35 -050045 SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
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,
Mike Reedb612b6c2020-12-08 21:58:35 -050072 SkSamplingOptions(), &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 Phillipsb25e0652020-07-22 09:35:49 -0400106static void gpu_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
piotaixrcef04f82014-07-14 07:48:04 -0700107 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
108
Robert Phillipsb25e0652020-07-22 09:35:49 -0400109 auto sourceSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
110 auto destinationSurface(SkSurface::MakeRenderTarget(rContext, 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 Phillipsb25e0652020-07-22 09:35:49 -0400115static void raster_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
piotaixrcef04f82014-07-14 07:48:04 -0700116 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
117
reede8f30622016-03-23 18:59:25 -0700118 auto sourceSurface(SkSurface::MakeRaster(info));
Robert Phillipsb25e0652020-07-22 09:35:49 -0400119 auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, 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
bsalomon68d91342016-04-12 09:59:58 -0700124DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400125 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -0400126
kkinnunen15302832015-12-01 04:35:26 -0800127 // GPU -> GPU
Robert Phillipsb25e0652020-07-22 09:35:49 -0400128 gpu_to_gpu(reporter, dContext);
piotaixrcef04f82014-07-14 07:48:04 -0700129
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400130 // GPU -> RASTER not currently supported
piotaixrcef04f82014-07-14 07:48:04 -0700131
kkinnunen15302832015-12-01 04:35:26 -0800132 // RASTER -> GPU
Robert Phillipsb25e0652020-07-22 09:35:49 -0400133 raster_to_gpu(reporter, dContext);
piotaixrcef04f82014-07-14 07:48:04 -0700134}