| 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 "GrPipelineBuilder.h" | 10 #include "GrPipelineBuilder.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 paint.setStrokeWidth(stroke.getWidth()); | 139 paint.setStrokeWidth(stroke.getWidth()); |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 paint.setAntiAlias(antiAlias); | 142 paint.setAntiAlias(antiAlias); |
| 143 | 143 |
| 144 SkTBlitterAllocator allocator; | 144 SkTBlitterAllocator allocator; |
| 145 SkBlitter* blitter = NULL; | 145 SkBlitter* blitter = NULL; |
| 146 if (kBlitter_CompressionMode == fCompressionMode) { | 146 if (kBlitter_CompressionMode == fCompressionMode) { |
| 147 SkASSERT(fCompressedBuffer.get()); | 147 SkASSERT(fCompressedBuffer.get()); |
| 148 blitter = SkTextureCompressor::CreateBlitterForFormat( | 148 blitter = SkTextureCompressor::CreateBlitterForFormat( |
| 149 fPixels.width(), fPixels.height(), fCompressedBuffer.get(), &allocat
or, | 149 fBM.width(), fBM.height(), fCompressedBuffer.get(), &allocator, fCom
pressedFormat); |
| 150 fCompressedFormat)
; | |
| 151 } | 150 } |
| 152 | 151 |
| 153 if (SkRegion::kReplace_Op == op && 0xFF == alpha) { | 152 if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 154 SkASSERT(0xFF == paint.getAlpha()); | 153 SkASSERT(0xFF == paint.getAlpha()); |
| 155 fDraw.drawPathCoverage(path, paint, blitter); | 154 fDraw.drawPathCoverage(path, paint, blitter); |
| 156 } else { | 155 } else { |
| 157 paint.setXfermodeMode(op_to_mode(op)); | 156 paint.setXfermodeMode(op_to_mode(op)); |
| 158 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); | 157 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
| 159 fDraw.drawPath(path, paint, blitter); | 158 fDraw.drawPath(path, paint, blitter); |
| 160 } | 159 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 int cmpSz = SkTextureCompressor::GetCompressedDataSize( | 195 int cmpSz = SkTextureCompressor::GetCompressedDataSize( |
| 197 fCompressedFormat, cmpWidth, cmpHeight); | 196 fCompressedFormat, cmpWidth, cmpHeight); |
| 198 | 197 |
| 199 SkASSERT(cmpSz > 0); | 198 SkASSERT(cmpSz > 0); |
| 200 SkASSERT(NULL == fCompressedBuffer.get()); | 199 SkASSERT(NULL == fCompressedBuffer.get()); |
| 201 fCompressedBuffer.reset(cmpSz); | 200 fCompressedBuffer.reset(cmpSz); |
| 202 fCompressionMode = kBlitter_CompressionMode; | 201 fCompressionMode = kBlitter_CompressionMode; |
| 203 } | 202 } |
| 204 } | 203 } |
| 205 | 204 |
| 206 sk_bzero(&fDraw, sizeof(fDraw)); | |
| 207 | |
| 208 // If we don't have a custom blitter, then we either need a bitmap to compre
ss | 205 // If we don't have a custom blitter, then we either need a bitmap to compre
ss |
| 209 // from or a bitmap that we're going to use as a texture. In any case, we sh
ould | 206 // from or a bitmap that we're going to use as a texture. In any case, we sh
ould |
| 210 // allocate the pixels for a bitmap | 207 // allocate the pixels for a bitmap |
| 211 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(cmpWidth, cmpHeight); | 208 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(cmpWidth, cmpHeight); |
| 212 if (kBlitter_CompressionMode != fCompressionMode) { | 209 if (kBlitter_CompressionMode != fCompressionMode) { |
| 213 if (!fPixels.tryAlloc(bmImageInfo)) { | 210 if (!fBM.tryAllocPixels(bmImageInfo)) { |
| 214 return false; | 211 return false; |
| 215 } | 212 } |
| 216 fPixels.erase(0); | 213 |
| 214 sk_bzero(fBM.getPixels(), fBM.getSafeSize()); |
| 217 } else { | 215 } else { |
| 218 // Otherwise, we just need to remember how big the buffer is... | 216 // Otherwise, we just need to remember how big the buffer is... |
| 219 fPixels.reset(bmImageInfo); | 217 fBM.setInfo(bmImageInfo); |
| 220 } | 218 } |
| 221 fDraw.fDst = fPixels; | 219 |
| 220 sk_bzero(&fDraw, sizeof(fDraw)); |
| 221 |
| 222 fRasterClip.setRect(bounds); | 222 fRasterClip.setRect(bounds); |
| 223 fDraw.fRC = &fRasterClip; | 223 fDraw.fRC = &fRasterClip; |
| 224 fDraw.fClip = &fRasterClip.bwRgn(); | 224 fDraw.fClip = &fRasterClip.bwRgn(); |
| 225 fDraw.fMatrix = &fMatrix; | 225 fDraw.fMatrix = &fMatrix; |
| 226 fDraw.fBitmap = &fBM; |
| 226 return true; | 227 return true; |
| 227 } | 228 } |
| 228 | 229 |
| 229 /** | 230 /** |
| 230 * Get a texture (from the texture cache) of the correct size & format. | 231 * Get a texture (from the texture cache) of the correct size & format. |
| 231 */ | 232 */ |
| 232 GrTexture* GrSWMaskHelper::createTexture() { | 233 GrTexture* GrSWMaskHelper::createTexture() { |
| 233 GrSurfaceDesc desc; | 234 GrSurfaceDesc desc; |
| 234 desc.fWidth = fPixels.width(); | 235 desc.fWidth = fBM.width(); |
| 235 desc.fHeight = fPixels.height(); | 236 desc.fHeight = fBM.height(); |
| 236 desc.fConfig = kAlpha_8_GrPixelConfig; | 237 desc.fConfig = kAlpha_8_GrPixelConfig; |
| 237 | 238 |
| 238 if (kNone_CompressionMode != fCompressionMode) { | 239 if (kNone_CompressionMode != fCompressionMode) { |
| 239 | 240 |
| 240 #ifdef SK_DEBUG | 241 #ifdef SK_DEBUG |
| 241 int dimX, dimY; | 242 int dimX, dimY; |
| 242 SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY)
; | 243 SkTextureCompressor::GetBlockDimensions(fCompressedFormat, &dimX, &dimY)
; |
| 243 SkASSERT((desc.fWidth % dimX) == 0); | 244 SkASSERT((desc.fWidth % dimX) == 0); |
| 244 SkASSERT((desc.fHeight % dimY) == 0); | 245 SkASSERT((desc.fHeight % dimY) == 0); |
| 245 #endif | 246 #endif |
| (...skipping 19 matching lines...) Expand all Loading... |
| 265 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, | 266 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, |
| 266 desc.fConfig, data, rowbytes, | 267 desc.fConfig, data, rowbytes, |
| 267 reuseScratch ? 0 : GrContext::kDontFlush_PixelOpsFlag); | 268 reuseScratch ? 0 : GrContext::kDontFlush_PixelOpsFlag); |
| 268 } | 269 } |
| 269 | 270 |
| 270 void GrSWMaskHelper::compressTextureData(GrTexture *texture, const GrSurfaceDesc
& desc) { | 271 void GrSWMaskHelper::compressTextureData(GrTexture *texture, const GrSurfaceDesc
& desc) { |
| 271 | 272 |
| 272 SkASSERT(GrPixelConfigIsCompressed(desc.fConfig)); | 273 SkASSERT(GrPixelConfigIsCompressed(desc.fConfig)); |
| 273 SkASSERT(fmt_to_config(fCompressedFormat) == desc.fConfig); | 274 SkASSERT(fmt_to_config(fCompressedFormat) == desc.fConfig); |
| 274 | 275 |
| 275 SkAutoDataUnref cmpData(SkTextureCompressor::CompressBitmapToFormat(fPixels, | 276 SkAutoDataUnref cmpData(SkTextureCompressor::CompressBitmapToFormat(fBM, fCo
mpressedFormat)); |
| 276 fCompres
sedFormat)); | |
| 277 SkASSERT(cmpData); | 277 SkASSERT(cmpData); |
| 278 | 278 |
| 279 this->sendTextureData(texture, desc, cmpData->data(), 0); | 279 this->sendTextureData(texture, desc, cmpData->data(), 0); |
| 280 } | 280 } |
| 281 | 281 |
| 282 /** | 282 /** |
| 283 * Move the result of the software mask generation back to the gpu | 283 * Move the result of the software mask generation back to the gpu |
| 284 */ | 284 */ |
| 285 void GrSWMaskHelper::toTexture(GrTexture *texture) { | 285 void GrSWMaskHelper::toTexture(GrTexture *texture) { |
| 286 SkAutoLockPixels alp(fBM); |
| 287 |
| 286 GrSurfaceDesc desc; | 288 GrSurfaceDesc desc; |
| 287 desc.fWidth = fPixels.width(); | 289 desc.fWidth = fBM.width(); |
| 288 desc.fHeight = fPixels.height(); | 290 desc.fHeight = fBM.height(); |
| 289 desc.fConfig = texture->config(); | 291 desc.fConfig = texture->config(); |
| 290 | 292 |
| 291 // First see if we should compress this texture before uploading. | 293 // First see if we should compress this texture before uploading. |
| 292 switch (fCompressionMode) { | 294 switch (fCompressionMode) { |
| 293 case kNone_CompressionMode: | 295 case kNone_CompressionMode: |
| 294 this->sendTextureData(texture, desc, fPixels.addr(), fPixels.rowByte
s()); | 296 this->sendTextureData(texture, desc, fBM.getPixels(), fBM.rowBytes()
); |
| 295 break; | 297 break; |
| 296 | 298 |
| 297 case kCompress_CompressionMode: | 299 case kCompress_CompressionMode: |
| 298 this->compressTextureData(texture, desc); | 300 this->compressTextureData(texture, desc); |
| 299 break; | 301 break; |
| 300 | 302 |
| 301 case kBlitter_CompressionMode: | 303 case kBlitter_CompressionMode: |
| 302 SkASSERT(fCompressedBuffer.get()); | 304 SkASSERT(fCompressedBuffer.get()); |
| 303 this->sendTextureData(texture, desc, fCompressedBuffer.get(), 0); | 305 this->sendTextureData(texture, desc, fCompressedBuffer.get(), 0); |
| 304 break; | 306 break; |
| 305 } | 307 } |
| 306 } | 308 } |
| 307 | 309 |
| 308 /** | 310 /** |
| 309 * Convert mask generation results to a signed distance field | 311 * Convert mask generation results to a signed distance field |
| 310 */ | 312 */ |
| 311 void GrSWMaskHelper::toSDF(unsigned char* sdf) { | 313 void GrSWMaskHelper::toSDF(unsigned char* sdf) { |
| 312 SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr()
, | 314 SkAutoLockPixels alp(fBM); |
| 313 fPixels.width(), fPixels.height(), fPixel
s.rowBytes()); | 315 |
| 316 SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fBM.getPixels(
), |
| 317 fBM.width(), fBM.height(), fBM.rowBytes()
); |
| 314 } | 318 } |
| 315 | 319 |
| 316 //////////////////////////////////////////////////////////////////////////////// | 320 //////////////////////////////////////////////////////////////////////////////// |
| 317 /** | 321 /** |
| 318 * Software rasterizes path to A8 mask (possibly using the context's matrix) | 322 * Software rasterizes path to A8 mask (possibly using the context's matrix) |
| 319 * and uploads the result to a scratch texture. Returns the resulting | 323 * and uploads the result to a scratch texture. Returns the resulting |
| 320 * texture on success; NULL on failure. | 324 * texture on success; NULL on failure. |
| 321 */ | 325 */ |
| 322 GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, | 326 GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, |
| 323 const SkPath& path, | 327 const SkPath& path, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop
)); | 372 maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop
)); |
| 369 | 373 |
| 370 pipelineBuilder->addCoverageProcessor( | 374 pipelineBuilder->addCoverageProcessor( |
| 371 GrSimpleTextureEffect::Create(texture, | 375 GrSimpleTextureEffect::Create(texture, |
| 372 maskMatrix, | 376 maskMatrix, |
| 373 GrTextureParams::kNone_Fi
lterMode, | 377 GrTextureParams::kNone_Fi
lterMode, |
| 374 kDevice_GrCoordSet))->unr
ef(); | 378 kDevice_GrCoordSet))->unr
ef(); |
| 375 | 379 |
| 376 target->drawBWRect(pipelineBuilder, color, SkMatrix::I(), dstRect, NULL, &in
vert); | 380 target->drawBWRect(pipelineBuilder, color, SkMatrix::I(), dstRect, NULL, &in
vert); |
| 377 } | 381 } |
| OLD | NEW |