aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkAntiRun.h
blob: 12930e66759221b290b0fcdcedfc45341d140503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* libs/graphics/sgl/SkAntiRun.h
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

#ifndef SkAntiRun_DEFINED
#define SkAntiRun_DEFINED

#include "SkBlitter.h"

inline int sk_make_nonzero_neg_one(int x)
{
    return (x | -x) >> 31;
}

#if 0
template <int kShift> class SkAntiRun {
    static uint8_t coverage_to_alpha(int aa)
    {
        aa <<= 8 - 2*kShift;
        aa -= aa >> (8 - kShift - 1);
        return SkToU8(aa);
    }
public:
    void set(int start, int stop)
    {
        SkASSERT(start >= 0 && stop > start);

#if 1
        int fb = start & kMask;
        int fe = stop & kMask;
        int n = (stop >> kShift) - (start >> kShift) - 1;

        if (n < 0)
        {
            fb = fe - fb;
            n = 0;
            fe = 0;
        }
        else
        {
            if (fb == 0)
                n += 1;
            else
                fb = (1 << kShift) - fb;
        }

        fStartAlpha = coverage_to_alpha(fb);
        fMiddleCount = n;
        fStopAlpha = coverage_to_alpha(fe);
#else
        int x0 = start >> kShift;
        int x1 = (stop - 1) >> kShift;
        int middle = x1 - x0;
        int aa;

        if (middle == 0)
        {
            aa = stop - start;
            aa <<= 8 - 2*kShift;
            aa -= aa >> (8 - kShift - 1);
            SkASSERT(aa > 0 && aa < kMax);
            fStartAlpha = SkToU8(aa);
            fMiddleCount = 0;
            fStopAlpha = 0;
        }
        else
        {
            int aa = start & kMask;
            aa <<= 8 - 2*kShift;
            aa -= aa >> (8 - kShift - 1);
            SkASSERT(aa >= 0 && aa < kMax);
            if (aa)
                fStartAlpha = SkToU8(kMax - aa);
            else
            {
                fStartAlpha = 0;
                middle += 1;
            }
            aa = stop & kMask;
            aa <<= 8 - 2*kShift;
            aa -= aa >> (8 - kShift - 1);
            SkASSERT(aa >= 0 && aa < kMax);
            middle += sk_make_nonzero_neg_one(aa);

            fStopAlpha = SkToU8(aa);
            fMiddleCount = middle;
        }
        SkASSERT(fStartAlpha < kMax);
        SkASSERT(fStopAlpha < kMax);
#endif
    }

    void blit(int x, int y, SkBlitter* blitter)
    {
        int16_t runs[2];
        runs[0] = 1;
        runs[1] = 0;

        if (fStartAlpha)
        {
            blitter->blitAntiH(x, y, &fStartAlpha, runs);
            x += 1;
        }
        if (fMiddleCount)
        {
            blitter->blitH(x, y, fMiddleCount);
            x += fMiddleCount;
        }
        if (fStopAlpha)
            blitter->blitAntiH(x, y, &fStopAlpha, runs);
    }

    uint8_t  getStartAlpha() const { return fStartAlpha; }
    int getMiddleCount() const { return fMiddleCount; }
    uint8_t  getStopAlpha() const { return fStopAlpha; }

private:
    uint8_t  fStartAlpha, fStopAlpha;
    int fMiddleCount;

    enum {
        kMask = (1 << kShift) - 1,
        kMax = (1 << (8 - kShift)) - 1
    };
};
#endif

////////////////////////////////////////////////////////////////////////////////////

class SkAlphaRuns {
public:
    int16_t*    fRuns;
    uint8_t*     fAlpha;

    bool    empty() const
    {
        SkASSERT(fRuns[0] > 0);
        return fAlpha[0] == 0 && fRuns[fRuns[0]] == 0;
    }
    void    reset(int width);
    void    add(int x, U8CPU startAlpha, int middleCount, U8CPU stopAlpha, U8CPU maxValue);
    SkDEBUGCODE(void assertValid(int y, int maxStep) const;)
    SkDEBUGCODE(void dump() const;)

    static void Break(int16_t runs[], uint8_t alpha[], int x, int count);
    static void BreakAt(int16_t runs[], uint8_t alpha[], int x)
    {
        while (x > 0)
        {
            int n = runs[0];
            SkASSERT(n > 0);

            if (x < n)
            {
                alpha[x] = alpha[0];
                runs[0] = SkToS16(x);
                runs[x] = SkToS16(n - x);
                break;
            }
            runs += n;
            alpha += n;
            x -= n;
        }
    }

private:
    SkDEBUGCODE(int fWidth;)
    SkDEBUGCODE(void validate() const;)
};

#endif