xref: /third_party/skia/tests/BlendTest.cpp (revision cb93a386)
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
8#include "include/core/SkBitmap.h"
9#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkSurface.h"
18#include "include/core/SkTypes.h"
19#include "include/gpu/GrDirectContext.h"
20#include "include/gpu/GrTypes.h"
21#include "tests/Test.h"
22#include "tools/gpu/BackendSurfaceFactory.h"
23
24#include <initializer_list>
25#include <vector>
26
27struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
28
29static bool acceptable(const Results& r) {
30#if 0
31    SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
32             r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
33#endif
34    return r.diffs_by_1 == r.diffs   // never off by more than 1
35        && r.diffs_0x00 == 0         // transparent must stay transparent
36        && r.diffs_0xff == 0;        // opaque must stay opaque
37}
38
39template <typename Fn>
40static Results test(Fn&& multiply) {
41    Results r = { 0,0,0,0 };
42    for (int x = 0; x < 256; x++) {
43    for (int y = 0; y < 256; y++) {
44        int p = multiply(x, y),
45            ideal = (x*y+127)/255;
46        if (p != ideal) {
47            r.diffs++;
48            if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
49            if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
50            if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
51        }
52    }}
53    return r;
54}
55
56DEF_TEST(Blend_byte_multiply, r) {
57    // These are all temptingly close but fundamentally broken.
58    int (*broken[])(int, int) = {
59        [](int x, int y) { return (x*y)>>8; },
60        [](int x, int y) { return (x*y+128)>>8; },
61        [](int x, int y) { y += y>>7; return (x*y)>>8; },
62    };
63    for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
64
65    // These are fine to use, but not perfect.
66    int (*fine[])(int, int) = {
67        [](int x, int y) { return (x*y+x)>>8; },
68        [](int x, int y) { return (x*y+y)>>8; },
69        [](int x, int y) { return (x*y+255)>>8; },
70        [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
71    };
72    for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
73
74    // These are pefect.
75    int (*perfect[])(int, int) = {
76        [](int x, int y) { return (x*y+127)/255; },  // Duh.
77        [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
78        [](int x, int y) { return ((x*y+128)*257)>>16; },
79    };
80    for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
81}
82
83// Tests blending to a surface with no texture available.
84DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
85    auto context = ctxInfo.directContext();
86    static constexpr SkISize kDimensions{10, 10};
87    const SkColorType kColorType = kRGBA_8888_SkColorType;
88
89    // Build our test cases:
90    struct RectAndSamplePoint {
91        SkRect rect;
92        SkIPoint outPoint;
93        SkIPoint inPoint;
94    } allRectsAndPoints[3] = {
95            {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
96            {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
97            {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
98    };
99
100    struct TestCase {
101        RectAndSamplePoint fRectAndPoints;
102        SkRect             fClip;
103        int                fSampleCnt;
104        GrSurfaceOrigin    fOrigin;
105    };
106    std::vector<TestCase> testCases;
107
108    for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
109        for (int sampleCnt : {1, 4}) {
110            for (auto rectAndPoints : allRectsAndPoints) {
111                for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
112                    testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
113                }
114            }
115        }
116    }
117
118    // Run each test case:
119    for (auto testCase : testCases) {
120        int sampleCnt = testCase.fSampleCnt;
121        SkRect paintRect = testCase.fRectAndPoints.rect;
122        SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
123        SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
124        GrSurfaceOrigin origin = testCase.fOrigin;
125
126        // BGRA forces a framebuffer blit on ES2.
127        sk_sp<SkSurface> surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
128                                                                               kDimensions,
129                                                                               origin,
130                                                                               sampleCnt,
131                                                                               kColorType);
132
133        if (!surface && sampleCnt > 1) {
134            // Some platforms don't support MSAA.
135            continue;
136        }
137        REPORTER_ASSERT(reporter, !!surface);
138
139        // Fill our canvas with 0xFFFF80
140        SkCanvas* canvas = surface->getCanvas();
141        canvas->clipRect(testCase.fClip, false);
142        SkPaint black_paint;
143        black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
144        canvas->drawRect(SkRect::Make(kDimensions), black_paint);
145
146        // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
147        // a copy of the destination.
148        SkPaint white_paint;
149        white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
150        white_paint.setBlendMode(SkBlendMode::kMultiply);
151        canvas->drawRect(paintRect, white_paint);
152
153        // Read the result into a bitmap.
154        SkBitmap bitmap;
155        REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(kDimensions, kColorType,
156                                                                          kPremul_SkAlphaType)));
157        REPORTER_ASSERT(
158                reporter,
159                surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
160
161        // Check the in/out pixels.
162        REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
163                                          SkColorSetRGB(0xFF, 0xFF, 0x80));
164        REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
165                                          SkColorSetRGB(0x80, 0xFF, 0x80));
166    }
167}
168