• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkShader.h"
10 #include "include/core/SkString.h"
11 #include "include/utils/SkCamera.h"
12 #include "samplecode/DecodeFile.h"
13 #include "samplecode/Sample.h"
14 #include "src/effects/SkEmbossMaskFilter.h"
15 #include "tools/Resources.h"
16 #include "tools/timer/TimeUtils.h"
17 
18 namespace {
19 class CameraView : public Sample {
20     SkTArray<sk_sp<SkShader>> fShaders;
21     int fShaderIndex = 0;
22     bool fFrontFace = false;
23     SkScalar fRX = 0;
24     SkScalar fRY = 0;
25 
name()26     SkString name() override { return SkString("Camera"); }
27 
onOnceBeforeDraw()28     void onOnceBeforeDraw() override {
29         for (const char* resource : {
30             "images/mandrill_512_q075.jpg",
31             "images/dog.jpg",
32             "images/gamut.png",
33         }) {
34             SkBitmap bm;
35             if (GetResourceAsBitmap(resource, &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                 fShaders.push_back(bm.makeShader(&matrix));
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             paint.setFilterQuality(kLow_SkFilterQuality);
65             SkRect r = { -150, -150, 150, 150 };
66             canvas->drawRoundRect(r, 30, 30, paint);
67         }
68     }
69 
onAnimate(double nanos)70     bool onAnimate(double nanos) override {
71         fRY = nanos ? TimeUtils::Scaled(1e-9 * nanos, 90, 360) : 0;
72         return true;
73     }
74 };
75 }  // namespace
76 DEF_SAMPLE( return new CameraView(); )
77