blob: 46a5576b113358492361240cf4737d38f9f6a699 [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000012#include "effects/GrConfigConversionEffect.h"
egdaniele61c4112014-06-12 10:24:21 -070013#include "effects/GrDashingEffect.h"
14#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000016#include "GrAARectRenderer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000017#include "GrBufferAllocPool.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000018#include "GrGpu.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070019#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000020#include "GrDrawTargetCaps.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000021#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000022#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000023#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000024#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000025#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000026#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000027#include "GrResourceCache.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070028#include "GrResourceCache2.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000029#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000030#include "GrStencilBuffer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070031#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070032#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070033#include "GrSurfacePriv.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000034#include "GrTextStrike.h"
bsalomonafbf2d62014-09-30 12:18:44 -070035#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070036#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000037#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070038#include "SkDashPathPriv.h"
reed@google.com7111d462014-03-25 16:20:24 +000039#include "SkGr.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000040#include "SkRTConf.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000041#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000042#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000043#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000044#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000045#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000046
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000047// It can be useful to set this to false to test whether a bug is caused by using the
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000048// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000049// debugging simpler.
bsalomon@google.come932c982013-09-10 19:47:01 +000050SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true,
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000051 "Defers rendering in GrContext via GrInOrderDrawBuffer.");
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000052
53#define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000055#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000056 // change this to a 1 to see notifications when partial coverage fails
57 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
58#else
59 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
60#endif
61
robertphillips@google.com4e5559a2013-10-30 17:04:16 +000062static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT;
63static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000064
bsalomon@google.com60361492012-03-15 17:47:06 +000065static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000066static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
67
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000068static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
69static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000070
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000071#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000072
bsalomonafbf2d62014-09-30 12:18:44 -070073GrTexture* GrAutoScratchTexture::detach() {
74 if (NULL == fTexture) {
75 return NULL;
76 }
77 GrTexture* texture = fTexture;
78 fTexture = NULL;
79
80 // This GrAutoScratchTexture has a ref from lockAndRefScratchTexture, which we give up now.
81 // The cache also has a ref which we are lending to the caller of detach(). When the caller
82 // lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is
83 // set and re-ref the texture, thereby restoring the cache's ref.
84 SkASSERT(!texture->unique());
85 texture->texturePriv().setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
86 texture->unref();
87 SkASSERT(texture->getCacheEntry());
88
89 return texture;
90}
91
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000092// Glorified typedef to avoid including GrDrawState.h in GrContext.h
93class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {};
94
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000095class GrContext::AutoCheckFlush {
96public:
bsalomon49f085d2014-09-05 13:34:00 -070097 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000098
99 ~AutoCheckFlush() {
100 if (fContext->fFlushToReduceCacheSize) {
101 fContext->flush();
102 }
103 }
104
105private:
106 GrContext* fContext;
107};
108
krajcevski9c6d4d72014-08-12 07:26:25 -0700109GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
110 const Options* opts) {
111 GrContext* context;
112 if (NULL == opts) {
113 context = SkNEW_ARGS(GrContext, (Options()));
114 } else {
115 context = SkNEW_ARGS(GrContext, (*opts));
116 }
117
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000118 if (context->init(backend, backendContext)) {
119 return context;
120 } else {
121 context->unref();
122 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000123 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000124}
125
krajcevski9c6d4d72014-08-12 07:26:25 -0700126GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000127 fDrawState = NULL;
128 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000129 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000130 fPathRendererChain = NULL;
131 fSoftwarePathRenderer = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000132 fResourceCache = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -0700133 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000134 fFontCache = NULL;
135 fDrawBuffer = NULL;
136 fDrawBufferVBAllocPool = NULL;
137 fDrawBufferIBAllocPool = NULL;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000138 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000139 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000140 fOvalRenderer = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000141 fViewMatrix.reset();
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000142 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000143}
144
145bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000146 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000147
148 fGpu = GrGpu::Create(backend, backendContext, this);
149 if (NULL == fGpu) {
150 return false;
151 }
152
153 fDrawState = SkNEW(GrDrawState);
154 fGpu->setDrawState(fDrawState);
155
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000156 fResourceCache = SkNEW_ARGS(GrResourceCache, (MAX_RESOURCE_CACHE_COUNT,
157 MAX_RESOURCE_CACHE_BYTES));
158 fResourceCache->setOverbudgetCallback(OverbudgetCB, this);
bsalomonc8dc1f72014-08-21 13:02:13 -0700159 fResourceCache2 = SkNEW(GrResourceCache2);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000160
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000161 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
162
robertphillips4ec84da2014-06-24 13:10:43 -0700163 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000164
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000165 fLastDrawWasBuffered = kNo_BufferedDraw;
166
167 fAARectRenderer = SkNEW(GrAARectRenderer);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000168 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000169
170 fDidTestPMConversions = false;
171
172 this->setupDrawBuffer();
173
174 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000175}
176
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000178 if (NULL == fGpu) {
179 return;
180 }
181
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000183
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000184 for (int i = 0; i < fCleanUpData.count(); ++i) {
185 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
186 }
187
bsalomonc8dc1f72014-08-21 13:02:13 -0700188 delete fResourceCache2;
189 fResourceCache2 = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000190 delete fResourceCache;
191 fResourceCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000192 delete fFontCache;
193 delete fDrawBuffer;
194 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000195 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000196
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000197 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000198 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000199
bsalomon@google.com205d4602011-04-25 12:43:45 +0000200 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000201 SkSafeUnref(fPathRendererChain);
202 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000203 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000204}
205
bsalomon2354f842014-07-28 13:48:36 -0700206void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000207 // abandon first to so destructors
208 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700209 fResourceCache2->abandonAll();
210
robertphillipse3371302014-09-17 06:01:06 -0700211 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000212
bsalomon@google.com30085192011-08-19 15:42:31 +0000213 // a path renderer may be holding onto resources that
214 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000215 SkSafeSetNull(fPathRendererChain);
216 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000217
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000218 delete fDrawBuffer;
219 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000220
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000221 delete fDrawBufferVBAllocPool;
222 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000223
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000224 delete fDrawBufferIBAllocPool;
225 fDrawBufferIBAllocPool = NULL;
226
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000227 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000228 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000229
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000230 fResourceCache->purgeAllUnlocked();
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +0000231
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000232 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000233 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000234}
235
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000236void GrContext::resetContext(uint32_t state) {
237 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000238}
239
240void GrContext::freeGpuResources() {
241 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000242
robertphillips@google.comff175842012-05-14 19:31:39 +0000243 fGpu->purgeResources();
bsalomon49f085d2014-09-05 13:34:00 -0700244 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700245 fDrawBuffer->purgeResources();
246 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000247
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000248 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000249 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000250
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000251 fResourceCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000252 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000253 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000254 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000255 SkSafeSetNull(fPathRendererChain);
256 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000257}
258
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000259void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon49f085d2014-09-05 13:34:00 -0700260 if (resourceCount) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000261 *resourceCount = fResourceCache->getCachedResourceCount();
262 }
bsalomon49f085d2014-09-05 13:34:00 -0700263 if (resourceBytes) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000264 *resourceBytes = fResourceCache->getCachedResourceBytes();
265 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000266}
267
kkinnunenc6cb56f2014-06-24 00:12:27 -0700268GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
269 const SkDeviceProperties&
270 leakyProperties,
271 bool enableDistanceFieldFonts) {
272 if (fGpu->caps()->pathRenderingSupport()) {
273 if (renderTarget->getStencilBuffer() && renderTarget->isMultisampled()) {
274 return SkNEW_ARGS(GrStencilAndCoverTextContext, (this, leakyProperties));
275 }
276 }
277 return SkNEW_ARGS(GrDistanceFieldTextContext, (this, leakyProperties,
278 enableDistanceFieldFonts));
279}
280
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000281////////////////////////////////////////////////////////////////////////////////
282
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000283GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
284 const GrCacheID& cacheID,
285 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700286 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon6d3fe022014-07-25 08:35:45 -0700287 GrGpuResource* resource = fResourceCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000288 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000289 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290}
291
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000292bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000293 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000294 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700295 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000296 return fResourceCache->hasKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000297}
298
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000299void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000300 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000301
302 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
303 sb->height(),
304 sb->numSamples());
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000305 fResourceCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000306}
307
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000308GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000309 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000310 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
311 height,
312 sampleCnt);
bsalomon6d3fe022014-07-25 08:35:45 -0700313 GrGpuResource* resource = fResourceCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000314 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000315}
316
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000317static void stretch_image(void* dst,
318 int dstW,
319 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000320 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000321 int srcW,
322 int srcH,
323 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000324 SkFixed dx = (srcW << 16) / dstW;
325 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000326
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000327 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000328
robertphillips@google.com8b169312013-10-15 17:47:36 +0000329 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000330 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000331 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000332 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
333 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000334 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000335 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000336 x += dx;
337 }
338 y += dy;
339 }
340}
341
robertphillips@google.com42903302013-04-20 12:26:07 +0000342namespace {
343
344// position + local coordinate
345extern const GrVertexAttrib gVertexAttribs[] = {
346 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000347 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000348};
349
350};
351
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000352// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000353// the current hardware. Resize the texture to be a POT
354GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000355 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000356 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000357 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000358 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000359 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000360 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000361 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000362
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000363 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000364 return NULL;
365 }
366 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000367
robertphillips@google.com3319f332012-08-13 18:00:36 +0000368 GrTextureDesc rtDesc = desc;
369 rtDesc.fFlags = rtDesc.fFlags |
370 kRenderTarget_GrTextureFlagBit |
371 kNoStencil_GrTextureFlagBit;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000372 rtDesc.fWidth = GrNextPow2(desc.fWidth);
373 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000374
375 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
376
bsalomon49f085d2014-09-05 13:34:00 -0700377 if (texture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
379 GrDrawState* drawState = fGpu->drawState();
380 drawState->setRenderTarget(texture->asRenderTarget());
381
382 // if filtering is not desired then we want to ensure all
383 // texels in the resampled image are copies of texels from
384 // the original.
humper@google.comb86add12013-07-25 18:49:07 +0000385 GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTextureParams::kBilerp_FilterMode :
386 GrTextureParams::kNone_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700387 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000388
egdaniel7b3d5ee2014-08-28 05:41:14 -0700389 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs),
390 2 * sizeof(SkPoint));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000391
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000392 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000393
394 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000395 SkPoint* verts = (SkPoint*) arg.vertices();
396 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
397 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000398 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000399 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000400 } else {
401 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000402 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000403 // not. Either implement filtered stretch blit on CPU or just create
404 // one when FBO case fails.
405
406 rtDesc.fFlags = kNone_GrTextureFlags;
407 // no longer need to clamp at min RT size.
408 rtDesc.fWidth = GrNextPow2(desc.fWidth);
409 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000410
411 // We shouldn't be resizing a compressed texture.
412 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
413
robertphillips@google.com8b169312013-10-15 17:47:36 +0000414 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700415 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000416 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
417 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000418
419 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
420
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000421 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700422 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000423 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000424
425 return texture;
426}
427
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000428GrTexture* GrContext::createTexture(const GrTextureParams* params,
429 const GrTextureDesc& desc,
430 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000431 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000432 size_t rowBytes,
433 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700434 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000435
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000436 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700437 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700438 // We do not know how to resize compressed textures.
439 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
440
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000441 texture = this->createResizedTexture(desc, cacheID,
442 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700443 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700445 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000447
bsalomon49f085d2014-09-05 13:34:00 -0700448 if (texture) {
robertphillips@google.com1622a6d2013-07-18 17:11:45 +0000449 // Adding a resource could put us overbudget. Try to free up the
450 // necessary space before adding it.
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000451 fResourceCache->purgeAsNeeded(1, texture->gpuMemorySize());
452 fResourceCache->addResource(resourceKey, texture);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000453
bsalomon49f085d2014-09-05 13:34:00 -0700454 if (cacheKey) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000455 *cacheKey = resourceKey;
456 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000457 }
458
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000459 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000460}
461
robertphillipsdbe60742014-09-30 06:54:17 -0700462static GrTexture* create_scratch_texture(GrGpu* gpu,
463 GrResourceCache* resourceCache,
464 const GrTextureDesc& desc) {
465 GrTexture* texture = gpu->createTexture(desc, NULL, 0);
466 if (texture) {
bsalomonafbf2d62014-09-30 12:18:44 -0700467 GrResourceKey key = GrTexturePriv::ComputeScratchKey(texture->desc());
robertphillipsdbe60742014-09-30 06:54:17 -0700468 // Adding a resource could put us overbudget. Try to free up the
469 // necessary space before adding it.
470 resourceCache->purgeAsNeeded(1, texture->gpuMemorySize());
471 // Make the resource exclusive so future 'find' calls don't return it
472 resourceCache->addResource(key, texture, GrResourceCache::kHide_OwnershipFlag);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000473 }
robertphillipsdbe60742014-09-30 06:54:17 -0700474 return texture;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000475}
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000476
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000477GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match) {
478
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000479 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000480 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit));
481
robertphillipsdbe60742014-09-30 06:54:17 -0700482 // Renderable A8 targets are not universally supported (e.g., not on ANGLE)
483 SkASSERT(this->isConfigRenderable(kAlpha_8_GrPixelConfig, inDesc.fSampleCnt > 0) ||
484 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
485 (inDesc.fConfig != kAlpha_8_GrPixelConfig));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000486
robertphillipsdbe60742014-09-30 06:54:17 -0700487 if (!fGpu->caps()->reuseScratchTextures() &&
488 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit)) {
489 // If we're never recycling this texture we can always make it the right size
490 return create_scratch_texture(fGpu, fResourceCache, inDesc);
491 }
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000492
robertphillipsdbe60742014-09-30 06:54:17 -0700493 GrTextureDesc desc = inDesc;
494
495 if (kApprox_ScratchTexMatch == match) {
496 // bin by pow2 with a reasonable min
497 static const int MIN_SIZE = 16;
498 desc.fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc.fWidth));
499 desc.fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc.fHeight));
500 }
501
502 GrGpuResource* resource = NULL;
503 int origWidth = desc.fWidth;
504 int origHeight = desc.fHeight;
505
506 do {
bsalomonafbf2d62014-09-30 12:18:44 -0700507 GrResourceKey key = GrTexturePriv::ComputeScratchKey(desc);
robertphillipsdbe60742014-09-30 06:54:17 -0700508 // Ensure we have exclusive access to the texture so future 'find' calls don't return it
509 resource = fResourceCache->find(key, GrResourceCache::kHide_OwnershipFlag);
510 if (resource) {
511 resource->ref();
512 break;
513 }
514 if (kExact_ScratchTexMatch == match) {
515 break;
516 }
517 // We had a cache miss and we are in approx mode, relax the fit of the flags.
518
519 // We no longer try to reuse textures that were previously used as render targets in
520 // situations where no RT is needed; doing otherwise can confuse the video driver and
521 // cause significant performance problems in some cases.
522 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
523 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
524 } else {
525 break;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000526 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000527
robertphillipsdbe60742014-09-30 06:54:17 -0700528 } while (true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000529
robertphillipsdbe60742014-09-30 06:54:17 -0700530 if (NULL == resource) {
531 desc.fFlags = inDesc.fFlags;
532 desc.fWidth = origWidth;
533 desc.fHeight = origHeight;
534 resource = create_scratch_texture(fGpu, fResourceCache, desc);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000535 }
536
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000537 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000538}
539
robertphillipsdbe60742014-09-30 06:54:17 -0700540void GrContext::addExistingTextureToCache(GrTexture* texture) {
541
542 if (NULL == texture) {
543 return;
544 }
545
546 // This texture should already have a cache entry since it was once
547 // attached
548 SkASSERT(texture->getCacheEntry());
549
550 // Conceptually, the cache entry is going to assume responsibility
551 // for the creation ref. Assert refcnt == 1.
552 // Except that this also gets called when the texture is prematurely
553 // abandoned. In that case the ref count may be > 1.
554 // SkASSERT(texture->unique());
555
556 if (fGpu->caps()->reuseScratchTextures() || texture->asRenderTarget()) {
557 // Since this texture came from an AutoScratchTexture it should
558 // still be in the exclusive pile. Recycle it.
559 fResourceCache->makeNonExclusive(texture->getCacheEntry());
560 this->purgeCache();
561 } else {
562 // When we aren't reusing textures we know this scratch texture
563 // will never be reused and would be just wasting time in the cache
564 fResourceCache->makeNonExclusive(texture->getCacheEntry());
565 fResourceCache->deleteResource(texture->getCacheEntry());
566 }
567}
568
569void GrContext::unlockScratchTexture(GrTexture* texture) {
570 if (texture->wasDestroyed()) {
571 if (texture->getCacheEntry()->key().isScratch()) {
572 // This texture was detached from the cache but the cache still had a ref to it but
573 // not a pointer to it. This will unref the texture and delete its resource cache
574 // entry.
575 delete texture->getCacheEntry();
576 }
577 return;
578 }
579
580 ASSERT_OWNED_RESOURCE(texture);
581 SkASSERT(texture->getCacheEntry());
582
583 // If this is a scratch texture we detached it from the cache
584 // while it was locked (to avoid two callers simultaneously getting
585 // the same texture).
586 if (texture->getCacheEntry()->key().isScratch()) {
587 if (fGpu->caps()->reuseScratchTextures() || texture->asRenderTarget()) {
588 fResourceCache->makeNonExclusive(texture->getCacheEntry());
589 this->purgeCache();
590 } else if (texture->unique()) {
591 // Only the cache now knows about this texture. Since we're never
592 // reusing scratch textures (in this code path) it would just be
593 // wasting time sitting in the cache.
594 fResourceCache->makeNonExclusive(texture->getCacheEntry());
595 fResourceCache->deleteResource(texture->getCacheEntry());
596 } else {
597 // In this case (there is still a non-cache ref) but we don't really
598 // want to readd it to the cache (since it will never be reused).
599 // Instead, give up the cache's ref and leave the decision up to
600 // addExistingTextureToCache once its ref count reaches 0. For
601 // this to work we need to leave it in the exclusive list.
bsalomonafbf2d62014-09-30 12:18:44 -0700602 texture->texturePriv().setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
robertphillipsdbe60742014-09-30 06:54:17 -0700603 // Give up the cache's ref to the texture
604 texture->unref();
605 }
606 }
607}
608
609void GrContext::purgeCache() {
610 if (fResourceCache) {
611 fResourceCache->purgeAsNeeded();
612 }
613}
614
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000615bool GrContext::OverbudgetCB(void* data) {
bsalomon49f085d2014-09-05 13:34:00 -0700616 SkASSERT(data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000617
618 GrContext* context = reinterpret_cast<GrContext*>(data);
619
620 // Flush the InOrderDrawBuffer to possibly free up some textures
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000621 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000622
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000623 return true;
624}
625
626
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000627GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000628 void* srcData,
629 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000630 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000631 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632}
633
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000634void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
635 fResourceCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000636}
637
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000638void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
639 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000640}
641
bsalomon@google.com91958362011-06-13 17:58:13 +0000642int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000643 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000644}
645
646int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000647 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000648}
649
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000650int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000651 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000652}
653
bsalomon@google.com27847de2011-02-22 20:59:41 +0000654///////////////////////////////////////////////////////////////////////////////
655
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000656GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
657 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000658}
659
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000660GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
661 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000662}
663
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000664///////////////////////////////////////////////////////////////////////////////
665
bsalomon@google.comb8670992012-07-25 21:27:09 +0000666bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000667 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000668 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000669 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670 return false;
671 }
672
tfarinaf9dae782014-06-06 06:35:28 -0700673 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000674
675 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700676 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000677 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000678 return false;
679 }
680 }
681 return true;
682}
683
bsalomon@google.com27847de2011-02-22 20:59:41 +0000684
bsalomon@google.com27847de2011-02-22 20:59:41 +0000685////////////////////////////////////////////////////////////////////////////////
686
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000687void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000688 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000689 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700690 GrRenderTarget* renderTarget) {
691 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000692 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000693 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700694 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
bsalomon41ebbdd2014-08-04 08:31:39 -0700695 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
696 if (NULL == target) {
697 return;
698 }
699 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000700}
701
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000702void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000703 // set rect to be big enough to fill the space, but not super-huge, so we
704 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000705 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000706 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000707 SkIntToScalar(getRenderTarget()->width()),
708 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000709 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000710 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000711 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700712 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000713
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000714 // We attempt to map r by the inverse matrix and draw that. mapRect will
715 // map the four corners and bound them with a new rect. This will not
716 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000717 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000718 if (!fViewMatrix.invert(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000719 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000720 return;
721 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722 inverse.mapRect(&r);
723 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000724 if (!am.setIdentity(this, paint.writable())) {
725 GrPrintf("Could not invert matrix\n");
726 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000727 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000728 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000729 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000730 if (paint->isAntiAlias()) {
731 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000732 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000733 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000734}
735
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000736#ifdef SK_DEVELOPER
737void GrContext::dumpFontCache() const {
738 fFontCache->dump();
739}
740#endif
741
bsalomon@google.com205d4602011-04-25 12:43:45 +0000742////////////////////////////////////////////////////////////////////////////////
743
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744/* create a triangle strip that strokes the specified triangle. There are 8
745 unique vertices, but we repreat the last 2 to close up. Alternatively we
746 could use an indices array, and then only send 8 verts, but not sure that
747 would be faster.
748 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000749static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000750 SkScalar width) {
751 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000752 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000753
754 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
755 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
756 verts[2].set(rect.fRight - rad, rect.fTop + rad);
757 verts[3].set(rect.fRight + rad, rect.fTop - rad);
758 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
759 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
760 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
761 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
762 verts[8] = verts[0];
763 verts[9] = verts[1];
764}
765
bsalomonc30aaa02014-08-13 07:15:29 -0700766static inline bool is_irect(const SkRect& r) {
767 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
768 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
769}
770
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000772 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000773 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000774 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700775 SkRect* devBoundRect) {
776 if (!target->getDrawState().canTweakAlphaForCoverage() &&
777 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000778#ifdef SK_DEBUG
bsalomon9c0822a2014-08-11 11:07:48 -0700779 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000780#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700781 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000782 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000783 const GrDrawState& drawState = target->getDrawState();
784 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000785 return false;
786 }
787
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000788#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000789 if (strokeWidth >= 0) {
790#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000791 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000792 return false;
793 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000794
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000795#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000796 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000797 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000798 return false;
799 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000800 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000801#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000802
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000803 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700804 if (strokeWidth < 0) {
805 return !is_irect(*devBoundRect);
806 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000807
bsalomon9c0822a2014-08-11 11:07:48 -0700808 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000809}
810
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000811static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
812 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
813 point.fY >= rect.fTop && point.fY <= rect.fBottom;
814}
815
bsalomon@google.com27847de2011-02-22 20:59:41 +0000816void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000817 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700818 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700819 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700820 SkPath path;
821 path.addRect(rect);
822 this->drawPath(paint, path, *strokeInfo);
823 return;
824 }
825
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000826 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000827 AutoCheckFlush acf(this);
828 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700829 if (NULL == target) {
830 return;
831 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000832
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000833 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700834 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700835 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000836
837 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
838 // cases where the RT is fully inside a stroke.
839 if (width < 0) {
840 SkRect rtRect;
841 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
842 SkRect clipSpaceRTRect = rtRect;
843 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700844 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000845 checkClip = true;
846 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
847 SkIntToScalar(this->getClip()->fOrigin.fY));
848 }
849 // Does the clip contain the entire RT?
850 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
851 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700852 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000853 return;
854 }
855 // Does the rect bound the RT?
856 SkPoint srcSpaceRTQuad[4];
857 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
858 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
859 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
860 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
861 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
862 // Will it blend?
863 GrColor clearColor;
864 if (paint.isOpaqueAndConstantColor(&clearColor)) {
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000865 target->clear(NULL, clearColor, true);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000866 return;
867 }
868 }
869 }
870 }
871
872 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000873 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000874 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700875 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700876
877 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
878
bsalomon@google.com205d4602011-04-25 12:43:45 +0000879 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000880 GrDrawState::AutoViewMatrixRestore avmr;
881 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000882 return;
883 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000884 if (width >= 0) {
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000885 fAARectRenderer->strokeAARect(this->getGpu(), target, rect,
bsalomon01c8da12014-08-04 09:21:30 -0700886 matrix, devBoundRect,
bsalomon9c0822a2014-08-11 11:07:48 -0700887 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000888 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000889 // filled AA rect
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000890 fAARectRenderer->fillAARect(this->getGpu(), target,
bsalomon9c0822a2014-08-11 11:07:48 -0700891 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000892 }
893 return;
894 }
895
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896 if (width >= 0) {
897 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000898 // Hairline could be done by just adding closing vertex to
899 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000900
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000902 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000903 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904
905 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000906 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907 return;
908 }
909
910 GrPrimitiveType primType;
911 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000912 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913
914 if (width > 0) {
915 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000916 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000917 setStrokeRectStrip(vertex, rect, width);
918 } else {
919 // hairline
920 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000921 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 vertex[0].set(rect.fLeft, rect.fTop);
923 vertex[1].set(rect.fRight, rect.fTop);
924 vertex[2].set(rect.fRight, rect.fBottom);
925 vertex[3].set(rect.fLeft, rect.fBottom);
926 vertex[4].set(rect.fLeft, rect.fTop);
927 }
928
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 target->drawNonIndexed(primType, 0, vertCount);
930 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000931 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700932 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933 }
934}
935
936void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000937 const SkRect& dstRect,
938 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000939 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000940 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000941 AutoCheckFlush acf(this);
942 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700943 if (NULL == target) {
944 return;
945 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000946
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000947 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
948
bsalomon01c8da12014-08-04 09:21:30 -0700949 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000950}
951
robertphillips@google.com42903302013-04-20 12:26:07 +0000952namespace {
953
954extern const GrVertexAttrib gPosUVColorAttribs[] = {
955 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000956 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding },
957 {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000958};
959
egdaniel7b3d5ee2014-08-28 05:41:14 -0700960static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint);
961static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
962
robertphillips@google.com42903302013-04-20 12:26:07 +0000963extern const GrVertexAttrib gPosColorAttribs[] = {
964 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000965 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000966};
967
egdaniel7b3d5ee2014-08-28 05:41:14 -0700968static const size_t kPosAttribsSize = sizeof(SkPoint);
969static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor);
970
robertphillips@google.com42903302013-04-20 12:26:07 +0000971static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000972 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000973 const GrColor* colors,
974 int* colorOffset,
975 int* texOffset) {
976 *texOffset = -1;
977 *colorOffset = -1;
978
bsalomon49f085d2014-09-05 13:34:00 -0700979 if (texCoords && colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000980 *texOffset = sizeof(SkPoint);
981 *colorOffset = 2*sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700982 drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700983 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000984 *texOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700985 drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700986 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000987 *colorOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700988 drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000989 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700990 drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000991 }
992}
993
994};
995
bsalomon@google.com27847de2011-02-22 20:59:41 +0000996void GrContext::drawVertices(const GrPaint& paint,
997 GrPrimitiveType primitiveType,
998 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000999 const SkPoint positions[],
1000 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001001 const GrColor colors[],
1002 const uint16_t indices[],
1003 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001004 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001005 AutoCheckFlush acf(this);
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001006 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
1007
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001008 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001009 if (NULL == target) {
1010 return;
1011 }
egdaniele61c4112014-06-12 10:24:21 -07001012 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001013
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001014 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1015
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001016 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +00001017 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001018
egdaniel7b3d5ee2014-08-28 05:41:14 -07001019 size_t VertexStride = drawState->getVertexStride();
1020 if (sizeof(SkPoint) != VertexStride) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001021 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001022 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001023 return;
1024 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001025 void* curVertex = geo.vertices();
1026
1027 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001028 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +00001029
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001030 if (texOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001031 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +00001032 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001033 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001034 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1035 }
egdaniel7b3d5ee2014-08-28 05:41:14 -07001036 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001037 }
1038 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001039 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001040 }
1041
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001042 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +00001043 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001044
bsalomon49f085d2014-09-05 13:34:00 -07001045 if (indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001046 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001047 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +00001048 target->resetIndexSource();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001049 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001050 target->drawNonIndexed(primitiveType, 0, vertexCount);
1051 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001052}
1053
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001054///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001055
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001056void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001057 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001058 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001059 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001060 return;
1061 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001062
egdanield58a0ba2014-06-11 10:30:05 -07001063 if (strokeInfo.isDashed()) {
1064 SkPath path;
1065 path.addRRect(rrect);
1066 this->drawPath(paint, path, strokeInfo);
1067 return;
1068 }
1069
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001070 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001071 AutoCheckFlush acf(this);
1072 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001073 if (NULL == target) {
1074 return;
1075 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001076
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001077 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1078
egdanield58a0ba2014-06-11 10:30:05 -07001079 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1080
1081 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001082 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001083 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -07001084 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001085 }
1086}
1087
1088///////////////////////////////////////////////////////////////////////////////
1089
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001090void GrContext::drawDRRect(const GrPaint& paint,
1091 const SkRRect& outer,
1092 const SkRRect& inner) {
1093 if (outer.isEmpty()) {
1094 return;
1095 }
1096
1097 AutoRestoreEffects are;
1098 AutoCheckFlush acf(this);
1099 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
1100
1101 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1102
1103 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
1104 SkPath path;
1105 path.addRRect(inner);
1106 path.addRRect(outer);
1107 path.setFillType(SkPath::kEvenOdd_FillType);
1108
egdanield58a0ba2014-06-11 10:30:05 -07001109 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001110 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
1111 }
1112}
1113
1114///////////////////////////////////////////////////////////////////////////////
1115
bsalomon@google.com93c96602012-04-27 13:05:21 +00001116void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001117 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001118 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001119 if (oval.isEmpty()) {
1120 return;
1121 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001122
egdanield58a0ba2014-06-11 10:30:05 -07001123 if (strokeInfo.isDashed()) {
1124 SkPath path;
1125 path.addOval(oval);
1126 this->drawPath(paint, path, strokeInfo);
1127 return;
1128 }
1129
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001130 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001131 AutoCheckFlush acf(this);
1132 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001133 if (NULL == target) {
1134 return;
1135 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001136
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001137 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1138
egdanield58a0ba2014-06-11 10:30:05 -07001139 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1140
1141
1142 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001143 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001144 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -07001145 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001146 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001147}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001148
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001149// Can 'path' be drawn as a pair of filled nested rectangles?
1150static bool is_nested_rects(GrDrawTarget* target,
1151 const SkPath& path,
1152 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001153 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001154 SkASSERT(stroke.isFillStyle());
1155
1156 if (path.isInverseFillType()) {
1157 return false;
1158 }
1159
1160 const GrDrawState& drawState = target->getDrawState();
1161
1162 // TODO: this restriction could be lifted if we were willing to apply
1163 // the matrix to all the points individually rather than just to the rect
1164 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1165 return false;
1166 }
1167
bsalomon9c0822a2014-08-11 11:07:48 -07001168 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1169 target->shouldDisableCoverageAAForBlend()) {
1170 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001171 }
1172
1173 SkPath::Direction dirs[2];
1174 if (!path.isNestedRects(rects, dirs)) {
1175 return false;
1176 }
1177
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001178 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001179 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001180 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001181 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001182
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001183 // Right now, nested rects where the margin is not the same width
1184 // all around do not render correctly
1185 const SkScalar* outer = rects[0].asScalars();
1186 const SkScalar* inner = rects[1].asScalars();
1187
1188 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1189 for (int i = 1; i < 4; ++i) {
1190 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1191 if (!SkScalarNearlyEqual(margin, temp)) {
1192 return false;
1193 }
1194 }
1195
1196 return true;
1197}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001198
egdanield58a0ba2014-06-11 10:30:05 -07001199void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001200
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001201 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001202 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001203 this->drawPaint(paint);
1204 }
1205 return;
1206 }
1207
egdanield58a0ba2014-06-11 10:30:05 -07001208 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001209 SkPoint pts[2];
1210 if (path.isLine(pts)) {
1211 AutoRestoreEffects are;
1212 AutoCheckFlush acf(this);
1213 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001214 if (NULL == target) {
1215 return;
1216 }
egdaniele61c4112014-06-12 10:24:21 -07001217 GrDrawState* drawState = target->drawState();
1218
1219 SkMatrix origViewMatrix = drawState->getViewMatrix();
1220 GrDrawState::AutoViewMatrixRestore avmr;
1221 if (avmr.setIdentity(target->drawState())) {
1222 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1223 origViewMatrix)) {
1224 return;
1225 }
1226 }
1227 }
1228
1229 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001230 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1231 SkTLazy<SkPath> effectPath;
1232 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1233 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1234 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1235 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1236 return;
1237 }
1238
1239 this->drawPath(paint, path, newStrokeInfo);
1240 return;
1241 }
1242
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001243 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001244 // Scratch textures can be recycled after they are returned to the texture
1245 // cache. This presents a potential hazard for buffered drawing. However,
1246 // the writePixels that uploads to the scratch will perform a flush so we're
1247 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001248 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001249 AutoCheckFlush acf(this);
1250 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001251 if (NULL == target) {
1252 return;
1253 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001254 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001255
egdaniel93a37bc2014-07-21 13:47:57 -07001256 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001257
egdanield58a0ba2014-06-11 10:30:05 -07001258 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1259
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001260 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1261
egdanield58a0ba2014-06-11 10:30:05 -07001262 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001263 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001264 SkRect rects[2];
1265
bsalomon9c0822a2014-08-11 11:07:48 -07001266 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001267 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001268 GrDrawState::AutoViewMatrixRestore avmr;
1269 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001270 return;
1271 }
1272
bsalomon9c0822a2014-08-11 11:07:48 -07001273 fAARectRenderer->fillAANestedRects(this->getGpu(), target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001274 return;
1275 }
1276 }
1277
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001278 SkRect ovalRect;
1279 bool isOval = path.isOval(&ovalRect);
1280
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001281 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001282 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1283 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001284 }
1285}
1286
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001287void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001288 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001289 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001290
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001291 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1292
1293
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001294 // An Assumption here is that path renderer would use some form of tweaking
1295 // the src color (either the input alpha or in the frag shader) to implement
1296 // aa. If we have some future driver-mojo path AA that can do the right
1297 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001298 bool useCoverageAA = useAA &&
1299 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1300 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001301
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001302
1303 GrPathRendererChain::DrawType type =
1304 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001305 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001306
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001307 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001308 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001309 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001310
1311 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001312 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001313
robertphillips@google.come79f3202014-02-11 16:30:21 +00001314 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001315 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001316 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001317 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1318 pathPtr = tmpPath.get();
1319 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001320 if (pathPtr->isEmpty()) {
1321 return;
1322 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001323 }
1324 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001325
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001326 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001327 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001328 }
1329
robertphillips@google.come79f3202014-02-11 16:30:21 +00001330 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001331#ifdef SK_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001332 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001333#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001334 return;
1335 }
1336
robertphillips@google.come79f3202014-02-11 16:30:21 +00001337 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001338}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001339
bsalomon@google.com27847de2011-02-22 20:59:41 +00001340////////////////////////////////////////////////////////////////////////////////
1341
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001342void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001343 if (NULL == fDrawBuffer) {
1344 return;
1345 }
1346
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001347 if (kDiscard_FlushBit & flagsBitfield) {
1348 fDrawBuffer->reset();
1349 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001350 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001351 }
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001352 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001353}
1354
bsalomon@google.com9c680582013-02-06 18:17:50 +00001355bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001356 int left, int top, int width, int height,
1357 GrPixelConfig config, const void* buffer, size_t rowBytes,
1358 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001359 ASSERT_OWNED_RESOURCE(texture);
1360
bsalomon@google.com9c680582013-02-06 18:17:50 +00001361 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
bsalomon49f085d2014-09-05 13:34:00 -07001362 if (texture->asRenderTarget()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001363 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1364 left, top, width, height,
1365 config, buffer, rowBytes, flags);
1366 } else {
1367 return false;
1368 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001369 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001370
bsalomonafbf2d62014-09-30 12:18:44 -07001371 if (!(kDontFlush_PixelOpsFlag & flags) && texture->surfacePriv().hasPendingIO()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001372 this->flush();
1373 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001374
bsalomon@google.com9c680582013-02-06 18:17:50 +00001375 return fGpu->writeTexturePixels(texture, left, top, width, height,
1376 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001377}
1378
bsalomon@google.com0342a852012-08-20 19:22:38 +00001379bool GrContext::readTexturePixels(GrTexture* texture,
1380 int left, int top, int width, int height,
1381 GrPixelConfig config, void* buffer, size_t rowBytes,
1382 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001383 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001384
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001385 GrRenderTarget* target = texture->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -07001386 if (target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001387 return this->readRenderTargetPixels(target,
1388 left, top, width, height,
1389 config, buffer, rowBytes,
1390 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001391 } else {
commit-bot@chromium.org3f80dd52013-10-23 15:32:39 +00001392 // TODO: make this more efficient for cases where we're reading the entire
1393 // texture, i.e., use GetTexImage() instead
1394
1395 // create scratch rendertarget and read from that
1396 GrAutoScratchTexture ast;
1397 GrTextureDesc desc;
1398 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1399 desc.fWidth = width;
1400 desc.fHeight = height;
1401 desc.fConfig = config;
1402 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
1403 ast.set(this, desc, kExact_ScratchTexMatch);
1404 GrTexture* dst = ast.texture();
bsalomon49f085d2014-09-05 13:34:00 -07001405 if (dst && (target = dst->asRenderTarget())) {
commit-bot@chromium.org3f80dd52013-10-23 15:32:39 +00001406 this->copyTexture(texture, target, NULL);
1407 return this->readRenderTargetPixels(target,
1408 left, top, width, height,
1409 config, buffer, rowBytes,
1410 flags);
1411 }
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +00001412
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001413 return false;
1414 }
1415}
1416
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001417#include "SkConfig8888.h"
1418
reed@google.com7111d462014-03-25 16:20:24 +00001419// toggles between RGBA and BGRA
1420static SkColorType toggle_colortype32(SkColorType ct) {
1421 if (kRGBA_8888_SkColorType == ct) {
1422 return kBGRA_8888_SkColorType;
1423 } else {
1424 SkASSERT(kBGRA_8888_SkColorType == ct);
1425 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001426 }
1427}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001428
bsalomon@google.com0342a852012-08-20 19:22:38 +00001429bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1430 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001431 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001432 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001433 ASSERT_OWNED_RESOURCE(target);
1434
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001435 if (NULL == target) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001436 target = fRenderTarget.get();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001437 if (NULL == target) {
1438 return false;
1439 }
1440 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001441
bsalomonafbf2d62014-09-30 12:18:44 -07001442 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001443 this->flush();
1444 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001445
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001446 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001447
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001448 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1449 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1450 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001451 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001452 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001453 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1454 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1455 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1456 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1457 // dstConfig.
1458 GrPixelConfig readConfig = dstConfig;
1459 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001460 if (GrPixelConfigSwapRAndB(dstConfig) ==
1461 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001462 readConfig = GrPixelConfigSwapRAndB(readConfig);
1463 swapRAndB = true;
1464 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001465
bsalomon@google.com0342a852012-08-20 19:22:38 +00001466 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001467
bsalomon@google.com9c680582013-02-06 18:17:50 +00001468 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001469 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001470 return false;
1471 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001472
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001473 // If the src is a texture and we would have to do conversions after read pixels, we instead
1474 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1475 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1476 // on the read back pixels.
1477 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001478 GrAutoScratchTexture ast;
bsalomon49f085d2014-09-05 13:34:00 -07001479 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001480 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1481 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001482 GrTextureDesc desc;
1483 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1484 desc.fWidth = width;
1485 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001486 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001487 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001488
bsalomon@google.com9c680582013-02-06 18:17:50 +00001489 // 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 +00001490 // match the passed rect. However, if we see many different size rectangles we will trash
1491 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1492 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001493 ScratchTexMatch match = kApprox_ScratchTexMatch;
1494 if (0 == left &&
1495 0 == top &&
1496 target->width() == width &&
1497 target->height() == height &&
1498 fGpu->fullReadPixelsIsFasterThanPartial()) {
1499 match = kExact_ScratchTexMatch;
1500 }
1501 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001502 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001504 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001505 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001506 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001507 textureMatrix.postIDiv(src->width(), src->height());
1508
joshualittb0a8a372014-09-23 09:50:21 -07001509 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001510 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001511 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1512 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001513 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001514 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001515 }
1516 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1517 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001518 if (fp || flipY || swapRAndB) {
1519 if (!fp) {
1520 fp.reset(GrConfigConversionEffect::Create(
1521 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1522 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001523 }
1524 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001525
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001526 // We protect the existing geometry here since it may not be
1527 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1528 // can be invoked in this method
1529 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001530 GrDrawState* drawState = fGpu->drawState();
joshualittb0a8a372014-09-23 09:50:21 -07001531 SkASSERT(fp);
1532 drawState->addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001533
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001534 drawState->setRenderTarget(texture->asRenderTarget());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001535 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon01c8da12014-08-04 09:21:30 -07001536 fGpu->drawSimpleRect(rect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001537 // we want to read back from the scratch's origin
1538 left = 0;
1539 top = 0;
1540 target = texture->asRenderTarget();
1541 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001542 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001543 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001544 if (!fGpu->readPixels(target,
1545 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001546 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001547 return false;
1548 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001549 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001550 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001551 SkDstPixelInfo dstPI;
1552 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1553 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001554 }
reed@google.com7111d462014-03-25 16:20:24 +00001555 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1556 dstPI.fPixels = buffer;
1557 dstPI.fRowBytes = rowBytes;
1558
1559 SkSrcPixelInfo srcPI;
1560 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1561 srcPI.fAlphaType = kPremul_SkAlphaType;
1562 srcPI.fPixels = buffer;
1563 srcPI.fRowBytes = rowBytes;
1564
1565 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001566 }
1567 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001568}
1569
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001570void GrContext::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001571 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001572 ASSERT_OWNED_RESOURCE(target);
1573 // In the future we may track whether there are any pending draws to this
1574 // target. We don't today so we always perform a flush. We don't promise
1575 // this to our clients, though.
1576 this->flush();
bsalomon49f085d2014-09-05 13:34:00 -07001577 if (fGpu) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001578 fGpu->resolveRenderTarget(target);
1579 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001580}
1581
bsalomon41ebbdd2014-08-04 08:31:39 -07001582void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1583 SkASSERT(renderTarget);
1584 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001585 AutoRestoreEffects are;
1586 AutoCheckFlush acf(this);
bsalomon41ebbdd2014-08-04 08:31:39 -07001587 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
1588 if (NULL == target) {
1589 return;
1590 }
1591 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001592}
1593
scroggo@google.coma2a31922012-12-07 19:14:45 +00001594void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001595 if (NULL == src || NULL == dst) {
1596 return;
1597 }
bsalomone3d4bf22014-09-23 09:15:03 -07001598 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001599 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001600
scroggo@google.coma2a31922012-12-07 19:14:45 +00001601 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
bsalomon49f085d2014-09-05 13:34:00 -07001602 if (topLeft) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001603 srcRect.offset(*topLeft);
1604 }
1605 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1606 if (!srcRect.intersect(srcBounds)) {
1607 return;
1608 }
junov96c118e2014-09-26 13:09:47 -07001609
1610 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
1611 if (NULL == target) {
1612 return;
1613 }
1614 SkIPoint dstPoint;
1615 dstPoint.setZero();
1616 target->copySurface(dst, src, srcRect, dstPoint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001617}
1618
bsalomon656f2cd2014-09-29 11:01:58 -07001619bool GrContext::writeRenderTargetPixels(GrRenderTarget* renderTarget,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001620 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001621 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001622 const void* buffer,
1623 size_t rowBytes,
1624 uint32_t flags) {
bsalomon656f2cd2014-09-29 11:01:58 -07001625 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001626
bsalomon656f2cd2014-09-29 11:01:58 -07001627 if (NULL == renderTarget) {
1628 renderTarget = fRenderTarget.get();
1629 if (NULL == renderTarget) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001630 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001631 }
1632 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001633
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001634 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1635 // desktop GL).
1636
1637 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1638 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1639 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640
bsalomon@google.com0342a852012-08-20 19:22:38 +00001641 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1642 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1643 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001644
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001645#if !defined(SK_BUILD_FOR_MAC)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001646 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1647 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1648 // HW is affected.
bsalomon656f2cd2014-09-29 11:01:58 -07001649 if (renderTarget->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1650 fGpu->canWriteTexturePixels(renderTarget->asTexture(), srcConfig)) {
1651 return this->writeTexturePixels(renderTarget->asTexture(),
bsalomon@google.com9c680582013-02-06 18:17:50 +00001652 left, top, width, height,
1653 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001654 }
1655#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001656
bsalomon@google.com9c680582013-02-06 18:17:50 +00001657 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1658 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1659 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1660 // when drawing the scratch to the dst using a conversion effect.
1661 bool swapRAndB = false;
1662 GrPixelConfig writeConfig = srcConfig;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001663 if (GrPixelConfigSwapRAndB(srcConfig) ==
bsalomon656f2cd2014-09-29 11:01:58 -07001664 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001665 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1666 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001667 }
1668
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001669 GrTextureDesc desc;
1670 desc.fWidth = width;
1671 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001672 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001673 GrAutoScratchTexture ast(this, desc);
1674 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001675 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001676 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001677 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001678
joshualittb0a8a372014-09-23 09:50:21 -07001679 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001680 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001681 textureMatrix.setIDiv(texture->width(), texture->height());
1682
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001683 // allocate a tmp buffer and sw convert the pixels to premul
1684 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1685
1686 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001687 if (!GrPixelConfigIs8888(srcConfig)) {
1688 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001689 }
joshualittb0a8a372014-09-23 09:50:21 -07001690 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001691 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
joshualittb0a8a372014-09-23 09:50:21 -07001692 if (NULL == fp) {
reed@google.com7111d462014-03-25 16:20:24 +00001693 SkSrcPixelInfo srcPI;
1694 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
1695 return false;
1696 }
1697 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1698 srcPI.fPixels = buffer;
1699 srcPI.fRowBytes = rowBytes;
1700
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +00001701 tmpPixels.reset(width * height);
reed@google.com7111d462014-03-25 16:20:24 +00001702
1703 SkDstPixelInfo dstPI;
1704 dstPI.fColorType = srcPI.fColorType;
1705 dstPI.fAlphaType = kPremul_SkAlphaType;
1706 dstPI.fPixels = tmpPixels.get();
1707 dstPI.fRowBytes = 4 * width;
1708
1709 if (!srcPI.convertPixelsTo(&dstPI, width, height)) {
1710 return false;
1711 }
1712
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001713 buffer = tmpPixels.get();
1714 rowBytes = 4 * width;
1715 }
1716 }
joshualittb0a8a372014-09-23 09:50:21 -07001717 if (NULL == fp) {
1718 fp.reset(GrConfigConversionEffect::Create(texture,
bsalomon656f2cd2014-09-29 11:01:58 -07001719 swapRAndB,
1720 GrConfigConversionEffect::kNone_PMConversion,
1721 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001722 }
1723
bsalomon@google.com9c680582013-02-06 18:17:50 +00001724 if (!this->writeTexturePixels(texture,
1725 0, 0, width, height,
1726 writeConfig, buffer, rowBytes,
1727 flags & ~kUnpremul_PixelOpsFlag)) {
1728 return false;
1729 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001730
bsalomon@google.com137f1342013-05-29 21:27:53 +00001731 SkMatrix matrix;
1732 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon656f2cd2014-09-29 11:01:58 -07001733
1734 // This function can be called in the midst of drawing another object (e.g., when uploading a
1735 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1736 // drawing a rect to the render target.
1737 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, kYes_BufferedDraw, NULL, NULL);
1738 GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit, &matrix);
1739 GrDrawState* drawState = drawTarget->drawState();
joshualittb0a8a372014-09-23 09:50:21 -07001740 drawState->addColorProcessor(fp);
bsalomon656f2cd2014-09-29 11:01:58 -07001741 drawState->setRenderTarget(renderTarget);
1742 drawState->disableState(GrDrawState::kClip_StateBit);
1743 drawTarget->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001744 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001745}
1746////////////////////////////////////////////////////////////////////////////////
1747
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001748GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
1749 BufferedDraw buffered,
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001750 AutoRestoreEffects* are,
1751 AutoCheckFlush* acf) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001752 // All users of this draw state should be freeing up all effects when they're done.
1753 // Otherwise effects that own resources may keep those resources alive indefinitely.
joshualittbd769d02014-09-04 08:56:46 -07001754 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() &&
1755 !fDrawState->hasGeometryProcessor());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001756
bsalomon41ebbdd2014-08-04 08:31:39 -07001757 if (NULL == fGpu) {
1758 return NULL;
1759 }
1760
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001761 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +00001762 fDrawBuffer->flush();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001763 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001764 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001765 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon49f085d2014-09-05 13:34:00 -07001766 if (paint) {
1767 SkASSERT(are);
1768 SkASSERT(acf);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001769 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001770 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001771#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1772 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001773 !fDrawState->couldApplyCoverage(fGpu->caps())) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001774 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1775 }
1776#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001777 // Clear any vertex attributes configured for the previous use of the
1778 // GrDrawState which can effect which blend optimizations are in effect.
1779 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001780 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001781 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001782 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001783 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001784 GrDrawTarget* target;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001785 if (kYes_BufferedDraw == buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001786 fLastDrawWasBuffered = kYes_BufferedDraw;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001787 target = fDrawBuffer;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001788 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001789 SkASSERT(kNo_BufferedDraw == buffered);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001790 fLastDrawWasBuffered = kNo_BufferedDraw;
1791 target = fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001792 }
bsalomon49f085d2014-09-05 13:34:00 -07001793 fDrawState->setState(GrDrawState::kClip_StateBit, fClip &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001794 !fClip->fClipStack->isWideOpen());
1795 target->setClip(fClip);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001796 SkASSERT(fDrawState == target->drawState());
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001797 return target;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001798}
1799
robertphillips@google.com72176b22012-05-23 13:19:12 +00001800/*
1801 * This method finds a path renderer that can draw the specified path on
1802 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001803 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001804 * can be individually allowed/disallowed via the "allowSW" boolean.
1805 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001806GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001807 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001808 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001809 bool allowSW,
1810 GrPathRendererChain::DrawType drawType,
1811 GrPathRendererChain::StencilSupport* stencilSupport) {
1812
bsalomon@google.com30085192011-08-19 15:42:31 +00001813 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001814 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001815 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001816
sugoi@google.com12b4e272012-12-06 20:13:11 +00001817 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1818 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001819 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001820 drawType,
1821 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001822
1823 if (NULL == pr && allowSW) {
1824 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001825 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001826 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001827 pr = fSoftwarePathRenderer;
1828 }
1829
1830 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001831}
1832
bsalomon@google.com27847de2011-02-22 20:59:41 +00001833////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001834bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1835 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001836}
1837
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001838int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1839 SkScalar dpi) const {
1840 if (!this->isConfigRenderable(config, true)) {
1841 return 0;
1842 }
1843 int chosenSampleCount = 0;
1844 if (fGpu->caps()->pathRenderingSupport()) {
1845 if (dpi >= 250.0f) {
1846 chosenSampleCount = 4;
1847 } else {
1848 chosenSampleCount = 16;
1849 }
1850 }
1851 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1852 chosenSampleCount : 0;
1853}
1854
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001855void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001856 SkASSERT(NULL == fDrawBuffer);
1857 SkASSERT(NULL == fDrawBufferVBAllocPool);
1858 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001859
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001860 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001861 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001862 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001863 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001864 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001865 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001866 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001867 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001868
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001869 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001870 fDrawBufferVBAllocPool,
1871 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001872
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001873 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001874}
1875
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001876GrDrawTarget* GrContext::getTextTarget() {
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001877 return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001878}
1879
1880const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1881 return fGpu->getQuadIndexBuffer();
1882}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001883
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001884namespace {
1885void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1886 GrConfigConversionEffect::PMConversion pmToUPM;
1887 GrConfigConversionEffect::PMConversion upmToPM;
1888 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1889 *pmToUPMValue = pmToUPM;
1890 *upmToPMValue = upmToPM;
1891}
1892}
1893
joshualittb0a8a372014-09-23 09:50:21 -07001894const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1895 bool swapRAndB,
1896 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001897 if (!fDidTestPMConversions) {
1898 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001899 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001900 }
1901 GrConfigConversionEffect::PMConversion pmToUPM =
1902 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1903 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001904 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001905 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001906 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001907 }
1908}
1909
joshualittb0a8a372014-09-23 09:50:21 -07001910const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1911 bool swapRAndB,
1912 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001913 if (!fDidTestPMConversions) {
1914 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001915 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001916 }
1917 GrConfigConversionEffect::PMConversion upmToPM =
1918 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1919 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001920 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001921 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001922 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001923 }
1924}
1925
bsalomon6d3fe022014-07-25 08:35:45 -07001926void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001927 fResourceCache->purgeAsNeeded(1, resource->gpuMemorySize());
1928 fResourceCache->addResource(resourceKey, resource);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001929}
1930
bsalomon6d3fe022014-07-25 08:35:45 -07001931GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
1932 GrGpuResource* resource = fResourceCache->find(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001933 SkSafeRef(resource);
1934 return resource;
1935}
1936
egdanielbbcb38d2014-06-19 10:19:29 -07001937void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1938 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001939 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001940 fDrawBuffer->addGpuTraceMarker(marker);
1941 }
1942}
1943
1944void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1945 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001946 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001947 fDrawBuffer->removeGpuTraceMarker(marker);
1948 }
1949}
1950
bsalomon@google.comc4364992011-11-07 15:54:49 +00001951///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001952#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001953void GrContext::printCacheStats() const {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001954 fResourceCache->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001955}
1956#endif
robertphillips754f4e92014-09-18 13:52:08 -07001957
1958#if GR_GPU_STATS
1959const GrContext::GPUStats* GrContext::gpuStats() const {
1960 return fGpu->gpuStats();
1961}
1962#endif
1963