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