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