| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Diagnostics; |
| | 5 | | using System.IO; |
| | 6 | | using System.Threading; |
| | 7 | | using Cysharp.Threading.Tasks; |
| | 8 | | using DCL.Helpers; |
| | 9 | | using ThreeDISevenZeroR.UnityGifDecoder; |
| | 10 | | using ThreeDISevenZeroR.UnityGifDecoder.Model; |
| | 11 | | using UnityEngine; |
| | 12 | | using Debug = UnityEngine.Debug; |
| | 13 | |
|
| | 14 | | namespace DCL |
| | 15 | | { |
| | 16 | | public class GifWebRequestException : Exception |
| | 17 | | { |
| | 18 | | public GifWebRequestException(string message) : base(message) { } |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public class GifDecoderProcessor : IGifProcessor |
| | 22 | | { |
| | 23 | | private struct RawImage |
| | 24 | | { |
| | 25 | | public readonly Color32[] colors; |
| | 26 | | public readonly float delay; |
| | 27 | | public RawImage(GifImage gifImage) |
| | 28 | | { |
| | 29 | | //(Kinerius): We have to clone the colors as the pointer gets modifier as we read the gif |
| 270 | 30 | | colors = gifImage.colors.Clone() as Color32[]; |
| 270 | 31 | | delay = gifImage.SafeDelaySeconds; |
| 270 | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| | 35 | | private static bool isRunning; |
| | 36 | | private bool isRunningInternal; |
| 12 | 37 | | private readonly ThrottlingCounter throttlingCounter = new ThrottlingCounter(); |
| | 38 | |
|
| | 39 | | private readonly string url; |
| | 40 | | private readonly Stream stream; |
| | 41 | | private readonly IWebRequestController webRequestController; |
| | 42 | | private GifFrameData[] gifFrameData; |
| | 43 | |
|
| 12 | 44 | | public GifDecoderProcessor(string url, IWebRequestController webRequestController) |
| | 45 | | { |
| 12 | 46 | | this.url = url; |
| 12 | 47 | | this.webRequestController = webRequestController; |
| 12 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | public GifDecoderProcessor(Stream stream) { this.stream = stream; } |
| | 51 | |
|
| | 52 | | public void DisposeGif() |
| | 53 | | { |
| 12 | 54 | | gifFrameData = null; |
| 12 | 55 | | webRequestController.Dispose(); |
| 12 | 56 | | stream?.Dispose(); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public async UniTask Load(Action<GifFrameData[]> loadSuccsess, Action<Exception> fail, CancellationToken token) |
| | 60 | | { |
| | 61 | | try |
| | 62 | | { |
| 36 | 63 | | await StartDecoding(loadSuccsess, fail, token); |
| 5 | 64 | | } |
| 7 | 65 | | catch (Exception e) when (!(e is OperationCanceledException)) |
| | 66 | | { |
| 3 | 67 | | Debug.LogException(e); |
| 3 | 68 | | fail(e); |
| 3 | 69 | | } |
| 8 | 70 | | } |
| | 71 | |
|
| | 72 | | private async UniTask StartDecoding(Action<GifFrameData[]> loadSuccsess, Action<Exception> fail, CancellationTok |
| | 73 | | { |
| | 74 | | try |
| | 75 | | { |
| | 76 | | // (Kinerius): I noticed that overall the loading of multiple gifs at the same time is faster when only |
| | 77 | | // one is loading, this also avoids the "burst" of gifs loading at the same time, overall |
| | 78 | | // improving the smoothness and the experience, this could be further improved by prioritizing |
| | 79 | | // the processing of gifs whether im close or looking at them like GLFTs. |
| 45 | 80 | | await UniTask.WaitUntil(() => isRunning == false, cancellationToken: token); |
| | 81 | |
|
| 9 | 82 | | isRunning = false; |
| 9 | 83 | | isRunningInternal = true; |
| | 84 | |
|
| 9 | 85 | | var stopwatch = new Stopwatch(); |
| 9 | 86 | | stopwatch.Start(); |
| | 87 | |
|
| | 88 | | GifStream gifStream; |
| | 89 | |
|
| 9 | 90 | | if (stream != null) |
| | 91 | | { |
| 0 | 92 | | gifStream = new GifStream(stream); |
| 0 | 93 | | } |
| | 94 | | else |
| | 95 | | { |
| 27 | 96 | | gifStream = new GifStream( await DownloadGifAndReadStream(token)); |
| | 97 | | } |
| | 98 | |
|
| 18 | 99 | | var images = await ReadStream(gifStream, token); |
| | 100 | |
|
| 6 | 101 | | token.ThrowIfCancellationRequested(); |
| | 102 | |
|
| 18 | 103 | | await TaskUtils.RunThrottledCoroutine(ProcessGifData(images, gifStream.Header.width, gifStream.Header.he |
| | 104 | | .AttachExternalCancellation(token); |
| | 105 | |
|
| 5 | 106 | | loadSuccsess(gifFrameData); |
| | 107 | |
|
| 5 | 108 | | stopwatch.Stop(); |
| 5 | 109 | | } |
| 7 | 110 | | catch (Exception e) when (!(e is OperationCanceledException)) |
| | 111 | | { |
| 3 | 112 | | throw; |
| | 113 | | } |
| | 114 | | finally |
| | 115 | | { |
| 12 | 116 | | if (isRunningInternal) |
| 9 | 117 | | isRunning = false; |
| | 118 | | } |
| 5 | 119 | | } |
| | 120 | |
|
| | 121 | | private IEnumerator ProcessGifData(List<RawImage> rawImages, int width, int height) |
| | 122 | | { |
| 6 | 123 | | gifFrameData = new GifFrameData[rawImages.Count]; |
| 6 | 124 | | SkipFrameIfDepletedTimeBudget skipFrameIfDepletedTimeBudget = new SkipFrameIfDepletedTimeBudget(); |
| | 125 | |
|
| 552 | 126 | | for (var i = 0; i < rawImages.Count; i++) |
| | 127 | | { |
| 270 | 128 | | var frame = new Texture2D( |
| | 129 | | width, |
| | 130 | | height, |
| | 131 | | TextureFormat.ARGB32, false); |
| | 132 | |
|
| 270 | 133 | | frame.SetPixels32(rawImages[i].colors); |
| 270 | 134 | | frame.Compress(false); |
| 270 | 135 | | frame.Apply(); |
| | 136 | |
|
| 270 | 137 | | gifFrameData[i] = new GifFrameData() |
| | 138 | | { |
| | 139 | | texture = frame, |
| | 140 | | delay = rawImages[i].delay |
| | 141 | | }; |
| | 142 | |
|
| 270 | 143 | | yield return skipFrameIfDepletedTimeBudget; |
| | 144 | | } |
| 6 | 145 | | } |
| | 146 | |
|
| | 147 | | private async UniTask<byte[]> DownloadGifAndReadStream(CancellationToken token) |
| | 148 | | { |
| 9 | 149 | | var operation = webRequestController.Get(url, timeout: 15, disposeOnCompleted: false); |
| 36 | 150 | | await UniTask.WaitUntil( () => operation.isDone || operation.isDisposed || operation.isSucceded, cancellatio |
| | 151 | |
|
| 9 | 152 | | if (!operation.isSucceded) |
| | 153 | | { |
| 3 | 154 | | throw new GifWebRequestException(url); |
| | 155 | | } |
| | 156 | |
|
| 6 | 157 | | return operation.webRequest.downloadHandler.data; |
| 6 | 158 | | } |
| | 159 | |
|
| | 160 | | private static UniTask<List<RawImage>> ReadStream(GifStream gifStream, CancellationToken token) |
| | 161 | | { |
| 6 | 162 | | return TaskUtils.Run( () => |
| | 163 | | { |
| 6 | 164 | | var rawImages = new List<RawImage>(); |
| | 165 | |
|
| 834 | 166 | | while (gifStream.HasMoreData) |
| | 167 | | { |
| 828 | 168 | | if (gifStream.CurrentToken == GifStream.Token.Image) |
| | 169 | | { |
| 270 | 170 | | GifImage gifImage = gifStream.ReadImage(); |
| 270 | 171 | | rawImages.Add(new RawImage(gifImage)); |
| 270 | 172 | | } |
| | 173 | | else |
| | 174 | | { |
| 558 | 175 | | gifStream.SkipToken(); |
| | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| 6 | 179 | | return rawImages; |
| | 180 | | }, token); |
| | 181 | | } |
| | 182 | | } |
| | 183 | | } |