2015-11-08 16:07:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
2020-04-06 17:57:30 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2015-11-08 16:07:24 +00:00
|
|
|
|
2015-11-17 17:38:05 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2015-11-17 17:38:05 +00:00
|
|
|
#endif
|
2015-11-08 16:07:24 +00:00
|
|
|
|
|
|
|
SkRandom gRand;
|
|
|
|
|
|
|
|
static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
|
2018-12-22 03:22:31 +00:00
|
|
|
const SkFont& font, const SkPaint& paint) {
|
|
|
|
SkFont f(font);
|
|
|
|
f.setSubpixel(true);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This sample demonstrates the cache behavior of bitmap vs. distance field text
|
|
|
|
// It renders variously sized text with an animated scale and rotation.
|
|
|
|
// Specifically one should:
|
|
|
|
// use 'D' to toggle between bitmap and distance field fonts
|
|
|
|
// use '2' to toggle between scaling the image by 2x
|
|
|
|
// -- this feature boosts the rendering out of the small point-size
|
|
|
|
// SDF-text special case (which falls back to bitmap fonts for small points)
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class AnimatedTextView : public Sample {
|
2019-07-17 13:08:11 +00:00
|
|
|
float fScale = 1;
|
|
|
|
float fScaleInc = 0.1f;
|
|
|
|
float fRotation = 0;
|
|
|
|
int fSizeScale = 1;
|
2015-11-08 16:07:24 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("AnimatedText"); }
|
2015-11-08 16:07:24 +00:00
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2015-11-08 16:07:24 +00:00
|
|
|
if ('2' == uni) {
|
|
|
|
if (fSizeScale == 2) {
|
|
|
|
fSizeScale = 1;
|
|
|
|
} else {
|
|
|
|
fSizeScale = 2;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2018-12-22 03:22:31 +00:00
|
|
|
SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
|
|
|
|
|
2015-11-08 16:07:24 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (direct) {
|
2020-12-19 20:14:06 +00:00
|
|
|
SkSamplingOptions sampling(SkFilterMode::kLinear, SkMipmapMode::kNearest);
|
2020-07-09 17:25:17 +00:00
|
|
|
sk_sp<SkImage> image = direct->priv().testingOnly_getFontAtlasImage(
|
2018-03-06 13:20:37 +00:00
|
|
|
GrMaskFormat::kA8_GrMaskFormat);
|
2020-12-19 20:14:06 +00:00
|
|
|
const SkRect rect = SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f);
|
Reland "Add new virts, hide old ones"
This reverts commit 8f924ac0ce63806886b7297e8be554984a6e7ce5.
Reason for revert: suppressions landed for fuchsia images to rebaseline
Original change's description:
> Revert "Add new virts, hide old ones"
>
> This reverts commit c56e2e5aa65dd129e5927224d2f6c1f82edff74e.
>
> Reason for revert: suspected of breaking chrome roll
>
> Original change's description:
> > Add new virts, hide old ones
> >
> > Add virtuals for the draw methods that now take sampling/filtermode.
> >
> > drawImage
> > drawImageRect
> > drawImageLattice
> > drawAtlas
> >
> > Add a flag that can remove the older virtuals, once each client has
> > stopped overriding them. In that situation, the older public methods
> > will simplify extract the sampling from the paint, and call the new
> > public methods.
> >
> > Bug: skia:11105, skia:7650
> > Change-Id: I8b0029727295caa983e8148fc743a55cfbecd043
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/347022
> > Commit-Queue: Mike Reed <reed@google.com>
> > Reviewed-by: Florin Malita <fmalita@chromium.org>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
>
> TBR=bsalomon@google.com,fmalita@chromium.org,reed@google.com
>
> Change-Id: I0a90952c11a180d918126ea06a630f4a0bf9b49b
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:11105
> Bug: skia:7650
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348194
> Reviewed-by: Derek Sollenberger <djsollen@google.com>
> Commit-Queue: Derek Sollenberger <djsollen@google.com>
TBR=djsollen@google.com,bsalomon@google.com,fmalita@chromium.org,reed@google.com
# Not skipping CQ checks because this is a reland.
Bug: skia:11105
Bug: skia:7650
Change-Id: Ia2b4537a2d330460b7554278d2c05075cf27162a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348876
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2020-12-30 14:22:42 +00:00
|
|
|
canvas->drawImageRect(image.get(), rect, rect, sampling, &paint,
|
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
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++) {
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setSize(SkIntToScalar(i*fSizeScale));
|
|
|
|
y += font.getSpacing();
|
|
|
|
DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setSize(16);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
2019-07-12 16:51:44 +00:00
|
|
|
// TODO: use nanos
|
2016-03-29 16:03:52 +00:00
|
|
|
// We add noise to the scale and rotation animations to
|
2015-11-08 16:07:24 +00:00
|
|
|
// keep the font atlas from falling into a steady state
|
|
|
|
fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
|
|
|
|
fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
|
|
|
|
if (fScale >= 2.0f) {
|
|
|
|
fScaleInc = -0.1f;
|
|
|
|
} else if (fScale <= 1.0f) {
|
|
|
|
fScaleInc = 0.1f;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new AnimatedTextView(); )
|