Add GM that draws vertices, atlas, and patch with runtime colorfilter

Make CPU drawVertices and drawAtlas use SkVM if a SkRasterPipeline
blitter creation fails (e.g. due to runtime color filter).

Change-Id: Ib272f58cb729b7047e5c48eba866b435f4e075a3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/469903
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
This commit is contained in:
Brian Salomon 2021-11-11 09:49:54 -05:00 committed by SkCQ
parent 42a59fd147
commit 33c8f05bbd
3 changed files with 285 additions and 166 deletions

View File

@ -11,9 +11,12 @@
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRSXform.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkVertices.h"
#include "include/effects/SkRuntimeEffect.h"
#include "tools/Resources.h"
@ -105,3 +108,92 @@ DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 3, 256 * 2) {
draw_filter(src);
}
}
DEF_SIMPLE_GM(runtimecolorfilter_vertices_atlas_and_patch, canvas, 256, 256) {
constexpr SkRect r = SkRect::MakeWH(128, 128);
// Make a vertices that draws the same as SkRect 'r'.
SkPoint pos[4];
r.toQuad(pos);
constexpr SkColor kColors[] = {SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN, SK_ColorYELLOW};
auto verts = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pos, pos, kColors);
// Make an image from the vertices to do equivalent drawAtlas, drawPatch using an image shader.
auto info = SkImageInfo::Make({128, 128},
kRGBA_8888_SkColorType,
kPremul_SkAlphaType,
canvas->imageInfo().refColorSpace());
auto surf = SkSurface::MakeRaster(info);
surf->getCanvas()->drawVertices(verts, SkPaint());
auto atlas = surf->makeImageSnapshot();
auto xform = SkRSXform::Make(1, 0, 0, 0);
// Make a patch that draws the same as the SkRect 'r'
SkVector vx = pos[1] - pos[0];
SkVector vy = pos[3] - pos[0];
vx.setLength(vx.length()/3.f);
vy.setLength(vy.length()/3.f);
const SkPoint cubics[12] = {
pos[0], pos[0] + vx, pos[1] - vx,
pos[1], pos[1] + vy, pos[2] - vy,
pos[2], pos[2] - vx, pos[3] + vx,
pos[3], pos[3] - vy, pos[0] + vy
};
auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString(gLumaSrc));
if (!effect) {
SkDebugf("%s\n%s\n", gLumaSrc, err.c_str());
}
SkASSERT(effect);
sk_sp<SkColorFilter> colorfilter = effect->makeColorFilter(nullptr);
auto makePaint = [&](bool useCF, bool useShader) {
SkPaint paint;
paint.setColorFilter(useCF ? colorfilter : nullptr);
paint.setShader(useShader ? atlas->makeShader(SkSamplingOptions{}) : nullptr);
return paint;
};
auto drawVertices = [&](float x, bool useCF, bool useShader) {
SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
canvas->translate(x, 0);
// Use just the shader or just the vertex colors.
auto mode = useShader ? SkBlendMode::kDst : SkBlendMode::kSrc;
canvas->drawVertices(verts, mode, makePaint(useCF, useShader));
};
auto drawAtlas = [&](float x, bool useCF) {
SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
canvas->translate(x, 0);
SkPaint paint = makePaint(useCF, /*useShader=*/false);
constexpr SkColor kColor = SK_ColorWHITE;
canvas->drawAtlas(atlas.get(),
&xform,
&r,
&kColor,
1,
SkBlendMode::kModulate,
SkSamplingOptions{},
nullptr,
&paint);
};
auto drawPatch = [&](float x, bool useCF) {
SkAutoCanvasRestore acr(canvas, true);
canvas->translate(x, 0);
SkPaint paint = makePaint(useCF, /*useShader=*/true);
canvas->drawPatch(cubics, nullptr, pos, paint);
};
drawVertices( 0, /*useCF=*/false, /*useShader=*/false);
drawVertices( r.width() + 10, /*useCF=*/ true, /*useShader=*/false);
drawVertices(2*(r.width() + 10), /*useCF=*/ true, /*useShader=*/ true);
canvas->translate(0, r.height() + 10);
drawAtlas( 0, /*useCF=*/false);
drawAtlas(r.width() + 10, /*useCF=*/ true);
canvas->translate(0, r.height() + 10);
drawPatch( 0, /*useCF=*/false);
drawPatch(r.width() + 10, /*useCF=*/ true);
}

