File size: 19,576 Bytes
5cdf637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors

#include <unistd.h>

#include <cstddef>
#include <cstring>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>

#include "boost/json/parse.hpp"
#include "gtest/gtest.h"
#include "gui/heatMap.h"
#include "odb/db.h"
#include "odb/dbTypes.h"
#include "odb/geom.h"
#include "third-party/lodepng/lodepng.h"
#include "tile_generator.h"
#include "tst/nangate45_fixture.h"

namespace web {
namespace {

class SaveImageTest : public tst::Nangate45Fixture
{
 protected:
  void SetUp() override
  {
    block_->setDieArea(odb::Rect(0, 0, 100000, 100000));
    placeInst("BUF_X16", "buf1", 10000, 10000);
    makeTileGen();
  }

  void TearDown() override
  {
    // Clean up any output files.
    for (const auto& path : output_files_) {
      std::filesystem::remove(path);
    }
  }

  void makeTileGen()
  {
    tile_gen_ = std::make_unique<TileGenerator>(
        getDb(), /*sta=*/nullptr, getLogger());
    tile_gen_->eagerInit();
  }

  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);
  }

  // Save to a temp file and register for cleanup.
  std::string tempPng(const std::string& label)
  {
    std::string path
        = std::filesystem::temp_directory_path()
          / ("web_test_" + label + "_" + std::to_string(::getpid()) + ".png");
    output_files_.push_back(path);
    return path;
  }

  // Decode a PNG file from disk; returns RGBA pixels.
  std::vector<unsigned char> decodePngFile(const std::string& path,
                                           unsigned& width,
                                           unsigned& height)
  {
    std::vector<unsigned char> pixels;
    unsigned err = lodepng::decode(pixels, width, height, path);
    EXPECT_EQ(err, 0u) << lodepng_error_text(err);
    return pixels;
  }

  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;
  }

  // True if any visible pixel isn't part of the always-on die/core outline,
  // which getBounds() now guarantees is in every saved image.  Matches
  // TileGeneratorTest::hasNonOutlinePixel: the outline is neutral gray
  // (kOutlineGray) and alpha is NOT checked, because tiles are rasterized
  // supersampled and decimated, so its edge pixels come back at partial
  // coverage while the RGB stays 128,128,128.  Testing by colour rather than
  // by carving out a border keeps the die edge itself in scope β€” that is
  // where pin markers are drawn.
  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 (rgba[i] != 128 || rgba[i + 1] != 128 || rgba[i + 2] != 128) {
        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;
  }

  std::unique_ptr<TileGenerator> tile_gen_;
  std::vector<std::string> output_files_;
};

// ─── Basic functionality ─────────────────────────────────────────────────────

TEST_F(SaveImageTest, DefaultProducesValidPng)
{
  const std::string path = tempPng("default");
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 0, 0, {});

  ASSERT_TRUE(std::filesystem::exists(path));
  unsigned w = 0, h = 0;
  auto pixels = decodePngFile(path, w, h);
  EXPECT_GT(w, 0u);
  EXPECT_GT(h, 0u);
  // Should contain visible content (placed instance).
  EXPECT_TRUE(hasNonTransparentPixel(pixels));
}

TEST_F(SaveImageTest, WidthOption)
{
  const std::string path = tempPng("width");
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 512, 0, {});

  unsigned w = 0, h = 0;
  decodePngFile(path, w, h);
  EXPECT_EQ(w, 512u);
}

TEST_F(SaveImageTest, ResolutionOption)
{
  const std::string path = tempPng("resolution");
  // 10 dbu per pixel on a 100000 dbu (+margin) design β†’ ~10000+ pixels wide.
  // Use a coarser resolution to keep the test fast.
  const double dbu_per_pixel = 100.0;
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 0, dbu_per_pixel, {});

  unsigned w = 0, h = 0;
  decodePngFile(path, w, h);
  // Expected: ~100000 / 100 * 1.05 (bloat) β‰ˆ 1050.
  // Allow some tolerance for rounding and bloat margin.
  EXPECT_GT(w, 500u);
  EXPECT_LT(w, 2000u);
}

TEST_F(SaveImageTest, ExplicitAreaOption)
{
  const std::string path = tempPng("area");
  // Render only the bottom-left quadrant.
  const odb::Rect area(0, 0, 50000, 50000);
  tile_gen_->saveImage(path, area, 256, 0, {});

  unsigned w = 0, h = 0;
  auto pixels = decodePngFile(path, w, h);
  EXPECT_EQ(w, 256u);
  // Aspect ratio should be ~1:1 for a square area.
  EXPECT_EQ(h, 256u);
}

// ─── Visibility options ──────────────────────────────────────────────────────

