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