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