-
Notifications
You must be signed in to change notification settings - Fork 511
Layer ResourceCache support ImageBItMap As a key #2524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
import Browser from './Browser'; | ||
import { isSVG, isImageBitMap, isString, hasOwn } from './util'; | ||
|
||
type ResourceUrl = string | string[]; | ||
|
||
type ResourceCacheItem = { | ||
image: ImageBitmap; | ||
width: number; | ||
height: number; | ||
refCnt: number; | ||
} | ||
|
||
/** | ||
* resouce key support ImageBitMap by Map | ||
* this.resources.set(imagebitmap,cache); | ||
* https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map | ||
*/ | ||
|
||
|
||
class ResourceCacheMap { | ||
resources: Map<string | ImageBitmap, ResourceCacheItem>; | ||
|
||
//@internal | ||
_errors: Map<string | ImageBitmap, number>; | ||
|
||
constructor() { | ||
this.resources = new Map(); | ||
this._errors = new Map(); | ||
} | ||
|
||
|
||
addResource(url: [string, number | string, number | string], img) { | ||
const imgUrl = this._getImgUrl(url as any); | ||
this.resources.set(imgUrl, { | ||
image: img, | ||
width: +url[1], | ||
height: +url[2], | ||
refCnt: 0 | ||
}) | ||
//is Image | ||
if (img && img.width && img.height && !img.close && Browser.imageBitMap && !Browser.safari && !Browser.iosWeixin) { | ||
if (img.src && isSVG(img.src)) { | ||
return; | ||
} | ||
createImageBitmap(img).then(imageBitmap => { | ||
if (!this.resources.has(imgUrl)) { | ||
//removed | ||
return; | ||
} | ||
this.resources.get(imgUrl).image = imageBitmap; | ||
}); | ||
} | ||
} | ||
|
||
isResourceLoaded(url: ResourceUrl, checkSVG?: boolean) { | ||
if (!url) { | ||
return false; | ||
} | ||
const imgUrl = this._getImgUrl(url); | ||
if (this._errors.has(imgUrl)) { | ||
return true; | ||
} | ||
const img = this.resources.get(imgUrl); | ||
if (!img) { | ||
return false; | ||
} | ||
if (checkSVG && isSVG(url[0]) && (+url[1] > img.width || +url[2] > img.height)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
login(url: string) { | ||
const res = this.resources.get(url); | ||
if (res) { | ||
res.refCnt++; | ||
} | ||
} | ||
|
||
logout(url: string) { | ||
const res = this.resources.get(url); | ||
if (res && res.refCnt-- <= 0) { | ||
if (res.image && res.image.close) { | ||
res.image.close(); | ||
} | ||
this.resources.delete(url); | ||
} | ||
} | ||
|
||
getImage(url: ResourceUrl) { | ||
const imgUrl = this._getImgUrl(url); | ||
if (!this.isResourceLoaded(url) || this._errors.has(imgUrl)) { | ||
return null; | ||
} | ||
return this.resources.get(imgUrl).image; | ||
} | ||
|
||
markErrorResource(url: ResourceUrl) { | ||
const imgUrl = this._getImgUrl(url); | ||
this._errors.set(imgUrl, 1); | ||
} | ||
|
||
merge(res: ResourceCacheMap) { | ||
if (!res) { | ||
return this; | ||
} | ||
const otherResource = res.resources; | ||
if (otherResource) { | ||
otherResource.forEach((value, key) => { | ||
const img = value; | ||
this.addResource([key as string, img.width, img.height], img.image); | ||
}) | ||
} | ||
return this; | ||
} | ||
|
||
forEach(fn: (key: string | ImageBitmap, value: any) => void) { | ||
if (!this.resources) { | ||
return this; | ||
} | ||
this.resources.forEach((value, key) => { | ||
fn(key, value); | ||
}) | ||
return this; | ||
} | ||
|
||
//@internal | ||
_getImgUrl(url: ResourceUrl) { | ||
if (isImageBitMap(url)) { | ||
return url; | ||
} | ||
let imgUrl; | ||
if (Array.isArray(url)) { | ||
imgUrl = url[0]; | ||
} else if (isString(url)) { | ||
imgUrl = url; | ||
} | ||
if (!imgUrl) { | ||
console.error(`get url key error,the url is:`, url); | ||
} | ||
return imgUrl; | ||
} | ||
|
||
remove() { | ||
if (!this.resources) { | ||
return this; | ||
} | ||
this.resources.forEach((value) => { | ||
if (value && value.image && value.image.close) { | ||
value.image.close(); | ||
} | ||
}) | ||
this.resources.clear(); | ||
return this; | ||
} | ||
} | ||
|
||
|
||
export class ResourceCache { | ||
resources: any; | ||
|
||
//@internal | ||
_errors: any; | ||
|
||
constructor() { | ||
this.resources = {}; | ||
this._errors = {}; | ||
} | ||
|
||
addResource(url: [string, number | string, number | string], img) { | ||
this.resources[url[0]] = { | ||
image: img, | ||
width: +url[1], | ||
height: +url[2], | ||
refCnt: 0 | ||
}; | ||
if (img && img.width && img.height && !img.close && Browser.imageBitMap && !Browser.safari && !Browser.iosWeixin) { | ||
if (img.src && isSVG(img.src)) { | ||
return; | ||
} | ||
createImageBitmap(img).then(imageBitmap => { | ||
if (!this.resources[url[0]]) { | ||
//removed | ||
return; | ||
} | ||
this.resources[url[0]].image = imageBitmap; | ||
}); | ||
} | ||
} | ||
|
||
isResourceLoaded(url: ResourceUrl, checkSVG?: boolean) { | ||
if (!url) { | ||
return false; | ||
} | ||
const imgUrl = this._getImgUrl(url); | ||
if (this._errors[imgUrl]) { | ||
return true; | ||
} | ||
const img = this.resources[imgUrl]; | ||
if (!img) { | ||
return false; | ||
} | ||
if (checkSVG && isSVG(url[0]) && (+url[1] > img.width || +url[2] > img.height)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
login(url: string) { | ||
const res = this.resources[url]; | ||
if (res) { | ||
res.refCnt++; | ||
} | ||
} | ||
|
||
logout(url: string) { | ||
const res = this.resources[url]; | ||
if (res && res.refCnt-- <= 0) { | ||
if (res.image && res.image.close) { | ||
res.image.close(); | ||
} | ||
delete this.resources[url]; | ||
} | ||
} | ||
|
||
getImage(url: ResourceUrl) { | ||
const imgUrl = this._getImgUrl(url); | ||
if (!this.isResourceLoaded(url) || this._errors[imgUrl]) { | ||
return null; | ||
} | ||
return this.resources[imgUrl].image; | ||
} | ||
|
||
markErrorResource(url: ResourceUrl) { | ||
this._errors[this._getImgUrl(url)] = 1; | ||
} | ||
|
||
merge(res: any) { | ||
if (!res) { | ||
return this; | ||
} | ||
for (const p in res.resources) { | ||
const img = res.resources[p]; | ||
this.addResource([p, img.width, img.height], img.image); | ||
} | ||
return this; | ||
} | ||
|
||
forEach(fn: (key: string, value: any) => void) { | ||
if (!this.resources) { | ||
return this; | ||
} | ||
for (const p in this.resources) { | ||
if (hasOwn(this.resources, p)) { | ||
fn(p, this.resources[p]); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
//@internal | ||
_getImgUrl(url: ResourceUrl) { | ||
if (!Array.isArray(url)) { | ||
return url; | ||
} | ||
return url[0]; | ||
} | ||
|
||
remove() { | ||
for (const p in this.resources) { | ||
const res = this.resources[p]; | ||
if (res && res.image && res.image.close) { | ||
// close bitmap | ||
res.image.close(); | ||
} | ||
} | ||
this.resources = {}; | ||
} | ||
} | ||
|
||
// Dynamically obtain resource cache instances | ||
export function getResouceCacheInstance(): ResourceCache { | ||
if (Browser.decodeImageInWorker) { | ||
return new ResourceCacheMap() as ResourceCache; | ||
} | ||
return new ResourceCache(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里感觉应该检查url[0]是否已经存在?