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 "include/core/SkCanvas.h" 9 #include "include/core/SkImage.h" 10 #include "include/core/SkShader.h" 11 #include "include/core/SkString.h" 12 #include "include/utils/SkCamera.h" 13 #include "samplecode/DecodeFile.h" 14 #include "samplecode/Sample.h" 15 #include "src/effects/SkEmbossMaskFilter.h" 16 #include "tools/Resources.h" 17 #include "tools/timer/TimeUtils.h" 18 19 namespace { 20 class CameraView : public Sample { 21 SkTArray<sk_sp<SkShader>> fShaders; 22 int fShaderIndex = 0; 23 bool fFrontFace = false; 24 SkScalar fRX = 0; 25 SkScalar fRY = 0; 26 name()27 SkString name() override { return SkString("Camera"); } 28 onOnceBeforeDraw()29 void onOnceBeforeDraw() override { 30 for (const char* resource : { 31 "images/mandrill_512_q075.jpg", 32 "images/dog.jpg", 33 "images/gamut.png", 34 }) { 35 SkBitmap bm; 36 if (GetResourceAsBitmap(resource, &bm)) { 37 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 38 SkRect dst = { -150, -150, 150, 150 }; 39 fShaders.push_back(bm.makeShader(SkSamplingOptions(SkFilterMode::kLinear), 40 SkMatrix::RectToRect(src, dst))); 41 } 42 } 43 this->setBGColor(0xFFDDDDDD); 44 } 45 onDrawContent(SkCanvas * canvas)46 void onDrawContent(SkCanvas* canvas) override { 47 if (fShaders.count() > 0) { 48 canvas->translate(this->width()/2, this->height()/2); 49 50 Sk3DView view; 51 view.rotateX(fRX); 52 view.rotateY(fRY); 53 view.applyToCanvas(canvas); 54 55 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0; 56 if (frontFace != fFrontFace) { 57 fFrontFace = frontFace; 58 fShaderIndex = (fShaderIndex + 1) % fShaders.count(); 59 } 60 61 SkPaint paint; 62 paint.setAntiAlias(true); 63 paint.setShader(fShaders[fShaderIndex]); 64 SkRect r = { -150, -150, 150, 150 }; 65 canvas->drawRoundRect(r, 30, 30, paint); 66 } 67 } 68 onAnimate(double nanos)69 bool onAnimate(double nanos) override { 70 fRY = nanos ? TimeUtils::Scaled(1e-9 * nanos, 90, 360) : 0; 71 return true; 72 } 73 }; 74 } // namespace 75 DEF_SAMPLE( return new CameraView(); ) 76