Chromium Code Reviews| Index: src/codec/SkRawCodec.cpp |
| diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp |
| index 05f18ac635918d448149fe112374ebd57636789c..955d54652782aa5b8207354fd1507811da27214d 100644 |
| --- a/src/codec/SkRawCodec.cpp |
| +++ b/src/codec/SkRawCodec.cpp |
| @@ -17,9 +17,11 @@ |
| #include "SkStream.h" |
| #include "SkStreamPriv.h" |
| #include "SkSwizzler.h" |
| +#include "SkTaskGroup.h" |
| #include "SkTemplates.h" |
| #include "SkTypes.h" |
| +#include "dng_area_task.h" |
| #include "dng_color_space.h" |
| #include "dng_exceptions.h" |
| #include "dng_host.h" |
| @@ -35,6 +37,82 @@ |
| namespace { |
| +// Caluclates the number of tiles of tile_size that fit into the area in vertical and horizontal |
| +// directions. |
| +dng_point num_tiles_in_area(const dng_point &areaSize, |
| + const dng_point_real64 &tileSize) { |
| + return dng_point((areaSize.v + tileSize.v - 1) / tileSize.v, |
| + (areaSize.h + tileSize.h - 1) / tileSize.h); |
| +} |
| + |
| +int num_threads_required(const dng_point& tilesInThread, |
| + const dng_point& tilesInArea) { |
| + return ((tilesInArea.v + tilesInThread.v - 1) / tilesInThread.v) * |
| + ((tilesInArea.h + tilesInThread.h - 1) / tilesInThread.h); |
| +} |
| + |
| +// Calculate the number of tiles to process per thread, taking into account the maximum number of |
| +// threads. |
| +dng_point num_tiles_per_thread(const int maxThreads, |
| + const dng_point &tilesInArea) { |
| + dng_point tilesInThread = {1, 1}; |
| + while (num_threads_required(tilesInThread, tilesInArea) > maxThreads) { |
| + if (tilesInThread.h < tilesInArea.h) { |
| + ++tilesInThread.h; |
| + } else if (tilesInThread.v < tilesInArea.v) { |
| + ++tilesInThread.v; |
| + } else { |
| + ThrowProgramError("num_tiles_per_thread calculation is wrong."); |
| + } |
| + } |
| + return tilesInThread; |
| +} |
| + |
| +class SkDngHost : public dng_host { |
| +public: |
| + using dng_host::dng_host; |
| + |
| + void PerformAreaTask(dng_area_task& task, const dng_rect& area) override { |
| + const int maxThreads = static_cast<int>(kMaxMPThreads); |
| + SkTaskGroup taskGroup; |
| + |
| + const dng_point tileSize(task.FindTileSize(area)); |
| + const dng_point tilesInArea = num_tiles_in_area(area.Size(), tileSize); |
| + |
| + const dng_point tilesInThread = num_tiles_per_thread(maxThreads, tilesInArea); |
| + const dng_point threadAreaSize(tilesInThread.v * tileSize.v, tilesInThread.h * tileSize.h); |
| + dng_rect threadArea = dng_rect(threadAreaSize) + area.TL(); |
| + |
| + const int numThreads = num_threads_required(tilesInThread, tilesInArea); |
| + |
| + task.Start(numThreads, tileSize, &Allocator(), Sniffer()); |
| + int taskIndex = 0; |
| + for (int v = 0; v < tilesInArea.v; v += tilesInThread.v) { |
| + for (int h = 0; h < tilesInArea.h; h += tilesInThread.h) { |
| + threadArea.l = area.l + h * tileSize.h; |
| + threadArea.t = area.t + v * tileSize.v; |
| + threadArea.r = Min_int32(threadArea.l + threadAreaSize.h, area.r); |
| + threadArea.b = Min_int32(threadArea.t + threadAreaSize.v, area.b); |
| + |
| + taskGroup.add([&task, this, taskIndex, threadArea, tileSize] { |
| + task.ProcessOnThread(taskIndex, threadArea, tileSize, this->Sniffer()); |
| + }); |
| + } |
| + ++taskIndex; |
|
adaubert
2016/01/26 14:20:24
++taskIndex; should be in the inner scope.
ebrauer
2016/01/26 14:28:17
Done.
|
| + } |
| + |
| + taskGroup.wait(); |
| + task.Finish(numThreads); |
| + } |
| + |
| + uint32 PerformAreaTaskThreads() override { |
| + return kMaxMPThreads; |
| + } |
| + |
| +private: |
| + typedef dng_host INHERITED; |
| +}; |
| + |
| // T must be unsigned type. |
| template <class T> |
| bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { |
| @@ -288,7 +366,7 @@ public: |
| private: |
| bool readDng() { |
| // Due to the limit of DNG SDK, we need to reset host and info. |
| - fHost.reset(new dng_host(&fAllocator)); |
| + fHost.reset(new SkDngHost(&fAllocator)); |
| fInfo.reset(new dng_info); |
| fDngStream.reset(new SkDngStream(fStream)); |
| try { |