1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #include "Sample.h" 8 #include "SkCanvas.h" 9 #include "SkPaint.h" 10 #include "SkRandom.h" 11 #include "SkShader.h" 12 13 /** 14 * Animated sample used to develop a predecessor of GrDrawOp combining. 15 */ 16 class ManyRectsView : public Sample { 17 private: 18 enum { 19 N = 1000, 20 }; 21 22 public: ManyRectsView()23 ManyRectsView() {} 24 25 protected: onQuery(Sample::Event * evt)26 bool onQuery(Sample::Event* evt) override { 27 if (Sample::TitleQ(*evt)) { 28 Sample::TitleR(evt, "ManyRects"); 29 return true; 30 } 31 return this->INHERITED::onQuery(evt); 32 } 33 onDrawContent(SkCanvas * canvas)34 void onDrawContent(SkCanvas* canvas) override { 35 SkISize dsize = canvas->getBaseLayerSize(); 36 canvas->clear(0xFFF0E0F0); 37 38 for (int i = 0; i < N; ++i) { 39 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)), 40 SkIntToScalar(fRandom.nextRangeU(10, 100))); 41 int x = fRandom.nextRangeU(0, dsize.fWidth); 42 int y = fRandom.nextRangeU(0, dsize.fHeight); 43 canvas->save(); 44 45 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 46 // Uncomment to test rotated rect draw combining. 47 if (false) { 48 SkMatrix rotate; 49 rotate.setRotate(fRandom.nextUScalar1() * 360, 50 SkIntToScalar(x) + SkScalarHalf(rect.fRight), 51 SkIntToScalar(y) + SkScalarHalf(rect.fBottom)); 52 canvas->concat(rotate); 53 } 54 SkRect clipRect = rect; 55 // This clip will always contain the entire rect. It's here to give the GPU op combining 56 // code a little more challenge. 57 clipRect.outset(10, 10); 58 canvas->clipRect(clipRect); 59 SkPaint paint; 60 paint.setColor(fRandom.nextU()); 61 canvas->drawRect(rect, paint); 62 canvas->restore(); 63 } 64 } 65 66 private: 67 SkRandom fRandom; 68 typedef Sample INHERITED; 69 }; 70 71 ////////////////////////////////////////////////////////////////////////////// 72 73 DEF_SAMPLE( return new ManyRectsView(); ) 74