blob: 8f688e2d69c440321b0a63925aff2328b0ac12d8 [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,
Robert Phillipsbf25d432017-04-07 10:08:53 -040087 GrSurfaceOrigin origin,
Eric Karl74480882017-04-03 14:49:05 -070088 sk_sp<GrTexture>* backingSurface) {
89 GrSurfaceDesc backingDesc;
Robert Phillips9a121cc2017-04-06 21:17:15 +000090 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -040091 backingDesc.fOrigin = origin;
92 backingDesc.fWidth = width;
93 backingDesc.fHeight = height;
94 backingDesc.fConfig = config;
95 backingDesc.fSampleCnt = sampleCnt;
Eric Karl74480882017-04-03 14:49:05 -070096
Robert Phillipse78b7252017-04-06 07:59:41 -040097 *backingSurface = context->resourceProvider()->createTexture(backingDesc, SkBudgeted::kNo);
Robert Phillipsbf25d432017-04-07 10:08:53 -040098 if (!(*backingSurface)) {
99 return nullptr;
100 }
Eric Karl74480882017-04-03 14:49:05 -0700101
102 GrBackendTextureDesc desc;
Robert Phillipsbf25d432017-04-07 10:08:53 -0400103 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
104 desc.fOrigin = origin;
Eric Karl74480882017-04-03 14:49:05 -0700105 desc.fWidth = width;
106 desc.fHeight = height;
Robert Phillipsbf25d432017-04-07 10:08:53 -0400107 desc.fConfig = config;
Robert Phillips9a121cc2017-04-06 21:17:15 +0000108 desc.fSampleCnt = sampleCnt;
Robert Phillipsbf25d432017-04-07 10:08:53 -0400109 desc.fTextureHandle = (*backingSurface)->getTextureHandle();
Eric Karl74480882017-04-03 14:49:05 -0700110 sk_sp<SkSurface> surface =
111 SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc, nullptr);
112 return surface;
113}
114}
115
116// Tests blending to a surface with no texture available.
117DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
118 GrContext* context = ctxInfo.grContext();
119 const int kWidth = 10;
120 const int kHeight = 10;
121 const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
122 const SkColorType kColorType = kRGBA_8888_SkColorType;
123
124 // Build our test cases:
125 struct RectAndSamplePoint {
126 SkRect rect;
127 SkIPoint outPoint;
128 SkIPoint inPoint;
129 } allRectsAndPoints[3] = {
130 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
Eric Karl72e551e2017-04-04 13:42:10 -0700131 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
Eric Karl74480882017-04-03 14:49:05 -0700132 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
133 };
134
135 struct TestCase {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400136 RectAndSamplePoint fRectAndPoints;
137 SkRect fClip;
138 int fSampleCnt;
139 GrSurfaceOrigin fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700140 };
141 std::vector<TestCase> testCases;
142
Robert Phillipsbf25d432017-04-07 10:08:53 -0400143 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
144 for (int sampleCnt : {0, 4}) {
145 for (auto rectAndPoints : allRectsAndPoints) {
146 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
147 testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
148 }
Eric Karl72e551e2017-04-04 13:42:10 -0700149 }
Eric Karl74480882017-04-03 14:49:05 -0700150 }
151 }
152
153 // Run each test case:
154 for (auto testCase : testCases) {
Robert Phillipsbf25d432017-04-07 10:08:53 -0400155 int sampleCnt = testCase.fSampleCnt;
156 SkRect paintRect = testCase.fRectAndPoints.rect;
157 SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
158 SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
159 GrSurfaceOrigin origin = testCase.fOrigin;
Eric Karl74480882017-04-03 14:49:05 -0700160
161 sk_sp<GrTexture> backingSurface;
162 // BGRA forces a framebuffer blit on ES2.
163 sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
Robert Phillipsbf25d432017-04-07 10:08:53 -0400164 context, sampleCnt, kWidth, kHeight, kConfig, origin, &backingSurface);
Eric Karl74480882017-04-03 14:49:05 -0700165
166 if (!surface && sampleCnt > 0) {
167 // Some platforms don't support MSAA.
168 continue;
169 }
170 REPORTER_ASSERT(reporter, !!surface);
171
172 // Fill our canvas with 0xFFFF80
173 SkCanvas* canvas = surface->getCanvas();
Robert Phillipsbf25d432017-04-07 10:08:53 -0400174 canvas->clipRect(testCase.fClip, false);
Eric Karl74480882017-04-03 14:49:05 -0700175 SkPaint black_paint;
176 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
177 canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
178
179 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
180 // a copy of the destination.
181 SkPaint white_paint;
182 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
183 white_paint.setBlendMode(SkBlendMode::kMultiply);
184 canvas->drawRect(paintRect, white_paint);
185
186 // Read the result into a bitmap.
187 SkBitmap bitmap;
188 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
189 kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
190 bitmap.lockPixels();
191 REPORTER_ASSERT(
192 reporter,
193 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
194
195 // Check the in/out pixels.
196 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
197 SkColorSetRGB(0xFF, 0xFF, 0x80));
198 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
199 SkColorSetRGB(0x80, 0xFF, 0x80));
200
201 // Clean up - surface depends on backingSurface and must be released first.
202 bitmap.unlockPixels();
203 surface.reset();
204 backingSurface.reset();
205 }
206}
207#endif