epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrContext.h" |
| 12 | #include "GrTextContext.h" |
| 13 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "SkGpuDevice.h" |
| 15 | #include "SkGrTexturePixelRef.h" |
| 16 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 17 | #include "SkColorFilter.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | #include "SkDrawProcs.h" |
| 19 | #include "SkGlyphCache.h" |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 20 | #include "SkTLazy.h" |
reed@google.com | c9aa587 | 2011-04-05 21:05:37 +0000 | [diff] [blame] | 21 | #include "SkUtils.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | |
| 23 | #define CACHE_LAYER_TEXTURES 1 |
| 24 | |
| 25 | #if 0 |
| 26 | extern bool (*gShouldDrawProc)(); |
| 27 | #define CHECK_SHOULD_DRAW(draw) \ |
| 28 | do { \ |
| 29 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
| 30 | this->prepareRenderTarget(draw); \ |
| 31 | } while (0) |
| 32 | #else |
| 33 | #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw) |
| 34 | #endif |
| 35 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 36 | // we use the same texture slot on GrPaint for bitmaps and shaders |
| 37 | // (since drawBitmap, drawSprite, and drawDevice ignore skia's shader) |
| 38 | enum { |
| 39 | kBitmapTextureIdx = 0, |
| 40 | kShaderTextureIdx = 0 |
| 41 | }; |
| 42 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 43 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 44 | #define MAX_BLUR_SIGMA 4.0f |
| 45 | // FIXME: This value comes from from SkBlurMaskFilter.cpp. |
| 46 | // Should probably be put in a common header someplace. |
| 47 | #define MAX_BLUR_RADIUS SkIntToScalar(128) |
| 48 | // This constant approximates the scaling done in the software path's |
| 49 | // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). |
| 50 | // IMHO, it actually should be 1: we blur "less" than we should do |
| 51 | // according to the CSS and canvas specs, simply because Safari does the same. |
| 52 | // Firefox used to do the same too, until 4.0 where they fixed it. So at some |
| 53 | // point we should probably get rid of these scaling constants and rebaseline |
| 54 | // all the blur tests. |
| 55 | #define BLUR_SIGMA_SCALE 0.6f |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
| 58 | SkGpuDevice::SkAutoCachedTexture:: |
| 59 | SkAutoCachedTexture(SkGpuDevice* device, |
| 60 | const SkBitmap& bitmap, |
| 61 | const GrSamplerState& sampler, |
| 62 | GrTexture** texture) { |
| 63 | GrAssert(texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | *texture = this->set(device, bitmap, sampler); |
| 65 | } |
| 66 | |
| 67 | SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device, |
| 71 | const SkBitmap& bitmap, |
| 72 | const GrSamplerState& sampler) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 73 | if (fTex.texture()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | fDevice->unlockCachedTexture(fTex); |
| 75 | } |
| 76 | fDevice = device; |
| 77 | GrTexture* texture = (GrTexture*)bitmap.getTexture(); |
| 78 | if (texture) { |
| 79 | // return the native texture |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 80 | fTex.reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | } else { |
| 82 | // look it up in our cache |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 83 | fTex = device->lockCachedTexture(bitmap, sampler); |
| 84 | texture = fTex.texture(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | } |
| 86 | return texture; |
| 87 | } |
| 88 | |
| 89 | SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 90 | if (fTex.texture()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | fDevice->unlockCachedTexture(fTex); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | bool gDoTraceDraw; |
| 98 | |
| 99 | struct GrSkDrawProcs : public SkDrawProcs { |
| 100 | public: |
| 101 | GrContext* fContext; |
| 102 | GrTextContext* fTextContext; |
| 103 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 104 | }; |
| 105 | |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 108 | static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) { |
| 109 | switch (config) { |
| 110 | case kAlpha_8_GrPixelConfig: |
| 111 | *isOpaque = false; |
| 112 | return SkBitmap::kA8_Config; |
| 113 | case kRGB_565_GrPixelConfig: |
| 114 | *isOpaque = true; |
| 115 | return SkBitmap::kRGB_565_Config; |
| 116 | case kRGBA_4444_GrPixelConfig: |
| 117 | *isOpaque = false; |
| 118 | return SkBitmap::kARGB_4444_Config; |
| 119 | case kRGBA_8888_GrPixelConfig: |
| 120 | case kRGBX_8888_GrPixelConfig: |
| 121 | *isOpaque = (kRGBX_8888_GrPixelConfig == config); |
| 122 | return SkBitmap::kARGB_8888_Config; |
| 123 | default: |
| 124 | *isOpaque = false; |
| 125 | return SkBitmap::kNo_Config; |
| 126 | } |
| 127 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 129 | static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) { |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 130 | GrPixelConfig config = renderTarget->config(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 131 | |
| 132 | bool isOpaque; |
| 133 | SkBitmap bitmap; |
| 134 | bitmap.setConfig(grConfig2skConfig(config, &isOpaque), |
| 135 | renderTarget->width(), renderTarget->height()); |
| 136 | bitmap.setIsOpaque(isOpaque); |
| 137 | return bitmap; |
| 138 | } |
| 139 | |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 140 | SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture) |
| 141 | : SkDevice(make_bitmap(context, texture->asRenderTarget())) { |
| 142 | this->initFromRenderTarget(context, texture->asRenderTarget()); |
| 143 | } |
| 144 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 145 | SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget) |
| 146 | : SkDevice(make_bitmap(context, renderTarget)) { |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 147 | this->initFromRenderTarget(context, renderTarget); |
| 148 | } |
| 149 | |
| 150 | void SkGpuDevice::initFromRenderTarget(GrContext* context, |
| 151 | GrRenderTarget* renderTarget) { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 152 | fNeedPrepareRenderTarget = false; |
| 153 | fDrawProcs = NULL; |
| 154 | |
| 155 | fContext = context; |
| 156 | fContext->ref(); |
| 157 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 158 | fTexture = NULL; |
| 159 | fRenderTarget = NULL; |
| 160 | fNeedClear = false; |
| 161 | |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 162 | GrAssert(NULL != renderTarget); |
| 163 | fRenderTarget = renderTarget; |
| 164 | fRenderTarget->ref(); |
| 165 | // if this RT is also a texture, hold a ref on it |
| 166 | fTexture = fRenderTarget->asTexture(); |
| 167 | SkSafeRef(fTexture); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 168 | |
| 169 | SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget); |
| 170 | this->setPixelRef(pr, 0)->unref(); |
| 171 | } |
| 172 | |
| 173 | SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 174 | int height, Usage usage) |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 175 | : SkDevice(config, width, height, false /*isOpaque*/) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | fNeedPrepareRenderTarget = false; |
| 177 | fDrawProcs = NULL; |
| 178 | |
reed@google.com | 7b201d2 | 2011-01-11 18:59:23 +0000 | [diff] [blame] | 179 | fContext = context; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 180 | fContext->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | fTexture = NULL; |
| 183 | fRenderTarget = NULL; |
| 184 | fNeedClear = false; |
| 185 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 186 | if (config != SkBitmap::kRGB_565_Config) { |
| 187 | config = SkBitmap::kARGB_8888_Config; |
| 188 | } |
| 189 | SkBitmap bm; |
| 190 | bm.setConfig(config, width, height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 191 | |
| 192 | #if CACHE_LAYER_TEXTURES |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 193 | TexType type = (kSaveLayer_Usage == usage) ? |
| 194 | kSaveLayerDeviceRenderTarget_TexType : |
| 195 | kDeviceRenderTarget_TexType; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 196 | fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type); |
| 197 | fTexture = fCache.texture(); |
| 198 | if (fTexture) { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 199 | SkASSERT(NULL != fTexture->asRenderTarget()); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 200 | // hold a ref directly on fTexture (even though fCache has one) to match |
| 201 | // other constructor paths. Simplifies cleanup. |
| 202 | fTexture->ref(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 203 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | #else |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 205 | const GrTextureDesc desc = { |
| 206 | kRenderTarget_GrTextureFlagBit, |
| 207 | kNone_GrAALevel, |
| 208 | width, |
| 209 | height, |
| 210 | SkGr::Bitmap2PixelConfig(bm) |
| 211 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 213 | fTexture = fContext->createUncachedTexture(desc, NULL, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | #endif |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 215 | if (NULL != fTexture) { |
| 216 | fRenderTarget = fTexture->asRenderTarget(); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 217 | fRenderTarget->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 219 | GrAssert(NULL != fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 220 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 221 | // we defer the actual clear until our gainFocus() |
| 222 | fNeedClear = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 224 | // wrap the bitmap with a pixelref to expose our texture |
| 225 | SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 226 | this->setPixelRef(pr, 0)->unref(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 227 | } else { |
| 228 | GrPrintf("--- failed to create gpu-offscreen [%d %d]\n", |
| 229 | width, height); |
| 230 | GrAssert(false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | SkGpuDevice::~SkGpuDevice() { |
| 235 | if (fDrawProcs) { |
| 236 | delete fDrawProcs; |
| 237 | } |
| 238 | |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 239 | SkSafeUnref(fTexture); |
| 240 | SkSafeUnref(fRenderTarget); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 241 | if (fCache.texture()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | GrAssert(NULL != fTexture); |
| 243 | GrAssert(fRenderTarget == fTexture->asRenderTarget()); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 244 | fContext->unlockTexture(fCache); |
bsalomon@google.com | f9046fe | 2011-06-17 15:10:21 +0000 | [diff] [blame] | 245 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 246 | fContext->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | } |
| 248 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | /////////////////////////////////////////////////////////////////////////////// |
| 250 | |
| 251 | void SkGpuDevice::makeRenderTargetCurrent() { |
| 252 | fContext->setRenderTarget(fRenderTarget); |
| 253 | fContext->flush(true); |
| 254 | fNeedPrepareRenderTarget = true; |
| 255 | } |
| 256 | |
| 257 | /////////////////////////////////////////////////////////////////////////////// |
| 258 | |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame^] | 259 | bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap, int x, int y) { |
| 260 | SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); |
| 261 | SkASSERT(!bitmap.isNull()); |
| 262 | SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()))); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 263 | |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame^] | 264 | SkAutoLockPixels alp(bitmap); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 265 | return fContext->readRenderTargetPixels(fRenderTarget, |
| 266 | x, y, |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame^] | 267 | bitmap.width(), |
| 268 | bitmap.height(), |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 269 | kRGBA_8888_GrPixelConfig, |
bsalomon@google.com | 910267d | 2011-11-02 20:06:25 +0000 | [diff] [blame^] | 270 | bitmap.getPixels(), |
| 271 | bitmap.rowBytes()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) { |
| 275 | SkAutoLockPixels alp(bitmap); |
| 276 | if (!bitmap.readyToDraw()) { |
| 277 | return; |
| 278 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 279 | GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(), |
| 280 | bitmap.isOpaque()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 281 | fContext->setRenderTarget(fRenderTarget); |
| 282 | // we aren't setting the clip or matrix, so mark as dirty |
| 283 | // we don't need to set them for this call and don't have them anyway |
| 284 | fNeedPrepareRenderTarget = true; |
| 285 | |
| 286 | fContext->writePixels(x, y, bitmap.width(), bitmap.height(), |
| 287 | config, bitmap.getPixels(), bitmap.rowBytes()); |
| 288 | } |
| 289 | |
| 290 | /////////////////////////////////////////////////////////////////////////////// |
| 291 | |
| 292 | static void convert_matrixclip(GrContext* context, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 293 | const SkClipStack& clipStack, |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 294 | const SkRegion& clipRegion, |
| 295 | const SkIPoint& origin) { |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 296 | context->setMatrix(matrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | |
| 298 | SkGrClipIterator iter; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 299 | iter.reset(clipStack); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 300 | const SkIRect& skBounds = clipRegion.getBounds(); |
| 301 | GrRect bounds; |
| 302 | bounds.setLTRB(GrIntToScalar(skBounds.fLeft), |
| 303 | GrIntToScalar(skBounds.fTop), |
| 304 | GrIntToScalar(skBounds.fRight), |
| 305 | GrIntToScalar(skBounds.fBottom)); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 306 | GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()), |
| 307 | &bounds); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 308 | context->setClip(grc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // call this ever each draw call, to ensure that the context reflects our state, |
| 312 | // and not the state from some other canvas/device |
| 313 | void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) { |
| 314 | if (fNeedPrepareRenderTarget || |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 315 | fContext->getRenderTarget() != fRenderTarget) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 316 | |
| 317 | fContext->setRenderTarget(fRenderTarget); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 318 | SkASSERT(draw.fClipStack); |
| 319 | convert_matrixclip(fContext, *draw.fMatrix, |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 320 | *draw.fClipStack, *draw.fClip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 321 | fNeedPrepareRenderTarget = false; |
| 322 | } |
| 323 | } |
| 324 | |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 325 | void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip, |
| 326 | const SkClipStack& clipStack) { |
| 327 | this->INHERITED::setMatrixClip(matrix, clip, clipStack); |
| 328 | // We don't need to set them now because the context may not reflect this device. |
bsalomon@google.com | a7bf6e2 | 2011-04-11 19:20:46 +0000 | [diff] [blame] | 329 | fNeedPrepareRenderTarget = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 333 | const SkRegion& clip, const SkClipStack& clipStack) { |
| 334 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 335 | fContext->setRenderTarget(fRenderTarget); |
| 336 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 337 | this->INHERITED::gainFocus(canvas, matrix, clip, clipStack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 338 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 339 | convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | |
| 341 | if (fNeedClear) { |
bsalomon@google.com | 31a5840 | 2011-04-27 21:00:02 +0000 | [diff] [blame] | 342 | fContext->clear(NULL, 0x0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 343 | fNeedClear = false; |
| 344 | } |
| 345 | } |
| 346 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 347 | bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 348 | if (NULL != fTexture) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 349 | paint->setTexture(kBitmapTextureIdx, fTexture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | /////////////////////////////////////////////////////////////////////////////// |
| 356 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 357 | SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch); |
| 358 | SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch); |
| 359 | SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch); |
| 360 | SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch); |
| 361 | SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4, |
| 362 | shader_type_mismatch); |
| 363 | SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 364 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 365 | static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = { |
| 366 | (GrSamplerState::SampleMode) -1, // kNone_BitmapType |
| 367 | GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType |
| 368 | GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType |
| 369 | GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType |
| 370 | GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType |
| 371 | }; |
| 372 | |
| 373 | bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint, |
| 374 | bool justAlpha, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 375 | GrPaint* grPaint, |
| 376 | bool constantColor) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 377 | |
| 378 | grPaint->fDither = skPaint.isDither(); |
| 379 | grPaint->fAntiAlias = skPaint.isAntiAlias(); |
| 380 | |
| 381 | SkXfermode::Coeff sm = SkXfermode::kOne_Coeff; |
| 382 | SkXfermode::Coeff dm = SkXfermode::kISA_Coeff; |
| 383 | |
| 384 | SkXfermode* mode = skPaint.getXfermode(); |
| 385 | if (mode) { |
| 386 | if (!mode->asCoeff(&sm, &dm)) { |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 387 | SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");) |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 388 | #if 0 |
| 389 | return false; |
| 390 | #endif |
| 391 | } |
| 392 | } |
| 393 | grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm); |
| 394 | grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm); |
| 395 | |
| 396 | if (justAlpha) { |
| 397 | uint8_t alpha = skPaint.getAlpha(); |
| 398 | grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha); |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 399 | // justAlpha is currently set to true only if there is a texture, |
| 400 | // so constantColor should not also be true. |
| 401 | GrAssert(!constantColor); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 402 | } else { |
| 403 | grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 404 | grPaint->setTexture(kShaderTextureIdx, NULL); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 405 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 406 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 407 | SkColor color; |
| 408 | SkXfermode::Mode filterMode; |
| 409 | if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) { |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 410 | if (!constantColor) { |
| 411 | grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color); |
| 412 | grPaint->fColorFilterXfermode = filterMode; |
| 413 | return true; |
| 414 | } |
| 415 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 416 | grPaint->fColor = SkGr::SkColor2GrColor(filtered); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 417 | } |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 418 | grPaint->resetColorFilter(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 419 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 420 | } |
| 421 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 422 | bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint, |
| 423 | SkAutoCachedTexture* act, |
| 424 | const SkMatrix& ctm, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 425 | GrPaint* grPaint, |
| 426 | bool constantColor) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 427 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 428 | SkASSERT(NULL != act); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 429 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 430 | SkShader* shader = skPaint.getShader(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 431 | if (NULL == shader) { |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 432 | return this->skPaint2GrPaintNoShader(skPaint, |
| 433 | false, |
| 434 | grPaint, |
| 435 | constantColor); |
| 436 | } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 437 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 438 | } |
| 439 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 440 | SkBitmap bitmap; |
| 441 | SkMatrix matrix; |
| 442 | SkShader::TileMode tileModes[2]; |
| 443 | SkScalar twoPointParams[3]; |
| 444 | SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix, |
| 445 | tileModes, twoPointParams); |
| 446 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 447 | GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype]; |
| 448 | if (-1 == sampleMode) { |
reed@google.com | 2be9e8b | 2011-07-06 21:18:09 +0000 | [diff] [blame] | 449 | SkShader::GradientInfo info; |
| 450 | SkColor color; |
| 451 | |
| 452 | info.fColors = &color; |
| 453 | info.fColorOffsets = NULL; |
| 454 | info.fColorCount = 1; |
| 455 | if (SkShader::kColor_GradientType == shader->asAGradient(&info)) { |
| 456 | SkPaint copy(skPaint); |
| 457 | copy.setShader(NULL); |
bsalomon@google.com | cd9cfd7 | 2011-07-08 16:55:04 +0000 | [diff] [blame] | 458 | // modulate the paint alpha by the shader's solid color alpha |
| 459 | U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); |
| 460 | copy.setColor(SkColorSetA(color, newA)); |
reed@google.com | 2be9e8b | 2011-07-06 21:18:09 +0000 | [diff] [blame] | 461 | return this->skPaint2GrPaintNoShader(copy, |
| 462 | false, |
| 463 | grPaint, |
| 464 | constantColor); |
| 465 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 466 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 467 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 468 | GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx); |
| 469 | sampler->setSampleMode(sampleMode); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 470 | if (skPaint.isFilterBitmap()) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 471 | sampler->setFilter(GrSamplerState::kBilinear_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 472 | } else { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 473 | sampler->setFilter(GrSamplerState::kNearest_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 474 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 475 | sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0])); |
| 476 | sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1])); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 477 | if (GrSamplerState::kRadial2_SampleMode == sampleMode) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 478 | sampler->setRadial2Params(twoPointParams[0], |
| 479 | twoPointParams[1], |
| 480 | twoPointParams[2] < 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 481 | } |
| 482 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 483 | GrTexture* texture = act->set(this, bitmap, *sampler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | if (NULL == texture) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 485 | SkDebugf("Couldn't convert bitmap to texture.\n"); |
| 486 | return false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 487 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 488 | grPaint->setTexture(kShaderTextureIdx, texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 489 | |
| 490 | // since our texture coords will be in local space, we wack the texture |
| 491 | // matrix to map them back into 0...1 before we load it |
| 492 | SkMatrix localM; |
| 493 | if (shader->getLocalMatrix(&localM)) { |
| 494 | SkMatrix inverse; |
| 495 | if (localM.invert(&inverse)) { |
| 496 | matrix.preConcat(inverse); |
| 497 | } |
| 498 | } |
| 499 | if (SkShader::kDefault_BitmapType == bmptype) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 500 | GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width()); |
| 501 | GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 502 | matrix.postScale(sx, sy); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 503 | } else if (SkShader::kRadial_BitmapType == bmptype) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 504 | GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 505 | matrix.postScale(s, s); |
| 506 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 507 | sampler->setMatrix(matrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 508 | |
| 509 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 513 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 514 | void SkGpuDevice::clear(SkColor color) { |
bsalomon@google.com | 31a5840 | 2011-04-27 21:00:02 +0000 | [diff] [blame] | 515 | fContext->clear(NULL, color); |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 516 | } |
| 517 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 518 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 519 | CHECK_SHOULD_DRAW(draw); |
| 520 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 521 | GrPaint grPaint; |
| 522 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 523 | if (!this->skPaint2GrPaintShader(paint, |
| 524 | &act, |
| 525 | *draw.fMatrix, |
| 526 | &grPaint, |
| 527 | true)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 528 | return; |
| 529 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 530 | |
| 531 | fContext->drawPaint(grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // must be in SkCanvas::PointMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 535 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 536 | kPoints_PrimitiveType, |
| 537 | kLines_PrimitiveType, |
| 538 | kLineStrip_PrimitiveType |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 542 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 543 | CHECK_SHOULD_DRAW(draw); |
| 544 | |
| 545 | SkScalar width = paint.getStrokeWidth(); |
| 546 | if (width < 0) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | // we only handle hairlines here, else we let the SkDraw call our drawPath() |
| 551 | if (width > 0) { |
| 552 | draw.drawPoints(mode, count, pts, paint, true); |
| 553 | return; |
| 554 | } |
| 555 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 556 | GrPaint grPaint; |
| 557 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 558 | if (!this->skPaint2GrPaintShader(paint, |
| 559 | &act, |
| 560 | *draw.fMatrix, |
| 561 | &grPaint, |
| 562 | true)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 566 | fContext->drawVertices(grPaint, |
| 567 | gPointMode2PrimtiveType[mode], |
| 568 | count, |
| 569 | (GrPoint*)pts, |
| 570 | NULL, |
| 571 | NULL, |
| 572 | NULL, |
| 573 | 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 574 | } |
| 575 | |
reed@google.com | c9aa587 | 2011-04-05 21:05:37 +0000 | [diff] [blame] | 576 | /////////////////////////////////////////////////////////////////////////////// |
| 577 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 578 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 579 | const SkPaint& paint) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 580 | CHECK_SHOULD_DRAW(draw); |
| 581 | |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 582 | bool doStroke = paint.getStyle() != SkPaint::kFill_Style; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 583 | SkScalar width = paint.getStrokeWidth(); |
| 584 | |
| 585 | /* |
| 586 | We have special code for hairline strokes, miter-strokes, and fills. |
| 587 | Anything else we just call our path code. |
| 588 | */ |
| 589 | bool usePath = doStroke && width > 0 && |
| 590 | paint.getStrokeJoin() != SkPaint::kMiter_Join; |
| 591 | // another reason we might need to call drawPath... |
| 592 | if (paint.getMaskFilter()) { |
| 593 | usePath = true; |
| 594 | } |
reed@google.com | 67db664 | 2011-05-26 11:46:35 +0000 | [diff] [blame] | 595 | // until we aa rotated rects... |
| 596 | if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) { |
| 597 | usePath = true; |
| 598 | } |
bungeman@google.com | 633722e | 2011-08-09 18:32:51 +0000 | [diff] [blame] | 599 | // small miter limit means right angles show bevel... |
| 600 | if (SkPaint::kMiter_Join == paint.getStrokeJoin() && |
| 601 | paint.getStrokeMiter() < SK_ScalarSqrt2) |
| 602 | { |
| 603 | usePath = true; |
| 604 | } |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 605 | // until we can both stroke and fill rectangles |
bungeman@google.com | 79bd877 | 2011-07-18 15:34:08 +0000 | [diff] [blame] | 606 | if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 607 | usePath = true; |
| 608 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 609 | |
| 610 | if (usePath) { |
| 611 | SkPath path; |
| 612 | path.addRect(rect); |
| 613 | this->drawPath(draw, path, paint, NULL, true); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | GrPaint grPaint; |
| 618 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 619 | if (!this->skPaint2GrPaintShader(paint, |
| 620 | &act, |
| 621 | *draw.fMatrix, |
| 622 | &grPaint, |
| 623 | true)) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 624 | return; |
| 625 | } |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 626 | fContext->drawRect(grPaint, rect, doStroke ? width : -1); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 627 | } |
| 628 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 629 | #include "SkMaskFilter.h" |
| 630 | #include "SkBounder.h" |
| 631 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 632 | static GrPathFill skToGrFillType(SkPath::FillType fillType) { |
| 633 | switch (fillType) { |
| 634 | case SkPath::kWinding_FillType: |
| 635 | return kWinding_PathFill; |
| 636 | case SkPath::kEvenOdd_FillType: |
| 637 | return kEvenOdd_PathFill; |
| 638 | case SkPath::kInverseWinding_FillType: |
| 639 | return kInverseWinding_PathFill; |
| 640 | case SkPath::kInverseEvenOdd_FillType: |
| 641 | return kInverseEvenOdd_PathFill; |
| 642 | default: |
| 643 | SkDebugf("Unsupported path fill type\n"); |
| 644 | return kHairLine_PathFill; |
| 645 | } |
| 646 | } |
| 647 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 648 | static void buildKernel(float sigma, float* kernel, int kernelWidth) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 649 | int halfWidth = (kernelWidth - 1) / 2; |
| 650 | float sum = 0.0f; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 651 | float denom = 1.0f / (2.0f * sigma * sigma); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 652 | for (int i = 0; i < kernelWidth; ++i) { |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 653 | float x = static_cast<float>(i - halfWidth); |
| 654 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 655 | // is dropped here, since we renormalize the kernel below. |
| 656 | kernel[i] = sk_float_exp(- x * x * denom); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 657 | sum += kernel[i]; |
| 658 | } |
| 659 | // Normalize the kernel |
| 660 | float scale = 1.0f / sum; |
| 661 | for (int i = 0; i < kernelWidth; ++i) |
| 662 | kernel[i] *= scale; |
| 663 | } |
| 664 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 665 | static void scaleRect(SkRect* rect, float scale) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 666 | rect->fLeft *= scale; |
| 667 | rect->fTop *= scale; |
| 668 | rect->fRight *= scale; |
| 669 | rect->fBottom *= scale; |
| 670 | } |
| 671 | |
| 672 | static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path, |
| 673 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 674 | const SkRegion& clip, SkBounder* bounder, |
| 675 | GrPaint* grp) { |
senorblanco@chromium.org | a479fc7 | 2011-07-19 16:40:58 +0000 | [diff] [blame] | 676 | #ifdef SK_DISABLE_GPU_BLUR |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 677 | return false; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 678 | #endif |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 679 | SkMaskFilter::BlurInfo info; |
| 680 | SkMaskFilter::BlurType blurType = filter->asABlur(&info); |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 681 | if (SkMaskFilter::kNone_BlurType == blurType || |
| 682 | !context->supportsShaders()) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 683 | return false; |
| 684 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 685 | SkScalar radius = info.fIgnoreTransform ? info.fRadius |
| 686 | : matrix.mapRadius(info.fRadius); |
| 687 | radius = SkMinScalar(radius, MAX_BLUR_RADIUS); |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 688 | if (radius <= 0) { |
| 689 | return false; |
| 690 | } |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 691 | float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 692 | SkRect srcRect = path.getBounds(); |
| 693 | |
| 694 | int scaleFactor = 1; |
| 695 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 696 | while (sigma > MAX_BLUR_SIGMA) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 697 | scaleFactor *= 2; |
| 698 | sigma *= 0.5f; |
| 699 | } |
senorblanco@chromium.org | 4a947d2 | 2011-07-18 21:48:35 +0000 | [diff] [blame] | 700 | int halfWidth = static_cast<int>(ceilf(sigma * 3.0f)); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 701 | int kernelWidth = halfWidth * 2 + 1; |
| 702 | |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 703 | float invScale = 1.0f / scaleFactor; |
| 704 | scaleRect(&srcRect, invScale); |
| 705 | srcRect.roundOut(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 706 | srcRect.inset(-halfWidth, -halfWidth); |
| 707 | |
senorblanco@chromium.org | 68c4d12 | 2011-08-01 21:20:31 +0000 | [diff] [blame] | 708 | SkRect clipBounds; |
| 709 | clipBounds.set(clip.getBounds()); |
| 710 | scaleRect(&clipBounds, invScale); |
| 711 | clipBounds.roundOut(); |
| 712 | clipBounds.inset(-halfWidth, -halfWidth); |
| 713 | |
| 714 | srcRect.intersect(clipBounds); |
| 715 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 716 | scaleRect(&srcRect, scaleFactor); |
| 717 | SkRect finalRect = srcRect; |
| 718 | |
| 719 | SkIRect finalIRect; |
| 720 | finalRect.roundOut(&finalIRect); |
| 721 | if (clip.quickReject(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 722 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 723 | } |
| 724 | if (bounder && !bounder->doIRect(finalIRect)) { |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 725 | return true; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 726 | } |
| 727 | GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop); |
| 728 | srcRect.offset(-srcRect.fLeft, -srcRect.fTop); |
| 729 | const GrTextureDesc desc = { |
| 730 | kRenderTarget_GrTextureFlagBit, |
| 731 | kNone_GrAALevel, |
| 732 | srcRect.width(), |
| 733 | srcRect.height(), |
| 734 | // We actually only need A8, but it often isn't supported as a |
| 735 | // render target |
| 736 | kRGBA_8888_GrPixelConfig |
| 737 | }; |
| 738 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 739 | GrAutoScratchTexture srcEntry(context, desc); |
| 740 | GrAutoScratchTexture dstEntry(context, desc); |
| 741 | if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) { |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 742 | return false; |
| 743 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 744 | GrTexture* srcTexture = srcEntry.texture(); |
| 745 | GrTexture* dstTexture = dstEntry.texture(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 746 | if (NULL == srcTexture || NULL == dstTexture) { |
| 747 | return false; |
| 748 | } |
| 749 | GrRenderTarget* oldRenderTarget = context->getRenderTarget(); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 750 | // Once this code moves into GrContext, this should be changed to use |
| 751 | // an AutoClipRestore. |
| 752 | GrClip oldClip = context->getClip(); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 753 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 754 | context->setClip(srcRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 755 | context->clear(NULL, 0); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 756 | GrPaint tempPaint; |
| 757 | tempPaint.reset(); |
| 758 | |
| 759 | GrAutoMatrix avm(context, GrMatrix::I()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 760 | tempPaint.fAntiAlias = grp->fAntiAlias; |
| 761 | if (tempPaint.fAntiAlias) { |
| 762 | // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst |
| 763 | // blend coeff of zero requires dual source blending support in order |
| 764 | // to properly blend partially covered pixels. This means the AA |
| 765 | // code path may not be taken. So we use a dst blend coeff of ISA. We |
| 766 | // could special case AA draws to a dst surface with known alpha=0 to |
| 767 | // use a zero dst coeff when dual source blending isn't available. |
| 768 | tempPaint.fSrcBlendCoeff = kOne_BlendCoeff; |
| 769 | tempPaint.fDstBlendCoeff = kISC_BlendCoeff; |
| 770 | } |
| 771 | // Draw hard shadow to dstTexture with path topleft at origin 0,0. |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 772 | context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 773 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 774 | |
| 775 | GrMatrix sampleM; |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 776 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 777 | GrPaint paint; |
| 778 | paint.reset(); |
| 779 | paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
| 780 | paint.getTextureSampler(0)->setMatrix(sampleM); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 781 | GrAutoScratchTexture origEntry; |
| 782 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 783 | if (blurType != SkMaskFilter::kNormal_BlurType) { |
| 784 | // Stash away a copy of the unblurred image. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 785 | origEntry.set(context, desc); |
| 786 | if (NULL == origEntry.texture()) { |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 787 | return false; |
| 788 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 789 | context->setRenderTarget(origEntry.texture()->asRenderTarget()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 790 | paint.setTexture(0, srcTexture); |
| 791 | context->drawRect(paint, srcRect); |
| 792 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 793 | for (int i = 1; i < scaleFactor; i *= 2) { |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 794 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
| 795 | paint.getTextureSampler(0)->setMatrix(sampleM); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 796 | context->setRenderTarget(dstTexture->asRenderTarget()); |
| 797 | SkRect dstRect(srcRect); |
| 798 | scaleRect(&dstRect, 0.5f); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 799 | paint.setTexture(0, srcTexture); |
| 800 | context->drawRectToRect(paint, dstRect, srcRect); |
| 801 | srcRect = dstRect; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 802 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | SkAutoTMalloc<float> kernelStorage(kernelWidth); |
| 806 | float* kernel = kernelStorage.get(); |
| 807 | buildKernel(sigma, kernel, kernelWidth); |
| 808 | |
senorblanco@chromium.org | 2ce9a04 | 2011-07-22 15:31:14 +0000 | [diff] [blame] | 809 | // Clear out a halfWidth to the right of the srcRect to prevent the |
| 810 | // X convolution from reading garbage. |
| 811 | SkIRect clearRect = SkIRect::MakeXYWH( |
| 812 | srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height()); |
| 813 | context->clear(&clearRect, 0x0); |
| 814 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 815 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 816 | context->convolveInX(srcTexture, srcRect, kernel, kernelWidth); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 817 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 818 | |
senorblanco@chromium.org | 2ce9a04 | 2011-07-22 15:31:14 +0000 | [diff] [blame] | 819 | // Clear out a halfWidth below the srcRect to prevent the Y |
| 820 | // convolution from reading garbage. |
| 821 | clearRect = SkIRect::MakeXYWH( |
| 822 | srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth); |
| 823 | context->clear(&clearRect, 0x0); |
| 824 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 825 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 826 | context->convolveInY(srcTexture, srcRect, kernel, kernelWidth); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 827 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 828 | |
senorblanco@chromium.org | 2ce9a04 | 2011-07-22 15:31:14 +0000 | [diff] [blame] | 829 | // Clear one pixel to the right and below, to accommodate bilinear |
| 830 | // upsampling. |
| 831 | clearRect = SkIRect::MakeXYWH( |
| 832 | srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); |
| 833 | context->clear(&clearRect, 0x0); |
| 834 | clearRect = SkIRect::MakeXYWH( |
| 835 | srcRect.fRight, srcRect.fTop, 1, srcRect.height()); |
| 836 | context->clear(&clearRect, 0x0); |
| 837 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 838 | if (scaleFactor > 1) { |
| 839 | // FIXME: This should be mitchell, not bilinear. |
| 840 | paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter); |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 841 | sampleM.setIDiv(srcTexture->width(), srcTexture->height()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 842 | paint.getTextureSampler(0)->setMatrix(sampleM); |
| 843 | context->setRenderTarget(dstTexture->asRenderTarget()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 844 | paint.setTexture(0, srcTexture); |
| 845 | SkRect dstRect(srcRect); |
| 846 | scaleRect(&dstRect, scaleFactor); |
| 847 | context->drawRectToRect(paint, dstRect, srcRect); |
| 848 | srcRect = dstRect; |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 849 | SkTSwap(srcTexture, dstTexture); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 850 | } |
| 851 | |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 852 | if (blurType != SkMaskFilter::kNormal_BlurType) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 853 | GrTexture* origTexture = origEntry.texture(); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 854 | paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter); |
senorblanco@chromium.org | 422b67d | 2011-07-19 21:22:13 +0000 | [diff] [blame] | 855 | sampleM.setIDiv(origTexture->width(), origTexture->height()); |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 856 | paint.getTextureSampler(0)->setMatrix(sampleM); |
| 857 | // Blend origTexture over srcTexture. |
| 858 | context->setRenderTarget(srcTexture->asRenderTarget()); |
| 859 | paint.setTexture(0, origTexture); |
| 860 | if (SkMaskFilter::kInner_BlurType == blurType) { |
| 861 | // inner: dst = dst * src |
| 862 | paint.fSrcBlendCoeff = kDC_BlendCoeff; |
| 863 | paint.fDstBlendCoeff = kZero_BlendCoeff; |
| 864 | } else if (SkMaskFilter::kSolid_BlurType == blurType) { |
| 865 | // solid: dst = src + dst - src * dst |
| 866 | // = (1 - dst) * src + 1 * dst |
| 867 | paint.fSrcBlendCoeff = kIDC_BlendCoeff; |
| 868 | paint.fDstBlendCoeff = kOne_BlendCoeff; |
| 869 | } else if (SkMaskFilter::kOuter_BlurType == blurType) { |
| 870 | // outer: dst = dst * (1 - src) |
| 871 | // = 0 * src + (1 - src) * dst |
| 872 | paint.fSrcBlendCoeff = kZero_BlendCoeff; |
| 873 | paint.fDstBlendCoeff = kISC_BlendCoeff; |
| 874 | } |
| 875 | context->drawRect(paint, srcRect); |
| 876 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 877 | context->setRenderTarget(oldRenderTarget); |
senorblanco@chromium.org | 42dd0f9 | 2011-07-14 15:29:57 +0000 | [diff] [blame] | 878 | context->setClip(oldClip); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 879 | |
| 880 | if (grp->hasTextureOrMask()) { |
| 881 | GrMatrix inverse; |
| 882 | if (!matrix.invert(&inverse)) { |
| 883 | return false; |
| 884 | } |
| 885 | grp->preConcatActiveSamplerMatrices(inverse); |
| 886 | } |
| 887 | |
| 888 | static const int MASK_IDX = GrPaint::kMaxMasks - 1; |
| 889 | // we assume the last mask index is available for use |
| 890 | GrAssert(NULL == grp->getMask(MASK_IDX)); |
| 891 | grp->setMask(MASK_IDX, srcTexture); |
| 892 | grp->getMaskSampler(MASK_IDX)->setClampNoFilter(); |
| 893 | |
| 894 | GrMatrix m; |
| 895 | m.setTranslate(-finalRect.fLeft, -finalRect.fTop); |
| 896 | m.postIDiv(srcTexture->width(), srcTexture->height()); |
| 897 | grp->getMaskSampler(MASK_IDX)->setMatrix(m); |
| 898 | context->drawRect(*grp, finalRect); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 899 | return true; |
| 900 | } |
| 901 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 902 | static bool drawWithMaskFilter(GrContext* context, const SkPath& path, |
| 903 | SkMaskFilter* filter, const SkMatrix& matrix, |
| 904 | const SkRegion& clip, SkBounder* bounder, |
| 905 | GrPaint* grp) { |
| 906 | SkMask srcM, dstM; |
| 907 | |
| 908 | if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM, |
| 909 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 910 | return false; |
| 911 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 912 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 913 | |
| 914 | if (!filter->filterMask(&dstM, srcM, matrix, NULL)) { |
| 915 | return false; |
| 916 | } |
| 917 | // this will free-up dstM when we're done (allocated in filterMask()) |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 918 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 919 | |
| 920 | if (clip.quickReject(dstM.fBounds)) { |
| 921 | return false; |
| 922 | } |
| 923 | if (bounder && !bounder->doIRect(dstM.fBounds)) { |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 928 | // the current clip (and identity matrix) and grpaint settings |
| 929 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 930 | // used to compute inverse view, if necessary |
| 931 | GrMatrix ivm = context->getMatrix(); |
| 932 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 933 | GrAutoMatrix avm(context, GrMatrix::I()); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 934 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 935 | const GrTextureDesc desc = { |
| 936 | kNone_GrTextureFlags, |
| 937 | kNone_GrAALevel, |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 938 | dstM.fBounds.width(), |
| 939 | dstM.fBounds.height(), |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 940 | kAlpha_8_GrPixelConfig |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 941 | }; |
| 942 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 943 | GrAutoScratchTexture ast(context, desc); |
| 944 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 945 | |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 946 | if (NULL == texture) { |
| 947 | return false; |
| 948 | } |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 949 | texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight, |
| 950 | dstM.fImage, dstM.fRowBytes); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 951 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 952 | if (grp->hasTextureOrMask() && ivm.invert(&ivm)) { |
| 953 | grp->preConcatActiveSamplerMatrices(ivm); |
| 954 | } |
| 955 | |
| 956 | static const int MASK_IDX = GrPaint::kMaxMasks - 1; |
| 957 | // we assume the last mask index is available for use |
| 958 | GrAssert(NULL == grp->getMask(MASK_IDX)); |
| 959 | grp->setMask(MASK_IDX, texture); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 960 | grp->getMaskSampler(MASK_IDX)->setClampNoFilter(); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 961 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 962 | GrRect d; |
| 963 | d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft), |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 964 | GrIntToScalar(dstM.fBounds.fTop), |
| 965 | GrIntToScalar(dstM.fBounds.fRight), |
| 966 | GrIntToScalar(dstM.fBounds.fBottom)); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 967 | |
| 968 | GrMatrix m; |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 969 | m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, |
| 970 | -dstM.fBounds.fTop*SK_Scalar1); |
bsalomon@google.com | eb2aa1d | 2011-07-14 15:45:19 +0000 | [diff] [blame] | 971 | m.postIDiv(texture->width(), texture->height()); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 972 | grp->getMaskSampler(MASK_IDX)->setMatrix(m); |
| 973 | |
| 974 | context->drawRect(*grp, d); |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 975 | return true; |
| 976 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 977 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 978 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 979 | const SkPaint& origPaint, const SkMatrix* prePathMatrix, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 980 | bool pathIsMutable) { |
| 981 | CHECK_SHOULD_DRAW(draw); |
| 982 | |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 983 | bool doFill = true; |
| 984 | SkTLazy<SkPaint> lazyPaint; |
| 985 | const SkPaint* paint = &origPaint; |
| 986 | |
| 987 | // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha) |
| 988 | // if we can, we draw lots faster (raster device does this same test) |
| 989 | { |
| 990 | SkAlpha newAlpha; |
| 991 | if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) { |
| 992 | lazyPaint.set(*paint); |
| 993 | lazyPaint.get()->setAlpha(newAlpha); |
| 994 | lazyPaint.get()->setStrokeWidth(0); |
| 995 | paint = lazyPaint.get(); |
| 996 | doFill = false; |
| 997 | } |
| 998 | } |
| 999 | // must reference paint from here down, and not origPaint |
| 1000 | // since we may have change the paint (using lazyPaint for storage) |
| 1001 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1002 | GrPaint grPaint; |
| 1003 | SkAutoCachedTexture act; |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 1004 | if (!this->skPaint2GrPaintShader(*paint, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1005 | &act, |
| 1006 | *draw.fMatrix, |
| 1007 | &grPaint, |
| 1008 | true)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1009 | return; |
| 1010 | } |
| 1011 | |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 1012 | // If we have a prematrix, apply it to the path, optimizing for the case |
| 1013 | // where the original path can in fact be modified in place (even though |
| 1014 | // its parameter type is const). |
| 1015 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| 1016 | SkPath tmpPath; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1017 | |
| 1018 | if (prePathMatrix) { |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1019 | SkPath* result = pathPtr; |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1020 | |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1021 | if (!pathIsMutable) { |
| 1022 | result = &tmpPath; |
| 1023 | pathIsMutable = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1024 | } |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1025 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 1026 | // of applying it here? See SkDraw.cpp |
| 1027 | pathPtr->transform(*prePathMatrix, result); |
| 1028 | pathPtr = result; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1029 | } |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1030 | // at this point we're done with prePathMatrix |
| 1031 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1032 | |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 1033 | if (doFill && (paint->getPathEffect() || |
| 1034 | paint->getStyle() != SkPaint::kFill_Style)) { |
| 1035 | // it is safe to use tmpPath here, even if we already used it for the |
| 1036 | // prepathmatrix, since getFillPath can take the same object for its |
| 1037 | // input and output safely. |
| 1038 | doFill = paint->getFillPath(*pathPtr, &tmpPath); |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1039 | pathPtr = &tmpPath; |
| 1040 | } |
| 1041 | |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 1042 | if (paint->getMaskFilter()) { |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1043 | // avoid possibly allocating a new path in transform if we can |
| 1044 | SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; |
| 1045 | |
| 1046 | // transform the path into device space |
reed@google.com | e344564 | 2011-02-16 23:20:39 +0000 | [diff] [blame] | 1047 | pathPtr->transform(*draw.fMatrix, devPathPtr); |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 1048 | if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(), |
senorblanco@chromium.org | b08ea1b | 2011-07-12 16:54:59 +0000 | [diff] [blame] | 1049 | *draw.fMatrix, *draw.fClip, draw.fBounder, |
| 1050 | &grPaint)) { |
reed@google.com | fe62638 | 2011-09-21 13:50:35 +0000 | [diff] [blame] | 1051 | drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(), |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1052 | *draw.fMatrix, *draw.fClip, draw.fBounder, |
| 1053 | &grPaint); |
| 1054 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1055 | return; |
| 1056 | } |
reed@google.com | 6930285 | 2011-02-16 18:08:07 +0000 | [diff] [blame] | 1057 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1058 | GrPathFill fill = kHairLine_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1059 | |
reed@google.com | 0c219b6 | 2011-02-16 21:31:18 +0000 | [diff] [blame] | 1060 | if (doFill) { |
| 1061 | switch (pathPtr->getFillType()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1062 | case SkPath::kWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1063 | fill = kWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1066 | fill = kEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1069 | fill = kInverseWinding_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1072 | fill = kInverseEvenOdd_PathFill; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1073 | break; |
| 1074 | default: |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1075 | SkDebugf("Unsupported path fill type\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1076 | return; |
| 1077 | } |
| 1078 | } |
| 1079 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 1080 | fContext->drawPath(grPaint, *pathPtr, fill); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1083 | void SkGpuDevice::drawBitmap(const SkDraw& draw, |
| 1084 | const SkBitmap& bitmap, |
| 1085 | const SkIRect* srcRectPtr, |
| 1086 | const SkMatrix& m, |
| 1087 | const SkPaint& paint) { |
| 1088 | CHECK_SHOULD_DRAW(draw); |
| 1089 | |
| 1090 | SkIRect srcRect; |
| 1091 | if (NULL == srcRectPtr) { |
| 1092 | srcRect.set(0, 0, bitmap.width(), bitmap.height()); |
| 1093 | } else { |
| 1094 | srcRect = *srcRectPtr; |
| 1095 | } |
| 1096 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1097 | if (paint.getMaskFilter()){ |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1098 | // Convert the bitmap to a shader so that the rect can be drawn |
| 1099 | // through drawRect, which supports mask filters. |
| 1100 | SkBitmap tmp; // subset of bitmap, if necessary |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1101 | const SkBitmap* bitmapPtr = &bitmap; |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1102 | if (srcRectPtr) { |
| 1103 | if (!bitmap.extractSubset(&tmp, srcRect)) { |
| 1104 | return; // extraction failed |
| 1105 | } |
| 1106 | bitmapPtr = &tmp; |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1107 | srcRect.set(0,0, srcRect.width(), srcRect.height()); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1108 | } |
| 1109 | SkPaint paintWithTexture(paint); |
| 1110 | paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr, |
| 1111 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref(); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1112 | SkRect ScalarRect; |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 1113 | ScalarRect.set(srcRect); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1114 | |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 1115 | // Transform 'm' needs to be concatenated to the draw matrix, |
| 1116 | // rather than transforming the primitive directly, so that 'm' will |
| 1117 | // also affect the behavior of the mask filter. |
| 1118 | SkMatrix drawMatrix; |
| 1119 | drawMatrix.setConcat(*draw.fMatrix, m); |
| 1120 | SkDraw transformedDraw(draw); |
| 1121 | transformedDraw.fMatrix = &drawMatrix; |
| 1122 | |
| 1123 | this->drawRect(transformedDraw, ScalarRect, paintWithTexture); |
| 1124 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
| 1127 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1128 | GrPaint grPaint; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1129 | if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1132 | GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1133 | if (paint.isFilterBitmap()) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1134 | sampler->setFilter(GrSamplerState::kBilinear_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1135 | } else { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1136 | sampler->setFilter(GrSamplerState::kNearest_Filter); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1137 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1138 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1139 | const int maxTextureSize = fContext->getMaxTextureSize(); |
| 1140 | if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize && |
| 1141 | bitmap.height() <= maxTextureSize)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1142 | // take the fast case |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1143 | this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | // undo the translate done by SkCanvas |
| 1148 | int DX = SkMax32(0, srcRect.fLeft); |
| 1149 | int DY = SkMax32(0, srcRect.fTop); |
| 1150 | // compute clip bounds in local coordinates |
| 1151 | SkIRect clipRect; |
| 1152 | { |
| 1153 | SkRect r; |
| 1154 | r.set(draw.fClip->getBounds()); |
| 1155 | SkMatrix matrix, inverse; |
| 1156 | matrix.setConcat(*draw.fMatrix, m); |
| 1157 | if (!matrix.invert(&inverse)) { |
| 1158 | return; |
| 1159 | } |
| 1160 | inverse.mapRect(&r); |
| 1161 | r.roundOut(&clipRect); |
| 1162 | // apply the canvas' translate to our local clip |
| 1163 | clipRect.offset(DX, DY); |
| 1164 | } |
| 1165 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1166 | int nx = bitmap.width() / maxTextureSize; |
| 1167 | int ny = bitmap.height() / maxTextureSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1168 | for (int x = 0; x <= nx; x++) { |
| 1169 | for (int y = 0; y <= ny; y++) { |
| 1170 | SkIRect tileR; |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1171 | tileR.set(x * maxTextureSize, y * maxTextureSize, |
| 1172 | (x + 1) * maxTextureSize, (y + 1) * maxTextureSize); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1173 | if (!SkIRect::Intersects(tileR, clipRect)) { |
| 1174 | continue; |
| 1175 | } |
| 1176 | |
| 1177 | SkIRect srcR = tileR; |
| 1178 | if (!srcR.intersect(srcRect)) { |
| 1179 | continue; |
| 1180 | } |
| 1181 | |
| 1182 | SkBitmap tmpB; |
| 1183 | if (bitmap.extractSubset(&tmpB, tileR)) { |
| 1184 | // now offset it to make it "local" to our tmp bitmap |
| 1185 | srcR.offset(-tileR.fLeft, -tileR.fTop); |
| 1186 | |
| 1187 | SkMatrix tmpM(m); |
| 1188 | { |
| 1189 | int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft); |
| 1190 | int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop); |
| 1191 | tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy)); |
| 1192 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1193 | this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1201 | * large to be represented by a single texture. |
| 1202 | * |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1203 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1204 | * 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] | 1205 | */ |
| 1206 | void SkGpuDevice::internalDrawBitmap(const SkDraw& draw, |
| 1207 | const SkBitmap& bitmap, |
| 1208 | const SkIRect& srcRect, |
| 1209 | const SkMatrix& m, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1210 | GrPaint* grPaint) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1211 | SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1212 | bitmap.height() <= fContext->getMaxTextureSize()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1213 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1214 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1215 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 1216 | SkDebugf("nothing to draw\n"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1217 | return; |
| 1218 | } |
| 1219 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1220 | GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx); |
| 1221 | |
| 1222 | sampler->setWrapX(GrSamplerState::kClamp_WrapMode); |
| 1223 | sampler->setWrapY(GrSamplerState::kClamp_WrapMode); |
| 1224 | sampler->setSampleMode(GrSamplerState::kNormal_SampleMode); |
| 1225 | sampler->setMatrix(GrMatrix::I()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1226 | |
| 1227 | GrTexture* texture; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1228 | SkAutoCachedTexture act(this, bitmap, *sampler, &texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1229 | if (NULL == texture) { |
| 1230 | return; |
| 1231 | } |
| 1232 | |
bsalomon@google.com | 452943d | 2011-10-31 17:37:14 +0000 | [diff] [blame] | 1233 | grPaint->setTexture(kBitmapTextureIdx, texture); |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 1234 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1235 | GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()), |
| 1236 | GrIntToScalar(srcRect.height())); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1237 | GrRect paintRect; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1238 | paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()), |
| 1239 | GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()), |
| 1240 | GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()), |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1241 | GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1242 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1243 | if (GrSamplerState::kNearest_Filter != sampler->getFilter() && |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1244 | (srcRect.width() < bitmap.width() || |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1245 | srcRect.height() < bitmap.height())) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1246 | // If drawing a subrect of the bitmap and filtering is enabled, |
| 1247 | // use a constrained texture domain to avoid color bleeding |
| 1248 | GrScalar left, top, right, bottom; |
| 1249 | if (srcRect.width() > 1) { |
| 1250 | GrScalar border = GR_ScalarHalf / bitmap.width(); |
| 1251 | left = paintRect.left() + border; |
| 1252 | right = paintRect.right() - border; |
| 1253 | } else { |
| 1254 | left = right = GrScalarHalf(paintRect.left() + paintRect.right()); |
| 1255 | } |
| 1256 | if (srcRect.height() > 1) { |
| 1257 | GrScalar border = GR_ScalarHalf / bitmap.height(); |
| 1258 | top = paintRect.top() + border; |
| 1259 | bottom = paintRect.bottom() - border; |
| 1260 | } else { |
| 1261 | top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1262 | } |
| 1263 | GrRect textureDomain; |
| 1264 | textureDomain.setLTRB(left, top, right, bottom); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1265 | sampler->setTextureDomain(textureDomain); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1268 | fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1272 | int left, int top, const SkPaint& paint) { |
| 1273 | CHECK_SHOULD_DRAW(draw); |
| 1274 | |
| 1275 | SkAutoLockPixels alp(bitmap); |
| 1276 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1277 | return; |
| 1278 | } |
| 1279 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1280 | GrPaint grPaint; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1281 | if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1282 | return; |
| 1283 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1284 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1285 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 1286 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1287 | GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1288 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1289 | GrTexture* texture; |
| 1290 | sampler->setClampNoFilter(); |
| 1291 | SkAutoCachedTexture act(this, bitmap, *sampler, &texture); |
| 1292 | |
| 1293 | grPaint.setTexture(kBitmapTextureIdx, texture); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1294 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1295 | fContext->drawRectToRect(grPaint, |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 1296 | GrRect::MakeXYWH(GrIntToScalar(left), |
| 1297 | GrIntToScalar(top), |
| 1298 | GrIntToScalar(bitmap.width()), |
| 1299 | GrIntToScalar(bitmap.height())), |
| 1300 | GrRect::MakeWH(GR_Scalar1, GR_Scalar1)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev, |
| 1304 | int x, int y, const SkPaint& paint) { |
| 1305 | CHECK_SHOULD_DRAW(draw); |
| 1306 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1307 | GrPaint grPaint; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 1308 | if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) || |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1309 | !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1310 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1311 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1312 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1313 | GrTexture* devTex = grPaint.getTexture(0); |
| 1314 | SkASSERT(NULL != devTex); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1315 | |
| 1316 | const SkBitmap& bm = dev->accessBitmap(false); |
| 1317 | int w = bm.width(); |
| 1318 | int h = bm.height(); |
| 1319 | |
| 1320 | GrAutoMatrix avm(fContext, GrMatrix::I()); |
| 1321 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 1322 | grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1323 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1324 | GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x), |
| 1325 | GrIntToScalar(y), |
| 1326 | GrIntToScalar(w), |
| 1327 | GrIntToScalar(h)); |
| 1328 | // The device being drawn may not fill up its texture (saveLayer uses |
| 1329 | // the approximate ). |
| 1330 | GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(), |
| 1331 | GR_Scalar1 * h / devTex->height()); |
| 1332 | |
| 1333 | fContext->drawRectToRect(grPaint, dstRect, srcRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /////////////////////////////////////////////////////////////////////////////// |
| 1337 | |
| 1338 | // must be in SkCanvas::VertexMode order |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 1339 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1340 | kTriangles_PrimitiveType, |
| 1341 | kTriangleStrip_PrimitiveType, |
| 1342 | kTriangleFan_PrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1343 | }; |
| 1344 | |
| 1345 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1346 | int vertexCount, const SkPoint vertices[], |
| 1347 | const SkPoint texs[], const SkColor colors[], |
| 1348 | SkXfermode* xmode, |
| 1349 | const uint16_t indices[], int indexCount, |
| 1350 | const SkPaint& paint) { |
| 1351 | CHECK_SHOULD_DRAW(draw); |
| 1352 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1353 | GrPaint grPaint; |
| 1354 | SkAutoCachedTexture act; |
| 1355 | // we ignore the shader if texs is null. |
| 1356 | if (NULL == texs) { |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1357 | if (!this->skPaint2GrPaintNoShader(paint, |
| 1358 | false, |
| 1359 | &grPaint, |
| 1360 | NULL == colors)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1361 | return; |
| 1362 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1363 | } else { |
reed@google.com | 1a2e8d2 | 2011-01-21 22:08:29 +0000 | [diff] [blame] | 1364 | if (!this->skPaint2GrPaintShader(paint, &act, |
| 1365 | *draw.fMatrix, |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1366 | &grPaint, |
| 1367 | NULL == colors)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1368 | return; |
| 1369 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1370 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (NULL != xmode && NULL != texs && NULL != colors) { |
| 1373 | SkXfermode::Mode mode; |
| 1374 | if (!SkXfermode::IsMode(xmode, &mode) || |
| 1375 | SkXfermode::kMultiply_Mode != mode) { |
| 1376 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
| 1377 | #if 0 |
| 1378 | return |
| 1379 | #endif |
| 1380 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1381 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1382 | |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1383 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
| 1384 | if (NULL != colors) { |
| 1385 | // need to convert byte order and from non-PM to PM |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 1386 | convertedColors.reset(vertexCount); |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1387 | for (int i = 0; i < vertexCount; ++i) { |
| 1388 | convertedColors[i] = SkGr::SkColor2GrColor(colors[i]); |
| 1389 | } |
| 1390 | colors = convertedColors.get(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1391 | } |
bsalomon@google.com | 498776a | 2011-08-16 19:20:44 +0000 | [diff] [blame] | 1392 | fContext->drawVertices(grPaint, |
| 1393 | gVertexMode2PrimitiveType[vmode], |
| 1394 | vertexCount, |
| 1395 | (GrPoint*) vertices, |
| 1396 | (GrPoint*) texs, |
| 1397 | colors, |
| 1398 | indices, |
| 1399 | indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | /////////////////////////////////////////////////////////////////////////////// |
| 1403 | |
| 1404 | static void GlyphCacheAuxProc(void* data) { |
| 1405 | delete (GrFontScaler*)data; |
| 1406 | } |
| 1407 | |
| 1408 | static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) { |
| 1409 | void* auxData; |
| 1410 | GrFontScaler* scaler = NULL; |
| 1411 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 1412 | scaler = (GrFontScaler*)auxData; |
| 1413 | } |
| 1414 | if (NULL == scaler) { |
| 1415 | scaler = new SkGrFontScaler(cache); |
| 1416 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 1417 | } |
| 1418 | return scaler; |
| 1419 | } |
| 1420 | |
| 1421 | static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state, |
| 1422 | SkFixed fx, SkFixed fy, |
| 1423 | const SkGlyph& glyph) { |
| 1424 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1425 | |
| 1426 | GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs; |
| 1427 | |
| 1428 | if (NULL == procs->fFontScaler) { |
| 1429 | procs->fFontScaler = get_gr_font_scaler(state.fCache); |
| 1430 | } |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1431 | |
| 1432 | /* |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1433 | * What should we do with fy? (assuming horizontal/latin text) |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1434 | * |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1435 | * The raster code calls SkFixedFloorToFixed on it, as it does with fx. |
| 1436 | * It calls that rather than round, because our caller has already added |
| 1437 | * SK_FixedHalf, so that calling floor gives us the rounded integer. |
| 1438 | * |
| 1439 | * Test code between raster and gpu (they should draw the same) |
| 1440 | * |
| 1441 | * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint); |
| 1442 | * |
| 1443 | * Perhaps we should only perform this integralization if there is no |
| 1444 | * fExtMatrix... |
reed@google.com | 39ce0ac | 2011-04-08 15:42:19 +0000 | [diff] [blame] | 1445 | */ |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1446 | fy = SkFixedFloorToFixed(fy); |
| 1447 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1448 | procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0), |
reed@google.com | 3b139f5 | 2011-06-07 17:56:25 +0000 | [diff] [blame] | 1449 | SkFixedFloorToFixed(fx), fy, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1450 | procs->fFontScaler); |
| 1451 | } |
| 1452 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1453 | SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1454 | |
| 1455 | // deferred allocation |
| 1456 | if (NULL == fDrawProcs) { |
| 1457 | fDrawProcs = new GrSkDrawProcs; |
| 1458 | fDrawProcs->fD1GProc = SkGPU_Draw1Glyph; |
| 1459 | fDrawProcs->fContext = fContext; |
| 1460 | } |
| 1461 | |
| 1462 | // init our (and GL's) state |
| 1463 | fDrawProcs->fTextContext = context; |
| 1464 | fDrawProcs->fFontScaler = NULL; |
| 1465 | return fDrawProcs; |
| 1466 | } |
| 1467 | |
| 1468 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1469 | size_t byteLength, SkScalar x, SkScalar y, |
| 1470 | const SkPaint& paint) { |
| 1471 | CHECK_SHOULD_DRAW(draw); |
| 1472 | |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 1473 | if (draw.fMatrix->hasPerspective()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1474 | // this guy will just call our drawPath() |
| 1475 | draw.drawText((const char*)text, byteLength, x, y, paint); |
| 1476 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1477 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1478 | |
| 1479 | GrPaint grPaint; |
| 1480 | SkAutoCachedTexture act; |
| 1481 | |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1482 | if (!this->skPaint2GrPaintShader(paint, |
| 1483 | &act, |
| 1484 | *draw.fMatrix, |
| 1485 | &grPaint, |
| 1486 | true)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1487 | return; |
| 1488 | } |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1489 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1490 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1491 | this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1496 | size_t byteLength, const SkScalar pos[], |
| 1497 | SkScalar constY, int scalarsPerPos, |
| 1498 | const SkPaint& paint) { |
| 1499 | CHECK_SHOULD_DRAW(draw); |
| 1500 | |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 1501 | if (draw.fMatrix->hasPerspective()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1502 | // this guy will just call our drawPath() |
| 1503 | draw.drawPosText((const char*)text, byteLength, pos, constY, |
| 1504 | scalarsPerPos, paint); |
| 1505 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1506 | SkDraw myDraw(draw); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1507 | |
| 1508 | GrPaint grPaint; |
| 1509 | SkAutoCachedTexture act; |
Scroggo | d757df2 | 2011-05-16 13:11:16 +0000 | [diff] [blame] | 1510 | if (!this->skPaint2GrPaintShader(paint, |
| 1511 | &act, |
| 1512 | *draw.fMatrix, |
| 1513 | &grPaint, |
| 1514 | true)) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1515 | return; |
| 1516 | } |
| 1517 | |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 1518 | GrTextContext context(fContext, grPaint, draw.fExtMatrix); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 1519 | myDraw.fProcs = this->initDrawForText(&context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1520 | this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY, |
| 1521 | scalarsPerPos, paint); |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1526 | size_t len, const SkPath& path, |
| 1527 | const SkMatrix* m, const SkPaint& paint) { |
| 1528 | CHECK_SHOULD_DRAW(draw); |
| 1529 | |
| 1530 | SkASSERT(draw.fDevice == this); |
| 1531 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1532 | } |
| 1533 | |
| 1534 | /////////////////////////////////////////////////////////////////////////////// |
| 1535 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1536 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1537 | if (!paint.isLCDRenderText()) { |
| 1538 | // we're cool with the paint as is |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
| 1542 | if (paint.getShader() || |
| 1543 | paint.getXfermode() || // unless its srcover |
| 1544 | paint.getMaskFilter() || |
| 1545 | paint.getRasterizer() || |
| 1546 | paint.getColorFilter() || |
| 1547 | paint.getPathEffect() || |
| 1548 | paint.isFakeBoldText() || |
| 1549 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1550 | // turn off lcd |
| 1551 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1552 | flags->fHinting = paint.getHinting(); |
| 1553 | return true; |
| 1554 | } |
| 1555 | // we're cool with the paint as is |
| 1556 | return false; |
| 1557 | } |
| 1558 | |
| 1559 | /////////////////////////////////////////////////////////////////////////////// |
| 1560 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1561 | SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap, |
| 1562 | const GrSamplerState& sampler, |
| 1563 | TexType type) { |
| 1564 | GrContext::TextureCacheEntry entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1565 | GrContext* ctx = this->context(); |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1566 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1567 | if (kBitmap_TexType != type) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 1568 | const GrTextureDesc desc = { |
| 1569 | kRenderTarget_GrTextureFlagBit, |
| 1570 | kNone_GrAALevel, |
| 1571 | bitmap.width(), |
| 1572 | bitmap.height(), |
| 1573 | SkGr::Bitmap2PixelConfig(bitmap) |
| 1574 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1575 | GrContext::ScratchTexMatch match; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1576 | if (kSaveLayerDeviceRenderTarget_TexType == type) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1577 | // we know layers will only be drawn through drawDevice. |
| 1578 | // drawDevice has been made to work with content embedded in a |
| 1579 | // larger texture so its okay to use the approximate version. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1580 | match = GrContext::kApprox_ScratchTexMatch; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1581 | } else { |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1582 | SkASSERT(kDeviceRenderTarget_TexType == type); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1583 | match = GrContext::kExact_ScratchTexMatch; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 1584 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1585 | entry = ctx->lockScratchTexture(desc, match); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1586 | } else { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1587 | if (!bitmap.isVolatile()) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1588 | GrContext::TextureKey key = bitmap.getGenerationID(); |
| 1589 | key |= ((uint64_t) bitmap.pixelRefOffset()) << 32; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1590 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1591 | entry = ctx->findAndLockTexture(key, bitmap.width(), |
| 1592 | bitmap.height(), sampler); |
| 1593 | if (NULL == entry.texture()) { |
| 1594 | entry = sk_gr_create_bitmap_texture(ctx, key, sampler, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1595 | bitmap); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1596 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1597 | } else { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1598 | entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1599 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1600 | if (NULL == entry.texture()) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 1601 | GrPrintf("---- failed to create texture for cache [%d %d]\n", |
| 1602 | bitmap.width(), bitmap.height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1605 | return entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1608 | void SkGpuDevice::unlockCachedTexture(TexCache cache) { |
| 1609 | this->context()->unlockTexture(cache); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1612 | SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config, |
| 1613 | int width, int height, |
| 1614 | bool isOpaque, |
| 1615 | Usage usage) { |
| 1616 | return SkNEW_ARGS(SkGpuDevice,(this->context(), config, |
| 1617 | width, height, usage)); |
| 1618 | } |
| 1619 | |