aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2015-06-17 15:26:15 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-06-17 15:26:15 -0700
commit00b621cfc0dac2a0028757a974de33a78bb8579d (patch)
tree696d1a4560d37af4929a6d0501611ceb88c0b45e /samplecode
parent5a9e2994c9915f76b1e3720f107e87fc952ffab2 (diff)
Add sk_parallel_for()
This should be a drop-in replacement for most for-loops to make them run in parallel: for (int i = 0; i < N; i++) { code... } ~~~> sk_parallel_for(N, [&](int i) { code... }); This is just syntax sugar over SkTaskGroup to make this use case really easy to write. There's no more overhead that we weren't already forced to add using an interface like batch(), and no extra heap allocations. I've replaced 3 uses of SkTaskGroup with sk_parallel_for: 1) My unit tests for SkOnce. 2) Cary's path fuzzer. 3) SkMultiPictureDraw. Performance should be the same. Please compare left and right for readability. :) BUG=skia: No public API changes. TBR=reed@google.com Review URL: https://codereview.chromium.org/1184373003
Diffstat (limited to 'samplecode')
-rw-r--r--samplecode/SamplePathFuzz.cpp61
1 files changed, 25 insertions, 36 deletions
diff --git a/samplecode/SamplePathFuzz.cpp b/samplecode/SamplePathFuzz.cpp
index f2595c59ea..55f7f4129d 100644
--- a/samplecode/SamplePathFuzz.cpp
+++ b/samplecode/SamplePathFuzz.cpp
@@ -617,42 +617,31 @@ static bool contains_only_moveTo(const SkPath& path) {
#include "SkTaskGroup.h"
#include "SkTDArray.h"
-struct ThreadState {
- int fSeed;
- const SkBitmap* fBitmap;
-};
-
-static void test_fuzz(ThreadState* data) {
- FuzzPath fuzzPath;
- fuzzPath.setStrokeOnly();
- fuzzPath.setSeed(data->fSeed);
- fuzzPath.randomize();
- const SkPath& path = fuzzPath.getPath();
- const SkPaint& paint = fuzzPath.getPaint();
- const SkImageInfo& info = data->fBitmap->info();
- SkCanvas* canvas(SkCanvas::NewRasterDirect(info, data->fBitmap->getPixels(),
- data->fBitmap->rowBytes()));
- int w = info.width() / 4;
- int h = info.height() / 4;
- int x = data->fSeed / 4 % 4;
- int y = data->fSeed % 4;
- SkRect clipBounds = SkRect::MakeXYWH(SkIntToScalar(x) * w, SkIntToScalar(y) * h,
- SkIntToScalar(w), SkIntToScalar(h));
- canvas->save();
- canvas->clipRect(clipBounds);
- canvas->translate(SkIntToScalar(x) * w, SkIntToScalar(y) * h);
- canvas->drawPath(path, paint);
- canvas->restore();
-}
-
static void path_fuzz_stroker(SkBitmap* bitmap, int seed) {
- ThreadState states[100];
- for (size_t i = 0; i < SK_ARRAY_COUNT(states); i++) {
- states[i].fSeed = seed + (int) i;
- states[i].fBitmap = bitmap;
- }
- SkTaskGroup tg;
- tg.batch(test_fuzz, states, SK_ARRAY_COUNT(states));
+ sk_parallel_for(100, [&](int i) {
+ int localSeed = seed + i;
+
+ FuzzPath fuzzPath;
+ fuzzPath.setStrokeOnly();
+ fuzzPath.setSeed(localSeed);
+ fuzzPath.randomize();
+ const SkPath& path = fuzzPath.getPath();
+ const SkPaint& paint = fuzzPath.getPaint();
+ const SkImageInfo& info = bitmap->info();
+ SkCanvas* canvas(
+ SkCanvas::NewRasterDirect(info, bitmap->getPixels(), bitmap->rowBytes()));
+ int w = info.width() / 4;
+ int h = info.height() / 4;
+ int x = localSeed / 4 % 4;
+ int y = localSeed % 4;
+ SkRect clipBounds = SkRect::MakeXYWH(SkIntToScalar(x) * w, SkIntToScalar(y) * h,
+ SkIntToScalar(w), SkIntToScalar(h));
+ canvas->save();
+ canvas->clipRect(clipBounds);
+ canvas->translate(SkIntToScalar(x) * w, SkIntToScalar(y) * h);
+ canvas->drawPath(path, paint);
+ canvas->restore();
+ });
}
class PathFuzzView : public SampleView {
@@ -673,7 +662,7 @@ protected:
void onOnceBeforeDraw() override {
fIndex = 0;
- SkImageInfo info(SkImageInfo::MakeN32Premul(SkScalarRoundToInt(width()),
+ SkImageInfo info(SkImageInfo::MakeN32Premul(SkScalarRoundToInt(width()),
SkScalarRoundToInt(height())));
offscreen.allocPixels(info);
path_fuzz_stroker(&offscreen, fIndex);