blob: 3c700eca66b84caf0c3665b00acdb0f0ece09bb4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed76113a92015-02-02 12:55:02 -08007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
19#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkTime.h"
23#include "SkTypeface.h"
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkOSFile.h"
26#include "SkStream.h"
27
reed@google.com33535f32012-09-25 15:37:50 +000028#define INT_SIZE 64
29#define SCALAR_SIZE SkIntToScalar(INT_SIZE)
reed@google.com3cec4d72011-07-06 13:59:47 +000030
reed@google.com33535f32012-09-25 15:37:50 +000031static void make_bitmap(SkBitmap* bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000032 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
reed@google.com33535f32012-09-25 15:37:50 +000033 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000034
reed@android.comfead49e2009-10-15 18:51:46 +000035 canvas.drawColor(SK_ColorRED);
36 SkPaint paint;
37 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000038 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000039 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
40 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
reed@google.com3cec4d72011-07-06 13:59:47 +000041 SkShader::kClamp_TileMode))->unref();
reed@google.com33535f32012-09-25 15:37:50 +000042 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
43}
44
45static SkPoint unit_vec(int degrees) {
46 SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
47 SkScalar s, c;
48 s = SkScalarSinCos(rad, &c);
49 return SkPoint::Make(c, s);
50}
51
52static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
53 *value += *delta;
54 if (*value < min) {
55 *value = min;
56 *delta = - *delta;
57 } else if (*value > max) {
58 *value = max;
59 *delta = - *delta;
60 }
61}
62
63static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
64 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
65 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000066}
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
reed@google.comf2183392011-04-22 14:10:48 +000068class BitmapRectView : public SampleView {
reed@google.com33535f32012-09-25 15:37:50 +000069 SkPoint fSrcPts[2];
70 SkPoint fSrcVec[2];
71 SkRect fSrcLimit;
72 SkRect fDstR[2];
73
74 void bounce() {
75 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
76 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
77 }
78
reed76113a92015-02-02 12:55:02 -080079 void resetBounce() {
80 fSrcPts[0].set(0, 0);
81 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
82
83 fSrcVec[0] = unit_vec(30);
84 fSrcVec[1] = unit_vec(107);
85 }
86
reed@android.com8a1c16f2008-12-17 15:59:43 +000087public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000088 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000089 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000090
reed76113a92015-02-02 12:55:02 -080091 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +000092
93 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
94 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
95
96 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
97 SkIntToScalar(250), SkIntToScalar(300));
98 fDstR[1] = fDstR[0];
99 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
100
101 fSrcPts[0].set(32, 32);
102 fSrcPts[1].set(90, 90);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105protected:
reed76113a92015-02-02 12:55:02 -0800106 bool onQuery(SkEvent* evt) SK_OVERRIDE {
reed@android.comfead49e2009-10-15 18:51:46 +0000107 if (SampleCode::TitleQ(*evt)) {
108 SampleCode::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 return true;
110 }
111 return this->INHERITED::onQuery(evt);
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed76113a92015-02-02 12:55:02 -0800114 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com33535f32012-09-25 15:37:50 +0000115 SkRect srcR;
116 srcR.set(fSrcPts[0], fSrcPts[1]);
117 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
118 srcR.offset(-srcR.width()/2, -srcR.height()/2);
reed@android.comfead49e2009-10-15 18:51:46 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000121 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000122 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000123
reed@google.com3cec4d72011-07-06 13:59:47 +0000124 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000125 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000126
reed@google.com33535f32012-09-25 15:37:50 +0000127 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000128
reed@google.com33535f32012-09-25 15:37:50 +0000129 canvas->drawBitmap(bitmap, 0, 0, &paint);
130 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000131
reed@google.com33535f32012-09-25 15:37:50 +0000132 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700133 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@google.com33535f32012-09-25 15:37:50 +0000134 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
135 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000136 }
reed76113a92015-02-02 12:55:02 -0800137 }
reed@google.com33535f32012-09-25 15:37:50 +0000138
reed76113a92015-02-02 12:55:02 -0800139 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
140 if (timer.isStopped()) {
141 this->resetBounce();
142 } else if (timer.isRunning()) {
143 this->bounce();
144 }
145 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148private:
reed76113a92015-02-02 12:55:02 -0800149 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150};
151
152//////////////////////////////////////////////////////////////////////////////
153
reed@google.com33535f32012-09-25 15:37:50 +0000154static void make_big_bitmap(SkBitmap* bm) {
155 static const char gText[] =
156 "We the people, in order to form a more perfect union, establish justice,"
157 " ensure domestic tranquility, provide for the common defense, promote the"
158 " general welfare and ensure the blessings of liberty to ourselves and our"
159 " posterity, do ordain and establish this constitution for the United"
160 " States of America.";
161
162 const int BIG_H = 120;
163
164 SkPaint paint;
165 paint.setAntiAlias(true);
166 paint.setTextSize(SkIntToScalar(BIG_H));
167
168 const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
169
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000170 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000171 bm->eraseColor(SK_ColorWHITE);
172
173 SkCanvas canvas(*bm);
174
175 canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint);
176}
177
178class BitmapRectView2 : public SampleView {
179 SkBitmap fBitmap;
180
181 SkRect fSrcR;
182 SkRect fLimitR;
183 SkScalar fDX;
184 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000185
reed@google.com33535f32012-09-25 15:37:50 +0000186 void bounceMe() {
187 SkScalar width = fSrcR.width();
188 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
189 fSrcR.fRight = fSrcR.fLeft + width;
190 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000191
reed76113a92015-02-02 12:55:02 -0800192 void resetBounce() {
193 fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
194 fDX = SK_Scalar1;
195 }
196
reed@google.com33535f32012-09-25 15:37:50 +0000197public:
198 BitmapRectView2() {
199 make_big_bitmap(&fBitmap);
200
201 this->setBGColor(SK_ColorGRAY);
202
reed76113a92015-02-02 12:55:02 -0800203 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +0000204
reed76113a92015-02-02 12:55:02 -0800205 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
reed@google.com33535f32012-09-25 15:37:50 +0000206
reed76113a92015-02-02 12:55:02 -0800207 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
reed@google.com33535f32012-09-25 15:37:50 +0000208 fDstR[1] = fDstR[0];
209 fDstR[1].offset(0, fDstR[0].height() * 5/4);
210 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000211
reed@google.com33535f32012-09-25 15:37:50 +0000212protected:
reed76113a92015-02-02 12:55:02 -0800213 bool onQuery(SkEvent* evt) SK_OVERRIDE {
reed@google.com33535f32012-09-25 15:37:50 +0000214 if (SampleCode::TitleQ(*evt)) {
215 SampleCode::TitleR(evt, "BigBitmapRect");
216 return true;
217 }
218 return this->INHERITED::onQuery(evt);
219 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000220
reed76113a92015-02-02 12:55:02 -0800221 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com33535f32012-09-25 15:37:50 +0000222 SkPaint paint;
223 paint.setStyle(SkPaint::kStroke_Style);
224 paint.setColor(SK_ColorYELLOW);
225
226 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700227 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@google.com33535f32012-09-25 15:37:50 +0000228 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
229 canvas->drawRect(fDstR[i], paint);
230 }
reed76113a92015-02-02 12:55:02 -0800231 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000232
reed76113a92015-02-02 12:55:02 -0800233 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
234 if (timer.isStopped()) {
235 this->resetBounce();
236 } else if (timer.isRunning()) {
237 this->bounceMe();
238 }
239 return true;
reed@google.com33535f32012-09-25 15:37:50 +0000240 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000241
reed@google.com33535f32012-09-25 15:37:50 +0000242private:
reed76113a92015-02-02 12:55:02 -0800243 typedef SampleView INHERITED;
reed@google.com33535f32012-09-25 15:37:50 +0000244};
245
246//////////////////////////////////////////////////////////////////////////////
247
248static SkView* F0() { return new BitmapRectView; }
249static SkView* F1() { return new BitmapRectView2; }
250static SkViewRegister gR0(F0);
251static SkViewRegister gR1(F1);