epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkCamera.h" |
| 13 | #include "SkEmbossMaskFilter.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkRegion.h" |
| 17 | #include "SkShader.h" |
| 18 | #include "SkUtils.h" |
| 19 | #include "SkRandom.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 20 | #include "SkImageDecoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 22 | class CameraView : public SampleView { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 23 | SkTDArray<SkShader*> fShaders; |
| 24 | int fShaderIndex; |
| 25 | bool fFrontFace; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 27 | CameraView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | fRX = fRY = fRZ = 0; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 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 (SkImageDecoder::DecodeFile(str.c_str(), &bm)) { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 37 | SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 38 | SkRect dst = { -150, -150, 150, 150 }; |
| 39 | SkMatrix matrix; |
| 40 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 41 | |
| 42 | SkShader* s = SkShader::CreateBitmapShader(bm, |
| 43 | SkShader::kClamp_TileMode, |
| 44 | SkShader::kClamp_TileMode, |
| 45 | &matrix); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 46 | *fShaders.append() = s; |
| 47 | } else { |
| 48 | break; |
| 49 | } |
| 50 | } |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 51 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 54 | virtual ~CameraView() { |
| 55 | fShaders.unrefAll(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
| 59 | // overrides from SkEventSink |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 60 | virtual bool onQuery(SkEvent* evt) { |
mike@reedtribe.org | d173b87 | 2014-01-23 02:02:45 +0000 | [diff] [blame] | 61 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | SampleCode::TitleR(evt, "Camera"); |
| 63 | return true; |
| 64 | } |
| 65 | return this->INHERITED::onQuery(evt); |
| 66 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 67 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 68 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | canvas->translate(this->width()/2, this->height()/2); |
| 70 | |
| 71 | Sk3DView view; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 72 | view.rotateX(fRX); |
| 73 | view.rotateY(fRY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | view.applyToCanvas(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkPaint paint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 77 | if (fShaders.count() > 0) { |
| 78 | bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0; |
| 79 | if (frontFace != fFrontFace) { |
| 80 | fFrontFace = frontFace; |
| 81 | fShaderIndex = (fShaderIndex + 1) % fShaders.count(); |
| 82 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 84 | paint.setAntiAlias(true); |
| 85 | paint.setShader(fShaders[fShaderIndex]); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame^] | 86 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 87 | SkRect r = { -150, -150, 150, 150 }; |
| 88 | canvas->drawRoundRect(r, 30, 30, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 90 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 92 | bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE { |
| 93 | if (timer.isStopped()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | fRY = 0; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 95 | } else { |
| 96 | fRY = timer.scaled(90, 360); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 97 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 98 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | private: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 102 | SkScalar fRX, fRY, fRZ; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 103 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | ////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | static SkView* MyFactory() { return new CameraView; } |
| 109 | static SkViewRegister reg(MyFactory); |