1 2 /* 3 * Copyright 2012 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 #include "gm/gm.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkPath.h" 13 #include "include/core/SkPicture.h" 14 #include "include/core/SkPictureRecorder.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkRefCnt.h" 17 #include "include/core/SkScalar.h" 18 #include "include/core/SkSize.h" 19 #include "include/core/SkString.h" 20 21 namespace skiagm { 22 23 class DistantClipGM : public GM { onShortName()24 SkString onShortName() override { return SkString("distantclip"); } 25 onISize()26 SkISize onISize() override { return {100, 100}; } 27 onDraw(SkCanvas * canvas)28 void onDraw(SkCanvas* canvas) override { 29 constexpr SkScalar kOffset = 35000.0f; 30 constexpr SkScalar kExtents = 1000.0f; 31 32 SkPictureRecorder recorder; 33 // We record a picture of huge vertical extents in which we clear the canvas to red, create 34 // a 'extents' by 'extents' round rect clip at a vertical offset of 'offset', then draw 35 // green into that. 36 SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents, nullptr, 0); 37 rec->drawColor(SK_ColorRED); 38 rec->save(); 39 SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents, 2 * kExtents); 40 SkPath p; 41 p.addRoundRect(r, 5, 5); 42 rec->clipPath(p, true); 43 rec->drawColor(SK_ColorGREEN); 44 rec->restore(); 45 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); 46 47 // Next we play that picture into another picture of the same size. 48 pict->playback(recorder.beginRecording(pict->cullRect().width(), 49 pict->cullRect().height(), 50 nullptr, 0)); 51 sk_sp<SkPicture> pict2(recorder.finishRecordingAsPicture()); 52 53 // Finally we play the part of that second picture that should be green into the canvas. 54 canvas->save(); 55 canvas->translate(kExtents / 2, -(kOffset - kExtents / 2)); 56 pict2->playback(canvas); 57 canvas->restore(); 58 59 // If the image is red, we erroneously decided the clipPath was empty and didn't record 60 // the green drawColor, if it's green we're all good. 61 } 62 }; 63 64 /////////////////////////////////////////////////////////////////////////////// 65 66 DEF_GM( return new DistantClipGM; ) 67 68 } 69