blob: 9a180ee6539ec487cb64191630c303994504f650 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
10
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000011#include "effects/GrConfigConversionEffect.h"
egdaniele61c4112014-06-12 10:24:21 -070012#include "effects/GrDashingEffect.h"
13#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000014
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000015#include "GrAARectRenderer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000017#include "GrGpu.h"
bsalomon453cf402014-11-11 14:15:57 -080018#include "GrGpuResource.h"
19#include "GrGpuResourceCacheAccess.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070020#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000021#include "GrDrawTargetCaps.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000022#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000023#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000024#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000025#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000026#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000027#include "GrPathUtils.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"
bsalomon81beccc2014-10-13 12:32:55 -070039#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000040#include "SkGr.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
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000047#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000048 // change this to a 1 to see notifications when partial coverage fails
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#else
51 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
52#endif
53
bsalomon@google.com60361492012-03-15 17:47:06 +000054static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
56
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000057static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
58static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000059
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000060#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000061
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000062// Glorified typedef to avoid including GrDrawState.h in GrContext.h
63class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {};
64
krajcevski9c6d4d72014-08-12 07:26:25 -070065GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
66 const Options* opts) {
67 GrContext* context;
68 if (NULL == opts) {
69 context = SkNEW_ARGS(GrContext, (Options()));
70 } else {
71 context = SkNEW_ARGS(GrContext, (*opts));
72 }
73
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000074 if (context->init(backend, backendContext)) {
75 return context;
76 } else {
77 context->unref();
78 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000079 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000080}
81
krajcevski9c6d4d72014-08-12 07:26:25 -070082GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000083 fDrawState = NULL;
84 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000085 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086 fPathRendererChain = NULL;
87 fSoftwarePathRenderer = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -070088 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000089 fFontCache = NULL;
90 fDrawBuffer = NULL;
91 fDrawBufferVBAllocPool = NULL;
92 fDrawBufferIBAllocPool = NULL;
93 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000094 fOvalRenderer = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000095 fViewMatrix.reset();
robertphillips@google.com44a91dc2013-07-25 15:32:06 +000096 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000097}
98
99bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000100 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000101
102 fGpu = GrGpu::Create(backend, backendContext, this);
103 if (NULL == fGpu) {
104 return false;
105 }
bsalomon33435572014-11-05 14:47:41 -0800106 this->initCommon();
107 return true;
108}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000109
bsalomon33435572014-11-05 14:47:41 -0800110void GrContext::initCommon() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000111 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000112
bsalomonc8dc1f72014-08-21 13:02:13 -0700113 fResourceCache2 = SkNEW(GrResourceCache2);
bsalomon66a450f2014-11-13 13:19:10 -0800114 fResourceCache2->setOverBudgetCallback(OverBudgetCB, this);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000115
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000116 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
117
robertphillips4ec84da2014-06-24 13:10:43 -0700118 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000119
joshualittb44293e2014-10-28 08:12:18 -0700120 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000121 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000122
123 fDidTestPMConversions = false;
124
125 this->setupDrawBuffer();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000126}
127
bsalomon@google.com27847de2011-02-22 20:59:41 +0000128GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000129 if (NULL == fGpu) {
130 return;
131 }
132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000134
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000135 for (int i = 0; i < fCleanUpData.count(); ++i) {
136 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
137 }
138
bsalomon33435572014-11-05 14:47:41 -0800139 SkDELETE(fResourceCache2);
bsalomon33435572014-11-05 14:47:41 -0800140 SkDELETE(fFontCache);
141 SkDELETE(fDrawBuffer);
142 SkDELETE(fDrawBufferVBAllocPool);
143 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000144
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000145 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000146 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000147
bsalomon@google.com205d4602011-04-25 12:43:45 +0000148 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000149 SkSafeUnref(fPathRendererChain);
150 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000151 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000152}
153
bsalomon2354f842014-07-28 13:48:36 -0700154void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000155 // abandon first to so destructors
156 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700157 fResourceCache2->abandonAll();
158
robertphillipse3371302014-09-17 06:01:06 -0700159 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000160
bsalomon@google.com30085192011-08-19 15:42:31 +0000161 // a path renderer may be holding onto resources that
162 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000163 SkSafeSetNull(fPathRendererChain);
164 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000165
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000166 delete fDrawBuffer;
167 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000168
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000169 delete fDrawBufferVBAllocPool;
170 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000171
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000172 delete fDrawBufferIBAllocPool;
173 fDrawBufferIBAllocPool = NULL;
174
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000175 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000176 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000177
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000178 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000179 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000180}
181
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000182void GrContext::resetContext(uint32_t state) {
183 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000184}
185
186void GrContext::freeGpuResources() {
187 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000188
bsalomon49f085d2014-09-05 13:34:00 -0700189 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700190 fDrawBuffer->purgeResources();
191 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000192
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000193 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000194 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000195
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000196 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000197 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000198 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000199 SkSafeSetNull(fPathRendererChain);
200 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000201}
202
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000203void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon66a450f2014-11-13 13:19:10 -0800204 if (resourceCount) {
205 *resourceCount = fResourceCache2->getResourceCount();
206 }
207 if (resourceBytes) {
208 *resourceBytes = fResourceCache2->getResourceBytes();
209 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000210}
211
kkinnunenc6cb56f2014-06-24 00:12:27 -0700212GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
213 const SkDeviceProperties&
214 leakyProperties,
215 bool enableDistanceFieldFonts) {
jvanverth8c27a182014-10-14 08:45:50 -0700216 if (fGpu->caps()->pathRenderingSupport() && renderTarget->getStencilBuffer() &&
217 renderTarget->isMultisampled()) {
218 return GrStencilAndCoverTextContext::Create(this, leakyProperties);
219 }
220
221 return GrDistanceFieldTextContext::Create(this, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700222}
223
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000224////////////////////////////////////////////////////////////////////////////////
225
bsalomonf2703d82014-10-28 14:33:06 -0700226GrTexture* GrContext::findAndRefTexture(const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000227 const GrCacheID& cacheID,
228 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700229 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon8b79d232014-11-10 10:19:06 -0800230
231 GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
bsalomon37dd3312014-11-03 08:47:23 -0800232 if (resource) {
bsalomon8b79d232014-11-10 10:19:06 -0800233 SkASSERT(static_cast<GrSurface*>(resource)->asTexture());
bsalomon37dd3312014-11-03 08:47:23 -0800234 return static_cast<GrSurface*>(resource)->asTexture();
bsalomon37dd3312014-11-03 08:47:23 -0800235 }
bsalomon8b79d232014-11-10 10:19:06 -0800236 return NULL;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000237}
238
bsalomonf2703d82014-10-28 14:33:06 -0700239bool GrContext::isTextureInCache(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000240 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000241 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700242 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon8b79d232014-11-10 10:19:06 -0800243 return fResourceCache2->hasContentKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000244}
245
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000246void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon66a450f2014-11-13 13:19:10 -0800247 // TODO: Make GrStencilBuffers use the scratch mechanism rather than content keys.
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000248 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000249
250 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
251 sb->height(),
252 sb->numSamples());
bsalomon66a450f2014-11-13 13:19:10 -0800253 SkAssertResult(sb->cacheAccess().setContentKey(resourceKey));
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000254}
255
bsalomon8b79d232014-11-10 10:19:06 -0800256GrStencilBuffer* GrContext::findAndRefStencilBuffer(int width, int height, int sampleCnt) {
257 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width, height, sampleCnt);
258 GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000259 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000260}
261
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000262static void stretch_image(void* dst,
263 int dstW,
264 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000265 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000266 int srcW,
267 int srcH,
268 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000269 SkFixed dx = (srcW << 16) / dstW;
270 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000271
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000272 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000273
robertphillips@google.com8b169312013-10-15 17:47:36 +0000274 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000275 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000276 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000277 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
278 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000279 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000280 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000281 x += dx;
282 }
283 y += dy;
284 }
285}
286
robertphillips@google.com42903302013-04-20 12:26:07 +0000287namespace {
288
289// position + local coordinate
290extern const GrVertexAttrib gVertexAttribs[] = {
291 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000292 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000293};
294
295};
296
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000297// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000298// the current hardware. Resize the texture to be a POT
bsalomonf2703d82014-10-28 14:33:06 -0700299GrTexture* GrContext::createResizedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000300 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000301 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000302 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000303 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000304 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000305 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000306 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000307
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000308 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000309 return NULL;
310 }
311 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000312
bsalomonf2703d82014-10-28 14:33:06 -0700313 GrSurfaceDesc rtDesc = desc;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000314 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700315 kRenderTarget_GrSurfaceFlag |
316 kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000317 rtDesc.fWidth = GrNextPow2(desc.fWidth);
318 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000319
320 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
321
bsalomon49f085d2014-09-05 13:34:00 -0700322 if (texture) {
joshualitt5c55fef2014-10-31 14:04:35 -0700323 GrDrawTarget::AutoStateRestore asr(fDrawBuffer, GrDrawTarget::kReset_ASRInit);
324 GrDrawState* drawState = fDrawBuffer->drawState();
robertphillips@google.com3319f332012-08-13 18:00:36 +0000325 drawState->setRenderTarget(texture->asRenderTarget());
326
327 // if filtering is not desired then we want to ensure all
328 // texels in the resampled image are copies of texels from
329 // the original.
joshualitt5c55fef2014-10-31 14:04:35 -0700330 GrTextureParams params(SkShader::kClamp_TileMode,
331 filter ? GrTextureParams::kBilerp_FilterMode :
332 GrTextureParams::kNone_FilterMode);
joshualittb0a8a372014-09-23 09:50:21 -0700333 drawState->addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000334
egdaniel7b3d5ee2014-08-28 05:41:14 -0700335 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs),
336 2 * sizeof(SkPoint));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000337
joshualitt5c55fef2014-10-31 14:04:35 -0700338 GrDrawTarget::AutoReleaseGeometry arg(fDrawBuffer, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000339
340 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000341 SkPoint* verts = (SkPoint*) arg.vertices();
342 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
343 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
joshualitt5c55fef2014-10-31 14:04:35 -0700344 fDrawBuffer->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000345 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000346 } else {
347 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000348 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000349 // not. Either implement filtered stretch blit on CPU or just create
350 // one when FBO case fails.
351
bsalomonf2703d82014-10-28 14:33:06 -0700352 rtDesc.fFlags = kNone_GrSurfaceFlags;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000353 // no longer need to clamp at min RT size.
354 rtDesc.fWidth = GrNextPow2(desc.fWidth);
355 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000356
357 // We shouldn't be resizing a compressed texture.
358 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
359
robertphillips@google.com8b169312013-10-15 17:47:36 +0000360 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700361 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000362 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
363 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000364
365 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
366
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000367 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700368 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000369 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000370
371 return texture;
372}
373
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000374GrTexture* GrContext::createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700375 const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000376 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000377 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000378 size_t rowBytes,
379 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700380 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000381
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000382 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700383 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700384 // We do not know how to resize compressed textures.
385 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
386
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000387 texture = this->createResizedTexture(desc, cacheID,
388 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700389 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000390 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700391 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000393
bsalomon49f085d2014-09-05 13:34:00 -0700394 if (texture) {
bsalomon66a450f2014-11-13 13:19:10 -0800395 if (texture->cacheAccess().setContentKey(resourceKey)) {
396 if (cacheKey) {
397 *cacheKey = resourceKey;
398 }
399 } else {
400 texture->unref();
401 texture = NULL;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000402 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000403 }
404
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000405 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000406}
407
bsalomonf2703d82014-10-28 14:33:06 -0700408GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700409 bool calledDuringFlush) {
bsalomonbcf0a522014-10-08 08:40:09 -0700410 // kNoStencil has no meaning if kRT isn't set.
bsalomonf2703d82014-10-28 14:33:06 -0700411 SkASSERT((inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
412 !(inDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000413
bsalomonbcf0a522014-10-08 08:40:09 -0700414 // Make sure caller has checked for renderability if kRT is set.
bsalomonf2703d82014-10-28 14:33:06 -0700415 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
bsalomonbcf0a522014-10-08 08:40:09 -0700416 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000417
bsalomonf2703d82014-10-28 14:33:06 -0700418 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000419
bsalomonf2703d82014-10-28 14:33:06 -0700420 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
421 GrSurfaceFlags origFlags = desc->fFlags;
bsalomonbcf0a522014-10-08 08:40:09 -0700422 if (kApprox_ScratchTexMatch == match) {
423 // bin by pow2 with a reasonable min
424 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700425 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700426 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
427 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000428 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000429
bsalomonbcf0a522014-10-08 08:40:09 -0700430 do {
431 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
bsalomon000f8292014-10-15 19:04:14 -0700432 uint32_t scratchFlags = 0;
433 if (calledDuringFlush) {
434 scratchFlags = GrResourceCache2::kRequireNoPendingIO_ScratchFlag;
bsalomonf2703d82014-10-28 14:33:06 -0700435 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon000f8292014-10-15 19:04:14 -0700436 // If it is not a render target then it will most likely be populated by
437 // writePixels() which will trigger a flush if the texture has pending IO.
438 scratchFlags = GrResourceCache2::kPreferNoPendingIO_ScratchFlag;
439 }
440 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
bsalomonbcf0a522014-10-08 08:40:09 -0700441 if (resource) {
bsalomon37dd3312014-11-03 08:47:23 -0800442 return static_cast<GrSurface*>(resource)->asTexture();
bsalomonbcf0a522014-10-08 08:40:09 -0700443 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000444
bsalomonbcf0a522014-10-08 08:40:09 -0700445 if (kExact_ScratchTexMatch == match) {
446 break;
447 }
448 // We had a cache miss and we are in approx mode, relax the fit of the flags.
449
450 // We no longer try to reuse textures that were previously used as render targets in
451 // situations where no RT is needed; doing otherwise can confuse the video driver and
452 // cause significant performance problems in some cases.
bsalomonf2703d82014-10-28 14:33:06 -0700453 if (desc->fFlags & kNoStencil_GrSurfaceFlag) {
454 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrSurfaceFlag;
bsalomonbcf0a522014-10-08 08:40:09 -0700455 } else {
456 break;
457 }
458
459 } while (true);
460
461 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000462 }
463
bsalomon66a450f2014-11-13 13:19:10 -0800464 GrTexture* texture = fGpu->createTexture(*desc, NULL, 0);
bsalomonbcf0a522014-10-08 08:40:09 -0700465 SkASSERT(NULL == texture ||
bsalomon453cf402014-11-11 14:15:57 -0800466 texture->cacheAccess().getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
bsalomonbcf0a522014-10-08 08:40:09 -0700467 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400468}
469
bsalomon66a450f2014-11-13 13:19:10 -0800470void GrContext::OverBudgetCB(void* data) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000471 // Flush the InOrderDrawBuffer to possibly free up some textures
bsalomon66a450f2014-11-13 13:19:10 -0800472 SkASSERT(data);
473 GrContext* context = reinterpret_cast<GrContext*>(data);
474 context->flush();
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000475}
476
477
bsalomonf2703d82014-10-28 14:33:06 -0700478GrTexture* GrContext::createUncachedTexture(const GrSurfaceDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000479 void* srcData,
480 size_t rowBytes) {
bsalomonf2703d82014-10-28 14:33:06 -0700481 GrSurfaceDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000482 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000483}
484
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000485void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
bsalomon66a450f2014-11-13 13:19:10 -0800486 if (maxTextures) {
487 *maxTextures = fResourceCache2->getMaxResourceCount();
488 }
489 if (maxTextureBytes) {
490 *maxTextureBytes = fResourceCache2->getMaxResourceBytes();
491 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000492}
493
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000494void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon66a450f2014-11-13 13:19:10 -0800495 fResourceCache2->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000496}
497
bsalomon@google.com91958362011-06-13 17:58:13 +0000498int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000499 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000500}
501
502int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000503 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000504}
505
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000506int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000507 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000508}
509
bsalomon@google.com27847de2011-02-22 20:59:41 +0000510///////////////////////////////////////////////////////////////////////////////
511
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000512GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
513 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000514}
515
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000516GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
517 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000518}
519
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000520///////////////////////////////////////////////////////////////////////////////
521
bsalomon@google.comb8670992012-07-25 21:27:09 +0000522bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000523 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000524 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000525 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000526 return false;
527 }
528
tfarinaf9dae782014-06-06 06:35:28 -0700529 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000530
531 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700532 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000533 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000534 return false;
535 }
536 }
537 return true;
538}
539
bsalomon@google.com27847de2011-02-22 20:59:41 +0000540
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541////////////////////////////////////////////////////////////////////////////////
542
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000543void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000544 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000545 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700546 GrRenderTarget* renderTarget) {
547 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800548 SkASSERT(renderTarget);
549
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000550 AutoRestoreEffects are;
egdanield78a1682014-07-09 10:41:26 -0700551 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
bsalomon66a450f2014-11-13 13:19:10 -0800552 GrDrawTarget* target = this->prepareToDraw(NULL, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -0700553 if (NULL == target) {
554 return;
555 }
556 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557}
558
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000559void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560 // set rect to be big enough to fill the space, but not super-huge, so we
561 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000562 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000563 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000564 SkIntToScalar(getRenderTarget()->width()),
565 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000566 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000567 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000568 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700569 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000570
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000571 // We attempt to map r by the inverse matrix and draw that. mapRect will
572 // map the four corners and bound them with a new rect. This will not
573 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000574 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000575 if (!fViewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700576 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000577 return;
578 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000579 inverse.mapRect(&r);
580 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000581 if (!am.setIdentity(this, paint.writable())) {
tfarina38406c82014-10-31 07:11:12 -0700582 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000583 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000584 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000585 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000586 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000587 if (paint->isAntiAlias()) {
588 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000589 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000590 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000591}
592
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000593#ifdef SK_DEVELOPER
594void GrContext::dumpFontCache() const {
595 fFontCache->dump();
596}
597#endif
598
bsalomon@google.com205d4602011-04-25 12:43:45 +0000599////////////////////////////////////////////////////////////////////////////////
600
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601/* create a triangle strip that strokes the specified triangle. There are 8
602 unique vertices, but we repreat the last 2 to close up. Alternatively we
603 could use an indices array, and then only send 8 verts, but not sure that
604 would be faster.
605 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000606static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000607 SkScalar width) {
608 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000609 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000610
611 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
612 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
613 verts[2].set(rect.fRight - rad, rect.fTop + rad);
614 verts[3].set(rect.fRight + rad, rect.fTop - rad);
615 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
616 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
617 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
618 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
619 verts[8] = verts[0];
620 verts[9] = verts[1];
621}
622
bsalomonc30aaa02014-08-13 07:15:29 -0700623static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700624 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
625 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700626}
627
bsalomon@google.com205d4602011-04-25 12:43:45 +0000628static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000629 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000630 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000631 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700632 SkRect* devBoundRect) {
633 if (!target->getDrawState().canTweakAlphaForCoverage() &&
634 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000635#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700636 //SkDebugf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000637#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700638 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000639 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000640 const GrDrawState& drawState = target->getDrawState();
641 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000642 return false;
643 }
644
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000645#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000646 if (strokeWidth >= 0) {
647#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000648 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000649 return false;
650 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000651
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000652#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000653 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000654 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000655 return false;
656 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000657 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000658#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000659
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000660 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700661 if (strokeWidth < 0) {
662 return !is_irect(*devBoundRect);
663 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000664
bsalomon9c0822a2014-08-11 11:07:48 -0700665 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000666}
667
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000668static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
669 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
670 point.fY >= rect.fTop && point.fY <= rect.fBottom;
671}
672
bsalomon@google.com27847de2011-02-22 20:59:41 +0000673void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000674 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700675 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700676 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700677 SkPath path;
678 path.addRect(rect);
679 this->drawPath(paint, path, *strokeInfo);
680 return;
681 }
682
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000683 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -0800684 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -0700685 if (NULL == target) {
686 return;
687 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000688
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000689 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700690 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700691 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000692
693 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
694 // cases where the RT is fully inside a stroke.
695 if (width < 0) {
696 SkRect rtRect;
697 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
698 SkRect clipSpaceRTRect = rtRect;
699 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700700 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000701 checkClip = true;
702 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
703 SkIntToScalar(this->getClip()->fOrigin.fY));
704 }
705 // Does the clip contain the entire RT?
706 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
707 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700708 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000709 return;
710 }
711 // Does the rect bound the RT?
712 SkPoint srcSpaceRTQuad[4];
713 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
714 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
715 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
716 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
717 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
718 // Will it blend?
719 GrColor clearColor;
720 if (paint.isOpaqueAndConstantColor(&clearColor)) {
bsalomon89c62982014-11-03 12:08:42 -0800721 target->clear(NULL, clearColor, true, fRenderTarget);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000722 return;
723 }
724 }
725 }
726 }
727
728 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000729 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000730 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700731 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700732
bsalomon@google.com205d4602011-04-25 12:43:45 +0000733 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000734 GrDrawState::AutoViewMatrixRestore avmr;
735 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000736 return;
737 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000738 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800739 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
740 fAARectRenderer->strokeAARect(target, rect, matrix, devBoundRect, strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000741 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000742 // filled AA rect
joshualittb44293e2014-10-28 08:12:18 -0700743 fAARectRenderer->fillAARect(target,
bsalomon9c0822a2014-08-11 11:07:48 -0700744 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 }
746 return;
747 }
748
bsalomon@google.com27847de2011-02-22 20:59:41 +0000749 if (width >= 0) {
750 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000751 // Hairline could be done by just adding closing vertex to
752 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000753
bsalomon@google.com27847de2011-02-22 20:59:41 +0000754 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000755 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000756 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000757
758 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700759 SkDebugf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000760 return;
761 }
762
763 GrPrimitiveType primType;
764 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000765 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000766
767 if (width > 0) {
768 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000769 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000770 setStrokeRectStrip(vertex, rect, width);
771 } else {
772 // hairline
773 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000774 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775 vertex[0].set(rect.fLeft, rect.fTop);
776 vertex[1].set(rect.fRight, rect.fTop);
777 vertex[2].set(rect.fRight, rect.fBottom);
778 vertex[3].set(rect.fLeft, rect.fBottom);
779 vertex[4].set(rect.fLeft, rect.fTop);
780 }
781
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782 target->drawNonIndexed(primType, 0, vertCount);
783 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000784 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700785 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 }
787}
788
789void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000790 const SkRect& dstRect,
791 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000792 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000793 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -0800794 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -0700795 if (NULL == target) {
796 return;
797 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000798
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000799 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
800
bsalomon01c8da12014-08-04 09:21:30 -0700801 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000802}
803
robertphillips@google.com42903302013-04-20 12:26:07 +0000804namespace {
805
806extern const GrVertexAttrib gPosUVColorAttribs[] = {
807 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000808 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding },
809 {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000810};
811
egdaniel7b3d5ee2014-08-28 05:41:14 -0700812static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint);
813static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
814
robertphillips@google.com42903302013-04-20 12:26:07 +0000815extern const GrVertexAttrib gPosColorAttribs[] = {
816 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000817 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000818};
819
egdaniel7b3d5ee2014-08-28 05:41:14 -0700820static const size_t kPosAttribsSize = sizeof(SkPoint);
821static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor);
822
robertphillips@google.com42903302013-04-20 12:26:07 +0000823static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000824 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000825 const GrColor* colors,
826 int* colorOffset,
827 int* texOffset) {
828 *texOffset = -1;
829 *colorOffset = -1;
830
bsalomon49f085d2014-09-05 13:34:00 -0700831 if (texCoords && colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000832 *texOffset = sizeof(SkPoint);
833 *colorOffset = 2*sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700834 drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700835 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000836 *texOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700837 drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize);
bsalomon49f085d2014-09-05 13:34:00 -0700838 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000839 *colorOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700840 drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000841 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700842 drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000843 }
844}
845
846};
847
bsalomon@google.com27847de2011-02-22 20:59:41 +0000848void GrContext::drawVertices(const GrPaint& paint,
849 GrPrimitiveType primitiveType,
850 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000851 const SkPoint positions[],
852 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853 const GrColor colors[],
854 const uint16_t indices[],
855 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000856 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -0800857 GrDrawTarget::AutoReleaseGeometry geo;
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000858
bsalomon66a450f2014-11-13 13:19:10 -0800859 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -0700860 if (NULL == target) {
861 return;
862 }
egdaniele61c4112014-06-12 10:24:21 -0700863 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000864
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000865 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
866
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000867 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +0000868 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000869
egdaniel7b3d5ee2014-08-28 05:41:14 -0700870 size_t VertexStride = drawState->getVertexStride();
joshualittd1aa8ff2014-11-04 07:47:55 -0800871 if (!geo.set(target, vertexCount, indexCount)) {
872 SkDebugf("Failed to get space for vertices!\n");
873 return;
874 }
875 void* curVertex = geo.vertices();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876
joshualittd1aa8ff2014-11-04 07:47:55 -0800877 for (int i = 0; i < vertexCount; ++i) {
878 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879
joshualittd1aa8ff2014-11-04 07:47:55 -0800880 if (texOffset >= 0) {
881 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000882 }
joshualittd1aa8ff2014-11-04 07:47:55 -0800883 if (colorOffset >= 0) {
884 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
885 }
886 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000887 }
888
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000889 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000890 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon49f085d2014-09-05 13:34:00 -0700891 if (indices) {
joshualittd1aa8ff2014-11-04 07:47:55 -0800892 uint16_t* curIndex = (uint16_t*)geo.indices();
893 for (int i = 0; i < indexCount; ++i) {
894 curIndex[i] = indices[i];
895 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000896 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000898 target->drawNonIndexed(primitiveType, 0, vertexCount);
899 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900}
901
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000902///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000903
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000904void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000905 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700906 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000907 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000908 return;
909 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000910
egdanield58a0ba2014-06-11 10:30:05 -0700911 if (strokeInfo.isDashed()) {
912 SkPath path;
913 path.addRRect(rrect);
914 this->drawPath(paint, path, strokeInfo);
915 return;
916 }
917
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000918 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -0800919 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -0700920 if (NULL == target) {
921 return;
922 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000923
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000924 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
925
egdanield58a0ba2014-06-11 10:30:05 -0700926 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
927
928 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000929 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000930 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -0700931 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000932 }
933}
934
935///////////////////////////////////////////////////////////////////////////////
936
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000937void GrContext::drawDRRect(const GrPaint& paint,
938 const SkRRect& outer,
939 const SkRRect& inner) {
940 if (outer.isEmpty()) {
941 return;
942 }
943
944 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -0800945 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000946
947 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
948
949 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
950 SkPath path;
951 path.addRRect(inner);
952 path.addRRect(outer);
953 path.setFillType(SkPath::kEvenOdd_FillType);
954
egdanield58a0ba2014-06-11 10:30:05 -0700955 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000956 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
957 }
958}
959
960///////////////////////////////////////////////////////////////////////////////
961
bsalomon@google.com93c96602012-04-27 13:05:21 +0000962void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000963 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700964 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000965 if (oval.isEmpty()) {
966 return;
967 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000968
egdanield58a0ba2014-06-11 10:30:05 -0700969 if (strokeInfo.isDashed()) {
970 SkPath path;
971 path.addOval(oval);
972 this->drawPath(paint, path, strokeInfo);
973 return;
974 }
975
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000976 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -0800977 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -0700978 if (NULL == target) {
979 return;
980 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000981
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000982 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
983
egdanield58a0ba2014-06-11 10:30:05 -0700984 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
985
986
987 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +0000988 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000989 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -0700990 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +0000991 }
bsalomon@google.com150d2842012-01-12 20:19:56 +0000992}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000993
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000994// Can 'path' be drawn as a pair of filled nested rectangles?
995static bool is_nested_rects(GrDrawTarget* target,
996 const SkPath& path,
997 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -0700998 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +0000999 SkASSERT(stroke.isFillStyle());
1000
1001 if (path.isInverseFillType()) {
1002 return false;
1003 }
1004
1005 const GrDrawState& drawState = target->getDrawState();
1006
1007 // TODO: this restriction could be lifted if we were willing to apply
1008 // the matrix to all the points individually rather than just to the rect
1009 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1010 return false;
1011 }
1012
bsalomon9c0822a2014-08-11 11:07:48 -07001013 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1014 target->shouldDisableCoverageAAForBlend()) {
1015 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001016 }
1017
1018 SkPath::Direction dirs[2];
1019 if (!path.isNestedRects(rects, dirs)) {
1020 return false;
1021 }
1022
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001023 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001024 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001025 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001026 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001027
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001028 // Right now, nested rects where the margin is not the same width
1029 // all around do not render correctly
1030 const SkScalar* outer = rects[0].asScalars();
1031 const SkScalar* inner = rects[1].asScalars();
1032
robertphillips183e9852014-10-21 11:25:37 -07001033 bool allEq = true;
1034
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001035 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001036 bool allGoE1 = margin >= SK_Scalar1;
1037
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001038 for (int i = 1; i < 4; ++i) {
1039 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001040 if (temp < SK_Scalar1) {
1041 allGoE1 = false;
1042 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001043 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001044 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001045 }
1046 }
1047
robertphillips183e9852014-10-21 11:25:37 -07001048 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001049}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001050
egdanield58a0ba2014-06-11 10:30:05 -07001051void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001052
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001053 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001054 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001055 this->drawPaint(paint);
1056 }
1057 return;
1058 }
1059
egdanield58a0ba2014-06-11 10:30:05 -07001060 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001061 SkPoint pts[2];
1062 if (path.isLine(pts)) {
1063 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -08001064 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -07001065 if (NULL == target) {
1066 return;
1067 }
egdaniele61c4112014-06-12 10:24:21 -07001068 GrDrawState* drawState = target->drawState();
1069
1070 SkMatrix origViewMatrix = drawState->getViewMatrix();
1071 GrDrawState::AutoViewMatrixRestore avmr;
1072 if (avmr.setIdentity(target->drawState())) {
1073 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1074 origViewMatrix)) {
1075 return;
1076 }
1077 }
1078 }
1079
1080 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001081 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1082 SkTLazy<SkPath> effectPath;
1083 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1084 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1085 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1086 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1087 return;
1088 }
1089
1090 this->drawPath(paint, path, newStrokeInfo);
1091 return;
1092 }
1093
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001094 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001095 // Scratch textures can be recycled after they are returned to the texture
1096 // cache. This presents a potential hazard for buffered drawing. However,
1097 // the writePixels that uploads to the scratch will perform a flush so we're
1098 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001099 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -08001100 GrDrawTarget* target = this->prepareToDraw(&paint, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -07001101 if (NULL == target) {
1102 return;
1103 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001104 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001105
egdaniel93a37bc2014-07-21 13:47:57 -07001106 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001107
egdanield58a0ba2014-06-11 10:30:05 -07001108 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1109
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001110 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1111
egdanield58a0ba2014-06-11 10:30:05 -07001112 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001113 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001114 SkRect rects[2];
1115
bsalomon9c0822a2014-08-11 11:07:48 -07001116 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001117 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001118 GrDrawState::AutoViewMatrixRestore avmr;
1119 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001120 return;
1121 }
1122
joshualittb44293e2014-10-28 08:12:18 -07001123 fAARectRenderer->fillAANestedRects(target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001124 return;
1125 }
1126 }
1127
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001128 SkRect ovalRect;
1129 bool isOval = path.isOval(&ovalRect);
1130
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001131 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001132 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1133 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001134 }
1135}
1136
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001137void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001138 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001139 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001140
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001141 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1142
1143
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001144 // An Assumption here is that path renderer would use some form of tweaking
1145 // the src color (either the input alpha or in the frag shader) to implement
1146 // aa. If we have some future driver-mojo path AA that can do the right
1147 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001148 bool useCoverageAA = useAA &&
1149 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1150 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001151
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001152
1153 GrPathRendererChain::DrawType type =
1154 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001155 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001156
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001157 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001158 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001159 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001160
1161 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001162 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001163
robertphillips@google.come79f3202014-02-11 16:30:21 +00001164 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001165 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001166 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001167 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1168 pathPtr = tmpPath.get();
1169 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001170 if (pathPtr->isEmpty()) {
1171 return;
1172 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001173 }
1174 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001175
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001176 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001177 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001178 }
1179
robertphillips@google.come79f3202014-02-11 16:30:21 +00001180 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001181#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001182 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001183#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001184 return;
1185 }
1186
robertphillips@google.come79f3202014-02-11 16:30:21 +00001187 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001188}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001189
bsalomon@google.com27847de2011-02-22 20:59:41 +00001190////////////////////////////////////////////////////////////////////////////////
1191
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001192void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001193 if (NULL == fDrawBuffer) {
1194 return;
1195 }
1196
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001197 if (kDiscard_FlushBit & flagsBitfield) {
1198 fDrawBuffer->reset();
1199 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001200 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001201 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001202}
1203
bsalomon81beccc2014-10-13 12:32:55 -07001204bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1205 const void* inPixels, size_t outRowBytes, void* outPixels) {
1206 SkSrcPixelInfo srcPI;
1207 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001208 return false;
1209 }
bsalomon81beccc2014-10-13 12:32:55 -07001210 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1211 srcPI.fPixels = inPixels;
1212 srcPI.fRowBytes = inRowBytes;
1213
1214 SkDstPixelInfo dstPI;
1215 dstPI.fColorType = srcPI.fColorType;
1216 dstPI.fAlphaType = kPremul_SkAlphaType;
1217 dstPI.fPixels = outPixels;
1218 dstPI.fRowBytes = outRowBytes;
1219
1220 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001221}
1222
bsalomon81beccc2014-10-13 12:32:55 -07001223bool GrContext::writeSurfacePixels(GrSurface* surface,
1224 int left, int top, int width, int height,
1225 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1226 uint32_t pixelOpsFlags) {
1227
1228 {
1229 GrTexture* texture = NULL;
1230 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1231 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1232
1233 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1234 surface->surfacePriv().hasPendingIO()) {
1235 this->flush();
1236 }
1237 return fGpu->writeTexturePixels(texture, left, top, width, height,
1238 srcConfig, buffer, rowBytes);
1239 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1240 // upload is already flushed.
1241 }
1242 }
1243
1244 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1245 GrRenderTarget* renderTarget = surface->asRenderTarget();
1246 if (NULL == renderTarget) {
1247 return false;
1248 }
1249
1250 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1251 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1252 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1253 // when drawing the scratch to the dst using a conversion effect.
1254 bool swapRAndB = false;
1255 GrPixelConfig writeConfig = srcConfig;
1256 if (GrPixelConfigSwapRAndB(srcConfig) ==
1257 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1258 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1259 swapRAndB = true;
1260 }
1261
bsalomonf2703d82014-10-28 14:33:06 -07001262 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001263 desc.fWidth = width;
1264 desc.fHeight = height;
1265 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001266 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1267 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001268 return false;
1269 }
1270
1271 SkAutoTUnref<const GrFragmentProcessor> fp;
1272 SkMatrix textureMatrix;
1273 textureMatrix.setIDiv(texture->width(), texture->height());
1274
1275 // allocate a tmp buffer and sw convert the pixels to premul
1276 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1277
1278 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1279 if (!GrPixelConfigIs8888(srcConfig)) {
1280 return false;
1281 }
1282 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1283 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1284 if (NULL == fp) {
1285 size_t tmpRowBytes = 4 * width;
1286 tmpPixels.reset(width * height);
1287 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1288 tmpPixels.get())) {
1289 return false;
1290 }
1291 rowBytes = tmpRowBytes;
1292 buffer = tmpPixels.get();
1293 }
1294 }
1295 if (NULL == fp) {
1296 fp.reset(GrConfigConversionEffect::Create(texture,
1297 swapRAndB,
1298 GrConfigConversionEffect::kNone_PMConversion,
1299 textureMatrix));
1300 }
1301
1302 // Even if the client told us not to flush, we still flush here. The client may have known that
1303 // writes to the original surface caused no data hazards, but they can't know that the scratch
1304 // we just got is safe.
1305 if (texture->surfacePriv().hasPendingIO()) {
1306 this->flush();
1307 }
1308 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1309 writeConfig, buffer, rowBytes)) {
1310 return false;
1311 }
1312
1313 SkMatrix matrix;
1314 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1315
1316 // This function can be called in the midst of drawing another object (e.g., when uploading a
1317 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1318 // drawing a rect to the render target.
1319 // The bracket ensures we pop the stack if we wind up flushing below.
1320 {
bsalomon66a450f2014-11-13 13:19:10 -08001321 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL);
bsalomon81beccc2014-10-13 12:32:55 -07001322 GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit,
1323 &matrix);
1324 GrDrawState* drawState = drawTarget->drawState();
1325 drawState->addColorProcessor(fp);
1326 drawState->setRenderTarget(renderTarget);
1327 drawState->disableState(GrDrawState::kClip_StateBit);
1328 drawTarget->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
1329 }
1330
1331 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1332 this->flushSurfaceWrites(surface);
1333 }
1334
1335 return true;
1336}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001337
reed@google.com7111d462014-03-25 16:20:24 +00001338// toggles between RGBA and BGRA
1339static SkColorType toggle_colortype32(SkColorType ct) {
1340 if (kRGBA_8888_SkColorType == ct) {
1341 return kBGRA_8888_SkColorType;
1342 } else {
1343 SkASSERT(kBGRA_8888_SkColorType == ct);
1344 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001345 }
1346}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001347
bsalomon@google.com0342a852012-08-20 19:22:38 +00001348bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1349 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001350 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001351 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001352 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001353 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001354
bsalomonafbf2d62014-09-30 12:18:44 -07001355 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001356 this->flush();
1357 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001358
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001359 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001360
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001361 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1362 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1363 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001364 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001365 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001366 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1367 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1368 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1369 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1370 // dstConfig.
1371 GrPixelConfig readConfig = dstConfig;
1372 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001373 if (GrPixelConfigSwapRAndB(dstConfig) ==
1374 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001375 readConfig = GrPixelConfigSwapRAndB(readConfig);
1376 swapRAndB = true;
1377 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001378
bsalomon@google.com0342a852012-08-20 19:22:38 +00001379 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001380
bsalomon@google.com9c680582013-02-06 18:17:50 +00001381 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001382 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001383 return false;
1384 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001385
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001386 // If the src is a texture and we would have to do conversions after read pixels, we instead
1387 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1388 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1389 // on the read back pixels.
1390 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001391 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001392 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001393 GrSurfaceDesc desc;
1394 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001395 desc.fWidth = width;
1396 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001397 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001398 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001399
bsalomon@google.com9c680582013-02-06 18:17:50 +00001400 // 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 +00001401 // match the passed rect. However, if we see many different size rectangles we will trash
1402 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1403 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001404 ScratchTexMatch match = kApprox_ScratchTexMatch;
1405 if (0 == left &&
1406 0 == top &&
1407 target->width() == width &&
1408 target->height() == height &&
1409 fGpu->fullReadPixelsIsFasterThanPartial()) {
1410 match = kExact_ScratchTexMatch;
1411 }
bsalomone3059732014-10-14 11:47:22 -07001412 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, match));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001413 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001414 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001415 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001416 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001417 textureMatrix.postIDiv(src->width(), src->height());
1418
joshualittb0a8a372014-09-23 09:50:21 -07001419 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001420 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001421 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1422 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001423 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001424 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001425 }
1426 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1427 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001428 if (fp || flipY || swapRAndB) {
1429 if (!fp) {
1430 fp.reset(GrConfigConversionEffect::Create(
1431 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1432 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001433 }
1434 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001435
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001436 // We protect the existing geometry here since it may not be
1437 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1438 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001439 {
1440 GrDrawTarget::AutoGeometryAndStatePush agasp(fDrawBuffer,
1441 GrDrawTarget::kReset_ASRInit);
1442 GrDrawState* drawState = fDrawBuffer->drawState();
1443 SkASSERT(fp);
1444 drawState->addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001445
joshualitt5c55fef2014-10-31 14:04:35 -07001446 drawState->setRenderTarget(texture->asRenderTarget());
1447 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
1448 fDrawBuffer->drawSimpleRect(rect);
1449 // we want to read back from the scratch's origin
1450 left = 0;
1451 top = 0;
1452 target = texture->asRenderTarget();
1453 }
1454 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001455 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001456 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001457 }
joshualitt5c55fef2014-10-31 14:04:35 -07001458
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001459 if (!fGpu->readPixels(target,
1460 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001461 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001462 return false;
1463 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001464 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001465 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001466 SkDstPixelInfo dstPI;
1467 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1468 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001469 }
reed@google.com7111d462014-03-25 16:20:24 +00001470 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1471 dstPI.fPixels = buffer;
1472 dstPI.fRowBytes = rowBytes;
1473
1474 SkSrcPixelInfo srcPI;
1475 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1476 srcPI.fAlphaType = kPremul_SkAlphaType;
1477 srcPI.fPixels = buffer;
1478 srcPI.fRowBytes = rowBytes;
1479
1480 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001481 }
1482 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001483}
1484
bsalomon87a94eb2014-11-03 14:28:32 -08001485void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
1486 SkASSERT(surface);
1487 ASSERT_OWNED_RESOURCE(surface);
1488 if (surface->surfacePriv().hasPendingIO()) {
1489 this->flush();
1490 }
1491 GrRenderTarget* rt = surface->asRenderTarget();
1492 if (fGpu && rt) {
1493 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001494 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001495}
1496
bsalomon41ebbdd2014-08-04 08:31:39 -07001497void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1498 SkASSERT(renderTarget);
1499 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001500 AutoRestoreEffects are;
bsalomon66a450f2014-11-13 13:19:10 -08001501 GrDrawTarget* target = this->prepareToDraw(NULL, &are);
bsalomon41ebbdd2014-08-04 08:31:39 -07001502 if (NULL == target) {
1503 return;
1504 }
1505 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001506}
1507
bsalomonf80bfed2014-10-07 05:56:02 -07001508void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1509 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001510 if (NULL == src || NULL == dst) {
1511 return;
1512 }
bsalomone3d4bf22014-09-23 09:15:03 -07001513 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001514 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001515
bsalomonf80bfed2014-10-07 05:56:02 -07001516 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1517 // here.
junov96c118e2014-09-26 13:09:47 -07001518
bsalomon66a450f2014-11-13 13:19:10 -08001519 GrDrawTarget* target = this->prepareToDraw(NULL, NULL);
junov96c118e2014-09-26 13:09:47 -07001520 if (NULL == target) {
1521 return;
1522 }
junov96c118e2014-09-26 13:09:47 -07001523 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001524
1525 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1526 this->flush();
1527 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001528}
1529
bsalomonf80bfed2014-10-07 05:56:02 -07001530void GrContext::flushSurfaceWrites(GrSurface* surface) {
1531 if (surface->surfacePriv().hasPendingWrite()) {
1532 this->flush();
1533 }
1534}
1535
bsalomon@google.com27847de2011-02-22 20:59:41 +00001536////////////////////////////////////////////////////////////////////////////////
1537
bsalomon66a450f2014-11-13 13:19:10 -08001538GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, AutoRestoreEffects* are) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001539 // All users of this draw state should be freeing up all effects when they're done.
1540 // Otherwise effects that own resources may keep those resources alive indefinitely.
joshualittbd769d02014-09-04 08:56:46 -07001541 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() &&
1542 !fDrawState->hasGeometryProcessor());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001543
bsalomon41ebbdd2014-08-04 08:31:39 -07001544 if (NULL == fGpu) {
1545 return NULL;
1546 }
1547
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001548 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon49f085d2014-09-05 13:34:00 -07001549 if (paint) {
1550 SkASSERT(are);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001551 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001552 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001553#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1554 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001555 !fDrawState->couldApplyCoverage(fGpu->caps())) {
tfarina38406c82014-10-31 07:11:12 -07001556 SkDebugf("Partial pixel coverage will be incorrectly blended.\n");
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001557 }
1558#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001559 // Clear any vertex attributes configured for the previous use of the
1560 // GrDrawState which can effect which blend optimizations are in effect.
1561 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001562 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001563 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001564 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001565 }
bsalomon49f085d2014-09-05 13:34:00 -07001566 fDrawState->setState(GrDrawState::kClip_StateBit, fClip &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001567 !fClip->fClipStack->isWideOpen());
joshualitt5c55fef2014-10-31 14:04:35 -07001568 fDrawBuffer->setClip(fClip);
1569 SkASSERT(fDrawState == fDrawBuffer->drawState());
1570 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571}
1572
robertphillips@google.com72176b22012-05-23 13:19:12 +00001573/*
1574 * This method finds a path renderer that can draw the specified path on
1575 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001576 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001577 * can be individually allowed/disallowed via the "allowSW" boolean.
1578 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001579GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001580 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001581 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001582 bool allowSW,
1583 GrPathRendererChain::DrawType drawType,
1584 GrPathRendererChain::StencilSupport* stencilSupport) {
1585
bsalomon@google.com30085192011-08-19 15:42:31 +00001586 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001587 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001588 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001589
sugoi@google.com12b4e272012-12-06 20:13:11 +00001590 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1591 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001592 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001593 drawType,
1594 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001595
1596 if (NULL == pr && allowSW) {
1597 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001598 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001599 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001600 pr = fSoftwarePathRenderer;
1601 }
1602
1603 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001604}
1605
bsalomon@google.com27847de2011-02-22 20:59:41 +00001606////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001607bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1608 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001609}
1610
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001611int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1612 SkScalar dpi) const {
1613 if (!this->isConfigRenderable(config, true)) {
1614 return 0;
1615 }
1616 int chosenSampleCount = 0;
1617 if (fGpu->caps()->pathRenderingSupport()) {
1618 if (dpi >= 250.0f) {
1619 chosenSampleCount = 4;
1620 } else {
1621 chosenSampleCount = 16;
1622 }
1623 }
1624 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1625 chosenSampleCount : 0;
1626}
1627
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001628void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001629 SkASSERT(NULL == fDrawBuffer);
1630 SkASSERT(NULL == fDrawBufferVBAllocPool);
1631 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001632
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001633 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001634 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001635 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001636 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001637 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001638 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001639 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001640 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001641
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001642 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001643 fDrawBufferVBAllocPool,
1644 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001645
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001646 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001647}
1648
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001649GrDrawTarget* GrContext::getTextTarget() {
bsalomon66a450f2014-11-13 13:19:10 -08001650 return this->prepareToDraw(NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001651}
1652
1653const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1654 return fGpu->getQuadIndexBuffer();
1655}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001656
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001657namespace {
1658void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1659 GrConfigConversionEffect::PMConversion pmToUPM;
1660 GrConfigConversionEffect::PMConversion upmToPM;
1661 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1662 *pmToUPMValue = pmToUPM;
1663 *upmToPMValue = upmToPM;
1664}
1665}
1666
joshualittb0a8a372014-09-23 09:50:21 -07001667const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1668 bool swapRAndB,
1669 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001670 if (!fDidTestPMConversions) {
1671 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001672 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001673 }
1674 GrConfigConversionEffect::PMConversion pmToUPM =
1675 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1676 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001677 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001678 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001679 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001680 }
1681}
1682
joshualittb0a8a372014-09-23 09:50:21 -07001683const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1684 bool swapRAndB,
1685 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001686 if (!fDidTestPMConversions) {
1687 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001688 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001689 }
1690 GrConfigConversionEffect::PMConversion upmToPM =
1691 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1692 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001693 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001694 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001695 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001696 }
1697}
1698
bsalomon6d3fe022014-07-25 08:35:45 -07001699void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
bsalomon66a450f2014-11-13 13:19:10 -08001700 resource->cacheAccess().setContentKey(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001701}
1702
bsalomon6d3fe022014-07-25 08:35:45 -07001703GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
bsalomon66a450f2014-11-13 13:19:10 -08001704 return fResourceCache2->findAndRefContentResource(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001705}
1706
egdanielbbcb38d2014-06-19 10:19:29 -07001707void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1708 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001709 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001710 fDrawBuffer->addGpuTraceMarker(marker);
1711 }
1712}
1713
1714void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1715 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001716 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001717 fDrawBuffer->removeGpuTraceMarker(marker);
1718 }
1719}
1720
bsalomon@google.comc4364992011-11-07 15:54:49 +00001721///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001722#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001723void GrContext::printCacheStats() const {
bsalomon66a450f2014-11-13 13:19:10 -08001724 fResourceCache2->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001725}
1726#endif
robertphillips754f4e92014-09-18 13:52:08 -07001727
1728#if GR_GPU_STATS
1729const GrContext::GPUStats* GrContext::gpuStats() const {
1730 return fGpu->gpuStats();
1731}
1732#endif
1733