Skip to content

Commit 5db7bc6

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "aapt: Preprocess images in parallel."
2 parents 31a7403 + c0f7366 commit 5db7bc6

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

tools/aapt/Bundle.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Bundle {
8383
bool getForce(void) const { return mForce; }
8484
void setForce(bool val) { mForce = val; }
8585
void setGrayscaleTolerance(int val) { mGrayscaleTolerance = val; }
86-
int getGrayscaleTolerance() { return mGrayscaleTolerance; }
86+
int getGrayscaleTolerance() const { return mGrayscaleTolerance; }
8787
bool getMakePackageDirs(void) const { return mMakePackageDirs; }
8888
void setMakePackageDirs(bool val) { mMakePackageDirs = val; }
8989
bool getUpdate(void) const { return mUpdate; }
@@ -166,14 +166,14 @@ class Bundle {
166166
void setExtraPackages(const char* val) { mExtraPackages = val; }
167167
const char* getMaxResVersion() const { return mMaxResVersion; }
168168
void setMaxResVersion(const char * val) { mMaxResVersion = val; }
169-
bool getDebugMode() { return mDebugMode; }
169+
bool getDebugMode() const { return mDebugMode; }
170170
void setDebugMode(bool val) { mDebugMode = val; }
171-
bool getNonConstantId() { return mNonConstantId; }
171+
bool getNonConstantId() const { return mNonConstantId; }
172172
void setNonConstantId(bool val) { mNonConstantId = val; }
173173
const char* getProduct() const { return mProduct; }
174174
void setProduct(const char * val) { mProduct = val; }
175175
void setUseCrunchCache(bool val) { mUseCrunchCache = val; }
176-
bool getUseCrunchCache() { return mUseCrunchCache; }
176+
bool getUseCrunchCache() const { return mUseCrunchCache; }
177177

178178
/*
179179
* Set and get the file specification.

tools/aapt/Images.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ static void write_png(const char* imageName,
964964
compression_type));
965965
}
966966

967-
status_t preProcessImage(Bundle* bundle, const sp<AaptAssets>& assets,
967+
status_t preProcessImage(const Bundle* bundle, const sp<AaptAssets>& assets,
968968
const sp<AaptFile>& file, String8* outNewLeafName)
969969
{
970970
String8 ext(file->getPath().getPathExtension());
@@ -1084,7 +1084,7 @@ status_t preProcessImage(Bundle* bundle, const sp<AaptAssets>& assets,
10841084
return error;
10851085
}
10861086

1087-
status_t preProcessImageToCache(Bundle* bundle, String8 source, String8 dest)
1087+
status_t preProcessImageToCache(const Bundle* bundle, const String8& source, const String8& dest)
10881088
{
10891089
png_structp read_ptr = NULL;
10901090
png_infop read_info = NULL;

tools/aapt/Images.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
using android::String8;
1717

18-
status_t preProcessImage(Bundle* bundle, const sp<AaptAssets>& assets,
18+
status_t preProcessImage(const Bundle* bundle, const sp<AaptAssets>& assets,
1919
const sp<AaptFile>& file, String8* outNewLeafName);
2020

21-
status_t preProcessImageToCache(Bundle* bundle, String8 source, String8 dest);
21+
status_t preProcessImageToCache(const Bundle* bundle, const String8& source, const String8& dest);
2222

2323
status_t postProcessImage(const sp<AaptAssets>& assets,
2424
ResourceTable* table, const sp<AaptFile>& file);

tools/aapt/Resource.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "FileFinder.h"
1515
#include "CacheUpdater.h"
1616

17+
#include <utils/WorkQueue.h>
18+
1719
#if HAVE_PRINTF_ZD
1820
# define ZD "%zd"
1921
# define ZD_TYPE ssize_t
@@ -24,6 +26,9 @@
2426

2527
#define NOISY(x) // x
2628

29+
// Number of threads to use for preprocessing images.
30+
static const size_t MAX_THREADS = 4;
31+
2732
// ==========================================================================
2833
// ==========================================================================
2934
// ==========================================================================
@@ -302,21 +307,52 @@ static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets,
302307
return hasErrors ? UNKNOWN_ERROR : NO_ERROR;
303308
}
304309

305-
static status_t preProcessImages(Bundle* bundle, const sp<AaptAssets>& assets,
310+
class PreProcessImageWorkUnit : public WorkQueue::WorkUnit {
311+
public:
312+
PreProcessImageWorkUnit(const Bundle* bundle, const sp<AaptAssets>& assets,
313+
const sp<AaptFile>& file, volatile bool* hasErrors) :
314+
mBundle(bundle), mAssets(assets), mFile(file), mHasErrors(hasErrors) {
315+
}
316+
317+
virtual bool run() {
318+
status_t status = preProcessImage(mBundle, mAssets, mFile, NULL);
319+
if (status) {
320+
*mHasErrors = true;
321+
}
322+
return true; // continue even if there are errors
323+
}
324+
325+
private:
326+
const Bundle* mBundle;
327+
sp<AaptAssets> mAssets;
328+
sp<AaptFile> mFile;
329+
volatile bool* mHasErrors;
330+
};
331+
332+
static status_t preProcessImages(const Bundle* bundle, const sp<AaptAssets>& assets,
306333
const sp<ResourceTypeSet>& set, const char* type)
307334
{
308-
bool hasErrors = false;
335+
volatile bool hasErrors = false;
309336
ssize_t res = NO_ERROR;
310337
if (bundle->getUseCrunchCache() == false) {
338+
WorkQueue wq(MAX_THREADS, false);
311339
ResourceDirIterator it(set, String8(type));
312-
Vector<sp<AaptFile> > newNameFiles;
313-
Vector<String8> newNamePaths;
314340
while ((res=it.next()) == NO_ERROR) {
315-
res = preProcessImage(bundle, assets, it.getFile(), NULL);
316-
if (res < NO_ERROR) {
341+
PreProcessImageWorkUnit* w = new PreProcessImageWorkUnit(
342+
bundle, assets, it.getFile(), &hasErrors);
343+
status_t status = wq.schedule(w);
344+
if (status) {
345+
fprintf(stderr, "preProcessImages failed: schedule() returned %d\n", status);
317346
hasErrors = true;
347+
delete w;
348+
break;
318349
}
319350
}
351+
status_t status = wq.finish();
352+
if (status) {
353+
fprintf(stderr, "preProcessImages failed: finish() returned %d\n", status);
354+
hasErrors = true;
355+
}
320356
}
321357
return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR;
322358
}

0 commit comments

Comments
 (0)