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