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