View File

@ -105,80 +105,58 @@ void SkDraw::drawAtlas(const SkRSXform xform[],
p.setShader(nullptr);
p.setMaskFilter(nullptr);
if (gUseSkVMBlitter) {
auto updateShader = as_SB(atlasShader)->updatableShader(&alloc);
UpdatableColorShader* colorShader = nullptr;
SkShaderBase* shader = nullptr;
if (colors) {
colorShader = alloc.make<UpdatableColorShader>(fDst.colorSpace());
shader = alloc.make<SkShader_Blend>(
bmode, sk_ref_sp(colorShader), sk_ref_sp(updateShader));
} else {
shader = as_SB(updateShader);
}
p.setShader(sk_ref_sp(shader));
if (auto blitter = SkVMBlitter::Make(fDst, p,*fMatrixProvider, &alloc, fRC->clipShader())) {
SkPath scratchPath;
for (int i = 0; i < count; ++i) {
if (colorShader) {
colorShader->updateColor(colors[i]);
}
auto rpblit = [&]() {
SkRasterPipeline pipeline(&alloc);
SkStageRec rec = {&pipeline,
&alloc,
fDst.colorType(),
fDst.colorSpace(),
p,
nullptr,
*fMatrixProvider};
SkStageUpdater* updator = as_SB(atlasShader.get())->appendUpdatableStages(rec);
if (!updator) {
SkDraw draw(*this);
p.setShader(atlasShader);
for (int i = 0; i < count; ++i) {
if (colors) {
p.setShader(SkShaders::Blend(bmode, SkShaders::Color(colors[i]), atlasShader));
}
SkMatrix mx;
mx.setRSXform(xform[i]);
mx.preTranslate(-textures[i].fLeft, -textures[i].fTop);
mx.postConcat(fMatrixProvider->localToDevice());
if (updateShader->update(mx)) {
fill_rect(mx, *fRC, textures[i], blitter, &scratchPath);
}
SkPreConcatMatrixProvider matrixProvider(*fMatrixProvider, mx);
draw.fMatrixProvider = &matrixProvider;
draw.drawRect(textures[i], p);
}
return;
return true;
}
} // gUseSkVMBlitter
SkRasterPipeline pipeline(&alloc);
SkStageRec rec = {
&pipeline, &alloc, fDst.colorType(), fDst.colorSpace(), p, nullptr, *fMatrixProvider
};
SkRasterPipeline_UniformColorCtx* uniformCtx = nullptr;
SkColorSpaceXformSteps steps(
sk_srgb_singleton(), kUnpremul_SkAlphaType, rec.fDstCS, kUnpremul_SkAlphaType);
SkStageUpdater* updator = as_SB(atlasShader.get())->appendUpdatableStages(rec);
if (!updator) {
SkDraw draw(*this);
p.setShader(atlasShader);
for (int i = 0; i < count; ++i) {
if (colors) {
p.setShader(SkShaders::Blend(bmode, SkShaders::Color(colors[i]), atlasShader));
}
SkMatrix mx;
mx.setRSXform(xform[i]);
mx.preTranslate(-textures[i].fLeft, -textures[i].fTop);
SkPreConcatMatrixProvider matrixProvider(*fMatrixProvider, mx);
draw.fMatrixProvider = &matrixProvider;
draw.drawRect(textures[i], p);
if (colors) {
// we will late-bind the values in ctx, once for each color in the loop
uniformCtx = alloc.make<SkRasterPipeline_UniformColorCtx>();
rec.fPipeline->append(SkRasterPipeline::uniform_color_dst, uniformCtx);
SkBlendMode_AppendStages(bmode, rec.fPipeline);
}
return;
}
SkRasterPipeline_UniformColorCtx* uniformCtx = nullptr;
SkColorSpaceXformSteps steps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
rec.fDstCS, kUnpremul_SkAlphaType);
bool isOpaque = !colors && atlasShader->isOpaque();
if (p.getAlphaf() != 1) {
rec.fPipeline->append(SkRasterPipeline::scale_1_float,
alloc.make<float>(p.getAlphaf()));
isOpaque = false;
}
if (colors) {
// we will late-bind the values in ctx, once for each color in the loop
uniformCtx = alloc.make<SkRasterPipeline_UniformColorCtx>();
rec.fPipeline->append(SkRasterPipeline::uniform_color_dst, uniformCtx);
SkBlendMode_AppendStages(bmode, rec.fPipeline);
}
bool isOpaque = !colors && atlasShader->isOpaque();
if (p.getAlphaf() != 1) {
rec.fPipeline->append(SkRasterPipeline::scale_1_float, alloc.make<float>(p.getAlphaf()));
isOpaque = false;
}
if (auto blitter = SkCreateRasterPipelineBlitter(fDst, p, pipeline, isOpaque, &alloc,
fRC->clipShader())) {
auto blitter = SkCreateRasterPipelineBlitter(
fDst, p, pipeline, isOpaque, &alloc, fRC->clipShader());
if (!blitter) {
return false;
}
SkPath scratchPath;
for (int i = 0; i < count; ++i) {
@ -197,5 +175,37 @@ void SkDraw::drawAtlas(const SkRSXform xform[],
fill_rect(mx, *fRC, textures[i], blitter, &scratchPath);
}
}
return true;
};
if (gUseSkVMBlitter || !rpblit()) {
auto updateShader = as_SB(atlasShader)->updatableShader(&alloc);
UpdatableColorShader* colorShader = nullptr;
SkShaderBase* shader = nullptr;
if (colors) {
colorShader = alloc.make<UpdatableColorShader>(fDst.colorSpace());
shader = alloc.make<SkShader_Blend>(
bmode, sk_ref_sp(colorShader), sk_ref_sp(updateShader));
} else {
shader = as_SB(updateShader);
}
p.setShader(sk_ref_sp(shader));
if (auto blitter = SkVMBlitter::Make(fDst, p, *fMatrixProvider, &alloc,
fRC->clipShader())) {
SkPath scratchPath;
for (int i = 0; i < count; ++i) {
if (colorShader) {
colorShader->updateColor(colors[i]);
}
SkMatrix mx;
mx.setRSXform(xform[i]);
mx.preTranslate(-textures[i].fLeft, -textures[i].fTop);
mx.postConcat(fMatrixProvider->localToDevice());
if (updateShader->update(mx)) {
fill_rect(mx, *fRC, textures[i], blitter, &scratchPath);
}
}
}
}
}

View File

@ -318,8 +318,9 @@ void SkDraw::drawFixedVertices(const SkVertices* vertices, SkBlendMode blendMode
const uint16_t* indices = info.indices();
const SkColor* colors = info.colors();
SkShader* shader = paint.getShader();
if (shader) {
SkShader* paintShader = paint.getShader();
if (paintShader) {
if (!texCoords) {
texCoords = positions;
}
@ -336,85 +337,47 @@ void SkDraw::drawFixedVertices(const SkVertices* vertices, SkBlendMode blendMode
break;
case SkBlendMode::kDst:
texCoords = nullptr;
shader = nullptr;
paintShader = nullptr;
break;
default: break;
}
}
// There is a paintShader iff there is texCoords.
SkASSERT((texCoords != nullptr) == (shader != nullptr));
SkASSERT((texCoords != nullptr) == (paintShader != nullptr));
VertState state(vertexCount, indices, indexCount);
VertState::Proc vertProc = state.chooseProc(info.mode());
SkMatrix ctm = fMatrixProvider->localToDevice();
const bool usePerspective = ctm.hasPerspective();
// No colors are changing and no texture coordinates are changing, so no updates between
// triangles are needed. Use SkVM to blit the triangles.
if (gUseSkVMBlitter) {
SkUpdatableShader* texCoordShader = nullptr;
if (texCoords && texCoords != positions) {
texCoordShader = as_SB(shader)->updatableShader(outerAlloc);
shader = texCoordShader;
}
SkTriColorShader* colorShader = nullptr;
auto rpblit = [&]() {
VertState state(vertexCount, indices, indexCount);
VertState::Proc vertProc = state.chooseProc(info.mode());
SkShader* shader = paintShader;
SkTriColorShader* triShader = nullptr;
SkPMColor4f* dstColors = nullptr;
if (colors) {
colorShader = outerAlloc->make<SkTriColorShader>(
compute_is_opaque(colors, vertexCount), usePerspective);
dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), outerAlloc);
triShader = outerAlloc->make<SkTriColorShader>(compute_is_opaque(colors, vertexCount),
usePerspective);
if (shader) {
shader = outerAlloc->make<SkShader_Blend>(
blendMode, sk_ref_sp(colorShader), sk_ref_sp(shader));
blendMode, sk_ref_sp(triShader), sk_ref_sp(shader));
} else {
shader = colorShader;
shader = triShader;
}
}
SkPaint shaderPaint{paint};
SkPaint shaderPaint(paint);
shaderPaint.setShader(sk_ref_sp(shader));
if (auto blitter = SkVMBlitter::Make(
fDst, shaderPaint, *fMatrixProvider, outerAlloc, this->fRC->clipShader())) {
while (vertProc(&state)) {
SkMatrix localM;
if (texCoordShader && !(texture_to_matrix(state, positions, texCoords, &localM)
&& texCoordShader->update(SkMatrix::Concat(ctm, localM)))) {
continue;
}
if (colorShader && !colorShader->update(ctmInverse, positions, dstColors,state.f0,
state.f1, state.f2)) {
continue;
}
fill_triangle(state, blitter, *fRC, dev2, dev3);
if (!texCoords) { // only tricolor shader
auto blitter = SkCreateRasterPipelineBlitter(
fDst, shaderPaint, *fMatrixProvider, outerAlloc, this->fRC->clipShader());
if (!blitter) {
return false;
}
return;
}
}
SkTriColorShader* triShader = nullptr;
SkPMColor4f* dstColors = nullptr;
if (colors) {
dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), outerAlloc);
triShader = outerAlloc->make<SkTriColorShader>(compute_is_opaque(colors, vertexCount),
usePerspective);
if (shader) {
shader = outerAlloc->make<SkShader_Blend>(blendMode,
sk_ref_sp(triShader), sk_ref_sp(shader));
} else {
shader = triShader;
}
}
SkPaint shaderPaint(paint);
shaderPaint.setShader(sk_ref_sp(shader));
if (!texCoords) { // only tricolor shader
if (auto blitter = SkCreateRasterPipelineBlitter(
fDst, shaderPaint, *fMatrixProvider, outerAlloc, this->fRC->clipShader())) {
while (vertProc(&state)) {
if (triShader &&
!triShader->update(ctmInverse, positions, dstColors,
@ -423,36 +386,36 @@ void SkDraw::drawFixedVertices(const SkVertices* vertices, SkBlendMode blendMode
}
fill_triangle(state, blitter, *fRC, dev2, dev3);
}
}
return;
}
SkRasterPipeline pipeline(outerAlloc);
SkStageRec rec = {
&pipeline,
outerAlloc,
fDst.colorType(),
fDst.colorSpace(),
shaderPaint,
nullptr,
*fMatrixProvider
};
if (auto updater = as_SB(shader)->appendUpdatableStages(rec)) {
bool isOpaque = shader->isOpaque();
if (triShader) {
isOpaque = false; // unless we want to walk all the colors, and see if they are
// all opaque (and the blend mode will keep them that way
return true;
}
// Positions as texCoords? The local matrix is always identity, so update once
if (texCoords == positions) {
if (!updater->update(ctm)) {
return;
SkRasterPipeline pipeline(outerAlloc);
SkStageRec rec = {&pipeline,
outerAlloc,
fDst.colorType(),
fDst.colorSpace(),
shaderPaint,
nullptr,
*fMatrixProvider};
if (auto updater = as_SB(shader)->appendUpdatableStages(rec)) {
bool isOpaque = shader->isOpaque();
if (triShader) {
isOpaque = false; // unless we want to walk all the colors, and see if they are
// all opaque (and the blend mode will keep them that way
}
}
if (auto blitter = SkCreateRasterPipelineBlitter(
fDst, shaderPaint, pipeline, isOpaque, outerAlloc, fRC->clipShader())) {
// Positions as texCoords? The local matrix is always identity, so update once
if (texCoords == positions) {
if (!updater->update(ctm)) {
return true;
}
}
auto blitter = SkCreateRasterPipelineBlitter(
fDst, shaderPaint, pipeline, isOpaque, outerAlloc, fRC->clipShader());
if (!blitter) {
return false;
}
while (vertProc(&state)) {
if (triShader && !triShader->update(ctmInverse, positions, dstColors,
state.f0, state.f1, state.f2)) {
@ -466,31 +429,85 @@ void SkDraw::drawFixedVertices(const SkVertices* vertices, SkBlendMode blendMode
fill_triangle(state, blitter, *fRC, dev2, dev3);
}
}
} else {
// must rebuild pipeline for each triangle, to pass in the computed ctm
while (vertProc(&state)) {
if (triShader && !triShader->update(ctmInverse, positions, dstColors,
state.f0, state.f1, state.f2)) {
continue;
}
SkSTArenaAlloc<2048> innerAlloc;
const SkMatrixProvider* matrixProvider = fMatrixProvider;
SkTLazy<SkPreConcatMatrixProvider> preConcatMatrixProvider;
if (texCoords && (texCoords != positions)) {
SkMatrix localM;
if (!texture_to_matrix(state, positions, texCoords, &localM)) {
continue;
}
matrixProvider = preConcatMatrixProvider.init(*matrixProvider, localM);
}
// It'd be nice if we could detect this will fail earlier.
auto blitter = SkCreateRasterPipelineBlitter(
fDst, shaderPaint, *matrixProvider, &innerAlloc, this->fRC->clipShader());
if (!blitter) {
return false;
}
fill_triangle(state, blitter, *fRC, dev2, dev3);
}
}
return true;
};
if (gUseSkVMBlitter || !rpblit()) {
VertState state(vertexCount, indices, indexCount);
VertState::Proc vertProc = state.chooseProc(info.mode());
// No colors are changing and no texture coordinates are changing, so no updates between
// triangles are needed. Use SkVM to blit the triangles.
SkShader* shader = paintShader;
SkUpdatableShader* texCoordShader = nullptr;
if (texCoords && texCoords != positions) {
texCoordShader = as_SB(shader)->updatableShader(outerAlloc);
shader = texCoordShader;
}
SkTriColorShader* colorShader = nullptr;
SkPMColor4f* dstColors = nullptr;
if (colors) {
colorShader = outerAlloc->make<SkTriColorShader>(compute_is_opaque(colors, vertexCount),
usePerspective);
dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), outerAlloc);
if (shader) {
shader = outerAlloc->make<SkShader_Blend>(
blendMode, sk_ref_sp(colorShader), sk_ref_sp(shader));
} else {
shader = colorShader;
}
}
SkPaint shaderPaint{paint};
shaderPaint.setShader(sk_ref_sp(shader));
auto blitter = SkVMBlitter::Make(
fDst, shaderPaint, *fMatrixProvider, outerAlloc, this->fRC->clipShader());
if (!blitter) {
return;
}
} else {
// must rebuild pipeline for each triangle, to pass in the computed ctm
while (vertProc(&state)) {
if (triShader && !triShader->update(ctmInverse, positions, dstColors,
state.f0, state.f1, state.f2)) {
SkMatrix localM;
if (texCoordShader && !(texture_to_matrix(state, positions, texCoords, &localM) &&
texCoordShader->update(SkMatrix::Concat(ctm, localM)))) {
continue;
}
SkSTArenaAlloc<2048> innerAlloc;
const SkMatrixProvider* matrixProvider = fMatrixProvider;
SkTLazy<SkPreConcatMatrixProvider> preConcatMatrixProvider;
if (texCoords && (texCoords != positions)) {
SkMatrix localM;
if (!texture_to_matrix(state, positions, texCoords, &localM)) {
continue;
}
matrixProvider = preConcatMatrixProvider.init(*matrixProvider, localM);
if (colorShader && !colorShader->update(ctmInverse, positions, dstColors,state.f0,
state.f1, state.f2)) {
continue;
}
if (auto blitter = SkCreateRasterPipelineBlitter(
fDst, shaderPaint, *matrixProvider, &innerAlloc, this->fRC->clipShader())) {
fill_triangle(state, blitter, *fRC, dev2, dev3);
}
fill_triangle(state, blitter, *fRC, dev2, dev3);
}
}
}