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 */ 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" 15#include "SkRegion.h" 16#include "SkShader.h" 17#include "SkUtils.h" 18#include "SkXfermode.h" 19#include "SkColorPriv.h" 20#include "SkColorFilter.h" 21#include "SkTime.h" 22#include "SkTypeface.h" 23 24#include "SkOSFile.h" 25#include "SkStream.h" 26 27#if SK_SUPPORT_GPU 28#include "SkGpuDevice.h" 29#else 30class GrContext; 31#endif 32 33#define INT_SIZE 64 34#define SCALAR_SIZE SkIntToScalar(INT_SIZE) 35 36static void make_bitmap(SkBitmap* bitmap) { 37 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE); 38 SkCanvas canvas(*bitmap); 39 40 canvas.drawColor(SK_ColorRED); 41 SkPaint paint; 42 paint.setAntiAlias(true); 43 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; 44 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; 45 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, 46 SkShader::kClamp_TileMode))->unref(); 47 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); 71} 72 73class BitmapRectView : public SampleView { 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 84public: 85 BitmapRectView() { 86 this->setBGColor(SK_ColorGRAY); 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); 104 } 105 106protected: 107 // overrides from SkEventSink 108 virtual bool onQuery(SkEvent* evt) { 109 if (SampleCode::TitleQ(*evt)) { 110 SampleCode::TitleR(evt, "BitmapRect"); 111 return true; 112 } 113 return this->INHERITED::onQuery(evt); 114 } 115 116 virtual void onDrawContent(SkCanvas* canvas) { 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); 121 122 SkPaint paint; 123 paint.setStyle(SkPaint::kStroke_Style); 124 paint.setColor(SK_ColorYELLOW); 125 126 SkBitmap bitmap; 127 make_bitmap(&bitmap); 128 129 canvas->translate(20, 20); 130 131 canvas->drawBitmap(bitmap, 0, 0, &paint); 132 canvas->drawRect(srcR, paint); 133 134 for (int i = 0; i < 2; ++i) { 135 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); 136 canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint); 137 canvas->drawRect(fDstR[i], paint); 138 } 139 140 this->bounce(); 141 this->inval(NULL); 142 } 143 144private: 145 typedef SkView INHERITED; 146}; 147 148////////////////////////////////////////////////////////////////////////////// 149 150static 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 166 bm->allocN32Pixels(BIG_W, BIG_H); 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 174class BitmapRectView2 : public SampleView { 175 SkBitmap fBitmap; 176 177 SkRect fSrcR; 178 SkRect fLimitR; 179 SkScalar fDX; 180 SkRect fDstR[2]; 181 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 } 187 188public: 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 } 210 211protected: 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 } 220 221 virtual void onDrawContent(SkCanvas* canvas) { 222 SkPaint paint; 223 paint.setStyle(SkPaint::kStroke_Style); 224 paint.setColor(SK_ColorYELLOW); 225 226 for (int i = 0; i < 2; ++i) { 227 paint.setFilterLevel(1 == i ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); 228 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint); 229 canvas->drawRect(fDstR[i], paint); 230 } 231 232 this->bounceMe(); 233 this->inval(NULL); 234 } 235 236private: 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); 246