blob: ff80729d7b90ea625639f3498c6f74a5454c5c64 [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
Robert Phillips67c18d62017-01-20 12:44:06 -050019static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
Mike Reed98a62162017-09-26 12:47:08 -040020 REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
21 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
piotaixrcef04f82014-07-14 07:48:04 -070022}
23
Robert Phillips67c18d62017-01-20 12:44:06 -050024static void paint_source(SkSurface* sourceSurface) {
piotaixr76d5b472014-07-22 15:02:05 -070025 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
26 sourceCanvas->clear(0xFFDEDEDE);
piotaixrcef04f82014-07-14 07:48:04 -070027
piotaixr76d5b472014-07-22 15:02:05 -070028 SkPaint paintColor;
29 paintColor.setColor(0xFFFF0000);
30 paintColor.setStyle(SkPaint::kFill_Style);
31
32 SkRect rect = SkRect::MakeXYWH(
33 SkIntToScalar(1),
34 SkIntToScalar(0),
35 SkIntToScalar(1),
36 SkIntToScalar(sourceSurface->height()));
37
38 sourceCanvas->drawRect(rect, paintColor);
39}
40
Robert Phillips67c18d62017-01-20 12:44:06 -050041static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
42 SkSurface* destinationSurface, SkImageInfo& info) {
43 paint_source(sourceSurface);
piotaixr76d5b472014-07-22 15:02:05 -070044
reed9ce9d672016-03-17 10:51:11 -070045 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
reed5671c5b2016-03-09 14:47:34 -080046 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
piotaixrcef04f82014-07-14 07:48:04 -070047 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080048 SkShader::kRepeat_TileMode);
piotaixrcef04f82014-07-14 07:48:04 -070049
50 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070051 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070052
piotaixr76d5b472014-07-22 15:02:05 -070053 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
54 destinationCanvas->clear(SK_ColorTRANSPARENT);
55 destinationCanvas->drawPaint(paint);
56
piotaixrcef04f82014-07-14 07:48:04 -070057 SkBitmap bmOrig;
Mike Reed12e946b2017-04-17 10:53:29 -040058 bmOrig.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040059 sourceSurface->readPixels(bmOrig, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070060
piotaixrcef04f82014-07-14 07:48:04 -070061
62 SkBitmap bm;
Mike Reed12e946b2017-04-17 10:53:29 -040063 bm.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040064 destinationSurface->readPixels(bm, 0, 0);
piotaixrcef04f82014-07-14 07:48:04 -070065
Robert Phillips67c18d62017-01-20 12:44:06 -050066 test_bitmap_equality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070067
68 // Test with a translated shader
69 SkMatrix matrix;
70 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
71
reed5671c5b2016-03-09 14:47:34 -080072 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
piotaixr76d5b472014-07-22 15:02:05 -070073 SkShader::kRepeat_TileMode,
74 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080075 &matrix);
piotaixr76d5b472014-07-22 15:02:05 -070076
77 destinationCanvas->clear(SK_ColorTRANSPARENT);
78
79 SkPaint paintTranslated;
80 paintTranslated.setShader(sourceShaderTranslated);
81
82 destinationCanvas->drawPaint(paintTranslated);
83
84 SkBitmap bmt;
Mike Reed12e946b2017-04-17 10:53:29 -040085 bmt.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040086 destinationSurface->readPixels(bmt, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070087
88 // Test correctness
89 {
piotaixr76d5b472014-07-22 15:02:05 -070090 for (int y = 0; y < info.height(); y++) {
91 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
92
93 for (int x = 1; x < info.width(); x++) {
94 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
95 }
96 }
97 }
piotaixrcef04f82014-07-14 07:48:04 -070098}
99
100DEF_TEST(ImageNewShader, reporter) {
101 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
102
reede8f30622016-03-23 18:59:25 -0700103 auto sourceSurface(SkSurface::MakeRaster(info));
104 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700105
Robert Phillips67c18d62017-01-20 12:44:06 -0500106 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700107}
108
109#if SK_SUPPORT_GPU
110
Robert Phillips67c18d62017-01-20 12:44:06 -0500111static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700112 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
113
reede8f30622016-03-23 18:59:25 -0700114 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
115 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700116
Robert Phillips67c18d62017-01-20 12:44:06 -0500117 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700118}
119
Robert Phillips67c18d62017-01-20 12:44:06 -0500120static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700121 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
122
reede8f30622016-03-23 18:59:25 -0700123 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
124 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700125
Robert Phillips67c18d62017-01-20 12:44:06 -0500126 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700127}
128
Robert Phillips67c18d62017-01-20 12:44:06 -0500129static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700130 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
131
reede8f30622016-03-23 18:59:25 -0700132 auto sourceSurface(SkSurface::MakeRaster(info));
133 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700134
Robert Phillips67c18d62017-01-20 12:44:06 -0500135 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700136}
137
bsalomon68d91342016-04-12 09:59:58 -0700138DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800139 // GPU -> GPU
Robert Phillips67c18d62017-01-20 12:44:06 -0500140 gpu_to_gpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700141
kkinnunen15302832015-12-01 04:35:26 -0800142 // GPU -> RASTER
Robert Phillips67c18d62017-01-20 12:44:06 -0500143 gpu_to_raster(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700144
kkinnunen15302832015-12-01 04:35:26 -0800145 // RASTER -> GPU
Robert Phillips67c18d62017-01-20 12:44:06 -0500146 raster_to_gpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700147}
148
149#endif