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