Bitmap should use sampling options

Change-Id: I611969d4707009411f20b74debad7655ac364eb8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/343596
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2020-12-12 09:51:11 -05:00 committed by Skia Commit-Bot
parent 1aa1f5fcba
commit 82abeceef9
28 changed files with 90 additions and 59 deletions

View File

@ -113,7 +113,9 @@ protected:
int w = 40;
int h = 40;
makebm(&fBmp, w, h);
fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
SkSamplingOptions(SkFilterMode::kLinear,
SkMipmapMode::kNone));
int offset = 2;
int count = 0;
for (int j = 0; j < NY; ++j) {
@ -144,7 +146,6 @@ protected:
void onDraw(int loops, SkCanvas* canvas) override {
SkPaint paint;
paint.setAntiAlias(false);
paint.setFilterQuality(kLow_SkFilterQuality);
for (int i = 0; i < loops; ++i) {
for (int j = 0; j < NUM_DRAWS; ++j) {

View File

@ -137,12 +137,11 @@ protected:
SkPaint p;
p.setColor(0xFF000000);
p.setFilterQuality(kLow_SkFilterQuality);
SkPaint p2; // for drawVertices path
p2.setColor(0xFF000000);
p2.setFilterQuality(kLow_SkFilterQuality);
p2.setShader(fAtlas.makeShader());
p2.setShader(fAtlas.makeShader(SkSamplingOptions(SkFilterMode::kLinear,
SkMipmapMode::kNone)));
for (int i = 0; i < loops; ++i, ++fNumSaved) {
if (0 == i % kNumBeforeClear) {

View File

@ -295,7 +295,7 @@ protected:
srcBM.allocN32Pixels(10, 1);
srcBM.eraseColor(0xFF00FF00);
paint.setShader(srcBM.makeShader());
paint.setShader(srcBM.makeShader(SkSamplingOptions()));
}
for (int loop = 0; loop < loops; loop++) {
for (size_t i = 0; i < sizes; i++) {

View File

@ -59,7 +59,8 @@ protected:
draw_into_bitmap(fBitmap);
fPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
fPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
SkSamplingOptions()));
}

View File

@ -209,7 +209,7 @@ static sk_sp<SkShader> make_fuzz_shader(Fuzz* fuzz, int depth) {
if (useMatrix) {
FuzzNiceMatrix(fuzz, &matrix);
}
return bitmap.makeShader(tmX, tmY, useMatrix ? &matrix : nullptr);
return bitmap.makeShader(tmX, tmY, SkSamplingOptions(), useMatrix ? &matrix : nullptr);
case 5:
shader1 = make_fuzz_shader(fuzz, depth - 1); // limit recursion.
FuzzNiceMatrix(fuzz, &matrix);

View File

@ -122,8 +122,8 @@ static sk_sp<SkShader> make_bg_shader() {
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE,
0xCF, 0xCE);
const SkMatrix m = SkMatrix::Scale(SkIntToScalar(6), SkIntToScalar(6));
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(),
SkMatrix::Scale(6, 6));
}
DEF_SIMPLE_GM(aarectmodes, canvas, 640, 480) {

View File

@ -39,11 +39,11 @@ protected:
// Empty bitmap.
fPaints.push_back().setColor(SK_ColorGREEN);
fPaints.back().setShader(emptyBmp.makeShader());
fPaints.back().setShader(emptyBmp.makeShader(SkSamplingOptions()));
// Non-invertible local matrix.
fPaints.push_back().setColor(SK_ColorGREEN);
fPaints.back().setShader(blueBmp.makeShader(&badMatrix));
fPaints.back().setShader(blueBmp.makeShader(SkSamplingOptions(), badMatrix));
}
void onDraw(SkCanvas* canvas) override {

View File

@ -61,9 +61,11 @@ DEF_SIMPLE_GM_BG(bigmatrix, canvas, 50, 50, ToolUtils::color_to_565(0xFF66AA99))
SkMatrix s;
s.reset();
s.setScale(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
paint.setShader(bmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &s));
paint.setShader(bmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
SkSamplingOptions(SkFilterMode::kLinear,
SkMipmapMode::kNone),
s));
paint.setAntiAlias(false);
paint.setFilterQuality(kLow_SkFilterQuality);
rect.setLTRB(pt.fX - small, pt.fY - small, pt.fX + small, pt.fY + small);
canvas->drawRect(rect, paint);
}

View File

@ -86,8 +86,8 @@ private:
for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) {
constexpr SkTileMode kTM = SkTileMode::kRepeat;
bmpPaint.setShader(fBmp.makeShader(kTM, kTM, &lm));
bmpPaint.setFilterQuality(kQualities[q].fQuality);
bmpPaint.setShader(fBmp.makeShader(kTM, kTM,
SkSamplingOptions(kQualities[q].fQuality), lm));
canvas->drawRect(rect, bmpPaint);
canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint);
canvas->translate(250, 0);

View File

@ -91,11 +91,10 @@ protected:
s.setScale(8, 8);
s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
SkPaint paint;
paint.setShader(bmp.makeShader(fMode, fMode, &s));
if (fHQ) {
paint.setFilterQuality(kHigh_SkFilterQuality);
}
paint.setShader(bmp.makeShader(fMode, fMode,
fHQ ? SkSamplingOptions({1.0f/3, 1.0f/3})
: SkSamplingOptions(),
s));
SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
for (int i = 0; i < 3; i++) {

View File

@ -113,7 +113,7 @@ protected:
const SkScalar h = SkIntToScalar(H);
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), m);
SkFont labelFont(ToolUtils::create_portable_typeface());

