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