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