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