piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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 Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 15 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 16 | static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) { |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 17 | REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize()); |
| 18 | REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize())); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 21 | static void paint_source(SkSurface* sourceSurface) { |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 22 | SkCanvas* sourceCanvas = sourceSurface->getCanvas(); |
| 23 | sourceCanvas->clear(0xFFDEDEDE); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 24 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 25 | 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 Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 38 | static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface, |
| 39 | SkSurface* destinationSurface, SkImageInfo& info) { |
| 40 | paint_source(sourceSurface); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 41 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 42 | sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot()); |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 43 | sk_sp<SkShader> sourceShader = sourceImage->makeShader( |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 44 | SkTileMode::kRepeat, |
| 45 | SkTileMode::kRepeat); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 46 | |
| 47 | SkPaint paint; |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 48 | paint.setShader(sourceShader); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 49 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 50 | SkCanvas* destinationCanvas = destinationSurface->getCanvas(); |
| 51 | destinationCanvas->clear(SK_ColorTRANSPARENT); |
| 52 | destinationCanvas->drawPaint(paint); |
| 53 | |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 54 | SkBitmap bmOrig; |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 55 | bmOrig.allocN32Pixels(info.width(), info.height()); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 56 | sourceSurface->readPixels(bmOrig, 0, 0); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 57 | |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 58 | |
| 59 | SkBitmap bm; |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 60 | bm.allocN32Pixels(info.width(), info.height()); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 61 | destinationSurface->readPixels(bm, 0, 0); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 62 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 63 | test_bitmap_equality(reporter, bmOrig, bm); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 64 | |
| 65 | // Test with a translated shader |
| 66 | SkMatrix matrix; |
| 67 | matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0)); |
| 68 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 69 | sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader( |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 70 | SkTileMode::kRepeat, |
| 71 | SkTileMode::kRepeat, |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 72 | &matrix); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 73 | |
| 74 | destinationCanvas->clear(SK_ColorTRANSPARENT); |
| 75 | |
| 76 | SkPaint paintTranslated; |
| 77 | paintTranslated.setShader(sourceShaderTranslated); |
| 78 | |
| 79 | destinationCanvas->drawPaint(paintTranslated); |
| 80 | |
| 81 | SkBitmap bmt; |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 82 | bmt.allocN32Pixels(info.width(), info.height()); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 83 | destinationSurface->readPixels(bmt, 0, 0); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 84 | |
| 85 | // Test correctness |
| 86 | { |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 87 | 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 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | DEF_TEST(ImageNewShader, reporter) { |
| 98 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 99 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 100 | auto sourceSurface(SkSurface::MakeRaster(info)); |
| 101 | auto destinationSurface(SkSurface::MakeRaster(info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 102 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 103 | run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 106 | static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) { |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 107 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 108 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 109 | auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
| 110 | auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 111 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 112 | run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 115 | static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) { |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 116 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 117 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 118 | auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
| 119 | auto destinationSurface(SkSurface::MakeRaster(info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 120 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 121 | run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 124 | static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) { |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 125 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 126 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 127 | auto sourceSurface(SkSurface::MakeRaster(info)); |
| 128 | auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 129 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 130 | run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 131 | } |
| 132 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 133 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 134 | auto context = ctxInfo.directContext(); |
| 135 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 136 | // GPU -> GPU |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 137 | gpu_to_gpu(reporter, context); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 138 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 139 | // GPU -> RASTER |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 140 | gpu_to_raster(reporter, context); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 141 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 142 | // RASTER -> GPU |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 143 | raster_to_gpu(reporter, context); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 144 | } |