blob: 13de4fec26bce2a2bf5f53f62caed71a97feb103 [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
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000047#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000048 // change this to a 1 to see notifications when partial coverage fails
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#else
51 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
52#endif
53
bsalomon@google.com60361492012-03-15 17:47:06 +000054static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
56
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000057static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
58static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000059
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000060#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000061
bsalomonf21dab92014-11-13 13:33:28 -080062class GrContext::AutoCheckFlush {
63public:
64 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
65
66 ~AutoCheckFlush() {
67 if (fContext->fFlushToReduceCacheSize) {
68 fContext->flush();
69 }
70 }
71
72private:
73 GrContext* fContext;
74};
75
krajcevski9c6d4d72014-08-12 07:26:25 -070076GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
77 const Options* opts) {
78 GrContext* context;
79 if (NULL == opts) {
80 context = SkNEW_ARGS(GrContext, (Options()));
81 } else {
82 context = SkNEW_ARGS(GrContext, (*opts));
83 }
84
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000085 if (context->init(backend, backendContext)) {
86 return context;
87 } else {
88 context->unref();
89 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000090 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000091}
92
krajcevski9c6d4d72014-08-12 07:26:25 -070093GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000094 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000095 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000096 fPathRendererChain = NULL;
97 fSoftwarePathRenderer = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -070098 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000099 fFontCache = NULL;
100 fDrawBuffer = NULL;
101 fDrawBufferVBAllocPool = NULL;
102 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800103 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000104 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000105 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000106 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000107}
108
109bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000110 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000111
112 fGpu = GrGpu::Create(backend, backendContext, this);
113 if (NULL == fGpu) {
114 return false;
115 }
bsalomon33435572014-11-05 14:47:41 -0800116 this->initCommon();
117 return true;
118}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000119
bsalomon33435572014-11-05 14:47:41 -0800120void GrContext::initCommon() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700121 fResourceCache2 = SkNEW(GrResourceCache2);
bsalomon71cb0c22014-11-14 12:10:14 -0800122 fResourceCache2->setOverBudgetCallback(OverBudgetCB, this);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000123
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000124 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
125
robertphillips4ec84da2014-06-24 13:10:43 -0700126 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000127
joshualittb44293e2014-10-28 08:12:18 -0700128 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
joshualitt5531d512014-12-17 15:50:11 -0800129 fOvalRenderer = SkNEW_ARGS(GrOvalRenderer, (fGpu));
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000130
131 fDidTestPMConversions = false;
132
133 this->setupDrawBuffer();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000134}
135
bsalomon@google.com27847de2011-02-22 20:59:41 +0000136GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000137 if (NULL == fGpu) {
138 return;
139 }
140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000142
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000143 for (int i = 0; i < fCleanUpData.count(); ++i) {
144 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
145 }
146
bsalomon33435572014-11-05 14:47:41 -0800147 SkDELETE(fResourceCache2);
bsalomon33435572014-11-05 14:47:41 -0800148 SkDELETE(fFontCache);
149 SkDELETE(fDrawBuffer);
150 SkDELETE(fDrawBufferVBAllocPool);
151 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000152
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000153 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000154 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155
bsalomon@google.com205d4602011-04-25 12:43:45 +0000156 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000157 SkSafeUnref(fPathRendererChain);
158 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
bsalomon2354f842014-07-28 13:48:36 -0700161void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000162 // abandon first to so destructors
163 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700164 fResourceCache2->abandonAll();
165
robertphillipse3371302014-09-17 06:01:06 -0700166 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000167
bsalomon@google.com30085192011-08-19 15:42:31 +0000168 // a path renderer may be holding onto resources that
169 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000170 SkSafeSetNull(fPathRendererChain);
171 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000172
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000173 delete fDrawBuffer;
174 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000175
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000176 delete fDrawBufferVBAllocPool;
177 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000178
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179 delete fDrawBufferIBAllocPool;
180 fDrawBufferIBAllocPool = NULL;
181
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000182 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000183 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000184
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000185 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000186 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000187}
188
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000189void GrContext::resetContext(uint32_t state) {
190 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000191}
192
193void GrContext::freeGpuResources() {
194 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000195
bsalomon49f085d2014-09-05 13:34:00 -0700196 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700197 fDrawBuffer->purgeResources();
198 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000199
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000200 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000201 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000202
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000203 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000204 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000205 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000206 SkSafeSetNull(fPathRendererChain);
207 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000208}
209
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000210void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800211 if (resourceCount) {
bsalomondace19e2014-11-17 07:34:06 -0800212 *resourceCount = fResourceCache2->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800213 }
214 if (resourceBytes) {
bsalomondace19e2014-11-17 07:34:06 -0800215 *resourceBytes = fResourceCache2->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800216 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000217}
218
kkinnunenc6cb56f2014-06-24 00:12:27 -0700219GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
220 const SkDeviceProperties&
221 leakyProperties,
222 bool enableDistanceFieldFonts) {
jvanverth8c27a182014-10-14 08:45:50 -0700223 if (fGpu->caps()->pathRenderingSupport() && renderTarget->getStencilBuffer() &&
224 renderTarget->isMultisampled()) {
225 return GrStencilAndCoverTextContext::Create(this, leakyProperties);
226 }
227
228 return GrDistanceFieldTextContext::Create(this, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700229}
230
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000231////////////////////////////////////////////////////////////////////////////////
232
bsalomonf2703d82014-10-28 14:33:06 -0700233GrTexture* GrContext::findAndRefTexture(const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000234 const GrCacheID& cacheID,
235 const GrTextureParams* params) {
bsalomonafbf2d62014-09-30 12:18:44 -0700236 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon8b79d232014-11-10 10:19:06 -0800237
238 GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
bsalomon37dd3312014-11-03 08:47:23 -0800239 if (resource) {
bsalomon8b79d232014-11-10 10:19:06 -0800240 SkASSERT(static_cast<GrSurface*>(resource)->asTexture());
bsalomon37dd3312014-11-03 08:47:23 -0800241 return static_cast<GrSurface*>(resource)->asTexture();
bsalomon37dd3312014-11-03 08:47:23 -0800242 }
bsalomon8b79d232014-11-10 10:19:06 -0800243 return NULL;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000244}
245
bsalomonf2703d82014-10-28 14:33:06 -0700246bool GrContext::isTextureInCache(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000247 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000248 const GrTextureParams* params) const {
bsalomonafbf2d62014-09-30 12:18:44 -0700249 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
bsalomon8b79d232014-11-10 10:19:06 -0800250 return fResourceCache2->hasContentKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000251}
252
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000253static void stretch_image(void* dst,
254 int dstW,
255 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000256 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000257 int srcW,
258 int srcH,
259 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000260 SkFixed dx = (srcW << 16) / dstW;
261 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000262
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000263 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000264
robertphillips@google.com8b169312013-10-15 17:47:36 +0000265 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000266 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000267 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000268 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
269 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000270 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000271 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000272 x += dx;
273 }
274 y += dy;
275 }
276}
277
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000278// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000279// the current hardware. Resize the texture to be a POT
bsalomonf2703d82014-10-28 14:33:06 -0700280GrTexture* GrContext::createResizedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000281 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000282 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000283 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000284 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000285 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000286 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000287 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000288
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000289 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000290 return NULL;
291 }
292 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000293
bsalomonf2703d82014-10-28 14:33:06 -0700294 GrSurfaceDesc rtDesc = desc;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000295 rtDesc.fFlags = rtDesc.fFlags |
bsalomonf2703d82014-10-28 14:33:06 -0700296 kRenderTarget_GrSurfaceFlag |
297 kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000298 rtDesc.fWidth = GrNextPow2(desc.fWidth);
299 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000300
301 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
302
bsalomon49f085d2014-09-05 13:34:00 -0700303 if (texture) {
joshualitt9853cce2014-11-17 14:22:48 -0800304 GrDrawState drawState;
305 drawState.setRenderTarget(texture->asRenderTarget());
robertphillips@google.com3319f332012-08-13 18:00:36 +0000306
307 // if filtering is not desired then we want to ensure all
308 // texels in the resampled image are copies of texels from
309 // the original.
joshualitt5c55fef2014-10-31 14:04:35 -0700310 GrTextureParams params(SkShader::kClamp_TileMode,
311 filter ? GrTextureParams::kBilerp_FilterMode :
312 GrTextureParams::kNone_FilterMode);
joshualitt9853cce2014-11-17 14:22:48 -0800313 drawState.addColorTextureProcessor(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000314
joshualitt2dd1ae02014-12-03 06:24:10 -0800315 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType |
316 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt56995b52014-12-11 15:44:02 -0800317 SkAutoTUnref<const GrGeometryProcessor> gp(
318 GrDefaultGeoProcFactory::Create(GrColor_WHITE, flags));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000319
joshualitt2dd1ae02014-12-03 06:24:10 -0800320 GrDrawTarget::AutoReleaseGeometry arg(fDrawBuffer, 4, gp->getVertexStride(), 0);
321 SkASSERT(gp->getVertexStride() == 2 * sizeof(SkPoint));
robertphillips@google.com3319f332012-08-13 18:00:36 +0000322
323 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000324 SkPoint* verts = (SkPoint*) arg.vertices();
325 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
326 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
joshualitt56995b52014-12-11 15:44:02 -0800327 fDrawBuffer->drawNonIndexed(&drawState, gp, kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000328 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000329 } else {
330 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000331 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000332 // not. Either implement filtered stretch blit on CPU or just create
333 // one when FBO case fails.
334
bsalomonf2703d82014-10-28 14:33:06 -0700335 rtDesc.fFlags = kNone_GrSurfaceFlags;
robertphillips@google.com3319f332012-08-13 18:00:36 +0000336 // no longer need to clamp at min RT size.
337 rtDesc.fWidth = GrNextPow2(desc.fWidth);
338 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000339
340 // We shouldn't be resizing a compressed texture.
341 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
342
robertphillips@google.com8b169312013-10-15 17:47:36 +0000343 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700344 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000345 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
346 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000347
348 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
349
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000350 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
bsalomon49f085d2014-09-05 13:34:00 -0700351 SkASSERT(texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000352 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000353
354 return texture;
355}
356
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000357GrTexture* GrContext::createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700358 const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000359 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000360 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000361 size_t rowBytes,
362 GrResourceKey* cacheKey) {
bsalomonafbf2d62014-09-30 12:18:44 -0700363 GrResourceKey resourceKey = GrTexturePriv::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000364
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000365 GrTexture* texture;
bsalomonafbf2d62014-09-30 12:18:44 -0700366 if (GrTexturePriv::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700367 // We do not know how to resize compressed textures.
368 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
369
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000370 texture = this->createResizedTexture(desc, cacheID,
371 srcData, rowBytes,
bsalomonafbf2d62014-09-30 12:18:44 -0700372 GrTexturePriv::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000373 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700374 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000375 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000376
bsalomon49f085d2014-09-05 13:34:00 -0700377 if (texture) {
bsalomon71cb0c22014-11-14 12:10:14 -0800378 if (texture->cacheAccess().setContentKey(resourceKey)) {
379 if (cacheKey) {
380 *cacheKey = resourceKey;
381 }
382 } else {
383 texture->unref();
384 texture = NULL;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000385 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000386 }
387
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000388 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389}
390
bsalomonf2703d82014-10-28 14:33:06 -0700391GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700392 bool calledDuringFlush) {
bsalomonbcf0a522014-10-08 08:40:09 -0700393 // kNoStencil has no meaning if kRT isn't set.
bsalomonf2703d82014-10-28 14:33:06 -0700394 SkASSERT((inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
395 !(inDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000396
bsalomonbcf0a522014-10-08 08:40:09 -0700397 // Make sure caller has checked for renderability if kRT is set.
bsalomonf2703d82014-10-28 14:33:06 -0700398 SkASSERT(!(inDesc.fFlags & kRenderTarget_GrSurfaceFlag) ||
bsalomonbcf0a522014-10-08 08:40:09 -0700399 this->isConfigRenderable(inDesc.fConfig, inDesc.fSampleCnt > 0));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000400
bsalomonf2703d82014-10-28 14:33:06 -0700401 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000402
bsalomonf2703d82014-10-28 14:33:06 -0700403 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
404 GrSurfaceFlags origFlags = desc->fFlags;
bsalomonbcf0a522014-10-08 08:40:09 -0700405 if (kApprox_ScratchTexMatch == match) {
406 // bin by pow2 with a reasonable min
407 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700408 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700409 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
410 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000411 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000412
bsalomonbcf0a522014-10-08 08:40:09 -0700413 do {
414 GrResourceKey key = GrTexturePriv::ComputeScratchKey(*desc);
bsalomon000f8292014-10-15 19:04:14 -0700415 uint32_t scratchFlags = 0;
416 if (calledDuringFlush) {
417 scratchFlags = GrResourceCache2::kRequireNoPendingIO_ScratchFlag;
bsalomonf2703d82014-10-28 14:33:06 -0700418 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon000f8292014-10-15 19:04:14 -0700419 // If it is not a render target then it will most likely be populated by
420 // writePixels() which will trigger a flush if the texture has pending IO.
421 scratchFlags = GrResourceCache2::kPreferNoPendingIO_ScratchFlag;
422 }
423 GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags);
bsalomonbcf0a522014-10-08 08:40:09 -0700424 if (resource) {
bsalomon37dd3312014-11-03 08:47:23 -0800425 return static_cast<GrSurface*>(resource)->asTexture();
bsalomonbcf0a522014-10-08 08:40:09 -0700426 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427
bsalomonbcf0a522014-10-08 08:40:09 -0700428 if (kExact_ScratchTexMatch == match) {
429 break;
430 }
431 // We had a cache miss and we are in approx mode, relax the fit of the flags.
432
433 // We no longer try to reuse textures that were previously used as render targets in
434 // situations where no RT is needed; doing otherwise can confuse the video driver and
435 // cause significant performance problems in some cases.
bsalomonf2703d82014-10-28 14:33:06 -0700436 if (desc->fFlags & kNoStencil_GrSurfaceFlag) {
437 desc.writable()->fFlags = desc->fFlags & ~kNoStencil_GrSurfaceFlag;
bsalomonbcf0a522014-10-08 08:40:09 -0700438 } else {
439 break;
440 }
441
442 } while (true);
443
444 desc.writable()->fFlags = origFlags;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000445 }
446
bsalomon71cb0c22014-11-14 12:10:14 -0800447 GrTexture* texture = fGpu->createTexture(*desc, NULL, 0);
bsalomonbcf0a522014-10-08 08:40:09 -0700448 SkASSERT(NULL == texture ||
bsalomon453cf402014-11-11 14:15:57 -0800449 texture->cacheAccess().getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
bsalomonbcf0a522014-10-08 08:40:09 -0700450 return texture;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400451}
452
bsalomon71cb0c22014-11-14 12:10:14 -0800453void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800454 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800455
bsalomon66a450f2014-11-13 13:19:10 -0800456 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800457
458 // Flush the InOrderDrawBuffer to possibly free up some textures
459 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000460}
461
462
bsalomonf2703d82014-10-28 14:33:06 -0700463GrTexture* GrContext::createUncachedTexture(const GrSurfaceDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000464 void* srcData,
465 size_t rowBytes) {
bsalomonf2703d82014-10-28 14:33:06 -0700466 GrSurfaceDesc descCopy = descIn;
bsalomon84c8e622014-11-17 09:33:27 -0800467 GrTexture* texture = fGpu->createTexture(descCopy, srcData, rowBytes);
468 if (texture) {
469 // TODO: It'd be nice to be able to do this before creation so we don't boot something
470 // out of the cache. We could temporarily boost the cache budget.
471 texture->cacheAccess().setBudgeted(false);
472 }
473 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000474}
475
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000476void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800477 if (maxTextures) {
478 *maxTextures = fResourceCache2->getMaxResourceCount();
479 }
480 if (maxTextureBytes) {
481 *maxTextureBytes = fResourceCache2->getMaxResourceBytes();
482 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000483}
484
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000485void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800486 fResourceCache2->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000487}
488
bsalomon@google.com91958362011-06-13 17:58:13 +0000489int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000490 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000491}
492
493int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000494 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000495}
496
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000497int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000498 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000499}
500
bsalomon@google.com27847de2011-02-22 20:59:41 +0000501///////////////////////////////////////////////////////////////////////////////
502
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000503GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
504 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000505}
506
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000507GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
508 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000509}
510
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000511///////////////////////////////////////////////////////////////////////////////
512
bsalomon@google.comb8670992012-07-25 21:27:09 +0000513bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000514 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000515 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000516 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000517 return false;
518 }
519
tfarinaf9dae782014-06-06 06:35:28 -0700520 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000521
522 if (!isPow2) {
bsalomon49f085d2014-09-05 13:34:00 -0700523 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000524 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000525 return false;
526 }
527 }
528 return true;
529}
530
bsalomon@google.com27847de2011-02-22 20:59:41 +0000531
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532////////////////////////////////////////////////////////////////////////////////
533
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000534void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000535 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000536 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700537 GrRenderTarget* renderTarget) {
538 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800539 SkASSERT(renderTarget);
540
bsalomonf21dab92014-11-13 13:33:28 -0800541 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700542 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt5531d512014-12-17 15:50:11 -0800543 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700544 if (NULL == target) {
545 return;
546 }
547 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000548}
549
joshualitt5531d512014-12-17 15:50:11 -0800550void GrContext::drawPaint(const GrPaint& origPaint, const SkMatrix& viewMatrix) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551 // set rect to be big enough to fill the space, but not super-huge, so we
552 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000553 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000554 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000555 SkIntToScalar(getRenderTarget()->width()),
556 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000557 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800558
559 // by definition this fills the entire clip, no need for AA
560 if (paint->isAntiAlias()) {
561 paint.writable()->setAntiAlias(false);
562 }
563
564 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000565
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000566 // We attempt to map r by the inverse matrix and draw that. mapRect will
567 // map the four corners and bound them with a new rect. This will not
568 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800569 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800570 SkMatrix inverse;
571 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700572 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000573 return;
574 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575 inverse.mapRect(&r);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800576 this->drawRect(*paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800578 SkMatrix localMatrix;
579 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700580 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000581 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000582 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800583
584 AutoCheckFlush acf(this);
585 GrDrawState drawState;
586 GrDrawTarget* target = this->prepareToDraw(&drawState, paint, &SkMatrix::I(), &acf);
587 if (NULL == target) {
588 return;
589 }
590
591 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
592 target->drawRect(&drawState, paint->getColor(), r, NULL, &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594}
595
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000596#ifdef SK_DEVELOPER
597void GrContext::dumpFontCache() const {
598 fFontCache->dump();
599}
600#endif
601
bsalomon@google.com205d4602011-04-25 12:43:45 +0000602////////////////////////////////////////////////////////////////////////////////
603
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604/* create a triangle strip that strokes the specified triangle. There are 8
605 unique vertices, but we repreat the last 2 to close up. Alternatively we
606 could use an indices array, and then only send 8 verts, but not sure that
607 would be faster.
608 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000609static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000610 SkScalar width) {
611 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000612 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000613
614 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
615 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
616 verts[2].set(rect.fRight - rad, rect.fTop + rad);
617 verts[3].set(rect.fRight + rad, rect.fTop - rad);
618 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
619 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
620 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
621 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
622 verts[8] = verts[0];
623 verts[9] = verts[1];
624}
625
bsalomonc30aaa02014-08-13 07:15:29 -0700626static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700627 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
628 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700629}
630
bsalomon@google.com205d4602011-04-25 12:43:45 +0000631static bool apply_aa_to_rect(GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -0800632 GrDrawState* ds,
633 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000634 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000635 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800636 const SkMatrix& combinedMatrix,
637 GrColor color) {
638 if (!ds->canTweakAlphaForCoverage() && !ds->canUseFracCoveragePrimProc(color,
639 *target->caps())) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000640#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700641 //SkDebugf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000642#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700643 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000644 }
joshualitt9853cce2014-11-17 14:22:48 -0800645
646 if (ds->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000647 return false;
648 }
649
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000650#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000651 if (strokeWidth >= 0) {
652#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000653 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000654 return false;
655 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000656
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000657#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000658 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000659 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000660 return false;
661 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000662 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000663#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000664
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000665 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800666 if (!combinedMatrix.rectStaysRect()) {
667 return true;
668 }
669
bsalomonc30aaa02014-08-13 07:15:29 -0700670 if (strokeWidth < 0) {
671 return !is_irect(*devBoundRect);
672 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000673
bsalomon9c0822a2014-08-11 11:07:48 -0700674 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000675}
676
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000677static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
678 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
679 point.fY >= rect.fTop && point.fY <= rect.fBottom;
680}
681
bsalomon@google.com27847de2011-02-22 20:59:41 +0000682void GrContext::drawRect(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800683 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000684 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700685 const GrStrokeInfo* strokeInfo) {
bsalomon49f085d2014-09-05 13:34:00 -0700686 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700687 SkPath path;
688 path.addRect(rect);
joshualitt5531d512014-12-17 15:50:11 -0800689 this->drawPath(paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700690 return;
691 }
692
bsalomonf21dab92014-11-13 13:33:28 -0800693 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -0800694 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -0800695 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700696 if (NULL == target) {
697 return;
698 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000699
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000700 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700701 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
joshualitt9853cce2014-11-17 14:22:48 -0800702 SkMatrix matrix = drawState.getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000703
704 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
705 // cases where the RT is fully inside a stroke.
706 if (width < 0) {
707 SkRect rtRect;
joshualitt9853cce2014-11-17 14:22:48 -0800708 drawState.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000709 SkRect clipSpaceRTRect = rtRect;
710 bool checkClip = false;
bsalomon49f085d2014-09-05 13:34:00 -0700711 if (this->getClip()) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000712 checkClip = true;
713 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
714 SkIntToScalar(this->getClip()->fOrigin.fY));
715 }
716 // Does the clip contain the entire RT?
717 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
718 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700719 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000720 return;
721 }
722 // Does the rect bound the RT?
723 SkPoint srcSpaceRTQuad[4];
724 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
725 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
726 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
727 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
728 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
729 // Will it blend?
730 GrColor clearColor;
731 if (paint.isOpaqueAndConstantColor(&clearColor)) {
bsalomon89c62982014-11-03 12:08:42 -0800732 target->clear(NULL, clearColor, true, fRenderTarget);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000733 return;
734 }
735 }
736 }
737 }
738
joshualitt2e3b3e32014-12-09 13:31:14 -0800739 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000740 SkRect devBoundRect;
joshualitt9853cce2014-11-17 14:22:48 -0800741 bool needAA = paint.isAntiAlias() && !drawState.getRenderTarget()->isMultisampled();
joshualitt2e3b3e32014-12-09 13:31:14 -0800742 bool doAA = needAA && apply_aa_to_rect(target, &drawState, &devBoundRect, rect, width, matrix,
743 color);
egdanield58a0ba2014-06-11 10:30:05 -0700744
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 if (doAA) {
joshualittd27f73e2014-12-29 07:43:36 -0800746 SkMatrix invert;
747 if (!drawState.getViewMatrix().invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000748 return;
749 }
joshualittd27f73e2014-12-29 07:43:36 -0800750 GrDrawState::AutoViewMatrixRestore avmr(&drawState);
joshualitt9853cce2014-11-17 14:22:48 -0800751
bsalomon@google.com205d4602011-04-25 12:43:45 +0000752 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800753 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800754 fAARectRenderer->strokeAARect(target,
755 &drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800756 color,
joshualittd27f73e2014-12-29 07:43:36 -0800757 invert,
joshualitt9853cce2014-11-17 14:22:48 -0800758 rect,
759 matrix,
760 devBoundRect,
761 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000762 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000763 // filled AA rect
joshualittd27f73e2014-12-29 07:43:36 -0800764 fAARectRenderer->fillAARect(target, &drawState, color, invert, rect, matrix,
765 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000766 }
767 return;
768 }
769
bsalomon@google.com27847de2011-02-22 20:59:41 +0000770 if (width >= 0) {
771 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000772 // Hairline could be done by just adding closing vertex to
773 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000774
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775 static const int worstCaseVertCount = 10;
joshualitt56995b52014-12-11 15:44:02 -0800776 SkAutoTUnref<const GrGeometryProcessor> gp(GrDefaultGeoProcFactory::Create(color));
joshualitt9853cce2014-11-17 14:22:48 -0800777 GrDrawTarget::AutoReleaseGeometry geo(target,
778 worstCaseVertCount,
joshualitt2dd1ae02014-12-03 06:24:10 -0800779 gp->getVertexStride(),
joshualitt9853cce2014-11-17 14:22:48 -0800780 0);
joshualitt2dd1ae02014-12-03 06:24:10 -0800781 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782
783 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700784 SkDebugf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785 return;
786 }
787
788 GrPrimitiveType primType;
789 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000790 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791
792 if (width > 0) {
793 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000794 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795 setStrokeRectStrip(vertex, rect, width);
796 } else {
797 // hairline
798 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000799 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 vertex[0].set(rect.fLeft, rect.fTop);
801 vertex[1].set(rect.fRight, rect.fTop);
802 vertex[2].set(rect.fRight, rect.fBottom);
803 vertex[3].set(rect.fLeft, rect.fBottom);
804 vertex[4].set(rect.fLeft, rect.fTop);
805 }
806
joshualitt56995b52014-12-11 15:44:02 -0800807 target->drawNonIndexed(&drawState, gp, primType, 0, vertCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000809 // filled BW rect
joshualitt2e3b3e32014-12-09 13:31:14 -0800810 target->drawSimpleRect(&drawState, color, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811 }
812}
813
joshualitt16b27892014-12-18 07:47:16 -0800814void GrContext::drawNonAARectToRect(const GrPaint& paint,
815 const SkMatrix& viewMatrix,
816 const SkRect& rectToDraw,
817 const SkRect& localRect,
818 const SkMatrix* localMatrix) {
bsalomonf21dab92014-11-13 13:33:28 -0800819 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -0800820 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -0800821 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700822 if (NULL == target) {
823 return;
824 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000825
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000826 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
827
joshualitt16b27892014-12-18 07:47:16 -0800828 target->drawRect(&drawState, paint.getColor(), rectToDraw, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000829}
830
joshualitt56995b52014-12-11 15:44:02 -0800831static const GrGeometryProcessor* set_vertex_attributes(const SkPoint* texCoords,
832 const GrColor* colors,
833 int* colorOffset,
834 int* texOffset,
835 GrColor color) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000836 *texOffset = -1;
837 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800838 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700839 if (texCoords && colors) {
joshualitt5478d422014-11-14 16:00:38 -0800840 *colorOffset = sizeof(SkPoint);
841 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
842 flags |= GrDefaultGeoProcFactory::kColor_GPType |
843 GrDefaultGeoProcFactory::kLocalCoord_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700844 } else if (texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000845 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800846 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
bsalomon49f085d2014-09-05 13:34:00 -0700847 } else if (colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000848 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800849 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000850 }
joshualitt56995b52014-12-11 15:44:02 -0800851 return GrDefaultGeoProcFactory::Create(color, flags);
robertphillips@google.com42903302013-04-20 12:26:07 +0000852}
853
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854void GrContext::drawVertices(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800855 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856 GrPrimitiveType primitiveType,
857 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000858 const SkPoint positions[],
859 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000860 const GrColor colors[],
861 const uint16_t indices[],
862 int indexCount) {
bsalomonf21dab92014-11-13 13:33:28 -0800863 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -0800864 GrDrawState drawState;
bsalomonf21dab92014-11-13 13:33:28 -0800865 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000866
joshualitt5531d512014-12-17 15:50:11 -0800867 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700868 if (NULL == target) {
869 return;
870 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000871
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000872 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
873
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000874 int colorOffset = -1, texOffset = -1;
joshualitt56995b52014-12-11 15:44:02 -0800875 SkAutoTUnref<const GrGeometryProcessor> gp(
876 set_vertex_attributes(texCoords, colors, &colorOffset, &texOffset, paint.getColor()));
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000877
joshualitt56995b52014-12-11 15:44:02 -0800878 size_t vertexStride = gp->getVertexStride();
joshualitt2dd1ae02014-12-03 06:24:10 -0800879 SkASSERT(vertexStride == sizeof(SkPoint) + (SkToBool(texCoords) ? sizeof(SkPoint) : 0)
880 + (SkToBool(colors) ? sizeof(GrColor) : 0));
joshualitt9853cce2014-11-17 14:22:48 -0800881 if (!geo.set(target, vertexCount, vertexStride, indexCount)) {
joshualittd1aa8ff2014-11-04 07:47:55 -0800882 SkDebugf("Failed to get space for vertices!\n");
883 return;
884 }
885 void* curVertex = geo.vertices();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000886
joshualittd1aa8ff2014-11-04 07:47:55 -0800887 for (int i = 0; i < vertexCount; ++i) {
888 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889
joshualittd1aa8ff2014-11-04 07:47:55 -0800890 if (texOffset >= 0) {
891 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000892 }
joshualittd1aa8ff2014-11-04 07:47:55 -0800893 if (colorOffset >= 0) {
894 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
895 }
joshualitt9853cce2014-11-17 14:22:48 -0800896 curVertex = (void*)((intptr_t)curVertex + vertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897 }
898
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000899 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000900 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon49f085d2014-09-05 13:34:00 -0700901 if (indices) {
joshualittd1aa8ff2014-11-04 07:47:55 -0800902 uint16_t* curIndex = (uint16_t*)geo.indices();
903 for (int i = 0; i < indexCount; ++i) {
904 curIndex[i] = indices[i];
905 }
joshualitt56995b52014-12-11 15:44:02 -0800906 target->drawIndexed(&drawState, gp, primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907 } else {
joshualitt56995b52014-12-11 15:44:02 -0800908 target->drawNonIndexed(&drawState, gp, primitiveType, 0, vertexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000909 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910}
911
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000912///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +0000913
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000914void GrContext::drawRRect(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800915 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000916 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -0700917 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000918 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000919 return;
920 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000921
egdanield58a0ba2014-06-11 10:30:05 -0700922 if (strokeInfo.isDashed()) {
923 SkPath path;
924 path.addRRect(rrect);
joshualitt5531d512014-12-17 15:50:11 -0800925 this->drawPath(paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700926 return;
927 }
928
bsalomonf21dab92014-11-13 13:33:28 -0800929 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -0800930 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -0800931 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700932 if (NULL == target) {
933 return;
934 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000935
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000936 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
937
egdanield58a0ba2014-06-11 10:30:05 -0700938 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
939
joshualitt2e3b3e32014-12-09 13:31:14 -0800940 GrColor color = paint.getColor();
joshualitt5531d512014-12-17 15:50:11 -0800941 if (!fOvalRenderer->drawRRect(target, &drawState, color, paint.isAntiAlias(), rrect,
joshualitt9853cce2014-11-17 14:22:48 -0800942 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000943 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000944 path.addRRect(rrect);
joshualitt5531d512014-12-17 15:50:11 -0800945 this->internalDrawPath(target, &drawState, viewMatrix, color, paint.isAntiAlias(), path,
946 strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000947 }
948}
949
950///////////////////////////////////////////////////////////////////////////////
951
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000952void GrContext::drawDRRect(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800953 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000954 const SkRRect& outer,
955 const SkRRect& inner) {
956 if (outer.isEmpty()) {
957 return;
958 }
959
bsalomonf21dab92014-11-13 13:33:28 -0800960 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -0800961 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -0800962 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000963
964 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
965
joshualitt2e3b3e32014-12-09 13:31:14 -0800966 GrColor color = paint.getColor();
joshualitt5531d512014-12-17 15:50:11 -0800967 if (!fOvalRenderer->drawDRRect(target, &drawState, color, paint.isAntiAlias(), outer, inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000968 SkPath path;
969 path.addRRect(inner);
970 path.addRRect(outer);
971 path.setFillType(SkPath::kEvenOdd_FillType);
972
egdanield58a0ba2014-06-11 10:30:05 -0700973 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
joshualitt5531d512014-12-17 15:50:11 -0800974 this->internalDrawPath(target, &drawState, viewMatrix, color, paint.isAntiAlias(), path,
975 fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000976 }
977}
978
979///////////////////////////////////////////////////////////////////////////////
980
bsalomon@google.com93c96602012-04-27 13:05:21 +0000981void GrContext::drawOval(const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800982 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000983 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700984 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000985 if (oval.isEmpty()) {
986 return;
987 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000988
egdanield58a0ba2014-06-11 10:30:05 -0700989 if (strokeInfo.isDashed()) {
990 SkPath path;
991 path.addOval(oval);
joshualitt5531d512014-12-17 15:50:11 -0800992 this->drawPath(paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700993 return;
994 }
995
bsalomonf21dab92014-11-13 13:33:28 -0800996 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -0800997 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -0800998 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700999 if (NULL == target) {
1000 return;
1001 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001002
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001003 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1004
egdanield58a0ba2014-06-11 10:30:05 -07001005 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1006
joshualitt2e3b3e32014-12-09 13:31:14 -08001007 GrColor color = paint.getColor();
joshualitt5531d512014-12-17 15:50:11 -08001008 if (!fOvalRenderer->drawOval(target, &drawState, color, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001009 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001010 path.addOval(oval);
joshualitt5531d512014-12-17 15:50:11 -08001011 this->internalDrawPath(target, &drawState, viewMatrix, color, paint.isAntiAlias(), path,
1012 strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001013 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001014}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001015
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001016// Can 'path' be drawn as a pair of filled nested rectangles?
1017static bool is_nested_rects(GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -08001018 GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -08001019 GrColor color,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001020 const SkPath& path,
1021 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001022 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001023 SkASSERT(stroke.isFillStyle());
1024
1025 if (path.isInverseFillType()) {
1026 return false;
1027 }
1028
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001029 // TODO: this restriction could be lifted if we were willing to apply
1030 // the matrix to all the points individually rather than just to the rect
joshualitt9853cce2014-11-17 14:22:48 -08001031 if (!drawState->getViewMatrix().preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001032 return false;
1033 }
1034
joshualitt2e3b3e32014-12-09 13:31:14 -08001035 if (!drawState->canTweakAlphaForCoverage() &&
1036 !drawState->canUseFracCoveragePrimProc(color, *target->caps())) {
bsalomon9c0822a2014-08-11 11:07:48 -07001037 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001038 }
1039
1040 SkPath::Direction dirs[2];
1041 if (!path.isNestedRects(rects, dirs)) {
1042 return false;
1043 }
1044
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001045 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001046 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001047 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001048 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001049
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001050 // Right now, nested rects where the margin is not the same width
1051 // all around do not render correctly
1052 const SkScalar* outer = rects[0].asScalars();
1053 const SkScalar* inner = rects[1].asScalars();
1054
robertphillips183e9852014-10-21 11:25:37 -07001055 bool allEq = true;
1056
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001057 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001058 bool allGoE1 = margin >= SK_Scalar1;
1059
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001060 for (int i = 1; i < 4; ++i) {
1061 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001062 if (temp < SK_Scalar1) {
1063 allGoE1 = false;
1064 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001065 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001066 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001067 }
1068 }
1069
robertphillips183e9852014-10-21 11:25:37 -07001070 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001071}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001072
joshualitt5531d512014-12-17 15:50:11 -08001073void GrContext::drawPath(const GrPaint& paint,
1074 const SkMatrix& viewMatrix,
1075 const SkPath& path,
1076 const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001077
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001078 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001079 if (path.isInverseFillType()) {
joshualitt5531d512014-12-17 15:50:11 -08001080 this->drawPaint(paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001081 }
1082 return;
1083 }
1084
joshualitt2e3b3e32014-12-09 13:31:14 -08001085 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001086 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001087 SkPoint pts[2];
1088 if (path.isLine(pts)) {
bsalomonf21dab92014-11-13 13:33:28 -08001089 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -08001090 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -08001091 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001092 if (NULL == target) {
1093 return;
joshualittd27f73e2014-12-29 07:43:36 -08001094 }
egdaniele61c4112014-06-12 10:24:21 -07001095
joshualittd27f73e2014-12-29 07:43:36 -08001096 if (GrDashingEffect::DrawDashLine(fGpu, target, &drawState, color, pts, paint,
1097 strokeInfo)) {
1098 return;
egdaniele61c4112014-06-12 10:24:21 -07001099 }
1100 }
1101
1102 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001103 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1104 SkTLazy<SkPath> effectPath;
1105 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1106 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1107 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
joshualitt5531d512014-12-17 15:50:11 -08001108 this->drawPath(paint, viewMatrix, *effectPath.get(), newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001109 return;
1110 }
1111
joshualitt5531d512014-12-17 15:50:11 -08001112 this->drawPath(paint, viewMatrix, path, newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001113 return;
1114 }
1115
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001116 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001117 // Scratch textures can be recycled after they are returned to the texture
1118 // cache. This presents a potential hazard for buffered drawing. However,
1119 // the writePixels that uploads to the scratch will perform a flush so we're
1120 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001121 AutoCheckFlush acf(this);
joshualitt9853cce2014-11-17 14:22:48 -08001122 GrDrawState drawState;
joshualitt5531d512014-12-17 15:50:11 -08001123 GrDrawTarget* target = this->prepareToDraw(&drawState, &paint, &viewMatrix, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001124 if (NULL == target) {
1125 return;
1126 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001127
egdaniel93a37bc2014-07-21 13:47:57 -07001128 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001129
egdanield58a0ba2014-06-11 10:30:05 -07001130 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1131
joshualitt9853cce2014-11-17 14:22:48 -08001132 bool useCoverageAA = paint.isAntiAlias() && !drawState.getRenderTarget()->isMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001133
egdanield58a0ba2014-06-11 10:30:05 -07001134 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001135 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001136 SkRect rects[2];
1137
joshualitt2e3b3e32014-12-09 13:31:14 -08001138 if (is_nested_rects(target, &drawState, color, path, strokeRec, rects)) {
joshualitt9853cce2014-11-17 14:22:48 -08001139 SkMatrix origViewMatrix = drawState.getViewMatrix();
joshualittd27f73e2014-12-29 07:43:36 -08001140
1141 SkMatrix invert;
1142 if (!drawState.getViewMatrix().invert(&invert)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001143 return;
1144 }
joshualittd27f73e2014-12-29 07:43:36 -08001145 GrDrawState::AutoViewMatrixRestore avmr(&drawState);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001146
joshualittd27f73e2014-12-29 07:43:36 -08001147 fAARectRenderer->fillAANestedRects(target, &drawState, color, invert, rects,
1148 origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001149 return;
1150 }
1151 }
1152
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001153 SkRect ovalRect;
1154 bool isOval = path.isOval(&ovalRect);
1155
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001156 if (!isOval || path.isInverseFillType()
joshualitt5531d512014-12-17 15:50:11 -08001157 || !fOvalRenderer->drawOval(target, &drawState, color, paint.isAntiAlias(), ovalRect,
joshualitt9853cce2014-11-17 14:22:48 -08001158 strokeRec)) {
joshualitt5531d512014-12-17 15:50:11 -08001159 this->internalDrawPath(target, &drawState, viewMatrix, color, paint.isAntiAlias(), path,
1160 strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001161 }
1162}
1163
joshualitt9853cce2014-11-17 14:22:48 -08001164void GrContext::internalDrawPath(GrDrawTarget* target,
1165 GrDrawState* drawState,
joshualitt5531d512014-12-17 15:50:11 -08001166 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001167 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001168 bool useAA,
1169 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001170 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001171 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001172
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001173 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1174
1175
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001176 // An Assumption here is that path renderer would use some form of tweaking
1177 // the src color (either the input alpha or in the frag shader) to implement
1178 // aa. If we have some future driver-mojo path AA that can do the right
1179 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001180 bool useCoverageAA = useAA &&
joshualitt9853cce2014-11-17 14:22:48 -08001181 !drawState->getRenderTarget()->isMultisampled() &&
joshualitt2e3b3e32014-12-09 13:31:14 -08001182 drawState->canUseFracCoveragePrimProc(color, *target->caps());
bsalomon@google.com289533a2011-10-27 12:34:25 +00001183
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001184
1185 GrPathRendererChain::DrawType type =
1186 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001187 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001188
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001189 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001190 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001191 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001192
1193 // Try a 1st time without stroking the path and without allowing the SW renderer
joshualitt9853cce2014-11-17 14:22:48 -08001194 GrPathRenderer* pr = this->getPathRenderer(target, drawState, *pathPtr, *stroke, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001195
robertphillips@google.come79f3202014-02-11 16:30:21 +00001196 if (NULL == pr) {
joshualitt5531d512014-12-17 15:50:11 -08001197 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001198 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001199 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1200 pathPtr = tmpPath.get();
1201 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001202 if (pathPtr->isEmpty()) {
1203 return;
1204 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001205 }
1206 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001207
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001208 // This time, allow SW renderer
joshualitt9853cce2014-11-17 14:22:48 -08001209 pr = this->getPathRenderer(target, drawState, *pathPtr, *stroke, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001210 }
1211
robertphillips@google.come79f3202014-02-11 16:30:21 +00001212 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001213#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001214 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001215#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001216 return;
1217 }
1218
joshualitt2e3b3e32014-12-09 13:31:14 -08001219 pr->drawPath(target, drawState, color, *pathPtr, *stroke, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001220}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001221
bsalomon@google.com27847de2011-02-22 20:59:41 +00001222////////////////////////////////////////////////////////////////////////////////
1223
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001224void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001225 if (NULL == fDrawBuffer) {
1226 return;
1227 }
1228
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001229 if (kDiscard_FlushBit & flagsBitfield) {
1230 fDrawBuffer->reset();
1231 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001232 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001233 }
bsalomonf21dab92014-11-13 13:33:28 -08001234 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001235}
1236
bsalomon81beccc2014-10-13 12:32:55 -07001237bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1238 const void* inPixels, size_t outRowBytes, void* outPixels) {
1239 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001240 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001241 return false;
1242 }
bsalomon81beccc2014-10-13 12:32:55 -07001243 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1244 srcPI.fPixels = inPixels;
1245 srcPI.fRowBytes = inRowBytes;
1246
1247 SkDstPixelInfo dstPI;
1248 dstPI.fColorType = srcPI.fColorType;
1249 dstPI.fAlphaType = kPremul_SkAlphaType;
1250 dstPI.fPixels = outPixels;
1251 dstPI.fRowBytes = outRowBytes;
1252
1253 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001254}
1255
bsalomon81beccc2014-10-13 12:32:55 -07001256bool GrContext::writeSurfacePixels(GrSurface* surface,
1257 int left, int top, int width, int height,
1258 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1259 uint32_t pixelOpsFlags) {
1260
1261 {
1262 GrTexture* texture = NULL;
1263 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1264 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1265
1266 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1267 surface->surfacePriv().hasPendingIO()) {
1268 this->flush();
1269 }
1270 return fGpu->writeTexturePixels(texture, left, top, width, height,
1271 srcConfig, buffer, rowBytes);
1272 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1273 // upload is already flushed.
1274 }
1275 }
1276
1277 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1278 GrRenderTarget* renderTarget = surface->asRenderTarget();
1279 if (NULL == renderTarget) {
1280 return false;
1281 }
1282
1283 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1284 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1285 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1286 // when drawing the scratch to the dst using a conversion effect.
1287 bool swapRAndB = false;
1288 GrPixelConfig writeConfig = srcConfig;
1289 if (GrPixelConfigSwapRAndB(srcConfig) ==
1290 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1291 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1292 swapRAndB = true;
1293 }
1294
bsalomonf2703d82014-10-28 14:33:06 -07001295 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001296 desc.fWidth = width;
1297 desc.fHeight = height;
1298 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001299 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1300 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001301 return false;
1302 }
1303
1304 SkAutoTUnref<const GrFragmentProcessor> fp;
1305 SkMatrix textureMatrix;
1306 textureMatrix.setIDiv(texture->width(), texture->height());
1307
1308 // allocate a tmp buffer and sw convert the pixels to premul
1309 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1310
1311 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1312 if (!GrPixelConfigIs8888(srcConfig)) {
1313 return false;
1314 }
1315 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1316 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1317 if (NULL == fp) {
1318 size_t tmpRowBytes = 4 * width;
1319 tmpPixels.reset(width * height);
1320 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1321 tmpPixels.get())) {
1322 return false;
1323 }
1324 rowBytes = tmpRowBytes;
1325 buffer = tmpPixels.get();
1326 }
1327 }
1328 if (NULL == fp) {
1329 fp.reset(GrConfigConversionEffect::Create(texture,
1330 swapRAndB,
1331 GrConfigConversionEffect::kNone_PMConversion,
1332 textureMatrix));
1333 }
1334
1335 // Even if the client told us not to flush, we still flush here. The client may have known that
1336 // writes to the original surface caused no data hazards, but they can't know that the scratch
1337 // we just got is safe.
1338 if (texture->surfacePriv().hasPendingIO()) {
1339 this->flush();
1340 }
1341 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1342 writeConfig, buffer, rowBytes)) {
1343 return false;
1344 }
1345
1346 SkMatrix matrix;
1347 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1348
1349 // This function can be called in the midst of drawing another object (e.g., when uploading a
1350 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1351 // drawing a rect to the render target.
1352 // The bracket ensures we pop the stack if we wind up flushing below.
1353 {
joshualitt5531d512014-12-17 15:50:11 -08001354 GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL, NULL, NULL);
joshualitt9853cce2014-11-17 14:22:48 -08001355 GrDrawTarget::AutoGeometryPush agp(drawTarget);
1356
1357 GrDrawState drawState(matrix);
1358 drawState.addColorProcessor(fp);
1359 drawState.setRenderTarget(renderTarget);
joshualitt2e3b3e32014-12-09 13:31:14 -08001360 drawTarget->drawSimpleRect(&drawState, GrColor_WHITE,SkRect::MakeWH(SkIntToScalar(width),
1361 SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001362 }
1363
1364 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1365 this->flushSurfaceWrites(surface);
1366 }
1367
1368 return true;
1369}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001370
reed@google.com7111d462014-03-25 16:20:24 +00001371// toggles between RGBA and BGRA
1372static SkColorType toggle_colortype32(SkColorType ct) {
1373 if (kRGBA_8888_SkColorType == ct) {
1374 return kBGRA_8888_SkColorType;
1375 } else {
1376 SkASSERT(kBGRA_8888_SkColorType == ct);
1377 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001378 }
1379}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001380
bsalomon@google.com0342a852012-08-20 19:22:38 +00001381bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1382 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001383 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001384 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001385 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001386 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001387
bsalomonafbf2d62014-09-30 12:18:44 -07001388 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001389 this->flush();
1390 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001391
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001392 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001393
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001394 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1395 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1396 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001397 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001398 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001399 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1400 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1401 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1402 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1403 // dstConfig.
1404 GrPixelConfig readConfig = dstConfig;
1405 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001406 if (GrPixelConfigSwapRAndB(dstConfig) ==
1407 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001408 readConfig = GrPixelConfigSwapRAndB(readConfig);
1409 swapRAndB = true;
1410 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001411
bsalomon@google.com0342a852012-08-20 19:22:38 +00001412 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001413
bsalomon@google.com9c680582013-02-06 18:17:50 +00001414 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001415 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001416 return false;
1417 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001418
bsalomon191bcc02014-11-14 11:31:13 -08001419 SkAutoTUnref<GrTexture> tempTexture;
1420
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001421 // If the src is a texture and we would have to do conversions after read pixels, we instead
1422 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1423 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1424 // on the read back pixels.
1425 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001426 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001427 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001428 GrSurfaceDesc desc;
1429 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001430 desc.fWidth = width;
1431 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001432 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001433 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001434
bsalomon@google.com9c680582013-02-06 18:17:50 +00001435 // 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 +00001436 // match the passed rect. However, if we see many different size rectangles we will trash
1437 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1438 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001439 ScratchTexMatch match = kApprox_ScratchTexMatch;
1440 if (0 == left &&
1441 0 == top &&
1442 target->width() == width &&
1443 target->height() == height &&
1444 fGpu->fullReadPixelsIsFasterThanPartial()) {
1445 match = kExact_ScratchTexMatch;
1446 }
bsalomon191bcc02014-11-14 11:31:13 -08001447 tempTexture.reset(this->refScratchTexture(desc, match));
1448 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001449 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001450 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001451 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001452 textureMatrix.postIDiv(src->width(), src->height());
1453
joshualittb0a8a372014-09-23 09:50:21 -07001454 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001455 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001456 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1457 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001458 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001459 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001460 }
1461 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1462 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001463 if (fp || flipY || swapRAndB) {
1464 if (!fp) {
1465 fp.reset(GrConfigConversionEffect::Create(
1466 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1467 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001468 }
1469 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001470
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001471 // We protect the existing geometry here since it may not be
1472 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1473 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001474 {
joshualitt9853cce2014-11-17 14:22:48 -08001475 GrDrawTarget::AutoGeometryPush agp(fDrawBuffer);
1476 GrDrawState drawState;
joshualitt5c55fef2014-10-31 14:04:35 -07001477 SkASSERT(fp);
joshualitt9853cce2014-11-17 14:22:48 -08001478 drawState.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001479
joshualitt9853cce2014-11-17 14:22:48 -08001480 drawState.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001481 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt2e3b3e32014-12-09 13:31:14 -08001482 fDrawBuffer->drawSimpleRect(&drawState, GrColor_WHITE, rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001483 // we want to read back from the scratch's origin
1484 left = 0;
1485 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001486 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001487 }
1488 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001489 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001490 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001491 }
joshualitt5c55fef2014-10-31 14:04:35 -07001492
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001493 if (!fGpu->readPixels(target,
1494 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001495 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001496 return false;
1497 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001498 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001499 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001500 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001501 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001502 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 }
reed@google.com7111d462014-03-25 16:20:24 +00001504 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1505 dstPI.fPixels = buffer;
1506 dstPI.fRowBytes = rowBytes;
1507
1508 SkSrcPixelInfo srcPI;
1509 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1510 srcPI.fAlphaType = kPremul_SkAlphaType;
1511 srcPI.fPixels = buffer;
1512 srcPI.fRowBytes = rowBytes;
1513
1514 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001515 }
1516 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001517}
1518
bsalomon87a94eb2014-11-03 14:28:32 -08001519void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
1520 SkASSERT(surface);
1521 ASSERT_OWNED_RESOURCE(surface);
1522 if (surface->surfacePriv().hasPendingIO()) {
1523 this->flush();
1524 }
1525 GrRenderTarget* rt = surface->asRenderTarget();
1526 if (fGpu && rt) {
1527 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001528 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001529}
1530
bsalomon41ebbdd2014-08-04 08:31:39 -07001531void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1532 SkASSERT(renderTarget);
1533 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001534 AutoCheckFlush acf(this);
joshualitt5531d512014-12-17 15:50:11 -08001535 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001536 if (NULL == target) {
1537 return;
1538 }
1539 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001540}
1541
bsalomonf80bfed2014-10-07 05:56:02 -07001542void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1543 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001544 if (NULL == src || NULL == dst) {
1545 return;
1546 }
bsalomone3d4bf22014-09-23 09:15:03 -07001547 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001548 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001549
bsalomonf80bfed2014-10-07 05:56:02 -07001550 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1551 // here.
junov96c118e2014-09-26 13:09:47 -07001552
joshualitt5531d512014-12-17 15:50:11 -08001553 GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL, NULL);
junov96c118e2014-09-26 13:09:47 -07001554 if (NULL == target) {
1555 return;
1556 }
junov96c118e2014-09-26 13:09:47 -07001557 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001558
1559 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1560 this->flush();
1561 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001562}
1563
bsalomonf80bfed2014-10-07 05:56:02 -07001564void GrContext::flushSurfaceWrites(GrSurface* surface) {
1565 if (surface->surfacePriv().hasPendingWrite()) {
1566 this->flush();
1567 }
1568}
1569
joshualitt9853cce2014-11-17 14:22:48 -08001570GrDrawTarget* GrContext::prepareToDraw(GrDrawState* ds,
1571 const GrPaint* paint,
joshualitt5531d512014-12-17 15:50:11 -08001572 const SkMatrix* viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001573 const AutoCheckFlush* acf) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001574 if (NULL == fGpu) {
1575 return NULL;
1576 }
1577
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001578 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
joshualitt9853cce2014-11-17 14:22:48 -08001579 if (ds) {
joshualitt5531d512014-12-17 15:50:11 -08001580 SkASSERT(viewMatrix);
joshualitt9853cce2014-11-17 14:22:48 -08001581 if (paint) {
1582 SkASSERT(acf);
joshualitt5531d512014-12-17 15:50:11 -08001583 ds->setFromPaint(*paint, *viewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001584#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
joshualitt2e3b3e32014-12-09 13:31:14 -08001585 if ((paint->hasMask()) &&
1586 !fDrawState->canUseFracCoveragePrimProc(paint.getColor(), fGpu->caps())) {
joshualitt9853cce2014-11-17 14:22:48 -08001587 SkDebugf("Partial pixel coverage will be incorrectly blended.\n");
1588 }
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001589#endif
joshualitt9853cce2014-11-17 14:22:48 -08001590 } else {
joshualitt5531d512014-12-17 15:50:11 -08001591 ds->reset(*viewMatrix);
joshualitt9853cce2014-11-17 14:22:48 -08001592 ds->setRenderTarget(fRenderTarget.get());
1593 }
1594 ds->setState(GrDrawState::kClip_StateBit, fClip && !fClip->fClipStack->isWideOpen());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001595 }
joshualitt5c55fef2014-10-31 14:04:35 -07001596 fDrawBuffer->setClip(fClip);
joshualitt5c55fef2014-10-31 14:04:35 -07001597 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598}
1599
robertphillips@google.com72176b22012-05-23 13:19:12 +00001600/*
1601 * This method finds a path renderer that can draw the specified path on
1602 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001603 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001604 * can be individually allowed/disallowed via the "allowSW" boolean.
1605 */
joshualitt9853cce2014-11-17 14:22:48 -08001606GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
1607 const GrDrawState* drawState,
1608 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001609 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001610 bool allowSW,
1611 GrPathRendererChain::DrawType drawType,
1612 GrPathRendererChain::StencilSupport* stencilSupport) {
1613
bsalomon@google.com30085192011-08-19 15:42:31 +00001614 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001615 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001616 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001617
joshualitt9853cce2014-11-17 14:22:48 -08001618 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
1619 drawState,
1620 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001621 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001622 drawType,
1623 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001624
1625 if (NULL == pr && allowSW) {
1626 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001627 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001628 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001629 pr = fSoftwarePathRenderer;
1630 }
1631
1632 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001633}
1634
bsalomon@google.com27847de2011-02-22 20:59:41 +00001635////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001636bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1637 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001638}
1639
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001640int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1641 SkScalar dpi) const {
1642 if (!this->isConfigRenderable(config, true)) {
1643 return 0;
1644 }
1645 int chosenSampleCount = 0;
1646 if (fGpu->caps()->pathRenderingSupport()) {
1647 if (dpi >= 250.0f) {
1648 chosenSampleCount = 4;
1649 } else {
1650 chosenSampleCount = 16;
1651 }
1652 }
1653 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1654 chosenSampleCount : 0;
1655}
1656
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001657void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001658 SkASSERT(NULL == fDrawBuffer);
1659 SkASSERT(NULL == fDrawBufferVBAllocPool);
1660 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001661
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001662 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001663 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001665 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001666 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001667 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001668 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001669 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001670
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001671 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001672 fDrawBufferVBAllocPool,
1673 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001674}
1675
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001676GrDrawTarget* GrContext::getTextTarget() {
joshualitt5531d512014-12-17 15:50:11 -08001677 return this->prepareToDraw(NULL, NULL, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001678}
1679
1680const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1681 return fGpu->getQuadIndexBuffer();
1682}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001683
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001684namespace {
1685void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1686 GrConfigConversionEffect::PMConversion pmToUPM;
1687 GrConfigConversionEffect::PMConversion upmToPM;
1688 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1689 *pmToUPMValue = pmToUPM;
1690 *upmToPMValue = upmToPM;
1691}
1692}
1693
joshualittb0a8a372014-09-23 09:50:21 -07001694const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1695 bool swapRAndB,
1696 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001697 if (!fDidTestPMConversions) {
1698 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001699 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001700 }
1701 GrConfigConversionEffect::PMConversion pmToUPM =
1702 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1703 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001704 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001705 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001706 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001707 }
1708}
1709
joshualittb0a8a372014-09-23 09:50:21 -07001710const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1711 bool swapRAndB,
1712 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001713 if (!fDidTestPMConversions) {
1714 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001715 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001716 }
1717 GrConfigConversionEffect::PMConversion upmToPM =
1718 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1719 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001720 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001721 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001722 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001723 }
1724}
1725
bsalomon6d3fe022014-07-25 08:35:45 -07001726void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
bsalomon71cb0c22014-11-14 12:10:14 -08001727 resource->cacheAccess().setContentKey(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001728}
1729
bsalomon6d3fe022014-07-25 08:35:45 -07001730GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
bsalomon71cb0c22014-11-14 12:10:14 -08001731 return fResourceCache2->findAndRefContentResource(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001732}
1733
egdanielbbcb38d2014-06-19 10:19:29 -07001734void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1735 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001736 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001737 fDrawBuffer->addGpuTraceMarker(marker);
1738 }
1739}
1740
1741void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1742 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001743 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001744 fDrawBuffer->removeGpuTraceMarker(marker);
1745 }
1746}
1747
bsalomon@google.comc4364992011-11-07 15:54:49 +00001748///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001749#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001750void GrContext::printCacheStats() const {
bsalomon71cb0c22014-11-14 12:10:14 -08001751 fResourceCache2->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001752}
1753#endif
robertphillips754f4e92014-09-18 13:52:08 -07001754
1755#if GR_GPU_STATS
1756const GrContext::GPUStats* GrContext::gpuStats() const {
1757 return fGpu->gpuStats();
1758}
1759#endif
1760