blob: a78bdf7becf1a2bfcdb144902263fd8d7bd6d40e [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"
17#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000018#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019#include "GrIndexBuffer.h"
20#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000022#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000024#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000025#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000026#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000027#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000028#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000029#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000030
reed@google.comfa35e3d2012-06-26 20:16:17 +000031SK_DEFINE_INST_COUNT(GrContext)
32SK_DEFINE_INST_COUNT(GrDrawState)
33
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000034#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000036#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000037
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000038#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000039
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000040#define MAX_BLUR_SIGMA 4.0f
41
bsalomon@google.comd46e2422011-09-23 17:40:07 +000042// When we're using coverage AA but the blend is incompatible (given gpu
43// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000044#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000045
reed@google.com4b2d3f32012-05-15 18:05:50 +000046#if GR_DEBUG
47 // change this to a 1 to see notifications when partial coverage fails
48 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
49#else
50 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
51#endif
52
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000053static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
54static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055
bsalomon@google.com60361492012-03-15 17:47:06 +000056static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
58
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000059// path rendering is the only thing we defer today that uses non-static indices
60static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
61static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000062
bsalomon@google.combc4b6542011-11-19 13:56:11 +000063#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
64
bsalomon@google.com05ef5102011-05-02 21:14:59 +000065GrContext* GrContext::Create(GrEngine engine,
66 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000067 GrContext* ctx = NULL;
68 GrGpu* fGpu = GrGpu::Create(engine, context3D);
69 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000070 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000071 fGpu->unref();
72 }
73 return ctx;
74}
75
bsalomon@google.comc0af3172012-06-15 14:10:09 +000076namespace {
77void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000078 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000079}
80void DeleteThreadInstanceCount(void* v) {
81 delete reinterpret_cast<int*>(v);
82}
83#define THREAD_INSTANCE_COUNT \
84 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
85 DeleteThreadInstanceCount)))
86
87}
88
89int GrContext::GetThreadInstanceCount() {
90 return THREAD_INSTANCE_COUNT;
91}
92
bsalomon@google.com27847de2011-02-22 20:59:41 +000093GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000095
96 // Since the gpu can hold scratch textures, give it a chance to let go
97 // of them before freeing the texture cache
98 fGpu->purgeResources();
99
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100 delete fTextureCache;
101 delete fFontCache;
102 delete fDrawBuffer;
103 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000104 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000105
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000106 fAARectRenderer->unref();
107
bsalomon@google.com205d4602011-04-25 12:43:45 +0000108 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000109 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000110 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000111 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000112
113 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000114}
115
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000116void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000117 contextDestroyed();
118 this->setupDrawBuffer();
119}
120
121void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000122 // abandon first to so destructors
123 // don't try to free the resources in the API.
124 fGpu->abandonResources();
125
bsalomon@google.com30085192011-08-19 15:42:31 +0000126 // a path renderer may be holding onto resources that
127 // are now unusable
128 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000129 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000130
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000131 delete fDrawBuffer;
132 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 delete fDrawBufferVBAllocPool;
135 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000136
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137 delete fDrawBufferIBAllocPool;
138 fDrawBufferIBAllocPool = NULL;
139
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000140 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000141
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000142 fTextureCache->removeAll();
143 fFontCache->freeAll();
144 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000145}
146
147void GrContext::resetContext() {
148 fGpu->markContextDirty();
149}
150
151void GrContext::freeGpuResources() {
152 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000153
154 fGpu->purgeResources();
155
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000156 fAARectRenderer->reset();
157
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158 fTextureCache->removeAll();
159 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000160 // a path renderer may be holding onto resources
161 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000162 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000163}
164
twiz@google.com05e70242012-01-27 19:12:00 +0000165size_t GrContext::getGpuTextureCacheBytes() const {
166 return fTextureCache->getCachedResourceBytes();
167}
168
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000169////////////////////////////////////////////////////////////////////////////////
170
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000171GrTexture* GrContext::TextureCacheEntry::texture() const {
172 if (NULL == fEntry) {
173 return NULL;
174 } else {
175 return (GrTexture*) fEntry->resource();
176 }
177}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000178
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000179namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000180
181// we should never have more than one stencil buffer with same combo of
182// (width,height,samplecount)
183void gen_stencil_key_values(int width, int height,
184 int sampleCnt, uint32_t v[4]) {
185 v[0] = width;
186 v[1] = height;
187 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000188 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000189}
190
191void gen_stencil_key_values(const GrStencilBuffer* sb,
192 uint32_t v[4]) {
193 gen_stencil_key_values(sb->width(), sb->height(),
194 sb->numSamples(), v);
195}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000196
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000197void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000198 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
199 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
200 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
201 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000202}
203
bsalomon@google.comb505a122012-05-31 18:40:36 +0000204float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000205 *scaleFactor = 1;
206 while (sigma > MAX_BLUR_SIGMA) {
207 *scaleFactor *= 2;
208 sigma *= 0.5f;
209 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000210 *radius = static_cast<int>(ceilf(sigma * 3.0f));
211 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000212 return sigma;
213}
214
215void apply_morphology(GrGpu* gpu,
216 GrTexture* texture,
217 const SkRect& rect,
218 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000219 GrContext::MorphologyType morphType,
220 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000222 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
223 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000224 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000225 drawState->setRenderTarget(target);
226 GrMatrix sampleM;
227 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000228 drawState->sampler(0)->reset(sampleM);
229 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000230 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000231 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000232 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000233}
234
bsalomon@google.comb505a122012-05-31 18:40:36 +0000235void convolve_gaussian(GrGpu* gpu,
236 GrTexture* texture,
237 const SkRect& rect,
238 float sigma,
239 int radius,
240 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000241 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
242 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000243 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000244 drawState->setRenderTarget(target);
245 GrMatrix sampleM;
246 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000247 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000248 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000249 (texture, direction, radius,
250 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000251 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000252 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000253}
254
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000255}
256
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000257GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000258 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000259 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000260 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000261 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
262 GrResourceCache::kNested_LockType));
263}
264
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000265bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000266 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000267 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000268 return fTextureCache->hasKey(resourceKey);
269}
270
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000271GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000272 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000273 uint32_t v[4];
274 gen_stencil_key_values(sb, v);
275 GrResourceKey resourceKey(v);
276 return fTextureCache->createAndLock(resourceKey, sb);
277}
278
279GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
280 int sampleCnt) {
281 uint32_t v[4];
282 gen_stencil_key_values(width, height, sampleCnt, v);
283 GrResourceKey resourceKey(v);
284 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
285 GrResourceCache::kSingle_LockType);
286 if (NULL != entry) {
287 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
288 return sb;
289 } else {
290 return NULL;
291 }
292}
293
294void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000295 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000296 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000297}
298
299static void stretchImage(void* dst,
300 int dstW,
301 int dstH,
302 void* src,
303 int srcW,
304 int srcH,
305 int bpp) {
306 GrFixed dx = (srcW << 16) / dstW;
307 GrFixed dy = (srcH << 16) / dstH;
308
309 GrFixed y = dy >> 1;
310
311 int dstXLimit = dstW*bpp;
312 for (int j = 0; j < dstH; ++j) {
313 GrFixed x = dx >> 1;
314 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
315 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
316 for (int i = 0; i < dstXLimit; i += bpp) {
317 memcpy((uint8_t*) dstRow + i,
318 (uint8_t*) srcRow + (x>>16)*bpp,
319 bpp);
320 x += dx;
321 }
322 y += dy;
323 }
324}
325
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000326GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000327 const GrSamplerState* sampler,
328 const GrTextureDesc& desc,
329 void* srcData,
330 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000331 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000332
333#if GR_DUMP_TEXTURE_UPLOAD
334 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
335#endif
336
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000337 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000338
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000339 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000340 desc, false);
341
342 if (GrTexture::NeedsResizing(resourceKey)) {
343 // The desired texture is NPOT and tiled but that isn't supported by
344 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000346 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000347 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000348
349 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000350 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000351 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000352 GrAssert(NULL != clampEntry.texture());
353 if (NULL == clampEntry.texture()) {
354 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355 }
356 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000357 GrTextureDesc rtDesc = desc;
358 rtDesc.fFlags = rtDesc.fFlags |
359 kRenderTarget_GrTextureFlagBit |
360 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000361 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
362 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000363
364 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
365
366 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000367 GrDrawTarget::AutoStateRestore asr(fGpu,
368 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000369 GrDrawState* drawState = fGpu->drawState();
370 drawState->setRenderTarget(texture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +0000371 drawState->sampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
372 (clampEntry.texture())))->unref();
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000373
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000374 GrSamplerState::Filter filter;
375 // if filtering is not desired then we want to ensure all
376 // texels in the resampled image are copies of texels from
377 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000378 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000379 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000380 } else {
381 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000382 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000383 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
384 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000385
386 static const GrVertexLayout layout =
387 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
388 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
389
390 if (arg.succeeded()) {
391 GrPoint* verts = (GrPoint*) arg.vertices();
392 verts[0].setIRectFan(0, 0,
393 texture->width(),
394 texture->height(),
395 2*sizeof(GrPoint));
396 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000397 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000398 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000399 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000401 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402 } else {
403 // TODO: Our CPU stretch doesn't filter. But we create separate
404 // stretched textures when the sampler state is either filtered or
405 // not. Either implement filtered stretch blit on CPU or just create
406 // one when FBO case fails.
407
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000408 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000409 // no longer need to clamp at min RT size.
410 rtDesc.fWidth = GrNextPow2(desc.fWidth);
411 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000412 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000413 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000414 rtDesc.fWidth *
415 rtDesc.fHeight);
416 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
417 srcData, desc.fWidth, desc.fHeight, bpp);
418
419 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
420
421 GrTexture* texture = fGpu->createTexture(rtDesc,
422 stretchedPixels.get(),
423 stretchedRowBytes);
424 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000425 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000426 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000427 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000428
429 } else {
430 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
431 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000432 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000433 }
434 }
435 return entry;
436}
437
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000438GrContext::TextureCacheEntry GrContext::lockScratchTexture(
439 const GrTextureDesc& inDesc,
440 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000441 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000442 desc.fClientCacheID = kScratch_CacheID;
443
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000444 if (kExact_ScratchTexMatch != match) {
445 // bin by pow2 with a reasonable min
446 static const int MIN_SIZE = 256;
447 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
448 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
449 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000450
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000451 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000452 int origWidth = desc.fWidth;
453 int origHeight = desc.fHeight;
454 bool doubledW = false;
455 bool doubledH = false;
456
457 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000458 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000459 entry = fTextureCache->findAndLock(key,
460 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000461 // if we miss, relax the fit of the flags...
462 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000463 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000464 break;
465 }
466 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
467 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
468 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
469 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
470 } else if (!doubledW) {
471 desc.fFlags = inDesc.fFlags;
472 desc.fWidth *= 2;
473 doubledW = true;
474 } else if (!doubledH) {
475 desc.fFlags = inDesc.fFlags;
476 desc.fWidth = origWidth;
477 desc.fHeight *= 2;
478 doubledH = true;
479 } else {
480 break;
481 }
482
483 } while (true);
484
485 if (NULL == entry) {
486 desc.fFlags = inDesc.fFlags;
487 desc.fWidth = origWidth;
488 desc.fHeight = origHeight;
489 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
490 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000491 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000492 texture->desc(),
493 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000494 entry = fTextureCache->createAndLock(key, texture);
495 }
496 }
497
498 // If the caller gives us the same desc/sampler twice we don't want
499 // to return the same texture the second time (unless it was previously
500 // released). So we detach the entry from the cache and reattach at release.
501 if (NULL != entry) {
502 fTextureCache->detach(entry);
503 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000504 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000505}
506
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000507void GrContext::addExistingTextureToCache(GrTexture* texture) {
508
509 if (NULL == texture) {
510 return;
511 }
512
513 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
514 texture->desc(),
515 true);
516 fTextureCache->attach(key, texture);
517}
518
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000519void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000520 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000521 // If this is a scratch texture we detached it from the cache
522 // while it was locked (to avoid two callers simultaneously getting
523 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000524 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000525 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000526 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000527 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000528 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529}
530
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000531void GrContext::freeEntry(TextureCacheEntry entry) {
532 ASSERT_OWNED_RESOURCE(entry.texture());
533
534 fTextureCache->freeEntry(entry.cacheEntry());
535}
536
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000537GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000538 void* srcData,
539 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000540 GrTextureDesc descCopy = descIn;
541 descCopy.fClientCacheID = kUncached_CacheID;
542 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000543}
544
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000545void GrContext::getTextureCacheLimits(int* maxTextures,
546 size_t* maxTextureBytes) const {
547 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000548}
549
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000550void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
551 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000552}
553
bsalomon@google.com91958362011-06-13 17:58:13 +0000554int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000555 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000556}
557
558int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000559 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560}
561
562///////////////////////////////////////////////////////////////////////////////
563
bsalomon@google.come269f212011-11-07 13:29:52 +0000564GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
565 return fGpu->createPlatformTexture(desc);
566}
567
568GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
569 return fGpu->createPlatformRenderTarget(desc);
570}
571
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000572///////////////////////////////////////////////////////////////////////////////
573
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000574bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000575 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000576 const GrDrawTarget::Caps& caps = fGpu->getCaps();
577 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578 return false;
579 }
580
bsalomon@google.com27847de2011-02-22 20:59:41 +0000581 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
582
583 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000584 bool tiled = NULL != sampler &&
585 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
586 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000587 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000588 return false;
589 }
590 }
591 return true;
592}
593
594////////////////////////////////////////////////////////////////////////////////
595
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000596const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
597
bsalomon@google.com27847de2011-02-22 20:59:41 +0000598void GrContext::setClip(const GrClip& clip) {
599 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000600 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601}
602
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603////////////////////////////////////////////////////////////////////////////////
604
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000605void GrContext::clear(const GrIRect* rect,
606 const GrColor color,
607 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000608 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000609 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000610}
611
612void GrContext::drawPaint(const GrPaint& paint) {
613 // set rect to be big enough to fill the space, but not super-huge, so we
614 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000615 GrRect r;
616 r.setLTRB(0, 0,
617 GrIntToScalar(getRenderTarget()->width()),
618 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000620 SkTLazy<GrPaint> tmpPaint;
621 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000622 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000624 // We attempt to map r by the inverse matrix and draw that. mapRect will
625 // map the four corners and bound them with a new rect. This will not
626 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000627 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000628 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000629 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 return;
631 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632 inverse.mapRect(&r);
633 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000634 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000635 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000636 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000637 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
638 GrPrintf("Could not invert matrix\n");
639 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000640 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000641 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000642 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000643 // by definition this fills the entire clip, no need for AA
644 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000645 if (!tmpPaint.isValid()) {
646 tmpPaint.set(paint);
647 p = tmpPaint.get();
648 }
649 GrAssert(p == tmpPaint.get());
650 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000651 }
652 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653}
654
bsalomon@google.com205d4602011-04-25 12:43:45 +0000655////////////////////////////////////////////////////////////////////////////////
656
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000657namespace {
658inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
659 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
660}
661}
662
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000663////////////////////////////////////////////////////////////////////////////////
664
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665/* create a triangle strip that strokes the specified triangle. There are 8
666 unique vertices, but we repreat the last 2 to close up. Alternatively we
667 could use an indices array, and then only send 8 verts, but not sure that
668 would be faster.
669 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000670static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000671 GrScalar width) {
672 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000673 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000674
675 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
676 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
677 verts[2].set(rect.fRight - rad, rect.fTop + rad);
678 verts[3].set(rect.fRight + rad, rect.fTop - rad);
679 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
680 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
681 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
682 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
683 verts[8] = verts[0];
684 verts[9] = verts[1];
685}
686
reed@google.com20efde72011-05-09 17:00:02 +0000687/**
688 * Returns true if the rects edges are integer-aligned.
689 */
690static bool isIRect(const GrRect& r) {
691 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
692 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
693}
694
bsalomon@google.com205d4602011-04-25 12:43:45 +0000695static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 const GrRect& rect,
697 GrScalar width,
698 const GrMatrix* matrix,
699 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000700 GrRect* devRect,
701 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000702 // we use a simple coverage ramp to do aa on axis-aligned rects
703 // we check if the rect will be axis-aligned, and the rect won't land on
704 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000705
bsalomon@google.coma3108262011-10-10 14:08:47 +0000706 // we are keeping around the "tweak the alpha" trick because
707 // it is our only hope for the fixed-pipe implementation.
708 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000709 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000710 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000711 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000712 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000713#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000714 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000715#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000716 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000717 } else {
718 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000719 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000721 const GrDrawState& drawState = target->getDrawState();
722 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000723 return false;
724 }
725
bsalomon@google.com471d4712011-08-23 15:45:25 +0000726 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000727 return false;
728 }
729
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000731 return false;
732 }
733
734 if (NULL != matrix &&
735 !matrix->preservesAxisAlignment()) {
736 return false;
737 }
738
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000739 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000740 if (NULL != matrix) {
741 combinedMatrix->preConcat(*matrix);
742 GrAssert(combinedMatrix->preservesAxisAlignment());
743 }
744
745 combinedMatrix->mapRect(devRect, rect);
746 devRect->sort();
747
748 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000749 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 } else {
751 return true;
752 }
753}
754
bsalomon@google.com27847de2011-02-22 20:59:41 +0000755void GrContext::drawRect(const GrPaint& paint,
756 const GrRect& rect,
757 GrScalar width,
758 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000759 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000760
761 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000762 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000763
bsalomon@google.com205d4602011-04-25 12:43:45 +0000764 GrRect devRect = rect;
765 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000766 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000767 bool needAA = paint.fAntiAlias &&
768 !this->getRenderTarget()->isMultisampled();
769 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
770 &combinedMatrix, &devRect,
771 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000772
773 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000774 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
775 if (!adcd.succeeded()) {
776 return;
777 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000778 if (width >= 0) {
779 GrVec strokeSize;;
780 if (width > 0) {
781 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000782 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000783 strokeSize.setAbs(strokeSize);
784 } else {
785 strokeSize.set(GR_Scalar1, GR_Scalar1);
786 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000787 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
788 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000789 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000790 fAARectRenderer->fillAARect(this->getGpu(), target,
791 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000792 }
793 return;
794 }
795
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 if (width >= 0) {
797 // TODO: consider making static vertex buffers for these cases.
798 // Hairline could be done by just adding closing vertex to
799 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000800
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000802 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803
804 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000805 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 return;
807 }
808
809 GrPrimitiveType primType;
810 int vertCount;
811 GrPoint* vertex = geo.positions();
812
813 if (width > 0) {
814 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000815 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000816 setStrokeRectStrip(vertex, rect, width);
817 } else {
818 // hairline
819 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000820 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000821 vertex[0].set(rect.fLeft, rect.fTop);
822 vertex[1].set(rect.fRight, rect.fTop);
823 vertex[2].set(rect.fRight, rect.fBottom);
824 vertex[3].set(rect.fLeft, rect.fBottom);
825 vertex[4].set(rect.fLeft, rect.fTop);
826 }
827
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000828 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000829 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 GrDrawState* drawState = target->drawState();
831 avmr.set(drawState);
832 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000833 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000834 }
835
836 target->drawNonIndexed(primType, 0, vertCount);
837 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000838#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000839 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
840 if (NULL == sqVB) {
841 GrPrintf("Failed to create static rect vb.\n");
842 return;
843 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000844 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000845 GrDrawState* drawState = target->drawState();
846 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000847 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000848 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000849 0, rect.height(), rect.fTop,
850 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851
852 if (NULL != matrix) {
853 m.postConcat(*matrix);
854 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000856 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000857
bsalomon@google.com47059542012-06-06 20:51:20 +0000858 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000859#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000860 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000862 }
863}
864
865void GrContext::drawRectToRect(const GrPaint& paint,
866 const GrRect& dstRect,
867 const GrRect& srcRect,
868 const GrMatrix* dstMatrix,
869 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000870 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000871
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000872 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000873 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000874 drawRect(paint, dstRect, -1, dstMatrix);
875 return;
876 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000877
bsalomon@google.com27847de2011-02-22 20:59:41 +0000878 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
879
880#if GR_STATIC_RECT_VB
881 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000882 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000883 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885
886 GrMatrix m;
887
888 m.setAll(dstRect.width(), 0, dstRect.fLeft,
889 0, dstRect.height(), dstRect.fTop,
890 0, 0, GrMatrix::I()[8]);
891 if (NULL != dstMatrix) {
892 m.postConcat(*dstMatrix);
893 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000894 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000895
bsalomon@google.come3d32162012-07-20 13:37:06 +0000896 // we explicitly setup the correct coords for the first stage. The others
897 // must know about the view matrix change.
898 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
899 if (drawState->isStageEnabled(s)) {
900 drawState->sampler(s)->preConcatMatrix(m);
901 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000902 }
903
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904 m.setAll(srcRect.width(), 0, srcRect.fLeft,
905 0, srcRect.height(), srcRect.fTop,
906 0, 0, GrMatrix::I()[8]);
907 if (NULL != srcMatrix) {
908 m.postConcat(*srcMatrix);
909 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000910 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000912 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
913 if (NULL == sqVB) {
914 GrPrintf("Failed to create static rect vb.\n");
915 return;
916 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000917 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000918 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919#else
920
921 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000922#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000923 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000924#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000925 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
926#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000927 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000928
tomhudson@google.com93813632011-10-27 20:21:16 +0000929 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
930 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931 srcRects[0] = &srcRect;
932 srcMatrices[0] = srcMatrix;
933
bsalomon@google.come3d32162012-07-20 13:37:06 +0000934 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935#endif
936}
937
938void GrContext::drawVertices(const GrPaint& paint,
939 GrPrimitiveType primitiveType,
940 int vertexCount,
941 const GrPoint positions[],
942 const GrPoint texCoords[],
943 const GrColor colors[],
944 const uint16_t indices[],
945 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000946 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000947
948 GrDrawTarget::AutoReleaseGeometry geo;
949
950 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000951 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000952
bsalomon@google.come3d32162012-07-20 13:37:06 +0000953 GrVertexLayout layout = 0;
954 if (NULL != texCoords) {
955 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
956 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957 if (NULL != colors) {
958 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000959 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000960 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000961
962 if (sizeof(GrPoint) != vertexSize) {
963 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000964 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965 return;
966 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000967 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000969 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
970 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000971 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000972 NULL,
973 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000974 void* curVertex = geo.vertices();
975
976 for (int i = 0; i < vertexCount; ++i) {
977 *((GrPoint*)curVertex) = positions[i];
978
979 if (texOffsets[0] > 0) {
980 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
981 }
982 if (colorOffset > 0) {
983 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
984 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000985 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000986 }
987 } else {
988 target->setVertexSourceToArray(layout, positions, vertexCount);
989 }
990
bsalomon@google.com91958362011-06-13 17:58:13 +0000991 // we don't currently apply offscreen AA to this path. Need improved
992 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000993
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000994 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000995 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000996 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000997 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000998 target->drawNonIndexed(primitiveType, 0, vertexCount);
999 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001000}
1001
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001002///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001003namespace {
1004
bsalomon@google.com93c96602012-04-27 13:05:21 +00001005struct CircleVertex {
1006 GrPoint fPos;
1007 GrPoint fCenter;
1008 GrScalar fOuterRadius;
1009 GrScalar fInnerRadius;
1010};
1011
1012/* Returns true if will map a circle to another circle. This can be true
1013 * if the matrix only includes square-scale, rotation, translation.
1014 */
1015inline bool isSimilarityTransformation(const SkMatrix& matrix,
1016 SkScalar tol = SK_ScalarNearlyZero) {
1017 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1018 return true;
1019 }
1020 if (matrix.hasPerspective()) {
1021 return false;
1022 }
1023
1024 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1025 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1026 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1027 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1028
1029 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1030 return false;
1031 }
1032
1033 // it has scales or skews, but it could also be rotation, check it out.
1034 SkVector vec[2];
1035 vec[0].set(mx, sx);
1036 vec[1].set(sy, my);
1037
1038 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1039 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1040 SkScalarSquare(tol));
1041}
1042
1043}
1044
1045// TODO: strokeWidth can't be larger than zero right now.
1046// It will be fixed when drawPath() can handle strokes.
1047void GrContext::drawOval(const GrPaint& paint,
1048 const GrRect& rect,
1049 SkScalar strokeWidth) {
1050 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1051 kUnbuffered_DrawCategory;
1052 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001053 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001054 GrDrawState* drawState = target->drawState();
1055 GrMatrix vm = drawState->getViewMatrix();
1056
1057 if (!isSimilarityTransformation(vm) ||
1058 !paint.fAntiAlias ||
1059 rect.height() != rect.width()) {
1060 SkPath path;
1061 path.addOval(rect);
1062 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001063 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001064 this->internalDrawPath(paint, path, fill, NULL);
1065 return;
1066 }
1067
1068 const GrRenderTarget* rt = drawState->getRenderTarget();
1069 if (NULL == rt) {
1070 return;
1071 }
1072
bsalomon@google.come3d32162012-07-20 13:37:06 +00001073 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1074 if (!adcd.succeeded()) {
1075 return;
1076 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001077
bsalomon@google.come3d32162012-07-20 13:37:06 +00001078 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001079 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1080
1081 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1082 GrScalar radius = SkScalarHalf(rect.width());
1083
1084 vm.mapPoints(&center, 1);
1085 radius = vm.mapRadius(radius);
1086
1087 GrScalar outerRadius = radius;
1088 GrScalar innerRadius = 0;
1089 SkScalar halfWidth = 0;
1090 if (strokeWidth == 0) {
1091 halfWidth = SkScalarHalf(SK_Scalar1);
1092
1093 outerRadius += halfWidth;
1094 innerRadius = SkMaxScalar(0, radius - halfWidth);
1095 }
1096
1097 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1098 if (!geo.succeeded()) {
1099 GrPrintf("Failed to get space for vertices!\n");
1100 return;
1101 }
1102
1103 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1104
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001105 // The fragment shader will extend the radius out half a pixel
1106 // to antialias. Expand the drawn rect here so all the pixels
1107 // will be captured.
1108 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1109 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1110 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1111 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001112
1113 verts[0].fPos = SkPoint::Make(L, T);
1114 verts[1].fPos = SkPoint::Make(R, T);
1115 verts[2].fPos = SkPoint::Make(L, B);
1116 verts[3].fPos = SkPoint::Make(R, B);
1117
1118 for (int i = 0; i < 4; ++i) {
1119 // this goes to fragment shader, it should be in y-points-up space.
1120 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1121
1122 verts[i].fOuterRadius = outerRadius;
1123 verts[i].fInnerRadius = innerRadius;
1124 }
1125
1126 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001127 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001128}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001129
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001130void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001131 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001132
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001133 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001134 if (GrIsFillInverted(fill)) {
1135 this->drawPaint(paint);
1136 }
1137 return;
1138 }
1139
bsalomon@google.com93c96602012-04-27 13:05:21 +00001140 SkRect ovalRect;
1141 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1142 if (translate) {
1143 ovalRect.offset(*translate);
1144 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001145 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001146 this->drawOval(paint, ovalRect, width);
1147 return;
1148 }
1149
1150 internalDrawPath(paint, path, fill, translate);
1151}
1152
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001153void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001154 GrPathFill fill, const GrPoint* translate) {
1155
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001156 // Note that below we may sw-rasterize the path into a scratch texture.
1157 // Scratch textures can be recycled after they are returned to the texture
1158 // cache. This presents a potential hazard for buffered drawing. However,
1159 // the writePixels that uploads to the scratch will perform a flush so we're
1160 // OK.
1161 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1162 kUnbuffered_DrawCategory;
1163 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001164 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001165
bsalomon@google.com289533a2011-10-27 12:34:25 +00001166 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1167
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001168 // An Assumption here is that path renderer would use some form of tweaking
1169 // the src color (either the input alpha or in the frag shader) to implement
1170 // aa. If we have some future driver-mojo path AA that can do the right
1171 // thing WRT to the blend then we'll need some query on the PR.
1172 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001173#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001174 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001175#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001176 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001177 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001178
robertphillips@google.com72176b22012-05-23 13:19:12 +00001179 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001180 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001181#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001182 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001183#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001184 return;
1185 }
1186
bsalomon@google.come3d32162012-07-20 13:37:06 +00001187 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001188}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001189
bsalomon@google.com27847de2011-02-22 20:59:41 +00001190////////////////////////////////////////////////////////////////////////////////
1191
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001192void GrContext::flush(int flagsBitfield) {
1193 if (kDiscard_FlushBit & flagsBitfield) {
1194 fDrawBuffer->reset();
1195 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001196 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001197 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001198 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001199 fGpu->forceRenderTargetFlush();
1200 }
1201}
1202
bsalomon@google.com27847de2011-02-22 20:59:41 +00001203void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001204 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001205 // With addition of the AA clip path, flushing the draw buffer can
1206 // result in the generation of an AA clip mask. During this
1207 // process the SW path renderer may be invoked which recusively
1208 // calls this method (via internalWriteTexturePixels) creating
1209 // infinite recursion
1210 GrInOrderDrawBuffer* temp = fDrawBuffer;
1211 fDrawBuffer = NULL;
1212
1213 temp->flushTo(fGpu);
1214
1215 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001216 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001217}
1218
bsalomon@google.com6f379512011-11-16 20:36:03 +00001219void GrContext::internalWriteTexturePixels(GrTexture* texture,
1220 int left, int top,
1221 int width, int height,
1222 GrPixelConfig config,
1223 const void* buffer,
1224 size_t rowBytes,
1225 uint32_t flags) {
1226 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001227 ASSERT_OWNED_RESOURCE(texture);
1228
bsalomon@google.com6f379512011-11-16 20:36:03 +00001229 if (!(kDontFlush_PixelOpsFlag & flags)) {
1230 this->flush();
1231 }
1232 // TODO: use scratch texture to perform conversion
1233 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1234 GrPixelConfigIsUnpremultiplied(config)) {
1235 return;
1236 }
1237
1238 fGpu->writeTexturePixels(texture, left, top, width, height,
1239 config, buffer, rowBytes);
1240}
1241
1242bool GrContext::internalReadTexturePixels(GrTexture* texture,
1243 int left, int top,
1244 int width, int height,
1245 GrPixelConfig config,
1246 void* buffer,
1247 size_t rowBytes,
1248 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001249 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001250 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001251
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001252 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001253 GrRenderTarget* target = texture->asRenderTarget();
1254 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001255 return this->internalReadRenderTargetPixels(target,
1256 left, top, width, height,
1257 config, buffer, rowBytes,
1258 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001259 } else {
1260 return false;
1261 }
1262}
1263
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001264#include "SkConfig8888.h"
1265
1266namespace {
1267/**
1268 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1269 * formats are representable as Config8888 and so the function returns false
1270 * if the GrPixelConfig has no equivalent Config8888.
1271 */
1272bool grconfig_to_config8888(GrPixelConfig config,
1273 SkCanvas::Config8888* config8888) {
1274 switch (config) {
1275 case kRGBA_8888_PM_GrPixelConfig:
1276 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1277 return true;
1278 case kRGBA_8888_UPM_GrPixelConfig:
1279 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1280 return true;
1281 case kBGRA_8888_PM_GrPixelConfig:
1282 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1283 return true;
1284 case kBGRA_8888_UPM_GrPixelConfig:
1285 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1286 return true;
1287 default:
1288 return false;
1289 }
1290}
1291}
1292
bsalomon@google.com6f379512011-11-16 20:36:03 +00001293bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1294 int left, int top,
1295 int width, int height,
1296 GrPixelConfig config,
1297 void* buffer,
1298 size_t rowBytes,
1299 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001300 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001301 ASSERT_OWNED_RESOURCE(target);
1302
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001303 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001304 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001305 if (NULL == target) {
1306 return false;
1307 }
1308 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001309
bsalomon@google.com6f379512011-11-16 20:36:03 +00001310 if (!(kDontFlush_PixelOpsFlag & flags)) {
1311 this->flush();
1312 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001313
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001314 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1315 GrPixelConfigIsUnpremultiplied(config) &&
1316 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1317 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1318 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1319 !grconfig_to_config8888(config, &dstConfig8888)) {
1320 return false;
1321 }
1322 // do read back using target's own config
1323 this->internalReadRenderTargetPixels(target,
1324 left, top,
1325 width, height,
1326 target->config(),
1327 buffer, rowBytes,
1328 kDontFlush_PixelOpsFlag);
1329 // sw convert the pixels to unpremul config
1330 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1331 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1332 pixels, rowBytes, srcConfig8888,
1333 width, height);
1334 return true;
1335 }
1336
bsalomon@google.comc4364992011-11-07 15:54:49 +00001337 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001338 bool swapRAndB = NULL != src &&
1339 fGpu->preferredReadPixelsConfig(config) ==
1340 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001341
1342 bool flipY = NULL != src &&
1343 fGpu->readPixelsWillPayForYFlip(target, left, top,
1344 width, height, config,
1345 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001346 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1347 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001348
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001349 if (NULL == src && alphaConversion) {
1350 // we should fallback to cpu conversion here. This could happen when
1351 // we were given an external render target by the client that is not
1352 // also a texture (e.g. FBO 0 in GL)
1353 return false;
1354 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001355 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001356 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001357 if (flipY || swapRAndB || alphaConversion) {
1358 GrAssert(NULL != src);
1359 if (swapRAndB) {
1360 config = GrPixelConfigSwapRAndB(config);
1361 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001362 }
1363 // Make the scratch a render target because we don't have a robust
1364 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001365 GrTextureDesc desc;
1366 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1367 desc.fWidth = width;
1368 desc.fHeight = height;
1369 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001370
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001371 // When a full readback is faster than a partial we could always make
1372 // the scratch exactly match the passed rect. However, if we see many
1373 // different size rectangles we will trash our texture cache and pay the
1374 // cost of creating and destroying many textures. So, we only request
1375 // an exact match when the caller is reading an entire RT.
1376 ScratchTexMatch match = kApprox_ScratchTexMatch;
1377 if (0 == left &&
1378 0 == top &&
1379 target->width() == width &&
1380 target->height() == height &&
1381 fGpu->fullReadPixelsIsFasterThanPartial()) {
1382 match = kExact_ScratchTexMatch;
1383 }
1384 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001385 GrTexture* texture = ast.texture();
1386 if (!texture) {
1387 return false;
1388 }
1389 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001390 GrAssert(NULL != target);
1391
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001392 GrDrawTarget::AutoStateRestore asr(fGpu,
1393 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001394 GrDrawState* drawState = fGpu->drawState();
1395 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001396
bsalomon@google.comc4364992011-11-07 15:54:49 +00001397 GrMatrix matrix;
1398 if (flipY) {
1399 matrix.setTranslate(SK_Scalar1 * left,
1400 SK_Scalar1 * (top + height));
1401 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1402 } else {
1403 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1404 }
1405 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001406 drawState->sampler(0)->reset(matrix);
1407 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001408 drawState->sampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (src)))->unref();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001409 GrRect rect;
1410 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001411 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001412 left = 0;
1413 top = 0;
1414 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001415 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001416 left, top, width, height,
1417 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001418}
1419
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001420void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1421 GrAssert(target);
1422 ASSERT_OWNED_RESOURCE(target);
1423 // In the future we may track whether there are any pending draws to this
1424 // target. We don't today so we always perform a flush. We don't promise
1425 // this to our clients, though.
1426 this->flush();
1427 fGpu->resolveRenderTarget(target);
1428}
1429
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001430void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1431 if (NULL == src || NULL == dst) {
1432 return;
1433 }
1434 ASSERT_OWNED_RESOURCE(src);
1435
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001436 // Writes pending to the source texture are not tracked, so a flush
1437 // is required to ensure that the copy captures the most recent contents
1438 // of the source texture. See similar behaviour in
1439 // GrContext::resolveRenderTarget.
1440 this->flush();
1441
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001442 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001443 GrDrawState* drawState = fGpu->drawState();
1444 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001445 GrMatrix sampleM;
1446 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001447 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001448 drawState->sampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (src)))->unref();
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001449 SkRect rect = SkRect::MakeXYWH(0, 0,
1450 SK_Scalar1 * src->width(),
1451 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001452 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001453}
1454
bsalomon@google.com6f379512011-11-16 20:36:03 +00001455void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1456 int left, int top,
1457 int width, int height,
1458 GrPixelConfig config,
1459 const void* buffer,
1460 size_t rowBytes,
1461 uint32_t flags) {
1462 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001463 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001464
1465 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001466 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001467 if (NULL == target) {
1468 return;
1469 }
1470 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001471
1472 // TODO: when underlying api has a direct way to do this we should use it
1473 // (e.g. glDrawPixels on desktop GL).
1474
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001475 // If the RT is also a texture and we don't have to do PM/UPM conversion
1476 // then take the texture path, which we expect to be at least as fast or
1477 // faster since it doesn't use an intermediate texture as we do below.
1478
1479#if !GR_MAC_BUILD
1480 // At least some drivers on the Mac get confused when glTexImage2D is called
1481 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1482 // determine what OS versions and/or HW is affected.
1483 if (NULL != target->asTexture() &&
1484 GrPixelConfigIsUnpremultiplied(target->config()) ==
1485 GrPixelConfigIsUnpremultiplied(config)) {
1486
1487 this->internalWriteTexturePixels(target->asTexture(),
1488 left, top, width, height,
1489 config, buffer, rowBytes, flags);
1490 return;
1491 }
1492#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001493 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1494 GrPixelConfigIsUnpremultiplied(config) &&
1495 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1496 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1497 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1498 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1499 return;
1500 }
1501 // allocate a tmp buffer and sw convert the pixels to premul
1502 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1503 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1504 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1505 src, rowBytes, srcConfig8888,
1506 width, height);
1507 // upload the already premul pixels
1508 this->internalWriteRenderTargetPixels(target,
1509 left, top,
1510 width, height,
1511 target->config(),
1512 tmpPixels, 4 * width, flags);
1513 return;
1514 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001515
1516 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1517 GrPixelConfigSwapRAndB(config);
1518 if (swapRAndB) {
1519 config = GrPixelConfigSwapRAndB(config);
1520 }
1521
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001522 GrTextureDesc desc;
1523 desc.fWidth = width;
1524 desc.fHeight = height;
1525 desc.fConfig = config;
1526
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001527 GrAutoScratchTexture ast(this, desc);
1528 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001529 if (NULL == texture) {
1530 return;
1531 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001532 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1533 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001534
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001535 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001536 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001537
1538 GrMatrix matrix;
1539 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001540 drawState->setViewMatrix(matrix);
1541 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001542
bsalomon@google.com5c638652011-07-18 19:31:59 +00001543 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001544 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1545 GrSamplerState::kNearest_Filter,
1546 matrix);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001547 drawState->sampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001548 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001549
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001550 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001552 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001553 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1554 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001555 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001556 return;
1557 }
1558 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001559 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001560}
1561////////////////////////////////////////////////////////////////////////////////
1562
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001563void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001564 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001565
1566 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1567 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001568 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001569 if (paint.isTextureStageEnabled(i)) {
1570 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001572 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001573 }
1574
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001575 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001576
1577 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1578 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001579 ASSERT_OWNED_RESOURCE(paint.getMask(i));
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001580 if (paint.isMaskStageEnabled(i)) {
1581 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001582 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001583 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001584 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001585
1586 // disable all stages not accessible via the paint
1587 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001588 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001589 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001590
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001592
1593 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001594 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001595 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001596 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001597 }
1598 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001599 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001600 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001601 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001602 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001603 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001604 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1605 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001606 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001608 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001609 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1610 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1611 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001612#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001613 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001614 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001615 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1616 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001617#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001618}
1619
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001620GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621 DrawCategory category) {
1622 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001623 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 fLastDrawCategory = category;
1625 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001626 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627 GrDrawTarget* target = fGpu;
1628 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001629 case kUnbuffered_DrawCategory:
1630 target = fGpu;
1631 break;
1632 case kBuffered_DrawCategory:
1633 target = fDrawBuffer;
1634 fDrawBuffer->setClip(fGpu->getClip());
1635 break;
1636 default:
1637 GrCrash("Unexpected DrawCategory.");
1638 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001639 }
1640 return target;
1641}
1642
robertphillips@google.com72176b22012-05-23 13:19:12 +00001643/*
1644 * This method finds a path renderer that can draw the specified path on
1645 * the provided target.
1646 * Due to its expense, the software path renderer has split out so it can
1647 * can be individually allowed/disallowed via the "allowSW" boolean.
1648 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001649GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001650 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001651 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001652 bool antiAlias,
1653 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001654 if (NULL == fPathRendererChain) {
1655 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001656 SkNEW_ARGS(GrPathRendererChain,
1657 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001658 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001659
1660 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1661 target,
1662 antiAlias);
1663
1664 if (NULL == pr && allowSW) {
1665 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001666 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001667 }
1668
1669 pr = fSoftwarePathRenderer;
1670 }
1671
1672 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001673}
1674
bsalomon@google.com27847de2011-02-22 20:59:41 +00001675////////////////////////////////////////////////////////////////////////////////
1676
bsalomon@google.com27847de2011-02-22 20:59:41 +00001677void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001678 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001679 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001680 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001681 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001682 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683}
1684
1685GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001686 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001687}
1688
1689const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001690 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001691}
1692
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001693bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1694 return fGpu->isConfigRenderable(config);
1695}
1696
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001698 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699}
1700
1701void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001702 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
1705void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1710 intptr_t mask = 1 << shift;
1711 if (pred) {
1712 bits |= mask;
1713 } else {
1714 bits &= ~mask;
1715 }
1716 return bits;
1717}
1718
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001719GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001720 ++THREAD_INSTANCE_COUNT;
1721
bsalomon@google.com27847de2011-02-22 20:59:41 +00001722 fGpu = gpu;
1723 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001724 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001725
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001726 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001727 fGpu->setDrawState(fDrawState);
1728
bsalomon@google.com30085192011-08-19 15:42:31 +00001729 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001730 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001731
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001732 fTextureCache = SkNEW_ARGS(GrResourceCache,
1733 (MAX_TEXTURE_CACHE_COUNT,
1734 MAX_TEXTURE_CACHE_BYTES));
1735 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001736
1737 fLastDrawCategory = kUnbuffered_DrawCategory;
1738
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001739 fDrawBuffer = NULL;
1740 fDrawBufferVBAllocPool = NULL;
1741 fDrawBufferIBAllocPool = NULL;
1742
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001743 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001744
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001745 this->setupDrawBuffer();
1746}
1747
1748void GrContext::setupDrawBuffer() {
1749
1750 GrAssert(NULL == fDrawBuffer);
1751 GrAssert(NULL == fDrawBufferVBAllocPool);
1752 GrAssert(NULL == fDrawBufferIBAllocPool);
1753
bsalomon@google.com92edd312012-04-04 21:40:21 +00001754#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001755 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001756 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001757 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001758 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001759 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001760 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001761 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001762 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001763
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001764 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001765 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001766 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001767#endif
1768
1769#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001770 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001771#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001772 if (fDrawBuffer) {
1773 fDrawBuffer->setAutoFlushTarget(fGpu);
1774 fDrawBuffer->setDrawState(fDrawState);
1775 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001776}
1777
bsalomon@google.com27847de2011-02-22 20:59:41 +00001778GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001780 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001781#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001782 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001783#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784}
1785
1786const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1787 return fGpu->getQuadIndexBuffer();
1788}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001789
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001790GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001791 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001792 const SkRect& rect,
1793 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001794 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001795 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001796 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001797 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001798 int scaleFactorX, radiusX;
1799 int scaleFactorY, radiusY;
1800 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1801 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001802
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001803 SkRect srcRect(rect);
1804 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1805 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001806 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1807 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001808
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001809 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001810
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001811 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1812 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1813 kAlpha_8_GrPixelConfig == srcTexture->config());
1814
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001815 GrTextureDesc desc;
1816 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1817 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1818 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1819 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001820
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001821 GrAutoScratchTexture temp1, temp2;
1822 GrTexture* dstTexture = temp1.set(this, desc);
1823 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001824
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 GrPaint paint;
1826 paint.reset();
1827 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1828
1829 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1830 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1831 srcTexture->height());
1832 this->setRenderTarget(dstTexture->asRenderTarget());
1833 SkRect dstRect(srcRect);
1834 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1835 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001836 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1837 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 this->drawRectToRect(paint, dstRect, srcRect);
1839 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001840 srcTexture = dstTexture;
1841 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001842 }
1843
robertphillips@google.com7a396332012-05-10 15:11:27 +00001844 SkIRect srcIRect;
1845 srcRect.roundOut(&srcIRect);
1846
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001847 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001849 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001850 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001851 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001852 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001853 this->clear(&clearRect, 0x0);
1854 }
1855
1856 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001857 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1858 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001859 srcTexture = dstTexture;
1860 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861 }
1862
1863 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001865 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001866 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001867 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001868 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 this->clear(&clearRect, 0x0);
1870 }
1871
1872 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001873 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1874 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001875 srcTexture = dstTexture;
1876 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001877 }
1878
1879 if (scaleFactorX > 1 || scaleFactorY > 1) {
1880 // Clear one pixel to the right and below, to accommodate bilinear
1881 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001882 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1883 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001884 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001885 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1886 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001887 this->clear(&clearRect, 0x0);
1888 // FIXME: This should be mitchell, not bilinear.
1889 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1890 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1891 srcTexture->height());
1892 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001893 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1894 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001896 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001897 this->drawRectToRect(paint, dstRect, srcRect);
1898 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001899 srcTexture = dstTexture;
1900 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 }
1902 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001903 if (srcTexture == temp1.texture()) {
1904 return temp1.detach();
1905 } else if (srcTexture == temp2.texture()) {
1906 return temp2.detach();
1907 } else {
1908 srcTexture->ref();
1909 return srcTexture;
1910 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001911}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001912
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001913GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1914 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001915 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001916 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001917 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001918 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001919 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001920
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001921 AutoMatrix avm(this, GrMatrix::I());
1922
1923 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1924 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001925 GrTextureDesc desc;
1926 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1927 desc.fWidth = SkScalarCeilToInt(rect.width());
1928 desc.fHeight = SkScalarCeilToInt(rect.height());
1929 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001931 GrAutoScratchTexture ast(this, desc);
1932 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001933 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1934 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001935 SkIRect clearRect = SkIRect::MakeXYWH(
1936 SkScalarFloorToInt(rect.fLeft),
1937 SkScalarFloorToInt(rect.fBottom),
1938 SkScalarFloorToInt(rect.width()),
1939 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001940 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001941 srcTexture->unref();
1942 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001943 }
1944 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001945 GrAutoScratchTexture ast(this, desc);
1946 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001947 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1948 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001949 srcTexture->unref();
1950 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001951 }
1952 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001953 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001954}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001955
1956///////////////////////////////////////////////////////////////////////////////