skia2/gm/downsamplebitmap.cpp
msarett e8597a4dd0 Delete SkImageDecoder
This image decoding implementation has been replaced
by SkCodec in Android.

Additionally, we have replaced uses of SkImageDecoder
in Skia and Google3 with uses of SkCodec.

Now we can delete SkImageDecoder :).

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1820503002
CQ_EXTRA_TRYBOTS=client.skia.compile:Build-Ubuntu-GCC-x86_64-Release-CMake-Trybot,Build-Mac-Clang-x86_64-Release-CMake-Trybot

Committed: https://skia.googlesource.com/skia/+/f799706656f2581c5bf5510d94df3fa17cce1607

Committed: https://skia.googlesource.com/skia/+/5b6e73e0c8282c4d85accbfbcecc6dee84f8a1eb

Committed: https://skia.googlesource.com/skia/+/f037fdebda2a2626e6512d7532063f2cd41a264d

Review URL: https://codereview.chromium.org/1820503002
2016-03-24 10:41:47 -07:00

198 lines
6.1 KiB
C++

/*
* 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"
#include "Resources.h"
#include "SkGradientShader.h"
#include "SkTypeface.h"
#include "SkStream.h"
#include "SkPaint.h"
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;
}
}
}
}
static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
sk_tool_utils::set_portable_typeface(paint, name, style);
}
class DownsampleBitmapGM : public skiagm::GM {
public:
SkBitmap fBM;
SkString fName;
bool fBitmapMade;
SkFilterQuality fFilterQuality;
DownsampleBitmapGM(SkFilterQuality filterQuality)
: fFilterQuality(filterQuality)
{
this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
fBitmapMade = false;
}
const char* filterQualityToString() {
static const char *filterQualityNames[] = {
"none", "low", "medium", "high"
};
return filterQualityNames[fFilterQuality];
}
protected:
SkString onShortName() override {
return fName;
}
SkISize onISize() override {
make_bitmap_wrapper();
return SkISize::Make(fBM.width(), 4 * fBM.height());
}
void make_bitmap_wrapper() {
if (!fBitmapMade) {
fBitmapMade = true;
make_bitmap();
}
}
virtual void make_bitmap() = 0;
void onDraw(SkCanvas* canvas) override {
make_bitmap_wrapper();
int curY = 0;
int curHeight;
float curScale = 1;
do {
SkMatrix matrix;
matrix.setScale( curScale, curScale );
SkPaint paint;
paint.setFilterQuality(fFilterQuality);
canvas->save();
canvas->translate(0, (SkScalar)curY);
canvas->concat(matrix);
canvas->drawBitmap(fBM, 0, 0, &paint);
canvas->restore();
curHeight = (int) (fBM.height() * curScale + 2);
curY += curHeight;
curScale *= 0.75f;
} while (curHeight >= 2 && curY < 4*fBM.height());
}
private:
typedef skiagm::GM INHERITED;
};
class DownsampleBitmapTextGM: public DownsampleBitmapGM {
public:
DownsampleBitmapTextGM(float textSize, SkFilterQuality filterQuality)
: INHERITED(filterQuality), fTextSize(textSize)
{
fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterQualityToString(), fTextSize);
}
protected:
float fTextSize;
void make_bitmap() override {
fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
SkCanvas canvas(fBM);
canvas.drawColor(SK_ColorWHITE);
SkPaint paint;
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTextSize(fTextSize);
setTypeface(&paint, "serif", SkTypeface::kNormal);
canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
setTypeface(&paint, "serif", SkTypeface::kBold);
canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
setTypeface(&paint, "serif", SkTypeface::kItalic);
canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
setTypeface(&paint, "serif", SkTypeface::kBoldItalic);
canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
}
private:
typedef DownsampleBitmapGM INHERITED;
};
class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
public:
DownsampleBitmapCheckerboardGM(int size, int numChecks, SkFilterQuality filterQuality)
: INHERITED(filterQuality), fSize(size), fNumChecks(numChecks)
{
fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterQualityToString(), fSize, fNumChecks);
}
protected:
int fSize;
int fNumChecks;
void make_bitmap() override {
make_checker(&fBM, fSize, fNumChecks);
}
private:
typedef DownsampleBitmapGM INHERITED;
};
class DownsampleBitmapImageGM: public DownsampleBitmapGM {
public:
DownsampleBitmapImageGM(const char filename[], SkFilterQuality filterQuality)
: INHERITED(filterQuality), fFilename(filename)
{
fName.printf("downsamplebitmap_image_%s_%s", this->filterQualityToString(), filename);
}
protected:
SkString fFilename;
int fSize;
void make_bitmap() override {
if (!GetResourceAsBitmap(fFilename.c_str(), &fBM)) {
fBM.allocN32Pixels(1, 1);
fBM.eraseARGB(255, 255, 0 , 0); // red == bad
}
fSize = fBM.height();
}
private:
typedef DownsampleBitmapGM INHERITED;
};
DEF_GM( return new DownsampleBitmapTextGM(72, kHigh_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kHigh_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kHigh_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapTextGM(72, kMedium_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kMedium_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kMedium_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapTextGM(72, kLow_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kLow_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kLow_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapTextGM(72, kNone_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kNone_SkFilterQuality); )
DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kNone_SkFilterQuality); )