blob: ca84e629eeaf81a5d06e564f501180acb13bd55d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bungeman903dcb02015-03-16 13:00:09 -04007#define __STDC_LIMIT_MACROS
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkDraw.h"
10#include "SkBlitter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkColorPriv.h"
13#include "SkDevice.h"
reed@google.com1c028bd2013-08-28 15:23:19 +000014#include "SkDeviceLooper.h"
herbe5911c92015-11-09 13:15:21 -080015#include "SkFindAndPlaceGlyph.h"
bungeman@google.com2211b622012-01-13 15:02:58 +000016#include "SkFixed.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkMaskFilter.h"
herbf553e4e2015-11-09 08:51:56 -080018#include "SkMatrix.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkPaint.h"
20#include "SkPathEffect.h"
reed@google.com045e62d2011-10-24 12:19:46 +000021#include "SkRasterClip.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkRasterizer.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000023#include "SkRRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkScan.h"
25#include "SkShader.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000026#include "SkSmallAllocator.h"
robertphillips@google.com76f9e932013-01-15 20:17:47 +000027#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkStroke.h"
halcanary435657f2015-09-15 12:53:07 -070029#include "SkStrokeRec.h"
herbf553e4e2015-11-09 08:51:56 -080030#include "SkTemplates.h"
kkinnunencb9a2c82014-06-12 23:06:28 -070031#include "SkTextMapStateProc.h"
reed@google.com32e5d972011-07-27 18:25:57 +000032#include "SkTLazy.h"
herbf553e4e2015-11-09 08:51:56 -080033#include "SkUtility.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000034#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000035#include "SkVertState.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037#include "SkBitmapProcShader.h"
38#include "SkDrawProcs.h"
reed@google.comae573582013-01-03 15:22:40 +000039#include "SkMatrixUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
41//#define TRACE_BITMAP_DRAWS
42
reed@android.com8a1c16f2008-12-17 15:59:43 +000043
reed@google.comfd4236e2011-07-25 21:16:22 +000044/** Helper for allocating small blitters on the stack.
45 */
reed@google.com40c2ba22011-07-25 19:41:22 +000046class SkAutoBlitterChoose : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047public:
reed@google.com1d6ee0b2011-07-05 17:44:56 +000048 SkAutoBlitterChoose() {
halcanary96fcdcc2015-08-27 07:41:13 -070049 fBlitter = nullptr;
reed@google.com1d6ee0b2011-07-05 17:44:56 +000050 }
reed41e010c2015-06-09 12:16:53 -070051 SkAutoBlitterChoose(const SkPixmap& dst, const SkMatrix& matrix,
reed@google.com126f7f52013-11-07 16:06:53 +000052 const SkPaint& paint, bool drawCoverage = false) {
reed41e010c2015-06-09 12:16:53 -070053 fBlitter = SkBlitter::Choose(dst, matrix, paint, &fAllocator, drawCoverage);
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000054 }
herbe59124e2015-11-18 10:54:39 -080055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 SkBlitter* operator->() { return fBlitter; }
57 SkBlitter* get() const { return fBlitter; }
58
reed41e010c2015-06-09 12:16:53 -070059 void choose(const SkPixmap& dst, const SkMatrix& matrix,
krajcevski53f09592014-08-06 11:12:14 -070060 const SkPaint& paint, bool drawCoverage = false) {
reed@google.com1d6ee0b2011-07-05 17:44:56 +000061 SkASSERT(!fBlitter);
reed41e010c2015-06-09 12:16:53 -070062 fBlitter = SkBlitter::Choose(dst, matrix, paint, &fAllocator, drawCoverage);
reed@google.com1d6ee0b2011-07-05 17:44:56 +000063 }
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000066 // Owned by fAllocator, which will handle the delete.
67 SkBlitter* fBlitter;
68 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000070#define SkAutoBlitterChoose(...) SK_REQUIRE_LOCAL_VAR(SkAutoBlitterChoose)
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
reed@google.com40c2ba22011-07-25 19:41:22 +000072/**
73 * Since we are providing the storage for the shader (to avoid the perf cost
74 * of calling new) we insist that in our destructor we can account for all
75 * owners of the shader.
76 */
77class SkAutoBitmapShaderInstall : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078public:
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000079 SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint,
halcanary96fcdcc2015-08-27 07:41:13 -070080 const SkMatrix* localMatrix = nullptr)
reed@google.com40c2ba22011-07-25 19:41:22 +000081 : fPaint(paint) /* makes a copy of the paint */ {
mtklein7ef849d2014-11-24 09:11:45 -080082 fPaint.setShader(SkCreateBitmapShader(src, SkShader::kClamp_TileMode,
83 SkShader::kClamp_TileMode,
84 localMatrix, &fAllocator));
reed@google.com40c2ba22011-07-25 19:41:22 +000085 // we deliberately left the shader with an owner-count of 2
86 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 }
reed@google.com82065d62011-02-07 15:30:46 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 ~SkAutoBitmapShaderInstall() {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000090 // since fAllocator will destroy shader, we insist that owners == 2
91 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
halcanary96fcdcc2015-08-27 07:41:13 -070093 fPaint.setShader(nullptr); // unref the shader by 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 }
reed@google.com82065d62011-02-07 15:30:46 +000096
reed@google.com40c2ba22011-07-25 19:41:22 +000097 // return the new paint that has the shader applied
98 const SkPaint& paintWithShader() const { return fPaint; }
99
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000101 // copy of caller's paint (which we then modify)
102 SkPaint fPaint;
103 // Stores the shader.
104 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000106#define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108///////////////////////////////////////////////////////////////////////////////
109
reed@android.comf2b98d62010-12-20 18:26:13 +0000110SkDraw::SkDraw() {
111 sk_bzero(this, sizeof(*this));
112}
113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114SkDraw::SkDraw(const SkDraw& src) {
115 memcpy(this, &src, sizeof(*this));
116}
117
reed@google.com4bbdeac2013-01-24 21:03:11 +0000118bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
119 if (fRC->isEmpty()) {
120 return false;
121 }
122
123 SkMatrix inverse;
124 if (!fMatrix->invert(&inverse)) {
125 return false;
126 }
127
128 SkIRect devBounds = fRC->getBounds();
129 // outset to have slop for antialasing and hairlines
130 devBounds.outset(1, 1);
131 inverse.mapRect(localBounds, SkRect::Make(devBounds));
132 return true;
133}
134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135///////////////////////////////////////////////////////////////////////////////
136
137typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
138
139static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
reed@android.com4516f472009-06-29 16:25:36 +0000140 sk_bzero(pixels, bytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141}
142
143static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
144
145static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000146 sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147}
148
149static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000150 sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151}
152
153static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
154 memset(pixels, data, bytes);
155}
156
reed41e010c2015-06-09 12:16:53 -0700157static BitmapXferProc ChooseBitmapXferProc(const SkPixmap& dst, const SkPaint& paint,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 uint32_t* data) {
159 // todo: we can apply colorfilter up front if no shader, so we wouldn't
160 // need to abort this fastpath
161 if (paint.getShader() || paint.getColorFilter()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 }
164
reed@android.com845fdac2009-06-23 03:01:32 +0000165 SkXfermode::Mode mode;
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000166 if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 SkColor color = paint.getColor();
reed@google.coma76de3d2011-01-13 18:30:42 +0000171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 // collaps modes based on color...
reed@android.com845fdac2009-06-23 03:01:32 +0000173 if (SkXfermode::kSrcOver_Mode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 unsigned alpha = SkColorGetA(color);
175 if (0 == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000176 mode = SkXfermode::kDst_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 } else if (0xFF == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000178 mode = SkXfermode::kSrc_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 }
180 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000183 case SkXfermode::kClear_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184// SkDebugf("--- D_Clear_BitmapXferProc\n");
185 return D_Clear_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000186 case SkXfermode::kDst_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187// SkDebugf("--- D_Dst_BitmapXferProc\n");
188 return D_Dst_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000189 case SkXfermode::kSrc_Mode: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 /*
reed@google.coma76de3d2011-01-13 18:30:42 +0000191 should I worry about dithering for the lower depths?
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 */
193 SkPMColor pmc = SkPreMultiplyColor(color);
reed41e010c2015-06-09 12:16:53 -0700194 switch (dst.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000195 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 if (data) {
197 *data = pmc;
198 }
199// SkDebugf("--- D32_Src_BitmapXferProc\n");
200 return D32_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000201 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 if (data) {
203 *data = SkPixel32ToPixel16(pmc);
204 }
205// SkDebugf("--- D16_Src_BitmapXferProc\n");
206 return D16_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000207 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 if (data) {
209 *data = SkGetPackedA32(pmc);
210 }
211// SkDebugf("--- DA8_Src_BitmapXferProc\n");
212 return DA8_Src_BitmapXferProc;
213 default:
214 break;
215 }
216 break;
217 }
218 default:
219 break;
220 }
halcanary96fcdcc2015-08-27 07:41:13 -0700221 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222}
223
reed41e010c2015-06-09 12:16:53 -0700224static void CallBitmapXferProc(const SkPixmap& dst, const SkIRect& rect, BitmapXferProc proc,
225 uint32_t procData) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 int shiftPerPixel;
reed41e010c2015-06-09 12:16:53 -0700227 switch (dst.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000228 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 shiftPerPixel = 2;
230 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000231 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232 shiftPerPixel = 1;
233 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000234 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 shiftPerPixel = 0;
236 break;
237 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000238 SkDEBUGFAIL("Can't use xferproc on this config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 return;
240 }
241
reed41e010c2015-06-09 12:16:53 -0700242 uint8_t* pixels = (uint8_t*)dst.writable_addr();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 SkASSERT(pixels);
reed41e010c2015-06-09 12:16:53 -0700244 const size_t rowBytes = dst.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 const int widthBytes = rect.width() << shiftPerPixel;
246
247 // skip down to the first scanline and X position
248 pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
249 for (int scans = rect.height() - 1; scans >= 0; --scans) {
250 proc(pixels, widthBytes, procData);
251 pixels += rowBytes;
252 }
253}
254
255void SkDraw::drawPaint(const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000256 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257
reed@google.com045e62d2011-10-24 12:19:46 +0000258 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 return;
260 }
261
262 SkIRect devRect;
reed41e010c2015-06-09 12:16:53 -0700263 devRect.set(0, 0, fDst.width(), fDst.height());
reed@google.coma76de3d2011-01-13 18:30:42 +0000264
reed@google.com045e62d2011-10-24 12:19:46 +0000265 if (fRC->isBW()) {
266 /* If we don't have a shader (i.e. we're just a solid color) we may
267 be faster to operate directly on the device bitmap, rather than invoking
268 a blitter. Esp. true for xfermodes, which require a colorshader to be
269 present, which is just redundant work. Since we're drawing everywhere
270 in the clip, we don't have to worry about antialiasing.
271 */
272 uint32_t procData = 0; // to avoid the warning
reed41e010c2015-06-09 12:16:53 -0700273 BitmapXferProc proc = ChooseBitmapXferProc(fDst, paint, &procData);
reed@google.com045e62d2011-10-24 12:19:46 +0000274 if (proc) {
275 if (D_Dst_BitmapXferProc == proc) { // nothing to do
276 return;
277 }
278
279 SkRegion::Iterator iter(fRC->bwRgn());
280 while (!iter.done()) {
reed41e010c2015-06-09 12:16:53 -0700281 CallBitmapXferProc(fDst, iter.rect(), proc, procData);
reed@google.com045e62d2011-10-24 12:19:46 +0000282 iter.next();
283 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 return;
285 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 }
reed@google.com045e62d2011-10-24 12:19:46 +0000287
288 // normal case: use a blitter
reed41e010c2015-06-09 12:16:53 -0700289 SkAutoBlitterChoose blitter(fDst, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +0000290 SkScan::FillIRect(devRect, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291}
292
293///////////////////////////////////////////////////////////////////////////////
294
295struct PtProcRec {
296 SkCanvas::PointMode fMode;
297 const SkPaint* fPaint;
298 const SkRegion* fClip;
reed@google.com045e62d2011-10-24 12:19:46 +0000299 const SkRasterClip* fRC;
reed@google.coma76de3d2011-01-13 18:30:42 +0000300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 // computed values
302 SkFixed fRadius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000303
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
305 SkBlitter*);
306
307 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
reed@google.com045e62d2011-10-24 12:19:46 +0000308 const SkRasterClip*);
309 Proc chooseProc(SkBlitter** blitter);
310
311private:
312 SkAAClipBlitterWrapper fWrapper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313};
314
315static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
316 int count, SkBlitter* blitter) {
317 SkASSERT(rec.fClip->isRect());
318 const SkIRect& r = rec.fClip->getBounds();
reed@google.coma76de3d2011-01-13 18:30:42 +0000319
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000321 int x = SkScalarFloorToInt(devPts[i].fX);
322 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 if (r.contains(x, y)) {
324 blitter->blitH(x, y, 1);
325 }
326 }
327}
328
329static void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
330 const SkPoint devPts[], int count,
331 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000332 SkASSERT(rec.fRC->isRect());
333 const SkIRect& r = rec.fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 uint32_t value;
reed41e010c2015-06-09 12:16:53 -0700335 const SkPixmap* dst = blitter->justAnOpaqueColor(&value);
336 SkASSERT(dst);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000337
reed41e010c2015-06-09 12:16:53 -0700338 uint16_t* addr = dst->writable_addr16(0, 0);
339 size_t rb = dst->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000342 int x = SkScalarFloorToInt(devPts[i].fX);
343 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 if (r.contains(x, y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
346 }
347 }
348}
349
reed@google.com2d47a212012-11-29 21:01:00 +0000350static void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
351 const SkPoint devPts[], int count,
352 SkBlitter* blitter) {
353 SkASSERT(rec.fRC->isRect());
354 const SkIRect& r = rec.fRC->getBounds();
355 uint32_t value;
reed41e010c2015-06-09 12:16:53 -0700356 const SkPixmap* dst = blitter->justAnOpaqueColor(&value);
357 SkASSERT(dst);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000358
reed41e010c2015-06-09 12:16:53 -0700359 SkPMColor* addr = dst->writable_addr32(0, 0);
360 size_t rb = dst->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000361
reed@google.com2d47a212012-11-29 21:01:00 +0000362 for (int i = 0; i < count; i++) {
363 int x = SkScalarFloorToInt(devPts[i].fX);
364 int y = SkScalarFloorToInt(devPts[i].fY);
365 if (r.contains(x, y)) {
366 ((SkPMColor*)((char*)addr + y * rb))[x] = value;
367 }
368 }
369}
370
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
372 int count, SkBlitter* blitter) {
373 for (int i = 0; i < count; i++) {
reed@google.come1ca7052013-12-17 19:22:07 +0000374 int x = SkScalarFloorToInt(devPts[i].fX);
375 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 if (rec.fClip->contains(x, y)) {
377 blitter->blitH(x, y, 1);
378 }
379 }
380}
381
382static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
383 int count, SkBlitter* blitter) {
384 for (int i = 0; i < count; i += 2) {
reed5dc6b7d2015-04-14 10:40:44 -0700385 SkScan::HairLine(&devPts[i], 2, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 }
387}
388
389static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
390 int count, SkBlitter* blitter) {
reed5dc6b7d2015-04-14 10:40:44 -0700391 SkScan::HairLine(devPts, count, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392}
393
394// aa versions
395
396static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
397 int count, SkBlitter* blitter) {
398 for (int i = 0; i < count; i += 2) {
reed5dc6b7d2015-04-14 10:40:44 -0700399 SkScan::AntiHairLine(&devPts[i], 2, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 }
401}
402
403static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
404 int count, SkBlitter* blitter) {
reed5dc6b7d2015-04-14 10:40:44 -0700405 SkScan::AntiHairLine(devPts, count, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406}
407
408// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
409
410static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
411 int count, SkBlitter* blitter) {
412 const SkFixed radius = rec.fRadius;
413 for (int i = 0; i < count; i++) {
414 SkFixed x = SkScalarToFixed(devPts[i].fX);
415 SkFixed y = SkScalarToFixed(devPts[i].fY);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000416
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 SkXRect r;
418 r.fLeft = x - radius;
419 r.fTop = y - radius;
420 r.fRight = x + radius;
421 r.fBottom = y + radius;
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000422
reed@google.com045e62d2011-10-24 12:19:46 +0000423 SkScan::FillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 }
425}
426
427static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
428 int count, SkBlitter* blitter) {
429 const SkFixed radius = rec.fRadius;
430 for (int i = 0; i < count; i++) {
431 SkFixed x = SkScalarToFixed(devPts[i].fX);
432 SkFixed y = SkScalarToFixed(devPts[i].fY);
reed@google.coma76de3d2011-01-13 18:30:42 +0000433
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 SkXRect r;
435 r.fLeft = x - radius;
436 r.fTop = y - radius;
437 r.fRight = x + radius;
438 r.fBottom = y + radius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000439
reed@google.com045e62d2011-10-24 12:19:46 +0000440 SkScan::AntiFillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441 }
442}
443
reed@android.comb4f404a2009-07-10 17:02:17 +0000444// If this guy returns true, then chooseProc() must return a valid proc
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
reed@google.com045e62d2011-10-24 12:19:46 +0000446 const SkMatrix* matrix, const SkRasterClip* rc) {
ochang3ece53e2015-05-21 15:44:53 -0700447 if ((unsigned)mode > (unsigned)SkCanvas::kPolygon_PointMode) {
448 return false;
449 }
450
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451 if (paint.getPathEffect()) {
452 return false;
453 }
454 SkScalar width = paint.getStrokeWidth();
455 if (0 == width) {
456 fMode = mode;
457 fPaint = &paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700458 fClip = nullptr;
reed@google.com045e62d2011-10-24 12:19:46 +0000459 fRC = rc;
reed@google.com2d47a212012-11-29 21:01:00 +0000460 fRadius = SK_FixedHalf;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 return true;
462 }
reed@android.comb4f404a2009-07-10 17:02:17 +0000463 if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
robertphillips9f2251c2014-11-04 13:33:50 -0800464 matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
466 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
467 if (SkScalarNearlyZero(sx - sy)) {
468 if (sx < 0) {
469 sx = -sx;
470 }
471
472 fMode = mode;
473 fPaint = &paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700474 fClip = nullptr;
reed@google.com045e62d2011-10-24 12:19:46 +0000475 fRC = rc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476 fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
477 return true;
478 }
479 }
480 return false;
481}
482
reed@google.com045e62d2011-10-24 12:19:46 +0000483PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700484 Proc proc = nullptr;
reed@google.coma76de3d2011-01-13 18:30:42 +0000485
reed@google.com045e62d2011-10-24 12:19:46 +0000486 SkBlitter* blitter = *blitterPtr;
487 if (fRC->isBW()) {
488 fClip = &fRC->bwRgn();
489 } else {
490 fWrapper.init(*fRC, blitter);
491 fClip = &fWrapper.getRgn();
492 blitter = fWrapper.getBlitter();
493 *blitterPtr = blitter;
494 }
495
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 // for our arrays
497 SkASSERT(0 == SkCanvas::kPoints_PointMode);
498 SkASSERT(1 == SkCanvas::kLines_PointMode);
499 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
500 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
501
reed@google.com2d47a212012-11-29 21:01:00 +0000502 if (fPaint->isAntiAlias()) {
503 if (0 == fPaint->getStrokeWidth()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504 static const Proc gAAProcs[] = {
505 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
506 };
507 proc = gAAProcs[fMode];
reed@google.com2d47a212012-11-29 21:01:00 +0000508 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
509 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
510 proc = aa_square_proc;
511 }
512 } else { // BW
513 if (fRadius <= SK_FixedHalf) { // small radii and hairline
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514 if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
515 uint32_t value;
reed41e010c2015-06-09 12:16:53 -0700516 const SkPixmap* bm = blitter->justAnOpaqueColor(&value);
reed@google.com900ecf22014-02-20 20:55:37 +0000517 if (bm && kRGB_565_SkColorType == bm->colorType()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 proc = bw_pt_rect_16_hair_proc;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000519 } else if (bm && kN32_SkColorType == bm->colorType()) {
reed@google.com2d47a212012-11-29 21:01:00 +0000520 proc = bw_pt_rect_32_hair_proc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 } else {
522 proc = bw_pt_rect_hair_proc;
523 }
524 } else {
525 static Proc gBWProcs[] = {
526 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
527 };
528 proc = gBWProcs[fMode];
529 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000530 } else {
531 proc = bw_square_proc;
532 }
533 }
534 return proc;
535}
536
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537// each of these costs 8-bytes of stack space, so don't make it too large
538// must be even for lines/polygon to work
539#define MAX_DEV_PTS 32
540
541void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
reed@android.comf2b98d62010-12-20 18:26:13 +0000542 const SkPoint pts[], const SkPaint& paint,
543 bool forceUseDevice) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 // if we're in lines mode, force count to be even
545 if (SkCanvas::kLines_PointMode == mode) {
546 count &= ~(size_t)1;
547 }
548
549 if ((long)count <= 0) {
550 return;
551 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000552
halcanary96fcdcc2015-08-27 07:41:13 -0700553 SkASSERT(pts != nullptr);
reed@android.comf2b98d62010-12-20 18:26:13 +0000554 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +0000555
reed@android.com8a1c16f2008-12-17 15:59:43 +0000556 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000557 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 return;
559 }
560
561 PtProcRec rec;
reed@google.com045e62d2011-10-24 12:19:46 +0000562 if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
reed41e010c2015-06-09 12:16:53 -0700563 SkAutoBlitterChoose blitter(fDst, *fMatrix, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564
565 SkPoint devPts[MAX_DEV_PTS];
566 const SkMatrix* matrix = fMatrix;
567 SkBlitter* bltr = blitter.get();
reed@google.com045e62d2011-10-24 12:19:46 +0000568 PtProcRec::Proc proc = rec.chooseProc(&bltr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 // we have to back up subsequent passes if we're in polygon mode
570 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
reed@google.coma76de3d2011-01-13 18:30:42 +0000571
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572 do {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000573 int n = SkToInt(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 if (n > MAX_DEV_PTS) {
575 n = MAX_DEV_PTS;
576 }
577 matrix->mapPoints(devPts, pts, n);
578 proc(rec, devPts, n, bltr);
579 pts += n - backup;
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000580 SkASSERT(SkToInt(count) >= n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581 count -= n;
582 if (count > 0) {
583 count += backup;
584 }
585 } while (count != 0);
586 } else {
587 switch (mode) {
588 case SkCanvas::kPoints_PointMode: {
589 // temporarily mark the paint as filling.
reed@google.com40c2ba22011-07-25 19:41:22 +0000590 SkPaint newPaint(paint);
591 newPaint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000592
reed@google.com40c2ba22011-07-25 19:41:22 +0000593 SkScalar width = newPaint.getStrokeWidth();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000594 SkScalar radius = SkScalarHalf(width);
reed@google.coma76de3d2011-01-13 18:30:42 +0000595
reed@google.com40c2ba22011-07-25 19:41:22 +0000596 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000597 SkPath path;
598 SkMatrix preMatrix;
mtklein7ef849d2014-11-24 09:11:45 -0800599
reed@android.com8a1c16f2008-12-17 15:59:43 +0000600 path.addCircle(0, 0, radius);
601 for (size_t i = 0; i < count; i++) {
602 preMatrix.setTranslate(pts[i].fX, pts[i].fY);
603 // pass true for the last point, since we can modify
604 // then path then
jvanverthb3eb6872014-10-24 07:12:51 -0700605 path.setIsVolatile((count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000606 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000607 fDevice->drawPath(*this, path, newPaint, &preMatrix,
reed@android.comf2b98d62010-12-20 18:26:13 +0000608 (count-1) == i);
609 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000610 this->drawPath(path, newPaint, &preMatrix,
611 (count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000612 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000613 }
614 } else {
615 SkRect r;
reed@google.coma76de3d2011-01-13 18:30:42 +0000616
reed@android.com8a1c16f2008-12-17 15:59:43 +0000617 for (size_t i = 0; i < count; i++) {
618 r.fLeft = pts[i].fX - radius;
619 r.fTop = pts[i].fY - radius;
620 r.fRight = r.fLeft + width;
621 r.fBottom = r.fTop + width;
reed@android.comf2b98d62010-12-20 18:26:13 +0000622 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000623 fDevice->drawRect(*this, r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000624 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000625 this->drawRect(r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000626 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 }
628 }
629 break;
630 }
631 case SkCanvas::kLines_PointMode:
bsalomon49f085d2014-09-05 13:34:00 -0700632 if (2 == count && paint.getPathEffect()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000633 // most likely a dashed line - see if it is one of the ones
634 // we can accelerate
635 SkStrokeRec rec(paint);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000636 SkPathEffect::PointData pointData;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000637
638 SkPath path;
639 path.moveTo(pts[0]);
640 path.lineTo(pts[1]);
641
reed@google.com4bbdeac2013-01-24 21:03:11 +0000642 SkRect cullRect = SkRect::Make(fRC->getBounds());
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000643
reed@google.com4bbdeac2013-01-24 21:03:11 +0000644 if (paint.getPathEffect()->asPoints(&pointData, path, rec,
645 *fMatrix, &cullRect)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000646 // 'asPoints' managed to find some fast path
647
robertphillips@google.com629ab542012-11-28 17:18:11 +0000648 SkPaint newP(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700649 newP.setPathEffect(nullptr);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000650 newP.setStyle(SkPaint::kFill_Style);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000651
robertphillips@google.com6d875572012-12-17 18:56:29 +0000652 if (!pointData.fFirst.isEmpty()) {
653 if (fDevice) {
654 fDevice->drawPath(*this, pointData.fFirst, newP);
655 } else {
656 this->drawPath(pointData.fFirst, newP);
657 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000658 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000659
660 if (!pointData.fLast.isEmpty()) {
661 if (fDevice) {
662 fDevice->drawPath(*this, pointData.fLast, newP);
663 } else {
664 this->drawPath(pointData.fLast, newP);
665 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000666 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000667
668 if (pointData.fSize.fX == pointData.fSize.fY) {
669 // The rest of the dashed line can just be drawn as points
670 SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
671
672 if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
673 newP.setStrokeCap(SkPaint::kRound_Cap);
674 } else {
675 newP.setStrokeCap(SkPaint::kButt_Cap);
676 }
677
678 if (fDevice) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000679 fDevice->drawPoints(*this,
robertphillips@google.com6d875572012-12-17 18:56:29 +0000680 SkCanvas::kPoints_PointMode,
681 pointData.fNumPoints,
682 pointData.fPoints,
683 newP);
684 } else {
685 this->drawPoints(SkCanvas::kPoints_PointMode,
686 pointData.fNumPoints,
687 pointData.fPoints,
688 newP,
689 forceUseDevice);
690 }
691 break;
692 } else {
693 // The rest of the dashed line must be drawn as rects
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000694 SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
robertphillips@google.com6d875572012-12-17 18:56:29 +0000695 pointData.fFlags));
696
697 SkRect r;
698
699 for (int i = 0; i < pointData.fNumPoints; ++i) {
700 r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
701 pointData.fPoints[i].fY - pointData.fSize.fY,
702 pointData.fPoints[i].fX + pointData.fSize.fX,
703 pointData.fPoints[i].fY + pointData.fSize.fY);
704 if (fDevice) {
705 fDevice->drawRect(*this, r, newP);
706 } else {
707 this->drawRect(r, newP);
708 }
709 }
710 }
711
robertphillips@google.com629ab542012-11-28 17:18:11 +0000712 break;
713 }
714 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000715 // couldn't take fast path so fall through!
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 case SkCanvas::kPolygon_PointMode: {
717 count -= 1;
718 SkPath path;
719 SkPaint p(paint);
720 p.setStyle(SkPaint::kStroke_Style);
721 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
jvanverthb3eb6872014-10-24 07:12:51 -0700722 path.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723 for (size_t i = 0; i < count; i += inc) {
724 path.moveTo(pts[i]);
725 path.lineTo(pts[i+1]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000726 if (fDevice) {
halcanary96fcdcc2015-08-27 07:41:13 -0700727 fDevice->drawPath(*this, path, p, nullptr, true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000728 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700729 this->drawPath(path, p, nullptr, true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000730 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 path.rewind();
732 }
733 break;
734 }
735 }
736 }
737}
738
fmalita1a178ca2015-01-15 06:01:23 -0800739static inline SkPoint compute_stroke_size(const SkPaint& paint, const SkMatrix& matrix) {
740 SkASSERT(matrix.rectStaysRect());
741 SkASSERT(SkPaint::kFill_Style != paint.getStyle());
742
743 SkVector size;
744 SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
745 matrix.mapVectors(&size, &pt, 1);
746 return SkPoint::Make(SkScalarAbs(size.fX), SkScalarAbs(size.fY));
747}
748
reed@google.com761fb622011-04-04 18:58:05 +0000749static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
750 SkPoint* strokeSize) {
751 if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
752 paint.getStrokeMiter() < SK_ScalarSqrt2) {
753 return false;
754 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000755
fmalita1a178ca2015-01-15 06:01:23 -0800756 *strokeSize = compute_stroke_size(paint, matrix);
reed@google.com761fb622011-04-04 18:58:05 +0000757 return true;
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000758}
759
reed@google.com62ab7ad2011-04-05 14:08:25 +0000760SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
761 const SkMatrix& matrix,
762 SkPoint* strokeSize) {
763 RectType rtype;
764 const SkScalar width = paint.getStrokeWidth();
765 const bool zeroWidth = (0 == width);
766 SkPaint::Style style = paint.getStyle();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000767
reed@google.com62ab7ad2011-04-05 14:08:25 +0000768 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
769 style = SkPaint::kFill_Style;
770 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000771
reed@google.com62ab7ad2011-04-05 14:08:25 +0000772 if (paint.getPathEffect() || paint.getMaskFilter() ||
773 paint.getRasterizer() || !matrix.rectStaysRect() ||
774 SkPaint::kStrokeAndFill_Style == style) {
775 rtype = kPath_RectType;
776 } else if (SkPaint::kFill_Style == style) {
777 rtype = kFill_RectType;
778 } else if (zeroWidth) {
779 rtype = kHair_RectType;
780 } else if (easy_rect_join(paint, matrix, strokeSize)) {
781 rtype = kStroke_RectType;
782 } else {
783 rtype = kPath_RectType;
784 }
785 return rtype;
786}
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000787
reed@google.com73244152012-05-11 14:35:23 +0000788static const SkPoint* rect_points(const SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000789 return SkTCast<const SkPoint*>(&r);
reed@google.com73244152012-05-11 14:35:23 +0000790}
791
792static SkPoint* rect_points(SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000793 return SkTCast<SkPoint*>(&r);
reed@google.com40c2ba22011-07-25 19:41:22 +0000794}
795
reed03939122014-12-15 13:42:51 -0800796void SkDraw::drawRect(const SkRect& prePaintRect, const SkPaint& paint,
797 const SkMatrix* paintMatrix, const SkRect* postPaintRect) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000798 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799
800 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000801 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000802 return;
803 }
804
reed03939122014-12-15 13:42:51 -0800805 const SkMatrix* matrix;
806 SkMatrix combinedMatrixStorage;
807 if (paintMatrix) {
808 SkASSERT(postPaintRect);
809 combinedMatrixStorage.setConcat(*fMatrix, *paintMatrix);
810 matrix = &combinedMatrixStorage;
811 } else {
812 SkASSERT(!postPaintRect);
813 matrix = fMatrix;
814 }
815
reed@google.com761fb622011-04-04 18:58:05 +0000816 SkPoint strokeSize;
reed@google.com62ab7ad2011-04-05 14:08:25 +0000817 RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000818
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000819 if (kPath_RectType == rtype) {
reed03939122014-12-15 13:42:51 -0800820 SkDraw draw(*this);
821 if (paintMatrix) {
822 draw.fMatrix = matrix;
823 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 SkPath tmp;
reed03939122014-12-15 13:42:51 -0800825 tmp.addRect(prePaintRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -0700827 draw.drawPath(tmp, paint, nullptr, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000828 return;
829 }
830
reed03939122014-12-15 13:42:51 -0800831 SkRect devRect;
fmalita1a178ca2015-01-15 06:01:23 -0800832 const SkRect& paintRect = paintMatrix ? *postPaintRect : prePaintRect;
833 // skip the paintMatrix when transforming the rect by the CTM
834 fMatrix->mapPoints(rect_points(devRect), rect_points(paintRect), 2);
reed@google.com73244152012-05-11 14:35:23 +0000835 devRect.sort();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837 // look for the quick exit, before we build a blitter
fmalita1a178ca2015-01-15 06:01:23 -0800838 SkRect bbox = devRect;
reed@google.com1c028bd2013-08-28 15:23:19 +0000839 if (paint.getStyle() != SkPaint::kFill_Style) {
840 // extra space for hairlines
georgeb3eba472014-09-09 11:33:57 -0700841 if (paint.getStrokeWidth() == 0) {
fmalita1a178ca2015-01-15 06:01:23 -0800842 bbox.outset(1, 1);
georgeb3eba472014-09-09 11:33:57 -0700843 } else {
fmalita1a178ca2015-01-15 06:01:23 -0800844 // For kStroke_RectType, strokeSize is already computed.
845 const SkPoint& ssize = (kStroke_RectType == rtype)
846 ? strokeSize
847 : compute_stroke_size(paint, *fMatrix);
848 bbox.outset(SkScalarHalf(ssize.x()), SkScalarHalf(ssize.y()));
georgeb3eba472014-09-09 11:33:57 -0700849 }
reed@google.com1c028bd2013-08-28 15:23:19 +0000850 }
fmalita1a178ca2015-01-15 06:01:23 -0800851
852 SkIRect ir = bbox.roundOut();
reed@google.com1c028bd2013-08-28 15:23:19 +0000853 if (fRC->quickReject(ir)) {
854 return;
rmistry@google.come09d6f42013-08-27 18:53:41 +0000855 }
856
reed41e010c2015-06-09 12:16:53 -0700857 SkDeviceLooper looper(fDst, *fRC, ir, paint.isAntiAlias());
reed@google.com1c028bd2013-08-28 15:23:19 +0000858 while (looper.next()) {
859 SkRect localDevRect;
860 looper.mapRect(&localDevRect, devRect);
861 SkMatrix localMatrix;
reed03939122014-12-15 13:42:51 -0800862 looper.mapMatrix(&localMatrix, *matrix);
rmistry@google.come09d6f42013-08-27 18:53:41 +0000863
reed41e010c2015-06-09 12:16:53 -0700864 SkAutoBlitterChoose blitterStorage(looper.getPixmap(), localMatrix, paint);
reed@google.com1c028bd2013-08-28 15:23:19 +0000865 const SkRasterClip& clip = looper.getRC();
866 SkBlitter* blitter = blitterStorage.get();
867
868 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
869 // case we are also hairline (if we've gotten to here), which devolves to
870 // effectively just kFill
871 switch (rtype) {
872 case kFill_RectType:
873 if (paint.isAntiAlias()) {
874 SkScan::AntiFillRect(localDevRect, clip, blitter);
875 } else {
876 SkScan::FillRect(localDevRect, clip, blitter);
877 }
878 break;
879 case kStroke_RectType:
880 if (paint.isAntiAlias()) {
881 SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter);
882 } else {
883 SkScan::FrameRect(localDevRect, strokeSize, clip, blitter);
884 }
885 break;
886 case kHair_RectType:
887 if (paint.isAntiAlias()) {
888 SkScan::AntiHairRect(localDevRect, clip, blitter);
889 } else {
890 SkScan::HairRect(localDevRect, clip, blitter);
891 }
892 break;
893 default:
894 SkDEBUGFAIL("bad rtype");
895 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 }
897}
898
899void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
900 if (srcM.fBounds.isEmpty()) {
901 return;
902 }
903
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000904 const SkMask* mask = &srcM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000906 SkMask dstM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907 if (paint.getMaskFilter() &&
halcanary96fcdcc2015-08-27 07:41:13 -0700908 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, nullptr)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909 mask = &dstM;
bungeman@google.combf2ac7e2011-09-26 19:51:33 +0000910 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700911 dstM.fImage = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000913 SkAutoMaskFreeImage ami(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914
reed41e010c2015-06-09 12:16:53 -0700915 SkAutoBlitterChoose blitterChooser(fDst, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +0000916 SkBlitter* blitter = blitterChooser.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000917
reed@google.com045e62d2011-10-24 12:19:46 +0000918 SkAAClipBlitterWrapper wrapper;
919 const SkRegion* clipRgn;
920
921 if (fRC->isBW()) {
922 clipRgn = &fRC->bwRgn();
923 } else {
924 wrapper.init(*fRC, blitter);
925 clipRgn = &wrapper.getRgn();
926 blitter = wrapper.getBlitter();
927 }
928 blitter->blitMaskRegion(*mask, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929}
930
reed@android.comebdeeb82009-09-03 21:45:49 +0000931static SkScalar fast_len(const SkVector& vec) {
932 SkScalar x = SkScalarAbs(vec.fX);
933 SkScalar y = SkScalarAbs(vec.fY);
934 if (x < y) {
935 SkTSwap(x, y);
936 }
937 return x + SkScalarHalf(y);
938}
939
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000940bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix,
941 SkScalar* coverage) {
942 SkASSERT(strokeWidth > 0);
943 // We need to try to fake a thick-stroke with a modulated hairline.
reed@google.comecadf992011-09-19 19:18:18 +0000944
tomhudson@google.com8d430182011-06-06 19:11:19 +0000945 if (matrix.hasPerspective()) {
reed@android.comebdeeb82009-09-03 21:45:49 +0000946 return false;
947 }
reed@google.comecadf992011-09-19 19:18:18 +0000948
reed@android.comebdeeb82009-09-03 21:45:49 +0000949 SkVector src[2], dst[2];
reed@google.comecadf992011-09-19 19:18:18 +0000950 src[0].set(strokeWidth, 0);
951 src[1].set(0, strokeWidth);
reed@android.comebdeeb82009-09-03 21:45:49 +0000952 matrix.mapVectors(dst, src, 2);
953 SkScalar len0 = fast_len(dst[0]);
954 SkScalar len1 = fast_len(dst[1]);
agl@chromium.org652807b2010-04-27 15:47:34 +0000955 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
bsalomon49f085d2014-09-05 13:34:00 -0700956 if (coverage) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000957 *coverage = SkScalarAve(len0, len1);
958 }
reed@android.comebdeeb82009-09-03 21:45:49 +0000959 return true;
960 }
961 return false;
962}
963
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000964void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const {
965 SkDEBUGCODE(this->validate());
966
967 if (fRC->isEmpty()) {
968 return;
969 }
970
971 {
972 // TODO: Investigate optimizing these options. They are in the same
973 // order as SkDraw::drawPath, which handles each case. It may be
974 // that there is no way to optimize for these using the SkRRect path.
975 SkScalar coverage;
976 if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) {
977 goto DRAW_PATH;
978 }
979
980 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
981 goto DRAW_PATH;
982 }
983
984 if (paint.getRasterizer()) {
985 goto DRAW_PATH;
986 }
987 }
988
989 if (paint.getMaskFilter()) {
990 // Transform the rrect into device space.
991 SkRRect devRRect;
992 if (rrect.transform(*fMatrix, &devRRect)) {
reed41e010c2015-06-09 12:16:53 -0700993 SkAutoBlitterChoose blitter(fDst, *fMatrix, paint);
reed868074b2014-06-03 10:53:59 -0700994 if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, blitter.get(),
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000995 SkPaint::kFill_Style)) {
996 return; // filterRRect() called the blitter, so we're done
997 }
998 }
999 }
1000
1001DRAW_PATH:
1002 // Now fall back to the default case of using a path.
1003 SkPath path;
1004 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -07001005 this->drawPath(path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +00001006}
1007
reed05d90442015-02-12 13:35:52 -08001008static SkScalar compute_res_scale_for_stroking(const SkMatrix& matrix) {
1009 if (!matrix.hasPerspective()) {
1010 SkScalar sx = SkPoint::Length(matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY]);
1011 SkScalar sy = SkPoint::Length(matrix[SkMatrix::kMSkewX], matrix[SkMatrix::kMScaleY]);
1012 if (SkScalarsAreFinite(sx, sy)) {
1013 return SkTMax(sx, sy);
1014 }
1015 }
1016 return 1;
1017}
1018
reed@google.com32e5d972011-07-27 18:25:57 +00001019void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
reed@google.com126f7f52013-11-07 16:06:53 +00001020 const SkMatrix* prePathMatrix, bool pathIsMutable,
krajcevski53f09592014-08-06 11:12:14 -07001021 bool drawCoverage, SkBlitter* customBlitter) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001022 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023
1024 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001025 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026 return;
1027 }
1028
1029 SkPath* pathPtr = (SkPath*)&origSrcPath;
1030 bool doFill = true;
1031 SkPath tmpPath;
1032 SkMatrix tmpMatrix;
1033 const SkMatrix* matrix = fMatrix;
jvanverthb3eb6872014-10-24 07:12:51 -07001034 tmpPath.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001035
1036 if (prePathMatrix) {
reed@google.com32e5d972011-07-27 18:25:57 +00001037 if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
1038 origPaint.getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039 SkPath* result = pathPtr;
reed@google.coma76de3d2011-01-13 18:30:42 +00001040
reed@android.com8a1c16f2008-12-17 15:59:43 +00001041 if (!pathIsMutable) {
1042 result = &tmpPath;
1043 pathIsMutable = true;
1044 }
1045 pathPtr->transform(*prePathMatrix, result);
1046 pathPtr = result;
1047 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001048 tmpMatrix.setConcat(*matrix, *prePathMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001049 matrix = &tmpMatrix;
1050 }
1051 }
1052 // at this point we're done with prePathMatrix
1053 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.coma76de3d2011-01-13 18:30:42 +00001054
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001055 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
reed@google.coma76de3d2011-01-13 18:30:42 +00001056
reed@google.comecadf992011-09-19 19:18:18 +00001057 {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001058 SkScalar coverage;
1059 if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1060 if (SK_Scalar1 == coverage) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001061 paint.writable()->setStrokeWidth(0);
egdanieldcfb7cf2015-01-22 06:52:29 -08001062 } else if (SkXfermode::SupportsCoverageAsAlpha(origPaint.getXfermode())) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001063 U8CPU newAlpha;
1064#if 0
1065 newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1066 origPaint.getAlpha()));
1067#else
1068 // this is the old technique, which we preserve for now so
1069 // we don't change previous results (testing)
1070 // the new way seems fine, its just (a tiny bit) different
1071 int scale = (int)SkScalarMul(coverage, 256);
1072 newAlpha = origPaint.getAlpha() * scale >> 8;
1073#endif
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001074 SkPaint* writablePaint = paint.writable();
1075 writablePaint->setStrokeWidth(0);
1076 writablePaint->setAlpha(newAlpha);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001077 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001078 }
1079 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001080
reed@google.com32e5d972011-07-27 18:25:57 +00001081 if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
reed@google.com4bbdeac2013-01-24 21:03:11 +00001082 SkRect cullRect;
halcanary96fcdcc2015-08-27 07:41:13 -07001083 const SkRect* cullRectPtr = nullptr;
reed@google.com4bbdeac2013-01-24 21:03:11 +00001084 if (this->computeConservativeLocalClipBounds(&cullRect)) {
1085 cullRectPtr = &cullRect;
1086 }
reed05d90442015-02-12 13:35:52 -08001087 doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr,
1088 compute_res_scale_for_stroking(*fMatrix));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089 pathPtr = &tmpPath;
1090 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001091
reed@google.com32e5d972011-07-27 18:25:57 +00001092 if (paint->getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001093 SkMask mask;
reed@google.com32e5d972011-07-27 18:25:57 +00001094 if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
reed@google.com045e62d2011-10-24 12:19:46 +00001095 &fRC->getBounds(), paint->getMaskFilter(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
reed@google.com32e5d972011-07-27 18:25:57 +00001097 this->drawDevMask(mask, *paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 SkMask::FreeImage(mask.fImage);
1099 }
1100 return;
1101 }
1102
1103 // avoid possibly allocating a new path in transform if we can
1104 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1105
1106 // transform the path into device space
1107 pathPtr->transform(*matrix, devPathPtr);
1108
halcanary96fcdcc2015-08-27 07:41:13 -07001109 SkBlitter* blitter = nullptr;
krajcevski53f09592014-08-06 11:12:14 -07001110 SkAutoBlitterChoose blitterStorage;
halcanary96fcdcc2015-08-27 07:41:13 -07001111 if (nullptr == customBlitter) {
reed41e010c2015-06-09 12:16:53 -07001112 blitterStorage.choose(fDst, *fMatrix, *paint, drawCoverage);
krajcevski53f09592014-08-06 11:12:14 -07001113 blitter = blitterStorage.get();
1114 } else {
1115 blitter = customBlitter;
1116 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001118 if (paint->getMaskFilter()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001119 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001120 SkPaint::kStroke_Style;
krajcevski53f09592014-08-06 11:12:14 -07001121 if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC, blitter, style)) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001122 return; // filterPath() called the blitter, so we're done
1123 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001124 }
1125
reed@google.com045e62d2011-10-24 12:19:46 +00001126 void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001127 if (doFill) {
reed@google.com32e5d972011-07-27 18:25:57 +00001128 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001129 proc = SkScan::AntiFillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001131 proc = SkScan::FillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132 }
1133 } else { // hairline
reed@google.com32e5d972011-07-27 18:25:57 +00001134 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001135 proc = SkScan::AntiHairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001137 proc = SkScan::HairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138 }
1139 }
krajcevski53f09592014-08-06 11:12:14 -07001140 proc(*devPathPtr, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141}
1142
reed@android.com0baf1932009-06-24 12:41:42 +00001143/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
1144 go ahead and treat it as if it were, so that subsequent code can go fast.
1145 */
1146static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
reed@google.com070dcd82013-01-07 20:27:52 +00001147 unsigned bits = 0; // TODO: find a way to allow the caller to tell us to
1148 // respect filtering.
1149 return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150}
1151
1152void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1153 const SkPaint& paint) const {
reed@google.com900ecf22014-02-20 20:55:37 +00001154 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155
reed@google.coma76de3d2011-01-13 18:30:42 +00001156 if (just_translate(*fMatrix, bitmap)) {
reed@google.come1ca7052013-12-17 19:22:07 +00001157 int ix = SkScalarRoundToInt(fMatrix->getTranslateX());
1158 int iy = SkScalarRoundToInt(fMatrix->getTranslateY());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159
reed92fc2ae2015-05-22 08:06:21 -07001160 SkAutoPixmapUnlock result;
1161 if (!bitmap.requestLock(&result)) {
reed@google.coma641f3f2012-12-13 22:16:30 +00001162 return;
1163 }
reed92fc2ae2015-05-22 08:06:21 -07001164 const SkPixmap& pmap = result.pixmap();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165 SkMask mask;
reed92fc2ae2015-05-22 08:06:21 -07001166 mask.fBounds.set(ix, iy, ix + pmap.width(), iy + pmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001167 mask.fFormat = SkMask::kA8_Format;
reed92fc2ae2015-05-22 08:06:21 -07001168 mask.fRowBytes = SkToU32(pmap.rowBytes());
1169 // fImage is typed as writable, but in this case it is used read-only
1170 mask.fImage = (uint8_t*)pmap.addr8(0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001171
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172 this->drawDevMask(mask, paint);
1173 } else { // need to xform the bitmap first
1174 SkRect r;
1175 SkMask mask;
reed@google.coma76de3d2011-01-13 18:30:42 +00001176
reed@android.com8a1c16f2008-12-17 15:59:43 +00001177 r.set(0, 0,
1178 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1179 fMatrix->mapRect(&r);
1180 r.round(&mask.fBounds);
reed@google.coma76de3d2011-01-13 18:30:42 +00001181
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182 // set the mask's bounds to the transformed bitmap-bounds,
1183 // clipped to the actual device
1184 {
1185 SkIRect devBounds;
reed41e010c2015-06-09 12:16:53 -07001186 devBounds.set(0, 0, fDst.width(), fDst.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001187 // need intersect(l, t, r, b) on irect
1188 if (!mask.fBounds.intersect(devBounds)) {
1189 return;
1190 }
1191 }
reed@android.com543ed932009-04-24 12:43:40 +00001192
reed@android.com8a1c16f2008-12-17 15:59:43 +00001193 mask.fFormat = SkMask::kA8_Format;
1194 mask.fRowBytes = SkAlign4(mask.fBounds.width());
reed@android.com543ed932009-04-24 12:43:40 +00001195 size_t size = mask.computeImageSize();
1196 if (0 == size) {
1197 // the mask is too big to allocated, draw nothing
1198 return;
1199 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001200
1201 // allocate (and clear) our temp buffer to hold the transformed bitmap
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 SkAutoMalloc storage(size);
1203 mask.fImage = (uint8_t*)storage.get();
1204 memset(mask.fImage, 0, size);
reed@google.coma76de3d2011-01-13 18:30:42 +00001205
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1207 {
1208 SkBitmap device;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001209 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
1210 mask.fImage, mask.fRowBytes);
reed@google.coma76de3d2011-01-13 18:30:42 +00001211
reed@android.com8a1c16f2008-12-17 15:59:43 +00001212 SkCanvas c(device);
1213 // need the unclipped top/left for the translate
1214 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1215 -SkIntToScalar(mask.fBounds.fTop));
1216 c.concat(*fMatrix);
reed@android.com3469c762009-02-24 19:03:20 +00001217
1218 // We can't call drawBitmap, or we'll infinitely recurse. Instead
reed@android.comfb12c3e2009-03-05 20:43:42 +00001219 // we manually build a shader and draw that into our new mask
reed@android.com3469c762009-02-24 19:03:20 +00001220 SkPaint tmpPaint;
1221 tmpPaint.setFlags(paint.getFlags());
reed@google.com40c2ba22011-07-25 19:41:22 +00001222 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
reed@android.com3469c762009-02-24 19:03:20 +00001223 SkRect rr;
1224 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1225 SkIntToScalar(bitmap.height()));
reed@google.com40c2ba22011-07-25 19:41:22 +00001226 c.drawRect(rr, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001227 }
1228 this->drawDevMask(mask, paint);
1229 }
1230}
1231
reed@google.com045e62d2011-10-24 12:19:46 +00001232static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233 const SkRect& srcR) {
1234 SkRect dstR;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001235 m.mapRect(&dstR, srcR);
reedb07a94f2014-11-19 05:03:18 -08001236 return c.quickReject(dstR.roundOut());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001237}
1238
reed@google.com045e62d2011-10-24 12:19:46 +00001239static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240 int width, int height) {
1241 SkRect r;
1242 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1243 return clipped_out(matrix, clip, r);
1244}
1245
reedc240e712015-05-23 12:26:41 -07001246static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y, const SkPixmap& pmap) {
1247 return clip.isBW() || clip.quickContains(x, y, x + pmap.width(), y + pmap.height());
reed@google.com045e62d2011-10-24 12:19:46 +00001248}
1249
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
reed03939122014-12-15 13:42:51 -08001251 const SkRect* dstBounds, const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001252 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253
1254 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001255 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001256 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001257 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258 return;
1259 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001260
reed@google.com40c2ba22011-07-25 19:41:22 +00001261 SkPaint paint(origPaint);
1262 paint.setStyle(SkPaint::kFill_Style);
reed@google.coma76de3d2011-01-13 18:30:42 +00001263
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264 SkMatrix matrix;
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001265 matrix.setConcat(*fMatrix, prematrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266
reed@google.com045e62d2011-10-24 12:19:46 +00001267 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001268 return;
1269 }
1270
reed868074b2014-06-03 10:53:59 -07001271 if (bitmap.colorType() != kAlpha_8_SkColorType && just_translate(matrix, bitmap)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001272 //
1273 // It is safe to call lock pixels now, since we know the matrix is
1274 // (more or less) identity.
1275 //
reedc240e712015-05-23 12:26:41 -07001276 SkAutoPixmapUnlock unlocker;
1277 if (!bitmap.requestLock(&unlocker)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001278 return;
1279 }
reedc240e712015-05-23 12:26:41 -07001280 const SkPixmap& pmap = unlocker.pixmap();
reed@google.come1ca7052013-12-17 19:22:07 +00001281 int ix = SkScalarRoundToInt(matrix.getTranslateX());
1282 int iy = SkScalarRoundToInt(matrix.getTranslateY());
reedc240e712015-05-23 12:26:41 -07001283 if (clipHandlesSprite(*fRC, ix, iy, pmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001284 SkTBlitterAllocator allocator;
1285 // blitter will be owned by the allocator.
reed41e010c2015-06-09 12:16:53 -07001286 SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, paint, pmap, ix, iy, &allocator);
reed@google.com045e62d2011-10-24 12:19:46 +00001287 if (blitter) {
reedc240e712015-05-23 12:26:41 -07001288 SkScan::FillIRect(SkIRect::MakeXYWH(ix, iy, pmap.width(), pmap.height()),
1289 *fRC, blitter);
reed@google.com045e62d2011-10-24 12:19:46 +00001290 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001291 }
reedc240e712015-05-23 12:26:41 -07001292 // if !blitter, then we fall-through to the slower case
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001295
reed@android.com8a1c16f2008-12-17 15:59:43 +00001296 // now make a temp draw on the stack, and use it
1297 //
1298 SkDraw draw(*this);
1299 draw.fMatrix = &matrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001300
reed@google.com900ecf22014-02-20 20:55:37 +00001301 if (bitmap.colorType() == kAlpha_8_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001302 draw.drawBitmapAsMask(bitmap, paint);
1303 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +00001304 SkAutoBitmapShaderInstall install(bitmap, paint);
reed03939122014-12-15 13:42:51 -08001305 const SkPaint& paintWithShader = install.paintWithShader();
1306 const SkRect srcBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
1307 if (dstBounds) {
1308 this->drawRect(srcBounds, paintWithShader, &prematrix, dstBounds);
1309 } else {
1310 draw.drawRect(srcBounds, paintWithShader);
1311 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001312 }
1313}
1314
reedc240e712015-05-23 12:26:41 -07001315void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001316 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +00001317
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001319 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001321 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001322 return;
1323 }
1324
reedc240e712015-05-23 12:26:41 -07001325 const SkIRect bounds = SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326
reed@google.com045e62d2011-10-24 12:19:46 +00001327 if (fRC->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001328 return; // nothing to draw
1329 }
1330
reed@google.com40c2ba22011-07-25 19:41:22 +00001331 SkPaint paint(origPaint);
1332 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001333
reedc240e712015-05-23 12:26:41 -07001334 SkAutoPixmapUnlock unlocker;
1335 if (!bitmap.requestLock(&unlocker)) {
1336 return;
1337 }
1338 const SkPixmap& pmap = unlocker.pixmap();
1339
halcanary96fcdcc2015-08-27 07:41:13 -07001340 if (nullptr == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, pmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001341 SkTBlitterAllocator allocator;
1342 // blitter will be owned by the allocator.
reed41e010c2015-06-09 12:16:53 -07001343 SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, paint, pmap, x, y, &allocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001344 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001345 SkScan::FillIRect(bounds, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 return;
1347 }
1348 }
1349
reed@android.com8a1c16f2008-12-17 15:59:43 +00001350 SkMatrix matrix;
1351 SkRect r;
1352
1353 // get a scalar version of our rect
1354 r.set(bounds);
1355
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001356 // create shader with offset
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357 matrix.setTranslate(r.fLeft, r.fTop);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001358 SkAutoBitmapShaderInstall install(bitmap, paint, &matrix);
1359 const SkPaint& shaderPaint = install.paintWithShader();
reed@google.coma76de3d2011-01-13 18:30:42 +00001360
reed@android.com8a1c16f2008-12-17 15:59:43 +00001361 SkDraw draw(*this);
1362 matrix.reset();
1363 draw.fMatrix = &matrix;
1364 // call ourself with a rect
reed@google.coma76de3d2011-01-13 18:30:42 +00001365 // is this OK if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001366 draw.drawRect(r, shaderPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001367}
1368
1369///////////////////////////////////////////////////////////////////////////////
1370
1371#include "SkScalerContext.h"
1372#include "SkGlyphCache.h"
reed@google.come6913762012-08-07 15:19:47 +00001373#include "SkTextToPathIter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001374#include "SkUtils.h"
1375
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001376bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) {
1377 // hairline glyphs are fast enough so we don't need to cache them
1378 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
1379 return true;
1380 }
1381
1382 // we don't cache perspective
1383 if (ctm.hasPerspective()) {
1384 return true;
1385 }
1386
1387 SkMatrix textM;
1388 return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM));
1389}
1390
reed@android.com8a1c16f2008-12-17 15:59:43 +00001391void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1392 SkScalar x, SkScalar y,
1393 const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001394 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395
djsollen@google.com166e6532012-03-20 14:24:38 +00001396 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001397
1398 SkMatrix matrix;
1399 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1400 matrix.postTranslate(x, y);
1401
1402 const SkPath* iterPath;
1403 SkScalar xpos, prevXPos = 0;
1404
reed@google.com7b4531f2012-08-07 15:53:00 +00001405 while (iter.next(&iterPath, &xpos)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406 matrix.postTranslate(xpos - prevXPos, 0);
reed@google.com7b4531f2012-08-07 15:53:00 +00001407 if (iterPath) {
1408 const SkPaint& pnt = iter.getPaint();
1409 if (fDevice) {
1410 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1411 } else {
1412 this->drawPath(*iterPath, pnt, &matrix, false);
1413 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001414 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001415 prevXPos = xpos;
1416 }
1417}
1418
reed@android.com8a1c16f2008-12-17 15:59:43 +00001419// disable warning : local variable used without having been initialized
reed@google.coma76de3d2011-01-13 18:30:42 +00001420#if defined _WIN32 && _MSC_VER >= 1300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001421#pragma warning ( push )
1422#pragma warning ( disable : 4701 )
1423#endif
1424
herbf553e4e2015-11-09 08:51:56 -08001425////////////////////////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001426
bungeman79738cc2015-03-11 14:05:29 -07001427static void D1G_RectClip(const SkDraw1Glyph& state, Sk48Dot16 fx, Sk48Dot16 fy, const SkGlyph& glyph) {
bungemand095f2b2015-03-16 09:49:54 -07001428 // Prevent glyphs from being drawn outside of or straddling the edge of device space.
1429 if ((fx >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) ||
1430 (fx >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/) ||
1431 (fy >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) ||
1432 (fy >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/))
1433 {
1434 return;
1435 }
1436
bungeman79738cc2015-03-11 14:05:29 -07001437 int left = Sk48Dot16FloorToInt(fx);
1438 int top = Sk48Dot16FloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001439 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
halcanary96fcdcc2015-08-27 07:41:13 -07001440 SkASSERT((nullptr == state.fClip && state.fAAClip) ||
1441 (state.fClip && nullptr == state.fAAClip && state.fClip->isRect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001442
1443 left += glyph.fLeft;
1444 top += glyph.fTop;
1445
1446 int right = left + glyph.fWidth;
1447 int bottom = top + glyph.fHeight;
1448
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001449 SkMask mask;
1450 SkIRect storage;
1451 SkIRect* bounds = &mask.fBounds;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001452
tomhudson@google.com83a44462011-10-27 15:27:51 +00001453 mask.fBounds.set(left, top, right, bottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001454
tomhudson@google.com83a44462011-10-27 15:27:51 +00001455 // this extra test is worth it, assuming that most of the time it succeeds
1456 // since we can avoid writing to storage
1457 if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
1458 if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
1459 return;
1460 bounds = &storage;
1461 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001462
herbcd7f0352015-09-15 15:15:40 -07001463 uint8_t* aa = (uint8_t*)glyph.fImage;
halcanary96fcdcc2015-08-27 07:41:13 -07001464 if (nullptr == aa) {
herbcd7f0352015-09-15 15:15:40 -07001465 aa = (uint8_t*)state.fCache->findImage(glyph);
1466 if (nullptr == aa) {
1467 return; // can't rasterize glyph
1468 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001469 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001470
tomhudson@google.com83a44462011-10-27 15:27:51 +00001471 mask.fRowBytes = glyph.rowBytes();
1472 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1473 mask.fImage = aa;
reed@google.com5bdfb332013-05-02 18:55:44 +00001474 state.blitMask(mask, *bounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001475}
1476
bungeman79738cc2015-03-11 14:05:29 -07001477static void D1G_RgnClip(const SkDraw1Glyph& state, Sk48Dot16 fx, Sk48Dot16 fy, const SkGlyph& glyph) {
1478 int left = Sk48Dot16FloorToInt(fx);
1479 int top = Sk48Dot16FloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001480 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001481 SkASSERT(!state.fClip->isRect());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001482
1483 SkMask mask;
1484
1485 left += glyph.fLeft;
1486 top += glyph.fTop;
1487
1488 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001489 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001490
tomhudson@google.com83a44462011-10-27 15:27:51 +00001491 if (!clipper.done()) {
1492 const SkIRect& cr = clipper.rect();
herb8e740462015-09-09 14:48:33 -07001493 const uint8_t* aa = (uint8_t*)state.fCache->findImage(glyph);
halcanary96fcdcc2015-08-27 07:41:13 -07001494 if (nullptr == aa) {
herb8e740462015-09-09 14:48:33 -07001495 return;
tomhudson@google.com83a44462011-10-27 15:27:51 +00001496 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001497
tomhudson@google.com83a44462011-10-27 15:27:51 +00001498 mask.fRowBytes = glyph.rowBytes();
1499 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1500 mask.fImage = (uint8_t*)aa;
1501 do {
reed@google.com5bdfb332013-05-02 18:55:44 +00001502 state.blitMask(mask, cr);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001503 clipper.next();
1504 } while (!clipper.done());
1505 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001506}
1507
reed@google.comfd4236e2011-07-25 21:16:22 +00001508static bool hasCustomD1GProc(const SkDraw& draw) {
1509 return draw.fProcs && draw.fProcs->fD1GProc;
1510}
1511
1512static bool needsRasterTextBlit(const SkDraw& draw) {
1513 return !hasCustomD1GProc(draw);
1514}
1515
reed868074b2014-06-03 10:53:59 -07001516SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
1517 const SkPaint& pnt) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001518 fDraw = draw;
bungeman@google.com2211b622012-01-13 15:02:58 +00001519 fBlitter = blitter;
1520 fCache = cache;
reed@google.com5bdfb332013-05-02 18:55:44 +00001521 fPaint = &pnt;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522
bungeman@google.com94471032013-02-25 15:55:13 +00001523 if (cache->isSubpixel()) {
bungeman79738cc2015-03-11 14:05:29 -07001524 fHalfSampleX = fHalfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
bungeman@google.com94471032013-02-25 15:55:13 +00001525 } else {
bungeman79738cc2015-03-11 14:05:29 -07001526 fHalfSampleX = fHalfSampleY = SK_ScalarHalf;
bungeman@google.com94471032013-02-25 15:55:13 +00001527 }
1528
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001529 if (hasCustomD1GProc(*draw)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001530 // todo: fix this assumption about clips w/ custom
1531 fClip = draw->fClip;
1532 fClipBounds = fClip->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001533 return draw->fProcs->fD1GProc;
1534 }
1535
reed@google.com045e62d2011-10-24 12:19:46 +00001536 if (draw->fRC->isBW()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001537 fAAClip = nullptr;
reed@google.com045e62d2011-10-24 12:19:46 +00001538 fClip = &draw->fRC->bwRgn();
1539 fClipBounds = fClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001540 if (fClip->isRect()) {
1541 return D1G_RectClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001542 } else {
reed868074b2014-06-03 10:53:59 -07001543 return D1G_RgnClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001544 }
1545 } else { // aaclip
1546 fAAClip = &draw->fRC->aaRgn();
halcanary96fcdcc2015-08-27 07:41:13 -07001547 fClip = nullptr;
reed@google.com045e62d2011-10-24 12:19:46 +00001548 fClipBounds = fAAClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001549 return D1G_RectClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001550 }
1551}
1552
reed@google.com5bdfb332013-05-02 18:55:44 +00001553void SkDraw1Glyph::blitMaskAsSprite(const SkMask& mask) const {
1554 SkASSERT(SkMask::kARGB32_Format == mask.fFormat);
1555
1556 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001557 bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()),
1558 (SkPMColor*)mask.fImage, mask.fRowBytes);
reed@google.com5bdfb332013-05-02 18:55:44 +00001559
1560 fDraw->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), *fPaint);
1561}
1562
reed@android.com8a1c16f2008-12-17 15:59:43 +00001563///////////////////////////////////////////////////////////////////////////////
1564
1565void SkDraw::drawText(const char text[], size_t byteLength,
1566 SkScalar x, SkScalar y, const SkPaint& paint) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001567 SkASSERT(byteLength == 0 || text != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001568
reed@android.comf2b98d62010-12-20 18:26:13 +00001569 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001570
1571 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -07001572 if (text == nullptr || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001573 return;
1574 }
1575
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001576 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1577 // will fill if its frame-width is 0.
reed@google.comed43dff2013-06-04 16:56:27 +00001578 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001579 this->drawText_asPaths(text, byteLength, x, y, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001580 return;
1581 }
1582
herbe59124e2015-11-18 10:54:39 -08001583 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
robertphillips7b05ff12015-06-19 14:14:54 -07001584 SkAutoGlyphCache autoCache(paint, &fDevice->surfaceProps(), fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001585 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001586
reed@google.com045e62d2011-10-24 12:19:46 +00001587 SkAAClipBlitter aaBlitter;
1588 SkAutoBlitterChoose blitterChooser;
halcanary96fcdcc2015-08-27 07:41:13 -07001589 SkBlitter* blitter = nullptr;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001590 if (needsRasterTextBlit(*this)) {
reed41e010c2015-06-09 12:16:53 -07001591 blitterChooser.choose(fDst, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001592 blitter = blitterChooser.get();
1593 if (fRC->isAA()) {
1594 aaBlitter.init(blitter, &fRC->aaRgn());
1595 blitter = &aaBlitter;
1596 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001597 }
1598
bungeman@google.com52c748b2011-08-22 21:30:43 +00001599 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001600 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001601
herbe59124e2015-11-18 10:54:39 -08001602 SkFindAndPlaceGlyph::ProcessText(
1603 text, byteLength, {x, y}, *fMatrix, paint.getTextAlign(), glyphCacheProc, cache,
1604 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
1605 position += rounding;
1606 proc(d1g, SkScalarTo48Dot16(position.fX), SkScalarTo48Dot16(position.fY), glyph);
bungeman@google.com94471032013-02-25 15:55:13 +00001607 }
herbe59124e2015-11-18 10:54:39 -08001608 );
reed@android.com8a1c16f2008-12-17 15:59:43 +00001609}
1610
reed@android.com8a1c16f2008-12-17 15:59:43 +00001611//////////////////////////////////////////////////////////////////////////////
1612
reed@google.comed43dff2013-06-04 16:56:27 +00001613void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001614 const SkScalar pos[], int scalarsPerPosition,
1615 const SkPoint& offset, const SkPaint& origPaint) const {
reed@google.comed43dff2013-06-04 16:56:27 +00001616 // setup our std paint, in hopes of getting hits in the cache
1617 SkPaint paint(origPaint);
1618 SkScalar matrixScale = paint.setupForAsPaths();
1619
reed@google.com5a649022013-06-05 18:00:30 +00001620 SkMatrix matrix;
1621 matrix.setScale(matrixScale, matrixScale);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001622
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001623 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
1624 paint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001625 paint.setPathEffect(nullptr);
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001626
reed@google.comed43dff2013-06-04 16:56:27 +00001627 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
halcanary96fcdcc2015-08-27 07:41:13 -07001628 SkAutoGlyphCache autoCache(paint, &fDevice->surfaceProps(), nullptr);
reed@google.comed43dff2013-06-04 16:56:27 +00001629 SkGlyphCache* cache = autoCache.getCache();
1630
1631 const char* stop = text + byteLength;
bungeman79738cc2015-03-11 14:05:29 -07001632 SkTextAlignProc alignProc(paint.getTextAlign());
fmalita05c4a432014-09-29 06:29:53 -07001633 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001634
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001635 // Now restore the original settings, so we "draw" with whatever style/stroking.
1636 paint.setStyle(origPaint.getStyle());
1637 paint.setPathEffect(origPaint.getPathEffect());
1638
reed@google.comed43dff2013-06-04 16:56:27 +00001639 while (text < stop) {
1640 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1641 if (glyph.fWidth) {
1642 const SkPath* path = cache->findPath(glyph);
1643 if (path) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001644 SkPoint tmsLoc;
1645 tmsProc(pos, &tmsLoc);
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001646 SkPoint loc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001647 alignProc(tmsLoc, glyph, &loc);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001648
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001649 matrix[SkMatrix::kMTransX] = loc.fX;
1650 matrix[SkMatrix::kMTransY] = loc.fY;
reed@google.com5a649022013-06-05 18:00:30 +00001651 if (fDevice) {
1652 fDevice->drawPath(*this, *path, paint, &matrix, false);
1653 } else {
1654 this->drawPath(*path, paint, &matrix, false);
1655 }
reed@google.comed43dff2013-06-04 16:56:27 +00001656 }
1657 }
1658 pos += scalarsPerPosition;
1659 }
1660}
1661
reed@android.com8a1c16f2008-12-17 15:59:43 +00001662void SkDraw::drawPosText(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001663 const SkScalar pos[], int scalarsPerPosition,
1664 const SkPoint& offset, const SkPaint& paint) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001665 SkASSERT(byteLength == 0 || text != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001666 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1667
reed@android.comf2b98d62010-12-20 18:26:13 +00001668 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001669
1670 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -07001671 if (text == nullptr || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001672 return;
1673 }
1674
reed@google.comed43dff2013-06-04 16:56:27 +00001675 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
fmalita05c4a432014-09-29 06:29:53 -07001676 this->drawPosText_asPaths(text, byteLength, pos, scalarsPerPosition, offset, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001677 return;
1678 }
1679
herb9be5ff62015-11-11 11:30:11 -08001680 // The Blitter Choose needs to be live while using the blitter below.
1681 SkAutoBlitterChoose blitterChooser;
reed@google.com045e62d2011-10-24 12:19:46 +00001682 SkAAClipBlitterWrapper wrapper;
herb9be5ff62015-11-11 11:30:11 -08001683 SkBlitter* blitter = nullptr;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001684 if (needsRasterTextBlit(*this)) {
reed41e010c2015-06-09 12:16:53 -07001685 blitterChooser.choose(fDst, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001686 blitter = blitterChooser.get();
1687 if (fRC->isAA()) {
1688 wrapper.init(*fRC, blitter);
1689 blitter = wrapper.getBlitter();
1690 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001691 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001692
herb9be5ff62015-11-11 11:30:11 -08001693 SkAutoGlyphCache autoCache(paint, &fDevice->surfaceProps(), fMatrix);
1694 SkGlyphCache* cache = autoCache.getCache();
bungeman@google.com2211b622012-01-13 15:02:58 +00001695 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001696 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
herb9be5ff62015-11-11 11:30:11 -08001697 SkPaint::Align textAlignment = paint.getTextAlign();
1698 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001699
herb9be5ff62015-11-11 11:30:11 -08001700 SkFindAndPlaceGlyph::ProcessPosText(
1701 text, byteLength, offset, *fMatrix, pos, scalarsPerPosition,
1702 textAlignment, glyphCacheProc, cache,
herbf553e4e2015-11-09 08:51:56 -08001703 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
1704 position += rounding;
herbe59124e2015-11-18 10:54:39 -08001705 proc(d1g, SkScalarTo48Dot16(position.fX), SkScalarTo48Dot16(position.fY), glyph);
herb9be5ff62015-11-11 11:30:11 -08001706 }
1707 );
reed@android.com8a1c16f2008-12-17 15:59:43 +00001708}
1709
1710#if defined _WIN32 && _MSC_VER >= 1300
1711#pragma warning ( pop )
1712#endif
1713
1714///////////////////////////////////////////////////////////////////////////////
1715
reed5dc6b7d2015-04-14 10:40:44 -07001716static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001717 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
1718}
1719
1720static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
1721 const SkPoint texs[], SkMatrix* matrix) {
1722 SkPoint src[3], dst[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00001723
reed@android.com8a1c16f2008-12-17 15:59:43 +00001724 src[0] = texs[state.f0];
1725 src[1] = texs[state.f1];
1726 src[2] = texs[state.f2];
1727 dst[0] = verts[state.f0];
1728 dst[1] = verts[state.f1];
1729 dst[2] = verts[state.f2];
1730 return matrix->setPolyToPoly(src, dst, 3);
1731}
1732
1733class SkTriColorShader : public SkShader {
1734public:
1735 SkTriColorShader() {}
1736
mtklein36352bf2015-03-25 18:17:31 -07001737 size_t contextSize() const override;
reed@google.coma76de3d2011-01-13 18:30:42 +00001738
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001739 class TriColorShaderContext : public SkShader::Context {
1740 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00001741 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001742 virtual ~TriColorShaderContext();
1743
1744 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
1745
mtklein36352bf2015-03-25 18:17:31 -07001746 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001747
1748 private:
1749 SkMatrix fDstToUnit;
1750 SkPMColor fColors[3];
1751
1752 typedef SkShader::Context INHERITED;
1753 };
reed@google.coma76de3d2011-01-13 18:30:42 +00001754
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001755 SK_TO_STRING_OVERRIDE()
mtklein7e44bb12015-01-07 09:06:08 -08001756
1757 // For serialization. This will never be called.
halcanary96fcdcc2015-08-27 07:41:13 -07001758 Factory getFactory() const override { sk_throw(); return nullptr; }
djsollen@google.comba28d032012-03-26 17:57:35 +00001759
reed@android.com8a1c16f2008-12-17 15:59:43 +00001760protected:
mtklein36352bf2015-03-25 18:17:31 -07001761 Context* onCreateContext(const ContextRec& rec, void* storage) const override {
halcanary385fe4d2015-08-26 13:07:48 -07001762 return new (storage) TriColorShaderContext(*this, rec);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +00001763 }
1764
reed@android.com8a1c16f2008-12-17 15:59:43 +00001765private:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001766 typedef SkShader INHERITED;
1767};
1768
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001769bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
1770 int index0, int index1, int index2) {
reed@google.coma76de3d2011-01-13 18:30:42 +00001771
reed@android.com8a1c16f2008-12-17 15:59:43 +00001772 fColors[0] = SkPreMultiplyColor(colors[index0]);
1773 fColors[1] = SkPreMultiplyColor(colors[index1]);
1774 fColors[2] = SkPreMultiplyColor(colors[index2]);
reed@google.coma76de3d2011-01-13 18:30:42 +00001775
reed@android.com8a1c16f2008-12-17 15:59:43 +00001776 SkMatrix m, im;
1777 m.reset();
1778 m.set(0, pts[index1].fX - pts[index0].fX);
1779 m.set(1, pts[index2].fX - pts[index0].fX);
1780 m.set(2, pts[index0].fX);
1781 m.set(3, pts[index1].fY - pts[index0].fY);
1782 m.set(4, pts[index2].fY - pts[index0].fY);
1783 m.set(5, pts[index0].fY);
1784 if (!m.invert(&im)) {
1785 return false;
1786 }
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001787 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix
1788 // as our interators are intrinsically tied to the vertices, and nothing else.
1789 SkMatrix ctmInv;
1790 if (!this->getCTM().invert(&ctmInv)) {
1791 return false;
1792 }
1793 fDstToUnit.setConcat(im, ctmInv);
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001794 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001795}
1796
1797#include "SkColorPriv.h"
reed@android.com689411a2008-12-18 02:52:32 +00001798#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001799
1800static int ScalarTo256(SkScalar v) {
1801 int scale = SkScalarToFixed(v) >> 8;
1802 if (scale < 0) {
1803 scale = 0;
1804 }
1805 if (scale > 255) {
1806 scale = 255;
1807 }
1808 return SkAlpha255To256(scale);
1809}
1810
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001811
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00001812SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorShader& shader,
1813 const ContextRec& rec)
1814 : INHERITED(shader, rec) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001815
1816SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
1817
1818size_t SkTriColorShader::contextSize() const {
1819 return sizeof(TriColorShaderContext);
1820}
1821void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001822 const int alphaScale = Sk255To256(this->getPaintAlpha());
1823
reed@android.com8a1c16f2008-12-17 15:59:43 +00001824 SkPoint src;
reed@google.coma76de3d2011-01-13 18:30:42 +00001825
reed@android.com8a1c16f2008-12-17 15:59:43 +00001826 for (int i = 0; i < count; i++) {
1827 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
1828 x += 1;
reed@google.coma76de3d2011-01-13 18:30:42 +00001829
reed@android.com8a1c16f2008-12-17 15:59:43 +00001830 int scale1 = ScalarTo256(src.fX);
1831 int scale2 = ScalarTo256(src.fY);
1832 int scale0 = 256 - scale1 - scale2;
1833 if (scale0 < 0) {
1834 if (scale1 > scale2) {
1835 scale2 = 256 - scale1;
1836 } else {
1837 scale1 = 256 - scale2;
1838 }
1839 scale0 = 0;
1840 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001841
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001842 if (256 != alphaScale) {
1843 scale0 = SkAlphaMul(scale0, alphaScale);
1844 scale1 = SkAlphaMul(scale1, alphaScale);
1845 scale2 = SkAlphaMul(scale2, alphaScale);
1846 }
1847
reed@android.com8a1c16f2008-12-17 15:59:43 +00001848 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001849 SkAlphaMulQ(fColors[1], scale1) +
1850 SkAlphaMulQ(fColors[2], scale2);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001851 }
1852}
1853
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001854#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001855void SkTriColorShader::toString(SkString* str) const {
1856 str->append("SkTriColorShader: (");
1857
1858 this->INHERITED::toString(str);
1859
1860 str->append(")");
1861}
1862#endif
1863
reed@android.com8a1c16f2008-12-17 15:59:43 +00001864void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
1865 const SkPoint vertices[], const SkPoint textures[],
1866 const SkColor colors[], SkXfermode* xmode,
1867 const uint16_t indices[], int indexCount,
1868 const SkPaint& paint) const {
bsalomon49f085d2014-09-05 13:34:00 -07001869 SkASSERT(0 == count || vertices);
reed@google.coma76de3d2011-01-13 18:30:42 +00001870
reed@android.com8a1c16f2008-12-17 15:59:43 +00001871 // abort early if there is nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001872 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001873 return;
1874 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001875
reed@android.com8a1c16f2008-12-17 15:59:43 +00001876 // transform out vertices into device coordinates
1877 SkAutoSTMalloc<16, SkPoint> storage(count);
1878 SkPoint* devVerts = storage.get();
1879 fMatrix->mapPoints(devVerts, vertices, count);
reed@google.coma76de3d2011-01-13 18:30:42 +00001880
reed@android.com8a1c16f2008-12-17 15:59:43 +00001881 /*
1882 We can draw the vertices in 1 of 4 ways:
1883
1884 - solid color (no shader/texture[], no colors[])
1885 - just colors (no shader/texture[], has colors[])
1886 - just texture (has shader/texture[], no colors[])
1887 - colors * texture (has shader/texture[], has colors[])
reed@google.coma76de3d2011-01-13 18:30:42 +00001888
reed@android.com8a1c16f2008-12-17 15:59:43 +00001889 Thus for texture drawing, we need both texture[] and a shader.
1890 */
1891
1892 SkTriColorShader triShader; // must be above declaration of p
1893 SkPaint p(paint);
1894
1895 SkShader* shader = p.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -07001896 if (nullptr == shader) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001897 // if we have no shader, we ignore the texture coordinates
halcanary96fcdcc2015-08-27 07:41:13 -07001898 textures = nullptr;
1899 } else if (nullptr == textures) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001900 // if we don't have texture coordinates, ignore the shader
halcanary96fcdcc2015-08-27 07:41:13 -07001901 p.setShader(nullptr);
1902 shader = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001903 }
1904
1905 // setup the custom shader (if needed)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001906 SkAutoTUnref<SkComposeShader> composeShader;
bsalomon49f085d2014-09-05 13:34:00 -07001907 if (colors) {
halcanary96fcdcc2015-08-27 07:41:13 -07001908 if (nullptr == textures) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001909 // just colors (no texture)
reed@google.coma641f3f2012-12-13 22:16:30 +00001910 shader = p.setShader(&triShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001911 } else {
1912 // colors * texture
1913 SkASSERT(shader);
1914 bool releaseMode = false;
halcanary96fcdcc2015-08-27 07:41:13 -07001915 if (nullptr == xmode) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001916 xmode = SkXfermode::Create(SkXfermode::kModulate_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001917 releaseMode = true;
1918 }
halcanary385fe4d2015-08-26 13:07:48 -07001919 composeShader.reset(new SkComposeShader(&triShader, shader, xmode));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001920 p.setShader(composeShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001921 if (releaseMode) {
1922 xmode->unref();
1923 }
1924 }
1925 }
1926
reed41e010c2015-06-09 12:16:53 -07001927 SkAutoBlitterChoose blitter(fDst, *fMatrix, p);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001928 // Abort early if we failed to create a shader context.
reed@google.comea033602012-12-14 13:13:55 +00001929 if (blitter->isNullBlitter()) {
1930 return;
1931 }
1932
reed@android.com8a1c16f2008-12-17 15:59:43 +00001933 // setup our state and function pointer for iterating triangles
1934 VertState state(count, indices, indexCount);
1935 VertState::Proc vertProc = state.chooseProc(vmode);
reed@google.coma76de3d2011-01-13 18:30:42 +00001936
bsalomon49f085d2014-09-05 13:34:00 -07001937 if (textures || colors) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001938 while (vertProc(&state)) {
bsalomon49f085d2014-09-05 13:34:00 -07001939 if (textures) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001940 SkMatrix tempM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001941 if (texture_to_matrix(state, vertices, textures, &tempM)) {
reed56263c72015-06-05 11:31:26 -07001942 SkShader::ContextRec rec(p, *fMatrix, &tempM);
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001943 if (!blitter->resetShaderContext(rec)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001944 continue;
1945 }
1946 }
1947 }
bsalomon49f085d2014-09-05 13:34:00 -07001948 if (colors) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001949 // Find the context for triShader.
1950 SkTriColorShader::TriColorShaderContext* triColorShaderContext;
1951
1952 SkShader::Context* shaderContext = blitter->getShaderContext();
1953 SkASSERT(shaderContext);
1954 if (p.getShader() == &triShader) {
1955 triColorShaderContext =
1956 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContext);
1957 } else {
1958 // The shader is a compose shader and triShader is its first shader.
1959 SkASSERT(p.getShader() == composeShader);
1960 SkASSERT(composeShader->getShaderA() == &triShader);
1961 SkComposeShader::ComposeShaderContext* composeShaderContext =
1962 static_cast<SkComposeShader::ComposeShaderContext*>(shaderContext);
1963 SkShader::Context* shaderContextA = composeShaderContext->getShaderContextA();
1964 triColorShaderContext =
1965 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContextA);
1966 }
1967
1968 if (!triColorShaderContext->setup(vertices, colors,
1969 state.f0, state.f1, state.f2)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001970 continue;
1971 }
1972 }
reed@google.com045e62d2011-10-24 12:19:46 +00001973
1974 SkPoint tmp[] = {
1975 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
1976 };
1977 SkScan::FillTriangle(tmp, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001978 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001979 } else {
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001980 // no colors[] and no texture, stroke hairlines with paint's color.
reed5dc6b7d2015-04-14 10:40:44 -07001981 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
reed@google.com045e62d2011-10-24 12:19:46 +00001982 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001983 while (vertProc(&state)) {
reed5dc6b7d2015-04-14 10:40:44 -07001984 SkPoint array[] = {
1985 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
1986 };
1987 hairProc(array, 4, clip, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001988 }
1989 }
1990}
1991
reed@google.com0a0a2362011-03-23 13:51:55 +00001992///////////////////////////////////////////////////////////////////////////////
1993///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001994
1995#ifdef SK_DEBUG
1996
reed@android.comf2b98d62010-12-20 18:26:13 +00001997void SkDraw::validate() const {
halcanary96fcdcc2015-08-27 07:41:13 -07001998 SkASSERT(fMatrix != nullptr);
1999 SkASSERT(fClip != nullptr);
2000 SkASSERT(fRC != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002001
reed@google.com045e62d2011-10-24 12:19:46 +00002002 const SkIRect& cr = fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002003 SkIRect br;
2004
reed41e010c2015-06-09 12:16:53 -07002005 br.set(0, 0, fDst.width(), fDst.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002006 SkASSERT(cr.isEmpty() || br.contains(cr));
2007}
2008
2009#endif
2010
reed@android.com8a1c16f2008-12-17 15:59:43 +00002011////////////////////////////////////////////////////////////////////////////////////////////////
2012
2013#include "SkPath.h"
2014#include "SkDraw.h"
2015#include "SkRegion.h"
2016#include "SkBlitter.h"
2017
2018static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
reedb07a94f2014-11-19 05:03:18 -08002019 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
reed@android.com8a1c16f2008-12-17 15:59:43 +00002020 SkIRect* bounds) {
2021 if (devPath.isEmpty()) {
2022 return false;
2023 }
2024
reed@android.com8a1c16f2008-12-17 15:59:43 +00002025 // init our bounds from the path
reed11fa2242015-03-13 06:08:28 -07002026 *bounds = devPath.getBounds().makeOutset(SK_ScalarHalf, SK_ScalarHalf).roundOut();
reed@google.coma76de3d2011-01-13 18:30:42 +00002027
tomhudson@google.com6db75fc2012-03-23 14:46:48 +00002028 SkIPoint margin = SkIPoint::Make(0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002029 if (filter) {
2030 SkASSERT(filterMatrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00002031
bungeman@google.com5af16f82011-09-02 15:06:44 +00002032 SkMask srcM, dstM;
reed@google.coma76de3d2011-01-13 18:30:42 +00002033
reed@android.com8a1c16f2008-12-17 15:59:43 +00002034 srcM.fBounds = *bounds;
2035 srcM.fFormat = SkMask::kA8_Format;
halcanary96fcdcc2015-08-27 07:41:13 -07002036 srcM.fImage = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002037 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2038 return false;
2039 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002040 }
2041
bungeman@google.com5af16f82011-09-02 15:06:44 +00002042 // (possibly) trim the bounds to reflect the clip
reed@android.com8a1c16f2008-12-17 15:59:43 +00002043 // (plus whatever slop the filter needs)
bungeman@google.com5af16f82011-09-02 15:06:44 +00002044 if (clipBounds) {
reed@android.com35555912009-03-16 18:46:55 +00002045 // Ugh. Guard against gigantic margins from wacky filters. Without this
2046 // check we can request arbitrary amounts of slop beyond our visible
2047 // clip, and bring down the renderer (at least on finite RAM machines
2048 // like handsets, etc.). Need to balance this invented value between
2049 // quality of large filters like blurs, and the corresponding memory
2050 // requests.
2051 static const int MAX_MARGIN = 128;
reed11fa2242015-03-13 06:08:28 -07002052 if (!bounds->intersect(clipBounds->makeOutset(SkMin32(margin.fX, MAX_MARGIN),
2053 SkMin32(margin.fY, MAX_MARGIN)))) {
bungeman@google.com5af16f82011-09-02 15:06:44 +00002054 return false;
2055 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002056 }
2057
2058 return true;
2059}
2060
reed41e010c2015-06-09 12:16:53 -07002061static void draw_into_mask(const SkMask& mask, const SkPath& devPath, SkPaint::Style style) {
2062 SkDraw draw;
2063 if (!draw.fDst.reset(mask)) {
2064 return;
2065 }
2066
reed@google.com045e62d2011-10-24 12:19:46 +00002067 SkRasterClip clip;
2068 SkMatrix matrix;
2069 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002070
reed@google.com045e62d2011-10-24 12:19:46 +00002071 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00002072 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2073 -SkIntToScalar(mask.fBounds.fTop));
2074
reed@google.com045e62d2011-10-24 12:19:46 +00002075 draw.fRC = &clip;
2076 draw.fClip = &clip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002077 draw.fMatrix = &matrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002078 paint.setAntiAlias(true);
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002079 paint.setStyle(style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002080 draw.drawPath(devPath, paint);
2081}
2082
2083bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002084 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002085 SkMask* mask, SkMask::CreateMode mode,
2086 SkPaint::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002087 if (SkMask::kJustRenderImage_CreateMode != mode) {
2088 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2089 return false;
2090 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002091
reed@android.com8a1c16f2008-12-17 15:59:43 +00002092 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2093 mask->fFormat = SkMask::kA8_Format;
2094 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +00002095 size_t size = mask->computeImageSize();
2096 if (0 == size) {
2097 // we're too big to allocate the mask, abort
2098 return false;
2099 }
2100 mask->fImage = SkMask::AllocImage(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002101 memset(mask->fImage, 0, mask->computeImageSize());
2102 }
2103
2104 if (SkMask::kJustComputeBounds_CreateMode != mode) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002105 draw_into_mask(*mask, devPath, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002106 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002107
reed@android.com8a1c16f2008-12-17 15:59:43 +00002108 return true;
2109}