View File

@ -115,8 +115,10 @@ protected:
SkScalar scale = 11*SK_Scalar1/12;
m.setScale(scale, scale);
}
paint.setShader(getBitmap().makeShader(fMode, fMode, &m));
paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
paint.setShader(getBitmap().makeShader(fMode, fMode,
SkSamplingOptions(fDoFilter ? kLow_SkFilterQuality
: kNone_SkFilterQuality),
m));
canvas->translate(SkIntToScalar(50), SkIntToScalar(50));

View File

@ -75,7 +75,7 @@ static sk_sp<SkShader> make_bg_shader() {
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), m);
}
namespace skiagm {

View File

@ -63,7 +63,7 @@ static sk_sp<SkShader> make_bg_shader(int checkSize) {
SkIntToScalar(checkSize), SkIntToScalar(checkSize));
canvas.drawRect(rect1, paint);
canvas.drawRect(rect0, paint);
return bmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
return bmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
}
class ModeColorFilterGM : public GM {

View File

@ -172,11 +172,10 @@ private:
canvas->translate(fSceneSize * 1.1f, 0);
auto bitmapShader = fBitmap.makeShader(
kTileConfigs[tileMode].tmx,
kTileConfigs[tileMode].tmy,
fUseLocalMatrixWrapper
? nullptr : &localMatrix);
auto bitmapShader = fBitmap.makeShader(kTileConfigs[tileMode].tmx,
kTileConfigs[tileMode].tmy,
SkSamplingOptions(),
fUseLocalMatrixWrapper ? nullptr : &localMatrix);
paint.setShader(fUseLocalMatrixWrapper
? bitmapShader->makeWithLocalMatrix(localMatrix)
: bitmapShader);

View File

@ -14,6 +14,7 @@
#include "include/core/SkPixmap.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkShader.h"
#include "include/core/SkTileMode.h"
struct SkMask;
@ -1114,12 +1115,32 @@ public:
example: https://fiddle.skia.org/c/@Bitmap_peekPixels
*/
bool peekPixels(SkPixmap* pixmap) const;
sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions&,
const SkMatrix* = nullptr) const;
sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions& sampling,
const SkMatrix& localMatrix) const {
return this->makeShader(tmx, tmy, sampling, &localMatrix);
}
sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling,
const SkMatrix* localMatrix = nullptr) const {
return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, localMatrix);
}
sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling,
const SkMatrix& localMatrix) const {
return this->makeShader(sampling, &localMatrix);
}
//#ifdef SK_SUPPORT_LEGACY_IMPLICIT_FILTERQUALITY
sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy,
const SkMatrix* localMatrix = nullptr) const;
// defaults to Clamp in x, and y
sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const;
sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const {
return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, localMatrix);
}
//#endif
/** Asserts if internal values are illegal or inconsistent. Only available if
SK_DEBUG is defined at compile time.
*/

View File

@ -62,10 +62,8 @@ static sk_sp<SkShader> make_bg_shader() {
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC,
0xCC, 0xCC);
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(),
SkMatrix::Scale(6, 6));
}
class AARectsModesView : public Sample {

View File

@ -49,7 +49,8 @@ class AARectView : public Sample {
SkPaint bluePaint;
bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
SkPaint bmpPaint;
bmpPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
bmpPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
SkSamplingOptions()));
bluePaint.setStrokeWidth(3);
bmpPaint.setStrokeWidth(3);

View File

@ -37,7 +37,9 @@ class CameraView : public Sample {
SkRect dst = { -150, -150, 150, 150 };
SkMatrix matrix;
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
fShaders.push_back(bm.makeShader(&matrix));
fShaders.push_back(bm.makeShader(SkSamplingOptions(SkFilterMode::kLinear,
SkMipmapMode::kNone),
matrix));
}
}
this->setBGColor(0xFFDDDDDD);
@ -61,7 +63,6 @@ class CameraView : public Sample {
SkPaint paint;
paint.setAntiAlias(true);
paint.setShader(fShaders[fShaderIndex]);
paint.setFilterQuality(kLow_SkFilterQuality);
SkRect r = { -150, -150, 150, 150 };
canvas->drawRoundRect(r, 30, 30, paint);
}

View File

@ -61,10 +61,8 @@ static sk_sp<SkShader> make_bg_shader() {
*bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC);
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(),
SkMatrix::Scale(6, 6));
}
class HairModesView : public Sample {

View File

@ -35,7 +35,7 @@ static void make_paint(SkPaint* paint, SkTileMode tm) {
SkBitmap bm;
make_bitmap(&bm);
paint->setShader(bm.makeShader(tm, tm));
paint->setShader(bm.makeShader(tm, tm, SkSamplingOptions()));
}
class RepeatTileView : public Sample {

View File

@ -23,7 +23,7 @@ static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
SkColorSetARGB(0, 0, 0, 0),
};
return SkShaders::Blend(SkBlendMode::kDstIn,
bm.makeShader(),
bm.makeShader(SkSamplingOptions()),
SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
SkTileMode::kClamp));
}

