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