visual_server_canvas.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. /**************************************************************************/
  2. /* visual_server_canvas.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "visual_server_canvas.h"
  31. #include "visual_server_globals.h"
  32. #include "visual_server_raster.h"
  33. #include "visual_server_viewport.h"
  34. static const int z_range = VS::CANVAS_ITEM_Z_MAX - VS::CANVAS_ITEM_Z_MIN + 1;
  35. void VisualServerCanvas::_render_canvas_item_tree(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights) {
  36. memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  37. memset(z_last_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  38. if (_canvas_cull_mode == CANVAS_CULL_MODE_NODE) {
  39. _prepare_tree_bounds(p_canvas_item);
  40. _render_canvas_item_cull_by_node(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false);
  41. } else {
  42. _render_canvas_item_cull_by_item(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  43. }
  44. VSG::canvas_render->canvas_render_items_begin(p_modulate, p_lights, p_transform);
  45. for (int i = 0; i < z_range; i++) {
  46. if (!z_list[i]) {
  47. continue;
  48. }
  49. VSG::canvas_render->canvas_render_items(z_list[i], VS::CANVAS_ITEM_Z_MIN + i, p_modulate, p_lights, p_transform);
  50. }
  51. VSG::canvas_render->canvas_render_items_end();
  52. }
  53. void _collect_ysort_children(VisualServerCanvas::Item *p_canvas_item, Transform2D p_transform, VisualServerCanvas::Item *p_material_owner, const Color p_modulate, VisualServerCanvas::Item **r_items, int &r_index) {
  54. int child_item_count = p_canvas_item->child_items.size();
  55. VisualServerCanvas::Item **child_items = p_canvas_item->child_items.ptrw();
  56. for (int i = 0; i < child_item_count; i++) {
  57. if (child_items[i]->visible) {
  58. if (r_items) {
  59. r_items[r_index] = child_items[i];
  60. child_items[i]->ysort_modulate = p_modulate;
  61. child_items[i]->ysort_xform = p_transform;
  62. child_items[i]->ysort_pos = p_transform.xform(child_items[i]->xform.elements[2]);
  63. child_items[i]->material_owner = child_items[i]->use_parent_material ? p_material_owner : nullptr;
  64. child_items[i]->ysort_index = r_index;
  65. }
  66. r_index++;
  67. if (child_items[i]->sort_y) {
  68. _collect_ysort_children(child_items[i],
  69. p_transform * child_items[i]->xform,
  70. child_items[i]->use_parent_material ? p_material_owner : child_items[i],
  71. p_modulate * child_items[i]->modulate,
  72. r_items, r_index);
  73. }
  74. }
  75. }
  76. }
  77. void _mark_ysort_dirty(VisualServerCanvas::Item *ysort_owner, RID_Owner<VisualServerCanvas::Item> &canvas_item_owner) {
  78. do {
  79. ysort_owner->ysort_children_count = -1;
  80. ysort_owner = canvas_item_owner.owns(ysort_owner->parent) ? canvas_item_owner.getornull(ysort_owner->parent) : nullptr;
  81. } while (ysort_owner && ysort_owner->sort_y);
  82. }
  83. void VisualServerCanvas::_make_bound_dirty_reparent(Item *p_item) {
  84. MutexLock lock(_bound_mutex);
  85. DEV_ASSERT(p_item);
  86. Item *p_orig_item = p_item;
  87. // propagate up
  88. while (p_item) {
  89. // Don't worry about invisible objects
  90. if (!p_item->visible) {
  91. return;
  92. }
  93. if (!p_item->bound_dirty) {
  94. p_item->bound_dirty = true;
  95. if (canvas_item_owner.owns(p_item->parent)) {
  96. p_item = canvas_item_owner.get(p_item->parent);
  97. } else {
  98. break;
  99. }
  100. } else {
  101. break;
  102. }
  103. }
  104. // propagate down
  105. _make_bound_dirty_down(p_orig_item);
  106. }
  107. void VisualServerCanvas::_make_bound_dirty(Item *p_item, bool p_changing_visibility) {
  108. if (_canvas_cull_mode != CANVAS_CULL_MODE_NODE) {
  109. return;
  110. }
  111. MutexLock lock(_bound_mutex);
  112. DEV_ASSERT(p_item);
  113. if (!p_changing_visibility) {
  114. _check_bound_integrity(p_item);
  115. }
  116. if (!p_changing_visibility) {
  117. // Traverse up the tree, making each item bound dirty until
  118. // we reach an item that is already dirty (as by definition, if this happens,
  119. // the tree should already be dirty up until the root).
  120. while (p_item) {
  121. // Don't worry about invisible objects
  122. if (!p_item->visible) {
  123. return;
  124. }
  125. if (!p_item->bound_dirty) {
  126. p_item->bound_dirty = true;
  127. if (canvas_item_owner.owns(p_item->parent)) {
  128. p_item = canvas_item_owner.get(p_item->parent);
  129. } else {
  130. break;
  131. }
  132. } else {
  133. break;
  134. }
  135. }
  136. } else {
  137. // special case for visibility changes.
  138. // if hiding, we propagate upwards.
  139. while (p_item) {
  140. if (!p_item->bound_dirty) {
  141. p_item->bound_dirty = true;
  142. }
  143. if (canvas_item_owner.owns(p_item->parent)) {
  144. p_item = canvas_item_owner.get(p_item->parent);
  145. } else {
  146. break;
  147. }
  148. }
  149. // if showing we propagate upwards AND downwards
  150. if (p_item->visible) {
  151. _make_bound_dirty_down(p_item);
  152. }
  153. }
  154. }
  155. void VisualServerCanvas::_make_bound_dirty_down(Item *p_item) {
  156. // Bounds below an item that is being made visible may be out of date,
  157. // so we make them all dirty.
  158. if (!p_item->visible) {
  159. return;
  160. }
  161. p_item->bound_dirty = true;
  162. int child_item_count = p_item->child_items.size();
  163. Item **child_items = p_item->child_items.ptrw();
  164. for (int i = 0; i < child_item_count; i++) {
  165. _make_bound_dirty_down(child_items[i]);
  166. }
  167. }
  168. void VisualServerCanvas::_prepare_tree_bounds(Item *p_root) {
  169. Rect2 root_bound;
  170. _calculate_canvas_item_bound(p_root, &root_bound);
  171. }
  172. // This function provides an alternative means of recursively calculating canvas item
  173. // bounds through a branch, leading to an identical (hopefully) result to that
  174. // calculated for the bound in _render_canvas_item().
  175. // The reason for this function's existence is that there are some conditions which
  176. // prevent further drawing in the tree (such as alpha nearing 0.0), in which we
  177. // *still* need to calculate the bounds for lower branches and use them for culling,
  178. // just in case alpha increases above the threshold in a later frame.
  179. void VisualServerCanvas::_calculate_canvas_item_bound(Item *p_canvas_item, Rect2 *r_branch_bound) {
  180. // TODO - this higher level technique may be able to be optimized better,
  181. // to perhaps only recalculate this on "reappearance" of the child branch, in a
  182. // similar manner to how visibility is handled.
  183. Item *ci = p_canvas_item;
  184. if (!ci->visible) {
  185. return;
  186. }
  187. // easy case, not dirty
  188. if (!ci->bound_dirty) {
  189. _merge_local_bound_to_branch(ci, r_branch_bound);
  190. return;
  191. }
  192. // recalculate the local bound only if out of date
  193. Rect2 *local_bound = nullptr;
  194. if (ci->bound_dirty) {
  195. local_bound = &ci->local_bound;
  196. *local_bound = Rect2();
  197. ci->bound_dirty = false;
  198. }
  199. int child_item_count = ci->child_items.size();
  200. Item **child_items = ci->child_items.ptrw();
  201. for (int i = 0; i < child_item_count; i++) {
  202. // if (ci->sort_y)
  203. // NYI do we need to apply the child_items[i]->ysort_xform? TEST
  204. // See the _render_canvas_item for how to apply.
  205. _calculate_canvas_item_bound(child_items[i], local_bound);
  206. }
  207. _finalize_and_merge_local_bound_to_branch(ci, r_branch_bound);
  208. }
  209. void VisualServerCanvas::_finalize_and_merge_local_bound_to_branch(Item *p_canvas_item, Rect2 *r_branch_bound) {
  210. if (r_branch_bound) {
  211. Rect2 this_rect = p_canvas_item->get_rect();
  212. // If this item has a bound...
  213. if (!p_canvas_item->local_bound.has_no_area()) {
  214. // If the rect has an area...
  215. if (!this_rect.has_no_area()) {
  216. p_canvas_item->local_bound = p_canvas_item->local_bound.merge(this_rect);
  217. } else {
  218. // The local bound is set by the children, but is not affected by the canvas item rect.
  219. // So pass through and merge the local bound to the parent.
  220. }
  221. } else {
  222. p_canvas_item->local_bound = this_rect;
  223. // don't merge zero area, as it may expand the branch bound
  224. // unnecessarily.
  225. if (p_canvas_item->local_bound.has_no_area()) {
  226. return;
  227. }
  228. }
  229. // Merge the local bound to the parent.
  230. _merge_local_bound_to_branch(p_canvas_item, r_branch_bound);
  231. }
  232. }
  233. void VisualServerCanvas::_merge_local_bound_to_branch(Item *p_canvas_item, Rect2 *r_branch_bound) {
  234. if (!r_branch_bound) {
  235. return;
  236. }
  237. Rect2 this_item_total_local_bound = p_canvas_item->xform.xform(p_canvas_item->local_bound);
  238. if (!r_branch_bound->has_no_area()) {
  239. *r_branch_bound = r_branch_bound->merge(this_item_total_local_bound);
  240. } else {
  241. *r_branch_bound = this_item_total_local_bound;
  242. }
  243. }
  244. void VisualServerCanvas::_render_canvas_item_cull_by_item(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner) {
  245. Item *ci = p_canvas_item;
  246. if (!ci->visible) {
  247. return;
  248. }
  249. if (ci->children_order_dirty) {
  250. ci->child_items.sort_custom<ItemIndexSort>();
  251. ci->children_order_dirty = false;
  252. }
  253. Rect2 rect = ci->get_rect();
  254. Transform2D xform = ci->xform;
  255. xform = p_transform * xform;
  256. Rect2 global_rect = xform.xform(rect);
  257. global_rect.position += p_clip_rect.position;
  258. if (ci->use_parent_material && p_material_owner) {
  259. ci->material_owner = p_material_owner;
  260. } else {
  261. p_material_owner = ci;
  262. ci->material_owner = nullptr;
  263. }
  264. Color modulate(ci->modulate.r * p_modulate.r, ci->modulate.g * p_modulate.g, ci->modulate.b * p_modulate.b, ci->modulate.a * p_modulate.a);
  265. if (modulate.a < 0.007) {
  266. return;
  267. }
  268. int child_item_count = ci->child_items.size();
  269. Item **child_items = ci->child_items.ptrw();
  270. if (ci->clip) {
  271. if (p_canvas_clip != nullptr) {
  272. ci->final_clip_rect = p_canvas_clip->final_clip_rect.clip(global_rect);
  273. } else {
  274. ci->final_clip_rect = global_rect;
  275. }
  276. ci->final_clip_rect.position = ci->final_clip_rect.position.round();
  277. ci->final_clip_rect.size = ci->final_clip_rect.size.round();
  278. ci->final_clip_owner = ci;
  279. } else {
  280. ci->final_clip_owner = p_canvas_clip;
  281. }
  282. if (ci->sort_y) {
  283. if (ci->ysort_children_count == -1) {
  284. ci->ysort_children_count = 0;
  285. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), nullptr, ci->ysort_children_count);
  286. }
  287. child_item_count = ci->ysort_children_count;
  288. child_items = (Item **)alloca(child_item_count * sizeof(Item *));
  289. int i = 0;
  290. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), child_items, i);
  291. SortArray<Item *, ItemPtrSort> sorter;
  292. sorter.sort(child_items, child_item_count);
  293. }
  294. if (ci->z_relative) {
  295. p_z = CLAMP(p_z + ci->z_index, VS::CANVAS_ITEM_Z_MIN, VS::CANVAS_ITEM_Z_MAX);
  296. } else {
  297. p_z = ci->z_index;
  298. }
  299. for (int i = 0; i < child_item_count; i++) {
  300. if (!child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  301. continue;
  302. }
  303. if (ci->sort_y) {
  304. _render_canvas_item_cull_by_item(child_items[i], xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner);
  305. } else {
  306. _render_canvas_item_cull_by_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner);
  307. }
  308. }
  309. if (ci->copy_back_buffer) {
  310. ci->copy_back_buffer->screen_rect = xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  311. }
  312. if (ci->update_when_visible) {
  313. VisualServerRaster::redraw_request(false);
  314. }
  315. if ((!ci->commands.empty() && p_clip_rect.intersects(global_rect, true)) || ci->vp_render || ci->copy_back_buffer) {
  316. //something to draw?
  317. ci->final_transform = xform;
  318. ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a);
  319. ci->global_rect_cache = global_rect;
  320. ci->global_rect_cache.position -= p_clip_rect.position;
  321. ci->light_masked = false;
  322. int zidx = p_z - VS::CANVAS_ITEM_Z_MIN;
  323. if (z_last_list[zidx]) {
  324. z_last_list[zidx]->next = ci;
  325. z_last_list[zidx] = ci;
  326. } else {
  327. z_list[zidx] = ci;
  328. z_last_list[zidx] = ci;
  329. }
  330. ci->next = nullptr;
  331. }
  332. for (int i = 0; i < child_item_count; i++) {
  333. if (child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  334. continue;
  335. }
  336. if (ci->sort_y) {
  337. _render_canvas_item_cull_by_item(child_items[i], xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner);
  338. } else {
  339. _render_canvas_item_cull_by_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner);
  340. }
  341. }
  342. }
  343. void VisualServerCanvas::_render_canvas_item_cull_by_node(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool p_enclosed) {
  344. Item *ci = p_canvas_item;
  345. if (!ci->visible) {
  346. return;
  347. }
  348. // This should have been calculated as a pre-process.
  349. DEV_ASSERT(!ci->bound_dirty);
  350. Rect2 rect = ci->get_rect();
  351. Transform2D final_xform = ci->xform;
  352. final_xform = p_transform * final_xform;
  353. Rect2 global_rect = final_xform.xform(rect);
  354. ci->global_rect_cache = global_rect;
  355. ci->final_transform = final_xform;
  356. global_rect.position += p_clip_rect.position;
  357. int child_item_count = ci->child_items.size();
  358. // If there are children, we can maybe cull them all out if the cached bound has not changed.
  359. if (!p_enclosed && child_item_count) {
  360. // Get the bound in final space.
  361. Rect2 bound = final_xform.xform(ci->local_bound);
  362. bound.position += p_clip_rect.position;
  363. if (!ci->vp_render && !ci->copy_back_buffer) {
  364. // Cull out ALL children in one step.
  365. if (!p_clip_rect.intersects(bound, true)) {
  366. return;
  367. }
  368. }
  369. // can we combine with earlier check?
  370. // if we enclose the bound completely, no need to check further children
  371. p_enclosed = p_clip_rect.encloses(bound);
  372. }
  373. // if we are culled, and no children, no more needs doing
  374. bool item_is_visible = ((!ci->commands.empty() && (p_enclosed ? true : p_clip_rect.intersects(global_rect, true))) || ci->vp_render || ci->copy_back_buffer);
  375. if (!item_is_visible && !child_item_count) {
  376. return;
  377. }
  378. if (ci->use_parent_material && p_material_owner) {
  379. ci->material_owner = p_material_owner;
  380. } else {
  381. p_material_owner = ci;
  382. ci->material_owner = nullptr;
  383. }
  384. Color modulate(ci->modulate.r * p_modulate.r, ci->modulate.g * p_modulate.g, ci->modulate.b * p_modulate.b, ci->modulate.a * p_modulate.a);
  385. if (modulate.a < 0.007) {
  386. return;
  387. }
  388. if (ci->children_order_dirty) {
  389. ci->child_items.sort_custom<ItemIndexSort>();
  390. ci->children_order_dirty = false;
  391. }
  392. Item **child_items = ci->child_items.ptrw();
  393. if (ci->clip) {
  394. if (p_canvas_clip != nullptr) {
  395. ci->final_clip_rect = p_canvas_clip->final_clip_rect.clip(global_rect);
  396. } else {
  397. ci->final_clip_rect = global_rect;
  398. }
  399. ci->final_clip_rect.position = ci->final_clip_rect.position.round();
  400. ci->final_clip_rect.size = ci->final_clip_rect.size.round();
  401. ci->final_clip_owner = ci;
  402. } else {
  403. ci->final_clip_owner = p_canvas_clip;
  404. }
  405. if (ci->sort_y) {
  406. if (ci->ysort_children_count == -1) {
  407. ci->ysort_children_count = 0;
  408. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), nullptr, ci->ysort_children_count);
  409. }
  410. child_item_count = ci->ysort_children_count;
  411. // NOTE : Use of alloca here in a recursive function could make it susceptible to stack overflow.
  412. // This was present in the original Item code. Consider changing to make safer.
  413. child_items = (Item **)alloca(child_item_count * sizeof(Item *));
  414. int i = 0;
  415. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), child_items, i);
  416. SortArray<Item *, ItemPtrSort> sorter;
  417. sorter.sort(child_items, child_item_count);
  418. }
  419. if (ci->z_relative) {
  420. p_z = CLAMP(p_z + ci->z_index, VS::CANVAS_ITEM_Z_MIN, VS::CANVAS_ITEM_Z_MAX);
  421. } else {
  422. p_z = ci->z_index;
  423. }
  424. for (int i = 0; i < child_item_count; i++) {
  425. if (!child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  426. continue;
  427. }
  428. if (ci->sort_y) {
  429. _render_canvas_item_cull_by_node(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, p_enclosed);
  430. } else {
  431. _render_canvas_item_cull_by_node(child_items[i], final_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner, p_enclosed);
  432. }
  433. }
  434. if (ci->copy_back_buffer) {
  435. ci->copy_back_buffer->screen_rect = final_xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  436. }
  437. // something to draw?
  438. if (item_is_visible) {
  439. // Note : This has been moved to inside the (item_is_visible) check.
  440. // It was OUTSIDE in the item culled code, which I suspect was incorrect.
  441. // A redraw should not be issued if an item is not on screen?
  442. // Even so, watch for regressions here.
  443. if (ci->update_when_visible) {
  444. VisualServerRaster::redraw_request(false);
  445. }
  446. // Note we have already stored ci->final_transform
  447. // and ci->global_rect_cache, and made sure these are up to date.
  448. ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a);
  449. ci->light_masked = false;
  450. int zidx = p_z - VS::CANVAS_ITEM_Z_MIN;
  451. if (z_last_list[zidx]) {
  452. z_last_list[zidx]->next = ci;
  453. z_last_list[zidx] = ci;
  454. } else {
  455. z_list[zidx] = ci;
  456. z_last_list[zidx] = ci;
  457. }
  458. ci->next = nullptr;
  459. }
  460. for (int i = 0; i < child_item_count; i++) {
  461. if (child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  462. continue;
  463. }
  464. if (ci->sort_y) {
  465. _render_canvas_item_cull_by_node(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, p_enclosed);
  466. } else {
  467. _render_canvas_item_cull_by_node(child_items[i], final_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner, p_enclosed);
  468. }
  469. }
  470. }
  471. void VisualServerCanvas::_light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights, int p_canvas_layer_id) {
  472. RasterizerCanvas::Item *ci = p_canvas_item;
  473. while (ci) {
  474. RasterizerCanvas::Light *light = p_masked_lights;
  475. while (light) {
  476. if ((p_canvas_layer_id >= light->layer_min) && (p_canvas_layer_id <= light->layer_max) && (ci->light_mask & light->item_mask) && (p_z >= light->z_min) && (p_z <= light->z_max) && (ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache))) {
  477. ci->light_masked = true;
  478. }
  479. light = light->mask_next_ptr;
  480. }
  481. ci = ci->next;
  482. }
  483. }
  484. void VisualServerCanvas::render_canvas(Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, int p_canvas_layer_id) {
  485. VSG::canvas_render->canvas_begin();
  486. if (p_canvas->children_order_dirty) {
  487. p_canvas->child_items.sort();
  488. p_canvas->children_order_dirty = false;
  489. }
  490. int l = p_canvas->child_items.size();
  491. Canvas::ChildItem *ci = p_canvas->child_items.ptrw();
  492. bool has_mirror = false;
  493. for (int i = 0; i < l; i++) {
  494. if (ci[i].mirror.x || ci[i].mirror.y) {
  495. has_mirror = true;
  496. break;
  497. }
  498. }
  499. if (!has_mirror) {
  500. static const int z_range = VS::CANVAS_ITEM_Z_MAX - VS::CANVAS_ITEM_Z_MIN + 1;
  501. RasterizerCanvas::Item *z_list[z_range];
  502. RasterizerCanvas::Item *z_last_list[z_range];
  503. memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  504. memset(z_last_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  505. #ifdef VISUAL_SERVER_CANVAS_TIME_NODE_CULLING
  506. bool measure = (Engine::get_singleton()->get_frames_drawn() % 100) == 0;
  507. measure &= !Engine::get_singleton()->is_editor_hint();
  508. if (measure) {
  509. uint64_t totalA = 0;
  510. uint64_t totalB = 0;
  511. for (int i = 0; i < l; i++) {
  512. uint64_t beforeB = OS::get_singleton()->get_ticks_usec();
  513. _render_canvas_item_cull_by_item(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  514. uint64_t afterB = OS::get_singleton()->get_ticks_usec();
  515. uint64_t beforeA = OS::get_singleton()->get_ticks_usec();
  516. _prepare_tree_bounds(ci[i].item);
  517. _render_canvas_item_cull_by_node(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false);
  518. uint64_t afterA = OS::get_singleton()->get_ticks_usec();
  519. totalA += afterA - beforeA;
  520. totalB += afterB - beforeB;
  521. } // for i
  522. print_line("old : " + itos(totalB) + ", new : " + itos(totalA));
  523. } // if measure
  524. else {
  525. #else
  526. {
  527. #endif
  528. if (_canvas_cull_mode == CANVAS_CULL_MODE_NODE) {
  529. for (int i = 0; i < l; i++) {
  530. _prepare_tree_bounds(ci[i].item);
  531. _render_canvas_item_cull_by_node(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false);
  532. }
  533. } else {
  534. for (int i = 0; i < l; i++) {
  535. _render_canvas_item_cull_by_item(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  536. }
  537. }
  538. } // if not measure
  539. VSG::canvas_render->canvas_render_items_begin(p_canvas->modulate, p_lights, p_transform);
  540. for (int i = 0; i < z_range; i++) {
  541. if (!z_list[i]) {
  542. continue;
  543. }
  544. if (p_masked_lights) {
  545. _light_mask_canvas_items(VS::CANVAS_ITEM_Z_MIN + i, z_list[i], p_masked_lights, p_canvas_layer_id);
  546. }
  547. VSG::canvas_render->canvas_render_items(z_list[i], VS::CANVAS_ITEM_Z_MIN + i, p_canvas->modulate, p_lights, p_transform);
  548. }
  549. VSG::canvas_render->canvas_render_items_end();
  550. } else {
  551. for (int i = 0; i < l; i++) {
  552. const Canvas::ChildItem &ci2 = p_canvas->child_items[i];
  553. _render_canvas_item_tree(ci2.item, p_transform, p_clip_rect, p_canvas->modulate, p_lights);
  554. //mirroring (useful for scrolling backgrounds)
  555. if (ci2.mirror.x != 0) {
  556. Transform2D xform2 = p_transform * Transform2D(0, Vector2(ci2.mirror.x, 0));
  557. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  558. }
  559. if (ci2.mirror.y != 0) {
  560. Transform2D xform2 = p_transform * Transform2D(0, Vector2(0, ci2.mirror.y));
  561. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  562. }
  563. if (ci2.mirror.y != 0 && ci2.mirror.x != 0) {
  564. Transform2D xform2 = p_transform * Transform2D(0, ci2.mirror);
  565. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  566. }
  567. }
  568. }
  569. VSG::canvas_render->canvas_end();
  570. }
  571. RID VisualServerCanvas::canvas_create() {
  572. Canvas *canvas = memnew(Canvas);
  573. ERR_FAIL_COND_V(!canvas, RID());
  574. RID rid = canvas_owner.make_rid(canvas);
  575. return rid;
  576. }
  577. void VisualServerCanvas::canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring) {
  578. Canvas *canvas = canvas_owner.getornull(p_canvas);
  579. ERR_FAIL_COND(!canvas);
  580. Item *canvas_item = canvas_item_owner.getornull(p_item);
  581. ERR_FAIL_COND(!canvas_item);
  582. int idx = canvas->find_item(canvas_item);
  583. ERR_FAIL_COND(idx == -1);
  584. canvas->child_items.write[idx].mirror = p_mirroring;
  585. }
  586. void VisualServerCanvas::canvas_set_modulate(RID p_canvas, const Color &p_color) {
  587. Canvas *canvas = canvas_owner.get(p_canvas);
  588. ERR_FAIL_COND(!canvas);
  589. canvas->modulate = p_color;
  590. }
  591. void VisualServerCanvas::canvas_set_disable_scale(bool p_disable) {
  592. disable_scale = p_disable;
  593. }
  594. void VisualServerCanvas::canvas_set_parent(RID p_canvas, RID p_parent, float p_scale) {
  595. Canvas *canvas = canvas_owner.get(p_canvas);
  596. ERR_FAIL_COND(!canvas);
  597. canvas->parent = p_parent;
  598. canvas->parent_scale = p_scale;
  599. }
  600. RID VisualServerCanvas::canvas_item_create() {
  601. Item *canvas_item = memnew(Item);
  602. ERR_FAIL_COND_V(!canvas_item, RID());
  603. return canvas_item_owner.make_rid(canvas_item);
  604. }
  605. void VisualServerCanvas::canvas_item_set_name(RID p_item, String p_name) {
  606. #ifdef VISUAL_SERVER_CANVAS_DEBUG_ITEM_NAMES
  607. Item *canvas_item = canvas_item_owner.getornull(p_item);
  608. ERR_FAIL_COND(!canvas_item);
  609. canvas_item->name = p_name;
  610. #endif
  611. }
  612. void VisualServerCanvas::canvas_item_set_parent(RID p_item, RID p_parent) {
  613. Item *canvas_item = canvas_item_owner.getornull(p_item);
  614. ERR_FAIL_COND(!canvas_item);
  615. // dirty the item and any previous parents
  616. _make_bound_dirty(canvas_item);
  617. if (canvas_item->parent.is_valid()) {
  618. if (canvas_owner.owns(canvas_item->parent)) {
  619. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  620. canvas->erase_item(canvas_item);
  621. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  622. Item *item_owner = canvas_item_owner.get(canvas_item->parent);
  623. item_owner->child_items.erase(canvas_item);
  624. if (item_owner->sort_y) {
  625. _mark_ysort_dirty(item_owner, canvas_item_owner);
  626. }
  627. }
  628. canvas_item->parent = RID();
  629. }
  630. if (p_parent.is_valid()) {
  631. if (canvas_owner.owns(p_parent)) {
  632. Canvas *canvas = canvas_owner.get(p_parent);
  633. Canvas::ChildItem ci;
  634. ci.item = canvas_item;
  635. canvas->child_items.push_back(ci);
  636. canvas->children_order_dirty = true;
  637. _make_bound_dirty(canvas_item);
  638. } else if (canvas_item_owner.owns(p_parent)) {
  639. Item *item_owner = canvas_item_owner.get(p_parent);
  640. item_owner->child_items.push_back(canvas_item);
  641. item_owner->children_order_dirty = true;
  642. if (item_owner->sort_y) {
  643. _mark_ysort_dirty(item_owner, canvas_item_owner);
  644. }
  645. // keep the integrity of the bounds when adding to avoid false
  646. // warning flags, by forcing the added child to be dirty
  647. canvas_item->bound_dirty = false;
  648. canvas_item->parent = p_parent;
  649. _make_bound_dirty_reparent(canvas_item);
  650. } else {
  651. ERR_FAIL_MSG("Invalid parent.");
  652. }
  653. }
  654. canvas_item->parent = p_parent;
  655. _check_bound_integrity(canvas_item);
  656. }
  657. void VisualServerCanvas::canvas_item_set_visible(RID p_item, bool p_visible) {
  658. Item *canvas_item = canvas_item_owner.getornull(p_item);
  659. ERR_FAIL_COND(!canvas_item);
  660. // check for noop
  661. if (p_visible != canvas_item->visible) {
  662. canvas_item->visible = p_visible;
  663. _make_bound_dirty(canvas_item, true);
  664. }
  665. // could this be enclosed in the noop? not sure
  666. _mark_ysort_dirty(canvas_item, canvas_item_owner);
  667. }
  668. void VisualServerCanvas::canvas_item_set_light_mask(RID p_item, int p_mask) {
  669. Item *canvas_item = canvas_item_owner.getornull(p_item);
  670. ERR_FAIL_COND(!canvas_item);
  671. canvas_item->light_mask = p_mask;
  672. _check_bound_integrity(canvas_item);
  673. }
  674. void VisualServerCanvas::canvas_item_set_transform(RID p_item, const Transform2D &p_transform) {
  675. Item *canvas_item = canvas_item_owner.getornull(p_item);
  676. ERR_FAIL_COND(!canvas_item);
  677. canvas_item->xform = p_transform;
  678. // Special case!
  679. // Modifying the transform DOES NOT affect the local bound.
  680. // It only affects the local bound of the PARENT node (if there is one).
  681. if (canvas_item_owner.owns(canvas_item->parent)) {
  682. Item *canvas_item_parent = canvas_item_owner.get(canvas_item->parent);
  683. _make_bound_dirty(canvas_item_parent);
  684. }
  685. }
  686. void VisualServerCanvas::canvas_item_set_clip(RID p_item, bool p_clip) {
  687. Item *canvas_item = canvas_item_owner.getornull(p_item);
  688. ERR_FAIL_COND(!canvas_item);
  689. canvas_item->clip = p_clip;
  690. _make_bound_dirty(canvas_item);
  691. }
  692. void VisualServerCanvas::canvas_item_set_distance_field_mode(RID p_item, bool p_enable) {
  693. Item *canvas_item = canvas_item_owner.getornull(p_item);
  694. ERR_FAIL_COND(!canvas_item);
  695. canvas_item->distance_field = p_enable;
  696. _make_bound_dirty(canvas_item);
  697. }
  698. void VisualServerCanvas::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect) {
  699. Item *canvas_item = canvas_item_owner.getornull(p_item);
  700. ERR_FAIL_COND(!canvas_item);
  701. canvas_item->custom_rect = p_custom_rect;
  702. canvas_item->rect = p_rect;
  703. _make_bound_dirty(canvas_item);
  704. }
  705. void VisualServerCanvas::canvas_item_set_modulate(RID p_item, const Color &p_color) {
  706. Item *canvas_item = canvas_item_owner.getornull(p_item);
  707. ERR_FAIL_COND(!canvas_item);
  708. canvas_item->modulate = p_color;
  709. _make_bound_dirty(canvas_item);
  710. }
  711. void VisualServerCanvas::canvas_item_set_self_modulate(RID p_item, const Color &p_color) {
  712. Item *canvas_item = canvas_item_owner.getornull(p_item);
  713. ERR_FAIL_COND(!canvas_item);
  714. canvas_item->self_modulate = p_color;
  715. _make_bound_dirty(canvas_item);
  716. }
  717. void VisualServerCanvas::canvas_item_set_draw_behind_parent(RID p_item, bool p_enable) {
  718. Item *canvas_item = canvas_item_owner.getornull(p_item);
  719. ERR_FAIL_COND(!canvas_item);
  720. canvas_item->behind = p_enable;
  721. _check_bound_integrity(canvas_item);
  722. }
  723. void VisualServerCanvas::canvas_item_set_update_when_visible(RID p_item, bool p_update) {
  724. Item *canvas_item = canvas_item_owner.getornull(p_item);
  725. ERR_FAIL_COND(!canvas_item);
  726. canvas_item->update_when_visible = p_update;
  727. _check_bound_integrity(canvas_item);
  728. }
  729. void VisualServerCanvas::canvas_item_add_line(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width, bool p_antialiased) {
  730. // Try drawing as a poly, because polys are batched and thus should run faster than thick lines,
  731. // which run extremely slowly.
  732. if (!p_antialiased && (p_width > 1.0)) {
  733. // use poly drawing, as it is faster as it can use batching
  734. static Vector<Point2> points;
  735. static Vector<Color> colors;
  736. static Vector<Point2> uvs;
  737. if (points.size() != 4) {
  738. // this should only be done once at runtime due to use of a static
  739. points.resize(4);
  740. colors.resize(4);
  741. uvs.resize(4);
  742. }
  743. Vector2 side = p_to - p_from;
  744. real_t length = side.length();
  745. if (length == 0.0) {
  746. // Not sure yet whether zero length is a noop operation later on,
  747. // watch for visual errors. If there are visual errors, pass through
  748. // to the line drawing routine below.
  749. return;
  750. }
  751. // normalize
  752. side /= length;
  753. // 90 degrees
  754. side = Vector2(-side.y, side.x);
  755. side *= p_width * 0.5;
  756. points.set(0, p_from + side);
  757. points.set(1, p_from - side);
  758. points.set(2, p_to - side);
  759. points.set(3, p_to + side);
  760. for (int n = 0; n < 4; n++) {
  761. colors.set(n, p_color);
  762. }
  763. canvas_item_add_polygon(p_item, points, colors, uvs, RID(), RID(), false);
  764. return;
  765. }
  766. Item *canvas_item = canvas_item_owner.getornull(p_item);
  767. ERR_FAIL_COND(!canvas_item);
  768. Item::CommandLine *line = memnew(Item::CommandLine);
  769. ERR_FAIL_COND(!line);
  770. line->color = p_color;
  771. line->from = p_from;
  772. line->to = p_to;
  773. line->width = p_width;
  774. line->antialiased = p_antialiased;
  775. canvas_item->rect_dirty = true;
  776. canvas_item->commands.push_back(line);
  777. _make_bound_dirty(canvas_item);
  778. }
  779. void VisualServerCanvas::canvas_item_add_polyline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  780. ERR_FAIL_COND(p_points.size() < 2);
  781. Item *canvas_item = canvas_item_owner.getornull(p_item);
  782. ERR_FAIL_COND(!canvas_item);
  783. Item::CommandPolyLine *pline = memnew(Item::CommandPolyLine);
  784. ERR_FAIL_COND(!pline);
  785. pline->antialiased = p_antialiased;
  786. pline->multiline = false;
  787. if (p_width <= 1) {
  788. pline->lines = p_points;
  789. pline->line_colors = p_colors;
  790. if (pline->line_colors.size() == 0) {
  791. pline->line_colors.push_back(Color(1, 1, 1, 1));
  792. } else if (pline->line_colors.size() > 1 && pline->line_colors.size() != pline->lines.size()) {
  793. pline->line_colors.resize(1);
  794. }
  795. } else {
  796. //make a trianglestrip for drawing the line...
  797. Vector2 prev_t;
  798. pline->triangles.resize(p_points.size() * 2);
  799. if (p_antialiased) {
  800. pline->lines.resize(p_points.size() * 2);
  801. }
  802. if (p_colors.size() == 0) {
  803. pline->triangle_colors.push_back(Color(1, 1, 1, 1));
  804. if (p_antialiased) {
  805. pline->line_colors.push_back(Color(1, 1, 1, 1));
  806. }
  807. } else if (p_colors.size() == 1) {
  808. pline->triangle_colors = p_colors;
  809. pline->line_colors = p_colors;
  810. } else {
  811. if (p_colors.size() != p_points.size()) {
  812. pline->triangle_colors.push_back(p_colors[0]);
  813. pline->line_colors.push_back(p_colors[0]);
  814. } else {
  815. pline->triangle_colors.resize(pline->triangles.size());
  816. pline->line_colors.resize(pline->lines.size());
  817. }
  818. }
  819. for (int i = 0; i < p_points.size(); i++) {
  820. Vector2 t;
  821. if (i == p_points.size() - 1) {
  822. t = prev_t;
  823. } else {
  824. t = (p_points[i + 1] - p_points[i]).normalized().tangent();
  825. if (i == 0) {
  826. prev_t = t;
  827. }
  828. }
  829. Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5;
  830. if (p_antialiased) {
  831. pline->lines.write[i] = p_points[i] + tangent;
  832. pline->lines.write[p_points.size() * 2 - i - 1] = p_points[i] - tangent;
  833. if (pline->line_colors.size() > 1) {
  834. pline->line_colors.write[i] = p_colors[i];
  835. pline->line_colors.write[p_points.size() * 2 - i - 1] = p_colors[i];
  836. }
  837. }
  838. pline->triangles.write[i * 2 + 0] = p_points[i] + tangent;
  839. pline->triangles.write[i * 2 + 1] = p_points[i] - tangent;
  840. if (pline->triangle_colors.size() > 1) {
  841. pline->triangle_colors.write[i * 2 + 0] = p_colors[i];
  842. pline->triangle_colors.write[i * 2 + 1] = p_colors[i];
  843. }
  844. prev_t = t;
  845. }
  846. }
  847. canvas_item->rect_dirty = true;
  848. canvas_item->commands.push_back(pline);
  849. _make_bound_dirty(canvas_item);
  850. }
  851. void VisualServerCanvas::canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  852. ERR_FAIL_COND(p_points.size() < 2);
  853. Item *canvas_item = canvas_item_owner.getornull(p_item);
  854. ERR_FAIL_COND(!canvas_item);
  855. Item::CommandPolyLine *pline = memnew(Item::CommandPolyLine);
  856. ERR_FAIL_COND(!pline);
  857. pline->antialiased = false; //todo
  858. pline->multiline = true;
  859. pline->lines = p_points;
  860. pline->line_colors = p_colors;
  861. if (pline->line_colors.size() == 0) {
  862. pline->line_colors.push_back(Color(1, 1, 1, 1));
  863. } else if (pline->line_colors.size() > 1 && pline->line_colors.size() != pline->lines.size()) {
  864. pline->line_colors.resize(1);
  865. }
  866. canvas_item->rect_dirty = true;
  867. canvas_item->commands.push_back(pline);
  868. _make_bound_dirty(canvas_item);
  869. }
  870. void VisualServerCanvas::canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color) {
  871. Item *canvas_item = canvas_item_owner.getornull(p_item);
  872. ERR_FAIL_COND(!canvas_item);
  873. Item::CommandRect *rect = memnew(Item::CommandRect);
  874. ERR_FAIL_COND(!rect);
  875. rect->modulate = p_color;
  876. rect->rect = p_rect;
  877. canvas_item->rect_dirty = true;
  878. canvas_item->commands.push_back(rect);
  879. _make_bound_dirty(canvas_item);
  880. }
  881. void VisualServerCanvas::canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color) {
  882. Item *canvas_item = canvas_item_owner.getornull(p_item);
  883. ERR_FAIL_COND(!canvas_item);
  884. Item::CommandCircle *circle = memnew(Item::CommandCircle);
  885. ERR_FAIL_COND(!circle);
  886. circle->color = p_color;
  887. circle->pos = p_pos;
  888. circle->radius = p_radius;
  889. canvas_item->commands.push_back(circle);
  890. _make_bound_dirty(canvas_item);
  891. }
  892. void VisualServerCanvas::canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile, const Color &p_modulate, bool p_transpose, RID p_normal_map) {
  893. Item *canvas_item = canvas_item_owner.getornull(p_item);
  894. ERR_FAIL_COND(!canvas_item);
  895. Item::CommandRect *rect = memnew(Item::CommandRect);
  896. ERR_FAIL_COND(!rect);
  897. rect->modulate = p_modulate;
  898. rect->rect = p_rect;
  899. rect->flags = 0;
  900. if (p_tile) {
  901. rect->flags |= RasterizerCanvas::CANVAS_RECT_TILE;
  902. rect->flags |= RasterizerCanvas::CANVAS_RECT_REGION;
  903. rect->source = Rect2(0, 0, fabsf(p_rect.size.width), fabsf(p_rect.size.height));
  904. }
  905. if (p_rect.size.x < 0) {
  906. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  907. rect->rect.size.x = -rect->rect.size.x;
  908. }
  909. if (p_rect.size.y < 0) {
  910. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  911. rect->rect.size.y = -rect->rect.size.y;
  912. }
  913. if (p_transpose) {
  914. rect->flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  915. SWAP(rect->rect.size.x, rect->rect.size.y);
  916. }
  917. rect->texture = p_texture;
  918. rect->normal_map = p_normal_map;
  919. canvas_item->rect_dirty = true;
  920. canvas_item->commands.push_back(rect);
  921. _make_bound_dirty(canvas_item);
  922. }
  923. void VisualServerCanvas::canvas_item_add_texture_multirect_region(RID p_item, const Vector<Rect2> &p_rects, RID p_texture, const Vector<Rect2> &p_src_rects, const Color &p_modulate, uint32_t p_canvas_rect_flags, RID p_normal_map) {
  924. Item *canvas_item = canvas_item_owner.getornull(p_item);
  925. ERR_FAIL_COND(!canvas_item);
  926. ERR_FAIL_COND(p_rects.size() != p_src_rects.size());
  927. ERR_FAIL_COND(!p_rects.size());
  928. Item::CommandMultiRect *rect = memnew(Item::CommandMultiRect);
  929. ERR_FAIL_COND(!rect);
  930. rect->modulate = p_modulate;
  931. rect->texture = p_texture;
  932. rect->normal_map = p_normal_map;
  933. // Rects should have flips and transposes pre-applied, and the relevant
  934. // flags added to p_canvas_rect_flags.
  935. // A single Multirect should contain rects ALL of the same flag type.
  936. // The idea is to simplify the renderer as much as possible, and push the complexity
  937. // to the one off creation code.
  938. rect->flags = p_canvas_rect_flags | RasterizerCanvas::CANVAS_RECT_REGION;
  939. rect->rects = p_rects;
  940. rect->sources = p_src_rects;
  941. canvas_item->rect_dirty = true;
  942. canvas_item->commands.push_back(rect);
  943. }
  944. void VisualServerCanvas::canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, RID p_normal_map, bool p_clip_uv) {
  945. Item *canvas_item = canvas_item_owner.getornull(p_item);
  946. ERR_FAIL_COND(!canvas_item);
  947. Item::CommandRect *rect = memnew(Item::CommandRect);
  948. ERR_FAIL_COND(!rect);
  949. rect->modulate = p_modulate;
  950. rect->rect = p_rect;
  951. rect->texture = p_texture;
  952. rect->normal_map = p_normal_map;
  953. rect->source = p_src_rect;
  954. rect->flags = RasterizerCanvas::CANVAS_RECT_REGION;
  955. if (p_rect.size.x < 0) {
  956. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  957. rect->rect.size.x = -rect->rect.size.x;
  958. }
  959. if (p_src_rect.size.x < 0) {
  960. rect->flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  961. rect->source.size.x = -rect->source.size.x;
  962. }
  963. if (p_rect.size.y < 0) {
  964. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  965. rect->rect.size.y = -rect->rect.size.y;
  966. }
  967. if (p_src_rect.size.y < 0) {
  968. rect->flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  969. rect->source.size.y = -rect->source.size.y;
  970. }
  971. if (p_transpose) {
  972. rect->flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  973. SWAP(rect->rect.size.x, rect->rect.size.y);
  974. }
  975. if (p_clip_uv) {
  976. rect->flags |= RasterizerCanvas::CANVAS_RECT_CLIP_UV;
  977. }
  978. canvas_item->rect_dirty = true;
  979. canvas_item->commands.push_back(rect);
  980. _make_bound_dirty(canvas_item);
  981. }
  982. void VisualServerCanvas::canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, VS::NinePatchAxisMode p_x_axis_mode, VS::NinePatchAxisMode p_y_axis_mode, bool p_draw_center, const Color &p_modulate, RID p_normal_map) {
  983. Item *canvas_item = canvas_item_owner.getornull(p_item);
  984. ERR_FAIL_COND(!canvas_item);
  985. Item::CommandNinePatch *style = memnew(Item::CommandNinePatch);
  986. ERR_FAIL_COND(!style);
  987. style->texture = p_texture;
  988. style->normal_map = p_normal_map;
  989. style->rect = p_rect;
  990. style->source = p_source;
  991. style->draw_center = p_draw_center;
  992. style->color = p_modulate;
  993. style->margin[MARGIN_LEFT] = p_topleft.x;
  994. style->margin[MARGIN_TOP] = p_topleft.y;
  995. style->margin[MARGIN_RIGHT] = p_bottomright.x;
  996. style->margin[MARGIN_BOTTOM] = p_bottomright.y;
  997. style->axis_x = p_x_axis_mode;
  998. style->axis_y = p_y_axis_mode;
  999. canvas_item->rect_dirty = true;
  1000. canvas_item->commands.push_back(style);
  1001. _make_bound_dirty(canvas_item);
  1002. }
  1003. void VisualServerCanvas::canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width, RID p_normal_map) {
  1004. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1005. ERR_FAIL_COND(!canvas_item);
  1006. Item::CommandPrimitive *prim = memnew(Item::CommandPrimitive);
  1007. ERR_FAIL_COND(!prim);
  1008. prim->texture = p_texture;
  1009. prim->normal_map = p_normal_map;
  1010. prim->points = p_points;
  1011. prim->uvs = p_uvs;
  1012. prim->colors = p_colors;
  1013. prim->width = p_width;
  1014. canvas_item->rect_dirty = true;
  1015. canvas_item->commands.push_back(prim);
  1016. _make_bound_dirty(canvas_item);
  1017. }
  1018. void VisualServerCanvas::canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, RID p_normal_map, bool p_antialiased) {
  1019. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1020. ERR_FAIL_COND(!canvas_item);
  1021. #ifdef DEBUG_ENABLED
  1022. int pointcount = p_points.size();
  1023. ERR_FAIL_COND(pointcount < 3);
  1024. int color_size = p_colors.size();
  1025. int uv_size = p_uvs.size();
  1026. ERR_FAIL_COND(color_size != 0 && color_size != 1 && color_size != pointcount);
  1027. ERR_FAIL_COND(uv_size != 0 && (uv_size != pointcount));
  1028. #endif
  1029. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  1030. ERR_FAIL_COND_MSG(indices.empty(), "Invalid polygon data, triangulation failed.");
  1031. Item::CommandPolygon *polygon = memnew(Item::CommandPolygon);
  1032. ERR_FAIL_COND(!polygon);
  1033. polygon->texture = p_texture;
  1034. polygon->normal_map = p_normal_map;
  1035. polygon->points = p_points;
  1036. polygon->uvs = p_uvs;
  1037. polygon->colors = p_colors;
  1038. polygon->indices = indices;
  1039. polygon->count = indices.size();
  1040. polygon->antialiased = p_antialiased;
  1041. polygon->antialiasing_use_indices = false;
  1042. canvas_item->rect_dirty = true;
  1043. canvas_item->commands.push_back(polygon);
  1044. _make_bound_dirty(canvas_item);
  1045. }
  1046. void VisualServerCanvas::canvas_item_add_triangle_array(RID p_item, const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, const Vector<int> &p_bones, const Vector<float> &p_weights, RID p_texture, int p_count, RID p_normal_map, bool p_antialiased, bool p_antialiasing_use_indices) {
  1047. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1048. ERR_FAIL_COND(!canvas_item);
  1049. int vertex_count = p_points.size();
  1050. ERR_FAIL_COND(vertex_count == 0);
  1051. ERR_FAIL_COND(!p_colors.empty() && p_colors.size() != vertex_count && p_colors.size() != 1);
  1052. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size() != vertex_count);
  1053. ERR_FAIL_COND(!p_bones.empty() && p_bones.size() != vertex_count * 4);
  1054. ERR_FAIL_COND(!p_weights.empty() && p_weights.size() != vertex_count * 4);
  1055. const Vector<int> &indices = p_indices;
  1056. int count = p_count * 3;
  1057. if (indices.empty()) {
  1058. ERR_FAIL_COND(vertex_count % 3 != 0);
  1059. if (p_count == -1) {
  1060. count = vertex_count;
  1061. }
  1062. } else {
  1063. ERR_FAIL_COND(indices.size() % 3 != 0);
  1064. if (p_count == -1) {
  1065. count = indices.size();
  1066. }
  1067. }
  1068. Item::CommandPolygon *polygon = memnew(Item::CommandPolygon);
  1069. ERR_FAIL_COND(!polygon);
  1070. polygon->texture = p_texture;
  1071. polygon->normal_map = p_normal_map;
  1072. polygon->points = p_points;
  1073. polygon->uvs = p_uvs;
  1074. polygon->colors = p_colors;
  1075. polygon->bones = p_bones;
  1076. polygon->weights = p_weights;
  1077. polygon->indices = indices;
  1078. polygon->count = count;
  1079. polygon->antialiased = p_antialiased;
  1080. polygon->antialiasing_use_indices = p_antialiasing_use_indices;
  1081. canvas_item->rect_dirty = true;
  1082. canvas_item->commands.push_back(polygon);
  1083. _make_bound_dirty(canvas_item);
  1084. }
  1085. void VisualServerCanvas::canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform) {
  1086. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1087. ERR_FAIL_COND(!canvas_item);
  1088. Item::CommandTransform *tr = memnew(Item::CommandTransform);
  1089. ERR_FAIL_COND(!tr);
  1090. tr->xform = p_transform;
  1091. canvas_item->commands.push_back(tr);
  1092. _make_bound_dirty(canvas_item);
  1093. }
  1094. void VisualServerCanvas::canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform, const Color &p_modulate, RID p_texture, RID p_normal_map) {
  1095. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1096. ERR_FAIL_COND(!canvas_item);
  1097. Item::CommandMesh *m = memnew(Item::CommandMesh);
  1098. ERR_FAIL_COND(!m);
  1099. m->mesh = p_mesh;
  1100. m->texture = p_texture;
  1101. m->normal_map = p_normal_map;
  1102. m->transform = p_transform;
  1103. m->modulate = p_modulate;
  1104. canvas_item->commands.push_back(m);
  1105. _make_bound_dirty(canvas_item);
  1106. }
  1107. void VisualServerCanvas::canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture, RID p_normal) {
  1108. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1109. ERR_FAIL_COND(!canvas_item);
  1110. Item::CommandParticles *part = memnew(Item::CommandParticles);
  1111. ERR_FAIL_COND(!part);
  1112. part->particles = p_particles;
  1113. part->texture = p_texture;
  1114. part->normal_map = p_normal;
  1115. //take the chance and request processing for them, at least once until they become visible again
  1116. VSG::storage->particles_request_process(p_particles);
  1117. canvas_item->rect_dirty = true;
  1118. canvas_item->commands.push_back(part);
  1119. _make_bound_dirty(canvas_item);
  1120. }
  1121. void VisualServerCanvas::canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture, RID p_normal_map) {
  1122. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1123. ERR_FAIL_COND(!canvas_item);
  1124. Item::CommandMultiMesh *mm = memnew(Item::CommandMultiMesh);
  1125. ERR_FAIL_COND(!mm);
  1126. mm->multimesh = p_mesh;
  1127. mm->texture = p_texture;
  1128. mm->normal_map = p_normal_map;
  1129. canvas_item->rect_dirty = true;
  1130. canvas_item->commands.push_back(mm);
  1131. _make_bound_dirty(canvas_item);
  1132. }
  1133. void VisualServerCanvas::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  1134. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1135. ERR_FAIL_COND(!canvas_item);
  1136. Item::CommandClipIgnore *ci = memnew(Item::CommandClipIgnore);
  1137. ERR_FAIL_COND(!ci);
  1138. ci->ignore = p_ignore;
  1139. canvas_item->commands.push_back(ci);
  1140. _make_bound_dirty(canvas_item);
  1141. }
  1142. void VisualServerCanvas::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
  1143. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1144. ERR_FAIL_COND(!canvas_item);
  1145. canvas_item->sort_y = p_enable;
  1146. _mark_ysort_dirty(canvas_item, canvas_item_owner);
  1147. _check_bound_integrity(canvas_item);
  1148. }
  1149. void VisualServerCanvas::canvas_item_set_z_index(RID p_item, int p_z) {
  1150. ERR_FAIL_COND(p_z < VS::CANVAS_ITEM_Z_MIN || p_z > VS::CANVAS_ITEM_Z_MAX);
  1151. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1152. ERR_FAIL_COND(!canvas_item);
  1153. canvas_item->z_index = p_z;
  1154. _check_bound_integrity(canvas_item);
  1155. }
  1156. void VisualServerCanvas::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
  1157. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1158. ERR_FAIL_COND(!canvas_item);
  1159. canvas_item->z_relative = p_enable;
  1160. _check_bound_integrity(canvas_item);
  1161. }
  1162. Rect2 VisualServerCanvas::_debug_canvas_item_get_rect(RID p_item) {
  1163. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1164. ERR_FAIL_COND_V(!canvas_item, Rect2());
  1165. return canvas_item->get_rect();
  1166. }
  1167. void VisualServerCanvas::canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform) {
  1168. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1169. ERR_FAIL_COND(!canvas_item);
  1170. if (!canvas_item->skinning_data) {
  1171. canvas_item->skinning_data = memnew(Item::SkinningData);
  1172. }
  1173. canvas_item->skinning_data->skeleton_relative_xform = p_relative_xform;
  1174. canvas_item->skinning_data->skeleton_relative_xform_inv = p_relative_xform.affine_inverse();
  1175. // Set any Polygon2Ds pre-calced bone bounds to dirty.
  1176. for (int n = 0; n < canvas_item->commands.size(); n++) {
  1177. Item::Command *c = canvas_item->commands[n];
  1178. if (c->type == Item::Command::TYPE_POLYGON) {
  1179. Item::CommandPolygon *polygon = static_cast<Item::CommandPolygon *>(c);
  1180. // Make sure skinning data is present.
  1181. if (!polygon->skinning_data) {
  1182. polygon->skinning_data = memnew(Item::CommandPolygon::SkinningData);
  1183. }
  1184. polygon->skinning_data->dirty = true;
  1185. }
  1186. }
  1187. }
  1188. void VisualServerCanvas::canvas_item_attach_skeleton(RID p_item, RID p_skeleton) {
  1189. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1190. ERR_FAIL_COND(!canvas_item);
  1191. if (_canvas_cull_mode == CANVAS_CULL_MODE_NODE) {
  1192. // No op?
  1193. if (canvas_item->skeleton == p_skeleton) {
  1194. return;
  1195. }
  1196. // Detach from any previous skeleton.
  1197. if (canvas_item->skeleton.is_valid()) {
  1198. VSG::storage->skeleton_attach_canvas_item(canvas_item->skeleton, p_item, false);
  1199. }
  1200. canvas_item->skeleton = p_skeleton;
  1201. // Attach to new skeleton.
  1202. if (p_skeleton.is_valid()) {
  1203. VSG::storage->skeleton_attach_canvas_item(p_skeleton, p_item, true);
  1204. }
  1205. _make_bound_dirty(canvas_item);
  1206. } else {
  1207. canvas_item->skeleton = p_skeleton;
  1208. }
  1209. }
  1210. void VisualServerCanvas::_canvas_item_skeleton_moved(RID p_item) {
  1211. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1212. ERR_FAIL_COND(!canvas_item);
  1213. _make_bound_dirty(canvas_item);
  1214. }
  1215. void VisualServerCanvas::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect) {
  1216. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1217. ERR_FAIL_COND(!canvas_item);
  1218. if (bool(canvas_item->copy_back_buffer != nullptr) != p_enable) {
  1219. if (p_enable) {
  1220. canvas_item->copy_back_buffer = memnew(RasterizerCanvas::Item::CopyBackBuffer);
  1221. } else {
  1222. memdelete(canvas_item->copy_back_buffer);
  1223. canvas_item->copy_back_buffer = nullptr;
  1224. }
  1225. }
  1226. if (p_enable) {
  1227. canvas_item->copy_back_buffer->rect = p_rect;
  1228. canvas_item->copy_back_buffer->full = p_rect == Rect2();
  1229. }
  1230. _make_bound_dirty(canvas_item);
  1231. }
  1232. void VisualServerCanvas::canvas_item_clear(RID p_item) {
  1233. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1234. ERR_FAIL_COND(!canvas_item);
  1235. _make_bound_dirty(canvas_item);
  1236. canvas_item->clear();
  1237. }
  1238. void VisualServerCanvas::canvas_item_set_draw_index(RID p_item, int p_index) {
  1239. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1240. ERR_FAIL_COND(!canvas_item);
  1241. canvas_item->index = p_index;
  1242. if (canvas_item_owner.owns(canvas_item->parent)) {
  1243. Item *canvas_item_parent = canvas_item_owner.getornull(canvas_item->parent);
  1244. canvas_item_parent->children_order_dirty = true;
  1245. return;
  1246. }
  1247. Canvas *canvas = canvas_owner.getornull(canvas_item->parent);
  1248. if (canvas) {
  1249. canvas->children_order_dirty = true;
  1250. return;
  1251. }
  1252. _check_bound_integrity(canvas_item);
  1253. }
  1254. void VisualServerCanvas::canvas_item_set_material(RID p_item, RID p_material) {
  1255. Item *canvas_item = canvas_item_owner.get(p_item);
  1256. ERR_FAIL_COND(!canvas_item);
  1257. canvas_item->material = p_material;
  1258. _make_bound_dirty(canvas_item);
  1259. }
  1260. void VisualServerCanvas::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
  1261. Item *canvas_item = canvas_item_owner.get(p_item);
  1262. ERR_FAIL_COND(!canvas_item);
  1263. canvas_item->use_parent_material = p_enable;
  1264. _make_bound_dirty(canvas_item);
  1265. }
  1266. RID VisualServerCanvas::canvas_light_create() {
  1267. RasterizerCanvas::Light *clight = memnew(RasterizerCanvas::Light);
  1268. clight->light_internal = VSG::canvas_render->light_internal_create();
  1269. return canvas_light_owner.make_rid(clight);
  1270. }
  1271. void VisualServerCanvas::canvas_light_attach_to_canvas(RID p_light, RID p_canvas) {
  1272. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1273. ERR_FAIL_COND(!clight);
  1274. if (clight->canvas.is_valid()) {
  1275. Canvas *canvas = canvas_owner.getornull(clight->canvas);
  1276. canvas->lights.erase(clight);
  1277. }
  1278. if (!canvas_owner.owns(p_canvas)) {
  1279. p_canvas = RID();
  1280. }
  1281. clight->canvas = p_canvas;
  1282. if (clight->canvas.is_valid()) {
  1283. Canvas *canvas = canvas_owner.get(clight->canvas);
  1284. canvas->lights.insert(clight);
  1285. }
  1286. }
  1287. void VisualServerCanvas::canvas_light_set_enabled(RID p_light, bool p_enabled) {
  1288. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1289. ERR_FAIL_COND(!clight);
  1290. clight->enabled = p_enabled;
  1291. }
  1292. void VisualServerCanvas::canvas_light_set_scale(RID p_light, float p_scale) {
  1293. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1294. ERR_FAIL_COND(!clight);
  1295. clight->scale = p_scale;
  1296. }
  1297. void VisualServerCanvas::canvas_light_set_transform(RID p_light, const Transform2D &p_transform) {
  1298. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1299. ERR_FAIL_COND(!clight);
  1300. clight->xform = p_transform;
  1301. }
  1302. void VisualServerCanvas::canvas_light_set_texture(RID p_light, RID p_texture) {
  1303. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1304. ERR_FAIL_COND(!clight);
  1305. clight->texture = p_texture;
  1306. }
  1307. void VisualServerCanvas::canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset) {
  1308. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1309. ERR_FAIL_COND(!clight);
  1310. clight->texture_offset = p_offset;
  1311. }
  1312. void VisualServerCanvas::canvas_light_set_color(RID p_light, const Color &p_color) {
  1313. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1314. ERR_FAIL_COND(!clight);
  1315. clight->color = p_color;
  1316. }
  1317. void VisualServerCanvas::canvas_light_set_height(RID p_light, float p_height) {
  1318. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1319. ERR_FAIL_COND(!clight);
  1320. clight->height = p_height;
  1321. }
  1322. void VisualServerCanvas::canvas_light_set_energy(RID p_light, float p_energy) {
  1323. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1324. ERR_FAIL_COND(!clight);
  1325. clight->energy = p_energy;
  1326. }
  1327. void VisualServerCanvas::canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z) {
  1328. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1329. ERR_FAIL_COND(!clight);
  1330. clight->z_min = p_min_z;
  1331. clight->z_max = p_max_z;
  1332. }
  1333. void VisualServerCanvas::canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer) {
  1334. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1335. ERR_FAIL_COND(!clight);
  1336. clight->layer_max = p_max_layer;
  1337. clight->layer_min = p_min_layer;
  1338. }
  1339. void VisualServerCanvas::canvas_light_set_item_cull_mask(RID p_light, int p_mask) {
  1340. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1341. ERR_FAIL_COND(!clight);
  1342. clight->item_mask = p_mask;
  1343. }
  1344. void VisualServerCanvas::canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask) {
  1345. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1346. ERR_FAIL_COND(!clight);
  1347. clight->item_shadow_mask = p_mask;
  1348. }
  1349. void VisualServerCanvas::canvas_light_set_mode(RID p_light, VS::CanvasLightMode p_mode) {
  1350. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1351. ERR_FAIL_COND(!clight);
  1352. clight->mode = p_mode;
  1353. }
  1354. void VisualServerCanvas::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled) {
  1355. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1356. ERR_FAIL_COND(!clight);
  1357. if (clight->shadow_buffer.is_valid() == p_enabled) {
  1358. return;
  1359. }
  1360. if (p_enabled) {
  1361. clight->shadow_buffer = VSG::storage->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  1362. } else {
  1363. VSG::storage->free(clight->shadow_buffer);
  1364. clight->shadow_buffer = RID();
  1365. }
  1366. }
  1367. void VisualServerCanvas::canvas_light_set_shadow_buffer_size(RID p_light, int p_size) {
  1368. ERR_FAIL_COND(p_size < 32 || p_size > 16384);
  1369. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1370. ERR_FAIL_COND(!clight);
  1371. int new_size = next_power_of_2(p_size);
  1372. if (new_size == clight->shadow_buffer_size) {
  1373. return;
  1374. }
  1375. clight->shadow_buffer_size = next_power_of_2(p_size);
  1376. if (clight->shadow_buffer.is_valid()) {
  1377. VSG::storage->free(clight->shadow_buffer);
  1378. clight->shadow_buffer = VSG::storage->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  1379. }
  1380. }
  1381. void VisualServerCanvas::canvas_light_set_shadow_gradient_length(RID p_light, float p_length) {
  1382. ERR_FAIL_COND(p_length < 0);
  1383. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1384. ERR_FAIL_COND(!clight);
  1385. clight->shadow_gradient_length = p_length;
  1386. }
  1387. void VisualServerCanvas::canvas_light_set_shadow_filter(RID p_light, VS::CanvasLightShadowFilter p_filter) {
  1388. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1389. ERR_FAIL_COND(!clight);
  1390. clight->shadow_filter = p_filter;
  1391. }
  1392. void VisualServerCanvas::canvas_light_set_shadow_color(RID p_light, const Color &p_color) {
  1393. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1394. ERR_FAIL_COND(!clight);
  1395. clight->shadow_color = p_color;
  1396. }
  1397. void VisualServerCanvas::canvas_light_set_shadow_smooth(RID p_light, float p_smooth) {
  1398. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1399. ERR_FAIL_COND(!clight);
  1400. clight->shadow_smooth = p_smooth;
  1401. }
  1402. RID VisualServerCanvas::canvas_light_occluder_create() {
  1403. RasterizerCanvas::LightOccluderInstance *occluder = memnew(RasterizerCanvas::LightOccluderInstance);
  1404. return canvas_light_occluder_owner.make_rid(occluder);
  1405. }
  1406. void VisualServerCanvas::canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) {
  1407. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1408. ERR_FAIL_COND(!occluder);
  1409. if (occluder->canvas.is_valid()) {
  1410. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1411. canvas->occluders.erase(occluder);
  1412. }
  1413. if (!canvas_owner.owns(p_canvas)) {
  1414. p_canvas = RID();
  1415. }
  1416. occluder->canvas = p_canvas;
  1417. if (occluder->canvas.is_valid()) {
  1418. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1419. canvas->occluders.insert(occluder);
  1420. }
  1421. }
  1422. void VisualServerCanvas::canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled) {
  1423. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1424. ERR_FAIL_COND(!occluder);
  1425. occluder->enabled = p_enabled;
  1426. }
  1427. void VisualServerCanvas::canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon) {
  1428. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1429. ERR_FAIL_COND(!occluder);
  1430. if (occluder->polygon.is_valid()) {
  1431. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  1432. if (occluder_poly) {
  1433. occluder_poly->owners.erase(occluder);
  1434. }
  1435. }
  1436. occluder->polygon = p_polygon;
  1437. occluder->polygon_buffer = RID();
  1438. if (occluder->polygon.is_valid()) {
  1439. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  1440. if (!occluder_poly) {
  1441. occluder->polygon = RID();
  1442. ERR_FAIL_COND(!occluder_poly);
  1443. } else {
  1444. occluder_poly->owners.insert(occluder);
  1445. occluder->polygon_buffer = occluder_poly->occluder;
  1446. occluder->aabb_cache = occluder_poly->aabb;
  1447. occluder->cull_cache = occluder_poly->cull_mode;
  1448. }
  1449. }
  1450. }
  1451. void VisualServerCanvas::canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform) {
  1452. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1453. ERR_FAIL_COND(!occluder);
  1454. occluder->xform = p_xform;
  1455. }
  1456. void VisualServerCanvas::canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask) {
  1457. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1458. ERR_FAIL_COND(!occluder);
  1459. occluder->light_mask = p_mask;
  1460. }
  1461. RID VisualServerCanvas::canvas_occluder_polygon_create() {
  1462. LightOccluderPolygon *occluder_poly = memnew(LightOccluderPolygon);
  1463. occluder_poly->occluder = VSG::storage->canvas_light_occluder_create();
  1464. return canvas_light_occluder_polygon_owner.make_rid(occluder_poly);
  1465. }
  1466. void VisualServerCanvas::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape, bool p_closed) {
  1467. if (p_shape.size() < 3) {
  1468. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon, p_shape);
  1469. return;
  1470. }
  1471. PoolVector<Vector2> lines;
  1472. int lc = p_shape.size() * 2;
  1473. lines.resize(lc - (p_closed ? 0 : 2));
  1474. {
  1475. PoolVector<Vector2>::Write w = lines.write();
  1476. PoolVector<Vector2>::Read r = p_shape.read();
  1477. int max = lc / 2;
  1478. if (!p_closed) {
  1479. max--;
  1480. }
  1481. for (int i = 0; i < max; i++) {
  1482. Vector2 a = r[i];
  1483. Vector2 b = r[(i + 1) % (lc / 2)];
  1484. w[i * 2 + 0] = a;
  1485. w[i * 2 + 1] = b;
  1486. }
  1487. }
  1488. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon, lines);
  1489. }
  1490. void VisualServerCanvas::canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape) {
  1491. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  1492. ERR_FAIL_COND(!occluder_poly);
  1493. ERR_FAIL_COND(p_shape.size() & 1);
  1494. int lc = p_shape.size();
  1495. occluder_poly->aabb = Rect2();
  1496. {
  1497. PoolVector<Vector2>::Read r = p_shape.read();
  1498. for (int i = 0; i < lc; i++) {
  1499. if (i == 0) {
  1500. occluder_poly->aabb.position = r[i];
  1501. } else {
  1502. occluder_poly->aabb.expand_to(r[i]);
  1503. }
  1504. }
  1505. }
  1506. VSG::storage->canvas_light_occluder_set_polylines(occluder_poly->occluder, p_shape);
  1507. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = occluder_poly->owners.front(); E; E = E->next()) {
  1508. E->get()->aabb_cache = occluder_poly->aabb;
  1509. }
  1510. }
  1511. void VisualServerCanvas::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, VS::CanvasOccluderPolygonCullMode p_mode) {
  1512. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  1513. ERR_FAIL_COND(!occluder_poly);
  1514. occluder_poly->cull_mode = p_mode;
  1515. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = occluder_poly->owners.front(); E; E = E->next()) {
  1516. E->get()->cull_cache = p_mode;
  1517. }
  1518. }
  1519. bool VisualServerCanvas::free(RID p_rid) {
  1520. if (canvas_owner.owns(p_rid)) {
  1521. Canvas *canvas = canvas_owner.get(p_rid);
  1522. ERR_FAIL_COND_V(!canvas, false);
  1523. while (canvas->viewports.size()) {
  1524. VisualServerViewport::Viewport *vp = VSG::viewport->viewport_owner.get(canvas->viewports.front()->get());
  1525. ERR_FAIL_COND_V(!vp, true);
  1526. Map<RID, VisualServerViewport::Viewport::CanvasData>::Element *E = vp->canvas_map.find(p_rid);
  1527. ERR_FAIL_COND_V(!E, true);
  1528. vp->canvas_map.erase(p_rid);
  1529. canvas->viewports.erase(canvas->viewports.front());
  1530. }
  1531. for (int i = 0; i < canvas->child_items.size(); i++) {
  1532. canvas->child_items[i].item->parent = RID();
  1533. }
  1534. for (Set<RasterizerCanvas::Light *>::Element *E = canvas->lights.front(); E; E = E->next()) {
  1535. E->get()->canvas = RID();
  1536. }
  1537. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = canvas->occluders.front(); E; E = E->next()) {
  1538. E->get()->canvas = RID();
  1539. }
  1540. canvas_owner.free(p_rid);
  1541. memdelete(canvas);
  1542. } else if (canvas_item_owner.owns(p_rid)) {
  1543. Item *canvas_item = canvas_item_owner.get(p_rid);
  1544. ERR_FAIL_COND_V(!canvas_item, true);
  1545. _make_bound_dirty(canvas_item);
  1546. if (canvas_item->parent.is_valid()) {
  1547. if (canvas_owner.owns(canvas_item->parent)) {
  1548. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  1549. canvas->erase_item(canvas_item);
  1550. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  1551. Item *item_owner = canvas_item_owner.get(canvas_item->parent);
  1552. item_owner->child_items.erase(canvas_item);
  1553. if (item_owner->sort_y) {
  1554. _mark_ysort_dirty(item_owner, canvas_item_owner);
  1555. }
  1556. _check_bound_integrity(item_owner);
  1557. }
  1558. }
  1559. for (int i = 0; i < canvas_item->child_items.size(); i++) {
  1560. canvas_item->child_items[i]->parent = RID();
  1561. }
  1562. /*
  1563. if (canvas_item->material) {
  1564. canvas_item->material->owners.erase(canvas_item);
  1565. }
  1566. */
  1567. canvas_item_owner.free(p_rid);
  1568. memdelete(canvas_item);
  1569. } else if (canvas_light_owner.owns(p_rid)) {
  1570. RasterizerCanvas::Light *canvas_light = canvas_light_owner.get(p_rid);
  1571. ERR_FAIL_COND_V(!canvas_light, true);
  1572. if (canvas_light->canvas.is_valid()) {
  1573. Canvas *canvas = canvas_owner.get(canvas_light->canvas);
  1574. if (canvas) {
  1575. canvas->lights.erase(canvas_light);
  1576. }
  1577. }
  1578. if (canvas_light->shadow_buffer.is_valid()) {
  1579. VSG::storage->free(canvas_light->shadow_buffer);
  1580. }
  1581. VSG::canvas_render->light_internal_free(canvas_light->light_internal);
  1582. canvas_light_owner.free(p_rid);
  1583. memdelete(canvas_light);
  1584. } else if (canvas_light_occluder_owner.owns(p_rid)) {
  1585. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_rid);
  1586. ERR_FAIL_COND_V(!occluder, true);
  1587. if (occluder->polygon.is_valid()) {
  1588. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  1589. if (occluder_poly) {
  1590. occluder_poly->owners.erase(occluder);
  1591. }
  1592. }
  1593. if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
  1594. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1595. canvas->occluders.erase(occluder);
  1596. }
  1597. canvas_light_occluder_owner.free(p_rid);
  1598. memdelete(occluder);
  1599. } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
  1600. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_rid);
  1601. ERR_FAIL_COND_V(!occluder_poly, true);
  1602. VSG::storage->free(occluder_poly->occluder);
  1603. while (occluder_poly->owners.size()) {
  1604. occluder_poly->owners.front()->get()->polygon = RID();
  1605. occluder_poly->owners.erase(occluder_poly->owners.front());
  1606. }
  1607. canvas_light_occluder_polygon_owner.free(p_rid);
  1608. memdelete(occluder_poly);
  1609. } else {
  1610. return false;
  1611. }
  1612. return true;
  1613. }
  1614. #ifdef VISUAL_SERVER_CANVAS_CHECK_BOUNDS
  1615. // Debugging function to check that the bound dirty flags in the tree make sense.
  1616. // Any item that has is dirty, all parents should be dirty up to the root
  1617. // (except in hidden branches, which are not kept track of for performance reasons).
  1618. bool VisualServerCanvas::_check_bound_integrity(const Item *p_item) {
  1619. while (p_item) {
  1620. if (canvas_item_owner.owns(p_item->parent)) {
  1621. p_item = canvas_item_owner.get(p_item->parent);
  1622. } else {
  1623. return _check_bound_integrity_down(p_item, p_item->bound_dirty);
  1624. }
  1625. }
  1626. return true;
  1627. }
  1628. bool VisualServerCanvas::_check_bound_integrity_down(const Item *p_item, bool p_bound_dirty) {
  1629. // don't care about integrity into invisible branches
  1630. if (!p_item->visible) {
  1631. return true;
  1632. }
  1633. if (p_item->bound_dirty) {
  1634. if (!p_bound_dirty) {
  1635. _print_tree(p_item);
  1636. ERR_PRINT("bound integrity check failed");
  1637. return false;
  1638. }
  1639. }
  1640. // go through children
  1641. int child_item_count = p_item->child_items.size();
  1642. Item *const *child_items = p_item->child_items.ptr();
  1643. for (int n = 0; n < child_item_count; n++) {
  1644. if (!_check_bound_integrity_down(child_items[n], p_bound_dirty)) {
  1645. return false;
  1646. }
  1647. }
  1648. return true;
  1649. }
  1650. void VisualServerCanvas::_print_tree(const Item *p_item) {
  1651. const Item *highlight = p_item;
  1652. while (p_item) {
  1653. if (canvas_item_owner.owns(p_item->parent)) {
  1654. p_item = canvas_item_owner.get(p_item->parent);
  1655. } else {
  1656. _print_tree_down(0, 0, p_item, highlight);
  1657. return;
  1658. }
  1659. }
  1660. }
  1661. void VisualServerCanvas::_print_tree_down(int p_child_id, int p_depth, const Item *p_item, const Item *p_highlight, bool p_hidden) {
  1662. String sz;
  1663. for (int n = 0; n < p_depth; n++) {
  1664. sz += "\t";
  1665. }
  1666. if (p_item == p_highlight) {
  1667. sz += "* ";
  1668. }
  1669. sz += itos(p_child_id) + " ";
  1670. #ifdef VISUAL_SERVER_CANVAS_DEBUG_ITEM_NAMES
  1671. sz += p_item->name + "\t";
  1672. #endif
  1673. sz += String(Variant(p_item->global_rect_cache)) + " ";
  1674. if (!p_item->visible) {
  1675. sz += "(H) ";
  1676. p_hidden = true;
  1677. } else if (p_hidden) {
  1678. sz += "(HI) ";
  1679. }
  1680. if (p_item->bound_dirty) {
  1681. sz += "(dirty) ";
  1682. }
  1683. if (p_item->parent == RID()) {
  1684. sz += "(parent NULL) ";
  1685. }
  1686. print_line(sz);
  1687. // go through children
  1688. int child_item_count = p_item->child_items.size();
  1689. Item *const *child_items = p_item->child_items.ptr();
  1690. for (int n = 0; n < child_item_count; n++) {
  1691. _print_tree_down(n, p_depth + 1, child_items[n], p_highlight, p_hidden);
  1692. }
  1693. }
  1694. #endif
  1695. VisualServerCanvas::VisualServerCanvas() {
  1696. z_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));
  1697. z_last_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));
  1698. disable_scale = false;
  1699. int mode = GLOBAL_DEF("rendering/2d/options/culling_mode", 1);
  1700. ProjectSettings::get_singleton()->set_custom_property_info("rendering/2d/options/culling_mode", PropertyInfo(Variant::INT, "rendering/2d/options/culling_mode", PROPERTY_HINT_ENUM, "Item,Node"));
  1701. switch (mode) {
  1702. default: {
  1703. _canvas_cull_mode = CANVAS_CULL_MODE_NODE;
  1704. } break;
  1705. case 0: {
  1706. _canvas_cull_mode = CANVAS_CULL_MODE_ITEM;
  1707. } break;
  1708. }
  1709. }
  1710. VisualServerCanvas::~VisualServerCanvas() {
  1711. memfree(z_list);
  1712. memfree(z_last_list);
  1713. }