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 | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkColorPriv.h" |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 11 | #include "SkView.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkUtils.h" |
| 14 | |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 15 | static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 16 | canvas->drawRect(r, p); |
| 17 | |
| 18 | SkPaint frame(p); |
| 19 | frame.setShader(NULL); |
| 20 | frame.setStyle(SkPaint::kStroke_Style); |
| 21 | canvas->drawRect(r, frame); |
| 22 | } |
| 23 | |
| 24 | static void draw_gradient(SkCanvas* canvas) { |
| 25 | SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 26 | SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 27 | SkColor colors[] = { 0xFF000000, 0xFFFF0000 }; |
| 28 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 29 | SkShader::kClamp_TileMode); |
| 30 | |
| 31 | SkPaint p; |
| 32 | p.setShader(s)->unref(); |
| 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 | SkColorTable* ctable = new SkColorTable(c, 256, kOpaque_SkAlphaType); |
| 61 | |
| 62 | SkBitmap bm; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 63 | bm.setConfig(SkBitmap::kIndex8_Config, 256, 32); |
| 64 | bm.allocPixels(ctable); |
| 65 | ctable->unref(); |
| 66 | |
| 67 | bm.lockPixels(); |
| 68 | for (int y = 0; y < bm.height(); y++) { |
| 69 | uint8_t* p = bm.getAddr8(0, y); |
| 70 | for (int x = 0; x < 256; x++) { |
| 71 | p[x] = x; |
| 72 | } |
| 73 | } |
| 74 | bm.unlockPixels(); |
| 75 | return bm; |
| 76 | } |
| 77 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 78 | class DitherBitmapView : public SampleView { |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 79 | SkBitmap fBM8; |
| 80 | SkBitmap fBM32; |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 81 | bool fResult; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 82 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | DitherBitmapView() { |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 84 | fResult = test_pathregion(); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 85 | fBM8 = make_bitmap(); |
| 86 | fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 88 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 89 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 90 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 91 | protected: |
| 92 | // overrides from SkEventSink |
| 93 | virtual bool onQuery(SkEvent* evt) { |
| 94 | if (SampleCode::TitleQ(*evt)) { |
| 95 | SampleCode::TitleR(evt, "DitherBitmap"); |
| 96 | return true; |
| 97 | } |
| 98 | return this->INHERITED::onQuery(evt); |
| 99 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 100 | |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 101 | static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { |
| 102 | SkAutoLockPixels alp(*bm); // needed for ctable |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 103 | bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 104 | #if 0 |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 105 | SkColorTable* ctable = bm->getColorTable(); |
| 106 | if (ctable) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 107 | if (ctable->isOpaque() != isOpaque) { |
| 108 | // how do we change a colortable? don't want to |
| 109 | } |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 110 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 111 | #endif |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 112 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 113 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 114 | static void draw2(SkCanvas* canvas, const SkBitmap& bm) { |
| 115 | SkPaint paint; |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 116 | SkBitmap bitmap(bm); |
| 117 | |
| 118 | setBitmapOpaque(&bitmap, false); |
| 119 | paint.setDither(false); |
| 120 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 121 | paint.setDither(true); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 122 | canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint); |
| 123 | |
| 124 | setBitmapOpaque(&bitmap, true); |
| 125 | SkScalar x = SkIntToScalar(bm.width() + 10); |
| 126 | paint.setDither(false); |
| 127 | canvas->drawBitmap(bitmap, x, 0, &paint); |
| 128 | paint.setDither(true); |
| 129 | canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 130 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 131 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 132 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 133 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 134 | |
| 135 | draw2(canvas, fBM8); |
| 136 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 137 | draw2(canvas, fBM32); |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 138 | |
| 139 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 140 | draw_gradient(canvas); |
halcanary@google.com | 9acb8cd | 2014-01-10 14:53:49 +0000 | [diff] [blame] | 141 | |
| 142 | char resultTrue[] = "SkRegion::setPath returned true"; |
| 143 | char resultFalse[] = "SkRegion::setPath returned false"; |
| 144 | SkPaint p; |
| 145 | if (fResult) |
| 146 | canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p); |
| 147 | else |
| 148 | canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 149 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 150 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 151 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 152 | typedef SampleView INHERITED; |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | ////////////////////////////////////////////////////////////////////////////// |
| 156 | |
| 157 | static SkView* MyFactory() { return new DitherBitmapView; } |
| 158 | static SkViewRegister reg(MyFactory); |