SampleIdentityScale.cpp revision c1f7774e8d327e3c98b4094c9c01d26e27013f71
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 "gm.h"
10
11#include "Resources.h"
12#include "SampleCode.h"
13#include "SkBlurMaskFilter.h"
14#include "SkCanvas.h"
15#include "SkColorPriv.h"
16#include "SkPath.h"
17#include "SkRandom.h"
18#include "SkStream.h"
19#include "SkTime.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
24class IdentityScaleView : public SampleView {
25public:
26    IdentityScaleView(const char imageFilename[]) {
27      SkString resourcePath = GetResourcePath(imageFilename);
28      if (!decode_file(resourcePath.c_str(), &fBM)) {
29          fBM.allocN32Pixels(1, 1);
30          *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
31      }
32    }
33
34protected:
35    SkBitmap fBM;
36
37    // overrides from SkEventSink
38    bool onQuery(SkEvent* evt) override {
39        if (SampleCode::TitleQ(*evt)) {
40            SampleCode::TitleR(evt, "IdentityScale");
41            return true;
42        }
43        return this->INHERITED::onQuery(evt);
44    }
45
46    void onDrawContent(SkCanvas* canvas) override {
47
48        SkPaint paint;
49
50        paint.setAntiAlias(true);
51        paint.setTextSize(48);
52        paint.setFilterQuality(kHigh_SkFilterQuality);
53
54        SkTime::DateTime time;
55        SkTime::GetDateTime(&time);
56
57        bool use_scale = (time.fSecond % 2 == 1);
58        const char *text;
59
60        canvas->save();
61        if (use_scale) {
62          text = "Scaled = 1";
63        } else {
64
65          SkRect r = { 100, 100, 356, 356 };
66          SkPath clipPath;
67          clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5));
68          canvas->clipPath(clipPath, kIntersect_SkClipOp, true);
69          text = "Scaled = 0";
70        }
71        canvas->drawBitmap( fBM, 100, 100, &paint );
72        canvas->restore();
73        canvas->drawText( text, strlen(text), 100, 400, paint );
74        this->inval(nullptr);
75    }
76
77private:
78    typedef SampleView INHERITED;
79};
80
81//////////////////////////////////////////////////////////////////////////////
82
83static SkView* MyFactory() { return new IdentityScaleView("mandrill_256.png"); }
84static SkViewRegister reg(MyFactory);
85