Add Chinese zoom Sample.

Bug: skia:
Change-Id: Ibe5ffee64fe9fccd6b8d86c1310a78d2d51e424e
Reviewed-on: https://skia-review.googlesource.com/97065
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Jim Van Verth 2018-01-22 12:45:47 -05:00 committed by Skia Commit-Bot
parent de161cb256
commit 87d18ced0a
4 changed files with 138 additions and 11 deletions

View File

@ -332,7 +332,7 @@ public:
/** Get pointer to atlas texture for given mask format. Note that this wraps an
actively mutating texture in an SkImage. This could yield unexpected results
if it gets cached or used more generally. */
sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format);
sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format, uint32_t index = 0);
GrAuditTrail* getAuditTrail() { return &fAuditTrail; }

View File

@ -15,6 +15,10 @@
#include "SkTypeface.h"
#include "SkTextBlob.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#endif
static void make_paint(SkPaint* paint, sk_sp<SkTypeface> typeface) {
static const int kTextSize = 56;
@ -124,7 +128,134 @@ private:
typedef SkView INHERITED;
};
class ChineseZoomView : public SampleView {
public:
ChineseZoomView() : fBlobs(kNumBlobs), fScale(1.0f) {}
protected:
bool onQuery(SkEvent* evt) override {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "chinese-zoom");
return true;
}
SkUnichar uni;
if (SampleCode::CharQ(*evt, &uni)) {
if ('>' == uni) {
fScale += 0.125f;
return true;
}
if ('<' == uni) {
fScale -= 0.125f;
return true;
}
}
return this->INHERITED::onQuery(evt);
}
void onDrawContent(SkCanvas* canvas) override {
if (!fInitialized) {
this->init();
fInitialized = true;
}
canvas->clear(0xFFDDDDDD);
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(0xDE000000);
paint.setTypeface(fTypeface);
paint.setTextSize(11);
paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
#if SK_SUPPORT_GPU
GrContext* grContext = canvas->getGrContext();
if (grContext) {
sk_sp<SkImage> image =
grContext->getFontAtlasImage_ForTesting(GrMaskFormat::kA8_GrMaskFormat, 0);
canvas->drawImageRect(image,
SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0), &paint);
image = grContext->getFontAtlasImage_ForTesting(GrMaskFormat::kA8_GrMaskFormat, 1);
canvas->drawImageRect(image,
SkRect::MakeXYWH(1024.0f, 10.0f, 512.f, 512.0f), &paint);
image = grContext->getFontAtlasImage_ForTesting(GrMaskFormat::kA8_GrMaskFormat, 2);
canvas->drawImageRect(image,
SkRect::MakeXYWH(512.0f, 522.0f, 512.0f, 512.0f), &paint);
image = grContext->getFontAtlasImage_ForTesting(GrMaskFormat::kA8_GrMaskFormat, 3);
canvas->drawImageRect(image,
SkRect::MakeXYWH(1024.0f, 522.0f, 512.0f, 512.0f), &paint);
}
#endif
canvas->scale(fScale, fScale);
// draw a consistent run of the 'words' - one word per line
SkScalar y = 0;
for (int index = 0; index < kNumBlobs; ++index) {
y += -fMetrics.fAscent;
canvas->drawTextBlob(fBlobs[index], 0, y, paint);
y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading);
}
}
private:
static constexpr auto kNumBlobs = 8;
static constexpr auto kParagraphLength = 175;
void init() {
fTypeface = chinese_typeface();
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(0xDE000000);
paint.setTypeface(fTypeface);
paint.setTextSize(11);
paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
paint.getFontMetrics(&fMetrics);
SkUnichar glyphs[45];
for (int32_t i = 0; i < kNumBlobs; ++i) {
SkTextBlobBuilder builder;
auto paragraphLength = kParagraphLength;
SkScalar y = 0;
while (paragraphLength - 45 > 0) {
auto currentLineLength = SkTMin(45, paragraphLength - 45);
this->createRandomLine(glyphs, currentLineLength);
sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs,
currentLineLength*4, paint, 0, y);
y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
paragraphLength -= 45;
}
fBlobs.emplace_back(builder.make());
}
fIndex = 0;
}
// Construct a random kWordLength character 'word' drawing from the full Chinese set
void createRandomLine(SkUnichar glyphs[45], int lineLength) {
for (auto i = 0; i < lineLength; ++i) {
glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0);
}
}
bool fInitialized = false;
sk_sp<SkTypeface> fTypeface;
SkPaint::FontMetrics fMetrics;
SkTArray<sk_sp<SkTextBlob>> fBlobs;
SkRandom fRand;
SkScalar fScale;
int fIndex;
typedef SkView INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new ChineseFlingView; }
static SkViewRegister reg(MyFactory);
static SkView* FlingFactory() { return new ChineseFlingView; }
static SkViewRegister regFling(FlingFactory);
static SkView* ZoomFactory() { return new ChineseZoomView; }
static SkViewRegister regZoom(ZoomFactory);

View File

@ -147,17 +147,17 @@ void GrContext::printGpuStats() const {
SkDebugf("%s", out.c_str());
}
sk_sp<SkImage> GrContext::getFontAtlasImage_ForTesting(GrMaskFormat format) {
sk_sp<SkImage> GrContext::getFontAtlasImage_ForTesting(GrMaskFormat format, uint32_t index) {
GrAtlasGlyphCache* cache = this->contextPriv().getAtlasGlyphCache();
const sk_sp<GrTextureProxy>* proxies = cache->getProxies(format);
if (!proxies[0]) {
if (index >= cache->getAtlasPageCount(format) || !proxies[index]) {
return nullptr;
}
SkASSERT(proxies[0]->priv().isExact());
SkASSERT(proxies[index]->priv().isExact());
sk_sp<SkImage> image(new SkImage_Gpu(this, kNeedNewImageUniqueID, kPremul_SkAlphaType,
std::move(proxies[0]), nullptr, SkBudgeted::kNo));
std::move(proxies[index]), nullptr, SkBudgeted::kNo));
return image;
}

View File

@ -819,12 +819,8 @@ void Viewer::drawSlide(SkCanvas* canvas) {
}
void Viewer::onBackendCreated() {
this->updateTitle();
this->updateUIState();
this->setupCurrentSlide();
fStatsLayer.resetMeasurements();
fWindow->show();
fWindow->inval();
}
void Viewer::onPaint(SkCanvas* canvas) {