|
| 1 | +using UnityEngine; |
| 2 | +using Unity.Collections; |
| 3 | +using UnityEditorInternal; |
| 4 | +using UnityEditor.Profiling; |
| 5 | + |
| 6 | +namespace UTJ.SS2Profiler.Editor |
| 7 | +{ |
| 8 | + internal struct TagInfo |
| 9 | + { |
| 10 | + public int id; |
| 11 | + public int width; |
| 12 | + public int height; |
| 13 | + public int originWidth; |
| 14 | + public int originHeight; |
| 15 | + public ScreenShotToProfiler.TextureCompress compress; |
| 16 | + } |
| 17 | + internal class ProfilerScreenShotEditorLogic |
| 18 | + { |
| 19 | + public static bool TryGetTagInfo(int frameIdx, out TagInfo tagInfo) |
| 20 | + { |
| 21 | + HierarchyFrameDataView hierarchyFrameDataView = |
| 22 | + ProfilerDriver.GetHierarchyFrameDataView(frameIdx, 0, HierarchyFrameDataView.ViewModes.Default, 0, false); ; |
| 23 | + if (hierarchyFrameDataView == null || !hierarchyFrameDataView.valid) |
| 24 | + { |
| 25 | + tagInfo = default(TagInfo); |
| 26 | + return false; |
| 27 | + } |
| 28 | + NativeArray<byte> bytes = |
| 29 | + hierarchyFrameDataView.GetFrameMetaData<byte>(ScreenShotToProfiler.MetadataGuid, ScreenShotToProfiler.InfoTag); |
| 30 | + if (bytes != null && bytes.Length >= 12) |
| 31 | + { |
| 32 | + tagInfo = GenerateTagInfo(bytes); |
| 33 | + return true; |
| 34 | + } |
| 35 | + tagInfo = default(TagInfo); |
| 36 | + return false; |
| 37 | + } |
| 38 | + public static Texture2D GenerateTagTexture(TagInfo info, int idx,int tryFrame = 10) |
| 39 | + { |
| 40 | + Texture2D texture = null; |
| 41 | + |
| 42 | + for (int i = idx; i < idx + tryFrame; ++i) |
| 43 | + { |
| 44 | + HierarchyFrameDataView hierarchyFrameDataView = |
| 45 | + ProfilerDriver.GetHierarchyFrameDataView(i, 0, HierarchyFrameDataView.ViewModes.Default, 0, false); |
| 46 | + if (hierarchyFrameDataView == null || !hierarchyFrameDataView.valid) |
| 47 | + { |
| 48 | + continue; |
| 49 | + } |
| 50 | + NativeArray<byte> bytes = |
| 51 | + hierarchyFrameDataView.GetFrameMetaData<byte>(ScreenShotToProfiler.MetadataGuid, info.id); |
| 52 | + |
| 53 | + if (bytes.IsCreated && bytes.Length > 16) |
| 54 | + { |
| 55 | + texture = new Texture2D(info.width, info.height, ScreenShotProfilerUtil.GetTextureFormat(info.compress), false); |
| 56 | + switch (info.compress) |
| 57 | + { |
| 58 | + case ScreenShotToProfiler.TextureCompress.None: |
| 59 | + case ScreenShotToProfiler.TextureCompress.RGB_565: |
| 60 | + texture.LoadRawTextureData(bytes); |
| 61 | + texture.Apply(); |
| 62 | + break; |
| 63 | + case ScreenShotToProfiler.TextureCompress.PNG: |
| 64 | + case ScreenShotToProfiler.TextureCompress.JPG_BufferRGB565: |
| 65 | + case ScreenShotToProfiler.TextureCompress.JPG_BufferRGBA: |
| 66 | + texture.LoadImage(bytes.ToArray()); |
| 67 | + texture.Apply(); |
| 68 | + break; |
| 69 | + } |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + return texture; |
| 74 | + } |
| 75 | + |
| 76 | + private static TagInfo GenerateTagInfo(NativeArray<byte> data) |
| 77 | + { |
| 78 | + TagInfo info = new TagInfo(); |
| 79 | + info.id = GetIntData(data, 0); |
| 80 | + info.width = GetShortData(data, 4); |
| 81 | + info.height = GetShortData(data, 6); |
| 82 | + info.originWidth = GetShortData(data, 8); |
| 83 | + info.originHeight = GetShortData(data, 10); |
| 84 | + if (data.Length > 12) |
| 85 | + { |
| 86 | + info.compress = (ScreenShotToProfiler.TextureCompress)data[12]; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + info.compress = ScreenShotToProfiler.TextureCompress.None; |
| 91 | + } |
| 92 | + return info; |
| 93 | + } |
| 94 | + private static int GetIntData(NativeArray<byte> data, int idx) |
| 95 | + { |
| 96 | + int val = (data[idx + 0]) + |
| 97 | + (data[idx + 1] << 8) + |
| 98 | + (data[idx + 2] << 16) + |
| 99 | + (data[idx + 3] << 24); |
| 100 | + return val; |
| 101 | + } |
| 102 | + private static int GetShortData(NativeArray<byte> data, int idx) |
| 103 | + { |
| 104 | + int val = (data[idx + 0]) + |
| 105 | + (data[idx + 1] << 8); |
| 106 | + return val; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + } |
| 111 | +} |
0 commit comments