Skip to content

Commit a84ac25

Browse files
committed
improve scheduler singleton implementation
1 parent 6a364d7 commit a84ac25

17 files changed

Lines changed: 197 additions & 173 deletions

File tree

examples/codec/codec_compress.cpp

Lines changed: 128 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -27,154 +27,160 @@ const std::string dataRoot = GRK_DATA_ROOT;
2727

2828
int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
2929
{
30-
const uint32_t dimX = 640;
31-
const uint32_t dimY = 480;
32-
const uint32_t numComps = 3;
33-
const uint32_t precision = 8;
34-
grk_image_comp* compParams = nullptr;
35-
grk_image* image = nullptr;
36-
grk_stream_params streamParams;
37-
grk_set_default_stream_params(&streamParams);
38-
39-
bool inputFromImage = true;
40-
bool outputToBuffer = true;
41-
42-
if(outputToBuffer)
43-
{
44-
streamParams.buf_len = (size_t)numComps * (precision / 8) * dimX * dimY;
45-
streamParams.buf = new uint8_t[streamParams.buf_len];
46-
}
47-
48-
std::vector<std::string> argString;
49-
std::vector<char*> args;
50-
51-
std::string inputFile, outputFile;
5230
int rc = 1;
53-
54-
if(inputFromImage)
31+
// perform two identical compressions
32+
for(uint32_t i = 0; i < 2; ++i)
5533
{
56-
// create blank image
57-
compParams = new grk_image_comp[numComps];
58-
for(uint32_t i = 0; i < numComps; ++i)
34+
const uint32_t dimX = 640;
35+
const uint32_t dimY = 480;
36+
const uint32_t numComps = 3;
37+
const uint32_t precision = 8;
38+
grk_image_comp* compParams = nullptr;
39+
grk_image* image = nullptr;
40+
grk_stream_params streamParams;
41+
grk_set_default_stream_params(&streamParams);
42+
bool inputFromImage = true;
43+
bool outputToBuffer = true;
44+
if(outputToBuffer)
5945
{
60-
auto c = compParams + i;
61-
c->w = dimX;
62-
c->h = dimY;
63-
c->dx = 1;
64-
c->dy = 1;
65-
c->prec = precision;
66-
c->sgnd = false;
46+
streamParams.buf_len = (size_t)numComps * (precision / 8) * dimX * dimY;
47+
streamParams.buf = new uint8_t[streamParams.buf_len];
6748
}
68-
image = grk_image_new(numComps, compParams, GRK_CLRSPC_SRGB, true);
49+
std::vector<std::string> argString;
50+
std::vector<char*> args;
51+
std::string inputFile, outputFile;
6952

70-
// fill in component data
71-
// see grok.h header for full details of image structure
72-
for(uint16_t compno = 0; compno < image->numcomps; ++compno)
53+
if(inputFromImage)
7354
{
74-
auto comp = image->comps + compno;
75-
auto compWidth = comp->w;
76-
auto compHeight = comp->h;
77-
auto compData = comp->data;
78-
if(!compData)
55+
// create blank image
56+
compParams = new grk_image_comp[numComps];
57+
for(uint32_t i = 0; i < numComps; ++i)
7958
{
80-
fprintf(stderr, "Image has null data for component %d\n", compno);
81-
goto beach;
59+
auto c = compParams + i;
60+
memset(c, 0, sizeof(grk_image_comp));
61+
c->w = dimX;
62+
c->h = dimY;
63+
c->dx = 1;
64+
c->dy = 1;
65+
c->prec = precision;
66+
c->sgnd = false;
8267
}
83-
// fill in component data, taking component stride into account
84-
auto srcData = new int32_t[compWidth * compHeight];
85-
auto srcPtr = srcData;
86-
for(uint32_t j = 0; j < compHeight; ++j)
68+
image = grk_image_new(numComps, compParams, GRK_CLRSPC_SRGB, true);
69+
70+
// fill in component data
71+
// see grok.h header for full details of image structure
72+
for(uint16_t compno = 0; compno < image->numcomps; ++compno)
8773
{
88-
memcpy(compData, srcPtr, compWidth * sizeof(int32_t));
89-
srcPtr += compWidth;
90-
compData += comp->stride;
74+
auto comp = image->comps + compno;
75+
auto compWidth = comp->w;
76+
auto compHeight = comp->h;
77+
auto compData = comp->data;
78+
if(!compData)
79+
{
80+
fprintf(stderr, "Image has null data for component %d\n", compno);
81+
goto beach;
82+
}
83+
// fill in component data, taking component stride into account
84+
// Note: in this example, we just fill the buffer with a constant value whose precision
85+
// matches the precision specified above.
86+
// !! do not pass in data whose precision exceeds the precision specified above
87+
auto srcData = new int32_t[compWidth * compHeight];
88+
for(uint32_t i = 0; i < compWidth * compHeight; ++i)
89+
srcData[i] = 0xFF;
90+
auto srcPtr = srcData;
91+
for(uint32_t j = 0; j < compHeight; ++j)
92+
{
93+
memcpy(compData, srcPtr, compWidth * sizeof(int32_t));
94+
srcPtr += compWidth;
95+
compData += comp->stride;
96+
}
97+
delete[] srcData;
9198
}
92-
delete[] srcData;
9399
}
94-
}
95100

96-
// 1. form vector of command line args
101+
// 1. form vector of command line args
97102

98-
// first entry must always be the name of the program, as is
99-
// required by argv/argc variables in main method
100-
argString.push_back("codec_compress");
103+
// first entry must always be the name of the program, as is
104+
// required by argv/argc variables in main method
105+
argString.push_back("codec_compress");
101106

102-
// verbose output
103-
argString.push_back("-v");
107+
// verbose output
108+
argString.push_back("-v");
104109

105-
// a file can be passed in as a command line argument
106-
// example:
107-
// $ codec_compress foo.tif
108-
// otherwise a file from the Grok test suite, specified below, will be used.
110+
// a file can be passed in as a command line argument
111+
// example:
112+
// $ codec_compress foo.tif
113+
// otherwise a file from the Grok test suite, specified below, will be used.
109114

110-
inputFile = dataRoot + std::filesystem::path::preferred_separator + "input" +
111-
std::filesystem::path::preferred_separator + "nonregression" +
112-
std::filesystem::path::preferred_separator + "basn6a08.tif";
113-
outputFile = "basn6a08.jp2";
114-
if(argc > 1)
115-
{
116-
inputFile = argv[1];
117-
outputFile = inputFile + ".tif";
118-
}
119-
if(!inputFromImage)
120-
{
121-
argString.push_back("-i " + inputFile);
122-
}
115+
inputFile = dataRoot + std::filesystem::path::preferred_separator + "input" +
116+
std::filesystem::path::preferred_separator + "nonregression" +
117+
std::filesystem::path::preferred_separator + "basn6a08.tif";
118+
outputFile = "basn6a08.jp2";
119+
if(argc > 1)
120+
{
121+
inputFile = argv[1];
122+
outputFile = inputFile + ".tif";
123+
}
124+
if(!inputFromImage)
125+
{
126+
argString.push_back("-i " + inputFile);
127+
}
123128

124-
if(outputToBuffer)
125-
{
126-
argString.push_back("-out_fmt jp2");
127-
}
128-
else
129-
{
130-
argString.push_back("-o " + outputFile);
131-
}
129+
if(outputToBuffer)
130+
{
131+
argString.push_back("-out_fmt jp2");
132+
}
133+
else
134+
{
135+
argString.push_back("-o " + outputFile);
136+
}
132137

133-
// 2. convert to array of C strings
134-
for(auto& s : argString)
135-
{
136-
char* arg = new char[s.size() + 1];
137-
copy(s.begin(), s.end(), arg);
138-
arg[s.size()] = '\0';
139-
args.push_back(arg);
140-
}
138+
// 2. convert to array of C strings
139+
for(auto& s : argString)
140+
{
141+
char* arg = new char[s.size() + 1];
142+
copy(s.begin(), s.end(), arg);
143+
arg[s.size()] = '\0';
144+
args.push_back(arg);
145+
}
141146

142-
// 3. decompress
143-
rc = grk_codec_compress((int)args.size(), &args[0], image,
144-
outputToBuffer ? &streamParams : nullptr);
145-
if(rc)
146-
fprintf(stderr, "Failed to compress\n");
147+
// 3. decompress
148+
rc = grk_codec_compress((int)args.size(), &args[0], image,
149+
outputToBuffer ? &streamParams : nullptr);
150+
if(rc)
151+
fprintf(stderr, "Failed to compress\n");
147152

148-
if(outputToBuffer)
149-
printf("Compressed to memory : %ld bytes\n", streamParams.buf_compressed_len);
153+
if(outputToBuffer)
154+
printf("Compressed to memory : %ld bytes\n", streamParams.buf_compressed_len);
150155

151-
// write buffer to file
152-
if(outputToBuffer)
153-
{
154-
auto fp = fopen(outputFile.c_str(), "wb");
155-
if(!fp)
156-
{
157-
fprintf(stderr, "Buffer compress: failed to open file %s for writing", outputFile.c_str());
158-
}
159-
else
156+
// write buffer to file
157+
if(outputToBuffer)
160158
{
161-
size_t written = fwrite(streamParams.buf, 1, streamParams.buf_compressed_len, fp);
162-
if(written != streamParams.buf_compressed_len)
159+
auto fp = fopen(outputFile.c_str(), "wb");
160+
if(!fp)
163161
{
164-
fprintf(stderr, "Buffer compress: only %ld bytes written out of %ld total",
165-
streamParams.buf_compressed_len, written);
162+
fprintf(stderr, "Buffer compress: failed to open file %s for writing", outputFile.c_str());
163+
}
164+
else
165+
{
166+
size_t written = fwrite(streamParams.buf, 1, streamParams.buf_compressed_len, fp);
167+
if(written != streamParams.buf_compressed_len)
168+
{
169+
fprintf(stderr, "Buffer compress: only %ld bytes written out of %ld total",
170+
streamParams.buf_compressed_len, written);
171+
}
172+
fclose(fp);
166173
}
167-
fclose(fp);
168174
}
169-
}
170175

171-
beach:
172-
// 4. cleanup
173-
for(auto& s : args)
174-
delete[] s;
175-
delete[] compParams;
176-
delete[] streamParams.buf;
177-
grk_object_unref(&image->obj);
176+
beach:
177+
// 4. cleanup
178+
for(auto& s : args)
179+
delete[] s;
180+
delete[] compParams;
181+
delete[] streamParams.buf;
182+
grk_object_unref(&image->obj);
183+
}
178184

