2013-05-28 15:29:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-05-28 15:29:25 +00:00
|
|
|
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkGradientShader.h"
|
2013-05-31 12:50:39 +00:00
|
|
|
#include "SkStream.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "SkTypeface.h"
|
2013-05-28 16:39:52 +00:00
|
|
|
|
2016-05-31 18:42:36 +00:00
|
|
|
static void setTypeface(SkPaint* paint, const char name[], SkFontStyle style) {
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(paint, name, style);
|
2013-05-28 16:39:52 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
|
2013-05-28 15:42:21 +00:00
|
|
|
SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
|
|
|
|
SkIntToScalar(bm.height()));
|
2013-05-28 15:29:25 +00:00
|
|
|
mat.mapRect(&bounds);
|
2013-05-28 15:42:21 +00:00
|
|
|
return SkSize::Make(bounds.width(), bounds.height());
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 17:02:16 +00:00
|
|
|
static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
|
2015-03-16 17:08:34 +00:00
|
|
|
SkFilterQuality lvl) {
|
2013-05-28 15:29:25 +00:00
|
|
|
SkPaint paint;
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(lvl);
|
2013-05-28 15:29:25 +00:00
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2013-12-16 21:02:29 +00:00
|
|
|
canvas->translate(dx, 0);
|
2014-12-10 17:02:16 +00:00
|
|
|
canvas->concat(mat);
|
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
}
|
2013-12-16 21:02:29 +00:00
|
|
|
|
2014-12-10 17:02:16 +00:00
|
|
|
static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
|
2015-03-16 17:08:34 +00:00
|
|
|
draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality);
|
|
|
|
draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality);
|
|
|
|
draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality);
|
|
|
|
draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality);
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class FilterBitmapGM : public skiagm::GM {
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-12-16 21:02:29 +00:00
|
|
|
this->makeBitmap();
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
SkScalar cx = SkScalarHalf(fBM.width());
|
|
|
|
SkScalar cy = SkScalarHalf(fBM.height());
|
2013-12-16 21:02:29 +00:00
|
|
|
SkScalar scale = this->getScale();
|
2013-05-28 15:29:25 +00:00
|
|
|
|
2013-12-16 21:02:29 +00:00
|
|
|
// these two matrices use a scale factor configured by the subclass
|
2013-05-28 15:29:25 +00:00
|
|
|
fMatrix[0].setScale(scale, scale);
|
|
|
|
fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale);
|
2013-12-16 21:02:29 +00:00
|
|
|
|
|
|
|
// up/down scaling mix
|
|
|
|
fMatrix[2].setScale(0.7f, 1.05f);
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
SkBitmap fBM;
|
2013-12-16 21:02:29 +00:00
|
|
|
SkMatrix fMatrix[3];
|
2013-05-31 12:50:39 +00:00
|
|
|
SkString fName;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
FilterBitmapGM()
|
|
|
|
{
|
2015-06-15 13:51:08 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-05-31 12:50:39 +00:00
|
|
|
return fName;
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-12-16 21:02:29 +00:00
|
|
|
return SkISize::Make(1024, 768);
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-12-16 21:02:29 +00:00
|
|
|
virtual void makeBitmap() = 0;
|
|
|
|
virtual SkScalar getScale() = 0;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-05-28 15:29:25 +00:00
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
|
|
|
|
SkSize size = computeSize(fBM, fMatrix[i]);
|
|
|
|
size.fWidth += 20;
|
|
|
|
size.fHeight += 20;
|
|
|
|
|
2013-12-16 21:02:29 +00:00
|
|
|
draw_row(canvas, fBM, fMatrix[i], size.fWidth);
|
2013-05-28 15:29:25 +00:00
|
|
|
canvas->translate(0, size.fHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
class FilterBitmapTextGM: public FilterBitmapGM {
|
|
|
|
public:
|
2013-07-09 21:37:14 +00:00
|
|
|
FilterBitmapTextGM(float textSize)
|
|
|
|
: fTextSize(textSize)
|
2013-05-31 12:50:39 +00:00
|
|
|
{
|
2013-07-25 19:14:37 +00:00
|
|
|
fName.printf("filterbitmap_text_%.2fpt", fTextSize);
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
protected:
|
|
|
|
float fTextSize;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkScalar getScale() override {
|
2013-05-31 12:50:39 +00:00
|
|
|
return 32.f/fTextSize;
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void makeBitmap() override {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
|
2013-05-31 12:50:39 +00:00
|
|
|
SkCanvas canvas(fBM);
|
|
|
|
canvas.drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(fTextSize);
|
|
|
|
|
2017-07-27 14:45:29 +00:00
|
|
|
setTypeface(&paint, "serif", SkFontStyle::Normal());
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas.drawString("Hamburgefons", fTextSize/2, 1.2f*fTextSize, paint);
|
2017-07-27 14:45:29 +00:00
|
|
|
setTypeface(&paint, "serif", SkFontStyle::Bold());
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas.drawString("Hamburgefons", fTextSize/2, 2.4f*fTextSize, paint);
|
2017-07-27 14:45:29 +00:00
|
|
|
setTypeface(&paint, "serif", SkFontStyle::Italic());
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas.drawString("Hamburgefons", fTextSize/2, 3.6f*fTextSize, paint);
|
2017-07-27 14:45:29 +00:00
|
|
|
setTypeface(&paint, "serif", SkFontStyle::BoldItalic());
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas.drawString("Hamburgefons", fTextSize/2, 4.8f*fTextSize, paint);
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
private:
|
2013-05-31 12:50:39 +00:00
|
|
|
typedef FilterBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FilterBitmapCheckerboardGM: public FilterBitmapGM {
|
2015-03-18 00:44:06 +00:00
|
|
|
public:
|
|
|
|
FilterBitmapCheckerboardGM(int size, int num_checks, bool convertToG8 = false)
|
|
|
|
: fSize(size), fNumChecks(num_checks), fConvertToG8(convertToG8)
|
|
|
|
{
|
|
|
|
fName.printf("filterbitmap_checkerboard_%d_%d%s",
|
|
|
|
fSize, fNumChecks, convertToG8 ? "_g8" : "");
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
protected:
|
|
|
|
int fSize;
|
|
|
|
int fNumChecks;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkScalar getScale() override {
|
2013-05-31 12:50:39 +00:00
|
|
|
return 192.f/fSize;
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void makeBitmap() override {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBM.allocN32Pixels(fSize, fSize);
|
2013-05-31 12:50:39 +00:00
|
|
|
for (int y = 0; y < fSize; y ++) {
|
|
|
|
for (int x = 0; x < fSize; x ++) {
|
|
|
|
SkPMColor* s = fBM.getAddr32(x, y);
|
|
|
|
int cx = (x * fNumChecks) / fSize;
|
|
|
|
int cy = (y * fNumChecks) / fSize;
|
|
|
|
if ((cx+cy)%2) {
|
2013-06-01 07:01:39 +00:00
|
|
|
*s = 0xFFFFFFFF;
|
|
|
|
} else {
|
2013-05-31 12:50:39 +00:00
|
|
|
*s = 0xFF000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 00:44:06 +00:00
|
|
|
if (fConvertToG8) {
|
|
|
|
SkBitmap tmp;
|
2017-01-18 16:49:33 +00:00
|
|
|
sk_tool_utils::copy_to_g8(&tmp, fBM);
|
2015-03-18 00:44:06 +00:00
|
|
|
fBM = tmp;
|
|
|
|
}
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2015-03-18 00:44:06 +00:00
|
|
|
private:
|
|
|
|
const bool fConvertToG8;
|
|
|
|
typedef FilterBitmapGM INHERITED;
|
2013-05-31 12:50:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FilterBitmapImageGM: public FilterBitmapGM {
|
2015-03-18 00:44:06 +00:00
|
|
|
public:
|
|
|
|
FilterBitmapImageGM(const char filename[], bool convertToG8 = false)
|
|
|
|
: fFilename(filename), fConvertToG8(convertToG8)
|
|
|
|
{
|
|
|
|
fName.printf("filterbitmap_image_%s%s", filename, convertToG8 ? "_g8" : "");
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-18 00:44:06 +00:00
|
|
|
protected:
|
2013-05-31 12:50:39 +00:00
|
|
|
SkString fFilename;
|
|
|
|
int fSize;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkScalar getScale() override {
|
2013-05-31 12:50:39 +00:00
|
|
|
return 192.f/fSize;
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void makeBitmap() override {
|
2017-12-08 15:21:31 +00:00
|
|
|
SkString resource = SkStringPrintf("images/%s", fFilename.c_str());
|
|
|
|
if (!GetResourceAsBitmap(resource.c_str(), &fBM)) {
|
2016-03-18 19:13:47 +00:00
|
|
|
fBM.allocN32Pixels(1, 1);
|
|
|
|
fBM.eraseARGB(255, 255, 0 , 0); // red == bad
|
|
|
|
}
|
|
|
|
fSize = fBM.height();
|
|
|
|
|
|
|
|
if (fConvertToG8) {
|
|
|
|
SkBitmap tmp;
|
2017-01-18 16:49:33 +00:00
|
|
|
sk_tool_utils::copy_to_g8(&tmp, fBM);
|
2016-03-18 19:13:47 +00:00
|
|
|
fBM = tmp;
|
|
|
|
}
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2015-03-18 00:44:06 +00:00
|
|
|
private:
|
|
|
|
const bool fConvertToG8;
|
|
|
|
typedef FilterBitmapGM INHERITED;
|
2013-05-31 12:50:39 +00:00
|
|
|
};
|
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
DEF_GM( return new FilterBitmapTextGM(3); )
|
|
|
|
DEF_GM( return new FilterBitmapTextGM(7); )
|
|
|
|
DEF_GM( return new FilterBitmapTextGM(10); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
|
2015-03-18 00:44:06 +00:00
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,32, true); )
|
2013-05-31 12:50:39 +00:00
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); )
|
2015-03-18 00:44:06 +00:00
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_64.png", true); )
|
2013-05-31 12:50:39 +00:00
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); )
|
2014-10-27 17:32:18 +00:00
|
|
|
DEF_GM( return new FilterBitmapImageGM("color_wheel.png"); )
|