blob: b4bcfd31344ea0b0a606aaccc09dd73c632ce841 [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
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000011#include "effects/GrConfigConversionEffect.h"
egdaniele61c4112014-06-12 10:24:21 -070012#include "effects/GrDashingEffect.h"
13#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000014
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000015#include "GrAARectRenderer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000017#include "GrGpu.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070018#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000019#include "GrDrawTargetCaps.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000020#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000021#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000022#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000023#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000024#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000025#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000026#include "GrResourceCache.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070027#include "GrResourceCache2.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000028#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000029#include "GrStencilBuffer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070030#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070031#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070032#include "GrSurfacePriv.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000033#include "GrTextStrike.h"
bsalomonafbf2d62014-09-30 12:18:44 -070034#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070035#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000036#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070037#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070038#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000039#include "SkGr.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000040#include "SkRTConf.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000041#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000042#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000043#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000044#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000045#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000046
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000047// It can be useful to set this to false to test whether a bug is caused by using the
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000048// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000049// debugging simpler.
bsalomon@google.come932c982013-09-10 19:47:01 +000050SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true,
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000051 "Defers rendering in GrContext via GrInOrderDrawBuffer.");
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000052
53#define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000055#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000056 // change this to a 1 to see notifications when partial coverage fails
57 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
58#else
59 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
60#endif
61
robertphillips@google.com4e5559a2013-10-30 17:04:16 +000062static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT;
63static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000064
bsalomon@google.com60361492012-03-15 17:47:06 +000065static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000066static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
67
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000068static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
69static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000070
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000071#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000072
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000073// Glorified typedef to avoid including GrDrawState.h in GrContext.h
74class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {};
75
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000076class GrContext::AutoCheckFlush {
77public:
bsalomon49f085d2014-09-05 13:34:00 -070078 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000079
80 ~AutoCheckFlush() {
81 if (fContext->fFlushToReduceCacheSize) {
82 fContext->flush();
83 }
84 }
85
86private:
87 GrContext* fContext;
88};
89
krajcevski9c6d4d72014-08-12 07:26:25 -070090GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
91 const Options* opts) {
92 GrContext* context;
93 if (NULL == opts) {
94 context = SkNEW_ARGS(GrContext, (Options()));
95 } else {
96 context = SkNEW_ARGS(GrContext, (*opts));
97 }
98
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000099 if (context->init(backend, backendContext)) {
100 return context;
101 } else {
102 context->unref();
103 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000104 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000105}
106
krajcevski9c6d4d72014-08-12 07:26:25 -0700107GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000108 fDrawState = NULL;
109 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000110 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000111 fPathRendererChain = NULL;
112 fSoftwarePathRenderer = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000113 fResourceCache = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -0700114 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000115 fFontCache = NULL;
116 fDrawBuffer = NULL;
117 fDrawBufferVBAllocPool = NULL;
118 fDrawBufferIBAllocPool = NULL;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000119 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000120 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000121 fOvalRenderer = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000122 fViewMatrix.reset();
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000123 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000124}
125
126bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000127 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000128
129 fGpu = GrGpu::Create(backend, backendContext, this);
130 if (NULL == fGpu) {
131 return false;
132 }
133
134 fDrawState = SkNEW(GrDrawState);
135 fGpu->setDrawState(fDrawState);
136
bsalomonbcf0a522014-10-08 08:40:09 -0700137 fResourceCache = SkNEW_ARGS(GrResourceCache, (fGpu->caps(),
138 MAX_RESOURCE_CACHE_COUNT,
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000139 MAX_RESOURCE_CACHE_BYTES));
140 fResourceCache->setOverbudgetCallback(OverbudgetCB, this);
bsalomonc8dc1f72014-08-21 13:02:13 -0700141 fResourceCache2 = SkNEW(GrResourceCache2);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000142
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000143 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
144
robertphillips4ec84da2014-06-24 13:10:43 -0700145 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000146
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000147 fLastDrawWasBuffered = kNo_BufferedDraw;
148
149 fAARectRenderer = SkNEW(GrAARectRenderer);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000150 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000151
152 fDidTestPMConversions = false;
153
154 this->setupDrawBuffer();
155
156 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000157}
158
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000160 if (NULL == fGpu) {
161 return;
162 }
163
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000164 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000165
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000166 for (int i = 0; i < fCleanUpData.count(); ++i) {
167 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
168 }
169
bsalomonc8dc1f72014-08-21 13:02:13 -0700170 delete fResourceCache2;
171 fResourceCache2 = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000172 delete fResourceCache;
173 fResourceCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000174 delete fFontCache;
175 delete fDrawBuffer;
176 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000177 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000178
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000179 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000180 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000181
bsalomon@google.com205d4602011-04-25 12:43:45 +0000182 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000183 SkSafeUnref(fPathRendererChain);
184 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000185 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000186}
187
bsalomon2354f842014-07-28 13:48:36 -0700188void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000189 // abandon first to so destructors
190 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700191 fResourceCache2->abandonAll();
192
robertphillipse3371302014-09-17 06:01:06 -0700193 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000194
bsalomon@google.com30085192011-08-19 15:42:31 +0000195 // a path renderer may be holding onto resources that
196 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000197 SkSafeSetNull(fPathRendererChain);
198 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000199
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000200 delete fDrawBuffer;
201 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000202
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000203 delete fDrawBufferVBAllocPool;
204 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000205
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000206 delete fDrawBufferIBAllocPool;
207 fDrawBufferIBAllocPool = NULL;
208
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000209 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000210 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000211
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000212 fResourceCache->purgeAllUnlocked();
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +0000213
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000214 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000215 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000216}
217
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000218void GrContext::resetContext(uint32_t state) {
219 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000220}
221
222void GrContext::freeGpuResources() {
223 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224
robertphillips@google.comff175842012-05-14 19:31:39 +0000225 fGpu->purgeResources();
bsalomon49f085d2014-09-05 13:34:00 -0700226 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700227 fDrawBuffer->purgeResources();
228 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000229
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000230 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000231 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000232
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000233 fResourceCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000234 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000235 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000236 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000237 SkSafeSetNull(fPathRendererChain);
238 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239}
240
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000241void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon49f085d2014-09-05 13:34:00 -0700242 if (resourceCount) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000243 *resourceCount = fResourceCache->getCachedResourceCount();
244 }
bsalomon49f085d2014-09-05 13:34:00 -0700245 if (resourceBytes) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000246 *resourceBytes = fResourceCache->getCachedResourceBytes();
247 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000248}
249
kkinnunenc6cb56f2014-06-24 00:12:27 -0700250GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
251 const SkDeviceProperties&
252 leakyProperties,
253 bool enableDistanceFieldFonts) {
jvanverth8c27a182014-10-14 08:45:50 -0700254 if (fGpu->caps()->pathRenderingSupport() && renderTarget->getStencilBuffer() &&
255 renderTarget->isMultisampled()) {
256 return GrStencilAndCoverTextContext::Create(this, leakyProperties);
257 }
258
259 return GrDistanceFieldTextContext::Create(this, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700260}
261
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000262////////////////////////////////////////////////////////////////////////////////
263
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000264GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
265 const GrCacheID& cacheID,
266 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700267 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon6d3fe022014-07-25 08:35:45 -0700268 GrGpuResource* resource = fResourceCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000269 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000270 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000271}
272
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000273bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000274 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000275 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700276 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000277 return fResourceCache->hasKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000278}
279
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000280void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000281 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000282
283 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
284 sb->height(),
285 sb->numSamples());
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000286 fResourceCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000287}
288
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000289GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000291 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
292 height,
293 sampleCnt);
bsalomon6d3fe022014-07-25 08:35:45 -0700294 GrGpuResource* resource = fResourceCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000295 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000296}
297
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000298static void stretch_image(void* dst,
299 int dstW,
300 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000301 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000302 int srcW,
303 int srcH,
304 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000305 SkFixed dx = (srcW << 16) / dstW;
306 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000307
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000308 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000309
robertphillips@google.com8b169312013-10-15 17:47:36 +0000310 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000311 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000312 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000313 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
314 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000315 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000316 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000317 x += dx;
318 }
319 y += dy;
320 }
321}
322
robertphillips@google.com42903302013-04-20 12:26:07 +0000323namespace {
324
325// position + local coordinate
326extern const GrVertexAttrib gVertexAttribs[] = {
327 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000328 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000329};
330
331};
332
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000333// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000334// the current hardware. Resize the texture to be a POT
335GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000336 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000337 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000338 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000339 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000340 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000341 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000342 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000343
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000344 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000345 return NULL;
346 }
347 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000348
robertphillips@google.com3319f332012-08-13 18:00:36 +0000349 GrTextureDesc rtDesc = desc;
350 rtDesc.fFlags = rtDesc.fFlags |
351 kRenderTarget_GrTextureFlagBit |
352 kNoStencil_GrTextureFlagBit;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000353 rtDesc.fWidth = GrNextPow2(desc.fWidth);
354 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000355
356 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
357
bsalomon49f085d2014-09-05 13:34:00 -0700358 if (texture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000359 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
360 GrDrawState* drawState = fGpu->drawState();
361 drawState->setRenderTarget(texture->asRenderTarget());
362
363 // if filtering is not desired then we want to ensure all
364 // texels in the resampled image are copies of texels from
365 // the original.
humper@google.comb86add12013-07-25 18:49:07 +0000366 GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTextureParams::kBilerp_FilterMode :
367 GrTextureParams::kNone_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700368 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000369
egdaniel7b3d5ee2014-08-28 05:41:14 -0700370 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs),
371 2 * sizeof(SkPoint));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000372
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000373 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000374
375 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000376 SkPoint* verts = (SkPoint*) arg.vertices();
377 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
378 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000379 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000380 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000381 } else {
382 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000383 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000384 // not. Either implement filtered stretch blit on CPU or just create
385 // one when FBO case fails.
386
387 rtDesc.fFlags = kNone_GrTextureFlags;
388 // no longer need to clamp at min RT size.
389 rtDesc.fWidth = GrNextPow2(desc.fWidth);
390 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000391
392 // We shouldn't be resizing a compressed texture.
393 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
394
robertphillips@google.com8b169312013-10-15 17:47:36 +0000395 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700396 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000397 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
398 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000399
400 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
401
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000402 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700403 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000404 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000405
406 return texture;
407}
408
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000409GrTexture* GrContext::createTexture(const GrTextureParams* params,
410 const GrTextureDesc& desc,
411 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000412 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000413 size_t rowBytes,
414 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700415 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000416
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000417 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700418 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700419 // We do not know how to resize compressed textures.
420 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
421
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000422 texture = this->createResizedTexture(desc, cacheID,
423 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700424 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700426 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000428
bsalomon49f085d2014-09-05 13:34:00 -0700429 if (texture) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000430 fResourceCache->addResource(resourceKey, texture);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000431
bsalomon49f085d2014-09-05 13:34:00 -0700432 if (cacheKey) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000433 *cacheKey = resourceKey;
434 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000435 }
436
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000437 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000438}
439
bsalomonbcf0a522014-10-08 08:40:09 -0700440GrTexture* GrContext::createNewScratchTexture(const GrTextureDesc& desc) {
441 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
442 if (!texture) {
443 return NULL;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000444 }
bsalomonbcf0a522014-10-08 08:40:09 -0700445 fResourceCache->addResource(texture->getScratchKey(), texture);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400446 return texture;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000447}
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000448
bsalomone3059732014-10-14 11:47:22 -0700449GrTexture* GrContext::refScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match,
450 bool calledDuringFlush) {
bsalomonbcf0a522014-10-08 08:40:09 -0700451 // kNoStencil has no meaning if kRT isn't set.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000452 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000453 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit));
454
bsalomonbcf0a522014-10-08 08:40:09 -0700455 // Make sure caller has checked for renderability if kRT is set.
456 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
457 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000458
bsalomonbcf0a522014-10-08 08:40:09 -0700459 SkTCopyOnFirstWrite<GrTextureDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000460
bsalomonbcf0a522014-10-08 08:40:09 -0700461 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrTextureFlagBit)) {
462 GrTextureFlags origFlags = desc->fFlags;
463 if (kApprox_ScratchTexMatch == match) {
464 // bin by pow2 with a reasonable min
465 static const int MIN_SIZE = 16;
466 GrTextureDesc* wdesc = desc.writable();
467 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
468 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000469 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000470
bsalomonbcf0a522014-10-08 08:40:09 -0700471 do {
472 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
bsalomon000f8292014-10-15 19:04:14 -0700473 uint32_t scratchFlags = 0;
474 if (calledDuringFlush) {
475 scratchFlags = GrResourceCache2::kRequireNoPendingIO_ScratchFlag;
476 } else if (!(desc->fFlags & kRenderTarget_GrTextureFlagBit)) {
477 // If it is not a render target then it will most likely be populated by
478 // writePixels() which will trigger a flush if the texture has pending IO.
479 scratchFlags = GrResourceCache2::kPreferNoPendingIO_ScratchFlag;
480 }
481 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
bsalomonbcf0a522014-10-08 08:40:09 -0700482 if (resource) {
483 fResourceCache->makeResourceMRU(resource);
484 return static_cast<GrTexture*>(resource);
485 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000486
bsalomonbcf0a522014-10-08 08:40:09 -0700487 if (kExact_ScratchTexMatch == match) {
488 break;
489 }
490 // We had a cache miss and we are in approx mode, relax the fit of the flags.
491
492 // We no longer try to reuse textures that were previously used as render targets in
493 // situations where no RT is needed; doing otherwise can confuse the video driver and
494 // cause significant performance problems in some cases.
495 if (desc->fFlags & kNoStencil_GrTextureFlagBit) {
496 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrTextureFlagBit;
497 } else {
498 break;
499 }
500
501 } while (true);
502
503 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000504 }
505
bsalomonbcf0a522014-10-08 08:40:09 -0700506 GrTexture* texture = this->createNewScratchTexture(*desc);
507 SkASSERT(NULL == texture ||
508 texture->getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
509 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400510}
511
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000512bool GrContext::OverbudgetCB(void* data) {
bsalomon49f085d2014-09-05 13:34:00 -0700513 SkASSERT(data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000514
515 GrContext* context = reinterpret_cast<GrContext*>(data);
516
517 // Flush the InOrderDrawBuffer to possibly free up some textures
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000518 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000519
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000520 return true;
521}
522
523
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000524GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000525 void* srcData,
526 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000527 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529}
530
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000531void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
532 fResourceCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000535void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
536 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000537}
538
bsalomon@google.com91958362011-06-13 17:58:13 +0000539int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000540 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000541}
542
543int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000544 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545}
546
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000547int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000548 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000549}
550
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551///////////////////////////////////////////////////////////////////////////////
552
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000553GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
554 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000555}
556
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000557GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
558 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000559}
560
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000561///////////////////////////////////////////////////////////////////////////////
562
bsalomon@google.comb8670992012-07-25 21:27:09 +0000563bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000564 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000565 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000566 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567 return false;
568 }
569
tfarinaf9dae782014-06-06 06:35:28 -0700570 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571
572 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700573 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000574 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575 return false;
576 }
577 }
578 return true;
579}
580
bsalomon@google.com27847de2011-02-22 20:59:41 +0000581
bsalomon@google.com27847de2011-02-22 20:59:41 +0000582////////////////////////////////////////////////////////////////////////////////
583
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000584void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000585 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000586 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700587 GrRenderTarget* renderTarget) {
588 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000589 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000590 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700591 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
bsalomon41ebbdd2014-08-04 08:31:39 -0700592 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
593 if (NULL == target) {
594 return;
595 }
596 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597}
598
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000599void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000600 // set rect to be big enough to fill the space, but not super-huge, so we
601 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000602 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000603 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000604 SkIntToScalar(getRenderTarget()->width()),
605 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000606 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000607 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000608 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700609 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000611 // We attempt to map r by the inverse matrix and draw that. mapRect will
612 // map the four corners and bound them with a new rect. This will not
613 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000614 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000615 if (!fViewMatrix.invert(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000616 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 return;
618 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619 inverse.mapRect(&r);
620 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000621 if (!am.setIdentity(this, paint.writable())) {
622 GrPrintf("Could not invert matrix\n");
623 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000624 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000625 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000626 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000627 if (paint->isAntiAlias()) {
628 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000629 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000630 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631}
632
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000633#ifdef SK_DEVELOPER
634void GrContext::dumpFontCache() const {
635 fFontCache->dump();
636}
637#endif
638
bsalomon@google.com205d4602011-04-25 12:43:45 +0000639////////////////////////////////////////////////////////////////////////////////
640
bsalomon@google.com27847de2011-02-22 20:59:41 +0000641/* create a triangle strip that strokes the specified triangle. There are 8
642 unique vertices, but we repreat the last 2 to close up. Alternatively we
643 could use an indices array, and then only send 8 verts, but not sure that
644 would be faster.
645 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000646static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000647 SkScalar width) {
648 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000649 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650
651 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
652 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
653 verts[2].set(rect.fRight - rad, rect.fTop + rad);
654 verts[3].set(rect.fRight + rad, rect.fTop - rad);
655 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
656 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
657 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
658 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
659 verts[8] = verts[0];
660 verts[9] = verts[1];
661}
662
bsalomonc30aaa02014-08-13 07:15:29 -0700663static inline bool is_irect(const SkRect& r) {
664 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
665 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
666}
667
bsalomon@google.com205d4602011-04-25 12:43:45 +0000668static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000669 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000670 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000671 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700672 SkRect* devBoundRect) {
673 if (!target->getDrawState().canTweakAlphaForCoverage() &&
674 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000675#ifdef SK_DEBUG
bsalomon9c0822a2014-08-11 11:07:48 -0700676 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000677#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700678 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000679 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000680 const GrDrawState& drawState = target->getDrawState();
681 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000682 return false;
683 }
684
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000685#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000686 if (strokeWidth >= 0) {
687#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000688 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000689 return false;
690 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000691
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000692#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000693 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000694 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000695 return false;
696 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000697 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000698#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000700 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700701 if (strokeWidth < 0) {
702 return !is_irect(*devBoundRect);
703 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000704
bsalomon9c0822a2014-08-11 11:07:48 -0700705 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000706}
707
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000708static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
709 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
710 point.fY >= rect.fTop && point.fY <= rect.fBottom;
711}
712
bsalomon@google.com27847de2011-02-22 20:59:41 +0000713void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000714 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700715 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700716 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700717 SkPath path;
718 path.addRect(rect);
719 this->drawPath(paint, path, *strokeInfo);
720 return;
721 }
722
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000723 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000724 AutoCheckFlush acf(this);
725 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700726 if (NULL == target) {
727 return;
728 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000729
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000730 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700731 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700732 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000733
734 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
735 // cases where the RT is fully inside a stroke.
736 if (width < 0) {
737 SkRect rtRect;
738 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
739 SkRect clipSpaceRTRect = rtRect;
740 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700741 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000742 checkClip = true;
743 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
744 SkIntToScalar(this->getClip()->fOrigin.fY));
745 }
746 // Does the clip contain the entire RT?
747 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
748 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700749 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000750 return;
751 }
752 // Does the rect bound the RT?
753 SkPoint srcSpaceRTQuad[4];
754 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
755 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
756 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
757 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
758 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
759 // Will it blend?
760 GrColor clearColor;
761 if (paint.isOpaqueAndConstantColor(&clearColor)) {
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000762 target->clear(NULL, clearColor, true);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000763 return;
764 }
765 }
766 }
767 }
768
769 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000770 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000771 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700772 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700773
774 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
775
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000777 GrDrawState::AutoViewMatrixRestore avmr;
778 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000779 return;
780 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000781 if (width >= 0) {
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000782 fAARectRenderer->strokeAARect(this->getGpu(), target, rect,
bsalomon01c8da12014-08-04 09:21:30 -0700783 matrix, devBoundRect,
bsalomon9c0822a2014-08-11 11:07:48 -0700784 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000785 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000786 // filled AA rect
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000787 fAARectRenderer->fillAARect(this->getGpu(), target,
bsalomon9c0822a2014-08-11 11:07:48 -0700788 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000789 }
790 return;
791 }
792
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793 if (width >= 0) {
794 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000795 // Hairline could be done by just adding closing vertex to
796 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000797
bsalomon@google.com27847de2011-02-22 20:59:41 +0000798 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000799 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000800 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801
802 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000803 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000804 return;
805 }
806
807 GrPrimitiveType primType;
808 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000809 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000810
811 if (width > 0) {
812 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 setStrokeRectStrip(vertex, rect, width);
815 } else {
816 // hairline
817 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000818 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 vertex[0].set(rect.fLeft, rect.fTop);
820 vertex[1].set(rect.fRight, rect.fTop);
821 vertex[2].set(rect.fRight, rect.fBottom);
822 vertex[3].set(rect.fLeft, rect.fBottom);
823 vertex[4].set(rect.fLeft, rect.fTop);
824 }
825
bsalomon@google.com27847de2011-02-22 20:59:41 +0000826 target->drawNonIndexed(primType, 0, vertCount);
827 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000828 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700829 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830 }
831}
832
833void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000834 const SkRect& dstRect,
835 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000836 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000837 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000838 AutoCheckFlush acf(this);
839 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700840 if (NULL == target) {
841 return;
842 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000843
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000844 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
845
bsalomon01c8da12014-08-04 09:21:30 -0700846 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000847}
848
robertphillips@google.com42903302013-04-20 12:26:07 +0000849namespace {
850
851extern const GrVertexAttrib gPosUVColorAttribs[] = {
852 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000853 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding },
854 {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000855};
856
egdaniel7b3d5ee2014-08-28 05:41:14 -0700857static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint);
858static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
859
robertphillips@google.com42903302013-04-20 12:26:07 +0000860extern const GrVertexAttrib gPosColorAttribs[] = {
861 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000862 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000863};
864
egdaniel7b3d5ee2014-08-28 05:41:14 -0700865static const size_t kPosAttribsSize = sizeof(SkPoint);
866static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor);
867
robertphillips@google.com42903302013-04-20 12:26:07 +0000868static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000869 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000870 const GrColor* colors,
871 int* colorOffset,
872 int* texOffset) {
873 *texOffset = -1;
874 *colorOffset = -1;
875
bsalomon49f085d2014-09-05 13:34:00 -0700876 if (texCoords && colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000877 *texOffset = sizeof(SkPoint);
878 *colorOffset = 2*sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700879 drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700880 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000881 *texOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700882 drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700883 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000884 *colorOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700885 drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000886 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700887 drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000888 }
889}
890
891};
892
bsalomon@google.com27847de2011-02-22 20:59:41 +0000893void GrContext::drawVertices(const GrPaint& paint,
894 GrPrimitiveType primitiveType,
895 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000896 const SkPoint positions[],
897 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898 const GrColor colors[],
899 const uint16_t indices[],
900 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000901 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000902 AutoCheckFlush acf(this);
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000903 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
904
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000905 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700906 if (NULL == target) {
907 return;
908 }
egdaniele61c4112014-06-12 10:24:21 -0700909 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000911 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
912
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000913 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +0000914 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000915
egdaniel7b3d5ee2014-08-28 05:41:14 -0700916 size_t VertexStride = drawState->getVertexStride();
917 if (sizeof(SkPoint) != VertexStride) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000918 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000919 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000920 return;
921 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 void* curVertex = geo.vertices();
923
924 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000925 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000927 if (texOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000928 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000930 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
932 }
egdaniel7b3d5ee2014-08-28 05:41:14 -0700933 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934 }
935 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000936 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937 }
938
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000939 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000940 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000941
bsalomon49f085d2014-09-05 13:34:00 -0700942 if (indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000943 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000944 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000945 target->resetIndexSource();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000947 target->drawNonIndexed(primitiveType, 0, vertexCount);
948 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949}
950
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000951///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000952
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000953void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000954 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700955 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000956 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000957 return;
958 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000959
egdanield58a0ba2014-06-11 10:30:05 -0700960 if (strokeInfo.isDashed()) {
961 SkPath path;
962 path.addRRect(rrect);
963 this->drawPath(paint, path, strokeInfo);
964 return;
965 }
966
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000967 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000968 AutoCheckFlush acf(this);
969 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700970 if (NULL == target) {
971 return;
972 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000973
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000974 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
975
egdanield58a0ba2014-06-11 10:30:05 -0700976 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
977
978 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000979 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000980 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -0700981 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000982 }
983}
984
985///////////////////////////////////////////////////////////////////////////////
986
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000987void GrContext::drawDRRect(const GrPaint& paint,
988 const SkRRect& outer,
989 const SkRRect& inner) {
990 if (outer.isEmpty()) {
991 return;
992 }
993
994 AutoRestoreEffects are;
995 AutoCheckFlush acf(this);
996 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
997
998 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
999
1000 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
1001 SkPath path;
1002 path.addRRect(inner);
1003 path.addRRect(outer);
1004 path.setFillType(SkPath::kEvenOdd_FillType);
1005
egdanield58a0ba2014-06-11 10:30:05 -07001006 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001007 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
1008 }
1009}
1010
1011///////////////////////////////////////////////////////////////////////////////
1012
bsalomon@google.com93c96602012-04-27 13:05:21 +00001013void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001014 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001015 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001016 if (oval.isEmpty()) {
1017 return;
1018 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001019
egdanield58a0ba2014-06-11 10:30:05 -07001020 if (strokeInfo.isDashed()) {
1021 SkPath path;
1022 path.addOval(oval);
1023 this->drawPath(paint, path, strokeInfo);
1024 return;
1025 }
1026
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001027 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001028 AutoCheckFlush acf(this);
1029 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001030 if (NULL == target) {
1031 return;
1032 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001033
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001034 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1035
egdanield58a0ba2014-06-11 10:30:05 -07001036 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1037
1038
1039 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001040 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001041 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -07001042 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001043 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001044}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001045
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001046// Can 'path' be drawn as a pair of filled nested rectangles?
1047static bool is_nested_rects(GrDrawTarget* target,
1048 const SkPath& path,
1049 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001050 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001051 SkASSERT(stroke.isFillStyle());
1052
1053 if (path.isInverseFillType()) {
1054 return false;
1055 }
1056
1057 const GrDrawState& drawState = target->getDrawState();
1058
1059 // TODO: this restriction could be lifted if we were willing to apply
1060 // the matrix to all the points individually rather than just to the rect
1061 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1062 return false;
1063 }
1064
bsalomon9c0822a2014-08-11 11:07:48 -07001065 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1066 target->shouldDisableCoverageAAForBlend()) {
1067 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001068 }
1069
1070 SkPath::Direction dirs[2];
1071 if (!path.isNestedRects(rects, dirs)) {
1072 return false;
1073 }
1074
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001075 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001076 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001077 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001078 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001079
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001080 // Right now, nested rects where the margin is not the same width
1081 // all around do not render correctly
1082 const SkScalar* outer = rects[0].asScalars();
1083 const SkScalar* inner = rects[1].asScalars();
1084
robertphillips183e9852014-10-21 11:25:37 -07001085 bool allEq = true;
1086
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001087 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001088 bool allGoE1 = margin >= SK_Scalar1;
1089
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001090 for (int i = 1; i < 4; ++i) {
1091 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001092 if (temp < SK_Scalar1) {
1093 allGoE1 = false;
1094 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001095 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001096 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001097 }
1098 }
1099
robertphillips183e9852014-10-21 11:25:37 -07001100 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001101}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001102
egdanield58a0ba2014-06-11 10:30:05 -07001103void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001104
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001105 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001106 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001107 this->drawPaint(paint);
1108 }
1109 return;
1110 }
1111
egdanield58a0ba2014-06-11 10:30:05 -07001112 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001113 SkPoint pts[2];
1114 if (path.isLine(pts)) {
1115 AutoRestoreEffects are;
1116 AutoCheckFlush acf(this);
1117 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001118 if (NULL == target) {
1119 return;
1120 }
egdaniele61c4112014-06-12 10:24:21 -07001121 GrDrawState* drawState = target->drawState();
1122
1123 SkMatrix origViewMatrix = drawState->getViewMatrix();
1124 GrDrawState::AutoViewMatrixRestore avmr;
1125 if (avmr.setIdentity(target->drawState())) {
1126 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1127 origViewMatrix)) {
1128 return;
1129 }
1130 }
1131 }
1132
1133 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001134 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1135 SkTLazy<SkPath> effectPath;
1136 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1137 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1138 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1139 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1140 return;
1141 }
1142
1143 this->drawPath(paint, path, newStrokeInfo);
1144 return;
1145 }
1146
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001147 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001148 // Scratch textures can be recycled after they are returned to the texture
1149 // cache. This presents a potential hazard for buffered drawing. However,
1150 // the writePixels that uploads to the scratch will perform a flush so we're
1151 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001152 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001153 AutoCheckFlush acf(this);
1154 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001155 if (NULL == target) {
1156 return;
1157 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001158 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001159
egdaniel93a37bc2014-07-21 13:47:57 -07001160 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001161
egdanield58a0ba2014-06-11 10:30:05 -07001162 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1163
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001164 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1165
egdanield58a0ba2014-06-11 10:30:05 -07001166 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001167 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001168 SkRect rects[2];
1169
bsalomon9c0822a2014-08-11 11:07:48 -07001170 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001171 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001172 GrDrawState::AutoViewMatrixRestore avmr;
1173 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001174 return;
1175 }
1176
bsalomon9c0822a2014-08-11 11:07:48 -07001177 fAARectRenderer->fillAANestedRects(this->getGpu(), target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001178 return;
1179 }
1180 }
1181
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001182 SkRect ovalRect;
1183 bool isOval = path.isOval(&ovalRect);
1184
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001185 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001186 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1187 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001188 }
1189}
1190
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001191void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001192 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001193 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001194
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001195 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1196
1197
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001198 // An Assumption here is that path renderer would use some form of tweaking
1199 // the src color (either the input alpha or in the frag shader) to implement
1200 // aa. If we have some future driver-mojo path AA that can do the right
1201 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001202 bool useCoverageAA = useAA &&
1203 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1204 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001205
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001206
1207 GrPathRendererChain::DrawType type =
1208 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001209 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001210
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001211 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001212 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001213 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001214
1215 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001216 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001217
robertphillips@google.come79f3202014-02-11 16:30:21 +00001218 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001219 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001220 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001221 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1222 pathPtr = tmpPath.get();
1223 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001224 if (pathPtr->isEmpty()) {
1225 return;
1226 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001227 }
1228 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001229
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001230 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001231 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001232 }
1233
robertphillips@google.come79f3202014-02-11 16:30:21 +00001234 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001235#ifdef SK_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001236 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001237#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001238 return;
1239 }
1240
robertphillips@google.come79f3202014-02-11 16:30:21 +00001241 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001242}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001243
bsalomon@google.com27847de2011-02-22 20:59:41 +00001244////////////////////////////////////////////////////////////////////////////////
1245
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001246void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001247 if (NULL == fDrawBuffer) {
1248 return;
1249 }
1250
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001251 if (kDiscard_FlushBit & flagsBitfield) {
1252 fDrawBuffer->reset();
1253 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001254 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001255 }
bsalomonbcf0a522014-10-08 08:40:09 -07001256 fResourceCache->purgeAsNeeded();
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001257 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001258}
1259
bsalomon81beccc2014-10-13 12:32:55 -07001260bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1261 const void* inPixels, size_t outRowBytes, void* outPixels) {
1262 SkSrcPixelInfo srcPI;
1263 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001264 return false;
1265 }
bsalomon81beccc2014-10-13 12:32:55 -07001266 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1267 srcPI.fPixels = inPixels;
1268 srcPI.fRowBytes = inRowBytes;
1269
1270 SkDstPixelInfo dstPI;
1271 dstPI.fColorType = srcPI.fColorType;
1272 dstPI.fAlphaType = kPremul_SkAlphaType;
1273 dstPI.fPixels = outPixels;
1274 dstPI.fRowBytes = outRowBytes;
1275
1276 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001277}
1278
bsalomon81beccc2014-10-13 12:32:55 -07001279bool GrContext::writeSurfacePixels(GrSurface* surface,
1280 int left, int top, int width, int height,
1281 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1282 uint32_t pixelOpsFlags) {
1283
1284 {
1285 GrTexture* texture = NULL;
1286 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1287 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1288
1289 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1290 surface->surfacePriv().hasPendingIO()) {
1291 this->flush();
1292 }
1293 return fGpu->writeTexturePixels(texture, left, top, width, height,
1294 srcConfig, buffer, rowBytes);
1295 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1296 // upload is already flushed.
1297 }
1298 }
1299
1300 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1301 GrRenderTarget* renderTarget = surface->asRenderTarget();
1302 if (NULL == renderTarget) {
1303 return false;
1304 }
1305
1306 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1307 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1308 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1309 // when drawing the scratch to the dst using a conversion effect.
1310 bool swapRAndB = false;
1311 GrPixelConfig writeConfig = srcConfig;
1312 if (GrPixelConfigSwapRAndB(srcConfig) ==
1313 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1314 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1315 swapRAndB = true;
1316 }
1317
1318 GrTextureDesc desc;
1319 desc.fWidth = width;
1320 desc.fHeight = height;
1321 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001322 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1323 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001324 return false;
1325 }
1326
1327 SkAutoTUnref<const GrFragmentProcessor> fp;
1328 SkMatrix textureMatrix;
1329 textureMatrix.setIDiv(texture->width(), texture->height());
1330
1331 // allocate a tmp buffer and sw convert the pixels to premul
1332 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1333
1334 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1335 if (!GrPixelConfigIs8888(srcConfig)) {
1336 return false;
1337 }
1338 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1339 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1340 if (NULL == fp) {
1341 size_t tmpRowBytes = 4 * width;
1342 tmpPixels.reset(width * height);
1343 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1344 tmpPixels.get())) {
1345 return false;
1346 }
1347 rowBytes = tmpRowBytes;
1348 buffer = tmpPixels.get();
1349 }
1350 }
1351 if (NULL == fp) {
1352 fp.reset(GrConfigConversionEffect::Create(texture,
1353 swapRAndB,
1354 GrConfigConversionEffect::kNone_PMConversion,
1355 textureMatrix));
1356 }
1357
1358 // Even if the client told us not to flush, we still flush here. The client may have known that
1359 // writes to the original surface caused no data hazards, but they can't know that the scratch
1360 // we just got is safe.
1361 if (texture->surfacePriv().hasPendingIO()) {
1362 this->flush();
1363 }
1364 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1365 writeConfig, buffer, rowBytes)) {
1366 return false;
1367 }
1368
1369 SkMatrix matrix;
1370 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1371
1372 // This function can be called in the midst of drawing another object (e.g., when uploading a
1373 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1374 // drawing a rect to the render target.
1375 // The bracket ensures we pop the stack if we wind up flushing below.
1376 {
1377 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, kYes_BufferedDraw, NULL, NULL);
1378 GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit,
1379 &matrix);
1380 GrDrawState* drawState = drawTarget->drawState();
1381 drawState->addColorProcessor(fp);
1382 drawState->setRenderTarget(renderTarget);
1383 drawState->disableState(GrDrawState::kClip_StateBit);
1384 drawTarget->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
1385 }
1386
1387 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1388 this->flushSurfaceWrites(surface);
1389 }
1390
1391 return true;
1392}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001393
reed@google.com7111d462014-03-25 16:20:24 +00001394// toggles between RGBA and BGRA
1395static SkColorType toggle_colortype32(SkColorType ct) {
1396 if (kRGBA_8888_SkColorType == ct) {
1397 return kBGRA_8888_SkColorType;
1398 } else {
1399 SkASSERT(kBGRA_8888_SkColorType == ct);
1400 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001401 }
1402}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001403
bsalomon@google.com0342a852012-08-20 19:22:38 +00001404bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1405 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001406 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001407 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001408 ASSERT_OWNED_RESOURCE(target);
1409
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001410 if (NULL == target) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001411 target = fRenderTarget.get();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001412 if (NULL == target) {
1413 return false;
1414 }
1415 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001416
bsalomonafbf2d62014-09-30 12:18:44 -07001417 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001418 this->flush();
1419 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001420
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001421 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001422
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001423 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1424 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1425 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001426 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001427 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001428 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1429 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1430 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1431 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1432 // dstConfig.
1433 GrPixelConfig readConfig = dstConfig;
1434 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001435 if (GrPixelConfigSwapRAndB(dstConfig) ==
1436 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001437 readConfig = GrPixelConfigSwapRAndB(readConfig);
1438 swapRAndB = true;
1439 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001440
bsalomon@google.com0342a852012-08-20 19:22:38 +00001441 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001442
bsalomon@google.com9c680582013-02-06 18:17:50 +00001443 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001444 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001445 return false;
1446 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001447
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001448 // If the src is a texture and we would have to do conversions after read pixels, we instead
1449 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1450 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1451 // on the read back pixels.
1452 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001453 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001454 // Make the scratch a render so we can read its pixels.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001455 GrTextureDesc desc;
1456 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1457 desc.fWidth = width;
1458 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001459 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001460 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001461
bsalomon@google.com9c680582013-02-06 18:17:50 +00001462 // 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 +00001463 // match the passed rect. However, if we see many different size rectangles we will trash
1464 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1465 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001466 ScratchTexMatch match = kApprox_ScratchTexMatch;
1467 if (0 == left &&
1468 0 == top &&
1469 target->width() == width &&
1470 target->height() == height &&
1471 fGpu->fullReadPixelsIsFasterThanPartial()) {
1472 match = kExact_ScratchTexMatch;
1473 }
bsalomone3059732014-10-14 11:47:22 -07001474 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, match));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001475 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001476 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001477 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001478 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001479 textureMatrix.postIDiv(src->width(), src->height());
1480
joshualittb0a8a372014-09-23 09:50:21 -07001481 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001482 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001483 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1484 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001485 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001486 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001487 }
1488 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1489 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001490 if (fp || flipY || swapRAndB) {
1491 if (!fp) {
1492 fp.reset(GrConfigConversionEffect::Create(
1493 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1494 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001495 }
1496 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001497
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001498 // We protect the existing geometry here since it may not be
1499 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1500 // can be invoked in this method
1501 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001502 GrDrawState* drawState = fGpu->drawState();
joshualittb0a8a372014-09-23 09:50:21 -07001503 SkASSERT(fp);
1504 drawState->addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001505
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001506 drawState->setRenderTarget(texture->asRenderTarget());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001507 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon01c8da12014-08-04 09:21:30 -07001508 fGpu->drawSimpleRect(rect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001509 // we want to read back from the scratch's origin
1510 left = 0;
1511 top = 0;
1512 target = texture->asRenderTarget();
1513 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001514 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001515 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001516 if (!fGpu->readPixels(target,
1517 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001518 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001519 return false;
1520 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001521 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001522 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001523 SkDstPixelInfo dstPI;
1524 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1525 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001526 }
reed@google.com7111d462014-03-25 16:20:24 +00001527 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1528 dstPI.fPixels = buffer;
1529 dstPI.fRowBytes = rowBytes;
1530
1531 SkSrcPixelInfo srcPI;
1532 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1533 srcPI.fAlphaType = kPremul_SkAlphaType;
1534 srcPI.fPixels = buffer;
1535 srcPI.fRowBytes = rowBytes;
1536
1537 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001538 }
1539 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001540}
1541
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001542void GrContext::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001543 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001544 ASSERT_OWNED_RESOURCE(target);
1545 // In the future we may track whether there are any pending draws to this
1546 // target. We don't today so we always perform a flush. We don't promise
1547 // this to our clients, though.
1548 this->flush();
bsalomon49f085d2014-09-05 13:34:00 -07001549 if (fGpu) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001550 fGpu->resolveRenderTarget(target);
1551 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001552}
1553
bsalomon41ebbdd2014-08-04 08:31:39 -07001554void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1555 SkASSERT(renderTarget);
1556 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001557 AutoRestoreEffects are;
1558 AutoCheckFlush acf(this);
bsalomon41ebbdd2014-08-04 08:31:39 -07001559 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
1560 if (NULL == target) {
1561 return;
1562 }
1563 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001564}
1565
bsalomonf80bfed2014-10-07 05:56:02 -07001566void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1567 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001568 if (NULL == src || NULL == dst) {
1569 return;
1570 }
bsalomone3d4bf22014-09-23 09:15:03 -07001571 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001572 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001573
bsalomonf80bfed2014-10-07 05:56:02 -07001574 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1575 // here.
junov96c118e2014-09-26 13:09:47 -07001576
1577 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
1578 if (NULL == target) {
1579 return;
1580 }
junov96c118e2014-09-26 13:09:47 -07001581 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001582
1583 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1584 this->flush();
1585 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001586}
1587
bsalomonf80bfed2014-10-07 05:56:02 -07001588void GrContext::flushSurfaceWrites(GrSurface* surface) {
1589 if (surface->surfacePriv().hasPendingWrite()) {
1590 this->flush();
1591 }
1592}
1593
bsalomon@google.com27847de2011-02-22 20:59:41 +00001594////////////////////////////////////////////////////////////////////////////////
1595
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001596GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
1597 BufferedDraw buffered,
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001598 AutoRestoreEffects* are,
1599 AutoCheckFlush* acf) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001600 // All users of this draw state should be freeing up all effects when they're done.
1601 // Otherwise effects that own resources may keep those resources alive indefinitely.
joshualittbd769d02014-09-04 08:56:46 -07001602 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() &&
1603 !fDrawState->hasGeometryProcessor());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001604
bsalomon41ebbdd2014-08-04 08:31:39 -07001605 if (NULL == fGpu) {
1606 return NULL;
1607 }
1608
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001609 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +00001610 fDrawBuffer->flush();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001611 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001612 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001613 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon49f085d2014-09-05 13:34:00 -07001614 if (paint) {
1615 SkASSERT(are);
1616 SkASSERT(acf);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001617 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001618 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001619#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1620 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001621 !fDrawState->couldApplyCoverage(fGpu->caps())) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001622 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1623 }
1624#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001625 // Clear any vertex attributes configured for the previous use of the
1626 // GrDrawState which can effect which blend optimizations are in effect.
1627 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001628 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001629 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001630 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001631 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001632 GrDrawTarget* target;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001633 if (kYes_BufferedDraw == buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001634 fLastDrawWasBuffered = kYes_BufferedDraw;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001635 target = fDrawBuffer;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001636 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001637 SkASSERT(kNo_BufferedDraw == buffered);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001638 fLastDrawWasBuffered = kNo_BufferedDraw;
1639 target = fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640 }
bsalomon49f085d2014-09-05 13:34:00 -07001641 fDrawState->setState(GrDrawState::kClip_StateBit, fClip &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001642 !fClip->fClipStack->isWideOpen());
1643 target->setClip(fClip);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001644 SkASSERT(fDrawState == target->drawState());
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001645 return target;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646}
1647
robertphillips@google.com72176b22012-05-23 13:19:12 +00001648/*
1649 * This method finds a path renderer that can draw the specified path on
1650 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001651 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001652 * can be individually allowed/disallowed via the "allowSW" boolean.
1653 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001654GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001655 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001656 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001657 bool allowSW,
1658 GrPathRendererChain::DrawType drawType,
1659 GrPathRendererChain::StencilSupport* stencilSupport) {
1660
bsalomon@google.com30085192011-08-19 15:42:31 +00001661 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001662 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001663 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001664
sugoi@google.com12b4e272012-12-06 20:13:11 +00001665 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1666 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001667 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001668 drawType,
1669 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001670
1671 if (NULL == pr && allowSW) {
1672 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001673 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001674 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001675 pr = fSoftwarePathRenderer;
1676 }
1677
1678 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001679}
1680
bsalomon@google.com27847de2011-02-22 20:59:41 +00001681////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001682bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1683 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001684}
1685
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001686int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1687 SkScalar dpi) const {
1688 if (!this->isConfigRenderable(config, true)) {
1689 return 0;
1690 }
1691 int chosenSampleCount = 0;
1692 if (fGpu->caps()->pathRenderingSupport()) {
1693 if (dpi >= 250.0f) {
1694 chosenSampleCount = 4;
1695 } else {
1696 chosenSampleCount = 16;
1697 }
1698 }
1699 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1700 chosenSampleCount : 0;
1701}
1702
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001703void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001704 SkASSERT(NULL == fDrawBuffer);
1705 SkASSERT(NULL == fDrawBufferVBAllocPool);
1706 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001707
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001708 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001709 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001710 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001711 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001712 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001713 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001714 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001715 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001716
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001717 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001718 fDrawBufferVBAllocPool,
1719 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001720
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001721 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001722}
1723
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001724GrDrawTarget* GrContext::getTextTarget() {
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001725 return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001726}
1727
1728const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1729 return fGpu->getQuadIndexBuffer();
1730}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001731
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001732namespace {
1733void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1734 GrConfigConversionEffect::PMConversion pmToUPM;
1735 GrConfigConversionEffect::PMConversion upmToPM;
1736 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1737 *pmToUPMValue = pmToUPM;
1738 *upmToPMValue = upmToPM;
1739}
1740}
1741
joshualittb0a8a372014-09-23 09:50:21 -07001742const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1743 bool swapRAndB,
1744 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001745 if (!fDidTestPMConversions) {
1746 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001747 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001748 }
1749 GrConfigConversionEffect::PMConversion pmToUPM =
1750 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1751 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001752 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001753 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001754 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001755 }
1756}
1757
joshualittb0a8a372014-09-23 09:50:21 -07001758const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1759 bool swapRAndB,
1760 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001761 if (!fDidTestPMConversions) {
1762 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001763 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001764 }
1765 GrConfigConversionEffect::PMConversion upmToPM =
1766 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1767 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001768 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001769 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001770 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001771 }
1772}
1773
bsalomon6d3fe022014-07-25 08:35:45 -07001774void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001775 fResourceCache->addResource(resourceKey, resource);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001776}
1777
bsalomon6d3fe022014-07-25 08:35:45 -07001778GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
1779 GrGpuResource* resource = fResourceCache->find(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001780 SkSafeRef(resource);
1781 return resource;
1782}
1783
egdanielbbcb38d2014-06-19 10:19:29 -07001784void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1785 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001786 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001787 fDrawBuffer->addGpuTraceMarker(marker);
1788 }
1789}
1790
1791void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1792 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001793 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001794 fDrawBuffer->removeGpuTraceMarker(marker);
1795 }
1796}
1797
bsalomon@google.comc4364992011-11-07 15:54:49 +00001798///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001799#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001800void GrContext::printCacheStats() const {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001801 fResourceCache->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001802}
1803#endif
robertphillips754f4e92014-09-18 13:52:08 -07001804
1805#if GR_GPU_STATS
1806const GrContext::GPUStats* GrContext::gpuStats() const {
1807 return fGpu->gpuStats();
1808}
1809#endif
1810