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