blob: d5b211678b3087920f8ca25d13d6f43be2019970 [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) {
254 if (fGpu->caps()->pathRenderingSupport()) {
255 if (renderTarget->getStencilBuffer() && renderTarget->isMultisampled()) {
256 return SkNEW_ARGS(GrStencilAndCoverTextContext, (this, leakyProperties));
257 }
258 }
259 return SkNEW_ARGS(GrDistanceFieldTextContext, (this, leakyProperties,
260 enableDistanceFieldFonts));
261}
262
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000263////////////////////////////////////////////////////////////////////////////////
264
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000265GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
266 const GrCacheID& cacheID,
267 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700268 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon6d3fe022014-07-25 08:35:45 -0700269 GrGpuResource* resource = fResourceCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000270 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000271 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000272}
273
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000274bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000275 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000276 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700277 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000278 return fResourceCache->hasKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000279}
280
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000281void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000282 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000283
284 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
285 sb->height(),
286 sb->numSamples());
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000287 fResourceCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000288}
289
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000290GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000291 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000292 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
293 height,
294 sampleCnt);
bsalomon6d3fe022014-07-25 08:35:45 -0700295 GrGpuResource* resource = fResourceCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000296 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000297}
298
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000299static void stretch_image(void* dst,
300 int dstW,
301 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000302 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000303 int srcW,
304 int srcH,
305 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000306 SkFixed dx = (srcW << 16) / dstW;
307 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000308
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000309 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310
robertphillips@google.com8b169312013-10-15 17:47:36 +0000311 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000312 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000313 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000314 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
315 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000316 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000317 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000318 x += dx;
319 }
320 y += dy;
321 }
322}
323
robertphillips@google.com42903302013-04-20 12:26:07 +0000324namespace {
325
326// position + local coordinate
327extern const GrVertexAttrib gVertexAttribs[] = {
328 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000329 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000330};
331
332};
333
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000334// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000335// the current hardware. Resize the texture to be a POT
336GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000337 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000338 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000339 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000340 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000341 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000342 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000343 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000344
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000345 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000346 return NULL;
347 }
348 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000349
robertphillips@google.com3319f332012-08-13 18:00:36 +0000350 GrTextureDesc rtDesc = desc;
351 rtDesc.fFlags = rtDesc.fFlags |
352 kRenderTarget_GrTextureFlagBit |
353 kNoStencil_GrTextureFlagBit;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000354 rtDesc.fWidth = GrNextPow2(desc.fWidth);
355 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000356
357 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
358
bsalomon49f085d2014-09-05 13:34:00 -0700359 if (texture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000360 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
361 GrDrawState* drawState = fGpu->drawState();
362 drawState->setRenderTarget(texture->asRenderTarget());
363
364 // if filtering is not desired then we want to ensure all
365 // texels in the resampled image are copies of texels from
366 // the original.
humper@google.comb86add12013-07-25 18:49:07 +0000367 GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTextureParams::kBilerp_FilterMode :
368 GrTextureParams::kNone_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700369 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000370
egdaniel7b3d5ee2014-08-28 05:41:14 -0700371 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs),
372 2 * sizeof(SkPoint));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000373
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000374 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000375
376 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000377 SkPoint* verts = (SkPoint*) arg.vertices();
378 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
379 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000380 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000381 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000382 } else {
383 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000384 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000385 // not. Either implement filtered stretch blit on CPU or just create
386 // one when FBO case fails.
387
388 rtDesc.fFlags = kNone_GrTextureFlags;
389 // no longer need to clamp at min RT size.
390 rtDesc.fWidth = GrNextPow2(desc.fWidth);
391 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000392
393 // We shouldn't be resizing a compressed texture.
394 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
395
robertphillips@google.com8b169312013-10-15 17:47:36 +0000396 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700397 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000398 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
399 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000400
401 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
402
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000403 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700404 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000405 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000406
407 return texture;
408}
409
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000410GrTexture* GrContext::createTexture(const GrTextureParams* params,
411 const GrTextureDesc& desc,
412 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000413 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000414 size_t rowBytes,
415 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700416 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000417
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000418 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700419 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700420 // We do not know how to resize compressed textures.
421 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
422
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000423 texture = this->createResizedTexture(desc, cacheID,
424 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700425 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000426 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700427 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000428 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000429
bsalomon49f085d2014-09-05 13:34:00 -0700430 if (texture) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000431 fResourceCache->addResource(resourceKey, texture);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000432
bsalomon49f085d2014-09-05 13:34:00 -0700433 if (cacheKey) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000434 *cacheKey = resourceKey;
435 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000436 }
437
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000438 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000439}
440
bsalomonbcf0a522014-10-08 08:40:09 -0700441GrTexture* GrContext::createNewScratchTexture(const GrTextureDesc& desc) {
442 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
443 if (!texture) {
444 return NULL;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000445 }
bsalomonbcf0a522014-10-08 08:40:09 -0700446 fResourceCache->addResource(texture->getScratchKey(), texture);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400447 return texture;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000448}
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000449
bsalomonbcf0a522014-10-08 08:40:09 -0700450GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match,
451 bool calledDuringFlush) {
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000452
bsalomonbcf0a522014-10-08 08:40:09 -0700453 // kNoStencil has no meaning if kRT isn't set.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000454 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000455 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit));
456
bsalomonbcf0a522014-10-08 08:40:09 -0700457 // Make sure caller has checked for renderability if kRT is set.
458 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
459 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000460
bsalomonbcf0a522014-10-08 08:40:09 -0700461 SkTCopyOnFirstWrite<GrTextureDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000462
bsalomonbcf0a522014-10-08 08:40:09 -0700463 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrTextureFlagBit)) {
464 GrTextureFlags origFlags = desc->fFlags;
465 if (kApprox_ScratchTexMatch == match) {
466 // bin by pow2 with a reasonable min
467 static const int MIN_SIZE = 16;
468 GrTextureDesc* wdesc = desc.writable();
469 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
470 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000471 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000472
bsalomonbcf0a522014-10-08 08:40:09 -0700473 do {
474 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
475 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key,
476 calledDuringFlush);
477 if (resource) {
478 fResourceCache->makeResourceMRU(resource);
479 return static_cast<GrTexture*>(resource);
480 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000481
bsalomonbcf0a522014-10-08 08:40:09 -0700482 if (kExact_ScratchTexMatch == match) {
483 break;
484 }
485 // We had a cache miss and we are in approx mode, relax the fit of the flags.
486
487 // We no longer try to reuse textures that were previously used as render targets in
488 // situations where no RT is needed; doing otherwise can confuse the video driver and
489 // cause significant performance problems in some cases.
490 if (desc->fFlags & kNoStencil_GrTextureFlagBit) {
491 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrTextureFlagBit;
492 } else {
493 break;
494 }
495
496 } while (true);
497
498 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000499 }
500
bsalomonbcf0a522014-10-08 08:40:09 -0700501 GrTexture* texture = this->createNewScratchTexture(*desc);
502 SkASSERT(NULL == texture ||
503 texture->getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
504 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400505}
506
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000507bool GrContext::OverbudgetCB(void* data) {
bsalomon49f085d2014-09-05 13:34:00 -0700508 SkASSERT(data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000509
510 GrContext* context = reinterpret_cast<GrContext*>(data);
511
512 // Flush the InOrderDrawBuffer to possibly free up some textures
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000513 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000514
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000515 return true;
516}
517
518
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000519GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520 void* srcData,
521 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000522 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000523 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000524}
525
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000526void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
527 fResourceCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000528}
529
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000530void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
531 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532}
533
bsalomon@google.com91958362011-06-13 17:58:13 +0000534int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000535 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000536}
537
538int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000539 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000540}
541
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000542int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000543 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000544}
545
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546///////////////////////////////////////////////////////////////////////////////
547
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000548GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
549 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000550}
551
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000552GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
553 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000554}
555
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000556///////////////////////////////////////////////////////////////////////////////
557
bsalomon@google.comb8670992012-07-25 21:27:09 +0000558bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000559 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000560 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000561 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000562 return false;
563 }
564
tfarinaf9dae782014-06-06 06:35:28 -0700565 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566
567 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700568 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000569 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000570 return false;
571 }
572 }
573 return true;
574}
575
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577////////////////////////////////////////////////////////////////////////////////
578
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000579void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000580 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000581 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700582 GrRenderTarget* renderTarget) {
583 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000584 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000585 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700586 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
bsalomon41ebbdd2014-08-04 08:31:39 -0700587 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
588 if (NULL == target) {
589 return;
590 }
591 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000592}
593
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000594void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595 // set rect to be big enough to fill the space, but not super-huge, so we
596 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000597 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000598 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000599 SkIntToScalar(getRenderTarget()->width()),
600 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000601 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000602 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000603 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700604 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000606 // We attempt to map r by the inverse matrix and draw that. mapRect will
607 // map the four corners and bound them with a new rect. This will not
608 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000609 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000610 if (!fViewMatrix.invert(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000611 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 return;
613 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000614 inverse.mapRect(&r);
615 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000616 if (!am.setIdentity(this, paint.writable())) {
617 GrPrintf("Could not invert matrix\n");
618 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000620 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000621 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000622 if (paint->isAntiAlias()) {
623 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000624 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000625 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000626}
627
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000628#ifdef SK_DEVELOPER
629void GrContext::dumpFontCache() const {
630 fFontCache->dump();
631}
632#endif
633
bsalomon@google.com205d4602011-04-25 12:43:45 +0000634////////////////////////////////////////////////////////////////////////////////
635
bsalomon@google.com27847de2011-02-22 20:59:41 +0000636/* create a triangle strip that strokes the specified triangle. There are 8
637 unique vertices, but we repreat the last 2 to close up. Alternatively we
638 could use an indices array, and then only send 8 verts, but not sure that
639 would be faster.
640 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000641static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000642 SkScalar width) {
643 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000644 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000645
646 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
647 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
648 verts[2].set(rect.fRight - rad, rect.fTop + rad);
649 verts[3].set(rect.fRight + rad, rect.fTop - rad);
650 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
651 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
652 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
653 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
654 verts[8] = verts[0];
655 verts[9] = verts[1];
656}
657
bsalomonc30aaa02014-08-13 07:15:29 -0700658static inline bool is_irect(const SkRect& r) {
659 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
660 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
661}
662
bsalomon@google.com205d4602011-04-25 12:43:45 +0000663static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000664 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000665 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000666 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700667 SkRect* devBoundRect) {
668 if (!target->getDrawState().canTweakAlphaForCoverage() &&
669 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000670#ifdef SK_DEBUG
bsalomon9c0822a2014-08-11 11:07:48 -0700671 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000672#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700673 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000674 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000675 const GrDrawState& drawState = target->getDrawState();
676 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000677 return false;
678 }
679
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000680#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000681 if (strokeWidth >= 0) {
682#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000683 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000684 return false;
685 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000686
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000687#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000688 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000689 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000690 return false;
691 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000692 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000693#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000694
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000695 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700696 if (strokeWidth < 0) {
697 return !is_irect(*devBoundRect);
698 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000699
bsalomon9c0822a2014-08-11 11:07:48 -0700700 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000701}
702
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000703static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
704 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
705 point.fY >= rect.fTop && point.fY <= rect.fBottom;
706}
707
bsalomon@google.com27847de2011-02-22 20:59:41 +0000708void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000709 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700710 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700711 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700712 SkPath path;
713 path.addRect(rect);
714 this->drawPath(paint, path, *strokeInfo);
715 return;
716 }
717
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000718 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000719 AutoCheckFlush acf(this);
720 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700721 if (NULL == target) {
722 return;
723 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000724
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000725 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700726 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700727 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000728
729 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
730 // cases where the RT is fully inside a stroke.
731 if (width < 0) {
732 SkRect rtRect;
733 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
734 SkRect clipSpaceRTRect = rtRect;
735 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700736 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000737 checkClip = true;
738 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
739 SkIntToScalar(this->getClip()->fOrigin.fY));
740 }
741 // Does the clip contain the entire RT?
742 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
743 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700744 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000745 return;
746 }
747 // Does the rect bound the RT?
748 SkPoint srcSpaceRTQuad[4];
749 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
750 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
751 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
752 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
753 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
754 // Will it blend?
755 GrColor clearColor;
756 if (paint.isOpaqueAndConstantColor(&clearColor)) {
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000757 target->clear(NULL, clearColor, true);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000758 return;
759 }
760 }
761 }
762 }
763
764 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000765 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000766 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700767 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700768
769 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
770
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000772 GrDrawState::AutoViewMatrixRestore avmr;
773 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000774 return;
775 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776 if (width >= 0) {
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000777 fAARectRenderer->strokeAARect(this->getGpu(), target, rect,
bsalomon01c8da12014-08-04 09:21:30 -0700778 matrix, devBoundRect,
bsalomon9c0822a2014-08-11 11:07:48 -0700779 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000780 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000781 // filled AA rect
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000782 fAARectRenderer->fillAARect(this->getGpu(), target,
bsalomon9c0822a2014-08-11 11:07:48 -0700783 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000784 }
785 return;
786 }
787
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 if (width >= 0) {
789 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000790 // Hairline could be done by just adding closing vertex to
791 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000792
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000794 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000795 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796
797 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000798 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000799 return;
800 }
801
802 GrPrimitiveType primType;
803 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000804 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805
806 if (width > 0) {
807 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000809 setStrokeRectStrip(vertex, rect, width);
810 } else {
811 // hairline
812 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 vertex[0].set(rect.fLeft, rect.fTop);
815 vertex[1].set(rect.fRight, rect.fTop);
816 vertex[2].set(rect.fRight, rect.fBottom);
817 vertex[3].set(rect.fLeft, rect.fBottom);
818 vertex[4].set(rect.fLeft, rect.fTop);
819 }
820
bsalomon@google.com27847de2011-02-22 20:59:41 +0000821 target->drawNonIndexed(primType, 0, vertCount);
822 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000823 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700824 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 }
826}
827
828void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000829 const SkRect& dstRect,
830 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000831 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000832 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000833 AutoCheckFlush acf(this);
834 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700835 if (NULL == target) {
836 return;
837 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000838
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000839 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
840
bsalomon01c8da12014-08-04 09:21:30 -0700841 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000842}
843
robertphillips@google.com42903302013-04-20 12:26:07 +0000844namespace {
845
846extern const GrVertexAttrib gPosUVColorAttribs[] = {
847 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000848 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding },
849 {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000850};
851
egdaniel7b3d5ee2014-08-28 05:41:14 -0700852static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint);
853static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
854
robertphillips@google.com42903302013-04-20 12:26:07 +0000855extern const GrVertexAttrib gPosColorAttribs[] = {
856 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000857 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000858};
859
egdaniel7b3d5ee2014-08-28 05:41:14 -0700860static const size_t kPosAttribsSize = sizeof(SkPoint);
861static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor);
862
robertphillips@google.com42903302013-04-20 12:26:07 +0000863static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000864 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000865 const GrColor* colors,
866 int* colorOffset,
867 int* texOffset) {
868 *texOffset = -1;
869 *colorOffset = -1;
870
bsalomon49f085d2014-09-05 13:34:00 -0700871 if (texCoords && colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000872 *texOffset = sizeof(SkPoint);
873 *colorOffset = 2*sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700874 drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700875 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000876 *texOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700877 drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700878 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000879 *colorOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700880 drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000881 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700882 drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000883 }
884}
885
886};
887
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888void GrContext::drawVertices(const GrPaint& paint,
889 GrPrimitiveType primitiveType,
890 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000891 const SkPoint positions[],
892 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000893 const GrColor colors[],
894 const uint16_t indices[],
895 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000896 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000897 AutoCheckFlush acf(this);
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000898 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
899
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000900 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700901 if (NULL == target) {
902 return;
903 }
egdaniele61c4112014-06-12 10:24:21 -0700904 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000906 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
907
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000908 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +0000909 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000910
egdaniel7b3d5ee2014-08-28 05:41:14 -0700911 size_t VertexStride = drawState->getVertexStride();
912 if (sizeof(SkPoint) != VertexStride) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000913 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000914 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000915 return;
916 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000917 void* curVertex = geo.vertices();
918
919 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000920 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000921
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000922 if (texOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000923 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000925 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
927 }
egdaniel7b3d5ee2014-08-28 05:41:14 -0700928 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 }
930 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000931 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000932 }
933
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000934 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000935 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000936
bsalomon49f085d2014-09-05 13:34:00 -0700937 if (indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000938 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000939 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000940 target->resetIndexSource();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000941 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000942 target->drawNonIndexed(primitiveType, 0, vertexCount);
943 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944}
945
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000946///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000947
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000948void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000949 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700950 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000951 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000952 return;
953 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000954
egdanield58a0ba2014-06-11 10:30:05 -0700955 if (strokeInfo.isDashed()) {
956 SkPath path;
957 path.addRRect(rrect);
958 this->drawPath(paint, path, strokeInfo);
959 return;
960 }
961
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000962 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000963 AutoCheckFlush acf(this);
964 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700965 if (NULL == target) {
966 return;
967 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000968
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000969 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
970
egdanield58a0ba2014-06-11 10:30:05 -0700971 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
972
973 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000974 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000975 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -0700976 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000977 }
978}
979
980///////////////////////////////////////////////////////////////////////////////
981
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000982void GrContext::drawDRRect(const GrPaint& paint,
983 const SkRRect& outer,
984 const SkRRect& inner) {
985 if (outer.isEmpty()) {
986 return;
987 }
988
989 AutoRestoreEffects are;
990 AutoCheckFlush acf(this);
991 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
992
993 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
994
995 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
996 SkPath path;
997 path.addRRect(inner);
998 path.addRRect(outer);
999 path.setFillType(SkPath::kEvenOdd_FillType);
1000
egdanield58a0ba2014-06-11 10:30:05 -07001001 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001002 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
1003 }
1004}
1005
1006///////////////////////////////////////////////////////////////////////////////
1007
bsalomon@google.com93c96602012-04-27 13:05:21 +00001008void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001009 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001010 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001011 if (oval.isEmpty()) {
1012 return;
1013 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001014
egdanield58a0ba2014-06-11 10:30:05 -07001015 if (strokeInfo.isDashed()) {
1016 SkPath path;
1017 path.addOval(oval);
1018 this->drawPath(paint, path, strokeInfo);
1019 return;
1020 }
1021
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001022 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001023 AutoCheckFlush acf(this);
1024 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001025 if (NULL == target) {
1026 return;
1027 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001028
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001029 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1030
egdanield58a0ba2014-06-11 10:30:05 -07001031 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1032
1033
1034 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001035 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001036 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -07001037 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001038 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001039}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001040
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001041// Can 'path' be drawn as a pair of filled nested rectangles?
1042static bool is_nested_rects(GrDrawTarget* target,
1043 const SkPath& path,
1044 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001045 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001046 SkASSERT(stroke.isFillStyle());
1047
1048 if (path.isInverseFillType()) {
1049 return false;
1050 }
1051
1052 const GrDrawState& drawState = target->getDrawState();
1053
1054 // TODO: this restriction could be lifted if we were willing to apply
1055 // the matrix to all the points individually rather than just to the rect
1056 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1057 return false;
1058 }
1059
bsalomon9c0822a2014-08-11 11:07:48 -07001060 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1061 target->shouldDisableCoverageAAForBlend()) {
1062 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001063 }
1064
1065 SkPath::Direction dirs[2];
1066 if (!path.isNestedRects(rects, dirs)) {
1067 return false;
1068 }
1069
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001070 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001071 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001072 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001073 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001074
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001075 // Right now, nested rects where the margin is not the same width
1076 // all around do not render correctly
1077 const SkScalar* outer = rects[0].asScalars();
1078 const SkScalar* inner = rects[1].asScalars();
1079
1080 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1081 for (int i = 1; i < 4; ++i) {
1082 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1083 if (!SkScalarNearlyEqual(margin, temp)) {
1084 return false;
1085 }
1086 }
1087
1088 return true;
1089}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001090
egdanield58a0ba2014-06-11 10:30:05 -07001091void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001092
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001093 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001094 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001095 this->drawPaint(paint);
1096 }
1097 return;
1098 }
1099
egdanield58a0ba2014-06-11 10:30:05 -07001100 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001101 SkPoint pts[2];
1102 if (path.isLine(pts)) {
1103 AutoRestoreEffects are;
1104 AutoCheckFlush acf(this);
1105 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001106 if (NULL == target) {
1107 return;
1108 }
egdaniele61c4112014-06-12 10:24:21 -07001109 GrDrawState* drawState = target->drawState();
1110
1111 SkMatrix origViewMatrix = drawState->getViewMatrix();
1112 GrDrawState::AutoViewMatrixRestore avmr;
1113 if (avmr.setIdentity(target->drawState())) {
1114 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1115 origViewMatrix)) {
1116 return;
1117 }
1118 }
1119 }
1120
1121 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001122 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1123 SkTLazy<SkPath> effectPath;
1124 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1125 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1126 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1127 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1128 return;
1129 }
1130
1131 this->drawPath(paint, path, newStrokeInfo);
1132 return;
1133 }
1134
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001135 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001136 // Scratch textures can be recycled after they are returned to the texture
1137 // cache. This presents a potential hazard for buffered drawing. However,
1138 // the writePixels that uploads to the scratch will perform a flush so we're
1139 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001140 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001141 AutoCheckFlush acf(this);
1142 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001143 if (NULL == target) {
1144 return;
1145 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001146 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001147
egdaniel93a37bc2014-07-21 13:47:57 -07001148 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001149
egdanield58a0ba2014-06-11 10:30:05 -07001150 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1151
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001152 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1153
egdanield58a0ba2014-06-11 10:30:05 -07001154 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001155 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001156 SkRect rects[2];
1157
bsalomon9c0822a2014-08-11 11:07:48 -07001158 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001159 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001160 GrDrawState::AutoViewMatrixRestore avmr;
1161 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001162 return;
1163 }
1164
bsalomon9c0822a2014-08-11 11:07:48 -07001165 fAARectRenderer->fillAANestedRects(this->getGpu(), target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001166 return;
1167 }
1168 }
1169
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001170 SkRect ovalRect;
1171 bool isOval = path.isOval(&ovalRect);
1172
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001173 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001174 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1175 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001176 }
1177}
1178
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001179void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001180 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001181 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001182
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001183 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1184
1185
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001186 // An Assumption here is that path renderer would use some form of tweaking
1187 // the src color (either the input alpha or in the frag shader) to implement
1188 // aa. If we have some future driver-mojo path AA that can do the right
1189 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001190 bool useCoverageAA = useAA &&
1191 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1192 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001193
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001194
1195 GrPathRendererChain::DrawType type =
1196 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001197 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001198
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001199 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001200 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001201 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001202
1203 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001204 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001205
robertphillips@google.come79f3202014-02-11 16:30:21 +00001206 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001207 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001208 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001209 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1210 pathPtr = tmpPath.get();
1211 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001212 if (pathPtr->isEmpty()) {
1213 return;
1214 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001215 }
1216 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001217
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001218 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001219 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001220 }
1221
robertphillips@google.come79f3202014-02-11 16:30:21 +00001222 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001223#ifdef SK_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001224 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001225#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001226 return;
1227 }
1228
robertphillips@google.come79f3202014-02-11 16:30:21 +00001229 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001230}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001231
bsalomon@google.com27847de2011-02-22 20:59:41 +00001232////////////////////////////////////////////////////////////////////////////////
1233
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001234void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001235 if (NULL == fDrawBuffer) {
1236 return;
1237 }
1238
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001239 if (kDiscard_FlushBit & flagsBitfield) {
1240 fDrawBuffer->reset();
1241 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001242 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001243 }
bsalomonbcf0a522014-10-08 08:40:09 -07001244 fResourceCache->purgeAsNeeded();
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001245 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001246}
1247
bsalomon81beccc2014-10-13 12:32:55 -07001248bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1249 const void* inPixels, size_t outRowBytes, void* outPixels) {
1250 SkSrcPixelInfo srcPI;
1251 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001252 return false;
1253 }
bsalomon81beccc2014-10-13 12:32:55 -07001254 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1255 srcPI.fPixels = inPixels;
1256 srcPI.fRowBytes = inRowBytes;
1257
1258 SkDstPixelInfo dstPI;
1259 dstPI.fColorType = srcPI.fColorType;
1260 dstPI.fAlphaType = kPremul_SkAlphaType;
1261 dstPI.fPixels = outPixels;
1262 dstPI.fRowBytes = outRowBytes;
1263
1264 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001265}
1266
bsalomon81beccc2014-10-13 12:32:55 -07001267bool GrContext::writeSurfacePixels(GrSurface* surface,
1268 int left, int top, int width, int height,
1269 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1270 uint32_t pixelOpsFlags) {
1271
1272 {
1273 GrTexture* texture = NULL;
1274 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1275 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1276
1277 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1278 surface->surfacePriv().hasPendingIO()) {
1279 this->flush();
1280 }
1281 return fGpu->writeTexturePixels(texture, left, top, width, height,
1282 srcConfig, buffer, rowBytes);
1283 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1284 // upload is already flushed.
1285 }
1286 }
1287
1288 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1289 GrRenderTarget* renderTarget = surface->asRenderTarget();
1290 if (NULL == renderTarget) {
1291 return false;
1292 }
1293
1294 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1295 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1296 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1297 // when drawing the scratch to the dst using a conversion effect.
1298 bool swapRAndB = false;
1299 GrPixelConfig writeConfig = srcConfig;
1300 if (GrPixelConfigSwapRAndB(srcConfig) ==
1301 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1302 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1303 swapRAndB = true;
1304 }
1305
1306 GrTextureDesc desc;
1307 desc.fWidth = width;
1308 desc.fHeight = height;
1309 desc.fConfig = writeConfig;
1310 GrAutoScratchTexture ast(this, desc);
1311 GrTexture* texture = ast.texture();
1312 if (NULL == texture) {
1313 return false;
1314 }
1315
1316 SkAutoTUnref<const GrFragmentProcessor> fp;
1317 SkMatrix textureMatrix;
1318 textureMatrix.setIDiv(texture->width(), texture->height());
1319
1320 // allocate a tmp buffer and sw convert the pixels to premul
1321 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1322
1323 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1324 if (!GrPixelConfigIs8888(srcConfig)) {
1325 return false;
1326 }
1327 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1328 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1329 if (NULL == fp) {
1330 size_t tmpRowBytes = 4 * width;
1331 tmpPixels.reset(width * height);
1332 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1333 tmpPixels.get())) {
1334 return false;
1335 }
1336 rowBytes = tmpRowBytes;
1337 buffer = tmpPixels.get();
1338 }
1339 }
1340 if (NULL == fp) {
1341 fp.reset(GrConfigConversionEffect::Create(texture,
1342 swapRAndB,
1343 GrConfigConversionEffect::kNone_PMConversion,
1344 textureMatrix));
1345 }
1346
1347 // Even if the client told us not to flush, we still flush here. The client may have known that
1348 // writes to the original surface caused no data hazards, but they can't know that the scratch
1349 // we just got is safe.
1350 if (texture->surfacePriv().hasPendingIO()) {
1351 this->flush();
1352 }
1353 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1354 writeConfig, buffer, rowBytes)) {
1355 return false;
1356 }
1357
1358 SkMatrix matrix;
1359 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1360
1361 // This function can be called in the midst of drawing another object (e.g., when uploading a
1362 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1363 // drawing a rect to the render target.
1364 // The bracket ensures we pop the stack if we wind up flushing below.
1365 {
1366 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, kYes_BufferedDraw, NULL, NULL);
1367 GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit,
1368 &matrix);
1369 GrDrawState* drawState = drawTarget->drawState();
1370 drawState->addColorProcessor(fp);
1371 drawState->setRenderTarget(renderTarget);
1372 drawState->disableState(GrDrawState::kClip_StateBit);
1373 drawTarget->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
1374 }
1375
1376 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1377 this->flushSurfaceWrites(surface);
1378 }
1379
1380 return true;
1381}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001382
reed@google.com7111d462014-03-25 16:20:24 +00001383// toggles between RGBA and BGRA
1384static SkColorType toggle_colortype32(SkColorType ct) {
1385 if (kRGBA_8888_SkColorType == ct) {
1386 return kBGRA_8888_SkColorType;
1387 } else {
1388 SkASSERT(kBGRA_8888_SkColorType == ct);
1389 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001390 }
1391}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001392
bsalomon@google.com0342a852012-08-20 19:22:38 +00001393bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1394 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001395 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001396 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001397 ASSERT_OWNED_RESOURCE(target);
1398
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001399 if (NULL == target) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001400 target = fRenderTarget.get();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001401 if (NULL == target) {
1402 return false;
1403 }
1404 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001405
bsalomonafbf2d62014-09-30 12:18:44 -07001406 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001407 this->flush();
1408 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001409
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001410 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001411
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001412 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1413 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1414 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001415 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001416 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001417 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1418 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1419 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1420 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1421 // dstConfig.
1422 GrPixelConfig readConfig = dstConfig;
1423 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001424 if (GrPixelConfigSwapRAndB(dstConfig) ==
1425 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001426 readConfig = GrPixelConfigSwapRAndB(readConfig);
1427 swapRAndB = true;
1428 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001429
bsalomon@google.com0342a852012-08-20 19:22:38 +00001430 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431
bsalomon@google.com9c680582013-02-06 18:17:50 +00001432 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001433 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001434 return false;
1435 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001436
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001437 // If the src is a texture and we would have to do conversions after read pixels, we instead
1438 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1439 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1440 // on the read back pixels.
1441 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001442 GrAutoScratchTexture ast;
bsalomon49f085d2014-09-05 13:34:00 -07001443 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001444 // Make the scratch a render so we can read its pixels.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001445 GrTextureDesc desc;
1446 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1447 desc.fWidth = width;
1448 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001449 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001450 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001451
bsalomon@google.com9c680582013-02-06 18:17:50 +00001452 // 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 +00001453 // match the passed rect. However, if we see many different size rectangles we will trash
1454 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1455 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001456 ScratchTexMatch match = kApprox_ScratchTexMatch;
1457 if (0 == left &&
1458 0 == top &&
1459 target->width() == width &&
1460 target->height() == height &&
1461 fGpu->fullReadPixelsIsFasterThanPartial()) {
1462 match = kExact_ScratchTexMatch;
1463 }
1464 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001465 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001466 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001467 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001468 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001469 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001470 textureMatrix.postIDiv(src->width(), src->height());
1471
joshualittb0a8a372014-09-23 09:50:21 -07001472 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001473 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001474 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1475 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001476 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001477 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001478 }
1479 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1480 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001481 if (fp || flipY || swapRAndB) {
1482 if (!fp) {
1483 fp.reset(GrConfigConversionEffect::Create(
1484 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1485 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001486 }
1487 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001488
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001489 // We protect the existing geometry here since it may not be
1490 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1491 // can be invoked in this method
1492 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001493 GrDrawState* drawState = fGpu->drawState();
joshualittb0a8a372014-09-23 09:50:21 -07001494 SkASSERT(fp);
1495 drawState->addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001496
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001497 drawState->setRenderTarget(texture->asRenderTarget());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001498 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon01c8da12014-08-04 09:21:30 -07001499 fGpu->drawSimpleRect(rect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001500 // we want to read back from the scratch's origin
1501 left = 0;
1502 top = 0;
1503 target = texture->asRenderTarget();
1504 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001505 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001506 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001507 if (!fGpu->readPixels(target,
1508 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001509 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001510 return false;
1511 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001512 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001513 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001514 SkDstPixelInfo dstPI;
1515 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1516 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001517 }
reed@google.com7111d462014-03-25 16:20:24 +00001518 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1519 dstPI.fPixels = buffer;
1520 dstPI.fRowBytes = rowBytes;
1521
1522 SkSrcPixelInfo srcPI;
1523 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1524 srcPI.fAlphaType = kPremul_SkAlphaType;
1525 srcPI.fPixels = buffer;
1526 srcPI.fRowBytes = rowBytes;
1527
1528 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001529 }
1530 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001531}
1532
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001533void GrContext::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001534 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001535 ASSERT_OWNED_RESOURCE(target);
1536 // In the future we may track whether there are any pending draws to this
1537 // target. We don't today so we always perform a flush. We don't promise
1538 // this to our clients, though.
1539 this->flush();
bsalomon49f085d2014-09-05 13:34:00 -07001540 if (fGpu) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001541 fGpu->resolveRenderTarget(target);
1542 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001543}
1544
bsalomon41ebbdd2014-08-04 08:31:39 -07001545void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1546 SkASSERT(renderTarget);
1547 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001548 AutoRestoreEffects are;
1549 AutoCheckFlush acf(this);
bsalomon41ebbdd2014-08-04 08:31:39 -07001550 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
1551 if (NULL == target) {
1552 return;
1553 }
1554 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001555}
1556
bsalomonf80bfed2014-10-07 05:56:02 -07001557void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1558 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001559 if (NULL == src || NULL == dst) {
1560 return;
1561 }
bsalomone3d4bf22014-09-23 09:15:03 -07001562 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001563 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001564
bsalomonf80bfed2014-10-07 05:56:02 -07001565 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1566 // here.
junov96c118e2014-09-26 13:09:47 -07001567
1568 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
1569 if (NULL == target) {
1570 return;
1571 }
junov96c118e2014-09-26 13:09:47 -07001572 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001573
1574 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1575 this->flush();
1576 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001577}
1578
bsalomonf80bfed2014-10-07 05:56:02 -07001579void GrContext::flushSurfaceWrites(GrSurface* surface) {
1580 if (surface->surfacePriv().hasPendingWrite()) {
1581 this->flush();
1582 }
1583}
1584
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585////////////////////////////////////////////////////////////////////////////////
1586
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001587GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
1588 BufferedDraw buffered,
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001589 AutoRestoreEffects* are,
1590 AutoCheckFlush* acf) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001591 // All users of this draw state should be freeing up all effects when they're done.
1592 // Otherwise effects that own resources may keep those resources alive indefinitely.
joshualittbd769d02014-09-04 08:56:46 -07001593 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() &&
1594 !fDrawState->hasGeometryProcessor());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001595
bsalomon41ebbdd2014-08-04 08:31:39 -07001596 if (NULL == fGpu) {
1597 return NULL;
1598 }
1599
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001600 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +00001601 fDrawBuffer->flush();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001602 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001603 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001604 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon49f085d2014-09-05 13:34:00 -07001605 if (paint) {
1606 SkASSERT(are);
1607 SkASSERT(acf);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001608 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001609 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001610#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1611 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001612 !fDrawState->couldApplyCoverage(fGpu->caps())) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001613 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1614 }
1615#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001616 // Clear any vertex attributes configured for the previous use of the
1617 // GrDrawState which can effect which blend optimizations are in effect.
1618 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001619 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001620 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001621 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001622 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001623 GrDrawTarget* target;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001624 if (kYes_BufferedDraw == buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001625 fLastDrawWasBuffered = kYes_BufferedDraw;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001626 target = fDrawBuffer;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001627 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001628 SkASSERT(kNo_BufferedDraw == buffered);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001629 fLastDrawWasBuffered = kNo_BufferedDraw;
1630 target = fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001631 }
bsalomon49f085d2014-09-05 13:34:00 -07001632 fDrawState->setState(GrDrawState::kClip_StateBit, fClip &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001633 !fClip->fClipStack->isWideOpen());
1634 target->setClip(fClip);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001635 SkASSERT(fDrawState == target->drawState());
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001636 return target;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001637}
1638
robertphillips@google.com72176b22012-05-23 13:19:12 +00001639/*
1640 * This method finds a path renderer that can draw the specified path on
1641 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001642 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001643 * can be individually allowed/disallowed via the "allowSW" boolean.
1644 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001645GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001646 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001647 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001648 bool allowSW,
1649 GrPathRendererChain::DrawType drawType,
1650 GrPathRendererChain::StencilSupport* stencilSupport) {
1651
bsalomon@google.com30085192011-08-19 15:42:31 +00001652 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001653 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001654 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001655
sugoi@google.com12b4e272012-12-06 20:13:11 +00001656 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1657 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001658 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001659 drawType,
1660 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001661
1662 if (NULL == pr && allowSW) {
1663 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001664 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001665 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001666 pr = fSoftwarePathRenderer;
1667 }
1668
1669 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001670}
1671
bsalomon@google.com27847de2011-02-22 20:59:41 +00001672////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001673bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1674 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001675}
1676
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001677int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1678 SkScalar dpi) const {
1679 if (!this->isConfigRenderable(config, true)) {
1680 return 0;
1681 }
1682 int chosenSampleCount = 0;
1683 if (fGpu->caps()->pathRenderingSupport()) {
1684 if (dpi >= 250.0f) {
1685 chosenSampleCount = 4;
1686 } else {
1687 chosenSampleCount = 16;
1688 }
1689 }
1690 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1691 chosenSampleCount : 0;
1692}
1693
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001694void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001695 SkASSERT(NULL == fDrawBuffer);
1696 SkASSERT(NULL == fDrawBufferVBAllocPool);
1697 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001698
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001699 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001700 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001701 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001702 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001703 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001704 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001705 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001706 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001708 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001709 fDrawBufferVBAllocPool,
1710 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001711
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001712 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001713}
1714
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001715GrDrawTarget* GrContext::getTextTarget() {
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001716 return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001717}
1718
1719const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1720 return fGpu->getQuadIndexBuffer();
1721}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001722
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001723namespace {
1724void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1725 GrConfigConversionEffect::PMConversion pmToUPM;
1726 GrConfigConversionEffect::PMConversion upmToPM;
1727 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1728 *pmToUPMValue = pmToUPM;
1729 *upmToPMValue = upmToPM;
1730}
1731}
1732
joshualittb0a8a372014-09-23 09:50:21 -07001733const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1734 bool swapRAndB,
1735 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001736 if (!fDidTestPMConversions) {
1737 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001738 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001739 }
1740 GrConfigConversionEffect::PMConversion pmToUPM =
1741 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1742 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001743 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001744 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001745 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001746 }
1747}
1748
joshualittb0a8a372014-09-23 09:50:21 -07001749const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1750 bool swapRAndB,
1751 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001752 if (!fDidTestPMConversions) {
1753 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001754 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001755 }
1756 GrConfigConversionEffect::PMConversion upmToPM =
1757 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1758 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001759 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001760 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001761 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001762 }
1763}
1764
bsalomon6d3fe022014-07-25 08:35:45 -07001765void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001766 fResourceCache->addResource(resourceKey, resource);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001767}
1768
bsalomon6d3fe022014-07-25 08:35:45 -07001769GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
1770 GrGpuResource* resource = fResourceCache->find(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001771 SkSafeRef(resource);
1772 return resource;
1773}
1774
egdanielbbcb38d2014-06-19 10:19:29 -07001775void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1776 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001777 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001778 fDrawBuffer->addGpuTraceMarker(marker);
1779 }
1780}
1781
1782void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1783 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001784 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001785 fDrawBuffer->removeGpuTraceMarker(marker);
1786 }
1787}
1788
bsalomon@google.comc4364992011-11-07 15:54:49 +00001789///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001790#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001791void GrContext::printCacheStats() const {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001792 fResourceCache->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001793}
1794#endif
robertphillips754f4e92014-09-18 13:52:08 -07001795
1796#if GR_GPU_STATS
1797const GrContext::GPUStats* GrContext::gpuStats() const {
1798 return fGpu->gpuStats();
1799}
1800#endif
1801