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