blob: 5b337d25392f08e9e9d81cac712e41f3ea6e412b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/utils/SkRandom.h"
15#include "samplecode/DecodeFile.h"
16#include "samplecode/Sample.h"
17#include "src/core/SkClipOpPriv.h"
18#include "tools/Resources.h"
humperd1d3bae2014-08-08 12:11:33 -070019
20// Intended to exercise pixel snapping observed with scaled images (and
21// with non-scaled images, but for a different reason): Bug 1145
22
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040023class IdentityScaleView : public Sample {
humperd1d3bae2014-08-08 12:11:33 -070024public:
25 IdentityScaleView(const char imageFilename[]) {
Mike Reed0933bc92017-12-09 01:27:41 +000026 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
27 fBM.allocN32Pixels(1, 1);
28 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
29 }
humperd1d3bae2014-08-08 12:11:33 -070030 }
31
32protected:
33 SkBitmap fBM;
34
Hal Canary8a027312019-07-03 10:55:44 -040035 SkString name() override { return SkString("IdentityScale"); }
humperd1d3bae2014-08-08 12:11:33 -070036
mtklein36352bf2015-03-25 18:17:31 -070037 void onDrawContent(SkCanvas* canvas) override {
humperd1d3bae2014-08-08 12:11:33 -070038
Hal Canary4484b8f2019-01-08 14:00:08 -050039 SkFont font(nullptr, 48);
humperd1d3bae2014-08-08 12:11:33 -070040 SkPaint paint;
41
42 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070043 paint.setFilterQuality(kHigh_SkFilterQuality);
humperd1d3bae2014-08-08 12:11:33 -070044
45 SkTime::DateTime time;
46 SkTime::GetDateTime(&time);
47
48 bool use_scale = (time.fSecond % 2 == 1);
49 const char *text;
50
51 canvas->save();
52 if (use_scale) {
53 text = "Scaled = 1";
54 } else {
55
56 SkRect r = { 100, 100, 356, 356 };
57 SkPath clipPath;
Mike Reed4241f5e2019-09-14 19:13:23 +000058 clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5));
Mike Reedc1f77742016-12-09 09:00:50 -050059 canvas->clipPath(clipPath, kIntersect_SkClipOp, true);
humperd1d3bae2014-08-08 12:11:33 -070060 text = "Scaled = 0";
61 }
62 canvas->drawBitmap( fBM, 100, 100, &paint );
63 canvas->restore();
Hal Canary4484b8f2019-01-08 14:00:08 -050064 canvas->drawString(text, 100, 400, font, paint);
humperd1d3bae2014-08-08 12:11:33 -070065 }
66
67private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040068 typedef Sample INHERITED;
humperd1d3bae2014-08-08 12:11:33 -070069};
70
71//////////////////////////////////////////////////////////////////////////////
72
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040073DEF_SAMPLE( return new IdentityScaleView("images/mandrill_256.png"); )