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.ColorUtils; 22 import ohos.devtools.views.trace.util.Final; 23 import ohos.devtools.views.trace.util.Utils; 24 25 import java.awt.AlphaComposite; 26 import java.awt.Color; 27 import java.awt.Graphics2D; 28 import java.awt.Rectangle; 29 import java.awt.event.MouseEvent; 30 31 /** 32 * Process memory data 33 * 34 * @since 2021/04/22 12:25 35 */ 36 public class ProcessMemData extends AbstractGraph { 37 private final int padding1 = 2; 38 private final int padding2 = 4; 39 private int maxValue; 40 private int id; 41 private final float alpha90 = .9f; 42 private final int strOffsetY = 16; 43 private final int redOff = 40; 44 private final int greenOff = 60; 45 private final int blueOff = 75; 46 private boolean isSelected; // Whether to be selected 47 @DField(name = "type") 48 private String type; 49 @DField(name = "track_id") 50 private int trackId; 51 @DField(name = "value") 52 private int value; 53 @DField(name = "startTime") 54 private long startTime; 55 private long duration; 56 private Long delta; 57 private IEventListener eventListener; 58 59 /** 60 * Gets the value of maxValue . 61 * 62 * @return the value of int 63 */ getMaxValue()64 public int getMaxValue() { 65 return maxValue; 66 } 67 68 /** 69 * Sets the maxValue . 70 * <p>You can use getMaxValue() to get the value of maxValue</p> 71 * 72 * @param max max 73 */ setMaxValue(final int max)74 public void setMaxValue(final int max) { 75 this.maxValue = max; 76 } 77 78 /** 79 * Gets the value of id . 80 * 81 * @return the value of int 82 */ getId()83 public int getId() { 84 return id; 85 } 86 87 /** 88 * Sets the id . 89 * <p>You can use getId() to get the value of id</p> 90 * 91 * @param id id 92 */ setId(final int id)93 public void setId(final int id) { 94 this.id = id; 95 } 96 97 /** 98 * Gets the value of type . 99 * 100 * @return the value of java.lang.String 101 */ getType()102 public String getType() { 103 return type; 104 } 105 106 /** 107 * Sets the type . 108 * <p>You can use getType() to get the value of type</p> 109 * 110 * @param type type 111 */ setType(final String type)112 public void setType(final String type) { 113 this.type = type; 114 } 115 116 /** 117 * Gets the value of trackId . 118 * 119 * @return the value of int 120 */ getTrackId()121 public int getTrackId() { 122 return trackId; 123 } 124 125 /** 126 * get delta value 127 * 128 * @return delta value 129 */ getDelta()130 public Long getDelta() { 131 return delta; 132 } 133 134 /** 135 * set delta value 136 * 137 * @param delta delta value 138 */ setDelta(Long delta)139 public void setDelta(Long delta) { 140 this.delta = delta; 141 } 142 143 /** 144 * Sets the trackId . 145 * <p>You can use getTrackId() to get the value of trackId</p> 146 * 147 * @param id id 148 */ setTrackId(final int id)149 public void setTrackId(final int id) { 150 this.trackId = id; 151 } 152 153 /** 154 * Gets the value of value . 155 * 156 * @return the value of int 157 */ getValue()158 public int getValue() { 159 return value; 160 } 161 162 /** 163 * Sets the value . 164 * <p>You can use getValue() to get the value of value</p> 165 * 166 * @param value value 167 */ setValue(final int value)168 public void setValue(final int value) { 169 this.value = value; 170 } 171 172 /** 173 * Gets the value of startTime . 174 * 175 * @return the value of long 176 */ getStartTime()177 public long getStartTime() { 178 return startTime; 179 } 180 181 /** 182 * Sets the startTime . 183 * <p>You can use getStartTime() to get the value of startTime</p> 184 * 185 * @param time time 186 */ setStartTime(final long time)187 public void setStartTime(final long time) { 188 this.startTime = time; 189 } 190 191 /** 192 * Gets the value of duration . 193 * 194 * @return the value of long 195 */ getDuration()196 public long getDuration() { 197 return duration; 198 } 199 200 /** 201 * Sets the duration . 202 * <p>You can use getDuration() to get the value of duration</p> 203 * 204 * @param dur dur 205 */ setDuration(final long dur)206 public void setDuration(final long dur) { 207 this.duration = dur; 208 } 209 210 /** 211 * Draw the corresponding shape according to the brush 212 * 213 * @param graphics graphics 214 */ 215 @Override draw(final Graphics2D graphics)216 public void draw(final Graphics2D graphics) { 217 Color color = ColorUtils.MD_PALETTE[trackId % ColorUtils.MD_PALETTE.length]; 218 int height = 0; 219 if (maxValue > 0) { 220 height = ((rect.height - 5) * value) / maxValue; 221 graphics.setColor(color); 222 graphics.fillRect(Utils.getX(rect), Utils.getY(rect) + rect.height - height, rect.width, height); 223 } 224 if (isSelected) { 225 drawSelect(graphics); 226 } else { 227 drawNoSelect(graphics); 228 } 229 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); 230 graphics.setColor(Color.white); 231 Rectangle rectangle = new Rectangle(); 232 graphics.setFont(Final.SMALL_FONT); 233 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha90)); 234 rectangle.setRect(rect.getX(), rect.getY() + strOffsetY, rect.getWidth(), rect.getHeight()); 235 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); 236 graphics.setFont(Final.NORMAL_FONT); 237 238 } 239 drawSelect(final Graphics2D graphics)240 private void drawSelect(final Graphics2D graphics) { 241 Color color = ColorUtils.colorForTid(maxValue); 242 graphics.setColor(color); 243 double tmpHeight = (rect.height - 5) * value * 1.0 / maxValue; 244 if (tmpHeight <= 0) { 245 tmpHeight = 1; 246 } 247 int yAxis = (int) (rect.getY() + rect.height - tmpHeight); 248 int xAxis = (int) rect.getX(); 249 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); 250 graphics.fillRect(xAxis, yAxis, rect.width, (int) tmpHeight); 251 graphics.drawRect(xAxis, yAxis, rect.width, (int) tmpHeight); 252 int red = color.getRed(); 253 int green = color.getGreen(); 254 int blue = color.getBlue(); 255 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); 256 Color borderColor = new Color(red <= redOff ? 0 : red - redOff, green <= greenOff ? 0 : green - greenOff, 257 blue <= blueOff ? 0 : blue - blueOff); 258 graphics.setColor(borderColor); 259 graphics.fillRect(xAxis, yAxis, rect.width, 3); 260 } 261 drawNoSelect(final Graphics2D graphics)262 private void drawNoSelect(final Graphics2D graphics) { 263 Color color = ColorUtils.colorForTid(maxValue); 264 graphics.setColor(color); 265 // int offset = rect.height/5;//value 为max的话 y = offset 266 double tmpHeight = (rect.height - 5) * value * 1.0 / maxValue; 267 if (tmpHeight <= 0) { 268 tmpHeight = 1; 269 } 270 int yAxis = (int) (rect.getY() + rect.height - tmpHeight); 271 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); 272 graphics.fillRect((int) rect.getX(), yAxis, rect.width, (int) tmpHeight); 273 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); 274 graphics.drawRect((int) rect.getX(), yAxis, rect.width, (int) tmpHeight); 275 } 276 277 /** 278 * Set selected state 279 * 280 * @param isSelected isSelected 281 */ select(final boolean isSelected)282 public void select(final boolean isSelected) { 283 this.isSelected = isSelected; 284 } 285 286 /** 287 * Redraw the current page 288 */ repaint()289 public void repaint() { 290 if (root != null) { 291 root.repaint(Utils.getX(rect), Utils.getY(rect) - padding1, rect.width, rect.height + padding2); 292 } 293 } 294 295 /** 296 * Focus acquisition event 297 * 298 * @param event event 299 */ 300 @Override onFocus(final MouseEvent event)301 public void onFocus(final MouseEvent event) { 302 if (eventListener != null) { 303 eventListener.focus(event, this); 304 } 305 } 306 307 /** 308 * Focus loss event 309 * 310 * @param event event 311 */ 312 @Override onBlur(final MouseEvent event)313 public void onBlur(final MouseEvent event) { 314 if (eventListener != null) { 315 eventListener.blur(event, this); 316 } 317 } 318 319 /** 320 * Click event 321 * 322 * @param event event 323 */ 324 @Override onClick(final MouseEvent event)325 public void onClick(final MouseEvent event) { 326 if (eventListener != null) { 327 AnalystPanel.clicked = true; 328 eventListener.click(event, this); 329 } 330 } 331 332 /** 333 * Mouse movement event 334 * 335 * @param event event 336 */ 337 @Override onMouseMove(final MouseEvent event)338 public void onMouseMove(final MouseEvent event) { 339 if (edgeInspect(event)) { 340 if (eventListener != null) { 341 eventListener.mouseMove(event, this); 342 } 343 } 344 } 345 346 /** 347 * Set callback event listener 348 * 349 * @param eventListener eventListener 350 */ setEventListener(final IEventListener eventListener)351 public void setEventListener(final IEventListener eventListener) { 352 this.eventListener = eventListener; 353 } 354 355 /** 356 * Listener 357 */ 358 public interface IEventListener { 359 /** 360 * Mouse click event 361 * 362 * @param event event 363 * @param data data 364 */ click(MouseEvent event, ProcessMemData data)365 void click(MouseEvent event, ProcessMemData data); 366 367 /** 368 * Mouse blur event 369 * 370 * @param event event 371 * @param data data 372 */ blur(MouseEvent event, ProcessMemData data)373 void blur(MouseEvent event, ProcessMemData data); 374 375 /** 376 * Mouse focus event 377 * 378 * @param event event 379 * @param data data 380 */ focus(MouseEvent event, ProcessMemData data)381 void focus(MouseEvent event, ProcessMemData data); 382 383 /** 384 * Mouse move event 385 * 386 * @param event event 387 * @param data data 388 */ mouseMove(MouseEvent event, ProcessMemData data)389 void mouseMove(MouseEvent event, ProcessMemData data); 390 } 391 } 392