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 | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 59 | SkColorTable* ctable = new SkColorTable(c, 256); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 60 | |
| 61 | SkBitmap bm; |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 62 | bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | nullptr, ctable); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 64 | ctable->unref(); |
| 65 | |
| 66 | bm.lockPixels(); |
| 67 | for (int y = 0; y < bm.height(); y++) { |
| 68 | uint8_t* p = bm.getAddr8(0, y); |
| 69 | for (int x = 0; x < 256; x++) { |
| 70 | p[x] = x; |
| 71 | } |
| 72 | } |
| 73 | bm.unlockPixels(); |
| 74 | return bm; |
| 75 | } |
| 76 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 77 | class DitherBitmapView : public SampleView { |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 78 | SkBitmap fBM8; |
| 79 | SkBitmap fBM32; |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 80 | bool fResult; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 81 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | DitherBitmapView() { |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 83 | fResult = test_pathregion(); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 84 | fBM8 = make_bitmap(); |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 85 | fBM8.copyTo(&fBM32, kN32_SkColorType); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 87 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 88 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 90 | protected: |
| 91 | // overrides from SkEventSink |
| 92 | virtual bool onQuery(SkEvent* evt) { |
| 93 | if (SampleCode::TitleQ(*evt)) { |
| 94 | SampleCode::TitleR(evt, "DitherBitmap"); |
| 95 | return true; |
| 96 | } |
| 97 | return this->INHERITED::onQuery(evt); |
| 98 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 100 | static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { |
| 101 | SkAutoLockPixels alp(*bm); // needed for ctable |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 102 | bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 103 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 105 | static void draw2(SkCanvas* canvas, const SkBitmap& bm) { |
| 106 | SkPaint paint; |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 107 | SkBitmap bitmap(bm); |
| 108 | |
| 109 | setBitmapOpaque(&bitmap, false); |
| 110 | paint.setDither(false); |
| 111 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 112 | paint.setDither(true); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 113 | canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint); |
| 114 | |
| 115 | setBitmapOpaque(&bitmap, true); |
| 116 | SkScalar x = SkIntToScalar(bm.width() + 10); |
| 117 | paint.setDither(false); |
| 118 | canvas->drawBitmap(bitmap, x, 0, &paint); |
| 119 | paint.setDither(true); |
| 120 | canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 121 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 123 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 124 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 125 | |
| 126 | draw2(canvas, fBM8); |
| 127 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 128 | draw2(canvas, fBM32); |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 129 | |
| 130 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 131 | draw_gradient(canvas); |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 132 | |
| 133 | char resultTrue[] = "SkRegion::setPath returned true"; |
| 134 | char resultFalse[] = "SkRegion::setPath returned false"; |
| 135 | SkPaint p; |
| 136 | if (fResult) |
| 137 | canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p); |
| 138 | else |
| 139 | canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 140 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 141 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 142 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 143 | typedef SampleView INHERITED; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | |
| 148 | static SkView* MyFactory() { return new DitherBitmapView; } |
| 149 | static SkViewRegister reg(MyFactory); |