blob: 6df29d371ca4c11a8ef21379d59f19586f55590e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkColorPriv.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkPath.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkTime.h"
14#include "include/effects/SkBlurMaskFilter.h"
15#include "include/utils/SkRandom.h"
16#include "samplecode/DecodeFile.h"
17#include "samplecode/Sample.h"
18#include "src/core/SkClipOpPriv.h"
19#include "tools/Resources.h"
humperd1d3bae2014-08-08 12:11:33 -070020
21// Intended to exercise pixel snapping observed with scaled images (and
22// with non-scaled images, but for a different reason): Bug 1145
23
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040024class IdentityScaleView : public Sample {
humperd1d3bae2014-08-08 12:11:33 -070025public:
26 IdentityScaleView(const char imageFilename[]) {
Mike Reed0933bc92017-12-09 01:27:41 +000027 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
28 fBM.allocN32Pixels(1, 1);
29 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
30 }
humperd1d3bae2014-08-08 12:11:33 -070031 }
32
33protected:
34 SkBitmap fBM;
35
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036 bool onQuery(Sample::Event* evt) override {
37 if (Sample::TitleQ(*evt)) {
38 Sample::TitleR(evt, "IdentityScale");
humperd1d3bae2014-08-08 12:11:33 -070039 return true;
40 }
41 return this->INHERITED::onQuery(evt);
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 void onDrawContent(SkCanvas* canvas) override {
humperd1d3bae2014-08-08 12:11:33 -070045
Hal Canary4484b8f2019-01-08 14:00:08 -050046 SkFont font(nullptr, 48);
humperd1d3bae2014-08-08 12:11:33 -070047 SkPaint paint;
48
49 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070050 paint.setFilterQuality(kHigh_SkFilterQuality);
humperd1d3bae2014-08-08 12:11:33 -070051
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));
Mike Reedc1f77742016-12-09 09:00:50 -050066 canvas->clipPath(clipPath, kIntersect_SkClipOp, true);
humperd1d3bae2014-08-08 12:11:33 -070067 text = "Scaled = 0";
68 }
69 canvas->drawBitmap( fBM, 100, 100, &paint );
70 canvas->restore();
Hal Canary4484b8f2019-01-08 14:00:08 -050071 canvas->drawString(text, 100, 400, font, paint);
humperd1d3bae2014-08-08 12:11:33 -070072 }
73
74private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040075 typedef Sample INHERITED;
humperd1d3bae2014-08-08 12:11:33 -070076};
77
78//////////////////////////////////////////////////////////////////////////////
79
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040080DEF_SAMPLE( return new IdentityScaleView("images/mandrill_256.png"); )