1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "SkClampRange.h" 18 19 /* 20 * returns [0..count] for the number of steps (<= count) for which x0 <= edge 21 * given each step is followed by x0 += dx 22 */ 23 static int chop(int64_t x0, SkFixed edge, int64_t x1, int64_t dx, int count) { 24 SkASSERT(dx > 0); 25 SkASSERT(count >= 0); 26 27 if (x0 >= edge) { 28 return 0; 29 } 30 if (x1 <= edge) { 31 return count; 32 } 33 int64_t n = (edge - x0 + dx - 1) / dx; 34 SkASSERT(n >= 0); 35 SkASSERT(n <= count); 36 return (int)n; 37 } 38 39 static bool overflows_fixed(int64_t x) { 40 return x < -SK_FixedMax || x > SK_FixedMax; 41 } 42 43 void SkClampRange::initFor1(SkFixed fx) { 44 fCount0 = fCount1 = fCount2 = 0; 45 if (fx <= 0) { 46 fCount0 = 1; 47 } else if (fx < 0xFFFF) { 48 fCount1 = 1; 49 fFx1 = fx; 50 } else { 51 fCount2 = 1; 52 } 53 } 54 55 void SkClampRange::init(SkFixed fx0, SkFixed dx0, int count, int v0, int v1) { 56 SkASSERT(count > 0); 57 58 fV0 = v0; 59 fV1 = v1; 60 fOverflowed = false; 61 62 // special case 1 == count, as it is slightly common for skia 63 // and avoids us ever calling divide or 64bit multiply 64 if (1 == count) { 65 this->initFor1(fx0); 66 return; 67 } 68 69 int64_t fx = fx0; 70 int64_t dx = dx0; 71 // start with ex equal to the last computed value 72 int64_t ex = fx + (count - 1) * dx; 73 fOverflowed = overflows_fixed(ex); 74 75 if ((uint64_t)(fx | ex) <= 0xFFFF) { 76 fCount0 = fCount2 = 0; 77 fCount1 = count; 78 fFx1 = fx0; 79 return; 80 } 81 if (fx <= 0 && ex <= 0) { 82 fCount1 = fCount2 = 0; 83 fCount0 = count; 84 return; 85 } 86 if (fx >= 0xFFFF && ex >= 0xFFFF) { 87 fCount0 = fCount1 = 0; 88 fCount2 = count; 89 return; 90 } 91 92 int extraCount = 0; 93 94 // now make ex be 1 past the last computed value 95 ex += dx; 96 fOverflowed = overflows_fixed(ex); 97 // now check for over/under flow 98 if (fOverflowed) { 99 int originalCount = count; 100 int64_t ccount; 101 bool swap = dx < 0; 102 if (swap) { 103 dx = -dx; 104 fx = -fx; 105 } 106 ccount = (SK_FixedMax - fx + dx - 1) / dx; 107 if (swap) { 108 dx = -dx; 109 fx = -fx; 110 } 111 SkASSERT(ccount > 0 && ccount <= SK_FixedMax); 112 113 count = (int)ccount; 114 if (0 == count) { 115 this->initFor1(fx0); 116 if (dx > 0) { 117 fCount2 += originalCount - 1; 118 } else { 119 fCount0 += originalCount - 1; 120 } 121 return; 122 } 123 extraCount = originalCount - count; 124 ex = fx + dx * count; 125 } 126 127 bool doSwap = dx < 0; 128 129 if (doSwap) { 130 ex -= dx; 131 fx -= dx; 132 SkTSwap(fx, ex); 133 dx = -dx; 134 } 135 136 137 fCount0 = chop(fx, 0, ex, dx, count); 138 count -= fCount0; 139 fx += fCount0 * dx; 140 SkASSERT(fx >= 0); 141 SkASSERT(fCount0 == 0 || (fx - dx) < 0); 142 fCount1 = chop(fx, 0xFFFF, ex, dx, count); 143 count -= fCount1; 144 fCount2 = count; 145 146 #ifdef SK_DEBUG 147 fx += fCount1 * dx; 148 SkASSERT(fx <= ex); 149 if (fCount2 > 0) { 150 SkASSERT(fx >= 0xFFFF); 151 if (fCount1 > 0) { 152 SkASSERT(fx - dx < 0xFFFF); 153 } 154 } 155 #endif 156 157 if (doSwap) { 158 SkTSwap(fCount0, fCount2); 159 SkTSwap(fV0, fV1); 160 dx = -dx; 161 } 162 163 if (fCount1 > 0) { 164 fFx1 = fx0 + fCount0 * (int)dx; 165 } 166 167 if (dx > 0) { 168 fCount2 += extraCount; 169 } else { 170 fCount0 += extraCount; 171 } 172 } 173 174