• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.views.trace.bean;
17 
18 import ohos.devtools.views.trace.DField;
19 import ohos.devtools.views.trace.component.AnalystPanel;
20 import ohos.devtools.views.trace.fragment.graph.AbstractGraph;
21 import ohos.devtools.views.trace.util.Final;
22 import ohos.devtools.views.trace.util.Utils;
23 
24 import java.awt.AlphaComposite;
25 import java.awt.Color;
26 import java.awt.Graphics2D;
27 import java.awt.Rectangle;
28 import java.awt.event.MouseEvent;
29 
30 /**
31  * Thread data
32  *
33  * @since 2021/04/22 12:25
34  */
35 public class ThreadData extends AbstractGraph {
36     private static Color runningColor = new Color(Final.RUNNING_COLOR);
37     private static Color rColor = new Color(Final.R_COLOR);
38     private static Color uninterruptibleSleepColor = new Color(Final.UNINTERRUPTIBLE_SLEEP_COLOR);
39     private static Color sColor = new Color(Final.S_COLOR);
40 
41     private final int padding1 = 5;
42     private final int padding2 = 10;
43     private final float alpha2 = 0.02f;
44     @DField(name = "upid")
45     private int uPid;
46     @DField(name = "utid")
47     private int uTid;
48     @DField(name = "pid")
49     private int pid; // Process id
50     @DField(name = "tid")
51     private int tid; // Thread id
52     @DField(name = "processName")
53     private String processName;
54     @DField(name = "threadName")
55     private String threadName;
56     @DField(name = "state")
57     private String state;
58     @DField(name = "startTime")
59     private long startTime;
60     @DField(name = "dur")
61     private long duration;
62     private boolean isSelected; // Whether to be selected
63     @DField(name = "cpu")
64     private int cpu;
65     private IEventListener eventListener;
66 
67     /**
68      * Gets the value of uPid .
69      *
70      * @return the value of uPid .
71      */
getuPid()72     public int getuPid() {
73         return uPid;
74     }
75 
76     /**
77      * Sets the uPid .
78      * <p>You can use getuPid() to get the value of uPid.</p>
79      *
80      * @param param param.
81      */
setuPid(final int param)82     public void setuPid(final int param) {
83         this.uPid = param;
84     }
85 
86     /**
87      * Gets the value of uTid .
88      *
89      * @return the value of uTid .
90      */
getuTid()91     public int getuTid() {
92         return uTid;
93     }
94 
95     /**
96      * Sets the uTid .
97      * <p>You can use getuTid() to get the value of uTid.</p>
98      *
99      * @param param .
100      */
setuTid(final int param)101     public void setuTid(final int param) {
102         this.uTid = param;
103     }
104 
105     /**
106      * Gets the value of pid .
107      *
108      * @return the value of pid .
109      */
getPid()110     public int getPid() {
111         return pid;
112     }
113 
114     /**
115      * Sets the pid .
116      * <p>You can use getPid() to get the value of pid.</p>
117      *
118      * @param param .
119      */
setPid(final int param)120     public void setPid(final int param) {
121         this.pid = param;
122     }
123 
124     /**
125      * Gets the value of tid .
126      *
127      * @return the value of tid .
128      */
getTid()129     public int getTid() {
130         return tid;
131     }
132 
133     /**
134      * Sets the tid .
135      * <p>You can use getTid() to get the value of tid.</p>
136      *
137      * @param param .
138      */
setTid(final int param)139     public void setTid(final int param) {
140         this.tid = param;
141     }
142 
143     /**
144      * Gets the value of processName .
145      *
146      * @return the value of processName .
147      */
getProcessName()148     public String getProcessName() {
149         return processName;
150     }
151 
152     /**
153      * Sets the processName .
154      * <p>You can use getProcessName() to get the value of processName.</p>
155      *
156      * @param param .
157      */
setProcessName(final String param)158     public void setProcessName(final String param) {
159         this.processName = param;
160     }
161 
162     /**
163      * Gets the value of threadName .
164      *
165      * @return the value of threadName .
166      */
getThreadName()167     public String getThreadName() {
168         return threadName == null ? "Thread" : threadName;
169     }
170 
171     /**
172      * Sets the threadName .
173      * <p>You can use getThreadName() to get the value of threadName.</p>
174      *
175      * @param param param
176      */
setThreadName(final String param)177     public void setThreadName(final String param) {
178         this.threadName = param;
179     }
180 
181     /**
182      * Gets the value of state .
183      *
184      * @return the value of state .
185      */
getState()186     public String getState() {
187         return state;
188     }
189 
190     /**
191      * Sets the state .
192      * <p>You can use getState() to get the value of state.</p>
193      *
194      * @param param param
195      */
setState(final String param)196     public void setState(final String param) {
197         this.state = param;
198     }
199 
200     /**
201      * Gets the value of startTime .
202      *
203      * @return the value of startTime .
204      */
getStartTime()205     public long getStartTime() {
206         return startTime;
207     }
208 
209     /**
210      * Sets the startTime .
211      * <p>You can use getStartTime() to get the value of startTime.</p>
212      *
213      * @param param param
214      */
setStartTime(final long param)215     public void setStartTime(final long param) {
216         this.startTime = param;
217     }
218 
219     /**
220      * Gets the value of duration .
221      *
222      * @return the value of duration .
223      */
getDuration()224     public long getDuration() {
225         return duration;
226     }
227 
228     /**
229      * Sets the duration .
230      * <p>You can use getDuration() to get the value of duration.</p>
231      *
232      * @param param param
233      */
setDuration(final long param)234     public void setDuration(final long param) {
235         this.duration = param;
236     }
237 
238     /**
239      * Gets the value of cpu .
240      *
241      * @return the value of cpu .
242      */
getCpu()243     public int getCpu() {
244         return cpu;
245     }
246 
247     /**
248      * Sets the cpu .
249      * <p>You can use getCpu() to get the value of cpu.</p>
250      *
251      * @param param param
252      */
setCpu(final int param)253     public void setCpu(final int param) {
254         this.cpu = param;
255     }
256 
257     /**
258      * Sets the isSelected .
259      * <p>You can use getSelected() to get the value of isSelected.</p>
260      *
261      * @param param param
262      */
select(final boolean param)263     public void select(final boolean param) {
264         isSelected = param;
265     }
266 
267     /**
268      * repaint.
269      */
repaint()270     public void repaint() {
271         if (root != null) {
272             root.repaint(Utils.getX(rect), Utils.getY(rect) - padding1, rect.width, rect.height + padding2);
273         }
274     }
275 
276     /**
277      * Draw the corresponding shape according to the brush
278      *
279      * @param graphics graphics
280      */
281     @Override
draw(final Graphics2D graphics)282     public void draw(final Graphics2D graphics) {
283         if (isSelected && !"S".equals(state)) {
284             graphics.setColor(Color.BLACK);
285             graphics.fillRect(Utils.getX(rect), Utils.getY(rect) - padding1, rect.width, rect.height + padding2);
286             drawSelected(graphics);
287         } else {
288             drawUnSelected(graphics);
289         }
290     }
291 
drawSelected(final Graphics2D graphics)292     private void drawSelected(final Graphics2D graphics) {
293         if ("S".equals(state)) {
294             graphics.setColor(sColor);
295             graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha2)); // transparency
296             graphics.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height);
297             graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); // transparency
298         } else if ("R".equals(state)) {
299             graphics.setColor(rColor);
300             graphics.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height);
301             graphics.setColor(Color.white);
302             drawString(graphics, rect, Utils.getEndState(state), Placement.CENTER_LINE);
303         } else if ("D".equals(state)) {
304             graphics.setColor(uninterruptibleSleepColor);
305             graphics.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height);
306             graphics.setColor(Color.white);
307             drawString(graphics, rect, Utils.getEndState(state), Placement.CENTER_LINE);
308         } else if ("Running".equals(state)) {
309             graphics.setColor(runningColor);
310             graphics.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height);
311             graphics.setColor(Color.white);
312             Rectangle rectangle = new Rectangle();
313             rectangle.setRect(rect.getX() + padding1, rect.getY(), rect.getWidth() - padding2, rect.getHeight());
314             drawString(graphics, rectangle, Utils.getEndState(state), Placement.CENTER_LINE);
315         } else {
316             graphics.setColor(rColor);
317             graphics.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height);
318             graphics.setColor(Color.white);
319             Rectangle rectangle = new Rectangle();
320             rectangle.setRect(rect.getX() + padding1, rect.getY(), rect.getWidth() - padding2, rect.getHeight());
321             drawString(graphics, rectangle, Utils.getEndState(state), Placement.CENTER_LINE);
322         }
323     }
324 
drawUnSelected(final Graphics2D graphics)325     private void drawUnSelected(final Graphics2D graphics) {
326         if ("S".equals(state)) {
327             graphics.setColor(sColor);
328             graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha2)); // transparency
329             graphics.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height);
330             graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); // transparency
331         } else if ("R".equals(state)) {
332             graphics.setColor(rColor);
333             graphics.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height);
334             graphics.setColor(Color.white);
335             drawString(graphics, rect, Utils.getEndState(state), Placement.CENTER_LINE);
336         } else if ("D".equals(state)) {
337             graphics.setColor(uninterruptibleSleepColor);
338             graphics.fillRect(Utils.getX(rect) + padding1, Utils.getY(rect), rect.width - padding2, rect.height);
339             graphics.setColor(Color.white);
340             drawString(graphics, rect, Utils.getEndState(state), Placement.CENTER_LINE);
341         } else if ("Running".equals(state)) {
342             graphics.setColor(runningColor);
343             graphics.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height);
344             graphics.setColor(Color.white);
345             drawString(graphics, rect, Utils.getEndState(state), Placement.CENTER_LINE);
346         } else {
347             graphics.setColor(rColor);
348             graphics.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height);
349             graphics.setColor(Color.white);
350             drawString(graphics, rect, Utils.getEndState(state), Placement.CENTER_LINE);
351         }
352     }
353 
354     /**
355      * Focus acquisition callback event
356      *
357      * @param event event
358      */
359     @Override
onFocus(final MouseEvent event)360     public void onFocus(final MouseEvent event) {
361         if (eventListener != null) {
362             eventListener.focus(event, this);
363         }
364     }
365 
366     /**
367      * Focus cancel callback event
368      *
369      * @param event event
370      */
371     @Override
onBlur(final MouseEvent event)372     public void onBlur(final MouseEvent event) {
373         if (eventListener != null) {
374             eventListener.blur(event, this);
375         }
376     }
377 
378     /**
379      * Click event callback
380      *
381      * @param event event
382      */
383     @Override
onClick(final MouseEvent event)384     public void onClick(final MouseEvent event) {
385         if (eventListener != null) {
386             AnalystPanel.clicked = true;
387             eventListener.click(event, this);
388         }
389     }
390 
391     /**
392      * Mouse movement event callback
393      *
394      * @param event event
395      */
396     @Override
onMouseMove(final MouseEvent event)397     public void onMouseMove(final MouseEvent event) {
398         if (edgeInspect(event)) {
399             if (eventListener != null) {
400                 eventListener.mouseMove(event, this);
401             }
402         }
403     }
404 
405     /**
406      * Set callback event listener
407      *
408      * @param listener listener
409      */
setEventListener(final IEventListener listener)410     public void setEventListener(final IEventListener listener) {
411         this.eventListener = listener;
412     }
413 
414     /**
415      * listener
416      */
417     public interface IEventListener {
418         /**
419          * Mouse click event
420          *
421          * @param event event
422          * @param data data
423          */
click(MouseEvent event, ThreadData data)424         void click(MouseEvent event, ThreadData data);
425 
426         /**
427          * Mouse blur event
428          *
429          * @param event event
430          * @param data data
431          */
blur(MouseEvent event, ThreadData data)432         void blur(MouseEvent event, ThreadData data);
433 
434         /**
435          * Mouse focus event
436          *
437          * @param event event
438          * @param data data
439          */
focus(MouseEvent event, ThreadData data)440         void focus(MouseEvent event, ThreadData data);
441 
442         /**
443          * Mouse move event
444          *
445          * @param event event
446          * @param data data
447          */
mouseMove(MouseEvent event, ThreadData data)448         void mouseMove(MouseEvent event, ThreadData data);
449     }
450 }
451 
452