Q:

Is there any way to save the path and restore it in Cairo?

I have two graphs of drawing signals on a gtkmm application.
The problem comes when I have to paint a graph with many points (around 300-350k) and lines to the following points since it slows down a lot to paint all the points each iteration.
bool DrawArea::on_draw(const Cairo::RefPtr<Cairo::Context>& c) 
{  
    cairo_t* cr = c->cobj();

    //xSignal.size() = ySignal.size() = 350000
    for (int j = 0; j < xSignal.size() - 1; ++j)
    {
        cairo_move_to(cr, xSignal[j], ySignal[j]);
        cairo_line_to(cr, xSignal[j + 1], ySignal[j + 1]);
    }
    cairo_stroke(cr);

    return true;
}

I know that exist a cairo_stroke_preserve but i think is not valid for me because when I switch between graphs, it disappears.
I've been researching about save the path and restore it on the Cairo documentation but i don´t see anything. In 2007, a user from Cairo suggested in the documentation 'to do' the same thing but apparently it has not been done.
Any suggestion?

A:

It's not necessary that you draw everything in on_draw. What I understand from your post is that you have a real-time waveform drawing application where samples are available at fixed periods (every few milliseconds I presume). There are three approaches you can follow.
Approach 1
This is good particularly when you have limited memory and do not care about retaining the plot if window is resized or uncovered. Following could be the function that receives samples (one by one).
NOTE: Variables prefixed with m_ are class members.
void DrawingArea::PlotSample(int nSample)
{
  Cairo::RefPtr <Cairo::Context> refCairoContext;
  double dNewY;

  //Get window's cairo context
  refCairoContext = get_window()->create_cairo_context();

  //TODO Scale and transform sample to new Y coordinate
  dNewY = nSample;

  //Clear area for new waveform segment
  {
    refCairoContext->rectangle(m_dPreviousX
                               + 1,
                               m_dPreviousY,
                               ERASER_WIDTH,
                               get_allocated_height()); //See note below on m_dPreviousX + 1
    refCairoContext->set_source_rgb(0,
                                    0,
                                    0);
    refCairoContext->fill();
  }

  //Setup Cairo context for the trace
  {
    refCairoContext->set_source_rgb(1,
                                    1,
                                    1);
    refCairoContext->set_antialias(Cairo::ANTIALIAS_SUBPIXEL); //This is up to you
    refCairoContext->set_line_width(1); //It's 2 by default and better that way with anti-aliasing
  }

  //Add sub-path and stroke
  refCairoContext->move_to(m_dPreviousX,
                           m_dPreviousY);
  m_dPreviousX += m_dXStep;
  refCairoContext->line_to(m_dPreviousX,
                           dNewY);
  refCairoContext->stroke();

  //Update coordinates
  if (m_dPreviousX
      >= get_allocated_width())
  {
    m_dPreviousX = 0;
  }
  m_dPreviousY = dNewY;
}

While clearing area the X coordinate has to be offset by 1 because otherwise the 'eraser' will clear of the anti-aliasing on the last coulmn and your trace will have jagged edges. It may need to be more than 1 depending on your line thickness.
Like I said before, with this method your trace will get cleared if the widget is resized or 'revealed'.
Approach 2
Even here the sample are plotted the same way as before. Only difference is that each sample received is pushed directly into a buffer. When the window is resized or 'reveled' the widget's on_draw is called and there you can plot all the samples one time. Of course you'll need some memory (quite a lot if you have 350K samples in queue) but the trace stays on screen no matter what.
Approach 3
This one also takes up a little bit of memory (probably much more depending on the size of you widget), and uses an off-screen buffer. Here instead of storing samples we store the rendered result. Override the widgets on_map method and on_size_allocate to create an offsceen buffer.
void DrawingArea::CreateOffscreenBuffer(void)
{
  Glib::RefPtr <Gdk::Window> refWindow = get_window();
  Gtk::Allocation oAllocation = get_allocation();

  if (refWindow)
  {
    Cairo::RefPtr <Cairo::Context> refCairoContext;

    m_refOffscreenSurface =
      refWindow->create_similar_surface(Cairo::CONTENT_COLOR,
                                        oAllocation.get_width(),
                                        oAllocation.get_height());

    refCairoContext = Cairo::Context::create(m_refOffscreenSurface);

    //TODO paint the background (grids  may be?)
  }
}

Now when you receive samples, instead of drawing into the window directly draw into the off-screen surface. Then block copy the off screen surface by setting this surface as your window's cairo context's source and then draw a rectangle to draw the newly plotted sample. Also in your widget's on_draw just set this surface as the source of widget's cairo context and do a Cairo::Context::paint(). This approach is particularly useful if your widget probably doesn't get resized and the advantage is that the blitting (where you transfer contents of one surface to the other) is way faster than plotting individual line segments.

