What is the optimal way to move pixels within a surface (smooth scrolling)

103 views
Skip to first unread message

Alexandre Bique

unread,
Feb 13, 2024, 8:38:30 AM2/13/24
to skia-discuss
Hi,

In order to implement a smooth scrolling, I wonder what is the fastest way with Skia to move a rectangle by an (x, y) offset?

The following code works:

      auto image = _surface->makeImageSnapshot();
      auto canvas = _surface->getCanvas();

      auto src = SkRect::MakeIWH(width, height);
      auto dst = SkRect::MakeIWH(width, height);

      if (deltaX < 0)
         src.offset(SkPoint::Make(-deltaX, 0));

      if (deltaY < 0)
         src.offset(SkPoint::Make(0, -deltaY));

      if (deltaX > 0)
         dst.offset(deltaX, 0);

      if (deltaY > 0)
         dst.offset(0, deltaY);

      canvas->drawImageRect(
          image, src, dst, SkSamplingOptions {}, nullptr, SkCanvas::kStrict_SrcRectConstraint);

While this one results in artifacts when the delta is positive:

      /* doesn't work at all */
      auto canvas = _surface->getCanvas();
      _surface->draw(canvas, deltaX, deltaY);

Thanks,
Alex

K. Moon

unread,
Feb 16, 2024, 3:29:18 AM2/16/24
to skia-d...@googlegroups.com
I'm not surprised the second doesn't work well, as you're trying to draw the surface into itself. This is like trying to do a memcpy() when you needed a memmove().

In general, the optimal way to move pixels is not to move them at all. :-) Telling the GPU to draw the same texture at a slightly different screen location is generally the fastest way to do scrolling.

If you can restructure your code to generate an image from a separate, off-screen canvas, and then draw that repeatedly at different offsets into a device-backed canvas, that's probably the best you can do without getting into lower-level graphics code. I've personally found the performance of this approach to be more than adequate.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/a0c15b33-691f-44ae-95af-526e0bab3ef1n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages