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