SkAAClip.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. /*
  2. * Copyright 2011 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. #include "src/core/SkAAClip.h"
  8. #include "include/core/SkPath.h"
  9. #include "include/private/SkColorData.h"
  10. #include "include/private/SkMacros.h"
  11. #include "include/private/SkTo.h"
  12. #include "src/core/SkBlitter.h"
  13. #include "src/core/SkRectPriv.h"
  14. #include "src/core/SkScan.h"
  15. #include "src/utils/SkUTF.h"
  16. #include <atomic>
  17. #include <utility>
  18. class AutoAAClipValidate {
  19. public:
  20. AutoAAClipValidate(const SkAAClip& clip) : fClip(clip) {
  21. fClip.validate();
  22. }
  23. ~AutoAAClipValidate() {
  24. fClip.validate();
  25. }
  26. private:
  27. const SkAAClip& fClip;
  28. };
  29. #ifdef SK_DEBUG
  30. #define AUTO_AACLIP_VALIDATE(clip) AutoAAClipValidate acv(clip)
  31. #else
  32. #define AUTO_AACLIP_VALIDATE(clip)
  33. #endif
  34. ///////////////////////////////////////////////////////////////////////////////
  35. #define kMaxInt32 0x7FFFFFFF
  36. #ifdef SK_DEBUG
  37. static inline bool x_in_rect(int x, const SkIRect& rect) {
  38. return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
  39. }
  40. #endif
  41. static inline bool y_in_rect(int y, const SkIRect& rect) {
  42. return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
  43. }
  44. /*
  45. * Data runs are packed [count, alpha]
  46. */
  47. struct SkAAClip::YOffset {
  48. int32_t fY;
  49. uint32_t fOffset;
  50. };
  51. struct SkAAClip::RunHead {
  52. std::atomic<int32_t> fRefCnt;
  53. int32_t fRowCount;
  54. size_t fDataSize;
  55. YOffset* yoffsets() {
  56. return (YOffset*)((char*)this + sizeof(RunHead));
  57. }
  58. const YOffset* yoffsets() const {
  59. return (const YOffset*)((const char*)this + sizeof(RunHead));
  60. }
  61. uint8_t* data() {
  62. return (uint8_t*)(this->yoffsets() + fRowCount);
  63. }
  64. const uint8_t* data() const {
  65. return (const uint8_t*)(this->yoffsets() + fRowCount);
  66. }
  67. static RunHead* Alloc(int rowCount, size_t dataSize) {
  68. size_t size = sizeof(RunHead) + rowCount * sizeof(YOffset) + dataSize;
  69. RunHead* head = (RunHead*)sk_malloc_throw(size);
  70. head->fRefCnt.store(1);
  71. head->fRowCount = rowCount;
  72. head->fDataSize = dataSize;
  73. return head;
  74. }
  75. static int ComputeRowSizeForWidth(int width) {
  76. // 2 bytes per segment, where each segment can store up to 255 for count
  77. int segments = 0;
  78. while (width > 0) {
  79. segments += 1;
  80. int n = SkMin32(width, 255);
  81. width -= n;
  82. }
  83. return segments * 2; // each segment is row[0] + row[1] (n + alpha)
  84. }
  85. static RunHead* AllocRect(const SkIRect& bounds) {
  86. SkASSERT(!bounds.isEmpty());
  87. int width = bounds.width();
  88. size_t rowSize = ComputeRowSizeForWidth(width);
  89. RunHead* head = RunHead::Alloc(1, rowSize);
  90. YOffset* yoff = head->yoffsets();
  91. yoff->fY = bounds.height() - 1;
  92. yoff->fOffset = 0;
  93. uint8_t* row = head->data();
  94. while (width > 0) {
  95. int n = SkMin32(width, 255);
  96. row[0] = n;
  97. row[1] = 0xFF;
  98. width -= n;
  99. row += 2;
  100. }
  101. return head;
  102. }
  103. };
  104. class SkAAClip::Iter {
  105. public:
  106. Iter(const SkAAClip&);
  107. bool done() const { return fDone; }
  108. int top() const { return fTop; }
  109. int bottom() const { return fBottom; }
  110. const uint8_t* data() const { return fData; }
  111. void next();
  112. private:
  113. const YOffset* fCurrYOff;
  114. const YOffset* fStopYOff;
  115. const uint8_t* fData;
  116. int fTop, fBottom;
  117. bool fDone;
  118. };
  119. SkAAClip::Iter::Iter(const SkAAClip& clip) {
  120. if (clip.isEmpty()) {
  121. fDone = true;
  122. fTop = fBottom = clip.fBounds.fBottom;
  123. fData = nullptr;
  124. fCurrYOff = nullptr;
  125. fStopYOff = nullptr;
  126. return;
  127. }
  128. const RunHead* head = clip.fRunHead;
  129. fCurrYOff = head->yoffsets();
  130. fStopYOff = fCurrYOff + head->fRowCount;
  131. fData = head->data() + fCurrYOff->fOffset;
  132. // setup first value
  133. fTop = clip.fBounds.fTop;
  134. fBottom = clip.fBounds.fTop + fCurrYOff->fY + 1;
  135. fDone = false;
  136. }
  137. void SkAAClip::Iter::next() {
  138. if (!fDone) {
  139. const YOffset* prev = fCurrYOff;
  140. const YOffset* curr = prev + 1;
  141. SkASSERT(curr <= fStopYOff);
  142. fTop = fBottom;
  143. if (curr >= fStopYOff) {
  144. fDone = true;
  145. fBottom = kMaxInt32;
  146. fData = nullptr;
  147. } else {
  148. fBottom += curr->fY - prev->fY;
  149. fData += curr->fOffset - prev->fOffset;
  150. fCurrYOff = curr;
  151. }
  152. }
  153. }
  154. #ifdef SK_DEBUG
  155. // assert we're exactly width-wide, and then return the number of bytes used
  156. static size_t compute_row_length(const uint8_t row[], int width) {
  157. const uint8_t* origRow = row;
  158. while (width > 0) {
  159. int n = row[0];
  160. SkASSERT(n > 0);
  161. SkASSERT(n <= width);
  162. row += 2;
  163. width -= n;
  164. }
  165. SkASSERT(0 == width);
  166. return row - origRow;
  167. }
  168. void SkAAClip::validate() const {
  169. if (nullptr == fRunHead) {
  170. SkASSERT(fBounds.isEmpty());
  171. return;
  172. }
  173. SkASSERT(!fBounds.isEmpty());
  174. const RunHead* head = fRunHead;
  175. SkASSERT(head->fRefCnt.load() > 0);
  176. SkASSERT(head->fRowCount > 0);
  177. const YOffset* yoff = head->yoffsets();
  178. const YOffset* ystop = yoff + head->fRowCount;
  179. const int lastY = fBounds.height() - 1;
  180. // Y and offset must be monotonic
  181. int prevY = -1;
  182. int32_t prevOffset = -1;
  183. while (yoff < ystop) {
  184. SkASSERT(prevY < yoff->fY);
  185. SkASSERT(yoff->fY <= lastY);
  186. prevY = yoff->fY;
  187. SkASSERT(prevOffset < (int32_t)yoff->fOffset);
  188. prevOffset = yoff->fOffset;
  189. const uint8_t* row = head->data() + yoff->fOffset;
  190. size_t rowLength = compute_row_length(row, fBounds.width());
  191. SkASSERT(yoff->fOffset + rowLength <= head->fDataSize);
  192. yoff += 1;
  193. }
  194. // check the last entry;
  195. --yoff;
  196. SkASSERT(yoff->fY == lastY);
  197. }
  198. static void dump_one_row(const uint8_t* SK_RESTRICT row,
  199. int width, int leading_num) {
  200. if (leading_num) {
  201. SkDebugf( "%03d ", leading_num );
  202. }
  203. while (width > 0) {
  204. int n = row[0];
  205. int val = row[1];
  206. char out = '.';
  207. if (val == 0xff) {
  208. out = '*';
  209. } else if (val > 0) {
  210. out = '+';
  211. }
  212. for (int i = 0 ; i < n ; i++) {
  213. SkDebugf( "%c", out );
  214. }
  215. row += 2;
  216. width -= n;
  217. }
  218. SkDebugf( "\n" );
  219. }
  220. void SkAAClip::debug(bool compress_y) const {
  221. Iter iter(*this);
  222. const int width = fBounds.width();
  223. int y = fBounds.fTop;
  224. while (!iter.done()) {
  225. if (compress_y) {
  226. dump_one_row(iter.data(), width, iter.bottom() - iter.top() + 1);
  227. } else {
  228. do {
  229. dump_one_row(iter.data(), width, 0);
  230. } while (++y < iter.bottom());
  231. }
  232. iter.next();
  233. }
  234. }
  235. #endif
  236. ///////////////////////////////////////////////////////////////////////////////
  237. // Count the number of zeros on the left and right edges of the passed in
  238. // RLE row. If 'row' is all zeros return 'width' in both variables.
  239. static void count_left_right_zeros(const uint8_t* row, int width,
  240. int* leftZ, int* riteZ) {
  241. int zeros = 0;
  242. do {
  243. if (row[1]) {
  244. break;
  245. }
  246. int n = row[0];
  247. SkASSERT(n > 0);
  248. SkASSERT(n <= width);
  249. zeros += n;
  250. row += 2;
  251. width -= n;
  252. } while (width > 0);
  253. *leftZ = zeros;
  254. if (0 == width) {
  255. // this line is completely empty return 'width' in both variables
  256. *riteZ = *leftZ;
  257. return;
  258. }
  259. zeros = 0;
  260. while (width > 0) {
  261. int n = row[0];
  262. SkASSERT(n > 0);
  263. if (0 == row[1]) {
  264. zeros += n;
  265. } else {
  266. zeros = 0;
  267. }
  268. row += 2;
  269. width -= n;
  270. }
  271. *riteZ = zeros;
  272. }
  273. // modify row in place, trimming off (zeros) from the left and right sides.
  274. // return the number of bytes that were completely eliminated from the left
  275. static int trim_row_left_right(uint8_t* row, int width, int leftZ, int riteZ) {
  276. int trim = 0;
  277. while (leftZ > 0) {
  278. SkASSERT(0 == row[1]);
  279. int n = row[0];
  280. SkASSERT(n > 0);
  281. SkASSERT(n <= width);
  282. width -= n;
  283. row += 2;
  284. if (n > leftZ) {
  285. row[-2] = n - leftZ;
  286. break;
  287. }
  288. trim += 2;
  289. leftZ -= n;
  290. SkASSERT(leftZ >= 0);
  291. }
  292. if (riteZ) {
  293. // walk row to the end, and then we'll back up to trim riteZ
  294. while (width > 0) {
  295. int n = row[0];
  296. SkASSERT(n <= width);
  297. width -= n;
  298. row += 2;
  299. }
  300. // now skip whole runs of zeros
  301. do {
  302. row -= 2;
  303. SkASSERT(0 == row[1]);
  304. int n = row[0];
  305. SkASSERT(n > 0);
  306. if (n > riteZ) {
  307. row[0] = n - riteZ;
  308. break;
  309. }
  310. riteZ -= n;
  311. SkASSERT(riteZ >= 0);
  312. } while (riteZ > 0);
  313. }
  314. return trim;
  315. }
  316. bool SkAAClip::trimLeftRight() {
  317. if (this->isEmpty()) {
  318. return false;
  319. }
  320. AUTO_AACLIP_VALIDATE(*this);
  321. const int width = fBounds.width();
  322. RunHead* head = fRunHead;
  323. YOffset* yoff = head->yoffsets();
  324. YOffset* stop = yoff + head->fRowCount;
  325. uint8_t* base = head->data();
  326. // After this loop, 'leftZeros' & 'rightZeros' will contain the minimum
  327. // number of zeros on the left and right of the clip. This information
  328. // can be used to shrink the bounding box.
  329. int leftZeros = width;
  330. int riteZeros = width;
  331. while (yoff < stop) {
  332. int L, R;
  333. count_left_right_zeros(base + yoff->fOffset, width, &L, &R);
  334. SkASSERT(L + R < width || (L == width && R == width));
  335. if (L < leftZeros) {
  336. leftZeros = L;
  337. }
  338. if (R < riteZeros) {
  339. riteZeros = R;
  340. }
  341. if (0 == (leftZeros | riteZeros)) {
  342. // no trimming to do
  343. return true;
  344. }
  345. yoff += 1;
  346. }
  347. SkASSERT(leftZeros || riteZeros);
  348. if (width == leftZeros) {
  349. SkASSERT(width == riteZeros);
  350. return this->setEmpty();
  351. }
  352. this->validate();
  353. fBounds.fLeft += leftZeros;
  354. fBounds.fRight -= riteZeros;
  355. SkASSERT(!fBounds.isEmpty());
  356. // For now we don't realloc the storage (for time), we just shrink in place
  357. // This means we don't have to do any memmoves either, since we can just
  358. // play tricks with the yoff->fOffset for each row
  359. yoff = head->yoffsets();
  360. while (yoff < stop) {
  361. uint8_t* row = base + yoff->fOffset;
  362. SkDEBUGCODE((void)compute_row_length(row, width);)
  363. yoff->fOffset += trim_row_left_right(row, width, leftZeros, riteZeros);
  364. SkDEBUGCODE((void)compute_row_length(base + yoff->fOffset, width - leftZeros - riteZeros);)
  365. yoff += 1;
  366. }
  367. return true;
  368. }
  369. static bool row_is_all_zeros(const uint8_t* row, int width) {
  370. SkASSERT(width > 0);
  371. do {
  372. if (row[1]) {
  373. return false;
  374. }
  375. int n = row[0];
  376. SkASSERT(n <= width);
  377. width -= n;
  378. row += 2;
  379. } while (width > 0);
  380. SkASSERT(0 == width);
  381. return true;
  382. }
  383. bool SkAAClip::trimTopBottom() {
  384. if (this->isEmpty()) {
  385. return false;
  386. }
  387. this->validate();
  388. const int width = fBounds.width();
  389. RunHead* head = fRunHead;
  390. YOffset* yoff = head->yoffsets();
  391. YOffset* stop = yoff + head->fRowCount;
  392. const uint8_t* base = head->data();
  393. // Look to trim away empty rows from the top.
  394. //
  395. int skip = 0;
  396. while (yoff < stop) {
  397. const uint8_t* data = base + yoff->fOffset;
  398. if (!row_is_all_zeros(data, width)) {
  399. break;
  400. }
  401. skip += 1;
  402. yoff += 1;
  403. }
  404. SkASSERT(skip <= head->fRowCount);
  405. if (skip == head->fRowCount) {
  406. return this->setEmpty();
  407. }
  408. if (skip > 0) {
  409. // adjust fRowCount and fBounds.fTop, and slide all the data up
  410. // as we remove [skip] number of YOffset entries
  411. yoff = head->yoffsets();
  412. int dy = yoff[skip - 1].fY + 1;
  413. for (int i = skip; i < head->fRowCount; ++i) {
  414. SkASSERT(yoff[i].fY >= dy);
  415. yoff[i].fY -= dy;
  416. }
  417. YOffset* dst = head->yoffsets();
  418. size_t size = head->fRowCount * sizeof(YOffset) + head->fDataSize;
  419. memmove(dst, dst + skip, size - skip * sizeof(YOffset));
  420. fBounds.fTop += dy;
  421. SkASSERT(!fBounds.isEmpty());
  422. head->fRowCount -= skip;
  423. SkASSERT(head->fRowCount > 0);
  424. this->validate();
  425. // need to reset this after the memmove
  426. base = head->data();
  427. }
  428. // Look to trim away empty rows from the bottom.
  429. // We know that we have at least one non-zero row, so we can just walk
  430. // backwards without checking for running past the start.
  431. //
  432. stop = yoff = head->yoffsets() + head->fRowCount;
  433. do {
  434. yoff -= 1;
  435. } while (row_is_all_zeros(base + yoff->fOffset, width));
  436. skip = SkToInt(stop - yoff - 1);
  437. SkASSERT(skip >= 0 && skip < head->fRowCount);
  438. if (skip > 0) {
  439. // removing from the bottom is easier than from the top, as we don't
  440. // have to adjust any of the Y values, we just have to trim the array
  441. memmove(stop - skip, stop, head->fDataSize);
  442. fBounds.fBottom = fBounds.fTop + yoff->fY + 1;
  443. SkASSERT(!fBounds.isEmpty());
  444. head->fRowCount -= skip;
  445. SkASSERT(head->fRowCount > 0);
  446. }
  447. this->validate();
  448. return true;
  449. }
  450. // can't validate before we're done, since trimming is part of the process of
  451. // making us valid after the Builder. Since we build from top to bottom, its
  452. // possible our fBounds.fBottom is bigger than our last scanline of data, so
  453. // we trim fBounds.fBottom back up.
  454. //
  455. // TODO: check for duplicates in X and Y to further compress our data
  456. //
  457. bool SkAAClip::trimBounds() {
  458. if (this->isEmpty()) {
  459. return false;
  460. }
  461. const RunHead* head = fRunHead;
  462. const YOffset* yoff = head->yoffsets();
  463. SkASSERT(head->fRowCount > 0);
  464. const YOffset& lastY = yoff[head->fRowCount - 1];
  465. SkASSERT(lastY.fY + 1 <= fBounds.height());
  466. fBounds.fBottom = fBounds.fTop + lastY.fY + 1;
  467. SkASSERT(lastY.fY + 1 == fBounds.height());
  468. SkASSERT(!fBounds.isEmpty());
  469. return this->trimTopBottom() && this->trimLeftRight();
  470. }
  471. ///////////////////////////////////////////////////////////////////////////////
  472. void SkAAClip::freeRuns() {
  473. if (fRunHead) {
  474. SkASSERT(fRunHead->fRefCnt.load() >= 1);
  475. if (1 == fRunHead->fRefCnt--) {
  476. sk_free(fRunHead);
  477. }
  478. }
  479. }
  480. SkAAClip::SkAAClip() {
  481. fBounds.setEmpty();
  482. fRunHead = nullptr;
  483. }
  484. SkAAClip::SkAAClip(const SkAAClip& src) {
  485. SkDEBUGCODE(fBounds.setEmpty();) // need this for validate
  486. fRunHead = nullptr;
  487. *this = src;
  488. }
  489. SkAAClip::~SkAAClip() {
  490. this->freeRuns();
  491. }
  492. SkAAClip& SkAAClip::operator=(const SkAAClip& src) {
  493. AUTO_AACLIP_VALIDATE(*this);
  494. src.validate();
  495. if (this != &src) {
  496. this->freeRuns();
  497. fBounds = src.fBounds;
  498. fRunHead = src.fRunHead;
  499. if (fRunHead) {
  500. fRunHead->fRefCnt++;
  501. }
  502. }
  503. return *this;
  504. }
  505. bool operator==(const SkAAClip& a, const SkAAClip& b) {
  506. a.validate();
  507. b.validate();
  508. if (&a == &b) {
  509. return true;
  510. }
  511. if (a.fBounds != b.fBounds) {
  512. return false;
  513. }
  514. const SkAAClip::RunHead* ah = a.fRunHead;
  515. const SkAAClip::RunHead* bh = b.fRunHead;
  516. // this catches empties and rects being equal
  517. if (ah == bh) {
  518. return true;
  519. }
  520. // now we insist that both are complex (but different ptrs)
  521. if (!a.fRunHead || !b.fRunHead) {
  522. return false;
  523. }
  524. return ah->fRowCount == bh->fRowCount &&
  525. ah->fDataSize == bh->fDataSize &&
  526. !memcmp(ah->data(), bh->data(), ah->fDataSize);
  527. }
  528. void SkAAClip::swap(SkAAClip& other) {
  529. AUTO_AACLIP_VALIDATE(*this);
  530. other.validate();
  531. using std::swap;
  532. swap(fBounds, other.fBounds);
  533. swap(fRunHead, other.fRunHead);
  534. }
  535. bool SkAAClip::set(const SkAAClip& src) {
  536. *this = src;
  537. return !this->isEmpty();
  538. }
  539. bool SkAAClip::setEmpty() {
  540. this->freeRuns();
  541. fBounds.setEmpty();
  542. fRunHead = nullptr;
  543. return false;
  544. }
  545. bool SkAAClip::setRect(const SkIRect& bounds) {
  546. if (bounds.isEmpty()) {
  547. return this->setEmpty();
  548. }
  549. AUTO_AACLIP_VALIDATE(*this);
  550. #if 0
  551. SkRect r;
  552. r.set(bounds);
  553. SkPath path;
  554. path.addRect(r);
  555. return this->setPath(path);
  556. #else
  557. this->freeRuns();
  558. fBounds = bounds;
  559. fRunHead = RunHead::AllocRect(bounds);
  560. SkASSERT(!this->isEmpty());
  561. return true;
  562. #endif
  563. }
  564. bool SkAAClip::isRect() const {
  565. if (this->isEmpty()) {
  566. return false;
  567. }
  568. const RunHead* head = fRunHead;
  569. if (head->fRowCount != 1) {
  570. return false;
  571. }
  572. const YOffset* yoff = head->yoffsets();
  573. if (yoff->fY != fBounds.fBottom - 1) {
  574. return false;
  575. }
  576. const uint8_t* row = head->data() + yoff->fOffset;
  577. int width = fBounds.width();
  578. do {
  579. if (row[1] != 0xFF) {
  580. return false;
  581. }
  582. int n = row[0];
  583. SkASSERT(n <= width);
  584. width -= n;
  585. row += 2;
  586. } while (width > 0);
  587. return true;
  588. }
  589. bool SkAAClip::setRect(const SkRect& r, bool doAA) {
  590. if (r.isEmpty()) {
  591. return this->setEmpty();
  592. }
  593. AUTO_AACLIP_VALIDATE(*this);
  594. // TODO: special case this
  595. SkPath path;
  596. path.addRect(r);
  597. return this->setPath(path, nullptr, doAA);
  598. }
  599. static void append_run(SkTDArray<uint8_t>& array, uint8_t value, int count) {
  600. SkASSERT(count >= 0);
  601. while (count > 0) {
  602. int n = count;
  603. if (n > 255) {
  604. n = 255;
  605. }
  606. uint8_t* data = array.append(2);
  607. data[0] = n;
  608. data[1] = value;
  609. count -= n;
  610. }
  611. }
  612. bool SkAAClip::setRegion(const SkRegion& rgn) {
  613. if (rgn.isEmpty()) {
  614. return this->setEmpty();
  615. }
  616. if (rgn.isRect()) {
  617. return this->setRect(rgn.getBounds());
  618. }
  619. #if 0
  620. SkAAClip clip;
  621. SkRegion::Iterator iter(rgn);
  622. for (; !iter.done(); iter.next()) {
  623. clip.op(iter.rect(), SkRegion::kUnion_Op);
  624. }
  625. this->swap(clip);
  626. return !this->isEmpty();
  627. #else
  628. const SkIRect& bounds = rgn.getBounds();
  629. const int offsetX = bounds.fLeft;
  630. const int offsetY = bounds.fTop;
  631. SkTDArray<YOffset> yArray;
  632. SkTDArray<uint8_t> xArray;
  633. yArray.setReserve(SkMin32(bounds.height(), 1024));
  634. xArray.setReserve(SkMin32(bounds.width(), 512) * 128);
  635. SkRegion::Iterator iter(rgn);
  636. int prevRight = 0;
  637. int prevBot = 0;
  638. YOffset* currY = nullptr;
  639. for (; !iter.done(); iter.next()) {
  640. const SkIRect& r = iter.rect();
  641. SkASSERT(bounds.contains(r));
  642. int bot = r.fBottom - offsetY;
  643. SkASSERT(bot >= prevBot);
  644. if (bot > prevBot) {
  645. if (currY) {
  646. // flush current row
  647. append_run(xArray, 0, bounds.width() - prevRight);
  648. }
  649. // did we introduce an empty-gap from the prev row?
  650. int top = r.fTop - offsetY;
  651. if (top > prevBot) {
  652. currY = yArray.append();
  653. currY->fY = top - 1;
  654. currY->fOffset = xArray.count();
  655. append_run(xArray, 0, bounds.width());
  656. }
  657. // create a new record for this Y value
  658. currY = yArray.append();
  659. currY->fY = bot - 1;
  660. currY->fOffset = xArray.count();
  661. prevRight = 0;
  662. prevBot = bot;
  663. }
  664. int x = r.fLeft - offsetX;
  665. append_run(xArray, 0, x - prevRight);
  666. int w = r.fRight - r.fLeft;
  667. append_run(xArray, 0xFF, w);
  668. prevRight = x + w;
  669. SkASSERT(prevRight <= bounds.width());
  670. }
  671. // flush last row
  672. append_run(xArray, 0, bounds.width() - prevRight);
  673. // now pack everything into a RunHead
  674. RunHead* head = RunHead::Alloc(yArray.count(), xArray.bytes());
  675. memcpy(head->yoffsets(), yArray.begin(), yArray.bytes());
  676. memcpy(head->data(), xArray.begin(), xArray.bytes());
  677. this->setEmpty();
  678. fBounds = bounds;
  679. fRunHead = head;
  680. this->validate();
  681. return true;
  682. #endif
  683. }
  684. ///////////////////////////////////////////////////////////////////////////////
  685. const uint8_t* SkAAClip::findRow(int y, int* lastYForRow) const {
  686. SkASSERT(fRunHead);
  687. if (!y_in_rect(y, fBounds)) {
  688. return nullptr;
  689. }
  690. y -= fBounds.y(); // our yoffs values are relative to the top
  691. const YOffset* yoff = fRunHead->yoffsets();
  692. while (yoff->fY < y) {
  693. yoff += 1;
  694. SkASSERT(yoff - fRunHead->yoffsets() < fRunHead->fRowCount);
  695. }
  696. if (lastYForRow) {
  697. *lastYForRow = fBounds.y() + yoff->fY;
  698. }
  699. return fRunHead->data() + yoff->fOffset;
  700. }
  701. const uint8_t* SkAAClip::findX(const uint8_t data[], int x, int* initialCount) const {
  702. SkASSERT(x_in_rect(x, fBounds));
  703. x -= fBounds.x();
  704. // first skip up to X
  705. for (;;) {
  706. int n = data[0];
  707. if (x < n) {
  708. if (initialCount) {
  709. *initialCount = n - x;
  710. }
  711. break;
  712. }
  713. data += 2;
  714. x -= n;
  715. }
  716. return data;
  717. }
  718. bool SkAAClip::quickContains(int left, int top, int right, int bottom) const {
  719. if (this->isEmpty()) {
  720. return false;
  721. }
  722. if (!fBounds.contains(left, top, right, bottom)) {
  723. return false;
  724. }
  725. #if 0
  726. if (this->isRect()) {
  727. return true;
  728. }
  729. #endif
  730. int lastY SK_INIT_TO_AVOID_WARNING;
  731. const uint8_t* row = this->findRow(top, &lastY);
  732. if (lastY < bottom) {
  733. return false;
  734. }
  735. // now just need to check in X
  736. int count;
  737. row = this->findX(row, left, &count);
  738. #if 0
  739. return count >= (right - left) && 0xFF == row[1];
  740. #else
  741. int rectWidth = right - left;
  742. while (0xFF == row[1]) {
  743. if (count >= rectWidth) {
  744. return true;
  745. }
  746. rectWidth -= count;
  747. row += 2;
  748. count = row[0];
  749. }
  750. return false;
  751. #endif
  752. }
  753. ///////////////////////////////////////////////////////////////////////////////
  754. class SkAAClip::Builder {
  755. SkIRect fBounds;
  756. struct Row {
  757. int fY;
  758. int fWidth;
  759. SkTDArray<uint8_t>* fData;
  760. };
  761. SkTDArray<Row> fRows;
  762. Row* fCurrRow;
  763. int fPrevY;
  764. int fWidth;
  765. int fMinY;
  766. public:
  767. Builder(const SkIRect& bounds) : fBounds(bounds) {
  768. fPrevY = -1;
  769. fWidth = bounds.width();
  770. fCurrRow = nullptr;
  771. fMinY = bounds.fTop;
  772. }
  773. ~Builder() {
  774. Row* row = fRows.begin();
  775. Row* stop = fRows.end();
  776. while (row < stop) {
  777. delete row->fData;
  778. row += 1;
  779. }
  780. }
  781. const SkIRect& getBounds() const { return fBounds; }
  782. void addRun(int x, int y, U8CPU alpha, int count) {
  783. SkASSERT(count > 0);
  784. SkASSERT(fBounds.contains(x, y));
  785. SkASSERT(fBounds.contains(x + count - 1, y));
  786. x -= fBounds.left();
  787. y -= fBounds.top();
  788. Row* row = fCurrRow;
  789. if (y != fPrevY) {
  790. SkASSERT(y > fPrevY);
  791. fPrevY = y;
  792. row = this->flushRow(true);
  793. row->fY = y;
  794. row->fWidth = 0;
  795. SkASSERT(row->fData);
  796. SkASSERT(0 == row->fData->count());
  797. fCurrRow = row;
  798. }
  799. SkASSERT(row->fWidth <= x);
  800. SkASSERT(row->fWidth < fBounds.width());
  801. SkTDArray<uint8_t>& data = *row->fData;
  802. int gap = x - row->fWidth;
  803. if (gap) {
  804. AppendRun(data, 0, gap);
  805. row->fWidth += gap;
  806. SkASSERT(row->fWidth < fBounds.width());
  807. }
  808. AppendRun(data, alpha, count);
  809. row->fWidth += count;
  810. SkASSERT(row->fWidth <= fBounds.width());
  811. }
  812. void addColumn(int x, int y, U8CPU alpha, int height) {
  813. SkASSERT(fBounds.contains(x, y + height - 1));
  814. this->addRun(x, y, alpha, 1);
  815. this->flushRowH(fCurrRow);
  816. y -= fBounds.fTop;
  817. SkASSERT(y == fCurrRow->fY);
  818. fCurrRow->fY = y + height - 1;
  819. }
  820. void addRectRun(int x, int y, int width, int height) {
  821. SkASSERT(fBounds.contains(x + width - 1, y + height - 1));
  822. this->addRun(x, y, 0xFF, width);
  823. // we assum the rect must be all we'll see for these scanlines
  824. // so we ensure our row goes all the way to our right
  825. this->flushRowH(fCurrRow);
  826. y -= fBounds.fTop;
  827. SkASSERT(y == fCurrRow->fY);
  828. fCurrRow->fY = y + height - 1;
  829. }
  830. void addAntiRectRun(int x, int y, int width, int height,
  831. SkAlpha leftAlpha, SkAlpha rightAlpha) {
  832. // According to SkBlitter.cpp, no matter whether leftAlpha is 0 or positive,
  833. // we should always consider [x, x+1] as the left-most column and [x+1, x+1+width]
  834. // as the rect with full alpha.
  835. SkASSERT(fBounds.contains(x + width + (rightAlpha > 0 ? 1 : 0),
  836. y + height - 1));
  837. SkASSERT(width >= 0);
  838. // Conceptually we're always adding 3 runs, but we should
  839. // merge or omit them if possible.
  840. if (leftAlpha == 0xFF) {
  841. width++;
  842. } else if (leftAlpha > 0) {
  843. this->addRun(x++, y, leftAlpha, 1);
  844. } else {
  845. // leftAlpha is 0, ignore the left column
  846. x++;
  847. }
  848. if (rightAlpha == 0xFF) {
  849. width++;
  850. }
  851. if (width > 0) {
  852. this->addRun(x, y, 0xFF, width);
  853. }
  854. if (rightAlpha > 0 && rightAlpha < 255) {
  855. this->addRun(x + width, y, rightAlpha, 1);
  856. }
  857. // if we never called addRun, we might not have a fCurrRow yet
  858. if (fCurrRow) {
  859. // we assume the rect must be all we'll see for these scanlines
  860. // so we ensure our row goes all the way to our right
  861. this->flushRowH(fCurrRow);
  862. y -= fBounds.fTop;
  863. SkASSERT(y == fCurrRow->fY);
  864. fCurrRow->fY = y + height - 1;
  865. }
  866. }
  867. bool finish(SkAAClip* target) {
  868. this->flushRow(false);
  869. const Row* row = fRows.begin();
  870. const Row* stop = fRows.end();
  871. size_t dataSize = 0;
  872. while (row < stop) {
  873. dataSize += row->fData->count();
  874. row += 1;
  875. }
  876. if (0 == dataSize) {
  877. return target->setEmpty();
  878. }
  879. SkASSERT(fMinY >= fBounds.fTop);
  880. SkASSERT(fMinY < fBounds.fBottom);
  881. int adjustY = fMinY - fBounds.fTop;
  882. fBounds.fTop = fMinY;
  883. RunHead* head = RunHead::Alloc(fRows.count(), dataSize);
  884. YOffset* yoffset = head->yoffsets();
  885. uint8_t* data = head->data();
  886. uint8_t* baseData = data;
  887. row = fRows.begin();
  888. SkDEBUGCODE(int prevY = row->fY - 1;)
  889. while (row < stop) {
  890. SkASSERT(prevY < row->fY); // must be monotonic
  891. SkDEBUGCODE(prevY = row->fY);
  892. yoffset->fY = row->fY - adjustY;
  893. yoffset->fOffset = SkToU32(data - baseData);
  894. yoffset += 1;
  895. size_t n = row->fData->count();
  896. memcpy(data, row->fData->begin(), n);
  897. #ifdef SK_DEBUG
  898. size_t bytesNeeded = compute_row_length(data, fBounds.width());
  899. SkASSERT(bytesNeeded == n);
  900. #endif
  901. data += n;
  902. row += 1;
  903. }
  904. target->freeRuns();
  905. target->fBounds = fBounds;
  906. target->fRunHead = head;
  907. return target->trimBounds();
  908. }
  909. void dump() {
  910. this->validate();
  911. int y;
  912. for (y = 0; y < fRows.count(); ++y) {
  913. const Row& row = fRows[y];
  914. SkDebugf("Y:%3d W:%3d", row.fY, row.fWidth);
  915. const SkTDArray<uint8_t>& data = *row.fData;
  916. int count = data.count();
  917. SkASSERT(!(count & 1));
  918. const uint8_t* ptr = data.begin();
  919. for (int x = 0; x < count; x += 2) {
  920. SkDebugf(" [%3d:%02X]", ptr[0], ptr[1]);
  921. ptr += 2;
  922. }
  923. SkDebugf("\n");
  924. }
  925. }
  926. void validate() {
  927. #ifdef SK_DEBUG
  928. int prevY = -1;
  929. for (int i = 0; i < fRows.count(); ++i) {
  930. const Row& row = fRows[i];
  931. SkASSERT(prevY < row.fY);
  932. SkASSERT(fWidth == row.fWidth);
  933. int count = row.fData->count();
  934. const uint8_t* ptr = row.fData->begin();
  935. SkASSERT(!(count & 1));
  936. int w = 0;
  937. for (int x = 0; x < count; x += 2) {
  938. int n = ptr[0];
  939. SkASSERT(n > 0);
  940. w += n;
  941. SkASSERT(w <= fWidth);
  942. ptr += 2;
  943. }
  944. SkASSERT(w == fWidth);
  945. prevY = row.fY;
  946. }
  947. #endif
  948. }
  949. // only called by BuilderBlitter
  950. void setMinY(int y) {
  951. fMinY = y;
  952. }
  953. private:
  954. void flushRowH(Row* row) {
  955. // flush current row if needed
  956. if (row->fWidth < fWidth) {
  957. AppendRun(*row->fData, 0, fWidth - row->fWidth);
  958. row->fWidth = fWidth;
  959. }
  960. }
  961. Row* flushRow(bool readyForAnother) {
  962. Row* next = nullptr;
  963. int count = fRows.count();
  964. if (count > 0) {
  965. this->flushRowH(&fRows[count - 1]);
  966. }
  967. if (count > 1) {
  968. // are our last two runs the same?
  969. Row* prev = &fRows[count - 2];
  970. Row* curr = &fRows[count - 1];
  971. SkASSERT(prev->fWidth == fWidth);
  972. SkASSERT(curr->fWidth == fWidth);
  973. if (*prev->fData == *curr->fData) {
  974. prev->fY = curr->fY;
  975. if (readyForAnother) {
  976. curr->fData->rewind();
  977. next = curr;
  978. } else {
  979. delete curr->fData;
  980. fRows.removeShuffle(count - 1);
  981. }
  982. } else {
  983. if (readyForAnother) {
  984. next = fRows.append();
  985. next->fData = new SkTDArray<uint8_t>;
  986. }
  987. }
  988. } else {
  989. if (readyForAnother) {
  990. next = fRows.append();
  991. next->fData = new SkTDArray<uint8_t>;
  992. }
  993. }
  994. return next;
  995. }
  996. static void AppendRun(SkTDArray<uint8_t>& data, U8CPU alpha, int count) {
  997. do {
  998. int n = count;
  999. if (n > 255) {
  1000. n = 255;
  1001. }
  1002. uint8_t* ptr = data.append(2);
  1003. ptr[0] = n;
  1004. ptr[1] = alpha;
  1005. count -= n;
  1006. } while (count > 0);
  1007. }
  1008. };
  1009. class SkAAClip::BuilderBlitter : public SkBlitter {
  1010. int fLastY;
  1011. /*
  1012. If we see a gap of 1 or more empty scanlines while building in Y-order,
  1013. we inject an explicit empty scanline (alpha==0)
  1014. See AAClipTest.cpp : test_path_with_hole()
  1015. */
  1016. void checkForYGap(int y) {
  1017. SkASSERT(y >= fLastY);
  1018. if (fLastY > -SK_MaxS32) {
  1019. int gap = y - fLastY;
  1020. if (gap > 1) {
  1021. fBuilder->addRun(fLeft, y - 1, 0, fRight - fLeft);
  1022. }
  1023. }
  1024. fLastY = y;
  1025. }
  1026. public:
  1027. BuilderBlitter(Builder* builder) {
  1028. fBuilder = builder;
  1029. fLeft = builder->getBounds().fLeft;
  1030. fRight = builder->getBounds().fRight;
  1031. fMinY = SK_MaxS32;
  1032. fLastY = -SK_MaxS32; // sentinel
  1033. }
  1034. void finish() {
  1035. if (fMinY < SK_MaxS32) {
  1036. fBuilder->setMinY(fMinY);
  1037. }
  1038. }
  1039. /**
  1040. Must evaluate clips in scan-line order, so don't want to allow blitV(),
  1041. but an AAClip can be clipped down to a single pixel wide, so we
  1042. must support it (given AntiRect semantics: minimum width is 2).
  1043. Instead we'll rely on the runtime asserts to guarantee Y monotonicity;
  1044. any failure cases that misses may have minor artifacts.
  1045. */
  1046. void blitV(int x, int y, int height, SkAlpha alpha) override {
  1047. if (height == 1) {
  1048. // We're still in scan-line order if height is 1
  1049. // This is useful for Analytic AA
  1050. const SkAlpha alphas[2] = {alpha, 0};
  1051. const int16_t runs[2] = {1, 0};
  1052. this->blitAntiH(x, y, alphas, runs);
  1053. } else {
  1054. this->recordMinY(y);
  1055. fBuilder->addColumn(x, y, alpha, height);
  1056. fLastY = y + height - 1;
  1057. }
  1058. }
  1059. void blitRect(int x, int y, int width, int height) override {
  1060. this->recordMinY(y);
  1061. this->checkForYGap(y);
  1062. fBuilder->addRectRun(x, y, width, height);
  1063. fLastY = y + height - 1;
  1064. }
  1065. virtual void blitAntiRect(int x, int y, int width, int height,
  1066. SkAlpha leftAlpha, SkAlpha rightAlpha) override {
  1067. this->recordMinY(y);
  1068. this->checkForYGap(y);
  1069. fBuilder->addAntiRectRun(x, y, width, height, leftAlpha, rightAlpha);
  1070. fLastY = y + height - 1;
  1071. }
  1072. void blitMask(const SkMask&, const SkIRect& clip) override
  1073. { unexpected(); }
  1074. const SkPixmap* justAnOpaqueColor(uint32_t*) override {
  1075. return nullptr;
  1076. }
  1077. void blitH(int x, int y, int width) override {
  1078. this->recordMinY(y);
  1079. this->checkForYGap(y);
  1080. fBuilder->addRun(x, y, 0xFF, width);
  1081. }
  1082. virtual void blitAntiH(int x, int y, const SkAlpha alpha[],
  1083. const int16_t runs[]) override {
  1084. this->recordMinY(y);
  1085. this->checkForYGap(y);
  1086. for (;;) {
  1087. int count = *runs;
  1088. if (count <= 0) {
  1089. return;
  1090. }
  1091. // The supersampler's buffer can be the width of the device, so
  1092. // we may have to trim the run to our bounds. Previously, we assert that
  1093. // the extra spans are always alpha==0.
  1094. // However, the analytic AA is too sensitive to precision errors
  1095. // so it may have extra spans with very tiny alpha because after several
  1096. // arithmatic operations, the edge may bleed the path boundary a little bit.
  1097. // Therefore, instead of always asserting alpha==0, we assert alpha < 0x10.
  1098. int localX = x;
  1099. int localCount = count;
  1100. if (x < fLeft) {
  1101. SkASSERT(0x10 > *alpha);
  1102. int gap = fLeft - x;
  1103. SkASSERT(gap <= count);
  1104. localX += gap;
  1105. localCount -= gap;
  1106. }
  1107. int right = x + count;
  1108. if (right > fRight) {
  1109. SkASSERT(0x10 > *alpha);
  1110. localCount -= right - fRight;
  1111. SkASSERT(localCount >= 0);
  1112. }
  1113. if (localCount) {
  1114. fBuilder->addRun(localX, y, *alpha, localCount);
  1115. }
  1116. // Next run
  1117. runs += count;
  1118. alpha += count;
  1119. x += count;
  1120. }
  1121. }
  1122. private:
  1123. Builder* fBuilder;
  1124. int fLeft; // cache of builder's bounds' left edge
  1125. int fRight;
  1126. int fMinY;
  1127. /*
  1128. * We track this, in case the scan converter skipped some number of
  1129. * scanlines at the (relative to the bounds it was given). This allows
  1130. * the builder, during its finish, to trip its bounds down to the "real"
  1131. * top.
  1132. */
  1133. void recordMinY(int y) {
  1134. if (y < fMinY) {
  1135. fMinY = y;
  1136. }
  1137. }
  1138. void unexpected() {
  1139. SK_ABORT("---- did not expect to get called here");
  1140. }
  1141. };
  1142. bool SkAAClip::setPath(const SkPath& path, const SkRegion* clip, bool doAA) {
  1143. AUTO_AACLIP_VALIDATE(*this);
  1144. if (clip && clip->isEmpty()) {
  1145. return this->setEmpty();
  1146. }
  1147. SkIRect ibounds;
  1148. path.getBounds().roundOut(&ibounds);
  1149. SkRegion tmpClip;
  1150. if (nullptr == clip) {
  1151. tmpClip.setRect(ibounds);
  1152. clip = &tmpClip;
  1153. }
  1154. // Since we assert that the BuilderBlitter will never blit outside the intersection
  1155. // of clip and ibounds, we create this snugClip to be that intersection and send it
  1156. // to the scan-converter.
  1157. SkRegion snugClip(*clip);
  1158. if (path.isInverseFillType()) {
  1159. ibounds = clip->getBounds();
  1160. } else {
  1161. if (ibounds.isEmpty() || !ibounds.intersect(clip->getBounds())) {
  1162. return this->setEmpty();
  1163. }
  1164. snugClip.op(ibounds, SkRegion::kIntersect_Op);
  1165. }
  1166. Builder builder(ibounds);
  1167. BuilderBlitter blitter(&builder);
  1168. if (doAA) {
  1169. SkScan::AntiFillPath(path, snugClip, &blitter, true);
  1170. } else {
  1171. SkScan::FillPath(path, snugClip, &blitter);
  1172. }
  1173. blitter.finish();
  1174. return builder.finish(this);
  1175. }
  1176. ///////////////////////////////////////////////////////////////////////////////
  1177. typedef void (*RowProc)(SkAAClip::Builder&, int bottom,
  1178. const uint8_t* rowA, const SkIRect& rectA,
  1179. const uint8_t* rowB, const SkIRect& rectB);
  1180. typedef U8CPU (*AlphaProc)(U8CPU alphaA, U8CPU alphaB);
  1181. static U8CPU sectAlphaProc(U8CPU alphaA, U8CPU alphaB) {
  1182. // Multiply
  1183. return SkMulDiv255Round(alphaA, alphaB);
  1184. }
  1185. static U8CPU unionAlphaProc(U8CPU alphaA, U8CPU alphaB) {
  1186. // SrcOver
  1187. return alphaA + alphaB - SkMulDiv255Round(alphaA, alphaB);
  1188. }
  1189. static U8CPU diffAlphaProc(U8CPU alphaA, U8CPU alphaB) {
  1190. // SrcOut
  1191. return SkMulDiv255Round(alphaA, 0xFF - alphaB);
  1192. }
  1193. static U8CPU xorAlphaProc(U8CPU alphaA, U8CPU alphaB) {
  1194. // XOR
  1195. return alphaA + alphaB - 2 * SkMulDiv255Round(alphaA, alphaB);
  1196. }
  1197. static AlphaProc find_alpha_proc(SkRegion::Op op) {
  1198. switch (op) {
  1199. case SkRegion::kIntersect_Op:
  1200. return sectAlphaProc;
  1201. case SkRegion::kDifference_Op:
  1202. return diffAlphaProc;
  1203. case SkRegion::kUnion_Op:
  1204. return unionAlphaProc;
  1205. case SkRegion::kXOR_Op:
  1206. return xorAlphaProc;
  1207. default:
  1208. SkDEBUGFAIL("unexpected region op");
  1209. return sectAlphaProc;
  1210. }
  1211. }
  1212. class RowIter {
  1213. public:
  1214. RowIter(const uint8_t* row, const SkIRect& bounds) {
  1215. fRow = row;
  1216. fLeft = bounds.fLeft;
  1217. fBoundsRight = bounds.fRight;
  1218. if (row) {
  1219. fRight = bounds.fLeft + row[0];
  1220. SkASSERT(fRight <= fBoundsRight);
  1221. fAlpha = row[1];
  1222. fDone = false;
  1223. } else {
  1224. fDone = true;
  1225. fRight = kMaxInt32;
  1226. fAlpha = 0;
  1227. }
  1228. }
  1229. bool done() const { return fDone; }
  1230. int left() const { return fLeft; }
  1231. int right() const { return fRight; }
  1232. U8CPU alpha() const { return fAlpha; }
  1233. void next() {
  1234. if (!fDone) {
  1235. fLeft = fRight;
  1236. if (fRight == fBoundsRight) {
  1237. fDone = true;
  1238. fRight = kMaxInt32;
  1239. fAlpha = 0;
  1240. } else {
  1241. fRow += 2;
  1242. fRight += fRow[0];
  1243. fAlpha = fRow[1];
  1244. SkASSERT(fRight <= fBoundsRight);
  1245. }
  1246. }
  1247. }
  1248. private:
  1249. const uint8_t* fRow;
  1250. int fLeft;
  1251. int fRight;
  1252. int fBoundsRight;
  1253. bool fDone;
  1254. uint8_t fAlpha;
  1255. };
  1256. static void adjust_row(RowIter& iter, int& leftA, int& riteA, int rite) {
  1257. if (rite == riteA) {
  1258. iter.next();
  1259. leftA = iter.left();
  1260. riteA = iter.right();
  1261. }
  1262. }
  1263. #if 0 // UNUSED
  1264. static bool intersect(int& min, int& max, int boundsMin, int boundsMax) {
  1265. SkASSERT(min < max);
  1266. SkASSERT(boundsMin < boundsMax);
  1267. if (min >= boundsMax || max <= boundsMin) {
  1268. return false;
  1269. }
  1270. if (min < boundsMin) {
  1271. min = boundsMin;
  1272. }
  1273. if (max > boundsMax) {
  1274. max = boundsMax;
  1275. }
  1276. return true;
  1277. }
  1278. #endif
  1279. static void operatorX(SkAAClip::Builder& builder, int lastY,
  1280. RowIter& iterA, RowIter& iterB,
  1281. AlphaProc proc, const SkIRect& bounds) {
  1282. int leftA = iterA.left();
  1283. int riteA = iterA.right();
  1284. int leftB = iterB.left();
  1285. int riteB = iterB.right();
  1286. int prevRite = bounds.fLeft;
  1287. do {
  1288. U8CPU alphaA = 0;
  1289. U8CPU alphaB = 0;
  1290. int left, rite;
  1291. if (leftA < leftB) {
  1292. left = leftA;
  1293. alphaA = iterA.alpha();
  1294. if (riteA <= leftB) {
  1295. rite = riteA;
  1296. } else {
  1297. rite = leftA = leftB;
  1298. }
  1299. } else if (leftB < leftA) {
  1300. left = leftB;
  1301. alphaB = iterB.alpha();
  1302. if (riteB <= leftA) {
  1303. rite = riteB;
  1304. } else {
  1305. rite = leftB = leftA;
  1306. }
  1307. } else {
  1308. left = leftA; // or leftB, since leftA == leftB
  1309. rite = leftA = leftB = SkMin32(riteA, riteB);
  1310. alphaA = iterA.alpha();
  1311. alphaB = iterB.alpha();
  1312. }
  1313. if (left >= bounds.fRight) {
  1314. break;
  1315. }
  1316. if (rite > bounds.fRight) {
  1317. rite = bounds.fRight;
  1318. }
  1319. if (left >= bounds.fLeft) {
  1320. SkASSERT(rite > left);
  1321. builder.addRun(left, lastY, proc(alphaA, alphaB), rite - left);
  1322. prevRite = rite;
  1323. }
  1324. adjust_row(iterA, leftA, riteA, rite);
  1325. adjust_row(iterB, leftB, riteB, rite);
  1326. } while (!iterA.done() || !iterB.done());
  1327. if (prevRite < bounds.fRight) {
  1328. builder.addRun(prevRite, lastY, 0, bounds.fRight - prevRite);
  1329. }
  1330. }
  1331. static void adjust_iter(SkAAClip::Iter& iter, int& topA, int& botA, int bot) {
  1332. if (bot == botA) {
  1333. iter.next();
  1334. topA = botA;
  1335. SkASSERT(botA == iter.top());
  1336. botA = iter.bottom();
  1337. }
  1338. }
  1339. static void operateY(SkAAClip::Builder& builder, const SkAAClip& A,
  1340. const SkAAClip& B, SkRegion::Op op) {
  1341. AlphaProc proc = find_alpha_proc(op);
  1342. const SkIRect& bounds = builder.getBounds();
  1343. SkAAClip::Iter iterA(A);
  1344. SkAAClip::Iter iterB(B);
  1345. SkASSERT(!iterA.done());
  1346. int topA = iterA.top();
  1347. int botA = iterA.bottom();
  1348. SkASSERT(!iterB.done());
  1349. int topB = iterB.top();
  1350. int botB = iterB.bottom();
  1351. do {
  1352. const uint8_t* rowA = nullptr;
  1353. const uint8_t* rowB = nullptr;
  1354. int top, bot;
  1355. if (topA < topB) {
  1356. top = topA;
  1357. rowA = iterA.data();
  1358. if (botA <= topB) {
  1359. bot = botA;
  1360. } else {
  1361. bot = topA = topB;
  1362. }
  1363. } else if (topB < topA) {
  1364. top = topB;
  1365. rowB = iterB.data();
  1366. if (botB <= topA) {
  1367. bot = botB;
  1368. } else {
  1369. bot = topB = topA;
  1370. }
  1371. } else {
  1372. top = topA; // or topB, since topA == topB
  1373. bot = topA = topB = SkMin32(botA, botB);
  1374. rowA = iterA.data();
  1375. rowB = iterB.data();
  1376. }
  1377. if (top >= bounds.fBottom) {
  1378. break;
  1379. }
  1380. if (bot > bounds.fBottom) {
  1381. bot = bounds.fBottom;
  1382. }
  1383. SkASSERT(top < bot);
  1384. if (!rowA && !rowB) {
  1385. builder.addRun(bounds.fLeft, bot - 1, 0, bounds.width());
  1386. } else if (top >= bounds.fTop) {
  1387. SkASSERT(bot <= bounds.fBottom);
  1388. RowIter rowIterA(rowA, rowA ? A.getBounds() : bounds);
  1389. RowIter rowIterB(rowB, rowB ? B.getBounds() : bounds);
  1390. operatorX(builder, bot - 1, rowIterA, rowIterB, proc, bounds);
  1391. }
  1392. adjust_iter(iterA, topA, botA, bot);
  1393. adjust_iter(iterB, topB, botB, bot);
  1394. } while (!iterA.done() || !iterB.done());
  1395. }
  1396. bool SkAAClip::op(const SkAAClip& clipAOrig, const SkAAClip& clipBOrig,
  1397. SkRegion::Op op) {
  1398. AUTO_AACLIP_VALIDATE(*this);
  1399. if (SkRegion::kReplace_Op == op) {
  1400. return this->set(clipBOrig);
  1401. }
  1402. const SkAAClip* clipA = &clipAOrig;
  1403. const SkAAClip* clipB = &clipBOrig;
  1404. if (SkRegion::kReverseDifference_Op == op) {
  1405. using std::swap;
  1406. swap(clipA, clipB);
  1407. op = SkRegion::kDifference_Op;
  1408. }
  1409. bool a_empty = clipA->isEmpty();
  1410. bool b_empty = clipB->isEmpty();
  1411. SkIRect bounds;
  1412. switch (op) {
  1413. case SkRegion::kDifference_Op:
  1414. if (a_empty) {
  1415. return this->setEmpty();
  1416. }
  1417. if (b_empty || !SkIRect::Intersects(clipA->fBounds, clipB->fBounds)) {
  1418. return this->set(*clipA);
  1419. }
  1420. bounds = clipA->fBounds;
  1421. break;
  1422. case SkRegion::kIntersect_Op:
  1423. if ((a_empty | b_empty) || !bounds.intersect(clipA->fBounds,
  1424. clipB->fBounds)) {
  1425. return this->setEmpty();
  1426. }
  1427. break;
  1428. case SkRegion::kUnion_Op:
  1429. case SkRegion::kXOR_Op:
  1430. if (a_empty) {
  1431. return this->set(*clipB);
  1432. }
  1433. if (b_empty) {
  1434. return this->set(*clipA);
  1435. }
  1436. bounds = clipA->fBounds;
  1437. bounds.join(clipB->fBounds);
  1438. break;
  1439. default:
  1440. SkDEBUGFAIL("unknown region op");
  1441. return !this->isEmpty();
  1442. }
  1443. SkASSERT(SkIRect::Intersects(bounds, clipB->fBounds));
  1444. SkASSERT(SkIRect::Intersects(bounds, clipB->fBounds));
  1445. Builder builder(bounds);
  1446. operateY(builder, *clipA, *clipB, op);
  1447. return builder.finish(this);
  1448. }
  1449. /*
  1450. * It can be expensive to build a local aaclip before applying the op, so
  1451. * we first see if we can restrict the bounds of new rect to our current
  1452. * bounds, or note that the new rect subsumes our current clip.
  1453. */
  1454. bool SkAAClip::op(const SkIRect& rOrig, SkRegion::Op op) {
  1455. SkIRect rStorage;
  1456. const SkIRect* r = &rOrig;
  1457. switch (op) {
  1458. case SkRegion::kIntersect_Op:
  1459. if (!rStorage.intersect(rOrig, fBounds)) {
  1460. // no overlap, so we're empty
  1461. return this->setEmpty();
  1462. }
  1463. if (rStorage == fBounds) {
  1464. // we were wholly inside the rect, no change
  1465. return !this->isEmpty();
  1466. }
  1467. if (this->quickContains(rStorage)) {
  1468. // the intersection is wholly inside us, we're a rect
  1469. return this->setRect(rStorage);
  1470. }
  1471. r = &rStorage; // use the intersected bounds
  1472. break;
  1473. case SkRegion::kDifference_Op:
  1474. break;
  1475. case SkRegion::kUnion_Op:
  1476. if (rOrig.contains(fBounds)) {
  1477. return this->setRect(rOrig);
  1478. }
  1479. break;
  1480. default:
  1481. break;
  1482. }
  1483. SkAAClip clip;
  1484. clip.setRect(*r);
  1485. return this->op(*this, clip, op);
  1486. }
  1487. bool SkAAClip::op(const SkRect& rOrig, SkRegion::Op op, bool doAA) {
  1488. SkRect rStorage, boundsStorage;
  1489. const SkRect* r = &rOrig;
  1490. boundsStorage.set(fBounds);
  1491. switch (op) {
  1492. case SkRegion::kIntersect_Op:
  1493. case SkRegion::kDifference_Op:
  1494. if (!rStorage.intersect(rOrig, boundsStorage)) {
  1495. if (SkRegion::kIntersect_Op == op) {
  1496. return this->setEmpty();
  1497. } else { // kDifference
  1498. return !this->isEmpty();
  1499. }
  1500. }
  1501. r = &rStorage; // use the intersected bounds
  1502. break;
  1503. case SkRegion::kUnion_Op:
  1504. if (rOrig.contains(boundsStorage)) {
  1505. return this->setRect(rOrig);
  1506. }
  1507. break;
  1508. default:
  1509. break;
  1510. }
  1511. SkAAClip clip;
  1512. clip.setRect(*r, doAA);
  1513. return this->op(*this, clip, op);
  1514. }
  1515. bool SkAAClip::op(const SkAAClip& clip, SkRegion::Op op) {
  1516. return this->op(*this, clip, op);
  1517. }
  1518. ///////////////////////////////////////////////////////////////////////////////
  1519. bool SkAAClip::translate(int dx, int dy, SkAAClip* dst) const {
  1520. if (nullptr == dst) {
  1521. return !this->isEmpty();
  1522. }
  1523. if (this->isEmpty()) {
  1524. return dst->setEmpty();
  1525. }
  1526. if (this != dst) {
  1527. fRunHead->fRefCnt++;
  1528. dst->freeRuns();
  1529. dst->fRunHead = fRunHead;
  1530. dst->fBounds = fBounds;
  1531. }
  1532. dst->fBounds.offset(dx, dy);
  1533. return true;
  1534. }
  1535. static void expand_row_to_mask(uint8_t* SK_RESTRICT mask,
  1536. const uint8_t* SK_RESTRICT row,
  1537. int width) {
  1538. while (width > 0) {
  1539. int n = row[0];
  1540. SkASSERT(width >= n);
  1541. memset(mask, row[1], n);
  1542. mask += n;
  1543. row += 2;
  1544. width -= n;
  1545. }
  1546. SkASSERT(0 == width);
  1547. }
  1548. void SkAAClip::copyToMask(SkMask* mask) const {
  1549. mask->fFormat = SkMask::kA8_Format;
  1550. if (this->isEmpty()) {
  1551. mask->fBounds.setEmpty();
  1552. mask->fImage = nullptr;
  1553. mask->fRowBytes = 0;
  1554. return;
  1555. }
  1556. mask->fBounds = fBounds;
  1557. mask->fRowBytes = fBounds.width();
  1558. size_t size = mask->computeImageSize();
  1559. mask->fImage = SkMask::AllocImage(size);
  1560. Iter iter(*this);
  1561. uint8_t* dst = mask->fImage;
  1562. const int width = fBounds.width();
  1563. int y = fBounds.fTop;
  1564. while (!iter.done()) {
  1565. do {
  1566. expand_row_to_mask(dst, iter.data(), width);
  1567. dst += mask->fRowBytes;
  1568. } while (++y < iter.bottom());
  1569. iter.next();
  1570. }
  1571. }
  1572. ///////////////////////////////////////////////////////////////////////////////
  1573. ///////////////////////////////////////////////////////////////////////////////
  1574. static void expandToRuns(const uint8_t* SK_RESTRICT data, int initialCount, int width,
  1575. int16_t* SK_RESTRICT runs, SkAlpha* SK_RESTRICT aa) {
  1576. // we don't read our initial n from data, since the caller may have had to
  1577. // clip it, hence the initialCount parameter.
  1578. int n = initialCount;
  1579. for (;;) {
  1580. if (n > width) {
  1581. n = width;
  1582. }
  1583. SkASSERT(n > 0);
  1584. runs[0] = n;
  1585. runs += n;
  1586. aa[0] = data[1];
  1587. aa += n;
  1588. data += 2;
  1589. width -= n;
  1590. if (0 == width) {
  1591. break;
  1592. }
  1593. // load the next count
  1594. n = data[0];
  1595. }
  1596. runs[0] = 0; // sentinel
  1597. }
  1598. SkAAClipBlitter::~SkAAClipBlitter() {
  1599. sk_free(fScanlineScratch);
  1600. }
  1601. void SkAAClipBlitter::ensureRunsAndAA() {
  1602. if (nullptr == fScanlineScratch) {
  1603. // add 1 so we can store the terminating run count of 0
  1604. int count = fAAClipBounds.width() + 1;
  1605. // we use this either for fRuns + fAA, or a scaline of a mask
  1606. // which may be as deep as 32bits
  1607. fScanlineScratch = sk_malloc_throw(count * sizeof(SkPMColor));
  1608. fRuns = (int16_t*)fScanlineScratch;
  1609. fAA = (SkAlpha*)(fRuns + count);
  1610. }
  1611. }
  1612. void SkAAClipBlitter::blitH(int x, int y, int width) {
  1613. SkASSERT(width > 0);
  1614. SkASSERT(fAAClipBounds.contains(x, y));
  1615. SkASSERT(fAAClipBounds.contains(x + width - 1, y));
  1616. const uint8_t* row = fAAClip->findRow(y);
  1617. int initialCount;
  1618. row = fAAClip->findX(row, x, &initialCount);
  1619. if (initialCount >= width) {
  1620. SkAlpha alpha = row[1];
  1621. if (0 == alpha) {
  1622. return;
  1623. }
  1624. if (0xFF == alpha) {
  1625. fBlitter->blitH(x, y, width);
  1626. return;
  1627. }
  1628. }
  1629. this->ensureRunsAndAA();
  1630. expandToRuns(row, initialCount, width, fRuns, fAA);
  1631. fBlitter->blitAntiH(x, y, fAA, fRuns);
  1632. }
  1633. static void merge(const uint8_t* SK_RESTRICT row, int rowN,
  1634. const SkAlpha* SK_RESTRICT srcAA,
  1635. const int16_t* SK_RESTRICT srcRuns,
  1636. SkAlpha* SK_RESTRICT dstAA,
  1637. int16_t* SK_RESTRICT dstRuns,
  1638. int width) {
  1639. SkDEBUGCODE(int accumulated = 0;)
  1640. int srcN = srcRuns[0];
  1641. // do we need this check?
  1642. if (0 == srcN) {
  1643. return;
  1644. }
  1645. for (;;) {
  1646. SkASSERT(rowN > 0);
  1647. SkASSERT(srcN > 0);
  1648. unsigned newAlpha = SkMulDiv255Round(srcAA[0], row[1]);
  1649. int minN = SkMin32(srcN, rowN);
  1650. dstRuns[0] = minN;
  1651. dstRuns += minN;
  1652. dstAA[0] = newAlpha;
  1653. dstAA += minN;
  1654. if (0 == (srcN -= minN)) {
  1655. srcN = srcRuns[0]; // refresh
  1656. srcRuns += srcN;
  1657. srcAA += srcN;
  1658. srcN = srcRuns[0]; // reload
  1659. if (0 == srcN) {
  1660. break;
  1661. }
  1662. }
  1663. if (0 == (rowN -= minN)) {
  1664. row += 2;
  1665. rowN = row[0]; // reload
  1666. }
  1667. SkDEBUGCODE(accumulated += minN;)
  1668. SkASSERT(accumulated <= width);
  1669. }
  1670. dstRuns[0] = 0;
  1671. }
  1672. void SkAAClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
  1673. const int16_t runs[]) {
  1674. const uint8_t* row = fAAClip->findRow(y);
  1675. int initialCount;
  1676. row = fAAClip->findX(row, x, &initialCount);
  1677. this->ensureRunsAndAA();
  1678. merge(row, initialCount, aa, runs, fAA, fRuns, fAAClipBounds.width());
  1679. fBlitter->blitAntiH(x, y, fAA, fRuns);
  1680. }
  1681. void SkAAClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
  1682. if (fAAClip->quickContains(x, y, x + 1, y + height)) {
  1683. fBlitter->blitV(x, y, height, alpha);
  1684. return;
  1685. }
  1686. for (;;) {
  1687. int lastY SK_INIT_TO_AVOID_WARNING;
  1688. const uint8_t* row = fAAClip->findRow(y, &lastY);
  1689. int dy = lastY - y + 1;
  1690. if (dy > height) {
  1691. dy = height;
  1692. }
  1693. height -= dy;
  1694. row = fAAClip->findX(row, x);
  1695. SkAlpha newAlpha = SkMulDiv255Round(alpha, row[1]);
  1696. if (newAlpha) {
  1697. fBlitter->blitV(x, y, dy, newAlpha);
  1698. }
  1699. SkASSERT(height >= 0);
  1700. if (height <= 0) {
  1701. break;
  1702. }
  1703. y = lastY + 1;
  1704. }
  1705. }
  1706. void SkAAClipBlitter::blitRect(int x, int y, int width, int height) {
  1707. if (fAAClip->quickContains(x, y, x + width, y + height)) {
  1708. fBlitter->blitRect(x, y, width, height);
  1709. return;
  1710. }
  1711. while (--height >= 0) {
  1712. this->blitH(x, y, width);
  1713. y += 1;
  1714. }
  1715. }
  1716. typedef void (*MergeAAProc)(const void* src, int width, const uint8_t* row,
  1717. int initialRowCount, void* dst);
  1718. static void small_memcpy(void* dst, const void* src, size_t n) {
  1719. memcpy(dst, src, n);
  1720. }
  1721. static void small_bzero(void* dst, size_t n) {
  1722. sk_bzero(dst, n);
  1723. }
  1724. static inline uint8_t mergeOne(uint8_t value, unsigned alpha) {
  1725. return SkMulDiv255Round(value, alpha);
  1726. }
  1727. static inline uint16_t mergeOne(uint16_t value, unsigned alpha) {
  1728. unsigned r = SkGetPackedR16(value);
  1729. unsigned g = SkGetPackedG16(value);
  1730. unsigned b = SkGetPackedB16(value);
  1731. return SkPackRGB16(SkMulDiv255Round(r, alpha),
  1732. SkMulDiv255Round(g, alpha),
  1733. SkMulDiv255Round(b, alpha));
  1734. }
  1735. template <typename T>
  1736. void mergeT(const void* inSrc, int srcN, const uint8_t* SK_RESTRICT row, int rowN, void* inDst) {
  1737. const T* SK_RESTRICT src = static_cast<const T*>(inSrc);
  1738. T* SK_RESTRICT dst = static_cast<T*>(inDst);
  1739. for (;;) {
  1740. SkASSERT(rowN > 0);
  1741. SkASSERT(srcN > 0);
  1742. int n = SkMin32(rowN, srcN);
  1743. unsigned rowA = row[1];
  1744. if (0xFF == rowA) {
  1745. small_memcpy(dst, src, n * sizeof(T));
  1746. } else if (0 == rowA) {
  1747. small_bzero(dst, n * sizeof(T));
  1748. } else {
  1749. for (int i = 0; i < n; ++i) {
  1750. dst[i] = mergeOne(src[i], rowA);
  1751. }
  1752. }
  1753. if (0 == (srcN -= n)) {
  1754. break;
  1755. }
  1756. src += n;
  1757. dst += n;
  1758. SkASSERT(rowN == n);
  1759. row += 2;
  1760. rowN = row[0];
  1761. }
  1762. }
  1763. static MergeAAProc find_merge_aa_proc(SkMask::Format format) {
  1764. switch (format) {
  1765. case SkMask::kBW_Format:
  1766. SkDEBUGFAIL("unsupported");
  1767. return nullptr;
  1768. case SkMask::kA8_Format:
  1769. case SkMask::k3D_Format:
  1770. return mergeT<uint8_t> ;
  1771. case SkMask::kLCD16_Format:
  1772. return mergeT<uint16_t>;
  1773. default:
  1774. SkDEBUGFAIL("unsupported");
  1775. return nullptr;
  1776. }
  1777. }
  1778. static U8CPU bit2byte(int bitInAByte) {
  1779. SkASSERT(bitInAByte <= 0xFF);
  1780. // negation turns any non-zero into 0xFFFFFF??, so we just shift down
  1781. // some value >= 8 to get a full FF value
  1782. return -bitInAByte >> 8;
  1783. }
  1784. static void upscaleBW2A8(SkMask* dstMask, const SkMask& srcMask) {
  1785. SkASSERT(SkMask::kBW_Format == srcMask.fFormat);
  1786. SkASSERT(SkMask::kA8_Format == dstMask->fFormat);
  1787. const int width = srcMask.fBounds.width();
  1788. const int height = srcMask.fBounds.height();
  1789. const uint8_t* SK_RESTRICT src = (const uint8_t*)srcMask.fImage;
  1790. const size_t srcRB = srcMask.fRowBytes;
  1791. uint8_t* SK_RESTRICT dst = (uint8_t*)dstMask->fImage;
  1792. const size_t dstRB = dstMask->fRowBytes;
  1793. const int wholeBytes = width >> 3;
  1794. const int leftOverBits = width & 7;
  1795. for (int y = 0; y < height; ++y) {
  1796. uint8_t* SK_RESTRICT d = dst;
  1797. for (int i = 0; i < wholeBytes; ++i) {
  1798. int srcByte = src[i];
  1799. d[0] = bit2byte(srcByte & (1 << 7));
  1800. d[1] = bit2byte(srcByte & (1 << 6));
  1801. d[2] = bit2byte(srcByte & (1 << 5));
  1802. d[3] = bit2byte(srcByte & (1 << 4));
  1803. d[4] = bit2byte(srcByte & (1 << 3));
  1804. d[5] = bit2byte(srcByte & (1 << 2));
  1805. d[6] = bit2byte(srcByte & (1 << 1));
  1806. d[7] = bit2byte(srcByte & (1 << 0));
  1807. d += 8;
  1808. }
  1809. if (leftOverBits) {
  1810. int srcByte = src[wholeBytes];
  1811. for (int x = 0; x < leftOverBits; ++x) {
  1812. *d++ = bit2byte(srcByte & 0x80);
  1813. srcByte <<= 1;
  1814. }
  1815. }
  1816. src += srcRB;
  1817. dst += dstRB;
  1818. }
  1819. }
  1820. void SkAAClipBlitter::blitMask(const SkMask& origMask, const SkIRect& clip) {
  1821. SkASSERT(fAAClip->getBounds().contains(clip));
  1822. if (fAAClip->quickContains(clip)) {
  1823. fBlitter->blitMask(origMask, clip);
  1824. return;
  1825. }
  1826. const SkMask* mask = &origMask;
  1827. // if we're BW, we need to upscale to A8 (ugh)
  1828. SkMask grayMask;
  1829. if (SkMask::kBW_Format == origMask.fFormat) {
  1830. grayMask.fFormat = SkMask::kA8_Format;
  1831. grayMask.fBounds = origMask.fBounds;
  1832. grayMask.fRowBytes = origMask.fBounds.width();
  1833. size_t size = grayMask.computeImageSize();
  1834. grayMask.fImage = (uint8_t*)fGrayMaskScratch.reset(size,
  1835. SkAutoMalloc::kReuse_OnShrink);
  1836. upscaleBW2A8(&grayMask, origMask);
  1837. mask = &grayMask;
  1838. }
  1839. this->ensureRunsAndAA();
  1840. // HACK -- we are devolving 3D into A8, need to copy the rest of the 3D
  1841. // data into a temp block to support it better (ugh)
  1842. const void* src = mask->getAddr(clip.fLeft, clip.fTop);
  1843. const size_t srcRB = mask->fRowBytes;
  1844. const int width = clip.width();
  1845. MergeAAProc mergeProc = find_merge_aa_proc(mask->fFormat);
  1846. SkMask rowMask;
  1847. rowMask.fFormat = SkMask::k3D_Format == mask->fFormat ? SkMask::kA8_Format : mask->fFormat;
  1848. rowMask.fBounds.fLeft = clip.fLeft;
  1849. rowMask.fBounds.fRight = clip.fRight;
  1850. rowMask.fRowBytes = mask->fRowBytes; // doesn't matter, since our height==1
  1851. rowMask.fImage = (uint8_t*)fScanlineScratch;
  1852. int y = clip.fTop;
  1853. const int stopY = y + clip.height();
  1854. do {
  1855. int localStopY SK_INIT_TO_AVOID_WARNING;
  1856. const uint8_t* row = fAAClip->findRow(y, &localStopY);
  1857. // findRow returns last Y, not stop, so we add 1
  1858. localStopY = SkMin32(localStopY + 1, stopY);
  1859. int initialCount;
  1860. row = fAAClip->findX(row, clip.fLeft, &initialCount);
  1861. do {
  1862. mergeProc(src, width, row, initialCount, rowMask.fImage);
  1863. rowMask.fBounds.fTop = y;
  1864. rowMask.fBounds.fBottom = y + 1;
  1865. fBlitter->blitMask(rowMask, rowMask.fBounds);
  1866. src = (const void*)((const char*)src + srcRB);
  1867. } while (++y < localStopY);
  1868. } while (y < stopY);
  1869. }
  1870. const SkPixmap* SkAAClipBlitter::justAnOpaqueColor(uint32_t* value) {
  1871. return nullptr;
  1872. }