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