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