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
Make Downloader::getContentSize asynchronous #1217
Comments
Is there any milestone for this enhancement in the coming release? Or is there any temporarily solution for this blocking main thread problem? Thanks. |
Any idea when is this going to be fixed, cc.loader.loadImg blocks main thread even loading local imgs! |
Loading local imgs won't use the downloader to check file size, so it's not the same issue. But if your local images are too large, it will take a little while to load the texture into memory. You can use cc.textureCache.addImageAsync which is asynchonous. |
Why there is no cc.textureCache.addImageAsync API show in the doc? http://www.cocos2d-x.org/reference/html5-js/V3.3/symbols/cc.textureCache.html So any idea how to load online images asynchronous? Thanks. |
Oh, you are right, it was not well considered, I created an issue here: Load online images is asynchronous, which I describe in the current issue is that the getContentSize API is synchronous so it blocks sometimes for just a tiny amount of time. |
I understand the issue is on getContentSize as you described. To make my question more precise, i should ask And thanks for the info in the issue #1507. Is it i just need to make the change as below to the frameworks/js-bindings/bindings/script/jsb_boot.js to solve the blocking issue? |
pandamicro/cocos2d-js@2a124b5 will only solve the issue of @vktrsm I will find some time to do this issue as soon as possible, sorry for keeping you guys waiting |
I'm looking forward to hearing good news from you. Thanks for your effort! |
@pandamicro Thx for the fix!! We didnt understand why addImgAsync was removed on 3.0 (was working in 2.2, docs say is merged into addImage and makes async load when callback is provided, but this was untrue), thats why I was trying to use loadImg. Again thanks. |
I've made a workaround for this issue by modifying the below code to jsbinding/binding/manual/extensions/jsb_cocos2dx_extension_manual.cpp (cocos2d-js 3.2):
Hope this helps while waiting for the official fix :) |
…ve remote image loading
cocos2d/cocos2d-js#1217: Add Downloader::getContentSizeAsync to improveremote image loading
Thanks @thangenius and sorry for being late |
Fixed #1217: make remote image loading fully asynchonous
@thangenius @pandamicro I have try the changes with Cocos2d-JS v3.3 But when the online image success loaded with cc.loader.loadImg, it still cause the app to freeze for awhile. cc.loader.loadImg is called multiple times to load different online image at the same time. cc.loader.loadImg(imageUrl, {isCrossOrigin : true}, function(err, texture) I have done the changes as this #1507 but same. The freeze is noticeable on Android and iOS, but on Mac, it just has very very short time freeze and can be ignored. Thanks. |
@pandamicro thanks for the official fix! I will apply it for my project instead of my workaround. @Zinitter I am not sure if the underlying http library of Downloader limits the number of concurrent http connection or not. If it does not, I think the problem is too many http connections being opened and do read operations at the same time causes the freeze, especially on mobile platform. Have you tried to put the image loading tasks to a queue (an array) and implement an executor who pop only a limited number of tasks at a same time from queue, do loading and repeat until the queue is empty (and may start back if there are more tasks put to queue)?. Try to keep the number of poped tasks at a same time as low as possible on mobile platform (I suggest 1 which means loading sequentially), on desktop, this number can be large. Hope this help you. |
Thanks for your suggestion. I have tested to call cc.loader.loadImg only once to load an online image, the app still freeze when the image success loaded. The freeze might cause by the callback of cc.loader.loadImg. |
@Zinitter Normally, the header request and download process are now totally asynchronous. Can you post your implementation so that we can analyze the reason ? Make sure also you have applied changes in https://github.com/cocos2d/cocos2d-js/pull/1508/files |
cocos2d/cocos2d-js#1217: Improved Downloader API
#1217: Improved jsb.loadRemoteImg implementation
Here is the simple sample code :
When touch a cc.MenuItemSprite, it call this loadImage function. I tested by scrolling the scene, the scene will not move for a while when the image loaded. |
Can you print some log to see where the freeze is happening ?
|
I tested with your code. The freeze happen when showing "2" in log. Mean it freeze a while then only print the "2" in log. There is no freeze before and after "1" in log. p/s : The freeze only happen on testing in Android and iOS devices. When testing on Mac runtime, the freeze is very short and can be ignored. Some question to make sure the patch take effect : If patch applied to the file in js-bindings/bindings/manual and js-bindings/cocos2d-x/ If patch applied to the file in js-bindings/bindings/auto/ Thanks. |
I don't understand, you mean like this ? 1 -> 2 -> freeze -> image show up If you want to know whether the runtime have successfully updated, you can print logs in the C++ logic. |
It is like this : It is because loading the online image take time, so there is free time between 1 -> 2 More specifically |
Sorry guys, I made a mistake, the real reason that the download process was frozen is not only the download process, it's also caused by the So I'm reverting my previous solutions and finally I think @thangenius had the right solution. It's just a problem about when to start another thread. My previous solution start one thread to check the size and then a second thread to download. Even though, I can't go through the problem that we don't have a asynchronous API to init image data. And @thangenius 's solution only start one thread, I'm making the content size check and download process synchronized in the thread. Thanks @thangenius for proposing the solution. |
You welcome @pandamicro :) |
Thanks for your effort to find out the real frozen issue. Do we must invoke Image::initWithImageData after the image data downloaded to buffer? Update : Thanks. |
Do we must invoke Image::initWithImageData after the image data downloaded to buffer?
Why the frozen issue is not happen on Mac but only Android and iOS?
|
What do you mean by image downloaded to file?Could we use this to solve the frozen problem? Thanks for your info, i know more about the folder structure. This issue is closed mean the frozen issue won't be solved soon? |
@thangenius I mean the frozen issue caused by Image::initWithImageData in Android and iOS. |
@Zinitter the fix for issue #1597 means the operation Image::initWithImageData also run in another thread so it will not block (freeze) the render thread anymore. Have you tried to apply it yet? |
I see. I tried to apply only this patch https://github.com/cocos2d/cocos2d-js/pull/1597/files but i can't rebuild the runtime. There are some error on js-bindings/bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp How should apply this patch? Do i need to apply other patch before this? Thanks. |
jsb.load_remote_img block the main thread a while to check the file size.
The text was updated successfully, but these errors were encountered: