blob: 5f0368692b4289814935c53e3b10ea33d7b9486b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrBackendSurface.h"
21#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/gpu/GrTypes.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040023#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000026#include "src/gpu/GrTexture.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040027#include "tests/Test.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "tools/gpu/GrContextFactory.h"
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000029
Ben Wagner9707a7e2019-05-06 17:17:19 -040030#include <initializer_list>
Ben Wagnere6b04a12018-03-09 14:41:36 -050031#include <vector>
32
mtklein195fe082015-11-17 08:39:01 -080033struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000034
mtklein195fe082015-11-17 08:39:01 -080035static bool acceptable(const Results& r) {
36#if 0
37 SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
38 r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
39#endif
40 return r.diffs_by_1 == r.diffs // never off by more than 1
41 && r.diffs_0x00 == 0 // transparent must stay transparent
42 && r.diffs_0xff == 0; // opaque must stay opaque
commit-bot@chromium.org40ac5e52014-02-26 21:40:07 +000043}
44
mtklein195fe082015-11-17 08:39:01 -080045template <typename Fn>
46static Results test(Fn&& multiply) {
47 Results r = { 0,0,0,0 };
48 for (int x = 0; x < 256; x++) {
49 for (int y = 0; y < 256; y++) {
50 int p = multiply(x, y),
51 ideal = (x*y+127)/255;
52 if (p != ideal) {
53 r.diffs++;
54 if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
55 if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
56 if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000057 }
mtklein195fe082015-11-17 08:39:01 -080058 }}
59 return r;
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000060}
61
mtklein195fe082015-11-17 08:39:01 -080062DEF_TEST(Blend_byte_multiply, r) {
63 // These are all temptingly close but fundamentally broken.
64 int (*broken[])(int, int) = {
65 [](int x, int y) { return (x*y)>>8; },
66 [](int x, int y) { return (x*y+128)>>8; },
67 [](int x, int y) { y += y>>7; return (x*y)>>8; },
68 };
69 for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000070
mtklein195fe082015-11-17 08:39:01 -080071 // These are fine to use, but not perfect.
72 int (*fine[])(int, int) = {
73 [](int x, int y) { return (x*y+x)>>8; },
74 [](int x, int y) { return (x*y+y)>>8; },
75 [](int x, int y) { return (x*y+255)>>8; },
76 [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
77 };
78 for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
79
80 // These are pefect.
81 int (*perfect[])(int, int) = {
82 [](int x, int y) { return (x*y+127)/255; }, // Duh.
83 [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
84 [](int x, int y) { return ((x*y+128)*257)>>16; },
85 };
86 for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000087}
Eric Karl74480882017-04-03 14:49:05 -070088
Eric Karl74480882017-04-03 14:49:05 -070089namespace {
90static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Brian Salomona56a7462020-02-07 14:17:25 -050091 GrContext* context, int sampleCnt, SkISize dimensions, SkColorType colorType,
Brian Salomon4eb38b72019-08-05 12:58:39 -040092 GrSurfaceOrigin origin, sk_sp<GrTexture>* backingSurface) {
Brian Salomon4eb38b72019-08-05 12:58:39 -040093 auto ct = SkColorTypeToGrColorType(colorType);
Brian Salomon4eb38b72019-08-05 12:58:39 -040094 auto format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
Eric Karl74480882017-04-03 14:49:05 -070095
Robert Phillips9da87e02019-02-04 13:26:26 -050096 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050097
Brian Salomona90382f2019-09-17 09:01:56 -040098 *backingSurface =
Brian Salomona56a7462020-02-07 14:17:25 -050099 resourceProvider->createTexture(dimensions, format, GrRenderable::kYes, sampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400100 GrMipMapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Robert Phillipsbf25d432017-04-07 10:08:53 -0400101 if (!(*backingSurface)) {
102 return nullptr;
103 }
Eric Karl74480882017-04-03 14:49:05 -0700104
Robert Phillipsb67821d2017-12-13 15:00:45 -0500105 GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
106
Eric Karl74480882017-04-03 14:49:05 -0700107 sk_sp<SkSurface> surface =
Greg Daniel7ef28f32017-04-20 16:41:55 +0000108 SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
Greg Danielfaa095e2017-12-19 13:15:02 -0500109 sampleCnt, colorType, nullptr, nullptr);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000110
Eric Karl74480882017-04-03 14:49:05 -0700111 return surface;
112}
113}
114
115// Tests blending to a surface with no texture available.
116DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
117 GrContext* context = ctxInfo.grContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500118 static constexpr SkISize kDimensions{10, 10};
Eric Karl74480882017-04-03 14:49:05 -0700119 const SkColorType kColorType = kRGBA_8888_SkColorType;
120
121 // Build our test cases:
122 struct RectAndSamplePoint {
123 SkRect rect;
124 SkIPoint outPoint;
125 SkIPoint inPoint;
126 } allRectsAndPoints[3] = {
127 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
Eric Karl72e551e2017-04-04 13:42:10 -0700128 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
Eric Karl74480882017-04-03 14:49:05 -0700129 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
130 };
131
132 struct TestCase {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400133 RectAndSamplePoint fRectAndPoints;
134 SkRect fClip;
135 int fSampleCnt;
136 GrSurfaceOrigin fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700137 };
138 std::vector<TestCase> testCases;
139
Robert Phillipsbf25d432017-04-07 10:08:53 -0400140 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500141 for (int sampleCnt : {1, 4}) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400142 for (auto rectAndPoints : allRectsAndPoints) {
143 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
144 testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
145 }
Eric Karl72e551e2017-04-04 13:42:10 -0700146 }
Eric Karl74480882017-04-03 14:49:05 -0700147 }
148 }
149
150 // Run each test case:
151 for (auto testCase : testCases) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400152 int sampleCnt = testCase.fSampleCnt;
153 SkRect paintRect = testCase.fRectAndPoints.rect;
154 SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
155 SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
156 GrSurfaceOrigin origin = testCase.fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700157
158 sk_sp<GrTexture> backingSurface;
159 // BGRA forces a framebuffer blit on ES2.
160 sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
Brian Salomona56a7462020-02-07 14:17:25 -0500161 context, sampleCnt, kDimensions, kColorType, origin, &backingSurface);
Eric Karl74480882017-04-03 14:49:05 -0700162
Brian Salomonbdecacf2018-02-02 20:32:49 -0500163 if (!surface && sampleCnt > 1) {
Eric Karl74480882017-04-03 14:49:05 -0700164 // Some platforms don't support MSAA.
165 continue;
166 }
167 REPORTER_ASSERT(reporter, !!surface);
168
169 // Fill our canvas with 0xFFFF80
170 SkCanvas* canvas = surface->getCanvas();
Robert Phillipsbf25d432017-04-07 10:08:53 -0400171 canvas->clipRect(testCase.fClip, false);
Eric Karl74480882017-04-03 14:49:05 -0700172 SkPaint black_paint;
173 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
Brian Salomona56a7462020-02-07 14:17:25 -0500174 canvas->drawRect(SkRect::Make(kDimensions), black_paint);
Eric Karl74480882017-04-03 14:49:05 -0700175
176 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
177 // a copy of the destination.
178 SkPaint white_paint;
179 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
180 white_paint.setBlendMode(SkBlendMode::kMultiply);
181 canvas->drawRect(paintRect, white_paint);
182
183 // Read the result into a bitmap.
184 SkBitmap bitmap;
Brian Salomona56a7462020-02-07 14:17:25 -0500185 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(kDimensions, kColorType,
186 kPremul_SkAlphaType)));
Eric Karl74480882017-04-03 14:49:05 -0700187 REPORTER_ASSERT(
188 reporter,
189 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
190
191 // Check the in/out pixels.
192 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
193 SkColorSetRGB(0xFF, 0xFF, 0x80));
194 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
195 SkColorSetRGB(0x80, 0xFF, 0x80));
196
197 // Clean up - surface depends on backingSurface and must be released first.
Eric Karl74480882017-04-03 14:49:05 -0700198 surface.reset();
199 backingSurface.reset();
200 }
201}