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