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