84b2bd5c770a9ef932107282ad33a1ef4c7e54bf
[platform/upstream/libSkiaSharp.git] / samplecode / SampleIdentityScale.cpp
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 "gm.h"
9
10 #include "Resources.h"
11 #include "SampleCode.h"
12 #include "SkBlurMaskFilter.h"
13 #include "SkCanvas.h"
14 #include "SkColorPriv.h"
15 #include "SkImageDecoder.h"
16 #include "SkRandom.h"
17 #include "SkStream.h"
18 #include "SkTime.h"
19
20 // Intended to exercise pixel snapping observed with scaled images (and
21 // with non-scaled images, but for a different reason):  Bug 1145
22
23 class IdentityScaleView : public SampleView {
24 public:
25     IdentityScaleView(const char imageFilename[]) {
26       SkString resourcePath = GetResourcePath(imageFilename);
27       SkImageDecoder* codec = NULL;
28       SkFILEStream stream(resourcePath.c_str());
29       if (stream.isValid()) {
30           codec = SkImageDecoder::Factory(&stream);
31       }
32       if (codec) {
33           stream.rewind();
34           codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
35           SkDELETE(codec);
36       } else {
37           fBM.allocN32Pixels(1, 1);
38           *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
39       }
40     }
41
42 protected:
43     SkBitmap fBM;
44
45     // overrides from SkEventSink
46     bool onQuery(SkEvent* evt) SK_OVERRIDE {
47         if (SampleCode::TitleQ(*evt)) {
48             SampleCode::TitleR(evt, "IdentityScale");
49             return true;
50         }
51         return this->INHERITED::onQuery(evt);
52     }
53
54     void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
55
56         SkPaint paint;
57
58         paint.setAntiAlias(true);
59         paint.setTextSize(48);
60         paint.setFilterQuality(kHigh_SkFilterQuality);
61
62         SkTime::DateTime time;
63         SkTime::GetDateTime(&time);
64
65         bool use_scale = (time.fSecond % 2 == 1);
66         const char *text;
67
68         canvas->save();
69         if (use_scale) {
70           text = "Scaled = 1";
71         } else {
72
73           SkRect r = { 100, 100, 356, 356 };
74           SkPath clipPath;
75           clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5));
76           canvas->clipPath(clipPath, SkRegion::kIntersect_Op, SkToBool(1));
77           text = "Scaled = 0";
78         }
79         canvas->drawBitmap( fBM, 100, 100, &paint );
80         canvas->restore();
81         canvas->drawText( text, strlen(text), 100, 400, paint );
82         this->inval(NULL);
83     }
84
85 private:
86     typedef SampleView INHERITED;
87 };
88
89 //////////////////////////////////////////////////////////////////////////////
90
91 static SkView* MyFactory() { return new IdentityScaleView("mandrill_256.png"); }
92 static SkViewRegister reg(MyFactory);