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/private/SkTArray.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2020-01-31 21:16:39 +00:00
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
|
|
|
#include "src/gpu/GrSamplerState.h"
|
2020-12-09 21:37:04 +00:00
|
|
|
#include "src/gpu/GrSurfaceDrawContext.h"
|
2021-04-28 16:39:21 +00:00
|
|
|
#include "src/gpu/SkGr.h"
|
2021-07-06 20:28:52 +00:00
|
|
|
#include "src/gpu/effects/GrTextureEffect.h"
|
2020-01-31 21:16:39 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/gpu/TestOps.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2020-07-22 15:18:06 +00:00
|
|
|
using MipmapMode = GrSamplerState::MipmapMode;
|
|
|
|
using Filter = GrSamplerState::Filter;
|
|
|
|
using Wrap = GrSamplerState::WrapMode;
|
|
|
|
|
2020-01-31 21:16:39 +00:00
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrTextureEffect::MakeTexelSubset.
|
|
|
|
*/
|
|
|
|
class TexelSubset : public GpuGM {
|
|
|
|
public:
|
2020-07-22 15:18:06 +00:00
|
|
|
TexelSubset(Filter filter, MipmapMode mm, bool upscale)
|
|
|
|
: fFilter(filter), fMipmapMode(mm), fUpscale(upscale) {
|
2020-01-31 21:16:39 +00:00
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString name("texel_subset");
|
|
|
|
switch (fFilter) {
|
2020-07-22 15:18:06 +00:00
|
|
|
case Filter::kNearest:
|
2020-01-31 21:16:39 +00:00
|
|
|
name.append("_nearest");
|
|
|
|
break;
|
2020-07-22 15:18:06 +00:00
|
|
|
case Filter::kLinear:
|
|
|
|
name.append("_linear");
|
2020-01-31 21:16:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-22 23:26:48 +00:00
|
|
|
switch (fMipmapMode) {
|
|
|
|
case MipmapMode::kNone:
|
|
|
|
break;
|
|
|
|
case MipmapMode::kNearest:
|
|
|
|
name.append("_mipmap_nearest");
|
|
|
|
break;
|
|
|
|
case MipmapMode::kLinear:
|
|
|
|
name.append("_mipmap_linear");
|
|
|
|
break;
|
|
|
|
}
|
2020-01-31 21:16:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:25:17 +00:00
|
|
|
DrawResult onDraw(GrRecordingContext* context, GrSurfaceDrawContext* surfaceDrawContext,
|
2020-01-31 21:16:39 +00:00
|
|
|
SkCanvas* canvas, SkString* errorMsg) override {
|
2020-07-22 15:18:06 +00:00
|
|
|
GrMipmapped mipmapped = (fMipmapMode != MipmapMode::kNone) ? GrMipmapped::kYes
|
|
|
|
: GrMipmapped::kNo;
|
|
|
|
if (mipmapped == GrMipmapped::kYes && !context->priv().caps()->mipmapSupport()) {
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
2021-04-28 16:39:21 +00:00
|
|
|
auto view = std::get<0>(GrMakeCachedBitmapProxyView(context, fBitmap, mipmapped));
|
2020-02-28 23:07:32 +00:00
|
|
|
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());
|
2021-01-15 17:26:22 +00:00
|
|
|
textureMatrices.push_back() = SkMatrix::RectToRect(a, b);
|
2020-01-31 21:16:39 +00:00
|
|
|
|
|
|
|
b = fUpscale ? a.makeInset (.25f * a.width(), .35f * a.height())
|
|
|
|
: a.makeOutset(.20f * a.width(), .35f * a.height());
|
2021-01-15 17:26:22 +00:00
|
|
|
textureMatrices.push_back() = SkMatrix::RectToRect(a, b);
|
2020-01-31 21:16:39 +00:00
|
|
|
textureMatrices.back().preRotate(45.f, a.centerX(), a.centerY());
|
|
|
|
textureMatrices.back().postSkew(.05f, -.05f);
|
|
|
|
|
|
|
|
SkBitmap subsetBmp;
|
|
|
|
fBitmap.extractSubset(&subsetBmp, texelSubset);
|
|
|
|
subsetBmp.setImmutable();
|
2021-04-28 16:39:21 +00:00
|
|
|
auto subsetView = std::get<0>(GrMakeCachedBitmapProxyView(context, subsetBmp, 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-07-22 15:18:06 +00:00
|
|
|
auto wmy = static_cast<Wrap>(my);
|
2020-02-13 16:25:11 +00:00
|
|
|
for (int mx = 0; mx < GrSamplerState::kWrapModeCount; ++mx) {
|
2020-07-22 15:18:06 +00:00
|
|
|
auto wmx = static_cast<Wrap>(mx);
|
2020-02-13 16:25:11 +00:00
|
|
|
|
2020-01-31 21:16:39 +00:00
|
|
|
const auto& caps = *context->priv().caps();
|
2020-02-13 16:25:11 +00:00
|
|
|
|
2020-07-22 15:18:06 +00:00
|
|
|
GrSamplerState sampler(wmx, wmy, fFilter, fMipmapMode);
|
2020-02-13 16:25:11 +00:00
|
|
|
|
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),
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix::Translate(-kT))) {
|
2020-12-11 22:25:17 +00:00
|
|
|
surfaceDrawContext->addDrawOp(std::move(op));
|
2020-01-31 21:16:39 +00:00
|
|
|
}
|
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(
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix::Translate(-texelSubset.topLeft()), textureMatrices[tm]);
|
2020-01-31 21:16:39 +00:00
|
|
|
|
2020-07-22 15:18:06 +00:00
|
|
|
auto fp2 = GrTextureEffect::Make(subsetView,
|
|
|
|
fBitmap.alphaType(),
|
2020-02-05 15:45:39 +00:00
|
|
|
subsetTextureMatrix,
|
2020-07-22 15:18:06 +00:00
|
|
|
sampler,
|
|
|
|
caps);
|
2020-01-31 21:16:39 +00:00
|
|
|
if (auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(fp2), drawRect,
|
|
|
|
localRect)) {
|
2020-12-11 22:25:17 +00:00
|
|
|
surfaceDrawContext->addDrawOp(std::move(op));
|
2020-01-31 21:16:39 +00:00
|
|
|
}
|
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;
|
2020-07-22 15:18:06 +00:00
|
|
|
Filter fFilter;
|
|
|
|
MipmapMode fMipmapMode;
|
2020-01-31 21:16:39 +00:00
|
|
|
bool fUpscale;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2020-01-31 21:16:39 +00:00
|
|
|
};
|
|
|
|
|
2020-07-22 23:26:48 +00:00
|
|
|
DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , false);)
|
|
|
|
DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , true );)
|
|
|
|
DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , false);)
|
|
|
|
DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , true );)
|
2020-01-31 21:16:39 +00:00
|
|
|
// It doesn't make sense to have upscaling MIP map.
|
2020-07-22 23:26:48 +00:00
|
|
|
DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNearest, false);)
|
|
|
|
DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNearest, false);)
|
|
|
|
DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kLinear , false);)
|
|
|
|
DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kLinear , false);)
|
2020-01-31 21:16:39 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|