SkScan_Antihair.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Copyright 2011 The Android Open Source Project
  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. #include "src/core/SkScan.h"
  8. #include "include/private/SkColorData.h"
  9. #include "include/private/SkTo.h"
  10. #include "src/core/SkBlitter.h"
  11. #include "src/core/SkFDot6.h"
  12. #include "src/core/SkLineClipper.h"
  13. #include "src/core/SkRasterClip.h"
  14. #include <utility>
  15. /* Our attempt to compute the worst case "bounds" for the horizontal and
  16. vertical cases has some numerical bug in it, and we sometimes undervalue
  17. our extends. The bug is that when this happens, we will set the clip to
  18. nullptr (for speed), and thus draw outside of the clip by a pixel, which might
  19. only look bad, but it might also access memory outside of the valid range
  20. allcoated for the device bitmap.
  21. This define enables our fix to outset our "bounds" by 1, thus avoiding the
  22. chance of the bug, but at the cost of sometimes taking the rectblitter
  23. case (i.e. not setting the clip to nullptr) when we might not actually need
  24. to. If we can improve/fix the actual calculations, then we can remove this
  25. step.
  26. */
  27. #define OUTSET_BEFORE_CLIP_TEST true
  28. #define HLINE_STACK_BUFFER 100
  29. static inline int SmallDot6Scale(int value, int dot6) {
  30. SkASSERT((int16_t)value == value);
  31. SkASSERT((unsigned)dot6 <= 64);
  32. return (value * dot6) >> 6;
  33. }
  34. //#define TEST_GAMMA
  35. #ifdef TEST_GAMMA
  36. static uint8_t gGammaTable[256];
  37. #define ApplyGamma(table, alpha) (table)[alpha]
  38. static void build_gamma_table() {
  39. static bool gInit = false;
  40. if (gInit == false) {
  41. for (int i = 0; i < 256; i++) {
  42. SkFixed n = i * 257;
  43. n += n >> 15;
  44. SkASSERT(n >= 0 && n <= SK_Fixed1);
  45. n = SkFixedSqrt(n);
  46. n = n * 255 >> 16;
  47. // SkDebugf("morph %d -> %d\n", i, n);
  48. gGammaTable[i] = SkToU8(n);
  49. }
  50. gInit = true;
  51. }
  52. }
  53. #else
  54. #define ApplyGamma(table, alpha) SkToU8(alpha)
  55. #endif
  56. ///////////////////////////////////////////////////////////////////////////////
  57. static void call_hline_blitter(SkBlitter* blitter, int x, int y, int count,
  58. U8CPU alpha) {
  59. SkASSERT(count > 0);
  60. int16_t runs[HLINE_STACK_BUFFER + 1];
  61. uint8_t aa[HLINE_STACK_BUFFER];
  62. aa[0] = ApplyGamma(gGammaTable, alpha);
  63. do {
  64. int n = count;
  65. if (n > HLINE_STACK_BUFFER) {
  66. n = HLINE_STACK_BUFFER;
  67. }
  68. runs[0] = SkToS16(n);
  69. runs[n] = 0;
  70. blitter->blitAntiH(x, y, aa, runs);
  71. x += n;
  72. count -= n;
  73. } while (count > 0);
  74. }
  75. class SkAntiHairBlitter {
  76. public:
  77. SkAntiHairBlitter() : fBlitter(nullptr) {}
  78. virtual ~SkAntiHairBlitter() {}
  79. SkBlitter* getBlitter() const { return fBlitter; }
  80. void setup(SkBlitter* blitter) {
  81. fBlitter = blitter;
  82. }
  83. virtual SkFixed drawCap(int x, SkFixed fy, SkFixed slope, int mod64) = 0;
  84. virtual SkFixed drawLine(int x, int stopx, SkFixed fy, SkFixed slope) = 0;
  85. private:
  86. SkBlitter* fBlitter;
  87. };
  88. class HLine_SkAntiHairBlitter : public SkAntiHairBlitter {
  89. public:
  90. SkFixed drawCap(int x, SkFixed fy, SkFixed slope, int mod64) override {
  91. fy += SK_Fixed1/2;
  92. int y = fy >> 16;
  93. uint8_t a = (uint8_t)((fy >> 8) & 0xFF);
  94. // lower line
  95. unsigned ma = SmallDot6Scale(a, mod64);
  96. if (ma) {
  97. call_hline_blitter(this->getBlitter(), x, y, 1, ma);
  98. }
  99. // upper line
  100. ma = SmallDot6Scale(255 - a, mod64);
  101. if (ma) {
  102. call_hline_blitter(this->getBlitter(), x, y - 1, 1, ma);
  103. }
  104. return fy - SK_Fixed1/2;
  105. }
  106. virtual SkFixed drawLine(int x, int stopx, SkFixed fy,
  107. SkFixed slope) override {
  108. SkASSERT(x < stopx);
  109. int count = stopx - x;
  110. fy += SK_Fixed1/2;
  111. int y = fy >> 16;
  112. uint8_t a = (uint8_t)((fy >> 8) & 0xFF);
  113. // lower line
  114. if (a) {
  115. call_hline_blitter(this->getBlitter(), x, y, count, a);
  116. }
  117. // upper line
  118. a = 255 - a;
  119. if (a) {
  120. call_hline_blitter(this->getBlitter(), x, y - 1, count, a);
  121. }
  122. return fy - SK_Fixed1/2;
  123. }
  124. };
  125. class Horish_SkAntiHairBlitter : public SkAntiHairBlitter {
  126. public:
  127. SkFixed drawCap(int x, SkFixed fy, SkFixed dy, int mod64) override {
  128. fy += SK_Fixed1/2;
  129. int lower_y = fy >> 16;
  130. uint8_t a = (uint8_t)((fy >> 8) & 0xFF);
  131. unsigned a0 = SmallDot6Scale(255 - a, mod64);
  132. unsigned a1 = SmallDot6Scale(a, mod64);
  133. this->getBlitter()->blitAntiV2(x, lower_y - 1, a0, a1);
  134. return fy + dy - SK_Fixed1/2;
  135. }
  136. SkFixed drawLine(int x, int stopx, SkFixed fy, SkFixed dy) override {
  137. SkASSERT(x < stopx);
  138. fy += SK_Fixed1/2;
  139. SkBlitter* blitter = this->getBlitter();
  140. do {
  141. int lower_y = fy >> 16;
  142. uint8_t a = (uint8_t)((fy >> 8) & 0xFF);
  143. blitter->blitAntiV2(x, lower_y - 1, 255 - a, a);
  144. fy += dy;
  145. } while (++x < stopx);
  146. return fy - SK_Fixed1/2;
  147. }
  148. };
  149. class VLine_SkAntiHairBlitter : public SkAntiHairBlitter {
  150. public:
  151. SkFixed drawCap(int y, SkFixed fx, SkFixed dx, int mod64) override {
  152. SkASSERT(0 == dx);
  153. fx += SK_Fixed1/2;
  154. int x = fx >> 16;
  155. int a = (uint8_t)((fx >> 8) & 0xFF);
  156. unsigned ma = SmallDot6Scale(a, mod64);
  157. if (ma) {
  158. this->getBlitter()->blitV(x, y, 1, ma);
  159. }
  160. ma = SmallDot6Scale(255 - a, mod64);
  161. if (ma) {
  162. this->getBlitter()->blitV(x - 1, y, 1, ma);
  163. }
  164. return fx - SK_Fixed1/2;
  165. }
  166. SkFixed drawLine(int y, int stopy, SkFixed fx, SkFixed dx) override {
  167. SkASSERT(y < stopy);
  168. SkASSERT(0 == dx);
  169. fx += SK_Fixed1/2;
  170. int x = fx >> 16;
  171. int a = (uint8_t)((fx >> 8) & 0xFF);
  172. if (a) {
  173. this->getBlitter()->blitV(x, y, stopy - y, a);
  174. }
  175. a = 255 - a;
  176. if (a) {
  177. this->getBlitter()->blitV(x - 1, y, stopy - y, a);
  178. }
  179. return fx - SK_Fixed1/2;
  180. }
  181. };
  182. class Vertish_SkAntiHairBlitter : public SkAntiHairBlitter {
  183. public:
  184. SkFixed drawCap(int y, SkFixed fx, SkFixed dx, int mod64) override {
  185. fx += SK_Fixed1/2;
  186. int x = fx >> 16;
  187. uint8_t a = (uint8_t)((fx >> 8) & 0xFF);
  188. this->getBlitter()->blitAntiH2(x - 1, y,
  189. SmallDot6Scale(255 - a, mod64), SmallDot6Scale(a, mod64));
  190. return fx + dx - SK_Fixed1/2;
  191. }
  192. SkFixed drawLine(int y, int stopy, SkFixed fx, SkFixed dx) override {
  193. SkASSERT(y < stopy);
  194. fx += SK_Fixed1/2;
  195. do {
  196. int x = fx >> 16;
  197. uint8_t a = (uint8_t)((fx >> 8) & 0xFF);
  198. this->getBlitter()->blitAntiH2(x - 1, y, 255 - a, a);
  199. fx += dx;
  200. } while (++y < stopy);
  201. return fx - SK_Fixed1/2;
  202. }
  203. };
  204. static inline SkFixed fastfixdiv(SkFDot6 a, SkFDot6 b) {
  205. SkASSERT((SkLeftShift(a, 16) >> 16) == a);
  206. SkASSERT(b != 0);
  207. return SkLeftShift(a, 16) / b;
  208. }
  209. #define SkBITCOUNT(x) (sizeof(x) << 3)
  210. #if 1
  211. // returns high-bit set iff x==0x8000...
  212. static inline int bad_int(int x) {
  213. return x & -x;
  214. }
  215. static int any_bad_ints(int a, int b, int c, int d) {
  216. return (bad_int(a) | bad_int(b) | bad_int(c) | bad_int(d)) >> (SkBITCOUNT(int) - 1);
  217. }
  218. #else
  219. static inline int good_int(int x) {
  220. return x ^ (1 << (SkBITCOUNT(x) - 1));
  221. }
  222. static int any_bad_ints(int a, int b, int c, int d) {
  223. return !(good_int(a) & good_int(b) & good_int(c) & good_int(d));
  224. }
  225. #endif
  226. #ifdef SK_DEBUG
  227. static bool canConvertFDot6ToFixed(SkFDot6 x) {
  228. const int maxDot6 = SK_MaxS32 >> (16 - 6);
  229. return SkAbs32(x) <= maxDot6;
  230. }
  231. #endif
  232. /*
  233. * We want the fractional part of ordinate, but we want multiples of 64 to
  234. * return 64, not 0, so we can't just say (ordinate & 63).
  235. * We basically want to compute those bits, and if they're 0, return 64.
  236. * We can do that w/o a branch with an extra sub and add.
  237. */
  238. static int contribution_64(SkFDot6 ordinate) {
  239. #if 0
  240. int result = ordinate & 63;
  241. if (0 == result) {
  242. result = 64;
  243. }
  244. #else
  245. int result = ((ordinate - 1) & 63) + 1;
  246. #endif
  247. SkASSERT(result > 0 && result <= 64);
  248. return result;
  249. }
  250. static void do_anti_hairline(SkFDot6 x0, SkFDot6 y0, SkFDot6 x1, SkFDot6 y1,
  251. const SkIRect* clip, SkBlitter* blitter) {
  252. // check for integer NaN (0x80000000) which we can't handle (can't negate it)
  253. // It appears typically from a huge float (inf or nan) being converted to int.
  254. // If we see it, just don't draw.
  255. if (any_bad_ints(x0, y0, x1, y1)) {
  256. return;
  257. }
  258. // The caller must clip the line to [-32767.0 ... 32767.0] ahead of time
  259. // (in dot6 format)
  260. SkASSERT(canConvertFDot6ToFixed(x0));
  261. SkASSERT(canConvertFDot6ToFixed(y0));
  262. SkASSERT(canConvertFDot6ToFixed(x1));
  263. SkASSERT(canConvertFDot6ToFixed(y1));
  264. if (SkAbs32(x1 - x0) > SkIntToFDot6(511) || SkAbs32(y1 - y0) > SkIntToFDot6(511)) {
  265. /* instead of (x0 + x1) >> 1, we shift each separately. This is less
  266. precise, but avoids overflowing the intermediate result if the
  267. values are huge. A better fix might be to clip the original pts
  268. directly (i.e. do the divide), so we don't spend time subdividing
  269. huge lines at all.
  270. */
  271. int hx = (x0 >> 1) + (x1 >> 1);
  272. int hy = (y0 >> 1) + (y1 >> 1);
  273. do_anti_hairline(x0, y0, hx, hy, clip, blitter);
  274. do_anti_hairline(hx, hy, x1, y1, clip, blitter);
  275. return;
  276. }
  277. int scaleStart, scaleStop;
  278. int istart, istop;
  279. SkFixed fstart, slope;
  280. HLine_SkAntiHairBlitter hline_blitter;
  281. Horish_SkAntiHairBlitter horish_blitter;
  282. VLine_SkAntiHairBlitter vline_blitter;
  283. Vertish_SkAntiHairBlitter vertish_blitter;
  284. SkAntiHairBlitter* hairBlitter = nullptr;
  285. if (SkAbs32(x1 - x0) > SkAbs32(y1 - y0)) { // mostly horizontal
  286. if (x0 > x1) { // we want to go left-to-right
  287. using std::swap;
  288. swap(x0, x1);
  289. swap(y0, y1);
  290. }
  291. istart = SkFDot6Floor(x0);
  292. istop = SkFDot6Ceil(x1);
  293. fstart = SkFDot6ToFixed(y0);
  294. if (y0 == y1) { // completely horizontal, take fast case
  295. slope = 0;
  296. hairBlitter = &hline_blitter;
  297. } else {
  298. slope = fastfixdiv(y1 - y0, x1 - x0);
  299. SkASSERT(slope >= -SK_Fixed1 && slope <= SK_Fixed1);
  300. fstart += (slope * (32 - (x0 & 63)) + 32) >> 6;
  301. hairBlitter = &horish_blitter;
  302. }
  303. SkASSERT(istop > istart);
  304. if (istop - istart == 1) {
  305. // we are within a single pixel
  306. scaleStart = x1 - x0;
  307. SkASSERT(scaleStart >= 0 && scaleStart <= 64);
  308. scaleStop = 0;
  309. } else {
  310. scaleStart = 64 - (x0 & 63);
  311. scaleStop = x1 & 63;
  312. }
  313. if (clip){
  314. if (istart >= clip->fRight || istop <= clip->fLeft) {
  315. return;
  316. }
  317. if (istart < clip->fLeft) {
  318. fstart += slope * (clip->fLeft - istart);
  319. istart = clip->fLeft;
  320. scaleStart = 64;
  321. if (istop - istart == 1) {
  322. // we are within a single pixel
  323. scaleStart = contribution_64(x1);
  324. scaleStop = 0;
  325. }
  326. }
  327. if (istop > clip->fRight) {
  328. istop = clip->fRight;
  329. scaleStop = 0; // so we don't draw this last column
  330. }
  331. SkASSERT(istart <= istop);
  332. if (istart == istop) {
  333. return;
  334. }
  335. // now test if our Y values are completely inside the clip
  336. int top, bottom;
  337. if (slope >= 0) { // T2B
  338. top = SkFixedFloorToInt(fstart - SK_FixedHalf);
  339. bottom = SkFixedCeilToInt(fstart + (istop - istart - 1) * slope + SK_FixedHalf);
  340. } else { // B2T
  341. bottom = SkFixedCeilToInt(fstart + SK_FixedHalf);
  342. top = SkFixedFloorToInt(fstart + (istop - istart - 1) * slope - SK_FixedHalf);
  343. }
  344. #ifdef OUTSET_BEFORE_CLIP_TEST
  345. top -= 1;
  346. bottom += 1;
  347. #endif
  348. if (top >= clip->fBottom || bottom <= clip->fTop) {
  349. return;
  350. }
  351. if (clip->fTop <= top && clip->fBottom >= bottom) {
  352. clip = nullptr;
  353. }
  354. }
  355. } else { // mostly vertical
  356. if (y0 > y1) { // we want to go top-to-bottom
  357. using std::swap;
  358. swap(x0, x1);
  359. swap(y0, y1);
  360. }
  361. istart = SkFDot6Floor(y0);
  362. istop = SkFDot6Ceil(y1);
  363. fstart = SkFDot6ToFixed(x0);
  364. if (x0 == x1) {
  365. if (y0 == y1) { // are we zero length?
  366. return; // nothing to do
  367. }
  368. slope = 0;
  369. hairBlitter = &vline_blitter;
  370. } else {
  371. slope = fastfixdiv(x1 - x0, y1 - y0);
  372. SkASSERT(slope <= SK_Fixed1 && slope >= -SK_Fixed1);
  373. fstart += (slope * (32 - (y0 & 63)) + 32) >> 6;
  374. hairBlitter = &vertish_blitter;
  375. }
  376. SkASSERT(istop > istart);
  377. if (istop - istart == 1) {
  378. // we are within a single pixel
  379. scaleStart = y1 - y0;
  380. SkASSERT(scaleStart >= 0 && scaleStart <= 64);
  381. scaleStop = 0;
  382. } else {
  383. scaleStart = 64 - (y0 & 63);
  384. scaleStop = y1 & 63;
  385. }
  386. if (clip) {
  387. if (istart >= clip->fBottom || istop <= clip->fTop) {
  388. return;
  389. }
  390. if (istart < clip->fTop) {
  391. fstart += slope * (clip->fTop - istart);
  392. istart = clip->fTop;
  393. scaleStart = 64;
  394. if (istop - istart == 1) {
  395. // we are within a single pixel
  396. scaleStart = contribution_64(y1);
  397. scaleStop = 0;
  398. }
  399. }
  400. if (istop > clip->fBottom) {
  401. istop = clip->fBottom;
  402. scaleStop = 0; // so we don't draw this last row
  403. }
  404. SkASSERT(istart <= istop);
  405. if (istart == istop)
  406. return;
  407. // now test if our X values are completely inside the clip
  408. int left, right;
  409. if (slope >= 0) { // L2R
  410. left = SkFixedFloorToInt(fstart - SK_FixedHalf);
  411. right = SkFixedCeilToInt(fstart + (istop - istart - 1) * slope + SK_FixedHalf);
  412. } else { // R2L
  413. right = SkFixedCeilToInt(fstart + SK_FixedHalf);
  414. left = SkFixedFloorToInt(fstart + (istop - istart - 1) * slope - SK_FixedHalf);
  415. }
  416. #ifdef OUTSET_BEFORE_CLIP_TEST
  417. left -= 1;
  418. right += 1;
  419. #endif
  420. if (left >= clip->fRight || right <= clip->fLeft) {
  421. return;
  422. }
  423. if (clip->fLeft <= left && clip->fRight >= right) {
  424. clip = nullptr;
  425. }
  426. }
  427. }
  428. SkRectClipBlitter rectClipper;
  429. if (clip) {
  430. rectClipper.init(blitter, *clip);
  431. blitter = &rectClipper;
  432. }
  433. SkASSERT(hairBlitter);
  434. hairBlitter->setup(blitter);
  435. #ifdef SK_DEBUG
  436. if (scaleStart > 0 && scaleStop > 0) {
  437. // be sure we don't draw twice in the same pixel
  438. SkASSERT(istart < istop - 1);
  439. }
  440. #endif
  441. fstart = hairBlitter->drawCap(istart, fstart, slope, scaleStart);
  442. istart += 1;
  443. int fullSpans = istop - istart - (scaleStop > 0);
  444. if (fullSpans > 0) {
  445. fstart = hairBlitter->drawLine(istart, istart + fullSpans, fstart, slope);
  446. }
  447. if (scaleStop > 0) {
  448. hairBlitter->drawCap(istop - 1, fstart, slope, scaleStop);
  449. }
  450. }
  451. void SkScan::AntiHairLineRgn(const SkPoint array[], int arrayCount, const SkRegion* clip,
  452. SkBlitter* blitter) {
  453. if (clip && clip->isEmpty()) {
  454. return;
  455. }
  456. SkASSERT(clip == nullptr || !clip->getBounds().isEmpty());
  457. #ifdef TEST_GAMMA
  458. build_gamma_table();
  459. #endif
  460. const SkScalar max = SkIntToScalar(32767);
  461. const SkRect fixedBounds = SkRect::MakeLTRB(-max, -max, max, max);
  462. SkRect clipBounds;
  463. if (clip) {
  464. clipBounds.set(clip->getBounds());
  465. /* We perform integral clipping later on, but we do a scalar clip first
  466. to ensure that our coordinates are expressible in fixed/integers.
  467. antialiased hairlines can draw up to 1/2 of a pixel outside of
  468. their bounds, so we need to outset the clip before calling the
  469. clipper. To make the numerics safer, we outset by a whole pixel,
  470. since the 1/2 pixel boundary is important to the antihair blitter,
  471. we don't want to risk numerical fate by chopping on that edge.
  472. */
  473. clipBounds.outset(SK_Scalar1, SK_Scalar1);
  474. }
  475. for (int i = 0; i < arrayCount - 1; ++i) {
  476. SkPoint pts[2];
  477. // We have to pre-clip the line to fit in a SkFixed, so we just chop
  478. // the line. TODO find a way to actually draw beyond that range.
  479. if (!SkLineClipper::IntersectLine(&array[i], fixedBounds, pts)) {
  480. continue;
  481. }
  482. if (clip && !SkLineClipper::IntersectLine(pts, clipBounds, pts)) {
  483. continue;
  484. }
  485. SkFDot6 x0 = SkScalarToFDot6(pts[0].fX);
  486. SkFDot6 y0 = SkScalarToFDot6(pts[0].fY);
  487. SkFDot6 x1 = SkScalarToFDot6(pts[1].fX);
  488. SkFDot6 y1 = SkScalarToFDot6(pts[1].fY);
  489. if (clip) {
  490. SkFDot6 left = SkMin32(x0, x1);
  491. SkFDot6 top = SkMin32(y0, y1);
  492. SkFDot6 right = SkMax32(x0, x1);
  493. SkFDot6 bottom = SkMax32(y0, y1);
  494. SkIRect ir;
  495. ir.set( SkFDot6Floor(left) - 1,
  496. SkFDot6Floor(top) - 1,
  497. SkFDot6Ceil(right) + 1,
  498. SkFDot6Ceil(bottom) + 1);
  499. if (clip->quickReject(ir)) {
  500. continue;
  501. }
  502. if (!clip->quickContains(ir)) {
  503. SkRegion::Cliperator iter(*clip, ir);
  504. const SkIRect* r = &iter.rect();
  505. while (!iter.done()) {
  506. do_anti_hairline(x0, y0, x1, y1, r, blitter);
  507. iter.next();
  508. }
  509. continue;
  510. }
  511. // fall through to no-clip case
  512. }
  513. do_anti_hairline(x0, y0, x1, y1, nullptr, blitter);
  514. }
  515. }
  516. void SkScan::AntiHairRect(const SkRect& rect, const SkRasterClip& clip,
  517. SkBlitter* blitter) {
  518. SkPoint pts[5];
  519. pts[0].set(rect.fLeft, rect.fTop);
  520. pts[1].set(rect.fRight, rect.fTop);
  521. pts[2].set(rect.fRight, rect.fBottom);
  522. pts[3].set(rect.fLeft, rect.fBottom);
  523. pts[4] = pts[0];
  524. SkScan::AntiHairLine(pts, 5, clip, blitter);
  525. }
  526. ///////////////////////////////////////////////////////////////////////////////
  527. typedef int FDot8; // 24.8 integer fixed point
  528. static inline FDot8 SkFixedToFDot8(SkFixed x) {
  529. return (x + 0x80) >> 8;
  530. }
  531. static void do_scanline(FDot8 L, int top, FDot8 R, U8CPU alpha,
  532. SkBlitter* blitter) {
  533. SkASSERT(L < R);
  534. if ((L >> 8) == ((R - 1) >> 8)) { // 1x1 pixel
  535. blitter->blitV(L >> 8, top, 1, SkAlphaMul(alpha, R - L));
  536. return;
  537. }
  538. int left = L >> 8;
  539. if (L & 0xFF) {
  540. blitter->blitV(left, top, 1, SkAlphaMul(alpha, 256 - (L & 0xFF)));
  541. left += 1;
  542. }
  543. int rite = R >> 8;
  544. int width = rite - left;
  545. if (width > 0) {
  546. call_hline_blitter(blitter, left, top, width, alpha);
  547. }
  548. if (R & 0xFF) {
  549. blitter->blitV(rite, top, 1, SkAlphaMul(alpha, R & 0xFF));
  550. }
  551. }
  552. static void antifilldot8(FDot8 L, FDot8 T, FDot8 R, FDot8 B, SkBlitter* blitter,
  553. bool fillInner) {
  554. // check for empty now that we're in our reduced precision space
  555. if (L >= R || T >= B) {
  556. return;
  557. }
  558. int top = T >> 8;
  559. if (top == ((B - 1) >> 8)) { // just one scanline high
  560. do_scanline(L, top, R, B - T - 1, blitter);
  561. return;
  562. }
  563. if (T & 0xFF) {
  564. do_scanline(L, top, R, 256 - (T & 0xFF), blitter);
  565. top += 1;
  566. }
  567. int bot = B >> 8;
  568. int height = bot - top;
  569. if (height > 0) {
  570. int left = L >> 8;
  571. if (left == ((R - 1) >> 8)) { // just 1-pixel wide
  572. blitter->blitV(left, top, height, R - L - 1);
  573. } else {
  574. if (L & 0xFF) {
  575. blitter->blitV(left, top, height, 256 - (L & 0xFF));
  576. left += 1;
  577. }
  578. int rite = R >> 8;
  579. int width = rite - left;
  580. if (width > 0 && fillInner) {
  581. blitter->blitRect(left, top, width, height);
  582. }
  583. if (R & 0xFF) {
  584. blitter->blitV(rite, top, height, R & 0xFF);
  585. }
  586. }
  587. }
  588. if (B & 0xFF) {
  589. do_scanline(L, bot, R, B & 0xFF, blitter);
  590. }
  591. }
  592. static void antifillrect(const SkXRect& xr, SkBlitter* blitter) {
  593. antifilldot8(SkFixedToFDot8(xr.fLeft), SkFixedToFDot8(xr.fTop),
  594. SkFixedToFDot8(xr.fRight), SkFixedToFDot8(xr.fBottom),
  595. blitter, true);
  596. }
  597. ///////////////////////////////////////////////////////////////////////////////
  598. void SkScan::AntiFillXRect(const SkXRect& xr, const SkRegion* clip,
  599. SkBlitter* blitter) {
  600. if (nullptr == clip) {
  601. antifillrect(xr, blitter);
  602. } else {
  603. SkIRect outerBounds;
  604. XRect_roundOut(xr, &outerBounds);
  605. if (clip->isRect()) {
  606. const SkIRect& clipBounds = clip->getBounds();
  607. if (clipBounds.contains(outerBounds)) {
  608. antifillrect(xr, blitter);
  609. } else {
  610. SkXRect tmpR;
  611. // this keeps our original edges fractional
  612. XRect_set(&tmpR, clipBounds);
  613. if (tmpR.intersect(xr)) {
  614. antifillrect(tmpR, blitter);
  615. }
  616. }
  617. } else {
  618. SkRegion::Cliperator clipper(*clip, outerBounds);
  619. const SkIRect& rr = clipper.rect();
  620. while (!clipper.done()) {
  621. SkXRect tmpR;
  622. // this keeps our original edges fractional
  623. XRect_set(&tmpR, rr);
  624. if (tmpR.intersect(xr)) {
  625. antifillrect(tmpR, blitter);
  626. }
  627. clipper.next();
  628. }
  629. }
  630. }
  631. }
  632. void SkScan::AntiFillXRect(const SkXRect& xr, const SkRasterClip& clip,
  633. SkBlitter* blitter) {
  634. if (clip.isBW()) {
  635. AntiFillXRect(xr, &clip.bwRgn(), blitter);
  636. } else {
  637. SkIRect outerBounds;
  638. XRect_roundOut(xr, &outerBounds);
  639. if (clip.quickContains(outerBounds)) {
  640. AntiFillXRect(xr, nullptr, blitter);
  641. } else {
  642. SkAAClipBlitterWrapper wrapper(clip, blitter);
  643. AntiFillXRect(xr, &wrapper.getRgn(), wrapper.getBlitter());
  644. }
  645. }
  646. }
  647. /* This guy takes a float-rect, but with the key improvement that it has
  648. already been clipped, so we know that it is safe to convert it into a
  649. XRect (fixedpoint), as it won't overflow.
  650. */
  651. static void antifillrect(const SkRect& r, SkBlitter* blitter) {
  652. SkXRect xr;
  653. XRect_set(&xr, r);
  654. antifillrect(xr, blitter);
  655. }
  656. /* We repeat the clipping logic of AntiFillXRect because the float rect might
  657. overflow if we blindly converted it to an XRect. This sucks that we have to
  658. repeat the clipping logic, but I don't see how to share the code/logic.
  659. We clip r (as needed) into one or more (smaller) float rects, and then pass
  660. those to our version of antifillrect, which converts it into an XRect and
  661. then calls the blit.
  662. */
  663. void SkScan::AntiFillRect(const SkRect& origR, const SkRegion* clip,
  664. SkBlitter* blitter) {
  665. if (clip) {
  666. SkRect newR;
  667. newR.set(clip->getBounds());
  668. if (!newR.intersect(origR)) {
  669. return;
  670. }
  671. const SkIRect outerBounds = newR.roundOut();
  672. if (clip->isRect()) {
  673. antifillrect(newR, blitter);
  674. } else {
  675. SkRegion::Cliperator clipper(*clip, outerBounds);
  676. while (!clipper.done()) {
  677. newR.set(clipper.rect());
  678. if (newR.intersect(origR)) {
  679. antifillrect(newR, blitter);
  680. }
  681. clipper.next();
  682. }
  683. }
  684. } else {
  685. antifillrect(origR, blitter);
  686. }
  687. }
  688. void SkScan::AntiFillRect(const SkRect& r, const SkRasterClip& clip,
  689. SkBlitter* blitter) {
  690. if (clip.isBW()) {
  691. AntiFillRect(r, &clip.bwRgn(), blitter);
  692. } else {
  693. SkAAClipBlitterWrapper wrap(clip, blitter);
  694. AntiFillRect(r, &wrap.getRgn(), wrap.getBlitter());
  695. }
  696. }
  697. ///////////////////////////////////////////////////////////////////////////////
  698. #define SkAlphaMulRound(a, b) SkMulDiv255Round(a, b)
  699. // calls blitRect() if the rectangle is non-empty
  700. static void fillcheckrect(int L, int T, int R, int B, SkBlitter* blitter) {
  701. if (L < R && T < B) {
  702. blitter->blitRect(L, T, R - L, B - T);
  703. }
  704. }
  705. static inline FDot8 SkScalarToFDot8(SkScalar x) {
  706. return (int)(x * 256);
  707. }
  708. static inline int FDot8Floor(FDot8 x) {
  709. return x >> 8;
  710. }
  711. static inline int FDot8Ceil(FDot8 x) {
  712. return (x + 0xFF) >> 8;
  713. }
  714. // 1 - (1 - a)*(1 - b)
  715. static inline U8CPU InvAlphaMul(U8CPU a, U8CPU b) {
  716. // need precise rounding (not just SkAlphaMul) so that values like
  717. // a=228, b=252 don't overflow the result
  718. return SkToU8(a + b - SkAlphaMulRound(a, b));
  719. }
  720. static void inner_scanline(FDot8 L, int top, FDot8 R, U8CPU alpha,
  721. SkBlitter* blitter) {
  722. SkASSERT(L < R);
  723. if ((L >> 8) == ((R - 1) >> 8)) { // 1x1 pixel
  724. FDot8 widClamp = R - L;
  725. // border case clamp 256 to 255 instead of going through call_hline_blitter
  726. // see skbug/4406
  727. widClamp = widClamp - (widClamp >> 8);
  728. blitter->blitV(L >> 8, top, 1, InvAlphaMul(alpha, widClamp));
  729. return;
  730. }
  731. int left = L >> 8;
  732. if (L & 0xFF) {
  733. blitter->blitV(left, top, 1, InvAlphaMul(alpha, L & 0xFF));
  734. left += 1;
  735. }
  736. int rite = R >> 8;
  737. int width = rite - left;
  738. if (width > 0) {
  739. call_hline_blitter(blitter, left, top, width, alpha);
  740. }
  741. if (R & 0xFF) {
  742. blitter->blitV(rite, top, 1, InvAlphaMul(alpha, ~R & 0xFF));
  743. }
  744. }
  745. static void innerstrokedot8(FDot8 L, FDot8 T, FDot8 R, FDot8 B,
  746. SkBlitter* blitter) {
  747. SkASSERT(L < R && T < B);
  748. int top = T >> 8;
  749. if (top == ((B - 1) >> 8)) { // just one scanline high
  750. // We want the inverse of B-T, since we're the inner-stroke
  751. int alpha = 256 - (B - T);
  752. if (alpha) {
  753. inner_scanline(L, top, R, alpha, blitter);
  754. }
  755. return;
  756. }
  757. if (T & 0xFF) {
  758. inner_scanline(L, top, R, T & 0xFF, blitter);
  759. top += 1;
  760. }
  761. int bot = B >> 8;
  762. int height = bot - top;
  763. if (height > 0) {
  764. if (L & 0xFF) {
  765. blitter->blitV(L >> 8, top, height, L & 0xFF);
  766. }
  767. if (R & 0xFF) {
  768. blitter->blitV(R >> 8, top, height, ~R & 0xFF);
  769. }
  770. }
  771. if (B & 0xFF) {
  772. inner_scanline(L, bot, R, ~B & 0xFF, blitter);
  773. }
  774. }
  775. static inline void align_thin_stroke(FDot8& edge1, FDot8& edge2) {
  776. SkASSERT(edge1 <= edge2);
  777. if (FDot8Floor(edge1) == FDot8Floor(edge2)) {
  778. edge2 -= (edge1 & 0xFF);
  779. edge1 &= ~0xFF;
  780. }
  781. }
  782. void SkScan::AntiFrameRect(const SkRect& r, const SkPoint& strokeSize,
  783. const SkRegion* clip, SkBlitter* blitter) {
  784. SkASSERT(strokeSize.fX >= 0 && strokeSize.fY >= 0);
  785. SkScalar rx = SkScalarHalf(strokeSize.fX);
  786. SkScalar ry = SkScalarHalf(strokeSize.fY);
  787. // outset by the radius
  788. FDot8 outerL = SkScalarToFDot8(r.fLeft - rx);
  789. FDot8 outerT = SkScalarToFDot8(r.fTop - ry);
  790. FDot8 outerR = SkScalarToFDot8(r.fRight + rx);
  791. FDot8 outerB = SkScalarToFDot8(r.fBottom + ry);
  792. SkIRect outer;
  793. // set outer to the outer rect of the outer section
  794. outer.set(FDot8Floor(outerL), FDot8Floor(outerT), FDot8Ceil(outerR), FDot8Ceil(outerB));
  795. SkBlitterClipper clipper;
  796. if (clip) {
  797. if (clip->quickReject(outer)) {
  798. return;
  799. }
  800. if (!clip->contains(outer)) {
  801. blitter = clipper.apply(blitter, clip, &outer);
  802. }
  803. // now we can ignore clip for the rest of the function
  804. }
  805. // in case we lost a bit with diameter/2
  806. rx = strokeSize.fX - rx;
  807. ry = strokeSize.fY - ry;
  808. // inset by the radius
  809. FDot8 innerL = SkScalarToFDot8(r.fLeft + rx);
  810. FDot8 innerT = SkScalarToFDot8(r.fTop + ry);
  811. FDot8 innerR = SkScalarToFDot8(r.fRight - rx);
  812. FDot8 innerB = SkScalarToFDot8(r.fBottom - ry);
  813. // For sub-unit strokes, tweak the hulls such that one of the edges coincides with the pixel
  814. // edge. This ensures that the general rect stroking logic below
  815. // a) doesn't blit the same scanline twice
  816. // b) computes the correct coverage when both edges fall within the same pixel
  817. if (strokeSize.fX < 1 || strokeSize.fY < 1) {
  818. align_thin_stroke(outerL, innerL);
  819. align_thin_stroke(outerT, innerT);
  820. align_thin_stroke(innerR, outerR);
  821. align_thin_stroke(innerB, outerB);
  822. }
  823. // stroke the outer hull
  824. antifilldot8(outerL, outerT, outerR, outerB, blitter, false);
  825. // set outer to the outer rect of the middle section
  826. outer.set(FDot8Ceil(outerL), FDot8Ceil(outerT), FDot8Floor(outerR), FDot8Floor(outerB));
  827. if (innerL >= innerR || innerT >= innerB) {
  828. fillcheckrect(outer.fLeft, outer.fTop, outer.fRight, outer.fBottom,
  829. blitter);
  830. } else {
  831. SkIRect inner;
  832. // set inner to the inner rect of the middle section
  833. inner.set(FDot8Floor(innerL), FDot8Floor(innerT), FDot8Ceil(innerR), FDot8Ceil(innerB));
  834. // draw the frame in 4 pieces
  835. fillcheckrect(outer.fLeft, outer.fTop, outer.fRight, inner.fTop,
  836. blitter);
  837. fillcheckrect(outer.fLeft, inner.fTop, inner.fLeft, inner.fBottom,
  838. blitter);
  839. fillcheckrect(inner.fRight, inner.fTop, outer.fRight, inner.fBottom,
  840. blitter);
  841. fillcheckrect(outer.fLeft, inner.fBottom, outer.fRight, outer.fBottom,
  842. blitter);
  843. // now stroke the inner rect, which is similar to antifilldot8() except that
  844. // it treats the fractional coordinates with the inverse bias (since its
  845. // inner).
  846. innerstrokedot8(innerL, innerT, innerR, innerB, blitter);
  847. }
  848. }
  849. void SkScan::AntiFrameRect(const SkRect& r, const SkPoint& strokeSize,
  850. const SkRasterClip& clip, SkBlitter* blitter) {
  851. if (clip.isBW()) {
  852. AntiFrameRect(r, strokeSize, &clip.bwRgn(), blitter);
  853. } else {
  854. SkAAClipBlitterWrapper wrap(clip, blitter);
  855. AntiFrameRect(r, strokeSize, &wrap.getRgn(), wrap.getBlitter());
  856. }
  857. }