1 2 /* 3 * Copyright 2013 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 #include "SampleCode.h" 9 #include "SkCanvas.h" 10 #include "SkDevice.h" 11 #include "SkPaint.h" 12 #include "SkRandom.h" 13 #include "SkShader.h" 14 #include "SkView.h" 15 16 /** 17 * Animated sample used to develop batched rect implementation in GrInOrderDrawBuffer. 18 */ 19 class ManyRectsView : public SampleView { 20 private: 21 enum { 22 N = 1000, 23 }; 24 25 public: 26 ManyRectsView() {} 27 28 protected: 29 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { 30 if (SampleCode::TitleQ(*evt)) { 31 SampleCode::TitleR(evt, "ManyRects"); 32 return true; 33 } 34 return this->INHERITED::onQuery(evt); 35 } 36 37 virtual void onDrawContent(SkCanvas* canvas) { 38 SkISize dsize = canvas->getDeviceSize(); 39 canvas->clear(0xFFF0E0F0); 40 41 for (int i = 0; i < N; ++i) { 42 SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)), 43 SkIntToScalar(fRandom.nextRangeU(10, 100))); 44 int x = fRandom.nextRangeU(0, dsize.fWidth); 45 int y = fRandom.nextRangeU(0, dsize.fHeight); 46 canvas->save(); 47 48 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 49 // Rotation messes up the GPU batching because of the clip below. We don't notice 50 // that the rect is inside the clip so the clip changes interrupt batching. 51 if (false) { 52 SkMatrix rotate; 53 rotate.setRotate(fRandom.nextUScalar1() * 360, 54 SkIntToScalar(x) + SkScalarHalf(rect.fRight), 55 SkIntToScalar(y) + SkScalarHalf(rect.fBottom)); 56 canvas->concat(rotate); 57 } 58 SkRect clipRect = rect; 59 // This clip will always contain the entire rect. It's here to give the GPU batching 60 // code a little more challenge. 61 clipRect.outset(10, 10); 62 canvas->clipRect(clipRect); 63 SkPaint paint; 64 paint.setColor(fRandom.nextU()); 65 canvas->drawRect(rect, paint); 66 canvas->restore(); 67 } 68 this->inval(NULL); 69 } 70 71 private: 72 SkRandom fRandom; 73 typedef SampleView INHERITED; 74 }; 75 76 ////////////////////////////////////////////////////////////////////////////// 77 78 static SkView* MyFactory() { return new ManyRectsView; } 79 static SkViewRegister reg(MyFactory); 80