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