Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2014 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 "DecodeFile.h"
      9 #include "Resources.h"
     10 #include "Sample.h"
     11 #include "SkBlurMaskFilter.h"
     12 #include "SkCanvas.h"
     13 #include "SkColorPriv.h"
     14 #include "SkFont.h"
     15 #include "SkPath.h"
     16 #include "SkRandom.h"
     17 #include "SkStream.h"
     18 #include "SkTime.h"
     19 #include "SkClipOpPriv.h"
     20 
     21 // Intended to exercise pixel snapping observed with scaled images (and
     22 // with non-scaled images, but for a different reason):  Bug 1145
     23 
     24 class IdentityScaleView : public Sample {
     25 public:
     26     IdentityScaleView(const char imageFilename[]) {
     27         if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
     28             fBM.allocN32Pixels(1, 1);
     29             *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
     30         }
     31     }
     32 
     33 protected:
     34     SkBitmap fBM;
     35 
     36     bool onQuery(Sample::Event* evt) override {
     37         if (Sample::TitleQ(*evt)) {
     38             Sample::TitleR(evt, "IdentityScale");
     39             return true;
     40         }
     41         return this->INHERITED::onQuery(evt);
     42     }
     43 
     44     void onDrawContent(SkCanvas* canvas) override {
     45 
     46         SkFont font(nullptr, 48);
     47         SkPaint paint;
     48 
     49         paint.setAntiAlias(true);
     50         paint.setFilterQuality(kHigh_SkFilterQuality);
     51 
     52         SkTime::DateTime time;
     53         SkTime::GetDateTime(&time);
     54 
     55         bool use_scale = (time.fSecond % 2 == 1);
     56         const char *text;
     57 
     58         canvas->save();
     59         if (use_scale) {
     60           text = "Scaled = 1";
     61         } else {
     62 
     63           SkRect r = { 100, 100, 356, 356 };
     64           SkPath clipPath;
     65           clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5));
     66           canvas->clipPath(clipPath, kIntersect_SkClipOp, true);
     67           text = "Scaled = 0";
     68         }
     69         canvas->drawBitmap( fBM, 100, 100, &paint );
     70         canvas->restore();
     71         canvas->drawString(text, 100, 400, font, paint);
     72     }
     73 
     74 private:
     75     typedef Sample INHERITED;
     76 };
     77 
     78 //////////////////////////////////////////////////////////////////////////////
     79 
     80 DEF_SAMPLE( return new IdentityScaleView("images/mandrill_256.png"); )
     81