MemberPreviewUpdater.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using ChunkyImageLib;
  6. using ChunkyImageLib.DataHolders;
  7. using ChunkyImageLib.Operations;
  8. using PixiEditor.AvaloniaUI.Helpers;
  9. using PixiEditor.AvaloniaUI.Models.DocumentModels;
  10. using PixiEditor.AvaloniaUI.Models.Handlers;
  11. using PixiEditor.AvaloniaUI.Models.Rendering.RenderInfos;
  12. using PixiEditor.AvaloniaUI.ViewModels.Document;
  13. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  14. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  15. using PixiEditor.ChangeableDocument.Rendering;
  16. using PixiEditor.DrawingApi.Core.Numerics;
  17. using PixiEditor.DrawingApi.Core.Surface;
  18. using PixiEditor.DrawingApi.Core.Surface.ImageData;
  19. using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
  20. using PixiEditor.Numerics;
  21. namespace PixiEditor.AvaloniaUI.Models.Rendering;
  22. internal class MemberPreviewUpdater
  23. {
  24. private const float smoothingThreshold = 1.5f;
  25. private readonly IDocument doc;
  26. private readonly DocumentInternalParts internals;
  27. private Dictionary<Guid, RectI> lastMainPreviewTightBounds = new();
  28. private Dictionary<Guid, RectI> lastMaskPreviewTightBounds = new();
  29. private Dictionary<Guid, AffectedArea> mainPreviewAreasAccumulator = new();
  30. private Dictionary<Guid, AffectedArea> maskPreviewAreasAccumulator = new();
  31. private static readonly Paint SmoothReplacingPaint = new()
  32. {
  33. BlendMode = BlendMode.Src, FilterQuality = FilterQuality.Medium, IsAntiAliased = true
  34. };
  35. private static readonly Paint ReplacingPaint = new() { BlendMode = BlendMode.Src };
  36. private static readonly Paint ClearPaint =
  37. new() { BlendMode = BlendMode.Src, Color = DrawingApi.Core.ColorsImpl.Colors.Transparent };
  38. public MemberPreviewUpdater(IDocument doc, DocumentInternalParts internals)
  39. {
  40. this.doc = doc;
  41. this.internals = internals;
  42. }
  43. /// <summary>
  44. /// Don't call this outside ActionAccumulator
  45. /// </summary>
  46. public async Task<List<IRenderInfo>> UpdateGatheredChunks
  47. (AffectedAreasGatherer chunkGatherer, bool rerenderPreviews)
  48. {
  49. AddAreasToAccumulator(chunkGatherer);
  50. if (!rerenderPreviews)
  51. return new List<IRenderInfo>();
  52. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?>? changedMainPreviewBounds = null;
  53. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?>? changedMaskPreviewBounds = null;
  54. int atFrame = doc.AnimationHandler.ActiveFrameBindable;
  55. await Task.Run(() =>
  56. {
  57. changedMainPreviewBounds = FindChangedTightBounds(atFrame, false);
  58. changedMaskPreviewBounds = FindChangedTightBounds(atFrame, true);
  59. }).ConfigureAwait(true);
  60. RecreatePreviewBitmaps(changedMainPreviewBounds!, changedMaskPreviewBounds!);
  61. var renderInfos = await Task.Run(() => Render(changedMainPreviewBounds!, changedMaskPreviewBounds))
  62. .ConfigureAwait(true);
  63. CleanupUnusedTightBounds();
  64. foreach (var a in changedMainPreviewBounds)
  65. {
  66. if (a.Value is not null)
  67. lastMainPreviewTightBounds[a.Key] = a.Value.Value.tightBounds;
  68. else
  69. lastMainPreviewTightBounds.Remove(a.Key);
  70. }
  71. foreach (var a in changedMaskPreviewBounds)
  72. {
  73. if (a.Value is not null)
  74. lastMaskPreviewTightBounds[a.Key] = a.Value.Value.tightBounds;
  75. else
  76. lastMaskPreviewTightBounds.Remove(a.Key);
  77. }
  78. return renderInfos;
  79. }
  80. /// <summary>
  81. /// Don't call this outside ActionAccumulator
  82. /// </summary>
  83. public List<IRenderInfo> UpdateGatheredChunksSync
  84. (AffectedAreasGatherer chunkGatherer, bool rerenderPreviews)
  85. {
  86. AddAreasToAccumulator(chunkGatherer);
  87. if (!rerenderPreviews)
  88. return new List<IRenderInfo>();
  89. int frame = doc.AnimationHandler.ActiveFrameBindable;
  90. var changedMainPreviewBounds = FindChangedTightBounds(frame, false);
  91. var changedMaskPreviewBounds = FindChangedTightBounds(frame, true);
  92. RecreatePreviewBitmaps(changedMainPreviewBounds, changedMaskPreviewBounds);
  93. var renderInfos = Render(changedMainPreviewBounds, changedMaskPreviewBounds);
  94. CleanupUnusedTightBounds();
  95. foreach (var a in changedMainPreviewBounds)
  96. {
  97. if (a.Value is not null)
  98. lastMainPreviewTightBounds[a.Key] = a.Value.Value.tightBounds;
  99. }
  100. foreach (var a in changedMaskPreviewBounds)
  101. {
  102. if (a.Value is not null)
  103. lastMaskPreviewTightBounds[a.Key] = a.Value.Value.tightBounds;
  104. }
  105. return renderInfos;
  106. }
  107. /// <summary>
  108. /// Cleans up <see cref="lastMainPreviewTightBounds"/> and <see cref="lastMaskPreviewTightBounds"/> to get rid of tight bounds that belonged to now deleted layers
  109. /// </summary>
  110. private void CleanupUnusedTightBounds()
  111. {
  112. Dictionary<Guid, RectI> clearedLastMainPreviewTightBounds = new Dictionary<Guid, RectI>();
  113. Dictionary<Guid, RectI> clearedLastMaskPreviewTightBounds = new Dictionary<Guid, RectI>();
  114. internals.Tracker.Document.ForEveryReadonlyMember(member =>
  115. {
  116. if (lastMainPreviewTightBounds.ContainsKey(member.Id))
  117. clearedLastMainPreviewTightBounds.Add(member.Id, lastMainPreviewTightBounds[member.Id]);
  118. if (lastMaskPreviewTightBounds.ContainsKey(member.Id))
  119. clearedLastMaskPreviewTightBounds.Add(member.Id, lastMaskPreviewTightBounds[member.Id]);
  120. });
  121. lastMainPreviewTightBounds = clearedLastMainPreviewTightBounds;
  122. lastMaskPreviewTightBounds = clearedLastMaskPreviewTightBounds;
  123. }
  124. /// <summary>
  125. /// Unions the areas inside <see cref="mainPreviewAreasAccumulator"/> and <see cref="maskPreviewAreasAccumulator"/> with the newly updated areas
  126. /// </summary>
  127. private void AddAreasToAccumulator(AffectedAreasGatherer areasGatherer)
  128. {
  129. AddAreas(areasGatherer.ImagePreviewAreas, mainPreviewAreasAccumulator);
  130. AddAreas(areasGatherer.MaskPreviewAreas, maskPreviewAreasAccumulator);
  131. }
  132. private static void AddAreas(Dictionary<Guid, AffectedArea> from, Dictionary<Guid, AffectedArea> to)
  133. {
  134. foreach ((Guid guid, AffectedArea area) in from)
  135. {
  136. if (!to.ContainsKey(guid))
  137. to[guid] = new AffectedArea();
  138. var toArea = to[guid];
  139. toArea.UnionWith(area);
  140. to[guid] = toArea;
  141. }
  142. }
  143. /// <summary>
  144. /// Looks at the accumulated areas and determines which members need to have their preview bitmaps resized or deleted
  145. /// </summary>
  146. private Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> FindChangedTightBounds(int atFrame, bool forMasks)
  147. {
  148. // VecI? == null stands for "layer is empty, the preview needs to be deleted"
  149. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> newPreviewBitmapSizes = new();
  150. var targetAreas = forMasks ? maskPreviewAreasAccumulator : mainPreviewAreasAccumulator;
  151. var targetLastBounds = forMasks ? lastMaskPreviewTightBounds : lastMainPreviewTightBounds;
  152. foreach (var (guid, area) in targetAreas)
  153. {
  154. var member = internals.Tracker.Document.FindMember(guid);
  155. if (member is null)
  156. continue;
  157. if (forMasks && member.Mask.Value is null)
  158. {
  159. newPreviewBitmapSizes.Add(guid, null);
  160. continue;
  161. }
  162. RectI? tightBounds = GetOrFindMemberTightBounds(member, atFrame, area, forMasks);
  163. RectI? maybeLastBounds = targetLastBounds.TryGetValue(guid, out RectI lastBounds) ? lastBounds : null;
  164. if (tightBounds == maybeLastBounds)
  165. continue;
  166. if (tightBounds is null)
  167. {
  168. newPreviewBitmapSizes.Add(guid, null);
  169. continue;
  170. }
  171. VecI previewSize = StructureHelpers.CalculatePreviewSize(tightBounds.Value.Size);
  172. newPreviewBitmapSizes.Add(guid, (previewSize, tightBounds.Value));
  173. }
  174. return newPreviewBitmapSizes;
  175. }
  176. /// <summary>
  177. /// Recreates the preview bitmaps using the passed sizes (or deletes them when new size is null)
  178. /// </summary>
  179. private void RecreatePreviewBitmaps(
  180. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> newPreviewSizes,
  181. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> newMaskSizes)
  182. {
  183. // update previews
  184. foreach (var (guid, newSize) in newPreviewSizes)
  185. {
  186. IStructureMemberHandler member = doc.StructureHelper.FindOrThrow(guid);
  187. if (newSize is null)
  188. {
  189. member.PreviewSurface?.Dispose();
  190. member.PreviewSurface = null;
  191. }
  192. else
  193. {
  194. if (member.PreviewSurface is not null && member.PreviewSurface.Size.X == newSize.Value.previewSize.X &&
  195. member.PreviewSurface.Size.Y == newSize.Value.previewSize.Y)
  196. {
  197. member.PreviewSurface!.DrawingSurface.Canvas.Clear();
  198. }
  199. else
  200. {
  201. member.PreviewSurface?.Dispose();
  202. member.PreviewSurface = new Surface(newSize.Value.previewSize);
  203. }
  204. }
  205. //TODO: Make sure PreviewBitmap implementation raises PropertyChanged
  206. //member.OnPropertyChanged(nameof(member.PreviewBitmap));
  207. }
  208. // update masks
  209. foreach (var (guid, newSize) in newMaskSizes)
  210. {
  211. IStructureMemberHandler member = doc.StructureHelper.FindOrThrow(guid);
  212. member.MaskPreviewSurface?.Dispose();
  213. if (newSize is null)
  214. {
  215. member.MaskPreviewSurface = null;
  216. }
  217. else
  218. {
  219. member.MaskPreviewSurface = new Surface(newSize.Value.previewSize); // TODO: premul bgra8888 was here
  220. }
  221. //TODO: Make sure MaskPreviewBitmap implementation raises PropertyChanged
  222. //member.OnPropertyChanged(nameof(member.MaskPreviewBitmap));
  223. }
  224. }
  225. /// <summary>
  226. /// Returns the previosly known committed tight bounds if there are no reasons to believe they have changed (based on the passed <paramref name="currentlyAffectedArea"/>).
  227. /// Otherwise, calculates the new bounds via <see cref="FindLayerTightBounds"/> and returns them.
  228. /// </summary>
  229. private RectI? GetOrFindMemberTightBounds(IReadOnlyStructureNode member, int atFrame,
  230. AffectedArea currentlyAffectedArea, bool forMask)
  231. {
  232. if (forMask && member.Mask.Value is null)
  233. throw new InvalidOperationException();
  234. RectI? prevTightBounds = null;
  235. var targetLastCollection = forMask ? lastMaskPreviewTightBounds : lastMainPreviewTightBounds;
  236. if (targetLastCollection.TryGetValue(member.Id, out RectI tightBounds))
  237. prevTightBounds = tightBounds;
  238. if (prevTightBounds is not null && currentlyAffectedArea.GlobalArea is not null &&
  239. prevTightBounds.Value.ContainsExclusive(currentlyAffectedArea.GlobalArea.Value))
  240. {
  241. // if the affected area is fully inside the previous tight bounds, the tight bounds couldn't possibly have changed
  242. return prevTightBounds.Value;
  243. }
  244. return member switch
  245. {
  246. IReadOnlyLayerNode layer => FindLayerTightBounds(layer, atFrame, forMask),
  247. IReadOnlyFolderNode folder => FindFolderTightBounds(folder, atFrame, forMask),
  248. _ => throw new ArgumentOutOfRangeException()
  249. };
  250. }
  251. /// <summary>
  252. /// Finds the current committed tight bounds for a layer.
  253. /// </summary>
  254. private RectI? FindLayerTightBounds(IReadOnlyLayerNode layer, int frame, bool forMask)
  255. {
  256. if (layer.Mask.Value is null && forMask)
  257. throw new InvalidOperationException();
  258. if (layer.Mask.Value is not null && forMask)
  259. return FindImageTightBoundsFast(layer.Mask.Value);
  260. if (layer is IReadOnlyImageNode raster)
  261. {
  262. return FindImageTightBoundsFast(raster.GetLayerImageAtFrame(frame));
  263. }
  264. return layer.GetTightBounds(frame);
  265. }
  266. /// <summary>
  267. /// Finds the current committed tight bounds for a folder recursively.
  268. /// </summary>
  269. private RectI? FindFolderTightBounds(IReadOnlyFolderNode folder, int frame, bool forMask)
  270. {
  271. if (forMask)
  272. {
  273. if (folder.Mask.Value is null)
  274. throw new InvalidOperationException();
  275. return FindImageTightBoundsFast(folder.Mask.Value);
  276. }
  277. /*RectI? combinedBounds = null;
  278. foreach (var child in folder.Children)
  279. {
  280. RectI? curBounds = null;
  281. if (child is IReadOnlyLayerNode childLayer)
  282. curBounds = FindLayerTightBounds(childLayer, frame, false);
  283. else if (child is IReadOnlyFolderNode childFolder)
  284. curBounds = FindFolderTightBounds(childFolder, frame, false);
  285. if (combinedBounds is null)
  286. combinedBounds = curBounds;
  287. else if (curBounds is not null)
  288. combinedBounds = combinedBounds.Value.Union(curBounds.Value);
  289. }
  290. return combinedBounds;*/
  291. return folder.GetTightBounds(frame);
  292. }
  293. /// <summary>
  294. /// Finds the current committed tight bounds for an image in a reasonably efficient way.
  295. /// Looks at the low-res chunks for large images, meaning the resulting bounds aren't 100% precise.
  296. /// </summary>
  297. private RectI? FindImageTightBoundsFast(IReadOnlyChunkyImage targetImage)
  298. {
  299. RectI? bounds = targetImage.FindChunkAlignedCommittedBounds();
  300. if (bounds is null)
  301. return null;
  302. int biggest = bounds.Value.Size.LongestAxis;
  303. ChunkResolution resolution = biggest switch
  304. {
  305. > ChunkyImage.FullChunkSize * 9 => ChunkResolution.Eighth,
  306. > ChunkyImage.FullChunkSize * 5 => ChunkResolution.Quarter,
  307. > ChunkyImage.FullChunkSize * 3 => ChunkResolution.Half,
  308. _ => ChunkResolution.Full,
  309. };
  310. return targetImage.FindTightCommittedBounds(resolution);
  311. }
  312. /// <summary>
  313. /// Re-renders changed chunks using <see cref="mainPreviewAreasAccumulator"/> and <see cref="maskPreviewAreasAccumulator"/> along with the passed lists of bitmaps that need full re-render.
  314. /// </summary>
  315. private List<IRenderInfo> Render(
  316. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> recreatedMainPreviewSizes,
  317. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> recreatedMaskPreviewSizes)
  318. {
  319. List<IRenderInfo> infos = new();
  320. var (mainPreviewChunksToRerender, maskPreviewChunksToRerender) = GetChunksToRerenderAndResetAccumulator();
  321. RenderWholeCanvasPreview(mainPreviewChunksToRerender, maskPreviewChunksToRerender, infos);
  322. RenderMainPreviews(mainPreviewChunksToRerender, recreatedMainPreviewSizes, infos);
  323. RenderMaskPreviews(maskPreviewChunksToRerender, recreatedMaskPreviewSizes, infos);
  324. RenderNodePreviews(infos);
  325. return infos;
  326. // asynchronously re-render changed chunks (where tight bounds didn't change) or the whole preview image (where the tight bounds did change)
  327. // don't forget to get rid of the bitmap recreation code in DocumentUpdater
  328. }
  329. private (Dictionary<Guid, AffectedArea> main, Dictionary<Guid, AffectedArea> mask)
  330. GetChunksToRerenderAndResetAccumulator()
  331. {
  332. var result = (mainPreviewPostponedChunks: mainPreviewAreasAccumulator,
  333. maskPreviewPostponedChunks: maskPreviewAreasAccumulator);
  334. mainPreviewAreasAccumulator = new();
  335. maskPreviewAreasAccumulator = new();
  336. return result;
  337. }
  338. /// <summary>
  339. /// Re-renders the preview of the whole canvas which is shown as the tab icon
  340. /// </summary>
  341. private void RenderWholeCanvasPreview(Dictionary<Guid, AffectedArea> mainPreviewChunks,
  342. Dictionary<Guid, AffectedArea> maskPreviewChunks, List<IRenderInfo> infos)
  343. {
  344. var cumulative = mainPreviewChunks
  345. .Concat(maskPreviewChunks)
  346. .Aggregate(new AffectedArea(), (set, pair) =>
  347. {
  348. set.UnionWith(pair.Value);
  349. return set;
  350. });
  351. if (cumulative.GlobalArea is null)
  352. return;
  353. var previewSize = StructureHelpers.CalculatePreviewSize(internals.Tracker.Document.Size);
  354. float scaling = (float)previewSize.X / doc.SizeBindable.X;
  355. bool somethingChanged = false;
  356. foreach (var chunkPos in cumulative.Chunks)
  357. {
  358. somethingChanged = true;
  359. ChunkResolution resolution = scaling switch
  360. {
  361. > 1 / 2f => ChunkResolution.Full,
  362. > 1 / 4f => ChunkResolution.Half,
  363. > 1 / 8f => ChunkResolution.Quarter,
  364. _ => ChunkResolution.Eighth,
  365. };
  366. var pos = chunkPos * resolution.PixelSize();
  367. var rendered = doc.Renderer.RenderChunk(chunkPos, resolution, doc.AnimationHandler.ActiveFrameTime);
  368. doc.PreviewSurface.DrawingSurface.Canvas.Save();
  369. doc.PreviewSurface.DrawingSurface.Canvas.Scale(scaling);
  370. doc.PreviewSurface.DrawingSurface.Canvas.ClipRect((RectD)cumulative.GlobalArea);
  371. doc.PreviewSurface.DrawingSurface.Canvas.Scale(1 / (float)resolution.Multiplier());
  372. if (rendered.IsT1)
  373. {
  374. doc.PreviewSurface.DrawingSurface.Canvas.DrawRect(pos.X, pos.Y, resolution.PixelSize(),
  375. resolution.PixelSize(), ClearPaint);
  376. }
  377. else if (rendered.IsT0)
  378. {
  379. using var renderedChunk = rendered.AsT0;
  380. renderedChunk.DrawChunkOn(doc.PreviewSurface.DrawingSurface, pos, SmoothReplacingPaint);
  381. }
  382. doc.PreviewSurface.DrawingSurface.Canvas.Restore();
  383. }
  384. if (somethingChanged)
  385. infos.Add(new CanvasPreviewDirty_RenderInfo());
  386. }
  387. private void RenderMainPreviews(
  388. Dictionary<Guid, AffectedArea> mainPreviewChunks,
  389. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> recreatedPreviewSizes,
  390. List<IRenderInfo> infos)
  391. {
  392. foreach (var guid in mainPreviewChunks.Select(a => a.Key).Concat(recreatedPreviewSizes.Select(a => a.Key)))
  393. {
  394. // find the true affected area
  395. AffectedArea? affArea = null;
  396. RectI? tightBounds = null;
  397. if (mainPreviewChunks.TryGetValue(guid, out AffectedArea areaFromChunks))
  398. affArea = areaFromChunks;
  399. if (recreatedPreviewSizes.TryGetValue(guid, out (VecI _, RectI tightBounds)? value))
  400. {
  401. if (value is null)
  402. continue;
  403. tightBounds = value.Value.tightBounds;
  404. affArea = new AffectedArea(
  405. OperationHelper.FindChunksTouchingRectangle(value.Value.tightBounds, ChunkyImage.FullChunkSize),
  406. value.Value.tightBounds);
  407. }
  408. if (affArea is null || affArea.Value.GlobalArea is null ||
  409. affArea.Value.GlobalArea.Value.IsZeroOrNegativeArea)
  410. continue;
  411. // re-render the area
  412. var memberVM = doc.StructureHelper.Find(guid);
  413. if (memberVM is null || memberVM.PreviewSurface is null)
  414. continue;
  415. if (tightBounds is null)
  416. tightBounds = lastMainPreviewTightBounds[guid];
  417. var member = internals.Tracker.Document.FindMemberOrThrow(guid);
  418. var previewSize = StructureHelpers.CalculatePreviewSize(tightBounds.Value.Size);
  419. float scaling = (float)previewSize.X / tightBounds.Value.Width;
  420. VecI position = tightBounds.Value.Pos;
  421. if (memberVM is ILayerHandler)
  422. {
  423. RenderLayerMainPreview((IReadOnlyLayerNode)member, memberVM, affArea.Value, position, scaling);
  424. if (doc.AnimationHandler.FindKeyFrame(guid, out IKeyFrameHandler? keyFrame))
  425. {
  426. if (keyFrame is IKeyFrameGroupHandler group)
  427. {
  428. foreach (var child in group.Children)
  429. {
  430. if (member is IReadOnlyImageNode rasterLayer)
  431. {
  432. RenderAnimationFramePreview(rasterLayer, child, affArea.Value);
  433. }
  434. }
  435. }
  436. }
  437. infos.Add(new PreviewDirty_RenderInfo(guid));
  438. }
  439. else if (memberVM is IFolderHandler)
  440. {
  441. RenderFolderMainPreview((IReadOnlyFolderNode)member, memberVM, affArea.Value, position, scaling);
  442. infos.Add(new PreviewDirty_RenderInfo(guid));
  443. }
  444. else
  445. {
  446. throw new ArgumentOutOfRangeException();
  447. }
  448. }
  449. }
  450. /// <summary>
  451. /// Re-render the <paramref name="area"/> of the main preview of the <paramref name="memberVM"/> folder
  452. /// </summary>
  453. private void RenderFolderMainPreview(IReadOnlyFolderNode folder, IStructureMemberHandler memberVM,
  454. AffectedArea area,
  455. VecI position, float scaling)
  456. {
  457. memberVM.PreviewSurface.DrawingSurface.Canvas.Save();
  458. memberVM.PreviewSurface.DrawingSurface.Canvas.Scale(scaling);
  459. memberVM.PreviewSurface.DrawingSurface.Canvas.Translate(-position);
  460. memberVM.PreviewSurface.DrawingSurface.Canvas.ClipRect((RectD)area.GlobalArea);
  461. foreach (var chunk in area.Chunks)
  462. {
  463. var pos = chunk * ChunkResolution.Full.PixelSize();
  464. // drawing in full res here is kinda slow
  465. // we could switch to a lower resolution based on (canvas size / preview size) to make it run faster
  466. var contentNode = folder.Content.Connection?.Node;
  467. OneOf<Chunk, EmptyChunk> rendered;
  468. if (contentNode is null)
  469. {
  470. rendered = new EmptyChunk();
  471. }
  472. else
  473. {
  474. rendered = doc.Renderer.RenderChunk(chunk, ChunkResolution.Full, contentNode, doc.AnimationHandler.ActiveFrameBindable);
  475. }
  476. if (rendered.IsT0)
  477. {
  478. memberVM.PreviewSurface.DrawingSurface.Canvas.DrawSurface(rendered.AsT0.Surface.DrawingSurface, pos,
  479. scaling < smoothingThreshold ? SmoothReplacingPaint : ReplacingPaint);
  480. rendered.AsT0.Dispose();
  481. }
  482. else
  483. {
  484. memberVM.PreviewSurface.DrawingSurface.Canvas.DrawRect(pos.X, pos.Y, ChunkResolution.Full.PixelSize(),
  485. ChunkResolution.Full.PixelSize(), ClearPaint);
  486. }
  487. }
  488. memberVM.PreviewSurface.DrawingSurface.Canvas.Restore();
  489. }
  490. /// <summary>
  491. /// Re-render the <paramref name="area"/> of the main preview of the <paramref name="memberVM"/> layer
  492. /// </summary>
  493. private void RenderLayerMainPreview(IReadOnlyLayerNode layer, IStructureMemberHandler memberVM, AffectedArea area,
  494. VecI position, float scaling)
  495. {
  496. memberVM.PreviewSurface.DrawingSurface.Canvas.Save();
  497. memberVM.PreviewSurface.DrawingSurface.Canvas.Scale(scaling);
  498. memberVM.PreviewSurface.DrawingSurface.Canvas.Translate(-position);
  499. memberVM.PreviewSurface.DrawingSurface.Canvas.ClipRect((RectD)area.GlobalArea);
  500. foreach (var chunk in area.Chunks)
  501. {
  502. var pos = chunk * ChunkResolution.Full.PixelSize();
  503. if (layer is not IReadOnlyImageNode raster) return;
  504. IReadOnlyChunkyImage? result = raster.GetLayerImageAtFrame(doc.AnimationHandler.ActiveFrameBindable);
  505. if (!result.DrawCommittedChunkOn(
  506. chunk,
  507. ChunkResolution.Full, memberVM.PreviewSurface.DrawingSurface, pos,
  508. scaling < smoothingThreshold ? SmoothReplacingPaint : ReplacingPaint))
  509. {
  510. memberVM.PreviewSurface.DrawingSurface.Canvas.DrawRect(pos.X, pos.Y, ChunkyImage.FullChunkSize,
  511. ChunkyImage.FullChunkSize, ClearPaint);
  512. }
  513. }
  514. memberVM.PreviewSurface.DrawingSurface.Canvas.Restore();
  515. }
  516. private void RenderAnimationFramePreview(IReadOnlyImageNode node, IKeyFrameHandler keyFrameVM, AffectedArea area)
  517. {
  518. if (keyFrameVM.PreviewSurface is null)
  519. {
  520. keyFrameVM.PreviewSurface =
  521. new Surface(StructureHelpers.CalculatePreviewSize(internals.Tracker.Document.Size));
  522. }
  523. keyFrameVM.PreviewSurface!.DrawingSurface.Canvas.Save();
  524. float scaling = (float)keyFrameVM.PreviewSurface.Size.X / internals.Tracker.Document.Size.X;
  525. keyFrameVM.PreviewSurface.DrawingSurface.Canvas.Scale(scaling);
  526. foreach (var chunk in area.Chunks)
  527. {
  528. var pos = chunk * ChunkResolution.Full.PixelSize();
  529. if (!node.GetLayerImageByKeyFrameGuid(keyFrameVM.Id).DrawCommittedChunkOn(chunk, ChunkResolution.Full,
  530. keyFrameVM.PreviewSurface!.DrawingSurface, pos, ReplacingPaint))
  531. {
  532. keyFrameVM.PreviewSurface!.DrawingSurface.Canvas.DrawRect(pos.X, pos.Y, ChunkyImage.FullChunkSize,
  533. ChunkyImage.FullChunkSize, ClearPaint);
  534. }
  535. }
  536. keyFrameVM.PreviewSurface!.DrawingSurface.Canvas.Restore();
  537. }
  538. private void RenderMaskPreviews(
  539. Dictionary<Guid, AffectedArea> maskPreviewChunks,
  540. Dictionary<Guid, (VecI previewSize, RectI tightBounds)?> recreatedMaskSizes,
  541. List<IRenderInfo> infos)
  542. {
  543. foreach (Guid guid in maskPreviewChunks.Select(a => a.Key).Concat(recreatedMaskSizes.Select(a => a.Key)))
  544. {
  545. // find the true affected area
  546. AffectedArea? affArea = null;
  547. RectI? tightBounds = null;
  548. if (maskPreviewChunks.TryGetValue(guid, out AffectedArea areaFromChunks))
  549. affArea = areaFromChunks;
  550. if (recreatedMaskSizes.TryGetValue(guid, out (VecI _, RectI tightBounds)? value))
  551. {
  552. if (value is null)
  553. continue;
  554. tightBounds = value.Value.tightBounds;
  555. affArea = new AffectedArea(
  556. OperationHelper.FindChunksTouchingRectangle(value.Value.tightBounds, ChunkyImage.FullChunkSize),
  557. value.Value.tightBounds);
  558. }
  559. if (affArea is null || affArea.Value.GlobalArea is null ||
  560. affArea.Value.GlobalArea.Value.IsZeroOrNegativeArea)
  561. continue;
  562. // re-render the area
  563. var memberVM = doc.StructureHelper.Find(guid);
  564. if (memberVM is null || !memberVM.HasMaskBindable || memberVM.MaskPreviewSurface is null)
  565. continue;
  566. if (tightBounds is null)
  567. tightBounds = lastMaskPreviewTightBounds[guid];
  568. var previewSize = StructureHelpers.CalculatePreviewSize(tightBounds.Value.Size);
  569. float scaling = (float)previewSize.X / tightBounds.Value.Width;
  570. VecI position = tightBounds.Value.Pos;
  571. var member = internals.Tracker.Document.FindMemberOrThrow(guid);
  572. memberVM.MaskPreviewSurface!.DrawingSurface.Canvas.Save();
  573. memberVM.MaskPreviewSurface.DrawingSurface.Canvas.Scale(scaling);
  574. memberVM.MaskPreviewSurface.DrawingSurface.Canvas.Translate(-position);
  575. memberVM.MaskPreviewSurface.DrawingSurface.Canvas.ClipRect((RectD)affArea.Value.GlobalArea);
  576. foreach (var chunk in affArea.Value.Chunks)
  577. {
  578. var pos = chunk * ChunkResolution.Full.PixelSize();
  579. member.Mask!.Value.DrawMostUpToDateChunkOn
  580. (chunk, ChunkResolution.Full, memberVM.MaskPreviewSurface.DrawingSurface, pos,
  581. scaling < smoothingThreshold ? SmoothReplacingPaint : ReplacingPaint);
  582. }
  583. memberVM.MaskPreviewSurface.DrawingSurface.Canvas.Restore();
  584. infos.Add(new MaskPreviewDirty_RenderInfo(guid));
  585. }
  586. }
  587. private void RenderNodePreviews(List<IRenderInfo> infos)
  588. {
  589. internals.Tracker.Document.NodeGraph.TryTraverse((node) =>
  590. {
  591. if (node is null)
  592. return;
  593. if (node.CachedResult == null)
  594. {
  595. return;
  596. }
  597. var nodeVm = doc.StructureHelper.FindNode<INodeHandler>(node.Id);
  598. if (nodeVm == null)
  599. {
  600. return;
  601. }
  602. if (nodeVm.ResultPreview == null)
  603. {
  604. nodeVm.ResultPreview =
  605. new Surface(StructureHelpers.CalculatePreviewSize(internals.Tracker.Document.Size));
  606. }
  607. float scalingX = (float)nodeVm.ResultPreview.Size.X / node.CachedResult.Size.X;
  608. float scalingY = (float)nodeVm.ResultPreview.Size.Y / node.CachedResult.Size.Y;
  609. nodeVm.ResultPreview.DrawingSurface.Canvas.Save();
  610. nodeVm.ResultPreview.DrawingSurface.Canvas.Scale(scalingX, scalingY);
  611. RectI region = new RectI(0, 0, node.CachedResult.Size.X, node.CachedResult.Size.Y);
  612. nodeVm.ResultPreview.DrawingSurface.Canvas.DrawSurface(node.CachedResult.DrawingSurface, 0, 0, ReplacingPaint);
  613. nodeVm.ResultPreview.DrawingSurface.Canvas.Restore();
  614. infos.Add(new NodePreviewDirty_RenderInfo(node.Id));
  615. });
  616. }
  617. }