Chromium Code Reviews| Index: samplecode/SampleAnimatedText.cpp |
| diff --git a/samplecode/SampleAnimatedText.cpp b/samplecode/SampleAnimatedText.cpp |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..65af9821caa17ea7aff7705c77b8066dd3b831b3 |
| --- /dev/null |
| +++ b/samplecode/SampleAnimatedText.cpp |
| @@ -0,0 +1,150 @@ |
| +/* |
|
robertphillips
2015/11/06 21:51:14
2015 ?
jvanverth1
2015/11/06 22:06:53
Done.
|
| + * Copyright 2011 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SampleCode.h" |
| +#include "SkView.h" |
| +#include "SkCanvas.h" |
| +#include "SkUtils.h" |
| +#include "SkColorPriv.h" |
| +#include "SkColorFilter.h" |
| +#include "SkGpuDevice.h" |
| +#include "SkRandom.h" |
| +#include "SkSystemEventTypes.h" |
| +#include "SkTime.h" |
| +#include "SkTypeface.h" |
| +#include "SkXfermode.h" |
| +#include "Timer.h" |
| + |
| +#include "GrContext.h" |
| + |
| +SkRandom gRand; |
| + |
| +static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y, |
| + const SkPaint& paint) { |
| + SkPaint p(paint); |
| + |
| + p.setSubpixelText(true); |
|
robertphillips
2015/11/06 21:51:14
Why are we adding 90 here ?
jvanverth1
2015/11/06 22:06:53
Done.
|
| + x += SkIntToScalar(90); |
| + canvas->drawText(text, length, x, y, p); |
| +} |
| + |
|
robertphillips
2015/11/06 21:51:14
// This sample demonstrates the cache behavior of
jvanverth1
2015/11/06 22:06:53
Done.
|
| +class AnimatedTextView : public SampleView { |
| +public: |
| + AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeScale(1) { |
| + fCurrentTime = 0; |
| + fTimer.start(); |
| + memset(fTimes, 0, sizeof(fTimes)); |
| + } |
| + |
| +protected: |
| + // overrides from SkEventSink |
| + bool onQuery(SkEvent* evt) override { |
| + if (SampleCode::TitleQ(*evt)) { |
| + SampleCode::TitleR(evt, "AnimatedText"); |
| + return true; |
| + } |
| + |
| + SkUnichar uni; |
| + if (SampleCode::CharQ(*evt, &uni)) { |
| + switch (uni) { |
|
robertphillips
2015/11/06 21:51:14
put in our standard formatting ?
jvanverth1
2015/11/06 22:06:52
Done.
|
| + case '2': if (fSizeScale == 2) fSizeScale = 1; else fSizeScale = 2; return true; |
| + default: break; |
| + } |
| + } |
| + return this->INHERITED::onQuery(evt); |
| + } |
| + |
| + void onDrawContent(SkCanvas* canvas) override { |
| + SkPaint paint; |
| + SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf"))); |
| + paint.setAntiAlias(true); |
| + paint.setFilterQuality(kMedium_SkFilterQuality); |
| + |
| + SkString outString("fps: "); |
| + fTimer.end(); |
| + |
|
robertphillips
2015/11/06 21:51:14
Isn't this the third instance of this code ? Time
jvanverth1
2015/11/06 22:06:52
Acknowledged.
|
| + fTimes[fCurrentTime] = (float)(fTimer.fWall); |
| + fCurrentTime = (fCurrentTime + 1) & 0x1f; |
| + |
| + float meanTime = 0.0f; |
| + for (int i = 0; i < 32; ++i) { |
| + meanTime += fTimes[i]; |
| + } |
| + meanTime /= 32.f; |
| + SkScalar fps = 1000.f / meanTime; |
| + outString.appendScalar(fps); |
| + outString.append(" ms: "); |
| + outString.appendScalar(meanTime); |
| + |
| + SkString modeString("Text scale: "); |
| + modeString.appendU32(fSizeScale); |
| + modeString.append("x"); |
| + |
| + fTimer.start(); |
| + |
| + canvas->save(); |
| + |
| + SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing(); |
| + GrContext* grContext = canvas->getGrContext(); |
| + if (grContext) { |
|
robertphillips
2015/11/06 21:51:14
Is passing in 'paint' useful ?
jvanverth1
2015/11/06 22:06:52
It allows you to change the texture filtering, for
|
| + grContext->drawFontCache(SkRect::MakeXYWH(512, 10, 512, 512), kA8_GrMaskFormat, paint, |
| + reinterpret_cast<SkGpuDevice*>(device)->accessRenderTarget()); |
| + } |
| + |
| + canvas->translate(180, 180); |
| + canvas->rotate(fRotation); |
| + canvas->scale(fScale, fScale); |
| + canvas->translate(-180, -180); |
| + |
| + const char* text = "Hamburgefons"; |
| + size_t length = strlen(text); |
| + |
| + SkScalar y = SkIntToScalar(0); |
| + for (int i = 12; i <= 26; i++) { |
| + paint.setTextSize(SkIntToScalar(i*fSizeScale)); |
|
robertphillips
2015/11/06 21:51:14
one line ?
remove this loop ?
jvanverth1
2015/11/06 22:06:52
Done.
|
| + for (SkScalar dx = 0; dx <= SkIntToScalar(3) / 4; |
|
robertphillips
2015/11/06 21:51:14
SK_Scalar1 ?
jvanverth1
2015/11/06 22:06:52
Done.
|
| + dx += SkIntToScalar(1)) { |
| + y += paint.getFontSpacing(); |
|
robertphillips
2015/11/06 21:51:14
Just draw at 110 ?
jvanverth1
2015/11/06 22:06:53
Done.
|
| + DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint); |
| + } |
| + } |
| + canvas->restore(); |
| + |
| + paint.setTextSize(16); |
| + canvas->drawText(outString.c_str(), outString.size(), 512.f, 540.f, paint); |
| + canvas->drawText(modeString.c_str(), modeString.size(), 768.f, 540.f, paint); |
| + } |
| + |
| + bool onAnimate(const SkAnimTimer& timer) override { |
|
robertphillips
2015/11/06 21:51:14
// We add noise to the scale and rotation animatio
jvanverth1
2015/11/06 22:06:52
Done.
|
| + fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f)); |
| + fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f)); |
|
robertphillips
2015/11/06 21:51:14
if (fScale >= 2.0f || fScale <= 1.0f) {
fScaleI
jvanverth1
2015/11/06 22:06:53
Done.
|
| + if (fScale >= 2.0f) { |
| + fScaleInc = -0.1f; |
| + } else if (fScale <= 1.0f) { |
| + fScaleInc = 0.1f; |
| + } |
| + return true; |
| + } |
| + |
| +private: |
| + float fScale; |
| + float fScaleInc; |
| + float fRotation; |
| + int fSizeScale; |
| + |
| + WallTimer fTimer; |
| + float fTimes[32]; |
| + int fCurrentTime; |
| + |
| + |
| + typedef SampleView INHERITED; |
| +}; |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +static SkView* MyFactory() { return new AnimatedTextView; } |
| +static SkViewRegister reg(MyFactory); |