clearing the clip region

1,351 views
Skip to first unread message

sha...@infograph.com

unread,
Jun 11, 2016, 5:03:48 PM6/11/16
to skia-discuss
I have been combing through the canvas doc and I can't find a command to clear the current clip region.

I tried doing a replace op with an empty path and an empty region, but those didn't work.

It is probably right under my nose and I am not seeing it.

Mike Klein

unread,
Jun 11, 2016, 6:50:55 PM6/11/16
to skia-discuss
Generally the best approach is to structure the clip (and the transform matrix) hierarchically with SkCanvas::save() and SkCanvas::restore().  Was that the thing under your nose, or is there some sort of special circumstance here that needs something fancier?

--
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 post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

sha...@infograph.com

unread,
Jun 11, 2016, 7:34:11 PM6/11/16
to skia-discuss
I am already taking advantage of the save and restore.  My data has the same concept.  push() and pop() clip regions.  

But I need to handle the scenario where there could be 3 clip regions saved to the stack and along comes an instruction to clear the clip region.

For the moment, I have it working by treating the command to clear as a command to replace using a path containing the shape of the view.  Which works, but probably isn't as fast as being truly clear.

Mike Klein

unread,
Jun 13, 2016, 9:29:41 AM6/13/16
to skia-discuss
Can you use SkCanvas::restoreToCount() (and maybe SkCanvas::getSaveCount()) to pop those 3 clip regions off?

sha...@infograph.com

unread,
Jun 13, 2016, 11:59:54 AM6/13/16
to skia-discuss
The problem is that I wouldn't want to lose the saved clips.  The situation hasn't called for pop yet.  In my file format, clear is a legal state.  A clear clip region can even be pushed onto the stack.

Like I said, I have a work-around implemented where I treat a clear instruction as a replace-with-device-rect set of instructions.

It works, but I suspect that the performance during those periods -- where the clip region is supposed to be clear -- are not as fast as actually having it be clear.  There is probably an optimization in there that says "don't bother with clip testing, if the current clip is clear".

Mike Klein

unread,
Jun 13, 2016, 12:28:25 PM6/13/16
to skia-discuss
That sounds right to me.  Skia does not offer a clear-clip capability, but there are definitely a couple of ways to emulate it, ranging from simple to mildly complex depending on what sort of transform matrices you are using.  I've hacked up a little fiddle as demo: https://fiddle.skia.org/c/7c8daea2ed923555b0d2718d9a947762 .  Is the green box the sort of thing you're doing?

I agree you're also more likely to run into performance and correctness issues with this approach.  It's just not a common way to use Skia, so it gets less testing and optimization attention.  The best supported use cases for clipping are the simplest, using save() and restore() to manage intersected clips.

sha...@infograph.com

unread,
Jun 13, 2016, 2:24:50 PM6/13/16
to skia-discuss
Yes.  Except I don't use Skia to transform anything Skia's transformation matrix stays at identity.  I transform all my stuff.  So, I only need to be concerned with the state of the clip stack.  

Mike Klein

unread,
Jun 13, 2016, 2:36:59 PM6/13/16
to skia-discuss
I suspect that if you both need to preserve the clip stack and want to temporarily set it to an open clip, you're probably already doing the right thing.  The code should be a lot simpler if you don't use Skia's transformation matrix, something like this?  https://fiddle.skia.org/c/d472869c765d6af4ef2a0607ab521564

sha...@infograph.com

unread,
Jun 13, 2016, 2:48:33 PM6/13/16
to skia-discuss
Yes, exactly.  I just miss out on the speed improvement of a truly empty clip region.
Reply all
Reply to author
Forward
0 new messages