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
|
|
|
|
2015-01-16 13:01:16 +00:00
|
|
|
static void make_checker(SkBitmap* bm, int size, int numChecks) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkString onShortName() SK_OVERRIDE {
|
2013-07-14 01:44:59 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkISize onISize() SK_OVERRIDE {
|
2013-07-14 01:44:59 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2013-07-14 01:44:59 +00:00
|
|
|
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);
|
2014-12-10 17:02:16 +00:00
|
|
|
canvas->concat(matrix);
|
|
|
|
canvas->drawBitmap(fBM, 0, 0, &paint);
|
2013-07-14 01:44:59 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void make_bitmap() SK_OVERRIDE {
|
2015-01-16 13:01:16 +00:00
|
|
|
make_checker(&fBM, fSize, fNumChecks);
|
2013-07-14 01:44:59 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-01-16 13:01:16 +00:00
|
|
|
#include "SkMipMap.h"
|
2015-02-24 17:25:09 +00:00
|
|
|
|
|
|
|
static void release_mipmap(void*, void* context) {
|
|
|
|
((SkMipMap*)context)->unref();
|
|
|
|
}
|
|
|
|
|
2015-01-16 13:01:16 +00:00
|
|
|
class ShowMipLevels : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
SkBitmap fBM;
|
|
|
|
|
|
|
|
ShowMipLevels() {
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
make_checker(&fBM, 512, 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() SK_OVERRIDE {
|
|
|
|
return SkString("showmiplevels");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() SK_OVERRIDE {
|
|
|
|
return SkISize::Make(fBM.width() + 8, 2 * fBM.height() + 80);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
SkScalar x = 4;
|
|
|
|
SkScalar y = 4;
|
|
|
|
canvas->drawBitmap(fBM, x, y, NULL);
|
|
|
|
y += fBM.height() + 4;
|
|
|
|
|
|
|
|
SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(fBM, NULL));
|
|
|
|
|
|
|
|
SkMipMap::Level level;
|
|
|
|
SkScalar scale = 0.5f;
|
|
|
|
while (mm->extractLevel(scale, &level)) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(level.fWidth, level.fHeight);
|
|
|
|
SkBitmap bm;
|
2015-02-24 17:25:09 +00:00
|
|
|
bm.installPixels(info, level.fPixels, level.fRowBytes, NULL,
|
|
|
|
&release_mipmap, (void*)(SkRef(mm.get())));
|
|
|
|
bm.setImmutable();
|
2015-01-16 13:01:16 +00:00
|
|
|
canvas->drawBitmap(bm, x, y, NULL);
|
|
|
|
y += bm.height() + 4;
|
|
|
|
scale /= 2;
|
|
|
|
if (info.width() == 1 || info.height() == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 19:07:07 +00:00
|
|
|
|
2015-01-16 13:01:16 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ShowMipLevels; )
|
|
|
|
|
2013-07-14 01:44:59 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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); )
|