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 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 7 | |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame^] | 12 | #include "SkMakeUnique.h" |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 13 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 14 | static sk_sp<SkShader> make_grad(SkScalar w, SkScalar h) { |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 15 | SkColor colors[] = { 0xFF000000, 0xFF333333 }; |
| 16 | SkPoint pts[] = { { 0, 0 }, { w, h } }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 17 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 18 | } |
| 19 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 20 | class BigGradientView : public SampleView { |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 21 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 22 | BigGradientView() {} |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 23 | |
| 24 | protected: |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 25 | bool onQuery(SkEvent* evt) override { |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 26 | if (SampleCode::TitleQ(*evt)) { |
| 27 | SampleCode::TitleR(evt, "BigGradient"); |
| 28 | return true; |
| 29 | } |
| 30 | return this->INHERITED::onQuery(evt); |
| 31 | } |
| 32 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 33 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 34 | SkRect r; |
| 35 | r.set(0, 0, this->width(), this->height()); |
| 36 | SkPaint p; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 37 | p.setShader(make_grad(this->width(), this->height())); |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 38 | canvas->drawRect(r, p); |
| 39 | } |
| 40 | |
| 41 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 42 | typedef SampleView INHERITED; |
reed@google.com | 55b8e8c | 2011-01-13 16:22:35 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | static SkView* MyFactory() { return new BigGradientView; } |
| 48 | static SkViewRegister reg(MyFactory); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame^] | 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | #ifdef SK_BUILD_FOR_MAC |
| 53 | |
| 54 | #include "SkCGUtils.h" |
| 55 | #include "SkRasterHandleAllocator.h" |
| 56 | |
| 57 | class GraphicsPort { |
| 58 | protected: |
| 59 | SkCanvas* fCanvas; |
| 60 | |
| 61 | public: |
| 62 | GraphicsPort(SkCanvas* canvas) : fCanvas(canvas) {} |
| 63 | virtual ~GraphicsPort() {} |
| 64 | |
| 65 | void save() { fCanvas->save(); } |
| 66 | void saveLayer(const SkRect& bounds, SkAlpha alpha) { |
| 67 | fCanvas->saveLayerAlpha(&bounds, alpha); |
| 68 | } |
| 69 | void restore() { fCanvas->restore(); } |
| 70 | |
| 71 | void translate(float x, float y) { fCanvas->translate(x, y); } |
| 72 | void scale(float s) { fCanvas->scale(s, s); } |
| 73 | |
| 74 | void drawOval(const SkRect& r, SkColor c) { |
| 75 | SkPaint p; |
| 76 | p.setColor(c); |
| 77 | fCanvas->drawOval(r, p); |
| 78 | } |
| 79 | |
| 80 | virtual void drawRect(const SkRect& r, SkColor c) { |
| 81 | SkPaint p; |
| 82 | p.setColor(c); |
| 83 | fCanvas->drawRect(r, p); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | class CGGraphicsPort : public GraphicsPort { |
| 88 | public: |
| 89 | CGGraphicsPort(SkCanvas* canvas) : GraphicsPort(canvas) {} |
| 90 | |
| 91 | void drawRect(const SkRect& r, SkColor c) override { |
| 92 | CGContextRef cg = (CGContextRef)fCanvas->accessTopRasterHandle(); |
| 93 | |
| 94 | CGColorRef color = CGColorCreateGenericRGB(SkColorGetR(c)/255.f, |
| 95 | SkColorGetG(c)/255.f, |
| 96 | SkColorGetB(c)/255.f, |
| 97 | SkColorGetA(c)/255.f); |
| 98 | |
| 99 | CGContextSetFillColorWithColor(cg, color); |
| 100 | CGContextFillRect(cg, CGRectMake(r.x(), r.y(), r.width(), r.height())); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | static CGAffineTransform matrix_to_transform(CGContextRef cg, const SkMatrix& ctm) { |
| 105 | SkMatrix matrix; |
| 106 | matrix.setScale(1, -1); |
| 107 | matrix.postTranslate(0, SkIntToScalar(CGBitmapContextGetHeight(cg))); |
| 108 | matrix.preConcat(ctm); |
| 109 | |
| 110 | return CGAffineTransformMake(matrix[SkMatrix::kMScaleX], |
| 111 | matrix[SkMatrix::kMSkewY], |
| 112 | matrix[SkMatrix::kMSkewX], |
| 113 | matrix[SkMatrix::kMScaleY], |
| 114 | matrix[SkMatrix::kMTransX], |
| 115 | matrix[SkMatrix::kMTransY]); |
| 116 | } |
| 117 | |
| 118 | class Allocator_CG : public SkRasterHandleAllocator { |
| 119 | public: |
| 120 | Allocator_CG() {} |
| 121 | |
| 122 | bool allocHandle(const SkImageInfo& info, Rec* rec) override { |
| 123 | // let CG allocate the pixels |
| 124 | CGContextRef cg = SkCreateCGContext(SkPixmap(info, nullptr, 0)); |
| 125 | if (!cg) { |
| 126 | return false; |
| 127 | } |
| 128 | rec->fReleaseProc = [](void* pixels, void* ctx){ CGContextRelease((CGContextRef)ctx); }; |
| 129 | rec->fReleaseCtx = cg; |
| 130 | rec->fPixels = CGBitmapContextGetData(cg); |
| 131 | rec->fRowBytes = CGBitmapContextGetBytesPerRow(cg); |
| 132 | rec->fHandle = cg; |
| 133 | CGContextSaveGState(cg); // balanced each time updateContext is called |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void updateHandle(Handle hndl, const SkMatrix& ctm, const SkIRect& clip) override { |
| 138 | CGContextRef cg = (CGContextRef)hndl; |
| 139 | |
| 140 | CGContextRestoreGState(cg); |
| 141 | CGContextSaveGState(cg); |
| 142 | CGContextClearRect(cg, CGRectMake(clip.x(), clip.y(), clip.width(), clip.height())); |
| 143 | CGContextConcatCTM(cg, matrix_to_transform(cg, ctm)); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | class RasterAllocatorSample : public SampleView { |
| 148 | public: |
| 149 | RasterAllocatorSample() {} |
| 150 | |
| 151 | protected: |
| 152 | bool onQuery(SkEvent* evt) override { |
| 153 | if (SampleCode::TitleQ(*evt)) { |
| 154 | SampleCode::TitleR(evt, "raster-allocator"); |
| 155 | return true; |
| 156 | } |
| 157 | return this->INHERITED::onQuery(evt); |
| 158 | } |
| 159 | |
| 160 | void doDraw(GraphicsPort* port) { |
| 161 | port->drawRect({0, 0, 256, 256}, SK_ColorRED); |
| 162 | port->save(); |
| 163 | port->translate(30, 30); |
| 164 | port->drawRect({0, 0, 30, 30}, SK_ColorBLUE); |
| 165 | port->drawOval({10, 10, 20, 20}, SK_ColorWHITE); |
| 166 | port->restore(); |
| 167 | |
| 168 | port->saveLayer({50, 50, 100, 100}, 0x80); |
| 169 | port->drawRect({55, 55, 95, 95}, SK_ColorGREEN); |
| 170 | port->restore(); |
| 171 | } |
| 172 | |
| 173 | void onDrawContent(SkCanvas* canvas) override { |
| 174 | GraphicsPort skp(canvas); |
| 175 | doDraw(&skp); |
| 176 | |
| 177 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 178 | std::unique_ptr<SkCanvas> c2 = |
| 179 | SkRasterHandleAllocator::MakeCanvas(skstd::make_unique<Allocator_CG>(), info); |
| 180 | CGGraphicsPort cgp(c2.get()); |
| 181 | doDraw(&cgp); |
| 182 | |
| 183 | SkPixmap pm; |
| 184 | c2->peekPixels(&pm); |
| 185 | SkBitmap bm; |
| 186 | bm.installPixels(pm); |
| 187 | canvas->drawBitmap(bm, 280, 0, nullptr); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | typedef SampleView INHERITED; |
| 192 | }; |
| 193 | DEF_SAMPLE( return new RasterAllocatorSample; ) |
| 194 | #endif |