mtklein | 767d273 | 2015-07-16 07:01:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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/SkBitmap.h" |
| 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImageInfo.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkSurface.h" |
| 18 | #include "include/core/SkTypes.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/gpu/GrTypes.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 21 | #include "tests/Test.h" |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 22 | #include "tools/gpu/BackendSurfaceFactory.h" |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 23 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 24 | #include <initializer_list> |
Ben Wagner | e6b04a1 | 2018-03-09 14:41:36 -0500 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 27 | struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; }; |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 28 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 29 | static bool acceptable(const Results& r) { |
| 30 | #if 0 |
| 31 | SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n", |
| 32 | r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1); |
| 33 | #endif |
| 34 | return r.diffs_by_1 == r.diffs // never off by more than 1 |
| 35 | && r.diffs_0x00 == 0 // transparent must stay transparent |
| 36 | && r.diffs_0xff == 0; // opaque must stay opaque |
commit-bot@chromium.org | 40ac5e5 | 2014-02-26 21:40:07 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 39 | template <typename Fn> |
| 40 | static Results test(Fn&& multiply) { |
| 41 | Results r = { 0,0,0,0 }; |
| 42 | for (int x = 0; x < 256; x++) { |
| 43 | for (int y = 0; y < 256; y++) { |
| 44 | int p = multiply(x, y), |
| 45 | ideal = (x*y+127)/255; |
| 46 | if (p != ideal) { |
| 47 | r.diffs++; |
| 48 | if (x == 0x00 || y == 0x00) { r.diffs_0x00++; } |
| 49 | if (x == 0xff || y == 0xff) { r.diffs_0xff++; } |
| 50 | if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; } |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 51 | } |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 52 | }} |
| 53 | return r; |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 54 | } |
| 55 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 56 | DEF_TEST(Blend_byte_multiply, r) { |
| 57 | // These are all temptingly close but fundamentally broken. |
| 58 | int (*broken[])(int, int) = { |
| 59 | [](int x, int y) { return (x*y)>>8; }, |
| 60 | [](int x, int y) { return (x*y+128)>>8; }, |
| 61 | [](int x, int y) { y += y>>7; return (x*y)>>8; }, |
| 62 | }; |
| 63 | for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); } |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 64 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 65 | // These are fine to use, but not perfect. |
| 66 | int (*fine[])(int, int) = { |
| 67 | [](int x, int y) { return (x*y+x)>>8; }, |
| 68 | [](int x, int y) { return (x*y+y)>>8; }, |
| 69 | [](int x, int y) { return (x*y+255)>>8; }, |
| 70 | [](int x, int y) { y += y>>7; return (x*y+128)>>8; }, |
| 71 | }; |
| 72 | for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); } |
| 73 | |
| 74 | // These are pefect. |
| 75 | int (*perfect[])(int, int) = { |
| 76 | [](int x, int y) { return (x*y+127)/255; }, // Duh. |
| 77 | [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; }, |
| 78 | [](int x, int y) { return ((x*y+128)*257)>>16; }, |
| 79 | }; |
| 80 | for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); } |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 81 | } |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 82 | |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 83 | // Tests blending to a surface with no texture available. |
| 84 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 85 | auto context = ctxInfo.directContext(); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 86 | static constexpr SkISize kDimensions{10, 10}; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 87 | const SkColorType kColorType = kRGBA_8888_SkColorType; |
| 88 | |
| 89 | // Build our test cases: |
| 90 | struct RectAndSamplePoint { |
| 91 | SkRect rect; |
| 92 | SkIPoint outPoint; |
| 93 | SkIPoint inPoint; |
| 94 | } allRectsAndPoints[3] = { |
| 95 | {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)}, |
Eric Karl | 72e551e | 2017-04-04 13:42:10 -0700 | [diff] [blame] | 96 | {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)}, |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 97 | {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)}, |
| 98 | }; |
| 99 | |
| 100 | struct TestCase { |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 101 | RectAndSamplePoint fRectAndPoints; |
| 102 | SkRect fClip; |
| 103 | int fSampleCnt; |
| 104 | GrSurfaceOrigin fOrigin; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 105 | }; |
| 106 | std::vector<TestCase> testCases; |
| 107 | |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 108 | for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 109 | for (int sampleCnt : {1, 4}) { |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 110 | for (auto rectAndPoints : allRectsAndPoints) { |
| 111 | for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) { |
| 112 | testCases.push_back({rectAndPoints, clip, sampleCnt, origin}); |
| 113 | } |
Eric Karl | 72e551e | 2017-04-04 13:42:10 -0700 | [diff] [blame] | 114 | } |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | // Run each test case: |
| 119 | for (auto testCase : testCases) { |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 120 | int sampleCnt = testCase.fSampleCnt; |
| 121 | SkRect paintRect = testCase.fRectAndPoints.rect; |
| 122 | SkIPoint outPoint = testCase.fRectAndPoints.outPoint; |
| 123 | SkIPoint inPoint = testCase.fRectAndPoints.inPoint; |
| 124 | GrSurfaceOrigin origin = testCase.fOrigin; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 125 | |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 126 | // BGRA forces a framebuffer blit on ES2. |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 127 | sk_sp<SkSurface> surface = sk_gpu_test::MakeBackendRenderTargetSurface(context, |
| 128 | kDimensions, |
| 129 | origin, |
| 130 | sampleCnt, |
| 131 | kColorType); |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 132 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 133 | if (!surface && sampleCnt > 1) { |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 134 | // Some platforms don't support MSAA. |
| 135 | continue; |
| 136 | } |
| 137 | REPORTER_ASSERT(reporter, !!surface); |
| 138 | |
| 139 | // Fill our canvas with 0xFFFF80 |
| 140 | SkCanvas* canvas = surface->getCanvas(); |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 141 | canvas->clipRect(testCase.fClip, false); |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 142 | SkPaint black_paint; |
| 143 | black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80)); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 144 | canvas->drawRect(SkRect::Make(kDimensions), black_paint); |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 145 | |
| 146 | // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger |
| 147 | // a copy of the destination. |
| 148 | SkPaint white_paint; |
| 149 | white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF)); |
| 150 | white_paint.setBlendMode(SkBlendMode::kMultiply); |
| 151 | canvas->drawRect(paintRect, white_paint); |
| 152 | |
| 153 | // Read the result into a bitmap. |
| 154 | SkBitmap bitmap; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 155 | REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(kDimensions, kColorType, |
| 156 | kPremul_SkAlphaType))); |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 157 | REPORTER_ASSERT( |
| 158 | reporter, |
| 159 | surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0)); |
| 160 | |
| 161 | // Check the in/out pixels. |
| 162 | REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) == |
| 163 | SkColorSetRGB(0xFF, 0xFF, 0x80)); |
| 164 | REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) == |
| 165 | SkColorSetRGB(0x80, 0xFF, 0x80)); |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 166 | } |
| 167 | } |