| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkGpuDevice.h" |
| 9 | |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 10 | #include "effects/GrBicubicEffect.h" |
| commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 11 | #include "effects/GrTextureDomain.h" |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 12 | #include "effects/GrSimpleTextureEffect.h" |
| 13 | |
| 14 | #include "GrContext.h" |
| 15 | #include "GrBitmapTextContext.h" |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 16 | #include "GrDistanceFieldTextContext.h" |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 17 | |
| 18 | #include "SkGrTexturePixelRef.h" |
| 19 | |
| commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 20 | #include "SkBounder.h" |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 21 | #include "SkColorFilter.h" |
| 22 | #include "SkDeviceImageFilterProxy.h" |
| 23 | #include "SkDrawProcs.h" |
| 24 | #include "SkGlyphCache.h" |
| 25 | #include "SkImageFilter.h" |
| commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 26 | #include "SkMaskFilter.h" |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 27 | #include "SkPathEffect.h" |
| 28 | #include "SkRRect.h" |
| 29 | #include "SkStroke.h" |
| reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 30 | #include "SkSurface.h" |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 31 | #include "SkTLazy.h" |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 32 | #include "SkUtils.h" |
| 33 | #include "SkErrorInternals.h" |
| 34 | |
| 35 | #define CACHE_COMPATIBLE_DEVICE_TEXTURES 1 |
| 36 | |
| 37 | #if 0 |
| 38 | extern bool (*gShouldDrawProc)(); |
| 39 | #define CHECK_SHOULD_DRAW(draw, forceI) \ |
| 40 | do { \ |
| 41 | if (gShouldDrawProc && !gShouldDrawProc()) return; \ |
| 42 | this->prepareDraw(draw, forceI); \ |
| 43 | } while (0) |
| 44 | #else |
| 45 | #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI) |
| 46 | #endif |
| 47 | |
| 48 | // This constant represents the screen alignment criterion in texels for |
| 49 | // requiring texture domain clamping to prevent color bleeding when drawing |
| 50 | // a sub region of a larger source image. |
| commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 51 | #define COLOR_BLEED_TOLERANCE 0.001f |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 52 | |
| 53 | #define DO_DEFERRED_CLEAR() \ |
| 54 | do { \ |
| 55 | if (fNeedClear) { \ |
| 56 | this->clear(SK_ColorTRANSPARENT); \ |
| 57 | } \ |
| 58 | } while (false) \ |
| 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | #define CHECK_FOR_ANNOTATION(paint) \ |
| 63 | do { if (paint.getAnnotation()) { return; } } while (0) |
| 64 | |
| 65 | /////////////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | |
| 68 | class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable { |
| 69 | public: |
| 70 | SkAutoCachedTexture() |
| 71 | : fDevice(NULL) |
| 72 | , fTexture(NULL) { |
| 73 | } |
| 74 | |
| 75 | SkAutoCachedTexture(SkGpuDevice* device, |
| 76 | const SkBitmap& bitmap, |
| 77 | const GrTextureParams* params, |
| 78 | GrTexture** texture) |
| 79 | : fDevice(NULL) |
| 80 | , fTexture(NULL) { |
| 81 | SkASSERT(NULL != texture); |
| 82 | *texture = this->set(device, bitmap, params); |
| 83 | } |
| 84 | |
| 85 | ~SkAutoCachedTexture() { |
| 86 | if (NULL != fTexture) { |
| 87 | GrUnlockAndUnrefCachedBitmapTexture(fTexture); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | GrTexture* set(SkGpuDevice* device, |
| 92 | const SkBitmap& bitmap, |
| 93 | const GrTextureParams* params) { |
| 94 | if (NULL != fTexture) { |
| 95 | GrUnlockAndUnrefCachedBitmapTexture(fTexture); |
| 96 | fTexture = NULL; |
| 97 | } |
| 98 | fDevice = device; |
| 99 | GrTexture* result = (GrTexture*)bitmap.getTexture(); |
| 100 | if (NULL == result) { |
| 101 | // Cannot return the native texture so look it up in our cache |
| 102 | fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params); |
| 103 | result = fTexture; |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | SkGpuDevice* fDevice; |
| 110 | GrTexture* fTexture; |
| 111 | }; |
| 112 | |
| 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
| 115 | struct GrSkDrawProcs : public SkDrawProcs { |
| 116 | public: |
| 117 | GrContext* fContext; |
| 118 | GrTextContext* fTextContext; |
| 119 | GrFontScaler* fFontScaler; // cached in the skia glyphcache |
| 120 | }; |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
| 124 | static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) { |
| 125 | switch (config) { |
| 126 | case kAlpha_8_GrPixelConfig: |
| 127 | *isOpaque = false; |
| 128 | return SkBitmap::kA8_Config; |
| 129 | case kRGB_565_GrPixelConfig: |
| 130 | *isOpaque = true; |
| 131 | return SkBitmap::kRGB_565_Config; |
| 132 | case kRGBA_4444_GrPixelConfig: |
| 133 | *isOpaque = false; |
| 134 | return SkBitmap::kARGB_4444_Config; |
| 135 | case kSkia8888_GrPixelConfig: |
| 136 | // we don't currently have a way of knowing whether |
| 137 | // a 8888 is opaque based on the config. |
| 138 | *isOpaque = false; |
| 139 | return SkBitmap::kARGB_8888_Config; |
| 140 | default: |
| 141 | *isOpaque = false; |
| 142 | return SkBitmap::kNo_Config; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * GrRenderTarget does not know its opaqueness, only its config, so we have |
| 148 | * to make conservative guesses when we return an "equivalent" bitmap. |
| 149 | */ |
| 150 | static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) { |
| 151 | bool isOpaque; |
| 152 | SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque); |
| 153 | |
| 154 | SkBitmap bitmap; |
| 155 | bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0, |
| 156 | isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 157 | return bitmap; |
| 158 | } |
| 159 | |
| 160 | SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) { |
| 161 | SkASSERT(NULL != surface); |
| 162 | if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) { |
| 163 | return NULL; |
| 164 | } |
| 165 | if (surface->asTexture()) { |
| 166 | return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture())); |
| 167 | } else { |
| 168 | return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget())); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture) |
| 173 | : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) { |
| 174 | this->initFromRenderTarget(context, texture->asRenderTarget(), false); |
| 175 | } |
| 176 | |
| 177 | SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget) |
| 178 | : SkBitmapDevice(make_bitmap(context, renderTarget)) { |
| 179 | this->initFromRenderTarget(context, renderTarget, false); |
| 180 | } |
| 181 | |
| 182 | void SkGpuDevice::initFromRenderTarget(GrContext* context, |
| 183 | GrRenderTarget* renderTarget, |
| 184 | bool cached) { |
| 185 | fDrawProcs = NULL; |
| 186 | |
| 187 | fContext = context; |
| 188 | fContext->ref(); |
| 189 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 190 | fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties)); |
| 191 | fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties)); |
| commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 192 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 193 | fRenderTarget = NULL; |
| 194 | fNeedClear = false; |
| 195 | |
| 196 | SkASSERT(NULL != renderTarget); |
| 197 | fRenderTarget = renderTarget; |
| 198 | fRenderTarget->ref(); |
| 199 | |
| 200 | // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref |
| 201 | // on the RT but not vice-versa. |
| 202 | // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without |
| 203 | // busting chrome (for a currently unknown reason). |
| 204 | GrSurface* surface = fRenderTarget->asTexture(); |
| 205 | if (NULL == surface) { |
| 206 | surface = fRenderTarget; |
| 207 | } |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 208 | |
| 209 | SkImageInfo info; |
| 210 | surface->asImageInfo(&info); |
| 211 | SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached)); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 212 | |
| reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 213 | this->setPixelRef(pr)->unref(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 216 | SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo, |
| 217 | int sampleCount) { |
| 218 | if (kUnknown_SkColorType == origInfo.colorType() || |
| 219 | origInfo.width() < 0 || origInfo.height() < 0) { |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | SkImageInfo info = origInfo; |
| 224 | // TODO: perhas we can loosen this check now that colortype is more detailed |
| 225 | // e.g. can we support both RGBA and BGRA here? |
| 226 | if (kRGB_565_SkColorType == info.colorType()) { |
| 227 | info.fAlphaType = kOpaque_SkAlphaType; // force this setting |
| 228 | } else { |
| 229 | info.fColorType = kPMColor_SkColorType; |
| 230 | if (kOpaque_SkAlphaType != info.alphaType()) { |
| 231 | info.fAlphaType = kPremul_SkAlphaType; // force this setting |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | GrTextureDesc desc; |
| 236 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 237 | desc.fWidth = info.width(); |
| 238 | desc.fHeight = info.height(); |
| 239 | desc.fConfig = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType()); |
| 240 | desc.fSampleCnt = sampleCount; |
| 241 | |
| 242 | SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0)); |
| 243 | if (!texture.get()) { |
| 244 | return NULL; |
| 245 | } |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 246 | |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 247 | return SkNEW_ARGS(SkGpuDevice, (context, texture.get())); |
| 248 | } |
| 249 | |
| 250 | #ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG |
| 251 | static SkBitmap make_bitmap(SkBitmap::Config config, int width, int height) { |
| 252 | SkBitmap bm; |
| 253 | bm.setConfig(SkImageInfo::Make(width, height, |
| 254 | SkBitmapConfigToColorType(config), |
| 255 | kPremul_SkAlphaType)); |
| 256 | return bm; |
| 257 | } |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 258 | SkGpuDevice::SkGpuDevice(GrContext* context, |
| 259 | SkBitmap::Config config, |
| 260 | int width, |
| 261 | int height, |
| 262 | int sampleCount) |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 263 | : SkBitmapDevice(make_bitmap(config, width, height)) |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 264 | { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 265 | fDrawProcs = NULL; |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 266 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 267 | fContext = context; |
| 268 | fContext->ref(); |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 269 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 270 | fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties)); |
| 271 | fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties)); |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 272 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 273 | fRenderTarget = NULL; |
| 274 | fNeedClear = false; |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 275 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 276 | if (config != SkBitmap::kRGB_565_Config) { |
| 277 | config = SkBitmap::kARGB_8888_Config; |
| 278 | } |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 279 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 280 | GrTextureDesc desc; |
| 281 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 282 | desc.fWidth = width; |
| 283 | desc.fHeight = height; |
| 284 | desc.fConfig = SkBitmapConfig2GrPixelConfig(config); |
| 285 | desc.fSampleCnt = sampleCount; |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 286 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 287 | SkImageInfo info; |
| 288 | if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) { |
| 289 | sk_throw(); |
| 290 | } |
| 291 | info.fWidth = width; |
| 292 | info.fHeight = height; |
| 293 | info.fAlphaType = kPremul_SkAlphaType; |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 294 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 295 | SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0)); |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 296 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 297 | if (NULL != texture) { |
| 298 | fRenderTarget = texture->asRenderTarget(); |
| 299 | fRenderTarget->ref(); |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 300 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 301 | SkASSERT(NULL != fRenderTarget); |
| skia.committer@gmail.com | 969588f | 2014-02-16 03:01:56 +0000 | [diff] [blame] | 302 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 303 | // wrap the bitmap with a pixelref to expose our texture |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 304 | SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture)); |
| reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 305 | this->setPixelRef(pr)->unref(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 306 | } else { |
| 307 | GrPrintf("--- failed to create gpu-offscreen [%d %d]\n", |
| 308 | width, height); |
| 309 | SkASSERT(false); |
| 310 | } |
| 311 | } |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 312 | #endif |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 313 | |
| 314 | SkGpuDevice::~SkGpuDevice() { |
| 315 | if (fDrawProcs) { |
| 316 | delete fDrawProcs; |
| 317 | } |
| skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 318 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 319 | delete fMainTextContext; |
| 320 | delete fFallbackTextContext; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 321 | |
| 322 | // The GrContext takes a ref on the target. We don't want to cause the render |
| 323 | // target to be unnecessarily kept alive. |
| 324 | if (fContext->getRenderTarget() == fRenderTarget) { |
| 325 | fContext->setRenderTarget(NULL); |
| 326 | } |
| 327 | |
| 328 | if (fContext->getClip() == &fClipData) { |
| 329 | fContext->setClip(NULL); |
| 330 | } |
| 331 | |
| 332 | SkSafeUnref(fRenderTarget); |
| 333 | fContext->unref(); |
| 334 | } |
| 335 | |
| 336 | /////////////////////////////////////////////////////////////////////////////// |
| 337 | |
| 338 | void SkGpuDevice::makeRenderTargetCurrent() { |
| 339 | DO_DEFERRED_CLEAR(); |
| 340 | fContext->setRenderTarget(fRenderTarget); |
| 341 | } |
| 342 | |
| 343 | /////////////////////////////////////////////////////////////////////////////// |
| 344 | |
| 345 | namespace { |
| 346 | GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) { |
| 347 | switch (config8888) { |
| 348 | case SkCanvas::kNative_Premul_Config8888: |
| 349 | *flags = 0; |
| 350 | return kSkia8888_GrPixelConfig; |
| 351 | case SkCanvas::kNative_Unpremul_Config8888: |
| 352 | *flags = GrContext::kUnpremul_PixelOpsFlag; |
| 353 | return kSkia8888_GrPixelConfig; |
| 354 | case SkCanvas::kBGRA_Premul_Config8888: |
| 355 | *flags = 0; |
| 356 | return kBGRA_8888_GrPixelConfig; |
| 357 | case SkCanvas::kBGRA_Unpremul_Config8888: |
| 358 | *flags = GrContext::kUnpremul_PixelOpsFlag; |
| 359 | return kBGRA_8888_GrPixelConfig; |
| 360 | case SkCanvas::kRGBA_Premul_Config8888: |
| 361 | *flags = 0; |
| 362 | return kRGBA_8888_GrPixelConfig; |
| 363 | case SkCanvas::kRGBA_Unpremul_Config8888: |
| 364 | *flags = GrContext::kUnpremul_PixelOpsFlag; |
| 365 | return kRGBA_8888_GrPixelConfig; |
| 366 | default: |
| 367 | GrCrash("Unexpected Config8888."); |
| 368 | *flags = 0; // suppress warning |
| 369 | return kSkia8888_GrPixelConfig; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap, |
| 375 | int x, int y, |
| 376 | SkCanvas::Config8888 config8888) { |
| 377 | DO_DEFERRED_CLEAR(); |
| 378 | SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); |
| 379 | SkASSERT(!bitmap.isNull()); |
| 380 | SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()))); |
| 381 | |
| 382 | SkAutoLockPixels alp(bitmap); |
| 383 | GrPixelConfig config; |
| 384 | uint32_t flags; |
| 385 | config = config8888_to_grconfig_and_flags(config8888, &flags); |
| 386 | return fContext->readRenderTargetPixels(fRenderTarget, |
| 387 | x, y, |
| 388 | bitmap.width(), |
| 389 | bitmap.height(), |
| 390 | config, |
| 391 | bitmap.getPixels(), |
| 392 | bitmap.rowBytes(), |
| 393 | flags); |
| 394 | } |
| 395 | |
| commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 396 | #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 397 | void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y, |
| 398 | SkCanvas::Config8888 config8888) { |
| 399 | SkAutoLockPixels alp(bitmap); |
| 400 | if (!bitmap.readyToDraw()) { |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | GrPixelConfig config; |
| 405 | uint32_t flags; |
| 406 | if (SkBitmap::kARGB_8888_Config == bitmap.config()) { |
| 407 | config = config8888_to_grconfig_and_flags(config8888, &flags); |
| 408 | } else { |
| 409 | flags = 0; |
| 410 | config= SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| 411 | } |
| 412 | |
| 413 | fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(), |
| 414 | config, bitmap.getPixels(), bitmap.rowBytes(), flags); |
| 415 | } |
| commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 416 | #endif |
| 417 | |
| 418 | bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, |
| 419 | int x, int y) { |
| 420 | // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels |
| 421 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType()); |
| 422 | if (kUnknown_GrPixelConfig == config) { |
| 423 | return false; |
| 424 | } |
| 425 | uint32_t flags = 0; |
| 426 | if (kUnpremul_SkAlphaType == info.alphaType()) { |
| 427 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 428 | } |
| 429 | fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags); |
| 430 | |
| 431 | // need to bump our genID for compatibility with clients that "know" we have a bitmap |
| 432 | this->onAccessBitmap().notifyPixelsChanged(); |
| 433 | |
| 434 | return true; |
| 435 | } |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 436 | |
| 437 | void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) { |
| 438 | INHERITED::onAttachToCanvas(canvas); |
| 439 | |
| 440 | // Canvas promises that this ptr is valid until onDetachFromCanvas is called |
| 441 | fClipData.fClipStack = canvas->getClipStack(); |
| 442 | } |
| 443 | |
| 444 | void SkGpuDevice::onDetachFromCanvas() { |
| 445 | INHERITED::onDetachFromCanvas(); |
| 446 | fClipData.fClipStack = NULL; |
| 447 | } |
| 448 | |
| 449 | // call this every draw call, to ensure that the context reflects our state, |
| 450 | // and not the state from some other canvas/device |
| 451 | void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) { |
| 452 | SkASSERT(NULL != fClipData.fClipStack); |
| 453 | |
| 454 | fContext->setRenderTarget(fRenderTarget); |
| 455 | |
| 456 | SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack); |
| 457 | |
| 458 | if (forceIdentity) { |
| 459 | fContext->setIdentityMatrix(); |
| 460 | } else { |
| 461 | fContext->setMatrix(*draw.fMatrix); |
| 462 | } |
| 463 | fClipData.fOrigin = this->getOrigin(); |
| 464 | |
| 465 | fContext->setClip(&fClipData); |
| 466 | |
| 467 | DO_DEFERRED_CLEAR(); |
| 468 | } |
| 469 | |
| 470 | GrRenderTarget* SkGpuDevice::accessRenderTarget() { |
| 471 | DO_DEFERRED_CLEAR(); |
| 472 | return fRenderTarget; |
| 473 | } |
| 474 | |
| 475 | /////////////////////////////////////////////////////////////////////////////// |
| 476 | |
| 477 | SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch); |
| 478 | SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch); |
| 479 | SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch); |
| 480 | SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch); |
| 481 | SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4, |
| 482 | shader_type_mismatch); |
| 483 | SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5, |
| 484 | shader_type_mismatch); |
| 485 | SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch); |
| 486 | SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch); |
| 487 | |
| 488 | namespace { |
| 489 | |
| 490 | // converts a SkPaint to a GrPaint, ignoring the skPaint's shader |
| 491 | // justAlpha indicates that skPaint's alpha should be used rather than the color |
| 492 | // Callers may subsequently modify the GrPaint. Setting constantColor indicates |
| 493 | // that the final paint will draw the same color at every pixel. This allows |
| 494 | // an optimization where the the color filter can be applied to the skPaint's |
| 495 | // color once while converting to GrPaint and then ignored. |
| 496 | inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev, |
| 497 | const SkPaint& skPaint, |
| 498 | bool justAlpha, |
| 499 | bool constantColor, |
| 500 | GrPaint* grPaint) { |
| 501 | |
| 502 | grPaint->setDither(skPaint.isDither()); |
| 503 | grPaint->setAntiAlias(skPaint.isAntiAlias()); |
| 504 | |
| 505 | SkXfermode::Coeff sm; |
| 506 | SkXfermode::Coeff dm; |
| 507 | |
| 508 | SkXfermode* mode = skPaint.getXfermode(); |
| 509 | GrEffectRef* xferEffect = NULL; |
| 510 | if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) { |
| 511 | if (NULL != xferEffect) { |
| 512 | grPaint->addColorEffect(xferEffect)->unref(); |
| 513 | sm = SkXfermode::kOne_Coeff; |
| 514 | dm = SkXfermode::kZero_Coeff; |
| 515 | } |
| 516 | } else { |
| 517 | //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");) |
| 518 | #if 0 |
| 519 | return false; |
| 520 | #else |
| 521 | // Fall back to src-over |
| 522 | sm = SkXfermode::kOne_Coeff; |
| 523 | dm = SkXfermode::kISA_Coeff; |
| 524 | #endif |
| 525 | } |
| 526 | grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm)); |
| 527 | |
| 528 | if (justAlpha) { |
| 529 | uint8_t alpha = skPaint.getAlpha(); |
| 530 | grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha)); |
| 531 | // justAlpha is currently set to true only if there is a texture, |
| 532 | // so constantColor should not also be true. |
| 533 | SkASSERT(!constantColor); |
| 534 | } else { |
| 535 | grPaint->setColor(SkColor2GrColor(skPaint.getColor())); |
| 536 | } |
| 537 | |
| 538 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 539 | if (NULL != colorFilter) { |
| 540 | // if the source color is a constant then apply the filter here once rather than per pixel |
| 541 | // in a shader. |
| 542 | if (constantColor) { |
| 543 | SkColor filtered = colorFilter->filterColor(skPaint.getColor()); |
| 544 | grPaint->setColor(SkColor2GrColor(filtered)); |
| 545 | } else { |
| 546 | SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context())); |
| 547 | if (NULL != effect.get()) { |
| 548 | grPaint->addColorEffect(effect); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | // This function is similar to skPaint2GrPaintNoShader but also converts |
| 557 | // skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to |
| 558 | // be used is set on grPaint and returned in param act. constantColor has the |
| 559 | // same meaning as in skPaint2GrPaintNoShader. |
| 560 | inline bool skPaint2GrPaintShader(SkGpuDevice* dev, |
| 561 | const SkPaint& skPaint, |
| 562 | bool constantColor, |
| 563 | GrPaint* grPaint) { |
| 564 | SkShader* shader = skPaint.getShader(); |
| 565 | if (NULL == shader) { |
| 566 | return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint); |
| 567 | } |
| 568 | |
| commit-bot@chromium.org | 6077057 | 2014-01-13 15:57:05 +0000 | [diff] [blame] | 569 | // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require |
| 570 | // the shader to set a render target . |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 571 | GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 572 | |
| 573 | // setup the shader as the first color effect on the paint |
| 574 | SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint)); |
| 575 | if (NULL != effect.get()) { |
| 576 | grPaint->addColorEffect(effect); |
| 577 | // Now setup the rest of the paint. |
| 578 | return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint); |
| 579 | } else { |
| 580 | // We still don't have SkColorShader::asNewEffect() implemented. |
| 581 | SkShader::GradientInfo info; |
| 582 | SkColor color; |
| 583 | |
| 584 | info.fColors = &color; |
| 585 | info.fColorOffsets = NULL; |
| 586 | info.fColorCount = 1; |
| 587 | if (SkShader::kColor_GradientType == shader->asAGradient(&info)) { |
| 588 | SkPaint copy(skPaint); |
| 589 | copy.setShader(NULL); |
| 590 | // modulate the paint alpha by the shader's solid color alpha |
| 591 | U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); |
| 592 | copy.setColor(SkColorSetA(color, newA)); |
| 593 | return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint); |
| 594 | } else { |
| 595 | return false; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /////////////////////////////////////////////////////////////////////////////// |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 602 | |
| 603 | SkBitmap::Config SkGpuDevice::config() const { |
| 604 | if (NULL == fRenderTarget) { |
| 605 | return SkBitmap::kNo_Config; |
| 606 | } |
| 607 | |
| 608 | bool isOpaque; |
| 609 | return grConfig2skConfig(fRenderTarget->config(), &isOpaque); |
| 610 | } |
| 611 | |
| 612 | void SkGpuDevice::clear(SkColor color) { |
| 613 | SkIRect rect = SkIRect::MakeWH(this->width(), this->height()); |
| 614 | fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget); |
| 615 | fNeedClear = false; |
| 616 | } |
| 617 | |
| 618 | void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 619 | CHECK_SHOULD_DRAW(draw, false); |
| 620 | |
| 621 | GrPaint grPaint; |
| 622 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | fContext->drawPaint(grPaint); |
| 627 | } |
| 628 | |
| 629 | // must be in SkCanvas::PointMode order |
| 630 | static const GrPrimitiveType gPointMode2PrimtiveType[] = { |
| 631 | kPoints_GrPrimitiveType, |
| 632 | kLines_GrPrimitiveType, |
| 633 | kLineStrip_GrPrimitiveType |
| 634 | }; |
| 635 | |
| 636 | void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| 637 | size_t count, const SkPoint pts[], const SkPaint& paint) { |
| 638 | CHECK_FOR_ANNOTATION(paint); |
| 639 | CHECK_SHOULD_DRAW(draw, false); |
| 640 | |
| 641 | SkScalar width = paint.getStrokeWidth(); |
| 642 | if (width < 0) { |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | // we only handle hairlines and paints without path effects or mask filters, |
| 647 | // else we let the SkDraw call our drawPath() |
| 648 | if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) { |
| 649 | draw.drawPoints(mode, count, pts, paint, true); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | GrPaint grPaint; |
| 654 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | fContext->drawVertices(grPaint, |
| 659 | gPointMode2PrimtiveType[mode], |
| robertphillips@google.com | a466286 | 2013-11-21 14:24:16 +0000 | [diff] [blame] | 660 | SkToS32(count), |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 661 | (GrPoint*)pts, |
| 662 | NULL, |
| 663 | NULL, |
| 664 | NULL, |
| 665 | 0); |
| 666 | } |
| 667 | |
| 668 | /////////////////////////////////////////////////////////////////////////////// |
| 669 | |
| 670 | void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| 671 | const SkPaint& paint) { |
| 672 | CHECK_FOR_ANNOTATION(paint); |
| 673 | CHECK_SHOULD_DRAW(draw, false); |
| 674 | |
| 675 | bool doStroke = paint.getStyle() != SkPaint::kFill_Style; |
| 676 | SkScalar width = paint.getStrokeWidth(); |
| 677 | |
| 678 | /* |
| 679 | We have special code for hairline strokes, miter-strokes, bevel-stroke |
| 680 | and fills. Anything else we just call our path code. |
| 681 | */ |
| 682 | bool usePath = doStroke && width > 0 && |
| 683 | (paint.getStrokeJoin() == SkPaint::kRound_Join || |
| 684 | (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty())); |
| 685 | // another two reasons we might need to call drawPath... |
| 686 | if (paint.getMaskFilter() || paint.getPathEffect()) { |
| 687 | usePath = true; |
| 688 | } |
| 689 | if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) { |
| 690 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
| 691 | if (doStroke) { |
| 692 | #endif |
| 693 | usePath = true; |
| 694 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
| 695 | } else { |
| 696 | usePath = !fContext->getMatrix().preservesRightAngles(); |
| 697 | } |
| 698 | #endif |
| 699 | } |
| 700 | // until we can both stroke and fill rectangles |
| 701 | if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 702 | usePath = true; |
| 703 | } |
| 704 | |
| 705 | if (usePath) { |
| 706 | SkPath path; |
| 707 | path.addRect(rect); |
| 708 | this->drawPath(draw, path, paint, NULL, true); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | GrPaint grPaint; |
| 713 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | if (!doStroke) { |
| 718 | fContext->drawRect(grPaint, rect); |
| 719 | } else { |
| 720 | SkStrokeRec stroke(paint); |
| 721 | fContext->drawRect(grPaint, rect, &stroke); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /////////////////////////////////////////////////////////////////////////////// |
| 726 | |
| 727 | void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect, |
| 728 | const SkPaint& paint) { |
| 729 | CHECK_FOR_ANNOTATION(paint); |
| 730 | CHECK_SHOULD_DRAW(draw, false); |
| 731 | |
| commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 732 | GrPaint grPaint; |
| 733 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | SkStrokeRec stroke(paint); |
| 738 | if (paint.getMaskFilter()) { |
| 739 | // try to hit the fast path for drawing filtered round rects |
| 740 | |
| 741 | SkRRect devRRect; |
| 742 | if (rect.transform(fContext->getMatrix(), &devRRect)) { |
| 743 | if (devRRect.allCornersCircular()) { |
| 744 | SkRect maskRect; |
| 745 | if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(), |
| 746 | draw.fClip->getBounds(), |
| 747 | fContext->getMatrix(), |
| 748 | &maskRect)) { |
| 749 | SkIRect finalIRect; |
| 750 | maskRect.roundOut(&finalIRect); |
| 751 | if (draw.fClip->quickReject(finalIRect)) { |
| 752 | // clipped out |
| 753 | return; |
| 754 | } |
| 755 | if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) { |
| 756 | // nothing to draw |
| 757 | return; |
| 758 | } |
| 759 | if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint, |
| 760 | stroke, devRRect)) { |
| 761 | return; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | } |
| 769 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 770 | bool usePath = !rect.isSimple(); |
| 771 | // another two reasons we might need to call drawPath... |
| 772 | if (paint.getMaskFilter() || paint.getPathEffect()) { |
| 773 | usePath = true; |
| 774 | } |
| 775 | // until we can rotate rrects... |
| 776 | if (!usePath && !fContext->getMatrix().rectStaysRect()) { |
| 777 | usePath = true; |
| 778 | } |
| 779 | |
| 780 | if (usePath) { |
| 781 | SkPath path; |
| 782 | path.addRRect(rect); |
| 783 | this->drawPath(draw, path, paint, NULL, true); |
| 784 | return; |
| 785 | } |
| 786 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 787 | fContext->drawRRect(grPaint, rect, stroke); |
| 788 | } |
| 789 | |
| commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 790 | ///////////////////////////////////////////////////////////////////////////// |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 791 | |
| 792 | void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, |
| 793 | const SkPaint& paint) { |
| 794 | CHECK_FOR_ANNOTATION(paint); |
| 795 | CHECK_SHOULD_DRAW(draw, false); |
| 796 | |
| 797 | bool usePath = false; |
| 798 | // some basic reasons we might need to call drawPath... |
| 799 | if (paint.getMaskFilter() || paint.getPathEffect()) { |
| 800 | usePath = true; |
| 801 | } |
| 802 | |
| 803 | if (usePath) { |
| 804 | SkPath path; |
| 805 | path.addOval(oval); |
| 806 | this->drawPath(draw, path, paint, NULL, true); |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | GrPaint grPaint; |
| 811 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 812 | return; |
| 813 | } |
| 814 | SkStrokeRec stroke(paint); |
| 815 | |
| 816 | fContext->drawOval(grPaint, oval, stroke); |
| 817 | } |
| 818 | |
| 819 | #include "SkMaskFilter.h" |
| 820 | #include "SkBounder.h" |
| 821 | |
| 822 | /////////////////////////////////////////////////////////////////////////////// |
| 823 | |
| 824 | // helpers for applying mask filters |
| 825 | namespace { |
| 826 | |
| 827 | // Draw a mask using the supplied paint. Since the coverage/geometry |
| 828 | // is already burnt into the mask this boils down to a rect draw. |
| 829 | // Return true if the mask was successfully drawn. |
| 830 | bool draw_mask(GrContext* context, const SkRect& maskRect, |
| 831 | GrPaint* grp, GrTexture* mask) { |
| 832 | GrContext::AutoMatrix am; |
| 833 | if (!am.setIdentity(context, grp)) { |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | SkMatrix matrix; |
| 838 | matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
| 839 | matrix.postIDiv(mask->width(), mask->height()); |
| 840 | |
| 841 | grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref(); |
| 842 | context->drawRect(*grp, maskRect); |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | bool draw_with_mask_filter(GrContext* context, const SkPath& devPath, |
| 847 | SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder, |
| 848 | GrPaint* grp, SkPaint::Style style) { |
| 849 | SkMask srcM, dstM; |
| 850 | |
| 851 | if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM, |
| 852 | SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) { |
| 853 | return false; |
| 854 | } |
| 855 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
| 856 | |
| 857 | if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) { |
| 858 | return false; |
| 859 | } |
| 860 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 861 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
| 862 | |
| 863 | if (clip.quickReject(dstM.fBounds)) { |
| 864 | return false; |
| 865 | } |
| 866 | if (bounder && !bounder->doIRect(dstM.fBounds)) { |
| 867 | return false; |
| 868 | } |
| 869 | |
| 870 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 871 | // the current clip (and identity matrix) and GrPaint settings |
| 872 | GrTextureDesc desc; |
| 873 | desc.fWidth = dstM.fBounds.width(); |
| 874 | desc.fHeight = dstM.fBounds.height(); |
| 875 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 876 | |
| 877 | GrAutoScratchTexture ast(context, desc); |
| 878 | GrTexture* texture = ast.texture(); |
| 879 | |
| 880 | if (NULL == texture) { |
| 881 | return false; |
| 882 | } |
| 883 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 884 | dstM.fImage, dstM.fRowBytes); |
| 885 | |
| 886 | SkRect maskRect = SkRect::Make(dstM.fBounds); |
| 887 | |
| 888 | return draw_mask(context, maskRect, grp, texture); |
| 889 | } |
| 890 | |
| 891 | // Create a mask of 'devPath' and place the result in 'mask'. Return true on |
| 892 | // success; false otherwise. |
| 893 | bool create_mask_GPU(GrContext* context, |
| 894 | const SkRect& maskRect, |
| 895 | const SkPath& devPath, |
| 896 | const SkStrokeRec& stroke, |
| 897 | bool doAA, |
| 898 | GrAutoScratchTexture* mask) { |
| 899 | GrTextureDesc desc; |
| 900 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 901 | desc.fWidth = SkScalarCeilToInt(maskRect.width()); |
| 902 | desc.fHeight = SkScalarCeilToInt(maskRect.height()); |
| 903 | // We actually only need A8, but it often isn't supported as a |
| 904 | // render target so default to RGBA_8888 |
| 905 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 906 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 907 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 908 | } |
| 909 | |
| 910 | mask->set(context, desc); |
| 911 | if (NULL == mask->texture()) { |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | GrTexture* maskTexture = mask->texture(); |
| 916 | SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height()); |
| 917 | |
| 918 | GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget()); |
| 919 | GrContext::AutoClip ac(context, clipRect); |
| 920 | |
| 921 | context->clear(NULL, 0x0, true); |
| 922 | |
| 923 | GrPaint tempPaint; |
| 924 | if (doAA) { |
| 925 | tempPaint.setAntiAlias(true); |
| 926 | // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst |
| 927 | // blend coeff of zero requires dual source blending support in order |
| 928 | // to properly blend partially covered pixels. This means the AA |
| 929 | // code path may not be taken. So we use a dst blend coeff of ISA. We |
| 930 | // could special case AA draws to a dst surface with known alpha=0 to |
| 931 | // use a zero dst coeff when dual source blending isn't available. |
| 932 | tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff); |
| 933 | } |
| 934 | |
| 935 | GrContext::AutoMatrix am; |
| 936 | |
| 937 | // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint. |
| 938 | SkMatrix translate; |
| 939 | translate.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
| 940 | am.set(context, translate); |
| 941 | context->drawPath(tempPaint, devPath, stroke); |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | SkBitmap wrap_texture(GrTexture* texture) { |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 946 | SkImageInfo info; |
| 947 | texture->asImageInfo(&info); |
| 948 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 949 | SkBitmap result; |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 950 | result.setConfig(info); |
| 951 | result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 952 | return result; |
| 953 | } |
| 954 | |
| 955 | }; |
| 956 | |
| 957 | void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath, |
| 958 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 959 | bool pathIsMutable) { |
| 960 | CHECK_FOR_ANNOTATION(paint); |
| 961 | CHECK_SHOULD_DRAW(draw, false); |
| 962 | |
| 963 | GrPaint grPaint; |
| 964 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 965 | return; |
| 966 | } |
| 967 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 968 | // If we have a prematrix, apply it to the path, optimizing for the case |
| 969 | // where the original path can in fact be modified in place (even though |
| 970 | // its parameter type is const). |
| 971 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 972 | SkTLazy<SkPath> tmpPath; |
| 973 | SkTLazy<SkPath> effectPath; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 974 | |
| 975 | if (prePathMatrix) { |
| 976 | SkPath* result = pathPtr; |
| 977 | |
| 978 | if (!pathIsMutable) { |
| commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 979 | result = tmpPath.init(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 980 | pathIsMutable = true; |
| 981 | } |
| 982 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 983 | // of applying it here? See SkDraw.cpp |
| 984 | pathPtr->transform(*prePathMatrix, result); |
| 985 | pathPtr = result; |
| 986 | } |
| 987 | // at this point we're done with prePathMatrix |
| 988 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
| 989 | |
| 990 | SkStrokeRec stroke(paint); |
| 991 | SkPathEffect* pathEffect = paint.getPathEffect(); |
| 992 | const SkRect* cullRect = NULL; // TODO: what is our bounds? |
| commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 993 | if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 994 | cullRect)) { |
| commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 995 | pathPtr = effectPath.get(); |
| 996 | pathIsMutable = true; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 999 | if (paint.getMaskFilter()) { |
| 1000 | if (!stroke.isHairlineStyle()) { |
| commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1001 | SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init(); |
| 1002 | if (stroke.applyToPath(strokedPath, *pathPtr)) { |
| 1003 | pathPtr = strokedPath; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1004 | pathIsMutable = true; |
| 1005 | stroke.setFillStyle(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // avoid possibly allocating a new path in transform if we can |
| commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1010 | SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1011 | |
| 1012 | // transform the path into device space |
| 1013 | pathPtr->transform(fContext->getMatrix(), devPathPtr); |
| skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1014 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1015 | SkRect maskRect; |
| 1016 | if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(), |
| 1017 | draw.fClip->getBounds(), |
| 1018 | fContext->getMatrix(), |
| 1019 | &maskRect)) { |
| commit-bot@chromium.org | 439ff1b | 2014-01-13 16:39:39 +0000 | [diff] [blame] | 1020 | // The context's matrix may change while creating the mask, so save the CTM here to |
| 1021 | // pass to filterMaskGPU. |
| 1022 | const SkMatrix ctm = fContext->getMatrix(); |
| 1023 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1024 | SkIRect finalIRect; |
| 1025 | maskRect.roundOut(&finalIRect); |
| 1026 | if (draw.fClip->quickReject(finalIRect)) { |
| 1027 | // clipped out |
| 1028 | return; |
| 1029 | } |
| 1030 | if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) { |
| 1031 | // nothing to draw |
| 1032 | return; |
| 1033 | } |
| skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1034 | |
| commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 1035 | if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint, |
| commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 1036 | stroke, *devPathPtr)) { |
| commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 1037 | // the mask filter was able to draw itself directly, so there's nothing |
| 1038 | // left to do. |
| 1039 | return; |
| 1040 | } |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1041 | |
| 1042 | GrAutoScratchTexture mask; |
| 1043 | |
| 1044 | if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke, |
| 1045 | grPaint.isAntiAlias(), &mask)) { |
| 1046 | GrTexture* filtered; |
| 1047 | |
| commit-bot@chromium.org | 41bf930 | 2014-01-08 22:25:53 +0000 | [diff] [blame] | 1048 | if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), |
| commit-bot@chromium.org | 439ff1b | 2014-01-13 16:39:39 +0000 | [diff] [blame] | 1049 | ctm, maskRect, &filtered, true)) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1050 | // filterMaskGPU gives us ownership of a ref to the result |
| 1051 | SkAutoTUnref<GrTexture> atu(filtered); |
| 1052 | |
| 1053 | // If the scratch texture that we used as the filter src also holds the filter |
| 1054 | // result then we must detach so that this texture isn't recycled for a later |
| 1055 | // draw. |
| 1056 | if (filtered == mask.texture()) { |
| 1057 | mask.detach(); |
| 1058 | filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us. |
| 1059 | } |
| 1060 | |
| 1061 | if (draw_mask(fContext, maskRect, &grPaint, filtered)) { |
| 1062 | // This path is completely drawn |
| 1063 | return; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | // draw the mask on the CPU - this is a fallthrough path in case the |
| 1070 | // GPU path fails |
| 1071 | SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style : |
| 1072 | SkPaint::kFill_Style; |
| 1073 | draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(), |
| 1074 | *draw.fClip, draw.fBounder, &grPaint, style); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | fContext->drawPath(grPaint, *pathPtr, stroke); |
| 1079 | } |
| 1080 | |
| 1081 | static const int kBmpSmallTileSize = 1 << 10; |
| 1082 | |
| 1083 | static inline int get_tile_count(const SkIRect& srcRect, int tileSize) { |
| 1084 | int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1; |
| 1085 | int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1; |
| 1086 | return tilesX * tilesY; |
| 1087 | } |
| 1088 | |
| 1089 | static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) { |
| 1090 | if (maxTileSize <= kBmpSmallTileSize) { |
| 1091 | return maxTileSize; |
| 1092 | } |
| 1093 | |
| 1094 | size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize); |
| 1095 | size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize); |
| 1096 | |
| 1097 | maxTileTotalTileSize *= maxTileSize * maxTileSize; |
| 1098 | smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize; |
| 1099 | |
| 1100 | if (maxTileTotalTileSize > 2 * smallTotalTileSize) { |
| 1101 | return kBmpSmallTileSize; |
| 1102 | } else { |
| 1103 | return maxTileSize; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what |
| 1108 | // pixels from the bitmap are necessary. |
| 1109 | static void determine_clipped_src_rect(const GrContext* context, |
| 1110 | const SkBitmap& bitmap, |
| 1111 | const SkRect* srcRectPtr, |
| 1112 | SkIRect* clippedSrcIRect) { |
| 1113 | const GrClipData* clip = context->getClip(); |
| 1114 | clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL); |
| 1115 | SkMatrix inv; |
| 1116 | if (!context->getMatrix().invert(&inv)) { |
| 1117 | clippedSrcIRect->setEmpty(); |
| 1118 | return; |
| 1119 | } |
| 1120 | SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect); |
| 1121 | inv.mapRect(&clippedSrcRect); |
| 1122 | if (NULL != srcRectPtr) { |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1123 | // we've setup src space 0,0 to map to the top left of the src rect. |
| 1124 | clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1125 | if (!clippedSrcRect.intersect(*srcRectPtr)) { |
| 1126 | clippedSrcIRect->setEmpty(); |
| 1127 | return; |
| 1128 | } |
| 1129 | } |
| 1130 | clippedSrcRect.roundOut(clippedSrcIRect); |
| 1131 | SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 1132 | if (!clippedSrcIRect->intersect(bmpBounds)) { |
| 1133 | clippedSrcIRect->setEmpty(); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap, |
| 1138 | const GrTextureParams& params, |
| 1139 | const SkRect* srcRectPtr, |
| 1140 | int maxTileSize, |
| 1141 | int* tileSize, |
| 1142 | SkIRect* clippedSrcRect) const { |
| 1143 | // if bitmap is explictly texture backed then just use the texture |
| 1144 | if (NULL != bitmap.getTexture()) { |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
| 1148 | // if it's larger than the max tile size, then we have no choice but tiling. |
| 1149 | if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) { |
| 1150 | determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect); |
| 1151 | *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize); |
| 1152 | return true; |
| 1153 | } |
| 1154 | |
| 1155 | if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) { |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
| 1159 | // if the entire texture is already in our cache then no reason to tile it |
| 1160 | if (GrIsBitmapInCache(fContext, bitmap, ¶ms)) { |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
| 1164 | // At this point we know we could do the draw by uploading the entire bitmap |
| 1165 | // as a texture. However, if the texture would be large compared to the |
| 1166 | // cache size and we don't require most of it for this draw then tile to |
| 1167 | // reduce the amount of upload and cache spill. |
| 1168 | |
| 1169 | // assumption here is that sw bitmap size is a good proxy for its size as |
| 1170 | // a texture |
| 1171 | size_t bmpSize = bitmap.getSize(); |
| 1172 | size_t cacheSize; |
| 1173 | fContext->getTextureCacheLimits(NULL, &cacheSize); |
| 1174 | if (bmpSize < cacheSize / 2) { |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
| 1178 | // Figure out how much of the src we will need based on the src rect and clipping. |
| 1179 | determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect); |
| 1180 | *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile. |
| 1181 | size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) * |
| 1182 | kBmpSmallTileSize * kBmpSmallTileSize; |
| 1183 | |
| 1184 | return usedTileBytes < 2 * bmpSize; |
| 1185 | } |
| 1186 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1187 | void SkGpuDevice::drawBitmap(const SkDraw& origDraw, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1188 | const SkBitmap& bitmap, |
| 1189 | const SkMatrix& m, |
| 1190 | const SkPaint& paint) { |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1191 | SkMatrix concat; |
| 1192 | SkTCopyOnFirstWrite<SkDraw> draw(origDraw); |
| 1193 | if (!m.isIdentity()) { |
| 1194 | concat.setConcat(*draw->fMatrix, m); |
| 1195 | draw.writable()->fMatrix = &concat; |
| 1196 | } |
| 1197 | this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1200 | // This method outsets 'iRect' by 'outset' all around and then clamps its extents to |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1201 | // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner |
| 1202 | // of 'iRect' for all possible outsets/clamps. |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1203 | static inline void clamped_outset_with_offset(SkIRect* iRect, |
| 1204 | int outset, |
| 1205 | SkPoint* offset, |
| 1206 | const SkIRect& clamp) { |
| 1207 | iRect->outset(outset, outset); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1208 | |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1209 | int leftClampDelta = clamp.fLeft - iRect->fLeft; |
| 1210 | if (leftClampDelta > 0) { |
| 1211 | offset->fX -= outset - leftClampDelta; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1212 | iRect->fLeft = clamp.fLeft; |
| 1213 | } else { |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1214 | offset->fX -= outset; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1215 | } |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1216 | |
| 1217 | int topClampDelta = clamp.fTop - iRect->fTop; |
| 1218 | if (topClampDelta > 0) { |
| 1219 | offset->fY -= outset - topClampDelta; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1220 | iRect->fTop = clamp.fTop; |
| 1221 | } else { |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1222 | offset->fY -= outset; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | if (iRect->fRight > clamp.fRight) { |
| 1226 | iRect->fRight = clamp.fRight; |
| 1227 | } |
| 1228 | if (iRect->fBottom > clamp.fBottom) { |
| 1229 | iRect->fBottom = clamp.fBottom; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | void SkGpuDevice::drawBitmapCommon(const SkDraw& draw, |
| 1234 | const SkBitmap& bitmap, |
| 1235 | const SkRect* srcRectPtr, |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1236 | const SkSize* dstSizePtr, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1237 | const SkPaint& paint, |
| 1238 | SkCanvas::DrawBitmapRectFlags flags) { |
| 1239 | CHECK_SHOULD_DRAW(draw, false); |
| 1240 | |
| 1241 | SkRect srcRect; |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1242 | SkSize dstSize; |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1243 | // If there is no src rect, or the src rect contains the entire bitmap then we're effectively |
| 1244 | // in the (easier) bleed case, so update flags. |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1245 | if (NULL == srcRectPtr) { |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1246 | SkScalar w = SkIntToScalar(bitmap.width()); |
| 1247 | SkScalar h = SkIntToScalar(bitmap.height()); |
| 1248 | dstSize.fWidth = w; |
| 1249 | dstSize.fHeight = h; |
| 1250 | srcRect.set(0, 0, w, h); |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1251 | flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1252 | } else { |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1253 | SkASSERT(NULL != dstSizePtr); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1254 | srcRect = *srcRectPtr; |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1255 | dstSize = *dstSizePtr; |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1256 | if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 && |
| 1257 | srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) { |
| 1258 | flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag); |
| 1259 | } |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | if (paint.getMaskFilter()){ |
| 1263 | // Convert the bitmap to a shader so that the rect can be drawn |
| 1264 | // through drawRect, which supports mask filters. |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1265 | SkBitmap tmp; // subset of bitmap, if necessary |
| 1266 | const SkBitmap* bitmapPtr = &bitmap; |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1267 | SkMatrix localM; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1268 | if (NULL != srcRectPtr) { |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1269 | localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop); |
| 1270 | localM.postScale(dstSize.fWidth / srcRectPtr->width(), |
| 1271 | dstSize.fHeight / srcRectPtr->height()); |
| commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1272 | // In bleed mode we position and trim the bitmap based on the src rect which is |
| 1273 | // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out |
| 1274 | // the desired portion of the bitmap and then update 'm' and 'srcRect' to |
| 1275 | // compensate. |
| 1276 | if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) { |
| 1277 | SkIRect iSrc; |
| 1278 | srcRect.roundOut(&iSrc); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1279 | |
| commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1280 | SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft), |
| 1281 | SkIntToScalar(iSrc.fTop)); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1282 | |
| commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1283 | if (!bitmap.extractSubset(&tmp, iSrc)) { |
| 1284 | return; // extraction failed |
| 1285 | } |
| 1286 | bitmapPtr = &tmp; |
| 1287 | srcRect.offset(-offset.fX, -offset.fY); |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1288 | |
| commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 1289 | // The source rect has changed so update the matrix |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1290 | localM.preTranslate(offset.fX, offset.fY); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1291 | } |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1292 | } else { |
| 1293 | localM.reset(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1296 | SkPaint paintWithShader(paint); |
| 1297 | paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1298 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref(); |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1299 | paintWithShader.getShader()->setLocalMatrix(localM); |
| 1300 | SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight}; |
| 1301 | this->drawRect(draw, dstRect, paintWithShader); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1302 | |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1306 | // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using |
| 1307 | // the view matrix rather than a local matrix. |
| 1308 | SkMatrix m; |
| 1309 | m.setScale(dstSize.fWidth / srcRect.width(), |
| 1310 | dstSize.fHeight / srcRect.height()); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1311 | fContext->concatMatrix(m); |
| 1312 | |
| 1313 | GrTextureParams params; |
| 1314 | SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel(); |
| 1315 | GrTextureParams::FilterMode textureFilterMode; |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1316 | |
| 1317 | int tileFilterPad; |
| 1318 | bool doBicubic = false; |
| 1319 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1320 | switch(paintFilterLevel) { |
| 1321 | case SkPaint::kNone_FilterLevel: |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1322 | tileFilterPad = 0; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1323 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 1324 | break; |
| 1325 | case SkPaint::kLow_FilterLevel: |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1326 | tileFilterPad = 1; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1327 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 1328 | break; |
| 1329 | case SkPaint::kMedium_FilterLevel: |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1330 | tileFilterPad = 1; |
| commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 1331 | if (fContext->getMatrix().getMinStretch() < SK_Scalar1) { |
| 1332 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 1333 | } else { |
| 1334 | // Don't trigger MIP level generation unnecessarily. |
| 1335 | textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 1336 | } |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1337 | break; |
| commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 1338 | case SkPaint::kHigh_FilterLevel: |
| commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 1339 | // Minification can look bad with the bicubic effect. |
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1340 | if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) { |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1341 | // We will install an effect that does the filtering in the shader. |
| 1342 | textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 1343 | tileFilterPad = GrBicubicEffect::kFilterTexelPad; |
| 1344 | doBicubic = true; |
| 1345 | } else { |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1346 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 1347 | tileFilterPad = 1; |
| 1348 | } |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1349 | break; |
| 1350 | default: |
| 1351 | SkErrorInternals::SetError( kInvalidPaint_SkError, |
| 1352 | "Sorry, I don't understand the filtering " |
| 1353 | "mode you asked for. Falling back to " |
| 1354 | "MIPMaps."); |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1355 | tileFilterPad = 1; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1356 | textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 1357 | break; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | params.setFilterMode(textureFilterMode); |
| 1361 | |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1362 | int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1363 | int tileSize; |
| 1364 | |
| 1365 | SkIRect clippedSrcRect; |
| 1366 | if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize, |
| 1367 | &clippedSrcRect)) { |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1368 | this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize, |
| 1369 | doBicubic); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | // take the simple case |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1372 | this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | // Break 'bitmap' into several tiles to draw it since it has already |
| 1377 | // been determined to be too large to fit in VRAM |
| 1378 | void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap, |
| 1379 | const SkRect& srcRect, |
| 1380 | const SkIRect& clippedSrcIRect, |
| 1381 | const GrTextureParams& params, |
| 1382 | const SkPaint& paint, |
| 1383 | SkCanvas::DrawBitmapRectFlags flags, |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1384 | int tileSize, |
| 1385 | bool bicubic) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1386 | SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect); |
| 1387 | |
| 1388 | int nx = bitmap.width() / tileSize; |
| 1389 | int ny = bitmap.height() / tileSize; |
| 1390 | for (int x = 0; x <= nx; x++) { |
| 1391 | for (int y = 0; y <= ny; y++) { |
| 1392 | SkRect tileR; |
| 1393 | tileR.set(SkIntToScalar(x * tileSize), |
| 1394 | SkIntToScalar(y * tileSize), |
| 1395 | SkIntToScalar((x + 1) * tileSize), |
| 1396 | SkIntToScalar((y + 1) * tileSize)); |
| 1397 | |
| 1398 | if (!SkRect::Intersects(tileR, clippedSrcRect)) { |
| 1399 | continue; |
| 1400 | } |
| 1401 | |
| 1402 | if (!tileR.intersect(srcRect)) { |
| 1403 | continue; |
| 1404 | } |
| 1405 | |
| 1406 | SkBitmap tmpB; |
| 1407 | SkIRect iTileR; |
| 1408 | tileR.roundOut(&iTileR); |
| 1409 | SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft), |
| 1410 | SkIntToScalar(iTileR.fTop)); |
| 1411 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1412 | // Adjust the context matrix to draw at the right x,y in device space |
| 1413 | SkMatrix tmpM; |
| 1414 | GrContext::AutoMatrix am; |
| 1415 | tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop); |
| 1416 | am.setPreConcat(fContext, tmpM); |
| 1417 | |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1418 | if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1419 | SkIRect iClampRect; |
| 1420 | |
| 1421 | if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) { |
| 1422 | // In bleed mode we want to always expand the tile on all edges |
| 1423 | // but stay within the bitmap bounds |
| 1424 | iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 1425 | } else { |
| 1426 | // In texture-domain/clamp mode we only want to expand the |
| 1427 | // tile on edges interior to "srcRect" (i.e., we want to |
| 1428 | // not bleed across the original clamped edges) |
| 1429 | srcRect.roundOut(&iClampRect); |
| 1430 | } |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1431 | int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1; |
| 1432 | clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | if (bitmap.extractSubset(&tmpB, iTileR)) { |
| 1436 | // now offset it to make it "local" to our tmp bitmap |
| 1437 | tileR.offset(-offset.fX, -offset.fY); |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1438 | |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1439 | this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | static bool has_aligned_samples(const SkRect& srcRect, |
| 1446 | const SkRect& transformedRect) { |
| 1447 | // detect pixel disalignment |
| 1448 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - |
| 1449 | transformedRect.left()) < COLOR_BLEED_TOLERANCE && |
| 1450 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - |
| 1451 | transformedRect.top()) < COLOR_BLEED_TOLERANCE && |
| 1452 | SkScalarAbs(transformedRect.width() - srcRect.width()) < |
| 1453 | COLOR_BLEED_TOLERANCE && |
| 1454 | SkScalarAbs(transformedRect.height() - srcRect.height()) < |
| 1455 | COLOR_BLEED_TOLERANCE) { |
| 1456 | return true; |
| 1457 | } |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | static bool may_color_bleed(const SkRect& srcRect, |
| 1462 | const SkRect& transformedRect, |
| 1463 | const SkMatrix& m) { |
| 1464 | // Only gets called if has_aligned_samples returned false. |
| 1465 | // So we can assume that sampling is axis aligned but not texel aligned. |
| 1466 | SkASSERT(!has_aligned_samples(srcRect, transformedRect)); |
| 1467 | SkRect innerSrcRect(srcRect), innerTransformedRect, |
| 1468 | outerTransformedRect(transformedRect); |
| 1469 | innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 1470 | m.mapRect(&innerTransformedRect, innerSrcRect); |
| 1471 | |
| 1472 | // The gap between outerTransformedRect and innerTransformedRect |
| 1473 | // represents the projection of the source border area, which is |
| 1474 | // problematic for color bleeding. We must check whether any |
| 1475 | // destination pixels sample the border area. |
| 1476 | outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 1477 | innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE); |
| 1478 | SkIRect outer, inner; |
| 1479 | outerTransformedRect.round(&outer); |
| 1480 | innerTransformedRect.round(&inner); |
| 1481 | // If the inner and outer rects round to the same result, it means the |
| 1482 | // border does not overlap any pixel centers. Yay! |
| 1483 | return inner != outer; |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | /* |
| 1488 | * This is called by drawBitmap(), which has to handle images that may be too |
| 1489 | * large to be represented by a single texture. |
| 1490 | * |
| 1491 | * internalDrawBitmap assumes that the specified bitmap will fit in a texture |
| 1492 | * and that non-texture portion of the GrPaint has already been setup. |
| 1493 | */ |
| 1494 | void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap, |
| 1495 | const SkRect& srcRect, |
| 1496 | const GrTextureParams& params, |
| 1497 | const SkPaint& paint, |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1498 | SkCanvas::DrawBitmapRectFlags flags, |
| 1499 | bool bicubic) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1500 | SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() && |
| 1501 | bitmap.height() <= fContext->getMaxTextureSize()); |
| 1502 | |
| 1503 | GrTexture* texture; |
| 1504 | SkAutoCachedTexture act(this, bitmap, ¶ms, &texture); |
| 1505 | if (NULL == texture) { |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1509 | SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() }; |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1510 | SkRect paintRect; |
| 1511 | SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width())); |
| 1512 | SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height())); |
| 1513 | paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv), |
| 1514 | SkScalarMul(srcRect.fTop, hInv), |
| 1515 | SkScalarMul(srcRect.fRight, wInv), |
| 1516 | SkScalarMul(srcRect.fBottom, hInv)); |
| 1517 | |
| 1518 | bool needsTextureDomain = false; |
| 1519 | if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) && |
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1520 | (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) { |
| 1521 | // Need texture domain if drawing a sub rect |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1522 | needsTextureDomain = srcRect.width() < bitmap.width() || |
| 1523 | srcRect.height() < bitmap.height(); |
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1524 | if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1525 | const SkMatrix& matrix = fContext->getMatrix(); |
| 1526 | // sampling is axis-aligned |
| 1527 | SkRect transformedRect; |
| 1528 | matrix.mapRect(&transformedRect, srcRect); |
| 1529 | |
| 1530 | if (has_aligned_samples(srcRect, transformedRect)) { |
| 1531 | // We could also turn off filtering here (but we already did a cache lookup with |
| 1532 | // params). |
| 1533 | needsTextureDomain = false; |
| 1534 | } else { |
| 1535 | needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix); |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | SkRect textureDomain = SkRect::MakeEmpty(); |
| 1541 | SkAutoTUnref<GrEffectRef> effect; |
| 1542 | if (needsTextureDomain) { |
| 1543 | // Use a constrained texture domain to avoid color bleeding |
| 1544 | SkScalar left, top, right, bottom; |
| 1545 | if (srcRect.width() > SK_Scalar1) { |
| 1546 | SkScalar border = SK_ScalarHalf / texture->width(); |
| 1547 | left = paintRect.left() + border; |
| 1548 | right = paintRect.right() - border; |
| 1549 | } else { |
| 1550 | left = right = SkScalarHalf(paintRect.left() + paintRect.right()); |
| 1551 | } |
| 1552 | if (srcRect.height() > SK_Scalar1) { |
| 1553 | SkScalar border = SK_ScalarHalf / texture->height(); |
| 1554 | top = paintRect.top() + border; |
| 1555 | bottom = paintRect.bottom() - border; |
| 1556 | } else { |
| 1557 | top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom()); |
| 1558 | } |
| 1559 | textureDomain.setLTRB(left, top, right, bottom); |
| commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 1560 | if (bicubic) { |
| 1561 | effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain)); |
| 1562 | } else { |
| 1563 | effect.reset(GrTextureDomainEffect::Create(texture, |
| 1564 | SkMatrix::I(), |
| 1565 | textureDomain, |
| 1566 | GrTextureDomain::kClamp_Mode, |
| 1567 | params.filterMode())); |
| 1568 | } |
| commit-bot@chromium.org | dec6150 | 2013-12-02 22:22:35 +0000 | [diff] [blame] | 1569 | } else if (bicubic) { |
| commit-bot@chromium.org | bc91fd7 | 2013-12-10 12:53:39 +0000 | [diff] [blame] | 1570 | SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode()); |
| 1571 | SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() }; |
| 1572 | effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes)); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1573 | } else { |
| 1574 | effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params)); |
| 1575 | } |
| 1576 | |
| 1577 | // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring |
| 1578 | // the rest from the SkPaint. |
| 1579 | GrPaint grPaint; |
| 1580 | grPaint.addColorEffect(effect); |
| 1581 | bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config()); |
| 1582 | if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) { |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL); |
| 1587 | } |
| 1588 | |
| 1589 | static bool filter_texture(SkBaseDevice* device, GrContext* context, |
| commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1590 | GrTexture* texture, const SkImageFilter* filter, |
| senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1591 | int w, int h, const SkImageFilter::Context& ctx, |
| 1592 | SkBitmap* result, SkIPoint* offset) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1593 | SkASSERT(filter); |
| 1594 | SkDeviceImageFilterProxy proxy(device); |
| 1595 | |
| 1596 | if (filter->canFilterImageGPU()) { |
| 1597 | // Save the render target and set it to NULL, so we don't accidentally draw to it in the |
| 1598 | // filter. Also set the clip wide open and the matrix to identity. |
| 1599 | GrContext::AutoWideOpenIdentityDraw awo(context, NULL); |
| senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1600 | return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1601 | } else { |
| 1602 | return false; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, |
| 1607 | int left, int top, const SkPaint& paint) { |
| 1608 | // drawSprite is defined to be in device coords. |
| 1609 | CHECK_SHOULD_DRAW(draw, true); |
| 1610 | |
| 1611 | SkAutoLockPixels alp(bitmap, !bitmap.getTexture()); |
| 1612 | if (!bitmap.getTexture() && !bitmap.readyToDraw()) { |
| 1613 | return; |
| 1614 | } |
| 1615 | |
| 1616 | int w = bitmap.width(); |
| 1617 | int h = bitmap.height(); |
| 1618 | |
| 1619 | GrTexture* texture; |
| 1620 | // draw sprite uses the default texture params |
| 1621 | SkAutoCachedTexture act(this, bitmap, NULL, &texture); |
| 1622 | |
| 1623 | SkImageFilter* filter = paint.getImageFilter(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1624 | // This bitmap will own the filtered result as a texture. |
| 1625 | SkBitmap filteredBitmap; |
| 1626 | |
| 1627 | if (NULL != filter) { |
| senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1628 | SkIPoint offset = SkIPoint::Make(0, 0); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1629 | SkMatrix matrix(*draw.fMatrix); |
| 1630 | matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); |
| senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1631 | SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 1632 | SkImageFilter::Context ctx(matrix, clipBounds); |
| 1633 | if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1634 | &offset)) { |
| 1635 | texture = (GrTexture*) filteredBitmap.getTexture(); |
| 1636 | w = filteredBitmap.width(); |
| 1637 | h = filteredBitmap.height(); |
| senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1638 | left += offset.x(); |
| 1639 | top += offset.y(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1640 | } else { |
| 1641 | return; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | GrPaint grPaint; |
| 1646 | grPaint.addColorTextureEffect(texture, SkMatrix::I()); |
| 1647 | |
| 1648 | if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) { |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | fContext->drawRectToRect(grPaint, |
| senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1653 | SkRect::MakeXYWH(SkIntToScalar(left), |
| 1654 | SkIntToScalar(top), |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1655 | SkIntToScalar(w), |
| 1656 | SkIntToScalar(h)), |
| 1657 | SkRect::MakeXYWH(0, |
| 1658 | 0, |
| 1659 | SK_Scalar1 * w / texture->width(), |
| 1660 | SK_Scalar1 * h / texture->height())); |
| 1661 | } |
| 1662 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1663 | void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1664 | const SkRect* src, const SkRect& dst, |
| 1665 | const SkPaint& paint, |
| 1666 | SkCanvas::DrawBitmapRectFlags flags) { |
| 1667 | SkMatrix matrix; |
| 1668 | SkRect bitmapBounds, tmpSrc; |
| 1669 | |
| 1670 | bitmapBounds.set(0, 0, |
| 1671 | SkIntToScalar(bitmap.width()), |
| 1672 | SkIntToScalar(bitmap.height())); |
| 1673 | |
| 1674 | // Compute matrix from the two rectangles |
| 1675 | if (NULL != src) { |
| 1676 | tmpSrc = *src; |
| 1677 | } else { |
| 1678 | tmpSrc = bitmapBounds; |
| 1679 | } |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1680 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1681 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 1682 | |
| 1683 | // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null. |
| 1684 | if (NULL != src) { |
| 1685 | if (!bitmapBounds.contains(tmpSrc)) { |
| 1686 | if (!tmpSrc.intersect(bitmapBounds)) { |
| 1687 | return; // nothing to draw |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
| commit-bot@chromium.org | a7d89c8 | 2014-01-13 14:47:00 +0000 | [diff] [blame] | 1692 | SkRect tmpDst; |
| 1693 | matrix.mapRect(&tmpDst, tmpSrc); |
| 1694 | |
| 1695 | SkTCopyOnFirstWrite<SkDraw> draw(origDraw); |
| 1696 | if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) { |
| 1697 | // Translate so that tempDst's top left is at the origin. |
| 1698 | matrix = *origDraw.fMatrix; |
| 1699 | matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop); |
| 1700 | draw.writable()->fMatrix = &matrix; |
| 1701 | } |
| 1702 | SkSize dstSize; |
| 1703 | dstSize.fWidth = tmpDst.width(); |
| 1704 | dstSize.fHeight = tmpDst.height(); |
| 1705 | |
| 1706 | this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, |
| 1710 | int x, int y, const SkPaint& paint) { |
| 1711 | // clear of the source device must occur before CHECK_SHOULD_DRAW |
| 1712 | SkGpuDevice* dev = static_cast<SkGpuDevice*>(device); |
| 1713 | if (dev->fNeedClear) { |
| 1714 | // TODO: could check here whether we really need to draw at all |
| 1715 | dev->clear(0x0); |
| 1716 | } |
| 1717 | |
| 1718 | // drawDevice is defined to be in device coords. |
| 1719 | CHECK_SHOULD_DRAW(draw, true); |
| 1720 | |
| 1721 | GrRenderTarget* devRT = dev->accessRenderTarget(); |
| 1722 | GrTexture* devTex; |
| 1723 | if (NULL == (devTex = devRT->asTexture())) { |
| 1724 | return; |
| 1725 | } |
| 1726 | |
| 1727 | const SkBitmap& bm = dev->accessBitmap(false); |
| 1728 | int w = bm.width(); |
| 1729 | int h = bm.height(); |
| 1730 | |
| 1731 | SkImageFilter* filter = paint.getImageFilter(); |
| 1732 | // This bitmap will own the filtered result as a texture. |
| 1733 | SkBitmap filteredBitmap; |
| 1734 | |
| 1735 | if (NULL != filter) { |
| 1736 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 1737 | SkMatrix matrix(*draw.fMatrix); |
| 1738 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1739 | SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); |
| 1740 | SkImageFilter::Context ctx(matrix, clipBounds); |
| 1741 | if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1742 | &offset)) { |
| 1743 | devTex = filteredBitmap.getTexture(); |
| 1744 | w = filteredBitmap.width(); |
| 1745 | h = filteredBitmap.height(); |
| 1746 | x += offset.fX; |
| 1747 | y += offset.fY; |
| 1748 | } else { |
| 1749 | return; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | GrPaint grPaint; |
| 1754 | grPaint.addColorTextureEffect(devTex, SkMatrix::I()); |
| 1755 | |
| 1756 | if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) { |
| 1757 | return; |
| 1758 | } |
| 1759 | |
| 1760 | SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x), |
| 1761 | SkIntToScalar(y), |
| 1762 | SkIntToScalar(w), |
| 1763 | SkIntToScalar(h)); |
| 1764 | |
| 1765 | // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate |
| 1766 | // scratch texture). |
| 1767 | SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(), |
| 1768 | SK_Scalar1 * h / devTex->height()); |
| 1769 | |
| 1770 | fContext->drawRectToRect(grPaint, dstRect, srcRect); |
| 1771 | } |
| 1772 | |
| commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1773 | bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1774 | return filter->canFilterImageGPU(); |
| 1775 | } |
| 1776 | |
| commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 1777 | bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src, |
| senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1778 | const SkImageFilter::Context& ctx, |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1779 | SkBitmap* result, SkIPoint* offset) { |
| 1780 | // want explicitly our impl, so guard against a subclass of us overriding it |
| 1781 | if (!this->SkGpuDevice::canHandleImageFilter(filter)) { |
| 1782 | return false; |
| 1783 | } |
| 1784 | |
| 1785 | SkAutoLockPixels alp(src, !src.getTexture()); |
| 1786 | if (!src.getTexture() && !src.readyToDraw()) { |
| 1787 | return false; |
| 1788 | } |
| 1789 | |
| 1790 | GrTexture* texture; |
| 1791 | // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup |
| 1792 | // must be pushed upstack. |
| 1793 | SkAutoCachedTexture act(this, src, NULL, &texture); |
| 1794 | |
| senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1795 | return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx, |
| 1796 | result, offset); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | /////////////////////////////////////////////////////////////////////////////// |
| 1800 | |
| 1801 | // must be in SkCanvas::VertexMode order |
| 1802 | static const GrPrimitiveType gVertexMode2PrimitiveType[] = { |
| 1803 | kTriangles_GrPrimitiveType, |
| 1804 | kTriangleStrip_GrPrimitiveType, |
| 1805 | kTriangleFan_GrPrimitiveType, |
| 1806 | }; |
| 1807 | |
| 1808 | void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, |
| 1809 | int vertexCount, const SkPoint vertices[], |
| 1810 | const SkPoint texs[], const SkColor colors[], |
| 1811 | SkXfermode* xmode, |
| 1812 | const uint16_t indices[], int indexCount, |
| 1813 | const SkPaint& paint) { |
| 1814 | CHECK_SHOULD_DRAW(draw, false); |
| 1815 | |
| 1816 | GrPaint grPaint; |
| 1817 | // we ignore the shader if texs is null. |
| 1818 | if (NULL == texs) { |
| 1819 | if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) { |
| 1820 | return; |
| 1821 | } |
| 1822 | } else { |
| 1823 | if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) { |
| 1824 | return; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | if (NULL != xmode && NULL != texs && NULL != colors) { |
| 1829 | if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) { |
| 1830 | SkDebugf("Unsupported vertex-color/texture xfer mode.\n"); |
| 1831 | #if 0 |
| 1832 | return |
| 1833 | #endif |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | SkAutoSTMalloc<128, GrColor> convertedColors(0); |
| 1838 | if (NULL != colors) { |
| 1839 | // need to convert byte order and from non-PM to PM |
| 1840 | convertedColors.reset(vertexCount); |
| 1841 | for (int i = 0; i < vertexCount; ++i) { |
| 1842 | convertedColors[i] = SkColor2GrColor(colors[i]); |
| 1843 | } |
| 1844 | colors = convertedColors.get(); |
| 1845 | } |
| 1846 | fContext->drawVertices(grPaint, |
| 1847 | gVertexMode2PrimitiveType[vmode], |
| 1848 | vertexCount, |
| 1849 | (GrPoint*) vertices, |
| 1850 | (GrPoint*) texs, |
| 1851 | colors, |
| 1852 | indices, |
| 1853 | indexCount); |
| 1854 | } |
| 1855 | |
| 1856 | /////////////////////////////////////////////////////////////////////////////// |
| 1857 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1858 | void SkGpuDevice::drawText(const SkDraw& draw, const void* text, |
| 1859 | size_t byteLength, SkScalar x, SkScalar y, |
| 1860 | const SkPaint& paint) { |
| 1861 | CHECK_SHOULD_DRAW(draw, false); |
| 1862 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1863 | if (fMainTextContext->canDraw(paint)) { |
| commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1864 | GrPaint grPaint; |
| 1865 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | SkDEBUGCODE(this->validate();) |
| 1870 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1871 | fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y); |
| 1872 | } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) { |
| commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1873 | GrPaint grPaint; |
| 1874 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | SkDEBUGCODE(this->validate();) |
| 1879 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1880 | fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y); |
| commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1881 | } else { |
| 1882 | // this guy will just call our drawPath() |
| 1883 | draw.drawText_asPaths((const char*)text, byteLength, x, y, paint); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, |
| 1888 | size_t byteLength, const SkScalar pos[], |
| 1889 | SkScalar constY, int scalarsPerPos, |
| 1890 | const SkPaint& paint) { |
| 1891 | CHECK_SHOULD_DRAW(draw, false); |
| 1892 | |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1893 | if (fMainTextContext->canDraw(paint)) { |
| commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1894 | GrPaint grPaint; |
| 1895 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 1896 | return; |
| 1897 | } |
| 1898 | |
| 1899 | SkDEBUGCODE(this->validate();) |
| 1900 | |
| skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1901 | fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos, |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1902 | constY, scalarsPerPos); |
| 1903 | } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) { |
| commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1904 | GrPaint grPaint; |
| 1905 | if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| 1906 | return; |
| 1907 | } |
| skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1908 | |
| commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1909 | SkDEBUGCODE(this->validate();) |
| skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 1910 | |
| 1911 | fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos, |
| commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 1912 | constY, scalarsPerPos); |
| commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 1913 | } else { |
| 1914 | draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY, |
| 1915 | scalarsPerPos, paint); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text, |
| 1920 | size_t len, const SkPath& path, |
| 1921 | const SkMatrix* m, const SkPaint& paint) { |
| 1922 | CHECK_SHOULD_DRAW(draw, false); |
| 1923 | |
| 1924 | SkASSERT(draw.fDevice == this); |
| 1925 | draw.drawTextOnPath((const char*)text, len, path, m, paint); |
| 1926 | } |
| 1927 | |
| 1928 | /////////////////////////////////////////////////////////////////////////////// |
| 1929 | |
| 1930 | bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { |
| 1931 | if (!paint.isLCDRenderText()) { |
| 1932 | // we're cool with the paint as is |
| 1933 | return false; |
| 1934 | } |
| 1935 | |
| 1936 | if (paint.getShader() || |
| 1937 | paint.getXfermode() || // unless its srcover |
| 1938 | paint.getMaskFilter() || |
| 1939 | paint.getRasterizer() || |
| 1940 | paint.getColorFilter() || |
| 1941 | paint.getPathEffect() || |
| 1942 | paint.isFakeBoldText() || |
| 1943 | paint.getStyle() != SkPaint::kFill_Style) { |
| 1944 | // turn off lcd |
| 1945 | flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag; |
| 1946 | flags->fHinting = paint.getHinting(); |
| 1947 | return true; |
| 1948 | } |
| 1949 | // we're cool with the paint as is |
| 1950 | return false; |
| 1951 | } |
| 1952 | |
| 1953 | void SkGpuDevice::flush() { |
| 1954 | DO_DEFERRED_CLEAR(); |
| 1955 | fContext->resolveRenderTarget(fRenderTarget); |
| 1956 | } |
| 1957 | |
| 1958 | /////////////////////////////////////////////////////////////////////////////// |
| 1959 | |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1960 | SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) { |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1961 | GrTextureDesc desc; |
| 1962 | desc.fConfig = fRenderTarget->config(); |
| 1963 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1964 | desc.fWidth = info.width(); |
| 1965 | desc.fHeight = info.height(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1966 | desc.fSampleCnt = fRenderTarget->numSamples(); |
| 1967 | |
| 1968 | SkAutoTUnref<GrTexture> texture; |
| 1969 | // Skia's convention is to only clear a device if it is non-opaque. |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1970 | bool needClear = !info.isOpaque(); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1971 | |
| 1972 | #if CACHE_COMPATIBLE_DEVICE_TEXTURES |
| 1973 | // layers are never draw in repeat modes, so we can request an approx |
| 1974 | // match and ignore any padding. |
| 1975 | const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ? |
| 1976 | GrContext::kApprox_ScratchTexMatch : |
| 1977 | GrContext::kExact_ScratchTexMatch; |
| 1978 | texture.reset(fContext->lockAndRefScratchTexture(desc, match)); |
| 1979 | #else |
| 1980 | texture.reset(fContext->createUncachedTexture(desc, NULL, 0)); |
| 1981 | #endif |
| 1982 | if (NULL != texture.get()) { |
| 1983 | return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear)); |
| 1984 | } else { |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1985 | GrPrintf("---- failed to create compatible device texture [%d %d]\n", |
| 1986 | info.width(), info.height()); |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1987 | return NULL; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1991 | SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) { |
| 1992 | return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples()); |
| 1993 | } |
| 1994 | |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 1995 | SkGpuDevice::SkGpuDevice(GrContext* context, |
| 1996 | GrTexture* texture, |
| 1997 | bool needClear) |
| 1998 | : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) { |
| 1999 | |
| 2000 | SkASSERT(texture && texture->asRenderTarget()); |
| commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 2001 | // This constructor is called from onCreateDevice. It has locked the RT in the texture |
| skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 2002 | // cache. We pass true for the third argument so that it will get unlocked. |
| 2003 | this->initFromRenderTarget(context, texture->asRenderTarget(), true); |
| 2004 | fNeedClear = needClear; |
| 2005 | } |