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) { |
| 37 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, INT_SIZE, INT_SIZE); |
| 38 | bitmap->allocPixels(); |
| 39 | SkCanvas canvas(*bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 40 | |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 41 | canvas.drawColor(SK_ColorRED); |
| 42 | SkPaint paint; |
| 43 | paint.setAntiAlias(true); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 44 | const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 45 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; |
| 46 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 47 | SkShader::kClamp_TileMode))->unref(); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 48 | canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint); |
| 49 | } |
| 50 | |
| 51 | static 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 | |
| 58 | static 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 | |
| 69 | static 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.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 72 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 74 | class BitmapRectView : public SampleView { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 75 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | BitmapRectView() { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 87 | this->setBGColor(SK_ColorGRAY); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 88 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | protected: |
| 108 | // overrides from SkEventSink |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 109 | virtual bool onQuery(SkEvent* evt) { |
| 110 | if (SampleCode::TitleQ(*evt)) { |
| 111 | SampleCode::TitleR(evt, "BitmapRect"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | return this->INHERITED::onQuery(evt); |
| 115 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 117 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 118 | 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.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | SkPaint paint; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 124 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 125 | paint.setColor(SK_ColorYELLOW); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 126 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 127 | SkBitmap bitmap; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 128 | make_bitmap(&bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 129 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 130 | canvas->translate(20, 20); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 131 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 132 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 133 | canvas->drawRect(srcR, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 134 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 135 | for (int i = 0; i < 2; ++i) { |
| 136 | paint.setFilterBitmap(1 == i); |
| 137 | canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); |
| 138 | canvas->drawRect(fDstR[i], paint); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 139 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 140 | |
| 141 | this->bounce(); |
| 142 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | private: |
| 146 | typedef SkView INHERITED; |
| 147 | }; |
| 148 | |
| 149 | ////////////////////////////////////////////////////////////////////////////// |
| 150 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 151 | static 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 | |
| 176 | class BitmapRectView2 : public SampleView { |
| 177 | SkBitmap fBitmap; |
| 178 | |
| 179 | SkRect fSrcR; |
| 180 | SkRect fLimitR; |
| 181 | SkScalar fDX; |
| 182 | SkRect fDstR[2]; |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 183 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 184 | 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 189 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 190 | public: |
| 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 212 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 213 | protected: |
| 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.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 222 | |
| 223 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 224 | SkPaint paint; |
| 225 | paint.setStyle(SkPaint::kStroke_Style); |
| 226 | paint.setColor(SK_ColorYELLOW); |
| 227 | |
| 228 | for (int i = 0; i < 2; ++i) { |
| 229 | paint.setFilterBitmap(1 == i); |
| 230 | canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); |
| 231 | canvas->drawRect(fDstR[i], paint); |
| 232 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 233 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 234 | this->bounceMe(); |
| 235 | this->inval(NULL); |
| 236 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 237 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 238 | private: |
| 239 | typedef SkView INHERITED; |
| 240 | }; |
| 241 | |
| 242 | ////////////////////////////////////////////////////////////////////////////// |
| 243 | |
| 244 | static SkView* F0() { return new BitmapRectView; } |
| 245 | static SkView* F1() { return new BitmapRectView2; } |
| 246 | static SkViewRegister gR0(F0); |
| 247 | static SkViewRegister gR1(F1); |