TEST_F(SaveImageTest, VisibilityStdcellsOff)
{
  const std::string path = tempPng("vis_off");
  TileVisibility vis;
  vis.stdcells = false;
  // With stdcells hidden and no routing, the _instances layer holds nothing
  // but the die outline, which Qt draws regardless of instance visibility.
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 256, 0, vis);

  unsigned w = 0, h = 0;
  auto pixels = decodePngFile(path, w, h);
  EXPECT_FALSE(hasNonOutlinePixel(pixels));
}

TEST_F(SaveImageTest, VisibilityPinsOff_Markers)
{
  // Place a BTerm to generate pin markers.
  makeBTermAtEdge("clk", "metal1", 0, 50000, 200, 200);
  makeTileGen();

  const std::string path_on = tempPng("pins_on");
  TileVisibility vis_on;
  vis_on.stdcells = false;
  tile_gen_->saveImage(path_on, odb::Rect(0, 0, 0, 0), 512, 0, vis_on);

  const std::string path_off = tempPng("pins_off");
  TileVisibility vis_off;
  vis_off.stdcells = false;
  vis_off.pins = false;
  tile_gen_->saveImage(path_off, odb::Rect(0, 0, 0, 0), 512, 0, vis_off);

  unsigned w1 = 0, h1 = 0, w2 = 0, h2 = 0;
  auto pixels_on = decodePngFile(path_on, w1, h1);
  auto pixels_off = decodePngFile(path_off, w2, h2);

  // With pins on, there should be visible content from the marker.
  // With it off, no content.
  EXPECT_TRUE(hasNonTransparentPixel(pixels_on));
  EXPECT_NE(pixels_on, pixels_off);
}

TEST_F(SaveImageTest, VisibilityPinsOff)
{
  // BTerm shapes (tech layers + markers) should be hidden when vis.pins=false.
  makeBTermAtEdge("clk", "metal1", 0, 50000, 5000, 5000);
  makeTileGen();

  const std::string path_on = tempPng("bterm_on");
  TileVisibility vis_on;
  vis_on.stdcells = false;
  vis_on.pins = true;
  tile_gen_->saveImage(path_on, odb::Rect(0, 0, 0, 0), 512, 0, vis_on);

  const std::string path_off = tempPng("bterm_off");
  TileVisibility vis_off;
  vis_off.stdcells = false;
  vis_off.pins = false;
  tile_gen_->saveImage(path_off, odb::Rect(0, 0, 0, 0), 512, 0, vis_off);

  unsigned w1 = 0, h1 = 0, w2 = 0, h2 = 0;
  auto pixels_on = decodePngFile(path_on, w1, h1);
  auto pixels_off = decodePngFile(path_off, w2, h2);

  // The die and core outlines are drawn unconditionally on the _instances
  // pass (Qt drawChip parity), so the "pins off" image is never fully
  // transparent.  What the toggle must guarantee is that hiding pins only
  // ever takes pixels away β€” every BTerm shape and marker disappears and
  // nothing new is drawn in their place.
  EXPECT_TRUE(hasNonTransparentPixel(pixels_on))
      << "BTerm shapes should appear with vis.pins=true";
  EXPECT_LT(countNonTransparentPixels(pixels_off),
            countNonTransparentPixels(pixels_on))
      << "BTerm shapes should be hidden with vis.pins=false";
}

// ─── Edge cases ──────────────────────────────────────────────────────────────

TEST_F(SaveImageTest, EmptyDesign)
{
  // Create a fresh block with no instances.
  odb::dbChip::destroy(chip_);
  chip_ = odb::dbChip::create(getDb(), getDb()->getTech());
  block_ = odb::dbBlock::create(chip_, "empty");
  block_->setDefUnits(lib_->getTech()->getLefUnits());
  block_->setDieArea(odb::Rect(0, 0, 100000, 100000));
  makeTileGen();

  const std::string path = tempPng("empty");
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 256, 0, {});

  ASSERT_TRUE(std::filesystem::exists(path));
  unsigned w = 0, h = 0;
  auto pixels = decodePngFile(path, w, h);
  EXPECT_EQ(w, 256u);
  // A design with no shapes still has a floorplan: the die outline is drawn
  // and nothing else.
  EXPECT_FALSE(hasNonOutlinePixel(pixels));
  EXPECT_TRUE(hasNonTransparentPixel(pixels))
      << "the die outline should still be drawn";
}

TEST_F(SaveImageTest, LargeWidthClamped)
{
  const std::string path = tempPng("clamped");
  // Request a very large image; should be clamped to max dimension.
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 100000, 0, {});

  unsigned w = 0, h = 0;
  decodePngFile(path, w, h);
  EXPECT_LE(w, 16384u);
  EXPECT_LE(h, 16384u);
}

