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