Repository navigation
Expand file tree
/
Copy pathTestTileGenerator.cpp
More file actions
6517 lines (5847 loc) · 249 KB
/
Copy pathTestTileGenerator.cpp
File metadata and controls
6517 lines (5847 loc) · 249 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors
#include <algorithm>
#include <array>
#include <atomic>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <map>
#include <memory>
#include <numbers>
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
#include "boost/json/object.hpp"
#include "boost/json/parse.hpp"
#include "boost/json/serialize.hpp"
#include "color.h"
#include "gtest/gtest.h"
#include "odb/db.h"
#include "odb/dbTypes.h"
#include "odb/geom.h"
#include "third-party/lodepng/lodepng.h"
#include "tile_generator.h"
#include "timing_report.h"
#include "tst/nangate45_fixture.h"
#include "web/heatMap.h"
namespace web {
namespace {
// Helper: parse a JSON literal into a boost::json::object for tests.
boost::json::object parseObj(std::string_view json)
{
return boost::json::parse(json).as_object();
}
// Fixed tile dimensions produced by TileGenerator (kTileSizeInPixel).
constexpr int kTileSize = 256;
// Square die side used by the tile-seam tests (DBU; 45 um at 2000 dbu/um).
constexpr int kSeamDieSide = 90000;
// The (CSS tile size, device pixel ratio) pairs the viewer actually asks for.
//
// A tile's CSS box is a whole number of device pixels only when the two
// multiply out whole, which is why the viewer uses 240 rather than 256 (see
// TILE_SIZE_CSS in tile-request.js). The ratios are real ones: display
// scaling, browser zoom, and the two multiplied. 1.6666666269302368 is
// verbatim from the display the tile seams were reported on — a float32 5/3,
// which is why nothing here can be asserted bit-exactly.
//
// Rendering is checked across all of them because every dpr bug in this
// pipeline was invisible at 1 and 2 and only appeared at a fractional ratio.
struct DprCase
{
int css_tile_size;
double dpr;
const char* what;
};
constexpr DprCase kDprCases[] = {
{240, 1.0, "no scaling"},
{240, 1.25, "125% display"},
{240, 1.3333333333333333, "133%, or 166% at 80% zoom"},
{240, 1.5, "150% display"},
{240, 1.6666666269302368, "166% display -- the reported case"},
{240, 1.75, "175% display"},
{240, 2.0, "200%"},
{240, 3.0, "300%"},
// The size a static report bakes its tiles at, which the viewer keeps.
{256, 1.0, "static report"},
};
// What the client asks the server to render for a case: the exact device-pixel
// square the tile will occupy, from the REAL ratio (mirrors tileDevicePx()).
int tilePxFor(const DprCase& c)
{
return static_cast<int>(std::lround(c.css_tile_size * c.dpr));
}
enum class Axis
{
kColumn,
kRow
};
// Minimal concrete heat map with a single populated bin, used to exercise
// number rendering across tile boundaries (issue #10925). getBounds() returns
// the block bbox passed by the caller (which the tests align to a tile seam);
// the tile grid uses TileGenerator::getBounds(), which adds a symmetric
// pin-label margin, so the seam stays at the bbox center where the bin sits.
class BoundaryHeatMap : public web::HeatMapDataSource
{
public:
BoundaryHeatMap(utl::Logger* logger,
const odb::Rect& bounds,
const odb::Rect& cell)
: web::HeatMapDataSource(logger,
"Boundary HM",
"BoundaryHM",
"BoundaryHM"),
bounds_(bounds),
cell_(cell)
{
}
odb::Rect getBounds() const override { return bounds_; }
// The label text is hardcoded here, so the numeric bin value is arbitrary --
// it only needs to mark the bin populated (see populateMap).
std::string formatValue(double /*value*/, bool /*legend*/) const override
{
return "29.89";
}
protected:
bool populateMap() override
{
// A sub-rectangle strictly inside the target bin, so addToMap (which marks
// every bin returned by getMapView, including zero-overlap neighbors)
// populates only that single bin. The value is arbitrary (see
// formatValue).
addToMap(odb::Rect(cell_.xMin() + 1,
cell_.yMin() + 1,
cell_.xMax() - 1,
cell_.yMax() - 1),
1.0);
return true;
}
void combineMapData(bool /*base_has_value*/,
double& base,
double new_data,
double /*data_area*/,
double /*intersection_area*/,
double /*rect_area*/) override
{
base = new_data;
}
private:
odb::Rect bounds_;
odb::Rect cell_;
};
// Return the set of columns (Axis::kColumn) or rows (Axis::kRow) where two RGBA
// tile buffers differ. Toggling "show numbers" leaves the bin fill untouched,
// so the diff isolates the rendered text pixels regardless of the fill color.
std::set<int> textPixels(const std::vector<unsigned char>& a,
const std::vector<unsigned char>& b,
Axis axis)
{
EXPECT_EQ(a.size(), b.size());
std::set<int> result;
const size_t num_pixels = std::min(a.size(), b.size()) / 4;
for (size_t p = 0; p < num_pixels; ++p) {
const size_t i = p * 4;
if (std::memcmp(&a[i], &b[i], 4) != 0) {
result.insert(axis == Axis::kColumn ? static_cast<int>(p % kTileSize)
: static_cast<int>(p / kTileSize));
}
}
return result;
}
// Sub-pixel x of a vertical coverage edge along `row`: the total UNCOVERED area
// to the left of it, in pixels. For a monotone left-to-right transition that
// integral *is* the edge position, whatever the reconstruction filter spreads
// over the pixels either side of it — a normalized filter preserves total
// coverage. The row must be fully covered at its right end; the alpha there is
// taken as the "covered" reference, so a fill drawn at any constant alpha
// works.
double coverageEdgeX(const std::vector<unsigned char>& rgba,
const int dim,
const int row)
{
const size_t base = static_cast<size_t>(row) * dim * 4;
const double full = rgba[base + static_cast<size_t>(dim - 1) * 4 + 3];
EXPECT_GT(full, 0.0) << "row " << row << " is not covered at its right edge";
if (full <= 0.0) {
return -1.0;
}
double uncovered = 0.0;
for (int x = 0; x < dim; ++x) {
const double a = rgba[base + static_cast<size_t>(x) * 4 + 3];
uncovered += 1.0 - std::min(1.0, a / full);
}
return uncovered;
}
// Columns of `row` with any coverage at all. Used where the fill is a faint
// wash under an opaque border (overlay highlights are alpha 30 with an alpha
// 255 outline), which defeats an alpha-weighted integral -- the border would
// count for eight times the fill it encloses.
int coveredColumns(const std::vector<unsigned char>& rgba,
const int dim,
const int row)
{
const size_t base = static_cast<size_t>(row) * dim * 4;
int covered = 0;
for (int x = 0; x < dim; ++x) {
if (rgba[base + static_cast<size_t>(x) * 4 + 3] > 0) {
covered++;
}
}
return covered;
}
// Covered width over columns [x0, x1) of `row`, in pixels: the coverage
// integral, normalized by the row's strongest alpha (the fill's own).
// Filter-independent for the same reason as coverageEdgeX.
double coveredWidthPx(const std::vector<unsigned char>& rgba,
const int dim,
const int row,
const int x0,
const int x1)
{
const size_t base = static_cast<size_t>(row) * dim * 4;
double full = 0.0;
for (int x = 0; x < dim; ++x) {
full = std::max(
full, static_cast<double>(rgba[base + static_cast<size_t>(x) * 4 + 3]));
}
if (full <= 0.0) {
return 0.0;
}
double covered = 0.0;
for (int x = x0; x < x1; ++x) {
covered += rgba[base + static_cast<size_t>(x) * 4 + 3] / full;
}
return covered;
}
constexpr double kPi = std::numbers::pi;
// Fraction of AC energy that sits in the moiré "beat band" (spatial periods
// 16..128 px), computed from the per-column and per-row alpha profiles (max of
// the two, so vertical/horizontal/diagonal beats are all caught). A real beat
// concentrates energy at long periods → high fraction; a finely-resolved grid
// concentrates at short periods → low fraction. This is the metric that
// distinguishes aliasing from legitimate detail (block-CV alone cannot).
double beatBandFraction1D(const std::vector<double>& sig)
{
const int n = static_cast<int>(sig.size());
if (n < 4) {
return 0.0;
}
double mean = 0.0;
for (const double v : sig) {
mean += v;
}
mean /= n;
double total = 0.0;
double band = 0.0;
for (int k = 1; k <= n / 2; ++k) {
double re = 0.0;
double im = 0.0;
for (int x = 0; x < n; ++x) {
const double ang = -2.0 * kPi * k * x / n;
const double centered = sig[x] - mean;
re += centered * std::cos(ang);
im += centered * std::sin(ang);
}
const double power = re * re + im * im;
total += power;
const double period = static_cast<double>(n) / k;
if (period >= 16.0 && period <= 128.0) {
band += power;
}
}
return total > 0.0 ? band / total : 0.0;
}
// Beat-band fraction measured over a sub-window of the tile. Measuring a
// central macro-uniform window (rather than the whole tile) avoids the
// low-frequency envelope from the array's outer edge / surrounding empty
// margin, which would otherwise masquerade as a beat. (x0,y0)-(x1,y1) half-
// open in pixels.
double beatFracWindow(const std::vector<unsigned char>& rgba,
int w,
int x0,
int y0,
int x1,
int y1)
{
const int ww = x1 - x0;
const int hh = y1 - y0;
std::vector<double> cols(ww, 0.0);
std::vector<double> rows(hh, 0.0);
for (int y = y0; y < y1; ++y) {
for (int x = x0; x < x1; ++x) {
const double a = rgba[(static_cast<size_t>(y) * w + x) * 4 + 3];
cols[x - x0] += a;
rows[y - y0] += a;
}
}
for (double& v : cols) {
v /= hh;
}
for (double& v : rows) {
v /= ww;
}
return std::max(beatBandFraction1D(cols), beatBandFraction1D(rows));
}
// Coefficient of variation of per-block mean alpha. High when the image has
// structure at the block scale (a resolved grid); ~0 for a uniform tint.
double blockAlphaCV(const std::vector<unsigned char>& rgba,
int w,
int h,
int block)
{
std::vector<double> means;
for (int by = 0; by + block <= h; by += block) {
for (int bx = 0; bx + block <= w; bx += block) {
double s = 0.0;
for (int y = by; y < by + block; ++y) {
for (int x = bx; x < bx + block; ++x) {
s += rgba[(static_cast<size_t>(y) * w + x) * 4 + 3];
}
}
means.push_back(s / (block * block));
}
}
if (means.empty()) {
return 0.0;
}
double mean = 0.0;
for (const double v : means) {
mean += v;
}
mean /= means.size();
if (mean <= 0.0) {
return 0.0;
}
double var = 0.0;
for (const double v : means) {
var += (v - mean) * (v - mean);
}
var /= means.size();
return std::sqrt(var) / mean;
}
class TileGeneratorTest : public tst::Nangate45Fixture
{
protected:
void SetUp() override
{
// Nangate45Fixture gives us a chip + block with die area (0,0)-(1000,1000).
// Enlarge to fit standard cells (Nangate45 LEF units = 2000, so
// 100000 dbu = 50 um).
block_->setDieArea(odb::Rect(0, 0, 100000, 100000));
}
// Create TileGenerator. Call this after placing any instances so
// that the block BBox (used by getBounds) is up to date.
void makeTileGen()
{
tile_gen_ = std::make_unique<TileGenerator>(
getDb(), /*sta=*/nullptr, getLogger());
}
// Shrink the die onto the placed content. getBounds() covers the die area
// too, so a test whose shapes sit in one corner of the fixture's 50 um die
// would otherwise frame the whole die and render those shapes sub-pixel.
// Call after placing content and before makeTileGen().
void fitDieToContent() { block_->setDieArea(block_->getBBox()->getBox()); }
// Decode a PNG byte vector into raw RGBA pixels.
std::vector<unsigned char> decodePng(
const std::vector<unsigned char>& png_data,
unsigned& width,
unsigned& height)
{
std::vector<unsigned char> pixels;
unsigned err = lodepng::decode(pixels, width, height, png_data);
EXPECT_EQ(err, 0u) << lodepng_error_text(err);
return pixels;
}
// Return true if any pixel in the RGBA buffer has alpha > 0.
static bool hasNonTransparentPixel(const std::vector<unsigned char>& rgba)
{
for (size_t i = 3; i < rgba.size(); i += 4) {
if (rgba[i] > 0) {
return true;
}
}
return false;
}
static size_t countNonTransparentPixels(
const std::vector<unsigned char>& rgba)
{
size_t count = 0;
for (size_t i = 3; i < rgba.size(); i += 4) {
if (rgba[i] > 0) {
++count;
}
}
return count;
}
// Render the _instances tile twice, with and without the blockage hatch,
// and report how many of the label's solid pixels the hatch washed out.
//
// The hatch shows up in the blue channel: the label over a hatch line stays
// strongly yellow (min(R,G) - B ~ 210), while a hatch line over the label
// flattens it (~0 once the hatch is opaque grey). Alpha is only tested for
// "mostly covered" because a glyph's anti-aliased pixels come back below the
// 220 the label colour carries.
struct LabelWash
{
int label_px = 0;
int washed_out = 0;
int min_yellowness = 255;
std::vector<unsigned char> hatched;
unsigned w = 0;
unsigned h = 0;
};
LabelWash measureLabelWash()
{
LabelWash out;
TileVisibility vis;
vis.inst_names = true;
unsigned pw = 0, ph = 0;
vis.placement_blockages = false;
const auto plain = decodePng(
tile_gen_->generateTile("_instances", 0, 0, 0, vis), pw, ph);
vis.placement_blockages = true;
out.hatched = decodePng(
tile_gen_->generateTile("_instances", 0, 0, 0, vis), out.w, out.h);
EXPECT_EQ(pw, out.w);
EXPECT_EQ(ph, out.h);
for (size_t i = 0; i + 3 < plain.size() && i + 3 < out.hatched.size();
i += 4) {
const bool solid_label = plain[i] > 200 && plain[i + 1] > 200
&& plain[i + 2] < 40 && plain[i + 3] > 128;
if (!solid_label) {
continue;
}
++out.label_px;
const int yellowness = std::min<int>(out.hatched[i], out.hatched[i + 1])
- out.hatched[i + 2];
out.min_yellowness = std::min(out.min_yellowness, yellowness);
if (yellowness < 100) {
++out.washed_out;
}
}
return out;
}
static void expectLabelSurvived(const LabelWash& wash)
{
ASSERT_GT(wash.label_px, 150)
<< "precondition: the label must be large enough to meet several "
"hatch lines; only "
<< wash.label_px << " solid pixels";
EXPECT_EQ(wash.washed_out, 0)
<< wash.washed_out << " of " << wash.label_px
<< " label pixels were painted over by the blockage hatch (weakest "
"yellowness "
<< wash.min_yellowness << ")";
}
// True if the RGBA pixel at `p` carries the die/core outline colour. Alpha
// is NOT checked because edge pixels can come back with partial coverage
// while the RGB stays kOutlineGray. Read from the shared
// constant rather than spelled out, so the renderer cannot drift from it.
static bool isOutlineGray(const unsigned char* p)
{
return p[0] == kOutlineGray.r && p[1] == kOutlineGray.g
&& p[2] == kOutlineGray.b;
}
// True if any visible pixel isn't part of the always-on die/core outline
// drawn on the _instances pass.
static bool hasNonOutlinePixel(const std::vector<unsigned char>& rgba)
{
for (size_t i = 0; i + 3 < rgba.size(); i += 4) {
if (rgba[i + 3] == 0) {
continue;
}
if (!isOutlineGray(&rgba[i])) {
return true;
}
}
return false;
}
// True if any pixel carries the green of a row/site outline (row_color in
// renderTileBuffer). Distinguishes them from the neutral gray die/core
// outline, which a plain "is anything drawn" check cannot: the outline is
// always painted on the _instances pass, so it satisfies that check on its
// own. Tested by dominance rather than equality because the green blends
// with whatever it crosses.
static bool hasRowColorPixel(const std::vector<unsigned char>& rgba)
{
for (size_t i = 0; i + 3 < rgba.size(); i += 4) {
if (rgba[i + 3] == 0) {
continue;
}
if (rgba[i + 1] > rgba[i] + 10 && rgba[i + 1] > rgba[i + 2] + 10) {
return true;
}
}
return false;
}
// DBU -> tile pixel, for the tile the generator produced at some zoom. The
// scale (bounds.maxDXDY() spread over the tile's width) and the Y flip are
// the tile georeference; keeping one copy means a change to it breaks the
// tests loudly instead of leaving them measuring the wrong pixel.
// Unclamped: callers that inset or window around the result need to see
// out-of-range values before they clamp.
static int colOf(const odb::Rect& bounds, unsigned w, int dbu)
{
const double dbu_per_px = static_cast<double>(bounds.maxDXDY()) / w;
return static_cast<int>((dbu - bounds.xMin()) / dbu_per_px);
}
static int rowOf(const odb::Rect& bounds, unsigned w, unsigned h, int dbu)
{
const double dbu_per_px = static_cast<double>(bounds.maxDXDY()) / w;
return static_cast<int>((h - 1) - (dbu - bounds.yMin()) / dbu_per_px);
}
odb::dbInst* placeInst(const char* master_name,
const char* inst_name,
int x,
int y)
{
odb::dbMaster* master = lib_->findMaster(master_name);
EXPECT_NE(master, nullptr) << "Master not found: " << master_name;
odb::dbInst* inst = odb::dbInst::create(block_, master, inst_name);
inst->setLocation(x, y);
inst->setPlacementStatus(odb::dbPlacementStatus::PLACED);
return inst;
}
// Create a BTerm pin on a metal layer at the die boundary.
void makeBTermAtEdge(const char* name,
const char* layer_name,
int x,
int y,
int w,
int h,
odb::dbIoType io_type = odb::dbIoType::INPUT)
{
odb::dbNet* net = odb::dbNet::create(block_, name);
odb::dbBTerm* bterm = odb::dbBTerm::create(net, name);
bterm->setIoType(io_type);
odb::dbBPin* bpin = odb::dbBPin::create(bterm);
odb::dbTechLayer* layer = getDb()->getTech()->findLayer(layer_name);
ASSERT_NE(layer, nullptr);
odb::dbBox::create(bpin, layer, x, y, x + w, y + h);
bpin->setPlacementStatus(odb::dbPlacementStatus::PLACED);
}
// Create a BTerm on an existing net (for net-type filtering tests).
void makeBTermOnNet(const char* name,
odb::dbNet* net,
const char* layer_name,
int x,
int y,
int w,
int h)
{
odb::dbBTerm* bterm = odb::dbBTerm::create(net, name);
bterm->setIoType(odb::dbIoType::INPUT);
odb::dbBPin* bpin = odb::dbBPin::create(bterm);
odb::dbTechLayer* layer = getDb()->getTech()->findLayer(layer_name);
ASSERT_NE(layer, nullptr);
odb::dbBox::create(bpin, layer, x, y, x + w, y + h);
bpin->setPlacementStatus(odb::dbPlacementStatus::PLACED);
}
// Build a square design whose z=1 tile seam is centered on the die, attach a
// single-bin heat map (bin grid over the block bbox) whose populated bin is
// `cell`, and stash it in heatmap_. Invoke via ASSERT_NO_FATAL_FAILURE so a
// geometry-assert failure aborts the caller.
void buildSeamDesign(const odb::Rect& cell)
{
odb::dbMaster* master = lib_->findMaster("BUF_X16");
ASSERT_NE(master, nullptr);
const int w = master->getWidth();
const int h = master->getHeight();
block_->setDieArea(odb::Rect(0, 0, kSeamDieSide, kSeamDieSide));
placeInst("BUF_X16", "buf_ll", 0, 0);
placeInst("BUF_X16", "buf_ur", kSeamDieSide - w, kSeamDieSide - h);
makeTileGen();
// The bin grid uses the (clean) block bbox; the tile grid uses getBounds(),
// which adds a symmetric pin-label margin, so both seams stay at the bbox
// center -- where the target bin is centered -- and bins/tiles agree there.
const odb::Rect blk = block_->getBBox()->getBox();
ASSERT_EQ(blk.xMin(), 0);
ASSERT_EQ(blk.yMin(), 0);
ASSERT_EQ(blk.xMax(), kSeamDieSide);
ASSERT_EQ(blk.yMax(), kSeamDieSide);
const odb::Rect bounds = tile_gen_->getBounds();
ASSERT_EQ(bounds.dx(), bounds.dy()); // square => seams at center...
ASSERT_EQ(bounds.xMin() + bounds.xMax(), kSeamDieSide); // ...x = kSide/2
ASSERT_EQ(bounds.yMin() + bounds.yMax(), kSeamDieSide); // ...y = kSide/2
heatmap_ = std::make_unique<BoundaryHeatMap>(getLogger(), blk, cell);
heatmap_->setChip(chip_);
heatmap_->setGridSizes(15.0, 15.0); // 15 um bins -> 30000 DBU (3x3 grid)
// Never gate the bin out of the visible map on value range.
heatmap_->setDrawBelowRangeMin(true);
heatmap_->setDrawAboveRangeMax(true);
}
// Render tile (zoom,x,y) of heatmap_ with numbers on and off and return the
// columns/rows (per `axis`) whose pixels the label adds.
std::set<int> seamTextPixels(int zoom, int x, int y, Axis axis)
{
unsigned width = 0;
unsigned height = 0;
heatmap_->setShowNumbers(true);
const std::vector<unsigned char> on = decodePng(
tile_gen_->generateHeatMapTile(*heatmap_, zoom, x, y), width, height);
heatmap_->setShowNumbers(false);
const std::vector<unsigned char> off = decodePng(
tile_gen_->generateHeatMapTile(*heatmap_, zoom, x, y), width, height);
return textPixels(on, off, axis);
}
// Create an IO pin (net+bterm+bpin box on metal1) carrying one
// dbAccessPoint at (2000,2000) with access granted. Returns nullptr
// if metal1 is missing.
odb::dbAccessPoint* makeMetal1AccessPoint()
{
odb::dbNet* net = odb::dbNet::create(block_, "ap_pin");
odb::dbBTerm* bterm = odb::dbBTerm::create(net, "ap_pin");
bterm->setIoType(odb::dbIoType::INPUT);
odb::dbBPin* bpin = odb::dbBPin::create(bterm);
odb::dbTechLayer* metal1 = getDb()->getTech()->findLayer("metal1");
if (!metal1) {
return nullptr;
}
odb::dbBox::create(bpin, metal1, 1900, 1900, 2100, 2100);
bpin->setPlacementStatus(odb::dbPlacementStatus::PLACED);
odb::dbAccessPoint* ap = odb::dbAccessPoint::create(bpin);
ap->setPoint(odb::Point(2000, 2000));
ap->setLayer(metal1);
ap->setAccess(true, odb::dbDirection::EAST);
return ap;
}
std::unique_ptr<TileGenerator> tile_gen_;
std::unique_ptr<BoundaryHeatMap> heatmap_;
};
TEST_F(TileGeneratorTest, HasStaFalseWhenNull)
{
makeTileGen();
EXPECT_FALSE(tile_gen_->hasSta());
}
TEST_F(TileGeneratorTest, GetBoundsReflectsInstances)
{
placeInst("BUF_X16", "buf1", 0, 0);
makeTileGen();
odb::Rect bounds = tile_gen_->getBounds();
// Bounds should encompass the placed instance.
EXPECT_GT(bounds.dx(), 0);
EXPECT_GT(bounds.dy(), 0);
EXPECT_LE(bounds.xMin(), 10000);
EXPECT_LE(bounds.yMin(), 10000);
}
TEST_F(TileGeneratorTest, BoundsIncludeLabelMargin)
{
// Place instances to fill the BBox across the die.
placeInst("BUF_X16", "buf_ll", 0, 0);
placeInst("BUF_X16", "buf_ur", 90000, 90000);
// Create a BTerm pin at the right die edge.
const char* pin_name = "my_long_pin_name";
odb::dbNet* net = odb::dbNet::create(block_, pin_name);
odb::dbBTerm* bterm = odb::dbBTerm::create(net, pin_name);
bterm->setIoType(odb::dbIoType::INPUT);
odb::dbBPin* bpin = odb::dbBPin::create(bterm);
odb::dbTechLayer* m1 = getDb()->getTech()->findLayer("metal1");
ASSERT_NE(m1, nullptr);
// Place at right die edge (x=99800..100000).
odb::dbBox::create(bpin, m1, 99800, 50000, 100000, 50200);
bpin->setPlacementStatus(odb::dbPlacementStatus::PLACED);
makeTileGen();
const odb::Rect die = block_->getDieArea();
const odb::Rect bounds = tile_gen_->getBounds();
// The margin should be larger than just the pin marker size,
// because it now accounts for the label text width.
const int pin_max = tile_gen_->getPinMaxSize();
const int margin = bounds.xMax() - die.xMax();
EXPECT_GT(margin, pin_max);
}
// The request path quantizes dpr into [1, 3] before it ever reaches the
// generator, but generateTile() takes it as a plain argument, and several
// derived quantities are unsafe at zero -- a zero font height, a zero hatch
// period, and latticeAnchor()'s modulo by that period. So the generator
// clamps too, and a caller that skips the request path still gets a tile
// rendered at the nearest supported ratio instead of a crash.
TEST_F(TileGeneratorTest, OutOfRangeDprIsClampedNotHonored)
{
placeInst("BUF_X16", "buf1", 0, 0);
makeTileGen();
tile_gen_->eagerInit();
TileVisibility vis;
const auto tile_at = [&](double dpr) {
return tile_gen_->generateTile("_instances",
0,
0,
0,
vis,
{},
{},
{},
{},
nullptr,
nullptr,
nullptr,
dpr);
};
unsigned w = 0, h = 0;
auto baseline = decodePng(tile_at(1.0), w, h);
ASSERT_GT(w, 0u);
const unsigned baseline_px = w;
// Below the range, and degenerate values, all render as dpr 1.
for (const double dpr : {0.001, 0.5, 0.0, -1.0}) {
SCOPED_TRACE(dpr);
unsigned dw = 0, dh = 0;
auto pixels = decodePng(tile_at(dpr), dw, dh);
EXPECT_EQ(dw, baseline_px);
EXPECT_EQ(pixels, baseline);
}
// Above the range it saturates rather than scaling without bound.
unsigned hw = 0, hh = 0;
decodePng(tile_at(1000.0), hw, hh);
EXPECT_EQ(hw, baseline_px * 3);
}
// Issue #11280: a floorplan holding one macro in a corner of a much larger die
// framed on the macro, because dbBlock::getBBox() covers the placed SHAPES and
// not the die. Qt's LayoutViewer::getBounds() merges the die area; so must
// this one.
TEST_F(TileGeneratorTest, BoundsCoverDieAreaWhenContentIsSmaller)
{
placeInst("BUF_X16", "lone", 90000, 90000);
makeTileGen();
const odb::Rect die = block_->getDieArea();
const odb::Rect bbox = block_->getBBox()->getBox();
ASSERT_LT(bbox.dx(), die.dx()) << "precondition: content smaller than die";
const odb::Rect bounds = tile_gen_->getBounds();
EXPECT_LE(bounds.xMin(), die.xMin());
EXPECT_LE(bounds.yMin(), die.yMin());
EXPECT_GE(bounds.xMax(), die.xMax());
EXPECT_GE(bounds.yMax(), die.yMax());
}
// Issue #11338: getBounds() reserves room for the pin labels that hang outward
// from the die edge, because the tile grid is clamped to it and labels outside
// would have no tiles. That margin must not reach the zoom-to-fit box, or the
// design is framed with it as dead space around the design.
TEST_F(TileGeneratorTest, FitBoundsExcludesThePinLabelMargin)
{
placeInst("BUF_X16", "inst", 0, 0);
// A long name, so the label margin is large enough to be unmistakable.
makeBTermAtEdge("a_deliberately_long_pin_name", "metal1", 0, 40000, 200, 200);
makeTileGen();
const odb::Rect fit = tile_gen_->getFitBounds();
const odb::Rect geo = tile_gen_->getBounds();
// The fit box is the design proper: the die, as Qt's LayoutViewer::getBounds
// returns it.
const odb::Rect die = block_->getDieArea();
EXPECT_LE(fit.xMin(), die.xMin());
EXPECT_LE(fit.yMin(), die.yMin());
EXPECT_GE(fit.xMax(), die.xMax());
EXPECT_GE(fit.yMax(), die.yMax());
// The georeference rect is that box grown by the same margin on all four
// sides -- which is what the tiles need and the fit must not carry.
const int margin = fit.xMin() - geo.xMin();
EXPECT_GT(margin, 0) << "precondition: this design has a label margin";
EXPECT_EQ(geo.xMax() - fit.xMax(), margin);
EXPECT_EQ(fit.yMin() - geo.yMin(), margin);
EXPECT_EQ(geo.yMax() - fit.yMax(), margin);
}
// The margin scales with the longest pin name, so the georeference rect grows
// as names get longer. The framing rect must not move with them at all --
// that is the whole point of keeping the two apart.
TEST_F(TileGeneratorTest, FitBoundsIsIndifferentToPinNameLength)
{
placeInst("BUF_X16", "inst", 0, 0);
makeBTermAtEdge("s", "metal1", 0, 40000, 200, 200);
makeTileGen();
const odb::Rect short_fit = tile_gen_->getFitBounds();
const odb::Rect short_geo = tile_gen_->getBounds();
makeBTermAtEdge(
"a_very_much_longer_pin_name_indeed", "metal1", 0, 50000, 200, 200);
makeTileGen();
EXPECT_EQ(tile_gen_->getFitBounds(), short_fit);
EXPECT_LT(tile_gen_->getBounds().xMin(), short_geo.xMin());
}
// The consequence of the bug above, and the one the reporter saw: the tile
// grid is georeferenced on getBounds() and its indices are clamped to it, so
// die area outside those bounds had no tiles at all and simply went missing.
TEST_F(TileGeneratorTest, DieOutlineFarFromContentIsRasterized)
{
// One instance in the upper-right corner; the die's lower-left corner is as
// far from it as this fixture allows.
placeInst("BUF_X16", "lone", 90000, 90000);
makeTileGen();
tile_gen_->eagerInit();
TileVisibility vis;
auto png = tile_gen_->generateTile("_instances", 0, 0, 0, vis);
unsigned w = 0, h = 0;
auto pixels = decodePng(png, w, h);
// Both dimensions: they bound the std::clamp ranges below, which need
// lo <= hi.
ASSERT_GT(w, 0u);
ASSERT_GT(h, 0u);
// Sample where the die's lower-left corner lands at z=0.
const odb::Rect bounds = tile_gen_->getBounds();
const odb::Rect die = block_->getDieArea();
const auto col_of = [&](int dbu) {
return static_cast<unsigned>(
std::clamp(colOf(bounds, w, dbu), 0, static_cast<int>(w) - 1));
};
const auto row_of = [&](int dbu) {
return static_cast<unsigned>(
std::clamp(rowOf(bounds, w, h, dbu), 0, static_cast<int>(h) - 1));
};
// The outline is a hairline that antialiasing spreads a pixel or two, so
// accept a hit anywhere in a small window around the corner.
const auto drawn_near = [&](unsigned cx, unsigned cy) {
for (unsigned y = (cy > 2 ? cy - 2 : 0); y <= std::min(cy + 2, h - 1);
++y) {
for (unsigned x = (cx > 2 ? cx - 2 : 0); x <= std::min(cx + 2, w - 1);
++x) {
if (pixels[4UL * (y * w + x) + 3] > 0) {
return true;
}
}
}
return false;
};
EXPECT_TRUE(drawn_near(col_of(die.xMin()), row_of(die.yMin() + die.dy() / 2)))
<< "the die's left edge must be rasterized, not clipped away";
EXPECT_TRUE(drawn_near(col_of(die.xMin() + die.dx() / 2), row_of(die.yMin())))
<< "the die's bottom edge must be rasterized, not clipped away";
}
// Qt draws a diagonal across the master's origin corner
// (drawInstanceOutlines), which is what tells a flipped instance from an
// unflipped one. The tag rides the instance transform, so R0 puts it at the
// bottom-left of the footprint and MX at the top-left.
//
// Renders the instance alone: the hatch and the name would both put ink in the
// interior, and the tag is the only thing left that can.
TEST_F(TileGeneratorTest, OrientationTagMarksTheMasterOrigin)
{
odb::dbInst* inst
= placeInst("BUF_X16", "a_long_instance_name_to_label", 0, 0);
TileVisibility vis;
vis.placement_blockages = false;
vis.inst_names = false;
// Counts interior ink per half of the footprint, ignoring a 3 px frame so
// the instance outline itself is never sampled.
const std::pair<odb::dbOrientType, bool> cases[]
= {{odb::dbOrientType::R0, true}, {odb::dbOrientType::MX, false}};
for (const auto& [orient, expect_bottom] : cases) {
SCOPED_TRACE(odb::dbOrientType(orient).getString());
inst->setOrient(orient);
fitDieToContent();
makeTileGen();
tile_gen_->eagerInit();
auto png = tile_gen_->generateTile("_instances", 0, 0, 0, vis);
unsigned w = 0, h = 0;
auto pixels = decodePng(png, w, h);
ASSERT_GT(w, 16u);
const odb::Rect bounds = tile_gen_->getBounds();
const odb::Rect box = inst->getBBox()->getBox();
// Rows grow downwards, so the footprint's yMin is the LAST row.
const int top = rowOf(bounds, w, h, box.yMax()) + 3;
const int bottom = rowOf(bounds, w, h, box.yMin()) - 3;
const int left = colOf(bounds, w, box.xMin()) + 3;
const int right = colOf(bounds, w, box.xMax()) - 3;
ASSERT_LT(top, bottom) << "footprint too small to sample";
const int mid = (top + bottom) / 2;
int ink_top = 0, ink_bottom = 0;
for (int y = std::max(top, 0); y <= std::min<int>(bottom, h - 1); ++y) {
for (int x = std::max(left, 0); x <= std::min<int>(right, w - 1); ++x) {
if (pixels[4UL * (y * w + x) + 3] > 0) {
if (y < mid) {
++ink_top;
} else {
++ink_bottom;
}
}
}
}
ASSERT_GT(ink_top + ink_bottom, 0) << "no orientation tag was drawn";
EXPECT_EQ(ink_bottom > ink_top, expect_bottom);
}
}
// Qt fills every visible instance with the placement-blockage hatch, in
// drawBlockages(), whether or not the design has a real dbBlockage — that is
// what makes a lone macro read as a solid object rather than an empty frame.
TEST_F(TileGeneratorTest, InstanceFootprintIsHatched)
{
placeInst("BUF_X16", "buf1", 0, 0);
fitDieToContent();
makeTileGen();
tile_gen_->eagerInit();
TileVisibility vis;
vis.inst_names = false;
vis.placement_blockages = true;
auto png_on = tile_gen_->generateTile("_instances", 0, 0, 0, vis);
unsigned w = 0, h = 0;
auto pixels_on = decodePng(png_on, w, h);
vis.placement_blockages = false;
auto png_off = tile_gen_->generateTile("_instances", 0, 0, 0, vis);
auto pixels_off = decodePng(png_off, w, h);
// The hatch is many pixels; the outline and the tag under it are a handful.
EXPECT_GT(countNonTransparentPixels(pixels_on),
2 * countNonTransparentPixels(pixels_off))
<< "the instance footprint should be hatched when blockages are shown";
}
// Qt brushes blockages with Qt::BDiagPattern, whose lines run "/" -- up to the
// right. Mirroring them to "\" is the same texture but not the same picture,
// and the two GUIs sit side by side in review screenshots.
TEST_F(TileGeneratorTest, BlockageHatchRunsUpToTheRightLikeQt)
{
placeInst("BUF_X16", "buf1", 0, 0);
fitDieToContent();
makeTileGen();
tile_gen_->eagerInit();
TileVisibility vis;
vis.inst_names = false;
vis.placement_blockages = true;
unsigned w = 0, h = 0;
const auto px
= decodePng(tile_gen_->generateTile("_instances", 0, 0, 0, vis), w, h);
// Chase each lit pixel along both diagonals and keep the longest unbroken
// run. A "/" line runs its whole length up-to-the-right, while across the
// other diagonal it is only as long as the band is wide -- a separation that
// holds whatever the lattice period is, unlike counting neighbours, where a
// band a quarter of the period wide already answers to both directions.
const auto is_hatch = [&](int x, int y) {
if (x < 0 || y < 0 || x >= static_cast<int>(w)
|| y >= static_cast<int>(h)) {
return false;
}
const size_t i = 4UL * (static_cast<unsigned>(y) * w + x);
// Any coverage counts: a 1 px line at 45 degrees need not leave a fully
// covered pixel. The hatch is the only neutral grey on this tile once the
// outline is excluded.