blob: 5b2f6faa146ccb40b7e00222b8d940d86a0ddd6e [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
Eric Karl74480882017-04-03 14:49:05 -07008#include "SkBitmap.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -05009#include "SkBlendMode.h"
Eric Karl74480882017-04-03 14:49:05 -070010#include "SkCanvas.h"
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000011#include "SkColor.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050012#include "SkColorSpace.h"
13#include "SkImageInfo.h"
14#include "SkPaint.h"
15#include "SkPoint.h"
16#include "SkRect.h"
17#include "SkRefCnt.h"
Eric Karl74480882017-04-03 14:49:05 -070018#include "SkSurface.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050019#include "SkTypes.h"
Eric Karl74480882017-04-03 14:49:05 -070020#include "Test.h"
21
22#if SK_SUPPORT_GPU
Ben Wagnere6b04a12018-03-09 14:41:36 -050023#include "GrBackendSurface.h"
Eric Karl74480882017-04-03 14:49:05 -070024#include "GrContext.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050025#include "GrContextFactory.h"
Eric Karl74480882017-04-03 14:49:05 -070026#include "GrContextPriv.h"
27#include "GrResourceProvider.h"
Eric Karl74480882017-04-03 14:49:05 -070028#include "GrTexture.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050029#include "GrTypes.h"
Eric Karl74480882017-04-03 14:49:05 -070030#endif
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000031
Ben Wagnere6b04a12018-03-09 14:41:36 -050032#include <vector>
33
mtklein195fe082015-11-17 08:39:01 -080034struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000035
mtklein195fe082015-11-17 08:39:01 -080036static bool acceptable(const Results& r) {
37#if 0
38 SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
39 r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
40#endif
41 return r.diffs_by_1 == r.diffs // never off by more than 1
42 && r.diffs_0x00 == 0 // transparent must stay transparent
43 && r.diffs_0xff == 0; // opaque must stay opaque
commit-bot@chromium.org40ac5e52014-02-26 21:40:07 +000044}
45
mtklein195fe082015-11-17 08:39:01 -080046template <typename Fn>
47static Results test(Fn&& multiply) {
48 Results r = { 0,0,0,0 };
49 for (int x = 0; x < 256; x++) {
50 for (int y = 0; y < 256; y++) {
51 int p = multiply(x, y),
52 ideal = (x*y+127)/255;
53 if (p != ideal) {
54 r.diffs++;
55 if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
56 if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
57 if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000058 }
mtklein195fe082015-11-17 08:39:01 -080059 }}
60 return r;
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000061}
62
mtklein195fe082015-11-17 08:39:01 -080063DEF_TEST(Blend_byte_multiply, r) {
64 // These are all temptingly close but fundamentally broken.
65 int (*broken[])(int, int) = {
66 [](int x, int y) { return (x*y)>>8; },
67 [](int x, int y) { return (x*y+128)>>8; },
68 [](int x, int y) { y += y>>7; return (x*y)>>8; },
69 };
70 for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000071
mtklein195fe082015-11-17 08:39:01 -080072 // These are fine to use, but not perfect.
73 int (*fine[])(int, int) = {
74 [](int x, int y) { return (x*y+x)>>8; },
75 [](int x, int y) { return (x*y+y)>>8; },
76 [](int x, int y) { return (x*y+255)>>8; },
77 [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
78 };
79 for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
80
81 // These are pefect.
82 int (*perfect[])(int, int) = {
83 [](int x, int y) { return (x*y+127)/255; }, // Duh.
84 [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
85 [](int x, int y) { return ((x*y+128)*257)>>16; },
86 };
87 for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000088}
Eric Karl74480882017-04-03 14:49:05 -070089
90#if SK_SUPPORT_GPU
91namespace {
92static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Greg Danielfaa095e2017-12-19 13:15:02 -050093 GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
94 GrPixelConfig config, GrSurfaceOrigin origin,
Eric Karl74480882017-04-03 14:49:05 -070095 sk_sp<GrTexture>* backingSurface) {
96 GrSurfaceDesc backingDesc;
Robert Phillips9a121cc2017-04-06 21:17:15 +000097 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -040098 backingDesc.fWidth = width;
99 backingDesc.fHeight = height;
100 backingDesc.fConfig = config;
101 backingDesc.fSampleCnt = sampleCnt;
Eric Karl74480882017-04-03 14:49:05 -0700102
Robert Phillips6be756b2018-01-16 15:07:54 -0500103 auto resourceProvider = context->contextPriv().resourceProvider();
104
105 *backingSurface = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo);
Robert Phillipsbf25d432017-04-07 10:08:53 -0400106 if (!(*backingSurface)) {
107 return nullptr;
108 }
Eric Karl74480882017-04-03 14:49:05 -0700109
Robert Phillipsb67821d2017-12-13 15:00:45 -0500110 GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
111
Eric Karl74480882017-04-03 14:49:05 -0700112 sk_sp<SkSurface> surface =
Greg Daniel7ef28f32017-04-20 16:41:55 +0000113 SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
Greg Danielfaa095e2017-12-19 13:15:02 -0500114 sampleCnt, colorType, nullptr, nullptr);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000115
Eric Karl74480882017-04-03 14:49:05 -0700116 return surface;
117}
118}
119
120// Tests blending to a surface with no texture available.
121DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
122 GrContext* context = ctxInfo.grContext();
123 const int kWidth = 10;
124 const int kHeight = 10;
125 const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
126 const SkColorType kColorType = kRGBA_8888_SkColorType;
127
128 // Build our test cases:
129 struct RectAndSamplePoint {
130 SkRect rect;
131 SkIPoint outPoint;
132 SkIPoint inPoint;
133 } allRectsAndPoints[3] = {
134 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
Eric Karl72e551e2017-04-04 13:42:10 -0700135 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
Eric Karl74480882017-04-03 14:49:05 -0700136 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
137 };
138
139 struct TestCase {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400140 RectAndSamplePoint fRectAndPoints;
141 SkRect fClip;
142 int fSampleCnt;
143 GrSurfaceOrigin fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700144 };
145 std::vector<TestCase> testCases;
146
Robert Phillipsbf25d432017-04-07 10:08:53 -0400147 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500148 for (int sampleCnt : {1, 4}) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400149 for (auto rectAndPoints : allRectsAndPoints) {
150 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
151 testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
152 }
Eric Karl72e551e2017-04-04 13:42:10 -0700153 }
Eric Karl74480882017-04-03 14:49:05 -0700154 }
155 }
156
157 // Run each test case:
158 for (auto testCase : testCases) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400159 int sampleCnt = testCase.fSampleCnt;
160 SkRect paintRect = testCase.fRectAndPoints.rect;
161 SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
162 SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
163 GrSurfaceOrigin origin = testCase.fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700164
165 sk_sp<GrTexture> backingSurface;
166 // BGRA forces a framebuffer blit on ES2.
167 sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
Greg Danielfaa095e2017-12-19 13:15:02 -0500168 context, sampleCnt, kWidth, kHeight, kColorType, kConfig, origin, &backingSurface);
Eric Karl74480882017-04-03 14:49:05 -0700169
Brian Salomonbdecacf2018-02-02 20:32:49 -0500170 if (!surface && sampleCnt > 1) {
Eric Karl74480882017-04-03 14:49:05 -0700171 // Some platforms don't support MSAA.
172 continue;
173 }
174 REPORTER_ASSERT(reporter, !!surface);
175
176 // Fill our canvas with 0xFFFF80
177 SkCanvas* canvas = surface->getCanvas();
Robert Phillipsbf25d432017-04-07 10:08:53 -0400178 canvas->clipRect(testCase.fClip, false);
Eric Karl74480882017-04-03 14:49:05 -0700179 SkPaint black_paint;
180 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
181 canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
182
183 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
184 // a copy of the destination.
185 SkPaint white_paint;
186 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
187 white_paint.setBlendMode(SkBlendMode::kMultiply);
188 canvas->drawRect(paintRect, white_paint);
189
190 // Read the result into a bitmap.
191 SkBitmap bitmap;
192 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
193 kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
Eric Karl74480882017-04-03 14:49:05 -0700194 REPORTER_ASSERT(
195 reporter,
196 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
197
198 // Check the in/out pixels.
199 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
200 SkColorSetRGB(0xFF, 0xFF, 0x80));
201 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
202 SkColorSetRGB(0x80, 0xFF, 0x80));
203
204 // Clean up - surface depends on backingSurface and must be released first.
Eric Karl74480882017-04-03 14:49:05 -0700205 surface.reset();
206 backingSurface.reset();
207 }
208}
209#endif