179185
return rc;
180186
}

src/lib/codec/image_format/PNGFormat.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,11 @@ grk_image* PNGFormat::do_decode(grk_cparameters* params)
584584
{
585585
const char* key = text_ptr[i].key;
586586
if(!strcmp(key, "Description"))
587-
{
588-
}
587+
{}
589588
else if(!strcmp(key, "Author"))
590-
{
591-
}
589+
{}
592590
else if(!strcmp(key, "Title"))
593-
{
594-
}
591+
{}
595592
else if(!strcmp(key, "XML:com.adobe.xmp"))
596593
{
597594
if(text_ptr[i].text_length)
@@ -604,8 +601,7 @@ grk_image* PNGFormat::do_decode(grk_cparameters* params)
604601
}
605602
// other comments
606603
else
607-
{
608-
}
604+
{}
609605
}
610606
}
611607

src/lib/core/cache/PLMarkerMgr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ uint32_t PLMarkerMgr::pop(void)
260260
{
261261
// read next packet length
262262
while(currMarkerBuf_->canRead() && !readNextByte(currMarkerBuf_->read(), &rc))
263-
{
264-
}
263+
{}
265264
// advance to next buffer
266265
if(currMarkerBuf_->offset == currMarkerBuf_->len)
267266
{

src/lib/core/canvas/ResWindow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ struct ResWindow
7777
ResSimple tileCompAtRes, ResSimple tileCompAtLowerRes, grk_rect32 resWindow,
7878
grk_rect32 tileCompWindowUnreduced, grk_rect32 tileCompUnreduced, uint32_t FILTER_WIDTH)
7979
: allocated_(false), filterWidth_(FILTER_WIDTH), tileCompAtRes_(tileCompAtRes),
80-
tileCompAtLowerRes_(tileCompAtLowerRes), resWindowBuffer_(new Buf2dAligned(resWindow)),
81-
resWindowBufferSplit_{nullptr, nullptr},
80+
tileCompAtLowerRes_(tileCompAtLowerRes),
81+
resWindowBuffer_(new Buf2dAligned(resWindow)), resWindowBufferSplit_{nullptr, nullptr},
8282
resWindowBufferHighestResREL_(resWindowHighestResREL),
8383
resWindowBufferREL_(new Buf2dAligned(resWindow.width(), resWindow.height())),
8484
resWindowBufferSplitREL_{nullptr, nullptr}

src/lib/core/codestream/CodeStreamCompress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ uint64_t CodeStreamCompress::compress(grk_plugin_tile* tile)
618618
return 0;
619619
}
620620
auto numRequiredThreads =
621-
std::min<uint32_t>((uint32_t)ExecSingleton::get()->num_workers(), numTiles);
621+
std::min<uint32_t>((uint32_t)ExecSingleton::get().num_workers(), numTiles);
622622
std::atomic<bool> success(true);
623623
if(numRequiredThreads > 1)
624624
{

src/lib/core/codestream/CodeStreamDecompress.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ bool CodeStreamDecompress::decompressTiles(void)
464464
return false;
465465

466466
auto numRequiredThreads =
467-
std::min<uint32_t>((uint32_t)ExecSingleton::get()->num_workers(), numTilesToDecompress);
467+
std::min<uint32_t>((uint32_t)ExecSingleton::get().num_workers(), numTilesToDecompress);
468468
if(outputImage_->supportsStripCache(&cp_))
469469
{
470470
uint32_t numStrips = cp_.t_grid_height;
@@ -473,7 +473,7 @@ bool CodeStreamDecompress::decompressTiles(void)
473473
numStrips =
474474
(outputImage_->height() + outputImage_->rowsPerStrip - 1) / outputImage_->rowsPerStrip;
475475
}
476-
stripCache_.init((uint32_t)ExecSingleton::get()->num_workers(), cp_.t_grid_width, numStrips,
476+
stripCache_.init((uint32_t)ExecSingleton::get().num_workers(), cp_.t_grid_width, numStrips,
477477
numTilesToDecompress > 1 ? cp_.t_height : outputImage_->rowsPerStrip,
478478
cp_.coding_params_.dec_.reduce_, outputImage_, ioBufferCallback, ioUserData,
479479
grkRegisterReclaimCallback_);
@@ -885,7 +885,7 @@ bool CodeStreamDecompress::decompressTile(void)
885885
{
886886
uint32_t numStrips =
887887
(outputImage_->height() + outputImage_->rowsPerStrip - 1) / outputImage_->rowsPerStrip;
888-
stripCache_.init((uint32_t)ExecSingleton::get()->num_workers(), 1, numStrips,
888+
stripCache_.init((uint32_t)ExecSingleton::get().num_workers(), 1, numStrips,
889889
outputImage_->rowsPerStrip, cp_.coding_params_.dec_.reduce_, outputImage_,
890890
ioBufferCallback, ioUserData, grkRegisterReclaimCallback_);
891891
}

src/lib/core/grok.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void GRK_CALLCONV grk_initialize(const char* pluginPath, uint32_t numthreads, bo
127127
GRK_API void GRK_CALLCONV grk_deinitialize()
128128
{
129129
grk_plugin_cleanup();
130-
ExecSingleton::release();
130+
ExecSingleton::destroy();
131131
}
132132

133133
GRK_API grk_object* GRK_CALLCONV grk_object_ref(grk_object* obj)

src/lib/core/plugin/minpf_plugin_manager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ static int32_t minpf_load(const char* path, bool verbose)
157157

158158
char fullPath[4096];
159159
if(minpf_get_full_path(path, (void*)postLoadFunc, lib->handle, fullPath, 4096))
160-
{
161-
}
160+
{}
162161
else
163162
{
164163
minpf_unload_dynamic_library(lib);

0 commit comments

Comments
 (0)