skia2/gm/bitmapcopy.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

126 lines
3.7 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"
namespace skiagm {
static const char* gColorTypeNames[] = {
"unknown",
"A8",
"565",
"4444",
"8888",
"8888",
"Index8",
};
static const SkColorType gColorTypes[] = {
kRGB_565_SkColorType,
kARGB_4444_SkColorType,
kN32_SkColorType,
};
#define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
static void draw_checks(SkCanvas* canvas, int width, int height) {
SkPaint paint;
paint.setColor(SK_ColorRED);
canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint);
paint.setColor(SK_ColorGREEN);
canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0),
SkIntToScalar(width), SkIntToScalar(height / 2), paint);
paint.setColor(SK_ColorBLUE);
canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2),
SkIntToScalar(width / 2), SkIntToScalar(height), paint);
paint.setColor(SK_ColorYELLOW);
canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2),
SkIntToScalar(width), SkIntToScalar(height), paint);
}
class BitmapCopyGM : public GM {
public:
SkBitmap fDst[NUM_CONFIGS];
BitmapCopyGM() {
this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
}
protected:
virtual SkString onShortName() {
return SkString("bitmapcopy");
}
virtual SkISize onISize() {
return SkISize::Make(540, 330);
}
virtual void onDraw(SkCanvas* canvas) {
SkPaint paint;
SkScalar horizMargin = 10;
SkScalar vertMargin = 10;
SkBitmap src;
src.allocN32Pixels(40, 40);
SkCanvas canvasTmp(src);
draw_checks(&canvasTmp, 40, 40);
for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
src.copyTo(&fDst[i], gColorTypes[i]);
}
canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD));
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface_always(&paint);
SkScalar width = SkIntToScalar(40);
SkScalar height = SkIntToScalar(40);
if (paint.getFontSpacing() > height) {
height = paint.getFontSpacing();
}
for (unsigned i = 0; i < NUM_CONFIGS; i++) {
const char* name = gColorTypeNames[src.colorType()];
SkScalar textWidth = paint.measureText(name, strlen(name));
if (textWidth > width) {
width = textWidth;
}
}
SkScalar horizOffset = width + horizMargin;
SkScalar vertOffset = height + vertMargin;
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
for (unsigned i = 0; i < NUM_CONFIGS; i++) {
canvas->save();
// Draw destination config name
const char* name = gColorTypeNames[fDst[i].colorType()];
SkScalar textWidth = paint.measureText(name, strlen(name));
SkScalar x = (width - textWidth) / SkScalar(2);
SkScalar y = paint.getFontSpacing() / SkScalar(2);
canvas->drawText(name, strlen(name), x, y, paint);
// Draw destination bitmap
canvas->translate(0, vertOffset);
x = (width - 40) / SkScalar(2);
canvas->drawBitmap(fDst[i], x, 0, &paint);
canvas->restore();
canvas->translate(horizOffset, 0);
}
}
private:
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new BitmapCopyGM; }
static GMRegistry reg(MyFactory);
}