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