1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "DecodeFile.h" 9 #include "Sample.h" 10 #include "SkAnimTimer.h" 11 #include "SkCanvas.h" 12 #include "SkCamera.h" 13 #include "SkEmbossMaskFilter.h" 14 #include "SkGradientShader.h" 15 #include "SkPath.h" 16 #include "SkRandom.h" 17 #include "SkRegion.h" 18 #include "SkShader.h" 19 #include "SkString.h" 20 #include "SkUTF.h" 21 22 class CameraView : public Sample { 23 SkTArray<sk_sp<SkShader>> fShaders; 24 int fShaderIndex; 25 bool fFrontFace; 26 public: CameraView()27 CameraView() { 28 fRX = fRY = fRZ = 0; 29 fShaderIndex = 0; 30 fFrontFace = false; 31 32 for (int i = 0;; i++) { 33 SkString str; 34 str.printf("/skimages/elephant%d.jpeg", i); 35 SkBitmap bm; 36 if (decode_file(str.c_str(), &bm)) { 37 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 38 SkRect dst = { -150, -150, 150, 150 }; 39 SkMatrix matrix; 40 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); 41 42 fShaders.push_back(SkShader::MakeBitmapShader(bm, 43 SkShader::kClamp_TileMode, 44 SkShader::kClamp_TileMode, 45 &matrix)); 46 } else { 47 break; 48 } 49 } 50 this->setBGColor(0xFFDDDDDD); 51 } 52 53 protected: onQuery(Sample::Event * evt)54 bool onQuery(Sample::Event* evt) override { 55 if (Sample::TitleQ(*evt)) { 56 Sample::TitleR(evt, "Camera"); 57 return true; 58 } 59 return this->INHERITED::onQuery(evt); 60 } 61 onDrawContent(SkCanvas * canvas)62 void onDrawContent(SkCanvas* canvas) override { 63 canvas->translate(this->width()/2, this->height()/2); 64 65 Sk3DView view; 66 view.rotateX(fRX); 67 view.rotateY(fRY); 68 view.applyToCanvas(canvas); 69 70 SkPaint paint; 71 if (fShaders.count() > 0) { 72 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0; 73 if (frontFace != fFrontFace) { 74 fFrontFace = frontFace; 75 fShaderIndex = (fShaderIndex + 1) % fShaders.count(); 76 } 77 78 paint.setAntiAlias(true); 79 paint.setShader(fShaders[fShaderIndex]); 80 paint.setFilterQuality(kLow_SkFilterQuality); 81 SkRect r = { -150, -150, 150, 150 }; 82 canvas->drawRoundRect(r, 30, 30, paint); 83 } 84 } 85 onAnimate(const SkAnimTimer & timer)86 bool onAnimate(const SkAnimTimer& timer) override { 87 if (timer.isStopped()) { 88 fRY = 0; 89 } else { 90 fRY = timer.scaled(90, 360); 91 } 92 return true; 93 } 94 95 private: 96 SkScalar fRX, fRY, fRZ; 97 typedef Sample INHERITED; 98 }; 99 100 ////////////////////////////////////////////////////////////////////////////// 101 102 DEF_SAMPLE( return new CameraView(); ) 103