blob: 2dc43bb85c7e786c4617d24c0617b592808098a2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrMorphologyEffect.h"
13#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000014#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000033#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000035#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000036
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000037#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000038
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000039#define MAX_BLUR_SIGMA 4.0f
40
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041// When we're using coverage AA but the blend is incompatible (given gpu
42// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000043#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000044
reed@google.com4b2d3f32012-05-15 18:05:50 +000045#if GR_DEBUG
46 // change this to a 1 to see notifications when partial coverage fails
47 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
48#else
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#endif
51
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000052static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
53static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
bsalomon@google.com60361492012-03-15 17:47:06 +000055static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000056static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
57
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000058// path rendering is the only thing we defer today that uses non-static indices
59static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
60static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
63
bsalomon@google.com05ef5102011-05-02 21:14:59 +000064GrContext* GrContext::Create(GrEngine engine,
65 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000066 GrContext* ctx = NULL;
67 GrGpu* fGpu = GrGpu::Create(engine, context3D);
68 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000069 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000070 fGpu->unref();
71 }
72 return ctx;
73}
74
bsalomon@google.comc0af3172012-06-15 14:10:09 +000075namespace {
76void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000077 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000078}
79void DeleteThreadInstanceCount(void* v) {
80 delete reinterpret_cast<int*>(v);
81}
82#define THREAD_INSTANCE_COUNT \
83 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
84 DeleteThreadInstanceCount)))
85
86}
87
88int GrContext::GetThreadInstanceCount() {
89 return THREAD_INSTANCE_COUNT;
90}
91
bsalomon@google.com27847de2011-02-22 20:59:41 +000092GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000094
95 // Since the gpu can hold scratch textures, give it a chance to let go
96 // of them before freeing the texture cache
97 fGpu->purgeResources();
98
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 delete fTextureCache;
100 delete fFontCache;
101 delete fDrawBuffer;
102 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000103 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000104
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000105 fAARectRenderer->unref();
106
bsalomon@google.com205d4602011-04-25 12:43:45 +0000107 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000108 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000109 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000110 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000111
112 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113}
114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000116 contextDestroyed();
117 this->setupDrawBuffer();
118}
119
120void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 // abandon first to so destructors
122 // don't try to free the resources in the API.
123 fGpu->abandonResources();
124
bsalomon@google.com30085192011-08-19 15:42:31 +0000125 // a path renderer may be holding onto resources that
126 // are now unusable
127 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000128 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBuffer;
131 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferVBAllocPool;
134 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 delete fDrawBufferIBAllocPool;
137 fDrawBufferIBAllocPool = NULL;
138
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000139 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 fTextureCache->removeAll();
142 fFontCache->freeAll();
143 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144}
145
146void GrContext::resetContext() {
147 fGpu->markContextDirty();
148}
149
150void GrContext::freeGpuResources() {
151 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000152
153 fGpu->purgeResources();
154
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155 fAARectRenderer->reset();
156
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157 fTextureCache->removeAll();
158 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000159 // a path renderer may be holding onto resources
160 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000161 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162}
163
twiz@google.com05e70242012-01-27 19:12:00 +0000164size_t GrContext::getGpuTextureCacheBytes() const {
165 return fTextureCache->getCachedResourceBytes();
166}
167
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000168////////////////////////////////////////////////////////////////////////////////
169
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000170GrTexture* GrContext::TextureCacheEntry::texture() const {
171 if (NULL == fEntry) {
172 return NULL;
173 } else {
174 return (GrTexture*) fEntry->resource();
175 }
176}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000177
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000178namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000179
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000180void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000181 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
182 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
183 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
184 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000185}
186
bsalomon@google.comb505a122012-05-31 18:40:36 +0000187float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000188 *scaleFactor = 1;
189 while (sigma > MAX_BLUR_SIGMA) {
190 *scaleFactor *= 2;
191 sigma *= 0.5f;
192 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000193 *radius = static_cast<int>(ceilf(sigma * 3.0f));
194 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000195 return sigma;
196}
197
198void apply_morphology(GrGpu* gpu,
199 GrTexture* texture,
200 const SkRect& rect,
201 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000202 GrContext::MorphologyType morphType,
203 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000204
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000205 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
206 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000207 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000208 drawState->setRenderTarget(target);
209 GrMatrix sampleM;
210 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000211 drawState->sampler(0)->reset(sampleM);
212 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000213 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000214 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000215 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000216}
217
bsalomon@google.comb505a122012-05-31 18:40:36 +0000218void convolve_gaussian(GrGpu* gpu,
219 GrTexture* texture,
220 const SkRect& rect,
221 float sigma,
222 int radius,
223 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000224 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
225 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000227 drawState->setRenderTarget(target);
228 GrMatrix sampleM;
229 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000230 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000231 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000232 (texture, direction, radius,
233 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000235 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000236}
237
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000238}
239
bsalomon@google.comb8670992012-07-25 21:27:09 +0000240GrContext::TextureCacheEntry GrContext::findAndLockTexture(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000241 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000242 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000243 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000244 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
245 GrResourceCache::kNested_LockType));
246}
247
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000248bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000249 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000250 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000251 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000252 return fTextureCache->hasKey(resourceKey);
253}
254
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000255GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000256 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000257
258 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
259 sb->height(),
260 sb->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000261 return fTextureCache->createAndLock(resourceKey, sb);
262}
263
264GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
265 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000266 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
267 height,
268 sampleCnt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000269 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
270 GrResourceCache::kSingle_LockType);
271 if (NULL != entry) {
272 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
273 return sb;
274 } else {
275 return NULL;
276 }
277}
278
279void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000280 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000281 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000282}
283
284static void stretchImage(void* dst,
285 int dstW,
286 int dstH,
287 void* src,
288 int srcW,
289 int srcH,
290 int bpp) {
291 GrFixed dx = (srcW << 16) / dstW;
292 GrFixed dy = (srcH << 16) / dstH;
293
294 GrFixed y = dy >> 1;
295
296 int dstXLimit = dstW*bpp;
297 for (int j = 0; j < dstH; ++j) {
298 GrFixed x = dx >> 1;
299 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
300 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
301 for (int i = 0; i < dstXLimit; i += bpp) {
302 memcpy((uint8_t*) dstRow + i,
303 (uint8_t*) srcRow + (x>>16)*bpp,
304 bpp);
305 x += dx;
306 }
307 y += dy;
308 }
309}
310
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000311GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000312 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000313 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000314 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000315 void* srcData,
316 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000317 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000318
319#if GR_DUMP_TEXTURE_UPLOAD
320 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
321#endif
322
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000323 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000324
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000325 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000326
327 if (GrTexture::NeedsResizing(resourceKey)) {
328 // The desired texture is NPOT and tiled but that isn't supported by
329 // the current hardware. Resize the texture to be a POT
bsalomon@google.comb8670992012-07-25 21:27:09 +0000330 GrAssert(NULL != params);
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000331 TextureCacheEntry clampEntry = this->findAndLockTexture(desc, cacheData, NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000332
333 if (NULL == clampEntry.texture()) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000334 clampEntry = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000335 GrAssert(NULL != clampEntry.texture());
336 if (NULL == clampEntry.texture()) {
337 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000338 }
339 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000340 GrTextureDesc rtDesc = desc;
341 rtDesc.fFlags = rtDesc.fFlags |
342 kRenderTarget_GrTextureFlagBit |
343 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000344 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
345 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000346
347 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
348
349 if (NULL != texture) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000350 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000351 GrDrawState* drawState = fGpu->drawState();
352 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000353
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000354 // if filtering is not desired then we want to ensure all
355 // texels in the resampled image are copies of texels from
356 // the original.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000357 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
358 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000359 drawState->createTextureEffect(0, clampEntry.texture());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000360
361 static const GrVertexLayout layout =
362 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
363 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
364
365 if (arg.succeeded()) {
366 GrPoint* verts = (GrPoint*) arg.vertices();
367 verts[0].setIRectFan(0, 0,
368 texture->width(),
369 texture->height(),
370 2*sizeof(GrPoint));
371 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000372 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000373 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000374 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000375 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000376 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000377 } else {
378 // TODO: Our CPU stretch doesn't filter. But we create separate
379 // stretched textures when the sampler state is either filtered or
380 // not. Either implement filtered stretch blit on CPU or just create
381 // one when FBO case fails.
382
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000383 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000384 // no longer need to clamp at min RT size.
385 rtDesc.fWidth = GrNextPow2(desc.fWidth);
386 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000387 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000388 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389 rtDesc.fWidth *
390 rtDesc.fHeight);
391 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
392 srcData, desc.fWidth, desc.fHeight, bpp);
393
394 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
395
396 GrTexture* texture = fGpu->createTexture(rtDesc,
397 stretchedPixels.get(),
398 stretchedRowBytes);
399 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000400 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000402 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403
404 } else {
405 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
406 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000407 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408 }
409 }
410 return entry;
411}
412
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000413GrContext::TextureCacheEntry GrContext::lockScratchTexture(
414 const GrTextureDesc& inDesc,
415 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000416 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000417 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000418
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000419 if (kExact_ScratchTexMatch != match) {
420 // bin by pow2 with a reasonable min
421 static const int MIN_SIZE = 256;
422 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
423 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
424 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000425
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000426 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427 int origWidth = desc.fWidth;
428 int origHeight = desc.fHeight;
429 bool doubledW = false;
430 bool doubledH = false;
431
432 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000433 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000434 entry = fTextureCache->findAndLock(key,
435 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000436 // if we miss, relax the fit of the flags...
437 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000438 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000439 break;
440 }
441 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
442 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
443 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
444 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
445 } else if (!doubledW) {
446 desc.fFlags = inDesc.fFlags;
447 desc.fWidth *= 2;
448 doubledW = true;
449 } else if (!doubledH) {
450 desc.fFlags = inDesc.fFlags;
451 desc.fWidth = origWidth;
452 desc.fHeight *= 2;
453 doubledH = true;
454 } else {
455 break;
456 }
457
458 } while (true);
459
460 if (NULL == entry) {
461 desc.fFlags = inDesc.fFlags;
462 desc.fWidth = origWidth;
463 desc.fHeight = origHeight;
464 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
465 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000466 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000467 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000468 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000469 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000470 entry = fTextureCache->createAndLock(key, texture);
471 }
472 }
473
474 // If the caller gives us the same desc/sampler twice we don't want
475 // to return the same texture the second time (unless it was previously
476 // released). So we detach the entry from the cache and reattach at release.
477 if (NULL != entry) {
478 fTextureCache->detach(entry);
479 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000480 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000481}
482
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000483void GrContext::addExistingTextureToCache(GrTexture* texture) {
484
485 if (NULL == texture) {
486 return;
487 }
488
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000489 // 'texture' is a scratch texture returning to the fold
490 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
491
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000492 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
493 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000494 cacheData,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000495 true);
496 fTextureCache->attach(key, texture);
497}
498
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000499void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000500 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000501 // If this is a scratch texture we detached it from the cache
502 // while it was locked (to avoid two callers simultaneously getting
503 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000504 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000505 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000506 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000507 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000508 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000509}
510
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000511void GrContext::freeEntry(TextureCacheEntry entry) {
512 ASSERT_OWNED_RESOURCE(entry.texture());
513
514 fTextureCache->freeEntry(entry.cacheEntry());
515}
516
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000517GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518 void* srcData,
519 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000520 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000521 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522}
523
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000524void GrContext::getTextureCacheLimits(int* maxTextures,
525 size_t* maxTextureBytes) const {
526 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000527}
528
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000529void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
530 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000531}
532
bsalomon@google.com91958362011-06-13 17:58:13 +0000533int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000534 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000535}
536
537int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000538 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539}
540
541///////////////////////////////////////////////////////////////////////////////
542
bsalomon@google.come269f212011-11-07 13:29:52 +0000543GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
544 return fGpu->createPlatformTexture(desc);
545}
546
547GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
548 return fGpu->createPlatformRenderTarget(desc);
549}
550
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000551///////////////////////////////////////////////////////////////////////////////
552
bsalomon@google.comb8670992012-07-25 21:27:09 +0000553bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000554 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000555 const GrDrawTarget::Caps& caps = fGpu->getCaps();
556 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557 return false;
558 }
559
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
561
562 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000563 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000564 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565 return false;
566 }
567 }
568 return true;
569}
570
571////////////////////////////////////////////////////////////////////////////////
572
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000573const GrClipData* GrContext::getClip() const {
574 return fGpu->getClip();
575}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000576
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000577void GrContext::setClip(const GrClipData* clipData) {
578 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000579 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580}
581
bsalomon@google.com27847de2011-02-22 20:59:41 +0000582////////////////////////////////////////////////////////////////////////////////
583
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000584void GrContext::clear(const GrIRect* rect,
585 const GrColor color,
586 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000587 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000588 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000589}
590
591void GrContext::drawPaint(const GrPaint& paint) {
592 // set rect to be big enough to fill the space, but not super-huge, so we
593 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000594 GrRect r;
595 r.setLTRB(0, 0,
596 GrIntToScalar(getRenderTarget()->width()),
597 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000598 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000599 SkTLazy<GrPaint> tmpPaint;
600 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000601 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000602
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000603 // We attempt to map r by the inverse matrix and draw that. mapRect will
604 // map the four corners and bound them with a new rect. This will not
605 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000606 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000607 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000608 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000609 return;
610 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000611 inverse.mapRect(&r);
612 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000613 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000614 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000615 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000616 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
617 GrPrintf("Could not invert matrix\n");
618 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000620 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000622 // by definition this fills the entire clip, no need for AA
623 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000624 if (!tmpPaint.isValid()) {
625 tmpPaint.set(paint);
626 p = tmpPaint.get();
627 }
628 GrAssert(p == tmpPaint.get());
629 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000630 }
631 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632}
633
bsalomon@google.com205d4602011-04-25 12:43:45 +0000634////////////////////////////////////////////////////////////////////////////////
635
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000636namespace {
637inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
638 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
639}
640}
641
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000642////////////////////////////////////////////////////////////////////////////////
643
bsalomon@google.com27847de2011-02-22 20:59:41 +0000644/* create a triangle strip that strokes the specified triangle. There are 8
645 unique vertices, but we repreat the last 2 to close up. Alternatively we
646 could use an indices array, and then only send 8 verts, but not sure that
647 would be faster.
648 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000649static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650 GrScalar width) {
651 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000652 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653
654 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
655 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
656 verts[2].set(rect.fRight - rad, rect.fTop + rad);
657 verts[3].set(rect.fRight + rad, rect.fTop - rad);
658 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
659 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
660 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
661 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
662 verts[8] = verts[0];
663 verts[9] = verts[1];
664}
665
reed@google.com20efde72011-05-09 17:00:02 +0000666/**
667 * Returns true if the rects edges are integer-aligned.
668 */
669static bool isIRect(const GrRect& r) {
670 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
671 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
672}
673
bsalomon@google.com205d4602011-04-25 12:43:45 +0000674static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000675 const GrRect& rect,
676 GrScalar width,
677 const GrMatrix* matrix,
678 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000679 GrRect* devRect,
680 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000681 // we use a simple coverage ramp to do aa on axis-aligned rects
682 // we check if the rect will be axis-aligned, and the rect won't land on
683 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000684
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 // we are keeping around the "tweak the alpha" trick because
686 // it is our only hope for the fixed-pipe implementation.
687 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000688 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000689 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000690 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000691 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000692#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000693 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000694#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000696 } else {
697 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000698 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 const GrDrawState& drawState = target->getDrawState();
701 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702 return false;
703 }
704
bsalomon@google.com471d4712011-08-23 15:45:25 +0000705 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000706 return false;
707 }
708
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000709 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000710 return false;
711 }
712
713 if (NULL != matrix &&
714 !matrix->preservesAxisAlignment()) {
715 return false;
716 }
717
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000718 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 if (NULL != matrix) {
720 combinedMatrix->preConcat(*matrix);
721 GrAssert(combinedMatrix->preservesAxisAlignment());
722 }
723
724 combinedMatrix->mapRect(devRect, rect);
725 devRect->sort();
726
727 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000728 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729 } else {
730 return true;
731 }
732}
733
bsalomon@google.com27847de2011-02-22 20:59:41 +0000734void GrContext::drawRect(const GrPaint& paint,
735 const GrRect& rect,
736 GrScalar width,
737 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000738 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000739
740 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000741 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000742
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 GrRect devRect = rect;
744 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000745 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000746 bool needAA = paint.fAntiAlias &&
747 !this->getRenderTarget()->isMultisampled();
748 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
749 &combinedMatrix, &devRect,
750 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751
752 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000753 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
754 if (!adcd.succeeded()) {
755 return;
756 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757 if (width >= 0) {
758 GrVec strokeSize;;
759 if (width > 0) {
760 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000761 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000762 strokeSize.setAbs(strokeSize);
763 } else {
764 strokeSize.set(GR_Scalar1, GR_Scalar1);
765 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000766 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
767 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000769 fAARectRenderer->fillAARect(this->getGpu(), target,
770 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771 }
772 return;
773 }
774
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775 if (width >= 0) {
776 // TODO: consider making static vertex buffers for these cases.
777 // Hairline could be done by just adding closing vertex to
778 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000779
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000781 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782
783 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000784 GrPrintf("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;
790 GrPoint* vertex = geo.positions();
791
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
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000807 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000809 GrDrawState* drawState = target->drawState();
810 avmr.set(drawState);
811 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000812 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000813 }
814
815 target->drawNonIndexed(primType, 0, vertCount);
816 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000817#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000818 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
819 if (NULL == sqVB) {
820 GrPrintf("Failed to create static rect vb.\n");
821 return;
822 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000823 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 GrDrawState* drawState = target->drawState();
825 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000826 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000827 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000828 0, rect.height(), rect.fTop,
829 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830
831 if (NULL != matrix) {
832 m.postConcat(*matrix);
833 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000835 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000836
bsalomon@google.com47059542012-06-06 20:51:20 +0000837 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000838#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000839 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841 }
842}
843
844void GrContext::drawRectToRect(const GrPaint& paint,
845 const GrRect& dstRect,
846 const GrRect& srcRect,
847 const GrMatrix* dstMatrix,
848 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000849 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000851 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000852 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853 drawRect(paint, dstRect, -1, dstMatrix);
854 return;
855 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000856
bsalomon@google.com27847de2011-02-22 20:59:41 +0000857 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
858
859#if GR_STATIC_RECT_VB
860 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000861 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000862 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000863 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000864
865 GrMatrix m;
866
867 m.setAll(dstRect.width(), 0, dstRect.fLeft,
868 0, dstRect.height(), dstRect.fTop,
869 0, 0, GrMatrix::I()[8]);
870 if (NULL != dstMatrix) {
871 m.postConcat(*dstMatrix);
872 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000873 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000874
bsalomon@google.come3d32162012-07-20 13:37:06 +0000875 // we explicitly setup the correct coords for the first stage. The others
876 // must know about the view matrix change.
877 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
878 if (drawState->isStageEnabled(s)) {
879 drawState->sampler(s)->preConcatMatrix(m);
880 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000881 }
882
bsalomon@google.com27847de2011-02-22 20:59:41 +0000883 m.setAll(srcRect.width(), 0, srcRect.fLeft,
884 0, srcRect.height(), srcRect.fTop,
885 0, 0, GrMatrix::I()[8]);
886 if (NULL != srcMatrix) {
887 m.postConcat(*srcMatrix);
888 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000889 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000890
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000891 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
892 if (NULL == sqVB) {
893 GrPrintf("Failed to create static rect vb.\n");
894 return;
895 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000896 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000897 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898#else
899
900 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000901#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000902 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000903#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
905#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000906 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907
tomhudson@google.com93813632011-10-27 20:21:16 +0000908 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
909 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910 srcRects[0] = &srcRect;
911 srcMatrices[0] = srcMatrix;
912
bsalomon@google.come3d32162012-07-20 13:37:06 +0000913 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914#endif
915}
916
917void GrContext::drawVertices(const GrPaint& paint,
918 GrPrimitiveType primitiveType,
919 int vertexCount,
920 const GrPoint positions[],
921 const GrPoint texCoords[],
922 const GrColor colors[],
923 const uint16_t indices[],
924 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000925 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926
927 GrDrawTarget::AutoReleaseGeometry geo;
928
929 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000930 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931
bsalomon@google.come3d32162012-07-20 13:37:06 +0000932 GrVertexLayout layout = 0;
933 if (NULL != texCoords) {
934 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
935 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936 if (NULL != colors) {
937 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000938 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000939 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000940
941 if (sizeof(GrPoint) != vertexSize) {
942 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000943 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944 return;
945 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000946 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000947 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000948 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
949 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000950 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000951 NULL,
952 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000953 void* curVertex = geo.vertices();
954
955 for (int i = 0; i < vertexCount; ++i) {
956 *((GrPoint*)curVertex) = positions[i];
957
958 if (texOffsets[0] > 0) {
959 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
960 }
961 if (colorOffset > 0) {
962 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
963 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000964 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965 }
966 } else {
967 target->setVertexSourceToArray(layout, positions, vertexCount);
968 }
969
bsalomon@google.com91958362011-06-13 17:58:13 +0000970 // we don't currently apply offscreen AA to this path. Need improved
971 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000972
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000973 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000974 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000975 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000976 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000977 target->drawNonIndexed(primitiveType, 0, vertexCount);
978 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000979}
980
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000981///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000982namespace {
983
bsalomon@google.com93c96602012-04-27 13:05:21 +0000984struct CircleVertex {
985 GrPoint fPos;
986 GrPoint fCenter;
987 GrScalar fOuterRadius;
988 GrScalar fInnerRadius;
989};
990
991/* Returns true if will map a circle to another circle. This can be true
992 * if the matrix only includes square-scale, rotation, translation.
993 */
994inline bool isSimilarityTransformation(const SkMatrix& matrix,
995 SkScalar tol = SK_ScalarNearlyZero) {
996 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
997 return true;
998 }
999 if (matrix.hasPerspective()) {
1000 return false;
1001 }
1002
1003 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1004 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1005 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1006 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1007
1008 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1009 return false;
1010 }
1011
1012 // it has scales or skews, but it could also be rotation, check it out.
1013 SkVector vec[2];
1014 vec[0].set(mx, sx);
1015 vec[1].set(sy, my);
1016
1017 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1018 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1019 SkScalarSquare(tol));
1020}
1021
1022}
1023
1024// TODO: strokeWidth can't be larger than zero right now.
1025// It will be fixed when drawPath() can handle strokes.
1026void GrContext::drawOval(const GrPaint& paint,
1027 const GrRect& rect,
1028 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001029 GrAssert(strokeWidth <= 0);
1030 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001031 !paint.fAntiAlias ||
1032 rect.height() != rect.width()) {
1033 SkPath path;
1034 path.addOval(rect);
1035 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001036 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001037 this->internalDrawPath(paint, path, fill, NULL);
1038 return;
1039 }
1040
bsalomon@google.com0982d352012-07-31 15:33:25 +00001041 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1042 kUnbuffered_DrawCategory;
1043 GrDrawTarget* target = this->prepareToDraw(paint, category);
1044 GrDrawState* drawState = target->drawState();
1045 GrDrawState::AutoStageDisable atr(fDrawState);
1046 const GrMatrix vm = drawState->getViewMatrix();
1047
bsalomon@google.com93c96602012-04-27 13:05:21 +00001048 const GrRenderTarget* rt = drawState->getRenderTarget();
1049 if (NULL == rt) {
1050 return;
1051 }
1052
bsalomon@google.come3d32162012-07-20 13:37:06 +00001053 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1054 if (!adcd.succeeded()) {
1055 return;
1056 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001057
bsalomon@google.come3d32162012-07-20 13:37:06 +00001058 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001059 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1060
1061 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1062 GrScalar radius = SkScalarHalf(rect.width());
1063
1064 vm.mapPoints(&center, 1);
1065 radius = vm.mapRadius(radius);
1066
1067 GrScalar outerRadius = radius;
1068 GrScalar innerRadius = 0;
1069 SkScalar halfWidth = 0;
1070 if (strokeWidth == 0) {
1071 halfWidth = SkScalarHalf(SK_Scalar1);
1072
1073 outerRadius += halfWidth;
1074 innerRadius = SkMaxScalar(0, radius - halfWidth);
1075 }
1076
1077 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1078 if (!geo.succeeded()) {
1079 GrPrintf("Failed to get space for vertices!\n");
1080 return;
1081 }
1082
1083 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1084
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001085 // The fragment shader will extend the radius out half a pixel
1086 // to antialias. Expand the drawn rect here so all the pixels
1087 // will be captured.
1088 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1089 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1090 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1091 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001092
1093 verts[0].fPos = SkPoint::Make(L, T);
1094 verts[1].fPos = SkPoint::Make(R, T);
1095 verts[2].fPos = SkPoint::Make(L, B);
1096 verts[3].fPos = SkPoint::Make(R, B);
1097
1098 for (int i = 0; i < 4; ++i) {
1099 // this goes to fragment shader, it should be in y-points-up space.
1100 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1101
1102 verts[i].fOuterRadius = outerRadius;
1103 verts[i].fInnerRadius = innerRadius;
1104 }
1105
1106 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001107 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001108}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001109
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001110void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001111 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001112
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001113 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001114 if (GrIsFillInverted(fill)) {
1115 this->drawPaint(paint);
1116 }
1117 return;
1118 }
1119
bsalomon@google.com93c96602012-04-27 13:05:21 +00001120 SkRect ovalRect;
1121 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1122 if (translate) {
1123 ovalRect.offset(*translate);
1124 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001125 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001126 this->drawOval(paint, ovalRect, width);
1127 return;
1128 }
1129
1130 internalDrawPath(paint, path, fill, translate);
1131}
1132
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001133void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001134 GrPathFill fill, const GrPoint* translate) {
1135
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001136 // Note that below we may sw-rasterize the path into a scratch texture.
1137 // Scratch textures can be recycled after they are returned to the texture
1138 // cache. This presents a potential hazard for buffered drawing. However,
1139 // the writePixels that uploads to the scratch will perform a flush so we're
1140 // OK.
1141 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1142 kUnbuffered_DrawCategory;
1143 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001144 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001145
bsalomon@google.com289533a2011-10-27 12:34:25 +00001146 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1147
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001148 // An Assumption here is that path renderer would use some form of tweaking
1149 // the src color (either the input alpha or in the frag shader) to implement
1150 // aa. If we have some future driver-mojo path AA that can do the right
1151 // thing WRT to the blend then we'll need some query on the PR.
1152 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001153#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001154 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001155#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001156 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001157 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001158
robertphillips@google.com72176b22012-05-23 13:19:12 +00001159 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001160 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001161#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001162 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001163#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001164 return;
1165 }
1166
bsalomon@google.come3d32162012-07-20 13:37:06 +00001167 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001168}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001169
bsalomon@google.com27847de2011-02-22 20:59:41 +00001170////////////////////////////////////////////////////////////////////////////////
1171
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001172void GrContext::flush(int flagsBitfield) {
1173 if (kDiscard_FlushBit & flagsBitfield) {
1174 fDrawBuffer->reset();
1175 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001176 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001177 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001178 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001179 fGpu->forceRenderTargetFlush();
1180 }
1181}
1182
bsalomon@google.com27847de2011-02-22 20:59:41 +00001183void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001184 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001185 // With addition of the AA clip path, flushing the draw buffer can
1186 // result in the generation of an AA clip mask. During this
1187 // process the SW path renderer may be invoked which recusively
1188 // calls this method (via internalWriteTexturePixels) creating
1189 // infinite recursion
1190 GrInOrderDrawBuffer* temp = fDrawBuffer;
1191 fDrawBuffer = NULL;
1192
1193 temp->flushTo(fGpu);
1194
1195 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001196 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001197}
1198
bsalomon@google.com6f379512011-11-16 20:36:03 +00001199void GrContext::internalWriteTexturePixels(GrTexture* texture,
1200 int left, int top,
1201 int width, int height,
1202 GrPixelConfig config,
1203 const void* buffer,
1204 size_t rowBytes,
1205 uint32_t flags) {
1206 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001207 ASSERT_OWNED_RESOURCE(texture);
1208
bsalomon@google.com6f379512011-11-16 20:36:03 +00001209 if (!(kDontFlush_PixelOpsFlag & flags)) {
1210 this->flush();
1211 }
1212 // TODO: use scratch texture to perform conversion
1213 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1214 GrPixelConfigIsUnpremultiplied(config)) {
1215 return;
1216 }
1217
1218 fGpu->writeTexturePixels(texture, left, top, width, height,
1219 config, buffer, rowBytes);
1220}
1221
1222bool GrContext::internalReadTexturePixels(GrTexture* texture,
1223 int left, int top,
1224 int width, int height,
1225 GrPixelConfig config,
1226 void* buffer,
1227 size_t rowBytes,
1228 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001229 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001230 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001231
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001232 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001233 GrRenderTarget* target = texture->asRenderTarget();
1234 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001235 return this->internalReadRenderTargetPixels(target,
1236 left, top, width, height,
1237 config, buffer, rowBytes,
1238 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001239 } else {
1240 return false;
1241 }
1242}
1243
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001244#include "SkConfig8888.h"
1245
1246namespace {
1247/**
1248 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1249 * formats are representable as Config8888 and so the function returns false
1250 * if the GrPixelConfig has no equivalent Config8888.
1251 */
1252bool grconfig_to_config8888(GrPixelConfig config,
1253 SkCanvas::Config8888* config8888) {
1254 switch (config) {
1255 case kRGBA_8888_PM_GrPixelConfig:
1256 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1257 return true;
1258 case kRGBA_8888_UPM_GrPixelConfig:
1259 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1260 return true;
1261 case kBGRA_8888_PM_GrPixelConfig:
1262 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1263 return true;
1264 case kBGRA_8888_UPM_GrPixelConfig:
1265 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1266 return true;
1267 default:
1268 return false;
1269 }
1270}
1271}
1272
bsalomon@google.com6f379512011-11-16 20:36:03 +00001273bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1274 int left, int top,
1275 int width, int height,
1276 GrPixelConfig config,
1277 void* buffer,
1278 size_t rowBytes,
1279 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001280 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001281 ASSERT_OWNED_RESOURCE(target);
1282
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001283 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001284 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001285 if (NULL == target) {
1286 return false;
1287 }
1288 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001289
bsalomon@google.com6f379512011-11-16 20:36:03 +00001290 if (!(kDontFlush_PixelOpsFlag & flags)) {
1291 this->flush();
1292 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001293
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001294 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1295 GrPixelConfigIsUnpremultiplied(config) &&
1296 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1297 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1298 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1299 !grconfig_to_config8888(config, &dstConfig8888)) {
1300 return false;
1301 }
1302 // do read back using target's own config
1303 this->internalReadRenderTargetPixels(target,
1304 left, top,
1305 width, height,
1306 target->config(),
1307 buffer, rowBytes,
1308 kDontFlush_PixelOpsFlag);
1309 // sw convert the pixels to unpremul config
1310 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1311 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1312 pixels, rowBytes, srcConfig8888,
1313 width, height);
1314 return true;
1315 }
1316
bsalomon@google.comc4364992011-11-07 15:54:49 +00001317 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001318 bool swapRAndB = NULL != src &&
1319 fGpu->preferredReadPixelsConfig(config) ==
1320 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001321
1322 bool flipY = NULL != src &&
1323 fGpu->readPixelsWillPayForYFlip(target, left, top,
1324 width, height, config,
1325 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001326 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1327 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001328
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001329 if (NULL == src && alphaConversion) {
1330 // we should fallback to cpu conversion here. This could happen when
1331 // we were given an external render target by the client that is not
1332 // also a texture (e.g. FBO 0 in GL)
1333 return false;
1334 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001335 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001336 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001337 if (flipY || swapRAndB || alphaConversion) {
1338 GrAssert(NULL != src);
1339 if (swapRAndB) {
1340 config = GrPixelConfigSwapRAndB(config);
1341 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001342 }
1343 // Make the scratch a render target because we don't have a robust
1344 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001345 GrTextureDesc desc;
1346 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1347 desc.fWidth = width;
1348 desc.fHeight = height;
1349 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001350
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001351 // When a full readback is faster than a partial we could always make
1352 // the scratch exactly match the passed rect. However, if we see many
1353 // different size rectangles we will trash our texture cache and pay the
1354 // cost of creating and destroying many textures. So, we only request
1355 // an exact match when the caller is reading an entire RT.
1356 ScratchTexMatch match = kApprox_ScratchTexMatch;
1357 if (0 == left &&
1358 0 == top &&
1359 target->width() == width &&
1360 target->height() == height &&
1361 fGpu->fullReadPixelsIsFasterThanPartial()) {
1362 match = kExact_ScratchTexMatch;
1363 }
1364 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001365 GrTexture* texture = ast.texture();
1366 if (!texture) {
1367 return false;
1368 }
1369 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001370 GrAssert(NULL != target);
1371
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001372 GrDrawTarget::AutoStateRestore asr(fGpu,
1373 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001374 GrDrawState* drawState = fGpu->drawState();
1375 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001376
bsalomon@google.comc4364992011-11-07 15:54:49 +00001377 GrMatrix matrix;
1378 if (flipY) {
1379 matrix.setTranslate(SK_Scalar1 * left,
1380 SK_Scalar1 * (top + height));
1381 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1382 } else {
1383 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1384 }
1385 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001386 drawState->sampler(0)->reset(matrix);
1387 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001388 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001389 GrRect rect;
1390 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001391 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001392 left = 0;
1393 top = 0;
1394 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001395 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001396 left, top, width, height,
1397 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001398}
1399
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001400void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1401 GrAssert(target);
1402 ASSERT_OWNED_RESOURCE(target);
1403 // In the future we may track whether there are any pending draws to this
1404 // target. We don't today so we always perform a flush. We don't promise
1405 // this to our clients, though.
1406 this->flush();
1407 fGpu->resolveRenderTarget(target);
1408}
1409
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001410void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1411 if (NULL == src || NULL == dst) {
1412 return;
1413 }
1414 ASSERT_OWNED_RESOURCE(src);
1415
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001416 // Writes pending to the source texture are not tracked, so a flush
1417 // is required to ensure that the copy captures the most recent contents
1418 // of the source texture. See similar behaviour in
1419 // GrContext::resolveRenderTarget.
1420 this->flush();
1421
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001422 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001423 GrDrawState* drawState = fGpu->drawState();
1424 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001425 GrMatrix sampleM;
1426 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001427 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001428 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001429 SkRect rect = SkRect::MakeXYWH(0, 0,
1430 SK_Scalar1 * src->width(),
1431 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001432 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001433}
1434
bsalomon@google.com6f379512011-11-16 20:36:03 +00001435void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1436 int left, int top,
1437 int width, int height,
1438 GrPixelConfig config,
1439 const void* buffer,
1440 size_t rowBytes,
1441 uint32_t flags) {
1442 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001443 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001444
1445 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001446 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001447 if (NULL == target) {
1448 return;
1449 }
1450 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001451
1452 // TODO: when underlying api has a direct way to do this we should use it
1453 // (e.g. glDrawPixels on desktop GL).
1454
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001455 // If the RT is also a texture and we don't have to do PM/UPM conversion
1456 // then take the texture path, which we expect to be at least as fast or
1457 // faster since it doesn't use an intermediate texture as we do below.
1458
1459#if !GR_MAC_BUILD
1460 // At least some drivers on the Mac get confused when glTexImage2D is called
1461 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1462 // determine what OS versions and/or HW is affected.
1463 if (NULL != target->asTexture() &&
1464 GrPixelConfigIsUnpremultiplied(target->config()) ==
1465 GrPixelConfigIsUnpremultiplied(config)) {
1466
1467 this->internalWriteTexturePixels(target->asTexture(),
1468 left, top, width, height,
1469 config, buffer, rowBytes, flags);
1470 return;
1471 }
1472#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001473 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1474 GrPixelConfigIsUnpremultiplied(config) &&
1475 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1476 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1477 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1478 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1479 return;
1480 }
1481 // allocate a tmp buffer and sw convert the pixels to premul
1482 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1483 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1484 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1485 src, rowBytes, srcConfig8888,
1486 width, height);
1487 // upload the already premul pixels
1488 this->internalWriteRenderTargetPixels(target,
1489 left, top,
1490 width, height,
1491 target->config(),
1492 tmpPixels, 4 * width, flags);
1493 return;
1494 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001495
1496 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1497 GrPixelConfigSwapRAndB(config);
1498 if (swapRAndB) {
1499 config = GrPixelConfigSwapRAndB(config);
1500 }
1501
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001502 GrTextureDesc desc;
1503 desc.fWidth = width;
1504 desc.fHeight = height;
1505 desc.fConfig = config;
1506
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001507 GrAutoScratchTexture ast(this, desc);
1508 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001509 if (NULL == texture) {
1510 return;
1511 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001512 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1513 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001514
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001515 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001516 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001517
1518 GrMatrix matrix;
1519 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001520 drawState->setViewMatrix(matrix);
1521 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001522
bsalomon@google.com5c638652011-07-18 19:31:59 +00001523 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001524 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001525 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001526 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001528 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001529 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001530 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001531 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1532 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001533 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001534 return;
1535 }
1536 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001537 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001538}
1539////////////////////////////////////////////////////////////////////////////////
1540
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001541void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001542 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001543
1544 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1545 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001546 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001547 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001548 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001549 }
1550
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001551 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001552
1553 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1554 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001555 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001556 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001557 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001558 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001559
1560 // disable all stages not accessible via the paint
1561 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001562 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001563 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001564
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001565 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001566
1567 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001568 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001570 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571 }
1572 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001573 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001575 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001576 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001577 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001578 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1579 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001580 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001582 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001583 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1584 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1585 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001586#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001587 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001588 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001589 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1590 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001591#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001592}
1593
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001594GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001595 DrawCategory category) {
1596 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001597 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598 fLastDrawCategory = category;
1599 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001601 GrDrawTarget* target = fGpu;
1602 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001603 case kUnbuffered_DrawCategory:
1604 target = fGpu;
1605 break;
1606 case kBuffered_DrawCategory:
1607 target = fDrawBuffer;
1608 fDrawBuffer->setClip(fGpu->getClip());
1609 break;
1610 default:
1611 GrCrash("Unexpected DrawCategory.");
1612 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001613 }
1614 return target;
1615}
1616
robertphillips@google.com72176b22012-05-23 13:19:12 +00001617/*
1618 * This method finds a path renderer that can draw the specified path on
1619 * the provided target.
1620 * Due to its expense, the software path renderer has split out so it can
1621 * can be individually allowed/disallowed via the "allowSW" boolean.
1622 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001623GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001624 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001625 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001626 bool antiAlias,
1627 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001628 if (NULL == fPathRendererChain) {
1629 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001630 SkNEW_ARGS(GrPathRendererChain,
1631 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001632 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001633
1634 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1635 target,
1636 antiAlias);
1637
1638 if (NULL == pr && allowSW) {
1639 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001640 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001641 }
1642
1643 pr = fSoftwarePathRenderer;
1644 }
1645
1646 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001647}
1648
bsalomon@google.com27847de2011-02-22 20:59:41 +00001649////////////////////////////////////////////////////////////////////////////////
1650
bsalomon@google.com27847de2011-02-22 20:59:41 +00001651void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001652 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001653 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001654 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001655 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001656 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001657}
1658
1659GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001660 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001661}
1662
1663const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001664 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001665}
1666
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001667bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1668 return fGpu->isConfigRenderable(config);
1669}
1670
bsalomon@google.com27847de2011-02-22 20:59:41 +00001671const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001672 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001673}
1674
1675void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001676 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001677}
1678
1679void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001680 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001681}
1682
1683static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1684 intptr_t mask = 1 << shift;
1685 if (pred) {
1686 bits |= mask;
1687 } else {
1688 bits &= ~mask;
1689 }
1690 return bits;
1691}
1692
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001693GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001694 ++THREAD_INSTANCE_COUNT;
1695
bsalomon@google.com27847de2011-02-22 20:59:41 +00001696 fGpu = gpu;
1697 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001698 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001699
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001700 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001701 fGpu->setDrawState(fDrawState);
1702
bsalomon@google.com30085192011-08-19 15:42:31 +00001703 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001704 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001705
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001706 fTextureCache = SkNEW_ARGS(GrResourceCache,
1707 (MAX_TEXTURE_CACHE_COUNT,
1708 MAX_TEXTURE_CACHE_BYTES));
1709 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001710
1711 fLastDrawCategory = kUnbuffered_DrawCategory;
1712
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001713 fDrawBuffer = NULL;
1714 fDrawBufferVBAllocPool = NULL;
1715 fDrawBufferIBAllocPool = NULL;
1716
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001717 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001718
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001719 this->setupDrawBuffer();
1720}
1721
1722void GrContext::setupDrawBuffer() {
1723
1724 GrAssert(NULL == fDrawBuffer);
1725 GrAssert(NULL == fDrawBufferVBAllocPool);
1726 GrAssert(NULL == fDrawBufferIBAllocPool);
1727
bsalomon@google.com92edd312012-04-04 21:40:21 +00001728#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001729 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001730 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001731 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001732 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001733 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001734 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001735 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001736 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001737
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001738 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001739 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001740 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001741#endif
1742
1743#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001744 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001745#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001746 if (fDrawBuffer) {
1747 fDrawBuffer->setAutoFlushTarget(fGpu);
1748 fDrawBuffer->setDrawState(fDrawState);
1749 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001750}
1751
bsalomon@google.com27847de2011-02-22 20:59:41 +00001752GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001754 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001756 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001757#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758}
1759
1760const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1761 return fGpu->getQuadIndexBuffer();
1762}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001763
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001764GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001765 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001766 const SkRect& rect,
1767 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001768 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001769 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001770 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001771 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001772 int scaleFactorX, radiusX;
1773 int scaleFactorY, radiusY;
1774 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1775 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001776
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001777 SkRect srcRect(rect);
1778 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1779 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001780 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1781 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001782
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001783 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001784
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001785 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1786 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1787 kAlpha_8_GrPixelConfig == srcTexture->config());
1788
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001789 GrTextureDesc desc;
1790 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1791 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1792 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1793 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001794
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001795 GrAutoScratchTexture temp1, temp2;
1796 GrTexture* dstTexture = temp1.set(this, desc);
1797 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001798
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001799 GrPaint paint;
1800 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001801 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001802
1803 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1804 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1805 srcTexture->height());
1806 this->setRenderTarget(dstTexture->asRenderTarget());
1807 SkRect dstRect(srcRect);
1808 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1809 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001810 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1811 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001812 this->drawRectToRect(paint, dstRect, srcRect);
1813 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001814 srcTexture = dstTexture;
1815 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001816 }
1817
robertphillips@google.com7a396332012-05-10 15:11:27 +00001818 SkIRect srcIRect;
1819 srcRect.roundOut(&srcIRect);
1820
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001821 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001822 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001823 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001824 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001825 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001826 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 this->clear(&clearRect, 0x0);
1828 }
1829
1830 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001831 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1832 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001833 srcTexture = dstTexture;
1834 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001835 }
1836
1837 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001839 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001840 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001841 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001842 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 this->clear(&clearRect, 0x0);
1844 }
1845
1846 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001847 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1848 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001849 srcTexture = dstTexture;
1850 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 }
1852
1853 if (scaleFactorX > 1 || scaleFactorY > 1) {
1854 // Clear one pixel to the right and below, to accommodate bilinear
1855 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001856 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1857 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001858 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001859 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1860 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861 this->clear(&clearRect, 0x0);
1862 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001863 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1865 srcTexture->height());
1866 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001867 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1868 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001870 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001871 this->drawRectToRect(paint, dstRect, srcRect);
1872 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001873 srcTexture = dstTexture;
1874 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 }
1876 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001877 if (srcTexture == temp1.texture()) {
1878 return temp1.detach();
1879 } else if (srcTexture == temp2.texture()) {
1880 return temp2.detach();
1881 } else {
1882 srcTexture->ref();
1883 return srcTexture;
1884 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001885}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001886
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001887GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1888 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001889 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001890 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001891 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001892 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001894
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001895 AutoMatrix avm(this, GrMatrix::I());
1896
1897 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001898 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001899 GrTextureDesc desc;
1900 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1901 desc.fWidth = SkScalarCeilToInt(rect.width());
1902 desc.fHeight = SkScalarCeilToInt(rect.height());
1903 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001905 GrAutoScratchTexture ast(this, desc);
1906 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001907 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1908 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001909 SkIRect clearRect = SkIRect::MakeXYWH(
1910 SkScalarFloorToInt(rect.fLeft),
1911 SkScalarFloorToInt(rect.fBottom),
1912 SkScalarFloorToInt(rect.width()),
1913 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001914 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001915 srcTexture->unref();
1916 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 }
1918 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001919 GrAutoScratchTexture ast(this, desc);
1920 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001921 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1922 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001923 srcTexture->unref();
1924 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 }
1926 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001927 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001928}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001929
1930///////////////////////////////////////////////////////////////////////////////