HAL  v4.5.0-283-g3f42a39fa
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
clock_tree_scene.cpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 // Copyright (c) 2025-2026 Sascha Tommasone. All rights reserved.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 
28 
29 #include <QBrush>
30 #include <QGraphicsLineItem>
31 #include <QPainter>
32 #include <QPen>
33 #include <QRectF>
34 #include <algorithm>
35 #include <cmath>
36 #include <string>
37 
38 namespace hal
39 {
40  namespace
41  {
42  // For colors that were not given, there is nothing to take after: a drawing is always visible, if not pretty.
43  const QColor FALLBACK_COLOR = Qt::black;
44 
45  QColor edge_color_of( const ClockTreeColors &colors )
46  {
47  return colors.edge.isValid() ? colors.edge : FALLBACK_COLOR;
48  }
49 
50  QColor highlight_color_of( const ClockTreeColors &colors )
51  {
52  return colors.highlight.isValid() ? colors.highlight : edge_color_of( colors );
53  }
54 
55  // The rings around highlighted gates are this large on the screen, however far the view is zoomed.
56  constexpr double RING_RADIUS_PIXELS = 12.0;
57  constexpr double RING_WIDTH_PIXELS = 2.0;
58 
59  // Above the lines and dots, which are at 0.
60  constexpr double RING_Z_VALUE = 2.0;
61  } // namespace
62 
64  : QGraphicsScene( parent )
65  , mStyle{ 0.0, 0.0, 0.0, 0.0, false }
66  , mHasLayout( false )
67  {
68  }
69 
70  QPointF ClockTreeScene::fromChip( double x, double y )
71  {
72  return { x, -y };
73  }
74 
75  QPointF ClockTreeScene::toChip( const QPointF &scenePosition )
76  {
77  return { scenePosition.x(), -scenePosition.y() };
78  }
79 
80  QColor ClockTreeScene::colorOfNode( const ClockTreeColors &colors, int index )
81  {
82  const QColor color = colors.nodes.value( index );
83  return color.isValid() ? color : edge_color_of( colors );
84  }
85 
86  void ClockTreeScene::setLayout( const ctv::Layout &layout, const ctv::Style &style, const ClockTreeColors &colors )
87  {
88  clearLayout();
89 
90  mStyle = style;
91  mEdgeColor = edge_color_of( colors );
92  mHighlightColor = highlight_color_of( colors );
93 
94  const QPen edge_pen( mEdgeColor, style.edge_width );
95  mEdgeItems.reserve( static_cast<int>( layout.edges.size() ) );
96  for( const ctv::Edge &edge : layout.edges )
97  {
98  const ctv::Node &from = layout.nodes[edge.from];
99  const ctv::Node &to = layout.nodes[edge.to];
100 
101  mEdgeItems.push_back( addLine( QLineF( fromChip( from.x, from.y ), fromChip( to.x, to.y ) ), edge_pen ) );
102  }
103 
104  // The nodes come last, so they are drawn on top of the lines.
105  mNodeItems.reserve( static_cast<int>( layout.nodes.size() ) );
106  mNodeColors.reserve( static_cast<int>( layout.nodes.size() ) );
107  mNodeCenters.reserve( static_cast<int>( layout.nodes.size() ) );
108  for( std::size_t idx = 0; idx < layout.nodes.size(); idx++ )
109  {
110  const ctv::Node &node = layout.nodes[idx];
111  const QPointF center = fromChip( node.x, node.y );
112  const QColor color = colorOfNode( colors, static_cast<int>( idx ) );
113 
114  mNodeItems.push_back( addEllipse( QRectF( center.x() - style.node_radius,
115  center.y() - style.node_radius,
116  2.0 * style.node_radius,
117  2.0 * style.node_radius ),
118  QPen( color, style.edge_width ),
119  QBrush( color ) ) );
120  mNodeColors.push_back( color );
121  mNodeCenters.push_back( center );
122  }
123 
124  // Taken now, while there is nothing but lines and dots: the rings around highlighted gates are as large as they are
125  // on the screen, so what they take up in the scene depends on the view.
126  mContentRect = itemsBoundingRect();
127 
128  const double margin = style.picture_margin;
129  setSceneRect( mContentRect.adjusted( -margin, -margin, margin, margin ) );
130 
131  mHasLayout = true;
132  }
133 
135  {
136  if( !mHasLayout )
137  {
138  return;
139  }
140 
141  const QColor edge = edge_color_of( colors );
142  if( edge != mEdgeColor )
143  {
144  mEdgeColor = edge;
145 
146  const QPen edge_pen( edge, mStyle.edge_width );
147  for( QGraphicsLineItem *item : mEdgeItems )
148  {
149  item->setPen( edge_pen );
150  }
151  }
152 
153  // A dot that is not given a color follows the lines, so this has to look at every one of them.
154  for( int idx = 0; idx < mNodeItems.size(); idx++ )
155  {
156  const QColor color = colorOfNode( colors, idx );
157  if( color == mNodeColors[idx] )
158  {
159  continue;
160  }
161 
162  mNodeColors[idx] = color;
163  mNodeItems[idx]->setPen( QPen( color, mStyle.edge_width ) );
164  mNodeItems[idx]->setBrush( QBrush( color ) );
165  }
166 
167  const QColor highlight = highlight_color_of( colors );
168  if( highlight != mHighlightColor )
169  {
170  mHighlightColor = highlight;
171 
172  QPen ring_pen( highlight, RING_WIDTH_PIXELS );
173  ring_pen.setCosmetic( true );
174  for( QGraphicsEllipseItem *ring : mRingItems )
175  {
176  ring->setPen( ring_pen );
177  }
178  }
179  }
180 
182  {
183  if( !mHasLayout )
184  {
185  return;
186  }
187 
188  QVector<int> wanted;
189  wanted.reserve( nodes.size() );
190  for( const int node : nodes )
191  {
192  if( node >= 0 && node < mNodeCenters.size() )
193  {
194  wanted.push_back( node );
195  }
196  }
197  std::sort( wanted.begin(), wanted.end() );
198  wanted.erase( std::unique( wanted.begin(), wanted.end() ), wanted.end() );
199 
200  if( wanted == mHighlighted )
201  {
202  return;
203  }
204 
205  // Deleting an item takes it out of the scene.
206  qDeleteAll( mRingItems );
207  mRingItems.clear();
208 
209  QPen ring_pen( mHighlightColor, RING_WIDTH_PIXELS );
210  ring_pen.setCosmetic( true );
211 
212  mRingItems.reserve( wanted.size() );
213  for( const int node : wanted )
214  {
215  auto *ring = new QGraphicsEllipseItem(
216  -RING_RADIUS_PIXELS, -RING_RADIUS_PIXELS, 2.0 * RING_RADIUS_PIXELS, 2.0 * RING_RADIUS_PIXELS );
218  ring->setPos( mNodeCenters[node] );
219  ring->setPen( ring_pen );
220  ring->setBrush( Qt::NoBrush );
221  ring->setZValue( RING_Z_VALUE );
222 
223  addItem( ring );
224  mRingItems.push_back( ring );
225  }
226 
227  mHighlighted = wanted;
228  }
229 
231  {
232  if( mHighlighted.isEmpty() )
233  {
234  return {};
235  }
236 
237  // Not QRectF::united, which leaves out a rectangle without an area, such as a single point.
238  double left = mNodeCenters[mHighlighted.first()].x();
239  double right = left;
240  double top = mNodeCenters[mHighlighted.first()].y();
241  double bottom = top;
242 
243  for( const int node : mHighlighted )
244  {
245  left = std::min( left, mNodeCenters[node].x() );
246  right = std::max( right, mNodeCenters[node].x() );
247  top = std::min( top, mNodeCenters[node].y() );
248  bottom = std::max( bottom, mNodeCenters[node].y() );
249  }
250 
251  return { QPointF( left, top ), QPointF( right, bottom ) };
252  }
253 
255  {
256  // The items are deleted by the scene, only the pointers to them are ours.
257  mEdgeItems.clear();
258  mNodeItems.clear();
259  mNodeColors.clear();
260  mNodeCenters.clear();
261  mRingItems.clear();
262  mHighlighted.clear();
263  mContentRect = QRectF();
264 
265  clear();
266 
267  // Without a rectangle of its own the scene is as large as its items again.
268  setSceneRect( QRectF() );
269 
270  mHasLayout = false;
271  }
272 
274  {
275  return mStyle;
276  }
277 
279  {
280  if( !mHasLayout )
281  {
282  return ERR( "there is no clock tree to take a picture of" );
283  }
284 
285  const double margin = mStyle.picture_margin;
286  const QRectF source = mContentRect.adjusted( -margin, -margin, margin, margin );
287 
288  // The source is the drawing as it is, lines and dots with the width of their pens, so it is the one to fit to the
289  // limit. A resolution the user chose is kept, and too large a picture is an error then.
290  double pixels_per_unit = mStyle.pixels_per_unit;
291  const double longest_side = std::max( source.width(), source.height() );
292  const bool shrink =
293  !mStyle.pixels_per_unit_is_fixed && longest_side * pixels_per_unit > ctv::MAX_PICTURE_SIDE_PIXELS;
294  if( shrink )
295  {
296  pixels_per_unit = ctv::MAX_PICTURE_SIDE_PIXELS / longest_side;
297  }
298 
299  double width = std::ceil( source.width() * pixels_per_unit );
300  double height = std::ceil( source.height() * pixels_per_unit );
301  if( shrink )
302  {
303  // Rounding must not push a side that was fitted to the limit over it.
306  }
307 
309  {
310  return ERR( "the picture would be " + std::to_string( static_cast<long long>( width ) ) + " x "
311  + std::to_string( static_cast<long long>( height ) ) + " pixels, more than the limit of "
312  + std::to_string( static_cast<long long>( ctv::MAX_PICTURE_SIDE_PIXELS ) )
313  + " pixels per side, use a smaller pixels_per_unit" );
314  }
315 
316  QImage image( static_cast<int>( width ), static_cast<int>( height ), QImage::Format_RGB32 );
317  if( image.isNull() )
318  {
319  return ERR( "could not allocate a picture of " + std::to_string( static_cast<long long>( width ) ) + " x "
320  + std::to_string( static_cast<long long>( height ) ) + " pixels" );
321  }
322 
323  image.fill( background.isValid() ? background : QColor( Qt::white ) );
324 
325  QPainter painter( &image );
327  render( &painter, QRectF( 0, 0, width, height ), source );
328  painter.end();
329 
330  return OK( image );
331  }
332 } // namespace hal
const ctv::Style & style() const
Result< QImage > renderPicture(const QColor &background)
static QPointF fromChip(double x, double y)
void setHighlight(const QVector< int > &nodes)
ClockTreeScene(QObject *parent=nullptr)
void setLayout(const ctv::Layout &layout, const ctv::Style &style, const ClockTreeColors &colors)
static QPointF toChip(const QPointF &scenePosition)
QRectF highlightedRect() const
void setColors(const ClockTreeColors &colors)
#define ERR(message)
Definition: result.h:60
#define OK(...)
Definition: result.h:56
S to(const T &str)
constexpr double MAX_PICTURE_SIDE_PIXELS
Definition: defines.h:45
bool isValid() const const
QGraphicsEllipseItem * addEllipse(const QRectF &rect, const QPen &pen, const QBrush &brush)
void addItem(QGraphicsItem *item)
QGraphicsLineItem * addLine(const QLineF &line, const QPen &pen)
qreal height() const const
QRectF itemsBoundingRect() const const
void render(QPainter *painter, const QRectF &target, const QRectF &source, Qt::AspectRatioMode aspectRatioMode)
void setSceneRect(const QRectF &rect)
qreal width() const const
void fill(uint pixelValue)
bool isNull() const const
bool end()
void setRenderHint(QPainter::RenderHint hint, bool on)
void setCosmetic(bool cosmetic)
qreal x() const const
qreal y() const const
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
qreal height() const const
qreal width() const const
QVector::iterator begin()
void clear()
QVector::iterator end()
QVector::iterator erase(QVector::iterator begin, QVector::iterator end)
T & first()
bool isEmpty() const const
void push_back(const T &value)
void reserve(int size)
int size() const const
T value(int i) const const
QVector< QColor > nodes
std::size_t from
std::vector< Node > nodes
std::vector< Edge > edges