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