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