blob: 7b2c5516a2aee90783f6f71b9307b8001a2dc41d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
15#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "Sk1DPathEffect.h"
17#include "SkCornerPathEffect.h"
18#include "SkPathMeasure.h"
19#include "SkRandom.h"
20#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkDither.h"
23
reed@google.com961ddb02011-05-05 14:03:48 +000024static void make_bm(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000025 const SkPMColor colors[] = {
26 SkPreMultiplyColor(SK_ColorRED), SkPreMultiplyColor(SK_ColorGREEN),
27 SkPreMultiplyColor(SK_ColorBLUE), SkPreMultiplyColor(SK_ColorWHITE)
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 };
29 SkColorTable* ctable = new SkColorTable(colors, 4);
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000030 bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType,
31 kOpaque_SkAlphaType),
32 NULL, ctable);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 ctable->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 *bm->getAddr8(0, 0) = 0;
36 *bm->getAddr8(1, 0) = 1;
37 *bm->getAddr8(0, 1) = 2;
38 *bm->getAddr8(1, 1) = 3;
39}
40
41static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
reed@google.com961ddb02011-05-05 14:03:48 +000042 SkScalar x, SkScalar y, SkPaint* paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 canvas->drawBitmap(bm, x, y, paint);
44 return SkIntToScalar(bm.width()) * 5/4;
reed@android.com8a1c16f2008-12-17 15:59:43 +000045}
46
reed@google.com961ddb02011-05-05 14:03:48 +000047static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, SkPaint* p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 x += draw_bm(c, bm, x, 0, p);
reed@google.com44699382013-10-31 17:28:30 +000049 p->setFilterLevel(SkPaint::kLow_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 x += draw_bm(c, bm, x, 0, p);
51 p->setDither(true);
52 return x + draw_bm(c, bm, x, 0, p);
53}
54
55static const char* gConfigNames[] = {
56 "unknown config",
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 "A8",
58 "Index8",
59 "565",
60 "4444",
61 "8888"
62};
63
reed@google.com961ddb02011-05-05 14:03:48 +000064static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 SkAutoCanvasRestore acr(canvas, true);
66
67 SkPaint paint;
68 SkScalar x = 0;
69 const int scale = 32;
70
71 paint.setAntiAlias(true);
72 const char* name = gConfigNames[bm.config()];
73 canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
74 paint);
75 canvas->translate(SkIntToScalar(48), 0);
76
77 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 x += draw_set(canvas, bm, 0, &paint);
80 paint.reset();
81 paint.setAlpha(0x80);
82 draw_set(canvas, bm, x, &paint);
83 return x * scale / 3;
84}
85
reed@google.com961ddb02011-05-05 14:03:48 +000086class FilterView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087public:
88 SkBitmap fBM8, fBM4444, fBM16, fBM32;
89
rmistry@google.comae933ce2012-08-23 18:19:56 +000090 FilterView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 make_bm(&fBM8);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000092 fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType);
93 fBM8.copyTo(&fBM16, kRGB_565_SkColorType);
commit-bot@chromium.org757ebd22014-04-10 22:36:34 +000094 fBM8.copyTo(&fBM32, kPMColor_SkColorType);
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
reed@google.com961ddb02011-05-05 14:03:48 +000096 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 }
98
99protected:
100 // overrides from SkEventSink
reed@google.com961ddb02011-05-05 14:03:48 +0000101 virtual bool onQuery(SkEvent* evt) {
102 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 SampleCode::TitleR(evt, "Filter");
104 return true;
105 }
106 return this->INHERITED::onQuery(evt);
107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@google.com961ddb02011-05-05 14:03:48 +0000109 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 SkScalar x = SkIntToScalar(10);
111 SkScalar y = SkIntToScalar(10);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 canvas->translate(x, y);
114 y = draw_row(canvas, fBM8);
115 canvas->translate(0, y);
116 y = draw_row(canvas, fBM4444);
117 canvas->translate(0, y);
118 y = draw_row(canvas, fBM16);
119 canvas->translate(0, y);
120 draw_row(canvas, fBM32);
121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123private:
reed@google.com961ddb02011-05-05 14:03:48 +0000124 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125};
126
127//////////////////////////////////////////////////////////////////////////////
128
129static SkView* MyFactory() { return new FilterView; }
130static SkViewRegister reg(MyFactory);