blob: 9d684aa1357a8ce86e227e8cee9262d69e8df321 [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.org47841822014-03-27 14:19:17 +000040#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000041#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000042#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000043#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000044#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000045
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000046#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000047 // change this to a 1 to see notifications when partial coverage fails
48 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
49#else
50 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
51#endif
52
robertphillips@google.com4e5559a2013-10-30 17:04:16 +000053static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT;
54static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055
bsalomon@google.com60361492012-03-15 17:47:06 +000056static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
58
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000059static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
60static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000062#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000063
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000064// Glorified typedef to avoid including GrDrawState.h in GrContext.h
65class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {};
66
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000067class GrContext::AutoCheckFlush {
68public:
bsalomon49f085d2014-09-05 13:34:00 -070069 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000070
71 ~AutoCheckFlush() {
72 if (fContext->fFlushToReduceCacheSize) {
73 fContext->flush();
74 }
75 }
76
77private:
78 GrContext* fContext;
79};
80
krajcevski9c6d4d72014-08-12 07:26:25 -070081GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
82 const Options* opts) {
83 GrContext* context;
84 if (NULL == opts) {
85 context = SkNEW_ARGS(GrContext, (Options()));
86 } else {
87 context = SkNEW_ARGS(GrContext, (*opts));
88 }
89
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000090 if (context->init(backend, backendContext)) {
91 return context;
92 } else {
93 context->unref();
94 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000095 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000096}
97
krajcevski9c6d4d72014-08-12 07:26:25 -070098GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000099 fDrawState = NULL;
100 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000101 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000102 fPathRendererChain = NULL;
103 fSoftwarePathRenderer = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000104 fResourceCache = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -0700105 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000106 fFontCache = NULL;
107 fDrawBuffer = NULL;
108 fDrawBufferVBAllocPool = NULL;
109 fDrawBufferIBAllocPool = NULL;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000110 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000111 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000112 fOvalRenderer = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000113 fViewMatrix.reset();
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000114 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000115}
116
117bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000118 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000119
120 fGpu = GrGpu::Create(backend, backendContext, this);
121 if (NULL == fGpu) {
122 return false;
123 }
124
125 fDrawState = SkNEW(GrDrawState);
126 fGpu->setDrawState(fDrawState);
127
bsalomonbcf0a522014-10-08 08:40:09 -0700128 fResourceCache = SkNEW_ARGS(GrResourceCache, (fGpu->caps(),
129 MAX_RESOURCE_CACHE_COUNT,
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000130 MAX_RESOURCE_CACHE_BYTES));
131 fResourceCache->setOverbudgetCallback(OverbudgetCB, this);
bsalomonc8dc1f72014-08-21 13:02:13 -0700132 fResourceCache2 = SkNEW(GrResourceCache2);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000133
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000134 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
135
robertphillips4ec84da2014-06-24 13:10:43 -0700136 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000137
joshualittb44293e2014-10-28 08:12:18 -0700138 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000139 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000140
141 fDidTestPMConversions = false;
142
143 this->setupDrawBuffer();
144
145 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000146}
147
bsalomon@google.com27847de2011-02-22 20:59:41 +0000148GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000149 if (NULL == fGpu) {
150 return;
151 }
152
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000153 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000154
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000155 for (int i = 0; i < fCleanUpData.count(); ++i) {
156 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
157 }
158
bsalomonc8dc1f72014-08-21 13:02:13 -0700159 delete fResourceCache2;
160 fResourceCache2 = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000161 delete fResourceCache;
162 fResourceCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000163 delete fFontCache;
164 delete fDrawBuffer;
165 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000166 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000167
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000168 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000169 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000170
bsalomon@google.com205d4602011-04-25 12:43:45 +0000171 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000172 SkSafeUnref(fPathRendererChain);
173 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000174 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000175}
176
bsalomon2354f842014-07-28 13:48:36 -0700177void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000178 // abandon first to so destructors
179 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700180 fResourceCache2->abandonAll();
181
robertphillipse3371302014-09-17 06:01:06 -0700182 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000183
bsalomon@google.com30085192011-08-19 15:42:31 +0000184 // a path renderer may be holding onto resources that
185 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000186 SkSafeSetNull(fPathRendererChain);
187 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000188
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000189 delete fDrawBuffer;
190 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000191
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000192 delete fDrawBufferVBAllocPool;
193 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000194
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000195 delete fDrawBufferIBAllocPool;
196 fDrawBufferIBAllocPool = NULL;
197
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000198 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000199 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000200
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000201 fResourceCache->purgeAllUnlocked();
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +0000202
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000203 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000204 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000205}
206
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000207void GrContext::resetContext(uint32_t state) {
208 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000209}
210
211void GrContext::freeGpuResources() {
212 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213
robertphillips@google.comff175842012-05-14 19:31:39 +0000214 fGpu->purgeResources();
bsalomon49f085d2014-09-05 13:34:00 -0700215 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700216 fDrawBuffer->purgeResources();
217 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000218
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000219 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000220 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000221
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000222 fResourceCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000223 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000224 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000225 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000226 SkSafeSetNull(fPathRendererChain);
227 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228}
229
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000230void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon49f085d2014-09-05 13:34:00 -0700231 if (resourceCount) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000232 *resourceCount = fResourceCache->getCachedResourceCount();
233 }
bsalomon49f085d2014-09-05 13:34:00 -0700234 if (resourceBytes) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000235 *resourceBytes = fResourceCache->getCachedResourceBytes();
236 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000237}
238
kkinnunenc6cb56f2014-06-24 00:12:27 -0700239GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
240 const SkDeviceProperties&
241 leakyProperties,
242 bool enableDistanceFieldFonts) {
jvanverth8c27a182014-10-14 08:45:50 -0700243 if (fGpu->caps()->pathRenderingSupport() && renderTarget->getStencilBuffer() &&
244 renderTarget->isMultisampled()) {
245 return GrStencilAndCoverTextContext::Create(this, leakyProperties);
246 }
247
248 return GrDistanceFieldTextContext::Create(this, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700249}
250
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000251////////////////////////////////////////////////////////////////////////////////
252
bsalomonf2703d82014-10-28 14:33:06 -0700253GrTexture* GrContext::findAndRefTexture(const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000254 const GrCacheID& cacheID,
255 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700256 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon6d3fe022014-07-25 08:35:45 -0700257 GrGpuResource* resource = fResourceCache->find(resourceKey);
bsalomon37dd3312014-11-03 08:47:23 -0800258 if (resource) {
259 resource->ref();
260 return static_cast<GrSurface*>(resource)->asTexture();
261 } else {
262 return NULL;
263 }
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000264}
265
bsalomonf2703d82014-10-28 14:33:06 -0700266bool GrContext::isTextureInCache(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000267 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000268 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700269 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000270 return fResourceCache->hasKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000271}
272
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000273void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000274 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000275
276 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
277 sb->height(),
278 sb->numSamples());
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000279 fResourceCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000280}
281
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000282GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000283 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000284 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
285 height,
286 sampleCnt);
bsalomon6d3fe022014-07-25 08:35:45 -0700287 GrGpuResource* resource = fResourceCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000288 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000289}
290
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000291static void stretch_image(void* dst,
292 int dstW,
293 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000294 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000295 int srcW,
296 int srcH,
297 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000298 SkFixed dx = (srcW << 16) / dstW;
299 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000300
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000301 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000302
robertphillips@google.com8b169312013-10-15 17:47:36 +0000303 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000304 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000305 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000306 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
307 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000308 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000309 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310 x += dx;
311 }
312 y += dy;
313 }
314}
315
robertphillips@google.com42903302013-04-20 12:26:07 +0000316namespace {
317
318// position + local coordinate
319extern const GrVertexAttrib gVertexAttribs[] = {
320 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000321 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000322};
323
324};
325
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000326// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000327// the current hardware. Resize the texture to be a POT
bsalomonf2703d82014-10-28 14:33:06 -0700328GrTexture* GrContext::createResizedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000329 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000330 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000331 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000332 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000333 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000334 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000335 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000336
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000337 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000338 return NULL;
339 }
340 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000341
bsalomonf2703d82014-10-28 14:33:06 -0700342 GrSurfaceDesc rtDesc = desc;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000343 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700344 kRenderTarget_GrSurfaceFlag |
345 kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000346 rtDesc.fWidth = GrNextPow2(desc.fWidth);
347 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000348
349 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
350
bsalomon49f085d2014-09-05 13:34:00 -0700351 if (texture) {
joshualitt5c55fef2014-10-31 14:04:35 -0700352 GrDrawTarget::AutoStateRestore asr(fDrawBuffer, GrDrawTarget::kReset_ASRInit);
353 GrDrawState* drawState = fDrawBuffer->drawState();
robertphillips@google.com3319f332012-08-13 18:00:36 +0000354 drawState->setRenderTarget(texture->asRenderTarget());
355
356 // if filtering is not desired then we want to ensure all
357 // texels in the resampled image are copies of texels from
358 // the original.
joshualitt5c55fef2014-10-31 14:04:35 -0700359 GrTextureParams params(SkShader::kClamp_TileMode,
360 filter ? GrTextureParams::kBilerp_FilterMode :
361 GrTextureParams::kNone_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700362 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000363
egdaniel7b3d5ee2014-08-28 05:41:14 -0700364 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs),
365 2 * sizeof(SkPoint));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000366
joshualitt5c55fef2014-10-31 14:04:35 -0700367 GrDrawTarget::AutoReleaseGeometry arg(fDrawBuffer, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000368
369 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000370 SkPoint* verts = (SkPoint*) arg.vertices();
371 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
372 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
joshualitt5c55fef2014-10-31 14:04:35 -0700373 fDrawBuffer->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000374 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000375 } else {
376 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000377 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 // not. Either implement filtered stretch blit on CPU or just create
379 // one when FBO case fails.
380
bsalomonf2703d82014-10-28 14:33:06 -0700381 rtDesc.fFlags = kNone_GrSurfaceFlags;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000382 // no longer need to clamp at min RT size.
383 rtDesc.fWidth = GrNextPow2(desc.fWidth);
384 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000385
386 // We shouldn't be resizing a compressed texture.
387 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
388
robertphillips@google.com8b169312013-10-15 17:47:36 +0000389 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700390 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000391 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
392 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000393
394 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
395
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000396 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700397 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000398 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000399
400 return texture;
401}
402
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000403GrTexture* GrContext::createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700404 const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000405 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000406 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000407 size_t rowBytes,
408 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700409 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000410
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000411 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700412 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700413 // We do not know how to resize compressed textures.
414 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
415
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000416 texture = this->createResizedTexture(desc, cacheID,
417 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700418 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700420 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000421 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000422
bsalomon49f085d2014-09-05 13:34:00 -0700423 if (texture) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000424 fResourceCache->addResource(resourceKey, texture);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000425
bsalomon49f085d2014-09-05 13:34:00 -0700426 if (cacheKey) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000427 *cacheKey = resourceKey;
428 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000429 }
430
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000431 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432}
433
bsalomonf2703d82014-10-28 14:33:06 -0700434GrTexture* GrContext::createNewScratchTexture(const GrSurfaceDesc& desc) {
bsalomonbcf0a522014-10-08 08:40:09 -0700435 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
436 if (!texture) {
437 return NULL;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000438 }
bsalomonbcf0a522014-10-08 08:40:09 -0700439 fResourceCache->addResource(texture->getScratchKey(), texture);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400440 return texture;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000441}
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000442
bsalomonf2703d82014-10-28 14:33:06 -0700443GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700444 bool calledDuringFlush) {
bsalomonbcf0a522014-10-08 08:40:09 -0700445 // kNoStencil has no meaning if kRT isn't set.
bsalomonf2703d82014-10-28 14:33:06 -0700446 SkASSERT((inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
447 !(inDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000448
bsalomonbcf0a522014-10-08 08:40:09 -0700449 // Make sure caller has checked for renderability if kRT is set.
bsalomonf2703d82014-10-28 14:33:06 -0700450 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
bsalomonbcf0a522014-10-08 08:40:09 -0700451 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000452
bsalomonf2703d82014-10-28 14:33:06 -0700453 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000454
bsalomonf2703d82014-10-28 14:33:06 -0700455 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
456 GrSurfaceFlags origFlags = desc->fFlags;
bsalomonbcf0a522014-10-08 08:40:09 -0700457 if (kApprox_ScratchTexMatch == match) {
458 // bin by pow2 with a reasonable min
459 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700460 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700461 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
462 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000463 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000464
bsalomonbcf0a522014-10-08 08:40:09 -0700465 do {
466 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
bsalomon000f8292014-10-15 19:04:14 -0700467 uint32_t scratchFlags = 0;
468 if (calledDuringFlush) {
469 scratchFlags = GrResourceCache2::kRequireNoPendingIO_ScratchFlag;
bsalomonf2703d82014-10-28 14:33:06 -0700470 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon000f8292014-10-15 19:04:14 -0700471 // If it is not a render target then it will most likely be populated by
472 // writePixels() which will trigger a flush if the texture has pending IO.
473 scratchFlags = GrResourceCache2::kPreferNoPendingIO_ScratchFlag;
474 }
475 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
bsalomonbcf0a522014-10-08 08:40:09 -0700476 if (resource) {
477 fResourceCache->makeResourceMRU(resource);
bsalomon37dd3312014-11-03 08:47:23 -0800478 return static_cast<GrSurface*>(resource)->asTexture();
bsalomonbcf0a522014-10-08 08:40:09 -0700479 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480
bsalomonbcf0a522014-10-08 08:40:09 -0700481 if (kExact_ScratchTexMatch == match) {
482 break;
483 }
484 // We had a cache miss and we are in approx mode, relax the fit of the flags.
485
486 // We no longer try to reuse textures that were previously used as render targets in
487 // situations where no RT is needed; doing otherwise can confuse the video driver and
488 // cause significant performance problems in some cases.
bsalomonf2703d82014-10-28 14:33:06 -0700489 if (desc->fFlags & kNoStencil_GrSurfaceFlag) {
490 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrSurfaceFlag;
bsalomonbcf0a522014-10-08 08:40:09 -0700491 } else {
492 break;
493 }
494
495 } while (true);
496
497 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000498 }
499
bsalomonbcf0a522014-10-08 08:40:09 -0700500 GrTexture* texture = this->createNewScratchTexture(*desc);
501 SkASSERT(NULL == texture ||
502 texture->getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
503 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400504}
505
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000506bool GrContext::OverbudgetCB(void* data) {
bsalomon49f085d2014-09-05 13:34:00 -0700507 SkASSERT(data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000508
509 GrContext* context = reinterpret_cast<GrContext*>(data);
510
511 // Flush the InOrderDrawBuffer to possibly free up some textures
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000512 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000513
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000514 return true;
515}
516
517
bsalomonf2703d82014-10-28 14:33:06 -0700518GrTexture* GrContext::createUncachedTexture(const GrSurfaceDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000519 void* srcData,
520 size_t rowBytes) {
bsalomonf2703d82014-10-28 14:33:06 -0700521 GrSurfaceDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000522 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000523}
524
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000525void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
526 fResourceCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000527}
528
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000529void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
530 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000531}
532
bsalomon@google.com91958362011-06-13 17:58:13 +0000533int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000534 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000535}
536
537int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000538 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539}
540
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000541int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000542 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000543}
544
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545///////////////////////////////////////////////////////////////////////////////
546
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000547GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
548 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000549}
550
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000551GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
552 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000553}
554
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000555///////////////////////////////////////////////////////////////////////////////
556
bsalomon@google.comb8670992012-07-25 21:27:09 +0000557bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000558 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000559 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000560 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561 return false;
562 }
563
tfarinaf9dae782014-06-06 06:35:28 -0700564 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565
566 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700567 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000568 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569 return false;
570 }
571 }
572 return true;
573}
574
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576////////////////////////////////////////////////////////////////////////////////
577
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000578void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000579 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000580 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700581 GrRenderTarget* renderTarget) {
582 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800583 SkASSERT(renderTarget);
584
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000585 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000586 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700587 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt5c55fef2014-10-31 14:04:35 -0700588 GrDrawTarget* target = this->prepareToDraw(NULL, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700589 if (NULL == target) {
590 return;
591 }
592 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593}
594
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000595void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000596 // set rect to be big enough to fill the space, but not super-huge, so we
597 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000598 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000599 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000600 SkIntToScalar(getRenderTarget()->width()),
601 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000602 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000603 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000604 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700605 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000606
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000607 // We attempt to map r by the inverse matrix and draw that. mapRect will
608 // map the four corners and bound them with a new rect. This will not
609 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000610 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000611 if (!fViewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700612 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000613 return;
614 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615 inverse.mapRect(&r);
616 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000617 if (!am.setIdentity(this, paint.writable())) {
tfarina38406c82014-10-31 07:11:12 -0700618 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000619 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000620 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000622 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000623 if (paint->isAntiAlias()) {
624 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000625 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000626 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627}
628
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000629#ifdef SK_DEVELOPER
630void GrContext::dumpFontCache() const {
631 fFontCache->dump();
632}
633#endif
634
bsalomon@google.com205d4602011-04-25 12:43:45 +0000635////////////////////////////////////////////////////////////////////////////////
636
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637/* create a triangle strip that strokes the specified triangle. There are 8
638 unique vertices, but we repreat the last 2 to close up. Alternatively we
639 could use an indices array, and then only send 8 verts, but not sure that
640 would be faster.
641 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000642static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000643 SkScalar width) {
644 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000645 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000646
647 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
648 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
649 verts[2].set(rect.fRight - rad, rect.fTop + rad);
650 verts[3].set(rect.fRight + rad, rect.fTop - rad);
651 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
652 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
653 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
654 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
655 verts[8] = verts[0];
656 verts[9] = verts[1];
657}
658
bsalomonc30aaa02014-08-13 07:15:29 -0700659static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700660 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
661 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700662}
663
bsalomon@google.com205d4602011-04-25 12:43:45 +0000664static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000665 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000666 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000667 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700668 SkRect* devBoundRect) {
669 if (!target->getDrawState().canTweakAlphaForCoverage() &&
670 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000671#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700672 //SkDebugf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000673#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700674 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000675 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 const GrDrawState& drawState = target->getDrawState();
677 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000678 return false;
679 }
680
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000681#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000682 if (strokeWidth >= 0) {
683#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000684 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000685 return false;
686 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000687
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000688#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000689 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000690 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000691 return false;
692 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000694#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000695
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000696 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700697 if (strokeWidth < 0) {
698 return !is_irect(*devBoundRect);
699 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000700
bsalomon9c0822a2014-08-11 11:07:48 -0700701 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702}
703
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000704static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
705 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
706 point.fY >= rect.fTop && point.fY <= rect.fBottom;
707}
708
bsalomon@google.com27847de2011-02-22 20:59:41 +0000709void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000710 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700711 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700712 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700713 SkPath path;
714 path.addRect(rect);
715 this->drawPath(paint, path, *strokeInfo);
716 return;
717 }
718
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000719 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000720 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -0700721 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700722 if (NULL == target) {
723 return;
724 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000725
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000726 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700727 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700728 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000729
730 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
731 // cases where the RT is fully inside a stroke.
732 if (width < 0) {
733 SkRect rtRect;
734 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
735 SkRect clipSpaceRTRect = rtRect;
736 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700737 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000738 checkClip = true;
739 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
740 SkIntToScalar(this->getClip()->fOrigin.fY));
741 }
742 // Does the clip contain the entire RT?
743 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
744 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700745 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000746 return;
747 }
748 // Does the rect bound the RT?
749 SkPoint srcSpaceRTQuad[4];
750 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
751 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
752 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
753 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
754 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
755 // Will it blend?
756 GrColor clearColor;
757 if (paint.isOpaqueAndConstantColor(&clearColor)) {
bsalomon89c62982014-11-03 12:08:42 -0800758 target->clear(NULL, clearColor, true, fRenderTarget);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000759 return;
760 }
761 }
762 }
763 }
764
765 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000766 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000767 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700768 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700769
770 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
771
bsalomon@google.com205d4602011-04-25 12:43:45 +0000772 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000773 GrDrawState::AutoViewMatrixRestore avmr;
774 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000775 return;
776 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000777 if (width >= 0) {
joshualittb44293e2014-10-28 08:12:18 -0700778 fAARectRenderer->strokeAARect(target, rect,
bsalomon01c8da12014-08-04 09:21:30 -0700779 matrix, devBoundRect,
bsalomon9c0822a2014-08-11 11:07:48 -0700780 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000781 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000782 // filled AA rect
joshualittb44293e2014-10-28 08:12:18 -0700783 fAARectRenderer->fillAARect(target,
bsalomon9c0822a2014-08-11 11:07:48 -0700784 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000785 }
786 return;
787 }
788
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789 if (width >= 0) {
790 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000791 // Hairline could be done by just adding closing vertex to
792 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000793
bsalomon@google.com27847de2011-02-22 20:59:41 +0000794 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000795 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000796 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000797
798 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700799 SkDebugf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 return;
801 }
802
803 GrPrimitiveType primType;
804 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000805 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806
807 if (width > 0) {
808 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000809 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000810 setStrokeRectStrip(vertex, rect, width);
811 } else {
812 // hairline
813 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000814 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000815 vertex[0].set(rect.fLeft, rect.fTop);
816 vertex[1].set(rect.fRight, rect.fTop);
817 vertex[2].set(rect.fRight, rect.fBottom);
818 vertex[3].set(rect.fLeft, rect.fBottom);
819 vertex[4].set(rect.fLeft, rect.fTop);
820 }
821
bsalomon@google.com27847de2011-02-22 20:59:41 +0000822 target->drawNonIndexed(primType, 0, vertCount);
823 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000824 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700825 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000826 }
827}
828
829void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000830 const SkRect& dstRect,
831 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000832 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000833 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000834 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -0700835 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700836 if (NULL == target) {
837 return;
838 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000839
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000840 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
841
bsalomon01c8da12014-08-04 09:21:30 -0700842 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000843}
844
robertphillips@google.com42903302013-04-20 12:26:07 +0000845namespace {
846
847extern const GrVertexAttrib gPosUVColorAttribs[] = {
848 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000849 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding },
850 {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000851};
852
egdaniel7b3d5ee2014-08-28 05:41:14 -0700853static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint);
854static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
855
robertphillips@google.com42903302013-04-20 12:26:07 +0000856extern const GrVertexAttrib gPosColorAttribs[] = {
857 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000858 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000859};
860
egdaniel7b3d5ee2014-08-28 05:41:14 -0700861static const size_t kPosAttribsSize = sizeof(SkPoint);
862static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor);
863
robertphillips@google.com42903302013-04-20 12:26:07 +0000864static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000865 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000866 const GrColor* colors,
867 int* colorOffset,
868 int* texOffset) {
869 *texOffset = -1;
870 *colorOffset = -1;
871
bsalomon49f085d2014-09-05 13:34:00 -0700872 if (texCoords && colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000873 *texOffset = sizeof(SkPoint);
874 *colorOffset = 2*sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700875 drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700876 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000877 *texOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700878 drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700879 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000880 *colorOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700881 drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000882 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700883 drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000884 }
885}
886
887};
888
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889void GrContext::drawVertices(const GrPaint& paint,
890 GrPrimitiveType primitiveType,
891 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000892 const SkPoint positions[],
893 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894 const GrColor colors[],
895 const uint16_t indices[],
896 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000897 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000898 AutoCheckFlush acf(this);
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000899 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
900
joshualitt5c55fef2014-10-31 14:04:35 -0700901 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700902 if (NULL == target) {
903 return;
904 }
egdaniele61c4112014-06-12 10:24:21 -0700905 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000907 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
908
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000909 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +0000910 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000911
egdaniel7b3d5ee2014-08-28 05:41:14 -0700912 size_t VertexStride = drawState->getVertexStride();
913 if (sizeof(SkPoint) != VertexStride) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000914 if (!geo.set(target, vertexCount, 0)) {
tfarina38406c82014-10-31 07:11:12 -0700915 SkDebugf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916 return;
917 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918 void* curVertex = geo.vertices();
919
920 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000921 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000923 if (texOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000924 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000925 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000926 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
928 }
egdaniel7b3d5ee2014-08-28 05:41:14 -0700929 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930 }
931 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000932 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933 }
934
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000935 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000936 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000937
bsalomon49f085d2014-09-05 13:34:00 -0700938 if (indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000939 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000940 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000941 target->resetIndexSource();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000943 target->drawNonIndexed(primitiveType, 0, vertexCount);
944 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945}
946
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000947///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000948
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000949void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000950 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700951 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000952 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000953 return;
954 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000955
egdanield58a0ba2014-06-11 10:30:05 -0700956 if (strokeInfo.isDashed()) {
957 SkPath path;
958 path.addRRect(rrect);
959 this->drawPath(paint, path, strokeInfo);
960 return;
961 }
962
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000963 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000964 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -0700965 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700966 if (NULL == target) {
967 return;
968 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000969
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000970 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
971
egdanield58a0ba2014-06-11 10:30:05 -0700972 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
973
974 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000975 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000976 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -0700977 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000978 }
979}
980
981///////////////////////////////////////////////////////////////////////////////
982
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000983void GrContext::drawDRRect(const GrPaint& paint,
984 const SkRRect& outer,
985 const SkRRect& inner) {
986 if (outer.isEmpty()) {
987 return;
988 }
989
990 AutoRestoreEffects are;
991 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -0700992 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000993
994 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
995
996 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
997 SkPath path;
998 path.addRRect(inner);
999 path.addRRect(outer);
1000 path.setFillType(SkPath::kEvenOdd_FillType);
1001
egdanield58a0ba2014-06-11 10:30:05 -07001002 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001003 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
1004 }
1005}
1006
1007///////////////////////////////////////////////////////////////////////////////
1008
bsalomon@google.com93c96602012-04-27 13:05:21 +00001009void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001010 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001011 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001012 if (oval.isEmpty()) {
1013 return;
1014 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001015
egdanield58a0ba2014-06-11 10:30:05 -07001016 if (strokeInfo.isDashed()) {
1017 SkPath path;
1018 path.addOval(oval);
1019 this->drawPath(paint, path, strokeInfo);
1020 return;
1021 }
1022
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001023 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001024 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -07001025 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001026 if (NULL == target) {
1027 return;
1028 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001029
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001030 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1031
egdanield58a0ba2014-06-11 10:30:05 -07001032 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1033
1034
1035 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001036 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001037 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -07001038 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001039 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001040}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001041
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001042// Can 'path' be drawn as a pair of filled nested rectangles?
1043static bool is_nested_rects(GrDrawTarget* target,
1044 const SkPath& path,
1045 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001046 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001047 SkASSERT(stroke.isFillStyle());
1048
1049 if (path.isInverseFillType()) {
1050 return false;
1051 }
1052
1053 const GrDrawState& drawState = target->getDrawState();
1054
1055 // TODO: this restriction could be lifted if we were willing to apply
1056 // the matrix to all the points individually rather than just to the rect
1057 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1058 return false;
1059 }
1060
bsalomon9c0822a2014-08-11 11:07:48 -07001061 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1062 target->shouldDisableCoverageAAForBlend()) {
1063 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001064 }
1065
1066 SkPath::Direction dirs[2];
1067 if (!path.isNestedRects(rects, dirs)) {
1068 return false;
1069 }
1070
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001071 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001072 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001073 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001074 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001075
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001076 // Right now, nested rects where the margin is not the same width
1077 // all around do not render correctly
1078 const SkScalar* outer = rects[0].asScalars();
1079 const SkScalar* inner = rects[1].asScalars();
1080
robertphillips183e9852014-10-21 11:25:37 -07001081 bool allEq = true;
1082
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001083 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001084 bool allGoE1 = margin >= SK_Scalar1;
1085
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001086 for (int i = 1; i < 4; ++i) {
1087 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001088 if (temp < SK_Scalar1) {
1089 allGoE1 = false;
1090 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001091 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001092 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001093 }
1094 }
1095
robertphillips183e9852014-10-21 11:25:37 -07001096 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001097}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001098
egdanield58a0ba2014-06-11 10:30:05 -07001099void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001100
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001101 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001102 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001103 this->drawPaint(paint);
1104 }
1105 return;
1106 }
1107
egdanield58a0ba2014-06-11 10:30:05 -07001108 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001109 SkPoint pts[2];
1110 if (path.isLine(pts)) {
1111 AutoRestoreEffects are;
1112 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -07001113 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001114 if (NULL == target) {
1115 return;
1116 }
egdaniele61c4112014-06-12 10:24:21 -07001117 GrDrawState* drawState = target->drawState();
1118
1119 SkMatrix origViewMatrix = drawState->getViewMatrix();
1120 GrDrawState::AutoViewMatrixRestore avmr;
1121 if (avmr.setIdentity(target->drawState())) {
1122 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1123 origViewMatrix)) {
1124 return;
1125 }
1126 }
1127 }
1128
1129 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001130 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1131 SkTLazy<SkPath> effectPath;
1132 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1133 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1134 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1135 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1136 return;
1137 }
1138
1139 this->drawPath(paint, path, newStrokeInfo);
1140 return;
1141 }
1142
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001143 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001144 // Scratch textures can be recycled after they are returned to the texture
1145 // cache. This presents a potential hazard for buffered drawing. However,
1146 // the writePixels that uploads to the scratch will perform a flush so we're
1147 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001148 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001149 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -07001150 GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001151 if (NULL == target) {
1152 return;
1153 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001154 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001155
egdaniel93a37bc2014-07-21 13:47:57 -07001156 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001157
egdanield58a0ba2014-06-11 10:30:05 -07001158 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1159
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001160 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1161
egdanield58a0ba2014-06-11 10:30:05 -07001162 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001163 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001164 SkRect rects[2];
1165
bsalomon9c0822a2014-08-11 11:07:48 -07001166 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001167 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001168 GrDrawState::AutoViewMatrixRestore avmr;
1169 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001170 return;
1171 }
1172
joshualittb44293e2014-10-28 08:12:18 -07001173 fAARectRenderer->fillAANestedRects(target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001174 return;
1175 }
1176 }
1177
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001178 SkRect ovalRect;
1179 bool isOval = path.isOval(&ovalRect);
1180
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001181 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001182 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1183 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001184 }
1185}
1186
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001187void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001188 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001189 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001190
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001191 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1192
1193
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001194 // An Assumption here is that path renderer would use some form of tweaking
1195 // the src color (either the input alpha or in the frag shader) to implement
1196 // aa. If we have some future driver-mojo path AA that can do the right
1197 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001198 bool useCoverageAA = useAA &&
1199 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1200 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001201
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001202
1203 GrPathRendererChain::DrawType type =
1204 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001205 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001206
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001207 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001208 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001209 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001210
1211 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001212 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001213
robertphillips@google.come79f3202014-02-11 16:30:21 +00001214 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001215 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001216 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001217 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1218 pathPtr = tmpPath.get();
1219 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001220 if (pathPtr->isEmpty()) {
1221 return;
1222 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001223 }
1224 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001225
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001226 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001227 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001228 }
1229
robertphillips@google.come79f3202014-02-11 16:30:21 +00001230 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001231#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001232 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001233#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001234 return;
1235 }
1236
robertphillips@google.come79f3202014-02-11 16:30:21 +00001237 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001238}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001239
bsalomon@google.com27847de2011-02-22 20:59:41 +00001240////////////////////////////////////////////////////////////////////////////////
1241
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001242void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001243 if (NULL == fDrawBuffer) {
1244 return;
1245 }
1246
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001247 if (kDiscard_FlushBit & flagsBitfield) {
1248 fDrawBuffer->reset();
1249 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001250 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001251 }
bsalomonbcf0a522014-10-08 08:40:09 -07001252 fResourceCache->purgeAsNeeded();
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001253 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001254}
1255
bsalomon81beccc2014-10-13 12:32:55 -07001256bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1257 const void* inPixels, size_t outRowBytes, void* outPixels) {
1258 SkSrcPixelInfo srcPI;
1259 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001260 return false;
1261 }
bsalomon81beccc2014-10-13 12:32:55 -07001262 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1263 srcPI.fPixels = inPixels;
1264 srcPI.fRowBytes = inRowBytes;
1265
1266 SkDstPixelInfo dstPI;
1267 dstPI.fColorType = srcPI.fColorType;
1268 dstPI.fAlphaType = kPremul_SkAlphaType;
1269 dstPI.fPixels = outPixels;
1270 dstPI.fRowBytes = outRowBytes;
1271
1272 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001273}
1274
bsalomon81beccc2014-10-13 12:32:55 -07001275bool GrContext::writeSurfacePixels(GrSurface* surface,
1276 int left, int top, int width, int height,
1277 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1278 uint32_t pixelOpsFlags) {
1279
1280 {
1281 GrTexture* texture = NULL;
1282 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1283 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1284
1285 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1286 surface->surfacePriv().hasPendingIO()) {
1287 this->flush();
1288 }
1289 return fGpu->writeTexturePixels(texture, left, top, width, height,
1290 srcConfig, buffer, rowBytes);
1291 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1292 // upload is already flushed.
1293 }
1294 }
1295
1296 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1297 GrRenderTarget* renderTarget = surface->asRenderTarget();
1298 if (NULL == renderTarget) {
1299 return false;
1300 }
1301
1302 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1303 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1304 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1305 // when drawing the scratch to the dst using a conversion effect.
1306 bool swapRAndB = false;
1307 GrPixelConfig writeConfig = srcConfig;
1308 if (GrPixelConfigSwapRAndB(srcConfig) ==
1309 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1310 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1311 swapRAndB = true;
1312 }
1313
bsalomonf2703d82014-10-28 14:33:06 -07001314 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001315 desc.fWidth = width;
1316 desc.fHeight = height;
1317 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001318 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1319 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001320 return false;
1321 }
1322
1323 SkAutoTUnref<const GrFragmentProcessor> fp;
1324 SkMatrix textureMatrix;
1325 textureMatrix.setIDiv(texture->width(), texture->height());
1326
1327 // allocate a tmp buffer and sw convert the pixels to premul
1328 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1329
1330 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1331 if (!GrPixelConfigIs8888(srcConfig)) {
1332 return false;
1333 }
1334 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1335 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1336 if (NULL == fp) {
1337 size_t tmpRowBytes = 4 * width;
1338 tmpPixels.reset(width * height);
1339 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1340 tmpPixels.get())) {
1341 return false;
1342 }
1343 rowBytes = tmpRowBytes;
1344 buffer = tmpPixels.get();
1345 }
1346 }
1347 if (NULL == fp) {
1348 fp.reset(GrConfigConversionEffect::Create(texture,
1349 swapRAndB,
1350 GrConfigConversionEffect::kNone_PMConversion,
1351 textureMatrix));
1352 }
1353
1354 // Even if the client told us not to flush, we still flush here. The client may have known that
1355 // writes to the original surface caused no data hazards, but they can't know that the scratch
1356 // we just got is safe.
1357 if (texture->surfacePriv().hasPendingIO()) {
1358 this->flush();
1359 }
1360 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1361 writeConfig, buffer, rowBytes)) {
1362 return false;
1363 }
1364
1365 SkMatrix matrix;
1366 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1367
1368 // This function can be called in the midst of drawing another object (e.g., when uploading a
1369 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1370 // drawing a rect to the render target.
1371 // The bracket ensures we pop the stack if we wind up flushing below.
1372 {
joshualitt5c55fef2014-10-31 14:04:35 -07001373 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL, NULL);
bsalomon81beccc2014-10-13 12:32:55 -07001374 GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit,
1375 &matrix);
1376 GrDrawState* drawState = drawTarget->drawState();
1377 drawState->addColorProcessor(fp);
1378 drawState->setRenderTarget(renderTarget);
1379 drawState->disableState(GrDrawState::kClip_StateBit);
1380 drawTarget->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
1381 }
1382
1383 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1384 this->flushSurfaceWrites(surface);
1385 }
1386
1387 return true;
1388}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001389
reed@google.com7111d462014-03-25 16:20:24 +00001390// toggles between RGBA and BGRA
1391static SkColorType toggle_colortype32(SkColorType ct) {
1392 if (kRGBA_8888_SkColorType == ct) {
1393 return kBGRA_8888_SkColorType;
1394 } else {
1395 SkASSERT(kBGRA_8888_SkColorType == ct);
1396 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001397 }
1398}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001399
bsalomon@google.com0342a852012-08-20 19:22:38 +00001400bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1401 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001402 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001403 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001404 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001405 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001406
bsalomonafbf2d62014-09-30 12:18:44 -07001407 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001408 this->flush();
1409 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001410
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001411 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001412
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001413 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1414 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1415 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001416 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001417 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001418 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1419 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1420 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1421 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1422 // dstConfig.
1423 GrPixelConfig readConfig = dstConfig;
1424 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001425 if (GrPixelConfigSwapRAndB(dstConfig) ==
1426 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001427 readConfig = GrPixelConfigSwapRAndB(readConfig);
1428 swapRAndB = true;
1429 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001430
bsalomon@google.com0342a852012-08-20 19:22:38 +00001431 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001432
bsalomon@google.com9c680582013-02-06 18:17:50 +00001433 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001434 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001435 return false;
1436 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001437
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001438 // If the src is a texture and we would have to do conversions after read pixels, we instead
1439 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1440 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1441 // on the read back pixels.
1442 GrTexture* src = target->asTexture();
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.
bsalomonf2703d82014-10-28 14:33:06 -07001445 GrSurfaceDesc desc;
1446 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001447 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 }
bsalomone3059732014-10-14 11:47:22 -07001464 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, match));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001465 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001466 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001467 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001468 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001469 textureMatrix.postIDiv(src->width(), src->height());
1470
joshualittb0a8a372014-09-23 09:50:21 -07001471 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001472 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001473 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1474 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001475 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001476 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001477 }
1478 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1479 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001480 if (fp || flipY || swapRAndB) {
1481 if (!fp) {
1482 fp.reset(GrConfigConversionEffect::Create(
1483 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1484 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001485 }
1486 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001487
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001488 // We protect the existing geometry here since it may not be
1489 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1490 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001491 {
1492 GrDrawTarget::AutoGeometryAndStatePush agasp(fDrawBuffer,
1493 GrDrawTarget::kReset_ASRInit);
1494 GrDrawState* drawState = fDrawBuffer->drawState();
1495 SkASSERT(fp);
1496 drawState->addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001497
joshualitt5c55fef2014-10-31 14:04:35 -07001498 drawState->setRenderTarget(texture->asRenderTarget());
1499 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
1500 fDrawBuffer->drawSimpleRect(rect);
1501 // we want to read back from the scratch's origin
1502 left = 0;
1503 top = 0;
1504 target = texture->asRenderTarget();
1505 }
1506 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001507 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001508 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001509 }
joshualitt5c55fef2014-10-31 14:04:35 -07001510
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001511 if (!fGpu->readPixels(target,
1512 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001513 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514 return false;
1515 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001516 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001517 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001518 SkDstPixelInfo dstPI;
1519 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1520 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001521 }
reed@google.com7111d462014-03-25 16:20:24 +00001522 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1523 dstPI.fPixels = buffer;
1524 dstPI.fRowBytes = rowBytes;
1525
1526 SkSrcPixelInfo srcPI;
1527 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1528 srcPI.fAlphaType = kPremul_SkAlphaType;
1529 srcPI.fPixels = buffer;
1530 srcPI.fRowBytes = rowBytes;
1531
1532 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001533 }
1534 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001535}
1536
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001537void GrContext::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001538 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001539 ASSERT_OWNED_RESOURCE(target);
bsalomon0537f292014-11-03 13:32:00 -08001540 this->flush();
bsalomon49f085d2014-09-05 13:34:00 -07001541 if (fGpu) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001542 fGpu->resolveRenderTarget(target);
1543 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001544}
1545
bsalomon41ebbdd2014-08-04 08:31:39 -07001546void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1547 SkASSERT(renderTarget);
1548 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001549 AutoRestoreEffects are;
1550 AutoCheckFlush acf(this);
joshualitt5c55fef2014-10-31 14:04:35 -07001551 GrDrawTarget* target = this->prepareToDraw(NULL, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001552 if (NULL == target) {
1553 return;
1554 }
1555 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001556}
1557
bsalomonf80bfed2014-10-07 05:56:02 -07001558void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1559 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001560 if (NULL == src || NULL == dst) {
1561 return;
1562 }
bsalomone3d4bf22014-09-23 09:15:03 -07001563 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001564 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001565
bsalomonf80bfed2014-10-07 05:56:02 -07001566 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1567 // here.
junov96c118e2014-09-26 13:09:47 -07001568
joshualitt5c55fef2014-10-31 14:04:35 -07001569 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL);
junov96c118e2014-09-26 13:09:47 -07001570 if (NULL == target) {
1571 return;
1572 }
junov96c118e2014-09-26 13:09:47 -07001573 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001574
1575 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1576 this->flush();
1577 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001578}
1579
bsalomonf80bfed2014-10-07 05:56:02 -07001580void GrContext::flushSurfaceWrites(GrSurface* surface) {
1581 if (surface->surfacePriv().hasPendingWrite()) {
1582 this->flush();
1583 }
1584}
1585
bsalomon@google.com27847de2011-02-22 20:59:41 +00001586////////////////////////////////////////////////////////////////////////////////
1587
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001588GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
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
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001600 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon49f085d2014-09-05 13:34:00 -07001601 if (paint) {
1602 SkASSERT(are);
1603 SkASSERT(acf);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001604 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001605 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001606#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1607 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001608 !fDrawState->couldApplyCoverage(fGpu->caps())) {
tfarina38406c82014-10-31 07:11:12 -07001609 SkDebugf("Partial pixel coverage will be incorrectly blended.\n");
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001610 }
1611#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001612 // Clear any vertex attributes configured for the previous use of the
1613 // GrDrawState which can effect which blend optimizations are in effect.
1614 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001615 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001616 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001617 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001618 }
bsalomon49f085d2014-09-05 13:34:00 -07001619 fDrawState->setState(GrDrawState::kClip_StateBit, fClip &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001620 !fClip->fClipStack->isWideOpen());
joshualitt5c55fef2014-10-31 14:04:35 -07001621 fDrawBuffer->setClip(fClip);
1622 SkASSERT(fDrawState == fDrawBuffer->drawState());
1623 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624}
1625
robertphillips@google.com72176b22012-05-23 13:19:12 +00001626/*
1627 * This method finds a path renderer that can draw the specified path on
1628 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001629 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001630 * can be individually allowed/disallowed via the "allowSW" boolean.
1631 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001632GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001633 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001634 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001635 bool allowSW,
1636 GrPathRendererChain::DrawType drawType,
1637 GrPathRendererChain::StencilSupport* stencilSupport) {
1638
bsalomon@google.com30085192011-08-19 15:42:31 +00001639 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001640 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001641 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001642
sugoi@google.com12b4e272012-12-06 20:13:11 +00001643 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1644 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001645 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001646 drawType,
1647 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001648
1649 if (NULL == pr && allowSW) {
1650 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001651 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001652 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001653 pr = fSoftwarePathRenderer;
1654 }
1655
1656 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001657}
1658
bsalomon@google.com27847de2011-02-22 20:59:41 +00001659////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001660bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1661 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001662}
1663
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001664int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1665 SkScalar dpi) const {
1666 if (!this->isConfigRenderable(config, true)) {
1667 return 0;
1668 }
1669 int chosenSampleCount = 0;
1670 if (fGpu->caps()->pathRenderingSupport()) {
1671 if (dpi >= 250.0f) {
1672 chosenSampleCount = 4;
1673 } else {
1674 chosenSampleCount = 16;
1675 }
1676 }
1677 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1678 chosenSampleCount : 0;
1679}
1680
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001681void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001682 SkASSERT(NULL == fDrawBuffer);
1683 SkASSERT(NULL == fDrawBufferVBAllocPool);
1684 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001685
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001686 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001687 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001688 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001689 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001690 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001691 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001692 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001693 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001694
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001695 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001696 fDrawBufferVBAllocPool,
1697 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001698
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001699 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001700}
1701
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001702GrDrawTarget* GrContext::getTextTarget() {
joshualitt5c55fef2014-10-31 14:04:35 -07001703 return this->prepareToDraw(NULL, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001704}
1705
1706const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1707 return fGpu->getQuadIndexBuffer();
1708}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001709
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001710namespace {
1711void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1712 GrConfigConversionEffect::PMConversion pmToUPM;
1713 GrConfigConversionEffect::PMConversion upmToPM;
1714 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1715 *pmToUPMValue = pmToUPM;
1716 *upmToPMValue = upmToPM;
1717}
1718}
1719
joshualittb0a8a372014-09-23 09:50:21 -07001720const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1721 bool swapRAndB,
1722 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001723 if (!fDidTestPMConversions) {
1724 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001725 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001726 }
1727 GrConfigConversionEffect::PMConversion pmToUPM =
1728 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1729 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001730 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001731 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001732 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001733 }
1734}
1735
joshualittb0a8a372014-09-23 09:50:21 -07001736const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1737 bool swapRAndB,
1738 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001739 if (!fDidTestPMConversions) {
1740 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001741 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001742 }
1743 GrConfigConversionEffect::PMConversion upmToPM =
1744 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1745 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001746 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001747 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001748 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001749 }
1750}
1751
bsalomon6d3fe022014-07-25 08:35:45 -07001752void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001753 fResourceCache->addResource(resourceKey, resource);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001754}
1755
bsalomon6d3fe022014-07-25 08:35:45 -07001756GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
1757 GrGpuResource* resource = fResourceCache->find(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001758 SkSafeRef(resource);
1759 return resource;
1760}
1761
egdanielbbcb38d2014-06-19 10:19:29 -07001762void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1763 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001764 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001765 fDrawBuffer->addGpuTraceMarker(marker);
1766 }
1767}
1768
1769void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1770 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001771 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001772 fDrawBuffer->removeGpuTraceMarker(marker);
1773 }
1774}
1775
bsalomon@google.comc4364992011-11-07 15:54:49 +00001776///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001777#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001778void GrContext::printCacheStats() const {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001779 fResourceCache->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001780}
1781#endif
robertphillips754f4e92014-09-18 13:52:08 -07001782
1783#if GR_GPU_STATS
1784const GrContext::GPUStats* GrContext::gpuStats() const {
1785 return fGpu->gpuStats();
1786}
1787#endif
1788