| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Collections; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.Helpers; |
| | 5 | |
|
| | 6 | | namespace 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 | |
|
| 0 | 31 | | public static GIFProcessingBridge i { get; private set; } |
| | 32 | |
|
| 0 | 33 | | private readonly Dictionary<string, PendingGifs> pendingGifs = new Dictionary<string, PendingGifs>(); |
| | 34 | |
|
| | 35 | | void Awake() |
| | 36 | | { |
| 0 | 37 | | if (i != null) |
| | 38 | | { |
| 0 | 39 | | Utils.SafeDestroy(this); |
| 0 | 40 | | return; |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | i = this; |
| | 44 | |
|
| 0 | 45 | | KernelConfig.i.EnsureConfigInitialized().Then(config => { jsGIFProcessingEnabled = config.gifSupported; }); |
| 0 | 46 | | } |
| | 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 | | { |
| 0 | 56 | | if (!jsGIFProcessingEnabled) |
| | 57 | | { |
| 0 | 58 | | onFail?.Invoke(); |
| 0 | 59 | | yield break; |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | if (!pendingGifs.TryGetValue(url, out PendingGifs gif)) |
| | 63 | | { |
| 0 | 64 | | gif = new PendingGifs() |
| | 65 | | { |
| | 66 | | status = PendingGifs.Status.PENDING |
| | 67 | | }; |
| 0 | 68 | | pendingGifs.Add(url, gif); |
| 0 | 69 | | DCL.Interface.WebInterface.RequestGIFProcessor( |
| | 70 | | url, url, SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2); |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | yield return new WaitUntil(() => gif.status != PendingGifs.Status.PENDING); |
| | 74 | |
|
| 0 | 75 | | if (gif.status == PendingGifs.Status.ERROR) |
| | 76 | | { |
| 0 | 77 | | onFail?.Invoke(); |
| 0 | 78 | | RemovePending(url); |
| 0 | 79 | | yield break; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | onSuccess?.Invoke(gif.textures); |
| 0 | 83 | | RemovePending(url); |
| 0 | 84 | | } |
| | 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 | | { |
| 0 | 91 | | var parsedPayload = Utils.SafeFromJson<UpdateGIFPointersPayload>(payload); |
| | 92 | |
|
| 0 | 93 | | if (pendingGifs.TryGetValue(parsedPayload.id, out PendingGifs gif)) |
| | 94 | | { |
| 0 | 95 | | gif.textures = GenerateTexturesList(parsedPayload.width, parsedPayload.height, parsedPayload.pointers, p |
| 0 | 96 | | gif.status = PendingGifs.Status.OK; |
| | 97 | | } |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | public void FailGIFFetch(string id) { RemovePending(id); } |
| | 101 | |
|
| 0 | 102 | | public void DeleteGIF(string id) { DCL.Interface.WebInterface.DeleteGIF(id); } |
| | 103 | |
|
| | 104 | | private GifFrameData[] GenerateTexturesList(int width, int height, int[] pointers, float[] frameDelays) |
| | 105 | | { |
| 0 | 106 | | if (width == 0 || height == 0) |
| | 107 | | { |
| 0 | 108 | | Debug.Log("Couldn't create external textures! width or height are 0!"); |
| 0 | 109 | | return null; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | GifFrameData[] gifTextures = new GifFrameData[pointers.Length]; |
| 0 | 113 | | for (int j = 0; j < pointers.Length; j++) |
| | 114 | | { |
| 0 | 115 | | Texture2D newTex = Texture2D.CreateExternalTexture(width, height, TextureFormat.ARGB32, |
| | 116 | | false, false, (System.IntPtr)pointers[j]); |
| | 117 | |
|
| 0 | 118 | | if (newTex == null) |
| | 119 | | { |
| 0 | 120 | | Debug.Log("Couldn't create external texture!"); |
| 0 | 121 | | continue; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | newTex.wrapMode = TextureWrapMode.Clamp; |
| | 125 | |
|
| 0 | 126 | | gifTextures[j] = new GifFrameData() { texture = newTex, delay = frameDelays[j] / 1000 }; |
| | 127 | | } |
| | 128 | |
|
| 0 | 129 | | return gifTextures; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | private void RemovePending(string id) |
| | 133 | | { |
| 0 | 134 | | if (pendingGifs.TryGetValue(id, out PendingGifs gif)) |
| | 135 | | { |
| 0 | 136 | | gif.status = PendingGifs.Status.ERROR; |
| 0 | 137 | | pendingGifs.Remove(id); |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | | } |
| | 141 | | } |