epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkRegion.h" |
| 16 | #include "SkShader.h" |
| 17 | #include "SkUtils.h" |
| 18 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
| 20 | #include "SkColorFilter.h" |
| 21 | #include "SkTime.h" |
| 22 | #include "SkTypeface.h" |
| 23 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkOSFile.h" |
| 25 | #include "SkStream.h" |
| 26 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 27 | #define INT_SIZE 64 |
| 28 | #define SCALAR_SIZE SkIntToScalar(INT_SIZE) |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 29 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 30 | static void make_bitmap(SkBitmap* bitmap) { |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 31 | bitmap->allocN32Pixels(INT_SIZE, INT_SIZE); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 32 | SkCanvas canvas(*bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 33 | |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 34 | canvas.drawColor(SK_ColorRED); |
| 35 | SkPaint paint; |
| 36 | paint.setAntiAlias(true); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 37 | const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 38 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; |
| 39 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 40 | SkShader::kClamp_TileMode))->unref(); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 41 | canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint); |
| 42 | } |
| 43 | |
| 44 | static 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 | |
| 51 | static 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 | |
| 62 | static 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.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 65 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 67 | class BitmapRectView : public SampleView { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 68 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 79 | BitmapRectView() { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 80 | this->setBGColor(SK_ColorGRAY); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 81 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | protected: |
| 101 | // overrides from SkEventSink |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 102 | virtual bool onQuery(SkEvent* evt) { |
| 103 | if (SampleCode::TitleQ(*evt)) { |
| 104 | SampleCode::TitleR(evt, "BitmapRect"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | return this->INHERITED::onQuery(evt); |
| 108 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 109 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 110 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 111 | 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.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 115 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | SkPaint paint; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 117 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 118 | paint.setColor(SK_ColorYELLOW); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 119 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 120 | SkBitmap bitmap; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 121 | make_bitmap(&bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 122 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 123 | canvas->translate(20, 20); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 124 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 125 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 126 | canvas->drawRect(srcR, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 127 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 128 | for (int i = 0; i < 2; ++i) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 129 | paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 130 | canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); |
| 131 | canvas->drawRect(fDstR[i], paint); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 132 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 133 | |
| 134 | this->bounce(); |
| 135 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 137 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | private: |
| 139 | typedef SkView INHERITED; |
| 140 | }; |
| 141 | |
| 142 | ////////////////////////////////////////////////////////////////////////////// |
| 143 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 144 | static 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.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 160 | bm->allocN32Pixels(BIG_W, BIG_H); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 161 | bm->eraseColor(SK_ColorWHITE); |
| 162 | |
| 163 | SkCanvas canvas(*bm); |
| 164 | |
| 165 | canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint); |
| 166 | } |
| 167 | |
| 168 | class BitmapRectView2 : public SampleView { |
| 169 | SkBitmap fBitmap; |
| 170 | |
| 171 | SkRect fSrcR; |
| 172 | SkRect fLimitR; |
| 173 | SkScalar fDX; |
| 174 | SkRect fDstR[2]; |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 175 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 176 | 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 181 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 182 | public: |
| 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 204 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 205 | protected: |
| 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 214 | |
| 215 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 216 | SkPaint paint; |
| 217 | paint.setStyle(SkPaint::kStroke_Style); |
| 218 | paint.setColor(SK_ColorYELLOW); |
| 219 | |
| 220 | for (int i = 0; i < 2; ++i) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 221 | paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 222 | canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); |
| 223 | canvas->drawRect(fDstR[i], paint); |
| 224 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 225 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 226 | this->bounceMe(); |
| 227 | this->inval(NULL); |
| 228 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 229 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 230 | private: |
| 231 | typedef SkView INHERITED; |
| 232 | }; |
| 233 | |
| 234 | ////////////////////////////////////////////////////////////////////////////// |
| 235 | |
| 236 | static SkView* F0() { return new BitmapRectView; } |
| 237 | static SkView* F1() { return new BitmapRectView2; } |
| 238 | static SkViewRegister gR0(F0); |
| 239 | static SkViewRegister gR1(F1); |