SampleTiling.cpp revision e2b193ca5c76f01f8e12b4a92e9bd6ccb3ed4280
1 2/* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8#include "SampleCode.h" 9#include "SkView.h" 10#include "SkCanvas.h" 11#include "SkPaint.h" 12#include "SkPath.h" 13#include "SkPictureRecorder.h" 14#include "SkRegion.h" 15#include "SkShader.h" 16#include "SkUtils.h" 17#include "SkColorPriv.h" 18#include "SkColorFilter.h" 19#include "SkPicture.h" 20#include "SkTypeface.h" 21 22// effects 23#include "SkGradientShader.h" 24#include "SkUnitMappers.h" 25#include "SkBlurMask.h" 26#include "SkBlurDrawLooper.h" 27 28static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { 29 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); 30 bm->eraseColor(SK_ColorTRANSPARENT); 31 32 SkCanvas canvas(*bm); 33 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } }; 34 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; 35 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; 36 SkPaint paint; 37 38 SkUnitMapper* um = NULL; 39 40 um = new SkCosineMapper; 41// um = new SkDiscreteMapper(12); 42 43 SkAutoUnref au(um); 44 45 paint.setDither(true); 46 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, 47 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); 48 canvas.drawPaint(paint); 49} 50 51static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, 52 SkShader::TileMode tmx, SkShader::TileMode tmy) { 53 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); 54 paint->setShader(shader)->unref(); 55 paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); 56} 57 58static const SkColorType gColorTypes[] = { 59 kN32_SkColorType, 60 kRGB_565_SkColorType, 61}; 62static const int gWidth = 32; 63static const int gHeight = 32; 64 65class TilingView : public SampleView { 66 SkAutoTUnref<SkPicture> fTextPicture; 67 SkAutoTUnref<SkBlurDrawLooper> fLooper; 68public: 69 TilingView() 70 : fLooper(SkBlurDrawLooper::Create(0x88000000, 71 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), 72 SkIntToScalar(2), SkIntToScalar(2))) { 73 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { 74 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight); 75 } 76 } 77 78 virtual ~TilingView() { 79 } 80 81 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; 82 83protected: 84 // overrides from SkEventSink 85 virtual bool onQuery(SkEvent* evt) { 86 if (SampleCode::TitleQ(*evt)) { 87 SampleCode::TitleR(evt, "Tiling"); 88 return true; 89 } 90 return this->INHERITED::onQuery(evt); 91 } 92 93 virtual void onDrawContent(SkCanvas* canvas) { 94 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; 95 96 static const char* gConfigNames[] = { "8888", "565", "4444" }; 97 98 static const bool gFilters[] = { false, true }; 99 static const char* gFilterNames[] = { "point", "bilinear" }; 100 101 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; 102 static const char* gModeNames[] = { "C", "R", "M" }; 103 104 SkScalar y = SkIntToScalar(24); 105 SkScalar x = SkIntToScalar(10); 106 107 SkPictureRecorder recorder; 108 SkCanvas* textCanvas = NULL; 109 if (NULL == fTextPicture) { 110 textCanvas = recorder.beginRecording(1000, 1000, NULL, 0); 111 } 112 113 if (NULL != textCanvas) { 114 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { 115 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { 116 SkPaint p; 117 SkString str; 118 p.setAntiAlias(true); 119 p.setDither(true); 120 p.setLooper(fLooper); 121 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); 122 123 p.setTextAlign(SkPaint::kCenter_Align); 124 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); 125 126 x += r.width() * 4 / 3; 127 } 128 } 129 } 130 131 y += SkIntToScalar(16); 132 133 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { 134 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { 135 x = SkIntToScalar(10); 136 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { 137 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { 138 SkPaint paint; 139 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); 140 paint.setDither(true); 141 142 canvas->save(); 143 canvas->translate(x, y); 144 canvas->drawRect(r, paint); 145 canvas->restore(); 146 147 x += r.width() * 4 / 3; 148 } 149 } 150 if (NULL != textCanvas) { 151 SkPaint p; 152 SkString str; 153 p.setAntiAlias(true); 154 p.setLooper(fLooper); 155 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); 156 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p); 157 } 158 159 y += r.height() * 4 / 3; 160 } 161 } 162 163 if (NULL != textCanvas) { 164 SkASSERT(NULL == fTextPicture); 165 fTextPicture.reset(recorder.endRecording()); 166 } 167 168 SkASSERT(NULL != fTextPicture); 169 canvas->drawPicture(*fTextPicture); 170 } 171 172private: 173 typedef SampleView INHERITED; 174}; 175 176////////////////////////////////////////////////////////////////////////////// 177 178static SkView* MyFactory() { return new TilingView; } 179static SkViewRegister reg(MyFactory); 180