blob: fe701ee5483221a20e71602b1f8496fb08cfe069 [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"
12#include "include/core/SkColorSpace.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkSurface.h"
19#include "include/core/SkTypes.h"
20#include "tests/Test.h"
Eric Karl74480882017-04-03 14:49:05 -070021
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/gpu/GrBackendSurface.h"
23#include "include/gpu/GrContext.h"
24#include "include/gpu/GrTexture.h"
25#include "include/gpu/GrTypes.h"
26#include "src/gpu/GrContextPriv.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "tools/gpu/GrContextFactory.h"
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000029
Ben Wagnere6b04a12018-03-09 14:41:36 -050030#include <vector>
31
mtklein195fe082015-11-17 08:39:01 -080032struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000033
mtklein195fe082015-11-17 08:39:01 -080034static bool acceptable(const Results& r) {
35#if 0
36 SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
37 r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
38#endif
39 return r.diffs_by_1 == r.diffs // never off by more than 1
40 && r.diffs_0x00 == 0 // transparent must stay transparent
41 && r.diffs_0xff == 0; // opaque must stay opaque
commit-bot@chromium.org40ac5e52014-02-26 21:40:07 +000042}
43
mtklein195fe082015-11-17 08:39:01 -080044template <typename Fn>
45static Results test(Fn&& multiply) {
46 Results r = { 0,0,0,0 };
47 for (int x = 0; x < 256; x++) {
48 for (int y = 0; y < 256; y++) {
49 int p = multiply(x, y),
50 ideal = (x*y+127)/255;
51 if (p != ideal) {
52 r.diffs++;
53 if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
54 if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
55 if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000056 }
mtklein195fe082015-11-17 08:39:01 -080057 }}
58 return r;
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000059}
60
mtklein195fe082015-11-17 08:39:01 -080061DEF_TEST(Blend_byte_multiply, r) {
62 // These are all temptingly close but fundamentally broken.
63 int (*broken[])(int, int) = {
64 [](int x, int y) { return (x*y)>>8; },
65 [](int x, int y) { return (x*y+128)>>8; },
66 [](int x, int y) { y += y>>7; return (x*y)>>8; },
67 };
68 for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000069
mtklein195fe082015-11-17 08:39:01 -080070 // These are fine to use, but not perfect.
71 int (*fine[])(int, int) = {
72 [](int x, int y) { return (x*y+x)>>8; },
73 [](int x, int y) { return (x*y+y)>>8; },
74 [](int x, int y) { return (x*y+255)>>8; },
75 [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
76 };
77 for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
78
79 // These are pefect.
80 int (*perfect[])(int, int) = {
81 [](int x, int y) { return (x*y+127)/255; }, // Duh.
82 [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
83 [](int x, int y) { return ((x*y+128)*257)>>16; },
84 };
85 for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000086}
Eric Karl74480882017-04-03 14:49:05 -070087
Eric Karl74480882017-04-03 14:49:05 -070088namespace {
89static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Greg Danielfaa095e2017-12-19 13:15:02 -050090 GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
91 GrPixelConfig config, GrSurfaceOrigin origin,
Eric Karl74480882017-04-03 14:49:05 -070092 sk_sp<GrTexture>* backingSurface) {
93 GrSurfaceDesc backingDesc;
Robert Phillips9a121cc2017-04-06 21:17:15 +000094 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -040095 backingDesc.fWidth = width;
96 backingDesc.fHeight = height;
97 backingDesc.fConfig = config;
98 backingDesc.fSampleCnt = sampleCnt;
Eric Karl74480882017-04-03 14:49:05 -070099
Robert Phillips9da87e02019-02-04 13:26:26 -0500100 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500101
Robert Phillips9313aa72019-04-09 18:41:27 -0400102 *backingSurface = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo,
103 GrResourceProvider::Flags::kNoPendingIO);
Robert Phillipsbf25d432017-04-07 10:08:53 -0400104 if (!(*backingSurface)) {
105 return nullptr;
106 }
Eric Karl74480882017-04-03 14:49:05 -0700107
Robert Phillipsb67821d2017-12-13 15:00:45 -0500108 GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
109
Eric Karl74480882017-04-03 14:49:05 -0700110 sk_sp<SkSurface> surface =
Greg Daniel7ef28f32017-04-20 16:41:55 +0000111 SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
Greg Danielfaa095e2017-12-19 13:15:02 -0500112 sampleCnt, colorType, nullptr, nullptr);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000113
Eric Karl74480882017-04-03 14:49:05 -0700114 return surface;
115}
116}
117
118// Tests blending to a surface with no texture available.
119DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
120 GrContext* context = ctxInfo.grContext();
121 const int kWidth = 10;
122 const int kHeight = 10;
123 const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
124 const SkColorType kColorType = kRGBA_8888_SkColorType;
125
126 // Build our test cases:
127 struct RectAndSamplePoint {
128 SkRect rect;
129 SkIPoint outPoint;
130 SkIPoint inPoint;
131 } allRectsAndPoints[3] = {
132 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
Eric Karl72e551e2017-04-04 13:42:10 -0700133 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
Eric Karl74480882017-04-03 14:49:05 -0700134 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
135 };
136
137 struct TestCase {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400138 RectAndSamplePoint fRectAndPoints;
139 SkRect fClip;
140 int fSampleCnt;
141 GrSurfaceOrigin fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700142 };
143 std::vector<TestCase> testCases;
144
Robert Phillipsbf25d432017-04-07 10:08:53 -0400145 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500146 for (int sampleCnt : {1, 4}) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400147 for (auto rectAndPoints : allRectsAndPoints) {
148 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
149 testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
150 }
Eric Karl72e551e2017-04-04 13:42:10 -0700151 }
Eric Karl74480882017-04-03 14:49:05 -0700152 }
153 }
154
155 // Run each test case:
156 for (auto testCase : testCases) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400157 int sampleCnt = testCase.fSampleCnt;
158 SkRect paintRect = testCase.fRectAndPoints.rect;
159 SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
160 SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
161 GrSurfaceOrigin origin = testCase.fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700162
163 sk_sp<GrTexture> backingSurface;
164 // BGRA forces a framebuffer blit on ES2.
165 sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
Greg Danielfaa095e2017-12-19 13:15:02 -0500166 context, sampleCnt, kWidth, kHeight, kColorType, kConfig, origin, &backingSurface);
Eric Karl74480882017-04-03 14:49:05 -0700167
Brian Salomonbdecacf2018-02-02 20:32:49 -0500168 if (!surface && sampleCnt > 1) {
Eric Karl74480882017-04-03 14:49:05 -0700169 // Some platforms don't support MSAA.
170 continue;
171 }
172 REPORTER_ASSERT(reporter, !!surface);
173
174 // Fill our canvas with 0xFFFF80
175 SkCanvas* canvas = surface->getCanvas();
Robert Phillipsbf25d432017-04-07 10:08:53 -0400176 canvas->clipRect(testCase.fClip, false);
Eric Karl74480882017-04-03 14:49:05 -0700177 SkPaint black_paint;
178 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
179 canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
180
181 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
182 // a copy of the destination.
183 SkPaint white_paint;
184 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
185 white_paint.setBlendMode(SkBlendMode::kMultiply);
186 canvas->drawRect(paintRect, white_paint);
187
188 // Read the result into a bitmap.
189 SkBitmap bitmap;
190 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
191 kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
Eric Karl74480882017-04-03 14:49:05 -0700192 REPORTER_ASSERT(
193 reporter,
194 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
195
196 // Check the in/out pixels.
197 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
198 SkColorSetRGB(0xFF, 0xFF, 0x80));
199 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
200 SkColorSetRGB(0x80, 0xFF, 0x80));
201
202 // Clean up - surface depends on backingSurface and must be released first.
Eric Karl74480882017-04-03 14:49:05 -0700203 surface.reset();
204 backingSurface.reset();
205 }
206}