2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
2010-12-20 18:26:13 +00:00
|
|
|
#include "SkPicture.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
// effects
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkUnitMappers.h"
|
|
|
|
#include "SkBlurDrawLooper.h"
|
|
|
|
|
|
|
|
static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
|
|
|
|
bm->setConfig(config, w, h);
|
|
|
|
bm->allocPixels();
|
2012-12-06 21:47:40 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkCanvas canvas(*bm);
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
SkUnitMapper* um = NULL;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
um = new SkCosineMapper;
|
|
|
|
// um = new SkDiscreteMapper(12);
|
|
|
|
|
|
|
|
SkAutoUnref au(um);
|
|
|
|
|
|
|
|
paint.setDither(true);
|
|
|
|
paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
|
|
|
|
SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
|
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
|
|
|
|
SkShader::TileMode tmx, SkShader::TileMode tmy) {
|
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
|
|
|
|
paint->setShader(shader)->unref();
|
|
|
|
paint->setFilterBitmap(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const SkBitmap::Config gConfigs[] = {
|
|
|
|
SkBitmap::kARGB_8888_Config,
|
|
|
|
SkBitmap::kRGB_565_Config,
|
|
|
|
};
|
|
|
|
static const int gWidth = 32;
|
|
|
|
static const int gHeight = 32;
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
class TilingView : public SampleView {
|
2011-09-13 18:15:52 +00:00
|
|
|
SkPicture* fTextPicture;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBlurDrawLooper fLooper;
|
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
TilingView()
|
2008-12-17 15:59:43 +00:00
|
|
|
: fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
|
|
|
|
0x88000000) {
|
2011-09-13 18:15:52 +00:00
|
|
|
fTextPicture = new SkPicture();
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-13 18:15:52 +00:00
|
|
|
~TilingView() {
|
|
|
|
fTextPicture->unref();
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Tiling");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
|
|
|
|
|
|
|
|
static const char* gConfigNames[] = { "8888", "565", "4444" };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static const bool gFilters[] = { false, true };
|
|
|
|
static const char* gFilterNames[] = { "point", "bilinear" };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
|
|
|
|
static const char* gModeNames[] = { "C", "R", "M" };
|
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(24);
|
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
SkCanvas* textCanvas = NULL;
|
2011-09-13 18:15:52 +00:00
|
|
|
if (fTextPicture->width() == 0) {
|
|
|
|
textCanvas = fTextPicture->beginRecording(1000, 1000);
|
2010-12-20 18:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (textCanvas) {
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
2010-12-20 18:26:13 +00:00
|
|
|
SkPaint p;
|
|
|
|
SkString str;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setDither(true);
|
|
|
|
p.setLooper(&fLooper);
|
|
|
|
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
|
|
|
|
|
|
|
|
p.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
y += SkIntToScalar(16);
|
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
x = SkIntToScalar(10);
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
|
|
|
|
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
|
|
|
|
paint.setDither(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
x += r.width() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
2010-12-20 18:26:13 +00:00
|
|
|
if (textCanvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
SkString str;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setLooper(&fLooper);
|
|
|
|
str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
|
2010-12-20 18:26:13 +00:00
|
|
|
textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
y += r.height() * 4 / 3;
|
|
|
|
}
|
|
|
|
}
|
2010-12-20 18:26:13 +00:00
|
|
|
|
2011-09-13 18:15:52 +00:00
|
|
|
canvas->drawPicture(*fTextPicture);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-04-22 14:10:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new TilingView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|