2014-12-29 15:05:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrFragmentProcessor.h"
|
|
|
|
#include "GrCoordTransform.h"
|
2016-06-23 21:07:00 +00:00
|
|
|
#include "GrInvariantOutput.h"
|
2015-09-22 13:41:59 +00:00
|
|
|
#include "effects/GrXfermodeFragmentProcessor.h"
|
2015-11-13 14:54:19 +00:00
|
|
|
#include "glsl/GrGLSLFragmentProcessor.h"
|
2015-11-11 21:06:05 +00:00
|
|
|
#include "glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
#include "glsl/GrGLSLProgramBuilder.h"
|
2015-10-28 14:26:40 +00:00
|
|
|
#include "glsl/GrGLSLProgramDataManager.h"
|
2014-12-29 15:05:27 +00:00
|
|
|
#include "Resources.h"
|
2015-01-07 16:02:28 +00:00
|
|
|
#include "SkReadBuffer.h"
|
2014-12-29 15:05:27 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypeface.h"
|
2015-01-07 16:02:28 +00:00
|
|
|
#include "SkWriteBuffer.h"
|
2014-12-29 15:05:27 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class DCShader : public SkShader {
|
|
|
|
public:
|
|
|
|
DCShader(const SkMatrix& matrix) : fDeviceMatrix(matrix) {}
|
|
|
|
|
2015-01-07 16:02:28 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DCShader);
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void flatten(SkWriteBuffer& buf) const override {
|
2015-01-07 16:02:28 +00:00
|
|
|
buf.writeMatrix(fDeviceMatrix);
|
|
|
|
}
|
2014-12-29 15:05:27 +00:00
|
|
|
|
2016-07-22 18:04:53 +00:00
|
|
|
sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
|
2015-01-26 14:08:52 +00:00
|
|
|
|
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2015-03-26 01:17:31 +00:00
|
|
|
void toString(SkString* str) const override {
|
2015-01-26 14:08:52 +00:00
|
|
|
str->appendf("DCShader: ()");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-29 15:05:27 +00:00
|
|
|
private:
|
|
|
|
const SkMatrix fDeviceMatrix;
|
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> DCShader::CreateProc(SkReadBuffer& buf) {
|
2015-01-07 16:02:28 +00:00
|
|
|
SkMatrix matrix;
|
|
|
|
buf.readMatrix(&matrix);
|
2016-04-03 16:11:13 +00:00
|
|
|
return sk_make_sp<DCShader>(matrix);
|
2015-01-07 16:02:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 15:05:27 +00:00
|
|
|
class DCFP : public GrFragmentProcessor {
|
|
|
|
public:
|
2015-10-06 15:40:50 +00:00
|
|
|
DCFP(const SkMatrix& m) : fDeviceTransform(kDevice_GrCoordSet, m) {
|
2014-12-29 15:05:27 +00:00
|
|
|
this->addCoordTransform(&fDeviceTransform);
|
|
|
|
this->initClassID<DCFP>();
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:57:27 +00:00
|
|
|
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
|
2015-11-13 14:54:19 +00:00
|
|
|
class DCGLFP : public GrGLSLFragmentProcessor {
|
2015-07-22 22:08:53 +00:00
|
|
|
void emitCode(EmitArgs& args) override {
|
2015-11-18 16:01:26 +00:00
|
|
|
GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
|
2016-09-12 16:30:36 +00:00
|
|
|
SkString coords2d = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
|
|
|
|
fragBuilder->codeAppendf("vec2 c = %s;", coords2d.c_str());
|
2015-11-18 16:01:26 +00:00
|
|
|
fragBuilder->codeAppend("vec2 r = mod(c, vec2(20.0));");
|
|
|
|
fragBuilder->codeAppend("vec4 color = vec4(0.5*sin(c.x / 15.0) + 0.5,"
|
|
|
|
"0.5*cos((c.x + c.y) / 15.0) + 0.5,"
|
|
|
|
"(r.x + r.y) / 20.0,"
|
|
|
|
"distance(r, vec2(15.0)) / 20.0 + 0.2);");
|
|
|
|
fragBuilder->codeAppendf("color.rgb *= color.a;"
|
|
|
|
"%s = color * %s;",
|
|
|
|
args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_str());
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
2015-10-28 14:26:40 +00:00
|
|
|
void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override {}
|
2014-12-29 15:05:27 +00:00
|
|
|
};
|
2015-08-26 20:07:48 +00:00
|
|
|
return new DCGLFP;
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* name() const override { return "DCFP"; }
|
2014-12-29 15:05:27 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
|
2014-12-29 15:05:27 +00:00
|
|
|
inout->mulByUnknownFourComponents();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-11-13 19:57:27 +00:00
|
|
|
void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
|
|
|
|
GrProcessorKeyBuilder* b) const override {}
|
2015-08-04 14:59:37 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
|
2014-12-29 15:05:27 +00:00
|
|
|
|
|
|
|
GrCoordTransform fDeviceTransform;
|
|
|
|
};
|
|
|
|
|
2016-07-22 18:04:53 +00:00
|
|
|
sk_sp<GrFragmentProcessor> DCShader::asFragmentProcessor(const AsFPArgs&) const {
|
2016-06-09 15:01:03 +00:00
|
|
|
sk_sp<GrFragmentProcessor> inner(new DCFP(fDeviceMatrix));
|
|
|
|
return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DCShaderGM : public GM {
|
|
|
|
public:
|
|
|
|
DCShaderGM() {
|
2015-06-15 13:51:08 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(0xFFAABBCC));
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
~DCShaderGM() override {
|
2014-12-29 15:05:27 +00:00
|
|
|
for (int i = 0; i < fPrims.count(); ++i) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete fPrims[i];
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-12-29 15:05:27 +00:00
|
|
|
return SkString("dcshader");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(1000, 900); }
|
2014-12-29 15:05:27 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-12-29 15:05:27 +00:00
|
|
|
struct Rect : public Prim {
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
|
2014-12-29 15:05:27 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(0, 0, 50, 50);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Circle : public Prim {
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar radius = 25;
|
2014-12-29 15:05:27 +00:00
|
|
|
canvas->drawCircle(radius, radius, radius, paint);
|
|
|
|
return SkRect::MakeXYWH(0, 0, 2 * radius, 2 * radius);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RRect : public Prim {
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
|
2014-12-29 15:05:27 +00:00
|
|
|
SkRRect rrect;
|
|
|
|
rrect.setRectXY(SkRect::MakeXYWH(0, 0, 50, 50), 10, 10);
|
|
|
|
canvas->drawRRect(rrect, paint);
|
|
|
|
return rrect.getBounds();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DRRect : public Prim {
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
|
2014-12-29 15:05:27 +00:00
|
|
|
SkRRect outerRRect;
|
|
|
|
outerRRect.setRectXY(SkRect::MakeXYWH(0, 0, 50, 50), 5, 5);
|
|
|
|
SkRRect innerRRect;
|
|
|
|
innerRRect.setRectXY(SkRect::MakeXYWH(5, 8, 35, 30), 8, 3);
|
|
|
|
canvas->drawDRRect(outerRRect, innerRRect, paint);
|
|
|
|
return outerRRect.getBounds();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct Path : public Prim {
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
|
2014-12-29 15:05:27 +00:00
|
|
|
SkPath path;
|
|
|
|
path.addCircle(15, 15, 10);
|
|
|
|
path.addOval(SkRect::MakeXYWH(2, 2, 22, 37));
|
|
|
|
path.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
return path.getBounds();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Points : public Prim {
|
|
|
|
Points(SkCanvas::PointMode mode) : fMode(mode) {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
|
2014-12-29 15:05:27 +00:00
|
|
|
SkRandom random;
|
|
|
|
SkPoint points[500];
|
|
|
|
SkRect bounds = SkRect::MakeWH(50, 50);
|
|
|
|
int count = SkToInt(SK_ARRAY_COUNT(points));
|
|
|
|
if (SkCanvas::kPoints_PointMode != fMode) {
|
|
|
|
count = SkTMin(count, 10);
|
|
|
|
}
|
|
|
|
for (int p = 0; p < count; ++p) {
|
|
|
|
points[p].fX = random.nextUScalar1() * bounds.width();
|
|
|
|
points[p].fY = random.nextUScalar1() * bounds.width();
|
|
|
|
}
|
|
|
|
canvas->drawPoints(fMode, count, points, paint);
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
SkCanvas::PointMode fMode;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Text : public Prim {
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect draw(SkCanvas* canvas, const SkPaint& origPaint) override {
|
2014-12-29 15:05:27 +00:00
|
|
|
SkPaint paint = origPaint;
|
|
|
|
paint.setTextSize(30.f);
|
|
|
|
this->setFont(&paint);
|
|
|
|
const char* text = this->text();
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkVector offset = SkVector::Make(10, 10);
|
2014-12-29 15:05:27 +00:00
|
|
|
canvas->drawText(text, strlen(text), offset.fX, offset.fY, paint);
|
|
|
|
SkRect bounds;
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
bounds.offset(offset);
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setFont(SkPaint* paint) {
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(paint);
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* text() const { return "Hello, Skia!"; }
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
fPrims.push_back(new Rect);
|
|
|
|
fPrims.push_back(new Circle);
|
|
|
|
fPrims.push_back(new RRect);
|
|
|
|
fPrims.push_back(new DRRect);
|
|
|
|
fPrims.push_back(new Path);
|
|
|
|
fPrims.push_back(new Points(SkCanvas::kPoints_PointMode));
|
|
|
|
fPrims.push_back(new Points(SkCanvas::kLines_PointMode));
|
|
|
|
fPrims.push_back(new Points(SkCanvas::kPolygon_PointMode));
|
|
|
|
fPrims.push_back(new Text);
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-01-31 15:51:14 +00:00
|
|
|
// This GM exists to test a specific feature of the GPU backend. It does not work with the
|
|
|
|
// sw rasterizer, tile modes, etc.
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == canvas->getGrContext()) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2015-01-31 15:51:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-29 15:05:27 +00:00
|
|
|
SkPaint paint;
|
|
|
|
SkTArray<SkMatrix> devMats;
|
|
|
|
devMats.push_back().reset();
|
|
|
|
devMats.push_back().setRotate(45, 500, 500);
|
|
|
|
devMats.push_back().setRotate(-30, 200, 200);
|
2015-01-21 03:58:36 +00:00
|
|
|
devMats.back().setPerspX(-SK_Scalar1 / 2000);
|
|
|
|
devMats.back().setPerspY(SK_Scalar1 / 1000);
|
2014-12-29 15:05:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
SkTArray<SkMatrix> viewMats;
|
|
|
|
viewMats.push_back().setScale(0.75f, 0.75f);
|
|
|
|
viewMats.push_back().setRotate(45, 50, 50);
|
|
|
|
viewMats.back().postScale(0.5f, 1.1f);
|
|
|
|
|
|
|
|
canvas->translate(10, 20);
|
|
|
|
canvas->save();
|
|
|
|
SkScalar tx = 0, maxTy = 0;
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kW = 900;
|
2014-12-29 15:05:27 +00:00
|
|
|
|
|
|
|
for (int aa = 0; aa < 2; ++aa) {
|
|
|
|
for (int i = 0; i < fPrims.count(); ++i) {
|
|
|
|
for (int j = 0; j < devMats.count(); ++j) {
|
|
|
|
for (int k = 0; k < viewMats.count(); ++k) {
|
2016-03-25 16:08:00 +00:00
|
|
|
paint.setShader(sk_make_sp<DCShader>(devMats[j]));
|
2014-12-29 15:05:27 +00:00
|
|
|
paint.setAntiAlias(SkToBool(aa));
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(viewMats[k]);
|
|
|
|
SkRect bounds = fPrims[i]->draw(canvas, paint);
|
|
|
|
canvas->restore();
|
|
|
|
viewMats[k].mapRect(&bounds);
|
|
|
|
// add margins
|
|
|
|
bounds.fRight += 20;
|
|
|
|
bounds.fBottom += 20;
|
|
|
|
canvas->translate(bounds.fRight, 0);
|
|
|
|
tx += bounds.fRight;
|
|
|
|
maxTy = SkTMax(bounds.fBottom, maxTy);
|
|
|
|
if (tx > kW) {
|
|
|
|
tx = 0;
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, maxTy);
|
|
|
|
canvas->save();
|
|
|
|
maxTy = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Prim {
|
|
|
|
virtual ~Prim() {}
|
|
|
|
virtual SkRect draw(SkCanvas*, const SkPaint&) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkTArray<Prim*> fPrims;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new DCShaderGM;)
|
2014-12-29 15:05:27 +00:00
|
|
|
}
|
|
|
|
#endif
|