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