blob: 006b9195543de58fd2e0944376294d651955ffcf [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
18#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
23
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkOSFile.h"
25#include "SkStream.h"
26
reed@google.com33535f32012-09-25 15:37:50 +000027#define INT_SIZE 64
28#define SCALAR_SIZE SkIntToScalar(INT_SIZE)
reed@google.com3cec4d72011-07-06 13:59:47 +000029
reed@google.com33535f32012-09-25 15:37:50 +000030static void make_bitmap(SkBitmap* bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000031 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
reed@google.com33535f32012-09-25 15:37:50 +000032 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000033
reed@android.comfead49e2009-10-15 18:51:46 +000034 canvas.drawColor(SK_ColorRED);
35 SkPaint paint;
36 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000037 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000038 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
39 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
reed@google.com3cec4d72011-07-06 13:59:47 +000040 SkShader::kClamp_TileMode))->unref();
reed@google.com33535f32012-09-25 15:37:50 +000041 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
42}
43
44static SkPoint unit_vec(int degrees) {
45 SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
46 SkScalar s, c;
47 s = SkScalarSinCos(rad, &c);
48 return SkPoint::Make(c, s);
49}
50
51static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
52 *value += *delta;
53 if (*value < min) {
54 *value = min;
55 *delta = - *delta;
56 } else if (*value > max) {
57 *value = max;
58 *delta = - *delta;
59 }
60}
61
62static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
63 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
64 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000065}
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
reed@google.comf2183392011-04-22 14:10:48 +000067class BitmapRectView : public SampleView {
reed@google.com33535f32012-09-25 15:37:50 +000068 SkPoint fSrcPts[2];
69 SkPoint fSrcVec[2];
70 SkRect fSrcLimit;
71 SkRect fDstR[2];
72
73 void bounce() {
74 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
75 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
76 }
77
reed@android.com8a1c16f2008-12-17 15:59:43 +000078public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000079 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000080 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000081
82 fSrcPts[0].set(0, 0);
83 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
84
85 fSrcVec[0] = unit_vec(30);
86 fSrcVec[1] = unit_vec(107);
87
88 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
89 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
90
91 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
92 SkIntToScalar(250), SkIntToScalar(300));
93 fDstR[1] = fDstR[0];
94 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
95
96 fSrcPts[0].set(32, 32);
97 fSrcPts[1].set(90, 90);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100protected:
101 // overrides from SkEventSink
reed@android.comfead49e2009-10-15 18:51:46 +0000102 virtual bool onQuery(SkEvent* evt) {
103 if (SampleCode::TitleQ(*evt)) {
104 SampleCode::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 return true;
106 }
107 return this->INHERITED::onQuery(evt);
108 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@google.comf2183392011-04-22 14:10:48 +0000110 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com33535f32012-09-25 15:37:50 +0000111 SkRect srcR;
112 srcR.set(fSrcPts[0], fSrcPts[1]);
113 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
114 srcR.offset(-srcR.width()/2, -srcR.height()/2);
reed@android.comfead49e2009-10-15 18:51:46 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000117 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000118 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000119
reed@google.com3cec4d72011-07-06 13:59:47 +0000120 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000121 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000122
reed@google.com33535f32012-09-25 15:37:50 +0000123 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000124
reed@google.com33535f32012-09-25 15:37:50 +0000125 canvas->drawBitmap(bitmap, 0, 0, &paint);
126 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000127
reed@google.com33535f32012-09-25 15:37:50 +0000128 for (int i = 0; i < 2; ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000129 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com33535f32012-09-25 15:37:50 +0000130 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
131 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000132 }
reed@google.com33535f32012-09-25 15:37:50 +0000133
134 this->bounce();
135 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138private:
139 typedef SkView INHERITED;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143
reed@google.com33535f32012-09-25 15:37:50 +0000144static void make_big_bitmap(SkBitmap* bm) {
145 static const char gText[] =
146 "We the people, in order to form a more perfect union, establish justice,"
147 " ensure domestic tranquility, provide for the common defense, promote the"
148 " general welfare and ensure the blessings of liberty to ourselves and our"
149 " posterity, do ordain and establish this constitution for the United"
150 " States of America.";
151
152 const int BIG_H = 120;
153
154 SkPaint paint;
155 paint.setAntiAlias(true);
156 paint.setTextSize(SkIntToScalar(BIG_H));
157
158 const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
159
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000160 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000161 bm->eraseColor(SK_ColorWHITE);
162
163 SkCanvas canvas(*bm);
164
165 canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint);
166}
167
168class BitmapRectView2 : public SampleView {
169 SkBitmap fBitmap;
170
171 SkRect fSrcR;
172 SkRect fLimitR;
173 SkScalar fDX;
174 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000175
reed@google.com33535f32012-09-25 15:37:50 +0000176 void bounceMe() {
177 SkScalar width = fSrcR.width();
178 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
179 fSrcR.fRight = fSrcR.fLeft + width;
180 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000181
reed@google.com33535f32012-09-25 15:37:50 +0000182public:
183 BitmapRectView2() {
184 make_big_bitmap(&fBitmap);
185
186 this->setBGColor(SK_ColorGRAY);
187
188 fSrcR.fLeft = 0;
189 fSrcR.fTop = 0;
190 fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3;
191 fSrcR.fBottom = SkIntToScalar(fBitmap.height());
192
193 fLimitR.set(0, 0,
194 SkIntToScalar(fBitmap.width()),
195 SkIntToScalar(fBitmap.height()));
196
197 fDX = SK_Scalar1;
198
199 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
200 SkIntToScalar(600), SkIntToScalar(200));
201 fDstR[1] = fDstR[0];
202 fDstR[1].offset(0, fDstR[0].height() * 5/4);
203 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000204
reed@google.com33535f32012-09-25 15:37:50 +0000205protected:
206 // overrides from SkEventSink
207 virtual bool onQuery(SkEvent* evt) {
208 if (SampleCode::TitleQ(*evt)) {
209 SampleCode::TitleR(evt, "BigBitmapRect");
210 return true;
211 }
212 return this->INHERITED::onQuery(evt);
213 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000214
215 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com33535f32012-09-25 15:37:50 +0000216 SkPaint paint;
217 paint.setStyle(SkPaint::kStroke_Style);
218 paint.setColor(SK_ColorYELLOW);
219
220 for (int i = 0; i < 2; ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000221 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com33535f32012-09-25 15:37:50 +0000222 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
223 canvas->drawRect(fDstR[i], paint);
224 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000225
reed@google.com33535f32012-09-25 15:37:50 +0000226 this->bounceMe();
227 this->inval(NULL);
228 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000229
reed@google.com33535f32012-09-25 15:37:50 +0000230private:
231 typedef SkView INHERITED;
232};
233
234//////////////////////////////////////////////////////////////////////////////
235
236static SkView* F0() { return new BitmapRectView; }
237static SkView* F1() { return new BitmapRectView2; }
238static SkViewRegister gR0(F0);
239static SkViewRegister gR1(F1);