Skip to content

Commit

Permalink
fix bug in webp sample for releasing memory and function return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ggfan committed Mar 31, 2017
1 parent db9f73f commit e468e98
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion webp/view/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ add_library(app-glue STATIC
include_directories(${webp_src_dir}/examples
${webp_src_dir}/src)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")
add_library(webp-view SHARED src/main/cpp/webp_decode.cpp
src/main/cpp/webp_view.cpp)

Expand Down
5 changes: 1 addition & 4 deletions webp/view/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ apply plugin: 'com.android.application'
cmake {
// when using arguments, the latest (cmake version 3.6+, android studio
// beta2+) must be used to compile

// I have to use gcc to compile, clang compiled code generate error
// at run time for any std::queue etc.
arguments '-DANDROID_STL=gnustl_static', '-DANDROID_TOOLCHAIN=gcc'
arguments '-DANDROID_STL=c++_static', '-DANDROID_TOOLCHAIN=clang'
arguments '-DANDROID_PLATFORM=android-9'
}
}
Expand Down
15 changes: 11 additions & 4 deletions webp/view/src/main/cpp/webp_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ uint8_t* WebpDecoder::GetDecodedFrame(void) {
*/
void* DecodeFrame(void * decoder) {
reinterpret_cast<WebpDecoder*>(decoder)->DecodeFrameInternal();
return nullptr;
}

/*
Expand Down Expand Up @@ -111,6 +112,7 @@ void WebpDecoder::DecodeFrameInternal() {
break;
default:
assert( 0 );
delete [] buf;
return;
}
config.output.width = bufInfo_.width_;
Expand All @@ -124,7 +126,7 @@ void WebpDecoder::DecodeFrameInternal() {

status = WebPDecode(buf, len, &config);
WebPFreeDecBuffer(&config.output);
delete buf;
delete [] buf;

// only change state when it is decoded OK. Our small decoder engine will be
// in decoding state when error happens
Expand Down Expand Up @@ -162,11 +164,16 @@ bool WebpDecoder::DecodeFrame(void) {
pthread_attr_init( &attrib);
pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_DETACHED);
int status = pthread_create(&worker_, &attrib, ::DecodeFrame, this);
assert(status == 0 );
pthread_attr_destroy(&attrib);

state_ = state_decoding;
return true;
if (status == 0) {
state_ = state_decoding;
return true;
}

// create thread failed...
assert(false);
return false;
}

/*
Expand Down
9 changes: 7 additions & 2 deletions webp/view/src/main/cpp/webp_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ const int kFRAME_DISPLAY_TIME = 2;
class Engine {
public:
explicit Engine(android_app* app) :
app_(app), animating_(false), decoder_(nullptr) {
app_(app),
decoder_(nullptr),
animating_(false) {
memset(&frameStartTime_, 0, sizeof(frameStartTime_));
}

~Engine() {}

struct android_app* AndroidApp(void) const { return app_; }
Expand All @@ -66,8 +69,8 @@ class Engine {

private:
void UpdateFrameBuffer(ANativeWindow_Buffer* buf, uint8_t* src);
WebpDecoder* decoder_;
struct android_app* app_;
WebpDecoder* decoder_;
bool animating_;
struct timespec frameStartTime_;
};
Expand Down Expand Up @@ -203,6 +206,8 @@ bool Engine::PrepareDrawing(void) {
return false;
}
decoder_->DecodeFrame();

return true;
}

/*
Expand Down

0 comments on commit e468e98

Please sign in to comment.