Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2011 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 
      8 #include "Sample.h"
      9 #include "SkAnimTimer.h"
     10 #include "SkBitmap.h"
     11 #include "SkCanvas.h"
     12 #include "SkFont.h"
     13 #include "SkGradientShader.h"
     14 #include "SkGraphics.h"
     15 #include "SkPath.h"
     16 #include "SkRegion.h"
     17 #include "SkShader.h"
     18 #include "SkUTF.h"
     19 #include "SkColorPriv.h"
     20 #include "SkColorFilter.h"
     21 #include "SkTime.h"
     22 #include "SkTypeface.h"
     23 
     24 #include "SkOSFile.h"
     25 #include "SkStream.h"
     26 
     27 #define INT_SIZE        64
     28 #define SCALAR_SIZE     SkIntToScalar(INT_SIZE)
     29 
     30 static void make_bitmap(SkBitmap* bitmap) {
     31     bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
     32     SkCanvas canvas(*bitmap);
     33 
     34     canvas.drawColor(SK_ColorRED);
     35     SkPaint paint;
     36     paint.setAntiAlias(true);
     37     const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
     38     const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
     39     paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
     40                                                    SkShader::kClamp_TileMode));
     41     canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
     42 }
     43 
     44 static SkPoint unit_vec(int degrees) {
     45     SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
     46     SkScalar s, c;
     47     s = SkScalarSinCos(rad, &c);
     48     return SkPoint::Make(c, s);
     49 }
     50 
     51 static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
     52     *value += *delta;
     53     if (*value < min) {
     54         *value = min;
     55         *delta = - *delta;
     56     } else if (*value > max) {
     57         *value = max;
     58         *delta = - *delta;
     59     }
     60 }
     61 
     62 static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
     63     bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
     64     bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
     65 }
     66 
     67 class BitmapRectView : public Sample {
     68     SkPoint fSrcPts[2];
     69     SkPoint fSrcVec[2];
     70     SkRect  fSrcLimit;
     71     SkRect  fDstR[2];
     72 
     73     void bounce() {
     74         bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
     75         bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
     76     }
     77 
     78     void resetBounce() {
     79         fSrcPts[0].set(0, 0);
     80         fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
     81 
     82         fSrcVec[0] = unit_vec(30);
     83         fSrcVec[1] = unit_vec(107);
     84     }
     85 
     86 public:
     87     BitmapRectView() {
     88         this->setBGColor(SK_ColorGRAY);
     89 
     90         this->resetBounce();
     91 
     92         fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
     93                       SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
     94 
     95         fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
     96                                        SkIntToScalar(250), SkIntToScalar(300));
     97         fDstR[1] = fDstR[0];
     98         fDstR[1].offset(fDstR[0].width() * 5/4, 0);
     99 
    100         fSrcPts[0].set(32, 32);
    101         fSrcPts[1].set(90, 90);
    102     }
    103 
    104 protected:
    105     bool onQuery(Sample::Event* evt) override {
    106         if (Sample::TitleQ(*evt)) {
    107             Sample::TitleR(evt, "BitmapRect");
    108             return true;
    109         }
    110         return this->INHERITED::onQuery(evt);
    111     }
    112 
    113     void onDrawContent(SkCanvas* canvas) override {
    114         SkRect srcR;
    115         srcR.set(fSrcPts[0], fSrcPts[1]);
    116         srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
    117         srcR.offset(-srcR.width()/2, -srcR.height()/2);
    118 
    119         SkPaint paint;
    120         paint.setStyle(SkPaint::kStroke_Style);
    121         paint.setColor(SK_ColorYELLOW);
    122 
    123         SkBitmap bitmap;
    124         make_bitmap(&bitmap);
    125 
    126         canvas->translate(20, 20);
    127 
    128         canvas->drawBitmap(bitmap, 0, 0, &paint);
    129         canvas->drawRect(srcR, paint);
    130 
    131         for (int i = 0; i < 2; ++i) {
    132             paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
    133             canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint,
    134                                    SkCanvas::kStrict_SrcRectConstraint);
    135             canvas->drawRect(fDstR[i], paint);
    136         }
    137     }
    138 
    139     bool onAnimate(const SkAnimTimer& timer) override {
    140         if (timer.isStopped()) {
    141             this->resetBounce();
    142         } else if (timer.isRunning()) {
    143             this->bounce();
    144         }
    145         return true;
    146     }
    147 
    148 private:
    149     typedef Sample INHERITED;
    150 };
    151 
    152 //////////////////////////////////////////////////////////////////////////////
    153 
    154 static void make_big_bitmap(SkBitmap* bm) {
    155     static const char gText[] =
    156         "We the people, in order to form a more perfect union, establish justice,"
    157         " ensure domestic tranquility, provide for the common defense, promote the"
    158         " general welfare and ensure the blessings of liberty to ourselves and our"
    159         " posterity, do ordain and establish this constitution for the United"
    160         " States of America.";
    161 
    162     const int BIG_H = 120;
    163 
    164     SkFont font;
    165     font.setSize(SkIntToScalar(BIG_H));
    166 
    167     const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), kUTF8_SkTextEncoding));
    168 
    169     bm->allocN32Pixels(BIG_W, BIG_H);
    170     bm->eraseColor(SK_ColorWHITE);
    171 
    172     SkCanvas canvas(*bm);
    173 
    174     canvas.drawSimpleText(gText, strlen(gText), kUTF8_SkTextEncoding, 0, font.getSize()*4/5, font, SkPaint());
    175 }
    176 
    177 class BitmapRectView2 : public Sample {
    178     SkBitmap fBitmap;
    179 
    180     SkRect  fSrcR;
    181     SkRect  fLimitR;
    182     SkScalar fDX;
    183     SkRect  fDstR[2];
    184 
    185     void bounceMe() {
    186         SkScalar width = fSrcR.width();
    187         bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
    188         fSrcR.fRight = fSrcR.fLeft + width;
    189     }
    190 
    191     void resetBounce() {
    192         fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
    193         fDX = SK_Scalar1;
    194     }
    195 
    196 public:
    197     BitmapRectView2() { }
    198 
    199 protected:
    200     bool onQuery(Sample::Event* evt) override {
    201         if (Sample::TitleQ(*evt)) {
    202             Sample::TitleR(evt, "BigBitmapRect");
    203             return true;
    204         }
    205         return this->INHERITED::onQuery(evt);
    206     }
    207 
    208     void onOnceBeforeDraw() override {
    209         make_big_bitmap(&fBitmap);
    210 
    211         this->setBGColor(SK_ColorGRAY);
    212 
    213         this->resetBounce();
    214 
    215         fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
    216 
    217         fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
    218         fDstR[1] = fDstR[0];
    219         fDstR[1].offset(0, fDstR[0].height() * 5/4);
    220     }
    221 
    222     void onDrawContent(SkCanvas* canvas) override {
    223         SkPaint paint;
    224         paint.setStyle(SkPaint::kStroke_Style);
    225         paint.setColor(SK_ColorYELLOW);
    226 
    227         for (int i = 0; i < 2; ++i) {
    228             paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
    229             canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
    230                                    SkCanvas::kStrict_SrcRectConstraint);
    231             canvas->drawRect(fDstR[i], paint);
    232         }
    233     }
    234 
    235     bool onAnimate(const SkAnimTimer& timer) override {
    236         if (timer.isStopped()) {
    237             this->resetBounce();
    238         } else if (timer.isRunning()) {
    239             this->bounceMe();
    240         }
    241         return true;
    242     }
    243 
    244 private:
    245     typedef Sample INHERITED;
    246 };
    247 
    248 //////////////////////////////////////////////////////////////////////////////
    249 
    250 DEF_SAMPLE( return new BitmapRectView(); )
    251 DEF_SAMPLE( return new BitmapRectView2(); )
    252