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