robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrSWMaskHelper.h" |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 9 | |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 12 | #include "GrGpu.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 13 | #include "GrPipelineBuilder.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 14 | |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 15 | #include "SkData.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 16 | #include "SkDistanceFieldGen.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 17 | #include "SkStrokeRec.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 18 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 19 | #include "batches/GrRectBatchFactory.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 20 | |
| 21 | namespace { |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 22 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Convert a boolean operation into a transfer mode code |
| 25 | */ |
| 26 | SkXfermode::Mode op_to_mode(SkRegion::Op op) { |
| 27 | |
| 28 | static const SkXfermode::Mode modeMap[] = { |
| 29 | SkXfermode::kDstOut_Mode, // kDifference_Op |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 30 | SkXfermode::kModulate_Mode, // kIntersect_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 31 | SkXfermode::kSrcOver_Mode, // kUnion_Op |
| 32 | SkXfermode::kXor_Mode, // kXOR_Op |
| 33 | SkXfermode::kClear_Mode, // kReverseDifference_Op |
| 34 | SkXfermode::kSrc_Mode, // kReplace_Op |
| 35 | }; |
| 36 | |
| 37 | return modeMap[op]; |
| 38 | } |
| 39 | |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 40 | static inline GrPixelConfig fmt_to_config(SkTextureCompressor::Format fmt) { |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 41 | |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 42 | GrPixelConfig config; |
| 43 | switch (fmt) { |
| 44 | case SkTextureCompressor::kLATC_Format: |
| 45 | config = kLATC_GrPixelConfig; |
| 46 | break; |
| 47 | |
| 48 | case SkTextureCompressor::kR11_EAC_Format: |
| 49 | config = kR11_EAC_GrPixelConfig; |
| 50 | break; |
| 51 | |
| 52 | case SkTextureCompressor::kASTC_12x12_Format: |
| 53 | config = kASTC_12x12_GrPixelConfig; |
| 54 | break; |
| 55 | |
| 56 | case SkTextureCompressor::kETC1_Format: |
| 57 | config = kETC1_GrPixelConfig; |
| 58 | break; |
| 59 | |
| 60 | default: |
| 61 | SkDEBUGFAIL("No GrPixelConfig for compression format!"); |
| 62 | // Best guess |
| 63 | config = kAlpha_8_GrPixelConfig; |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | return config; |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 68 | } |
| 69 | |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 70 | static bool choose_compressed_fmt(const GrCaps* caps, |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 71 | SkTextureCompressor::Format *fmt) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | if (nullptr == fmt) { |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // We can't use scratch textures without the ability to update |
| 77 | // compressed textures... |
| 78 | if (!(caps->compressedTexSubImageSupport())) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // Figure out what our preferred texture type is. If ASTC is available, that always |
| 83 | // gives the biggest win. Otherwise, in terms of compression speed and accuracy, |
| 84 | // LATC has a slight edge over R11 EAC. |
| 85 | if (caps->isConfigTexturable(kASTC_12x12_GrPixelConfig)) { |
| 86 | *fmt = SkTextureCompressor::kASTC_12x12_Format; |
| 87 | return true; |
| 88 | } else if (caps->isConfigTexturable(kLATC_GrPixelConfig)) { |
| 89 | *fmt = SkTextureCompressor::kLATC_Format; |
| 90 | return true; |
| 91 | } else if (caps->isConfigTexturable(kR11_EAC_GrPixelConfig)) { |
| 92 | *fmt = SkTextureCompressor::kR11_EAC_Format; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 98 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 103 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 104 | void GrSWMaskHelper::draw(const SkRect& rect, SkRegion::Op op, |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 105 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 106 | SkPaint paint; |
| 107 | |
| 108 | SkXfermode* mode = SkXfermode::Create(op_to_mode(op)); |
| 109 | |
krajcevski | 71614ac | 2014-08-13 10:36:18 -0700 | [diff] [blame] | 110 | SkASSERT(kNone_CompressionMode == fCompressionMode); |
| 111 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 112 | paint.setXfermode(mode); |
| 113 | paint.setAntiAlias(antiAlias); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 114 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 115 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 116 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 117 | |
| 118 | SkSafeUnref(mode); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 123 | */ |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 124 | void GrSWMaskHelper::draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op, |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 125 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 126 | |
| 127 | SkPaint paint; |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 128 | if (stroke.isHairlineStyle()) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 129 | paint.setStyle(SkPaint::kStroke_Style); |
| 130 | paint.setStrokeWidth(SK_Scalar1); |
| 131 | } else { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 132 | if (stroke.isFillStyle()) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 133 | paint.setStyle(SkPaint::kFill_Style); |
| 134 | } else { |
| 135 | paint.setStyle(SkPaint::kStroke_Style); |
| 136 | paint.setStrokeJoin(stroke.getJoin()); |
| 137 | paint.setStrokeCap(stroke.getCap()); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 138 | paint.setStrokeWidth(stroke.getWidth()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 141 | paint.setAntiAlias(antiAlias); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 142 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 143 | SkTBlitterAllocator allocator; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 144 | SkBlitter* blitter = nullptr; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 145 | if (kBlitter_CompressionMode == fCompressionMode) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 146 | SkASSERT(fCompressedBuffer.get()); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 147 | blitter = SkTextureCompressor::CreateBlitterForFormat( |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 148 | fPixels.width(), fPixels.height(), fCompressedBuffer.get(), &allocator, |
| 149 | fCompressedFormat); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 152 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 153 | SkASSERT(0xFF == paint.getAlpha()); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 154 | fDraw.drawPathCoverage(path, paint, blitter); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 155 | } else { |
| 156 | paint.setXfermodeMode(op_to_mode(op)); |
| 157 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 158 | fDraw.drawPath(path, paint, blitter); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 159 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 160 | } |
| 161 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 162 | bool GrSWMaskHelper::init(const SkIRect& resultBounds, |
krajcevski | 71614ac | 2014-08-13 10:36:18 -0700 | [diff] [blame] | 163 | const SkMatrix* matrix, |
| 164 | bool allowCompression) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 165 | if (matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 166 | fMatrix = *matrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 167 | } else { |
| 168 | fMatrix.setIdentity(); |
| 169 | } |
| 170 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 171 | // Now translate so the bound's UL corner is at the origin |
| 172 | fMatrix.postTranslate(-resultBounds.fLeft * SK_Scalar1, |
| 173 | -resultBounds.fTop * SK_Scalar1); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 174 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 175 | resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 176 | |
krajcevski | 71614ac | 2014-08-13 10:36:18 -0700 | [diff] [blame] | 177 | if (allowCompression && |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 178 | fContext->caps()->drawPathMasksToCompressedTexturesSupport() && |
| 179 | choose_compressed_fmt(fContext->caps(), &fCompressedFormat)) { |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 180 | fCompressionMode = kCompress_CompressionMode; |
| 181 | } |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 182 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 183 | // Make sure that the width is a multiple of the desired block dimensions |
| 184 | // to allow for specialized SIMD instructions that compress multiple blocks at a time. |
| 185 | int cmpWidth = bounds.fRight; |
| 186 | int cmpHeight = bounds.fBottom; |
| 187 | if (kCompress_CompressionMode == fCompressionMode) { |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 188 | int dimX, dimY; |
| 189 | SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 190 | cmpWidth = dimX * ((cmpWidth + (dimX - 1)) / dimX); |
| 191 | cmpHeight = dimY * ((cmpHeight + (dimY - 1)) / dimY); |
| 192 | |
| 193 | // Can we create a blitter? |
| 194 | if (SkTextureCompressor::ExistsBlitterForFormat(fCompressedFormat)) { |
| 195 | int cmpSz = SkTextureCompressor::GetCompressedDataSize( |
| 196 | fCompressedFormat, cmpWidth, cmpHeight); |
| 197 | |
| 198 | SkASSERT(cmpSz > 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | SkASSERT(nullptr == fCompressedBuffer.get()); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 200 | fCompressedBuffer.reset(cmpSz); |
| 201 | fCompressionMode = kBlitter_CompressionMode; |
| 202 | } |
| 203 | } |
| 204 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 205 | sk_bzero(&fDraw, sizeof(fDraw)); |
| 206 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 207 | // If we don't have a custom blitter, then we either need a bitmap to compress |
| 208 | // from or a bitmap that we're going to use as a texture. In any case, we should |
| 209 | // allocate the pixels for a bitmap |
| 210 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(cmpWidth, cmpHeight); |
| 211 | if (kBlitter_CompressionMode != fCompressionMode) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 212 | if (!fPixels.tryAlloc(bmImageInfo)) { |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 213 | return false; |
| 214 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 215 | fPixels.erase(0); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 216 | } else { |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 217 | // Otherwise, we just need to remember how big the buffer is... |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 218 | fPixels.reset(bmImageInfo); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 219 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 220 | fDraw.fDst = fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 221 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 222 | fDraw.fRC = &fRasterClip; |
| 223 | fDraw.fClip = &fRasterClip.bwRgn(); |
| 224 | fDraw.fMatrix = &fMatrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | |
| 228 | /** |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 229 | * Get a texture (from the texture cache) of the correct size & format. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 230 | */ |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 231 | GrTexture* GrSWMaskHelper::createTexture() { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 232 | GrSurfaceDesc desc; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 233 | desc.fWidth = fPixels.width(); |
| 234 | desc.fHeight = fPixels.height(); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 235 | desc.fConfig = kAlpha_8_GrPixelConfig; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 236 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 237 | if (kNone_CompressionMode != fCompressionMode) { |
krajcevski | b577c55 | 2014-07-16 13:26:43 -0700 | [diff] [blame] | 238 | |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 239 | #ifdef SK_DEBUG |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 240 | int dimX, dimY; |
| 241 | SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY); |
| 242 | SkASSERT((desc.fWidth % dimX) == 0); |
| 243 | SkASSERT((desc.fHeight % dimY) == 0); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 244 | #endif |
| 245 | |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 246 | desc.fConfig = fmt_to_config(fCompressedFormat); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 247 | SkASSERT(fContext->caps()->isConfigTexturable(desc.fConfig)); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 248 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 249 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 250 | return fContext->textureProvider()->createApproxTexture(desc); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 253 | void GrSWMaskHelper::sendTextureData(GrTexture *texture, const GrSurfaceDesc& desc, |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 254 | const void *data, size_t rowbytes) { |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 255 | // If we aren't reusing scratch textures we don't need to flush before |
| 256 | // writing since no one else will be using 'texture' |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 257 | bool reuseScratch = fContext->caps()->reuseScratchTextures(); |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 258 | |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 259 | // Since we're uploading to it, and it's compressed, 'texture' shouldn't |
| 260 | // have a render target. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 261 | SkASSERT(nullptr == texture->asRenderTarget()); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 262 | |
| 263 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, |
| 264 | desc.fConfig, data, rowbytes, |
| 265 | reuseScratch ? 0 : GrContext::kDontFlush_PixelOpsFlag); |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 266 | } |
| 267 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 268 | void GrSWMaskHelper::compressTextureData(GrTexture *texture, const GrSurfaceDesc& desc) { |
krajcevski | b577c55 | 2014-07-16 13:26:43 -0700 | [diff] [blame] | 269 | |
| 270 | SkASSERT(GrPixelConfigIsCompressed(desc.fConfig)); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 271 | SkASSERT(fmt_to_config(fCompressedFormat) == desc.fConfig); |
krajcevski | b577c55 | 2014-07-16 13:26:43 -0700 | [diff] [blame] | 272 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 273 | SkAutoDataUnref cmpData(SkTextureCompressor::CompressBitmapToFormat(fPixels, |
| 274 | fCompressedFormat)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 275 | SkASSERT(cmpData); |
krajcevski | b577c55 | 2014-07-16 13:26:43 -0700 | [diff] [blame] | 276 | |
| 277 | this->sendTextureData(texture, desc, cmpData->data(), 0); |
| 278 | } |
| 279 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 280 | /** |
| 281 | * Move the result of the software mask generation back to the gpu |
| 282 | */ |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 283 | void GrSWMaskHelper::toTexture(GrTexture *texture) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 284 | GrSurfaceDesc desc; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 285 | desc.fWidth = fPixels.width(); |
| 286 | desc.fHeight = fPixels.height(); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 287 | desc.fConfig = texture->config(); |
| 288 | |
| 289 | // First see if we should compress this texture before uploading. |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 290 | switch (fCompressionMode) { |
| 291 | case kNone_CompressionMode: |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 292 | this->sendTextureData(texture, desc, fPixels.addr(), fPixels.rowBytes()); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case kCompress_CompressionMode: |
| 296 | this->compressTextureData(texture, desc); |
| 297 | break; |
| 298 | |
| 299 | case kBlitter_CompressionMode: |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 300 | SkASSERT(fCompressedBuffer.get()); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 301 | this->sendTextureData(texture, desc, fCompressedBuffer.get(), 0); |
| 302 | break; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 303 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 304 | } |
| 305 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Convert mask generation results to a signed distance field |
| 308 | */ |
| 309 | void GrSWMaskHelper::toSDF(unsigned char* sdf) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 310 | SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(), |
| 311 | fPixels.width(), fPixels.height(), fPixels.rowBytes()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 312 | } |
| 313 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 314 | //////////////////////////////////////////////////////////////////////////////// |
| 315 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 316 | * Software rasterizes path to A8 mask (possibly using the context's matrix) |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 317 | * and uploads the result to a scratch texture. Returns the resulting |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 318 | * texture on success; nullptr on failure. |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 319 | */ |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 320 | GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, |
| 321 | const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 322 | const SkStrokeRec& stroke, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 323 | const SkIRect& resultBounds, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 324 | bool antiAlias, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 325 | const SkMatrix* matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 326 | GrSWMaskHelper helper(context); |
| 327 | |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 328 | if (!helper.init(resultBounds, matrix)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 329 | return nullptr; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 330 | } |
| 331 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 332 | helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 333 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 334 | GrTexture* texture(helper.createTexture()); |
| 335 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 336 | return nullptr; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 337 | } |
| 338 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 339 | helper.toTexture(texture); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 340 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 341 | return texture; |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void GrSWMaskHelper::DrawToTargetWithPathMask(GrTexture* texture, |
| 345 | GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 346 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 347 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 348 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 349 | const SkIRect& rect) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 350 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 351 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 354 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps(*pipelineBuilder); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 355 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 356 | SkRect dstRect = SkRect::MakeLTRB(SK_Scalar1 * rect.fLeft, |
| 357 | SK_Scalar1 * rect.fTop, |
| 358 | SK_Scalar1 * rect.fRight, |
| 359 | SK_Scalar1 * rect.fBottom); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 360 | |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 361 | // We use device coords to compute the texture coordinates. We take the device coords and apply |
| 362 | // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling |
| 363 | // matrix to normalized coords. |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 364 | SkMatrix maskMatrix; |
| 365 | maskMatrix.setIDiv(texture->width(), texture->height()); |
| 366 | maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop)); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 367 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 368 | pipelineBuilder->addCoverageFragmentProcessor( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 369 | GrSimpleTextureEffect::Create(texture, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 370 | maskMatrix, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 371 | GrTextureParams::kNone_FilterMode, |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 372 | kDevice_GrCoordSet))->unref(); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 373 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 374 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, SkMatrix::I(), |
| 375 | dstRect, nullptr, &invert)); |
| 376 | target->drawBatch(*pipelineBuilder, batch); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 377 | } |