blob: a49bf99924f627857b7765b1b9918f87d217d756 [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"
14
tomhudson@google.com278cbb42011-06-30 19:37:01 +000015#include "GrBufferAllocPool.h"
16#include "GrClipIterator.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000030#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000032#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000033
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000034#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000036#define MAX_BLUR_SIGMA 4.0f
37
bsalomon@google.comd46e2422011-09-23 17:40:07 +000038// When we're using coverage AA but the blend is incompatible (given gpu
39// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000040#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041
reed@google.com4b2d3f32012-05-15 18:05:50 +000042#if GR_DEBUG
43 // change this to a 1 to see notifications when partial coverage fails
44 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
45#else
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#endif
48
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000049static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
50static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000051
bsalomon@google.com60361492012-03-15 17:47:06 +000052static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
54
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000055// path rendering is the only thing we defer today that uses non-static indices
56static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
57static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
60
bsalomon@google.com05ef5102011-05-02 21:14:59 +000061GrContext* GrContext::Create(GrEngine engine,
62 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000063 GrContext* ctx = NULL;
64 GrGpu* fGpu = GrGpu::Create(engine, context3D);
65 if (NULL != fGpu) {
66 ctx = new GrContext(fGpu);
67 fGpu->unref();
68 }
69 return ctx;
70}
71
bsalomon@google.comc0af3172012-06-15 14:10:09 +000072namespace {
73void* CreateThreadInstanceCount() {
74 return new int(0);
75}
76void DeleteThreadInstanceCount(void* v) {
77 delete reinterpret_cast<int*>(v);
78}
79#define THREAD_INSTANCE_COUNT \
80 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
81 DeleteThreadInstanceCount)))
82
83}
84
85int GrContext::GetThreadInstanceCount() {
86 return THREAD_INSTANCE_COUNT;
87}
88
bsalomon@google.com27847de2011-02-22 20:59:41 +000089GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000091
92 // Since the gpu can hold scratch textures, give it a chance to let go
93 // of them before freeing the texture cache
94 fGpu->purgeResources();
95
bsalomon@google.com27847de2011-02-22 20:59:41 +000096 delete fTextureCache;
97 delete fFontCache;
98 delete fDrawBuffer;
99 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000100 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000101
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000102 fAARectRenderer->unref();
103
bsalomon@google.com205d4602011-04-25 12:43:45 +0000104 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000105 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000106 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000107 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000108
109 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110}
111
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000113 contextDestroyed();
114 this->setupDrawBuffer();
115}
116
117void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000118 // abandon first to so destructors
119 // don't try to free the resources in the API.
120 fGpu->abandonResources();
121
bsalomon@google.com30085192011-08-19 15:42:31 +0000122 // a path renderer may be holding onto resources that
123 // are now unusable
124 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000125 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000126
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 delete fDrawBuffer;
128 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBufferVBAllocPool;
131 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferIBAllocPool;
134 fDrawBufferIBAllocPool = NULL;
135
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000136 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000137
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138 fTextureCache->removeAll();
139 fFontCache->freeAll();
140 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141}
142
143void GrContext::resetContext() {
144 fGpu->markContextDirty();
145}
146
147void GrContext::freeGpuResources() {
148 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000149
150 fGpu->purgeResources();
151
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000152 fAARectRenderer->reset();
153
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154 fTextureCache->removeAll();
155 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000156 // a path renderer may be holding onto resources
157 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000158 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
twiz@google.com05e70242012-01-27 19:12:00 +0000161size_t GrContext::getGpuTextureCacheBytes() const {
162 return fTextureCache->getCachedResourceBytes();
163}
164
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165////////////////////////////////////////////////////////////////////////////////
166
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000167int GrContext::PaintStageVertexLayoutBits(
168 const GrPaint& paint,
169 const bool hasTexCoords[GrPaint::kTotalStages]) {
170 int stageMask = paint.getActiveStageMask();
171 int layout = 0;
172 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
173 if ((1 << i) & stageMask) {
174 if (NULL != hasTexCoords && hasTexCoords[i]) {
175 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000176 }
177 }
178 }
179 return layout;
180}
181
182
183////////////////////////////////////////////////////////////////////////////////
184
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000185GrTexture* GrContext::TextureCacheEntry::texture() const {
186 if (NULL == fEntry) {
187 return NULL;
188 } else {
189 return (GrTexture*) fEntry->resource();
190 }
191}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000192
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000193namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000194
195// we should never have more than one stencil buffer with same combo of
196// (width,height,samplecount)
197void gen_stencil_key_values(int width, int height,
198 int sampleCnt, uint32_t v[4]) {
199 v[0] = width;
200 v[1] = height;
201 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000202 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000203}
204
205void gen_stencil_key_values(const GrStencilBuffer* sb,
206 uint32_t v[4]) {
207 gen_stencil_key_values(sb->width(), sb->height(),
208 sb->numSamples(), v);
209}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000210
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000211void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000212 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
213 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
214 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
215 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000216}
217
bsalomon@google.comb505a122012-05-31 18:40:36 +0000218float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000219 *scaleFactor = 1;
220 while (sigma > MAX_BLUR_SIGMA) {
221 *scaleFactor *= 2;
222 sigma *= 0.5f;
223 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000224 *radius = static_cast<int>(ceilf(sigma * 3.0f));
225 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226 return sigma;
227}
228
229void apply_morphology(GrGpu* gpu,
230 GrTexture* texture,
231 const SkRect& rect,
232 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000233 GrContext::MorphologyType morphType,
234 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000235
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000236 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
237 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000238 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000239 drawState->setRenderTarget(target);
240 GrMatrix sampleM;
241 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000242 drawState->sampler(0)->reset(sampleM);
243 SkAutoTUnref<GrCustomStage> morph(
244 new GrMorphologyEffect(direction, radius, morphType));
245 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000246 drawState->setTexture(0, texture);
247 gpu->drawSimpleRect(rect, NULL, 1 << 0);
248}
249
bsalomon@google.comb505a122012-05-31 18:40:36 +0000250void convolve_gaussian(GrGpu* gpu,
251 GrTexture* texture,
252 const SkRect& rect,
253 float sigma,
254 int radius,
255 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000256 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
257 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000258 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000259 drawState->setRenderTarget(target);
260 GrMatrix sampleM;
261 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000262 drawState->sampler(0)->reset(sampleM);
263 SkAutoTUnref<GrConvolutionEffect> conv(new
264 GrConvolutionEffect(direction, radius));
265 conv->setGaussianKernel(sigma);
266 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000267 drawState->setTexture(0, texture);
268 gpu->drawSimpleRect(rect, NULL, 1 << 0);
269}
270
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000271}
272
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000273GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000274 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000275 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000276 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000277 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
278 GrResourceCache::kNested_LockType));
279}
280
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000281bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000282 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000283 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000284 return fTextureCache->hasKey(resourceKey);
285}
286
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000287GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000288 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000289 uint32_t v[4];
290 gen_stencil_key_values(sb, v);
291 GrResourceKey resourceKey(v);
292 return fTextureCache->createAndLock(resourceKey, sb);
293}
294
295GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
296 int sampleCnt) {
297 uint32_t v[4];
298 gen_stencil_key_values(width, height, sampleCnt, v);
299 GrResourceKey resourceKey(v);
300 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
301 GrResourceCache::kSingle_LockType);
302 if (NULL != entry) {
303 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
304 return sb;
305 } else {
306 return NULL;
307 }
308}
309
310void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000311 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000312 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000313}
314
315static void stretchImage(void* dst,
316 int dstW,
317 int dstH,
318 void* src,
319 int srcW,
320 int srcH,
321 int bpp) {
322 GrFixed dx = (srcW << 16) / dstW;
323 GrFixed dy = (srcH << 16) / dstH;
324
325 GrFixed y = dy >> 1;
326
327 int dstXLimit = dstW*bpp;
328 for (int j = 0; j < dstH; ++j) {
329 GrFixed x = dx >> 1;
330 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
331 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
332 for (int i = 0; i < dstXLimit; i += bpp) {
333 memcpy((uint8_t*) dstRow + i,
334 (uint8_t*) srcRow + (x>>16)*bpp,
335 bpp);
336 x += dx;
337 }
338 y += dy;
339 }
340}
341
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000342GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000343 const GrSamplerState* sampler,
344 const GrTextureDesc& desc,
345 void* srcData,
346 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000347 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000348
349#if GR_DUMP_TEXTURE_UPLOAD
350 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
351#endif
352
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000353 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000354
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000355 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000356 desc, false);
357
358 if (GrTexture::NeedsResizing(resourceKey)) {
359 // The desired texture is NPOT and tiled but that isn't supported by
360 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000361 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000362 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000363 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000364
365 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000366 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000367 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000368 GrAssert(NULL != clampEntry.texture());
369 if (NULL == clampEntry.texture()) {
370 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000371 }
372 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000373 GrTextureDesc rtDesc = desc;
374 rtDesc.fFlags = rtDesc.fFlags |
375 kRenderTarget_GrTextureFlagBit |
376 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000377 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
378 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000379
380 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
381
382 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000383 GrDrawTarget::AutoStateRestore asr(fGpu,
384 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000385 GrDrawState* drawState = fGpu->drawState();
386 drawState->setRenderTarget(texture->asRenderTarget());
387 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000388
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000389 GrSamplerState::Filter filter;
390 // if filtering is not desired then we want to ensure all
391 // texels in the resampled image are copies of texels from
392 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000393 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000394 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000395 } else {
396 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000397 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000398 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
399 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400
401 static const GrVertexLayout layout =
402 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
403 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
404
405 if (arg.succeeded()) {
406 GrPoint* verts = (GrPoint*) arg.vertices();
407 verts[0].setIRectFan(0, 0,
408 texture->width(),
409 texture->height(),
410 2*sizeof(GrPoint));
411 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000412 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000414 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000416 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000417 } else {
418 // TODO: Our CPU stretch doesn't filter. But we create separate
419 // stretched textures when the sampler state is either filtered or
420 // not. Either implement filtered stretch blit on CPU or just create
421 // one when FBO case fails.
422
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000423 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000424 // no longer need to clamp at min RT size.
425 rtDesc.fWidth = GrNextPow2(desc.fWidth);
426 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000427 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000428 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000429 rtDesc.fWidth *
430 rtDesc.fHeight);
431 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
432 srcData, desc.fWidth, desc.fHeight, bpp);
433
434 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
435
436 GrTexture* texture = fGpu->createTexture(rtDesc,
437 stretchedPixels.get(),
438 stretchedRowBytes);
439 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000440 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000441 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000442 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443
444 } else {
445 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
446 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000447 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000448 }
449 }
450 return entry;
451}
452
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000453GrContext::TextureCacheEntry GrContext::lockScratchTexture(
454 const GrTextureDesc& inDesc,
455 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000456 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000457 desc.fClientCacheID = kScratch_CacheID;
458
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000459 if (kExact_ScratchTexMatch != match) {
460 // bin by pow2 with a reasonable min
461 static const int MIN_SIZE = 256;
462 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
463 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
464 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000465
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000466 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000467 int origWidth = desc.fWidth;
468 int origHeight = desc.fHeight;
469 bool doubledW = false;
470 bool doubledH = false;
471
472 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000473 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000474 entry = fTextureCache->findAndLock(key,
475 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000476 // if we miss, relax the fit of the flags...
477 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000478 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000479 break;
480 }
481 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
482 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
483 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
484 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
485 } else if (!doubledW) {
486 desc.fFlags = inDesc.fFlags;
487 desc.fWidth *= 2;
488 doubledW = true;
489 } else if (!doubledH) {
490 desc.fFlags = inDesc.fFlags;
491 desc.fWidth = origWidth;
492 desc.fHeight *= 2;
493 doubledH = true;
494 } else {
495 break;
496 }
497
498 } while (true);
499
500 if (NULL == entry) {
501 desc.fFlags = inDesc.fFlags;
502 desc.fWidth = origWidth;
503 desc.fHeight = origHeight;
504 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
505 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000506 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000507 texture->desc(),
508 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000509 entry = fTextureCache->createAndLock(key, texture);
510 }
511 }
512
513 // If the caller gives us the same desc/sampler twice we don't want
514 // to return the same texture the second time (unless it was previously
515 // released). So we detach the entry from the cache and reattach at release.
516 if (NULL != entry) {
517 fTextureCache->detach(entry);
518 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000519 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000520}
521
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000522void GrContext::addExistingTextureToCache(GrTexture* texture) {
523
524 if (NULL == texture) {
525 return;
526 }
527
528 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
529 texture->desc(),
530 true);
531 fTextureCache->attach(key, texture);
532}
533
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000534void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000535 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000536 // If this is a scratch texture we detached it from the cache
537 // while it was locked (to avoid two callers simultaneously getting
538 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000539 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000540 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000541 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000542 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000543 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000544}
545
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000546void GrContext::freeEntry(TextureCacheEntry entry) {
547 ASSERT_OWNED_RESOURCE(entry.texture());
548
549 fTextureCache->freeEntry(entry.cacheEntry());
550}
551
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000552GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000553 void* srcData,
554 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000555 GrTextureDesc descCopy = descIn;
556 descCopy.fClientCacheID = kUncached_CacheID;
557 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000558}
559
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000560void GrContext::getTextureCacheLimits(int* maxTextures,
561 size_t* maxTextureBytes) const {
562 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563}
564
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000565void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
566 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567}
568
bsalomon@google.com91958362011-06-13 17:58:13 +0000569int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000570 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000571}
572
573int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000574 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575}
576
577///////////////////////////////////////////////////////////////////////////////
578
bsalomon@google.come269f212011-11-07 13:29:52 +0000579GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
580 return fGpu->createPlatformTexture(desc);
581}
582
583GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
584 return fGpu->createPlatformRenderTarget(desc);
585}
586
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000587///////////////////////////////////////////////////////////////////////////////
588
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000589bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000590 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000591 const GrDrawTarget::Caps& caps = fGpu->getCaps();
592 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593 return false;
594 }
595
bsalomon@google.com27847de2011-02-22 20:59:41 +0000596 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
597
598 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000599 bool tiled = NULL != sampler &&
600 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
601 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000602 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603 return false;
604 }
605 }
606 return true;
607}
608
609////////////////////////////////////////////////////////////////////////////////
610
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000611const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
612
bsalomon@google.com27847de2011-02-22 20:59:41 +0000613void GrContext::setClip(const GrClip& clip) {
614 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000615 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000616}
617
618void GrContext::setClip(const GrIRect& rect) {
619 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000620 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621 fGpu->setClip(clip);
622}
623
624////////////////////////////////////////////////////////////////////////////////
625
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000626void GrContext::clear(const GrIRect* rect,
627 const GrColor color,
628 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000629 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000630 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631}
632
633void GrContext::drawPaint(const GrPaint& paint) {
634 // set rect to be big enough to fill the space, but not super-huge, so we
635 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000636 GrRect r;
637 r.setLTRB(0, 0,
638 GrIntToScalar(getRenderTarget()->width()),
639 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000640 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000641 SkTLazy<GrPaint> tmpPaint;
642 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000643 GrAutoMatrix am;
644
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000645 // We attempt to map r by the inverse matrix and draw that. mapRect will
646 // map the four corners and bound them with a new rect. This will not
647 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000648 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000649 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000650 GrPrintf("Could not invert matrix");
651 return;
652 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653 inverse.mapRect(&r);
654 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000655 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000656 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000657 GrPrintf("Could not invert matrix");
658 return;
659 }
660 tmpPaint.set(paint);
661 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
662 p = tmpPaint.get();
663 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000664 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000666 // by definition this fills the entire clip, no need for AA
667 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000668 if (!tmpPaint.isValid()) {
669 tmpPaint.set(paint);
670 p = tmpPaint.get();
671 }
672 GrAssert(p == tmpPaint.get());
673 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000674 }
675 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000676}
677
bsalomon@google.com205d4602011-04-25 12:43:45 +0000678////////////////////////////////////////////////////////////////////////////////
679
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000680namespace {
681inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
682 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
683}
684}
685
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000686////////////////////////////////////////////////////////////////////////////////
687
bsalomon@google.com27847de2011-02-22 20:59:41 +0000688/* create a triangle strip that strokes the specified triangle. There are 8
689 unique vertices, but we repreat the last 2 to close up. Alternatively we
690 could use an indices array, and then only send 8 verts, but not sure that
691 would be faster.
692 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000694 GrScalar width) {
695 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000697
698 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
699 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
700 verts[2].set(rect.fRight - rad, rect.fTop + rad);
701 verts[3].set(rect.fRight + rad, rect.fTop - rad);
702 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
703 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
704 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
705 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
706 verts[8] = verts[0];
707 verts[9] = verts[1];
708}
709
reed@google.com20efde72011-05-09 17:00:02 +0000710/**
711 * Returns true if the rects edges are integer-aligned.
712 */
713static bool isIRect(const GrRect& r) {
714 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
715 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
716}
717
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 const GrRect& rect,
720 GrScalar width,
721 const GrMatrix* matrix,
722 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000723 GrRect* devRect,
724 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000725 // we use a simple coverage ramp to do aa on axis-aligned rects
726 // we check if the rect will be axis-aligned, and the rect won't land on
727 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000728
bsalomon@google.coma3108262011-10-10 14:08:47 +0000729 // we are keeping around the "tweak the alpha" trick because
730 // it is our only hope for the fixed-pipe implementation.
731 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000732 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000733 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000734 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000735 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000736#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000737 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000738#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000739 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000740 } else {
741 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000742 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000744 const GrDrawState& drawState = target->getDrawState();
745 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 return false;
747 }
748
bsalomon@google.com471d4712011-08-23 15:45:25 +0000749 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 return false;
751 }
752
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000753 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 return false;
755 }
756
757 if (NULL != matrix &&
758 !matrix->preservesAxisAlignment()) {
759 return false;
760 }
761
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763 if (NULL != matrix) {
764 combinedMatrix->preConcat(*matrix);
765 GrAssert(combinedMatrix->preservesAxisAlignment());
766 }
767
768 combinedMatrix->mapRect(devRect, rect);
769 devRect->sort();
770
771 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000772 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773 } else {
774 return true;
775 }
776}
777
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778void GrContext::drawRect(const GrPaint& paint,
779 const GrRect& rect,
780 GrScalar width,
781 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000782 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783
784 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000785 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000786 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000787
bsalomon@google.com205d4602011-04-25 12:43:45 +0000788 GrRect devRect = rect;
789 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000790 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000791 bool needAA = paint.fAntiAlias &&
792 !this->getRenderTarget()->isMultisampled();
793 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
794 &combinedMatrix, &devRect,
795 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000796
797 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000798 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000799 if (width >= 0) {
800 GrVec strokeSize;;
801 if (width > 0) {
802 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000803 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000804 strokeSize.setAbs(strokeSize);
805 } else {
806 strokeSize.set(GR_Scalar1, GR_Scalar1);
807 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000808 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
809 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000810 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000811 fAARectRenderer->fillAARect(this->getGpu(), target,
812 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000813 }
814 return;
815 }
816
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817 if (width >= 0) {
818 // TODO: consider making static vertex buffers for these cases.
819 // Hairline could be done by just adding closing vertex to
820 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000821 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
822
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823 static const int worstCaseVertCount = 10;
824 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
825
826 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000827 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828 return;
829 }
830
831 GrPrimitiveType primType;
832 int vertCount;
833 GrPoint* vertex = geo.positions();
834
835 if (width > 0) {
836 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000837 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 setStrokeRectStrip(vertex, rect, width);
839 } else {
840 // hairline
841 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000842 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000843 vertex[0].set(rect.fLeft, rect.fTop);
844 vertex[1].set(rect.fRight, rect.fTop);
845 vertex[2].set(rect.fRight, rect.fBottom);
846 vertex[3].set(rect.fLeft, rect.fBottom);
847 vertex[4].set(rect.fLeft, rect.fTop);
848 }
849
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852 GrDrawState* drawState = target->drawState();
853 avmr.set(drawState);
854 drawState->preConcatViewMatrix(*matrix);
855 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856 }
857
858 target->drawNonIndexed(primType, 0, vertCount);
859 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000861 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000862 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
863 if (NULL == sqVB) {
864 GrPrintf("Failed to create static rect vb.\n");
865 return;
866 }
867 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000868 GrDrawState* drawState = target->drawState();
869 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000871 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000872 0, rect.height(), rect.fTop,
873 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000874
875 if (NULL != matrix) {
876 m.postConcat(*matrix);
877 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 drawState->preConcatViewMatrix(m);
879 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000880
bsalomon@google.com47059542012-06-06 20:51:20 +0000881 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000882#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000883 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885 }
886}
887
888void GrContext::drawRectToRect(const GrPaint& paint,
889 const GrRect& dstRect,
890 const GrRect& srcRect,
891 const GrMatrix* dstMatrix,
892 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000893 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000895 // srcRect refers to paint's first texture
896 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897 drawRect(paint, dstRect, -1, dstMatrix);
898 return;
899 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000900
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
902
903#if GR_STATIC_RECT_VB
904 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000905 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000906 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000907 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000908 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000909
910 GrMatrix m;
911
912 m.setAll(dstRect.width(), 0, dstRect.fLeft,
913 0, dstRect.height(), dstRect.fTop,
914 0, 0, GrMatrix::I()[8]);
915 if (NULL != dstMatrix) {
916 m.postConcat(*dstMatrix);
917 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000918 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000920 // srcRect refers to first stage
921 int otherStageMask = paint.getActiveStageMask() &
922 (~(1 << GrPaint::kFirstTextureStage));
923 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000924 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 }
926
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 m.setAll(srcRect.width(), 0, srcRect.fLeft,
928 0, srcRect.height(), srcRect.fTop,
929 0, 0, GrMatrix::I()[8]);
930 if (NULL != srcMatrix) {
931 m.postConcat(*srcMatrix);
932 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000933 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000935 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
936 if (NULL == sqVB) {
937 GrPrintf("Failed to create static rect vb.\n");
938 return;
939 }
940 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000941 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942#else
943
944 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000945#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000947#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
949#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000950 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951
tomhudson@google.com93813632011-10-27 20:21:16 +0000952 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
953 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954 srcRects[0] = &srcRect;
955 srcMatrices[0] = srcMatrix;
956
957 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
958#endif
959}
960
961void GrContext::drawVertices(const GrPaint& paint,
962 GrPrimitiveType primitiveType,
963 int vertexCount,
964 const GrPoint positions[],
965 const GrPoint texCoords[],
966 const GrColor colors[],
967 const uint16_t indices[],
968 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000969 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000970
971 GrDrawTarget::AutoReleaseGeometry geo;
972
973 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000974 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000975
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000976 bool hasTexCoords[GrPaint::kTotalStages] = {
977 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
978 0 // remaining stages use positions
979 };
980
981 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982
983 if (NULL != colors) {
984 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000985 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000986 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987
988 if (sizeof(GrPoint) != vertexSize) {
989 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000990 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000991 return;
992 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000993 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000994 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000995 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
996 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000997 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000998 NULL,
999 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001000 void* curVertex = geo.vertices();
1001
1002 for (int i = 0; i < vertexCount; ++i) {
1003 *((GrPoint*)curVertex) = positions[i];
1004
1005 if (texOffsets[0] > 0) {
1006 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
1007 }
1008 if (colorOffset > 0) {
1009 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1010 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001011 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001012 }
1013 } else {
1014 target->setVertexSourceToArray(layout, positions, vertexCount);
1015 }
1016
bsalomon@google.com91958362011-06-13 17:58:13 +00001017 // we don't currently apply offscreen AA to this path. Need improved
1018 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001019
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001020 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001021 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001022 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001023 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001024 target->drawNonIndexed(primitiveType, 0, vertexCount);
1025 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001026}
1027
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001028///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001029namespace {
1030
bsalomon@google.com93c96602012-04-27 13:05:21 +00001031struct CircleVertex {
1032 GrPoint fPos;
1033 GrPoint fCenter;
1034 GrScalar fOuterRadius;
1035 GrScalar fInnerRadius;
1036};
1037
1038/* Returns true if will map a circle to another circle. This can be true
1039 * if the matrix only includes square-scale, rotation, translation.
1040 */
1041inline bool isSimilarityTransformation(const SkMatrix& matrix,
1042 SkScalar tol = SK_ScalarNearlyZero) {
1043 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1044 return true;
1045 }
1046 if (matrix.hasPerspective()) {
1047 return false;
1048 }
1049
1050 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1051 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1052 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1053 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1054
1055 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1056 return false;
1057 }
1058
1059 // it has scales or skews, but it could also be rotation, check it out.
1060 SkVector vec[2];
1061 vec[0].set(mx, sx);
1062 vec[1].set(sy, my);
1063
1064 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1065 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1066 SkScalarSquare(tol));
1067}
1068
1069}
1070
1071// TODO: strokeWidth can't be larger than zero right now.
1072// It will be fixed when drawPath() can handle strokes.
1073void GrContext::drawOval(const GrPaint& paint,
1074 const GrRect& rect,
1075 SkScalar strokeWidth) {
1076 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1077 kUnbuffered_DrawCategory;
1078 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001079 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001080 GrDrawState* drawState = target->drawState();
1081 GrMatrix vm = drawState->getViewMatrix();
1082
1083 if (!isSimilarityTransformation(vm) ||
1084 !paint.fAntiAlias ||
1085 rect.height() != rect.width()) {
1086 SkPath path;
1087 path.addOval(rect);
1088 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001089 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001090 this->internalDrawPath(paint, path, fill, NULL);
1091 return;
1092 }
1093
1094 const GrRenderTarget* rt = drawState->getRenderTarget();
1095 if (NULL == rt) {
1096 return;
1097 }
1098
1099 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1100
1101 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1102 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1103 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1104
1105 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1106 GrScalar radius = SkScalarHalf(rect.width());
1107
1108 vm.mapPoints(&center, 1);
1109 radius = vm.mapRadius(radius);
1110
1111 GrScalar outerRadius = radius;
1112 GrScalar innerRadius = 0;
1113 SkScalar halfWidth = 0;
1114 if (strokeWidth == 0) {
1115 halfWidth = SkScalarHalf(SK_Scalar1);
1116
1117 outerRadius += halfWidth;
1118 innerRadius = SkMaxScalar(0, radius - halfWidth);
1119 }
1120
1121 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1122 if (!geo.succeeded()) {
1123 GrPrintf("Failed to get space for vertices!\n");
1124 return;
1125 }
1126
1127 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1128
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001129 // The fragment shader will extend the radius out half a pixel
1130 // to antialias. Expand the drawn rect here so all the pixels
1131 // will be captured.
1132 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1133 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1134 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1135 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001136
1137 verts[0].fPos = SkPoint::Make(L, T);
1138 verts[1].fPos = SkPoint::Make(R, T);
1139 verts[2].fPos = SkPoint::Make(L, B);
1140 verts[3].fPos = SkPoint::Make(R, B);
1141
1142 for (int i = 0; i < 4; ++i) {
1143 // this goes to fragment shader, it should be in y-points-up space.
1144 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1145
1146 verts[i].fOuterRadius = outerRadius;
1147 verts[i].fInnerRadius = innerRadius;
1148 }
1149
1150 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001151 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001152}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001153
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001154void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001155 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001157 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001158 if (GrIsFillInverted(fill)) {
1159 this->drawPaint(paint);
1160 }
1161 return;
1162 }
1163
bsalomon@google.com93c96602012-04-27 13:05:21 +00001164 SkRect ovalRect;
1165 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1166 if (translate) {
1167 ovalRect.offset(*translate);
1168 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001169 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001170 this->drawOval(paint, ovalRect, width);
1171 return;
1172 }
1173
1174 internalDrawPath(paint, path, fill, translate);
1175}
1176
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001177void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001178 GrPathFill fill, const GrPoint* translate) {
1179
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001180 // Note that below we may sw-rasterize the path into a scratch texture.
1181 // Scratch textures can be recycled after they are returned to the texture
1182 // cache. This presents a potential hazard for buffered drawing. However,
1183 // the writePixels that uploads to the scratch will perform a flush so we're
1184 // OK.
1185 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1186 kUnbuffered_DrawCategory;
1187 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001188 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001189 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001190
bsalomon@google.com289533a2011-10-27 12:34:25 +00001191 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1192
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001193 // An Assumption here is that path renderer would use some form of tweaking
1194 // the src color (either the input alpha or in the frag shader) to implement
1195 // aa. If we have some future driver-mojo path AA that can do the right
1196 // thing WRT to the blend then we'll need some query on the PR.
1197 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001198#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001199 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001200#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001201 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001202 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001203
robertphillips@google.com72176b22012-05-23 13:19:12 +00001204 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001205 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001206#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001207 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001208#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001209 return;
1210 }
1211
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001212 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001213}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001214
bsalomon@google.com27847de2011-02-22 20:59:41 +00001215////////////////////////////////////////////////////////////////////////////////
1216
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001217void GrContext::flush(int flagsBitfield) {
1218 if (kDiscard_FlushBit & flagsBitfield) {
1219 fDrawBuffer->reset();
1220 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001221 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001222 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001223 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001224 fGpu->forceRenderTargetFlush();
1225 }
1226}
1227
bsalomon@google.com27847de2011-02-22 20:59:41 +00001228void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001229 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001230 // With addition of the AA clip path, flushing the draw buffer can
1231 // result in the generation of an AA clip mask. During this
1232 // process the SW path renderer may be invoked which recusively
1233 // calls this method (via internalWriteTexturePixels) creating
1234 // infinite recursion
1235 GrInOrderDrawBuffer* temp = fDrawBuffer;
1236 fDrawBuffer = NULL;
1237
1238 temp->flushTo(fGpu);
1239
1240 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001241 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001242}
1243
bsalomon@google.com6f379512011-11-16 20:36:03 +00001244void GrContext::internalWriteTexturePixels(GrTexture* texture,
1245 int left, int top,
1246 int width, int height,
1247 GrPixelConfig config,
1248 const void* buffer,
1249 size_t rowBytes,
1250 uint32_t flags) {
1251 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001252 ASSERT_OWNED_RESOURCE(texture);
1253
bsalomon@google.com6f379512011-11-16 20:36:03 +00001254 if (!(kDontFlush_PixelOpsFlag & flags)) {
1255 this->flush();
1256 }
1257 // TODO: use scratch texture to perform conversion
1258 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1259 GrPixelConfigIsUnpremultiplied(config)) {
1260 return;
1261 }
1262
1263 fGpu->writeTexturePixels(texture, left, top, width, height,
1264 config, buffer, rowBytes);
1265}
1266
1267bool GrContext::internalReadTexturePixels(GrTexture* texture,
1268 int left, int top,
1269 int width, int height,
1270 GrPixelConfig config,
1271 void* buffer,
1272 size_t rowBytes,
1273 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001274 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001275 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001276
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001277 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001278 GrRenderTarget* target = texture->asRenderTarget();
1279 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001280 return this->internalReadRenderTargetPixels(target,
1281 left, top, width, height,
1282 config, buffer, rowBytes,
1283 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001284 } else {
1285 return false;
1286 }
1287}
1288
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001289#include "SkConfig8888.h"
1290
1291namespace {
1292/**
1293 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1294 * formats are representable as Config8888 and so the function returns false
1295 * if the GrPixelConfig has no equivalent Config8888.
1296 */
1297bool grconfig_to_config8888(GrPixelConfig config,
1298 SkCanvas::Config8888* config8888) {
1299 switch (config) {
1300 case kRGBA_8888_PM_GrPixelConfig:
1301 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1302 return true;
1303 case kRGBA_8888_UPM_GrPixelConfig:
1304 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1305 return true;
1306 case kBGRA_8888_PM_GrPixelConfig:
1307 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1308 return true;
1309 case kBGRA_8888_UPM_GrPixelConfig:
1310 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1311 return true;
1312 default:
1313 return false;
1314 }
1315}
1316}
1317
bsalomon@google.com6f379512011-11-16 20:36:03 +00001318bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1319 int left, int top,
1320 int width, int height,
1321 GrPixelConfig config,
1322 void* buffer,
1323 size_t rowBytes,
1324 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001325 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001326 ASSERT_OWNED_RESOURCE(target);
1327
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001328 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001329 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001330 if (NULL == target) {
1331 return false;
1332 }
1333 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001334
bsalomon@google.com6f379512011-11-16 20:36:03 +00001335 if (!(kDontFlush_PixelOpsFlag & flags)) {
1336 this->flush();
1337 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001338
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001339 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1340 GrPixelConfigIsUnpremultiplied(config) &&
1341 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1342 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1343 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1344 !grconfig_to_config8888(config, &dstConfig8888)) {
1345 return false;
1346 }
1347 // do read back using target's own config
1348 this->internalReadRenderTargetPixels(target,
1349 left, top,
1350 width, height,
1351 target->config(),
1352 buffer, rowBytes,
1353 kDontFlush_PixelOpsFlag);
1354 // sw convert the pixels to unpremul config
1355 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1356 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1357 pixels, rowBytes, srcConfig8888,
1358 width, height);
1359 return true;
1360 }
1361
bsalomon@google.comc4364992011-11-07 15:54:49 +00001362 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001363 bool swapRAndB = NULL != src &&
1364 fGpu->preferredReadPixelsConfig(config) ==
1365 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001366
1367 bool flipY = NULL != src &&
1368 fGpu->readPixelsWillPayForYFlip(target, left, top,
1369 width, height, config,
1370 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001371 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1372 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001373
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001374 if (NULL == src && alphaConversion) {
1375 // we should fallback to cpu conversion here. This could happen when
1376 // we were given an external render target by the client that is not
1377 // also a texture (e.g. FBO 0 in GL)
1378 return false;
1379 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001380 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001381 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001382 if (flipY || swapRAndB || alphaConversion) {
1383 GrAssert(NULL != src);
1384 if (swapRAndB) {
1385 config = GrPixelConfigSwapRAndB(config);
1386 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001387 }
1388 // Make the scratch a render target because we don't have a robust
1389 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001390 GrTextureDesc desc;
1391 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1392 desc.fWidth = width;
1393 desc.fHeight = height;
1394 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001395
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001396 // When a full readback is faster than a partial we could always make
1397 // the scratch exactly match the passed rect. However, if we see many
1398 // different size rectangles we will trash our texture cache and pay the
1399 // cost of creating and destroying many textures. So, we only request
1400 // an exact match when the caller is reading an entire RT.
1401 ScratchTexMatch match = kApprox_ScratchTexMatch;
1402 if (0 == left &&
1403 0 == top &&
1404 target->width() == width &&
1405 target->height() == height &&
1406 fGpu->fullReadPixelsIsFasterThanPartial()) {
1407 match = kExact_ScratchTexMatch;
1408 }
1409 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001410 GrTexture* texture = ast.texture();
1411 if (!texture) {
1412 return false;
1413 }
1414 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001415 GrAssert(NULL != target);
1416
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001417 GrDrawTarget::AutoStateRestore asr(fGpu,
1418 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001419 GrDrawState* drawState = fGpu->drawState();
1420 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001421
bsalomon@google.comc4364992011-11-07 15:54:49 +00001422 GrMatrix matrix;
1423 if (flipY) {
1424 matrix.setTranslate(SK_Scalar1 * left,
1425 SK_Scalar1 * (top + height));
1426 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1427 } else {
1428 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1429 }
1430 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001431 drawState->sampler(0)->reset(matrix);
1432 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001434 GrRect rect;
1435 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1436 fGpu->drawSimpleRect(rect, NULL, 0x1);
1437 left = 0;
1438 top = 0;
1439 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001440 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001441 left, top, width, height,
1442 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001443}
1444
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001445void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1446 GrAssert(target);
1447 ASSERT_OWNED_RESOURCE(target);
1448 // In the future we may track whether there are any pending draws to this
1449 // target. We don't today so we always perform a flush. We don't promise
1450 // this to our clients, though.
1451 this->flush();
1452 fGpu->resolveRenderTarget(target);
1453}
1454
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001455void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1456 if (NULL == src || NULL == dst) {
1457 return;
1458 }
1459 ASSERT_OWNED_RESOURCE(src);
1460
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001461 // Writes pending to the source texture are not tracked, so a flush
1462 // is required to ensure that the copy captures the most recent contents
1463 // of the source texture. See similar behaviour in
1464 // GrContext::resolveRenderTarget.
1465 this->flush();
1466
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001467 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001468 GrDrawState* drawState = fGpu->drawState();
1469 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001470 GrMatrix sampleM;
1471 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001472 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001473 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001474 SkRect rect = SkRect::MakeXYWH(0, 0,
1475 SK_Scalar1 * src->width(),
1476 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001477 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1478}
1479
bsalomon@google.com6f379512011-11-16 20:36:03 +00001480void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1481 int left, int top,
1482 int width, int height,
1483 GrPixelConfig config,
1484 const void* buffer,
1485 size_t rowBytes,
1486 uint32_t flags) {
1487 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001488 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001489
1490 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001491 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001492 if (NULL == target) {
1493 return;
1494 }
1495 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001496
1497 // TODO: when underlying api has a direct way to do this we should use it
1498 // (e.g. glDrawPixels on desktop GL).
1499
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001500 // If the RT is also a texture and we don't have to do PM/UPM conversion
1501 // then take the texture path, which we expect to be at least as fast or
1502 // faster since it doesn't use an intermediate texture as we do below.
1503
1504#if !GR_MAC_BUILD
1505 // At least some drivers on the Mac get confused when glTexImage2D is called
1506 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1507 // determine what OS versions and/or HW is affected.
1508 if (NULL != target->asTexture() &&
1509 GrPixelConfigIsUnpremultiplied(target->config()) ==
1510 GrPixelConfigIsUnpremultiplied(config)) {
1511
1512 this->internalWriteTexturePixels(target->asTexture(),
1513 left, top, width, height,
1514 config, buffer, rowBytes, flags);
1515 return;
1516 }
1517#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001518 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1519 GrPixelConfigIsUnpremultiplied(config) &&
1520 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1521 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1522 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1523 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1524 return;
1525 }
1526 // allocate a tmp buffer and sw convert the pixels to premul
1527 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1528 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1529 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1530 src, rowBytes, srcConfig8888,
1531 width, height);
1532 // upload the already premul pixels
1533 this->internalWriteRenderTargetPixels(target,
1534 left, top,
1535 width, height,
1536 target->config(),
1537 tmpPixels, 4 * width, flags);
1538 return;
1539 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001540
1541 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1542 GrPixelConfigSwapRAndB(config);
1543 if (swapRAndB) {
1544 config = GrPixelConfigSwapRAndB(config);
1545 }
1546
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001547 GrTextureDesc desc;
1548 desc.fWidth = width;
1549 desc.fHeight = height;
1550 desc.fConfig = config;
1551
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001552 GrAutoScratchTexture ast(this, desc);
1553 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001554 if (NULL == texture) {
1555 return;
1556 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001557 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1558 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001560 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001561 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001562
1563 GrMatrix matrix;
1564 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001565 drawState->setViewMatrix(matrix);
1566 drawState->setRenderTarget(target);
1567 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001568
bsalomon@google.com5c638652011-07-18 19:31:59 +00001569 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001570 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1571 GrSamplerState::kNearest_Filter,
1572 matrix);
1573 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001575 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001576 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001577 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1579 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001580 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001581 return;
1582 }
1583 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001584 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585}
1586////////////////////////////////////////////////////////////////////////////////
1587
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001588void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001589 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001590
1591 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1592 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001593 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001594 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001595 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001596 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001597 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001598 }
1599
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001601
1602 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1603 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001604 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001605 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001606 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001608 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001609 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001610
1611 // disable all stages not accessible via the paint
1612 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001613 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001614 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001615
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001616 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001617
1618 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001619 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001621 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 }
1623 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001624 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001625 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001626 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001628 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1630 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001631 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001632 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001633 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001634 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1635 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1636 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001637#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001638 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001639 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001640 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1641 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001642#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001643}
1644
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001645GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646 DrawCategory category) {
1647 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001648 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001649 fLastDrawCategory = category;
1650 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001651 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652 GrDrawTarget* target = fGpu;
1653 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001654 case kUnbuffered_DrawCategory:
1655 target = fGpu;
1656 break;
1657 case kBuffered_DrawCategory:
1658 target = fDrawBuffer;
1659 fDrawBuffer->setClip(fGpu->getClip());
1660 break;
1661 default:
1662 GrCrash("Unexpected DrawCategory.");
1663 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664 }
1665 return target;
1666}
1667
robertphillips@google.com72176b22012-05-23 13:19:12 +00001668/*
1669 * This method finds a path renderer that can draw the specified path on
1670 * the provided target.
1671 * Due to its expense, the software path renderer has split out so it can
1672 * can be individually allowed/disallowed via the "allowSW" boolean.
1673 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001674GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001675 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001676 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001677 bool antiAlias,
1678 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001679 if (NULL == fPathRendererChain) {
1680 fPathRendererChain =
1681 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1682 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001683
1684 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1685 target,
1686 antiAlias);
1687
1688 if (NULL == pr && allowSW) {
1689 if (NULL == fSoftwarePathRenderer) {
1690 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1691 }
1692
1693 pr = fSoftwarePathRenderer;
1694 }
1695
1696 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001697}
1698
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699////////////////////////////////////////////////////////////////////////////////
1700
bsalomon@google.com27847de2011-02-22 20:59:41 +00001701void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001702 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001703 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001704 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001705 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001706 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001710 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
1713const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001714 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001715}
1716
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001717bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1718 return fGpu->isConfigRenderable(config);
1719}
1720
bsalomon@google.com27847de2011-02-22 20:59:41 +00001721const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001722 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723}
1724
1725void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727}
1728
1729void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001730 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001731}
1732
1733static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1734 intptr_t mask = 1 << shift;
1735 if (pred) {
1736 bits |= mask;
1737 } else {
1738 bits &= ~mask;
1739 }
1740 return bits;
1741}
1742
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001743GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001744 ++THREAD_INSTANCE_COUNT;
1745
bsalomon@google.com27847de2011-02-22 20:59:41 +00001746 fGpu = gpu;
1747 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001748 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001749
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001750 fDrawState = new GrDrawState();
1751 fGpu->setDrawState(fDrawState);
1752
bsalomon@google.com30085192011-08-19 15:42:31 +00001753 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001754 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001755
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001756 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
1757 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758 fFontCache = new GrFontCache(fGpu);
1759
1760 fLastDrawCategory = kUnbuffered_DrawCategory;
1761
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001762 fDrawBuffer = NULL;
1763 fDrawBufferVBAllocPool = NULL;
1764 fDrawBufferIBAllocPool = NULL;
1765
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001766 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001767
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001768 this->setupDrawBuffer();
1769}
1770
1771void GrContext::setupDrawBuffer() {
1772
1773 GrAssert(NULL == fDrawBuffer);
1774 GrAssert(NULL == fDrawBufferVBAllocPool);
1775 GrAssert(NULL == fDrawBufferIBAllocPool);
1776
bsalomon@google.com92edd312012-04-04 21:40:21 +00001777#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001778 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001779 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001780 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1781 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001782 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001783 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001784 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001785 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1786
bsalomon@google.com471d4712011-08-23 15:45:25 +00001787 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1788 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001789 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001790#endif
1791
1792#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001793 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001794#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001795 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001796 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001797}
1798
bsalomon@google.com27847de2011-02-22 20:59:41 +00001799GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001800#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001801 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001802#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001803 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001804#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001805}
1806
1807const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1808 return fGpu->getQuadIndexBuffer();
1809}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001810
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001811GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1812 GrAutoScratchTexture* temp1,
1813 GrAutoScratchTexture* temp2,
1814 const SkRect& rect,
1815 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001816 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001817 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1818 GrClip oldClip = this->getClip();
1819 GrTexture* origTexture = srcTexture;
1820 GrAutoMatrix avm(this, GrMatrix::I());
1821 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001822 int scaleFactorX, radiusX;
1823 int scaleFactorY, radiusY;
1824 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1825 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001826
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 SkRect srcRect(rect);
1828 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1829 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001830 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1831 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001832 this->setClip(srcRect);
1833
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001834 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1835 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1836 kAlpha_8_GrPixelConfig == srcTexture->config());
1837
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001838 GrTextureDesc desc;
1839 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1840 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1841 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1842 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843
1844 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001845 if (temp2) {
1846 temp2->set(this, desc);
1847 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848
1849 GrTexture* dstTexture = temp1->texture();
1850 GrPaint paint;
1851 paint.reset();
1852 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1853
1854 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1855 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1856 srcTexture->height());
1857 this->setRenderTarget(dstTexture->asRenderTarget());
1858 SkRect dstRect(srcRect);
1859 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1860 i < scaleFactorY ? 0.5f : 1.0f);
1861 paint.setTexture(0, srcTexture);
1862 this->drawRectToRect(paint, dstRect, srcRect);
1863 srcRect = dstRect;
1864 SkTSwap(srcTexture, dstTexture);
1865 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001866 if (temp2 && dstTexture == origTexture) {
1867 dstTexture = temp2->texture();
1868 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 }
1870
robertphillips@google.com7a396332012-05-10 15:11:27 +00001871 SkIRect srcIRect;
1872 srcRect.roundOut(&srcIRect);
1873
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001876 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001877 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001878 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001879 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880 this->clear(&clearRect, 0x0);
1881 }
1882
1883 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001884 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1885 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001887 if (temp2 && dstTexture == origTexture) {
1888 dstTexture = temp2->texture();
1889 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001890 }
1891
1892 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001894 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001896 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001897 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898 this->clear(&clearRect, 0x0);
1899 }
1900
1901 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001902 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1903 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001905 if (temp2 && dstTexture == origTexture) {
1906 dstTexture = temp2->texture();
1907 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001908 }
1909
1910 if (scaleFactorX > 1 || scaleFactorY > 1) {
1911 // Clear one pixel to the right and below, to accommodate bilinear
1912 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001913 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1914 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001915 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001916 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1917 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 this->clear(&clearRect, 0x0);
1919 // FIXME: This should be mitchell, not bilinear.
1920 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1921 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1922 srcTexture->height());
1923 this->setRenderTarget(dstTexture->asRenderTarget());
1924 paint.setTexture(0, srcTexture);
1925 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001926 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001927 this->drawRectToRect(paint, dstRect, srcRect);
1928 srcRect = dstRect;
1929 SkTSwap(srcTexture, dstTexture);
1930 }
1931 this->setRenderTarget(oldRenderTarget);
1932 this->setClip(oldClip);
1933 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001934}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001935
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001936GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1937 const GrRect& rect,
1938 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001939 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001940 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001941 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001942 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1943 GrAutoMatrix avm(this, GrMatrix::I());
1944 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001945 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1946 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001947 if (radius.fWidth > 0) {
1948 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001949 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1950 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001951 SkIRect clearRect = SkIRect::MakeXYWH(
1952 SkScalarFloorToInt(rect.fLeft),
1953 SkScalarFloorToInt(rect.fBottom),
1954 SkScalarFloorToInt(rect.width()),
1955 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001956 this->clear(&clearRect, 0x0);
1957 srcTexture = temp1;
1958 }
1959 if (radius.fHeight > 0) {
1960 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001961 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1962 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001963 srcTexture = temp2;
1964 }
1965 this->setRenderTarget(oldRenderTarget);
1966 this->setClip(oldClip);
1967 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001968}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001969
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001970void GrContext::postClipPush() {
1971 fGpu->postClipPush();
1972}
1973
1974void GrContext::preClipPop() {
1975 fGpu->preClipPop();
1976};
1977
bsalomon@google.comc4364992011-11-07 15:54:49 +00001978///////////////////////////////////////////////////////////////////////////////