Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: src/codec/SkRawCodec.cpp

Issue 1634763002: Multithreaded dng host implementation (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fixes the tile organization. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Calculate the number of tiles to process per thread, taking into account the maximum number of
49 // threads.
50 dng_point num_tiles_per_thread(const int maxThreads,
51 const dng_point &tilesInArea) {
52 dng_point tilesInThread = {1, 1};
53 while (((tilesInArea.v + tilesInThread.v - 1) / tilesInThread.v) *
54 ((tilesInArea.h + tilesInThread.h - 1) / tilesInThread.h) >
55 maxThreads) {
56 if (tilesInArea.h > tilesInThread.h) {
57 ++tilesInThread.h;
58 } else {
59 ++tilesInThread.v;
60 }
61 }
62 return tilesInThread;
63 }
64
65 class SkDngHost : public dng_host {
66 public:
67 using dng_host::dng_host;
68
69 void PerformAreaTask(dng_area_task& task, const dng_rect& area) override {
70 const int maxThreads = Min_int32(sk_num_cores(), static_cast<int>(task.M axThreads()));
71 SkTaskGroup threadPool;
mtklein 2016/01/25 19:50:59 This isn't really an independent thread pool; it r
ebrauer 2016/01/26 09:53:08 Changed the name. The dng_task has a maximum we mu
72
73 const dng_point tileSize(task.FindTileSize(area));
74 const dng_point tilesInArea = num_tiles_in_area(area.Size(), tileSize);
75
76 const dng_point tilesInThread = num_tiles_per_thread(maxThreads, tilesIn Area);
77 const dng_point threadAreaSize = {tilesInThread.v * tileSize.v,
78 tilesInThread.h * tileSize.h};
79 dng_rect threadArea(area.t, area.l, threadAreaSize.v + area.t, threadAre aSize.h + area.l);
80
81 task.Start(maxThreads, tileSize, &Allocator(), Sniffer());
82 int taskIndex = 0;
83 for (int v = 0; v < tilesInArea.v; v += tilesInThread.v) {
84 threadArea.l = area.l;
85 threadArea.r = threadAreaSize.h + threadArea.l;
86 for (int h = 0; h < tilesInArea.h; h += tilesInThread.h) {
87 std::function<void()> func = std::bind(&dng_area_task::ProcessOn Thread, &task,
mtklein 2016/01/25 19:50:59 Generally I'd rather see a lambda for this than st
ebrauer 2016/01/26 09:53:08 Done.
88 taskIndex, threadArea, ti leSize, Sniffer());
89 threadPool.add(func);
mtklein 2016/01/25 21:02:01 Something like, threadPool.add([&task, this, task
ebrauer 2016/01/26 09:53:07 Done.
90
91 threadArea = threadArea + dng_point(0, threadAreaSize.h);
92 threadArea.r = Min_int32(threadArea.r, area.r);
93 }
94 threadArea = threadArea + dng_point(threadAreaSize.v, 0);
95 threadArea.b = Min_int32(threadArea.b, area.b);
96 ++taskIndex;
97 }
98
99 threadPool.wait();
100 task.Finish(maxThreads);
101 }
102
103 private:
104 typedef dng_host INHERITED;
105 };
106
38 // T must be unsigned type. 107 // T must be unsigned type.
39 template <class T> 108 template <class T>
40 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { 109 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) {
41 SkASSERT(arg1 >= 0); 110 SkASSERT(arg1 >= 0);
42 SkASSERT(arg2 >= 0); 111 SkASSERT(arg2 >= 0);
43 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { 112 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) {
44 T sum = arg1 + arg2; 113 T sum = arg1 + arg2;
45 if (sum <= std::numeric_limits<size_t>::max()) { 114 if (sum <= std::numeric_limits<size_t>::max()) {
46 *result = static_cast<size_t>(sum); 115 *result = static_cast<size_t>(sum);
47 return true; 116 return true;
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 * returned size could be: 2000 x 1500 296 * returned size could be: 2000 x 1500
228 */ 297 */
229 dng_image* render(int width, int height) { 298 dng_image* render(int width, int height) {
230 if (!fHost || !fInfo || !fNegative || !fDngStream) { 299 if (!fHost || !fInfo || !fNegative || !fDngStream) {
231 if (!this->readDng()) { 300 if (!this->readDng()) {
232 return nullptr; 301 return nullptr;
233 } 302 }
234 } 303 }
235 304
236 // render() takes ownership of fHost, fInfo, fNegative and fDngStream wh en available. 305 // render() takes ownership of fHost, fInfo, fNegative and fDngStream wh en available.
237 SkAutoTDelete<dng_host> host(fHost.release()); 306 SkAutoTDelete<SkDngHost> host(fHost.release());
adaubert 2016/01/26 09:26:37 This change is not necessary.
ebrauer 2016/01/26 09:53:08 Done.
238 SkAutoTDelete<dng_info> info(fInfo.release()); 307 SkAutoTDelete<dng_info> info(fInfo.release());
239 SkAutoTDelete<dng_negative> negative(fNegative.release()); 308 SkAutoTDelete<dng_negative> negative(fNegative.release());
240 SkAutoTDelete<dng_stream> dngStream(fDngStream.release()); 309 SkAutoTDelete<dng_stream> dngStream(fDngStream.release());
241 310
242 // DNG SDK preserves the aspect ratio, so it only needs to know the long er dimension. 311 // DNG SDK preserves the aspect ratio, so it only needs to know the long er dimension.
243 const int preferredSize = SkTMax(width, height); 312 const int preferredSize = SkTMax(width, height);
244 try { 313 try {
245 host->SetPreferredSize(preferredSize); 314 host->SetPreferredSize(preferredSize);
246 host->ValidateSizes(); 315 host->ValidateSizes();
247 316
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 return fIsScalable; 350 return fIsScalable;
282 } 351 }
283 352
284 bool isXtransImage() const { 353 bool isXtransImage() const {
285 return fIsXtransImage; 354 return fIsXtransImage;
286 } 355 }
287 356
288 private: 357 private:
289 bool readDng() { 358 bool readDng() {
290 // Due to the limit of DNG SDK, we need to reset host and info. 359 // Due to the limit of DNG SDK, we need to reset host and info.
291 fHost.reset(new dng_host(&fAllocator)); 360 fHost.reset(new SkDngHost(&fAllocator));
292 fInfo.reset(new dng_info); 361 fInfo.reset(new dng_info);
293 fDngStream.reset(new SkDngStream(fStream)); 362 fDngStream.reset(new SkDngStream(fStream));
294 try { 363 try {
295 fHost->ValidateSizes(); 364 fHost->ValidateSizes();
296 fInfo->Parse(*fHost, *fDngStream); 365 fInfo->Parse(*fHost, *fDngStream);
297 fInfo->PostParse(*fHost); 366 fInfo->PostParse(*fHost);
298 if (!fInfo->IsValidDNG()) { 367 if (!fInfo->IsValidDNG()) {
299 return false; 368 return false;
300 } 369 }
301 370
(...skipping 18 matching lines...) Expand all
320 fNegative.reset(nullptr); 389 fNegative.reset(nullptr);
321 return false; 390 return false;
322 } 391 }
323 } 392 }
324 393
325 SkDngImage(SkRawStream* stream) 394 SkDngImage(SkRawStream* stream)
326 : fStream(stream) {} 395 : fStream(stream) {}
327 396
328 SkDngMemoryAllocator fAllocator; 397 SkDngMemoryAllocator fAllocator;
329 SkAutoTDelete<SkRawStream> fStream; 398 SkAutoTDelete<SkRawStream> fStream;
330 SkAutoTDelete<dng_host> fHost; 399 SkAutoTDelete<SkDngHost> fHost;
adaubert 2016/01/26 09:26:38 This change is not necessary.
ebrauer 2016/01/26 09:53:08 Done.
331 SkAutoTDelete<dng_info> fInfo; 400 SkAutoTDelete<dng_info> fInfo;
332 SkAutoTDelete<dng_negative> fNegative; 401 SkAutoTDelete<dng_negative> fNegative;
333 SkAutoTDelete<dng_stream> fDngStream; 402 SkAutoTDelete<dng_stream> fDngStream;
334 403
335 SkImageInfo fImageInfo; 404 SkImageInfo fImageInfo;
336 bool fIsScalable; 405 bool fIsScalable;
337 bool fIsXtransImage; 406 bool fIsXtransImage;
338 }; 407 };
339 408
340 /* 409 /*
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); 535 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge));
467 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); 536 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge));
468 return sizeFloor == dim || sizeCeil == dim; 537 return sizeFloor == dim || sizeCeil == dim;
469 } 538 }
470 539
471 SkRawCodec::~SkRawCodec() {} 540 SkRawCodec::~SkRawCodec() {}
472 541
473 SkRawCodec::SkRawCodec(SkDngImage* dngImage) 542 SkRawCodec::SkRawCodec(SkDngImage* dngImage)
474 : INHERITED(dngImage->getImageInfo(), nullptr) 543 : INHERITED(dngImage->getImageInfo(), nullptr)
475 , fDngImage(dngImage) {} 544 , fDngImage(dngImage) {}
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698