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