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 "src/text/gpu/DistanceFieldAdjustTable.h"
9
10 #include "src/core/SkScalerContext.h"
11
12 using namespace skia_private;
13
14 namespace sktext::gpu {
15
SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize=8;)16 SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;)
17
18 SkScalar* build_distance_adjust_table(SkScalar paintGamma, SkScalar deviceGamma) {
19 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
20 // text. The mask gamma hack is based off of guessing what the blend color is going to
21 // be, and adjusting the mask so that when run through the linear blend will
22 // produce the value closest to the desired result. However, in practice this means
23 // that the 'adjusted' mask is just increasing or decreasing the coverage of
24 // the mask depending on what it is thought it will blit against. For black (on
25 // assumed white) this means that coverages are decreased (on a curve). For white (on
26 // assumed black) this means that coverages are increased (on a a curve). At
27 // middle (perceptual) gray (which could be blit against anything) the coverages
28 // remain the same.
29 //
30 // The idea here is that instead of determining the initial (real) coverage and
31 // then adjusting that coverage, we determine an adjusted coverage directly by
32 // essentially manipulating the geometry (in this case, the distance to the glyph
33 // edge). So for black (on assumed white) this thins a bit; for white (on
34 // assumed black) this fake bolds the geometry a bit.
35 //
36 // The distance adjustment is calculated by determining the actual coverage value which
37 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
38 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
39 // actual edge. So by subtracting this distance adjustment and computing without the
40 // the coverage adjustment we should get 0.5 coverage at the same point.
41 //
42 // This has several implications:
43 // For non-gray lcd smoothed text, each subpixel essentially is using a
44 // slightly different geometry.
45 //
46 // For black (on assumed white) this may not cover some pixels which were
47 // previously covered; however those pixels would have been only slightly
48 // covered and that slight coverage would have been decreased anyway. Also, some pixels
49 // which were previously fully covered may no longer be fully covered.
50 //
51 // For white (on assumed black) this may cover some pixels which weren't
52 // previously covered at all.
53
54 int width, height;
55 size_t size;
56
57 #ifdef SK_GAMMA_CONTRAST
58 SkScalar contrast = SK_GAMMA_CONTRAST;
59 #else
60 SkScalar contrast = 0.5f;
61 #endif
62
63 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
64 &width, &height);
65
66 SkASSERT(kExpectedDistanceAdjustTableSize == height);
67 SkScalar* table = new SkScalar[height];
68
69 AutoTArray<uint8_t> data((int)size);
70 if (!SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get())) {
71 // if no valid data is available simply do no adjustment
72 for (int row = 0; row < height; ++row) {
73 table[row] = 0;
74 }
75 return table;
76 }
77
78 // find the inverse points where we cross 0.5
79 // binsearch might be better, but we only need to do this once on creation
80 for (int row = 0; row < height; ++row) {
81 uint8_t* rowPtr = data.get() + row*width;
82 for (int col = 0; col < width - 1; ++col) {
83 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
84 // compute point where a mask value will give us a result of 0.5
85 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
86 float borderAlpha = (col + interp) / 255.f;
87
88 // compute t value for that alpha
89 // this is an approximate inverse for smoothstep()
90 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
91
92 // compute distance which gives us that t value
93 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
94 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
95
96 table[row] = d;
97 break;
98 }
99 }
100 }
101
102 return table;
103 }
104
Get()105 const DistanceFieldAdjustTable* DistanceFieldAdjustTable::Get() {
106 static const DistanceFieldAdjustTable* dfat = new DistanceFieldAdjustTable;
107 return dfat;
108 }
109
DistanceFieldAdjustTable()110 DistanceFieldAdjustTable::DistanceFieldAdjustTable() {
111 fTable = build_distance_adjust_table(SK_GAMMA_EXPONENT, SK_GAMMA_EXPONENT);
112 fGammaCorrectTable = build_distance_adjust_table(SK_Scalar1, SK_Scalar1);
113 }
114
115 } // namespace sktext::gpu
116