epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkCanvas.h" |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
| 15 | #include "SkImageDecoder.h" |
| 16 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkRandom.h" |
| 18 | #include "SkRegion.h" |
| 19 | #include "SkShader.h" |
| 20 | #include "SkUtils.h" |
| 21 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkColorPriv.h" |
| 23 | #include "SkColorFilter.h" |
| 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
| 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | #include "SkOSFile.h" |
| 28 | #include "SkStream.h" |
| 29 | |
| 30 | static void check_for_nonwhite(const SkBitmap& bm, int alpha) { |
| 31 | if (bm.config() != SkBitmap::kRGB_565_Config) { |
| 32 | return; |
| 33 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 34 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | for (int y = 0; y < bm.height(); y++) { |
| 36 | for (int x = 0; x < bm.width(); x++) { |
| 37 | uint16_t c = *bm.getAddr16(x, y); |
| 38 | if (c != 0xFFFF) { |
| 39 | SkDebugf("------ nonwhite alpha=%x [%d %d] %x\n", alpha, x, y, c); |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 46 | class TextAlphaView : public SampleView { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | public: |
| 48 | TextAlphaView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | fByte = 0xFF; |
| 50 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | protected: |
| 53 | // overrides from SkEventSink |
| 54 | virtual bool onQuery(SkEvent* evt) { |
| 55 | if (SampleCode::TitleQ(*evt)) { |
tfarina@chromium.org | 45369a3 | 2012-09-30 11:30:01 +0000 | [diff] [blame] | 56 | SampleCode::TitleR(evt, "TextAlpha"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | return this->INHERITED::onQuery(evt); |
| 60 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 62 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 64 | SkPaint paint; |
| 65 | SkScalar x = SkIntToScalar(10); |
| 66 | SkScalar y = SkIntToScalar(20); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | paint.setFlags(0x105); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | paint.setARGB(fByte, 0xFF, 0xFF, 0xFF); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | paint.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3), |
| 73 | SkBlurMaskFilter::kNormal_BlurStyle)); |
| 74 | paint.getMaskFilter()->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | for (int ps = 6; ps <= 35; ps++) { |
| 79 | paint.setColor(rand.nextU() | (0xFF << 24)); |
| 80 | paint.setTextSize(SkIntToScalar(ps)); |
| 81 | paint.setTextSize(SkIntToScalar(24)); |
| 82 | canvas->drawText(str, strlen(str), x, y, paint); |
| 83 | y += paint.getFontMetrics(NULL); |
| 84 | } |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 85 | if (false) { // avoid bit rot, suppress warning |
| 86 | check_for_nonwhite(canvas->getDevice()->accessBitmap(false), fByte); |
| 87 | SkDebugf("------ byte %x\n", fByte); |
| 88 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | |
| 90 | if (false) { |
| 91 | fByte += 1; |
| 92 | fByte &= 0xFF; |
| 93 | this->inval(NULL); |
| 94 | } |
| 95 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame^] | 97 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | return new Click(this); |
| 99 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | virtual bool onClick(Click* click) { |
| 102 | int y = click->fICurr.fY; |
| 103 | if (y < 0) { |
| 104 | y = 0; |
| 105 | } else if (y > 255) { |
| 106 | y = 255; |
| 107 | } |
| 108 | fByte = y; |
| 109 | this->inval(NULL); |
| 110 | return true; |
| 111 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | private: |
| 114 | int fByte; |
| 115 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 116 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
| 121 | static SkView* MyFactory() { return new TextAlphaView; } |
| 122 | static SkViewRegister reg(MyFactory); |