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