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 "include/gpu/GrDirectContext.h"
14 #include "src/core/SkAutoPixmapStorage.h"
15 #include "tests/Test.h"
16 #include "tests/TestUtils.h"
17 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(MatrixColorFilter_TransparentBlack,reporter,info)18 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(MatrixColorFilter_TransparentBlack, reporter, info) {
19     auto context = info.directContext();
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 }
48