Merge pull request #846 from jareguo/develop

load image for existing texture
This commit is contained in:
James Chen
2017-09-26 01:59:32 -05:00
committed by GitHub
10 changed files with 207 additions and 18 deletions
@@ -1155,6 +1155,8 @@
826294331AAF001C00CB7CF7 /* HttpAsynConnection-apple.m in Sources */ = {isa = PBXBuildFile; fileRef = 52B47A2A1A5349A3004E4C60 /* HttpAsynConnection-apple.m */; };
826294341AAF003E00CB7CF7 /* HttpClient-apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 52B47A2B1A5349A3004E4C60 /* HttpClient-apple.mm */; };
826294351AAF004C00CB7CF7 /* HttpCookie.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 52B47A2C1A5349A3004E4C60 /* HttpCookie.cpp */; };
844EB11F1F78D4CB00EFE4CD /* CCThreadPool.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 844EB11D1F78D4CA00EFE4CD /* CCThreadPool.cpp */; };
844EB1201F78D4CB00EFE4CD /* CCThreadPool.h in Headers */ = {isa = PBXBuildFile; fileRef = 844EB11E1F78D4CA00EFE4CD /* CCThreadPool.h */; };
A0534A651B872FFD006B03E5 /* CCDownloader-apple.h in Headers */ = {isa = PBXBuildFile; fileRef = A0534A631B872FFD006B03E5 /* CCDownloader-apple.h */; };
A0534A661B872FFD006B03E5 /* CCDownloader-apple.h in Headers */ = {isa = PBXBuildFile; fileRef = A0534A631B872FFD006B03E5 /* CCDownloader-apple.h */; };
A0534A671B872FFD006B03E5 /* CCDownloader-apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = A0534A641B872FFD006B03E5 /* CCDownloader-apple.mm */; };
@@ -2229,6 +2231,8 @@
52B47A2B1A5349A3004E4C60 /* HttpClient-apple.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "HttpClient-apple.mm"; sourceTree = "<group>"; };
52B47A2C1A5349A3004E4C60 /* HttpCookie.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HttpCookie.cpp; sourceTree = "<group>"; };
52B47A2D1A5349A3004E4C60 /* HttpCookie.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpCookie.h; sourceTree = "<group>"; };
844EB11D1F78D4CA00EFE4CD /* CCThreadPool.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCThreadPool.cpp; path = ../base/CCThreadPool.cpp; sourceTree = "<group>"; };
844EB11E1F78D4CA00EFE4CD /* CCThreadPool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCThreadPool.h; path = ../base/CCThreadPool.h; sourceTree = "<group>"; };
A0534A631B872FFD006B03E5 /* CCDownloader-apple.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CCDownloader-apple.h"; sourceTree = "<group>"; };
A0534A641B872FFD006B03E5 /* CCDownloader-apple.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CCDownloader-apple.mm"; sourceTree = "<group>"; };
A0534A691B87306E006B03E5 /* CCIDownloaderImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCIDownloaderImpl.h; sourceTree = "<group>"; };
@@ -2734,6 +2738,8 @@
1A5700A2180BC5E60088DEC7 /* base */ = {
isa = PBXGroup;
children = (
844EB11D1F78D4CA00EFE4CD /* CCThreadPool.cpp */,
844EB11E1F78D4CA00EFE4CD /* CCThreadPool.h */,
A6F0D79F1C2796020029CC44 /* CCStencilStateManager.cpp */,
A6F0D7A01C2796020029CC44 /* CCStencilStateManager.hpp */,
291901411B05895600F8B4BA /* CCNinePatchImageParser.h */,
@@ -4529,6 +4535,7 @@
50ABBEBD1925AB6F00A911A9 /* ccUtils.h in Headers */,
4DED486E1DFFA4AF0070C5C4 /* b2MouseJoint.h in Headers */,
A0534A651B872FFD006B03E5 /* CCDownloader-apple.h in Headers */,
844EB1201F78D4CB00EFE4CD /* CCThreadPool.h in Headers */,
BA679B361CEC373000F875FA /* CCEventAssetsManagerEx.h in Headers */,
50ABC0231926664800A911A9 /* CCGLViewImpl-desktop.h in Headers */,
A63CF0081CD9CF3500A6971D /* CCUIPasswordTextField.h in Headers */,
@@ -5164,6 +5171,7 @@
1A570081180BC5A10088DEC7 /* CCActionManager.cpp in Sources */,
1A570085180BC5A10088DEC7 /* CCActionPageTurn3D.cpp in Sources */,
FAC8F66A1E1DF15D002B17E8 /* SkeletonBinary.c in Sources */,
844EB11F1F78D4CB00EFE4CD /* CCThreadPool.cpp in Sources */,
1A570089180BC5A10088DEC7 /* CCActionProgressTimer.cpp in Sources */,
1A57008D180BC5A10088DEC7 /* CCActionTiledGrid.cpp in Sources */,
1A570091180BC5A10088DEC7 /* CCActionTween.cpp in Sources */,
+2
View File
@@ -233,6 +233,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32\libs\*.*" "$(OutDir)"
<ClCompile Include="..\base\CCRef.cpp" />
<ClCompile Include="..\base\CCScheduler.cpp" />
<ClCompile Include="..\base\CCScriptSupport.cpp" />
<ClCompile Include="..\base\CCThreadPool.cpp" />
<ClCompile Include="..\base\CCTouch.cpp" />
<ClCompile Include="..\base\ccTypes.cpp" />
<ClCompile Include="..\base\CCUserDefault.cpp" />
@@ -481,6 +482,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32\libs\*.*" "$(OutDir)"
<ClInclude Include="..\base\CCRefPtr.h" />
<ClInclude Include="..\base\CCScheduler.h" />
<ClInclude Include="..\base\CCScriptSupport.h" />
<ClInclude Include="..\base\CCThreadPool.h" />
<ClInclude Include="..\base\CCTouch.h" />
<ClInclude Include="..\base\ccTypes.h" />
<ClInclude Include="..\base\CCUserDefault.h" />
+6 -5
View File
@@ -689,9 +689,6 @@
<ClCompile Include="..\base\CCStencilStateManager.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\ui\UIPageViewIndicator.cpp">
<Filter>ui\UIWidgets\ScrollWidget</Filter>
</ClCompile>
<ClCompile Include="..\editor-support\creator\CCScale9Sprite.cpp">
<Filter>creator</Filter>
</ClCompile>
@@ -786,6 +783,9 @@
<ClCompile Include="..\network\WebSocket-libwebsockets.cpp">
<Filter>network\Source Files</Filter>
</ClCompile>
<ClCompile Include="..\base\CCThreadPool.cpp">
<Filter>base</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\cocos2d.h" />
@@ -1480,8 +1480,6 @@
<ClInclude Include="..\..\external\sources\poly2tri\sweep\cdt.h" />
<ClInclude Include="..\..\external\sources\poly2tri\sweep\sweep.h" />
<ClInclude Include="..\..\external\sources\poly2tri\sweep\sweep_context.h" />
<ClInclude Include="..\ui\UIAbstractCheckButton.h" />
<ClInclude Include="..\ui\UITabControl.h" />
<ClInclude Include="..\editor-support\creator\CCCameraNode.h">
<Filter>creator</Filter>
</ClInclude>
@@ -1545,6 +1543,9 @@
<ClInclude Include="..\ui\UIEditBox\UIEditBoxImpl-common.h">
<Filter>ui\UIWidgets\EditBox</Filter>
</ClInclude>
<ClInclude Include="..\base\CCThreadPool.h">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\math\Mat4.inl">
+1
View File
@@ -126,6 +126,7 @@ base/CCProfiling.cpp \
base/CCRef.cpp \
base/CCScheduler.cpp \
base/CCScriptSupport.cpp \
base/CCThreadPool.cpp \
base/CCTouch.cpp \
base/CCUserDefault-android.cpp \
base/CCUserDefault.cpp \
-1
View File
@@ -9,7 +9,6 @@ LOCAL_MODULE_FILENAME := libaudioengine
LOCAL_SRC_FILES := AudioEngine-inl.cpp \
../AudioEngine.cpp \
CCThreadPool.cpp \
AssetFd.cpp \
AudioDecoder.cpp \
AudioDecoderProvider.cpp \
@@ -24,6 +24,7 @@ THE SOFTWARE.
#define LOG_TAG "AudioPlayerProvider"
#include "base/CCThreadPool.h"
#include "audio/android/AudioPlayerProvider.h"
#include "audio/android/UrlAudioPlayer.h"
#include "audio/android/PcmAudioPlayer.h"
@@ -31,7 +32,6 @@ THE SOFTWARE.
#include "audio/android/AudioDecoderProvider.h"
#include "audio/android/AudioMixerController.h"
#include "audio/android/PcmAudioService.h"
#include "audio/android/CCThreadPool.h"
#include "audio/android/ICallerThreadUtils.h"
#include "audio/android/utils/Utils.h"
@@ -25,8 +25,7 @@
****************************************************************************/
#include "audio/android/CCThreadPool.h"
#include <sys/time.h>
#include "base/CCThreadPool.h"
#ifdef __ANDROID__
@@ -165,7 +164,7 @@ bool ThreadPool::tryShrinkPool()
for (int i = 0; i < _maxThreadNum; ++i)
{
if (threadIDsToJoin.size() >= maxThreadNumToJoin)
if ((int)threadIDsToJoin.size() >= maxThreadNumToJoin)
{
break;
}
@@ -330,7 +329,7 @@ void ThreadPool::stopTasksByType(TaskType type)
void ThreadPool::joinThread(int tid)
{
if (tid < 0 || tid >= _threads.size())
if (tid < 0 || tid >= (int)_threads.size())
{
LOGD("Invalid thread id %d\n", tid);
return;
@@ -26,7 +26,7 @@
****************************************************************************/
#pragma once
//#include "platform/CCPlatformMacros.h"
#include "platform/CCStdC.h"
#include <functional>
#include <memory>
@@ -44,7 +44,7 @@ namespace cocos2d { namespace experimental {
* @{
*/
class ThreadPool
class CC_DLL ThreadPool
{
public:
@@ -11,12 +11,14 @@
#include "cocos/scripting/js-bindings/manual/jsb_conversions.hpp"
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
#include "cocos/scripting/js-bindings/auto/jsb_cocos2dx_extension_auto.hpp"
#include "cocos/base/CCThreadPool.h"
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
using namespace cocos2d;
using namespace cocos2d::extension;
using namespace cocos2d::experimental;
static bool jsb_cocos2d_extension_empty_func(se::State& s)
{
@@ -132,6 +134,181 @@ static bool js_cocos2dx_extension_loadRemoteImage(se::State& s)
}
SE_BIND_FUNC(js_cocos2dx_extension_loadRemoteImage)
static bool js_cocos2dx_extension_initRemoteImage(se::State& s)
{
const auto& args = s.args();
int argc = (int)args.size();
if (argc != 3)
{
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool ok = false;
// get texture
cocos2d::Texture2D* texture = nullptr;
ok = seval_to_native_ptr(args[0], &texture);
SE_PRECONDITION2(ok, false, "Converting 'texture' failed!");
// get url
std::string url;
ok = seval_to_std_string(args[1], &url);
SE_PRECONDITION2(ok, false, "Converting 'url' failed!");
// get callback
se::Value func = args[2];
assert(func.isObject() && func.toObject()->isFunction());
func.toObject()->root();
auto onCallback = [=](bool success){
se::ScriptEngine::getInstance()->clearException();
se::AutoHandleScope hs;
se::ValueArray args;
args.resize(1);
args[0].setBoolean(success);
func.toObject()->call(args, nullptr);
};
auto downloader = new (std::nothrow) cocos2d::network::Downloader();
downloader->onDataTaskSuccess = [=](const cocos2d::network::DownloadTask& task, std::vector<unsigned char>& data)
{
bool success = false;
Image* image = new (std::nothrow) Image();
if (image->initWithImageData(data.data(), data.size()))
{
if (texture->initWithImage(image))
{
success = true;
}
else
{
CCLOGERROR("js_cocos2dx_extension_loadRemoteImageOn: Failed to initWithImage.");
}
}
CC_SAFE_RELEASE_NULL(image);
onCallback(success);
Director::getInstance()->getScheduler()->performFunctionInCocosThread([downloader](){
delete downloader;
});
};
downloader->onTaskError = [=](const cocos2d::network::DownloadTask& task, int errorCode, int errorCodeInternal, const std::string& errorStr)
{
onCallback(false);
Director::getInstance()->getScheduler()->performFunctionInCocosThread([downloader](){
delete downloader;
});
};
downloader->createDownloadDataTask(url);
return true;
}
SE_BIND_FUNC(js_cocos2dx_extension_initRemoteImage)
static ThreadPool* _threadPool = nullptr;
static EventListenerCustom* _resetThreadPoolListener = nullptr;
static ThreadPool* getThreadPool()
{
if (_threadPool == nullptr)
{
_threadPool = ThreadPool::newSingleThreadPool();
_resetThreadPoolListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_RESET, [](cocos2d::EventCustom*){
CC_SAFE_DELETE(_threadPool);
cocos2d::Director::getInstance()->getEventDispatcher()->removeEventListener(_resetThreadPoolListener);
_resetThreadPoolListener = nullptr;
});
}
return _threadPool;
}
static bool js_cocos2dx_extension_initTextureAsync(se::State& s)
{
const auto& args = s.args();
int argc = (int)args.size();
if (argc != 3)
{
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool ok = false;
// get texture
cocos2d::Texture2D* texture = nullptr;
ok = seval_to_native_ptr(args[0], &texture);
SE_PRECONDITION2(ok, false, "Converting 'texture' failed!");
// get url
std::string url;
ok = seval_to_std_string(args[1], &url);
SE_PRECONDITION2(ok, false, "Converting 'url' failed!");
// get callback
se::Value func = args[2];
assert(func.isObject() && func.toObject()->isFunction());
func.toObject()->root();
auto onCallback = [=](bool success){
se::ScriptEngine::getInstance()->clearException();
se::AutoHandleScope hs;
se::ValueArray args;
args.resize(1);
args[0].setBoolean(success);
func.toObject()->call(args, nullptr);
};
// getDataFromFile isn't thread safe since fullPathForFilename usage in getDataFromFile isn't.
// So we should pass full path to getDataFromFile to avoid multi-thread issues.
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(url);
getThreadPool()->pushTask([texture, onCallback, fullPath](int tid){
Data data = FileUtils::getInstance()->getDataFromFile(fullPath);
if (!data.isNull())
{
Image* image = new (std::nothrow) Image();
if (image->initWithImageData(data.getBytes(), data.getSize()))
{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]() {
if (texture->initWithImage(image))
{
onCallback(true);
}
else
{
CCLOGERROR("js_cocos2dx_extension_initTextureAsync: Failed to init texture with image.");
onCallback(false);
}
CC_SAFE_RELEASE(image);
});
return;
}
else
{
CCLOGERROR("js_cocos2dx_extension_initTextureAsync: Failed to load image.");
CC_SAFE_RELEASE(image);
}
}
else
{
CCLOGERROR("js_cocos2dx_extension_initTextureAsync: Failed to load file.");
}
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=](){
onCallback(false);
});
});
return true;
}
SE_BIND_FUNC(js_cocos2dx_extension_initTextureAsync)
bool register_all_cocos2dx_extension_manual(se::Object* obj)
{
@@ -147,6 +324,8 @@ bool register_all_cocos2dx_extension_manual(se::Object* obj)
e->defineFunction("release", _SE(jsb_cocos2d_extension_empty_func));
}
__jsbObj->defineFunction("loadRemoteImg", _SE(js_cocos2dx_extension_loadRemoteImage));
__jsbObj->defineFunction("initRemoteImg", _SE(js_cocos2dx_extension_initRemoteImage));
__jsbObj->defineFunction("initTextureAsync", _SE(js_cocos2dx_extension_initTextureAsync));
se::ScriptEngine::getInstance()->clearException();
+5 -5
View File
@@ -176,9 +176,7 @@
"cocos/audio/android/AudioResamplerCubic.cpp",
"cocos/audio/android/AudioResamplerCubic.h",
"cocos/audio/android/AudioResamplerPublic.h",
"cocos/audio/android/CCThreadPool.cpp",
"cocos/audio/android/CCThreadPool.h",
"cocos/audio/android/IAudioPlayer.h",
"cocos/audio/android/IAudioPlayer.h",
"cocos/audio/android/ICallerThreadUtils.h",
"cocos/audio/android/IVolumeProvider.h",
"cocos/audio/android/OpenSLHelper.h",
@@ -305,8 +303,10 @@
"cocos/base/CCScriptSupport.cpp",
"cocos/base/CCScriptSupport.h",
"cocos/base/CCStencilStateManager.cpp",
"cocos/base/CCStencilStateManager.hpp",
"cocos/base/CCTouch.cpp",
"cocos/base/CCStencilStateManager.hpp",
"cocos/base/CCThreadPool.cpp",
"cocos/base/CCThreadPool.h",
"cocos/base/CCTouch.cpp",
"cocos/base/CCTouch.h",
"cocos/base/CCUserDefault-android.cpp",
"cocos/base/CCUserDefault-apple.mm",