2015-04-02 18:12:09 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This test only works with the GPU backend.
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrConfig.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
|
|
|
#include "include/private/SkColorData.h"
|
2020-04-15 20:43:00 +00:00
|
|
|
#include "src/core/SkMatrixProvider.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "src/gpu/GrColor.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrFragmentProcessor.h"
|
|
|
|
#include "src/gpu/GrPaint.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
|
|
|
#include "src/gpu/SkGr.h"
|
|
|
|
#include "src/gpu/effects/generated/GrConstColorProcessor.h"
|
|
|
|
#include "src/gpu/ops/GrDrawOp.h"
|
|
|
|
#include "src/gpu/ops/GrFillRectOp.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2020-07-13 13:43:27 +00:00
|
|
|
#include "tools/gpu/TestOps.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <utility>
|
2015-04-02 18:12:09 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
/**
|
2020-07-13 15:39:52 +00:00
|
|
|
* This GM directly exercises GrConstColorProcessor, ModulateRGBA and ModulateAlpha.
|
2015-04-02 18:12:09 +00:00
|
|
|
*/
|
2020-07-13 14:00:16 +00:00
|
|
|
class ColorProcessor : public GpuGM {
|
2015-04-02 18:12:09 +00:00
|
|
|
public:
|
2020-07-13 14:00:16 +00:00
|
|
|
enum class TestMode {
|
|
|
|
kConstColor,
|
|
|
|
kModulateRGBA,
|
|
|
|
kModulateAlpha
|
|
|
|
};
|
|
|
|
|
|
|
|
ColorProcessor(TestMode mode) : fMode(mode) {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
2020-07-13 14:00:16 +00:00
|
|
|
switch (fMode) {
|
|
|
|
case TestMode::kConstColor: return SkString("const_color_processor");
|
|
|
|
case TestMode::kModulateRGBA: return SkString("modulate_rgba");
|
|
|
|
case TestMode::kModulateAlpha: return SkString("modulate_alpha");
|
|
|
|
}
|
|
|
|
SkUNREACHABLE;
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
|
|
|
|
SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
|
2016-03-09 17:50:50 +00:00
|
|
|
fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:36:12 +00:00
|
|
|
void onDraw(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
|
2019-02-07 22:23:36 +00:00
|
|
|
SkCanvas* canvas) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr GrColor kColors[] = {
|
2015-04-02 18:12:09 +00:00
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFFF00FF,
|
|
|
|
0x80000000,
|
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
2020-07-13 13:43:27 +00:00
|
|
|
constexpr GrColor kPaintColors[] = {
|
2015-04-02 18:12:09 +00:00
|
|
|
0xFFFFFFFF,
|
2020-07-13 13:43:27 +00:00
|
|
|
0xFF0000FF,
|
|
|
|
0x80000080,
|
2015-04-02 18:12:09 +00:00
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
|
|
|
SkScalar y = kPad;
|
|
|
|
SkScalar x = kPad;
|
|
|
|
SkScalar maxW = 0;
|
|
|
|
for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
|
|
|
|
for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
|
2020-07-13 14:00:16 +00:00
|
|
|
// translate by x,y for the canvas draws and the test target draws.
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
|
|
|
|
// rect to draw
|
|
|
|
SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
|
|
|
|
|
|
|
|
// Create a base-layer FP for the const color processor to draw on top of.
|
|
|
|
std::unique_ptr<GrFragmentProcessor> baseFP;
|
|
|
|
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
|
|
|
|
GrColorInfo colorInfo;
|
|
|
|
GrFPArgs args(context, SkSimpleMatrixProvider(SkMatrix::I()),
|
|
|
|
kHigh_SkFilterQuality, &colorInfo);
|
|
|
|
baseFP = as_SB(fShader)->asFragmentProcessor(args);
|
|
|
|
} else {
|
|
|
|
baseFP = GrConstColorProcessor::Make(
|
|
|
|
SkPMColor4f::FromBytes_RGBA(kPaintColors[paintType]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layer a color/modulation FP on top of the base layer, using various colors.
|
|
|
|
std::unique_ptr<GrFragmentProcessor> colorFP;
|
|
|
|
switch (fMode) {
|
|
|
|
case TestMode::kConstColor:
|
|
|
|
colorFP = GrConstColorProcessor::Make(
|
|
|
|
SkPMColor4f::FromBytes_RGBA(kColors[procColor]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TestMode::kModulateRGBA:
|
2020-07-13 15:39:52 +00:00
|
|
|
colorFP = GrFragmentProcessor::ModulateRGBA(
|
|
|
|
std::move(baseFP), SkPMColor4f::FromBytes_RGBA(kColors[procColor]));
|
2020-07-13 14:00:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TestMode::kModulateAlpha:
|
2020-07-13 15:30:12 +00:00
|
|
|
colorFP = GrFragmentProcessor::ModulateAlpha(
|
|
|
|
std::move(baseFP), SkPMColor4f::FromBytes_RGBA(kColors[procColor]));
|
2020-07-13 14:00:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the FP tree.
|
|
|
|
if (auto op = sk_gpu_test::test_ops::MakeRect(context,
|
|
|
|
std::move(colorFP),
|
|
|
|
renderRect.makeOffset(x, y),
|
|
|
|
renderRect,
|
|
|
|
SkMatrix::I())) {
|
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw labels for the input to the processor and the processor to the right of
|
|
|
|
// the test rect. The input label appears above the processor label.
|
|
|
|
SkFont labelFont;
|
|
|
|
labelFont.setTypeface(ToolUtils::create_portable_typeface());
|
|
|
|
labelFont.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
labelFont.setSize(10.f);
|
|
|
|
SkPaint labelPaint;
|
|
|
|
labelPaint.setAntiAlias(true);
|
|
|
|
SkString inputLabel("Input: ");
|
|
|
|
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
|
|
|
|
inputLabel.append("gradient");
|
|
|
|
} else {
|
|
|
|
inputLabel.appendf("0x%08x", kPaintColors[paintType]);
|
|
|
|
}
|
|
|
|
SkString procLabel;
|
|
|
|
procLabel.printf("Proc: [0x%08x]", kColors[procColor]);
|
|
|
|
|
|
|
|
SkRect inputLabelBounds;
|
|
|
|
// get the bounds of the text in order to position it
|
|
|
|
labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
|
|
|
|
SkTextEncoding::kUTF8, &inputLabelBounds);
|
|
|
|
canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
|
|
|
|
labelFont, labelPaint);
|
|
|
|
// update the bounds to reflect the offset we used to draw it.
|
|
|
|
inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
|
|
|
|
|
|
|
|
SkRect procLabelBounds;
|
|
|
|
labelFont.measureText(procLabel.c_str(), procLabel.size(),
|
|
|
|
SkTextEncoding::kUTF8, &procLabelBounds);
|
|
|
|
canvas->drawString(procLabel, renderRect.fRight + kPad,
|
|
|
|
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
|
|
|
|
labelFont, labelPaint);
|
|
|
|
procLabelBounds.offset(renderRect.fRight + kPad,
|
|
|
|
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
|
|
|
|
|
|
|
|
labelPaint.setStrokeWidth(0);
|
|
|
|
labelPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(renderRect, labelPaint);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// update x and y for the next test case.
|
|
|
|
SkScalar height = renderRect.height();
|
|
|
|
SkScalar width = std::max(inputLabelBounds.fRight, procLabelBounds.fRight);
|
|
|
|
maxW = std::max(maxW, width);
|
|
|
|
y += height + kPad;
|
|
|
|
if (y + height > kHeight) {
|
|
|
|
y = kPad;
|
|
|
|
x += maxW + kPad;
|
|
|
|
maxW = 0;
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-13 13:43:27 +00:00
|
|
|
// Use this as a way of generating an input FP
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
2020-07-13 14:00:16 +00:00
|
|
|
TestMode fMode;
|
2015-04-02 18:12:09 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr SkScalar kPad = 10.f;
|
|
|
|
static constexpr SkScalar kRectSize = 20.f;
|
|
|
|
static constexpr int kWidth = 820;
|
|
|
|
static constexpr int kHeight = 500;
|
2015-04-02 18:12:09 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-04-02 18:12:09 +00:00
|
|
|
};
|
|
|
|
|
2020-07-13 14:00:16 +00:00
|
|
|
DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kConstColor};)
|
|
|
|
DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kModulateRGBA};)
|
|
|
|
DEF_GM(return new ColorProcessor{ColorProcessor::TestMode::kModulateAlpha};)
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|