1 /*
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
8 #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"
20 #include "include/gpu/GrBackendSurface.h"
21 #include "include/gpu/GrContext.h"
22 #include "include/gpu/GrTexture.h"
23 #include "include/gpu/GrTypes.h"
24 #include "include/private/GrTypesPriv.h"
25 #include "src/gpu/GrContextPriv.h"
26 #include "src/gpu/GrResourceProvider.h"
27 #include "tests/Test.h"
28 #include "tools/gpu/GrContextFactory.h"
29
30 #include <initializer_list>
31 #include <vector>
32
33 struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
34
acceptable(const Results & r)35 static 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
43 }
44
45 template <typename Fn>
test(Fn && multiply)46 static 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++; }
57 }
58 }}
59 return r;
60 }
61
DEF_TEST(Blend_byte_multiply,r)62 DEF_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))); }
70
71 // 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); }
87 }
88
89 namespace {
create_gpu_surface_backend_texture_as_render_target(GrContext * context,int sampleCnt,int width,int height,SkColorType colorType,GrSurfaceOrigin origin,sk_sp<GrTexture> * backingSurface)90 static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
91 GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
92 GrSurfaceOrigin origin, sk_sp<GrTexture>* backingSurface) {
93 GrSurfaceDesc backingDesc;
94 backingDesc.fWidth = width;
95 backingDesc.fHeight = height;
96 auto ct = SkColorTypeToGrColorType(colorType);
97 backingDesc.fConfig = GrColorTypeToPixelConfig(ct);
98 auto format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
99
100 auto resourceProvider = context->priv().resourceProvider();
101
102 *backingSurface = resourceProvider->createTexture(backingDesc, format, GrRenderable::kYes,
103 sampleCnt, SkBudgeted::kNo, GrProtected::kNo,
104 GrResourceProvider::Flags::kNoPendingIO);
105 if (!(*backingSurface)) {
106 return nullptr;
107 }
108
109 GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
110
111 sk_sp<SkSurface> surface =
112 SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
113 sampleCnt, colorType, nullptr, nullptr);
114
115 return surface;
116 }
117 }
118
119 // Tests blending to a surface with no texture available.
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture,reporter,ctxInfo)120 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
121 GrContext* context = ctxInfo.grContext();
122 const int kWidth = 10;
123 const int kHeight = 10;
124 const SkColorType kColorType = kRGBA_8888_SkColorType;
125
126 // Build our test cases:
127 struct RectAndSamplePoint {
128 SkRect rect;
129 SkIPoint outPoint;
130 SkIPoint inPoint;
131 } allRectsAndPoints[3] = {
132 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
133 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
134 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
135 };
136
137 struct TestCase {
138 RectAndSamplePoint fRectAndPoints;
139 SkRect fClip;
140 int fSampleCnt;
141 GrSurfaceOrigin fOrigin;
142 };
143 std::vector<TestCase> testCases;
144
145 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
146 for (int sampleCnt : {1, 4}) {
147 for (auto rectAndPoints : allRectsAndPoints) {
148 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
149 testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
150 }
151 }
152 }
153 }
154
155 // Run each test case:
156 for (auto testCase : testCases) {
157 int sampleCnt = testCase.fSampleCnt;
158 SkRect paintRect = testCase.fRectAndPoints.rect;
159 SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
160 SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
161 GrSurfaceOrigin origin = testCase.fOrigin;
162
163 sk_sp<GrTexture> backingSurface;
164 // BGRA forces a framebuffer blit on ES2.
165 sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
166 context, sampleCnt, kWidth, kHeight, kColorType, origin, &backingSurface);
167
168 if (!surface && sampleCnt > 1) {
169 // Some platforms don't support MSAA.
170 continue;
171 }
172 REPORTER_ASSERT(reporter, !!surface);
173
174 // Fill our canvas with 0xFFFF80
175 SkCanvas* canvas = surface->getCanvas();
176 canvas->clipRect(testCase.fClip, false);
177 SkPaint black_paint;
178 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
179 canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
180
181 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
182 // a copy of the destination.
183 SkPaint white_paint;
184 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
185 white_paint.setBlendMode(SkBlendMode::kMultiply);
186 canvas->drawRect(paintRect, white_paint);
187
188 // Read the result into a bitmap.
189 SkBitmap bitmap;
190 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
191 kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
192 REPORTER_ASSERT(
193 reporter,
194 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
195
196 // Check the in/out pixels.
197 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
198 SkColorSetRGB(0xFF, 0xFF, 0x80));
199 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
200 SkColorSetRGB(0x80, 0xFF, 0x80));
201
202 // Clean up - surface depends on backingSurface and must be released first.
203 surface.reset();
204 backingSurface.reset();
205 }
206 }
207