blob: bf985aa2ae5647b7fb9ce696708a987713bd154d [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 <functional>
9#include "SkBitmap.h"
10#include "SkCanvas.h"
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000011#include "SkColor.h"
mtklein767d2732015-07-16 07:01:39 -070012#include "SkColorPriv.h"
Eric Karl74480882017-04-03 14:49:05 -070013#include "SkSurface.h"
mtklein767d2732015-07-16 07:01:39 -070014#include "SkTaskGroup.h"
Eric Karl74480882017-04-03 14:49:05 -070015#include "SkUtils.h"
16#include "Test.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
20#include "GrContextPriv.h"
21#include "GrResourceProvider.h"
22#include "GrSurfaceContext.h"
23#include "GrSurfaceProxy.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000024#include "GrTest.h"
Eric Karl74480882017-04-03 14:49:05 -070025#include "GrTexture.h"
26#endif
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000027
mtklein195fe082015-11-17 08:39:01 -080028struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000029
mtklein195fe082015-11-17 08:39:01 -080030static bool acceptable(const Results& r) {
31#if 0
32 SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
33 r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
34#endif
35 return r.diffs_by_1 == r.diffs // never off by more than 1
36 && r.diffs_0x00 == 0 // transparent must stay transparent
37 && r.diffs_0xff == 0; // opaque must stay opaque
commit-bot@chromium.org40ac5e52014-02-26 21:40:07 +000038}
39
mtklein195fe082015-11-17 08:39:01 -080040template <typename Fn>
41static Results test(Fn&& multiply) {
42 Results r = { 0,0,0,0 };
43 for (int x = 0; x < 256; x++) {
44 for (int y = 0; y < 256; y++) {
45 int p = multiply(x, y),
46 ideal = (x*y+127)/255;
47 if (p != ideal) {
48 r.diffs++;
49 if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
50 if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
51 if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000052 }
mtklein195fe082015-11-17 08:39:01 -080053 }}
54 return r;
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000055}
56
mtklein195fe082015-11-17 08:39:01 -080057DEF_TEST(Blend_byte_multiply, r) {
58 // These are all temptingly close but fundamentally broken.
59 int (*broken[])(int, int) = {
60 [](int x, int y) { return (x*y)>>8; },
61 [](int x, int y) { return (x*y+128)>>8; },
62 [](int x, int y) { y += y>>7; return (x*y)>>8; },
63 };
64 for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000065
mtklein195fe082015-11-17 08:39:01 -080066 // These are fine to use, but not perfect.
67 int (*fine[])(int, int) = {
68 [](int x, int y) { return (x*y+x)>>8; },
69 [](int x, int y) { return (x*y+y)>>8; },
70 [](int x, int y) { return (x*y+255)>>8; },
71 [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
72 };
73 for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
74
75 // These are pefect.
76 int (*perfect[])(int, int) = {
77 [](int x, int y) { return (x*y+127)/255; }, // Duh.
78 [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
79 [](int x, int y) { return ((x*y+128)*257)>>16; },
80 };
81 for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000082}
Eric Karl74480882017-04-03 14:49:05 -070083
84#if SK_SUPPORT_GPU
85namespace {
86static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
Greg Danielfaa095e2017-12-19 13:15:02 -050087 GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
88 GrPixelConfig config, GrSurfaceOrigin origin,
Eric Karl74480882017-04-03 14:49:05 -070089 sk_sp<GrTexture>* backingSurface) {
90 GrSurfaceDesc backingDesc;
Robert Phillips9a121cc2017-04-06 21:17:15 +000091 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -040092 backingDesc.fOrigin = origin;
93 backingDesc.fWidth = width;
94 backingDesc.fHeight = height;
95 backingDesc.fConfig = config;
96 backingDesc.fSampleCnt = sampleCnt;
Eric Karl74480882017-04-03 14:49:05 -070097
Robert Phillipse78b7252017-04-06 07:59:41 -040098 *backingSurface = context->resourceProvider()->createTexture(backingDesc, SkBudgeted::kNo);
Robert Phillipsbf25d432017-04-07 10:08:53 -040099 if (!(*backingSurface)) {
100 return nullptr;
101 }
Eric Karl74480882017-04-03 14:49:05 -0700102
Robert Phillipsb67821d2017-12-13 15:00:45 -0500103 GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
104
Eric Karl74480882017-04-03 14:49:05 -0700105 sk_sp<SkSurface> surface =
Greg Daniel7ef28f32017-04-20 16:41:55 +0000106 SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
Greg Danielfaa095e2017-12-19 13:15:02 -0500107 sampleCnt, colorType, nullptr, nullptr);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000108
Eric Karl74480882017-04-03 14:49:05 -0700109 return surface;
110}
111}
112
113// Tests blending to a surface with no texture available.
114DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
115 GrContext* context = ctxInfo.grContext();
116 const int kWidth = 10;
117 const int kHeight = 10;
118 const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
119 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}) {
141 for (int sampleCnt : {0, 4}) {
142 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(
Greg Danielfaa095e2017-12-19 13:15:02 -0500161 context, sampleCnt, kWidth, kHeight, kColorType, kConfig, origin, &backingSurface);
Eric Karl74480882017-04-03 14:49:05 -0700162
163 if (!surface && sampleCnt > 0) {
164 // 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));
174 canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
175
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;
185 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
186 kWidth, kHeight, kColorType, 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}
202#endif