@next2d/display 3.13.1 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/display",
3
- "version": "3.13.1",
3
+ "version": "3.15.0",
4
4
  "description": "Next2D Display Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -24,13 +24,13 @@
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
26
  "dependencies": {
27
- "@next2d/ui": "3.13.1",
28
- "@next2d/text": "3.13.1",
29
- "@next2d/geom": "3.13.1",
30
- "@next2d/media": "3.13.1",
31
- "@next2d/net": "3.13.1",
32
- "@next2d/events": "3.13.1",
33
- "@next2d/filters": "3.13.1",
34
- "@next2d/render-queue": "3.13.1"
27
+ "@next2d/ui": "3.15.0",
28
+ "@next2d/text": "3.15.0",
29
+ "@next2d/geom": "3.15.0",
30
+ "@next2d/media": "3.15.0",
31
+ "@next2d/net": "3.15.0",
32
+ "@next2d/events": "3.15.0",
33
+ "@next2d/filters": "3.15.0",
34
+ "@next2d/render-queue": "3.15.0"
35
35
  }
36
36
  }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @description URLから読み込んだ画像の uniqueKey を、読み込み元のURLから生成します。
3
+ * 全ピクセルをハッシュすると、大きな画像ではその計算だけでフレームが止まるため
4
+ * (JITが使えない環境では数秒)、同じ内容を指すURLをキーの主体にします。
5
+ * 画像サイズと少数のピクセルも混ぜ、同じURLで内容が変わった場合に別のキーになりやすくします。
6
+ * 同じURLの画像は同じキーになるので、描画キャッシュは従来どおり共有されます。
7
+ *
8
+ * Generates the uniqueKey of an image loaded from a URL, based on that URL.
9
+ * Hashing every pixel stalls the frame on large images (for seconds where JIT is
10
+ * unavailable), so the URL, which identifies the content, is the main input.
11
+ * The image size and a few pixels are mixed in so that content replaced behind the
12
+ * same URL is likely to get another key. Images from the same URL get the same key,
13
+ * so the render cache is shared as before.
14
+ *
15
+ * @param {string} src
16
+ * @param {number} width
17
+ * @param {number} height
18
+ * @param {Uint8Array} buffer
19
+ * @return {number}
20
+ * @method
21
+ * @protected
22
+ */
23
+ export declare const execute: (src: string, width: number, height: number, buffer: Uint8Array) => number;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @description FNV-1a の 1 ステップ(32bit値を4バイトとして混ぜる)
3
+ * One FNV-1a step (mixes a 32-bit value as four bytes)
4
+ *
5
+ * @param {number} hash
6
+ * @param {number} value
7
+ * @return {number}
8
+ * @method
9
+ * @private
10
+ */
11
+ const mix = (hash, value) => {
12
+ hash = Math.imul(hash ^ value & 0xff, 16777619);
13
+ hash = Math.imul(hash ^ value >> 8 & 0xff, 16777619);
14
+ hash = Math.imul(hash ^ value >> 16 & 0xff, 16777619);
15
+ return Math.imul(hash ^ value >>> 24, 16777619);
16
+ };
17
+ /**
18
+ * @description URLのうちハッシュに使う最大文字数。これより長いURL(data URLなど)は等間隔に間引く。
19
+ * Maximum number of URL characters to hash. Longer URLs (e.g. data URLs) are sampled evenly.
20
+ *
21
+ * @type {number}
22
+ * @const
23
+ */
24
+ const MAX_URL_SAMPLES = 4096;
25
+ /**
26
+ * @description ピクセルのうちハッシュに使う最大数。読み込み元が同じで内容が差し替わった場合の検出用。
27
+ * Maximum number of pixels to hash. Detects content replaced behind the same source.
28
+ *
29
+ * @type {number}
30
+ * @const
31
+ */
32
+ const MAX_PIXEL_SAMPLES = 256;
33
+ /**
34
+ * @description URLから読み込んだ画像の uniqueKey を、読み込み元のURLから生成します。
35
+ * 全ピクセルをハッシュすると、大きな画像ではその計算だけでフレームが止まるため
36
+ * (JITが使えない環境では数秒)、同じ内容を指すURLをキーの主体にします。
37
+ * 画像サイズと少数のピクセルも混ぜ、同じURLで内容が変わった場合に別のキーになりやすくします。
38
+ * 同じURLの画像は同じキーになるので、描画キャッシュは従来どおり共有されます。
39
+ *
40
+ * Generates the uniqueKey of an image loaded from a URL, based on that URL.
41
+ * Hashing every pixel stalls the frame on large images (for seconds where JIT is
42
+ * unavailable), so the URL, which identifies the content, is the main input.
43
+ * The image size and a few pixels are mixed in so that content replaced behind the
44
+ * same URL is likely to get another key. Images from the same URL get the same key,
45
+ * so the render cache is shared as before.
46
+ *
47
+ * @param {string} src
48
+ * @param {number} width
49
+ * @param {number} height
50
+ * @param {Uint8Array} buffer
51
+ * @return {number}
52
+ * @method
53
+ * @protected
54
+ */
55
+ export const execute = (src, width, height, buffer) => {
56
+ let hash = 2166136261; // FNV-1aオフセット basis
57
+ const length = src.length;
58
+ hash = mix(hash, length);
59
+ if (length <= MAX_URL_SAMPLES) {
60
+ for (let idx = 0; idx < length; ++idx) {
61
+ hash = mix(hash, src.charCodeAt(idx));
62
+ }
63
+ }
64
+ else {
65
+ // 等間隔に間引き、末尾の文字は必ず含める
66
+ const step = length / MAX_URL_SAMPLES;
67
+ for (let idx = 0; idx < MAX_URL_SAMPLES; ++idx) {
68
+ hash = mix(hash, src.charCodeAt(Math.floor(idx * step)));
69
+ }
70
+ hash = mix(hash, src.charCodeAt(length - 1));
71
+ }
72
+ hash = mix(hash, width);
73
+ hash = mix(hash, height);
74
+ const pixels = buffer.length >>> 2;
75
+ hash = mix(hash, pixels);
76
+ if (pixels > 0) {
77
+ const samples = Math.min(pixels, MAX_PIXEL_SAMPLES);
78
+ const step = pixels / samples;
79
+ for (let idx = 0; idx < samples; ++idx) {
80
+ const offset = Math.floor(idx * step) << 2;
81
+ hash = mix(hash, buffer[offset]
82
+ | buffer[offset + 1] << 8
83
+ | buffer[offset + 2] << 16
84
+ | buffer[offset + 3] << 24);
85
+ }
86
+ }
87
+ // 32bitハッシュ値を24bitに圧縮 (DisplayObjectGenerateHashService と同じ範囲)
88
+ return (hash >>> 8 ^ hash & 0xff) & 0xffffff;
89
+ };
@@ -1,6 +1,7 @@
1
1
  import { $cacheStore } from "@next2d/cache";
