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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkGradientShader.h"
|
2013-07-14 01:44:59 +00:00
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkStream.h"
|
2013-07-25 14:34:13 +00:00
|
|
|
#include "SkPaint.h"
|
2013-07-14 01:44:59 +00:00
|
|
|
|
|
|
|
static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
|
2014-07-31 12:58:44 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(paint, name, style);
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DownsampleBitmapGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
SkBitmap fBM;
|
|
|
|
SkString fName;
|
|
|
|
bool fBitmapMade;
|
2013-07-25 14:34:13 +00:00
|
|
|
SkPaint::FilterLevel fFilterLevel;
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2013-07-25 14:34:13 +00:00
|
|
|
DownsampleBitmapGM(SkPaint::FilterLevel filterLevel)
|
|
|
|
: fFilterLevel(filterLevel)
|
2013-07-14 01:44:59 +00:00
|
|
|
{
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
fBitmapMade = false;
|
|
|
|
}
|
|
|
|
|
2013-07-25 19:14:37 +00:00
|
|
|
const char* filterLevelToString() {
|
|
|
|
static const char *filterLevelNames[] = {
|
|
|
|
"none", "low", "medium", "high"
|
|
|
|
};
|
|
|
|
return filterLevelNames[fFilterLevel];
|
2013-07-25 14:34:13 +00:00
|
|
|
}
|
2013-07-14 01:44:59 +00:00
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
2014-08-28 21:27:42 +00:00
|
|
|
return kSkipTiled_Flag;
|
2014-04-30 13:20:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 01:44:59 +00:00
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
|
|
|
make_bitmap_wrapper();
|
2014-09-03 20:40:52 +00:00
|
|
|
return SkISize::Make(fBM.width(), 4 * fBM.height());
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap_wrapper() {
|
|
|
|
if (!fBitmapMade) {
|
|
|
|
fBitmapMade = true;
|
|
|
|
make_bitmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void make_bitmap() = 0;
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
make_bitmap_wrapper();
|
|
|
|
|
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;
|
2013-07-25 14:34:13 +00:00
|
|
|
paint.setFilterLevel(fFilterLevel);
|
2013-07-14 01:44:59 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2014-09-03 20:40:52 +00:00
|
|
|
canvas->translate(0, (SkScalar)curY);
|
2013-07-14 01:44:59 +00:00
|
|
|
canvas->drawBitmapMatrix( fBM, matrix, &paint );
|
|
|
|
canvas->restore();
|
|
|
|
|
2014-09-03 20:40:52 +00:00
|
|
|
curHeight = (int) (fBM.height() * curScale + 2);
|
|
|
|
curY += curHeight;
|
2013-07-14 01:44:59 +00:00
|
|
|
curScale *= 0.75f;
|
2014-09-03 20:40:52 +00:00
|
|
|
} while (curHeight >= 2 && curY < 4*fBM.height());
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DownsampleBitmapTextGM: public DownsampleBitmapGM {
|
|
|
|
public:
|
2013-07-25 14:34:13 +00:00
|
|
|
DownsampleBitmapTextGM(float textSize, SkPaint::FilterLevel filterLevel)
|
|
|
|
: INHERITED(filterLevel), fTextSize(textSize)
|
2013-07-14 01:44:59 +00:00
|
|
|
{
|
2013-07-25 19:14:37 +00:00
|
|
|
fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterLevelToString(), fTextSize);
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
float fTextSize;
|
|
|
|
|
|
|
|
virtual void make_bitmap() SK_OVERRIDE {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
|
2013-07-14 01:44:59 +00:00
|
|
|
SkCanvas canvas(fBM);
|
|
|
|
canvas.drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(fTextSize);
|
|
|
|
|
|
|
|
setTypeface(&paint, "Times", SkTypeface::kNormal);
|
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
|
|
|
|
setTypeface(&paint, "Times", SkTypeface::kBold);
|
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
|
|
|
|
setTypeface(&paint, "Times", SkTypeface::kItalic);
|
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
|
|
|
|
setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
|
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef DownsampleBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
|
|
|
|
public:
|
2013-07-25 14:34:13 +00:00
|
|
|
DownsampleBitmapCheckerboardGM(int size, int numChecks, SkPaint::FilterLevel filterLevel)
|
|
|
|
: INHERITED(filterLevel), fSize(size), fNumChecks(numChecks)
|
2013-07-14 01:44:59 +00:00
|
|
|
{
|
2013-07-25 19:14:37 +00:00
|
|
|
fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterLevelToString(), fSize, fNumChecks);
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int fSize;
|
|
|
|
int fNumChecks;
|
|
|
|
|
|
|
|
virtual void make_bitmap() SK_OVERRIDE {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBM.allocN32Pixels(fSize, fSize);
|
2013-07-14 01:44:59 +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) {
|
|
|
|
*s = 0xFFFFFFFF;
|
|
|
|
} else {
|
|
|
|
*s = 0xFF000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef DownsampleBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DownsampleBitmapImageGM: public DownsampleBitmapGM {
|
|
|
|
public:
|
2013-07-25 14:34:13 +00:00
|
|
|
DownsampleBitmapImageGM(const char filename[], SkPaint::FilterLevel filterLevel)
|
|
|
|
: INHERITED(filterLevel), fFilename(filename)
|
2013-07-14 01:44:59 +00:00
|
|
|
{
|
2013-07-25 19:14:37 +00:00
|
|
|
fName.printf("downsamplebitmap_image_%s_%s", this->filterLevelToString(), filename);
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString fFilename;
|
|
|
|
int fSize;
|
|
|
|
|
|
|
|
virtual void make_bitmap() SK_OVERRIDE {
|
2014-06-18 21:32:48 +00:00
|
|
|
SkImageDecoder* codec = NULL;
|
2014-07-01 19:35:49 +00:00
|
|
|
SkString resourcePath = GetResourcePath(fFilename.c_str());
|
2014-06-18 21:32:48 +00:00
|
|
|
SkFILEStream stream(resourcePath.c_str());
|
2013-07-14 01:44:59 +00:00
|
|
|
if (stream.isValid()) {
|
|
|
|
codec = SkImageDecoder::Factory(&stream);
|
|
|
|
}
|
|
|
|
if (codec) {
|
|
|
|
stream.rewind();
|
2014-06-13 00:40:00 +00:00
|
|
|
codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
|
2013-07-14 01:44:59 +00:00
|
|
|
SkDELETE(codec);
|
|
|
|
} else {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBM.allocN32Pixels(1, 1);
|
2013-07-14 01:44:59 +00:00
|
|
|
*(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
|
|
|
|
}
|
|
|
|
fSize = fBM.height();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef DownsampleBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-07-25 14:34:13 +00:00
|
|
|
DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kHigh_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kHigh_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kHigh_FilterLevel); )
|
2014-10-23 21:17:46 +00:00
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
|
|
|
|
SkPaint::kHigh_FilterLevel); )
|
2013-07-25 14:34:13 +00:00
|
|
|
|
|
|
|
DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kMedium_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kMedium_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kMedium_FilterLevel); )
|
2014-10-23 21:17:46 +00:00
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
|
|
|
|
SkPaint::kMedium_FilterLevel); )
|
2013-07-25 14:34:13 +00:00
|
|
|
|
|
|
|
DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kLow_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kLow_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kLow_FilterLevel); )
|
2014-10-23 21:17:46 +00:00
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
|
|
|
|
SkPaint::kLow_FilterLevel); )
|
2013-07-25 14:34:13 +00:00
|
|
|
|
|
|
|
DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kNone_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kNone_FilterLevel); )
|
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kNone_FilterLevel); )
|
2014-10-23 21:17:46 +00:00
|
|
|
DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
|
|
|
|
SkPaint::kNone_FilterLevel); )
|