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 // FIXME: Add a ceil_div() helper in SkCodecPriv.h |
| 45 return dng_point((areaSize.v + tileSize.v - 1) / tileSize.v, |
| 46 (areaSize.h + tileSize.h - 1) / tileSize.h); |
| 47 } |
| 48 |
| 49 int num_tasks_required(const dng_point& tilesInTask, |
| 50 const dng_point& tilesInArea) { |
| 51 return ((tilesInArea.v + tilesInTask.v - 1) / tilesInTask.v) * |
| 52 ((tilesInArea.h + tilesInTask.h - 1) / tilesInTask.h); |
| 53 } |
| 54 |
| 55 // Calculate the number of tiles to process per task, taking into account the ma
ximum number of |
| 56 // tasks. It prefers to increase horizontally for better locality of reference. |
| 57 dng_point num_tiles_per_task(const int maxTasks, |
| 58 const dng_point &tilesInArea) { |
| 59 dng_point tilesInTask = {1, 1}; |
| 60 while (num_tasks_required(tilesInTask, tilesInArea) > maxTasks) { |
| 61 if (tilesInTask.h < tilesInArea.h) { |
| 62 ++tilesInTask.h; |
| 63 } else if (tilesInTask.v < tilesInArea.v) { |
| 64 ++tilesInTask.v; |
| 65 } else { |
| 66 ThrowProgramError("num_tiles_per_task calculation is wrong."); |
| 67 } |
| 68 } |
| 69 return tilesInTask; |
| 70 } |
| 71 |
| 72 std::vector<dng_rect> compute_task_areas(const int maxTasks, const dng_rect& are
a, |
| 73 const dng_point& tileSize) { |
| 74 std::vector<dng_rect> taskAreas; |
| 75 const dng_point tilesInArea = num_tiles_in_area(area.Size(), tileSize); |
| 76 const dng_point tilesPerTask = num_tiles_per_task(maxTasks, tilesInArea); |
| 77 const dng_point taskAreaSize = {tilesPerTask.v * tileSize.v, |
| 78 tilesPerTask.h * tileSize.h}; |
| 79 for (int v = 0; v < tilesInArea.v; v += tilesPerTask.v) { |
| 80 for (int h = 0; h < tilesInArea.h; h += tilesPerTask.h) { |
| 81 dng_rect taskArea; |
| 82 taskArea.t = area.t + v * tileSize.v; |
| 83 taskArea.l = area.l + h * tileSize.h; |
| 84 taskArea.b = Min_int32(taskArea.t + taskAreaSize.v, area.b); |
| 85 taskArea.r = Min_int32(taskArea.l + taskAreaSize.h, area.r); |
| 86 |
| 87 taskAreas.push_back(taskArea); |
| 88 } |
| 89 } |
| 90 return taskAreas; |
| 91 } |
| 92 |
| 93 class SkDngHost : public dng_host { |
| 94 public: |
| 95 using dng_host::dng_host; |
| 96 |
| 97 void PerformAreaTask(dng_area_task& task, const dng_rect& area) override { |
| 98 // The area task gets split up into max_tasks sub-tasks. The max_tasks i
s defined by the |
| 99 // dng-sdks default implementation of dng_area_task::MaxThreads() which
returns 8 or 32 |
| 100 // sub-tasks depending on the architecture. |
| 101 const int maxTasks = static_cast<int>(task.MaxThreads()); |
| 102 |
| 103 SkTaskGroup taskGroup; |
| 104 |
| 105 // tileSize is typically 256x256 |
| 106 const dng_point tileSize(task.FindTileSize(area)); |
| 107 const std::vector<dng_rect> taskAreas = compute_task_areas(maxTasks, are
a, tileSize); |
| 108 const int numTasks = taskAreas.size(); |
| 109 |
| 110 task.Start(numTasks, tileSize, &Allocator(), Sniffer()); |
| 111 for (int taskIndex = 0; taskIndex < numTasks; ++taskIndex) { |
| 112 taskGroup.add([&task, this, taskIndex, taskAreas, tileSize] { |
| 113 task.ProcessOnThread(taskIndex, taskAreas[taskIndex], tileSize,
this->Sniffer()); |
| 114 }); |
| 115 } |
| 116 |
| 117 taskGroup.wait(); |
| 118 task.Finish(numTasks); |
| 119 } |
| 120 |
| 121 uint32 PerformAreaTaskThreads() override { |
| 122 // FIXME: Need to get the real amount of available threads used in the S
kTaskGroup. |
| 123 return kMaxMPThreads; |
| 124 } |
| 125 |
| 126 private: |
| 127 typedef dng_host INHERITED; |
| 128 }; |
| 129 |
38 // T must be unsigned type. | 130 // T must be unsigned type. |
39 template <class T> | 131 template <class T> |
40 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { | 132 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { |
41 SkASSERT(arg1 >= 0); | 133 SkASSERT(arg1 >= 0); |
42 SkASSERT(arg2 >= 0); | 134 SkASSERT(arg2 >= 0); |
43 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { | 135 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { |
44 T sum = arg1 + arg2; | 136 T sum = arg1 + arg2; |
45 if (sum <= std::numeric_limits<size_t>::max()) { | 137 if (sum <= std::numeric_limits<size_t>::max()) { |
46 *result = static_cast<size_t>(sum); | 138 *result = static_cast<size_t>(sum); |
47 return true; | 139 return true; |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 return fIsScalable; | 373 return fIsScalable; |
282 } | 374 } |
283 | 375 |
284 bool isXtransImage() const { | 376 bool isXtransImage() const { |
285 return fIsXtransImage; | 377 return fIsXtransImage; |
286 } | 378 } |
287 | 379 |
288 private: | 380 private: |
289 bool readDng() { | 381 bool readDng() { |
290 // Due to the limit of DNG SDK, we need to reset host and info. | 382 // Due to the limit of DNG SDK, we need to reset host and info. |
291 fHost.reset(new dng_host(&fAllocator)); | 383 fHost.reset(new SkDngHost(&fAllocator)); |
292 fInfo.reset(new dng_info); | 384 fInfo.reset(new dng_info); |
293 fDngStream.reset(new SkDngStream(fStream)); | 385 fDngStream.reset(new SkDngStream(fStream)); |
294 try { | 386 try { |
295 fHost->ValidateSizes(); | 387 fHost->ValidateSizes(); |
296 fInfo->Parse(*fHost, *fDngStream); | 388 fInfo->Parse(*fHost, *fDngStream); |
297 fInfo->PostParse(*fHost); | 389 fInfo->PostParse(*fHost); |
298 if (!fInfo->IsValidDNG()) { | 390 if (!fInfo->IsValidDNG()) { |
299 return false; | 391 return false; |
300 } | 392 } |
301 | 393 |
(...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)); | 558 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd
ge / shortEdge)); |
467 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge
/ shortEdge)); | 559 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge
/ shortEdge)); |
468 return sizeFloor == dim || sizeCeil == dim; | 560 return sizeFloor == dim || sizeCeil == dim; |
469 } | 561 } |
470 | 562 |
471 SkRawCodec::~SkRawCodec() {} | 563 SkRawCodec::~SkRawCodec() {} |
472 | 564 |
473 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | 565 SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
474 : INHERITED(dngImage->getImageInfo(), nullptr) | 566 : INHERITED(dngImage->getImageInfo(), nullptr) |
475 , fDngImage(dngImage) {} | 567 , fDngImage(dngImage) {} |
OLD | NEW |