skia2/samplecode/SampleCowboy.cpp
Brian Salomon 9fa47cc1c6 Make class members that are static constexpr also be inline.
This is in prep for compiling with -std=c++14 and -Wno-c++17-extensions
when building with clang. Chrome has encountered problems with
third_party headers that are included both in Skia and other Chrome
sources that produce different code based on whether preprocessor macros
indicate a C++14 or C++17 compilation.

In C++17 they are already inline implicitly. When compiling with C++14
we can get linker errors unless they're explicitly inlined or defined
outside the class. With -Wno-c++17-extensions we can explicitly inline
them in the C++14 build because the warning that would be generated
about using a C++17 language extension is suppressed.

We cannot do this in public headers because we support compiling with
C++14 without suppressing the C++17 language extension warnings.

Bug: chromium:1257145
Change-Id: Iaf5f4c62a398f98dd4ca9b7dfb86f2d5cab21d66
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/457498
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-10-11 16:22:59 +00:00

124 lines
3.4 KiB
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkTypes.h"
#if defined(SK_ENABLE_SVG)
#include "include/core/SkCanvas.h"
#include "include/core/SkRect.h"
#include "include/core/SkStream.h"
#include "modules/svg/include/SkSVGDOM.h"
#include "modules/svg/include/SkSVGNode.h"
#include "samplecode/Sample.h"
#include "src/core/SkOSFile.h"
#include "src/utils/SkOSPath.h"
#include "src/xml/SkDOM.h"
#include "tools/Resources.h"
namespace {
class AnimatedSVGSample : public Sample {
inline static constexpr auto kAnimationIterations = 5;
enum State {
kZoomIn,
kScroll,
kZoomOut
};
sk_sp<SkSVGDOM> fDom;
const char* fResource = nullptr;
const char* fName = nullptr;
State fState = kZoomIn;
int fAnimationLoop = kAnimationIterations;
SkScalar fDelta = 1;
public:
AnimatedSVGSample(const char* r, const char* n) : fResource(r), fName(n) {}
private:
void onOnceBeforeDraw() override {
SkASSERT(fResource);
auto data = GetResourceAsData(fResource);
if (!data) {
SkDebugf("Resource not found: \"%s\"\n", fResource);
return;
}
SkMemoryStream svgStream(std::move(data));
fDom = SkSVGDOM::MakeFromStream(svgStream);
if (fDom) {
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
}
}
void onDrawContent(SkCanvas* canvas) override {
if (fDom) {
canvas->setMatrix(SkMatrix::Scale(3, 3));
canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
switch (fState) {
case kZoomIn:
fDelta += 0.2f;
canvas->scale(fDelta, fDelta);
break;
case kScroll:
if (fAnimationLoop > kAnimationIterations/2) {
fDelta += 80.f;
} else {
fDelta -= 80.f;
}
canvas->scale(fDelta, fDelta);
canvas->translate(fDelta, 0);
break;
case kZoomOut:
fDelta += 0.2f;
canvas->scale(fDelta, fDelta);
break;
}
fDom->render(canvas);
}
}
void onSizeChange() override {
if (fDom) {
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
}
}
SkString name() override { return SkASSERT(fName), SkString(fName); }
bool onAnimate(double nanos) override {
if (!fDom) {
return false;
}
--fAnimationLoop;
if (fAnimationLoop == 0) {
fAnimationLoop = kAnimationIterations;
switch (fState) {
case kZoomIn:
fState = kScroll;
fDelta = 0;
break;
case kScroll:
fState = kZoomOut;
fDelta = 2;
break;
case kZoomOut:
fState = kZoomIn;
fDelta = 1;
break;
}
}
return true;
}
};
} // namespace
DEF_SAMPLE( return new AnimatedSVGSample("Cowboy.svg", "SampleCowboy"); )
#endif // defined(SK_ENABLE_SVG)