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

Commit 585e209

Browse files
authored
Merge pull request #223 from UnityTech/gif
Fix gif not animating problem.
2 parents 9790f42 + 374eeed commit 585e209

File tree

4 files changed

+87
-21
lines changed

4 files changed

+87
-21
lines changed

Runtime/flow/raster_cache.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ internal _RasterCacheKey(Picture picture, Matrix3 matrix, float devicePixelRatio
5858
var y = this.matrix[5] * devicePixelRatio;
5959
this.matrix[2] = (x - (int) x) / devicePixelRatio; // x
6060
this.matrix[5] = (y - (int) y) / devicePixelRatio; // y
61-
61+
6262
D.assert(Mathf.Abs(this.matrix[2]) <= 1e-5);
6363
D.assert(Mathf.Abs(this.matrix[5]) <= 1e-5);
6464
return true;
6565
});
66-
66+
6767
this.matrix[2] = 0.0f;
68-
this.matrix[5] = 0.0f;
68+
this.matrix[5] = 0.0f;
6969
this.devicePixelRatio = devicePixelRatio;
7070
this.antiAliasing = antiAliasing;
7171
}
@@ -145,12 +145,14 @@ public RasterCache(int threshold = 3) {
145145
readonly Dictionary<_RasterCacheKey, _RasterCacheEntry> _cache;
146146

147147
MeshPool _meshPool;
148+
148149
public MeshPool meshPool {
149150
set { this._meshPool = value; }
150151
}
151152

152153
public RasterCacheResult getPrerolledImage(
153-
Picture picture, Matrix3 transform, float devicePixelRatio, int antiAliasing, bool isComplex, bool willChange) {
154+
Picture picture, Matrix3 transform, float devicePixelRatio, int antiAliasing, bool isComplex,
155+
bool willChange) {
154156
if (this.threshold == 0) {
155157
return null;
156158
}
@@ -176,7 +178,8 @@ public RasterCacheResult getPrerolledImage(
176178

177179
if (entry.image == null) {
178180
D.assert(this._meshPool != null);
179-
entry.image = this._rasterizePicture(picture, transform, devicePixelRatio, antiAliasing, this._meshPool);
181+
entry.image =
182+
this._rasterizePicture(picture, transform, devicePixelRatio, antiAliasing, this._meshPool);
180183
}
181184

182185
return entry.image;
@@ -213,6 +216,10 @@ static bool _canRasterizePicture(Picture picture) {
213216
return false;
214217
}
215218

219+
if (picture.isDynamic) {
220+
return false;
221+
}
222+
216223
return true;
217224
}
218225

Runtime/ui/painting/codec_gif.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
33
using System.IO;
4-
using RSG;
54
using Unity.UIWidgets.foundation;
65
using UnityEngine;
76

@@ -60,9 +59,10 @@ IEnumerator _startDecoding() {
6059
if (this._texture == null) {
6160
this._texture = new Texture2D(this._width, this._height, TextureFormat.BGRA32, false);
6261
this._texture.hideFlags = HideFlags.HideAndDontSave;
63-
this._image = new Image(this._texture);
62+
this._image = new Image(this._texture, isDynamic: true);
6463
this._frameData.frameInfo.image = this._image;
6564
}
65+
6666
this._frameData.gifFrame = this._decoder.currentFrame;
6767
D.assert(this._frameData.gifFrame != null);
6868

Runtime/ui/painting/image.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ public class Image : IEquatable<Image>, IDisposable {
88
Texture _texture;
99
readonly bool _noDispose;
1010
readonly bool _isAsset;
11+
readonly bool _isDynamic;
1112
AssetBundle _bundle;
1213

13-
public Image(Texture texture, bool noDispose = false, bool isAsset = false, AssetBundle bundle = null) {
14+
public Image(Texture texture, bool noDispose = false, bool isAsset = false, AssetBundle bundle = null,
15+
bool isDynamic = false) {
1416
D.assert(!noDispose || !isAsset && bundle == null);
1517
D.assert(isAsset || bundle == null);
1618

1719
this._texture = texture;
1820
this._noDispose = noDispose;
1921
this._isAsset = isAsset;
2022
this._bundle = bundle;
23+
this._isDynamic = isDynamic;
2124
}
2225

2326
public int width {
@@ -32,6 +35,10 @@ public Texture texture {
3235
get { return this._texture; }
3336
}
3437

38+
public bool isDynamic {
39+
get { return this._isDynamic; }
40+
}
41+
3542
~Image() {
3643
this._dispose(true);
3744
}

Runtime/ui/painting/picture.cs

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
using System;
22
using System.Collections.Generic;
33
using Unity.UIWidgets.foundation;
4-
using UnityEngine;
54

65
namespace Unity.UIWidgets.ui {
76
public class Picture {
8-
public Picture(List<DrawCmd> drawCmds, Rect paintBounds) {
7+
public Picture(List<DrawCmd> drawCmds, Rect paintBounds, bool isDynamic = false) {
98
this.drawCmds = drawCmds;
109
this.paintBounds = paintBounds;
10+
this._isDynamic = isDynamic;
1111
}
1212

1313
public readonly List<DrawCmd> drawCmds;
1414
public readonly Rect paintBounds;
15+
16+
public bool isDynamic {
17+
get { return this._isDynamic; }
18+
}
19+
20+
bool _isDynamic;
1521
}
1622

1723
public class PictureRecorder {
1824
readonly List<DrawCmd> _drawCmds = new List<DrawCmd>();
1925

2026
readonly List<CanvasState> _states = new List<CanvasState>();
2127

28+
bool _isDynamic;
29+
2230
public PictureRecorder() {
2331
this.reset();
2432
}
@@ -34,6 +42,7 @@ public Matrix3 getTotalMatrix() {
3442

3543
public void reset() {
3644
this._drawCmds.Clear();
45+
this._isDynamic = false;
3746
this._states.Clear();
3847
this._states.Add(new CanvasState {
3948
xform = Matrix3.I(),
@@ -49,8 +58,8 @@ public Picture endRecording() {
4958
throw new Exception("unmatched save/restore commands");
5059
}
5160

52-
var state = this._getState();
53-
return new Picture(new List<DrawCmd>(this._drawCmds), state.paintBounds);
61+
var state = this._getState();
62+
return new Picture(new List<DrawCmd>(this._drawCmds), state.paintBounds, this._isDynamic);
5463
}
5564

5665
public void addDrawCmd(DrawCmd drawCmd) {
@@ -70,80 +79,95 @@ public void addDrawCmd(DrawCmd drawCmd) {
7079
});
7180
break;
7281
}
82+
7383
case DrawRestore _: {
7484
var stateToRestore = this._getState();
7585
this._states.RemoveAt(this._states.Count - 1);
7686
var state = this._getState();
7787

7888
if (!stateToRestore.saveLayer) {
7989
state.paintBounds = stateToRestore.paintBounds;
80-
} else {
90+
}
91+
else {
8192
var paintBounds = stateToRestore.paintBounds.shift(stateToRestore.layerOffset);
8293
paintBounds = state.xform.mapRect(paintBounds);
8394
this._addPaintBounds(paintBounds);
8495
}
96+
8597
break;
8698
}
99+
87100
case DrawTranslate cmd: {
88101
var state = this._getState();
89102
state.xform = new Matrix3(state.xform);
90103
state.xform.preTranslate(cmd.dx, cmd.dy);
91104
break;
92105
}
106+
93107
case DrawScale cmd: {
94108
var state = this._getState();
95109
state.xform = new Matrix3(state.xform);
96110
state.xform.preScale(cmd.sx, (cmd.sy ?? cmd.sx));
97111
break;
98112
}
113+
99114
case DrawRotate cmd: {
100115
var state = this._getState();
101116
state.xform = new Matrix3(state.xform);
102117
if (cmd.offset == null) {
103118
state.xform.preRotate(cmd.radians);
104-
} else {
119+
}
120+
else {
105121
state.xform.preRotate(cmd.radians,
106122
cmd.offset.dx,
107123
cmd.offset.dy);
108124
}
125+
109126
break;
110127
}
128+
111129
case DrawSkew cmd: {
112130
var state = this._getState();
113131
state.xform = new Matrix3(state.xform);
114132
state.xform.preSkew(cmd.sx, cmd.sy);
115133
break;
116134
}
135+
117136
case DrawConcat cmd: {
118137
var state = this._getState();
119138
state.xform = new Matrix3(state.xform);
120139
state.xform.preConcat(cmd.matrix);
121140
break;
122141
}
142+
123143
case DrawResetMatrix _: {
124144
var state = this._getState();
125145
state.xform = Matrix3.I();
126146
break;
127147
}
148+
128149
case DrawSetMatrix cmd: {
129150
var state = this._getState();
130151
state.xform = new Matrix3(cmd.matrix);
131152
break;
132153
}
154+
133155
case DrawClipRect cmd: {
134156
var state = this._getState();
135157

136158
var rect = state.xform.mapRect(cmd.rect);
137159
state.scissor = state.scissor == null ? rect : state.scissor.intersect(rect);
138160
break;
139161
}
162+
140163
case DrawClipRRect cmd: {
141164
var state = this._getState();
142165

143166
var rect = state.xform.mapRect(cmd.rrect.outerRect);
144167
state.scissor = state.scissor == null ? rect : state.scissor.intersect(rect);
145168
break;
146169
}
170+
147171
case DrawClipPath cmd: {
148172
var state = this._getState();
149173
var scale = XformUtils.getScale(state.xform);
@@ -154,6 +178,7 @@ public void addDrawCmd(DrawCmd drawCmd) {
154178
state.scissor = state.scissor == null ? rect : state.scissor.intersect(rect);
155179
break;
156180
}
181+
157182
case DrawPath cmd: {
158183
var state = this._getState();
159184
var scale = XformUtils.getScale(state.xform);
@@ -165,7 +190,8 @@ public void addDrawCmd(DrawCmd drawCmd) {
165190
if (paint.style == PaintingStyle.fill) {
166191
var cache = path.flatten(scale * devicePixelRatio);
167192
mesh = cache.getFillMesh(out _).transform(state.xform);
168-
} else {
193+
}
194+
else {
169195
float strokeWidth = (paint.strokeWidth * scale).clamp(0, 200.0f);
170196
float fringeWidth = 1 / devicePixelRatio;
171197

@@ -180,59 +206,84 @@ public void addDrawCmd(DrawCmd drawCmd) {
180206
paint.strokeJoin,
181207
paint.strokeMiterLimit).transform(state.xform);
182208
}
183-
209+
184210
if (paint.maskFilter != null && paint.maskFilter.sigma != 0) {
185211
float sigma = scale * paint.maskFilter.sigma;
186212
float sigma3 = 3 * sigma;
187213
this._addPaintBounds(mesh.bounds.inflate(sigma3));
188-
} else {
214+
}
215+
else {
189216
this._addPaintBounds(mesh.bounds);
190217
}
218+
191219
break;
192220
}
221+
193222
case DrawImage cmd: {
194223
var state = this._getState();
195224
var rect = Rect.fromLTWH(cmd.offset.dx, cmd.offset.dy,
196225
cmd.image.width, cmd.image.height);
197226
rect = state.xform.mapRect(rect);
198227
this._addPaintBounds(rect);
228+
if (cmd.image.isDynamic) {
229+
this._isDynamic = true;
230+
}
231+
199232
break;
200233
}
234+
201235
case DrawImageRect cmd: {
202236
var state = this._getState();
203237
var rect = state.xform.mapRect(cmd.dst);
204238
this._addPaintBounds(rect);
239+
if (cmd.image.isDynamic) {
240+
this._isDynamic = true;
241+
}
242+
205243
break;
206244
}
245+
207246
case DrawImageNine cmd: {
208247
var state = this._getState();
209248
var rect = state.xform.mapRect(cmd.dst);
210249
this._addPaintBounds(rect);
250+
if (cmd.image.isDynamic) {
251+
this._isDynamic = true;
252+
}
253+
211254
break;
212255
}
256+
213257
case DrawPicture cmd: {
214258
var state = this._getState();
215259
var rect = state.xform.mapRect(cmd.picture.paintBounds);
216260
this._addPaintBounds(rect);
261+
if (cmd.picture.isDynamic) {
262+
this._isDynamic = true;
263+
}
264+
217265
break;
218266
}
267+
219268
case DrawTextBlob cmd: {
220269
var state = this._getState();
221270
var scale = XformUtils.getScale(state.xform);
222271
var rect = cmd.textBlob.boundsInText.shift(cmd.offset);
223272
rect = state.xform.mapRect(rect);
224-
273+
225274
var paint = cmd.paint;
226275
if (paint.maskFilter != null && paint.maskFilter.sigma != 0) {
227276
float sigma = scale * paint.maskFilter.sigma;
228277
float sigma3 = 3 * sigma;
229278
this._addPaintBounds(rect.inflate(sigma3));
230-
} else {
279+
}
280+
else {
231281
this._addPaintBounds(rect);
232282
}
233-
283+
234284
break;
235285
}
286+
236287
default:
237288
throw new Exception("unknown drawCmd: " + drawCmd);
238289
}
@@ -250,7 +301,8 @@ void _addPaintBounds(Rect paintBounds) {
250301

251302
if (state.paintBounds.isEmpty) {
252303
state.paintBounds = paintBounds;
253-
} else {
304+
}
305+
else {
254306
state.paintBounds = state.paintBounds.expandToInclude(paintBounds);
255307
}
256308
}

0 commit comments

Comments
 (0)