blob: 70eb5732e13e7361b815b7494fce08c60c04433c [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000033#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000034#include "SkVertState.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036#include "SkBitmapProcShader.h"
37#include "SkDrawProcs.h"
reed@google.comae573582013-01-03 15:22:40 +000038#include "SkMatrixUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40//#define TRACE_BITMAP_DRAWS
41
herbd4c24f62015-12-07 12:12:29 -080042// Helper function to fix code gen bug on ARM64.
43// See SkFindAndPlaceGlyph.h for more details.
44void FixGCC49Arm64Bug(int v) { }
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
reed@google.comfd4236e2011-07-25 21:16:22 +000046/** Helper for allocating small blitters on the stack.
47 */
reed@google.com40c2ba22011-07-25 19:41:22 +000048class SkAutoBlitterChoose : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049public:
reed@google.com1d6ee0b2011-07-05 17:44:56 +000050 SkAutoBlitterChoose() {
halcanary96fcdcc2015-08-27 07:41:13 -070051 fBlitter = nullptr;
reed@google.com1d6ee0b2011-07-05 17:44:56 +000052 }
reed41e010c2015-06-09 12:16:53 -070053 SkAutoBlitterChoose(const SkPixmap& dst, const SkMatrix& matrix,
reed@google.com126f7f52013-11-07 16:06:53 +000054 const SkPaint& paint, bool drawCoverage = false) {
reed41e010c2015-06-09 12:16:53 -070055 fBlitter = SkBlitter::Choose(dst, matrix, paint, &fAllocator, drawCoverage);
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000056 }
herbe59124e2015-11-18 10:54:39 -080057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkBlitter* operator->() { return fBlitter; }
59 SkBlitter* get() const { return fBlitter; }
60
reed41e010c2015-06-09 12:16:53 -070061 void choose(const SkPixmap& dst, const SkMatrix& matrix,
krajcevski53f09592014-08-06 11:12:14 -070062 const SkPaint& paint, bool drawCoverage = false) {
reed@google.com1d6ee0b2011-07-05 17:44:56 +000063 SkASSERT(!fBlitter);
reed41e010c2015-06-09 12:16:53 -070064 fBlitter = SkBlitter::Choose(dst, matrix, paint, &fAllocator, drawCoverage);
reed@google.com1d6ee0b2011-07-05 17:44:56 +000065 }
66
reed@android.com8a1c16f2008-12-17 15:59:43 +000067private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000068 // Owned by fAllocator, which will handle the delete.
69 SkBlitter* fBlitter;
70 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +000071};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000072#define SkAutoBlitterChoose(...) SK_REQUIRE_LOCAL_VAR(SkAutoBlitterChoose)
reed@android.com8a1c16f2008-12-17 15:59:43 +000073
reed@google.com40c2ba22011-07-25 19:41:22 +000074/**
75 * Since we are providing the storage for the shader (to avoid the perf cost
76 * of calling new) we insist that in our destructor we can account for all
77 * owners of the shader.
78 */
79class SkAutoBitmapShaderInstall : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080public:
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000081 SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint,
halcanary96fcdcc2015-08-27 07:41:13 -070082 const SkMatrix* localMatrix = nullptr)
reed@google.com40c2ba22011-07-25 19:41:22 +000083 : fPaint(paint) /* makes a copy of the paint */ {
reed8a21c9f2016-03-08 18:50:00 -080084 fPaint.setShader(SkMakeBitmapShader(src, SkShader::kClamp_TileMode,
85 SkShader::kClamp_TileMode, localMatrix, &fAllocator));
reed@google.com40c2ba22011-07-25 19:41:22 +000086 // we deliberately left the shader with an owner-count of 2
reed8a21c9f2016-03-08 18:50:00 -080087 fPaint.getShader()->ref();
reed@google.com40c2ba22011-07-25 19:41:22 +000088 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 }
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 ~SkAutoBitmapShaderInstall() {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000092 // since fAllocator will destroy shader, we insist that owners == 2
93 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
halcanary96fcdcc2015-08-27 07:41:13 -070095 fPaint.setShader(nullptr); // unref the shader by 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 }
reed@google.com82065d62011-02-07 15:30:46 +000098
reed@google.com40c2ba22011-07-25 19:41:22 +000099 // return the new paint that has the shader applied
100 const SkPaint& paintWithShader() const { return fPaint; }
101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000103 // copy of caller's paint (which we then modify)
104 SkPaint fPaint;
105 // Stores the shader.
106 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000108#define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110///////////////////////////////////////////////////////////////////////////////
111
reed@android.comf2b98d62010-12-20 18:26:13 +0000112SkDraw::SkDraw() {
113 sk_bzero(this, sizeof(*this));
114}
115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116SkDraw::SkDraw(const SkDraw& src) {
117 memcpy(this, &src, sizeof(*this));
118}
119
reed@google.com4bbdeac2013-01-24 21:03:11 +0000120bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
121 if (fRC->isEmpty()) {
122 return false;
123 }
124
125 SkMatrix inverse;
126 if (!fMatrix->invert(&inverse)) {
127 return false;
128 }
129
130 SkIRect devBounds = fRC->getBounds();
131 // outset to have slop for antialasing and hairlines
132 devBounds.outset(1, 1);
133 inverse.mapRect(localBounds, SkRect::Make(devBounds));
134 return true;
135}
136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137///////////////////////////////////////////////////////////////////////////////
138
139typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
140
141static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
reed@android.com4516f472009-06-29 16:25:36 +0000142 sk_bzero(pixels, bytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143}
144
145static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
146
147static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000148 sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149}
150
151static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000152 sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153}
154
155static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
156 memset(pixels, data, bytes);
157}
158
reed41e010c2015-06-09 12:16:53 -0700159static BitmapXferProc ChooseBitmapXferProc(const SkPixmap& dst, const SkPaint& paint,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 uint32_t* data) {
161 // todo: we can apply colorfilter up front if no shader, so we wouldn't
162 // need to abort this fastpath
163 if (paint.getShader() || paint.getColorFilter()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700164 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 }
166
reed@android.com845fdac2009-06-23 03:01:32 +0000167 SkXfermode::Mode mode;
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000168 if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 SkColor color = paint.getColor();
reed@google.coma76de3d2011-01-13 18:30:42 +0000173
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 // collaps modes based on color...
reed@android.com845fdac2009-06-23 03:01:32 +0000175 if (SkXfermode::kSrcOver_Mode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 unsigned alpha = SkColorGetA(color);
177 if (0 == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000178 mode = SkXfermode::kDst_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 } else if (0xFF == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000180 mode = SkXfermode::kSrc_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 }
182 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000185 case SkXfermode::kClear_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186// SkDebugf("--- D_Clear_BitmapXferProc\n");
187 return D_Clear_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000188 case SkXfermode::kDst_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189// SkDebugf("--- D_Dst_BitmapXferProc\n");
190 return D_Dst_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000191 case SkXfermode::kSrc_Mode: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 /*
reed@google.coma76de3d2011-01-13 18:30:42 +0000193 should I worry about dithering for the lower depths?
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 */
195 SkPMColor pmc = SkPreMultiplyColor(color);
reed41e010c2015-06-09 12:16:53 -0700196 switch (dst.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000197 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 if (data) {
199 *data = pmc;
200 }
201// SkDebugf("--- D32_Src_BitmapXferProc\n");
202 return D32_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000203 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 if (data) {
205 *data = SkPixel32ToPixel16(pmc);
206 }
207// SkDebugf("--- D16_Src_BitmapXferProc\n");
208 return D16_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000209 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 if (data) {
211 *data = SkGetPackedA32(pmc);
212 }
213// SkDebugf("--- DA8_Src_BitmapXferProc\n");
214 return DA8_Src_BitmapXferProc;
215 default:
216 break;
217 }
218 break;
219 }
220 default:
221 break;
222 }
halcanary96fcdcc2015-08-27 07:41:13 -0700223 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224}
225
reed41e010c2015-06-09 12:16:53 -0700226static void CallBitmapXferProc(const SkPixmap& dst, const SkIRect& rect, BitmapXferProc proc,
227 uint32_t procData) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 int shiftPerPixel;
reed41e010c2015-06-09 12:16:53 -0700229 switch (dst.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000230 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 shiftPerPixel = 2;
232 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000233 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 shiftPerPixel = 1;
235 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000236 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 shiftPerPixel = 0;
238 break;
239 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000240 SkDEBUGFAIL("Can't use xferproc on this config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 return;
242 }
243
reed41e010c2015-06-09 12:16:53 -0700244 uint8_t* pixels = (uint8_t*)dst.writable_addr();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 SkASSERT(pixels);
reed41e010c2015-06-09 12:16:53 -0700246 const size_t rowBytes = dst.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 const int widthBytes = rect.width() << shiftPerPixel;
248
249 // skip down to the first scanline and X position
250 pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
251 for (int scans = rect.height() - 1; scans >= 0; --scans) {
252 proc(pixels, widthBytes, procData);
253 pixels += rowBytes;
254 }
255}
256
257void SkDraw::drawPaint(const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000258 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259
reed@google.com045e62d2011-10-24 12:19:46 +0000260 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 return;
262 }
263
264 SkIRect devRect;
reed41e010c2015-06-09 12:16:53 -0700265 devRect.set(0, 0, fDst.width(), fDst.height());
reed@google.coma76de3d2011-01-13 18:30:42 +0000266
reed@google.com045e62d2011-10-24 12:19:46 +0000267 if (fRC->isBW()) {
268 /* If we don't have a shader (i.e. we're just a solid color) we may
269 be faster to operate directly on the device bitmap, rather than invoking
270 a blitter. Esp. true for xfermodes, which require a colorshader to be
271 present, which is just redundant work. Since we're drawing everywhere
272 in the clip, we don't have to worry about antialiasing.
273 */
274 uint32_t procData = 0; // to avoid the warning
reed41e010c2015-06-09 12:16:53 -0700275 BitmapXferProc proc = ChooseBitmapXferProc(fDst, paint, &procData);
reed@google.com045e62d2011-10-24 12:19:46 +0000276 if (proc) {
277 if (D_Dst_BitmapXferProc == proc) { // nothing to do
278 return;
279 }
280
281 SkRegion::Iterator iter(fRC->bwRgn());
282 while (!iter.done()) {
reed41e010c2015-06-09 12:16:53 -0700283 CallBitmapXferProc(fDst, iter.rect(), proc, procData);
reed@google.com045e62d2011-10-24 12:19:46 +0000284 iter.next();
285 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 return;
287 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 }
reed@google.com045e62d2011-10-24 12:19:46 +0000289
290 // normal case: use a blitter
reed41e010c2015-06-09 12:16:53 -0700291 SkAutoBlitterChoose blitter(fDst, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +0000292 SkScan::FillIRect(devRect, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293}
294
295///////////////////////////////////////////////////////////////////////////////
296
297struct PtProcRec {
298 SkCanvas::PointMode fMode;
299 const SkPaint* fPaint;
300 const SkRegion* fClip;
reed@google.com045e62d2011-10-24 12:19:46 +0000301 const SkRasterClip* fRC;
reed@google.coma76de3d2011-01-13 18:30:42 +0000302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 // computed values
304 SkFixed fRadius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000305
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
307 SkBlitter*);
308
309 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
reed@google.com045e62d2011-10-24 12:19:46 +0000310 const SkRasterClip*);
311 Proc chooseProc(SkBlitter** blitter);
312
313private:
314 SkAAClipBlitterWrapper fWrapper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000315};
316
317static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
318 int count, SkBlitter* blitter) {
319 SkASSERT(rec.fClip->isRect());
320 const SkIRect& r = rec.fClip->getBounds();
reed@google.coma76de3d2011-01-13 18:30:42 +0000321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000323 int x = SkScalarFloorToInt(devPts[i].fX);
324 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 if (r.contains(x, y)) {
326 blitter->blitH(x, y, 1);
327 }
328 }
329}
330
331static void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
332 const SkPoint devPts[], int count,
333 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000334 SkASSERT(rec.fRC->isRect());
335 const SkIRect& r = rec.fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336 uint32_t value;
reed41e010c2015-06-09 12:16:53 -0700337 const SkPixmap* dst = blitter->justAnOpaqueColor(&value);
338 SkASSERT(dst);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000339
reed41e010c2015-06-09 12:16:53 -0700340 uint16_t* addr = dst->writable_addr16(0, 0);
341 size_t rb = dst->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000342
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000344 int x = SkScalarFloorToInt(devPts[i].fX);
345 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 if (r.contains(x, y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
348 }
349 }
350}
351
reed@google.com2d47a212012-11-29 21:01:00 +0000352static void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
353 const SkPoint devPts[], int count,
354 SkBlitter* blitter) {
355 SkASSERT(rec.fRC->isRect());
356 const SkIRect& r = rec.fRC->getBounds();
357 uint32_t value;
reed41e010c2015-06-09 12:16:53 -0700358 const SkPixmap* dst = blitter->justAnOpaqueColor(&value);
359 SkASSERT(dst);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000360
reed41e010c2015-06-09 12:16:53 -0700361 SkPMColor* addr = dst->writable_addr32(0, 0);
362 size_t rb = dst->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000363
reed@google.com2d47a212012-11-29 21:01:00 +0000364 for (int i = 0; i < count; i++) {
365 int x = SkScalarFloorToInt(devPts[i].fX);
366 int y = SkScalarFloorToInt(devPts[i].fY);
367 if (r.contains(x, y)) {
368 ((SkPMColor*)((char*)addr + y * rb))[x] = value;
369 }
370 }
371}
372
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
374 int count, SkBlitter* blitter) {
375 for (int i = 0; i < count; i++) {
reed@google.come1ca7052013-12-17 19:22:07 +0000376 int x = SkScalarFloorToInt(devPts[i].fX);
377 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378 if (rec.fClip->contains(x, y)) {
379 blitter->blitH(x, y, 1);
380 }
381 }
382}
383
384static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
385 int count, SkBlitter* blitter) {
386 for (int i = 0; i < count; i += 2) {
reed5dc6b7d2015-04-14 10:40:44 -0700387 SkScan::HairLine(&devPts[i], 2, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 }
389}
390
391static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
392 int count, SkBlitter* blitter) {
reed5dc6b7d2015-04-14 10:40:44 -0700393 SkScan::HairLine(devPts, count, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394}
395
396// aa versions
397
398static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
399 int count, SkBlitter* blitter) {
400 for (int i = 0; i < count; i += 2) {
reed5dc6b7d2015-04-14 10:40:44 -0700401 SkScan::AntiHairLine(&devPts[i], 2, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 }
403}
404
405static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
406 int count, SkBlitter* blitter) {
reed5dc6b7d2015-04-14 10:40:44 -0700407 SkScan::AntiHairLine(devPts, count, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408}
409
410// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
411
412static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
413 int count, SkBlitter* blitter) {
414 const SkFixed radius = rec.fRadius;
415 for (int i = 0; i < count; i++) {
416 SkFixed x = SkScalarToFixed(devPts[i].fX);
417 SkFixed y = SkScalarToFixed(devPts[i].fY);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000418
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 SkXRect r;
420 r.fLeft = x - radius;
421 r.fTop = y - radius;
422 r.fRight = x + radius;
423 r.fBottom = y + radius;
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000424
reed@google.com045e62d2011-10-24 12:19:46 +0000425 SkScan::FillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 }
427}
428
429static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
430 int count, SkBlitter* blitter) {
431 const SkFixed radius = rec.fRadius;
432 for (int i = 0; i < count; i++) {
433 SkFixed x = SkScalarToFixed(devPts[i].fX);
434 SkFixed y = SkScalarToFixed(devPts[i].fY);
reed@google.coma76de3d2011-01-13 18:30:42 +0000435
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 SkXRect r;
437 r.fLeft = x - radius;
438 r.fTop = y - radius;
439 r.fRight = x + radius;
440 r.fBottom = y + radius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000441
reed@google.com045e62d2011-10-24 12:19:46 +0000442 SkScan::AntiFillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 }
444}
445
reed@android.comb4f404a2009-07-10 17:02:17 +0000446// If this guy returns true, then chooseProc() must return a valid proc
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
reed@google.com045e62d2011-10-24 12:19:46 +0000448 const SkMatrix* matrix, const SkRasterClip* rc) {
ochang3ece53e2015-05-21 15:44:53 -0700449 if ((unsigned)mode > (unsigned)SkCanvas::kPolygon_PointMode) {
450 return false;
451 }
452
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453 if (paint.getPathEffect()) {
454 return false;
455 }
456 SkScalar width = paint.getStrokeWidth();
457 if (0 == width) {
458 fMode = mode;
459 fPaint = &paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700460 fClip = nullptr;
reed@google.com045e62d2011-10-24 12:19:46 +0000461 fRC = rc;
reed@google.com2d47a212012-11-29 21:01:00 +0000462 fRadius = SK_FixedHalf;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463 return true;
464 }
reed@android.comb4f404a2009-07-10 17:02:17 +0000465 if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
robertphillips9f2251c2014-11-04 13:33:50 -0800466 matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
468 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
469 if (SkScalarNearlyZero(sx - sy)) {
470 if (sx < 0) {
471 sx = -sx;
472 }
473
474 fMode = mode;
475 fPaint = &paint;
halcanary96fcdcc2015-08-27 07:41:13 -0700476 fClip = nullptr;
reed@google.com045e62d2011-10-24 12:19:46 +0000477 fRC = rc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
479 return true;
480 }
481 }
482 return false;
483}
484
reed@google.com045e62d2011-10-24 12:19:46 +0000485PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700486 Proc proc = nullptr;
reed@google.coma76de3d2011-01-13 18:30:42 +0000487
reed@google.com045e62d2011-10-24 12:19:46 +0000488 SkBlitter* blitter = *blitterPtr;
489 if (fRC->isBW()) {
490 fClip = &fRC->bwRgn();
491 } else {
492 fWrapper.init(*fRC, blitter);
493 fClip = &fWrapper.getRgn();
494 blitter = fWrapper.getBlitter();
495 *blitterPtr = blitter;
496 }
497
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 // for our arrays
499 SkASSERT(0 == SkCanvas::kPoints_PointMode);
500 SkASSERT(1 == SkCanvas::kLines_PointMode);
501 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
502 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
503
reed@google.com2d47a212012-11-29 21:01:00 +0000504 if (fPaint->isAntiAlias()) {
505 if (0 == fPaint->getStrokeWidth()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000506 static const Proc gAAProcs[] = {
507 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
508 };
509 proc = gAAProcs[fMode];
reed@google.com2d47a212012-11-29 21:01:00 +0000510 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
511 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
512 proc = aa_square_proc;
513 }
514 } else { // BW
515 if (fRadius <= SK_FixedHalf) { // small radii and hairline
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516 if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
517 uint32_t value;
reed41e010c2015-06-09 12:16:53 -0700518 const SkPixmap* bm = blitter->justAnOpaqueColor(&value);
reed@google.com900ecf22014-02-20 20:55:37 +0000519 if (bm && kRGB_565_SkColorType == bm->colorType()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 proc = bw_pt_rect_16_hair_proc;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000521 } else if (bm && kN32_SkColorType == bm->colorType()) {
reed@google.com2d47a212012-11-29 21:01:00 +0000522 proc = bw_pt_rect_32_hair_proc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 } else {
524 proc = bw_pt_rect_hair_proc;
525 }
526 } else {
527 static Proc gBWProcs[] = {
528 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
529 };
530 proc = gBWProcs[fMode];
531 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532 } else {
533 proc = bw_square_proc;
534 }
535 }
536 return proc;
537}
538
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539// each of these costs 8-bytes of stack space, so don't make it too large
540// must be even for lines/polygon to work
541#define MAX_DEV_PTS 32
542
543void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
reed@android.comf2b98d62010-12-20 18:26:13 +0000544 const SkPoint pts[], const SkPaint& paint,
545 bool forceUseDevice) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546 // if we're in lines mode, force count to be even
547 if (SkCanvas::kLines_PointMode == mode) {
548 count &= ~(size_t)1;
549 }
550
551 if ((long)count <= 0) {
552 return;
553 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000554
halcanary96fcdcc2015-08-27 07:41:13 -0700555 SkASSERT(pts != nullptr);
reed@android.comf2b98d62010-12-20 18:26:13 +0000556 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +0000557
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000559 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560 return;
561 }
562
563 PtProcRec rec;
reed@google.com045e62d2011-10-24 12:19:46 +0000564 if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
reed41e010c2015-06-09 12:16:53 -0700565 SkAutoBlitterChoose blitter(fDst, *fMatrix, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000566
567 SkPoint devPts[MAX_DEV_PTS];
568 const SkMatrix* matrix = fMatrix;
569 SkBlitter* bltr = blitter.get();
reed@google.com045e62d2011-10-24 12:19:46 +0000570 PtProcRec::Proc proc = rec.chooseProc(&bltr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571 // we have to back up subsequent passes if we're in polygon mode
572 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
reed@google.coma76de3d2011-01-13 18:30:42 +0000573
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 do {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000575 int n = SkToInt(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576 if (n > MAX_DEV_PTS) {
577 n = MAX_DEV_PTS;
578 }
579 matrix->mapPoints(devPts, pts, n);
580 proc(rec, devPts, n, bltr);
581 pts += n - backup;
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000582 SkASSERT(SkToInt(count) >= n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583 count -= n;
584 if (count > 0) {
585 count += backup;
586 }
587 } while (count != 0);
588 } else {
589 switch (mode) {
590 case SkCanvas::kPoints_PointMode: {
591 // temporarily mark the paint as filling.
reed@google.com40c2ba22011-07-25 19:41:22 +0000592 SkPaint newPaint(paint);
593 newPaint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000594
reed@google.com40c2ba22011-07-25 19:41:22 +0000595 SkScalar width = newPaint.getStrokeWidth();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 SkScalar radius = SkScalarHalf(width);
reed@google.coma76de3d2011-01-13 18:30:42 +0000597
reed@google.com40c2ba22011-07-25 19:41:22 +0000598 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599 SkPath path;
600 SkMatrix preMatrix;
mtklein7ef849d2014-11-24 09:11:45 -0800601
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602 path.addCircle(0, 0, radius);
603 for (size_t i = 0; i < count; i++) {
604 preMatrix.setTranslate(pts[i].fX, pts[i].fY);
605 // pass true for the last point, since we can modify
606 // then path then
jvanverthb3eb6872014-10-24 07:12:51 -0700607 path.setIsVolatile((count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000608 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000609 fDevice->drawPath(*this, path, newPaint, &preMatrix,
reed@android.comf2b98d62010-12-20 18:26:13 +0000610 (count-1) == i);
611 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000612 this->drawPath(path, newPaint, &preMatrix,
613 (count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000614 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615 }
616 } else {
617 SkRect r;
reed@google.coma76de3d2011-01-13 18:30:42 +0000618
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619 for (size_t i = 0; i < count; i++) {
620 r.fLeft = pts[i].fX - radius;
621 r.fTop = pts[i].fY - radius;
622 r.fRight = r.fLeft + width;
623 r.fBottom = r.fTop + width;
reed@android.comf2b98d62010-12-20 18:26:13 +0000624 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000625 fDevice->drawRect(*this, r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000626 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000627 this->drawRect(r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000628 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 }
630 }
631 break;
632 }
633 case SkCanvas::kLines_PointMode:
bsalomon49f085d2014-09-05 13:34:00 -0700634 if (2 == count && paint.getPathEffect()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000635 // most likely a dashed line - see if it is one of the ones
636 // we can accelerate
637 SkStrokeRec rec(paint);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000638 SkPathEffect::PointData pointData;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000639
640 SkPath path;
641 path.moveTo(pts[0]);
642 path.lineTo(pts[1]);
643
reed@google.com4bbdeac2013-01-24 21:03:11 +0000644 SkRect cullRect = SkRect::Make(fRC->getBounds());
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000645
reed@google.com4bbdeac2013-01-24 21:03:11 +0000646 if (paint.getPathEffect()->asPoints(&pointData, path, rec,
647 *fMatrix, &cullRect)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000648 // 'asPoints' managed to find some fast path
649
robertphillips@google.com629ab542012-11-28 17:18:11 +0000650 SkPaint newP(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700651 newP.setPathEffect(nullptr);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000652 newP.setStyle(SkPaint::kFill_Style);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000653
robertphillips@google.com6d875572012-12-17 18:56:29 +0000654 if (!pointData.fFirst.isEmpty()) {
655 if (fDevice) {
656 fDevice->drawPath(*this, pointData.fFirst, newP);
657 } else {
658 this->drawPath(pointData.fFirst, newP);
659 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000660 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000661
662 if (!pointData.fLast.isEmpty()) {
663 if (fDevice) {
664 fDevice->drawPath(*this, pointData.fLast, newP);
665 } else {
666 this->drawPath(pointData.fLast, newP);
667 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000668 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000669
670 if (pointData.fSize.fX == pointData.fSize.fY) {
671 // The rest of the dashed line can just be drawn as points
672 SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
673
674 if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
675 newP.setStrokeCap(SkPaint::kRound_Cap);
676 } else {
677 newP.setStrokeCap(SkPaint::kButt_Cap);
678 }
679
680 if (fDevice) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000681 fDevice->drawPoints(*this,
robertphillips@google.com6d875572012-12-17 18:56:29 +0000682 SkCanvas::kPoints_PointMode,
683 pointData.fNumPoints,
684 pointData.fPoints,
685 newP);
686 } else {
687 this->drawPoints(SkCanvas::kPoints_PointMode,
688 pointData.fNumPoints,
689 pointData.fPoints,
690 newP,
691 forceUseDevice);
692 }
693 break;
694 } else {
695 // The rest of the dashed line must be drawn as rects
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000696 SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
robertphillips@google.com6d875572012-12-17 18:56:29 +0000697 pointData.fFlags));
698
699 SkRect r;
700
701 for (int i = 0; i < pointData.fNumPoints; ++i) {
702 r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
703 pointData.fPoints[i].fY - pointData.fSize.fY,
704 pointData.fPoints[i].fX + pointData.fSize.fX,
705 pointData.fPoints[i].fY + pointData.fSize.fY);
706 if (fDevice) {
707 fDevice->drawRect(*this, r, newP);
708 } else {
709 this->drawRect(r, newP);
710 }
711 }
712 }
713
robertphillips@google.com629ab542012-11-28 17:18:11 +0000714 break;
715 }
716 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000717 // couldn't take fast path so fall through!
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 case SkCanvas::kPolygon_PointMode: {
719 count -= 1;
720 SkPath path;
721 SkPaint p(paint);
722 p.setStyle(SkPaint::kStroke_Style);
723 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
jvanverthb3eb6872014-10-24 07:12:51 -0700724 path.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000725 for (size_t i = 0; i < count; i += inc) {
726 path.moveTo(pts[i]);
727 path.lineTo(pts[i+1]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000728 if (fDevice) {
halcanary96fcdcc2015-08-27 07:41:13 -0700729 fDevice->drawPath(*this, path, p, nullptr, true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000730 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700731 this->drawPath(path, p, nullptr, true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000732 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000733 path.rewind();
734 }
735 break;
736 }
737 }
738 }
739}
740
fmalita1a178ca2015-01-15 06:01:23 -0800741static inline SkPoint compute_stroke_size(const SkPaint& paint, const SkMatrix& matrix) {
742 SkASSERT(matrix.rectStaysRect());
743 SkASSERT(SkPaint::kFill_Style != paint.getStyle());
744
745 SkVector size;
746 SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
747 matrix.mapVectors(&size, &pt, 1);
748 return SkPoint::Make(SkScalarAbs(size.fX), SkScalarAbs(size.fY));
749}
750
reed@google.com761fb622011-04-04 18:58:05 +0000751static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
752 SkPoint* strokeSize) {
753 if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
754 paint.getStrokeMiter() < SK_ScalarSqrt2) {
755 return false;
756 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000757
fmalita1a178ca2015-01-15 06:01:23 -0800758 *strokeSize = compute_stroke_size(paint, matrix);
reed@google.com761fb622011-04-04 18:58:05 +0000759 return true;
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000760}
761
reed@google.com62ab7ad2011-04-05 14:08:25 +0000762SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
763 const SkMatrix& matrix,
764 SkPoint* strokeSize) {
765 RectType rtype;
766 const SkScalar width = paint.getStrokeWidth();
767 const bool zeroWidth = (0 == width);
768 SkPaint::Style style = paint.getStyle();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000769
reed@google.com62ab7ad2011-04-05 14:08:25 +0000770 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
771 style = SkPaint::kFill_Style;
772 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000773
reed@google.com62ab7ad2011-04-05 14:08:25 +0000774 if (paint.getPathEffect() || paint.getMaskFilter() ||
775 paint.getRasterizer() || !matrix.rectStaysRect() ||
776 SkPaint::kStrokeAndFill_Style == style) {
777 rtype = kPath_RectType;
778 } else if (SkPaint::kFill_Style == style) {
779 rtype = kFill_RectType;
780 } else if (zeroWidth) {
781 rtype = kHair_RectType;
782 } else if (easy_rect_join(paint, matrix, strokeSize)) {
783 rtype = kStroke_RectType;
784 } else {
785 rtype = kPath_RectType;
786 }
787 return rtype;
788}
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000789
reed@google.com73244152012-05-11 14:35:23 +0000790static const SkPoint* rect_points(const SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000791 return SkTCast<const SkPoint*>(&r);
reed@google.com73244152012-05-11 14:35:23 +0000792}
793
794static SkPoint* rect_points(SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000795 return SkTCast<SkPoint*>(&r);
reed@google.com40c2ba22011-07-25 19:41:22 +0000796}
797
reed03939122014-12-15 13:42:51 -0800798void SkDraw::drawRect(const SkRect& prePaintRect, const SkPaint& paint,
799 const SkMatrix* paintMatrix, const SkRect* postPaintRect) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000800 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801
802 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000803 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804 return;
805 }
806
reed03939122014-12-15 13:42:51 -0800807 const SkMatrix* matrix;
808 SkMatrix combinedMatrixStorage;
809 if (paintMatrix) {
810 SkASSERT(postPaintRect);
811 combinedMatrixStorage.setConcat(*fMatrix, *paintMatrix);
812 matrix = &combinedMatrixStorage;
813 } else {
814 SkASSERT(!postPaintRect);
815 matrix = fMatrix;
816 }
817
reed@google.com761fb622011-04-04 18:58:05 +0000818 SkPoint strokeSize;
reed@google.com62ab7ad2011-04-05 14:08:25 +0000819 RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000820
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000821 if (kPath_RectType == rtype) {
reed03939122014-12-15 13:42:51 -0800822 SkDraw draw(*this);
823 if (paintMatrix) {
824 draw.fMatrix = matrix;
825 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826 SkPath tmp;
reed03939122014-12-15 13:42:51 -0800827 tmp.addRect(prePaintRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000828 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -0700829 draw.drawPath(tmp, paint, nullptr, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 return;
831 }
832
reed03939122014-12-15 13:42:51 -0800833 SkRect devRect;
fmalita1a178ca2015-01-15 06:01:23 -0800834 const SkRect& paintRect = paintMatrix ? *postPaintRect : prePaintRect;
835 // skip the paintMatrix when transforming the rect by the CTM
836 fMatrix->mapPoints(rect_points(devRect), rect_points(paintRect), 2);
reed@google.com73244152012-05-11 14:35:23 +0000837 devRect.sort();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000838
reed@android.com8a1c16f2008-12-17 15:59:43 +0000839 // look for the quick exit, before we build a blitter
fmalita1a178ca2015-01-15 06:01:23 -0800840 SkRect bbox = devRect;
reed@google.com1c028bd2013-08-28 15:23:19 +0000841 if (paint.getStyle() != SkPaint::kFill_Style) {
842 // extra space for hairlines
georgeb3eba472014-09-09 11:33:57 -0700843 if (paint.getStrokeWidth() == 0) {
fmalita1a178ca2015-01-15 06:01:23 -0800844 bbox.outset(1, 1);
georgeb3eba472014-09-09 11:33:57 -0700845 } else {
fmalita1a178ca2015-01-15 06:01:23 -0800846 // For kStroke_RectType, strokeSize is already computed.
847 const SkPoint& ssize = (kStroke_RectType == rtype)
848 ? strokeSize
849 : compute_stroke_size(paint, *fMatrix);
850 bbox.outset(SkScalarHalf(ssize.x()), SkScalarHalf(ssize.y()));
georgeb3eba472014-09-09 11:33:57 -0700851 }
reed@google.com1c028bd2013-08-28 15:23:19 +0000852 }
fmalita1a178ca2015-01-15 06:01:23 -0800853
854 SkIRect ir = bbox.roundOut();
reed@google.com1c028bd2013-08-28 15:23:19 +0000855 if (fRC->quickReject(ir)) {
856 return;
rmistry@google.come09d6f42013-08-27 18:53:41 +0000857 }
858
reed41e010c2015-06-09 12:16:53 -0700859 SkDeviceLooper looper(fDst, *fRC, ir, paint.isAntiAlias());
reed@google.com1c028bd2013-08-28 15:23:19 +0000860 while (looper.next()) {
861 SkRect localDevRect;
862 looper.mapRect(&localDevRect, devRect);
863 SkMatrix localMatrix;
reed03939122014-12-15 13:42:51 -0800864 looper.mapMatrix(&localMatrix, *matrix);
rmistry@google.come09d6f42013-08-27 18:53:41 +0000865
reed41e010c2015-06-09 12:16:53 -0700866 SkAutoBlitterChoose blitterStorage(looper.getPixmap(), localMatrix, paint);
reed@google.com1c028bd2013-08-28 15:23:19 +0000867 const SkRasterClip& clip = looper.getRC();
868 SkBlitter* blitter = blitterStorage.get();
869
870 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
871 // case we are also hairline (if we've gotten to here), which devolves to
872 // effectively just kFill
873 switch (rtype) {
874 case kFill_RectType:
875 if (paint.isAntiAlias()) {
876 SkScan::AntiFillRect(localDevRect, clip, blitter);
877 } else {
878 SkScan::FillRect(localDevRect, clip, blitter);
879 }
880 break;
881 case kStroke_RectType:
882 if (paint.isAntiAlias()) {
883 SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter);
884 } else {
885 SkScan::FrameRect(localDevRect, strokeSize, clip, blitter);
886 }
887 break;
888 case kHair_RectType:
889 if (paint.isAntiAlias()) {
890 SkScan::AntiHairRect(localDevRect, clip, blitter);
891 } else {
892 SkScan::HairRect(localDevRect, clip, blitter);
893 }
894 break;
895 default:
896 SkDEBUGFAIL("bad rtype");
897 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 }
899}
900
901void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
902 if (srcM.fBounds.isEmpty()) {
903 return;
904 }
905
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000906 const SkMask* mask = &srcM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000908 SkMask dstM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909 if (paint.getMaskFilter() &&
robertphillipse80eb922015-12-17 11:33:12 -0800910 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, nullptr)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 mask = &dstM;
912 }
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
caryclark1a7eb262016-01-21 07:07:02 -08001008SkScalar SkDraw::ComputeResScaleForStroking(const SkMatrix& matrix) {
reed05d90442015-02-12 13:35:52 -08001009 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,
caryclark1a7eb262016-01-21 07:07:02 -08001088 ComputeResScaleForStroking(*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()) {
caryclark2028d7f2015-12-09 14:04:46 -08001135 switch (paint->getStrokeCap()) {
1136 case SkPaint::kButt_Cap:
1137 proc = SkScan::AntiHairPath;
1138 break;
1139 case SkPaint::kSquare_Cap:
1140 proc = SkScan::AntiHairSquarePath;
1141 break;
1142 case SkPaint::kRound_Cap:
1143 proc = SkScan::AntiHairRoundPath;
1144 break;
1145 default:
1146 proc SK_INIT_TO_AVOID_WARNING;
1147 SkDEBUGFAIL("unknown paint cap type");
1148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 } else {
caryclark2028d7f2015-12-09 14:04:46 -08001150 switch (paint->getStrokeCap()) {
1151 case SkPaint::kButt_Cap:
1152 proc = SkScan::HairPath;
1153 break;
1154 case SkPaint::kSquare_Cap:
1155 proc = SkScan::HairSquarePath;
1156 break;
1157 case SkPaint::kRound_Cap:
1158 proc = SkScan::HairRoundPath;
1159 break;
1160 default:
1161 proc SK_INIT_TO_AVOID_WARNING;
1162 SkDEBUGFAIL("unknown paint cap type");
1163 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164 }
1165 }
krajcevski53f09592014-08-06 11:12:14 -07001166 proc(*devPathPtr, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001167}
1168
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1170 const SkPaint& paint) const {
reed@google.com900ecf22014-02-20 20:55:37 +00001171 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172
fmalitac7e211a2016-01-07 10:34:46 -08001173 if (SkTreatAsSprite(*fMatrix, bitmap.dimensions(), paint)) {
reed@google.come1ca7052013-12-17 19:22:07 +00001174 int ix = SkScalarRoundToInt(fMatrix->getTranslateX());
1175 int iy = SkScalarRoundToInt(fMatrix->getTranslateY());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001176
reed92fc2ae2015-05-22 08:06:21 -07001177 SkAutoPixmapUnlock result;
1178 if (!bitmap.requestLock(&result)) {
reed@google.coma641f3f2012-12-13 22:16:30 +00001179 return;
1180 }
reed92fc2ae2015-05-22 08:06:21 -07001181 const SkPixmap& pmap = result.pixmap();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182 SkMask mask;
reed92fc2ae2015-05-22 08:06:21 -07001183 mask.fBounds.set(ix, iy, ix + pmap.width(), iy + pmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001184 mask.fFormat = SkMask::kA8_Format;
reed92fc2ae2015-05-22 08:06:21 -07001185 mask.fRowBytes = SkToU32(pmap.rowBytes());
1186 // fImage is typed as writable, but in this case it is used read-only
1187 mask.fImage = (uint8_t*)pmap.addr8(0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001188
reed@android.com8a1c16f2008-12-17 15:59:43 +00001189 this->drawDevMask(mask, paint);
1190 } else { // need to xform the bitmap first
1191 SkRect r;
1192 SkMask mask;
reed@google.coma76de3d2011-01-13 18:30:42 +00001193
reed@android.com8a1c16f2008-12-17 15:59:43 +00001194 r.set(0, 0,
1195 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1196 fMatrix->mapRect(&r);
1197 r.round(&mask.fBounds);
reed@google.coma76de3d2011-01-13 18:30:42 +00001198
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 // set the mask's bounds to the transformed bitmap-bounds,
1200 // clipped to the actual device
1201 {
1202 SkIRect devBounds;
reed41e010c2015-06-09 12:16:53 -07001203 devBounds.set(0, 0, fDst.width(), fDst.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 // need intersect(l, t, r, b) on irect
1205 if (!mask.fBounds.intersect(devBounds)) {
1206 return;
1207 }
1208 }
reed@android.com543ed932009-04-24 12:43:40 +00001209
reed@android.com8a1c16f2008-12-17 15:59:43 +00001210 mask.fFormat = SkMask::kA8_Format;
1211 mask.fRowBytes = SkAlign4(mask.fBounds.width());
reed@android.com543ed932009-04-24 12:43:40 +00001212 size_t size = mask.computeImageSize();
1213 if (0 == size) {
1214 // the mask is too big to allocated, draw nothing
1215 return;
1216 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217
1218 // allocate (and clear) our temp buffer to hold the transformed bitmap
scroggo565901d2015-12-10 10:44:13 -08001219 SkAutoTMalloc<uint8_t> storage(size);
1220 mask.fImage = storage.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 memset(mask.fImage, 0, size);
reed@google.coma76de3d2011-01-13 18:30:42 +00001222
reed@android.com8a1c16f2008-12-17 15:59:43 +00001223 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1224 {
1225 SkBitmap device;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001226 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
1227 mask.fImage, mask.fRowBytes);
reed@google.coma76de3d2011-01-13 18:30:42 +00001228
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229 SkCanvas c(device);
1230 // need the unclipped top/left for the translate
1231 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1232 -SkIntToScalar(mask.fBounds.fTop));
1233 c.concat(*fMatrix);
reed@android.com3469c762009-02-24 19:03:20 +00001234
1235 // We can't call drawBitmap, or we'll infinitely recurse. Instead
reed@android.comfb12c3e2009-03-05 20:43:42 +00001236 // we manually build a shader and draw that into our new mask
reed@android.com3469c762009-02-24 19:03:20 +00001237 SkPaint tmpPaint;
1238 tmpPaint.setFlags(paint.getFlags());
reed@google.com40c2ba22011-07-25 19:41:22 +00001239 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
reed@android.com3469c762009-02-24 19:03:20 +00001240 SkRect rr;
1241 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1242 SkIntToScalar(bitmap.height()));
reed@google.com40c2ba22011-07-25 19:41:22 +00001243 c.drawRect(rr, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244 }
1245 this->drawDevMask(mask, paint);
1246 }
1247}
1248
reed@google.com045e62d2011-10-24 12:19:46 +00001249static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250 const SkRect& srcR) {
1251 SkRect dstR;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001252 m.mapRect(&dstR, srcR);
reedb07a94f2014-11-19 05:03:18 -08001253 return c.quickReject(dstR.roundOut());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001254}
1255
reed@google.com045e62d2011-10-24 12:19:46 +00001256static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001257 int width, int height) {
1258 SkRect r;
1259 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1260 return clipped_out(matrix, clip, r);
1261}
1262
reedc240e712015-05-23 12:26:41 -07001263static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y, const SkPixmap& pmap) {
1264 return clip.isBW() || clip.quickContains(x, y, x + pmap.width(), y + pmap.height());
reed@google.com045e62d2011-10-24 12:19:46 +00001265}
1266
reed@android.com8a1c16f2008-12-17 15:59:43 +00001267void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
reed03939122014-12-15 13:42:51 -08001268 const SkRect* dstBounds, const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001269 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001270
1271 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001272 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001274 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275 return;
1276 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001277
reed@google.com40c2ba22011-07-25 19:41:22 +00001278 SkPaint paint(origPaint);
1279 paint.setStyle(SkPaint::kFill_Style);
reed@google.coma76de3d2011-01-13 18:30:42 +00001280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 SkMatrix matrix;
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001282 matrix.setConcat(*fMatrix, prematrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001283
reed@google.com045e62d2011-10-24 12:19:46 +00001284 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001285 return;
1286 }
1287
fmalitac7e211a2016-01-07 10:34:46 -08001288 if (bitmap.colorType() != kAlpha_8_SkColorType
1289 && SkTreatAsSprite(matrix, bitmap.dimensions(), paint)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001290 //
1291 // It is safe to call lock pixels now, since we know the matrix is
1292 // (more or less) identity.
1293 //
reedc240e712015-05-23 12:26:41 -07001294 SkAutoPixmapUnlock unlocker;
1295 if (!bitmap.requestLock(&unlocker)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001296 return;
1297 }
reedc240e712015-05-23 12:26:41 -07001298 const SkPixmap& pmap = unlocker.pixmap();
reed@google.come1ca7052013-12-17 19:22:07 +00001299 int ix = SkScalarRoundToInt(matrix.getTranslateX());
1300 int iy = SkScalarRoundToInt(matrix.getTranslateY());
reedc240e712015-05-23 12:26:41 -07001301 if (clipHandlesSprite(*fRC, ix, iy, pmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001302 SkTBlitterAllocator allocator;
1303 // blitter will be owned by the allocator.
reed41e010c2015-06-09 12:16:53 -07001304 SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, paint, pmap, ix, iy, &allocator);
reed@google.com045e62d2011-10-24 12:19:46 +00001305 if (blitter) {
reedc240e712015-05-23 12:26:41 -07001306 SkScan::FillIRect(SkIRect::MakeXYWH(ix, iy, pmap.width(), pmap.height()),
1307 *fRC, blitter);
reed@google.com045e62d2011-10-24 12:19:46 +00001308 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 }
reedc240e712015-05-23 12:26:41 -07001310 // if !blitter, then we fall-through to the slower case
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001312 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001313
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314 // now make a temp draw on the stack, and use it
1315 //
1316 SkDraw draw(*this);
1317 draw.fMatrix = &matrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001318
reed@google.com900ecf22014-02-20 20:55:37 +00001319 if (bitmap.colorType() == kAlpha_8_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 draw.drawBitmapAsMask(bitmap, paint);
1321 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +00001322 SkAutoBitmapShaderInstall install(bitmap, paint);
reed03939122014-12-15 13:42:51 -08001323 const SkPaint& paintWithShader = install.paintWithShader();
1324 const SkRect srcBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
1325 if (dstBounds) {
1326 this->drawRect(srcBounds, paintWithShader, &prematrix, dstBounds);
1327 } else {
1328 draw.drawRect(srcBounds, paintWithShader);
1329 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330 }
1331}
1332
reedc240e712015-05-23 12:26:41 -07001333void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001334 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +00001335
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001337 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001339 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340 return;
1341 }
1342
reedc240e712015-05-23 12:26:41 -07001343 const SkIRect bounds = SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001344
reed@google.com045e62d2011-10-24 12:19:46 +00001345 if (fRC->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 return; // nothing to draw
1347 }
1348
reed@google.com40c2ba22011-07-25 19:41:22 +00001349 SkPaint paint(origPaint);
1350 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351
reedc240e712015-05-23 12:26:41 -07001352 SkAutoPixmapUnlock unlocker;
1353 if (!bitmap.requestLock(&unlocker)) {
1354 return;
1355 }
1356 const SkPixmap& pmap = unlocker.pixmap();
1357
halcanary96fcdcc2015-08-27 07:41:13 -07001358 if (nullptr == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, pmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001359 SkTBlitterAllocator allocator;
1360 // blitter will be owned by the allocator.
reed41e010c2015-06-09 12:16:53 -07001361 SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, paint, pmap, x, y, &allocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001362 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001363 SkScan::FillIRect(bounds, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001364 return;
1365 }
1366 }
1367
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 SkMatrix matrix;
1369 SkRect r;
1370
1371 // get a scalar version of our rect
1372 r.set(bounds);
1373
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001374 // create shader with offset
reed@android.com8a1c16f2008-12-17 15:59:43 +00001375 matrix.setTranslate(r.fLeft, r.fTop);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001376 SkAutoBitmapShaderInstall install(bitmap, paint, &matrix);
1377 const SkPaint& shaderPaint = install.paintWithShader();
reed@google.coma76de3d2011-01-13 18:30:42 +00001378
reed@android.com8a1c16f2008-12-17 15:59:43 +00001379 SkDraw draw(*this);
1380 matrix.reset();
1381 draw.fMatrix = &matrix;
1382 // call ourself with a rect
reed@google.coma76de3d2011-01-13 18:30:42 +00001383 // is this OK if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001384 draw.drawRect(r, shaderPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001385}
1386
1387///////////////////////////////////////////////////////////////////////////////
1388
1389#include "SkScalerContext.h"
1390#include "SkGlyphCache.h"
reed@google.come6913762012-08-07 15:19:47 +00001391#include "SkTextToPathIter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392#include "SkUtils.h"
1393
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001394bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) {
1395 // hairline glyphs are fast enough so we don't need to cache them
1396 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
1397 return true;
1398 }
1399
1400 // we don't cache perspective
1401 if (ctm.hasPerspective()) {
1402 return true;
1403 }
1404
1405 SkMatrix textM;
1406 return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM));
1407}
1408
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1410 SkScalar x, SkScalar y,
1411 const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001412 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413
djsollen@google.com166e6532012-03-20 14:24:38 +00001414 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001415
1416 SkMatrix matrix;
1417 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1418 matrix.postTranslate(x, y);
1419
1420 const SkPath* iterPath;
1421 SkScalar xpos, prevXPos = 0;
1422
reed@google.com7b4531f2012-08-07 15:53:00 +00001423 while (iter.next(&iterPath, &xpos)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001424 matrix.postTranslate(xpos - prevXPos, 0);
reed@google.com7b4531f2012-08-07 15:53:00 +00001425 if (iterPath) {
1426 const SkPaint& pnt = iter.getPaint();
1427 if (fDevice) {
1428 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1429 } else {
1430 this->drawPath(*iterPath, pnt, &matrix, false);
1431 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001432 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433 prevXPos = xpos;
1434 }
1435}
1436
reed@android.com8a1c16f2008-12-17 15:59:43 +00001437// disable warning : local variable used without having been initialized
bungemand7dc76f2016-03-10 11:14:40 -08001438#if defined _WIN32
reed@android.com8a1c16f2008-12-17 15:59:43 +00001439#pragma warning ( push )
1440#pragma warning ( disable : 4701 )
1441#endif
1442
herbf553e4e2015-11-09 08:51:56 -08001443////////////////////////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001444
herbd4c24f62015-12-07 12:12:29 -08001445class DrawOneGlyph {
1446public:
1447 DrawOneGlyph(const SkDraw& draw, const SkPaint& paint, SkGlyphCache* cache, SkBlitter* blitter)
1448 : fUseRegionToDraw(UsingRegionToDraw(draw.fRC))
1449 , fGlyphCache(cache)
1450 , fBlitter(blitter)
1451 , fClip(fUseRegionToDraw ? &draw.fRC->bwRgn() : nullptr)
1452 , fDraw(draw)
1453 , fPaint(paint)
1454 , fClipBounds(PickClipBounds(draw)) { }
herb11a7f7f2015-11-24 12:41:00 -08001455
herbd4c24f62015-12-07 12:12:29 -08001456 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
1457 position += rounding;
1458 Sk48Dot16 fx = SkScalarTo48Dot16(position.fX);
1459 Sk48Dot16 fy = SkScalarTo48Dot16(position.fY);
1460 // Prevent glyphs from being drawn outside of or straddling the edge of device space.
1461 if ((fx >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) ||
1462 (fx >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/) ||
1463 (fy >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) ||
1464 (fy >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/)) {
1465 return;
1466 }
herb11a7f7f2015-11-24 12:41:00 -08001467
herbd4c24f62015-12-07 12:12:29 -08001468 int left = Sk48Dot16FloorToInt(fx);
1469 int top = Sk48Dot16FloorToInt(fy);
1470 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1471
1472 left += glyph.fLeft;
1473 top += glyph.fTop;
1474
1475 int right = left + glyph.fWidth;
1476 int bottom = top + glyph.fHeight;
1477
1478 SkMask mask;
1479 mask.fBounds.set(left, top, right, bottom);
1480
1481 if (fUseRegionToDraw) {
1482 SkRegion::Cliperator clipper(*fClip, mask.fBounds);
1483
1484 if (!clipper.done() && this->getImageData(glyph, &mask)) {
1485 const SkIRect& cr = clipper.rect();
1486 do {
1487 this->blitMask(mask, cr);
1488 clipper.next();
1489 } while (!clipper.done());
1490 }
1491 } else {
1492 SkIRect storage;
1493 SkIRect* bounds = &mask.fBounds;
1494
1495 // this extra test is worth it, assuming that most of the time it succeeds
1496 // since we can avoid writing to storage
1497 if (!fClipBounds.containsNoEmptyCheck(mask.fBounds)) {
1498 if (!storage.intersectNoEmptyCheck(mask.fBounds, fClipBounds))
1499 return;
1500 bounds = &storage;
1501 }
1502
1503 if (this->getImageData(glyph, &mask)) {
1504 this->blitMask(mask, *bounds);
1505 }
1506 }
1507 }
1508
1509private:
1510 static bool UsingRegionToDraw(const SkRasterClip* rClip) {
1511 return rClip->isBW() && !rClip->isRect();
1512 }
1513
1514 static SkIRect PickClipBounds(const SkDraw& draw) {
1515 const SkRasterClip& rasterClip = *draw.fRC;
1516
1517 if (rasterClip.isBW()) {
1518 return rasterClip.bwRgn().getBounds();
1519 } else {
1520 return rasterClip.aaRgn().getBounds();
1521 }
1522 }
1523
1524 bool getImageData(const SkGlyph& glyph, SkMask* mask) {
1525 uint8_t* bits = (uint8_t*)(fGlyphCache->findImage(glyph));
1526 if (nullptr == bits) {
1527 return false; // can't rasterize glyph
1528 }
1529 mask->fImage = bits;
1530 mask->fRowBytes = glyph.rowBytes();
1531 mask->fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1532 return true;
1533 }
1534
herb11a7f7f2015-11-24 12:41:00 -08001535 void blitMask(const SkMask& mask, const SkIRect& clip) const {
1536 if (SkMask::kARGB32_Format == mask.fFormat) {
herbd4c24f62015-12-07 12:12:29 -08001537 SkBitmap bm;
1538 bm.installPixels(
1539 SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()),
1540 (SkPMColor*)mask.fImage, mask.fRowBytes);
1541
1542 fDraw.drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), fPaint);
herb11a7f7f2015-11-24 12:41:00 -08001543 } else {
1544 fBlitter->blitMask(mask, clip);
1545 }
1546 }
1547
herbd4c24f62015-12-07 12:12:29 -08001548 const bool fUseRegionToDraw;
1549 SkGlyphCache * const fGlyphCache;
1550 SkBlitter * const fBlitter;
1551 const SkRegion* const fClip;
1552 const SkDraw& fDraw;
1553 const SkPaint& fPaint;
1554 const SkIRect fClipBounds;
herb11a7f7f2015-11-24 12:41:00 -08001555};
1556
herbd4c24f62015-12-07 12:12:29 -08001557////////////////////////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001558
bungemanf6d1e602016-02-22 13:20:28 -08001559SkPaint::FakeGamma SkDraw::fakeGamma() const {
1560 return fDevice->imageInfo().isLinear() ? SkPaint::FakeGamma::On : SkPaint::FakeGamma::Off;
1561}
1562
reed@android.com8a1c16f2008-12-17 15:59:43 +00001563void SkDraw::drawText(const char text[], size_t byteLength,
1564 SkScalar x, SkScalar y, const SkPaint& paint) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001565 SkASSERT(byteLength == 0 || text != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001566
reed@android.comf2b98d62010-12-20 18:26:13 +00001567 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001568
1569 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -07001570 if (text == nullptr || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001571 return;
1572 }
1573
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001574 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1575 // will fill if its frame-width is 0.
reed@google.comed43dff2013-06-04 16:56:27 +00001576 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001577 this->drawText_asPaths(text, byteLength, x, y, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001578 return;
1579 }
1580
bungemanf6d1e602016-02-22 13:20:28 -08001581 SkAutoGlyphCache cache(paint, &fDevice->surfaceProps(), this->fakeGamma(), fMatrix);
herb11a7f7f2015-11-24 12:41:00 -08001582
1583 // The Blitter Choose needs to be live while using the blitter below.
1584 SkAutoBlitterChoose blitterChooser(fDst, *fMatrix, paint);
1585 SkAAClipBlitterWrapper wrapper(*fRC, blitterChooser.get());
bungemanf6d1e602016-02-22 13:20:28 -08001586 DrawOneGlyph drawOneGlyph(*this, paint, cache.get(), wrapper.getBlitter());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001587
herbe59124e2015-11-18 10:54:39 -08001588 SkFindAndPlaceGlyph::ProcessText(
herb4c11b3f2015-11-20 13:53:12 -08001589 paint.getTextEncoding(), text, byteLength,
bungemanf6d1e602016-02-22 13:20:28 -08001590 {x, y}, *fMatrix, paint.getTextAlign(), cache.get(), drawOneGlyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001591}
1592
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593//////////////////////////////////////////////////////////////////////////////
1594
reed@google.comed43dff2013-06-04 16:56:27 +00001595void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001596 const SkScalar pos[], int scalarsPerPosition,
1597 const SkPoint& offset, const SkPaint& origPaint) const {
reed@google.comed43dff2013-06-04 16:56:27 +00001598 // setup our std paint, in hopes of getting hits in the cache
1599 SkPaint paint(origPaint);
1600 SkScalar matrixScale = paint.setupForAsPaths();
1601
reed@google.com5a649022013-06-05 18:00:30 +00001602 SkMatrix matrix;
1603 matrix.setScale(matrixScale, matrixScale);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001604
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001605 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
1606 paint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001607 paint.setPathEffect(nullptr);
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001608
benjaminwagnerd936f632016-02-23 10:44:31 -08001609 SkPaint::GlyphCacheProc glyphCacheProc = paint.getGlyphCacheProc(true);
1610 SkAutoGlyphCache cache(paint, &fDevice->surfaceProps(), this->fakeGamma(), nullptr);
reed@google.comed43dff2013-06-04 16:56:27 +00001611
1612 const char* stop = text + byteLength;
bungeman79738cc2015-03-11 14:05:29 -07001613 SkTextAlignProc alignProc(paint.getTextAlign());
fmalita05c4a432014-09-29 06:29:53 -07001614 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001615
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001616 // Now restore the original settings, so we "draw" with whatever style/stroking.
1617 paint.setStyle(origPaint.getStyle());
1618 paint.setPathEffect(origPaint.getPathEffect());
1619
reed@google.comed43dff2013-06-04 16:56:27 +00001620 while (text < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -08001621 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
reed@google.comed43dff2013-06-04 16:56:27 +00001622 if (glyph.fWidth) {
1623 const SkPath* path = cache->findPath(glyph);
1624 if (path) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001625 SkPoint tmsLoc;
1626 tmsProc(pos, &tmsLoc);
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001627 SkPoint loc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001628 alignProc(tmsLoc, glyph, &loc);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001629
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001630 matrix[SkMatrix::kMTransX] = loc.fX;
1631 matrix[SkMatrix::kMTransY] = loc.fY;
reed@google.com5a649022013-06-05 18:00:30 +00001632 if (fDevice) {
1633 fDevice->drawPath(*this, *path, paint, &matrix, false);
1634 } else {
1635 this->drawPath(*path, paint, &matrix, false);
1636 }
reed@google.comed43dff2013-06-04 16:56:27 +00001637 }
1638 }
1639 pos += scalarsPerPosition;
1640 }
1641}
1642
reed@android.com8a1c16f2008-12-17 15:59:43 +00001643void SkDraw::drawPosText(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001644 const SkScalar pos[], int scalarsPerPosition,
1645 const SkPoint& offset, const SkPaint& paint) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001646 SkASSERT(byteLength == 0 || text != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001647 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1648
reed@android.comf2b98d62010-12-20 18:26:13 +00001649 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001650
1651 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -07001652 if (text == nullptr || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001653 return;
1654 }
1655
reed@google.comed43dff2013-06-04 16:56:27 +00001656 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
fmalita05c4a432014-09-29 06:29:53 -07001657 this->drawPosText_asPaths(text, byteLength, pos, scalarsPerPosition, offset, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001658 return;
1659 }
1660
bungemanf6d1e602016-02-22 13:20:28 -08001661 SkAutoGlyphCache cache(paint, &fDevice->surfaceProps(), this->fakeGamma(), fMatrix);
herbd4c24f62015-12-07 12:12:29 -08001662
herb9be5ff62015-11-11 11:30:11 -08001663 // The Blitter Choose needs to be live while using the blitter below.
herb11a7f7f2015-11-24 12:41:00 -08001664 SkAutoBlitterChoose blitterChooser(fDst, *fMatrix, paint);
1665 SkAAClipBlitterWrapper wrapper(*fRC, blitterChooser.get());
bungemanf6d1e602016-02-22 13:20:28 -08001666 DrawOneGlyph drawOneGlyph(*this, paint, cache.get(), wrapper.getBlitter());
herbd4c24f62015-12-07 12:12:29 -08001667 SkPaint::Align textAlignment = paint.getTextAlign();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001668
herb9be5ff62015-11-11 11:30:11 -08001669 SkFindAndPlaceGlyph::ProcessPosText(
herb4c11b3f2015-11-20 13:53:12 -08001670 paint.getTextEncoding(), text, byteLength,
bungemanf6d1e602016-02-22 13:20:28 -08001671 offset, *fMatrix, pos, scalarsPerPosition, textAlignment, cache.get(), drawOneGlyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001672}
1673
bungemand7dc76f2016-03-10 11:14:40 -08001674#if defined _WIN32
reed@android.com8a1c16f2008-12-17 15:59:43 +00001675#pragma warning ( pop )
1676#endif
1677
1678///////////////////////////////////////////////////////////////////////////////
1679
reed5dc6b7d2015-04-14 10:40:44 -07001680static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001681 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
1682}
1683
1684static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
1685 const SkPoint texs[], SkMatrix* matrix) {
1686 SkPoint src[3], dst[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00001687
reed@android.com8a1c16f2008-12-17 15:59:43 +00001688 src[0] = texs[state.f0];
1689 src[1] = texs[state.f1];
1690 src[2] = texs[state.f2];
1691 dst[0] = verts[state.f0];
1692 dst[1] = verts[state.f1];
1693 dst[2] = verts[state.f2];
1694 return matrix->setPolyToPoly(src, dst, 3);
1695}
1696
1697class SkTriColorShader : public SkShader {
1698public:
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001699 SkTriColorShader();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001700
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001701 class TriColorShaderContext : public SkShader::Context {
1702 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00001703 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001704 virtual ~TriColorShaderContext();
mtklein36352bf2015-03-25 18:17:31 -07001705 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001706
1707 private:
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001708 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
1709
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001710 SkMatrix fDstToUnit;
1711 SkPMColor fColors[3];
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001712 bool fSetup;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001713
1714 typedef SkShader::Context INHERITED;
1715 };
reed@google.coma76de3d2011-01-13 18:30:42 +00001716
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001717 struct TriColorShaderData {
1718 const SkPoint* pts;
1719 const SkColor* colors;
1720 const VertState *state;
1721 };
1722
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001723 SK_TO_STRING_OVERRIDE()
mtklein7e44bb12015-01-07 09:06:08 -08001724
1725 // For serialization. This will never be called.
halcanary96fcdcc2015-08-27 07:41:13 -07001726 Factory getFactory() const override { sk_throw(); return nullptr; }
djsollen@google.comba28d032012-03-26 17:57:35 +00001727
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001728 // Supply setup data to context from drawing setup
1729 void bindSetupData(TriColorShaderData* setupData) { fSetupData = setupData; }
1730
1731 // Take the setup data from context when needed.
1732 TriColorShaderData* takeSetupData() {
1733 TriColorShaderData *data = fSetupData;
1734 fSetupData = NULL;
1735 return data;
1736 }
1737
reed@android.com8a1c16f2008-12-17 15:59:43 +00001738protected:
reed773ceda2016-03-03 18:18:25 -08001739 size_t onContextSize(const ContextRec&) const override;
mtklein36352bf2015-03-25 18:17:31 -07001740 Context* onCreateContext(const ContextRec& rec, void* storage) const override {
halcanary385fe4d2015-08-26 13:07:48 -07001741 return new (storage) TriColorShaderContext(*this, rec);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +00001742 }
1743
reed@android.com8a1c16f2008-12-17 15:59:43 +00001744private:
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001745 TriColorShaderData *fSetupData;
1746
reed@android.com8a1c16f2008-12-17 15:59:43 +00001747 typedef SkShader INHERITED;
1748};
1749
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001750bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
1751 int index0, int index1, int index2) {
reed@google.coma76de3d2011-01-13 18:30:42 +00001752
reed@android.com8a1c16f2008-12-17 15:59:43 +00001753 fColors[0] = SkPreMultiplyColor(colors[index0]);
1754 fColors[1] = SkPreMultiplyColor(colors[index1]);
1755 fColors[2] = SkPreMultiplyColor(colors[index2]);
reed@google.coma76de3d2011-01-13 18:30:42 +00001756
reed@android.com8a1c16f2008-12-17 15:59:43 +00001757 SkMatrix m, im;
1758 m.reset();
1759 m.set(0, pts[index1].fX - pts[index0].fX);
1760 m.set(1, pts[index2].fX - pts[index0].fX);
1761 m.set(2, pts[index0].fX);
1762 m.set(3, pts[index1].fY - pts[index0].fY);
1763 m.set(4, pts[index2].fY - pts[index0].fY);
1764 m.set(5, pts[index0].fY);
1765 if (!m.invert(&im)) {
1766 return false;
1767 }
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001768 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix
1769 // as our interators are intrinsically tied to the vertices, and nothing else.
1770 SkMatrix ctmInv;
1771 if (!this->getCTM().invert(&ctmInv)) {
1772 return false;
1773 }
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001774 // TODO replace INV(m) * INV(ctm) with INV(ctm * m)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001775 fDstToUnit.setConcat(im, ctmInv);
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001776 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001777}
1778
1779#include "SkColorPriv.h"
reed@android.com689411a2008-12-18 02:52:32 +00001780#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001781
1782static int ScalarTo256(SkScalar v) {
benjaminwagner43437c22016-02-24 10:57:47 -08001783 return static_cast<int>(SkScalarPin(v, 0, 1) * 256 + 0.5);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001784}
1785
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001786SkTriColorShader::SkTriColorShader()
1787 : INHERITED(NULL)
1788 , fSetupData(NULL) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001789
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00001790SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorShader& shader,
1791 const ContextRec& rec)
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001792 : INHERITED(shader, rec)
1793 , fSetup(false) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001794
1795SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
1796
reed773ceda2016-03-03 18:18:25 -08001797size_t SkTriColorShader::onContextSize(const ContextRec&) const {
reeda0cee5f2016-03-04 07:38:11 -08001798 return sizeof(TriColorShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001799}
reed773ceda2016-03-03 18:18:25 -08001800
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001801void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001802 SkTriColorShader* parent = static_cast<SkTriColorShader*>(const_cast<SkShader*>(&fShader));
1803 TriColorShaderData* set = parent->takeSetupData();
1804 if (set) {
1805 fSetup = setup(set->pts, set->colors, set->state->f0, set->state->f1, set->state->f2);
1806 }
1807
1808 if (!fSetup) {
1809 // Invalid matrices. Not checked before so no need to assert.
1810 return;
1811 }
1812
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001813 const int alphaScale = Sk255To256(this->getPaintAlpha());
1814
reed@android.com8a1c16f2008-12-17 15:59:43 +00001815 SkPoint src;
reed@google.coma76de3d2011-01-13 18:30:42 +00001816
reed@android.com8a1c16f2008-12-17 15:59:43 +00001817 for (int i = 0; i < count; i++) {
1818 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
1819 x += 1;
reed@google.coma76de3d2011-01-13 18:30:42 +00001820
reed@android.com8a1c16f2008-12-17 15:59:43 +00001821 int scale1 = ScalarTo256(src.fX);
1822 int scale2 = ScalarTo256(src.fY);
1823 int scale0 = 256 - scale1 - scale2;
1824 if (scale0 < 0) {
1825 if (scale1 > scale2) {
1826 scale2 = 256 - scale1;
1827 } else {
1828 scale1 = 256 - scale2;
1829 }
1830 scale0 = 0;
1831 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001832
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001833 if (256 != alphaScale) {
1834 scale0 = SkAlphaMul(scale0, alphaScale);
1835 scale1 = SkAlphaMul(scale1, alphaScale);
1836 scale2 = SkAlphaMul(scale2, alphaScale);
1837 }
1838
reed@android.com8a1c16f2008-12-17 15:59:43 +00001839 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001840 SkAlphaMulQ(fColors[1], scale1) +
1841 SkAlphaMulQ(fColors[2], scale2);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001842 }
1843}
1844
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001845#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001846void SkTriColorShader::toString(SkString* str) const {
1847 str->append("SkTriColorShader: (");
1848
1849 this->INHERITED::toString(str);
1850
1851 str->append(")");
1852}
1853#endif
1854
reed@android.com8a1c16f2008-12-17 15:59:43 +00001855void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
1856 const SkPoint vertices[], const SkPoint textures[],
1857 const SkColor colors[], SkXfermode* xmode,
1858 const uint16_t indices[], int indexCount,
1859 const SkPaint& paint) const {
bsalomon49f085d2014-09-05 13:34:00 -07001860 SkASSERT(0 == count || vertices);
reed@google.coma76de3d2011-01-13 18:30:42 +00001861
reed@android.com8a1c16f2008-12-17 15:59:43 +00001862 // abort early if there is nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001863 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001864 return;
1865 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001866
reed@android.com8a1c16f2008-12-17 15:59:43 +00001867 // transform out vertices into device coordinates
1868 SkAutoSTMalloc<16, SkPoint> storage(count);
1869 SkPoint* devVerts = storage.get();
1870 fMatrix->mapPoints(devVerts, vertices, count);
reed@google.coma76de3d2011-01-13 18:30:42 +00001871
reed@android.com8a1c16f2008-12-17 15:59:43 +00001872 /*
1873 We can draw the vertices in 1 of 4 ways:
1874
1875 - solid color (no shader/texture[], no colors[])
1876 - just colors (no shader/texture[], has colors[])
1877 - just texture (has shader/texture[], no colors[])
1878 - colors * texture (has shader/texture[], has colors[])
reed@google.coma76de3d2011-01-13 18:30:42 +00001879
reed@android.com8a1c16f2008-12-17 15:59:43 +00001880 Thus for texture drawing, we need both texture[] and a shader.
1881 */
1882
reed8a21c9f2016-03-08 18:50:00 -08001883 auto triShader = sk_make_sp<SkTriColorShader>();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001884 SkPaint p(paint);
1885
1886 SkShader* shader = p.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -07001887 if (nullptr == shader) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001888 // if we have no shader, we ignore the texture coordinates
halcanary96fcdcc2015-08-27 07:41:13 -07001889 textures = nullptr;
1890 } else if (nullptr == textures) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001891 // if we don't have texture coordinates, ignore the shader
halcanary96fcdcc2015-08-27 07:41:13 -07001892 p.setShader(nullptr);
1893 shader = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001894 }
1895
1896 // setup the custom shader (if needed)
reed8a21c9f2016-03-08 18:50:00 -08001897 sk_sp<SkShader> composeShader;
bsalomon49f085d2014-09-05 13:34:00 -07001898 if (colors) {
halcanary96fcdcc2015-08-27 07:41:13 -07001899 if (nullptr == textures) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001900 // just colors (no texture)
reed8a21c9f2016-03-08 18:50:00 -08001901 p.setShader(triShader);
1902 shader = p.getShader();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001903 } else {
1904 // colors * texture
1905 SkASSERT(shader);
1906 bool releaseMode = false;
halcanary96fcdcc2015-08-27 07:41:13 -07001907 if (nullptr == xmode) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001908 xmode = SkXfermode::Create(SkXfermode::kModulate_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001909 releaseMode = true;
1910 }
reed8a21c9f2016-03-08 18:50:00 -08001911 composeShader = sk_make_sp<SkComposeShader>(triShader, sk_ref_sp(shader), xmode);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001912 p.setShader(composeShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001913 if (releaseMode) {
1914 xmode->unref();
1915 }
1916 }
1917 }
1918
reed41e010c2015-06-09 12:16:53 -07001919 SkAutoBlitterChoose blitter(fDst, *fMatrix, p);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001920 // Abort early if we failed to create a shader context.
reed@google.comea033602012-12-14 13:13:55 +00001921 if (blitter->isNullBlitter()) {
1922 return;
1923 }
1924
reed@android.com8a1c16f2008-12-17 15:59:43 +00001925 // setup our state and function pointer for iterating triangles
1926 VertState state(count, indices, indexCount);
1927 VertState::Proc vertProc = state.chooseProc(vmode);
reed@google.coma76de3d2011-01-13 18:30:42 +00001928
bsalomon49f085d2014-09-05 13:34:00 -07001929 if (textures || colors) {
aleksandar.stojiljkovic88cb8222016-03-08 11:18:21 -08001930 SkTriColorShader::TriColorShaderData verticesSetup = { vertices, colors, &state };
1931
reed@android.com8a1c16f2008-12-17 15:59:43 +00001932 while (vertProc(&state)) {
bsalomon49f085d2014-09-05 13:34:00 -07001933 if (textures) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001934 SkMatrix tempM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001935 if (texture_to_matrix(state, vertices, textures, &tempM)) {
fmalitad0c4e092016-02-22 17:19:04 -08001936 SkShader::ContextRec rec(p, *fMatrix, &tempM,
1937 SkBlitter::PreferredShaderDest(fDst.info()));
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001938 if (!blitter->resetShaderContext(rec)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001939 continue;
1940 }
1941 }
1942 }
bsalomon49f085d2014-09-05 13:34:00 -07001943 if (colors) {
reed8a21c9f2016-03-08 18:50:00 -08001944 triShader->bindSetupData(&verticesSetup);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001945 }
reed@google.com045e62d2011-10-24 12:19:46 +00001946
1947 SkPoint tmp[] = {
1948 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
1949 };
1950 SkScan::FillTriangle(tmp, *fRC, blitter.get());
reed8a21c9f2016-03-08 18:50:00 -08001951 triShader->bindSetupData(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001952 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001953 } else {
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001954 // no colors[] and no texture, stroke hairlines with paint's color.
reed5dc6b7d2015-04-14 10:40:44 -07001955 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
reed@google.com045e62d2011-10-24 12:19:46 +00001956 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001957 while (vertProc(&state)) {
reed5dc6b7d2015-04-14 10:40:44 -07001958 SkPoint array[] = {
1959 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
1960 };
1961 hairProc(array, 4, clip, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001962 }
1963 }
1964}
1965
reed@google.com0a0a2362011-03-23 13:51:55 +00001966///////////////////////////////////////////////////////////////////////////////
1967///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001968
1969#ifdef SK_DEBUG
1970
reed@android.comf2b98d62010-12-20 18:26:13 +00001971void SkDraw::validate() const {
halcanary96fcdcc2015-08-27 07:41:13 -07001972 SkASSERT(fMatrix != nullptr);
1973 SkASSERT(fClip != nullptr);
1974 SkASSERT(fRC != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001975
reed@google.com045e62d2011-10-24 12:19:46 +00001976 const SkIRect& cr = fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001977 SkIRect br;
1978
reed41e010c2015-06-09 12:16:53 -07001979 br.set(0, 0, fDst.width(), fDst.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001980 SkASSERT(cr.isEmpty() || br.contains(cr));
1981}
1982
1983#endif
1984
reed@android.com8a1c16f2008-12-17 15:59:43 +00001985////////////////////////////////////////////////////////////////////////////////////////////////
1986
1987#include "SkPath.h"
1988#include "SkDraw.h"
1989#include "SkRegion.h"
1990#include "SkBlitter.h"
1991
1992static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
reedb07a94f2014-11-19 05:03:18 -08001993 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001994 SkIRect* bounds) {
1995 if (devPath.isEmpty()) {
1996 return false;
1997 }
1998
reed@android.com8a1c16f2008-12-17 15:59:43 +00001999 // init our bounds from the path
reed11fa2242015-03-13 06:08:28 -07002000 *bounds = devPath.getBounds().makeOutset(SK_ScalarHalf, SK_ScalarHalf).roundOut();
reed@google.coma76de3d2011-01-13 18:30:42 +00002001
tomhudson@google.com6db75fc2012-03-23 14:46:48 +00002002 SkIPoint margin = SkIPoint::Make(0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002003 if (filter) {
2004 SkASSERT(filterMatrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00002005
bungeman@google.com5af16f82011-09-02 15:06:44 +00002006 SkMask srcM, dstM;
reed@google.coma76de3d2011-01-13 18:30:42 +00002007
reed@android.com8a1c16f2008-12-17 15:59:43 +00002008 srcM.fBounds = *bounds;
2009 srcM.fFormat = SkMask::kA8_Format;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002010 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2011 return false;
2012 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002013 }
2014
bungeman@google.com5af16f82011-09-02 15:06:44 +00002015 // (possibly) trim the bounds to reflect the clip
reed@android.com8a1c16f2008-12-17 15:59:43 +00002016 // (plus whatever slop the filter needs)
bungeman@google.com5af16f82011-09-02 15:06:44 +00002017 if (clipBounds) {
reed@android.com35555912009-03-16 18:46:55 +00002018 // Ugh. Guard against gigantic margins from wacky filters. Without this
2019 // check we can request arbitrary amounts of slop beyond our visible
2020 // clip, and bring down the renderer (at least on finite RAM machines
2021 // like handsets, etc.). Need to balance this invented value between
2022 // quality of large filters like blurs, and the corresponding memory
2023 // requests.
2024 static const int MAX_MARGIN = 128;
reed11fa2242015-03-13 06:08:28 -07002025 if (!bounds->intersect(clipBounds->makeOutset(SkMin32(margin.fX, MAX_MARGIN),
2026 SkMin32(margin.fY, MAX_MARGIN)))) {
bungeman@google.com5af16f82011-09-02 15:06:44 +00002027 return false;
2028 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002029 }
2030
2031 return true;
2032}
2033
reed41e010c2015-06-09 12:16:53 -07002034static void draw_into_mask(const SkMask& mask, const SkPath& devPath, SkPaint::Style style) {
2035 SkDraw draw;
2036 if (!draw.fDst.reset(mask)) {
2037 return;
2038 }
2039
reed@google.com045e62d2011-10-24 12:19:46 +00002040 SkRasterClip clip;
2041 SkMatrix matrix;
2042 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002043
reed@google.com045e62d2011-10-24 12:19:46 +00002044 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00002045 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2046 -SkIntToScalar(mask.fBounds.fTop));
2047
reed@google.com045e62d2011-10-24 12:19:46 +00002048 draw.fRC = &clip;
2049 draw.fClip = &clip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002050 draw.fMatrix = &matrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002051 paint.setAntiAlias(true);
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002052 paint.setStyle(style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002053 draw.drawPath(devPath, paint);
2054}
2055
2056bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002057 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002058 SkMask* mask, SkMask::CreateMode mode,
2059 SkPaint::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002060 if (SkMask::kJustRenderImage_CreateMode != mode) {
2061 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2062 return false;
2063 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002064
reed@android.com8a1c16f2008-12-17 15:59:43 +00002065 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2066 mask->fFormat = SkMask::kA8_Format;
2067 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +00002068 size_t size = mask->computeImageSize();
2069 if (0 == size) {
2070 // we're too big to allocate the mask, abort
2071 return false;
2072 }
2073 mask->fImage = SkMask::AllocImage(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002074 memset(mask->fImage, 0, mask->computeImageSize());
2075 }
2076
2077 if (SkMask::kJustComputeBounds_CreateMode != mode) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002078 draw_into_mask(*mask, devPath, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002079 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002080
reed@android.com8a1c16f2008-12-17 15:59:43 +00002081 return true;
2082}