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