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