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