OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 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 "SkCanvas.h" | |
13 #include "SkColorFilter.h" | |
14 #include "SkColorPriv.h" | |
15 #include "SkData.h" | |
16 #include "SkImageGenerator.h" | |
17 #include "SkDumpCanvas.h" | |
18 #include "SkGradientShader.h" | |
19 #include "SkGraphics.h" | |
20 #include "SkImageDecoder.h" | |
21 #include "SkOSFile.h" | |
22 #include "SkPath.h" | |
23 #include "SkPicture.h" | |
24 #include "SkPictureRecorder.h" | |
25 #include "SkRandom.h" | |
26 #include "SkRegion.h" | |
27 #include "SkShader.h" | |
28 #include "SkStream.h" | |
29 #include "SkTime.h" | |
30 #include "SkTypeface.h" | |
31 #include "SkUtils.h" | |
32 #include "SkView.h" | |
33 #include "SkXMLParser.h" | |
34 #include "SkXfermode.h" | |
35 | |
36 /////////////////////////////////////////////////////////////////////////////// | |
37 | |
38 static SkBitmap load_bitmap() { | |
39 SkBitmap bm; | |
40 SkString pngFilename = GetResourcePath("mandrill_512.png"); | |
41 SkAutoDataUnref data(SkData::NewFromFileName(pngFilename.c_str())); | |
42 if (data.get() != nullptr) { | |
43 SkInstallDiscardablePixelRef(data, &bm); | |
44 } | |
45 return bm; | |
46 } | |
47 | |
48 static void drawCircle(SkCanvas* canvas, int r, SkColor color) { | |
49 SkPaint paint; | |
50 paint.setAntiAlias(true); | |
51 paint.setColor(color); | |
52 | |
53 canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r), | |
54 paint); | |
55 } | |
56 | |
57 class PictureView : public SampleView { | |
58 SkBitmap fBitmap; | |
59 public: | |
60 PictureView() { | |
61 | |
62 fBitmap = load_bitmap(); | |
63 | |
64 SkPictureRecorder recorder; | |
65 | |
66 recorder.beginRecording(100, 100, nullptr, 0); | |
67 fSubPicture = recorder.endRecording(); | |
68 | |
69 SkCanvas* canvas = recorder.beginRecording(100, 100, nullptr, 0); | |
70 SkPaint paint; | |
71 paint.setAntiAlias(true); | |
72 | |
73 canvas->drawBitmap(fBitmap, 0, 0, nullptr); | |
74 | |
75 drawCircle(canvas, 50, SK_ColorBLACK); | |
76 canvas->drawPicture(fSubPicture); | |
77 canvas->translate(SkIntToScalar(50), 0); | |
78 canvas->drawPicture(fSubPicture); | |
79 canvas->translate(0, SkIntToScalar(50)); | |
80 canvas->drawPicture(fSubPicture); | |
81 canvas->translate(SkIntToScalar(-50), 0); | |
82 canvas->drawPicture(fSubPicture); | |
83 | |
84 fPicture = recorder.endRecording(); | |
85 | |
86 // fPicture now has (4) references to fSubPicture. We can release our re
f, | |
87 // and just unref fPicture in our destructor, and it will in turn take c
are of | |
88 // the other references to fSubPicture | |
89 fSubPicture->unref(); | |
90 } | |
91 | |
92 virtual ~PictureView() { | |
93 fPicture->unref(); | |
94 } | |
95 | |
96 protected: | |
97 // overrides from SkEventSink | |
98 bool onQuery(SkEvent* evt) override { | |
99 if (SampleCode::TitleQ(*evt)) { | |
100 SampleCode::TitleR(evt, "Picture"); | |
101 return true; | |
102 } | |
103 return this->INHERITED::onQuery(evt); | |
104 } | |
105 | |
106 void drawSomething(SkCanvas* canvas) { | |
107 SkPaint paint; | |
108 | |
109 canvas->save(); | |
110 canvas->scale(0.5f, 0.5f); | |
111 canvas->drawBitmap(fBitmap, 0, 0, nullptr); | |
112 canvas->restore(); | |
113 | |
114 paint.setAntiAlias(true); | |
115 | |
116 paint.setColor(SK_ColorRED); | |
117 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), | |
118 SkIntToScalar(40), paint); | |
119 paint.setColor(SK_ColorBLACK); | |
120 paint.setTextSize(SkIntToScalar(40)); | |
121 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62), | |
122 paint); | |
123 | |
124 } | |
125 | |
126 void onDrawContent(SkCanvas* canvas) override { | |
127 this->drawSomething(canvas); | |
128 | |
129 SkPictureRecorder recorder; | |
130 this->drawSomething(recorder.beginRecording(100, 100, nullptr, 0)); | |
131 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); | |
132 | |
133 canvas->save(); | |
134 canvas->translate(SkIntToScalar(300), SkIntToScalar(50)); | |
135 canvas->scale(-SK_Scalar1, -SK_Scalar1); | |
136 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50)); | |
137 canvas->drawPicture(pict); | |
138 canvas->restore(); | |
139 | |
140 canvas->save(); | |
141 canvas->translate(SkIntToScalar(200), SkIntToScalar(150)); | |
142 canvas->scale(SK_Scalar1, -SK_Scalar1); | |
143 canvas->translate(0, -SkIntToScalar(50)); | |
144 canvas->drawPicture(pict); | |
145 canvas->restore(); | |
146 | |
147 canvas->save(); | |
148 canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); | |
149 canvas->scale(-SK_Scalar1, SK_Scalar1); | |
150 canvas->translate(-SkIntToScalar(100), 0); | |
151 canvas->drawPicture(pict); | |
152 canvas->restore(); | |
153 } | |
154 | |
155 private: | |
156 #define INVAL_ALL_TYPE "inval-all" | |
157 | |
158 void delayInval(SkMSec delay) { | |
159 (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay); | |
160 } | |
161 | |
162 bool onEvent(const SkEvent& evt) override { | |
163 if (evt.isType(INVAL_ALL_TYPE)) { | |
164 this->inval(nullptr); | |
165 return true; | |
166 } | |
167 return this->INHERITED::onEvent(evt); | |
168 } | |
169 | |
170 SkPicture* fPicture; | |
171 SkPicture* fSubPicture; | |
172 | |
173 typedef SampleView INHERITED; | |
174 }; | |
175 | |
176 ////////////////////////////////////////////////////////////////////////////// | |
177 | |
178 static SkView* MyFactory() { return new PictureView; } | |
179 static SkViewRegister reg(MyFactory); | |
OLD | NEW |