2013-07-14 01:44:59 +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"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFilterQuality.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
static const char* kFilterQualityNames[] = { "none", "low", "medium", "high" };
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
struct DownsampleBitmapGM : public skiagm::GM {
|
|
|
|
SkBitmap (*fMakeBitmap)(SkImageInfo);
|
|
|
|
SkString fName;
|
2015-03-16 17:08:34 +00:00
|
|
|
SkFilterQuality fFilterQuality;
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
DownsampleBitmapGM(SkBitmap (*fn)(SkImageInfo), const char* kind, SkFilterQuality fq)
|
|
|
|
: fMakeBitmap(fn)
|
|
|
|
, fName(SkStringPrintf("downsamplebitmap_%s_%s", kind, kFilterQualityNames[fq]))
|
|
|
|
, fFilterQuality(fq)
|
2013-07-14 01:44:59 +00:00
|
|
|
{
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
SkString onShortName() override { return fName; }
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2018-09-25 21:06:12 +00:00
|
|
|
SkBitmap bm = fMakeBitmap(SkImageInfo::MakeN32Premul(1,1)/*whatever*/);
|
|
|
|
return SkISize::Make(bm.width(), 4 * bm.height());
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2018-09-25 21:06:12 +00:00
|
|
|
SkImageInfo info = canvas->imageInfo();
|
|
|
|
if (!info.colorType()) { info = info.makeColorType( kN32_SkColorType); }
|
|
|
|
if (!info.alphaType()) { info = info.makeAlphaType(kPremul_SkAlphaType); }
|
|
|
|
|
|
|
|
SkBitmap bm = fMakeBitmap(info);
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2014-09-03 20:40:52 +00:00
|
|
|
int curY = 0;
|
|
|
|
int curHeight;
|
2013-07-14 01:44:59 +00:00
|
|
|
float curScale = 1;
|
|
|
|
do {
|
|
|
|
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setScale( curScale, curScale );
|
|
|
|
|
|
|
|
SkPaint paint;
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(fFilterQuality);
|
2013-07-14 01:44:59 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2018-09-25 21:06:12 +00:00
|
|
|
canvas->translate(0, (SkScalar)curY);
|
|
|
|
canvas->concat(matrix);
|
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
2013-07-14 01:44:59 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
curHeight = (int) (bm.height() * curScale + 2);
|
2014-09-03 20:40:52 +00:00
|
|
|
curY += curHeight;
|
2013-07-14 01:44:59 +00:00
|
|
|
curScale *= 0.75f;
|
2018-09-25 21:06:12 +00:00
|
|
|
} while (curHeight >= 2 && curY < 4*bm.height());
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
static SkBitmap convert_bitmap_format(SkBitmap src, SkImageInfo info) {
|
|
|
|
SkBitmap dst;
|
2019-10-03 17:26:54 +00:00
|
|
|
dst.allocPixels(info.makeDimensions(src.dimensions()));
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
SkPixmap pm;
|
|
|
|
SkAssertResult(dst.peekPixels(&pm));
|
|
|
|
SkAssertResult(src.readPixels(pm));
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
return dst;
|
|
|
|
}
|
2013-07-14 01:44:59 +00:00
|
|
|
|
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
static SkBitmap make_text(SkImageInfo info) {
|
|
|
|
const SkScalar textSize = 72;
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(info.makeWH(int(textSize * 8), int(textSize * 6)));
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.drawColor(SK_ColorWHITE);
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
SkPaint paint;
|
2019-01-07 14:31:58 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setSize(textSize);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
|
2019-01-07 14:31:58 +00:00
|
|
|
canvas.drawString("Hamburgefons", textSize/2, 1.2f*textSize, font, paint);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold()));
|
2019-01-07 14:31:58 +00:00
|
|
|
canvas.drawString("Hamburgefons", textSize/2, 2.4f*textSize, font, paint);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()));
|
2019-01-07 14:31:58 +00:00
|
|
|
canvas.drawString("Hamburgefons", textSize/2, 3.6f*textSize, font, paint);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::BoldItalic()));
|
2019-01-07 14:31:58 +00:00
|
|
|
canvas.drawString("Hamburgefons", textSize/2, 4.8f*textSize, font, paint);
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_text, "text", kHigh_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_text, "text", kMedium_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_text, "text", kLow_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_text, "text", kNone_SkFilterQuality); )
|
2013-07-25 14:34:13 +00:00
|
|
|
|
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
static SkBitmap make_checkerboard(SkImageInfo info) {
|
|
|
|
const auto size = 512;
|
|
|
|
const auto numChecks = 256;
|
2013-07-25 14:34:13 +00:00
|
|
|
|
2018-09-25 21:06:12 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(size,size);
|
|
|
|
for (int y = 0; y < size; ++y) {
|
|
|
|
for (int x = 0; x < size; ++x) {
|
|
|
|
SkPMColor* s = bm.getAddr32(x, y);
|
|
|
|
int cx = (x * numChecks) / size;
|
|
|
|
int cy = (y * numChecks) / size;
|
|
|
|
if ((cx+cy)%2) {
|
|
|
|
*s = 0xFFFFFFFF;
|
|
|
|
} else {
|
|
|
|
*s = 0xFF000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return convert_bitmap_format(bm, info);
|
|
|
|
}
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kHigh_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kMedium_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kLow_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kNone_SkFilterQuality); )
|
|
|
|
|
|
|
|
|
|
|
|
static SkBitmap make_image(SkImageInfo info) {
|
|
|
|
SkBitmap bm;
|
|
|
|
if (!GetResourceAsBitmap("images/mandrill_512.png", &bm)) {
|
|
|
|
bm.allocN32Pixels(1, 1);
|
|
|
|
bm.eraseARGB(255, 255, 0 , 0); // red == bad
|
|
|
|
}
|
|
|
|
return convert_bitmap_format(bm, info);
|
|
|
|
}
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_image, "image", kHigh_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_image, "image", kMedium_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_image, "image", kLow_SkFilterQuality); )
|
|
|
|
DEF_GM( return new DownsampleBitmapGM(make_image, "image", kNone_SkFilterQuality); )
|