skia2/gm/bitmapfilters.cpp
caryclark 65cdba6ba7 Revert of Revert of make gm background colors 565 compatible (patchset #1 id:1 of https://codereview.chromium.org/1184123002/)
Reason for revert:
underlying problem with portable refs deleted more than once fixed

Original issue's description:
> Revert of make gm background colors 565 compatible (patchset #2 id:20001 of https://codereview.chromium.org/1176243006/)
>
> Reason for revert:
> breaks many bots with refcnt error
>
> Original issue's description:
> > make gm background colors 565 compatible
> >
> > Change a batch of GM tests to convert their background color
> > so that it is representable in 8888 and 565.
> >
> > Enable portable text in those same tests to minimize platform
> > differences.
> >
> > In a couple of bitmap tests, use portable typefaces instead of
> > choosing 'Times' which may or may not be available on the platform.
> >
> > R=borenet@google.com
> >
> > Committed: https://skia.googlesource.com/skia/+/be7f768a357aefb39c42d24b81b24d647bb6ab70
>
> TBR=borenet@google.com
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
>
> Committed: https://skia.googlesource.com/skia/+/0bdb08b1ba8fbd18c4838f86a23f1ef4b3a3bfdf

TBR=borenet@google.com
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true

Review URL: https://codereview.chromium.org/1182403003
2015-06-15 06:51:08 -07:00

162 lines
4.3 KiB
C++

/*
* 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"
#include "sk_tool_utils.h"
static void make_bm(SkBitmap* bm) {
const SkColor colors[4] = {
SK_ColorRED, SK_ColorGREEN,
SK_ColorBLUE, SK_ColorWHITE
};
SkPMColor colorsPM[4];
for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
colorsPM[i] = SkPreMultiplyColor(colors[i]);
}
SkColorTable* ctable = new SkColorTable(colorsPM, 4);
bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType,
kPremul_SkAlphaType),
NULL, ctable);
ctable->unref();
*bm->getAddr8(0, 0) = 0;
*bm->getAddr8(1, 0) = 1;
*bm->getAddr8(0, 1) = 2;
*bm->getAddr8(1, 1) = 3;
}
static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
SkScalar x, SkScalar y, SkPaint* paint) {
canvas->drawBitmap(bm, x, y, paint);
return SkIntToScalar(bm.width()) * 5/4;
}
static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
SkPaint* p) {
x += draw_bm(c, bm, x, 0, p);
p->setFilterQuality(kLow_SkFilterQuality);
x += draw_bm(c, bm, x, 0, p);
p->setDither(true);
return x + draw_bm(c, bm, x, 0, p);
}
static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
SkAutoCanvasRestore acr(canvas, true);
SkPaint paint;
SkScalar x = 0;
const int scale = 32;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface_always(&paint);
const char* name = sk_tool_utils::colortype_name(bm.colorType());
canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
paint);
canvas->translate(SkIntToScalar(48), 0);
canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
x += draw_set(canvas, bm, 0, &paint);
paint.reset();
paint.setAlpha(0x80);
draw_set(canvas, bm, x, &paint);
return x * scale / 3;
}
class FilterGM : public skiagm::GM {
void onOnceBeforeDraw() override {
make_bm(&fBM8);
fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType);
fBM8.copyTo(&fBM16, kRGB_565_SkColorType);
fBM8.copyTo(&fBM32, kN32_SkColorType);
}
public:
SkBitmap fBM8, fBM4444, fBM16, fBM32;
FilterGM() {
this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
}
protected:
SkString onShortName() override {
return SkString("bitmapfilters");
}
SkISize onISize() override {
return SkISize::Make(540, 330);
}
void onDraw(SkCanvas* canvas) override {
SkScalar x = SkIntToScalar(10);
SkScalar y = SkIntToScalar(10);
canvas->translate(x, y);
y = draw_row(canvas, fBM8);
canvas->translate(0, y);
y = draw_row(canvas, fBM4444);
canvas->translate(0, y);
y = draw_row(canvas, fBM16);
canvas->translate(0, y);
draw_row(canvas, fBM32);
}
private:
typedef skiagm::GM INHERITED;
};
DEF_GM( return new FilterGM; )
//////////////////////////////////////////////////////////////////////////////
class TestExtractAlphaGM : public skiagm::GM {
void onOnceBeforeDraw() override {
// Make a bitmap with per-pixels alpha (stroked circle)
fBitmap.allocN32Pixels(100, 100);
SkCanvas canvas(fBitmap);
canvas.clear(0);
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(SK_ColorBLUE);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(20);
canvas.drawCircle(50, 50, 39, paint);
canvas.flush();
fBitmap.extractAlpha(&fAlpha);
}
public:
SkBitmap fBitmap, fAlpha;
protected:
SkString onShortName() override {
return SkString("extractalpha");
}
SkISize onISize() override {
return SkISize::Make(540, 330);
}
void onDraw(SkCanvas* canvas) override {
SkPaint paint;
paint.setAntiAlias(true);
paint.setFilterQuality(kLow_SkFilterQuality);
paint.setColor(SK_ColorRED);
canvas->drawBitmap(fBitmap, 10, 10, &paint); // should stay blue (ignore paint's color)
canvas->drawBitmap(fAlpha, 120, 10, &paint); // should draw red
}
private:
typedef skiagm::GM INHERITED;
};
DEF_GM( return new TestExtractAlphaGM; )