blob: 5e0755b229a774209c13680321bce230813777a3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
10
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000012#include "GrBufferAllocPool.h"
joshualitt5478d422014-11-14 16:00:38 -080013#include "GrDefaultGeoProcFactory.h"
bsalomon453cf402014-11-11 14:15:57 -080014#include "GrGpuResource.h"
15#include "GrGpuResourceCacheAccess.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070016#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000017#include "GrDrawTargetCaps.h"
joshualitt5478d422014-11-14 16:00:38 -080018#include "GrGpu.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000019#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000020#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000021#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000022#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000023#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000024#include "GrPathUtils.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070025#include "GrResourceCache2.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000026#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000027#include "GrStencilBuffer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070028#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070029#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070030#include "GrSurfacePriv.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000031#include "GrTextStrike.h"
bsalomonafbf2d62014-09-30 12:18:44 -070032#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070033#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000034#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070035#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070036#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000037#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000038#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000039#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000040#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000041#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000042#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000043
joshualitt5478d422014-11-14 16:00:38 -080044#include "effects/GrConfigConversionEffect.h"
45#include "effects/GrDashingEffect.h"
46#include "effects/GrSingleTextureEffect.h"
47
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000048#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000049 // change this to a 1 to see notifications when partial coverage fails
50 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
51#else
52 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
53#endif
54
bsalomon@google.com60361492012-03-15 17:47:06 +000055static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000056static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
57
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000058static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
59static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000060
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000061#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000063// Glorified typedef to avoid including GrDrawState.h in GrContext.h
64class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {};
65
bsalomonf21dab92014-11-13 13:33:28 -080066class GrContext::AutoCheckFlush {
67public:
68 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
69
70 ~AutoCheckFlush() {
71 if (fContext->fFlushToReduceCacheSize) {
72 fContext->flush();
73 }
74 }
75
76private:
77 GrContext* fContext;
78};
79
krajcevski9c6d4d72014-08-12 07:26:25 -070080GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
81 const Options* opts) {
82 GrContext* context;
83 if (NULL == opts) {
84 context = SkNEW_ARGS(GrContext, (Options()));
85 } else {
86 context = SkNEW_ARGS(GrContext, (*opts));
87 }
88
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000089 if (context->init(backend, backendContext)) {
90 return context;
91 } else {
92 context->unref();
93 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000094 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000095}
96
krajcevski9c6d4d72014-08-12 07:26:25 -070097GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000098 fDrawState = NULL;
99 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000100 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000101 fPathRendererChain = NULL;
102 fSoftwarePathRenderer = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -0700103 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000104 fFontCache = NULL;
105 fDrawBuffer = NULL;
106 fDrawBufferVBAllocPool = NULL;
107 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800108 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000109 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000110 fOvalRenderer = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000111 fViewMatrix.reset();
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000112 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000113}
114
115bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000116 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000117
118 fGpu = GrGpu::Create(backend, backendContext, this);
119 if (NULL == fGpu) {
120 return false;
121 }
bsalomon33435572014-11-05 14:47:41 -0800122 this->initCommon();
123 return true;
124}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000125
bsalomon33435572014-11-05 14:47:41 -0800126void GrContext::initCommon() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000127 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000128
bsalomonc8dc1f72014-08-21 13:02:13 -0700129 fResourceCache2 = SkNEW(GrResourceCache2);
bsalomon71cb0c22014-11-14 12:10:14 -0800130 fResourceCache2->setOverBudgetCallback(OverBudgetCB, this);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000131
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000132 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
133
robertphillips4ec84da2014-06-24 13:10:43 -0700134 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000135
joshualittb44293e2014-10-28 08:12:18 -0700136 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000137 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000138
139 fDidTestPMConversions = false;
140
141 this->setupDrawBuffer();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000142}
143
bsalomon@google.com27847de2011-02-22 20:59:41 +0000144GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000145 if (NULL == fGpu) {
146 return;
147 }
148
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000149 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000150
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000151 for (int i = 0; i < fCleanUpData.count(); ++i) {
152 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
153 }
154
bsalomon33435572014-11-05 14:47:41 -0800155 SkDELETE(fResourceCache2);
bsalomon33435572014-11-05 14:47:41 -0800156 SkDELETE(fFontCache);
157 SkDELETE(fDrawBuffer);
158 SkDELETE(fDrawBufferVBAllocPool);
159 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000160
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000161 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000162 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000163
bsalomon@google.com205d4602011-04-25 12:43:45 +0000164 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000165 SkSafeUnref(fPathRendererChain);
166 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000167 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000168}
169
bsalomon2354f842014-07-28 13:48:36 -0700170void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000171 // abandon first to so destructors
172 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700173 fResourceCache2->abandonAll();
174
robertphillipse3371302014-09-17 06:01:06 -0700175 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000176
bsalomon@google.com30085192011-08-19 15:42:31 +0000177 // a path renderer may be holding onto resources that
178 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000179 SkSafeSetNull(fPathRendererChain);
180 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000181
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182 delete fDrawBuffer;
183 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000184
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000185 delete fDrawBufferVBAllocPool;
186 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000187
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000188 delete fDrawBufferIBAllocPool;
189 fDrawBufferIBAllocPool = NULL;
190
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000191 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000192 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000193
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000194 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000195 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000196}
197
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000198void GrContext::resetContext(uint32_t state) {
199 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000200}
201
202void GrContext::freeGpuResources() {
203 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204
bsalomon49f085d2014-09-05 13:34:00 -0700205 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700206 fDrawBuffer->purgeResources();
207 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000208
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000209 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000210 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000211
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000212 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000213 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000214 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000215 SkSafeSetNull(fPathRendererChain);
216 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000217}
218
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000219void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800220 if (resourceCount) {
221 *resourceCount = fResourceCache2->getResourceCount();
222 }
223 if (resourceBytes) {
224 *resourceBytes = fResourceCache2->getResourceBytes();
225 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000226}
227
kkinnunenc6cb56f2014-06-24 00:12:27 -0700228GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
229 const SkDeviceProperties&
230 leakyProperties,
231 bool enableDistanceFieldFonts) {
jvanverth8c27a182014-10-14 08:45:50 -0700232 if (fGpu->caps()->pathRenderingSupport() && renderTarget->getStencilBuffer() &&
233 renderTarget->isMultisampled()) {
234 return GrStencilAndCoverTextContext::Create(this, leakyProperties);
235 }
236
237 return GrDistanceFieldTextContext::Create(this, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700238}
239
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000240////////////////////////////////////////////////////////////////////////////////
241
bsalomonf2703d82014-10-28 14:33:06 -0700242GrTexture* GrContext::findAndRefTexture(const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000243 const GrCacheID& cacheID,
244 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700245 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon8b79d232014-11-10 10:19:06 -0800246
247 GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
bsalomon37dd3312014-11-03 08:47:23 -0800248 if (resource) {
bsalomon8b79d232014-11-10 10:19:06 -0800249 SkASSERT(static_cast<GrSurface*>(resource)->asTexture());
bsalomon37dd3312014-11-03 08:47:23 -0800250 return static_cast<GrSurface*>(resource)->asTexture();
bsalomon37dd3312014-11-03 08:47:23 -0800251 }
bsalomon8b79d232014-11-10 10:19:06 -0800252 return NULL;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000253}
254
bsalomonf2703d82014-10-28 14:33:06 -0700255bool GrContext::isTextureInCache(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000256 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000257 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700258 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon8b79d232014-11-10 10:19:06 -0800259 return fResourceCache2->hasContentKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000260}
261
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000262void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon71cb0c22014-11-14 12:10:14 -0800263 // TODO: Make GrStencilBuffers use the scratch mechanism rather than content keys.
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000264 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000265
266 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
267 sb->height(),
268 sb->numSamples());
bsalomon71cb0c22014-11-14 12:10:14 -0800269 SkAssertResult(sb->cacheAccess().setContentKey(resourceKey));
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000270}
271
bsalomon8b79d232014-11-10 10:19:06 -0800272GrStencilBuffer* GrContext::findAndRefStencilBuffer(int width, int height, int sampleCnt) {
273 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width, height, sampleCnt);
274 GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000275 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000276}
277
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000278static void stretch_image(void* dst,
279 int dstW,
280 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000281 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000282 int srcW,
283 int srcH,
284 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000285 SkFixed dx = (srcW << 16) / dstW;
286 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000287
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000288 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000289
robertphillips@google.com8b169312013-10-15 17:47:36 +0000290 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000291 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000292 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000293 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
294 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000295 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000296 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000297 x += dx;
298 }
299 y += dy;
300 }
301}
302
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000303// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000304// the current hardware. Resize the texture to be a POT
bsalomonf2703d82014-10-28 14:33:06 -0700305GrTexture* GrContext::createResizedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000306 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000307 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000308 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000309 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000310 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000311 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000312 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000313
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000314 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000315 return NULL;
316 }
317 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000318
bsalomonf2703d82014-10-28 14:33:06 -0700319 GrSurfaceDesc rtDesc = desc;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000320 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700321 kRenderTarget_GrSurfaceFlag |
322 kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000323 rtDesc.fWidth = GrNextPow2(desc.fWidth);
324 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000325
326 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
327
bsalomon49f085d2014-09-05 13:34:00 -0700328 if (texture) {
joshualitt5c55fef2014-10-31 14:04:35 -0700329 GrDrawTarget::AutoStateRestore asr(fDrawBuffer, GrDrawTarget::kReset_ASRInit);
330 GrDrawState* drawState = fDrawBuffer->drawState();
robertphillips@google.com3319f332012-08-13 18:00:36 +0000331 drawState->setRenderTarget(texture->asRenderTarget());
332
333 // if filtering is not desired then we want to ensure all
334 // texels in the resampled image are copies of texels from
335 // the original.
joshualitt5c55fef2014-10-31 14:04:35 -0700336 GrTextureParams params(SkShader::kClamp_TileMode,
337 filter ? GrTextureParams::kBilerp_FilterMode :
338 GrTextureParams::kNone_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700339 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000340
joshualitt5478d422014-11-14 16:00:38 -0800341 drawState->setGeometryProcessor(
342 GrDefaultGeoProcFactory::CreateAndSetAttribs(
343 drawState,
344 GrDefaultGeoProcFactory::kPosition_GPType |
345 GrDefaultGeoProcFactory::kLocalCoord_GPType))->unref();
jvanverth@google.com054ae992013-04-01 20:06:51 +0000346
joshualitt5c55fef2014-10-31 14:04:35 -0700347 GrDrawTarget::AutoReleaseGeometry arg(fDrawBuffer, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000348
349 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000350 SkPoint* verts = (SkPoint*) arg.vertices();
351 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
352 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
joshualitt5c55fef2014-10-31 14:04:35 -0700353 fDrawBuffer->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000354 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000355 } else {
356 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000357 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000358 // not. Either implement filtered stretch blit on CPU or just create
359 // one when FBO case fails.
360
bsalomonf2703d82014-10-28 14:33:06 -0700361 rtDesc.fFlags = kNone_GrSurfaceFlags;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000362 // no longer need to clamp at min RT size.
363 rtDesc.fWidth = GrNextPow2(desc.fWidth);
364 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000365
366 // We shouldn't be resizing a compressed texture.
367 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
368
robertphillips@google.com8b169312013-10-15 17:47:36 +0000369 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700370 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000371 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
372 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000373
374 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
375
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000376 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700377 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000379
380 return texture;
381}
382
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000383GrTexture* GrContext::createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700384 const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000385 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000386 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000387 size_t rowBytes,
388 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700389 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000390
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000391 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700392 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700393 // We do not know how to resize compressed textures.
394 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
395
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000396 texture = this->createResizedTexture(desc, cacheID,
397 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700398 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700400 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000402
bsalomon49f085d2014-09-05 13:34:00 -0700403 if (texture) {
bsalomon71cb0c22014-11-14 12:10:14 -0800404 if (texture->cacheAccess().setContentKey(resourceKey)) {
405 if (cacheKey) {
406 *cacheKey = resourceKey;
407 }
408 } else {
409 texture->unref();
410 texture = NULL;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000411 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000412 }
413
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000414 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415}
416
bsalomonf2703d82014-10-28 14:33:06 -0700417GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700418 bool calledDuringFlush) {
bsalomonbcf0a522014-10-08 08:40:09 -0700419 // kNoStencil has no meaning if kRT isn't set.
bsalomonf2703d82014-10-28 14:33:06 -0700420 SkASSERT((inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
421 !(inDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000422
bsalomonbcf0a522014-10-08 08:40:09 -0700423 // Make sure caller has checked for renderability if kRT is set.
bsalomonf2703d82014-10-28 14:33:06 -0700424 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
bsalomonbcf0a522014-10-08 08:40:09 -0700425 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000426
bsalomonf2703d82014-10-28 14:33:06 -0700427 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000428
bsalomonf2703d82014-10-28 14:33:06 -0700429 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
430 GrSurfaceFlags origFlags = desc->fFlags;
bsalomonbcf0a522014-10-08 08:40:09 -0700431 if (kApprox_ScratchTexMatch == match) {
432 // bin by pow2 with a reasonable min
433 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700434 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700435 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
436 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000437 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000438
bsalomonbcf0a522014-10-08 08:40:09 -0700439 do {
440 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
bsalomon000f8292014-10-15 19:04:14 -0700441 uint32_t scratchFlags = 0;
442 if (calledDuringFlush) {
443 scratchFlags = GrResourceCache2::kRequireNoPendingIO_ScratchFlag;
bsalomonf2703d82014-10-28 14:33:06 -0700444 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon000f8292014-10-15 19:04:14 -0700445 // If it is not a render target then it will most likely be populated by
446 // writePixels() which will trigger a flush if the texture has pending IO.
447 scratchFlags = GrResourceCache2::kPreferNoPendingIO_ScratchFlag;
448 }
449 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
bsalomonbcf0a522014-10-08 08:40:09 -0700450 if (resource) {
bsalomon37dd3312014-11-03 08:47:23 -0800451 return static_cast<GrSurface*>(resource)->asTexture();
bsalomonbcf0a522014-10-08 08:40:09 -0700452 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000453
bsalomonbcf0a522014-10-08 08:40:09 -0700454 if (kExact_ScratchTexMatch == match) {
455 break;
456 }
457 // We had a cache miss and we are in approx mode, relax the fit of the flags.
458
459 // We no longer try to reuse textures that were previously used as render targets in
460 // situations where no RT is needed; doing otherwise can confuse the video driver and
461 // cause significant performance problems in some cases.
bsalomonf2703d82014-10-28 14:33:06 -0700462 if (desc->fFlags & kNoStencil_GrSurfaceFlag) {
463 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrSurfaceFlag;
bsalomonbcf0a522014-10-08 08:40:09 -0700464 } else {
465 break;
466 }
467
468 } while (true);
469
470 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000471 }
472
bsalomon71cb0c22014-11-14 12:10:14 -0800473 GrTexture* texture = fGpu->createTexture(*desc, NULL, 0);
bsalomonbcf0a522014-10-08 08:40:09 -0700474 SkASSERT(NULL == texture ||
bsalomon453cf402014-11-11 14:15:57 -0800475 texture->cacheAccess().getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
bsalomonbcf0a522014-10-08 08:40:09 -0700476 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400477}
478
bsalomon71cb0c22014-11-14 12:10:14 -0800479void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800480 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800481
bsalomon66a450f2014-11-13 13:19:10 -0800482 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800483
484 // Flush the InOrderDrawBuffer to possibly free up some textures
485 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000486}
487
488
bsalomonf2703d82014-10-28 14:33:06 -0700489GrTexture* GrContext::createUncachedTexture(const GrSurfaceDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000490 void* srcData,
491 size_t rowBytes) {
bsalomonf2703d82014-10-28 14:33:06 -0700492 GrSurfaceDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000493 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000494}
495
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000496void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800497 if (maxTextures) {
498 *maxTextures = fResourceCache2->getMaxResourceCount();
499 }
500 if (maxTextureBytes) {
501 *maxTextureBytes = fResourceCache2->getMaxResourceBytes();
502 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000503}
504
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000505void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800506 fResourceCache2->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000507}
508
bsalomon@google.com91958362011-06-13 17:58:13 +0000509int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000510 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000511}
512
513int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000514 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000515}
516
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000517int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000518 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000519}
520
bsalomon@google.com27847de2011-02-22 20:59:41 +0000521///////////////////////////////////////////////////////////////////////////////
522
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000523GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
524 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000525}
526
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000527GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
528 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000529}
530
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000531///////////////////////////////////////////////////////////////////////////////
532
bsalomon@google.comb8670992012-07-25 21:27:09 +0000533bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000534 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000535 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000536 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000537 return false;
538 }
539
tfarinaf9dae782014-06-06 06:35:28 -0700540 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541
542 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700543 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000544 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545 return false;
546 }
547 }
548 return true;
549}
550
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551
bsalomon@google.com27847de2011-02-22 20:59:41 +0000552////////////////////////////////////////////////////////////////////////////////
553
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000554void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000555 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000556 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700557 GrRenderTarget* renderTarget) {
558 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800559 SkASSERT(renderTarget);
560
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000561 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800562 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700563 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
bsalomonf21dab92014-11-13 13:33:28 -0800564 GrDrawTarget* target = this->prepareToDraw(NULL, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700565 if (NULL == target) {
566 return;
567 }
568 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569}
570
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000571void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000572 // set rect to be big enough to fill the space, but not super-huge, so we
573 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000574 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000575 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000576 SkIntToScalar(getRenderTarget()->width()),
577 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000578 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000579 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000580 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700581 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000582
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000583 // We attempt to map r by the inverse matrix and draw that. mapRect will
584 // map the four corners and bound them with a new rect. This will not
585 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000586 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000587 if (!fViewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700588 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000589 return;
590 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000591 inverse.mapRect(&r);
592 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000593 if (!am.setIdentity(this, paint.writable())) {
tfarina38406c82014-10-31 07:11:12 -0700594 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000595 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000596 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000598 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000599 if (paint->isAntiAlias()) {
600 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000601 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000602 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603}
604
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000605#ifdef SK_DEVELOPER
606void GrContext::dumpFontCache() const {
607 fFontCache->dump();
608}
609#endif
610
bsalomon@google.com205d4602011-04-25 12:43:45 +0000611////////////////////////////////////////////////////////////////////////////////
612
bsalomon@google.com27847de2011-02-22 20:59:41 +0000613/* create a triangle strip that strokes the specified triangle. There are 8
614 unique vertices, but we repreat the last 2 to close up. Alternatively we
615 could use an indices array, and then only send 8 verts, but not sure that
616 would be faster.
617 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000618static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000619 SkScalar width) {
620 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000621 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000622
623 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
624 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
625 verts[2].set(rect.fRight - rad, rect.fTop + rad);
626 verts[3].set(rect.fRight + rad, rect.fTop - rad);
627 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
628 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
629 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
630 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
631 verts[8] = verts[0];
632 verts[9] = verts[1];
633}
634
bsalomonc30aaa02014-08-13 07:15:29 -0700635static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700636 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
637 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700638}
639
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000641 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000642 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000643 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700644 SkRect* devBoundRect) {
645 if (!target->getDrawState().canTweakAlphaForCoverage() &&
646 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000647#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700648 //SkDebugf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000649#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700650 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000651 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000652 const GrDrawState& drawState = target->getDrawState();
653 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000654 return false;
655 }
656
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000657#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000658 if (strokeWidth >= 0) {
659#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000660 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000661 return false;
662 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000663
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000664#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000665 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000666 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000667 return false;
668 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000669 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000670#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000671
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000672 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700673 if (strokeWidth < 0) {
674 return !is_irect(*devBoundRect);
675 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000676
bsalomon9c0822a2014-08-11 11:07:48 -0700677 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000678}
679
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000680static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
681 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
682 point.fY >= rect.fTop && point.fY <= rect.fBottom;
683}
684
bsalomon@google.com27847de2011-02-22 20:59:41 +0000685void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000686 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700687 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700688 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700689 SkPath path;
690 path.addRect(rect);
691 this->drawPath(paint, path, *strokeInfo);
692 return;
693 }
694
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000695 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800696 AutoCheckFlush acf(this);
697 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700698 if (NULL == target) {
699 return;
700 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000701
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000702 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700703 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700704 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000705
706 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
707 // cases where the RT is fully inside a stroke.
708 if (width < 0) {
709 SkRect rtRect;
710 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
711 SkRect clipSpaceRTRect = rtRect;
712 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700713 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000714 checkClip = true;
715 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
716 SkIntToScalar(this->getClip()->fOrigin.fY));
717 }
718 // Does the clip contain the entire RT?
719 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
720 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700721 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000722 return;
723 }
724 // Does the rect bound the RT?
725 SkPoint srcSpaceRTQuad[4];
726 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
727 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
728 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
729 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
730 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
731 // Will it blend?
732 GrColor clearColor;
733 if (paint.isOpaqueAndConstantColor(&clearColor)) {
bsalomon89c62982014-11-03 12:08:42 -0800734 target->clear(NULL, clearColor, true, fRenderTarget);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000735 return;
736 }
737 }
738 }
739 }
740
741 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000742 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000743 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700744 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700745
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000747 GrDrawState::AutoViewMatrixRestore avmr;
748 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000749 return;
750 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800752 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
753 fAARectRenderer->strokeAARect(target, rect, matrix, devBoundRect, strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000755 // filled AA rect
joshualittb44293e2014-10-28 08:12:18 -0700756 fAARectRenderer->fillAARect(target,
bsalomon9c0822a2014-08-11 11:07:48 -0700757 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000758 }
759 return;
760 }
761
bsalomon@google.com27847de2011-02-22 20:59:41 +0000762 if (width >= 0) {
763 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000764 // Hairline could be done by just adding closing vertex to
765 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000766
bsalomon@google.com27847de2011-02-22 20:59:41 +0000767 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000768 target->drawState()->setDefaultVertexAttribs();
joshualitt5478d422014-11-14 16:00:38 -0800769 target->drawState()->setGeometryProcessor(GrDefaultGeoProcFactory::Create(false))->unref();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000770 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000771
772 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700773 SkDebugf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000774 return;
775 }
776
777 GrPrimitiveType primType;
778 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000779 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780
781 if (width > 0) {
782 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000783 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000784 setStrokeRectStrip(vertex, rect, width);
785 } else {
786 // hairline
787 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000788 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789 vertex[0].set(rect.fLeft, rect.fTop);
790 vertex[1].set(rect.fRight, rect.fTop);
791 vertex[2].set(rect.fRight, rect.fBottom);
792 vertex[3].set(rect.fLeft, rect.fBottom);
793 vertex[4].set(rect.fLeft, rect.fTop);
794 }
795
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 target->drawNonIndexed(primType, 0, vertCount);
797 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000798 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700799 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 }
801}
802
803void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000804 const SkRect& dstRect,
805 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000806 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000807 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800808 AutoCheckFlush acf(this);
809 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700810 if (NULL == target) {
811 return;
812 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000813
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000814 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
815
bsalomon01c8da12014-08-04 09:21:30 -0700816 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817}
818
robertphillips@google.com42903302013-04-20 12:26:07 +0000819static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000820 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000821 const GrColor* colors,
822 int* colorOffset,
823 int* texOffset) {
824 *texOffset = -1;
825 *colorOffset = -1;
826
joshualitt5478d422014-11-14 16:00:38 -0800827 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700828 if (texCoords && colors) {
joshualitt5478d422014-11-14 16:00:38 -0800829 *colorOffset = sizeof(SkPoint);
830 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
831 flags |= GrDefaultGeoProcFactory::kColor_GPType |
832 GrDefaultGeoProcFactory::kLocalCoord_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700833 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000834 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800835 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700836 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000837 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800838 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000839 }
joshualitt5478d422014-11-14 16:00:38 -0800840 drawState->setGeometryProcessor(GrDefaultGeoProcFactory::CreateAndSetAttribs(drawState,
841 flags))->unref();
robertphillips@google.com42903302013-04-20 12:26:07 +0000842}
843
bsalomon@google.com27847de2011-02-22 20:59:41 +0000844void GrContext::drawVertices(const GrPaint& paint,
845 GrPrimitiveType primitiveType,
846 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000847 const SkPoint positions[],
848 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849 const GrColor colors[],
850 const uint16_t indices[],
851 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000852 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800853 AutoCheckFlush acf(this);
854 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000855
bsalomonf21dab92014-11-13 13:33:28 -0800856 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700857 if (NULL == target) {
858 return;
859 }
egdaniele61c4112014-06-12 10:24:21 -0700860 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000861
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000862 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
863
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000864 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +0000865 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000866
egdaniel7b3d5ee2014-08-28 05:41:14 -0700867 size_t VertexStride = drawState->getVertexStride();
joshualittd1aa8ff2014-11-04 07:47:55 -0800868 if (!geo.set(target, vertexCount, indexCount)) {
869 SkDebugf("Failed to get space for vertices!\n");
870 return;
871 }
872 void* curVertex = geo.vertices();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000873
joshualittd1aa8ff2014-11-04 07:47:55 -0800874 for (int i = 0; i < vertexCount; ++i) {
875 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876
joshualittd1aa8ff2014-11-04 07:47:55 -0800877 if (texOffset >= 0) {
878 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879 }
joshualittd1aa8ff2014-11-04 07:47:55 -0800880 if (colorOffset >= 0) {
881 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
882 }
883 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000884 }
885
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000886 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000887 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon49f085d2014-09-05 13:34:00 -0700888 if (indices) {
joshualittd1aa8ff2014-11-04 07:47:55 -0800889 uint16_t* curIndex = (uint16_t*)geo.indices();
890 for (int i = 0; i < indexCount; ++i) {
891 curIndex[i] = indices[i];
892 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000893 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000895 target->drawNonIndexed(primitiveType, 0, vertexCount);
896 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897}
898
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000899///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000900
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000901void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000902 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700903 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000904 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000905 return;
906 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000907
egdanield58a0ba2014-06-11 10:30:05 -0700908 if (strokeInfo.isDashed()) {
909 SkPath path;
910 path.addRRect(rrect);
911 this->drawPath(paint, path, strokeInfo);
912 return;
913 }
914
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000915 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800916 AutoCheckFlush acf(this);
917 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700918 if (NULL == target) {
919 return;
920 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000921
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000922 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
923
egdanield58a0ba2014-06-11 10:30:05 -0700924 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
925
926 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000927 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000928 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -0700929 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000930 }
931}
932
933///////////////////////////////////////////////////////////////////////////////
934
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000935void GrContext::drawDRRect(const GrPaint& paint,
936 const SkRRect& outer,
937 const SkRRect& inner) {
938 if (outer.isEmpty()) {
939 return;
940 }
941
942 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800943 AutoCheckFlush acf(this);
944 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000945
946 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
947
948 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
949 SkPath path;
950 path.addRRect(inner);
951 path.addRRect(outer);
952 path.setFillType(SkPath::kEvenOdd_FillType);
953
egdanield58a0ba2014-06-11 10:30:05 -0700954 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000955 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
956 }
957}
958
959///////////////////////////////////////////////////////////////////////////////
960
bsalomon@google.com93c96602012-04-27 13:05:21 +0000961void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000962 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700963 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000964 if (oval.isEmpty()) {
965 return;
966 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000967
egdanield58a0ba2014-06-11 10:30:05 -0700968 if (strokeInfo.isDashed()) {
969 SkPath path;
970 path.addOval(oval);
971 this->drawPath(paint, path, strokeInfo);
972 return;
973 }
974
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000975 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -0800976 AutoCheckFlush acf(this);
977 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700978 if (NULL == target) {
979 return;
980 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000981
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000982 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
983
egdanield58a0ba2014-06-11 10:30:05 -0700984 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
985
986
987 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +0000988 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000989 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -0700990 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +0000991 }
bsalomon@google.com150d2842012-01-12 20:19:56 +0000992}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000993
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000994// Can 'path' be drawn as a pair of filled nested rectangles?
995static bool is_nested_rects(GrDrawTarget* target,
996 const SkPath& path,
997 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -0700998 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000999 SkASSERT(stroke.isFillStyle());
1000
1001 if (path.isInverseFillType()) {
1002 return false;
1003 }
1004
1005 const GrDrawState& drawState = target->getDrawState();
1006
1007 // TODO: this restriction could be lifted if we were willing to apply
1008 // the matrix to all the points individually rather than just to the rect
1009 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1010 return false;
1011 }
1012
bsalomon9c0822a2014-08-11 11:07:48 -07001013 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1014 target->shouldDisableCoverageAAForBlend()) {
1015 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001016 }
1017
1018 SkPath::Direction dirs[2];
1019 if (!path.isNestedRects(rects, dirs)) {
1020 return false;
1021 }
1022
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001023 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001024 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001025 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001026 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001027
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001028 // Right now, nested rects where the margin is not the same width
1029 // all around do not render correctly
1030 const SkScalar* outer = rects[0].asScalars();
1031 const SkScalar* inner = rects[1].asScalars();
1032
robertphillips183e9852014-10-21 11:25:37 -07001033 bool allEq = true;
1034
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001035 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001036 bool allGoE1 = margin >= SK_Scalar1;
1037
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001038 for (int i = 1; i < 4; ++i) {
1039 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001040 if (temp < SK_Scalar1) {
1041 allGoE1 = false;
1042 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001043 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001044 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001045 }
1046 }
1047
robertphillips183e9852014-10-21 11:25:37 -07001048 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001049}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001050
egdanield58a0ba2014-06-11 10:30:05 -07001051void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001052
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001053 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001054 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001055 this->drawPaint(paint);
1056 }
1057 return;
1058 }
1059
egdanield58a0ba2014-06-11 10:30:05 -07001060 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001061 SkPoint pts[2];
1062 if (path.isLine(pts)) {
1063 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -08001064 AutoCheckFlush acf(this);
1065 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001066 if (NULL == target) {
1067 return;
1068 }
egdaniele61c4112014-06-12 10:24:21 -07001069 GrDrawState* drawState = target->drawState();
1070
1071 SkMatrix origViewMatrix = drawState->getViewMatrix();
1072 GrDrawState::AutoViewMatrixRestore avmr;
1073 if (avmr.setIdentity(target->drawState())) {
1074 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1075 origViewMatrix)) {
1076 return;
1077 }
1078 }
1079 }
1080
1081 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001082 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1083 SkTLazy<SkPath> effectPath;
1084 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1085 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1086 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1087 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1088 return;
1089 }
1090
1091 this->drawPath(paint, path, newStrokeInfo);
1092 return;
1093 }
1094
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001095 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001096 // Scratch textures can be recycled after they are returned to the texture
1097 // cache. This presents a potential hazard for buffered drawing. However,
1098 // the writePixels that uploads to the scratch will perform a flush so we're
1099 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001100 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -08001101 AutoCheckFlush acf(this);
1102 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001103 if (NULL == target) {
1104 return;
1105 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001106 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001107
egdaniel93a37bc2014-07-21 13:47:57 -07001108 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001109
egdanield58a0ba2014-06-11 10:30:05 -07001110 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1111
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001112 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1113
egdanield58a0ba2014-06-11 10:30:05 -07001114 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001115 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001116 SkRect rects[2];
1117
bsalomon9c0822a2014-08-11 11:07:48 -07001118 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001119 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001120 GrDrawState::AutoViewMatrixRestore avmr;
1121 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001122 return;
1123 }
1124
joshualittb44293e2014-10-28 08:12:18 -07001125 fAARectRenderer->fillAANestedRects(target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001126 return;
1127 }
1128 }
1129
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001130 SkRect ovalRect;
1131 bool isOval = path.isOval(&ovalRect);
1132
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001133 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001134 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1135 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001136 }
1137}
1138
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001139void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001140 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001141 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001142
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001143 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1144
1145
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001146 // An Assumption here is that path renderer would use some form of tweaking
1147 // the src color (either the input alpha or in the frag shader) to implement
1148 // aa. If we have some future driver-mojo path AA that can do the right
1149 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001150 bool useCoverageAA = useAA &&
1151 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1152 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001153
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001154
1155 GrPathRendererChain::DrawType type =
1156 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001157 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001158
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001159 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001160 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001161 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001162
1163 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001164 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001165
robertphillips@google.come79f3202014-02-11 16:30:21 +00001166 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001167 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001168 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001169 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1170 pathPtr = tmpPath.get();
1171 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001172 if (pathPtr->isEmpty()) {
1173 return;
1174 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001175 }
1176 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001177
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001178 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001179 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001180 }
1181
robertphillips@google.come79f3202014-02-11 16:30:21 +00001182 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001183#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001184 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001185#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001186 return;
1187 }
1188
robertphillips@google.come79f3202014-02-11 16:30:21 +00001189 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001190}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001191
bsalomon@google.com27847de2011-02-22 20:59:41 +00001192////////////////////////////////////////////////////////////////////////////////
1193
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001194void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001195 if (NULL == fDrawBuffer) {
1196 return;
1197 }
1198
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001199 if (kDiscard_FlushBit & flagsBitfield) {
1200 fDrawBuffer->reset();
1201 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001202 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001203 }
bsalomonf21dab92014-11-13 13:33:28 -08001204 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001205}
1206
bsalomon81beccc2014-10-13 12:32:55 -07001207bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1208 const void* inPixels, size_t outRowBytes, void* outPixels) {
1209 SkSrcPixelInfo srcPI;
1210 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001211 return false;
1212 }
bsalomon81beccc2014-10-13 12:32:55 -07001213 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1214 srcPI.fPixels = inPixels;
1215 srcPI.fRowBytes = inRowBytes;
1216
1217 SkDstPixelInfo dstPI;
1218 dstPI.fColorType = srcPI.fColorType;
1219 dstPI.fAlphaType = kPremul_SkAlphaType;
1220 dstPI.fPixels = outPixels;
1221 dstPI.fRowBytes = outRowBytes;
1222
1223 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001224}
1225
bsalomon81beccc2014-10-13 12:32:55 -07001226bool GrContext::writeSurfacePixels(GrSurface* surface,
1227 int left, int top, int width, int height,
1228 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1229 uint32_t pixelOpsFlags) {
1230
1231 {
1232 GrTexture* texture = NULL;
1233 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1234 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1235
1236 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1237 surface->surfacePriv().hasPendingIO()) {
1238 this->flush();
1239 }
1240 return fGpu->writeTexturePixels(texture, left, top, width, height,
1241 srcConfig, buffer, rowBytes);
1242 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1243 // upload is already flushed.
1244 }
1245 }
1246
1247 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1248 GrRenderTarget* renderTarget = surface->asRenderTarget();
1249 if (NULL == renderTarget) {
1250 return false;
1251 }
1252
1253 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1254 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1255 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1256 // when drawing the scratch to the dst using a conversion effect.
1257 bool swapRAndB = false;
1258 GrPixelConfig writeConfig = srcConfig;
1259 if (GrPixelConfigSwapRAndB(srcConfig) ==
1260 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1261 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1262 swapRAndB = true;
1263 }
1264
bsalomonf2703d82014-10-28 14:33:06 -07001265 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001266 desc.fWidth = width;
1267 desc.fHeight = height;
1268 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001269 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1270 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001271 return false;
1272 }
1273
1274 SkAutoTUnref<const GrFragmentProcessor> fp;
1275 SkMatrix textureMatrix;
1276 textureMatrix.setIDiv(texture->width(), texture->height());
1277
1278 // allocate a tmp buffer and sw convert the pixels to premul
1279 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1280
1281 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1282 if (!GrPixelConfigIs8888(srcConfig)) {
1283 return false;
1284 }
1285 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1286 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1287 if (NULL == fp) {
1288 size_t tmpRowBytes = 4 * width;
1289 tmpPixels.reset(width * height);
1290 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1291 tmpPixels.get())) {
1292 return false;
1293 }
1294 rowBytes = tmpRowBytes;
1295 buffer = tmpPixels.get();
1296 }
1297 }
1298 if (NULL == fp) {
1299 fp.reset(GrConfigConversionEffect::Create(texture,
1300 swapRAndB,
1301 GrConfigConversionEffect::kNone_PMConversion,
1302 textureMatrix));
1303 }
1304
1305 // Even if the client told us not to flush, we still flush here. The client may have known that
1306 // writes to the original surface caused no data hazards, but they can't know that the scratch
1307 // we just got is safe.
1308 if (texture->surfacePriv().hasPendingIO()) {
1309 this->flush();
1310 }
1311 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1312 writeConfig, buffer, rowBytes)) {
1313 return false;
1314 }
1315
1316 SkMatrix matrix;
1317 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1318
1319 // This function can be called in the midst of drawing another object (e.g., when uploading a
1320 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1321 // drawing a rect to the render target.
1322 // The bracket ensures we pop the stack if we wind up flushing below.
1323 {
bsalomonf21dab92014-11-13 13:33:28 -08001324 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL, NULL);
bsalomon81beccc2014-10-13 12:32:55 -07001325 GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit,
1326 &matrix);
1327 GrDrawState* drawState = drawTarget->drawState();
1328 drawState->addColorProcessor(fp);
1329 drawState->setRenderTarget(renderTarget);
1330 drawState->disableState(GrDrawState::kClip_StateBit);
1331 drawTarget->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
1332 }
1333
1334 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1335 this->flushSurfaceWrites(surface);
1336 }
1337
1338 return true;
1339}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001340
reed@google.com7111d462014-03-25 16:20:24 +00001341// toggles between RGBA and BGRA
1342static SkColorType toggle_colortype32(SkColorType ct) {
1343 if (kRGBA_8888_SkColorType == ct) {
1344 return kBGRA_8888_SkColorType;
1345 } else {
1346 SkASSERT(kBGRA_8888_SkColorType == ct);
1347 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001348 }
1349}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001350
bsalomon@google.com0342a852012-08-20 19:22:38 +00001351bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1352 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001353 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001354 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001355 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001356 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001357
bsalomonafbf2d62014-09-30 12:18:44 -07001358 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001359 this->flush();
1360 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001362 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001363
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001364 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1365 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1366 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001367 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001368 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001369 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1370 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1371 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1372 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1373 // dstConfig.
1374 GrPixelConfig readConfig = dstConfig;
1375 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001376 if (GrPixelConfigSwapRAndB(dstConfig) ==
1377 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001378 readConfig = GrPixelConfigSwapRAndB(readConfig);
1379 swapRAndB = true;
1380 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001381
bsalomon@google.com0342a852012-08-20 19:22:38 +00001382 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001383
bsalomon@google.com9c680582013-02-06 18:17:50 +00001384 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001385 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001386 return false;
1387 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001388
bsalomon191bcc02014-11-14 11:31:13 -08001389 SkAutoTUnref<GrTexture> tempTexture;
1390
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001391 // If the src is a texture and we would have to do conversions after read pixels, we instead
1392 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1393 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1394 // on the read back pixels.
1395 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001396 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001397 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001398 GrSurfaceDesc desc;
1399 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001400 desc.fWidth = width;
1401 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001402 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001403 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001404
bsalomon@google.com9c680582013-02-06 18:17:50 +00001405 // When a full read back is faster than a partial we could always make the scratch exactly
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001406 // match the passed rect. However, if we see many different size rectangles we will trash
1407 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1408 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001409 ScratchTexMatch match = kApprox_ScratchTexMatch;
1410 if (0 == left &&
1411 0 == top &&
1412 target->width() == width &&
1413 target->height() == height &&
1414 fGpu->fullReadPixelsIsFasterThanPartial()) {
1415 match = kExact_ScratchTexMatch;
1416 }
bsalomon191bcc02014-11-14 11:31:13 -08001417 tempTexture.reset(this->refScratchTexture(desc, match));
1418 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001419 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001420 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001421 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001422 textureMatrix.postIDiv(src->width(), src->height());
1423
joshualittb0a8a372014-09-23 09:50:21 -07001424 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001425 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001426 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1427 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001428 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001429 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001430 }
1431 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1432 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001433 if (fp || flipY || swapRAndB) {
1434 if (!fp) {
1435 fp.reset(GrConfigConversionEffect::Create(
1436 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1437 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001438 }
1439 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001440
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001441 // We protect the existing geometry here since it may not be
1442 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1443 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001444 {
1445 GrDrawTarget::AutoGeometryAndStatePush agasp(fDrawBuffer,
1446 GrDrawTarget::kReset_ASRInit);
1447 GrDrawState* drawState = fDrawBuffer->drawState();
1448 SkASSERT(fp);
1449 drawState->addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001450
bsalomon191bcc02014-11-14 11:31:13 -08001451 drawState->setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001452 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
1453 fDrawBuffer->drawSimpleRect(rect);
1454 // we want to read back from the scratch's origin
1455 left = 0;
1456 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001457 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001458 }
1459 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001460 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001461 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001462 }
joshualitt5c55fef2014-10-31 14:04:35 -07001463
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001464 if (!fGpu->readPixels(target,
1465 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001466 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001467 return false;
1468 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001469 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001470 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001471 SkDstPixelInfo dstPI;
1472 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1473 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001474 }
reed@google.com7111d462014-03-25 16:20:24 +00001475 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1476 dstPI.fPixels = buffer;
1477 dstPI.fRowBytes = rowBytes;
1478
1479 SkSrcPixelInfo srcPI;
1480 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1481 srcPI.fAlphaType = kPremul_SkAlphaType;
1482 srcPI.fPixels = buffer;
1483 srcPI.fRowBytes = rowBytes;
1484
1485 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001486 }
1487 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001488}
1489
bsalomon87a94eb2014-11-03 14:28:32 -08001490void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
1491 SkASSERT(surface);
1492 ASSERT_OWNED_RESOURCE(surface);
1493 if (surface->surfacePriv().hasPendingIO()) {
1494 this->flush();
1495 }
1496 GrRenderTarget* rt = surface->asRenderTarget();
1497 if (fGpu && rt) {
1498 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001499 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001500}
1501
bsalomon41ebbdd2014-08-04 08:31:39 -07001502void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1503 SkASSERT(renderTarget);
1504 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001505 AutoRestoreEffects are;
bsalomonf21dab92014-11-13 13:33:28 -08001506 AutoCheckFlush acf(this);
1507 GrDrawTarget* target = this->prepareToDraw(NULL, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001508 if (NULL == target) {
1509 return;
1510 }
1511 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001512}
1513
bsalomonf80bfed2014-10-07 05:56:02 -07001514void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1515 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001516 if (NULL == src || NULL == dst) {
1517 return;
1518 }
bsalomone3d4bf22014-09-23 09:15:03 -07001519 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001520 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001521
bsalomonf80bfed2014-10-07 05:56:02 -07001522 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1523 // here.
junov96c118e2014-09-26 13:09:47 -07001524
bsalomonf21dab92014-11-13 13:33:28 -08001525 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL);
junov96c118e2014-09-26 13:09:47 -07001526 if (NULL == target) {
1527 return;
1528 }
junov96c118e2014-09-26 13:09:47 -07001529 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001530
1531 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1532 this->flush();
1533 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001534}
1535
bsalomonf80bfed2014-10-07 05:56:02 -07001536void GrContext::flushSurfaceWrites(GrSurface* surface) {
1537 if (surface->surfacePriv().hasPendingWrite()) {
1538 this->flush();
1539 }
1540}
1541
bsalomon@google.com27847de2011-02-22 20:59:41 +00001542////////////////////////////////////////////////////////////////////////////////
1543
bsalomonf21dab92014-11-13 13:33:28 -08001544GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
1545 AutoRestoreEffects* are,
1546 AutoCheckFlush* acf) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001547 // All users of this draw state should be freeing up all effects when they're done.
1548 // Otherwise effects that own resources may keep those resources alive indefinitely.
joshualittbd769d02014-09-04 08:56:46 -07001549 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() &&
1550 !fDrawState->hasGeometryProcessor());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001551
bsalomon41ebbdd2014-08-04 08:31:39 -07001552 if (NULL == fGpu) {
1553 return NULL;
1554 }
1555
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001556 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon49f085d2014-09-05 13:34:00 -07001557 if (paint) {
1558 SkASSERT(are);
bsalomonf21dab92014-11-13 13:33:28 -08001559 SkASSERT(acf);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001560 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001561 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001562#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1563 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001564 !fDrawState->couldApplyCoverage(fGpu->caps())) {
tfarina38406c82014-10-31 07:11:12 -07001565 SkDebugf("Partial pixel coverage will be incorrectly blended.\n");
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001566 }
1567#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001568 // Clear any vertex attributes configured for the previous use of the
1569 // GrDrawState which can effect which blend optimizations are in effect.
1570 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001571 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001572 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001573 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001574 }
bsalomon49f085d2014-09-05 13:34:00 -07001575 fDrawState->setState(GrDrawState::kClip_StateBit, fClip &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001576 !fClip->fClipStack->isWideOpen());
joshualitt5c55fef2014-10-31 14:04:35 -07001577 fDrawBuffer->setClip(fClip);
1578 SkASSERT(fDrawState == fDrawBuffer->drawState());
1579 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580}
1581
robertphillips@google.com72176b22012-05-23 13:19:12 +00001582/*
1583 * This method finds a path renderer that can draw the specified path on
1584 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001585 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001586 * can be individually allowed/disallowed via the "allowSW" boolean.
1587 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001588GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001589 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001590 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001591 bool allowSW,
1592 GrPathRendererChain::DrawType drawType,
1593 GrPathRendererChain::StencilSupport* stencilSupport) {
1594
bsalomon@google.com30085192011-08-19 15:42:31 +00001595 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001596 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001597 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001598
sugoi@google.com12b4e272012-12-06 20:13:11 +00001599 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1600 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001601 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001602 drawType,
1603 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001604
1605 if (NULL == pr && allowSW) {
1606 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001607 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001608 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001609 pr = fSoftwarePathRenderer;
1610 }
1611
1612 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001613}
1614
bsalomon@google.com27847de2011-02-22 20:59:41 +00001615////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001616bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1617 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001618}
1619
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001620int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1621 SkScalar dpi) const {
1622 if (!this->isConfigRenderable(config, true)) {
1623 return 0;
1624 }
1625 int chosenSampleCount = 0;
1626 if (fGpu->caps()->pathRenderingSupport()) {
1627 if (dpi >= 250.0f) {
1628 chosenSampleCount = 4;
1629 } else {
1630 chosenSampleCount = 16;
1631 }
1632 }
1633 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1634 chosenSampleCount : 0;
1635}
1636
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001637void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001638 SkASSERT(NULL == fDrawBuffer);
1639 SkASSERT(NULL == fDrawBufferVBAllocPool);
1640 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001641
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001642 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001643 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001644 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001645 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001646 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001647 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001648 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001649 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001650
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001651 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001652 fDrawBufferVBAllocPool,
1653 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001654
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001655 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001656}
1657
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001658GrDrawTarget* GrContext::getTextTarget() {
bsalomonf21dab92014-11-13 13:33:28 -08001659 return this->prepareToDraw(NULL, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001660}
1661
1662const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1663 return fGpu->getQuadIndexBuffer();
1664}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001665
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001666namespace {
1667void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1668 GrConfigConversionEffect::PMConversion pmToUPM;
1669 GrConfigConversionEffect::PMConversion upmToPM;
1670 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1671 *pmToUPMValue = pmToUPM;
1672 *upmToPMValue = upmToPM;
1673}
1674}
1675
joshualittb0a8a372014-09-23 09:50:21 -07001676const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1677 bool swapRAndB,
1678 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001679 if (!fDidTestPMConversions) {
1680 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001681 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001682 }
1683 GrConfigConversionEffect::PMConversion pmToUPM =
1684 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1685 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001686 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001687 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001688 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001689 }
1690}
1691
joshualittb0a8a372014-09-23 09:50:21 -07001692const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1693 bool swapRAndB,
1694 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001695 if (!fDidTestPMConversions) {
1696 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001697 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001698 }
1699 GrConfigConversionEffect::PMConversion upmToPM =
1700 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1701 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001702 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001703 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001704 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001705 }
1706}
1707
bsalomon6d3fe022014-07-25 08:35:45 -07001708void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
bsalomon71cb0c22014-11-14 12:10:14 -08001709 resource->cacheAccess().setContentKey(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001710}
1711
bsalomon6d3fe022014-07-25 08:35:45 -07001712GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
bsalomon71cb0c22014-11-14 12:10:14 -08001713 return fResourceCache2->findAndRefContentResource(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001714}
1715
egdanielbbcb38d2014-06-19 10:19:29 -07001716void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1717 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001718 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001719 fDrawBuffer->addGpuTraceMarker(marker);
1720 }
1721}
1722
1723void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1724 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001725 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001726 fDrawBuffer->removeGpuTraceMarker(marker);
1727 }
1728}
1729
bsalomon@google.comc4364992011-11-07 15:54:49 +00001730///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001731#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001732void GrContext::printCacheStats() const {
bsalomon71cb0c22014-11-14 12:10:14 -08001733 fResourceCache2->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001734}
1735#endif
robertphillips754f4e92014-09-18 13:52:08 -07001736
1737#if GR_GPU_STATS
1738const GrContext::GPUStats* GrContext::gpuStats() const {
1739 return fGpu->gpuStats();
1740}
1741#endif
1742