blob: 092a42227c5c5b758d151f1530618d6d192eb449 [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#include "GrContext.h"
kkinnunen15302832015-12-01 04:35:26 -080016
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(
piotaixrcef04f82014-07-14 07:48:04 -070045 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080046 SkShader::kRepeat_TileMode);
piotaixrcef04f82014-07-14 07:48:04 -070047
48 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070049 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070050
piotaixr76d5b472014-07-22 15:02:05 -070051 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
52 destinationCanvas->clear(SK_ColorTRANSPARENT);
53 destinationCanvas->drawPaint(paint);
54
piotaixrcef04f82014-07-14 07:48:04 -070055 SkBitmap bmOrig;
Mike Reed12e946b2017-04-17 10:53:29 -040056 bmOrig.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040057 sourceSurface->readPixels(bmOrig, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070058
piotaixrcef04f82014-07-14 07:48:04 -070059
60 SkBitmap bm;
Mike Reed12e946b2017-04-17 10:53:29 -040061 bm.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040062 destinationSurface->readPixels(bm, 0, 0);
piotaixrcef04f82014-07-14 07:48:04 -070063
Robert Phillips67c18d62017-01-20 12:44:06 -050064 test_bitmap_equality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070065
66 // Test with a translated shader
67 SkMatrix matrix;
68 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
69
reed5671c5b2016-03-09 14:47:34 -080070 sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
piotaixr76d5b472014-07-22 15:02:05 -070071 SkShader::kRepeat_TileMode,
72 SkShader::kRepeat_TileMode,
reed5671c5b2016-03-09 14:47:34 -080073 &matrix);
piotaixr76d5b472014-07-22 15:02:05 -070074
75 destinationCanvas->clear(SK_ColorTRANSPARENT);
76
77 SkPaint paintTranslated;
78 paintTranslated.setShader(sourceShaderTranslated);
79
80 destinationCanvas->drawPaint(paintTranslated);
81
82 SkBitmap bmt;
Mike Reed12e946b2017-04-17 10:53:29 -040083 bmt.allocN32Pixels(info.width(), info.height());
Mike Reedf1942192017-07-21 14:24:29 -040084 destinationSurface->readPixels(bmt, 0, 0);
piotaixr76d5b472014-07-22 15:02:05 -070085
86 // Test correctness
87 {
piotaixr76d5b472014-07-22 15:02:05 -070088 for (int y = 0; y < info.height(); y++) {
89 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
90
91 for (int x = 1; x < info.width(); x++) {
92 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
93 }
94 }
95 }
piotaixrcef04f82014-07-14 07:48:04 -070096}
97
98DEF_TEST(ImageNewShader, reporter) {
99 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
100
reede8f30622016-03-23 18:59:25 -0700101 auto sourceSurface(SkSurface::MakeRaster(info));
102 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700103
Robert Phillips67c18d62017-01-20 12:44:06 -0500104 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700105}
106
Robert Phillips67c18d62017-01-20 12:44:06 -0500107static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700108 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
109
reede8f30622016-03-23 18:59:25 -0700110 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
111 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700112
Robert Phillips67c18d62017-01-20 12:44:06 -0500113 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700114}
115
Robert Phillips67c18d62017-01-20 12:44:06 -0500116static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700117 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
118
reede8f30622016-03-23 18:59:25 -0700119 auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
120 auto destinationSurface(SkSurface::MakeRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700121
Robert Phillips67c18d62017-01-20 12:44:06 -0500122 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700123}
124
Robert Phillips67c18d62017-01-20 12:44:06 -0500125static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
piotaixrcef04f82014-07-14 07:48:04 -0700126 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
127
reede8f30622016-03-23 18:59:25 -0700128 auto sourceSurface(SkSurface::MakeRaster(info));
129 auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
piotaixrcef04f82014-07-14 07:48:04 -0700130
Robert Phillips67c18d62017-01-20 12:44:06 -0500131 run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700132}
133
bsalomon68d91342016-04-12 09:59:58 -0700134DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800135 // GPU -> GPU
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 gpu_to_gpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700137
kkinnunen15302832015-12-01 04:35:26 -0800138 // GPU -> RASTER
Robert Phillips67c18d62017-01-20 12:44:06 -0500139 gpu_to_raster(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700140
kkinnunen15302832015-12-01 04:35:26 -0800141 // RASTER -> GPU
Robert Phillips67c18d62017-01-20 12:44:06 -0500142 raster_to_gpu(reporter, ctxInfo.grContext());
piotaixrcef04f82014-07-14 07:48:04 -0700143}