2
2
  import { Event } from "@next2d/events";
3
3
  import { execute as shapeSetBitmapBufferUseCase } from "./ShapeSetBitmapBufferUseCase";
4
+ import { execute as shapeGenerateBitmapKeyService } from "../service/ShapeGenerateBitmapKeyService";
4
5
  /**
5
6
  * @description 指定のURLを画像を読み込み、Shapeに設定
6
7
  * Load the specified URL image and set it to Shape
@@ -22,7 +23,12 @@ export const execute = (shape, src) => {
22
23
  canvas.height = height;
23
24
  const context = canvas.getContext("2d", { "willReadFrequently": true });
24
25
  context.drawImage(image, 0, 0, width, height);
25
- shapeSetBitmapBufferUseCase(shape, width, height, new Uint8Array(context.getImageData(0, 0, width, height).data));
26
+ const buffer = new Uint8Array(context.getImageData(0, 0, width, height).data);
27
+ shapeSetBitmapBufferUseCase(shape, width, height, buffer);
28
+ // 読み込み元のURLから uniqueKey を決める。未設定のままだと、描画時に全ピクセルを
29
+ // ハッシュしてキーを作るため、大きな画像ではその計算だけでフレームが止まる。
30
+ // (shapeSetBitmapBufferUseCase が uniqueKey を空に戻すので、その後に設定する)
31
+ shape.uniqueKey = `${shapeGenerateBitmapKeyService(src, width, height, buffer)}`;
26
32
  $cacheStore.destroy(context);
27
33
  if (shape.hasEventListener(Event.COMPLETE)) {
28
34
  shape.dispatchEvent(new Event(Event.COMPLETE));