blob: ea3ea281fef063416f2a97a0f1d8672f58a6b326 [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
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000012#include "GrBufferAllocPool.h"
joshualitt5478d422014-11-14 16:00:38 -080013#include "GrDefaultGeoProcFactory.h"
jvanverth787cdf92014-12-04 10:46:50 -080014#include "GrFontCache.h"
bsalomon453cf402014-11-11 14:15:57 -080015#include "GrGpuResource.h"
16#include "GrGpuResourceCacheAccess.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070017#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000018#include "GrDrawTargetCaps.h"
joshualitt5478d422014-11-14 16:00:38 -080019#include "GrGpu.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000020#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000021#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000022#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000023#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000024#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000025#include "GrPathUtils.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070026#include "GrResourceCache2.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000027#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070028#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070029#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070030#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070031#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070032#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000033#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070034#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070035#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000036#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000037#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000038#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000039#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000040#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000041#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000042
joshualitt5478d422014-11-14 16:00:38 -080043#include "effects/GrConfigConversionEffect.h"
44#include "effects/GrDashingEffect.h"
45#include "effects/GrSingleTextureEffect.h"
46
bsalomon@google.com60361492012-03-15 17:47:06 +000047static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000048static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
49
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000050static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
51static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000054
bsalomonf21dab92014-11-13 13:33:28 -080055class GrContext::AutoCheckFlush {
56public:
57 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
58
59 ~AutoCheckFlush() {
60 if (fContext->fFlushToReduceCacheSize) {
61 fContext->flush();
62 }
63 }
64
65private:
66 GrContext* fContext;
67};
68
krajcevski9c6d4d72014-08-12 07:26:25 -070069GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
70 const Options* opts) {
71 GrContext* context;
72 if (NULL == opts) {
73 context = SkNEW_ARGS(GrContext, (Options()));
74 } else {
75 context = SkNEW_ARGS(GrContext, (*opts));
76 }
77
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078 if (context->init(backend, backendContext)) {
79 return context;
80 } else {
81 context->unref();
82 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000083 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000084}
85
krajcevski9c6d4d72014-08-12 07:26:25 -070086GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000088 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000089 fPathRendererChain = NULL;
90 fSoftwarePathRenderer = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -070091 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092 fFontCache = NULL;
93 fDrawBuffer = NULL;
94 fDrawBufferVBAllocPool = NULL;
95 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -080096 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000097 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000098 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +000099 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000100}
101
102bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000103 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000104
105 fGpu = GrGpu::Create(backend, backendContext, this);
106 if (NULL == fGpu) {
107 return false;
108 }
bsalomon33435572014-11-05 14:47:41 -0800109 this->initCommon();
110 return true;
111}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000112
bsalomon33435572014-11-05 14:47:41 -0800113void GrContext::initCommon() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700114 fResourceCache2 = SkNEW(GrResourceCache2);
bsalomon71cb0c22014-11-14 12:10:14 -0800115 fResourceCache2->setOverBudgetCallback(OverBudgetCB, this);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000116
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000117 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
118
robertphillips4ec84da2014-06-24 13:10:43 -0700119 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000120
joshualittb44293e2014-10-28 08:12:18 -0700121 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
joshualitt5531d512014-12-17 15:50:11 -0800122 fOvalRenderer = SkNEW_ARGS(GrOvalRenderer, (fGpu));
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000123
124 fDidTestPMConversions = false;
125
126 this->setupDrawBuffer();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000127}
128
bsalomon@google.com27847de2011-02-22 20:59:41 +0000129GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000130 if (NULL == fGpu) {
131 return;
132 }
133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000135
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000136 for (int i = 0; i < fCleanUpData.count(); ++i) {
137 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
138 }
139
bsalomon33435572014-11-05 14:47:41 -0800140 SkDELETE(fResourceCache2);
bsalomon33435572014-11-05 14:47:41 -0800141 SkDELETE(fFontCache);
142 SkDELETE(fDrawBuffer);
143 SkDELETE(fDrawBufferVBAllocPool);
144 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000145
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000146 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000147 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000148
bsalomon@google.com205d4602011-04-25 12:43:45 +0000149 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000150 SkSafeUnref(fPathRendererChain);
151 SkSafeUnref(fSoftwarePathRenderer);
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 {
bsalomon71cb0c22014-11-14 12:10:14 -0800204 if (resourceCount) {
bsalomondace19e2014-11-17 07:34:06 -0800205 *resourceCount = fResourceCache2->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800206 }
207 if (resourceBytes) {
bsalomondace19e2014-11-17 07:34:06 -0800208 *resourceBytes = fResourceCache2->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800209 }
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
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000226static void stretch_image(void* dst,
227 int dstW,
228 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000229 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000230 int srcW,
231 int srcH,
232 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000233 SkFixed dx = (srcW << 16) / dstW;
234 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000235
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000236 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000237
robertphillips@google.com8b169312013-10-15 17:47:36 +0000238 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000240 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000241 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
242 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000243 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000244 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000245 x += dx;
246 }
247 y += dy;
248 }
249}
250
bsalomon24db3b12015-01-23 04:24:04 -0800251enum ResizeFlags {
252 /**
253 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated or mipped but the
254 * hardware doesn't support that feature.
255 */
256 kStretchToPOT_ResizeFlag = 0x1,
257 /**
258 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
259 * stretched texture should be bilerped.
260 */
261 kBilerp_ResizeFlag = 0x2,
262};
263
264static uint32_t get_texture_flags(const GrGpu* gpu,
265 const GrTextureParams* params,
266 const GrSurfaceDesc& desc) {
267 uint32_t flags = 0;
268 bool tiled = params && params->isTiled();
269 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
270 if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
271 flags |= kStretchToPOT_ResizeFlag;
272 switch(params->filterMode()) {
273 case GrTextureParams::kNone_FilterMode:
274 break;
275 case GrTextureParams::kBilerp_FilterMode:
276 case GrTextureParams::kMipMap_FilterMode:
277 flags |= kBilerp_ResizeFlag;
278 break;
279 }
280 }
281 }
282 return flags;
283}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000285// the current hardware. Resize the texture to be a POT
bsalomonf2703d82014-10-28 14:33:06 -0700286GrTexture* GrContext::createResizedTexture(const GrSurfaceDesc& desc,
bsalomon24db3b12015-01-23 04:24:04 -0800287 const GrContentKey& origKey,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000288 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000289 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000290 bool filter) {
bsalomon24db3b12015-01-23 04:24:04 -0800291 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, origKey, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000292 if (NULL == clampedTexture) {
bsalomon24db3b12015-01-23 04:24:04 -0800293 clampedTexture.reset(this->createTexture(NULL, desc, origKey, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000294
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000295 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000296 return NULL;
297 }
bsalomon24db3b12015-01-23 04:24:04 -0800298 clampedTexture->cacheAccess().setContentKey(origKey);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000299 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000300
bsalomonf2703d82014-10-28 14:33:06 -0700301 GrSurfaceDesc rtDesc = desc;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000302 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700303 kRenderTarget_GrSurfaceFlag |
304 kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000305 rtDesc.fWidth = GrNextPow2(desc.fWidth);
306 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000307
bsalomon5236cf42015-01-14 10:42:08 -0800308 GrTexture* texture = fGpu->createTexture(rtDesc, true, NULL, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000309
bsalomon49f085d2014-09-05 13:34:00 -0700310 if (texture) {
egdaniel8dd688b2015-01-22 10:16:09 -0800311 GrPipelineBuilder pipelineBuilder;
312 pipelineBuilder.setRenderTarget(texture->asRenderTarget());
robertphillips@google.com3319f332012-08-13 18:00:36 +0000313
314 // if filtering is not desired then we want to ensure all
315 // texels in the resampled image are copies of texels from
316 // the original.
joshualitt5c55fef2014-10-31 14:04:35 -0700317 GrTextureParams params(SkShader::kClamp_TileMode,
318 filter ? GrTextureParams::kBilerp_FilterMode :
319 GrTextureParams::kNone_FilterMode);
egdaniel8dd688b2015-01-22 10:16:09 -0800320 pipelineBuilder.addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000321
joshualitt2dd1ae02014-12-03 06:24:10 -0800322 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType |
323 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt56995b52014-12-11 15:44:02 -0800324 SkAutoTUnref<const GrGeometryProcessor> gp(
joshualitt8059eb92014-12-29 15:10:07 -0800325 GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000326
joshualitt2dd1ae02014-12-03 06:24:10 -0800327 GrDrawTarget::AutoReleaseGeometry arg(fDrawBuffer, 4, gp->getVertexStride(), 0);
328 SkASSERT(gp->getVertexStride() == 2 * sizeof(SkPoint));
robertphillips@google.com3319f332012-08-13 18:00:36 +0000329
330 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000331 SkPoint* verts = (SkPoint*) arg.vertices();
332 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
333 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
egdaniel8dd688b2015-01-22 10:16:09 -0800334 fDrawBuffer->drawNonIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon24db3b12015-01-23 04:24:04 -0800335 } else {
336 texture->unref();
337 texture = NULL;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000338 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000339 } else {
340 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000341 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000342 // not. Either implement filtered stretch blit on CPU or just create
343 // one when FBO case fails.
344
bsalomonf2703d82014-10-28 14:33:06 -0700345 rtDesc.fFlags = kNone_GrSurfaceFlags;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000346 // no longer need to clamp at min RT size.
347 rtDesc.fWidth = GrNextPow2(desc.fWidth);
348 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000349
350 // We shouldn't be resizing a compressed texture.
351 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
352
robertphillips@google.com8b169312013-10-15 17:47:36 +0000353 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700354 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000355 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
356 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000357
358 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
359
bsalomon5236cf42015-01-14 10:42:08 -0800360 texture = fGpu->createTexture(rtDesc, true, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700361 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000362 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000363
364 return texture;
365}
366
bsalomon24db3b12015-01-23 04:24:04 -0800367static GrContentKey::Domain ResizeDomain() {
368 static const GrContentKey::Domain kDomain = GrContentKey::GenerateDomain();
369 return kDomain;
370}
371
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000372GrTexture* GrContext::createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700373 const GrSurfaceDesc& desc,
bsalomon24db3b12015-01-23 04:24:04 -0800374 const GrContentKey& origKey,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000375 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000376 size_t rowBytes,
bsalomon24db3b12015-01-23 04:24:04 -0800377 GrContentKey* outKey) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000378 GrTexture* texture;
bsalomon24db3b12015-01-23 04:24:04 -0800379 uint32_t flags = get_texture_flags(fGpu, params, desc);
380 SkTCopyOnFirstWrite<GrContentKey> key(origKey);
381 if (flags) {
382 // We don't have a code path to resize compressed textures.
383 if (GrPixelConfigIsCompressed(desc.fConfig)) {
384 return NULL;
385 }
386 texture = this->createResizedTexture(desc, origKey, srcData, rowBytes,
387 SkToBool(flags & kBilerp_ResizeFlag));
krajcevski9c0e6292014-06-02 07:38:14 -0700388
bsalomon24db3b12015-01-23 04:24:04 -0800389 GrContentKey::Builder builder(key.writable(), origKey, ResizeDomain(), 1);
390 builder[0] = flags;
391
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392 } else {
bsalomon5236cf42015-01-14 10:42:08 -0800393 texture = fGpu->createTexture(desc, true, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000394 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000395
bsalomon49f085d2014-09-05 13:34:00 -0700396 if (texture) {
bsalomon24db3b12015-01-23 04:24:04 -0800397 if (texture->cacheAccess().setContentKey(*key)) {
398 if (outKey) {
399 *outKey = *key;
bsalomon71cb0c22014-11-14 12:10:14 -0800400 }
401 } else {
402 texture->unref();
403 texture = NULL;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000404 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000405 }
406
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000407 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408}
409
bsalomon24db3b12015-01-23 04:24:04 -0800410GrTexture* GrContext::findAndRefTexture(const GrSurfaceDesc& desc,
411 const GrContentKey& origKey,
412 const GrTextureParams* params) {
413 uint32_t flags = get_texture_flags(fGpu, params, desc);
414 SkTCopyOnFirstWrite<GrContentKey> key(origKey);
415 if (flags) {
416 GrContentKey::Builder builder(key.writable(), origKey, ResizeDomain(), 1);
417 builder[0] = flags;
418 }
419
420 GrGpuResource* resource = this->findAndRefCachedResource(*key);
421 if (resource) {
422 SkASSERT(static_cast<GrSurface*>(resource)->asTexture());
423 return static_cast<GrSurface*>(resource)->asTexture();
424 }
425 return NULL;
426}
427
428bool GrContext::isTextureInCache(const GrSurfaceDesc& desc,
429 const GrContentKey& origKey,
430 const GrTextureParams* params) const {
431 uint32_t flags = get_texture_flags(fGpu, params, desc);
432 SkTCopyOnFirstWrite<GrContentKey> key(origKey);
433 if (flags) {
434 GrContentKey::Builder builder(key.writable(), origKey, ResizeDomain(), 1);
435 builder[0] = flags;
436 }
437
438 return fResourceCache2->hasContentKey(*key);
439}
440
bsalomonf2703d82014-10-28 14:33:06 -0700441GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700442 bool calledDuringFlush) {
bsalomonbcf0a522014-10-08 08:40:09 -0700443 // kNoStencil has no meaning if kRT isn't set.
bsalomonf2703d82014-10-28 14:33:06 -0700444 SkASSERT((inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
445 !(inDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000446
bsalomonbcf0a522014-10-08 08:40:09 -0700447 // Make sure caller has checked for renderability if kRT is set.
bsalomonf2703d82014-10-28 14:33:06 -0700448 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
bsalomonbcf0a522014-10-08 08:40:09 -0700449 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000450
bsalomonf2703d82014-10-28 14:33:06 -0700451 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000452
bsalomonf2703d82014-10-28 14:33:06 -0700453 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
454 GrSurfaceFlags origFlags = desc->fFlags;
bsalomonbcf0a522014-10-08 08:40:09 -0700455 if (kApprox_ScratchTexMatch == match) {
456 // bin by pow2 with a reasonable min
457 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700458 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700459 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
460 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000461 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000462
bsalomonbcf0a522014-10-08 08:40:09 -0700463 do {
bsalomon7775c852014-12-30 12:50:52 -0800464 GrScratchKey key;
465 GrTexturePriv::ComputeScratchKey(*desc, &key);
bsalomon000f8292014-10-15 19:04:14 -0700466 uint32_t scratchFlags = 0;
467 if (calledDuringFlush) {
468 scratchFlags = GrResourceCache2::kRequireNoPendingIO_ScratchFlag;
bsalomonf2703d82014-10-28 14:33:06 -0700469 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon000f8292014-10-15 19:04:14 -0700470 // If it is not a render target then it will most likely be populated by
471 // writePixels() which will trigger a flush if the texture has pending IO.
472 scratchFlags = GrResourceCache2::kPreferNoPendingIO_ScratchFlag;
473 }
474 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
bsalomonbcf0a522014-10-08 08:40:09 -0700475 if (resource) {
bsalomon37dd3312014-11-03 08:47:23 -0800476 return static_cast<GrSurface*>(resource)->asTexture();
bsalomonbcf0a522014-10-08 08:40:09 -0700477 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000478
bsalomonbcf0a522014-10-08 08:40:09 -0700479 if (kExact_ScratchTexMatch == match) {
480 break;
481 }
482 // We had a cache miss and we are in approx mode, relax the fit of the flags.
483
484 // We no longer try to reuse textures that were previously used as render targets in
485 // situations where no RT is needed; doing otherwise can confuse the video driver and
486 // cause significant performance problems in some cases.
bsalomonf2703d82014-10-28 14:33:06 -0700487 if (desc->fFlags & kNoStencil_GrSurfaceFlag) {
488 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrSurfaceFlag;
bsalomonbcf0a522014-10-08 08:40:09 -0700489 } else {
490 break;
491 }
492
493 } while (true);
494
495 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000496 }
497
bsalomon5236cf42015-01-14 10:42:08 -0800498 GrTexture* texture = fGpu->createTexture(*desc, true, NULL, 0);
bsalomon7775c852014-12-30 12:50:52 -0800499#ifdef SK_DEBUG
500 GrScratchKey key;
501 GrTexturePriv::ComputeScratchKey(*desc, &key);
502 SkASSERT(NULL == texture || texture->cacheAccess().getScratchKey() == key);
503#endif
bsalomonbcf0a522014-10-08 08:40:09 -0700504 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400505}
506
bsalomon71cb0c22014-11-14 12:10:14 -0800507void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800508 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800509
bsalomon66a450f2014-11-13 13:19:10 -0800510 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800511
512 // Flush the InOrderDrawBuffer to possibly free up some textures
513 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000514}
515
bsalomon5236cf42015-01-14 10:42:08 -0800516GrTexture* GrContext::createUncachedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000517 void* srcData,
518 size_t rowBytes) {
bsalomon5236cf42015-01-14 10:42:08 -0800519 return fGpu->createTexture(desc, false, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000522void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800523 if (maxTextures) {
524 *maxTextures = fResourceCache2->getMaxResourceCount();
525 }
526 if (maxTextureBytes) {
527 *maxTextureBytes = fResourceCache2->getMaxResourceBytes();
528 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529}
530
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000531void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800532 fResourceCache2->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
bsalomon@google.com91958362011-06-13 17:58:13 +0000535int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000536 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000537}
538
539int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000540 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541}
542
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000543int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000544 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000545}
546
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547///////////////////////////////////////////////////////////////////////////////
548
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000549GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
550 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000551}
552
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000553GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
554 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000555}
556
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000557///////////////////////////////////////////////////////////////////////////////
558
bsalomon@google.comb8670992012-07-25 21:27:09 +0000559bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000560 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000561 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000562 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563 return false;
564 }
565
tfarinaf9dae782014-06-06 06:35:28 -0700566 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567
568 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700569 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000570 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571 return false;
572 }
573 }
574 return true;
575}
576
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578////////////////////////////////////////////////////////////////////////////////
579
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000580void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000581 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000582 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700583 GrRenderTarget* renderTarget) {
584 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800585 SkASSERT(renderTarget);
586
bsalomonf21dab92014-11-13 13:33:28 -0800587 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700588 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt8059eb92014-12-29 15:10:07 -0800589 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700590 if (NULL == target) {
591 return;
592 }
593 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594}
595
joshualitt5531d512014-12-17 15:50:11 -0800596void GrContext::drawPaint(const GrPaint& origPaint, const SkMatrix& viewMatrix) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597 // set rect to be big enough to fill the space, but not super-huge, so we
598 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000599 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000600 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000601 SkIntToScalar(getRenderTarget()->width()),
602 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000603 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800604
605 // by definition this fills the entire clip, no need for AA
606 if (paint->isAntiAlias()) {
607 paint.writable()->setAntiAlias(false);
608 }
609
610 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000611
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000612 // We attempt to map r by the inverse matrix and draw that. mapRect will
613 // map the four corners and bound them with a new rect. This will not
614 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800615 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800616 SkMatrix inverse;
617 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700618 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 return;
620 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621 inverse.mapRect(&r);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800622 this->drawRect(*paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000623 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800624 SkMatrix localMatrix;
625 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700626 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000627 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000628 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800629
630 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800631 GrPipelineBuilder pipelineBuilder;
632 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800633 if (NULL == target) {
634 return;
635 }
636
637 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
egdaniel8dd688b2015-01-22 10:16:09 -0800638 target->drawRect(&pipelineBuilder, paint->getColor(), SkMatrix::I(), r, NULL, &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000639 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000640}
641
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000642#ifdef SK_DEVELOPER
643void GrContext::dumpFontCache() const {
644 fFontCache->dump();
645}
646#endif
647
bsalomon@google.com205d4602011-04-25 12:43:45 +0000648////////////////////////////////////////////////////////////////////////////////
649
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650/* create a triangle strip that strokes the specified triangle. There are 8
651 unique vertices, but we repreat the last 2 to close up. Alternatively we
652 could use an indices array, and then only send 8 verts, but not sure that
653 would be faster.
654 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000655static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000656 SkScalar width) {
657 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000659
660 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
661 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
662 verts[2].set(rect.fRight - rad, rect.fTop + rad);
663 verts[3].set(rect.fRight + rad, rect.fTop - rad);
664 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
665 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
666 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
667 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
668 verts[8] = verts[0];
669 verts[9] = verts[1];
670}
671
bsalomonc30aaa02014-08-13 07:15:29 -0700672static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700673 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
674 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700675}
676
bsalomon@google.com205d4602011-04-25 12:43:45 +0000677static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800678 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800679 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000680 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000681 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800682 const SkMatrix& combinedMatrix,
683 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800684 if (!pipelineBuilder->canTweakAlphaForCoverage() &&
685 !pipelineBuilder->canUseFracCoveragePrimProc(color, *target->caps())) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000686#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700687 //SkDebugf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000688#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700689 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000690 }
joshualitt9853cce2014-11-17 14:22:48 -0800691
egdaniel8dd688b2015-01-22 10:16:09 -0800692 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693 return false;
694 }
695
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000696#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000697 if (strokeWidth >= 0) {
698#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000699 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000700 return false;
701 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000703#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000704 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000705 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000706 return false;
707 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000709#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000710
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000711 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800712 if (!combinedMatrix.rectStaysRect()) {
713 return true;
714 }
715
bsalomonc30aaa02014-08-13 07:15:29 -0700716 if (strokeWidth < 0) {
717 return !is_irect(*devBoundRect);
718 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000719
bsalomon9c0822a2014-08-11 11:07:48 -0700720 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721}
722
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000723static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
724 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
725 point.fY >= rect.fTop && point.fY <= rect.fBottom;
726}
727
bsalomon@google.com27847de2011-02-22 20:59:41 +0000728void GrContext::drawRect(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800729 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000730 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700731 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700732 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700733 SkPath path;
734 path.addRect(rect);
joshualitt5531d512014-12-17 15:50:11 -0800735 this->drawPath(paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700736 return;
737 }
738
bsalomonf21dab92014-11-13 13:33:28 -0800739 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800740 GrPipelineBuilder pipelineBuilder;
741 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700742 if (NULL == target) {
743 return;
744 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000746 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700747 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000748
749 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
750 // cases where the RT is fully inside a stroke.
751 if (width < 0) {
752 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800753 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000754 SkRect clipSpaceRTRect = rtRect;
755 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700756 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000757 checkClip = true;
758 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
759 SkIntToScalar(this->getClip()->fOrigin.fY));
760 }
761 // Does the clip contain the entire RT?
762 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
763 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800764 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000765 return;
766 }
767 // Does the rect bound the RT?
768 SkPoint srcSpaceRTQuad[4];
769 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
770 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
771 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
772 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
773 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
774 // Will it blend?
775 GrColor clearColor;
776 if (paint.isOpaqueAndConstantColor(&clearColor)) {
bsalomon89c62982014-11-03 12:08:42 -0800777 target->clear(NULL, clearColor, true, fRenderTarget);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000778 return;
779 }
780 }
781 }
782 }
783
joshualitt2e3b3e32014-12-09 13:31:14 -0800784 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000785 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800786 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
787 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800788 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700789
bsalomon@google.com205d4602011-04-25 12:43:45 +0000790 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000791 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800792 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800793 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800794 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800795 color,
joshualitt8059eb92014-12-29 15:10:07 -0800796 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800797 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800798 devBoundRect,
799 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000800 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000801 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800802 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800803 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800804 color,
805 viewMatrix,
806 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800807 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000808 }
809 return;
810 }
811
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812 if (width >= 0) {
813 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000814 // Hairline could be done by just adding closing vertex to
815 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000816
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817 static const int worstCaseVertCount = 10;
joshualitt8059eb92014-12-29 15:10:07 -0800818 SkAutoTUnref<const GrGeometryProcessor> gp(
819 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
820 color,
821 viewMatrix,
822 SkMatrix::I()));
joshualitt9853cce2014-11-17 14:22:48 -0800823 GrDrawTarget::AutoReleaseGeometry geo(target,
824 worstCaseVertCount,
joshualitt2dd1ae02014-12-03 06:24:10 -0800825 gp->getVertexStride(),
joshualitt9853cce2014-11-17 14:22:48 -0800826 0);
joshualitt2dd1ae02014-12-03 06:24:10 -0800827 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828
829 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700830 SkDebugf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831 return;
832 }
833
834 GrPrimitiveType primType;
835 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000836 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000837
838 if (width > 0) {
839 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000840 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841 setStrokeRectStrip(vertex, rect, width);
842 } else {
843 // hairline
844 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000845 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 vertex[0].set(rect.fLeft, rect.fTop);
847 vertex[1].set(rect.fRight, rect.fTop);
848 vertex[2].set(rect.fRight, rect.fBottom);
849 vertex[3].set(rect.fLeft, rect.fBottom);
850 vertex[4].set(rect.fLeft, rect.fTop);
851 }
852
egdaniel8dd688b2015-01-22 10:16:09 -0800853 target->drawNonIndexed(&pipelineBuilder, gp, primType, 0, vertCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000855 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800856 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000857 }
858}
859
joshualitt16b27892014-12-18 07:47:16 -0800860void GrContext::drawNonAARectToRect(const GrPaint& paint,
861 const SkMatrix& viewMatrix,
862 const SkRect& rectToDraw,
863 const SkRect& localRect,
864 const SkMatrix* localMatrix) {
bsalomonf21dab92014-11-13 13:33:28 -0800865 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800866 GrPipelineBuilder pipelineBuilder;
867 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700868 if (NULL == target) {
869 return;
870 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000871
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000872 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
873
egdaniel8dd688b2015-01-22 10:16:09 -0800874 target->drawRect(&pipelineBuilder, paint.getColor(), viewMatrix, rectToDraw, &localRect,
875 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876}
877
joshualitt56995b52014-12-11 15:44:02 -0800878static const GrGeometryProcessor* set_vertex_attributes(const SkPoint* texCoords,
879 const GrColor* colors,
880 int* colorOffset,
881 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800882 GrColor color,
883 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000884 *texOffset = -1;
885 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800886 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700887 if (texCoords && colors) {
joshualitt5478d422014-11-14 16:00:38 -0800888 *colorOffset = sizeof(SkPoint);
889 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
890 flags |= GrDefaultGeoProcFactory::kColor_GPType |
891 GrDefaultGeoProcFactory::kLocalCoord_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700892 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000893 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800894 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700895 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000896 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800897 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000898 }
joshualitt8059eb92014-12-29 15:10:07 -0800899 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000900}
901
bsalomon@google.com27847de2011-02-22 20:59:41 +0000902void GrContext::drawVertices(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800903 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904 GrPrimitiveType primitiveType,
905 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000906 const SkPoint positions[],
907 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908 const GrColor colors[],
909 const uint16_t indices[],
910 int indexCount) {
bsalomonf21dab92014-11-13 13:33:28 -0800911 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800912 GrPipelineBuilder pipelineBuilder;
bsalomonf21dab92014-11-13 13:33:28 -0800913 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000914
egdaniel8dd688b2015-01-22 10:16:09 -0800915 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700916 if (NULL == target) {
917 return;
918 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000920 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
921
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000922 int colorOffset = -1, texOffset = -1;
joshualitt56995b52014-12-11 15:44:02 -0800923 SkAutoTUnref<const GrGeometryProcessor> gp(
joshualitt8059eb92014-12-29 15:10:07 -0800924 set_vertex_attributes(texCoords, colors, &colorOffset, &texOffset,
925 paint.getColor(), viewMatrix));
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000926
joshualitt56995b52014-12-11 15:44:02 -0800927 size_t vertexStride = gp->getVertexStride();
joshualitt2dd1ae02014-12-03 06:24:10 -0800928 SkASSERT(vertexStride == sizeof(SkPoint) + (SkToBool(texCoords) ? sizeof(SkPoint) : 0)
929 + (SkToBool(colors) ? sizeof(GrColor) : 0));
joshualitt9853cce2014-11-17 14:22:48 -0800930 if (!geo.set(target, vertexCount, vertexStride, indexCount)) {
joshualittd1aa8ff2014-11-04 07:47:55 -0800931 SkDebugf("Failed to get space for vertices!\n");
932 return;
933 }
934 void* curVertex = geo.vertices();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935
joshualittd1aa8ff2014-11-04 07:47:55 -0800936 for (int i = 0; i < vertexCount; ++i) {
937 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000938
joshualittd1aa8ff2014-11-04 07:47:55 -0800939 if (texOffset >= 0) {
940 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000941 }
joshualittd1aa8ff2014-11-04 07:47:55 -0800942 if (colorOffset >= 0) {
943 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
944 }
joshualitt9853cce2014-11-17 14:22:48 -0800945 curVertex = (void*)((intptr_t)curVertex + vertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 }
947
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000948 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000949 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon49f085d2014-09-05 13:34:00 -0700950 if (indices) {
joshualittd1aa8ff2014-11-04 07:47:55 -0800951 uint16_t* curIndex = (uint16_t*)geo.indices();
952 for (int i = 0; i < indexCount; ++i) {
953 curIndex[i] = indices[i];
954 }
egdaniel8dd688b2015-01-22 10:16:09 -0800955 target->drawIndexed(&pipelineBuilder, gp, primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800957 target->drawNonIndexed(&pipelineBuilder, gp, primitiveType, 0, vertexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000958 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000959}
960
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000961///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000962
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000963void GrContext::drawRRect(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800964 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000965 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700966 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000967 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000968 return;
969 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000970
egdanield58a0ba2014-06-11 10:30:05 -0700971 if (strokeInfo.isDashed()) {
972 SkPath path;
973 path.addRRect(rrect);
joshualitt5531d512014-12-17 15:50:11 -0800974 this->drawPath(paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700975 return;
976 }
977
bsalomonf21dab92014-11-13 13:33:28 -0800978 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800979 GrPipelineBuilder pipelineBuilder;
980 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700981 if (NULL == target) {
982 return;
983 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000984
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000985 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
986
egdanield58a0ba2014-06-11 10:30:05 -0700987 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
988
joshualitt2e3b3e32014-12-09 13:31:14 -0800989 GrColor color = paint.getColor();
egdaniel8dd688b2015-01-22 10:16:09 -0800990 if (!fOvalRenderer->drawRRect(target, &pipelineBuilder, color, viewMatrix, paint.isAntiAlias(),
991 rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000992 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000993 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -0800994 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
995 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000996 }
997}
998
999///////////////////////////////////////////////////////////////////////////////
1000
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001001void GrContext::drawDRRect(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001002 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001003 const SkRRect& outer,
1004 const SkRRect& inner) {
1005 if (outer.isEmpty()) {
1006 return;
1007 }
1008
bsalomonf21dab92014-11-13 13:33:28 -08001009 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001010 GrPipelineBuilder pipelineBuilder;
1011 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001012
1013 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1014
joshualitt2e3b3e32014-12-09 13:31:14 -08001015 GrColor color = paint.getColor();
egdaniel8dd688b2015-01-22 10:16:09 -08001016 if (!fOvalRenderer->drawDRRect(target, &pipelineBuilder, color, viewMatrix, paint.isAntiAlias(),
1017 outer, inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001018 SkPath path;
1019 path.addRRect(inner);
1020 path.addRRect(outer);
1021 path.setFillType(SkPath::kEvenOdd_FillType);
1022
egdanield58a0ba2014-06-11 10:30:05 -07001023 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001024 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1025 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001026 }
1027}
1028
1029///////////////////////////////////////////////////////////////////////////////
1030
bsalomon@google.com93c96602012-04-27 13:05:21 +00001031void GrContext::drawOval(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001032 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001033 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001034 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001035 if (oval.isEmpty()) {
1036 return;
1037 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001038
egdanield58a0ba2014-06-11 10:30:05 -07001039 if (strokeInfo.isDashed()) {
1040 SkPath path;
1041 path.addOval(oval);
joshualitt5531d512014-12-17 15:50:11 -08001042 this->drawPath(paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001043 return;
1044 }
1045
bsalomonf21dab92014-11-13 13:33:28 -08001046 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001047 GrPipelineBuilder pipelineBuilder;
1048 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001049 if (NULL == target) {
1050 return;
1051 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001052
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001053 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1054
egdanield58a0ba2014-06-11 10:30:05 -07001055 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1056
joshualitt2e3b3e32014-12-09 13:31:14 -08001057 GrColor color = paint.getColor();
egdaniel8dd688b2015-01-22 10:16:09 -08001058 if (!fOvalRenderer->drawOval(target, &pipelineBuilder, color, viewMatrix, paint.isAntiAlias(),
1059 oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001060 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001061 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001062 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1063 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001064 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001065}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001066
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001067// Can 'path' be drawn as a pair of filled nested rectangles?
1068static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001069 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001070 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001071 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001072 const SkPath& path,
1073 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001074 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001075 SkASSERT(stroke.isFillStyle());
1076
1077 if (path.isInverseFillType()) {
1078 return false;
1079 }
1080
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001081 // TODO: this restriction could be lifted if we were willing to apply
1082 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001083 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001084 return false;
1085 }
1086
egdaniel8dd688b2015-01-22 10:16:09 -08001087 if (!pipelineBuilder->canTweakAlphaForCoverage() &&
1088 !pipelineBuilder->canUseFracCoveragePrimProc(color, *target->caps())) {
bsalomon9c0822a2014-08-11 11:07:48 -07001089 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001090 }
1091
1092 SkPath::Direction dirs[2];
1093 if (!path.isNestedRects(rects, dirs)) {
1094 return false;
1095 }
1096
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001097 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001098 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001099 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001100 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001101
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001102 // Right now, nested rects where the margin is not the same width
1103 // all around do not render correctly
1104 const SkScalar* outer = rects[0].asScalars();
1105 const SkScalar* inner = rects[1].asScalars();
1106
robertphillips183e9852014-10-21 11:25:37 -07001107 bool allEq = true;
1108
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001109 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001110 bool allGoE1 = margin >= SK_Scalar1;
1111
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001112 for (int i = 1; i < 4; ++i) {
1113 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001114 if (temp < SK_Scalar1) {
1115 allGoE1 = false;
1116 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001117 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001118 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001119 }
1120 }
1121
robertphillips183e9852014-10-21 11:25:37 -07001122 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001123}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001124
joshualitt5531d512014-12-17 15:50:11 -08001125void GrContext::drawPath(const GrPaint& paint,
1126 const SkMatrix& viewMatrix,
1127 const SkPath& path,
1128 const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001129
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001130 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001131 if (path.isInverseFillType()) {
joshualitt5531d512014-12-17 15:50:11 -08001132 this->drawPaint(paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001133 }
1134 return;
1135 }
1136
joshualitt2e3b3e32014-12-09 13:31:14 -08001137 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001138 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001139 SkPoint pts[2];
1140 if (path.isLine(pts)) {
bsalomonf21dab92014-11-13 13:33:28 -08001141 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001142 GrPipelineBuilder pipelineBuilder;
1143 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001144 if (NULL == target) {
1145 return;
joshualittd27f73e2014-12-29 07:43:36 -08001146 }
egdaniele61c4112014-06-12 10:24:21 -07001147
egdaniel8dd688b2015-01-22 10:16:09 -08001148 if (GrDashingEffect::DrawDashLine(fGpu, target, &pipelineBuilder, color, viewMatrix,
1149 pts, paint, strokeInfo)) {
joshualittd27f73e2014-12-29 07:43:36 -08001150 return;
egdaniele61c4112014-06-12 10:24:21 -07001151 }
1152 }
1153
1154 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001155 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1156 SkTLazy<SkPath> effectPath;
1157 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1158 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1159 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
joshualitt5531d512014-12-17 15:50:11 -08001160 this->drawPath(paint, viewMatrix, *effectPath.get(), newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001161 return;
1162 }
1163
joshualitt5531d512014-12-17 15:50:11 -08001164 this->drawPath(paint, viewMatrix, path, newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001165 return;
1166 }
1167
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001168 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001169 // Scratch textures can be recycled after they are returned to the texture
1170 // cache. This presents a potential hazard for buffered drawing. However,
1171 // the writePixels that uploads to the scratch will perform a flush so we're
1172 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001173 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001174 GrPipelineBuilder pipelineBuilder;
1175 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001176 if (NULL == target) {
1177 return;
1178 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001179
egdaniel93a37bc2014-07-21 13:47:57 -07001180 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001181
egdanield58a0ba2014-06-11 10:30:05 -07001182 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1183
egdaniel8dd688b2015-01-22 10:16:09 -08001184 bool useCoverageAA = paint.isAntiAlias() &&
1185 !pipelineBuilder.getRenderTarget()->isMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001186
egdanield58a0ba2014-06-11 10:30:05 -07001187 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001188 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001189 SkRect rects[2];
1190
egdaniel8dd688b2015-01-22 10:16:09 -08001191 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec, rects)) {
1192 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix,rects);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001193 return;
1194 }
1195 }
1196
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001197 SkRect ovalRect;
1198 bool isOval = path.isOval(&ovalRect);
1199
joshualitt8059eb92014-12-29 15:10:07 -08001200 if (!isOval || path.isInverseFillType() ||
egdaniel8dd688b2015-01-22 10:16:09 -08001201 !fOvalRenderer->drawOval(target, &pipelineBuilder, color, viewMatrix, paint.isAntiAlias(),
joshualitt8059eb92014-12-29 15:10:07 -08001202 ovalRect, strokeRec)) {
egdaniel8dd688b2015-01-22 10:16:09 -08001203 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1204 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001205 }
1206}
1207
joshualitt9853cce2014-11-17 14:22:48 -08001208void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001209 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001210 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001211 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001212 bool useAA,
1213 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001214 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001215 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001216
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001217 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1218
1219
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001220 // An Assumption here is that path renderer would use some form of tweaking
1221 // the src color (either the input alpha or in the frag shader) to implement
1222 // aa. If we have some future driver-mojo path AA that can do the right
1223 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001224 bool useCoverageAA = useAA &&
egdaniel8dd688b2015-01-22 10:16:09 -08001225 !pipelineBuilder->getRenderTarget()->isMultisampled() &&
1226 pipelineBuilder->canUseFracCoveragePrimProc(color, *target->caps());
bsalomon@google.com289533a2011-10-27 12:34:25 +00001227
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001228
1229 GrPathRendererChain::DrawType type =
1230 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001231 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001232
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001233 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001234 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001235 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001236
1237 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001238 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
1239 *stroke, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001240
robertphillips@google.come79f3202014-02-11 16:30:21 +00001241 if (NULL == pr) {
joshualitt5531d512014-12-17 15:50:11 -08001242 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001243 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001244 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1245 pathPtr = tmpPath.get();
1246 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001247 if (pathPtr->isEmpty()) {
1248 return;
1249 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001250 }
1251 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001252
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001253 // This time, allow SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001254 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *stroke, true,
1255 type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001256 }
1257
robertphillips@google.come79f3202014-02-11 16:30:21 +00001258 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001259#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001260 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001261#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001262 return;
1263 }
1264
egdaniel8dd688b2015-01-22 10:16:09 -08001265 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *stroke, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001266}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001267
bsalomon@google.com27847de2011-02-22 20:59:41 +00001268////////////////////////////////////////////////////////////////////////////////
1269
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001270void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001271 if (NULL == fDrawBuffer) {
1272 return;
1273 }
1274
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001275 if (kDiscard_FlushBit & flagsBitfield) {
1276 fDrawBuffer->reset();
1277 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001278 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001279 }
bsalomonf21dab92014-11-13 13:33:28 -08001280 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001281}
1282
bsalomon81beccc2014-10-13 12:32:55 -07001283bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1284 const void* inPixels, size_t outRowBytes, void* outPixels) {
1285 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001286 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001287 return false;
1288 }
bsalomon81beccc2014-10-13 12:32:55 -07001289 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1290 srcPI.fPixels = inPixels;
1291 srcPI.fRowBytes = inRowBytes;
1292
1293 SkDstPixelInfo dstPI;
1294 dstPI.fColorType = srcPI.fColorType;
1295 dstPI.fAlphaType = kPremul_SkAlphaType;
1296 dstPI.fPixels = outPixels;
1297 dstPI.fRowBytes = outRowBytes;
1298
1299 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001300}
1301
bsalomon81beccc2014-10-13 12:32:55 -07001302bool GrContext::writeSurfacePixels(GrSurface* surface,
1303 int left, int top, int width, int height,
1304 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1305 uint32_t pixelOpsFlags) {
1306
1307 {
1308 GrTexture* texture = NULL;
1309 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1310 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1311
1312 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1313 surface->surfacePriv().hasPendingIO()) {
1314 this->flush();
1315 }
1316 return fGpu->writeTexturePixels(texture, left, top, width, height,
1317 srcConfig, buffer, rowBytes);
1318 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1319 // upload is already flushed.
1320 }
1321 }
1322
1323 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1324 GrRenderTarget* renderTarget = surface->asRenderTarget();
1325 if (NULL == renderTarget) {
1326 return false;
1327 }
1328
1329 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1330 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1331 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1332 // when drawing the scratch to the dst using a conversion effect.
1333 bool swapRAndB = false;
1334 GrPixelConfig writeConfig = srcConfig;
1335 if (GrPixelConfigSwapRAndB(srcConfig) ==
1336 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1337 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1338 swapRAndB = true;
1339 }
1340
bsalomonf2703d82014-10-28 14:33:06 -07001341 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001342 desc.fWidth = width;
1343 desc.fHeight = height;
1344 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001345 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1346 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001347 return false;
1348 }
1349
1350 SkAutoTUnref<const GrFragmentProcessor> fp;
1351 SkMatrix textureMatrix;
1352 textureMatrix.setIDiv(texture->width(), texture->height());
1353
1354 // allocate a tmp buffer and sw convert the pixels to premul
1355 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1356
1357 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1358 if (!GrPixelConfigIs8888(srcConfig)) {
1359 return false;
1360 }
1361 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1362 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1363 if (NULL == fp) {
1364 size_t tmpRowBytes = 4 * width;
1365 tmpPixels.reset(width * height);
1366 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1367 tmpPixels.get())) {
1368 return false;
1369 }
1370 rowBytes = tmpRowBytes;
1371 buffer = tmpPixels.get();
1372 }
1373 }
1374 if (NULL == fp) {
1375 fp.reset(GrConfigConversionEffect::Create(texture,
1376 swapRAndB,
1377 GrConfigConversionEffect::kNone_PMConversion,
1378 textureMatrix));
1379 }
1380
1381 // Even if the client told us not to flush, we still flush here. The client may have known that
1382 // writes to the original surface caused no data hazards, but they can't know that the scratch
1383 // we just got is safe.
1384 if (texture->surfacePriv().hasPendingIO()) {
1385 this->flush();
1386 }
1387 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1388 writeConfig, buffer, rowBytes)) {
1389 return false;
1390 }
1391
1392 SkMatrix matrix;
1393 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1394
1395 // This function can be called in the midst of drawing another object (e.g., when uploading a
1396 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1397 // drawing a rect to the render target.
1398 // The bracket ensures we pop the stack if we wind up flushing below.
1399 {
joshualitt8059eb92014-12-29 15:10:07 -08001400 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL, NULL);
joshualitt9853cce2014-11-17 14:22:48 -08001401 GrDrawTarget::AutoGeometryPush agp(drawTarget);
1402
egdaniel8dd688b2015-01-22 10:16:09 -08001403 GrPipelineBuilder pipelineBuilder;
1404 pipelineBuilder.addColorProcessor(fp);
1405 pipelineBuilder.setRenderTarget(renderTarget);
1406 drawTarget->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001407 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001408 }
1409
1410 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1411 this->flushSurfaceWrites(surface);
1412 }
1413
1414 return true;
1415}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001416
reed@google.com7111d462014-03-25 16:20:24 +00001417// toggles between RGBA and BGRA
1418static SkColorType toggle_colortype32(SkColorType ct) {
1419 if (kRGBA_8888_SkColorType == ct) {
1420 return kBGRA_8888_SkColorType;
1421 } else {
1422 SkASSERT(kBGRA_8888_SkColorType == ct);
1423 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001424 }
1425}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001426
bsalomon@google.com0342a852012-08-20 19:22:38 +00001427bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1428 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001429 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001430 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001431 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001432 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001433
bsalomonafbf2d62014-09-30 12:18:44 -07001434 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001435 this->flush();
1436 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001437
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001438 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001439
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001440 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1441 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1442 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001443 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001444 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001445 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1446 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1447 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1448 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1449 // dstConfig.
1450 GrPixelConfig readConfig = dstConfig;
1451 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001452 if (GrPixelConfigSwapRAndB(dstConfig) ==
1453 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001454 readConfig = GrPixelConfigSwapRAndB(readConfig);
1455 swapRAndB = true;
1456 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001457
bsalomon@google.com0342a852012-08-20 19:22:38 +00001458 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001459
bsalomon@google.com9c680582013-02-06 18:17:50 +00001460 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001461 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001462 return false;
1463 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001464
bsalomon191bcc02014-11-14 11:31:13 -08001465 SkAutoTUnref<GrTexture> tempTexture;
1466
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001467 // If the src is a texture and we would have to do conversions after read pixels, we instead
1468 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1469 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1470 // on the read back pixels.
1471 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001472 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001473 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001474 GrSurfaceDesc desc;
1475 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001476 desc.fWidth = width;
1477 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001478 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001479 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001480
bsalomon@google.com9c680582013-02-06 18:17:50 +00001481 // 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 +00001482 // match the passed rect. However, if we see many different size rectangles we will trash
1483 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1484 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001485 ScratchTexMatch match = kApprox_ScratchTexMatch;
1486 if (0 == left &&
1487 0 == top &&
1488 target->width() == width &&
1489 target->height() == height &&
1490 fGpu->fullReadPixelsIsFasterThanPartial()) {
1491 match = kExact_ScratchTexMatch;
1492 }
bsalomon191bcc02014-11-14 11:31:13 -08001493 tempTexture.reset(this->refScratchTexture(desc, match));
1494 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001495 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001496 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001497 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001498 textureMatrix.postIDiv(src->width(), src->height());
1499
joshualittb0a8a372014-09-23 09:50:21 -07001500 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001501 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001502 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1503 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001504 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001505 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001506 }
1507 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1508 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001509 if (fp || flipY || swapRAndB) {
1510 if (!fp) {
1511 fp.reset(GrConfigConversionEffect::Create(
1512 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1513 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514 }
1515 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001516
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001517 // We protect the existing geometry here since it may not be
1518 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1519 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001520 {
joshualitt9853cce2014-11-17 14:22:48 -08001521 GrDrawTarget::AutoGeometryPush agp(fDrawBuffer);
egdaniel8dd688b2015-01-22 10:16:09 -08001522 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001523 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001524 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001525
egdaniel8dd688b2015-01-22 10:16:09 -08001526 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001527 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
egdaniel8dd688b2015-01-22 10:16:09 -08001528 fDrawBuffer->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, SkMatrix::I(),
1529 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001530 // we want to read back from the scratch's origin
1531 left = 0;
1532 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001533 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001534 }
1535 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001536 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001537 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001538 }
joshualitt5c55fef2014-10-31 14:04:35 -07001539
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001540 if (!fGpu->readPixels(target,
1541 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001542 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001543 return false;
1544 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001545 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001546 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001547 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001548 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001549 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001550 }
reed@google.com7111d462014-03-25 16:20:24 +00001551 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1552 dstPI.fPixels = buffer;
1553 dstPI.fRowBytes = rowBytes;
1554
1555 SkSrcPixelInfo srcPI;
1556 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1557 srcPI.fAlphaType = kPremul_SkAlphaType;
1558 srcPI.fPixels = buffer;
1559 srcPI.fRowBytes = rowBytes;
1560
1561 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001562 }
1563 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001564}
1565
bsalomon87a94eb2014-11-03 14:28:32 -08001566void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
1567 SkASSERT(surface);
1568 ASSERT_OWNED_RESOURCE(surface);
1569 if (surface->surfacePriv().hasPendingIO()) {
1570 this->flush();
1571 }
1572 GrRenderTarget* rt = surface->asRenderTarget();
1573 if (fGpu && rt) {
1574 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001575 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001576}
1577
bsalomon41ebbdd2014-08-04 08:31:39 -07001578void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1579 SkASSERT(renderTarget);
1580 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001581 AutoCheckFlush acf(this);
joshualitt8059eb92014-12-29 15:10:07 -08001582 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001583 if (NULL == target) {
1584 return;
1585 }
1586 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001587}
1588
bsalomonf80bfed2014-10-07 05:56:02 -07001589void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1590 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001591 if (NULL == src || NULL == dst) {
1592 return;
1593 }
bsalomone3d4bf22014-09-23 09:15:03 -07001594 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001595 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001596
bsalomonf80bfed2014-10-07 05:56:02 -07001597 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1598 // here.
junov96c118e2014-09-26 13:09:47 -07001599
joshualitt8059eb92014-12-29 15:10:07 -08001600 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL);
junov96c118e2014-09-26 13:09:47 -07001601 if (NULL == target) {
1602 return;
1603 }
junov96c118e2014-09-26 13:09:47 -07001604 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001605
1606 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1607 this->flush();
1608 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001609}
1610
bsalomonf80bfed2014-10-07 05:56:02 -07001611void GrContext::flushSurfaceWrites(GrSurface* surface) {
1612 if (surface->surfacePriv().hasPendingWrite()) {
1613 this->flush();
1614 }
1615}
1616
egdaniel8dd688b2015-01-22 10:16:09 -08001617GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -08001618 const GrPaint* paint,
1619 const AutoCheckFlush* acf) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001620 if (NULL == fGpu) {
1621 return NULL;
1622 }
1623
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001624 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
egdaniel8dd688b2015-01-22 10:16:09 -08001625 if (pipelineBuilder) {
joshualitt2fdeda02015-01-22 07:11:44 -08001626 SkASSERT(paint && acf);
egdaniel8dd688b2015-01-22 10:16:09 -08001627 pipelineBuilder->setFromPaint(*paint, fRenderTarget.get());
1628 pipelineBuilder->setState(GrPipelineBuilder::kClip_StateBit,
1629 fClip && !fClip->fClipStack->isWideOpen());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001630 }
joshualitt5c55fef2014-10-31 14:04:35 -07001631 fDrawBuffer->setClip(fClip);
joshualitt5c55fef2014-10-31 14:04:35 -07001632 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001633}
1634
robertphillips@google.com72176b22012-05-23 13:19:12 +00001635/*
1636 * This method finds a path renderer that can draw the specified path on
1637 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001638 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001639 * can be individually allowed/disallowed via the "allowSW" boolean.
1640 */
joshualitt9853cce2014-11-17 14:22:48 -08001641GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001642 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001643 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001644 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001645 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001646 bool allowSW,
1647 GrPathRendererChain::DrawType drawType,
1648 GrPathRendererChain::StencilSupport* stencilSupport) {
1649
bsalomon@google.com30085192011-08-19 15:42:31 +00001650 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001651 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001652 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001653
joshualitt9853cce2014-11-17 14:22:48 -08001654 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001655 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001656 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001657 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001658 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001659 drawType,
1660 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001661
1662 if (NULL == pr && allowSW) {
1663 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001664 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001665 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001666 pr = fSoftwarePathRenderer;
1667 }
1668
1669 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001670}
1671
bsalomon@google.com27847de2011-02-22 20:59:41 +00001672////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001673bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1674 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001675}
1676
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001677int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1678 SkScalar dpi) const {
1679 if (!this->isConfigRenderable(config, true)) {
1680 return 0;
1681 }
1682 int chosenSampleCount = 0;
1683 if (fGpu->caps()->pathRenderingSupport()) {
1684 if (dpi >= 250.0f) {
1685 chosenSampleCount = 4;
1686 } else {
1687 chosenSampleCount = 16;
1688 }
1689 }
1690 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1691 chosenSampleCount : 0;
1692}
1693
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001694void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001695 SkASSERT(NULL == fDrawBuffer);
1696 SkASSERT(NULL == fDrawBufferVBAllocPool);
1697 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001698
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001699 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001700 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001701 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001702 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001703 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001704 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001705 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001706 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001708 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001709 fDrawBufferVBAllocPool,
1710 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001713GrDrawTarget* GrContext::getTextTarget() {
joshualitt8059eb92014-12-29 15:10:07 -08001714 return this->prepareToDraw(NULL, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001715}
1716
1717const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1718 return fGpu->getQuadIndexBuffer();
1719}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001720
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001721namespace {
1722void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1723 GrConfigConversionEffect::PMConversion pmToUPM;
1724 GrConfigConversionEffect::PMConversion upmToPM;
1725 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1726 *pmToUPMValue = pmToUPM;
1727 *upmToPMValue = upmToPM;
1728}
1729}
1730
joshualittb0a8a372014-09-23 09:50:21 -07001731const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1732 bool swapRAndB,
1733 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001734 if (!fDidTestPMConversions) {
1735 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001736 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001737 }
1738 GrConfigConversionEffect::PMConversion pmToUPM =
1739 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1740 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001741 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001742 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001743 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001744 }
1745}
1746
joshualittb0a8a372014-09-23 09:50:21 -07001747const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1748 bool swapRAndB,
1749 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001750 if (!fDidTestPMConversions) {
1751 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001752 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001753 }
1754 GrConfigConversionEffect::PMConversion upmToPM =
1755 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1756 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001757 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001758 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001759 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001760 }
1761}
1762
bsalomon24db3b12015-01-23 04:24:04 -08001763void GrContext::addResourceToCache(const GrContentKey& key, GrGpuResource* resource) {
1764 resource->cacheAccess().setContentKey(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001765}
1766
bsalomon24db3b12015-01-23 04:24:04 -08001767GrGpuResource* GrContext::findAndRefCachedResource(const GrContentKey& key) {
1768 return fResourceCache2->findAndRefContentResource(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001769}
1770
egdanielbbcb38d2014-06-19 10:19:29 -07001771void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1772 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001773 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001774 fDrawBuffer->addGpuTraceMarker(marker);
1775 }
1776}
1777
1778void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1779 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001780 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001781 fDrawBuffer->removeGpuTraceMarker(marker);
1782 }
1783}
1784
bsalomon@google.comc4364992011-11-07 15:54:49 +00001785///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001786#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001787void GrContext::printCacheStats() const {
bsalomon71cb0c22014-11-14 12:10:14 -08001788 fResourceCache2->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001789}
1790#endif
robertphillips754f4e92014-09-18 13:52:08 -07001791
1792#if GR_GPU_STATS
1793const GrContext::GPUStats* GrContext::gpuStats() const {
1794 return fGpu->gpuStats();
1795}
1796#endif
1797