Chromium Code Reviews| 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 |
| 10 #include "GrDrawState.h" | 10 #include "GrDrawState.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 paint.setStyle(SkPaint::kFill_Style); | 118 paint.setStyle(SkPaint::kFill_Style); |
| 119 } else { | 119 } else { |
| 120 paint.setStyle(SkPaint::kStroke_Style); | 120 paint.setStyle(SkPaint::kStroke_Style); |
| 121 paint.setStrokeJoin(stroke.getJoin()); | 121 paint.setStrokeJoin(stroke.getJoin()); |
| 122 paint.setStrokeCap(stroke.getCap()); | 122 paint.setStrokeCap(stroke.getCap()); |
| 123 paint.setStrokeWidth(stroke.getWidth()); | 123 paint.setStrokeWidth(stroke.getWidth()); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 paint.setAntiAlias(antiAlias); | 126 paint.setAntiAlias(antiAlias); |
| 127 | 127 |
| 128 SkTBlitterAllocator allocator; | |
| 129 SkBlitter* blitter = NULL; | |
| 130 if (kBlitter_CompressionMode == fCompressionMode) { | |
| 131 blitter = SkTextureCompressor::CreateBlitterForFormat( | |
| 132 fBM.width(), fBM.height(), fCompressedBuffer, &allocator, fCompresse dFormat); | |
| 133 } | |
| 134 | |
| 128 if (SkRegion::kReplace_Op == op && 0xFF == alpha) { | 135 if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 129 SkASSERT(0xFF == paint.getAlpha()); | 136 SkASSERT(0xFF == paint.getAlpha()); |
| 130 fDraw.drawPathCoverage(path, paint); | 137 fDraw.drawPathCoverage(path, paint, blitter); |
| 131 } else { | 138 } else { |
| 132 paint.setXfermodeMode(op_to_mode(op)); | 139 paint.setXfermodeMode(op_to_mode(op)); |
| 133 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); | 140 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
| 134 fDraw.drawPath(path, paint); | 141 fDraw.drawPath(path, paint, blitter); |
| 135 } | 142 } |
| 136 } | 143 } |
| 137 | 144 |
| 138 bool GrSWMaskHelper::init(const SkIRect& resultBounds, | 145 bool GrSWMaskHelper::init(const SkIRect& resultBounds, |
| 139 const SkMatrix* matrix) { | 146 const SkMatrix* matrix) { |
| 140 if (NULL != matrix) { | 147 if (NULL != matrix) { |
| 141 fMatrix = *matrix; | 148 fMatrix = *matrix; |
| 142 } else { | 149 } else { |
| 143 fMatrix.setIdentity(); | 150 fMatrix.setIdentity(); |
| 144 } | 151 } |
| 145 | 152 |
| 146 // Now translate so the bound's UL corner is at the origin | 153 // Now translate so the bound's UL corner is at the origin |
| 147 fMatrix.postTranslate(-resultBounds.fLeft * SK_Scalar1, | 154 fMatrix.postTranslate(-resultBounds.fLeft * SK_Scalar1, |
| 148 -resultBounds.fTop * SK_Scalar1); | 155 -resultBounds.fTop * SK_Scalar1); |
| 149 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), | 156 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), |
| 150 resultBounds.height()); | 157 resultBounds.height()); |
| 151 | 158 |
| 152 #if GR_COMPRESS_ALPHA_MASK | 159 #if GR_COMPRESS_ALPHA_MASK |
| 153 fCompressMask = choose_compressed_fmt(fContext->getGpu()->caps(), &fCompress edFormat); | 160 if (choose_compressed_fmt(fContext->getGpu()->caps(), &fCompressedFormat)) { |
| 154 #else | 161 fCompressionMode = kCompress_CompressionMode; |
| 155 fCompressMask = false; | 162 } |
| 156 #endif | 163 #endif |
| 157 | 164 |
|
robertphillips
2014/08/07 13:55:06
Update comment ?
krajcevski
2014/08/07 14:48:12
Done.
| |
| 158 // Make sure that the width is a multiple of 16 so that we can use | 165 // Make sure that the width is a multiple of 16 so that we can use |
| 159 // specialized SIMD instructions that compress 4 blocks at a time. | 166 // specialized SIMD instructions that compress 4 blocks at a time. |
| 160 int cmpWidth, cmpHeight; | 167 int cmpWidth = bounds.fRight; |
| 161 if (fCompressMask) { | 168 int cmpHeight = bounds.fBottom; |
| 169 if (kCompress_CompressionMode == fCompressionMode) { | |
| 162 int dimX, dimY; | 170 int dimX, dimY; |
| 163 SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY) ; | 171 SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY) ; |
| 164 cmpWidth = dimX * ((bounds.fRight + (dimX - 1)) / dimX); | 172 cmpWidth = dimX * ((cmpWidth + (dimX - 1)) / dimX); |
| 165 cmpHeight = dimY * ((bounds.fBottom + (dimY - 1)) / dimY); | 173 cmpHeight = dimY * ((cmpHeight + (dimY - 1)) / dimY); |
| 174 | |
| 175 // Can we create a blitter? | |
| 176 if (SkTextureCompressor::ExistsBlitterForFormat(fCompressedFormat)) { | |
| 177 int cmpSz = SkTextureCompressor::GetCompressedDataSize( | |
| 178 fCompressedFormat, cmpWidth, cmpHeight); | |
| 179 | |
| 180 SkASSERT(cmpSz > 0); | |
|
robertphillips
2014/08/07 13:55:06
SkASSERT(NULL == fCompressedBuffer); ?
krajcevski
2014/08/07 14:48:12
Done.
| |
| 181 fCompressedBuffer = sk_malloc_throw(cmpSz); | |
| 182 fCompressionMode = kBlitter_CompressionMode; | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 // If we don't have a custom blitter, then we either need a bitmap to compre ss | |
| 187 // from or a bitmap that we're going to use as a texture. In any case, we sh ould | |
| 188 // allocate the pixels for a bitmap | |
| 189 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(cmpWidth, cmpHeight); | |
| 190 if (kBlitter_CompressionMode != fCompressionMode) { | |
| 191 if (!fBM.allocPixels(bmImageInfo)) { | |
| 192 return false; | |
| 193 } | |
| 194 | |
| 195 sk_bzero(fBM.getPixels(), fBM.getSafeSize()); | |
| 166 } else { | 196 } else { |
| 167 cmpWidth = bounds.fRight; | 197 // Otherwise, we just need to remember how big the buffer is... |
| 168 cmpHeight = bounds.fBottom; | 198 fBM.setInfo(bmImageInfo); |
| 169 } | 199 } |
| 170 | 200 |
| 171 if (!fBM.allocPixels(SkImageInfo::MakeA8(cmpWidth, cmpHeight))) { | 201 sk_bzero(&fDraw, sizeof(fDraw)); |
| 172 return false; | |
| 173 } | |
| 174 | 202 |
| 175 sk_bzero(fBM.getPixels(), fBM.getSafeSize()); | |
| 176 | |
| 177 sk_bzero(&fDraw, sizeof(fDraw)); | |
| 178 fRasterClip.setRect(bounds); | 203 fRasterClip.setRect(bounds); |
| 179 fDraw.fRC = &fRasterClip; | 204 fDraw.fRC = &fRasterClip; |
| 180 fDraw.fClip = &fRasterClip.bwRgn(); | 205 fDraw.fClip = &fRasterClip.bwRgn(); |
| 181 fDraw.fMatrix = &fMatrix; | 206 fDraw.fMatrix = &fMatrix; |
| 182 fDraw.fBitmap = &fBM; | 207 fDraw.fBitmap = &fBM; |
| 183 return true; | 208 return true; |
| 184 } | 209 } |
| 185 | 210 |
| 186 /** | 211 /** |
| 187 * Get a texture (from the texture cache) of the correct size & format. | 212 * Get a texture (from the texture cache) of the correct size & format. |
| 188 * Return true on success; false on failure. | 213 * Return true on success; false on failure. |
| 189 */ | 214 */ |
| 190 bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* texture) { | 215 bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* texture) { |
| 191 GrTextureDesc desc; | 216 GrTextureDesc desc; |
| 192 desc.fWidth = fBM.width(); | 217 desc.fWidth = fBM.width(); |
| 193 desc.fHeight = fBM.height(); | 218 desc.fHeight = fBM.height(); |
| 194 desc.fConfig = kAlpha_8_GrPixelConfig; | 219 desc.fConfig = kAlpha_8_GrPixelConfig; |
| 195 | 220 |
| 196 if (fCompressMask) { | 221 if (kNone_CompressionMode != fCompressionMode) { |
| 197 | 222 |
| 198 #ifdef SK_DEBUG | 223 #ifdef SK_DEBUG |
| 199 int dimX, dimY; | 224 int dimX, dimY; |
| 200 SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY) ; | 225 SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY) ; |
| 201 SkASSERT((desc.fWidth % dimX) == 0); | 226 SkASSERT((desc.fWidth % dimX) == 0); |
| 202 SkASSERT((desc.fHeight % dimY) == 0); | 227 SkASSERT((desc.fHeight % dimY) == 0); |
| 203 #endif | 228 #endif |
| 204 | 229 |
| 205 desc.fConfig = fmt_to_config(fCompressedFormat); | 230 desc.fConfig = fmt_to_config(fCompressedFormat); |
| 206 | 231 SkASSERT(fContext->getGpu()->caps()->isConfigTexturable(desc.fConfig)); |
| 207 // If this config isn't supported then we should fall back to A8 | |
| 208 if (!(fContext->getGpu()->caps()->isConfigTexturable(desc.fConfig))) { | |
| 209 SkDEBUGFAIL("Determining compression should be set from choose_compr essed_fmt"); | |
| 210 desc.fConfig = kAlpha_8_GrPixelConfig; | |
| 211 } | |
| 212 } | 232 } |
| 213 | 233 |
| 214 texture->set(fContext, desc); | 234 texture->set(fContext, desc); |
| 215 return NULL != texture->texture(); | 235 return NULL != texture->texture(); |
| 216 } | 236 } |
| 217 | 237 |
| 218 void GrSWMaskHelper::sendTextureData(GrTexture *texture, const GrTextureDesc& de sc, | 238 void GrSWMaskHelper::sendTextureData(GrTexture *texture, const GrTextureDesc& de sc, |
| 219 const void *data, int rowbytes) { | 239 const void *data, int rowbytes) { |
| 220 // If we aren't reusing scratch textures we don't need to flush before | 240 // If we aren't reusing scratch textures we don't need to flush before |
| 221 // writing since no one else will be using 'texture' | 241 // writing since no one else will be using 'texture' |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 246 */ | 266 */ |
| 247 void GrSWMaskHelper::toTexture(GrTexture *texture) { | 267 void GrSWMaskHelper::toTexture(GrTexture *texture) { |
| 248 SkAutoLockPixels alp(fBM); | 268 SkAutoLockPixels alp(fBM); |
| 249 | 269 |
| 250 GrTextureDesc desc; | 270 GrTextureDesc desc; |
| 251 desc.fWidth = fBM.width(); | 271 desc.fWidth = fBM.width(); |
| 252 desc.fHeight = fBM.height(); | 272 desc.fHeight = fBM.height(); |
| 253 desc.fConfig = texture->config(); | 273 desc.fConfig = texture->config(); |
| 254 | 274 |
| 255 // First see if we should compress this texture before uploading. | 275 // First see if we should compress this texture before uploading. |
| 256 if (fCompressMask) { | 276 switch (fCompressionMode) { |
| 257 this->compressTextureData(texture, desc); | 277 case kNone_CompressionMode: |
| 258 } else { | 278 this->sendTextureData(texture, desc, fBM.getPixels(), fBM.rowBytes() ); |
| 259 // Looks like we have to send a full A8 texture. | 279 break; |
| 260 this->sendTextureData(texture, desc, fBM.getPixels(), fBM.rowBytes()); | 280 |
| 281 case kCompress_CompressionMode: | |
| 282 this->compressTextureData(texture, desc); | |
| 283 break; | |
| 284 | |
| 285 case kBlitter_CompressionMode: | |
| 286 SkASSERT(NULL != fCompressedBuffer); | |
| 287 this->sendTextureData(texture, desc, fCompressedBuffer, 0); | |
| 288 break; | |
| 261 } | 289 } |
| 262 } | 290 } |
| 263 | 291 |
| 264 //////////////////////////////////////////////////////////////////////////////// | 292 //////////////////////////////////////////////////////////////////////////////// |
| 265 /** | 293 /** |
| 266 * Software rasterizes path to A8 mask (possibly using the context's matrix) | 294 * Software rasterizes path to A8 mask (possibly using the context's matrix) |
| 267 * and uploads the result to a scratch texture. Returns the resulting | 295 * and uploads the result to a scratch texture. Returns the resulting |
| 268 * texture on success; NULL on failure. | 296 * texture on success; NULL on failure. |
| 269 */ | 297 */ |
| 270 GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, | 298 GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 maskMatrix.preConcat(drawState->getViewMatrix()); | 345 maskMatrix.preConcat(drawState->getViewMatrix()); |
| 318 | 346 |
| 319 drawState->addCoverageEffect( | 347 drawState->addCoverageEffect( |
| 320 GrSimpleTextureEffect::Create(texture, | 348 GrSimpleTextureEffect::Create(texture, |
| 321 maskMatrix, | 349 maskMatrix, |
| 322 GrTextureParams::kNone_Fi lterMode, | 350 GrTextureParams::kNone_Fi lterMode, |
| 323 kPosition_GrCoordSet))->u nref(); | 351 kPosition_GrCoordSet))->u nref(); |
| 324 | 352 |
| 325 target->drawSimpleRect(dstRect); | 353 target->drawSimpleRect(dstRect); |
| 326 } | 354 } |
| OLD | NEW |