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-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
2015-04-02 18:12:09 +00:00
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
2016-10-27 18:47:55 +00:00
|
|
|
#include "GrRenderTargetContextPriv.h"
|
2017-03-07 21:58:08 +00:00
|
|
|
#include "SkGr.h"
|
2015-04-02 18:12:09 +00:00
|
|
|
#include "SkGradientShader.h"
|
2019-04-10 16:06:19 +00:00
|
|
|
#include "effects/generated/GrConstColorProcessor.h"
|
2016-12-16 14:52:16 +00:00
|
|
|
#include "ops/GrDrawOp.h"
|
2018-12-10 17:43:36 +00:00
|
|
|
#include "ops/GrFillRectOp.h"
|
2015-04-02 18:12:09 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrConstColorProcessor.
|
|
|
|
*/
|
2019-02-07 22:23:36 +00:00
|
|
|
class ConstColorProcessor : public GpuGM {
|
2015-04-02 18:12:09 +00:00
|
|
|
public:
|
|
|
|
ConstColorProcessor() {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("const_color_processor");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor kPaintColors[] = {
|
2015-04-02 18:12:09 +00:00
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFFF0000,
|
|
|
|
0x80FF0000,
|
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const char* kModeStrs[] {
|
2015-04-02 18:12:09 +00:00
|
|
|
"kIgnore",
|
|
|
|
"kModulateRGBA",
|
|
|
|
"kModulateA",
|
|
|
|
};
|
|
|
|
GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
|
|
|
|
// translate by x,y for the canvas draws and the test target draws.
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
|
|
|
|
|
|
|
|
// rect to draw
|
|
|
|
SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
|
|
|
|
|
|
|
|
GrPaint grPaint;
|
|
|
|
SkPaint skPaint;
|
|
|
|
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
|
|
|
|
skPaint.setShader(fShader);
|
|
|
|
} else {
|
|
|
|
skPaint.setColor(kPaintColors[paintType]);
|
|
|
|
}
|
2017-10-24 16:52:33 +00:00
|
|
|
SkAssertResult(SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(),
|
|
|
|
skPaint, viewMatrix, &grPaint));
|
2015-04-02 18:12:09 +00:00
|
|
|
|
|
|
|
GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
|
2018-10-16 19:19:28 +00:00
|
|
|
SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(kColors[procColor]);
|
2017-08-11 13:40:37 +00:00
|
|
|
auto fp = GrConstColorProcessor::Make(color, mode);
|
2015-04-02 18:12:09 +00:00
|
|
|
|
2016-06-23 21:07:00 +00:00
|
|
|
grPaint.addColorFragmentProcessor(std::move(fp));
|
2017-05-08 14:43:33 +00:00
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(
|
2018-12-10 17:43:36 +00:00
|
|
|
GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone,
|
|
|
|
viewMatrix, renderRect));
|
2015-04-02 18:12:09 +00:00
|
|
|
|
|
|
|
// 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.
|
2018-12-02 21:04:27 +00:00
|
|
|
SkFont labelFont;
|
2019-03-20 16:12:10 +00:00
|
|
|
labelFont.setTypeface(ToolUtils::create_portable_typeface());
|
2018-12-02 21:04:27 +00:00
|
|
|
labelFont.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
labelFont.setSize(10.f);
|
2015-04-02 18:12:09 +00:00
|
|
|
SkPaint labelPaint;
|
|
|
|
labelPaint.setAntiAlias(true);
|
|
|
|
SkString inputLabel;
|
|
|
|
inputLabel.set("Input: ");
|
|
|
|
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
|
|
|
|
inputLabel.append("gradient");
|
|
|
|
} else {
|
|
|
|
inputLabel.appendf("0x%08x", kPaintColors[paintType]);
|
|
|
|
}
|
|
|
|
SkString procLabel;
|
|
|
|
procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
|
|
|
|
|
|
|
|
SkRect inputLabelBounds;
|
|
|
|
// get the bounds of the text in order to position it
|
2018-12-02 21:04:27 +00:00
|
|
|
labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
|
|
|
|
kUTF8_SkTextEncoding, &inputLabelBounds);
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
|
|
|
|
labelFont, labelPaint);
|
2015-04-02 18:12:09 +00:00
|
|
|
// update the bounds to reflect the offset we used to draw it.
|
|
|
|
inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
|
|
|
|
|
|
|
|
SkRect procLabelBounds;
|
2018-12-02 21:04:27 +00:00
|
|
|
labelFont.measureText(procLabel.c_str(), procLabel.size(),
|
|
|
|
kUTF8_SkTextEncoding, &procLabelBounds);
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(procLabel, renderRect.fRight + kPad,
|
|
|
|
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
|
|
|
|
labelFont, labelPaint);
|
2015-04-02 18:12:09 +00:00
|
|
|
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 = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
|
|
|
|
maxW = SkTMax(maxW, width);
|
|
|
|
y += height + kPad;
|
|
|
|
if (y + height > kHeight) {
|
|
|
|
y = kPad;
|
|
|
|
x += maxW + kPad;
|
|
|
|
maxW = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Use this as a way of generating and input FP
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
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
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ConstColorProcessor;)
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|