skia2/gm/rsxtext.cpp

111 lines
3.7 KiB
C++
Raw Normal View History

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkRSXform.h"
#include "include/core/SkShader.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTextBlob.h"
#include "tools/ToolUtils.h"
// Exercises RSX text blobs + shader with various local matrix combinations.
// Yellow grid should stay aligned for text vs. background.
class RSXShaderGM : public skiagm::GM {
public:
private:
SkString onShortName() override {
return SkString("rsx_blob_shader");
}
SkISize onISize() override {
return SkISize::Make(kSZ*kScale*2.1f, kSZ*kScale*2.1f);
}
void onOnceBeforeDraw() override {
const SkFontStyle style(SkFontStyle::kExtraBlack_Weight,
SkFontStyle::kNormal_Width,
SkFontStyle::kUpright_Slant);
SkFont font(ToolUtils::create_portable_typeface(nullptr, style), kFontSZ);
Reland "drawGlyphRunRSXform: post-compose shader local matrix adjustments" This reverts commit d150a58c04ed492be2e5812e5c9a67450cfb42e9. Reason for revert: hopefully not to culprit for roller issues Original change's description: > Revert "drawGlyphRunRSXform: post-compose shader local matrix adjustments" > > This reverts commit ff49f92e26ebd2ac09efe46846f75daf711e9ef4. > > Reason for revert: Likely the CL that's making fushcia_x64 grumpy > > Original change's description: > > drawGlyphRunRSXform: post-compose shader local matrix adjustments > > > > Bug: skia:11113 > > Change-Id: Ic34a0c80b44031d77d361d35e64301297b3f3189 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/347047 > > Commit-Queue: Florin Malita <fmalita@chromium.org> > > Commit-Queue: Florin Malita <fmalita@google.com> > > Reviewed-by: Mike Reed <reed@google.com> > > TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com > > Change-Id: Ieb9ac7597f518f4209d07b063b5752a2265244b4 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:11113 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348656 > Reviewed-by: Chris Dalton <csmartdalton@google.com> > Commit-Queue: Chris Dalton <csmartdalton@google.com> TBR=csmartdalton@google.com,fmalita@chromium.org,fmalita@google.com,reed@google.com # Not skipping CQ checks because this is a reland. Bug: skia:11113 Change-Id: I598653d9b79f00d36e7a0f700af92d273bbf6305 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348193 Reviewed-by: Florin Malita <fmalita@google.com> Commit-Queue: Florin Malita <fmalita@google.com>
2020-12-30 13:12:58 +00:00
font.setEdging(SkFont::Edging::kAntiAlias);
static constexpr char txt[] = "TEST";
SkGlyphID glyphs[16];
float widths[16];
const auto glyph_count = font.textToGlyphs(txt, strlen(txt), SkTextEncoding::kUTF8,
glyphs, SK_ARRAY_COUNT(glyphs));
font.getWidths(glyphs, glyph_count, widths);
SkTextBlobBuilder builder;
const auto& buf = builder.allocRunRSXform(font, glyph_count);
std::copy(glyphs, glyphs + glyph_count, buf.glyphs);
float x = 0;
for (int i = 0; i < glyph_count; ++i) {
buf.xforms()[i] = {
1, 0,
x, 0,
};
x += widths[i];
}
fBlob = builder.make();
}
void onDraw(SkCanvas* canvas) override {
canvas->scale(kScale, kScale);
this->draw_one(canvas,
{0, 0}, SkMatrix::I(), SkMatrix::I());
this->draw_one(canvas,
{kSZ*1.1f, 0}, SkMatrix::Scale(2, 2), SkMatrix::I());
this->draw_one(canvas,
{0, kSZ*1.1f}, SkMatrix::I(), SkMatrix::RotateDeg(45));
this->draw_one(canvas,
{kSZ*1.1f, kSZ*1.1f}, SkMatrix::Scale(2, 2), SkMatrix::RotateDeg(45));
}
void draw_one(SkCanvas* canvas, SkPoint pos, const SkMatrix& lm,
const SkMatrix& outer_lm) const {
SkAutoCanvasRestore acr(canvas, true);
canvas->translate(pos.fX, pos.fY);
SkPaint p;
p.setShader(make_shader(lm, outer_lm));
p.setAlphaf(0.75f);
canvas->drawRect(SkRect::MakeWH(kSZ, kSZ), p);
p.setAlphaf(1);
canvas->drawTextBlob(fBlob, 0, kFontSZ*1, p);
canvas->drawTextBlob(fBlob, 0, kFontSZ*2, p);
}
static sk_sp<SkShader> make_shader(const SkMatrix& lm, const SkMatrix& outer_lm) {
static constexpr SkISize kTileSize = { 30, 30 };
auto surface = SkSurface::MakeRasterN32Premul(kTileSize.width(), kTileSize.height());
SkPaint p;
p.setColor(0xffffff00);
surface->getCanvas()->drawPaint(p);
p.setColor(0xff008000);
surface->getCanvas()
->drawRect({0, 0, kTileSize.width()*0.9f, kTileSize.height()*0.9f}, p);
return surface->makeImageSnapshot()
Reland "drawGlyphRunRSXform: post-compose shader local matrix adjustments" This reverts commit d150a58c04ed492be2e5812e5c9a67450cfb42e9. Reason for revert: hopefully not to culprit for roller issues Original change's description: > Revert "drawGlyphRunRSXform: post-compose shader local matrix adjustments" > > This reverts commit ff49f92e26ebd2ac09efe46846f75daf711e9ef4. > > Reason for revert: Likely the CL that's making fushcia_x64 grumpy > > Original change's description: > > drawGlyphRunRSXform: post-compose shader local matrix adjustments > > > > Bug: skia:11113 > > Change-Id: Ic34a0c80b44031d77d361d35e64301297b3f3189 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/347047 > > Commit-Queue: Florin Malita <fmalita@chromium.org> > > Commit-Queue: Florin Malita <fmalita@google.com> > > Reviewed-by: Mike Reed <reed@google.com> > > TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com > > Change-Id: Ieb9ac7597f518f4209d07b063b5752a2265244b4 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:11113 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348656 > Reviewed-by: Chris Dalton <csmartdalton@google.com> > Commit-Queue: Chris Dalton <csmartdalton@google.com> TBR=csmartdalton@google.com,fmalita@chromium.org,fmalita@google.com,reed@google.com # Not skipping CQ checks because this is a reland. Bug: skia:11113 Change-Id: I598653d9b79f00d36e7a0f700af92d273bbf6305 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348193 Reviewed-by: Florin Malita <fmalita@google.com> Commit-Queue: Florin Malita <fmalita@google.com>
2020-12-30 13:12:58 +00:00
->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone), &lm)
->makeWithLocalMatrix(outer_lm);
}
static constexpr float kSZ = 300,
kFontSZ = kSZ * 0.38,
kScale = 1.4f;
sk_sp<SkTextBlob> fBlob;
};
DEF_GM(return new RSXShaderGM;)