Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 815c38c

Browse files
author
Yuncong Zhang
committed
Reduce instantiations.
1 parent dfd0f8b commit 815c38c

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

Runtime/ui/painting/codec_gif.cs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class FrameData {
2626
volatile Texture2D _texture;
2727
volatile FrameData _frameData;
2828
volatile Image _image;
29+
volatile GifDecoder _decoder;
30+
volatile MemoryStream _stream;
2931
IEnumerator _coroutine;
3032

3133
public GifCodec(byte[] bytes) {
@@ -38,59 +40,51 @@ public GifCodec(byte[] bytes) {
3840
this._frameIndex = 0;
3941
this._bytes = bytes;
4042
this._coroutine = this._startDecoding();
41-
this._init();
42-
this._texture = new Texture2D(this._width, this._height, TextureFormat.BGRA32, false);
43-
this._texture.hideFlags = HideFlags.HideAndDontSave;
44-
this._image = new Image(this._texture);
43+
this._decoder = new GifDecoder();
44+
this._stream = new MemoryStream(this._bytes);
45+
this._frameData = new FrameData() {
46+
frameInfo = new FrameInfo()
47+
};
4548
}
4649

47-
void _init() {
48-
var bytesStream = new MemoryStream(this._bytes);
50+
IEnumerator _startDecoding() {
51+
this._stream.Seek(0, SeekOrigin.Begin);
4952

50-
var gifDecoder = new GifDecoder();
51-
if (gifDecoder.read(bytesStream) != GifDecoder.STATUS_OK) {
53+
if (this._decoder.read(this._stream) != GifDecoder.STATUS_OK) {
5254
throw new Exception("Failed to decode gif.");
5355
}
5456

55-
this._width = gifDecoder.frameWidth;
56-
this._height = gifDecoder.frameHeight;
57-
}
58-
59-
IEnumerator _startDecoding() {
60-
var bytesStream = new MemoryStream(this._bytes);
57+
this._width = this._decoder.frameWidth;
58+
this._height = this._decoder.frameHeight;
6159

62-
var gifDecoder = new GifDecoder();
63-
if (gifDecoder.read(bytesStream) != GifDecoder.STATUS_OK) {
64-
throw new Exception("Failed to decode gif.");
60+
if (this._texture == null) {
61+
this._texture = new Texture2D(this._width, this._height, TextureFormat.BGRA32, false);
62+
this._texture.hideFlags = HideFlags.HideAndDontSave;
63+
this._image = new Image(this._texture);
64+
this._frameData.frameInfo.image = this._image;
6565
}
66-
67-
this._width = gifDecoder.frameWidth;
68-
this._height = gifDecoder.frameHeight;
66+
this._frameData.gifFrame = this._decoder.currentFrame;
67+
D.assert(this._frameData.gifFrame != null);
6968

7069
int i = 0;
7170
while (true) {
72-
if (gifDecoder.nextFrame() != GifDecoder.STATUS_OK) {
71+
if (this._decoder.nextFrame() != GifDecoder.STATUS_OK) {
7372
throw new Exception("Failed to decode gif.");
7473
}
7574

76-
if (gifDecoder.done) {
75+
if (this._decoder.done) {
7776
break;
7877
}
7978

80-
var frameData = new FrameData {
81-
gifFrame = gifDecoder.currentFrame
82-
};
83-
84-
this._frameData = frameData;
8579

8680
i++;
8781

8882
yield return null;
8983
}
9084

91-
D.assert(gifDecoder.frameCount == i);
92-
this._frameCount = gifDecoder.frameCount;
93-
this._repetitionCount = gifDecoder.loopCount;
85+
D.assert(this._decoder.frameCount == i);
86+
this._frameCount = this._decoder.frameCount;
87+
this._repetitionCount = this._decoder.loopCount;
9488
this._isDone = true;
9589
}
9690

@@ -120,10 +114,7 @@ public FrameInfo getNextFrame() {
120114
this._nextFrame();
121115
this._texture.LoadRawTextureData(this._frameData.gifFrame.bytes);
122116
this._texture.Apply();
123-
this._frameData.frameInfo = new FrameInfo() {
124-
image = this._image,
125-
duration = TimeSpan.FromMilliseconds(this._frameData.gifFrame.delay)
126-
};
117+
this._frameData.frameInfo.duration = TimeSpan.FromMilliseconds(this._frameData.gifFrame.delay);
127118
return this._frameData.frameInfo;
128119
}
129120

0 commit comments

Comments
 (0)