image_utils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright 2019 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { assert, shadow, unreachable, warn } from "../shared/util.js";
  16. import { RefSetCache } from "./primitives.js";
  17. class BaseLocalCache {
  18. constructor(options) {
  19. if (this.constructor === BaseLocalCache) {
  20. unreachable("Cannot initialize BaseLocalCache.");
  21. }
  22. this._onlyRefs = (options && options.onlyRefs) === true;
  23. if (!this._onlyRefs) {
  24. this._nameRefMap = new Map();
  25. this._imageMap = new Map();
  26. }
  27. this._imageCache = new RefSetCache();
  28. }
  29. getByName(name) {
  30. if (this._onlyRefs) {
  31. unreachable("Should not call `getByName` method.");
  32. }
  33. const ref = this._nameRefMap.get(name);
  34. if (ref) {
  35. return this.getByRef(ref);
  36. }
  37. return this._imageMap.get(name) || null;
  38. }
  39. getByRef(ref) {
  40. return this._imageCache.get(ref) || null;
  41. }
  42. set(name, ref, data) {
  43. unreachable("Abstract method `set` called.");
  44. }
  45. }
  46. class LocalImageCache extends BaseLocalCache {
  47. set(name, ref = null, data) {
  48. if (typeof name !== "string") {
  49. throw new Error('LocalImageCache.set - expected "name" argument.');
  50. }
  51. if (ref) {
  52. if (this._imageCache.has(ref)) {
  53. return;
  54. }
  55. this._nameRefMap.set(name, ref);
  56. this._imageCache.put(ref, data);
  57. return;
  58. }
  59. // name
  60. if (this._imageMap.has(name)) {
  61. return;
  62. }
  63. this._imageMap.set(name, data);
  64. }
  65. }
  66. class LocalColorSpaceCache extends BaseLocalCache {
  67. set(name = null, ref = null, data) {
  68. if (typeof name !== "string" && !ref) {
  69. throw new Error(
  70. 'LocalColorSpaceCache.set - expected "name" and/or "ref" argument.'
  71. );
  72. }
  73. if (ref) {
  74. if (this._imageCache.has(ref)) {
  75. return;
  76. }
  77. if (name !== null) {
  78. // Optional when `ref` is defined.
  79. this._nameRefMap.set(name, ref);
  80. }
  81. this._imageCache.put(ref, data);
  82. return;
  83. }
  84. // name
  85. if (this._imageMap.has(name)) {
  86. return;
  87. }
  88. this._imageMap.set(name, data);
  89. }
  90. }
  91. class LocalFunctionCache extends BaseLocalCache {
  92. constructor(options) {
  93. super({ onlyRefs: true });
  94. }
  95. set(name = null, ref, data) {
  96. if (!ref) {
  97. throw new Error('LocalFunctionCache.set - expected "ref" argument.');
  98. }
  99. if (this._imageCache.has(ref)) {
  100. return;
  101. }
  102. this._imageCache.put(ref, data);
  103. }
  104. }
  105. class LocalGStateCache extends BaseLocalCache {
  106. set(name, ref = null, data) {
  107. if (typeof name !== "string") {
  108. throw new Error('LocalGStateCache.set - expected "name" argument.');
  109. }
  110. if (ref) {
  111. if (this._imageCache.has(ref)) {
  112. return;
  113. }
  114. this._nameRefMap.set(name, ref);
  115. this._imageCache.put(ref, data);
  116. return;
  117. }
  118. // name
  119. if (this._imageMap.has(name)) {
  120. return;
  121. }
  122. this._imageMap.set(name, data);
  123. }
  124. }
  125. class LocalTilingPatternCache extends BaseLocalCache {
  126. constructor(options) {
  127. super({ onlyRefs: true });
  128. }
  129. set(name = null, ref, data) {
  130. if (!ref) {
  131. throw new Error('LocalTilingPatternCache.set - expected "ref" argument.');
  132. }
  133. if (this._imageCache.has(ref)) {
  134. return;
  135. }
  136. this._imageCache.put(ref, data);
  137. }
  138. }
  139. class GlobalImageCache {
  140. static get NUM_PAGES_THRESHOLD() {
  141. return shadow(this, "NUM_PAGES_THRESHOLD", 2);
  142. }
  143. static get MIN_IMAGES_TO_CACHE() {
  144. return shadow(this, "MIN_IMAGES_TO_CACHE", 10);
  145. }
  146. static get MAX_BYTE_SIZE() {
  147. return shadow(this, "MAX_BYTE_SIZE", /* Forty megabytes = */ 40e6);
  148. }
  149. constructor() {
  150. if (
  151. typeof PDFJSDev === "undefined" ||
  152. PDFJSDev.test("!PRODUCTION || TESTING")
  153. ) {
  154. assert(
  155. GlobalImageCache.NUM_PAGES_THRESHOLD > 1,
  156. "GlobalImageCache - invalid NUM_PAGES_THRESHOLD constant."
  157. );
  158. }
  159. this._refCache = new RefSetCache();
  160. this._imageCache = new RefSetCache();
  161. }
  162. get _byteSize() {
  163. let byteSize = 0;
  164. for (const imageData of this._imageCache) {
  165. byteSize += imageData.byteSize;
  166. }
  167. return byteSize;
  168. }
  169. get _cacheLimitReached() {
  170. if (this._imageCache.size < GlobalImageCache.MIN_IMAGES_TO_CACHE) {
  171. return false;
  172. }
  173. if (this._byteSize < GlobalImageCache.MAX_BYTE_SIZE) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. shouldCache(ref, pageIndex) {
  179. const pageIndexSet = this._refCache.get(ref);
  180. const numPages = pageIndexSet
  181. ? pageIndexSet.size + (pageIndexSet.has(pageIndex) ? 0 : 1)
  182. : 1;
  183. if (numPages < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  184. return false;
  185. }
  186. if (!this._imageCache.has(ref) && this._cacheLimitReached) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. addPageIndex(ref, pageIndex) {
  192. let pageIndexSet = this._refCache.get(ref);
  193. if (!pageIndexSet) {
  194. pageIndexSet = new Set();
  195. this._refCache.put(ref, pageIndexSet);
  196. }
  197. pageIndexSet.add(pageIndex);
  198. }
  199. /**
  200. * PLEASE NOTE: Must be called *after* the `setData` method.
  201. */
  202. addByteSize(ref, byteSize) {
  203. const imageData = this._imageCache.get(ref);
  204. if (!imageData) {
  205. return; // The image data isn't cached (the limit was reached).
  206. }
  207. if (imageData.byteSize) {
  208. return; // The byte-size has already been set.
  209. }
  210. imageData.byteSize = byteSize;
  211. }
  212. getData(ref, pageIndex) {
  213. const pageIndexSet = this._refCache.get(ref);
  214. if (!pageIndexSet) {
  215. return null;
  216. }
  217. if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
  218. return null;
  219. }
  220. const imageData = this._imageCache.get(ref);
  221. if (!imageData) {
  222. return null;
  223. }
  224. // Ensure that we keep track of all pages containing the image reference.
  225. pageIndexSet.add(pageIndex);
  226. return imageData;
  227. }
  228. setData(ref, data) {
  229. if (!this._refCache.has(ref)) {
  230. throw new Error(
  231. 'GlobalImageCache.setData - expected "addPageIndex" to have been called.'
  232. );
  233. }
  234. if (this._imageCache.has(ref)) {
  235. return;
  236. }
  237. if (this._cacheLimitReached) {
  238. warn("GlobalImageCache.setData - cache limit reached.");
  239. return;
  240. }
  241. this._imageCache.put(ref, data);
  242. }
  243. clear(onlyData = false) {
  244. if (!onlyData) {
  245. this._refCache.clear();
  246. }
  247. this._imageCache.clear();
  248. }
  249. }
  250. export {
  251. GlobalImageCache,
  252. LocalColorSpaceCache,
  253. LocalFunctionCache,
  254. LocalGStateCache,
  255. LocalImageCache,
  256. LocalTilingPatternCache,
  257. };