Debian Codegraph @

core/libs/dngwriter/extra/xmp_sdk/source/XMP_ProgressTracker.cpp

raw
1 // =================================================================================================
2 // Copyright Adobe
3 // Copyright 2012 Adobe
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it. If you have received this file from a source other
8 // than Adobe, then your use, modification, or distribution of it requires the prior written permission
9 // of Adobe.
10 // =================================================================================================
11
12 #if AdobePrivate
13 // =================================================================================================
14 // Change history
15 // ==============
16 //
17 // Writers:
18 // AWL Alan Lillich
19 // ADC Amandeep Chawla
20 //
21 // mm-dd-yy who Description of changes, most recent first.
22 //
23 // 05-13-13 ADC 5.6-f060 Removing usage of bool in APIs exposed at DLL boundaries.
24 //
25 // 06-22-12 AWL 5.5-f019 Add file update progress tracking to the MPEG-4 handler.
26 // 06-20-12 AWL 5.5-f018 Add outer layers for XMPFiles progress notifications, no handlers use it yet.
27 //
28 // =================================================================================================
29 #endif // AdobePrivate
30
31 #include "public/include/XMP_Environment.h" // ! XMP_Environment.h must be the first included header.
32
33 #include "public/include/XMP_Const.h"
34
35 #include "source/XMP_LibUtils.hpp"
36 #include "source/XMP_ProgressTracker.hpp"
37
38 // =================================================================================================
39 // XMP_ProgressTracker::XMP_ProgressTracker
40 // ========================================
41
42 XMP_ProgressTracker::XMP_ProgressTracker ( const CallbackInfo & _cbInfo )
43 {
44
45 this->Clear();
46 if ( _cbInfo.clientProc == 0 ) return;
47 XMP_Assert ( _cbInfo.wrapperProc != 0 );
48
49 this->cbInfo = _cbInfo;
50 if ( this->cbInfo.interval < 0.0 ) this->cbInfo.interval = 1.0;
51
52 } // XMP_ProgressTracker::XMP_ProgressTracker
53
54 // =================================================================================================
55 // XMP_ProgressTracker::BeginWork
56 // ==============================
57
58 void XMP_ProgressTracker::BeginWork ( float _totalWork )
59 {
60
61 if ( _totalWork < 0.0 ) _totalWork = 0.0;
62 this->totalWork = _totalWork;
63 this->workDone = 0.0;
64 this->workInProgress = true;
65
66 this->startTime = this->prevTime = PerfUtils::NoteThisMoment();
67 if ( this->cbInfo.sendStartStop ) this->NotifyClient ( true );
68
69 } // XMP_ProgressTracker::BeginWork
70
71 // =================================================================================================
72 // XMP_ProgressTracker::AddTotalWork
73 // =================================
74
75 void XMP_ProgressTracker::AddTotalWork ( float workIncrement )
76 {
77
78 if ( workIncrement < 0.0 ) workIncrement = 0.0;
79 this->totalWork += workIncrement;
80
81 } // XMP_ProgressTracker::AddTotalWork
82
83 // =================================================================================================
84 // XMP_ProgressTracker::AddWorkDone
85 // ================================
86
87 void XMP_ProgressTracker::AddWorkDone ( float workIncrement )
88 {
89
90 if ( workIncrement < 0.0 ) workIncrement = 0.0;
91 this->workDone += workIncrement;
92 this->NotifyClient();
93
94 } // XMP_ProgressTracker::AddWorkDone
95
96 // =================================================================================================
97 // XMP_ProgressTracker::WorkComplete
98 // =================================
99
100 void XMP_ProgressTracker::WorkComplete ()
101 {
102
103 if ( this->totalWork == 0.0 ) this->totalWork = 1.0; // Force non-zero fraction done.
104 this->workDone = this->totalWork;
105 XMP_Assert ( this->workDone > 0.0 ); // Needed in NotifyClient for start/stop case.
106
107 this->NotifyClient ( this->cbInfo.sendStartStop );
108 this->workInProgress = false;
109
110 } // XMP_ProgressTracker::WorkComplete
111
112 // =================================================================================================
113 // XMP_ProgressTracker::Clear
114 // ==========================
115
116 void XMP_ProgressTracker::Clear ()
117 {
118
119 this->cbInfo.Clear();
120 this->workInProgress = false;
121 this->totalWork = 0.0;
122 this->workDone = 0.0;
123 this->startTime = this->prevTime = PerfUtils::kZeroMoment;
124 // ! There is no standard zero value for PerfUtils::MomentValue.
125
126 } // XMP_ProgressTracker::Clear
127
128 // =================================================================================================
129 // XMP_ProgressTracker::NotifyClient
130 // =================================
131 //
132 // The math for the time remaining is easy but not immediately obvious. We know the elapsed time and
133 // fraction of work done:
134 //
135 // elapsedTime = totalTime * fractionDone or totalTime = (elapsedTime / fractionDone)
136 // remainingTime = totalTime * (1 - fractionDone)
137 // so:
138 // remainingTime = (elapsedTime / fractionDone) * (1 - fractionDone)
139
140 void XMP_ProgressTracker::NotifyClient ( bool isStartStop )
141 {
142 XMP_Bool ok = !kXMP_Bool_False;
143 float fractionDone = 0.0;
144
145 if ( this->cbInfo.clientProc == 0 ) return;
146 XMP_Assert ( this->cbInfo.wrapperProc != 0 );
147 XMP_Assert ( (this->totalWork >= 0.0) && (this->workDone >= 0.0) && (this->cbInfo.interval >= 0.0) );
148 // ! Note that totalWork might be unknown or understimated, and workDone greater than totalWork.
149
150 if ( isStartStop ) {
151
152 float totalTime = 0.0;
153 if ( this->workDone > 0.0 ) {
154 fractionDone = 1.0; // This is the stop call.
155 totalTime = (float)PerfUtils::GetElapsedSeconds ( this->startTime, PerfUtils::NoteThisMoment() );
156 }
157 ok = (*this->cbInfo.wrapperProc ) ( this->cbInfo.clientProc, this->cbInfo.context,
158 totalTime, fractionDone, 0.0 );
159
160 } else {
161
162 PerfUtils::MomentValue currentTime = PerfUtils::NoteThisMoment();
163 float elapsedTime =(float) PerfUtils::GetElapsedSeconds ( this->prevTime, currentTime );
164 if ( elapsedTime < this->cbInfo.interval ) return;
165
166 float remainingTime = 0.0;
167 if ( (this->totalWork > 0.0) && (this->workDone > 0.0) ) {
168 fractionDone = this->workDone / this->totalWork;
169 if ( fractionDone > 1.0 ) fractionDone = 1.0;
170 elapsedTime =(float) PerfUtils::GetElapsedSeconds ( this->startTime, currentTime );
171 remainingTime = (float)((elapsedTime / fractionDone) * (1.0F - fractionDone));
172 }
173
174 this->prevTime = currentTime;
175 ok = (*this->cbInfo.wrapperProc ) ( this->cbInfo.clientProc, this->cbInfo.context,
176 elapsedTime, fractionDone, remainingTime );
177
178 }
179
180 if ( ok == kXMP_Bool_False ) XMP_Throw ( "Abort signaled by progress reporting callback", kXMPErr_ProgressAbort );
181
182 } // XMP_ProgressTracker::NotifyClient
183
184 // =================================================================================================