Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkData.h" | 11 #include "SkData.h" |
| 12 #if !defined(GOOGLE3) | 12 #if !defined(GOOGLE3) |
| 13 #include "SkJpegCodec.h" | 13 #include "SkJpegCodec.h" |
| 14 #endif | 14 #endif |
| 15 #include "SkRawCodec.h" | 15 #include "SkRawCodec.h" |
| 16 #include "SkRefCnt.h" | 16 #include "SkRefCnt.h" |
| 17 #include "SkStream.h" | 17 #include "SkStream.h" |
| 18 #include "SkStreamPriv.h" | 18 #include "SkStreamPriv.h" |
| 19 #include "SkSwizzler.h" | 19 #include "SkSwizzler.h" |
| 20 #include "SkTaskGroup.h" | |
| 20 #include "SkTemplates.h" | 21 #include "SkTemplates.h" |
| 21 #include "SkTypes.h" | 22 #include "SkTypes.h" |
| 22 | 23 |
| 24 #include "dng_area_task.h" | |
| 23 #include "dng_color_space.h" | 25 #include "dng_color_space.h" |
| 24 #include "dng_exceptions.h" | 26 #include "dng_exceptions.h" |
| 25 #include "dng_host.h" | 27 #include "dng_host.h" |
| 26 #include "dng_info.h" | 28 #include "dng_info.h" |
| 27 #include "dng_memory.h" | 29 #include "dng_memory.h" |
| 28 #include "dng_render.h" | 30 #include "dng_render.h" |
| 29 #include "dng_stream.h" | 31 #include "dng_stream.h" |
| 30 | 32 |
| 31 #include "src/piex.h" | 33 #include "src/piex.h" |
| 32 | 34 |
| 33 #include <cmath> // for std::round,floor,ceil | 35 #include <cmath> // for std::round,floor,ceil |
| 34 #include <limits> | 36 #include <limits> |
| 35 | 37 |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 40 // Caluclates the number of tiles of tile_size that fit into the area in vertica l and horizontal | |
| 41 // directions. | |
| 42 dng_point num_tiles_in_area(const dng_point &areaSize, | |
| 43 const dng_point_real64 &tileSize) { | |
| 44 return dng_point((areaSize.v + tileSize.v - 1) / tileSize.v, | |
| 45 (areaSize.h + tileSize.h - 1) / tileSize.h); | |
| 46 } | |
| 47 | |
| 48 int num_threads_required(const dng_point& tilesInThread, | |
| 49 const dng_point& tilesInArea) { | |
| 50 return ((tilesInArea.v + tilesInThread.v - 1) / tilesInThread.v) * | |
| 51 ((tilesInArea.h + tilesInThread.h - 1) / tilesInThread.h); | |
| 52 } | |
| 53 | |
| 54 // Calculate the number of tiles to process per thread, taking into account the maximum number of | |
| 55 // threads. | |
| 56 dng_point num_tiles_per_thread(const int maxThreads, | |
| 57 const dng_point &tilesInArea) { | |
| 58 dng_point tilesInThread = {1, 1}; | |
| 59 while (num_threads_required(tilesInThread, tilesInArea) > maxThreads) { | |
| 60 if (tilesInThread.h < tilesInArea.h) { | |
| 61 ++tilesInThread.h; | |
| 62 } else if (tilesInThread.v < tilesInArea.v) { | |
| 63 ++tilesInThread.v; | |
| 64 } else { | |
| 65 ThrowProgramError("num_tiles_per_thread calculation is wrong."); | |
| 66 } | |
| 67 } | |
| 68 return tilesInThread; | |
| 69 } | |
| 70 | |
| 71 class SkDngHost : public dng_host { | |
| 72 public: | |
| 73 using dng_host::dng_host; | |
| 74 | |
| 75 void PerformAreaTask(dng_area_task& task, const dng_rect& area) override { | |
| 76 const int maxThreads = static_cast<int>(kMaxMPThreads); | |
| 77 SkTaskGroup taskGroup; | |
| 78 | |
| 79 const dng_point tileSize(task.FindTileSize(area)); | |
| 80 const dng_point tilesInArea = num_tiles_in_area(area.Size(), tileSize); | |
| 81 | |
| 82 const dng_point tilesInThread = num_tiles_per_thread(maxThreads, tilesIn Area); | |
| 83 const dng_point threadAreaSize(tilesInThread.v * tileSize.v, tilesInThre ad.h * tileSize.h); | |
| 84 dng_rect threadArea = dng_rect(threadAreaSize) + area.TL(); | |
| 85 | |
| 86 const int numThreads = num_threads_required(tilesInThread, tilesInArea); | |
| 87 | |
| 88 task.Start(numThreads, tileSize, &Allocator(), Sniffer()); | |
| 89 int taskIndex = 0; | |
| 90 for (int v = 0; v < tilesInArea.v; v += tilesInThread.v) { | |
| 91 for (int h = 0; h < tilesInArea.h; h += tilesInThread.h) { | |
| 92 threadArea.l = area.l + h * tileSize.h; | |
| 93 threadArea.t = area.t + v * tileSize.v; | |
| 94 threadArea.r = Min_int32(threadArea.l + threadAreaSize.h, area.r ); | |
| 95 threadArea.b = Min_int32(threadArea.t + threadAreaSize.v, area.b ); | |
| 96 | |
| 97 taskGroup.add([&task, this, taskIndex, threadArea, tileSize] { | |
| 98 task.ProcessOnThread(taskIndex, threadArea, tileSize, this-> Sniffer()); | |
| 99 }); | |
| 100 } | |
| 101 ++taskIndex; | |
|
adaubert
2016/01/26 14:20:24
++taskIndex; should be in the inner scope.
ebrauer
2016/01/26 14:28:17
Done.
| |
| 102 } | |
| 103 | |
| 104 taskGroup.wait(); | |
| 105 task.Finish(numThreads); | |
| 106 } | |
| 107 | |
| 108 uint32 PerformAreaTaskThreads() override { | |
| 109 return kMaxMPThreads; | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 typedef dng_host INHERITED; | |
| 114 }; | |
| 115 | |
| 38 // T must be unsigned type. | 116 // T must be unsigned type. |
| 39 template <class T> | 117 template <class T> |
| 40 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { | 118 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { |
| 41 SkASSERT(arg1 >= 0); | 119 SkASSERT(arg1 >= 0); |
| 42 SkASSERT(arg2 >= 0); | 120 SkASSERT(arg2 >= 0); |
| 43 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { | 121 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { |
| 44 T sum = arg1 + arg2; | 122 T sum = arg1 + arg2; |
| 45 if (sum <= std::numeric_limits<size_t>::max()) { | 123 if (sum <= std::numeric_limits<size_t>::max()) { |
| 46 *result = static_cast<size_t>(sum); | 124 *result = static_cast<size_t>(sum); |
| 47 return true; | 125 return true; |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 return fIsScalable; | 359 return fIsScalable; |
| 282 } | 360 } |
| 283 | 361 |
| 284 bool isXtransImage() const { | 362 bool isXtransImage() const { |
| 285 return fIsXtransImage; | 363 return fIsXtransImage; |
| 286 } | 364 } |
| 287 | 365 |
| 288 private: | 366 private: |
| 289 bool readDng() { | 367 bool readDng() { |
| 290 // Due to the limit of DNG SDK, we need to reset host and info. | 368 // Due to the limit of DNG SDK, we need to reset host and info. |
| 291 fHost.reset(new dng_host(&fAllocator)); | 369 fHost.reset(new SkDngHost(&fAllocator)); |
| 292 fInfo.reset(new dng_info); | 370 fInfo.reset(new dng_info); |
| 293 fDngStream.reset(new SkDngStream(fStream)); | 371 fDngStream.reset(new SkDngStream(fStream)); |
| 294 try { | 372 try { |
| 295 fHost->ValidateSizes(); | 373 fHost->ValidateSizes(); |
| 296 fInfo->Parse(*fHost, *fDngStream); | 374 fInfo->Parse(*fHost, *fDngStream); |
| 297 fInfo->PostParse(*fHost); | 375 fInfo->PostParse(*fHost); |
| 298 if (!fInfo->IsValidDNG()) { | 376 if (!fInfo->IsValidDNG()) { |
| 299 return false; | 377 return false; |
| 300 } | 378 } |
| 301 | 379 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); | 544 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); |
| 467 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); | 545 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); |
| 468 return sizeFloor == dim || sizeCeil == dim; | 546 return sizeFloor == dim || sizeCeil == dim; |
| 469 } | 547 } |
| 470 | 548 |
| 471 SkRawCodec::~SkRawCodec() {} | 549 SkRawCodec::~SkRawCodec() {} |
| 472 | 550 |
| 473 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | 551 SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
| 474 : INHERITED(dngImage->getImageInfo(), nullptr) | 552 : INHERITED(dngImage->getImageInfo(), nullptr) |
| 475 , fDngImage(dngImage) {} | 553 , fDngImage(dngImage) {} |
| OLD | NEW |