I want to achieve alpha masking with two complex shapes with skia.

Shape 1 : Complex draw operating , with multiple canvas save and restore. In the example text(“Skia”).

Shape 2 : Mask layer, complex drawing operation with multiple canvas save and restore. In example it’s a rectangle.

Deseried Result: Only the intersection is drawn but only with Shape 1 pixels.

I am not able to achieve this result without involving bitmap operations.
Is there any effective solution to achieve it without bitmap drawing (taking snapshot from canvas surface) ?

void draw(SkCanvas *canvas)
{
    SkFont font1;
    font1.setSize(100);
    font1.setEdging(SkFont::Edging::kAntiAlias);

    // clear canvas, transparent
    canvas->clear(SK_ColorTRANSPARENT);
    sk_sp<SkTextBlob> blob1 = SkTextBlob::MakeFromString("Skia", font1);

    SkPaint paint1;

    paint1.setAntiAlias(true);
    paint1.setColor(SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4));

    // Text draw
    canvas->drawTextBlob(blob1.get(), 0, 100, paint1);

    SkPaint p;
    p.setColor(SK_ColorRED);
    p.setAntiAlias(true);
    canvas->saveLayer(nullptr, nullptr);
    // canvas bounds, 400x400
    canvas->drawRect(SkRect::MakeXYWH(0, 0, 400, 400), p);
    p.setBlendMode(SkBlendMode::kClear);

    // Alpha drawing is complex operation with multiple save and restore
    // There can be multiple draw commands
    canvas->drawRect(SkRect::MakeXYWH(0, 0, 400, 50), p);
    canvas->restore();
}

Example: 400×400 canvas
enter image description here