< Summary

Class:DCL.GIFProcessingBridge
Assembly:GIFProcessingBridge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Bridges/GIFProcessingBridge/GIFProcessingBridge.cs
Covered lines:0
Uncovered lines:46
Coverable lines:46
Total lines:141
Line coverage:0% (0 of 46)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GIFProcessingBridge()0%2100%
Awake()0%6200%
RequestGIFProcessor()0%90900%
UpdateGIFPointers(...)0%6200%
FailGIFFetch(...)0%2100%
DeleteGIF(...)0%2100%
GenerateTexturesList(...)0%30500%
RemovePending(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Bridges/GIFProcessingBridge/GIFProcessingBridge.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Collections;
 3using UnityEngine;
 4using DCL.Helpers;
 5
 6namespace DCL
 7{
 8    /// <summary>
 9    /// Bridge that handles GIF processing requests for kernel (initiated at Asset_Gif.Load()), and relays the kernel co
 10    /// </summary>
 11    public class GIFProcessingBridge : MonoBehaviour
 12    {
 13        [System.Serializable]
 14        public class UpdateGIFPointersPayload
 15        {
 16            public string id;
 17            public int width;
 18            public int height;
 19            public int[] pointers;
 20            public float[] frameDelays;
 21        }
 22
 23        public class PendingGifs
 24        {
 25            public enum Status { PENDING, OK, ERROR }
 26
 27            public GifFrameData[] textures;
 28            public Status status;
 29        }
 30
 031        public static GIFProcessingBridge i { get; private set; }
 32
 033        private readonly Dictionary<string, PendingGifs> pendingGifs = new Dictionary<string, PendingGifs>();
 34
 35        void Awake()
 36        {
 037            if (i != null)
 38            {
 039                Utils.SafeDestroy(this);
 040                return;
 41            }
 42
 043            i = this;
 44
 045            KernelConfig.i.EnsureConfigInitialized().Then(config => { jsGIFProcessingEnabled = config.gifSupported; });
 046        }
 47
 48        bool jsGIFProcessingEnabled = false;
 49
 50        /// <summary>
 51        /// Tells Kernel to start processing a desired GIF, waits for the data to come back from Kernel and passes it to
 52        /// </summary>
 53        /// <param name="onSuccess">The callback that will be invoked with the generated textures list</param>
 54        public IEnumerator RequestGIFProcessor(string url, System.Action<GifFrameData[]> onSuccess, System.Action onFail
 55        {
 056            if (!jsGIFProcessingEnabled)
 57            {
 058                onFail?.Invoke();
 059                yield break;
 60            }
 61
 062            if (!pendingGifs.TryGetValue(url, out PendingGifs gif))
 63            {
 064                gif = new PendingGifs()
 65                {
 66                    status = PendingGifs.Status.PENDING
 67                };
 068                pendingGifs.Add(url, gif);
 069                DCL.Interface.WebInterface.RequestGIFProcessor(
 70                    url, url, SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2);
 71            }
 72
 073            yield return new WaitUntil(() => gif.status != PendingGifs.Status.PENDING);
 74
 075            if (gif.status == PendingGifs.Status.ERROR)
 76            {
 077                onFail?.Invoke();
 078                RemovePending(url);
 079                yield break;
 80            }
 81
 082            onSuccess?.Invoke(gif.textures);
 083            RemovePending(url);
 084        }
 85
 86        /// <summary>
 87        /// Method called by Kernel when a GIF finishes processing on that side. This method populates the pending GIF d
 88        /// </summary>
 89        public void UpdateGIFPointers(string payload)
 90        {
 091            var parsedPayload = Utils.SafeFromJson<UpdateGIFPointersPayload>(payload);
 92
 093            if (pendingGifs.TryGetValue(parsedPayload.id, out PendingGifs gif))
 94            {
 095                gif.textures = GenerateTexturesList(parsedPayload.width, parsedPayload.height, parsedPayload.pointers, p
 096                gif.status = PendingGifs.Status.OK;
 97            }
 098        }
 99
 0100        public void FailGIFFetch(string id) { RemovePending(id); }
 101
 0102        public void DeleteGIF(string id) { DCL.Interface.WebInterface.DeleteGIF(id); }
 103
 104        private GifFrameData[] GenerateTexturesList(int width, int height, int[] pointers, float[] frameDelays)
 105        {
 0106            if (width == 0 || height == 0)
 107            {
 0108                Debug.Log("Couldn't create external textures! width or height are 0!");
 0109                return null;
 110            }
 111
 0112            GifFrameData[] gifTextures = new GifFrameData[pointers.Length];
 0113            for (int j = 0; j < pointers.Length; j++)
 114            {
 0115                Texture2D newTex = Texture2D.CreateExternalTexture(width, height, TextureFormat.ARGB32,
 116                    false, false, (System.IntPtr)pointers[j]);
 117
 0118                if (newTex == null)
 119                {
 0120                    Debug.Log("Couldn't create external texture!");
 0121                    continue;
 122                }
 123
 0124                newTex.wrapMode = TextureWrapMode.Clamp;
 125
 0126                gifTextures[j] = new GifFrameData() { texture = newTex, delay = frameDelays[j] / 1000 };
 127            }
 128
 0129            return gifTextures;
 130        }
 131
 132        private void RemovePending(string id)
 133        {
 0134            if (pendingGifs.TryGetValue(id, out PendingGifs gif))
 135            {
 0136                gif.status = PendingGifs.Status.ERROR;
 0137                pendingGifs.Remove(id);
 138            }
 0139        }
 140    }
 141}