2013-08-21 23:10:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFilterQuality.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2013-08-21 23:10:45 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
namespace {
|
2013-08-21 23:10:45 +00:00
|
|
|
|
|
|
|
// This GM draws a 3x3 grid (with the center element excluded) of rectangles
|
|
|
|
// filled with a bitmap shader. The bitmap shader is transformed so that the
|
|
|
|
// pattern cell is at the center (excluded) region.
|
|
|
|
//
|
|
|
|
// In Repeat and Mirror mode, this tests that the bitmap shader still draws
|
|
|
|
// even though the pattern cell is outside the clip.
|
|
|
|
//
|
|
|
|
// In Clamp mode, this tests that the clamp is handled properly. For PDF,
|
|
|
|
// (and possibly other exported formats) this also "tests" that the image itself
|
|
|
|
// is not stored (well, you'll need to open it up with an external tool to
|
|
|
|
// verify that).
|
|
|
|
|
|
|
|
static SkBitmap create_bitmap() {
|
|
|
|
SkBitmap bmp;
|
2014-01-25 16:46:20 +00:00
|
|
|
bmp.allocN32Pixels(2, 2);
|
2013-08-21 23:10:45 +00:00
|
|
|
uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
|
|
|
|
pixels[0] = SkPreMultiplyColor(SK_ColorRED);
|
|
|
|
pixels[1] = SkPreMultiplyColor(SK_ColorGREEN);
|
|
|
|
pixels[2] = SkPreMultiplyColor(SK_ColorBLACK);
|
|
|
|
pixels[3] = SkPreMultiplyColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar RECT_SIZE = 64;
|
|
|
|
constexpr SkScalar SLIDE_SIZE = 300;
|
2013-08-21 23:10:45 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
class ClippedBitmapShadersGM : public skiagm::GM {
|
2013-08-21 23:10:45 +00:00
|
|
|
public:
|
2019-04-03 14:27:45 +00:00
|
|
|
ClippedBitmapShadersGM(SkTileMode mode, bool hq=false)
|
2013-10-23 16:18:50 +00:00
|
|
|
: fMode(mode), fHQ(hq) {
|
2013-08-21 23:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode fMode;
|
2013-10-23 16:18:50 +00:00
|
|
|
bool fHQ;
|
2013-08-21 23:10:45 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkString onShortName() override {
|
2013-08-21 23:10:45 +00:00
|
|
|
SkString descriptor;
|
|
|
|
switch (fMode) {
|
2019-04-03 14:27:45 +00:00
|
|
|
case SkTileMode::kRepeat:
|
2013-08-21 23:10:45 +00:00
|
|
|
descriptor = "tile";
|
|
|
|
break;
|
2019-04-03 14:27:45 +00:00
|
|
|
case SkTileMode::kMirror:
|
2013-08-21 23:10:45 +00:00
|
|
|
descriptor = "mirror";
|
|
|
|
break;
|
2019-04-03 14:27:45 +00:00
|
|
|
case SkTileMode::kClamp:
|
2013-08-21 23:10:45 +00:00
|
|
|
descriptor = "clamp";
|
|
|
|
break;
|
2019-04-03 14:27:45 +00:00
|
|
|
case SkTileMode::kDecal:
|
|
|
|
descriptor = "decal";
|
|
|
|
break;
|
2013-08-21 23:10:45 +00:00
|
|
|
}
|
|
|
|
descriptor.prepend("clipped-bitmap-shaders-");
|
2013-10-23 16:18:50 +00:00
|
|
|
if (fHQ) {
|
|
|
|
descriptor.append("-hq");
|
|
|
|
}
|
2013-08-21 23:10:45 +00:00
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkISize onISize() override { return {300, 300}; }
|
2013-10-24 07:01:59 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-08-21 23:10:45 +00:00
|
|
|
SkBitmap bmp = create_bitmap();
|
|
|
|
SkMatrix s;
|
|
|
|
s.reset();
|
|
|
|
s.setScale(8, 8);
|
|
|
|
s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
|
2014-04-28 14:55:39 +00:00
|
|
|
SkPaint paint;
|
2019-04-08 17:20:23 +00:00
|
|
|
paint.setShader(bmp.makeShader(fMode, fMode, &s));
|
2013-10-24 07:01:59 +00:00
|
|
|
|
2013-10-23 16:18:50 +00:00
|
|
|
if (fHQ) {
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(kHigh_SkFilterQuality);
|
2013-10-23 16:18:50 +00:00
|
|
|
}
|
2013-08-21 23:10:45 +00:00
|
|
|
|
|
|
|
SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
|
|
|
|
if (i == 1 && j == 1) {
|
|
|
|
continue; // skip center element
|
|
|
|
}
|
|
|
|
SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
|
|
|
|
RECT_SIZE, RECT_SIZE);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(rect);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-08-21 23:10:45 +00:00
|
|
|
};
|
2019-07-18 15:51:19 +00:00
|
|
|
} // namespace
|
2013-08-21 23:10:45 +00:00
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
|
|
|
|
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
|
|
|
|
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
|
2013-10-23 16:18:50 +00:00
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
|
|
|
|
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
|
|
|
|
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )
|