epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkPath.h" |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 13 | #include "SkUtils.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 14 | #include "SkView.h" |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 15 | |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 16 | static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 17 | canvas->drawRect(r, p); |
| 18 | |
| 19 | SkPaint frame(p); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | frame.setShader(nullptr); |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 21 | frame.setStyle(SkPaint::kStroke_Style); |
| 22 | canvas->drawRect(r, frame); |
| 23 | } |
| 24 | |
| 25 | static void draw_gradient(SkCanvas* canvas) { |
| 26 | SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 27 | SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 28 | SkColor colors[] = { 0xFF000000, 0xFFFF0000 }; |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 29 | SkPaint p; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 30 | p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode)); |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 31 | draw_rect(canvas, r, p); |
| 32 | |
| 33 | canvas->translate(0, SkIntToScalar(40)); |
| 34 | p.setDither(true); |
| 35 | draw_rect(canvas, r, p); |
| 36 | } |
| 37 | |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 38 | static bool test_pathregion() { |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 39 | SkPath path; |
| 40 | SkRegion region; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | path.moveTo(25071800.f, -141823808.f); |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 42 | path.lineTo(25075500.f, -141824000.f); |
| 43 | path.lineTo(25075400.f, -141827712.f); |
| 44 | path.lineTo(25071810.f, -141827600.f); |
| 45 | path.close(); |
| 46 | |
| 47 | SkIRect bounds; |
| 48 | path.getBounds().round(&bounds); |
| 49 | SkRegion clip(bounds); |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 50 | return region.setPath(path, clip); // <-- !! DOWN !! |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 51 | } |
| 52 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 53 | static SkBitmap make_bitmap() { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 54 | SkPMColor c[256]; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 55 | for (int i = 0; i < 256; i++) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 56 | c[i] = SkPackARGB32(0xFF, i, 0, 0); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 57 | } |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 58 | SkColorTable* ctable = new SkColorTable(c, 256); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 59 | |
| 60 | SkBitmap bm; |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 61 | bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | nullptr, ctable); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 63 | ctable->unref(); |
| 64 | |
| 65 | bm.lockPixels(); |
| 66 | for (int y = 0; y < bm.height(); y++) { |
| 67 | uint8_t* p = bm.getAddr8(0, y); |
| 68 | for (int x = 0; x < 256; x++) { |
| 69 | p[x] = x; |
| 70 | } |
| 71 | } |
| 72 | bm.unlockPixels(); |
| 73 | return bm; |
| 74 | } |
| 75 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 76 | class DitherBitmapView : public SampleView { |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 77 | SkBitmap fBM8; |
| 78 | SkBitmap fBM32; |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 79 | bool fResult; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 80 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | DitherBitmapView() { |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 82 | fResult = test_pathregion(); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 83 | fBM8 = make_bitmap(); |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 84 | fBM8.copyTo(&fBM32, kN32_SkColorType); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 86 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 87 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 89 | protected: |
| 90 | // overrides from SkEventSink |
| 91 | virtual bool onQuery(SkEvent* evt) { |
| 92 | if (SampleCode::TitleQ(*evt)) { |
| 93 | SampleCode::TitleR(evt, "DitherBitmap"); |
| 94 | return true; |
| 95 | } |
| 96 | return this->INHERITED::onQuery(evt); |
| 97 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 98 | |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 99 | static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { |
| 100 | SkAutoLockPixels alp(*bm); // needed for ctable |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 101 | bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 102 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 104 | static void draw2(SkCanvas* canvas, const SkBitmap& bm) { |
| 105 | SkPaint paint; |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 106 | SkBitmap bitmap(bm); |
| 107 | |
| 108 | setBitmapOpaque(&bitmap, false); |
| 109 | paint.setDither(false); |
| 110 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 111 | paint.setDither(true); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 112 | canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint); |
| 113 | |
| 114 | setBitmapOpaque(&bitmap, true); |
| 115 | SkScalar x = SkIntToScalar(bm.width() + 10); |
| 116 | paint.setDither(false); |
| 117 | canvas->drawBitmap(bitmap, x, 0, &paint); |
| 118 | paint.setDither(true); |
| 119 | canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 120 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 121 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 122 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 123 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 124 | |
| 125 | draw2(canvas, fBM8); |
| 126 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 127 | draw2(canvas, fBM32); |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 128 | |
| 129 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 130 | draw_gradient(canvas); |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 131 | |
| 132 | char resultTrue[] = "SkRegion::setPath returned true"; |
| 133 | char resultFalse[] = "SkRegion::setPath returned false"; |
| 134 | SkPaint p; |
| 135 | if (fResult) |
| 136 | canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p); |
| 137 | else |
| 138 | canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 139 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 140 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 141 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 142 | typedef SampleView INHERITED; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | ////////////////////////////////////////////////////////////////////////////// |
| 146 | |
| 147 | static SkView* MyFactory() { return new DitherBitmapView; } |
| 148 | static SkViewRegister reg(MyFactory); |