| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrSWMaskHelper.h" | 8 #include "GrSWMaskHelper.h" |
| 9 |
| 9 #include "GrDrawState.h" | 10 #include "GrDrawState.h" |
| 10 #include "GrDrawTargetCaps.h" | 11 #include "GrDrawTargetCaps.h" |
| 11 #include "GrGpu.h" | 12 #include "GrGpu.h" |
| 12 | 13 |
| 14 #include "SkData.h" |
| 13 #include "SkStrokeRec.h" | 15 #include "SkStrokeRec.h" |
| 16 #include "SkTextureCompressor.h" |
| 14 | 17 |
| 15 // TODO: try to remove this #include | 18 // TODO: try to remove this #include |
| 16 #include "GrContext.h" | 19 #include "GrContext.h" |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 22 |
| 19 /* | 23 /* |
| 20 * Convert a boolean operation into a transfer mode code | 24 * Convert a boolean operation into a transfer mode code |
| 21 */ | 25 */ |
| 22 SkXfermode::Mode op_to_mode(SkRegion::Op op) { | 26 SkXfermode::Mode op_to_mode(SkRegion::Op op) { |
| 23 | 27 |
| 24 static const SkXfermode::Mode modeMap[] = { | 28 static const SkXfermode::Mode modeMap[] = { |
| 25 SkXfermode::kDstOut_Mode, // kDifference_Op | 29 SkXfermode::kDstOut_Mode, // kDifference_Op |
| 26 SkXfermode::kModulate_Mode, // kIntersect_Op | 30 SkXfermode::kModulate_Mode, // kIntersect_Op |
| 27 SkXfermode::kSrcOver_Mode, // kUnion_Op | 31 SkXfermode::kSrcOver_Mode, // kUnion_Op |
| 28 SkXfermode::kXor_Mode, // kXOR_Op | 32 SkXfermode::kXor_Mode, // kXOR_Op |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* texture) { | 124 bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* texture) { |
| 121 GrTextureDesc desc; | 125 GrTextureDesc desc; |
| 122 desc.fWidth = fBM.width(); | 126 desc.fWidth = fBM.width(); |
| 123 desc.fHeight = fBM.height(); | 127 desc.fHeight = fBM.height(); |
| 124 desc.fConfig = kAlpha_8_GrPixelConfig; | 128 desc.fConfig = kAlpha_8_GrPixelConfig; |
| 125 | 129 |
| 126 texture->set(fContext, desc); | 130 texture->set(fContext, desc); |
| 127 return NULL != texture->texture(); | 131 return NULL != texture->texture(); |
| 128 } | 132 } |
| 129 | 133 |
| 134 GrTexture* GrSWMaskHelper::toLATCTexture(GrContext* ctx) { |
| 135 // Encode the BM into LATC data: |
| 136 SkAutoLockPixels alp(fBM); |
| 137 SkTextureCompressor::Format format = SkTextureCompressor::kLATC_Format; |
| 138 SkAutoDataUnref latcData(SkTextureCompressor::CompressBitmapToFormat(fBM, fo
rmat)); |
| 139 if (NULL == latcData) { |
| 140 return NULL; |
| 141 } |
| 142 |
| 143 GrTextureDesc desc; |
| 144 desc.fWidth = fBM.width(); |
| 145 desc.fHeight = fBM.height(); |
| 146 desc.fConfig = kLATC_GrPixelConfig; |
| 147 return ctx->getGpu()->createTexture(desc, latcData->bytes(), 0); |
| 148 } |
| 149 |
| 130 /** | 150 /** |
| 131 * Move the result of the software mask generation back to the gpu | 151 * Move the result of the software mask generation back to the gpu |
| 132 */ | 152 */ |
| 133 void GrSWMaskHelper::toTexture(GrTexture *texture) { | 153 void GrSWMaskHelper::toTexture(GrTexture *texture) { |
| 134 SkAutoLockPixels alp(fBM); | 154 SkAutoLockPixels alp(fBM); |
| 135 | 155 |
| 136 // If we aren't reusing scratch textures we don't need to flush before | 156 // If we aren't reusing scratch textures we don't need to flush before |
| 137 // writing since no one else will be using 'texture' | 157 // writing since no one else will be using 'texture' |
| 138 bool reuseScratch = fContext->getGpu()->caps()->reuseScratchTextures(); | 158 bool reuseScratch = fContext->getGpu()->caps()->reuseScratchTextures(); |
| 139 | 159 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 151 * Software rasterizes path to A8 mask (possibly using the context's matrix) | 171 * Software rasterizes path to A8 mask (possibly using the context's matrix) |
| 152 * and uploads the result to a scratch texture. Returns the resulting | 172 * and uploads the result to a scratch texture. Returns the resulting |
| 153 * texture on success; NULL on failure. | 173 * texture on success; NULL on failure. |
| 154 */ | 174 */ |
| 155 GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, | 175 GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, |
| 156 const SkPath& path, | 176 const SkPath& path, |
| 157 const SkStrokeRec& stroke, | 177 const SkStrokeRec& stroke, |
| 158 const SkIRect& resultBounds, | 178 const SkIRect& resultBounds, |
| 159 bool antiAlias, | 179 bool antiAlias, |
| 160 SkMatrix* matrix) { | 180 SkMatrix* matrix) { |
| 161 GrAutoScratchTexture ast; | |
| 162 | |
| 163 GrSWMaskHelper helper(context); | 181 GrSWMaskHelper helper(context); |
| 164 | 182 |
| 165 if (!helper.init(resultBounds, matrix)) { | 183 if (!helper.init(resultBounds, matrix)) { |
| 166 return NULL; | 184 return NULL; |
| 167 } | 185 } |
| 168 | 186 |
| 169 helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF); | 187 helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF); |
| 170 | 188 |
| 189 #if GR_COMPRESS_ALPHA_MASK |
| 190 // Can we create an LATC texture? |
| 191 GrTexture *latc = helper.toLATCTexture(context); |
| 192 if (NULL != latc) { |
| 193 return latc; |
| 194 } |
| 195 #endif |
| 196 |
| 197 // Looks like we have to send a full A8 texture. |
| 198 GrAutoScratchTexture ast; |
| 171 if (!helper.getTexture(&ast)) { | 199 if (!helper.getTexture(&ast)) { |
| 172 return NULL; | 200 return NULL; |
| 173 } | 201 } |
| 174 | 202 |
| 175 helper.toTexture(ast.texture()); | 203 helper.toTexture(ast.texture()); |
| 176 | 204 |
| 177 return ast.detach(); | 205 return ast.detach(); |
| 178 } | 206 } |
| 179 | 207 |
| 180 void GrSWMaskHelper::DrawToTargetWithPathMask(GrTexture* texture, | 208 void GrSWMaskHelper::DrawToTargetWithPathMask(GrTexture* texture, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 203 maskMatrix.preConcat(drawState->getViewMatrix()); | 231 maskMatrix.preConcat(drawState->getViewMatrix()); |
| 204 | 232 |
| 205 drawState->addCoverageEffect( | 233 drawState->addCoverageEffect( |
| 206 GrSimpleTextureEffect::Create(texture, | 234 GrSimpleTextureEffect::Create(texture, |
| 207 maskMatrix, | 235 maskMatrix, |
| 208 GrTextureParams::kNone_Fi
lterMode, | 236 GrTextureParams::kNone_Fi
lterMode, |
| 209 kPosition_GrCoordSet))->u
nref(); | 237 kPosition_GrCoordSet))->u
nref(); |
| 210 | 238 |
| 211 target->drawSimpleRect(dstRect); | 239 target->drawSimpleRect(dstRect); |
| 212 } | 240 } |
| OLD | NEW |