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 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 27 | #if SK_SUPPORT_GPU |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 28 | #include "SkGpuDevice.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 29 | #else |
| 30 | class GrContext; |
| 31 | #endif |
| 32 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 33 | #define INT_SIZE 64 |
| 34 | #define SCALAR_SIZE SkIntToScalar(INT_SIZE) |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 35 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 36 | static void make_bitmap(SkBitmap* bitmap) { |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 37 | bitmap->allocN32Pixels(INT_SIZE, INT_SIZE); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 38 | SkCanvas canvas(*bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 39 | |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 40 | canvas.drawColor(SK_ColorRED); |
| 41 | SkPaint paint; |
| 42 | paint.setAntiAlias(true); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 43 | const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 44 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; |
| 45 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 46 | SkShader::kClamp_TileMode))->unref(); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 47 | canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint); |
| 48 | } |
| 49 | |
| 50 | static 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 | |
| 57 | static 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 | |
| 68 | static 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.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 71 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 73 | class BitmapRectView : public SampleView { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 74 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | BitmapRectView() { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 86 | this->setBGColor(SK_ColorGRAY); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 87 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | protected: |
| 107 | // overrides from SkEventSink |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 108 | virtual bool onQuery(SkEvent* evt) { |
| 109 | if (SampleCode::TitleQ(*evt)) { |
| 110 | SampleCode::TitleR(evt, "BitmapRect"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | return this->INHERITED::onQuery(evt); |
| 114 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 116 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 117 | 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.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | SkPaint paint; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 123 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 124 | paint.setColor(SK_ColorYELLOW); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 125 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 126 | SkBitmap bitmap; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 127 | make_bitmap(&bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 128 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 129 | canvas->translate(20, 20); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 130 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 131 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 132 | canvas->drawRect(srcR, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 133 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 134 | for (int i = 0; i < 2; ++i) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 135 | paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 136 | canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); |
| 137 | canvas->drawRect(fDstR[i], paint); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 138 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 139 | |
| 140 | this->bounce(); |
| 141 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 143 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | private: |
| 145 | typedef SkView INHERITED; |
| 146 | }; |
| 147 | |
| 148 | ////////////////////////////////////////////////////////////////////////////// |
| 149 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 150 | static 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.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 166 | bm->allocN32Pixels(BIG_W, BIG_H); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 167 | bm->eraseColor(SK_ColorWHITE); |
| 168 | |
| 169 | SkCanvas canvas(*bm); |
| 170 | |
| 171 | canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint); |
| 172 | } |
| 173 | |
| 174 | class BitmapRectView2 : public SampleView { |
| 175 | SkBitmap fBitmap; |
| 176 | |
| 177 | SkRect fSrcR; |
| 178 | SkRect fLimitR; |
| 179 | SkScalar fDX; |
| 180 | SkRect fDstR[2]; |
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 | 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 187 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 188 | public: |
| 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 210 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 211 | protected: |
| 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 220 | |
| 221 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 222 | SkPaint paint; |
| 223 | paint.setStyle(SkPaint::kStroke_Style); |
| 224 | paint.setColor(SK_ColorYELLOW); |
| 225 | |
| 226 | for (int i = 0; i < 2; ++i) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 227 | paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 228 | canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); |
| 229 | canvas->drawRect(fDstR[i], paint); |
| 230 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 231 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 232 | this->bounceMe(); |
| 233 | this->inval(NULL); |
| 234 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 235 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 236 | private: |
| 237 | typedef SkView INHERITED; |
| 238 | }; |
| 239 | |
| 240 | ////////////////////////////////////////////////////////////////////////////// |
| 241 | |
| 242 | static SkView* F0() { return new BitmapRectView; } |
| 243 | static SkView* F1() { return new BitmapRectView2; } |
| 244 | static SkViewRegister gR0(F0); |
| 245 | static SkViewRegister gR1(F1); |