Project

General

Profile

Bug #8305 » test_wrasterimage_gm_race_20210401a.C

Bruce Toll, 04/05/2021 03:15 PM

 
1
#include <Wt/WApplication.h>
2
#include <Wt/WBrush.h>
3
#include <Wt/WColor.h>
4
#include <Wt/WContainerWidget.h>
5
#include <Wt/WEnvironment.h>
6
#include <Wt/WImage.h>
7
#include <Wt/WPainter.h>
8
#include <Wt/WPen.h>
9
#include <Wt/WRasterImage.h>
10
#include <Wt/WRectF.h>
11
#include <Wt/WTable.h>
12

    
13
using namespace Wt;
14

    
15
static constexpr int TABLE_ROWS = 16;
16
static constexpr int TABLE_COLS = 40;
17
static constexpr int ARC_BOX_SIDE_LEN = 40;
18

    
19
class ImageResource;
20

    
21
class PaintedImage : public WImage
22
{
23
public:
24
  PaintedImage(int arc_box_side_len, double span_angle);
25
  ~PaintedImage() { }
26

    
27
private:
28
  double span_angle_;
29
  std::shared_ptr<ImageResource> resource_;
30

    
31
friend ImageResource;
32
};
33

    
34
class ImageResource : public WResource
35
{
36
public:
37
  ImageResource(PaintedImage *img)
38
    : WResource(),
39
    img_(img)
40
  { }
41

    
42
  virtual ~ImageResource()
43
  {
44
    beingDeleted();
45
  }
46

    
47
  virtual void handleRequest(const Http::Request& request,
48
			     Http::Response& response) {
49
    WRasterImage device("png", img_->width(), img_->height());
50
    paint(&device, img_);
51

    
52
    device.handleRequest(request, response);
53
  }
54

    
55
  void paint(WPaintDevice *paintDevice, PaintedImage *img)
56
  {
57
    WPainter p(paintDevice);
58
    p.save();
59
    p.setBrush(WBrush(StandardColor::Blue));
60
    p.setPen(WPen(PenStyle::None));
61
    auto rect = WRectF(0.0, 0.0, img_->width().toPixels(), img_->height().toPixels());
62
    p.drawArc(rect, img_->span_angle_ * 16, 180 * 16);
63
    p.setBrush(WBrush(StandardColor::Yellow));
64
    p.drawArc(rect, img_->span_angle_ * 16 + 180 * 16, (360 - 180) * 16);
65
    p.restore();
66
  }
67

    
68
private:
69
  PaintedImage *img_;
70
};
71

    
72
PaintedImage::PaintedImage(int arc_box_side_len, double span_angle) :
73
    Wt::WImage(), span_angle_(span_angle)
74
{
75
  resize(arc_box_side_len, arc_box_side_len);
76
  resource_ = std::make_shared<ImageResource>(this);
77
  setImageLink(WLink(resource_));
78
}
79

    
80
class TestApp : public WApplication {
81
public:
82
  TestApp(const WEnvironment& env);
83
};
84

    
85
TestApp::TestApp(const WEnvironment& env) : WApplication(env)
86
{
87
  setTitle("Test WRasterImage-gm race condition");
88

    
89
  auto table = root()->addNew<WTable>();
90
  for (int r = 0; r < TABLE_ROWS; ++r) {
91
    for (int c = 0; c < TABLE_COLS; ++c) {
92
      double span_angle = 360.0 * ((double) r / TABLE_ROWS + (double) c / TABLE_ROWS / TABLE_COLS);
93
      table->elementAt(r, c)->addNew<PaintedImage>(ARC_BOX_SIDE_LEN, span_angle);
94
    }
95
  }
96
}
97

    
98
int main(int argc, char **argv)
99
{
100
  return WRun(argc, argv, [&](const WEnvironment& env) {return std::make_unique<TestApp>(env);});
101
}
(1-1/2)