diff options
author | Brian Salomon <bsalomon@google.com> | 2016-12-16 09:52:16 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2016-12-16 16:58:24 +0000 |
commit | 895274391db8df7357334aec260edca2e1735626 (patch) | |
tree | 6c447a64c6e376d67503ef18e20cfdb655b6a312 /src/gpu/ops/GrLatticeOp.cpp | |
parent | 6b316e9e20d67f0f13021ca62f4270105c6151b1 (diff) |
move src/gpu/batches -> src/gpu/ops
Change-Id: I6410eae41f051ce38bef6f38d670924c3483c325
Reviewed-on: https://skia-review.googlesource.com/6163
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Diffstat (limited to 'src/gpu/ops/GrLatticeOp.cpp')
-rw-r--r-- | src/gpu/ops/GrLatticeOp.cpp | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/src/gpu/ops/GrLatticeOp.cpp b/src/gpu/ops/GrLatticeOp.cpp new file mode 100644 index 0000000000..119032209d --- /dev/null +++ b/src/gpu/ops/GrLatticeOp.cpp @@ -0,0 +1,185 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "GrLatticeOp.h" + +#include "GrDefaultGeoProcFactory.h" +#include "GrMeshDrawOp.h" +#include "GrOpFlushState.h" +#include "GrResourceProvider.h" +#include "SkBitmap.h" +#include "SkLatticeIter.h" +#include "SkRect.h" + +static sk_sp<GrGeometryProcessor> create_gp(bool readsCoverage) { + using namespace GrDefaultGeoProcFactory; + Color color(Color::kAttribute_Type); + Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type); + LocalCoords localCoords(LocalCoords::kHasExplicit_Type); + return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I()); +} + +class NonAALatticeOp final : public GrMeshDrawOp { +public: + DEFINE_OP_CLASS_ID + + static const int kVertsPerRect = 4; + static const int kIndicesPerRect = 6; + + NonAALatticeOp(GrColor color, const SkMatrix& viewMatrix, int imageWidth, int imageHeight, + std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) + : INHERITED(ClassID()) { + Patch& patch = fPatches.push_back(); + patch.fViewMatrix = viewMatrix; + patch.fColor = color; + patch.fIter = std::move(iter); + patch.fDst = dst; + + fImageWidth = imageWidth; + fImageHeight = imageHeight; + + // setup bounds + this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); + } + + const char* name() const override { return "NonAALatticeOp"; } + + SkString dumpInfo() const override { + SkString str; + + for (int i = 0; i < fPatches.count(); ++i) { + str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i, + fPatches[i].fColor, fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop, + fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom); + } + + str.append(DumpPipelineInfo(*this->pipeline())); + str.append(INHERITED::dumpInfo()); + return str; + } + + void computePipelineOptimizations(GrInitInvariantOutput* color, + GrInitInvariantOutput* coverage, + GrBatchToXPOverrides* overrides) const override { + color->setUnknownFourComponents(); + coverage->setKnownSingleComponent(0xff); + } + +private: + void onPrepareDraws(Target* target) const override { + sk_sp<GrGeometryProcessor> gp(create_gp(fOverrides.readsCoverage())); + if (!gp) { + SkDebugf("Couldn't create GrGeometryProcessor\n"); + return; + } + + size_t vertexStride = gp->getVertexStride(); + int patchCnt = fPatches.count(); + int numRects = 0; + for (int i = 0; i < patchCnt; i++) { + numRects += fPatches[i].fIter->numRectsToDraw(); + } + + sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer()); + InstancedHelper helper; + void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, + indexBuffer.get(), kVertsPerRect, kIndicesPerRect, numRects); + if (!vertices || !indexBuffer) { + SkDebugf("Could not allocate vertices\n"); + return; + } + + intptr_t verts = reinterpret_cast<intptr_t>(vertices); + for (int i = 0; i < patchCnt; i++) { + const Patch& patch = fPatches[i]; + + // Apply the view matrix here if it is scale-translate. Otherwise, we need to + // wait until we've created the dst rects. + bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate(); + if (isScaleTranslate) { + patch.fIter->mapDstScaleTranslate(patch.fViewMatrix); + } + + SkRect srcR, dstR; + intptr_t patchVerts = verts; + while (patch.fIter->next(&srcR, &dstR)) { + SkPoint* positions = reinterpret_cast<SkPoint*>(verts); + positions->setRectFan(dstR.fLeft, dstR.fTop, dstR.fRight, dstR.fBottom, + vertexStride); + + // Setup local coords + static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); + SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset); + coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride); + + static const int kColorOffset = sizeof(SkPoint); + GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); + for (int j = 0; j < 4; ++j) { + *vertColor = patch.fColor; + vertColor = (GrColor*)((intptr_t)vertColor + vertexStride); + } + verts += kVertsPerRect * vertexStride; + } + + // If we didn't handle it above, apply the matrix here. + if (!isScaleTranslate) { + SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts); + patch.fViewMatrix.mapPointsWithStride( + positions, vertexStride, kVertsPerRect * patch.fIter->numRectsToDraw()); + } + } + helper.recordDraw(target, gp.get()); + } + + void initBatchTracker(const GrXPOverridesForBatch& overrides) override { + overrides.getOverrideColorIfSet(&fPatches[0].fColor); + fOverrides = overrides; + } + + bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { + NonAALatticeOp* that = t->cast<NonAALatticeOp>(); + if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), + that->bounds(), caps)) { + return false; + } + + SkASSERT(this->fImageWidth == that->fImageWidth && + this->fImageHeight == that->fImageHeight); + + // In the event of two ops, one who can tweak, one who cannot, we just fall back to not + // tweaking. + if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { + fOverrides = that->fOverrides; + } + + fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin()); + this->joinBounds(*that); + return true; + } + + struct Patch { + SkMatrix fViewMatrix; + std::unique_ptr<SkLatticeIter> fIter; + SkRect fDst; + GrColor fColor; + }; + + GrXPOverridesForBatch fOverrides; + int fImageWidth; + int fImageHeight; + SkSTArray<1, Patch, true> fPatches; + + typedef GrMeshDrawOp INHERITED; +}; + +namespace GrLatticeOp { +sk_sp<GrDrawOp> MakeNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth, + int imageHeight, std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) { + return sk_sp<GrDrawOp>( + new NonAALatticeOp(color, viewMatrix, imageWidth, imageHeight, std::move(iter), dst)); +} +}; |