2013-05-28 15:29:25 +00:00
|
|
|
/*
|
|
|
|
* 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 "SkGradientShader.h"
|
|
|
|
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkImageDecoder.h"
|
2013-05-31 12:50:39 +00:00
|
|
|
#include "SkStream.h"
|
2013-05-28 16:39:52 +00:00
|
|
|
|
|
|
|
static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
|
|
|
|
SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style)));
|
|
|
|
}
|
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
|
2013-05-28 15:42:21 +00:00
|
|
|
SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
|
|
|
|
SkIntToScalar(bm.height()));
|
2013-05-28 15:29:25 +00:00
|
|
|
mat.mapRect(&bounds);
|
2013-05-28 15:42:21 +00:00
|
|
|
return SkSize::Make(bounds.width(), bounds.height());
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat,
|
|
|
|
SkScalar dx) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->drawBitmapMatrix(bm, mat, &paint);
|
|
|
|
|
2013-07-22 17:18:18 +00:00
|
|
|
paint.setFilterLevel(SkPaint::kLow_FilterLevel);
|
2013-05-28 15:29:25 +00:00
|
|
|
canvas->translate(dx, 0);
|
|
|
|
canvas->drawBitmapMatrix(bm, mat, &paint);
|
|
|
|
|
2013-07-22 17:18:18 +00:00
|
|
|
paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
|
2013-05-28 15:29:25 +00:00
|
|
|
canvas->translate(dx, 0);
|
|
|
|
canvas->drawBitmapMatrix(bm, mat, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
class FilterBitmapGM : public skiagm::GM {
|
2013-05-31 12:50:39 +00:00
|
|
|
void onOnceBeforeDraw() {
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
make_bitmap();
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
SkScalar cx = SkScalarHalf(fBM.width());
|
|
|
|
SkScalar cy = SkScalarHalf(fBM.height());
|
2013-05-31 12:50:39 +00:00
|
|
|
SkScalar scale = get_scale();
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
|
|
|
|
fMatrix[0].setScale(scale, scale);
|
|
|
|
fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
SkBitmap fBM;
|
|
|
|
SkMatrix fMatrix[2];
|
2013-05-31 12:50:39 +00:00
|
|
|
SkString fName;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
FilterBitmapGM()
|
|
|
|
{
|
2013-05-28 15:29:25 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
}
|
|
|
|
|
2013-07-09 21:37:14 +00:00
|
|
|
void setName(const char name[]) {
|
2013-05-31 12:50:39 +00:00
|
|
|
fName.set(name);
|
|
|
|
}
|
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
2013-05-31 12:50:39 +00:00
|
|
|
return fName;
|
2013-05-28 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
|
|
|
return SkISize::Make(920, 480);
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
virtual void make_bitmap() = 0;
|
|
|
|
virtual SkScalar get_scale() = 0;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
|
|
|
|
SkSize size = computeSize(fBM, fMatrix[i]);
|
|
|
|
size.fWidth += 20;
|
|
|
|
size.fHeight += 20;
|
|
|
|
|
|
|
|
draw_col(canvas, fBM, fMatrix[i], size.fWidth);
|
|
|
|
canvas->translate(0, size.fHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
class FilterBitmapTextGM: public FilterBitmapGM {
|
|
|
|
public:
|
2013-07-09 21:37:14 +00:00
|
|
|
FilterBitmapTextGM(float textSize)
|
|
|
|
: fTextSize(textSize)
|
2013-05-31 12:50:39 +00:00
|
|
|
{
|
|
|
|
char name[1024];
|
2013-07-09 21:37:14 +00:00
|
|
|
sprintf(name, "filterbitmap_text_%.2fpt", fTextSize);
|
|
|
|
setName(name);
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
protected:
|
|
|
|
float fTextSize;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
SkScalar get_scale() SK_OVERRIDE {
|
|
|
|
return 32.f/fTextSize;
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
void make_bitmap() SK_OVERRIDE {
|
2013-05-31 12:58:29 +00:00
|
|
|
fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTextSize * 6));
|
2013-05-31 12:50:39 +00:00
|
|
|
fBM.allocPixels();
|
|
|
|
SkCanvas canvas(fBM);
|
|
|
|
canvas.drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setTextSize(fTextSize);
|
|
|
|
|
|
|
|
setTypeface(&paint, "Times", SkTypeface::kNormal);
|
2013-05-31 12:58:29 +00:00
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
|
2013-05-31 12:50:39 +00:00
|
|
|
setTypeface(&paint, "Times", SkTypeface::kBold);
|
2013-05-31 12:58:29 +00:00
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
|
2013-05-31 12:50:39 +00:00
|
|
|
setTypeface(&paint, "Times", SkTypeface::kItalic);
|
2013-05-31 12:58:29 +00:00
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
|
2013-05-31 12:50:39 +00:00
|
|
|
setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
|
2013-05-31 12:58:29 +00:00
|
|
|
canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
private:
|
2013-05-31 12:50:39 +00:00
|
|
|
typedef FilterBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FilterBitmapCheckerboardGM: public FilterBitmapGM {
|
|
|
|
public:
|
2013-07-09 21:37:14 +00:00
|
|
|
FilterBitmapCheckerboardGM(int size, int num_checks)
|
|
|
|
: fSize(size), fNumChecks(num_checks)
|
2013-05-31 12:50:39 +00:00
|
|
|
{
|
|
|
|
char name[1024];
|
2013-07-09 21:37:14 +00:00
|
|
|
sprintf(name, "filterbitmap_checkerboard_%d_%d", fSize, fNumChecks);
|
|
|
|
setName(name);
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
protected:
|
|
|
|
int fSize;
|
|
|
|
int fNumChecks;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
SkScalar get_scale() SK_OVERRIDE {
|
|
|
|
return 192.f/fSize;
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
void make_bitmap() SK_OVERRIDE {
|
|
|
|
fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize);
|
|
|
|
fBM.allocPixels();
|
|
|
|
SkAutoLockPixels lock(fBM);
|
|
|
|
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) {
|
2013-06-01 07:01:39 +00:00
|
|
|
*s = 0xFFFFFFFF;
|
|
|
|
} else {
|
2013-05-31 12:50:39 +00:00
|
|
|
*s = 0xFF000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
private:
|
2013-05-31 12:50:39 +00:00
|
|
|
typedef FilterBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FilterBitmapImageGM: public FilterBitmapGM {
|
|
|
|
public:
|
2013-07-09 21:37:14 +00:00
|
|
|
FilterBitmapImageGM(const char filename[])
|
|
|
|
: fFilename(filename)
|
2013-05-31 12:50:39 +00:00
|
|
|
{
|
|
|
|
char name[1024];
|
2013-07-09 21:37:14 +00:00
|
|
|
sprintf(name, "filterbitmap_image_%s", filename);
|
|
|
|
setName(name);
|
2013-05-31 12:50:39 +00:00
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
protected:
|
|
|
|
SkString fFilename;
|
|
|
|
int fSize;
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
SkScalar get_scale() SK_OVERRIDE {
|
|
|
|
return 192.f/fSize;
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
void make_bitmap() SK_OVERRIDE {
|
|
|
|
SkString path(skiagm::GM::gResourcePath);
|
2013-07-09 21:37:14 +00:00
|
|
|
path.append("/");
|
2013-05-31 12:50:39 +00:00
|
|
|
path.append(fFilename);
|
|
|
|
|
|
|
|
SkImageDecoder *codec = NULL;
|
|
|
|
SkFILEStream stream(path.c_str());
|
|
|
|
if (stream.isValid()) {
|
|
|
|
codec = SkImageDecoder::Factory(&stream);
|
|
|
|
}
|
|
|
|
if (codec) {
|
|
|
|
stream.rewind();
|
|
|
|
codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config,
|
|
|
|
SkImageDecoder::kDecodePixels_Mode);
|
|
|
|
SkDELETE(codec);
|
|
|
|
} else {
|
|
|
|
fBM.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
|
|
|
|
fBM.allocPixels();
|
|
|
|
*(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
|
|
|
|
}
|
|
|
|
fSize = fBM.height();
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
private:
|
2013-05-31 12:50:39 +00:00
|
|
|
typedef FilterBitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
2013-05-28 15:29:25 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-05-31 12:50:39 +00:00
|
|
|
DEF_GM( return new FilterBitmapTextGM(3); )
|
|
|
|
DEF_GM( return new FilterBitmapTextGM(7); )
|
|
|
|
DEF_GM( return new FilterBitmapTextGM(10); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
|
|
|
|
DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); )
|
|
|
|
DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); )
|