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 "SampleCode.h" 9 #include "SkView.h" 10 #include "SkCanvas.h" 11 #include "SkCamera.h" 12 #include "SkEmbossMaskFilter.h" 13 #include "SkGradientShader.h" 14 #include "SkPath.h" 15 #include "SkRegion.h" 16 #include "SkShader.h" 17 #include "SkUtils.h" 18 #include "SkRandom.h" 19 #include "SkImageDecoder.h" 20 21 class CameraView : public SampleView { 22 SkTDArray<SkShader*> fShaders; 23 int fShaderIndex; 24 bool fFrontFace; 25 public: 26 CameraView() { 27 fRX = fRY = fRZ = 0; 28 fShaderIndex = 0; 29 fFrontFace = false; 30 31 for (int i = 0;; i++) { 32 SkString str; 33 str.printf("/skimages/elephant%d.jpeg", i); 34 SkBitmap bm; 35 if (SkImageDecoder::DecodeFile(str.c_str(), &bm)) { 36 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 37 SkRect dst = { -150, -150, 150, 150 }; 38 SkMatrix matrix; 39 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); 40 41 SkShader* s = SkShader::CreateBitmapShader(bm, 42 SkShader::kClamp_TileMode, 43 SkShader::kClamp_TileMode, 44 &matrix); 45 *fShaders.append() = s; 46 } else { 47 break; 48 } 49 } 50 this->setBGColor(0xFFDDDDDD); 51 } 52 53 virtual ~CameraView() { 54 fShaders.unrefAll(); 55 } 56 57 protected: 58 // overrides from SkEventSink 59 virtual bool onQuery(SkEvent* evt) { 60 if (SampleCode::TitleQ(*evt)) { 61 SampleCode::TitleR(evt, "Camera"); 62 return true; 63 } 64 return this->INHERITED::onQuery(evt); 65 } 66 67 virtual void onDrawContent(SkCanvas* canvas) { 68 canvas->translate(this->width()/2, this->height()/2); 69 70 Sk3DView view; 71 view.rotateX(fRX); 72 view.rotateY(fRY); 73 view.applyToCanvas(canvas); 74 75 SkPaint paint; 76 if (fShaders.count() > 0) { 77 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0; 78 if (frontFace != fFrontFace) { 79 fFrontFace = frontFace; 80 fShaderIndex = (fShaderIndex + 1) % fShaders.count(); 81 } 82 83 paint.setAntiAlias(true); 84 paint.setShader(fShaders[fShaderIndex]); 85 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 86 SkRect r = { -150, -150, 150, 150 }; 87 canvas->drawRoundRect(r, 30, 30, paint); 88 } 89 90 fRY += SampleCode::GetAnimSecondsDelta() * 90; 91 if (fRY >= SkIntToScalar(360)) { 92 fRY = 0; 93 } 94 this->inval(NULL); 95 } 96 97 private: 98 SkScalar fRX, fRY, fRZ; 99 typedef SampleView INHERITED; 100 }; 101 102 ////////////////////////////////////////////////////////////////////////////// 103 104 static SkView* MyFactory() { return new CameraView; } 105 static SkViewRegister reg(MyFactory); 106