View File

@ -21,6 +21,7 @@
#include "src/shaders/SkBitmapProcShader.h"
#include "src/shaders/SkColorShader.h"
#include "src/shaders/SkEmptyShader.h"
#include "src/shaders/SkImageShader.h"
#include "src/shaders/SkPictureShader.h"
#include "src/shaders/SkShaderBase.h"
@ -137,15 +138,22 @@ sk_sp<SkShader> SkShaderBase::makeAsALocalMatrixShader(SkMatrix*) const {
sk_sp<SkShader> SkShaders::Empty() { return sk_make_sp<SkEmptyShader>(); }
sk_sp<SkShader> SkShaders::Color(SkColor color) { return sk_make_sp<SkColorShader>(color); }
sk_sp<SkShader> SkBitmap::makeShader(SkTileMode tmx, SkTileMode tmy,
const SkSamplingOptions& sampling,
const SkMatrix* lm) const {
if (lm && !lm->invert(nullptr)) {
return nullptr;
}
return SkImageShader::Make(SkMakeImageFromRasterBitmap(*this, kIfMutable_SkCopyPixelsMode),
tmx, tmy, &sampling, lm);
}
sk_sp<SkShader> SkBitmap::makeShader(SkTileMode tmx, SkTileMode tmy, const SkMatrix* lm) const {
if (lm && !lm->invert(nullptr)) {
return nullptr;
}
return SkMakeBitmapShader(*this, tmx, tmy, lm, kIfMutable_SkCopyPixelsMode);
}
sk_sp<SkShader> SkBitmap::makeShader(const SkMatrix* lm) const {
return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, lm);
return SkImageShader::Make(SkMakeImageFromRasterBitmap(*this, kIfMutable_SkCopyPixelsMode),
tmx, tmy, nullptr, lm);
}
bool SkShaderBase::appendStages(const SkStageRec& rec) const {

View File

@ -416,7 +416,7 @@ static CanvasTest kCanvasTests[] = {
pts[3].set(0, SkIntToScalar(kHeight));
SkPaint paint;
SkBitmap bitmap(make_n32_bitmap(kWidth, kHeight, 0x05060708));
paint.setShader(bitmap.makeShader());
paint.setShader(bitmap.makeShader(SkSamplingOptions()));
c->drawVertices(
SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts, nullptr),
SkBlendMode::kModulate, paint);

View File

@ -140,7 +140,8 @@ static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
SkIntToScalar(239),
0, 0, SK_Scalar1);
SkPaint paint;
paint.setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &matrix));
paint.setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
SkSamplingOptions(), matrix));
SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
c.drawRect(r, paint);

View File

@ -21,7 +21,7 @@ static void test_bitmap(skiatest::Reporter* reporter) {
bmp.setInfo(info);
// test 1: bitmap without pixel data
auto shader = bmp.makeShader(SkTileMode::kClamp, SkTileMode::kClamp);
auto shader = bmp.makeShader(SkSamplingOptions());
REPORTER_ASSERT(reporter, shader);
REPORTER_ASSERT(reporter, !shader->isOpaque());
@ -29,19 +29,19 @@ static void test_bitmap(skiatest::Reporter* reporter) {
bmp.allocPixels(info);
// test 2: not opaque by default
shader = bmp.makeShader();
shader = bmp.makeShader(SkSamplingOptions());
REPORTER_ASSERT(reporter, shader);
REPORTER_ASSERT(reporter, !shader->isOpaque());
// test 3: explicitly opaque
bmp.setAlphaType(kOpaque_SkAlphaType);
shader = bmp.makeShader();
shader = bmp.makeShader(SkSamplingOptions());
REPORTER_ASSERT(reporter, shader);
REPORTER_ASSERT(reporter, shader->isOpaque());
// test 4: explicitly not opaque
bmp.setAlphaType(kPremul_SkAlphaType);
shader = bmp.makeShader();
shader = bmp.makeShader(SkSamplingOptions());
REPORTER_ASSERT(reporter, shader);
REPORTER_ASSERT(reporter, !shader->isOpaque());
}

View File

@ -45,7 +45,7 @@ DEF_TEST(Shader_isAImage, reporter) {
const SkTileMode tmx = SkTileMode::kRepeat;
const SkTileMode tmy = SkTileMode::kMirror;
auto shader0 = bm.makeShader(tmx, tmy, &localM);
auto shader0 = bm.makeShader(tmx, tmy, SkSamplingOptions(), localM);
auto shader1 = SkImage::MakeFromBitmap(bm)->makeShader(tmx, tmy, SkSamplingOptions(), localM);
check_isaimage(reporter, shader0.get(), W, H, tmx, tmy, localM);

View File

@ -130,7 +130,7 @@ sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
bm.eraseColor(c1);
bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
}
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {