2020-01-31 21:16:39 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This test only works with the GPU backend.
|
|
|
|
|
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "src/gpu/GrBitmapTextureMaker.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
|
|
|
#include "src/gpu/GrSamplerState.h"
|
|
|
|
#include "src/gpu/GrTextureProxy.h"
|
2020-02-13 16:25:11 +00:00
|
|
|
#include "src/gpu/effects/generated/GrConstColorProcessor.h"
|
2020-01-31 21:16:39 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/gpu/TestOps.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrTextureEffect::MakeTexelSubset.
|
|
|
|
*/
|
|
|
|
class TexelSubset : public GpuGM {
|
|
|
|
public:
|
|
|
|
TexelSubset(GrSamplerState::Filter filter, bool upscale) : fFilter(filter), fUpscale(upscale) {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString name("texel_subset");
|
|
|
|
switch (fFilter) {
|
|
|
|
case GrSamplerState::Filter::kNearest:
|
|
|
|
name.append("_nearest");
|
|
|
|
break;
|
|
|
|
case GrSamplerState::Filter::kBilerp:
|
|
|
|
name.append("_bilerp");
|
|
|
|
break;
|
|
|
|
case GrSamplerState::Filter::kMipMap:
|
|
|
|
name.append("_mip_map");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
name.append(fUpscale ? "_up" : "_down");
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2020-02-13 16:25:11 +00:00
|
|
|
static constexpr int kN = GrSamplerState::kWrapModeCount;
|
2020-02-13 19:11:28 +00:00
|
|
|
int w = kTestPad + 2*kN*(kImageSize.width() + 2*kDrawPad + kTestPad);
|
|
|
|
int h = kTestPad + 2*kN*(kImageSize.height() + 2*kDrawPad + kTestPad);
|
2020-01-31 21:16:39 +00:00
|
|
|
return {w, h};
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
GetResourceAsBitmap("images/mandrill_128.png", &fBitmap);
|
|
|
|
// Make the bitmap non-square to detect any width/height confusion.
|
|
|
|
fBitmap.extractSubset(&fBitmap, SkIRect::MakeSize(fBitmap.dimensions()).makeInset(0, 20));
|
|
|
|
SkASSERT(fBitmap.dimensions() == kImageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
|
|
|
|
SkCanvas* canvas, SkString* errorMsg) override {
|
|
|
|
GrMipMapped mipMapped = fFilter == GrSamplerState::Filter::kMipMap &&
|
|
|
|
context->priv().caps()->mipMapSupport()
|
|
|
|
? GrMipMapped::kYes : GrMipMapped::kNo;
|
2020-03-18 14:06:13 +00:00
|
|
|
GrBitmapTextureMaker maker(context, fBitmap, GrImageTexGenPolicy::kDraw);
|
2020-02-28 23:07:32 +00:00
|
|
|
auto view = maker.view(mipMapped);
|
|
|
|
if (!view) {
|
2020-01-31 21:16:39 +00:00
|
|
|
*errorMsg = "Failed to create proxy.";
|
|
|
|
return DrawResult::kFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkIRect texelSubset;
|
|
|
|
// Use a smaller subset when upscaling so that wrap is hit on all sides of the rect we
|
|
|
|
// will draw.
|
|
|
|
if (fUpscale) {
|
|
|
|
texelSubset = SkIRect::MakeXYWH(fBitmap.width()/3 - 1, 2*fBitmap.height()/5 - 1,
|
|
|
|
fBitmap.width()/4 + 2, fBitmap.height()/5 + 2);
|
|
|
|
} else {
|
|
|
|
texelSubset = SkIRect::MakeXYWH( fBitmap.width()/8 - 1, fBitmap.height()/7 - 1,
|
|
|
|
3*fBitmap.width()/5 + 2, 4*fBitmap.height()/5 + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTArray<SkMatrix> textureMatrices;
|
|
|
|
|
|
|
|
SkRect a = SkRect::Make(texelSubset);
|
2020-02-03 15:47:05 +00:00
|
|
|
SkRect b = fUpscale ? a.makeInset (.31f * a.width(), .31f * a.height())
|
2020-01-31 21:16:39 +00:00
|
|
|
: a.makeOutset(.25f * a.width(), .25f * a.height());
|
|
|
|
textureMatrices.push_back().setRectToRect(a, b, SkMatrix::kFill_ScaleToFit);
|
|
|
|
|
|
|
|
b = fUpscale ? a.makeInset (.25f * a.width(), .35f * a.height())
|
|
|
|
: a.makeOutset(.20f * a.width(), .35f * a.height());
|
|
|
|
textureMatrices.push_back().setRectToRect(a, b, SkMatrix::kFill_ScaleToFit);
|
|
|
|
textureMatrices.back().preRotate(45.f, a.centerX(), a.centerY());
|
|
|
|
textureMatrices.back().postSkew(.05f, -.05f);
|
|
|
|
|
|
|
|
SkBitmap subsetBmp;
|
|
|
|
fBitmap.extractSubset(&subsetBmp, texelSubset);
|
|
|
|
subsetBmp.setImmutable();
|
2020-03-18 14:06:13 +00:00
|
|
|
GrBitmapTextureMaker subsetMaker(context, subsetBmp, GrImageTexGenPolicy::kDraw);
|
2020-02-28 23:07:32 +00:00
|
|
|
auto subsetView = subsetMaker.view(mipMapped);
|
2020-01-31 21:16:39 +00:00
|
|
|
|
|
|
|
SkRect localRect = SkRect::Make(fBitmap.bounds()).makeOutset(kDrawPad, kDrawPad);
|
|
|
|
|
|
|
|
auto size = this->onISize();
|
|
|
|
|
|
|
|
SkScalar y = kDrawPad + kTestPad;
|
|
|
|
SkRect drawRect;
|
|
|
|
for (int tm = 0; tm < textureMatrices.count(); ++tm) {
|
2020-02-13 16:25:11 +00:00
|
|
|
for (int my = 0; my < GrSamplerState::kWrapModeCount; ++my) {
|
2020-01-31 21:16:39 +00:00
|
|
|
SkScalar x = kDrawPad + kTestPad;
|
2020-02-13 16:25:11 +00:00
|
|
|
auto wmy = static_cast<GrSamplerState::WrapMode>(my);
|
|
|
|
for (int mx = 0; mx < GrSamplerState::kWrapModeCount; ++mx) {
|
|
|
|
auto wmx = static_cast<GrSamplerState::WrapMode>(mx);
|
|
|
|
|
2020-01-31 21:16:39 +00:00
|
|
|
const auto& caps = *context->priv().caps();
|
2020-02-13 16:25:11 +00:00
|
|
|
|
|
|
|
GrSamplerState sampler(wmx, wmy, fFilter);
|
|
|
|
|
2020-01-31 21:16:39 +00:00
|
|
|
drawRect = localRect.makeOffset(x, y);
|
2020-02-13 16:25:11 +00:00
|
|
|
|
|
|
|
std::unique_ptr<GrFragmentProcessor> fp1;
|
2020-02-14 16:22:52 +00:00
|
|
|
fp1 = GrTextureEffect::MakeSubset(view,
|
|
|
|
fBitmap.alphaType(),
|
|
|
|
textureMatrices[tm],
|
|
|
|
sampler,
|
|
|
|
SkRect::Make(texelSubset),
|
|
|
|
caps);
|
2020-02-13 19:11:28 +00:00
|
|
|
if (!fp1) {
|
|
|
|
continue;
|
2020-02-13 16:25:11 +00:00
|
|
|
}
|
2020-02-13 19:11:28 +00:00
|
|
|
|
2020-01-31 21:16:39 +00:00
|
|
|
// Throw a translate in the local matrix just to test having something other
|
|
|
|
// than identity. Compensate with an offset local rect.
|
|
|
|
static constexpr SkVector kT = {-100, 300};
|
|
|
|
if (auto op = sk_gpu_test::test_ops::MakeRect(context,
|
|
|
|
std::move(fp1),
|
|
|
|
drawRect,
|
|
|
|
localRect.makeOffset(kT),
|
|
|
|
SkMatrix::MakeTrans(-kT))) {
|
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
|
|
|
|
}
|
2020-02-13 16:25:11 +00:00
|
|
|
|
2020-01-31 21:16:39 +00:00
|
|
|
x += localRect.width() + kTestPad;
|
|
|
|
|
2020-02-13 16:25:11 +00:00
|
|
|
// Now draw with a subsetted proxy using fixed function texture sampling
|
|
|
|
// rather than a texture subset as a comparison.
|
|
|
|
drawRect = localRect.makeOffset(x, y);
|
2020-01-31 21:16:39 +00:00
|
|
|
SkMatrix subsetTextureMatrix = SkMatrix::Concat(
|
|
|
|
SkMatrix::MakeTrans(-texelSubset.topLeft()), textureMatrices[tm]);
|
|
|
|
|
2020-02-05 15:45:39 +00:00
|
|
|
auto fp2 = GrTextureEffect::Make(subsetView, fBitmap.alphaType(),
|
|
|
|
subsetTextureMatrix,
|
2020-01-31 21:16:39 +00:00
|
|
|
GrSamplerState(wmx, wmy, fFilter), caps);
|
|
|
|
if (auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(fp2), drawRect,
|
|
|
|
localRect)) {
|
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
|
|
|
|
}
|
2020-02-13 16:25:11 +00:00
|
|
|
|
|
|
|
if (mx < GrSamplerState::kWrapModeCount - 1) {
|
2020-02-02 12:57:38 +00:00
|
|
|
SkScalar midX =
|
|
|
|
SkScalarFloorToScalar(drawRect.right() + kTestPad/2.f) + 0.5f;
|
|
|
|
canvas->drawLine({midX, -1}, {midX, (float)size.fHeight+1}, {});
|
2020-01-31 21:16:39 +00:00
|
|
|
}
|
|
|
|
x += localRect.width() + kTestPad;
|
|
|
|
}
|
2020-02-13 16:25:11 +00:00
|
|
|
if (my < GrSamplerState::kWrapModeCount - 1) {
|
2020-02-02 12:57:38 +00:00
|
|
|
SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f;
|
|
|
|
canvas->drawLine({-1, midY}, {(float)size.fWidth+1, midY}, {});
|
2020-01-31 21:16:39 +00:00
|
|
|
}
|
|
|
|
y += localRect.height() + kTestPad;
|
|
|
|
}
|
|
|
|
if (tm < textureMatrices.count() - 1) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
2020-02-02 12:57:38 +00:00
|
|
|
SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f;
|
|
|
|
canvas->drawLine({-1, midY}, {(float)size.fWidth + 1, midY}, paint);
|
2020-01-31 21:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr SkISize kImageSize = {128, 88};
|
|
|
|
static constexpr SkScalar kDrawPad = 10.f;
|
|
|
|
static constexpr SkScalar kTestPad = 10.f;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
GrSamplerState::Filter fFilter;
|
|
|
|
bool fUpscale;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new TexelSubset(GrSamplerState::Filter::kNearest, false);)
|
|
|
|
DEF_GM(return new TexelSubset(GrSamplerState::Filter::kNearest, true);)
|
|
|
|
DEF_GM(return new TexelSubset(GrSamplerState::Filter::kBilerp , false);)
|
|
|
|
DEF_GM(return new TexelSubset(GrSamplerState::Filter::kBilerp , true);)
|
|
|
|
// It doesn't make sense to have upscaling MIP map.
|
|
|
|
DEF_GM(return new TexelSubset(GrSamplerState::Filter::kMipMap, false);)
|
|
|
|
|
|
|
|
}
|