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