blob: ad235416cd056f939bac8524826b90f754c037c4 [file] [log] [blame]
humperd1d3bae2014-08-08 12:11:33 -07001/*
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
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
humperd1d3bae2014-08-08 12:11:33 -07009#include "gm.h"
10
11#include "Resources.h"
12#include "SampleCode.h"
13#include "SkBlurMaskFilter.h"
14#include "SkCanvas.h"
15#include "SkColorPriv.h"
bungemand3ebb482015-08-05 13:57:49 -070016#include "SkPath.h"
humperd1d3bae2014-08-08 12:11:33 -070017#include "SkRandom.h"
18#include "SkStream.h"
19#include "SkTime.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050020#include "SkClipOpPriv.h"
humperd1d3bae2014-08-08 12:11:33 -070021
22// Intended to exercise pixel snapping observed with scaled images (and
23// with non-scaled images, but for a different reason): Bug 1145
24
25class IdentityScaleView : public SampleView {
26public:
27 IdentityScaleView(const char imageFilename[]) {
Mike Reed0933bc92017-12-09 01:27:41 +000028 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
29 fBM.allocN32Pixels(1, 1);
30 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
31 }
humperd1d3bae2014-08-08 12:11:33 -070032 }
33
34protected:
35 SkBitmap fBM;
36
37 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070038 bool onQuery(SkEvent* evt) override {
humperd1d3bae2014-08-08 12:11:33 -070039 if (SampleCode::TitleQ(*evt)) {
40 SampleCode::TitleR(evt, "IdentityScale");
41 return true;
42 }
43 return this->INHERITED::onQuery(evt);
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onDrawContent(SkCanvas* canvas) override {
humperd1d3bae2014-08-08 12:11:33 -070047
48 SkPaint paint;
49
50 paint.setAntiAlias(true);
51 paint.setTextSize(48);
reed93a12152015-03-16 10:08:34 -070052 paint.setFilterQuality(kHigh_SkFilterQuality);
humperd1d3bae2014-08-08 12:11:33 -070053
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));
Mike Reedc1f77742016-12-09 09:00:50 -050068 canvas->clipPath(clipPath, kIntersect_SkClipOp, true);
humperd1d3bae2014-08-08 12:11:33 -070069 text = "Scaled = 0";
70 }
71 canvas->drawBitmap( fBM, 100, 100, &paint );
72 canvas->restore();
Cary Clark2a475ea2017-04-28 15:35:12 -040073 canvas->drawString(text, 100, 400, paint );
humperd1d3bae2014-08-08 12:11:33 -070074 }
75
76private:
77 typedef SampleView INHERITED;
78};
79
80//////////////////////////////////////////////////////////////////////////////
81
Hal Canaryc465d132017-12-08 10:21:31 -050082static SkView* MyFactory() { return new IdentityScaleView("images/mandrill_256.png"); }
humperd1d3bae2014-08-08 12:11:33 -070083static SkViewRegister reg(MyFactory);