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" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 9 | #include "Sample.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 "SkCanvas.h" |
| 12 | #include "SkCamera.h" |
| 13 | #include "SkEmbossMaskFilter.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #include "SkPath.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 16 | #include "SkRandom.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkRegion.h" |
| 18 | #include "SkShader.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 19 | #include "SkString.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame^] | 20 | #include "SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 22 | class CameraView : public Sample { |
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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 54 | bool onQuery(Sample::Event* evt) override { |
| 55 | if (Sample::TitleQ(*evt)) { |
| 56 | Sample::TitleR(evt, "Camera"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | return this->INHERITED::onQuery(evt); |
| 60 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 61 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 62 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | canvas->translate(this->width()/2, this->height()/2); |
| 64 | |
| 65 | Sk3DView view; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 66 | view.rotateX(fRX); |
| 67 | view.rotateY(fRY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | view.applyToCanvas(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | SkPaint paint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 78 | paint.setAntiAlias(true); |
| 79 | paint.setShader(fShaders[fShaderIndex]); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 80 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 81 | SkRect r = { -150, -150, 150, 150 }; |
| 82 | canvas->drawRoundRect(r, 30, 30, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 84 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 87 | if (timer.isStopped()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | fRY = 0; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 89 | } else { |
| 90 | fRY = timer.scaled(90, 360); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 91 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 92 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | private: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 96 | SkScalar fRX, fRY, fRZ; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 97 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | ////////////////////////////////////////////////////////////////////////////// |
| 101 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 102 | DEF_SAMPLE( return new CameraView(); ) |