blob: 422ed583efb4b7ed79ce71ff110c740fd04a13fc [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000027#if SK_SUPPORT_GPU
reed@google.com3cec4d72011-07-06 13:59:47 +000028#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000029#else
30class GrContext;
31#endif
32
reed@google.com33535f32012-09-25 15:37:50 +000033#define INT_SIZE 64
34#define SCALAR_SIZE SkIntToScalar(INT_SIZE)
reed@google.com3cec4d72011-07-06 13:59:47 +000035
reed@google.com33535f32012-09-25 15:37:50 +000036static void make_bitmap(SkBitmap* bitmap) {
37 bitmap->setConfig(SkBitmap::kARGB_8888_Config, INT_SIZE, INT_SIZE);
38 bitmap->allocPixels();
39 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000040
reed@android.comfead49e2009-10-15 18:51:46 +000041 canvas.drawColor(SK_ColorRED);
42 SkPaint paint;
43 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000044 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000045 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
46 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
reed@google.com3cec4d72011-07-06 13:59:47 +000047 SkShader::kClamp_TileMode))->unref();
reed@google.com33535f32012-09-25 15:37:50 +000048 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
49}
50
51static SkPoint unit_vec(int degrees) {
52 SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
53 SkScalar s, c;
54 s = SkScalarSinCos(rad, &c);
55 return SkPoint::Make(c, s);
56}
57
58static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
59 *value += *delta;
60 if (*value < min) {
61 *value = min;
62 *delta = - *delta;
63 } else if (*value > max) {
64 *value = max;
65 *delta = - *delta;
66 }
67}
68
69static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
70 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
71 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000072}
reed@android.com8a1c16f2008-12-17 15:59:43 +000073
reed@google.comf2183392011-04-22 14:10:48 +000074class BitmapRectView : public SampleView {
reed@google.com33535f32012-09-25 15:37:50 +000075 SkPoint fSrcPts[2];
76 SkPoint fSrcVec[2];
77 SkRect fSrcLimit;
78 SkRect fDstR[2];
79
80 void bounce() {
81 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
82 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
83 }
84
reed@android.com8a1c16f2008-12-17 15:59:43 +000085public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000086 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000087 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000088
89 fSrcPts[0].set(0, 0);
90 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
91
92 fSrcVec[0] = unit_vec(30);
93 fSrcVec[1] = unit_vec(107);
94
95 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
96 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
97
98 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
99 SkIntToScalar(250), SkIntToScalar(300));
100 fDstR[1] = fDstR[0];
101 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
102
103 fSrcPts[0].set(32, 32);
104 fSrcPts[1].set(90, 90);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107protected:
108 // overrides from SkEventSink
reed@android.comfead49e2009-10-15 18:51:46 +0000109 virtual bool onQuery(SkEvent* evt) {
110 if (SampleCode::TitleQ(*evt)) {
111 SampleCode::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 return true;
113 }
114 return this->INHERITED::onQuery(evt);
115 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@google.comf2183392011-04-22 14:10:48 +0000117 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com33535f32012-09-25 15:37:50 +0000118 SkRect srcR;
119 srcR.set(fSrcPts[0], fSrcPts[1]);
120 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
121 srcR.offset(-srcR.width()/2, -srcR.height()/2);
reed@android.comfead49e2009-10-15 18:51:46 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000124 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000125 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000126
reed@google.com3cec4d72011-07-06 13:59:47 +0000127 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000128 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000129
reed@google.com33535f32012-09-25 15:37:50 +0000130 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000131
reed@google.com33535f32012-09-25 15:37:50 +0000132 canvas->drawBitmap(bitmap, 0, 0, &paint);
133 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000134
reed@google.com33535f32012-09-25 15:37:50 +0000135 for (int i = 0; i < 2; ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000136 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com33535f32012-09-25 15:37:50 +0000137 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
138 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000139 }
reed@google.com33535f32012-09-25 15:37:50 +0000140
141 this->bounce();
142 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145private:
146 typedef SkView INHERITED;
147};
148
149//////////////////////////////////////////////////////////////////////////////
150
reed@google.com33535f32012-09-25 15:37:50 +0000151static void make_big_bitmap(SkBitmap* bm) {
152 static const char gText[] =
153 "We the people, in order to form a more perfect union, establish justice,"
154 " ensure domestic tranquility, provide for the common defense, promote the"
155 " general welfare and ensure the blessings of liberty to ourselves and our"
156 " posterity, do ordain and establish this constitution for the United"
157 " States of America.";
158
159 const int BIG_H = 120;
160
161 SkPaint paint;
162 paint.setAntiAlias(true);
163 paint.setTextSize(SkIntToScalar(BIG_H));
164
165 const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
166
167 bm->setConfig(SkBitmap::kARGB_8888_Config, BIG_W, BIG_H);
168 bm->allocPixels();
169 bm->eraseColor(SK_ColorWHITE);
170
171 SkCanvas canvas(*bm);
172
173 canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint);
174}
175
176class BitmapRectView2 : public SampleView {
177 SkBitmap fBitmap;
178
179 SkRect fSrcR;
180 SkRect fLimitR;
181 SkScalar fDX;
182 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000183
reed@google.com33535f32012-09-25 15:37:50 +0000184 void bounceMe() {
185 SkScalar width = fSrcR.width();
186 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
187 fSrcR.fRight = fSrcR.fLeft + width;
188 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000189
reed@google.com33535f32012-09-25 15:37:50 +0000190public:
191 BitmapRectView2() {
192 make_big_bitmap(&fBitmap);
193
194 this->setBGColor(SK_ColorGRAY);
195
196 fSrcR.fLeft = 0;
197 fSrcR.fTop = 0;
198 fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3;
199 fSrcR.fBottom = SkIntToScalar(fBitmap.height());
200
201 fLimitR.set(0, 0,
202 SkIntToScalar(fBitmap.width()),
203 SkIntToScalar(fBitmap.height()));
204
205 fDX = SK_Scalar1;
206
207 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
208 SkIntToScalar(600), SkIntToScalar(200));
209 fDstR[1] = fDstR[0];
210 fDstR[1].offset(0, fDstR[0].height() * 5/4);
211 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000212
reed@google.com33535f32012-09-25 15:37:50 +0000213protected:
214 // overrides from SkEventSink
215 virtual bool onQuery(SkEvent* evt) {
216 if (SampleCode::TitleQ(*evt)) {
217 SampleCode::TitleR(evt, "BigBitmapRect");
218 return true;
219 }
220 return this->INHERITED::onQuery(evt);
221 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000222
223 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com33535f32012-09-25 15:37:50 +0000224 SkPaint paint;
225 paint.setStyle(SkPaint::kStroke_Style);
226 paint.setColor(SK_ColorYELLOW);
227
228 for (int i = 0; i < 2; ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000229 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com33535f32012-09-25 15:37:50 +0000230 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
231 canvas->drawRect(fDstR[i], paint);
232 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000233
reed@google.com33535f32012-09-25 15:37:50 +0000234 this->bounceMe();
235 this->inval(NULL);
236 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000237
reed@google.com33535f32012-09-25 15:37:50 +0000238private:
239 typedef SkView INHERITED;
240};
241
242//////////////////////////////////////////////////////////////////////////////
243
244static SkView* F0() { return new BitmapRectView; }
245static SkView* F1() { return new BitmapRectView2; }
246static SkViewRegister gR0(F0);
247static SkViewRegister gR1(F1);