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 | |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 19 | void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) { |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 20 | SkAutoLockPixels lockBm1(bm1); |
| 21 | SkAutoLockPixels lockBm2(bm2); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 22 | |
| 23 | REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize()); |
| 24 | REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize())); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 25 | } |
| 26 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 27 | void paintSource(SkSurface* sourceSurface) { |
| 28 | SkCanvas* sourceCanvas = sourceSurface->getCanvas(); |
| 29 | sourceCanvas->clear(0xFFDEDEDE); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 30 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 31 | 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 | |
| 44 | void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSurface* destinationSurface, SkImageInfo& info) { |
| 45 | paintSource(sourceSurface); |
| 46 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 47 | sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot()); |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 48 | sk_sp<SkShader> sourceShader = sourceImage->makeShader( |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 49 | SkShader::kRepeat_TileMode, |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 50 | SkShader::kRepeat_TileMode); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 51 | |
| 52 | SkPaint paint; |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 53 | paint.setShader(sourceShader); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 54 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 55 | SkCanvas* destinationCanvas = destinationSurface->getCanvas(); |
| 56 | destinationCanvas->clear(SK_ColorTRANSPARENT); |
| 57 | destinationCanvas->drawPaint(paint); |
| 58 | |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 59 | SkIRect rect = info.bounds(); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 60 | |
| 61 | SkBitmap bmOrig; |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 62 | sourceSurface->getCanvas()->readPixels(rect, &bmOrig); |
| 63 | |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 64 | |
| 65 | SkBitmap bm; |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 66 | destinationCanvas->readPixels(rect, &bm); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 67 | |
| 68 | testBitmapEquality(reporter, bmOrig, bm); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | |
| 72 | // Test with a translated shader |
| 73 | SkMatrix matrix; |
| 74 | matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0)); |
| 75 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 76 | sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader( |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 77 | SkShader::kRepeat_TileMode, |
| 78 | SkShader::kRepeat_TileMode, |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 79 | &matrix); |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 80 | |
| 81 | destinationCanvas->clear(SK_ColorTRANSPARENT); |
| 82 | |
| 83 | SkPaint paintTranslated; |
| 84 | paintTranslated.setShader(sourceShaderTranslated); |
| 85 | |
| 86 | destinationCanvas->drawPaint(paintTranslated); |
| 87 | |
| 88 | SkBitmap bmt; |
| 89 | destinationCanvas->readPixels(rect, &bmt); |
| 90 | |
| 91 | // Test correctness |
| 92 | { |
| 93 | SkAutoLockPixels lockBm(bmt); |
| 94 | for (int y = 0; y < info.height(); y++) { |
| 95 | REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y)); |
| 96 | |
| 97 | for (int x = 1; x < info.width(); x++) { |
| 98 | REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y)); |
| 99 | } |
| 100 | } |
| 101 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | DEF_TEST(ImageNewShader, reporter) { |
| 105 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 106 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 107 | auto sourceSurface(SkSurface::MakeRaster(info)); |
| 108 | auto destinationSurface(SkSurface::MakeRaster(info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 109 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 110 | runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | #if SK_SUPPORT_GPU |
| 114 | |
| 115 | void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) { |
| 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::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 120 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 121 | runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) { |
| 125 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 126 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 127 | auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
| 128 | auto destinationSurface(SkSurface::MakeRaster(info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 129 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 130 | runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) { |
| 134 | SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); |
| 135 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 136 | auto sourceSurface(SkSurface::MakeRaster(info)); |
| 137 | auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 138 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 139 | runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 140 | } |
| 141 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 142 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 143 | // GPU -> GPU |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 144 | gpuToGpu(reporter, ctxInfo.grContext()); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 145 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 146 | // GPU -> RASTER |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 147 | gpuToRaster(reporter, ctxInfo.grContext()); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 148 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 149 | // RASTER -> GPU |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 150 | rasterToGpu(reporter, ctxInfo.grContext()); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | #endif |