g2d_demo.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <csi_g2d.h>
  5. #include <csi_g2d_types.h>
  6. typedef struct _bitmap_file {
  7. const char *name;
  8. unsigned int width;
  9. unsigned int height;
  10. unsigned int format;
  11. } bitmap_file;
  12. const static bitmap_file sbitmaps[] = {
  13. {
  14. .name = "resource/image1_640x480_nv12.yuv",
  15. .width = 640,
  16. .height = 480,
  17. .format = CSI_G2D_FMT_NV12,
  18. },
  19. {
  20. .name = "resource/image2_640x480_argb.rgb",
  21. .width = 640,
  22. .height = 480,
  23. .format = CSI_G2D_FMT_BGRA8888,
  24. },
  25. {
  26. .name = "resource/image3_640x480_nv12.yuv",
  27. .width = 640,
  28. .height = 480,
  29. .format = CSI_G2D_FMT_NV12,
  30. },
  31. {
  32. .name = "resource/horse_640x480_nv12.yuv",
  33. .width = 640,
  34. .height = 480,
  35. .format = CSI_G2D_FMT_NV12,
  36. },
  37. };
  38. static int g2d_load_bitmap(csi_g2d_surface *surface, const char *path)
  39. {
  40. int i, j, bytes, lsize;
  41. int width[3], height[3];
  42. FILE *file;
  43. void *ptr;
  44. if (!surface || !surface->nplanes || surface->nplanes > 3)
  45. return -1;
  46. file = fopen(path, "r");
  47. if (!file)
  48. return -1;
  49. width[0] = surface->width;
  50. height[0] = surface->height;
  51. switch (surface->format) {
  52. case CSI_G2D_FMT_NV12:
  53. case CSI_G2D_FMT_NV21:
  54. width[1] = surface->width / 2;
  55. height[1] = surface->height / 2;
  56. break;
  57. case CSI_G2D_FMT_ARGB8888:
  58. case CSI_G2D_FMT_BGRA8888:
  59. break;
  60. /* TODO: add more formats support here */
  61. default:
  62. return -1;
  63. }
  64. for (i = 0; i < surface->nplanes; i++) {
  65. for (j = 0; j < height[i]; j++) {
  66. ptr = surface->lgcaddr[i] + j * surface->stride[i];
  67. lsize = width[i] * surface->cpp[i];
  68. bytes = fread(ptr, 1, lsize, file);
  69. if (bytes != lsize)
  70. return -1;
  71. }
  72. }
  73. return 0;
  74. }
  75. static int g2d_save_bitmap(csi_g2d_surface *surface, const char *path)
  76. {
  77. int i, j, bytes, lsize;
  78. int width[3], height[3];
  79. FILE *file;
  80. void *ptr;
  81. if (!surface || !surface->nplanes || surface->nplanes > 3)
  82. return -1;
  83. file = fopen(path, "w");
  84. if (!file)
  85. return -1;
  86. width[0] = surface->width;
  87. height[0] = surface->height;
  88. switch (surface->format) {
  89. case CSI_G2D_FMT_NV12:
  90. case CSI_G2D_FMT_NV21:
  91. width[1] = surface->width / 2;
  92. height[1] = surface->height / 2;
  93. break;
  94. /* TODO: add more formats support here */
  95. case CSI_G2D_FMT_ARGB8888:
  96. break;
  97. default:
  98. return -1;
  99. }
  100. for (i = 0; i < surface->nplanes; i++) {
  101. for (j = 0; j < height[i]; j++) {
  102. ptr = surface->lgcaddr[i] + j * surface->stride[i];
  103. lsize = width[i] * surface->cpp[i];
  104. bytes = fwrite(ptr, 1, lsize, file);
  105. if (bytes != lsize)
  106. return -1;
  107. }
  108. }
  109. return 0;
  110. }
  111. static int g2d_test_filterblit_with_rotation(void)
  112. {
  113. int ret;
  114. const bitmap_file *bitmap = &sbitmaps[0];
  115. csi_g2d_surface *source, *target;
  116. csi_g2d_region dstRect;
  117. printf("start to do filterblit operation with 90 rotation: ");
  118. source = calloc(1, sizeof(*source));
  119. if (!source) {
  120. printf("allocate source surface failed\n");
  121. exit(-1);
  122. }
  123. target = calloc(1, sizeof(*target));
  124. if (!target) {
  125. printf("allocate target surface failed\n");
  126. exit(-1);
  127. }
  128. source->width = bitmap->width;
  129. source->height = bitmap->height;
  130. source->format = bitmap->format;
  131. ret = csi_g2d_surface_create(source);
  132. if (ret) {
  133. printf("create source surface failed\n");
  134. exit(-1);
  135. }
  136. target->width = bitmap->width * 2;
  137. target->height = bitmap->height * 2;
  138. target->format = CSI_G2D_FMT_ARGB8888;
  139. ret = csi_g2d_surface_create(target);
  140. if (ret) {
  141. printf("create target surface failed\n");
  142. exit(-1);
  143. }
  144. printf("%ux%u NV12 -> %ux%u ARGB8888\n", source->width, source->height, target->width, target->height);
  145. ret = g2d_load_bitmap(source, bitmap->name);
  146. if (ret) {
  147. printf("load bitmap to source failed\n");
  148. exit(-1);
  149. }
  150. ret = csi_g2d_surface_set_source(source);
  151. if (ret) {
  152. printf("set source surface failed\n");
  153. exit(-1);
  154. }
  155. ret = csi_g2d_surface_set_target(target);
  156. if (ret) {
  157. printf("set target surface failed\n");
  158. exit(-1);
  159. }
  160. ret = csi_g2d_blit_set_rotation(CSI_G2D_ROTATION_0_DEGREE);
  161. if (ret) {
  162. printf("set rotation to 0 failed\n");
  163. exit(-1);
  164. }
  165. dstRect.left = dstRect.top = 0;
  166. dstRect.right = target->width;
  167. dstRect.bottom = target->height;
  168. ret = csi_g2d_blit_set_filter_tap(CSI_G2D_FILTER_TAP_5,
  169. CSI_G2D_FILTER_TAP_5);
  170. if (ret) {
  171. printf("set filter tap failed\n");
  172. exit(-1);
  173. }
  174. ret = csi_g2d_blit_filterblit(&dstRect, 1);
  175. if (ret) {
  176. printf("filterblit nv12 to argb8888 failed\n");
  177. exit(-1);
  178. }
  179. ret = csi_g2d_flush();
  180. if (ret) {
  181. printf("flush g2d failed\n");
  182. exit(-1);
  183. }
  184. ret = g2d_save_bitmap(target, "result/image1_result_1280x960_argb8888.rgb");
  185. if (ret) {
  186. printf("save result failed\n");
  187. exit(-1);
  188. }
  189. ret = csi_g2d_surface_destroy(source);
  190. if (ret) {
  191. printf("destroy source surface failed\n");
  192. exit(-1);
  193. }
  194. ret = csi_g2d_surface_destroy(target);
  195. if (ret) {
  196. printf("destroy target surface failed\n");
  197. exit(-1);
  198. }
  199. printf("filterblit operation finished\n");
  200. return 0;
  201. }
  202. static int g2d_test_stretchblit_with_rotation(void)
  203. {
  204. int ret;
  205. const bitmap_file *bitmap = &sbitmaps[0];
  206. csi_g2d_surface *source, *target;
  207. csi_g2d_region dstRect;
  208. printf("start to do stretchblit operation with 90 rotation: ");
  209. source = calloc(1, sizeof(*source));
  210. if (!source) {
  211. printf("allocate source surface failed\n");
  212. exit(-1);
  213. }
  214. target = calloc(1, sizeof(*target));
  215. if (!target) {
  216. printf("allocate target surface failed\n");
  217. exit(-1);
  218. }
  219. source->width = bitmap->width;
  220. source->height = bitmap->height;
  221. source->format = bitmap->format;
  222. ret = csi_g2d_surface_create(source);
  223. if (ret) {
  224. printf("create source surface failed\n");
  225. exit(-1);
  226. }
  227. target->width = bitmap->height * 2;
  228. target->height = bitmap->width * 2;
  229. target->format = CSI_G2D_FMT_ARGB8888;
  230. ret = csi_g2d_surface_create(target);
  231. if (ret) {
  232. printf("create target surface failed\n");
  233. exit(-1);
  234. }
  235. printf("%ux%u NV12 -> %ux%u ARGB8888\n", source->width, source->height, target->width, target->height);
  236. ret = g2d_load_bitmap(source, bitmap->name);
  237. if (ret) {
  238. printf("load bitmap to source failed\n");
  239. exit(-1);
  240. }
  241. ret = csi_g2d_surface_set_source(source);
  242. if (ret) {
  243. printf("set source surface failed\n");
  244. exit(-1);
  245. }
  246. ret = csi_g2d_surface_set_target(target);
  247. if (ret) {
  248. printf("set target surface failed\n");
  249. exit(-1);
  250. }
  251. dstRect.left = dstRect.top = 0;
  252. dstRect.right = target->width;
  253. dstRect.bottom = target->height;
  254. ret = csi_g2d_blit_set_rotation(CSI_G2D_ROTATION_90_DEGREE);
  255. if (ret) {
  256. printf("set rotation to 90 failed\n");
  257. exit(-1);
  258. }
  259. ret = csi_g2d_blit_stretchblit(&dstRect, 1);
  260. if (ret) {
  261. printf("stretchblit nv12 to argb8888 failed\n");
  262. exit(-1);
  263. }
  264. ret = csi_g2d_flush();
  265. if (ret) {
  266. printf("flush g2d failed\n");
  267. exit(-1);
  268. }
  269. ret = g2d_save_bitmap(target, "result/image1_result_960x1280_argb8888.rgb");
  270. if (ret) {
  271. printf("save result failed\n");
  272. exit(-1);
  273. }
  274. ret = csi_g2d_surface_destroy(source);
  275. if (ret) {
  276. printf("destroy source surface failed\n");
  277. exit(-1);
  278. }
  279. ret = csi_g2d_surface_destroy(target);
  280. if (ret) {
  281. printf("destroy target surface failed\n");
  282. exit(-1);
  283. }
  284. printf("stretchblit operation with 90 rotation finished\n");
  285. return 0;
  286. }
  287. static int g2d_test_bitblit_with_alpha_blending(void)
  288. {
  289. int ret;
  290. const bitmap_file *bitmap = &sbitmaps[0];
  291. csi_g2d_surface *source, *target;
  292. csi_g2d_region srcRect, dstRect;
  293. csi_g2d_blend_mode blend_mode;
  294. printf("start to do bitblit operation with alpha blending\n");
  295. source = calloc(1, sizeof(*source));
  296. if (!source) {
  297. printf("allocate source surface failed\n");
  298. exit(-1);
  299. }
  300. target = calloc(1, sizeof(*target));
  301. if (!target) {
  302. printf("allocate target surface failed\n");
  303. exit(-1);
  304. }
  305. source->width = bitmap->width;
  306. source->height = bitmap->height;
  307. source->format = CSI_G2D_FMT_ARGB8888;
  308. ret = csi_g2d_surface_create(source);
  309. if (ret) {
  310. printf("create source surface failed\n");
  311. exit(-1);
  312. }
  313. ret = csi_g2d_surface_set_target(source);
  314. if (ret) {
  315. printf("set source surface failed\n");
  316. exit(-1);
  317. }
  318. srcRect.left = srcRect.top = 0;
  319. srcRect.right = source->width;
  320. srcRect.bottom = source->height;
  321. ret = csi_g2d_fill(&srcRect, 1, 0x0);
  322. if (ret) {
  323. printf("fill source surface failed\n");
  324. exit(-1);
  325. }
  326. srcRect.left = 50;
  327. srcRect.top = 60;
  328. srcRect.right = 250;
  329. srcRect.bottom = 260;
  330. ret = csi_g2d_fill(&srcRect, 1, 0xFF0000FF);
  331. if (ret) {
  332. printf("fill source surface failed\n");
  333. exit(-1);
  334. }
  335. srcRect.left = srcRect.top = 0;
  336. srcRect.right = source->width;
  337. srcRect.bottom = source->height;
  338. target->width = bitmap->width;
  339. target->height = bitmap->height;
  340. target->format = CSI_G2D_FMT_ARGB8888;
  341. ret = csi_g2d_surface_create(target);
  342. if (ret) {
  343. printf("create target surface failed\n");
  344. exit(-1);
  345. }
  346. ret = csi_g2d_surface_set_target(target);
  347. if (ret) {
  348. printf("set target surface failed\n");
  349. exit(-1);
  350. }
  351. dstRect.left = dstRect.top = 0;
  352. dstRect.right = target->width;
  353. dstRect.bottom = target->height;
  354. ret = csi_g2d_fill(&dstRect, 1, 0x0);
  355. if (ret) {
  356. printf("fill source surface failed\n");
  357. exit(-1);
  358. }
  359. dstRect.left = 20;
  360. dstRect.top = 30;
  361. dstRect.right = 120;
  362. dstRect.bottom = 130;
  363. ret = csi_g2d_fill(&dstRect, 1, 0xFFFF0000);
  364. if (ret) {
  365. printf("fill source surface failed\n");
  366. exit(-1);
  367. }
  368. ret = csi_g2d_surface_set_source(source);
  369. if (ret) {
  370. printf("set source surface failed\n");
  371. exit(-1);
  372. }
  373. /* set alpha modes */
  374. blend_mode = CSI_G2D_BLEND_MODE_XOR;
  375. ret = csi_g2d_surface_set_source_alpha_mode(
  376. CSI_G2D_ALPHA_MODE_STRAIGHT,
  377. blend_mode
  378. );
  379. if (ret) {
  380. printf("set source alpha mode failed\n");
  381. exit(-1);
  382. }
  383. ret = csi_g2d_surface_set_source_global_alpha_mode(
  384. CSI_G2D_GLOBAL_ALPHA_MODE_OFF,
  385. 0xFFFFFFFF
  386. );
  387. if (ret) {
  388. printf("set source global alpha mode failed\n");
  389. exit(-1);
  390. }
  391. ret = csi_g2d_surface_set_target_alpha_mode(
  392. CSI_G2D_ALPHA_MODE_STRAIGHT,
  393. blend_mode
  394. );
  395. if (ret) {
  396. printf("set target alpha mode failed\n");
  397. exit(-1);
  398. }
  399. ret = csi_g2d_surface_set_target_global_alpha_mode(
  400. CSI_G2D_GLOBAL_ALPHA_MODE_OFF,
  401. 0xFFFFFFFF
  402. );
  403. if (ret) {
  404. printf("set target global alpha mode failed\n");
  405. exit(-1);
  406. }
  407. ret = csi_g2d_surface_enable_disable_alpha_blend(true);
  408. if (ret) {
  409. printf("enable alpha blend failed\n");
  410. exit(-1);
  411. }
  412. dstRect.left = dstRect.top = 0;
  413. dstRect.right = target->width;
  414. dstRect.bottom = target->height;
  415. ret = csi_g2d_blit_bitblit(&dstRect, 1);
  416. if (ret) {
  417. printf("bitblit argb8888 to argb8888 with alpha blend failed\n");
  418. exit(-1);
  419. }
  420. ret = csi_g2d_flush();
  421. if (ret) {
  422. printf("flush g2d failed\n");
  423. exit(-1);
  424. }
  425. ret = g2d_save_bitmap(target, "result/alpha_blend_result_640x480_argb8888.rgb");
  426. if (ret) {
  427. printf("save result failed\n");
  428. exit(-1);
  429. }
  430. ret = csi_g2d_surface_destroy(source);
  431. if (ret) {
  432. printf("destroy source surface failed\n");
  433. exit(-1);
  434. }
  435. ret = csi_g2d_surface_destroy(target);
  436. if (ret) {
  437. printf("destroy target surface failed\n");
  438. exit(-1);
  439. }
  440. printf("bitblit operation with alpha blending finished\n");
  441. return 0;
  442. }
  443. static int g2d_test_bitblit_with_rotation(void)
  444. {
  445. int ret;
  446. const bitmap_file *bitmap = &sbitmaps[0];
  447. csi_g2d_surface *source, *target;
  448. csi_g2d_region dstRect;
  449. printf("start to do bitblit operation with 90 rotation: ");
  450. source = calloc(1, sizeof(*source));
  451. if (!source) {
  452. printf("allocate source surface failed\n");
  453. exit(-1);
  454. }
  455. target = calloc(1, sizeof(*target));
  456. if (!target) {
  457. printf("allocate target surface failed\n");
  458. exit(-1);
  459. }
  460. source->width = bitmap->width;
  461. source->height = bitmap->height;
  462. source->format = bitmap->format;
  463. ret = csi_g2d_surface_create(source);
  464. if (ret) {
  465. printf("create source surface failed\n");
  466. exit(-1);
  467. }
  468. target->width = bitmap->height;
  469. target->height = bitmap->width;
  470. target->format = CSI_G2D_FMT_ARGB8888;
  471. ret = csi_g2d_surface_create(target);
  472. if (ret) {
  473. printf("create target surface failed\n");
  474. exit(-1);
  475. }
  476. printf("%ux%u NV12 -> %ux%u ARGB8888\n", source->width, source->height, target->width, target->height);
  477. ret = g2d_load_bitmap(source, bitmap->name);
  478. if (ret) {
  479. printf("load bitmap to source failed\n");
  480. exit(-1);
  481. }
  482. ret = csi_g2d_surface_set_source(source);
  483. if (ret) {
  484. printf("set source surface failed\n");
  485. exit(-1);
  486. }
  487. ret = csi_g2d_surface_set_target(target);
  488. if (ret) {
  489. printf("set target surface failed\n");
  490. exit(-1);
  491. }
  492. dstRect.left = dstRect.top = 0;
  493. dstRect.right = target->width;
  494. dstRect.bottom = target->height;
  495. ret = csi_g2d_blit_set_rotation(CSI_G2D_ROTATION_90_DEGREE);
  496. if (ret) {
  497. printf("set rotation to 90 failed\n");
  498. exit(-1);
  499. }
  500. ret = csi_g2d_blit_bitblit(&dstRect, 1);
  501. if (ret) {
  502. printf("bitblit nv12 to nv12 failed\n");
  503. exit(-1);
  504. }
  505. ret = csi_g2d_flush();
  506. if (ret) {
  507. printf("flush g2d failed\n");
  508. exit(-1);
  509. }
  510. ret = g2d_save_bitmap(target, "result/image1_result_480x640_argb8888.rgb");
  511. if (ret) {
  512. printf("save result failed\n");
  513. exit(-1);
  514. }
  515. ret = csi_g2d_surface_destroy(source);
  516. if (ret) {
  517. printf("destroy source surface failed\n");
  518. exit(-1);
  519. }
  520. ret = csi_g2d_surface_destroy(target);
  521. if (ret) {
  522. printf("destroy target surface failed\n");
  523. exit(-1);
  524. }
  525. printf("bitblit operation with 90 rotation finished\n");
  526. return 0;
  527. }
  528. static int g2d_test_fill_rectangle(void)
  529. {
  530. int ret;
  531. csi_g2d_surface *target;
  532. csi_g2d_region dstRect;
  533. printf("start to do rectangle fill operation: ");
  534. target = calloc(1, sizeof(*target));
  535. if (!target) {
  536. printf("allocate target surface failed\n");
  537. exit(-1);
  538. }
  539. target->width = 640;
  540. target->height = 480;
  541. target->format = CSI_G2D_FMT_ARGB8888;
  542. ret = csi_g2d_surface_create(target);
  543. if (ret) {
  544. printf("create target surface failed\n");
  545. exit(-1);
  546. }
  547. ret = csi_g2d_surface_set_target(target);
  548. if (ret) {
  549. printf("set target surface failed\n");
  550. exit(-1);
  551. }
  552. printf("fill red to %ux%u rectangle\n", target->width, target->height);
  553. dstRect.left = dstRect.top = 0;
  554. dstRect.right = target->width;
  555. dstRect.bottom = target->height;
  556. ret = csi_g2d_fill(&dstRect, 1, 0xffff0000);
  557. if (ret) {
  558. printf("g2d fill operation failed\n");
  559. exit(-1);
  560. }
  561. ret = csi_g2d_flush();
  562. if (ret) {
  563. printf("flush g2d failed\n");
  564. exit(-1);
  565. }
  566. ret = g2d_save_bitmap(target, "result/image1_result_640x480_argb8888_fill.rgb");
  567. if (ret) {
  568. printf("save result failed\n");
  569. exit(-1);
  570. }
  571. ret = csi_g2d_surface_destroy(target);
  572. if (ret) {
  573. printf("destroy target surface failed\n");
  574. exit(-1);
  575. }
  576. printf("fill rectangle operation finished\n");
  577. return 0;
  578. }
  579. static int g2d_test_draw_rectangle(void)
  580. {
  581. int ret;
  582. const bitmap_file *bitmap = &sbitmaps[1];
  583. csi_g2d_surface *target;
  584. csi_g2d_region dstRect;
  585. csi_g2d_rectangle rectangle;
  586. printf("start to do rectangle draw operation\n");
  587. target = calloc(1, sizeof(*target));
  588. if (!target) {
  589. printf("allocate target surface failed\n");
  590. exit(-1);
  591. }
  592. target->width = bitmap->width;
  593. target->height = bitmap->height;
  594. target->format = CSI_G2D_FMT_ARGB8888;
  595. ret = csi_g2d_surface_create(target);
  596. if (ret) {
  597. printf("create target surface failed\n");
  598. exit(-1);
  599. }
  600. ret = g2d_load_bitmap(target, bitmap->name);
  601. if (ret) {
  602. printf("load bitmap to target failed\n");
  603. exit(-1);
  604. }
  605. ret = csi_g2d_surface_set_target(target);
  606. if (ret) {
  607. printf("set target surface failed\n");
  608. exit(-1);
  609. }
  610. dstRect.left = dstRect.top = 0;
  611. dstRect.right = target->width;
  612. dstRect.bottom = target->height;
  613. ret = csi_g2d_brush_create(0xffff0000, false);
  614. if (ret) {
  615. printf("create brush failed\n");
  616. exit(-1);
  617. }
  618. rectangle.line[0].start.x = dstRect.left + 30;
  619. rectangle.line[0].start.y = dstRect.top + 30;
  620. rectangle.line[0].end.x = dstRect.right - 30;
  621. rectangle.line[0].end.y = dstRect.top + 30;
  622. rectangle.line[1].start.x = dstRect.left + 30;
  623. rectangle.line[1].start.y = dstRect.top + 30;
  624. rectangle.line[1].end.x = dstRect.left + 30;
  625. rectangle.line[1].end.y = dstRect.bottom - 30;
  626. rectangle.line[2].start.x = dstRect.left + 30;
  627. rectangle.line[2].start.y = dstRect.bottom - 30;
  628. rectangle.line[2].end.x = dstRect.right - 30;
  629. rectangle.line[2].end.y = dstRect.bottom - 30;
  630. rectangle.line[3].start.x = dstRect.right - 30;
  631. rectangle.line[3].start.y = dstRect.top + 30;
  632. rectangle.line[3].end.x = dstRect.right - 30;
  633. rectangle.line[3].end.y = dstRect.bottom - 30;
  634. ret = csi_g2d_line_draw_rectangle(&rectangle, 1);
  635. if (ret) {
  636. printf("draw one rectangle failed\n");
  637. exit(-1);
  638. }
  639. ret = csi_g2d_flush();
  640. if (ret) {
  641. printf("flush g2d failed\n");
  642. exit(-1);
  643. }
  644. ret = g2d_save_bitmap(target, "result/image1_result_640x480_argb8888_rectangle.rgb");
  645. if (ret) {
  646. printf("save result failed\n");
  647. exit(-1);
  648. }
  649. ret = csi_g2d_surface_destroy(target);
  650. if (ret) {
  651. printf("destroy target surface failed\n");
  652. exit(-1);
  653. }
  654. printf("draw rectangle operation finished\n");
  655. return 0;
  656. }
  657. static int g2d_test_multisrc_blit(void)
  658. {
  659. int i, ret;
  660. csi_g2d_surface *source[4], *target;
  661. csi_g2d_region srcRect, dstRect;
  662. printf("start to do multisrc blit operation\n");
  663. for (i = 0; i < 4; i++) {
  664. source[i] = calloc(1, sizeof(csi_g2d_surface));
  665. if (!source[i]) {
  666. printf("allocate source surface %d failed\n", i);
  667. exit(-1);
  668. }
  669. }
  670. target = calloc(1, sizeof(*target));
  671. if (!target) {
  672. printf("allocate target surface failed\n");
  673. exit(-1);
  674. }
  675. for (i = 0; i < 4; i++) {
  676. source[i]->width = sbitmaps[i].width;
  677. source[i]->height = sbitmaps[i].height;
  678. source[i]->format = sbitmaps[i].format;
  679. ret = csi_g2d_surface_create(source[i]);
  680. if (ret) {
  681. printf("create source surface failed\n");
  682. exit(-1);
  683. }
  684. ret = g2d_load_bitmap(source[i], sbitmaps[i].name);
  685. if (ret) {
  686. printf("load bitmap %d to source failed\n", i);
  687. exit(-1);
  688. }
  689. }
  690. target->width = 640;
  691. target->height = 480;
  692. target->format = CSI_G2D_FMT_ARGB8888;
  693. ret = csi_g2d_surface_create(target);
  694. if (ret) {
  695. printf("create target surface failed\n");
  696. exit(-1);
  697. }
  698. ret = csi_g2d_surface_set_target(target);
  699. if (ret) {
  700. printf("set target surface failed\n");
  701. exit(-1);
  702. }
  703. dstRect.left = dstRect.top = 0;
  704. dstRect.right = target->width;
  705. dstRect.bottom = target->height;
  706. ret = csi_g2d_fill(&dstRect, 1, 0x0);
  707. if (ret) {
  708. printf("fill target surface to 0x0 failed\n");
  709. exit(-1);
  710. }
  711. for (i = 0; i < 4; i++) {
  712. ret = csi_g2d_surface_select_source(i);
  713. if (ret) {
  714. printf("select source %d\n", i);
  715. exit(-1);
  716. }
  717. ret = csi_g2d_surface_set_source(source[i]);
  718. if (ret) {
  719. printf("set source %d surface failed\n", i);
  720. exit(-1);
  721. }
  722. switch (i % 4) {
  723. case 0:
  724. srcRect.left = 320;
  725. srcRect.top = 240;
  726. srcRect.right = srcRect.left + 320;
  727. srcRect.bottom = srcRect.top + 240;
  728. break;
  729. case 1:
  730. srcRect.left = 0;
  731. srcRect.top = 240;
  732. srcRect.right = srcRect.left + 320;
  733. srcRect.bottom = srcRect.top + 240;
  734. break;
  735. case 2:
  736. srcRect.left = 0;
  737. srcRect.top = 0;
  738. srcRect.right = srcRect.left + 320;
  739. srcRect.bottom = srcRect.top + 240;
  740. break;
  741. case 3:
  742. srcRect.left = 320;
  743. srcRect.top = 0;
  744. srcRect.right = srcRect.left + 320;
  745. srcRect.bottom = srcRect.top + 240;
  746. break;
  747. }
  748. ret = csi_g2d_surface_set_source_clipping(&srcRect);
  749. if (ret) {
  750. printf("set source %d clipping failed\n", i);
  751. return ret;
  752. }
  753. }
  754. ret = csi_g2d_blit_multisrc_blit(0xF, &dstRect, 1);
  755. if (ret) {
  756. printf("multisrc blit failed\n");
  757. exit(-1);
  758. }
  759. ret = csi_g2d_flush();
  760. if (ret) {
  761. printf("flush g2d failed\n");
  762. exit(-1);
  763. }
  764. ret = g2d_save_bitmap(target, "result/multisrc_result_640x480_argb.rgb");
  765. if (ret) {
  766. printf("save result failed\n");
  767. exit(-1);
  768. }
  769. for (i = 0; i < 4; i++) {
  770. ret = csi_g2d_surface_destroy(source[i]);
  771. if (ret) {
  772. printf("destroy source surface failed\n");
  773. exit(-1);
  774. }
  775. }
  776. ret = csi_g2d_surface_destroy(target);
  777. if (ret) {
  778. printf("destroy target surface failed\n");
  779. exit(-1);
  780. }
  781. printf("multisrc blit operation with 4 sources finished\n");
  782. return 0;
  783. }
  784. static int g2d_test_bitblit(void)
  785. {
  786. int ret;
  787. const bitmap_file *bitmap = &sbitmaps[0];
  788. csi_g2d_surface *source, *target;
  789. csi_g2d_region dstRect;
  790. printf("start to do bitblit operation: ");
  791. source = calloc(1, sizeof(*source));
  792. if (!source) {
  793. printf("allocate source surface failed\n");
  794. exit(-1);
  795. }
  796. target = calloc(1, sizeof(*target));
  797. if (!target) {
  798. printf("allocate target surface failed\n");
  799. exit(-1);
  800. }
  801. source->width = bitmap->width;
  802. source->height = bitmap->height;
  803. source->format = bitmap->format;
  804. ret = csi_g2d_surface_create(source);
  805. if (ret) {
  806. printf("create source surface failed\n");
  807. exit(-1);
  808. }
  809. target->width = bitmap->width;
  810. target->height = bitmap->height;
  811. target->format = CSI_G2D_FMT_ARGB8888;
  812. ret = csi_g2d_surface_create(target);
  813. if (ret) {
  814. printf("create target surface failed\n");
  815. exit(-1);
  816. }
  817. printf("%ux%u NV12 -> %ux%u ARGB8888\n", source->width, source->height, target->width, target->height);
  818. ret = g2d_load_bitmap(source, bitmap->name);
  819. if (ret) {
  820. printf("load bitmap to source failed\n");
  821. exit(-1);
  822. }
  823. ret = csi_g2d_surface_set_source(source);
  824. if (ret) {
  825. printf("set source surface failed\n");
  826. exit(-1);
  827. }
  828. ret = csi_g2d_surface_set_target(target);
  829. if (ret) {
  830. printf("set target surface failed\n");
  831. exit(-1);
  832. }
  833. dstRect.left = dstRect.top = 0;
  834. dstRect.right = target->width;
  835. dstRect.bottom = target->height;
  836. ret = csi_g2d_blit_bitblit(&dstRect, 1);
  837. if (ret) {
  838. printf("bitblit nv12 to nv12 failed\n");
  839. exit(-1);
  840. }
  841. ret = csi_g2d_flush();
  842. if (ret) {
  843. printf("flush g2d failed\n");
  844. exit(-1);
  845. }
  846. ret = g2d_save_bitmap(target, "result/image1_result_640x480_argb8888.rgb");
  847. if (ret) {
  848. printf("save result failed\n");
  849. exit(-1);
  850. }
  851. ret = csi_g2d_surface_destroy(source);
  852. if (ret) {
  853. printf("destroy source surface failed\n");
  854. exit(-1);
  855. }
  856. ret = csi_g2d_surface_destroy(target);
  857. if (ret) {
  858. printf("destroy target surface failed\n");
  859. exit(-1);
  860. }
  861. printf("bitblit operation with 90 rotation finished\n");
  862. return 0;
  863. }
  864. int main(void)
  865. {
  866. int ret;
  867. ret = csi_g2d_open();
  868. if (ret) {
  869. printf("open csi g2d failed\n");
  870. exit(-1);
  871. }
  872. g2d_test_bitblit();
  873. g2d_test_draw_rectangle();
  874. g2d_test_fill_rectangle();
  875. g2d_test_bitblit_with_rotation();
  876. g2d_test_stretchblit_with_rotation();
  877. g2d_test_filterblit_with_rotation();
  878. g2d_test_bitblit_with_alpha_blending();
  879. g2d_test_multisrc_blit();
  880. ret = csi_g2d_close();
  881. if (ret) {
  882. printf("close g2d failed\n");
  883. exit(-1);
  884. }
  885. exit(0);
  886. }