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