| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkImageFilter.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkSamplingOptions.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/effects/SkImageFilters.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
|
| 24 |
#include <utility> |
| 25 |
|
| 26 |
static void make_bm(SkBitmap* bm) { |
| 27 |
bm->allocN32Pixels(100, 100); |
| 28 |
bm->eraseColor(SK_ColorBLUE); |
| 29 |
|
| 30 |
SkCanvas canvas(*bm); |
| 31 |
SkPaint paint; |
| 32 |
paint.setAntiAlias(true); |
| 33 |
paint.setColor(SK_ColorRED); |
| 34 |
canvas.drawCircle(50, 50, 50, paint); |
| 35 |
} |
| 36 |
|
| 37 |
static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip, |
| 38 |
int dx, int dy, sk_sp<SkImageFilter> filter) { |
| 39 |
SkAutoCanvasRestore acr(canvas, true); |
| 40 |
SkPaint paint; |
| 41 |
|
| 42 |
SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx), |
| 43 |
SkIntToScalar(dy), |
| 44 |
SkIntToScalar(bm.width()), |
| 45 |
SkIntToScalar(bm.height())); |
| 46 |
|
| 47 |
paint.setImageFilter(std::move(filter)); |
| 48 |
clipR.inset(5, 5); |
| 49 |
|
| 50 |
canvas->translate(SkIntToScalar(bm.width() + 20), 0); |
| 51 |
|
| 52 |
if (doClip) { |
| 53 |
canvas->save(); |
| 54 |
canvas->clipRect(clipR); |
| 55 |
} |
| 56 |
canvas->drawImage(bm.asImage(), SkIntToScalar(dx), SkIntToScalar(dy), |
| 57 |
SkSamplingOptions(), &paint); |
| 58 |
if (doClip) { |
| 59 |
canvas->restore(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters) |
| 65 |
*/ |
| 66 |
class SpriteBitmapGM : public skiagm::GM { |
| 67 |
public: |
| 68 |
SpriteBitmapGM() {} |
| 69 |
|
| 70 |
protected: |
| 71 |
SkString getName() const override { return SkString("spritebitmap"); } |
| 72 |
|
| 73 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 74 |
|
| 75 |
void onDraw(SkCanvas* canvas) override { |
| 76 |
SkBitmap bm; |
| 77 |
make_bm(&bm); |
| 78 |
|
| 79 |
int dx = 10; |
| 80 |
int dy = 10; |
| 81 |
|
| 82 |
SkScalar sigma = 8; |
| 83 |
sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr)); |
| 84 |
|
| 85 |
draw_1_bitmap(canvas, bm, false, dx, dy, nullptr); |
| 86 |
dy += bm.height() + 20; |
| 87 |
draw_1_bitmap(canvas, bm, false, dx, dy, filter); |
| 88 |
dy += bm.height() + 20; |
| 89 |
draw_1_bitmap(canvas, bm, true, dx, dy, nullptr); |
| 90 |
dy += bm.height() + 20; |
| 91 |
draw_1_bitmap(canvas, bm, true, dx, dy, filter); |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
using INHERITED = GM; |
| 96 |
}; |
| 97 |
DEF_GM( return new SpriteBitmapGM; ) |
| 98 |
|
| 99 |
// b/41322892 : The CPU backend tries to detect when an image draw is landing perfectly on pixel |
| 100 |
// centers, so it can use a faster sprite-blitting path. That code just assumes that ANY translation |
| 101 |
// can be pixel-snapped. Image-shaders used to behave like this, but were fixed long-ago. |
| 102 |
// The correct result here is for ALL rectangles to be averaged to grey, rather than a pixel-snapped |
| 103 |
// black and white checkerboard. |
| 104 |
// |
| 105 |
// This GM now tests that linear filtering is preserved for subpixel translation, across several |
| 106 |
// different possible methods of drawing. |
| 107 |
DEF_SIMPLE_GM_BG(drawimagerect_filter, canvas, 180, 60, SK_ColorWHITE) { |
| 108 |
auto image = ToolUtils::create_checkerboard_image(50, 50, SK_ColorWHITE, SK_ColorBLACK, 1); |
| 109 |
SkSamplingOptions sampling{SkFilterMode::kLinear}; |
| 110 |
|
| 111 |
canvas->translate(5, 5); |
| 112 |
canvas->drawImage(image, 0.5f, 0.5f, sampling); |
| 113 |
|
| 114 |
canvas->translate(60, 0); |
| 115 |
canvas->drawImageRect(image, {0.5f, 0.5f, 50.5f, 50.5f}, sampling); |
| 116 |
|
| 117 |
auto shader = image->makeShader(sampling); |
| 118 |
canvas->translate(60, 0); |
| 119 |
SkPaint paint; |
| 120 |
SkMatrix offset = SkMatrix::Translate(0.5f, 0.5f); |
| 121 |
paint.setShader(image->makeShader(sampling, &offset)); |
| 122 |
canvas->drawRect({0.0f, 0.0f, 50.0f, 50.0f}, paint); |
| 123 |
} |