Home | History | Annotate | Download | only in samplecode
      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 "SampleCode.h"
      8 #include "SkCanvas.h"
      9 #include "SkPaint.h"
     10 #include "SkRandom.h"
     11 #include "SkShader.h"
     12 #include "SkView.h"
     13 
     14 /**
     15  * Animated sample used to develop a predecessor of GrDrawOp combining.
     16  */
     17 class ManyRectsView : public SampleView {
     18 private:
     19     enum {
     20         N = 1000,
     21     };
     22 
     23 public:
     24     ManyRectsView() {}
     25 
     26 protected:
     27     bool onQuery(SkEvent* evt) override {
     28         if (SampleCode::TitleQ(*evt)) {
     29             SampleCode::TitleR(evt, "ManyRects");
     30             return true;
     31         }
     32         return this->INHERITED::onQuery(evt);
     33     }
     34 
     35     void onDrawContent(SkCanvas* canvas) override {
     36         SkISize dsize = canvas->getBaseLayerSize();
     37         canvas->clear(0xFFF0E0F0);
     38 
     39         for (int i = 0; i < N; ++i) {
     40             SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
     41                                          SkIntToScalar(fRandom.nextRangeU(10, 100)));
     42             int x = fRandom.nextRangeU(0, dsize.fWidth);
     43             int y = fRandom.nextRangeU(0, dsize.fHeight);
     44             canvas->save();
     45 
     46             canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     47             // Uncomment to test rotated rect draw combining.
     48             if (false) {
     49                 SkMatrix rotate;
     50                 rotate.setRotate(fRandom.nextUScalar1() * 360,
     51                                  SkIntToScalar(x) + SkScalarHalf(rect.fRight),
     52                                  SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
     53                 canvas->concat(rotate);
     54             }
     55             SkRect clipRect = rect;
     56             // This clip will always contain the entire rect. It's here to give the GPU op combining
     57             // code a little more challenge.
     58             clipRect.outset(10, 10);
     59             canvas->clipRect(clipRect);
     60             SkPaint paint;
     61             paint.setColor(fRandom.nextU());
     62             canvas->drawRect(rect, paint);
     63             canvas->restore();
     64         }
     65         this->inval(nullptr);
     66     }
     67 
     68 private:
     69     SkRandom fRandom;
     70     typedef SampleView INHERITED;
     71 };
     72 
     73 //////////////////////////////////////////////////////////////////////////////
     74 
     75 static SkView* MyFactory() { return new ManyRectsView; }
     76 static SkViewRegister reg(MyFactory);
     77