blob: 2a96f860c6e0c2157b9b1788e81bca4d143492a3 [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"
24#include "GrTexture.h"
25#endif
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000026
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
83#if SK_SUPPORT_GPU
84namespace {
85static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
86 GrContext* context, int sampleCnt, int width, int height, GrPixelConfig config,
87 sk_sp<GrTexture>* backingSurface) {
88 GrSurfaceDesc backingDesc;
89 backingDesc.fHeight = height;
90 backingDesc.fWidth = width;
91 backingDesc.fConfig = config;
92 backingDesc.fOrigin = kDefault_GrSurfaceOrigin;
93 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
94
95 (*backingSurface)
96 .reset(context->resourceProvider()->createTexture(backingDesc, SkBudgeted::kNo));
97
98 GrBackendTextureDesc desc;
99 desc.fConfig = config;
100 desc.fWidth = width;
101 desc.fHeight = height;
102 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
103 desc.fTextureHandle = (*backingSurface)->getTextureHandle();
104 desc.fSampleCnt = sampleCnt;
105 sk_sp<SkSurface> surface =
106 SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc, nullptr);
107 return surface;
108}
109}
110
111// Tests blending to a surface with no texture available.
112DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
113 GrContext* context = ctxInfo.grContext();
114 const int kWidth = 10;
115 const int kHeight = 10;
116 const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
117 const SkColorType kColorType = kRGBA_8888_SkColorType;
118
119 // Build our test cases:
120 struct RectAndSamplePoint {
121 SkRect rect;
122 SkIPoint outPoint;
123 SkIPoint inPoint;
124 } allRectsAndPoints[3] = {
125 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
126 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(0, 0), SkIPoint::Make(4, 4)},
127 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
128 };
129
130 struct TestCase {
131 RectAndSamplePoint rectAndPoints;
132 int sampleCnt;
133 };
134 std::vector<TestCase> testCases;
135
136 for (int sampleCnt : {0, 4}) {
137 for (auto rectAndPoints : allRectsAndPoints) {
138 testCases.push_back({rectAndPoints, sampleCnt});
139 }
140 }
141
142 // Run each test case:
143 for (auto testCase : testCases) {
144 int sampleCnt = testCase.sampleCnt;
145 SkRect paintRect = testCase.rectAndPoints.rect;
146 SkIPoint outPoint = testCase.rectAndPoints.outPoint;
147 SkIPoint inPoint = testCase.rectAndPoints.inPoint;
148
149 sk_sp<GrTexture> backingSurface;
150 // BGRA forces a framebuffer blit on ES2.
151 sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
152 context, sampleCnt, kWidth, kHeight, kConfig, &backingSurface);
153
154 if (!surface && sampleCnt > 0) {
155 // Some platforms don't support MSAA.
156 continue;
157 }
158 REPORTER_ASSERT(reporter, !!surface);
159
160 // Fill our canvas with 0xFFFF80
161 SkCanvas* canvas = surface->getCanvas();
162 SkPaint black_paint;
163 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
164 canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
165
166 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
167 // a copy of the destination.
168 SkPaint white_paint;
169 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
170 white_paint.setBlendMode(SkBlendMode::kMultiply);
171 canvas->drawRect(paintRect, white_paint);
172
173 // Read the result into a bitmap.
174 SkBitmap bitmap;
175 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
176 kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
177 bitmap.lockPixels();
178 REPORTER_ASSERT(
179 reporter,
180 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
181
182 // Check the in/out pixels.
183 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
184 SkColorSetRGB(0xFF, 0xFF, 0x80));
185 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
186 SkColorSetRGB(0x80, 0xFF, 0x80));
187
188 // Clean up - surface depends on backingSurface and must be released first.
189 bitmap.unlockPixels();
190 surface.reset();
191 backingSurface.reset();
192 }
193}
194#endif