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.
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrTest.h"
|
|
|
|
#include "effects/GrConstColorProcessor.h"
|
2015-09-28 13:26:28 +00:00
|
|
|
#include "SkGrPriv.h"
|
2015-04-02 18:12:09 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrConstColorProcessor.
|
|
|
|
*/
|
|
|
|
class ConstColorProcessor : public GM {
|
|
|
|
public:
|
|
|
|
ConstColorProcessor() {
|
2015-06-15 13:51:08 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(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) };
|
2015-08-27 14:41:13 +00:00
|
|
|
fShader.reset(SkGradientShader::CreateLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
2015-04-02 18:12:09 +00:00
|
|
|
SkShader::kClamp_TileMode));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == rt) {
|
2015-04-02 18:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context = rt->getContext();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2015-04-02 18:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GrColor kColors[] = {
|
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFFF00FF,
|
|
|
|
0x80000000,
|
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SkColor kPaintColors[] = {
|
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFFF0000,
|
|
|
|
0x80FF0000,
|
|
|
|
0x00000000,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* kModeStrs[] {
|
|
|
|
"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);
|
|
|
|
|
|
|
|
GrTestTarget tt;
|
2015-11-16 19:02:05 +00:00
|
|
|
context->getTestTarget(&tt, rt);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == tt.target()) {
|
2015-04-02 18:12:09 +00:00
|
|
|
SkDEBUGFAIL("Couldn't get Gr test target.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrPaint grPaint;
|
|
|
|
SkPaint skPaint;
|
|
|
|
if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
|
|
|
|
skPaint.setShader(fShader);
|
|
|
|
} else {
|
|
|
|
skPaint.setColor(kPaintColors[paintType]);
|
|
|
|
}
|
2015-09-28 13:26:28 +00:00
|
|
|
SkAssertResult(SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint));
|
2015-04-02 18:12:09 +00:00
|
|
|
|
|
|
|
GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
|
|
|
|
GrColor color = kColors[procColor];
|
|
|
|
SkAutoTUnref<GrFragmentProcessor> fp(GrConstColorProcessor::Create(color, mode));
|
|
|
|
|
|
|
|
GrClip clip;
|
2015-07-09 20:25:02 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
|
2015-08-27 13:30:17 +00:00
|
|
|
pipelineBuilder.addColorFragmentProcessor(fp);
|
2015-04-02 18:12:09 +00:00
|
|
|
|
2015-08-21 17:53:34 +00:00
|
|
|
tt.target()->drawNonAARect(pipelineBuilder,
|
|
|
|
grPaint.getColor(),
|
|
|
|
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.
|
|
|
|
SkPaint labelPaint;
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&labelPaint);
|
2015-04-02 18:12:09 +00:00
|
|
|
labelPaint.setAntiAlias(true);
|
|
|
|
labelPaint.setTextSize(10.f);
|
|
|
|
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
|
|
|
|
labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
|
|
|
|
&inputLabelBounds);
|
|
|
|
canvas->drawText(inputLabel.c_str(), inputLabel.size(),
|
|
|
|
renderRect.fRight + kPad,
|
|
|
|
-inputLabelBounds.fTop, labelPaint);
|
|
|
|
// update the bounds to reflect the offset we used to draw it.
|
|
|
|
inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
|
|
|
|
|
|
|
|
SkRect procLabelBounds;
|
|
|
|
labelPaint.measureText(procLabel.c_str(), procLabel.size(),
|
|
|
|
&procLabelBounds);
|
|
|
|
canvas->drawText(procLabel.c_str(), procLabel.size(),
|
|
|
|
renderRect.fRight + kPad,
|
|
|
|
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
|
|
|
|
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 = 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
|
|
|
|
SkAutoTUnref<SkShader> fShader;
|
|
|
|
|
|
|
|
static const SkScalar kPad;
|
|
|
|
static const SkScalar kRectSize;
|
|
|
|
static const int kWidth = 820;
|
|
|
|
static const int kHeight = 500;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkScalar ConstColorProcessor::kPad = 10.f;
|
|
|
|
const SkScalar ConstColorProcessor::kRectSize = 20.f;
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ConstColorProcessor;)
|
2015-04-02 18:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|