• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/gpu/GrTypes.h"
23 #include "include/private/base/SkTemplates.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26 #include "tools/gpu/BackendSurfaceFactory.h"
27 
28 #include <initializer_list>
29 #include <vector>
30 
31 struct GrContextOptions;
32 struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
33 
acceptable(const Results & r)34 static bool acceptable(const Results& r) {
35 #if 0
36     SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
37              r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
38 #endif
39     return r.diffs_by_1 == r.diffs   // never off by more than 1
40         && r.diffs_0x00 == 0         // transparent must stay transparent
41         && r.diffs_0xff == 0;        // opaque must stay opaque
42 }
43 
44 template <typename Fn>
test(Fn && multiply)45 static Results test(Fn&& multiply) {
46     Results r = { 0,0,0,0 };
47     for (int x = 0; x < 256; x++) {
48     for (int y = 0; y < 256; y++) {
49         int p = multiply(x, y),
50             ideal = (x*y+127)/255;
51         if (p != ideal) {
52             r.diffs++;
53             if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
54             if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
55             if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
56         }
57     }}
58     return r;
59 }
60 
DEF_TEST(Blend_byte_multiply,r)61 DEF_TEST(Blend_byte_multiply, r) {
62     // These are all temptingly close but fundamentally broken.
63     int (*broken[])(int, int) = {
64         [](int x, int y) { return (x*y)>>8; },
65         [](int x, int y) { return (x*y+128)>>8; },
66         [](int x, int y) { y += y>>7; return (x*y)>>8; },
67     };
68     for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
69 
70     // These are fine to use, but not perfect.
71     int (*fine[])(int, int) = {
72         [](int x, int y) { return (x*y+x)>>8; },
73         [](int x, int y) { return (x*y+y)>>8; },
74         [](int x, int y) { return (x*y+255)>>8; },
75         [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
76     };
77     for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
78 
79     // These are pefect.
80     int (*perfect[])(int, int) = {
81         [](int x, int y) { return (x*y+127)/255; },  // Duh.
82         [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
83         [](int x, int y) { return ((x*y+128)*257)>>16; },
84     };
85     for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
86 }
87 
88 // Tests blending to a surface with no texture available.
DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)89 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture,
90                                           reporter,
91                                           ctxInfo,
92                                           CtsEnforcement::kApiLevel_T) {
93     auto context = ctxInfo.directContext();
94     static constexpr SkISize kDimensions{10, 10};
95     const SkColorType kColorType = kRGBA_8888_SkColorType;
96 
97     // Build our test cases:
98     struct RectAndSamplePoint {
99         SkRect rect;
100         SkIPoint outPoint;
101         SkIPoint inPoint;
102     } allRectsAndPoints[3] = {
103             {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
104             {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
105             {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
106     };
107 
108     struct TestCase {
109         RectAndSamplePoint fRectAndPoints;
110         SkRect             fClip;
111         int                fSampleCnt;
112         GrSurfaceOrigin    fOrigin;
113     };
114     std::vector<TestCase> testCases;
115 
116     for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
117         for (int sampleCnt : {1, 4}) {
118             for (auto rectAndPoints : allRectsAndPoints) {
119                 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
120                     testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
121                 }
122             }
123         }
124     }
125 
126     // Run each test case:
127     for (auto testCase : testCases) {
128         int sampleCnt = testCase.fSampleCnt;
129         SkRect paintRect = testCase.fRectAndPoints.rect;
130         SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
131         SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
132         GrSurfaceOrigin origin = testCase.fOrigin;
133 
134         // BGRA forces a framebuffer blit on ES2.
135         sk_sp<SkSurface> surface = sk_gpu_test::MakeBackendRenderTargetSurface(context,
136                                                                                kDimensions,
137                                                                                origin,
138                                                                                sampleCnt,
139                                                                                kColorType);
140 
141         if (!surface && sampleCnt > 1) {
142             // Some platforms don't support MSAA.
143             continue;
144         }
145         REPORTER_ASSERT(reporter, !!surface);
146 
147         // Fill our canvas with 0xFFFF80
148         SkCanvas* canvas = surface->getCanvas();
149         canvas->clipRect(testCase.fClip, false);
150         SkPaint black_paint;
151         black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
152         canvas->drawRect(SkRect::Make(kDimensions), black_paint);
153 
154         // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
155         // a copy of the destination.
156         SkPaint white_paint;
157         white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
158         white_paint.setBlendMode(SkBlendMode::kMultiply);
159         canvas->drawRect(paintRect, white_paint);
160 
161         // Read the result into a bitmap.
162         SkBitmap bitmap;
163         REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(kDimensions, kColorType,
164                                                                           kPremul_SkAlphaType)));
165         REPORTER_ASSERT(
166                 reporter,
167                 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
168 
169         // Check the in/out pixels.
170         REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
171                                           SkColorSetRGB(0xFF, 0xFF, 0x80));
172         REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
173                                           SkColorSetRGB(0x80, 0xFF, 0x80));
174     }
175 }
176