blob: c9313cd3e39d9daff1a6ef9cca989be258b82388 [file] [log] [blame]
mtklein767d2732015-07-16 07:01:39 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
Robert Phillips6d344c32020-07-06 10:56:46 -040019#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrTypes.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040021#include "tests/Test.h"
Brian Salomon72c7b982020-10-06 10:07:38 -040022#include "tools/gpu/BackendSurfaceFactory.h"
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000023
Ben Wagner9707a7e2019-05-06 17:17:19 -040024#include <initializer_list>
Ben Wagnere6b04a12018-03-09 14:41:36 -050025#include <vector>
26
mtklein195fe082015-11-17 08:39:01 -080027struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000028
mtklein195fe082015-11-17 08:39:01 -080029static 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
commit-bot@chromium.org40ac5e52014-02-26 21:40:07 +000037}
38
mtklein195fe082015-11-17 08:39:01 -080039template <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++; }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000051 }
mtklein195fe082015-11-17 08:39:01 -080052 }}
53 return r;
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000054}
55
mtklein195fe082015-11-17 08:39:01 -080056DEF_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))); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000064
mtklein195fe082015-11-17 08:39:01 -080065 // 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); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000081}
Eric Karl74480882017-04-03 14:49:05 -070082
Eric Karl74480882017-04-03 14:49:05 -070083// Tests blending to a surface with no texture available.
84DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040085 auto context = ctxInfo.directContext();
Brian Salomona56a7462020-02-07 14:17:25 -050086 static constexpr SkISize kDimensions{10, 10};
Eric Karl74480882017-04-03 14:49:05 -070087 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)},
Eric Karl72e551e2017-04-04 13:42:10 -070096 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
Eric Karl74480882017-04-03 14:49:05 -070097 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
98 };
99
100 struct TestCase {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400101 RectAndSamplePoint fRectAndPoints;
102 SkRect fClip;
103 int fSampleCnt;
104 GrSurfaceOrigin fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700105 };
106 std::vector<TestCase> testCases;
107
Robert Phillipsbf25d432017-04-07 10:08:53 -0400108 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109 for (int sampleCnt : {1, 4}) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400110 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 }
Eric Karl72e551e2017-04-04 13:42:10 -0700114 }
Eric Karl74480882017-04-03 14:49:05 -0700115 }
116 }
117
118 // Run each test case:
119 for (auto testCase : testCases) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400120 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;
Eric Karl74480882017-04-03 14:49:05 -0700125
Eric Karl74480882017-04-03 14:49:05 -0700126 // BGRA forces a framebuffer blit on ES2.
Brian Salomonf9b00422020-10-08 16:00:14 -0400127 sk_sp<SkSurface> surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
128 kDimensions,
129 origin,
130 sampleCnt,
131 kColorType);
Eric Karl74480882017-04-03 14:49:05 -0700132
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 if (!surface && sampleCnt > 1) {
Eric Karl74480882017-04-03 14:49:05 -0700134 // 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();
Robert Phillipsbf25d432017-04-07 10:08:53 -0400141 canvas->clipRect(testCase.fClip, false);
Eric Karl74480882017-04-03 14:49:05 -0700142 SkPaint black_paint;
143 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
Brian Salomona56a7462020-02-07 14:17:25 -0500144 canvas->drawRect(SkRect::Make(kDimensions), black_paint);
Eric Karl74480882017-04-03 14:49:05 -0700145
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;
Brian Salomona56a7462020-02-07 14:17:25 -0500155 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(kDimensions, kColorType,
156 kPremul_SkAlphaType)));
Eric Karl74480882017-04-03 14:49:05 -0700157 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));
Eric Karl74480882017-04-03 14:49:05 -0700166 }
167}