epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkAntiRun.h" |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 11 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 13 | void SkAlphaRuns::reset(int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | SkASSERT(width > 0); |
| 15 | |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 16 | #ifdef SK_DEBUG |
| 17 | sk_memset16((uint16_t*)fRuns, (uint16_t)(-42), width); |
| 18 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | fRuns[0] = SkToS16(width); |
| 20 | fRuns[width] = 0; |
| 21 | fAlpha[0] = 0; |
| 22 | |
| 23 | SkDEBUGCODE(fWidth = width;) |
| 24 | SkDEBUGCODE(this->validate();) |
| 25 | } |
| 26 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 27 | void SkAlphaRuns::Break(int16_t runs[], uint8_t alpha[], int x, int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | SkASSERT(count > 0 && x >= 0); |
| 29 | |
| 30 | // SkAlphaRuns::BreakAt(runs, alpha, x); |
| 31 | // SkAlphaRuns::BreakAt(&runs[x], &alpha[x], count); |
| 32 | |
| 33 | int16_t* next_runs = runs + x; |
| 34 | uint8_t* next_alpha = alpha + x; |
| 35 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 36 | while (x > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | int n = runs[0]; |
| 38 | SkASSERT(n > 0); |
| 39 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 40 | if (x < n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | alpha[x] = alpha[0]; |
| 42 | runs[0] = SkToS16(x); |
| 43 | runs[x] = SkToS16(n - x); |
| 44 | break; |
| 45 | } |
| 46 | runs += n; |
| 47 | alpha += n; |
| 48 | x -= n; |
| 49 | } |
| 50 | |
| 51 | runs = next_runs; |
| 52 | alpha = next_alpha; |
| 53 | x = count; |
| 54 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 55 | for (;;) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | int n = runs[0]; |
| 57 | SkASSERT(n > 0); |
| 58 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 59 | if (x < n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | alpha[x] = alpha[0]; |
| 61 | runs[0] = SkToS16(x); |
| 62 | runs[x] = SkToS16(n - x); |
| 63 | break; |
| 64 | } |
| 65 | x -= n; |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 66 | if (x <= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | break; |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 68 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | runs += n; |
| 70 | alpha += n; |
| 71 | } |
| 72 | } |
| 73 | |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 74 | int SkAlphaRuns::add(int x, U8CPU startAlpha, int middleCount, U8CPU stopAlpha, |
| 75 | U8CPU maxValue, int offsetX) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkASSERT(middleCount >= 0); |
| 77 | SkASSERT(x >= 0 && x + (startAlpha != 0) + middleCount + (stopAlpha != 0) <= fWidth); |
| 78 | |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 79 | SkASSERT(fRuns[offsetX] >= 0); |
| 80 | |
| 81 | int16_t* runs = fRuns + offsetX; |
| 82 | uint8_t* alpha = fAlpha + offsetX; |
| 83 | uint8_t* lastAlpha = alpha; |
| 84 | x -= offsetX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 86 | if (startAlpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | SkAlphaRuns::Break(runs, alpha, x, 1); |
| 88 | /* I should be able to just add alpha[x] + startAlpha. |
| 89 | However, if the trailing edge of the previous span and the leading |
| 90 | edge of the current span round to the same super-sampled x value, |
| 91 | I might overflow to 256 with this add, hence the funny subtract (crud). |
| 92 | */ |
| 93 | unsigned tmp = alpha[x] + startAlpha; |
| 94 | SkASSERT(tmp <= 256); |
| 95 | alpha[x] = SkToU8(tmp - (tmp >> 8)); // was (tmp >> 7), but that seems wrong if we're trying to catch 256 |
| 96 | |
| 97 | runs += x + 1; |
| 98 | alpha += x + 1; |
| 99 | x = 0; |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 100 | lastAlpha += x; // we don't want the +1 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | SkDEBUGCODE(this->validate();) |
| 102 | } |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 103 | |
| 104 | if (middleCount) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | SkAlphaRuns::Break(runs, alpha, x, middleCount); |
| 106 | alpha += x; |
| 107 | runs += x; |
| 108 | x = 0; |
| 109 | do { |
| 110 | alpha[0] = SkToU8(alpha[0] + maxValue); |
| 111 | int n = runs[0]; |
| 112 | SkASSERT(n <= middleCount); |
| 113 | alpha += n; |
| 114 | runs += n; |
| 115 | middleCount -= n; |
| 116 | } while (middleCount > 0); |
| 117 | SkDEBUGCODE(this->validate();) |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 118 | lastAlpha = alpha; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | } |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 120 | |
| 121 | if (stopAlpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | SkAlphaRuns::Break(runs, alpha, x, 1); |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 123 | alpha += x; |
| 124 | alpha[0] = SkToU8(alpha[0] + stopAlpha); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | SkDEBUGCODE(this->validate();) |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 126 | lastAlpha = alpha; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | } |
reed@google.com | fa57ae7 | 2011-05-31 19:18:02 +0000 | [diff] [blame] | 128 | |
| 129 | return lastAlpha - fAlpha; // new offsetX |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | #ifdef SK_DEBUG |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 133 | void SkAlphaRuns::assertValid(int y, int maxStep) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | int max = (y + 1) * maxStep - (y == maxStep - 1); |
| 135 | |
| 136 | const int16_t* runs = fRuns; |
| 137 | const uint8_t* alpha = fAlpha; |
| 138 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 139 | while (*runs) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | SkASSERT(*alpha <= max); |
| 141 | alpha += *runs; |
| 142 | runs += *runs; |
| 143 | } |
| 144 | } |
| 145 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 146 | void SkAlphaRuns::dump() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | const int16_t* runs = fRuns; |
| 148 | const uint8_t* alpha = fAlpha; |
| 149 | |
| 150 | SkDebugf("Runs"); |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 151 | while (*runs) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | int n = *runs; |
| 153 | |
| 154 | SkDebugf(" %02x", *alpha); |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 155 | if (n > 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | SkDebugf(",%d", n); |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 157 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | alpha += n; |
| 159 | runs += n; |
| 160 | } |
| 161 | SkDebugf("\n"); |
| 162 | } |
| 163 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 164 | void SkAlphaRuns::validate() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | SkASSERT(fWidth > 0); |
| 166 | |
| 167 | int count = 0; |
| 168 | const int16_t* runs = fRuns; |
| 169 | |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame] | 170 | while (*runs) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | SkASSERT(*runs > 0); |
| 172 | count += *runs; |
| 173 | SkASSERT(count <= fWidth); |
| 174 | runs += *runs; |
| 175 | } |
| 176 | SkASSERT(count == fWidth); |
| 177 | } |
| 178 | #endif |
| 179 | |