TEST_F(SaveImageTest, PinMarkersRendered)
{
  makeBTermAtEdge("in_pin", "metal1", 0, 40000, 200, 200, odb::dbIoType::INPUT);
  makeBTermAtEdge(
      "out_pin", "metal1", 99800, 60000, 200, 200, odb::dbIoType::OUTPUT);
  makeTileGen();

  const std::string path = tempPng("pin_markers");
  TileVisibility vis;
  vis.stdcells = false;
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 512, 0, vis);

  unsigned w = 0, h = 0;
  auto pixels = decodePngFile(path, w, h);
  EXPECT_TRUE(hasNonTransparentPixel(pixels));
}

TEST_F(SaveImageTest, MultipleLayersComposited)
{
  // Place instances to generate content on multiple layers.
  placeInst("BUF_X16", "buf2", 50000, 50000);
  makeTileGen();

  const std::string path = tempPng("multi_layer");
  tile_gen_->saveImage(path, odb::Rect(0, 0, 0, 0), 512, 0, {});

  unsigned w = 0, h = 0;
  auto pixels = decodePngFile(path, w, h);
  EXPECT_TRUE(hasNonTransparentPixel(pixels));
}

// ─── Composition order ───────────────────────────────────────────────────────

// saveImage must composite in the same z order Leaflet stacks the layers in on
// screen (display-controls.js addPseudoLayer), otherwise the saved PNG is not
// the view the user was looking at.  Before the fix every pseudo layer was
// appended after the tech layers, which put the manufacturing grid over the
// routing and the pin markers over the tech layers (PR #10806 review).
//
// Asserted on the layer list rather than on pixels: every layer here is
// semi-transparent, so a dot drawn over the routing still blends with it and no
// pixel test can tell the two orders apart reliably.
TEST_F(SaveImageTest, LayerCompositionOrderMatchesClientZIndex)
{
  const std::vector<std::string> tech_layers = {"metal1", "metal2"};
  TileVisibility vis;
  vis.pins = true;
  vis.mfg_grid = true;
  vis.access_points = true;
  vis.regions = true;
  vis.gcell_grid = true;
  vis.rudy = true;

  // zIndex on screen: _instances 0, _pins 1, _mfg_grid 2, tech layers 3.., then
  // _access_points 1000, _regions 1001, _gcell_grid 1002, _rudy 1003.
  const std::vector<std::string> expected = {"_instances",
                                             "_pins",
                                             "_mfg_grid",
                                             "metal1",
                                             "metal2",
                                             "_access_points",
                                             "_regions",
                                             "_gcell_grid",
                                             "_rudy"};
  EXPECT_EQ(TileGenerator::saveImageLayerOrder(vis, tech_layers), expected);
}

TEST_F(SaveImageTest, LayerCompositionOrderHonorsVisibility)
{
  const std::vector<std::string> tech_layers = {"metal1"};
  TileVisibility vis;
  vis.pins = false;
  // `regions` is the one overlay that defaults ON (Qt parity), so turn the
  // whole set off explicitly rather than relying on the defaults.
  vis.regions = false;
  vis.mfg_grid = false;
  vis.access_points = false;
  vis.gcell_grid = false;
  vis.rudy = false;

  EXPECT_EQ(TileGenerator::saveImageLayerOrder(vis, tech_layers),
            (std::vector<std::string>{"_instances", "metal1"}))
      << "hidden overlays must not be composited at all";
}

TEST_F(SaveImageTest, ParseFromJsonRudyOption)
{
  TileVisibility vis;
  EXPECT_FALSE(vis.rudy);
  auto json_obj = boost::json::parse("{\"rudy\":true}").as_object();
  vis.parseFromJson(json_obj);
  EXPECT_TRUE(vis.rudy);
}

class TestRUDYHeatMap : public gui::HeatMapDataSource
{
 public:
  explicit TestRUDYHeatMap(utl::Logger* logger)
      : gui::HeatMapDataSource(logger,
                               "Estimated Congestion (RUDY)",
                               "RUDY",
                               "RUDY")
  {
  }

  odb::Rect getBounds() const override
  {
    return getBlock() ? getBlock()->getDieArea()
                      : odb::Rect(0, 0, 100000, 100000);
  }

