SkRasterPipelineTest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright 2016 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 "include/private/SkHalf.h"
  8. #include "include/private/SkTo.h"
  9. #include "src/core/SkRasterPipeline.h"
  10. #include "src/gpu/GrSwizzle.h"
  11. #include "tests/Test.h"
  12. DEF_TEST(SkRasterPipeline, r) {
  13. // Build and run a simple pipeline to exercise SkRasterPipeline,
  14. // drawing 50% transparent blue over opaque red in half-floats.
  15. uint64_t red = 0x3c00000000003c00ull,
  16. blue = 0x3800380000000000ull,
  17. result;
  18. SkRasterPipeline_MemoryCtx load_s_ctx = { &blue, 0 },
  19. load_d_ctx = { &red, 0 },
  20. store_ctx = { &result, 0 };
  21. SkRasterPipeline_<256> p;
  22. p.append(SkRasterPipeline::load_f16, &load_s_ctx);
  23. p.append(SkRasterPipeline::load_f16_dst, &load_d_ctx);
  24. p.append(SkRasterPipeline::srcover);
  25. p.append(SkRasterPipeline::store_f16, &store_ctx);
  26. p.run(0,0,1,1);
  27. // We should see half-intensity magenta.
  28. REPORTER_ASSERT(r, ((result >> 0) & 0xffff) == 0x3800);
  29. REPORTER_ASSERT(r, ((result >> 16) & 0xffff) == 0x0000);
  30. REPORTER_ASSERT(r, ((result >> 32) & 0xffff) == 0x3800);
  31. REPORTER_ASSERT(r, ((result >> 48) & 0xffff) == 0x3c00);
  32. }
  33. DEF_TEST(SkRasterPipeline_empty, r) {
  34. // No asserts... just a test that this is safe to run.
  35. SkRasterPipeline_<256> p;
  36. p.run(0,0,20,1);
  37. }
  38. DEF_TEST(SkRasterPipeline_nonsense, r) {
  39. // No asserts... just a test that this is safe to run and terminates.
  40. // srcover() calls st->next(); this makes sure we've always got something there to call.
  41. SkRasterPipeline_<256> p;
  42. p.append(SkRasterPipeline::srcover);
  43. p.run(0,0,20,1);
  44. }
  45. DEF_TEST(SkRasterPipeline_JIT, r) {
  46. // This tests a couple odd corners that a JIT backend can stumble over.
  47. uint32_t buf[72] = {
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  50. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. };
  55. SkRasterPipeline_MemoryCtx src = { buf + 0, 0 },
  56. dst = { buf + 36, 0 };
  57. // Copy buf[x] to buf[x+36] for x in [15,35).
  58. SkRasterPipeline_<256> p;
  59. p.append(SkRasterPipeline:: load_8888, &src);
  60. p.append(SkRasterPipeline::store_8888, &dst);
  61. p.run(15,0, 20,1);
  62. for (int i = 0; i < 36; i++) {
  63. if (i < 15 || i == 35) {
  64. REPORTER_ASSERT(r, buf[i+36] == 0);
  65. } else {
  66. REPORTER_ASSERT(r, buf[i+36] == (uint32_t)(i - 11));
  67. }
  68. }
  69. }
  70. static uint16_t h(float f) {
  71. // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
  72. uint32_t sem;
  73. memcpy(&sem, &f, sizeof(sem));
  74. uint32_t s = sem & 0x80000000,
  75. em = sem ^ s;
  76. // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
  77. auto denorm = (int32_t)em < 0x38800000; // I32 comparison is often quicker, and always safe
  78. // here.
  79. return denorm ? SkTo<uint16_t>(0)
  80. : SkTo<uint16_t>((s>>16) + (em>>13) - ((127-15)<<10));
  81. }
  82. DEF_TEST(SkRasterPipeline_tail, r) {
  83. {
  84. float data[][4] = {
  85. {00, 01, 02, 03},
  86. {10, 11, 12, 13},
  87. {20, 21, 22, 23},
  88. {30, 31, 32, 33},
  89. };
  90. float buffer[4][4];
  91. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  92. dst = { &buffer[0][0], 0 };
  93. for (unsigned i = 1; i <= 4; i++) {
  94. memset(buffer, 0xff, sizeof(buffer));
  95. SkRasterPipeline_<256> p;
  96. p.append(SkRasterPipeline::load_f32, &src);
  97. p.append(SkRasterPipeline::store_f32, &dst);
  98. p.run(0,0, i,1);
  99. for (unsigned j = 0; j < i; j++) {
  100. for (unsigned k = 0; k < 4; k++) {
  101. if (buffer[j][k] != data[j][k]) {
  102. ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]);
  103. }
  104. }
  105. }
  106. for (int j = i; j < 4; j++) {
  107. for (auto f : buffer[j]) {
  108. REPORTER_ASSERT(r, SkScalarIsNaN(f));
  109. }
  110. }
  111. }
  112. }
  113. {
  114. float data[][2] = {
  115. {00, 01},
  116. {10, 11},
  117. {20, 21},
  118. {30, 31},
  119. };
  120. float buffer[4][4];
  121. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  122. dst = { &buffer[0][0], 0 };
  123. for (unsigned i = 1; i <= 4; i++) {
  124. memset(buffer, 0xff, sizeof(buffer));
  125. SkRasterPipeline_<256> p;
  126. p.append(SkRasterPipeline::load_rgf32, &src);
  127. p.append(SkRasterPipeline::store_f32, &dst);
  128. p.run(0,0, i,1);
  129. for (unsigned j = 0; j < i; j++) {
  130. for (unsigned k = 0; k < 2; k++) {
  131. if (buffer[j][k] != data[j][k]) {
  132. ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]);
  133. }
  134. }
  135. if (buffer[j][2] != 0) {
  136. ERRORF(r, "(%u, 2) - a: 0 r: %g\n", j, buffer[j][2]);
  137. }
  138. if (buffer[j][3] != 1) {
  139. ERRORF(r, "(%u, 3) - a: 1 r: %g\n", j, buffer[j][3]);
  140. }
  141. }
  142. for (int j = i; j < 4; j++) {
  143. for (auto f : buffer[j]) {
  144. REPORTER_ASSERT(r, SkScalarIsNaN(f));
  145. }
  146. }
  147. }
  148. }
  149. {
  150. float data[][4] = {
  151. {00, 01, 02, 03},
  152. {10, 11, 12, 13},
  153. {20, 21, 22, 23},
  154. {30, 31, 32, 33},
  155. };
  156. float buffer[4][2];
  157. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  158. dst = { &buffer[0][0], 0 };
  159. for (unsigned i = 1; i <= 4; i++) {
  160. memset(buffer, 0xff, sizeof(buffer));
  161. SkRasterPipeline_<256> p;
  162. p.append(SkRasterPipeline::load_f32, &src);
  163. p.append(SkRasterPipeline::store_rgf32, &dst);
  164. p.run(0,0, i,1);
  165. for (unsigned j = 0; j < i; j++) {
  166. for (unsigned k = 0; k < 2; k++) {
  167. if (buffer[j][k] != data[j][k]) {
  168. ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]);
  169. }
  170. }
  171. }
  172. for (int j = i; j < 4; j++) {
  173. for (auto f : buffer[j]) {
  174. REPORTER_ASSERT(r, SkScalarIsNaN(f));
  175. }
  176. }
  177. }
  178. }
  179. {
  180. alignas(8) uint16_t data[][4] = {
  181. {h(00), h(01), h(02), h(03)},
  182. {h(10), h(11), h(12), h(13)},
  183. {h(20), h(21), h(22), h(23)},
  184. {h(30), h(31), h(32), h(33)},
  185. };
  186. alignas(8) uint16_t buffer[4][4];
  187. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  188. dst = { &buffer[0][0], 0 };
  189. for (unsigned i = 1; i <= 4; i++) {
  190. memset(buffer, 0xff, sizeof(buffer));
  191. SkRasterPipeline_<256> p;
  192. p.append(SkRasterPipeline::load_f16, &src);
  193. p.append(SkRasterPipeline::store_f16, &dst);
  194. p.run(0,0, i,1);
  195. for (unsigned j = 0; j < i; j++) {
  196. REPORTER_ASSERT(r,
  197. !memcmp(&data[j][0], &buffer[j][0], sizeof(buffer[j])));
  198. }
  199. for (int j = i; j < 4; j++) {
  200. for (auto f : buffer[j]) {
  201. REPORTER_ASSERT(r, f == 0xffff);
  202. }
  203. }
  204. }
  205. }
  206. {
  207. alignas(8) uint16_t data[]= {
  208. h(00),
  209. h(10),
  210. h(20),
  211. h(30),
  212. };
  213. alignas(8) uint16_t buffer[4][4];
  214. SkRasterPipeline_MemoryCtx src = { &data[0], 0 },
  215. dst = { &buffer[0][0], 0 };
  216. for (unsigned i = 1; i <= 4; i++) {
  217. memset(buffer, 0xff, sizeof(buffer));
  218. SkRasterPipeline_<256> p;
  219. p.append(SkRasterPipeline::load_af16, &src);
  220. p.append(SkRasterPipeline::store_f16, &dst);
  221. p.run(0,0, i,1);
  222. for (unsigned j = 0; j < i; j++) {
  223. uint16_t expected[] = {0, 0, 0, data[j]};
  224. REPORTER_ASSERT(r, !memcmp(expected, &buffer[j][0], sizeof(buffer[j])));
  225. }
  226. for (int j = i; j < 4; j++) {
  227. for (auto f : buffer[j]) {
  228. REPORTER_ASSERT(r, f == 0xffff);
  229. }
  230. }
  231. }
  232. }
  233. {
  234. alignas(8) uint16_t data[][4] = {
  235. {h(00), h(01), h(02), h(03)},
  236. {h(10), h(11), h(12), h(13)},
  237. {h(20), h(21), h(22), h(23)},
  238. {h(30), h(31), h(32), h(33)},
  239. };
  240. alignas(8) uint16_t buffer[4];
  241. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  242. dst = { &buffer[0], 0 };
  243. for (unsigned i = 1; i <= 4; i++) {
  244. memset(buffer, 0xff, sizeof(buffer));
  245. SkRasterPipeline_<256> p;
  246. p.append(SkRasterPipeline::load_f16, &src);
  247. p.append(SkRasterPipeline::store_af16, &dst);
  248. p.run(0,0, i,1);
  249. for (unsigned j = 0; j < i; j++) {
  250. REPORTER_ASSERT(r, !memcmp(&data[j][3], &buffer[j], sizeof(buffer[j])));
  251. }
  252. for (int j = i; j < 4; j++) {
  253. REPORTER_ASSERT(r, buffer[j] == 0xffff);
  254. }
  255. }
  256. }
  257. {
  258. alignas(8) uint16_t data[][4] = {
  259. {h(00), h(01), h(02), h(03)},
  260. {h(10), h(11), h(12), h(13)},
  261. {h(20), h(21), h(22), h(23)},
  262. {h(30), h(31), h(32), h(33)},
  263. };
  264. alignas(8) uint16_t buffer[4][2];
  265. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  266. dst = { &buffer[0][0], 0 };
  267. for (unsigned i = 1; i <= 4; i++) {
  268. memset(buffer, 0xff, sizeof(buffer));
  269. SkRasterPipeline_<256> p;
  270. p.append(SkRasterPipeline::load_f16, &src);
  271. p.append(SkRasterPipeline::store_rgf16, &dst);
  272. p.run(0,0, i,1);
  273. for (unsigned j = 0; j < i; j++) {
  274. REPORTER_ASSERT(r, !memcmp(&buffer[j], &data[j], 2 * sizeof(uint16_t)));
  275. }
  276. for (int j = i; j < 4; j++) {
  277. for (auto h : buffer[j]) {
  278. REPORTER_ASSERT(r, h == 0xffff);
  279. }
  280. }
  281. }
  282. }
  283. {
  284. alignas(8) uint16_t data[][2] = {
  285. {h(00), h(01)},
  286. {h(10), h(11)},
  287. {h(20), h(21)},
  288. {h(30), h(31)},
  289. };
  290. alignas(8) uint16_t buffer[4][4];
  291. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  292. dst = { &buffer[0][0], 0 };
  293. for (unsigned i = 1; i <= 4; i++) {
  294. memset(buffer, 0xff, sizeof(buffer));
  295. SkRasterPipeline_<256> p;
  296. p.append(SkRasterPipeline::load_rgf16, &src);
  297. p.append(SkRasterPipeline::store_f16, &dst);
  298. p.run(0,0, i,1);
  299. for (unsigned j = 0; j < i; j++) {
  300. uint16_t expected[] = {data[j][0], data[j][1], h(0), h(1)};
  301. REPORTER_ASSERT(r, !memcmp(&buffer[j], expected, sizeof(expected)));
  302. }
  303. for (int j = i; j < 4; j++) {
  304. for (auto h : buffer[j]) {
  305. REPORTER_ASSERT(r, h == 0xffff);
  306. }
  307. }
  308. }
  309. }
  310. }
  311. DEF_TEST(SkRasterPipeline_u16, r) {
  312. {
  313. alignas(8) uint16_t data[][2] = {
  314. {0x0000, 0x0111},
  315. {0x1010, 0x1111},
  316. {0x2020, 0x2121},
  317. {0x3030, 0x3131},
  318. };
  319. uint8_t buffer[4][4];
  320. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  321. dst = { &buffer[0][0], 0 };
  322. for (unsigned i = 1; i <= 4; i++) {
  323. memset(buffer, 0xab, sizeof(buffer));
  324. SkRasterPipeline_<256> p;
  325. p.append(SkRasterPipeline::load_rg1616, &src);
  326. p.append(SkRasterPipeline::store_8888, &dst);
  327. p.run(0,0, i,1);
  328. for (unsigned j = 0; j < i; j++) {
  329. uint8_t expected[] = {
  330. SkToU8(data[j][0] >> 8),
  331. SkToU8(data[j][1] >> 8),
  332. 000,
  333. 0xff
  334. };
  335. REPORTER_ASSERT(r, !memcmp(&buffer[j], expected, sizeof(expected)));
  336. }
  337. for (int j = i; j < 4; j++) {
  338. for (auto b : buffer[j]) {
  339. REPORTER_ASSERT(r, b == 0xab);
  340. }
  341. }
  342. }
  343. }
  344. alignas(8) uint16_t data[] = {
  345. 0x0000,
  346. 0x1010,
  347. 0x2020,
  348. 0x3030,
  349. };
  350. uint8_t buffer[4][4];
  351. SkRasterPipeline_MemoryCtx src = { &data[0], 0 },
  352. dst = { &buffer[0][0], 0 };
  353. for (unsigned i = 1; i <= 4; i++) {
  354. memset(buffer, 0xff, sizeof(buffer));
  355. SkRasterPipeline_<256> p;
  356. p.append(SkRasterPipeline::load_a16, &src);
  357. p.append(SkRasterPipeline::store_8888, &dst);
  358. p.run(0,0, i,1);
  359. for (unsigned j = 0; j < i; j++) {
  360. uint8_t expected[] = {0x00, 0x00, 0x00, SkToU8(data[j] >> 8)};
  361. REPORTER_ASSERT(r, !memcmp(&buffer[j], expected, sizeof(expected)));
  362. }
  363. for (int j = i; j < 4; j++) {
  364. for (auto b : buffer[j]) {
  365. REPORTER_ASSERT(r, b == 0xff);
  366. }
  367. }
  368. }
  369. {
  370. uint8_t data[][4] = {
  371. {0x00, 0x01, 0x02, 0x03},
  372. {0x10, 0x11, 0x12, 0x13},
  373. {0x20, 0x21, 0x22, 0x23},
  374. {0x30, 0x31, 0x32, 0x33},
  375. };
  376. alignas(8) uint16_t buffer[4];
  377. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  378. dst = { &buffer[0], 0 };
  379. for (unsigned i = 1; i <= 4; i++) {
  380. memset(buffer, 0xff, sizeof(buffer));
  381. SkRasterPipeline_<256> p;
  382. p.append(SkRasterPipeline::load_8888, &src);
  383. p.append(SkRasterPipeline::store_a16, &dst);
  384. p.run(0,0, i,1);
  385. for (unsigned j = 0; j < i; j++) {
  386. uint16_t expected = (data[j][3] << 8) | data[j][3];
  387. REPORTER_ASSERT(r, buffer[j] == expected);
  388. }
  389. for (int j = i; j < 4; j++) {
  390. REPORTER_ASSERT(r, buffer[j] == 0xffff);
  391. }
  392. }
  393. }
  394. {
  395. alignas(8) uint16_t data[][4] = {
  396. {0x0000, 0x1000, 0x2000, 0x3000},
  397. {0x0001, 0x1001, 0x2001, 0x3001},
  398. {0x0002, 0x1002, 0x2002, 0x3002},
  399. {0x0003, 0x1003, 0x2003, 0x3003},
  400. };
  401. alignas(8) uint16_t buffer[4][4];
  402. SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
  403. dst = { &buffer[0], 0 };
  404. for (unsigned i = 1; i <= 4; i++) {
  405. memset(buffer, 0xff, sizeof(buffer));
  406. SkRasterPipeline_<256> p;
  407. p.append(SkRasterPipeline::load_16161616, &src);
  408. p.append(SkRasterPipeline::swap_rb);
  409. p.append(SkRasterPipeline::store_16161616, &dst);
  410. p.run(0,0, i,1);
  411. for (unsigned j = 0; j < i; j++) {
  412. uint16_t expected[4] = {data[j][2], data[j][1], data[j][0], data[j][3]};
  413. REPORTER_ASSERT(r, !memcmp(&expected[0], &buffer[j], sizeof(expected)));
  414. }
  415. for (int j = i; j < 4; j++) {
  416. for (uint16_t u16 : buffer[j])
  417. REPORTER_ASSERT(r, u16 == 0xffff);
  418. }
  419. }
  420. }
  421. }
  422. DEF_TEST(SkRasterPipeline_lowp, r) {
  423. uint32_t rgba[64];
  424. for (int i = 0; i < 64; i++) {
  425. rgba[i] = (4*i+0) << 0
  426. | (4*i+1) << 8
  427. | (4*i+2) << 16
  428. | (4*i+3) << 24;
  429. }
  430. SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
  431. SkRasterPipeline_<256> p;
  432. p.append(SkRasterPipeline::load_8888, &ptr);
  433. p.append(SkRasterPipeline::swap_rb);
  434. p.append(SkRasterPipeline::store_8888, &ptr);
  435. p.run(0,0,64,1);
  436. for (int i = 0; i < 64; i++) {
  437. uint32_t want = (4*i+0) << 16
  438. | (4*i+1) << 8
  439. | (4*i+2) << 0
  440. | (4*i+3) << 24;
  441. if (rgba[i] != want) {
  442. ERRORF(r, "got %08x, want %08x\n", rgba[i], want);
  443. }
  444. }
  445. }
  446. DEF_TEST(SkRasterPipeline_swizzle, r) {
  447. // This takes the lowp code path
  448. {
  449. uint16_t rg[64];
  450. for (int i = 0; i < 64; i++) {
  451. rg[i] = (4*i+0) << 0
  452. | (4*i+1) << 8;
  453. }
  454. GrSwizzle swizzle("g1b1");
  455. SkRasterPipeline_MemoryCtx ptr = { rg, 0 };
  456. SkRasterPipeline_<256> p;
  457. p.append(SkRasterPipeline::load_rg88, &ptr);
  458. swizzle.apply(&p);
  459. p.append(SkRasterPipeline::store_rg88, &ptr);
  460. p.run(0,0,64,1);
  461. for (int i = 0; i < 64; i++) {
  462. uint32_t want = 0xff << 8
  463. | (4*i+1) << 0;
  464. if (rg[i] != want) {
  465. ERRORF(r, "got %08x, want %08x\n", rg[i], want);
  466. }
  467. }
  468. }
  469. // This takes the highp code path
  470. {
  471. float rg[64][2];
  472. for (int i = 0; i < 64; i++) {
  473. rg[i][0] = i + 1;
  474. rg[i][1] = 2 * i + 1;
  475. }
  476. GrSwizzle swizzle("0gra");
  477. uint16_t buffer[64][4];
  478. SkRasterPipeline_MemoryCtx src = { rg, 0 },
  479. dst = { buffer, 0};
  480. SkRasterPipeline_<256> p;
  481. p.append(SkRasterPipeline::load_rgf32, &src);
  482. swizzle.apply(&p);
  483. p.append(SkRasterPipeline::store_f16, &dst);
  484. p.run(0,0,64,1);
  485. for (int i = 0; i < 64; i++) {
  486. uint16_t want[4] {
  487. h(0),
  488. h(2 * i + 1),
  489. h(i + 1),
  490. h(1),
  491. };
  492. REPORTER_ASSERT(r, !memcmp(want, buffer[i], sizeof(buffer[i])));
  493. }
  494. }
  495. }
  496. DEF_TEST(SkRasterPipeline_lowp_clamp01, r) {
  497. // This may seem like a funny pipeline to create,
  498. // but it certainly shouldn't crash when you run it.
  499. uint32_t rgba = 0xff00ff00;
  500. SkRasterPipeline_MemoryCtx ptr = { &rgba, 0 };
  501. SkRasterPipeline_<256> p;
  502. p.append(SkRasterPipeline::load_8888, &ptr);
  503. p.append(SkRasterPipeline::swap_rb);
  504. p.append(SkRasterPipeline::clamp_0);
  505. p.append(SkRasterPipeline::clamp_1);
  506. p.append(SkRasterPipeline::store_8888, &ptr);
  507. p.run(0,0,1,1);
  508. }