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