reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "GrContext.h" |
| 19 | #include "GrTextContext.h" |
| 20 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | #include "SkGpuDevice.h" |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 22 | #include "SkGpuDeviceFactory.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "SkGrTexturePixelRef.h" |
| 24 | |
| 25 | #include "SkDrawProcs.h" |
| 26 | #include "SkGlyphCache.h" |
| 27 | |
| 28 | #define CACHE_LAYER_TEXTURES 1 |
| 29 | |
| 30 | #if 0 |
| 31 | extern bool (*gShouldDrawProc)(); |
| 32 | #define CHECK_SHOULD_DRAW(draw) \ |
| 33 | do { \ |
| 34 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
| 35 | this->prepareRenderTarget(draw); \ |
| 36 | } while (0) |
| 37 | #else |
| 38 | #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw) |
| 39 | #endif |
| 40 | |
| 41 | class SkAutoExtMatrix { |
| 42 | public: |
| 43 | SkAutoExtMatrix(const SkMatrix* extMatrix) { |
| 44 | if (extMatrix) { |
| 45 | SkGr::SkMatrix2GrMatrix(*extMatrix, &fMatrix); |
| 46 | fExtMatrix = &fMatrix; |
| 47 | } else { |
| 48 | fExtMatrix = NULL; |
| 49 | } |
| 50 | } |
| 51 | const GrMatrix* extMatrix() const { return fExtMatrix; } |
| 52 | |
| 53 | private: |
| 54 | GrMatrix fMatrix; |
| 55 | GrMatrix* fExtMatrix; // NULL or &fMatrix |
| 56 | }; |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | SkGpuDevice::SkAutoCachedTexture:: |
| 61 | SkAutoCachedTexture(SkGpuDevice* device, |
| 62 | const SkBitmap& bitmap, |
| 63 | const GrSamplerState& sampler, |
| 64 | GrTexture** texture) { |
| 65 | GrAssert(texture); |
| 66 | fTex = NULL; |
| 67 | *texture = this->set(device, bitmap, sampler); |
| 68 | } |
| 69 | |
| 70 | SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() { |
| 71 | fTex = NULL; |
| 72 | } |
| 73 | |
| 74 | GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device, |
| 75 | const SkBitmap& bitmap, |
| 76 | const GrSamplerState& sampler) { |
| 77 | if (fTex) { |
| 78 | fDevice->unlockCachedTexture(fTex); |
| 79 | } |
| 80 | fDevice = device; |
| 81 | GrTexture* texture = (GrTexture*)bitmap.getTexture(); |
| 82 | if (texture) { |
| 83 | // return the native texture |
| 84 | fTex = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | } else { |
| 86 | // look it up in our cache |
| 87 | fTex = device->lockCachedTexture(bitmap, sampler, &texture, false); |
| 88 | } |
| 89 | return texture; |
| 90 | } |
| 91 | |
| 92 | SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() { |
| 93 | if (fTex) { |
| 94 | fDevice->unlockCachedTexture(fTex); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | bool gDoTraceDraw; |
| 101 | |
| 102 | struct GrSkDrawProcs : public SkDrawProcs { |
| 103 | public: |
| 104 | GrContext* fContext; |
| 105 | GrTextContext* fTextContext; |
| 106 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 107 | }; |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 111 | GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() { |
| 112 | return (GrRenderTarget*) -1; |
| 113 | } |
| 114 | |
| 115 | SkGpuDevice::SkGpuDevice(GrContext* context, |
| 116 | const SkBitmap& bitmap, |
| 117 | GrRenderTarget* renderTargetOrNull) |
| 118 | : SkDevice(NULL, bitmap, (NULL == renderTargetOrNull)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | |
| 120 | fNeedPrepareRenderTarget = false; |
| 121 | fDrawProcs = NULL; |
| 122 | |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 123 | fContext = context; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 124 | fContext->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | |
| 126 | fCache = NULL; |
| 127 | fTexture = NULL; |
| 128 | fRenderTarget = NULL; |
| 129 | fNeedClear = false; |
| 130 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 131 | if (NULL == renderTargetOrNull) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | SkBitmap::Config c = bitmap.config(); |
| 133 | if (c != SkBitmap::kRGB_565_Config) { |
| 134 | c = SkBitmap::kARGB_8888_Config; |
| 135 | } |
| 136 | SkBitmap bm; |
| 137 | bm.setConfig(c, this->width(), this->height()); |
| 138 | |
| 139 | #if CACHE_LAYER_TEXTURES |
| 140 | |
| 141 | fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), |
| 142 | &fTexture, true); |
| 143 | if (fCache) { |
| 144 | SkASSERT(NULL != fTexture); |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 145 | SkASSERT(NULL != fTexture->asRenderTarget()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | } |
| 147 | #else |
| 148 | const GrGpu::TextureDesc desc = { |
| 149 | GrGpu::kRenderTarget_TextureFlag, |
| 150 | GrGpu::kNone_AALevel, |
| 151 | this->width(), |
| 152 | this->height(), |
| 153 | SkGr::Bitmap2PixelConfig(bm) |
| 154 | }; |
| 155 | |
| 156 | fTexture = fContext->createUncachedTexture(desc, NULL, 0); |
| 157 | #endif |
| 158 | if (NULL != fTexture) { |
| 159 | fRenderTarget = fTexture->asRenderTarget(); |
| 160 | |
| 161 | GrAssert(NULL != fRenderTarget); |
| 162 | |
| 163 | // we defer the actual clear until our gainFocus() |
| 164 | fNeedClear = true; |
| 165 | |
| 166 | // wrap the bitmap with a pixelref to expose our texture |
| 167 | SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture); |
| 168 | this->setPixelRef(pr, 0)->unref(); |
| 169 | } else { |
| 170 | GrPrintf("--- failed to create gpu-offscreen [%d %d]\n", |
| 171 | this->width(), this->height()); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 172 | GrAssert(false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 174 | } else if (Current3DApiRenderTarget() == renderTargetOrNull) { |
| 175 | fRenderTarget = fContext->createRenderTargetFrom3DApiState(); |
| 176 | } else { |
| 177 | fRenderTarget = renderTargetOrNull; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | fRenderTarget->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | SkGpuDevice::~SkGpuDevice() { |
| 183 | if (fDrawProcs) { |
| 184 | delete fDrawProcs; |
| 185 | } |
| 186 | |
| 187 | if (fCache) { |
| 188 | GrAssert(NULL != fTexture); |
| 189 | GrAssert(fRenderTarget == fTexture->asRenderTarget()); |
| 190 | // IMPORTANT: reattach the rendertarget/tex back to the cache. |
| 191 | fContext->reattachAndUnlockCachedTexture((GrTextureEntry*)fCache); |
| 192 | } else if (NULL != fTexture) { |
| 193 | GrAssert(!CACHE_LAYER_TEXTURES); |
| 194 | GrAssert(fRenderTarget == fTexture->asRenderTarget()); |
| 195 | fTexture->unref(); |
| 196 | } else if (NULL != fRenderTarget) { |
| 197 | fRenderTarget->unref(); |
| 198 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 199 | fContext->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | } |
| 201 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 202 | intptr_t SkGpuDevice::getLayerTextureHandle() const { |
| 203 | if (fTexture) { |
| 204 | return fTexture->getTextureHandle(); |
| 205 | } else { |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | /////////////////////////////////////////////////////////////////////////////// |
| 210 | |
| 211 | void SkGpuDevice::makeRenderTargetCurrent() { |
| 212 | fContext->setRenderTarget(fRenderTarget); |
| 213 | fContext->flush(true); |
| 214 | fNeedPrepareRenderTarget = true; |
| 215 | } |
| 216 | |
| 217 | /////////////////////////////////////////////////////////////////////////////// |
| 218 | |
| 219 | bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
| 220 | SkIRect bounds; |
| 221 | bounds.set(0, 0, this->width(), this->height()); |
| 222 | if (!bounds.intersect(srcRect)) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | const int w = bounds.width(); |
| 227 | const int h = bounds.height(); |
| 228 | SkBitmap tmp; |
| 229 | // note we explicitly specify our rowBytes to be snug (no gap between rows) |
| 230 | tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4); |
| 231 | if (!tmp.allocPixels()) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | SkAutoLockPixels alp(tmp); |
| 236 | fContext->setRenderTarget(fRenderTarget); |
| 237 | // we aren't setting the clip or matrix, so mark as dirty |
| 238 | // we don't need to set them for this call and don't have them anyway |
| 239 | fNeedPrepareRenderTarget = true; |
| 240 | |
| 241 | if (!fContext->readPixels(bounds.fLeft, bounds.fTop, |
| 242 | bounds.width(), bounds.height(), |
| 243 | GrTexture::kRGBA_8888_PixelConfig, |
| 244 | tmp.getPixels())) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | tmp.swap(*bitmap); |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) { |
| 253 | SkAutoLockPixels alp(bitmap); |
| 254 | if (!bitmap.readyToDraw()) { |
| 255 | return; |
| 256 | } |
| 257 | GrTexture::PixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(), |
| 258 | bitmap.isOpaque()); |
| 259 | fContext->setRenderTarget(fRenderTarget); |
| 260 | // we aren't setting the clip or matrix, so mark as dirty |
| 261 | // we don't need to set them for this call and don't have them anyway |
| 262 | fNeedPrepareRenderTarget = true; |
| 263 | |
| 264 | fContext->writePixels(x, y, bitmap.width(), bitmap.height(), |
| 265 | config, bitmap.getPixels(), bitmap.rowBytes()); |
| 266 | } |
| 267 | |
| 268 | /////////////////////////////////////////////////////////////////////////////// |
| 269 | |
| 270 | static void convert_matrixclip(GrContext* context, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 271 | const SkClipStack& clipStack, |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 272 | const SkRegion& clipRegion, |
| 273 | const SkIPoint& origin) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 274 | GrMatrix grmat; |
| 275 | SkGr::SkMatrix2GrMatrix(matrix, &grmat); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 276 | context->setMatrix(grmat); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 277 | |
| 278 | SkGrClipIterator iter; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 279 | iter.reset(clipStack); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 280 | const SkIRect& skBounds = clipRegion.getBounds(); |
| 281 | GrRect bounds; |
| 282 | bounds.setLTRB(GrIntToScalar(skBounds.fLeft), |
| 283 | GrIntToScalar(skBounds.fTop), |
| 284 | GrIntToScalar(skBounds.fRight), |
| 285 | GrIntToScalar(skBounds.fBottom)); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 286 | GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()), |
| 287 | &bounds); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 288 | context->setClip(grc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // call this ever each draw call, to ensure that the context reflects our state, |
| 292 | // and not the state from some other canvas/device |
| 293 | void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) { |
| 294 | if (fNeedPrepareRenderTarget || |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 295 | fContext->getRenderTarget() != fRenderTarget) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 296 | |
| 297 | fContext->setRenderTarget(fRenderTarget); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 298 | SkASSERT(draw.fClipStack); |
| 299 | convert_matrixclip(fContext, *draw.fMatrix, |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 300 | *draw.fClipStack, *draw.fClip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | fNeedPrepareRenderTarget = false; |
| 302 | } |
| 303 | } |
| 304 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 305 | void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 306 | const SkClipStack& clipStack) { |
| 307 | this->INHERITED::setMatrixClip(matrix, clip, clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 309 | convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 313 | const SkRegion& clip, const SkClipStack& clipStack) { |
| 314 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 315 | fContext->setRenderTarget(fRenderTarget); |
| 316 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 317 | this->INHERITED::gainFocus(canvas, matrix, clip, clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 318 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 319 | convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | |
| 321 | if (fNeedClear) { |
| 322 | fContext->eraseColor(0x0); |
| 323 | fNeedClear = false; |
| 324 | } |
| 325 | } |
| 326 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 327 | bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 328 | if (NULL != fTexture) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 329 | paint->setTexture(fTexture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | /////////////////////////////////////////////////////////////////////////////// |
| 336 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 337 | SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch); |
| 338 | SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch); |
| 339 | SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch); |
| 340 | SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch); |
| 341 | SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4, |
| 342 | shader_type_mismatch); |
| 343 | SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 344 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 345 | static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = { |
| 346 | (GrSamplerState::SampleMode) -1, // kNone_BitmapType |
| 347 | GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType |
| 348 | GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType |
| 349 | GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType |
| 350 | GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType |
| 351 | }; |
| 352 | |
| 353 | bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint, |
| 354 | bool justAlpha, |
| 355 | GrPaint* grPaint) { |
| 356 | |
| 357 | grPaint->fDither = skPaint.isDither(); |
| 358 | grPaint->fAntiAlias = skPaint.isAntiAlias(); |
| 359 | |
| 360 | SkXfermode::Coeff sm = SkXfermode::kOne_Coeff; |
| 361 | SkXfermode::Coeff dm = SkXfermode::kISA_Coeff; |
| 362 | |
| 363 | SkXfermode* mode = skPaint.getXfermode(); |
| 364 | if (mode) { |
| 365 | if (!mode->asCoeff(&sm, &dm)) { |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 366 | SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");) |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 367 | #if 0 |
| 368 | return false; |
| 369 | #endif |
| 370 | } |
| 371 | } |
| 372 | grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm); |
| 373 | grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm); |
| 374 | |
| 375 | if (justAlpha) { |
| 376 | uint8_t alpha = skPaint.getAlpha(); |
| 377 | grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha); |
| 378 | } else { |
| 379 | grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor()); |
| 380 | grPaint->setTexture(NULL); |
| 381 | } |
| 382 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 383 | } |
| 384 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 385 | bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint, |
| 386 | SkAutoCachedTexture* act, |
| 387 | const SkMatrix& ctm, |
| 388 | GrPaint* grPaint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 389 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 390 | SkASSERT(NULL != act); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 392 | SkShader* shader = skPaint.getShader(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 393 | if (NULL == shader) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 394 | return this->skPaint2GrPaintNoShader(skPaint, false, grPaint); |
| 395 | grPaint->setTexture(NULL); |
| 396 | return true; |
| 397 | } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint)) { |
| 398 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 399 | } |
| 400 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 401 | SkPaint noAlphaPaint(skPaint); |
| 402 | noAlphaPaint.setAlpha(255); |
| 403 | shader->setContext(this->accessBitmap(false), noAlphaPaint, ctm); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 404 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 405 | SkBitmap bitmap; |
| 406 | SkMatrix matrix; |
| 407 | SkShader::TileMode tileModes[2]; |
| 408 | SkScalar twoPointParams[3]; |
| 409 | SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix, |
| 410 | tileModes, twoPointParams); |
| 411 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 412 | GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype]; |
| 413 | if (-1 == sampleMode) { |
| 414 | SkDebugf("shader->asABitmap() == kNone_BitmapType\n"); |
| 415 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 416 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 417 | grPaint->fSampler.setSampleMode(sampleMode); |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 418 | grPaint->fSampler.setFilter(skPaint.isFilterBitmap()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 419 | grPaint->fSampler.setWrapX(sk_tile_mode_to_grwrap(tileModes[0])); |
| 420 | grPaint->fSampler.setWrapY(sk_tile_mode_to_grwrap(tileModes[1])); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | if (GrSamplerState::kRadial2_SampleMode == sampleMode) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 422 | grPaint->fSampler.setRadial2Params(twoPointParams[0], |
| 423 | twoPointParams[1], |
| 424 | twoPointParams[2] < 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 425 | } |
| 426 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 427 | GrTexture* texture = act->set(this, bitmap, grPaint->fSampler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | if (NULL == texture) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 429 | SkDebugf("Couldn't convert bitmap to texture.\n"); |
| 430 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 431 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 432 | grPaint->setTexture(texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 433 | |
| 434 | // since our texture coords will be in local space, we wack the texture |
| 435 | // matrix to map them back into 0...1 before we load it |
| 436 | SkMatrix localM; |
| 437 | if (shader->getLocalMatrix(&localM)) { |
| 438 | SkMatrix inverse; |
| 439 | if (localM.invert(&inverse)) { |
| 440 | matrix.preConcat(inverse); |
| 441 | } |
| 442 | } |
| 443 | if (SkShader::kDefault_BitmapType == bmptype) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 444 | GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width()); |
| 445 | GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 446 | matrix.postScale(sx, sy); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 447 | } else if (SkShader::kRadial_BitmapType == bmptype) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 448 | GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | matrix.postScale(s, s); |
| 450 | } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 451 | GrMatrix grMat; |
| 452 | SkGr::SkMatrix2GrMatrix(matrix, &grMat); |
| 453 | grPaint->fSampler.setMatrix(grMat); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 454 | |
| 455 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 459 | |
| 460 | class SkPositionSource { |
| 461 | public: |
| 462 | SkPositionSource(const SkPoint* points, int count) |
| 463 | : fPoints(points), fCount(count) {} |
| 464 | |
| 465 | int count() const { return fCount; } |
| 466 | |
| 467 | void writeValue(int i, GrPoint* dstPosition) const { |
| 468 | SkASSERT(i < fCount); |
| 469 | dstPosition->fX = SkScalarToGrScalar(fPoints[i].fX); |
| 470 | dstPosition->fY = SkScalarToGrScalar(fPoints[i].fY); |
| 471 | } |
| 472 | private: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 473 | const SkPoint* fPoints; |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 474 | int fCount; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 475 | }; |
| 476 | |
| 477 | class SkTexCoordSource { |
| 478 | public: |
| 479 | SkTexCoordSource(const SkPoint* coords) |
| 480 | : fCoords(coords) {} |
| 481 | |
| 482 | void writeValue(int i, GrPoint* dstCoord) const { |
| 483 | dstCoord->fX = SkScalarToGrScalar(fCoords[i].fX); |
| 484 | dstCoord->fY = SkScalarToGrScalar(fCoords[i].fY); |
| 485 | } |
| 486 | private: |
| 487 | const SkPoint* fCoords; |
| 488 | }; |
| 489 | |
| 490 | class SkColorSource { |
| 491 | public: |
| 492 | SkColorSource(const SkColor* colors) : fColors(colors) {} |
| 493 | |
| 494 | void writeValue(int i, GrColor* dstColor) const { |
| 495 | *dstColor = SkGr::SkColor2GrColor(fColors[i]); |
| 496 | } |
| 497 | private: |
| 498 | const SkColor* fColors; |
| 499 | }; |
| 500 | |
| 501 | class SkIndexSource { |
| 502 | public: |
| 503 | SkIndexSource(const uint16_t* indices, int count) |
| 504 | : fIndices(indices), fCount(count) { |
| 505 | } |
| 506 | |
| 507 | int count() const { return fCount; } |
| 508 | |
| 509 | void writeValue(int i, uint16_t* dstIndex) const { |
| 510 | *dstIndex = fIndices[i]; |
| 511 | } |
| 512 | |
| 513 | private: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 514 | const uint16_t* fIndices; |
bsalomon@google.com | 1962832 | 2011-02-03 21:30:17 +0000 | [diff] [blame] | 515 | int fCount; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | /////////////////////////////////////////////////////////////////////////////// |
| 519 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 520 | #if 0 // not currently being used so don't compile, |
| 521 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 522 | // can be used for positions or texture coordinates |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 523 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 524 | class SkRectFanSource { |
| 525 | public: |
| 526 | SkRectFanSource(const SkRect& rect) : fRect(rect) {} |
| 527 | |
| 528 | int count() const { return 4; } |
| 529 | |
| 530 | void writeValue(int i, GrPoint* dstPoint) const { |
| 531 | SkASSERT(i < 4); |
| 532 | dstPoint->fX = SkScalarToGrScalar((i % 3) ? fRect.fRight : |
| 533 | fRect.fLeft); |
| 534 | dstPoint->fY = SkScalarToGrScalar((i < 2) ? fRect.fTop : |
| 535 | fRect.fBottom); |
| 536 | } |
| 537 | private: |
| 538 | const SkRect& fRect; |
| 539 | }; |
| 540 | |
| 541 | class SkIRectFanSource { |
| 542 | public: |
| 543 | SkIRectFanSource(const SkIRect& rect) : fRect(rect) {} |
| 544 | |
| 545 | int count() const { return 4; } |
| 546 | |
| 547 | void writeValue(int i, GrPoint* dstPoint) const { |
| 548 | SkASSERT(i < 4); |
| 549 | dstPoint->fX = (i % 3) ? GrIntToScalar(fRect.fRight) : |
| 550 | GrIntToScalar(fRect.fLeft); |
| 551 | dstPoint->fY = (i < 2) ? GrIntToScalar(fRect.fTop) : |
| 552 | GrIntToScalar(fRect.fBottom); |
| 553 | } |
| 554 | private: |
| 555 | const SkIRect& fRect; |
| 556 | }; |
| 557 | |
| 558 | class SkMatRectFanSource { |
| 559 | public: |
| 560 | SkMatRectFanSource(const SkRect& rect, const SkMatrix& matrix) |
| 561 | : fRect(rect), fMatrix(matrix) {} |
| 562 | |
| 563 | int count() const { return 4; } |
| 564 | |
| 565 | void writeValue(int i, GrPoint* dstPoint) const { |
| 566 | SkASSERT(i < 4); |
| 567 | |
| 568 | #if SK_SCALAR_IS_GR_SCALAR |
| 569 | fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft, |
| 570 | (i < 2) ? fRect.fTop : fRect.fBottom, |
| 571 | (SkPoint*)dstPoint); |
| 572 | #else |
| 573 | SkPoint dst; |
| 574 | fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft, |
| 575 | (i < 2) ? fRect.fTop : fRect.fBottom, |
| 576 | &dst); |
| 577 | dstPoint->fX = SkScalarToGrScalar(dst.fX); |
| 578 | dstPoint->fY = SkScalarToGrScalar(dst.fY); |
| 579 | #endif |
| 580 | } |
| 581 | private: |
| 582 | const SkRect& fRect; |
| 583 | const SkMatrix& fMatrix; |
| 584 | }; |
| 585 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 586 | #endif |
| 587 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 588 | /////////////////////////////////////////////////////////////////////////////// |
| 589 | |
| 590 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 591 | CHECK_SHOULD_DRAW(draw); |
| 592 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 593 | GrPaint grPaint; |
| 594 | SkAutoCachedTexture act; |
| 595 | if (!this->skPaint2GrPaintShader(paint, &act, *draw.fMatrix, &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 596 | return; |
| 597 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 598 | |
| 599 | fContext->drawPaint(grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // must be in SkCanvas::PointMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 603 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 604 | kPoints_PrimitiveType, |
| 605 | kLines_PrimitiveType, |
| 606 | kLineStrip_PrimitiveType |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 610 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 611 | CHECK_SHOULD_DRAW(draw); |
| 612 | |
| 613 | SkScalar width = paint.getStrokeWidth(); |
| 614 | if (width < 0) { |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | // we only handle hairlines here, else we let the SkDraw call our drawPath() |
| 619 | if (width > 0) { |
| 620 | draw.drawPoints(mode, count, pts, paint, true); |
| 621 | return; |
| 622 | } |
| 623 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 624 | GrPaint grPaint; |
| 625 | SkAutoCachedTexture act; |
| 626 | if (!this->skPaint2GrPaintShader(paint, &act, *draw.fMatrix, &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 630 | #if SK_SCALAR_IS_GR_SCALAR |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 631 | fContext->drawVertices(grPaint, |
| 632 | gPointMode2PrimtiveType[mode], |
| 633 | count, |
| 634 | (GrPoint*)pts, |
| 635 | NULL, |
| 636 | NULL, |
| 637 | NULL, |
| 638 | 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 639 | #else |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 640 | fContext->drawCustomVertices(grPaint, |
| 641 | gPointMode2PrimtiveType[mode], |
| 642 | SkPositionSource(pts, count)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 643 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 647 | const SkPaint& paint) { |
| 648 | CHECK_SHOULD_DRAW(draw); |
| 649 | |
| 650 | bool doStroke = paint.getStyle() == SkPaint::kStroke_Style; |
| 651 | SkScalar width = paint.getStrokeWidth(); |
| 652 | |
| 653 | /* |
| 654 | We have special code for hairline strokes, miter-strokes, and fills. |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 655 | Anything else we just call our path code. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 656 | */ |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 657 | bool usePath = doStroke && width > 0 && |
| 658 | paint.getStrokeJoin() != SkPaint::kMiter_Join; |
| 659 | // another reason we might need to call drawPath... |
| 660 | if (paint.getMaskFilter()) { |
| 661 | usePath = true; |
| 662 | } |
| 663 | |
| 664 | if (usePath) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 665 | SkPath path; |
| 666 | path.addRect(rect); |
| 667 | this->drawPath(draw, path, paint, NULL, true); |
| 668 | return; |
| 669 | } |
| 670 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 671 | GrPaint grPaint; |
| 672 | SkAutoCachedTexture act; |
| 673 | if (!this->skPaint2GrPaintShader(paint, &act, *draw.fMatrix, &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 674 | return; |
| 675 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 676 | fContext->drawRect(grPaint, Sk2Gr(rect), doStroke ? width : -1); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 677 | } |
| 678 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 679 | #include "SkMaskFilter.h" |
| 680 | #include "SkBounder.h" |
| 681 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 682 | static bool drawWithMaskFilter(GrContext* context, const SkPath& path, |
| 683 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 684 | const SkRegion& clip, SkBounder* bounder, |
| 685 | GrPaint* grp) { |
| 686 | SkMask srcM, dstM; |
| 687 | |
| 688 | if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM, |
| 689 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | SkAutoMaskImage autoSrc(&srcM, false); |
| 694 | |
| 695 | if (!filter->filterMask(&dstM, srcM, matrix, NULL)) { |
| 696 | return false; |
| 697 | } |
| 698 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 699 | SkAutoMaskImage autoDst(&dstM, false); |
| 700 | |
| 701 | if (clip.quickReject(dstM.fBounds)) { |
| 702 | return false; |
| 703 | } |
| 704 | if (bounder && !bounder->doIRect(dstM.fBounds)) { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 709 | // the current clip (and identity matrix) and grpaint settings |
| 710 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 711 | GrAutoMatrix avm(context, GrMatrix::I()); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 712 | |
| 713 | const GrGpu::TextureDesc desc = { |
| 714 | 0, |
| 715 | GrGpu::kNone_AALevel, |
| 716 | dstM.fBounds.width(), |
| 717 | dstM.fBounds.height(), |
| 718 | GrTexture::kAlpha_8_PixelConfig |
| 719 | }; |
| 720 | |
| 721 | GrTexture* texture = context->createUncachedTexture(desc, dstM.fImage, |
| 722 | dstM.fRowBytes); |
| 723 | if (NULL == texture) { |
| 724 | return false; |
| 725 | } |
| 726 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 727 | grp->setTexture(texture); |
| 728 | texture->unref(); |
| 729 | grp->fSampler.setClampNoFilter(); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 730 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 731 | GrRect d; |
| 732 | d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft), |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 733 | GrIntToScalar(dstM.fBounds.fTop), |
| 734 | GrIntToScalar(dstM.fBounds.fRight), |
| 735 | GrIntToScalar(dstM.fBounds.fBottom)); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 736 | GrRect s; |
| 737 | s.setLTRB(0, 0, GR_Scalar1, GR_Scalar1); |
| 738 | context->drawRectToRect(*grp, d, s); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 739 | return true; |
| 740 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 741 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 742 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 743 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 744 | bool pathIsMutable) { |
| 745 | CHECK_SHOULD_DRAW(draw); |
| 746 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 747 | GrPaint grPaint; |
| 748 | SkAutoCachedTexture act; |
| 749 | if (!this->skPaint2GrPaintShader(paint, &act, *draw.fMatrix, &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 753 | // BEGIN lift from SkDraw::drawPath() |
| 754 | |
| 755 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| 756 | bool doFill = true; |
| 757 | SkPath tmpPath; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 758 | |
| 759 | if (prePathMatrix) { |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 760 | SkPath* result = pathPtr; |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 761 | |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 762 | if (!pathIsMutable) { |
| 763 | result = &tmpPath; |
| 764 | pathIsMutable = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 765 | } |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 766 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 767 | // of applying it here? See SkDraw.cpp |
| 768 | pathPtr->transform(*prePathMatrix, result); |
| 769 | pathPtr = result; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 770 | } |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 771 | // at this point we're done with prePathMatrix |
| 772 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 773 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 774 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
| 775 | doFill = paint.getFillPath(*pathPtr, &tmpPath); |
| 776 | pathPtr = &tmpPath; |
| 777 | } |
| 778 | |
| 779 | // END lift from SkDraw::drawPath() |
| 780 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 781 | if (paint.getMaskFilter()) { |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 782 | // avoid possibly allocating a new path in transform if we can |
| 783 | SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; |
| 784 | |
| 785 | // transform the path into device space |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 786 | pathPtr->transform(*draw.fMatrix, devPathPtr); |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 787 | |
| 788 | drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(), |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 789 | *draw.fMatrix, *draw.fClip, draw.fBounder, &grPaint); |
| 790 | return; |
| 791 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 792 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 793 | GrPathFill fill = kHairLine_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 794 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 795 | if (doFill) { |
| 796 | switch (pathPtr->getFillType()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 797 | case SkPath::kWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 798 | fill = kWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 799 | break; |
| 800 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 801 | fill = kEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 802 | break; |
| 803 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 804 | fill = kInverseWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 805 | break; |
| 806 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 807 | fill = kInverseEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 808 | break; |
| 809 | default: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 810 | SkDebugf("Unsupported path fill type\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 811 | return; |
| 812 | } |
| 813 | } |
| 814 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 815 | SkGrPathIter iter(*pathPtr); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 816 | fContext->drawPath(grPaint, &iter, fill); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 817 | } |
| 818 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 819 | void SkGpuDevice::drawBitmap(const SkDraw& draw, |
| 820 | const SkBitmap& bitmap, |
| 821 | const SkIRect* srcRectPtr, |
| 822 | const SkMatrix& m, |
| 823 | const SkPaint& paint) { |
| 824 | CHECK_SHOULD_DRAW(draw); |
| 825 | |
| 826 | SkIRect srcRect; |
| 827 | if (NULL == srcRectPtr) { |
| 828 | srcRect.set(0, 0, bitmap.width(), bitmap.height()); |
| 829 | } else { |
| 830 | srcRect = *srcRectPtr; |
| 831 | } |
| 832 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 833 | GrPaint grPaint; |
| 834 | if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint)) { |
| 835 | return; |
| 836 | } |
| 837 | grPaint.fSampler.setFilter(paint.isFilterBitmap()); |
| 838 | |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 839 | const int maxTextureDim = fContext->getMaxTextureDimension(); |
| 840 | if (bitmap.getTexture() || (bitmap.width() <= maxTextureDim && |
| 841 | bitmap.height() <= maxTextureDim)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 842 | // take the fast case |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 843 | this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | |
| 847 | // undo the translate done by SkCanvas |
| 848 | int DX = SkMax32(0, srcRect.fLeft); |
| 849 | int DY = SkMax32(0, srcRect.fTop); |
| 850 | // compute clip bounds in local coordinates |
| 851 | SkIRect clipRect; |
| 852 | { |
| 853 | SkRect r; |
| 854 | r.set(draw.fClip->getBounds()); |
| 855 | SkMatrix matrix, inverse; |
| 856 | matrix.setConcat(*draw.fMatrix, m); |
| 857 | if (!matrix.invert(&inverse)) { |
| 858 | return; |
| 859 | } |
| 860 | inverse.mapRect(&r); |
| 861 | r.roundOut(&clipRect); |
| 862 | // apply the canvas' translate to our local clip |
| 863 | clipRect.offset(DX, DY); |
| 864 | } |
| 865 | |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 866 | int nx = bitmap.width() / maxTextureDim; |
| 867 | int ny = bitmap.height() / maxTextureDim; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 868 | for (int x = 0; x <= nx; x++) { |
| 869 | for (int y = 0; y <= ny; y++) { |
| 870 | SkIRect tileR; |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 871 | tileR.set(x * maxTextureDim, y * maxTextureDim, |
| 872 | (x + 1) * maxTextureDim, (y + 1) * maxTextureDim); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 873 | if (!SkIRect::Intersects(tileR, clipRect)) { |
| 874 | continue; |
| 875 | } |
| 876 | |
| 877 | SkIRect srcR = tileR; |
| 878 | if (!srcR.intersect(srcRect)) { |
| 879 | continue; |
| 880 | } |
| 881 | |
| 882 | SkBitmap tmpB; |
| 883 | if (bitmap.extractSubset(&tmpB, tileR)) { |
| 884 | // now offset it to make it "local" to our tmp bitmap |
| 885 | srcR.offset(-tileR.fLeft, -tileR.fTop); |
| 886 | |
| 887 | SkMatrix tmpM(m); |
| 888 | { |
| 889 | int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft); |
| 890 | int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop); |
| 891 | tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy)); |
| 892 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 893 | this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * This is called by drawBitmap(), which has to handle images that may be too |
| 901 | * large to be represented by a single texture. |
| 902 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 903 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 904 | * and that non-texture portion of the GrPaint has already been setup. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 905 | */ |
| 906 | void SkGpuDevice::internalDrawBitmap(const SkDraw& draw, |
| 907 | const SkBitmap& bitmap, |
| 908 | const SkIRect& srcRect, |
| 909 | const SkMatrix& m, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 910 | GrPaint* grPaint) { |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 911 | SkASSERT(bitmap.width() <= fContext->getMaxTextureDimension() && |
| 912 | bitmap.height() <= fContext->getMaxTextureDimension()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 913 | |
| 914 | SkAutoLockPixels alp(bitmap); |
| 915 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 916 | return; |
| 917 | } |
| 918 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 919 | grPaint->fSampler.setWrapX(GrSamplerState::kClamp_WrapMode); |
| 920 | grPaint->fSampler.setWrapY(GrSamplerState::kClamp_WrapMode); |
| 921 | grPaint->fSampler.setSampleMode(GrSamplerState::kNormal_SampleMode); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 922 | grPaint->fSampler.setMatrix(GrMatrix::I()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 923 | |
| 924 | GrTexture* texture; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 925 | SkAutoCachedTexture act(this, bitmap, grPaint->fSampler, &texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 926 | if (NULL == texture) { |
| 927 | return; |
| 928 | } |
| 929 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 930 | grPaint->setTexture(texture); |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 931 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 932 | GrRect dstRect(0, 0, GrIntToScalar(srcRect.width()), GrIntToScalar(srcRect.height())); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 933 | GrRect paintRect; |
| 934 | paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()), |
| 935 | GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()), |
| 936 | GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()), |
| 937 | GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 938 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 939 | GrMatrix grMat; |
| 940 | SkGr::SkMatrix2GrMatrix(m, &grMat); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 941 | |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 942 | fContext->drawRectToRect(*grPaint, dstRect, paintRect, &grMat); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 946 | int left, int top, const SkPaint& paint) { |
| 947 | CHECK_SHOULD_DRAW(draw); |
| 948 | |
| 949 | SkAutoLockPixels alp(bitmap); |
| 950 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 951 | return; |
| 952 | } |
| 953 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 954 | GrPaint grPaint; |
| 955 | if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint)) { |
| 956 | return; |
| 957 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 958 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 959 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 960 | |
| 961 | GrTexture* texture; |
| 962 | grPaint.fSampler.setClampNoFilter(); |
| 963 | SkAutoCachedTexture act(this, bitmap, grPaint.fSampler, &texture); |
| 964 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 965 | grPaint.setTexture(texture); |
| 966 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 967 | fContext->drawRectToRect(grPaint, |
| 968 | GrRect(GrIntToScalar(left), GrIntToScalar(top), |
| 969 | GrIntToScalar(left + bitmap.width()), |
| 970 | GrIntToScalar(top + bitmap.height())), |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 971 | GrRect(0, 0, GR_Scalar1, GR_Scalar1)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev, |
| 975 | int x, int y, const SkPaint& paint) { |
| 976 | CHECK_SHOULD_DRAW(draw); |
| 977 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 978 | GrPaint grPaint; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 979 | if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) || |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 980 | !this->skPaint2GrPaintNoShader(paint, true, &grPaint)) { |
| 981 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 982 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 983 | |
| 984 | SkASSERT(NULL != grPaint.getTexture()); |
| 985 | |
| 986 | const SkBitmap& bm = dev->accessBitmap(false); |
| 987 | int w = bm.width(); |
| 988 | int h = bm.height(); |
| 989 | |
| 990 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 991 | |
| 992 | grPaint.fSampler.setClampNoFilter(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 993 | |
| 994 | fContext->drawRectToRect(grPaint, |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 995 | GrRect(GrIntToScalar(x), |
| 996 | GrIntToScalar(y), |
| 997 | GrIntToScalar(x + w), |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 998 | GrIntToScalar(y + h)), |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 999 | GrRect(0, 0, GR_Scalar1, GR_Scalar1)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /////////////////////////////////////////////////////////////////////////////// |
| 1003 | |
| 1004 | // must be in SkCanvas::VertexMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1005 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1006 | kTriangles_PrimitiveType, |
| 1007 | kTriangleStrip_PrimitiveType, |
| 1008 | kTriangleFan_PrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1009 | }; |
| 1010 | |
| 1011 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1012 | int vertexCount, const SkPoint vertices[], |
| 1013 | const SkPoint texs[], const SkColor colors[], |
| 1014 | SkXfermode* xmode, |
| 1015 | const uint16_t indices[], int indexCount, |
| 1016 | const SkPaint& paint) { |
| 1017 | CHECK_SHOULD_DRAW(draw); |
| 1018 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1019 | GrPaint grPaint; |
| 1020 | SkAutoCachedTexture act; |
| 1021 | // we ignore the shader if texs is null. |
| 1022 | if (NULL == texs) { |
| 1023 | if (!this->skPaint2GrPaintNoShader(paint, false, &grPaint)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1024 | return; |
| 1025 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1026 | } else { |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 1027 | if (!this->skPaint2GrPaintShader(paint, &act, |
| 1028 | *draw.fMatrix, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1029 | &grPaint)) { |
| 1030 | return; |
| 1031 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1032 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (NULL != xmode && NULL != texs && NULL != colors) { |
| 1035 | SkXfermode::Mode mode; |
| 1036 | if (!SkXfermode::IsMode(xmode, &mode) || |
| 1037 | SkXfermode::kMultiply_Mode != mode) { |
| 1038 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
| 1039 | #if 0 |
| 1040 | return |
| 1041 | #endif |
| 1042 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1043 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1044 | |
| 1045 | #if SK_SCALAR_IS_GR_SCALAR |
| 1046 | // even if GrColor and SkColor byte offsets match we need |
| 1047 | // to perform pre-multiply. |
| 1048 | if (NULL == colors) { |
| 1049 | fContext->drawVertices(grPaint, |
| 1050 | gVertexMode2PrimitiveType[vmode], |
| 1051 | vertexCount, |
| 1052 | (GrPoint*) vertices, |
| 1053 | (GrPoint*) texs, |
| 1054 | NULL, |
| 1055 | indices, |
| 1056 | indexCount); |
| 1057 | } else |
| 1058 | #endif |
| 1059 | { |
| 1060 | SkTexCoordSource texSrc(texs); |
| 1061 | SkColorSource colSrc(colors); |
| 1062 | SkIndexSource idxSrc(indices, indexCount); |
| 1063 | |
| 1064 | fContext->drawCustomVertices(grPaint, |
| 1065 | gVertexMode2PrimitiveType[vmode], |
| 1066 | SkPositionSource(vertices, vertexCount), |
| 1067 | (NULL == texs) ? NULL : &texSrc, |
| 1068 | (NULL == colors) ? NULL : &colSrc, |
| 1069 | (NULL == indices) ? NULL : &idxSrc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /////////////////////////////////////////////////////////////////////////////// |
| 1074 | |
| 1075 | static void GlyphCacheAuxProc(void* data) { |
| 1076 | delete (GrFontScaler*)data; |
| 1077 | } |
| 1078 | |
| 1079 | static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) { |
| 1080 | void* auxData; |
| 1081 | GrFontScaler* scaler = NULL; |
| 1082 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 1083 | scaler = (GrFontScaler*)auxData; |
| 1084 | } |
| 1085 | if (NULL == scaler) { |
| 1086 | scaler = new SkGrFontScaler(cache); |
| 1087 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 1088 | } |
| 1089 | return scaler; |
| 1090 | } |
| 1091 | |
| 1092 | static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state, |
| 1093 | SkFixed fx, SkFixed fy, |
| 1094 | const SkGlyph& glyph) { |
| 1095 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1096 | |
| 1097 | GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs; |
| 1098 | |
| 1099 | if (NULL == procs->fFontScaler) { |
| 1100 | procs->fFontScaler = get_gr_font_scaler(state.fCache); |
| 1101 | } |
| 1102 | procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0), |
| 1103 | SkIntToFixed(SkFixedFloor(fx)), fy, |
| 1104 | procs->fFontScaler); |
| 1105 | } |
| 1106 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1107 | SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1108 | |
| 1109 | // deferred allocation |
| 1110 | if (NULL == fDrawProcs) { |
| 1111 | fDrawProcs = new GrSkDrawProcs; |
| 1112 | fDrawProcs->fD1GProc = SkGPU_Draw1Glyph; |
| 1113 | fDrawProcs->fContext = fContext; |
| 1114 | } |
| 1115 | |
| 1116 | // init our (and GL's) state |
| 1117 | fDrawProcs->fTextContext = context; |
| 1118 | fDrawProcs->fFontScaler = NULL; |
| 1119 | return fDrawProcs; |
| 1120 | } |
| 1121 | |
| 1122 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1123 | size_t byteLength, SkScalar x, SkScalar y, |
| 1124 | const SkPaint& paint) { |
| 1125 | CHECK_SHOULD_DRAW(draw); |
| 1126 | |
| 1127 | if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) { |
| 1128 | // this guy will just call our drawPath() |
| 1129 | draw.drawText((const char*)text, byteLength, x, y, paint); |
| 1130 | } else { |
| 1131 | SkAutoExtMatrix aem(draw.fExtMatrix); |
| 1132 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1133 | |
| 1134 | GrPaint grPaint; |
| 1135 | SkAutoCachedTexture act; |
| 1136 | |
| 1137 | if (!this->skPaint2GrPaintShader(paint, &act, *draw.fMatrix, &grPaint)) { |
| 1138 | return; |
| 1139 | } |
| 1140 | GrTextContext context(fContext, grPaint, aem.extMatrix()); |
| 1141 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1142 | this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1147 | size_t byteLength, const SkScalar pos[], |
| 1148 | SkScalar constY, int scalarsPerPos, |
| 1149 | const SkPaint& paint) { |
| 1150 | CHECK_SHOULD_DRAW(draw); |
| 1151 | |
| 1152 | if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) { |
| 1153 | // this guy will just call our drawPath() |
| 1154 | draw.drawPosText((const char*)text, byteLength, pos, constY, |
| 1155 | scalarsPerPos, paint); |
| 1156 | } else { |
| 1157 | SkAutoExtMatrix aem(draw.fExtMatrix); |
| 1158 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1159 | |
| 1160 | GrPaint grPaint; |
| 1161 | SkAutoCachedTexture act; |
| 1162 | if (!this->skPaint2GrPaintShader(paint, &act, *draw.fMatrix, &grPaint)) { |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | GrTextContext context(fContext, grPaint, aem.extMatrix()); |
| 1167 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1168 | this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY, |
| 1169 | scalarsPerPos, paint); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1174 | size_t len, const SkPath& path, |
| 1175 | const SkMatrix* m, const SkPaint& paint) { |
| 1176 | CHECK_SHOULD_DRAW(draw); |
| 1177 | |
| 1178 | SkASSERT(draw.fDevice == this); |
| 1179 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1180 | } |
| 1181 | |
| 1182 | /////////////////////////////////////////////////////////////////////////////// |
| 1183 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame^] | 1184 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1185 | if (!paint.isLCDRenderText()) { |
| 1186 | // we're cool with the paint as is |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | if (paint.getShader() || |
| 1191 | paint.getXfermode() || // unless its srcover |
| 1192 | paint.getMaskFilter() || |
| 1193 | paint.getRasterizer() || |
| 1194 | paint.getColorFilter() || |
| 1195 | paint.getPathEffect() || |
| 1196 | paint.isFakeBoldText() || |
| 1197 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1198 | // turn off lcd |
| 1199 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1200 | flags->fHinting = paint.getHinting(); |
| 1201 | return true; |
| 1202 | } |
| 1203 | // we're cool with the paint as is |
| 1204 | return false; |
| 1205 | } |
| 1206 | |
| 1207 | /////////////////////////////////////////////////////////////////////////////// |
| 1208 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1209 | SkGpuDevice::TexCache* SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap, |
| 1210 | const GrSamplerState& sampler, |
| 1211 | GrTexture** texture, |
| 1212 | bool forDeviceRenderTarget) { |
| 1213 | GrContext* ctx = this->context(); |
| 1214 | uint32_t p0, p1; |
| 1215 | if (forDeviceRenderTarget) { |
| 1216 | p0 = p1 = -1; |
| 1217 | } else { |
| 1218 | p0 = bitmap.getGenerationID(); |
| 1219 | p1 = bitmap.pixelRefOffset(); |
| 1220 | } |
| 1221 | |
| 1222 | GrTexture* newTexture = NULL; |
| 1223 | GrTextureKey key(p0, p1, bitmap.width(), bitmap.height()); |
| 1224 | GrTextureEntry* entry = ctx->findAndLockTexture(&key, sampler); |
| 1225 | |
| 1226 | if (NULL == entry) { |
| 1227 | |
| 1228 | if (forDeviceRenderTarget) { |
| 1229 | const GrGpu::TextureDesc desc = { |
| 1230 | GrGpu::kRenderTarget_TextureFlag, |
| 1231 | GrGpu::kNone_AALevel, |
| 1232 | bitmap.width(), |
| 1233 | bitmap.height(), |
| 1234 | SkGr::Bitmap2PixelConfig(bitmap) |
| 1235 | }; |
| 1236 | entry = ctx->createAndLockTexture(&key, sampler, desc, NULL, 0); |
| 1237 | |
| 1238 | } else { |
| 1239 | entry = sk_gr_create_bitmap_texture(ctx, &key, sampler, bitmap); |
| 1240 | } |
| 1241 | if (NULL == entry) { |
| 1242 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 1243 | bitmap.width(), bitmap.height()); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (NULL != entry) { |
| 1248 | newTexture = entry->texture(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1249 | if (texture) { |
| 1250 | *texture = newTexture; |
| 1251 | } |
| 1252 | // IMPORTANT: We can't allow another SkGpuDevice to get this |
| 1253 | // cache entry until this one is destroyed! |
| 1254 | if (forDeviceRenderTarget) { |
| 1255 | ctx->detachCachedTexture(entry); |
| 1256 | } |
| 1257 | } |
| 1258 | return (TexCache*)entry; |
| 1259 | } |
| 1260 | |
| 1261 | void SkGpuDevice::unlockCachedTexture(TexCache* cache) { |
| 1262 | this->context()->unlockTexture((GrTextureEntry*)cache); |
| 1263 | } |
| 1264 | |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 1265 | /////////////////////////////////////////////////////////////////////////////// |
| 1266 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1267 | SkGpuDeviceFactory::SkGpuDeviceFactory(GrContext* context, |
| 1268 | GrRenderTarget* rootRenderTarget) |
| 1269 | : fContext(context) { |
| 1270 | |
| 1271 | GrAssert(NULL != context); |
| 1272 | GrAssert(NULL != rootRenderTarget); |
| 1273 | |
| 1274 | // check this now rather than passing this value to SkGpuDevice cons. |
| 1275 | // we want the rt that is bound *now* in the 3D API, not the one |
| 1276 | // at the time of newDevice. |
| 1277 | if (SkGpuDevice::Current3DApiRenderTarget() == rootRenderTarget) { |
| 1278 | fRootRenderTarget = context->createRenderTargetFrom3DApiState(); |
| 1279 | } else { |
| 1280 | fRootRenderTarget = rootRenderTarget; |
| 1281 | rootRenderTarget->ref(); |
| 1282 | } |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 1283 | context->ref(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1284 | |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | SkGpuDeviceFactory::~SkGpuDeviceFactory() { |
| 1288 | fContext->unref(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1289 | fRootRenderTarget->unref(); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | SkDevice* SkGpuDeviceFactory::newDevice(SkCanvas*, SkBitmap::Config config, |
| 1293 | int width, int height, |
| 1294 | bool isOpaque, bool isLayer) { |
| 1295 | SkBitmap bm; |
| 1296 | bm.setConfig(config, width, height); |
| 1297 | bm.setIsOpaque(isOpaque); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1298 | return new SkGpuDevice(fContext, bm, isLayer ? NULL : fRootRenderTarget); |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 1299 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1300 | |