blob: 4091db0d9b86ef155d5b3682d905d8c7484958c6 [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) {
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
Robert Phillips67c18d62017-01-20 12:44:06 -050027static void paint_source(SkSurface* sourceSurface) {
piotaixr76d5b472014-07-22 15:02:05 -070028 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
Robert Phillips67c18d62017-01-20 12:44:06 -050044static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
45 SkSurface* destinationSurface, SkImageInfo& info) {
46 paint_source(sourceSurface);
piotaixr76d5b472014-07-22 15:02:05 -070047
reed9ce9d672016-03-17 10:51:11 -070048 sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
reed5671c5b2016-03-09 14:47:34 -080049 sk_sp<SkShader> sourceShader = sourceImage->makeShader(
piotaixrcef04f82014-07-14 07:48:04 -070050 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080051 SkShader::kRepeat_TileMode);
piotaixrcef04f82014-07-14 07:48:04 -070052
53 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070054 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070055
piotaixr76d5b472014-07-22 15:02:05 -070056 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
57 destinationCanvas->clear(SK_ColorTRANSPARENT);
58 destinationCanvas->drawPaint(paint);
59
halcanaryf622a6c2014-10-24 12:54:53 -070060 SkIRect rect = info.bounds();
piotaixrcef04f82014-07-14 07:48:04 -070061
62 SkBitmap bmOrig;
piotaixr76d5b472014-07-22 15:02:05 -070063 sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
64
piotaixrcef04f82014-07-14 07:48:04 -070065
66 SkBitmap bm;
piotaixr76d5b472014-07-22 15:02:05 -070067 destinationCanvas->readPixels(rect, &bm);
piotaixrcef04f82014-07-14 07:48:04 -070068
Robert Phillips67c18d62017-01-20 12:44:06 -050069 test_bitmap_equality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070070
71 // Test with a translated shader
72 SkMatrix matrix;
73 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
74
reed5671c5b2016-03-09 14:47:34 -080075 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
piotaixr76d5b472014-07-22 15:02:05 -070076 SkShader::kRepeat_TileMode,
77 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080078 &matrix);
piotaixr76d5b472014-07-22 15:02:05 -070079
80 destinationCanvas->clear(SK_ColorTRANSPARENT);
81
82 SkPaint paintTranslated;
83 paintTranslated.setShader(sourceShaderTranslated);
84
85 destinationCanvas->drawPaint(paintTranslated);
86
87 SkBitmap bmt;
88 destinationCanvas->readPixels(rect, &bmt);
89
90 // Test correctness
91 {
92 SkAutoLockPixels lockBm(bmt);
93 for (int y = 0; y < info.height(); y++) {
94 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
95
96 for (int x = 1; x < info.width(); x++) {
97 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
98 }
99 }
100 }
piotaixrcef04f82014-07-14 07:48:04 -0700101}
102
103DEF_TEST(ImageNewShader, reporter) {
104 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
105
reede8f30622016-03-23 18:59:25 -0700106 auto sourceSurface(SkSurface::MakeRaster(info));
107 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700108
Robert Phillips67c18d62017-01-20 12:44:06 -0500109 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700110}
111
112#if SK_SUPPORT_GPU
113
Robert Phillips67c18d62017-01-20 12:44:06 -0500114static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700115 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
116
reede8f30622016-03-23 18:59:25 -0700117 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
118 auto destinationSurface(SkSurface::MakeRenderTarget(context, 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
Robert Phillips67c18d62017-01-20 12:44:06 -0500123static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700124 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
125
reede8f30622016-03-23 18:59:25 -0700126 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
127 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700128
Robert Phillips67c18d62017-01-20 12:44:06 -0500129 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700130}
131
Robert Phillips67c18d62017-01-20 12:44:06 -0500132static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700133 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
134
reede8f30622016-03-23 18:59:25 -0700135 auto sourceSurface(SkSurface::MakeRaster(info));
136 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700137
Robert Phillips67c18d62017-01-20 12:44:06 -0500138 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700139}
140
bsalomon68d91342016-04-12 09:59:58 -0700141DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800142 // GPU -> GPU
Robert Phillips67c18d62017-01-20 12:44:06 -0500143 gpu_to_gpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700144
kkinnunen15302832015-12-01 04:35:26 -0800145 // GPU -> RASTER
Robert Phillips67c18d62017-01-20 12:44:06 -0500146 gpu_to_raster(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700147
kkinnunen15302832015-12-01 04:35:26 -0800148 // RASTER -> GPU
Robert Phillips67c18d62017-01-20 12:44:06 -0500149 raster_to_gpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700150}
151
152#endif