blob: cf1196c62d7fe9a96328baa0bb2b27804ebc1ae1 [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) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000037 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
reed@google.com33535f32012-09-25 15:37:50 +000038 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000039
reed@android.comfead49e2009-10-15 18:51:46 +000040 canvas.drawColor(SK_ColorRED);
41 SkPaint paint;
42 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000043 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000044 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
45 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
reed@google.com3cec4d72011-07-06 13:59:47 +000046 SkShader::kClamp_TileMode))->unref();
reed@google.com33535f32012-09-25 15:37:50 +000047 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
48}
49
50static SkPoint unit_vec(int degrees) {
51 SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
52 SkScalar s, c;
53 s = SkScalarSinCos(rad, &c);
54 return SkPoint::Make(c, s);
55}
56
57static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
58 *value += *delta;
59 if (*value < min) {
60 *value = min;
61 *delta = - *delta;
62 } else if (*value > max) {
63 *value = max;
64 *delta = - *delta;
65 }
66}
67
68static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
69 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
70 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000071}
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
reed@google.comf2183392011-04-22 14:10:48 +000073class BitmapRectView : public SampleView {
reed@google.com33535f32012-09-25 15:37:50 +000074 SkPoint fSrcPts[2];
75 SkPoint fSrcVec[2];
76 SkRect fSrcLimit;
77 SkRect fDstR[2];
78
79 void bounce() {
80 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
81 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
82 }
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000085 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000086 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000087
88 fSrcPts[0].set(0, 0);
89 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
90
91 fSrcVec[0] = unit_vec(30);
92 fSrcVec[1] = unit_vec(107);
93
94 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
95 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
96
97 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
98 SkIntToScalar(250), SkIntToScalar(300));
99 fDstR[1] = fDstR[0];
100 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
101
102 fSrcPts[0].set(32, 32);
103 fSrcPts[1].set(90, 90);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106protected:
107 // overrides from SkEventSink
reed@android.comfead49e2009-10-15 18:51:46 +0000108 virtual bool onQuery(SkEvent* evt) {
109 if (SampleCode::TitleQ(*evt)) {
110 SampleCode::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 return true;
112 }
113 return this->INHERITED::onQuery(evt);
114 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@google.comf2183392011-04-22 14:10:48 +0000116 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com33535f32012-09-25 15:37:50 +0000117 SkRect srcR;
118 srcR.set(fSrcPts[0], fSrcPts[1]);
119 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
120 srcR.offset(-srcR.width()/2, -srcR.height()/2);
reed@android.comfead49e2009-10-15 18:51:46 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000123 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000124 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000125
reed@google.com3cec4d72011-07-06 13:59:47 +0000126 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000127 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000128
reed@google.com33535f32012-09-25 15:37:50 +0000129 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000130
reed@google.com33535f32012-09-25 15:37:50 +0000131 canvas->drawBitmap(bitmap, 0, 0, &paint);
132 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000133
reed@google.com33535f32012-09-25 15:37:50 +0000134 for (int i = 0; i < 2; ++i) {
reed@google.com44699382013-10-31 17:28:30 +0000135 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com33535f32012-09-25 15:37:50 +0000136 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
137 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000138 }
reed@google.com33535f32012-09-25 15:37:50 +0000139
140 this->bounce();
141 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144private:
145 typedef SkView INHERITED;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149
reed@google.com33535f32012-09-25 15:37:50 +0000150static void make_big_bitmap(SkBitmap* bm) {
151 static const char gText[] =
152 "We the people, in order to form a more perfect union, establish justice,"
153 " ensure domestic tranquility, provide for the common defense, promote the"
154 " general welfare and ensure the blessings of liberty to ourselves and our"
155 " posterity, do ordain and establish this constitution for the United"
156 " States of America.";
157
158 const int BIG_H = 120;
159
160 SkPaint paint;
161 paint.setAntiAlias(true);
162 paint.setTextSize(SkIntToScalar(BIG_H));
163
164 const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
165
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000166 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000167 bm->eraseColor(SK_ColorWHITE);
168
169 SkCanvas canvas(*bm);
170
171 canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint);
172}
173
174class BitmapRectView2 : public SampleView {
175 SkBitmap fBitmap;
176
177 SkRect fSrcR;
178 SkRect fLimitR;
179 SkScalar fDX;
180 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000181
reed@google.com33535f32012-09-25 15:37:50 +0000182 void bounceMe() {
183 SkScalar width = fSrcR.width();
184 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
185 fSrcR.fRight = fSrcR.fLeft + width;
186 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000187
reed@google.com33535f32012-09-25 15:37:50 +0000188public:
189 BitmapRectView2() {
190 make_big_bitmap(&fBitmap);
191
192 this->setBGColor(SK_ColorGRAY);
193
194 fSrcR.fLeft = 0;
195 fSrcR.fTop = 0;
196 fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3;
197 fSrcR.fBottom = SkIntToScalar(fBitmap.height());
198
199 fLimitR.set(0, 0,
200 SkIntToScalar(fBitmap.width()),
201 SkIntToScalar(fBitmap.height()));
202
203 fDX = SK_Scalar1;
204
205 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
206 SkIntToScalar(600), SkIntToScalar(200));
207 fDstR[1] = fDstR[0];
208 fDstR[1].offset(0, fDstR[0].height() * 5/4);
209 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000210
reed@google.com33535f32012-09-25 15:37:50 +0000211protected:
212 // overrides from SkEventSink
213 virtual bool onQuery(SkEvent* evt) {
214 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
221 virtual void onDrawContent(SkCanvas* canvas) {
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) {
reed@google.com44699382013-10-31 17:28:30 +0000227 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com33535f32012-09-25 15:37:50 +0000228 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
229 canvas->drawRect(fDstR[i], paint);
230 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000231
reed@google.com33535f32012-09-25 15:37:50 +0000232 this->bounceMe();
233 this->inval(NULL);
234 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000235
reed@google.com33535f32012-09-25 15:37:50 +0000236private:
237 typedef SkView INHERITED;
238};
239
240//////////////////////////////////////////////////////////////////////////////
241
242static SkView* F0() { return new BitmapRectView; }
243static SkView* F1() { return new BitmapRectView2; }
244static SkViewRegister gR0(F0);
245static SkViewRegister gR1(F1);