 protected:
  bool populateMap() override
  {
    addToMap(odb::Rect(20000, 20000, 80000, 80000), 10.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;
  }
};

TEST_F(SaveImageTest, RudyHeatmapRendersInSavedImage)
{
  auto logger = getLogger();
  gui::registerHeatMapSource(
      "Estimated Congestion (RUDY)", "RUDY", "RUDY", [logger]() {
        return std::make_shared<TestRUDYHeatMap>(logger);
      });

  const std::string path_no_rudy = tempPng("no_rudy");
  TileVisibility vis_off;
  vis_off.rudy = false;
  tile_gen_->saveImage(path_no_rudy, odb::Rect(0, 0, 0, 0), 512, 0, vis_off);

  const std::string path_rudy = tempPng("with_rudy");
  TileVisibility vis_on;
  vis_on.rudy = true;
  tile_gen_->saveImage(path_rudy, odb::Rect(0, 0, 0, 0), 512, 0, vis_on);

  ASSERT_TRUE(std::filesystem::exists(path_no_rudy));
  ASSERT_TRUE(std::filesystem::exists(path_rudy));

  unsigned w1 = 0, h1 = 0;
  auto pixels_no_rudy = decodePngFile(path_no_rudy, w1, h1);
  unsigned w2 = 0, h2 = 0;
  auto pixels_rudy = decodePngFile(path_rudy, w2, h2);

  EXPECT_EQ(w1, 512u);
  EXPECT_EQ(w2, 512u);
  EXPECT_GT(countNonTransparentPixels(pixels_rudy),
            countNonTransparentPixels(pixels_no_rudy))
      << "Enabling rudy heatmap should render additional heatmap pixels";
}

TEST_F(SaveImageTest, LayerCompositionOrderHonorsTechLayerVisibility)
{
  const std::vector<std::string> tech_layers = {"metal1", "metal2", "metal3"};
  TileVisibility vis;
  vis.has_visible_layers = true;
  vis.visible_layers = {"metal2"};
  vis.pins = false;
  vis.regions = false;
  vis.mfg_grid = false;
  vis.access_points = false;
  vis.gcell_grid = false;
  vis.rudy = false;

  EXPECT_EQ(TileGenerator::saveImageLayerOrder(vis, tech_layers),
            (std::vector<std::string>{"_instances", "metal2"}))
      << "tech layers hidden via visible_layers must not be composited";
}

TEST_F(SaveImageTest, HiddenTechLayerIsNotDrawn)
{
  placeInst("BUF_X16", "buf2", 50000, 50000);
  makeTileGen();
  const odb::Rect region = tile_gen_->getBounds();

  TileVisibility all;
  const auto with_all = tile_gen_->renderImageBuffer(region, 512, 0, all);
  TileVisibility none;
  none.has_visible_layers = true;  // visible_layers empty: hide every one
  const auto with_none = tile_gen_->renderImageBuffer(region, 512, 0, none);

  ASSERT_FALSE(with_all.empty());
  ASSERT_EQ(with_all.size(), with_none.size());
  EXPECT_LT(countNonTransparentPixels(with_none),
            countNonTransparentPixels(with_all))
      << "hiding all tech layers must remove their pixels";
}

// Tiles are rendered concurrently into disjoint output rectangles, so the
// result must not depend on the thread count.  This is the test that catches
// a shared-state race in the render path.
TEST_F(SaveImageTest, ThreadCountDoesNotChangeOutput)
{
  for (int i = 0; i < 40; ++i) {
    placeInst("BUF_X16", ("b" + std::to_string(i)).c_str(), 2000 * i, 3000 * i);
  }
  makeTileGen();
  const odb::Rect region = tile_gen_->getBounds();
  TileVisibility vis;

  tile_gen_->setThreadCount(1);
  const auto one = tile_gen_->renderImageBuffer(region, 1024, 0, vis);
  tile_gen_->setThreadCount(8);
  const auto eight = tile_gen_->renderImageBuffer(region, 1024, 0, vis);

  ASSERT_FALSE(one.empty());
  EXPECT_EQ(one, eight);
}

// Qt gates drawLabels on the Misc/"Labels" control, and its save_image goes
// through the same painter, so a saved image reproduces a view with labels
// hidden.  save_image -display_option {labels false} has to do the same here.
TEST_F(SaveImageTest, LabelsFollowTheVisibilityFlag)
{
  // Render the generator's own bounds: labels outside them fall off every
  // tile and would make this pass for the wrong reason.
  const odb::Rect region = tile_gen_->getBounds();
  ASSERT_GT(region.maxDXDY(), 0);
  const Color white{.r = 255, .g = 255, .b = 255, .a = 255};

  TileVisibility vis;
  const std::vector<unsigned char> before
      = tile_gen_->renderImagePng(region, 512, 0, vis);
  ASSERT_FALSE(before.empty());

  const odb::Point centre((region.xMin() + region.xMax()) / 2,
                          (region.yMin() + region.yMax()) / 2);
  ASSERT_FALSE(
      tile_gen_->addLabel(centre, "PROBE", white, 24, "center", "L").empty());

  const std::vector<unsigned char> shown
      = tile_gen_->renderImagePng(region, 512, 0, vis);
  vis.labels = false;
  const std::vector<unsigned char> hidden
      = tile_gen_->renderImagePng(region, 512, 0, vis);

  // Drawn when on...
  EXPECT_NE(shown, before) << "label did not change the image";
  // ...and with it off the image is the one from before the label existed.
  EXPECT_EQ(hidden, before) << "label still drawn with labels off";
}

}  // namespace
}  // namespace web