Brian Salomon | 94724c6 | 2019-12-30 16:36:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
| 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkSurface.h" |
| 12 | #include "include/effects/SkColorMatrix.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | 94724c6 | 2019-12-30 16:36:25 -0500 | [diff] [blame] | 14 | #include "src/core/SkAutoPixmapStorage.h" |
| 15 | #include "tests/Test.h" |
| 16 | #include "tests/TestUtils.h" |
| 17 | |
| 18 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(MatrixColorFilter_TransparentBlack, reporter, info) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 19 | auto context = info.directContext(); |
Brian Salomon | 94724c6 | 2019-12-30 16:36:25 -0500 | [diff] [blame] | 20 | // Make a transparent black image rather than use a paint color to avoid an optimization that |
| 21 | // applies the color filter on the CPU to paint colors. |
| 22 | auto imgSurf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, |
| 23 | SkImageInfo::MakeN32(5, 5, kPremul_SkAlphaType)); |
| 24 | imgSurf->getCanvas()->drawColor(0x0000000); |
| 25 | auto shader = imgSurf->makeImageSnapshot()->makeShader(SkTileMode::kClamp, SkTileMode::kClamp); |
| 26 | SkColorMatrix m; |
| 27 | m.setScale(0, 0, 0, 127.f); |
| 28 | SkPaint p; |
| 29 | p.setColorFilter(SkColorFilters::Matrix(m)); |
| 30 | p.setShader(shader); |
| 31 | p.setBlendMode(SkBlendMode::kSrc); |
| 32 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, |
| 33 | SkImageInfo::MakeN32(5, 5, kPremul_SkAlphaType)); |
| 34 | // Seed the output surface with red so we would notice if we failed to draw at all. |
| 35 | surf->getCanvas()->clear(SK_ColorRED); |
| 36 | surf->getCanvas()->drawPaint(p); |
| 37 | SkAutoPixmapStorage pixels; |
| 38 | pixels.alloc(surf->imageInfo()); |
| 39 | surf->readPixels(pixels, 0, 0); |
| 40 | auto error = std::function<ComparePixmapsErrorReporter>( |
| 41 | [reporter](int x, int y, const float diffs[4]) { |
| 42 | ERRORF(reporter, "Expected transparent black, instead got (%f, %f, %f, %f)", |
| 43 | diffs[0], diffs[1], diffs[2], diffs[3]); |
| 44 | }); |
| 45 | static constexpr float kTol[] = {0, 0, 0, 0}; |
| 46 | CheckSolidPixels({0, 0, 0, 0}, pixels, kTol, error); |
| 47 | } |