blob: 2e5bd7fccf1e4832ad7b7155bb8af6162e65c20c [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"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000027#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000028
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000029#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000030
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000031#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000032
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000033#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000035#define MAX_BLUR_SIGMA 4.0f
36
bsalomon@google.comd46e2422011-09-23 17:40:07 +000037// When we're using coverage AA but the blend is incompatible (given gpu
38// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000039#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000040
reed@google.com4b2d3f32012-05-15 18:05:50 +000041#if GR_DEBUG
42 // change this to a 1 to see notifications when partial coverage fails
43 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
44#else
45 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
46#endif
47
bsalomon@google.com8ccaddd2011-08-09 16:49:03 +000048static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
49static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000050
bsalomon@google.com60361492012-03-15 17:47:06 +000051static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
53
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000054// path rendering is the only thing we defer today that uses non-static indices
55static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
56static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
59
bsalomon@google.com05ef5102011-05-02 21:14:59 +000060GrContext* GrContext::Create(GrEngine engine,
61 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000062 GrContext* ctx = NULL;
63 GrGpu* fGpu = GrGpu::Create(engine, context3D);
64 if (NULL != fGpu) {
65 ctx = new GrContext(fGpu);
66 fGpu->unref();
67 }
68 return ctx;
69}
70
bsalomon@google.com27847de2011-02-22 20:59:41 +000071GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000073
74 // Since the gpu can hold scratch textures, give it a chance to let go
75 // of them before freeing the texture cache
76 fGpu->purgeResources();
77
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 delete fTextureCache;
79 delete fFontCache;
80 delete fDrawBuffer;
81 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +000082 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +000083
robertphillips@google.comf6747b02012-06-12 00:32:28 +000084 fAARectRenderer->unref();
85
bsalomon@google.com205d4602011-04-25 12:43:45 +000086 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +000087 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +000088 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000089 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +000090}
91
bsalomon@google.com8fe72472011-03-30 21:26:44 +000092void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +000093 contextDestroyed();
94 this->setupDrawBuffer();
95}
96
97void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +000098 // abandon first to so destructors
99 // don't try to free the resources in the API.
100 fGpu->abandonResources();
101
bsalomon@google.com30085192011-08-19 15:42:31 +0000102 // a path renderer may be holding onto resources that
103 // are now unusable
104 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000105 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000106
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 delete fDrawBuffer;
108 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000109
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110 delete fDrawBufferVBAllocPool;
111 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000112
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113 delete fDrawBufferIBAllocPool;
114 fDrawBufferIBAllocPool = NULL;
115
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000116 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000117
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000118 fTextureCache->removeAll();
119 fFontCache->freeAll();
120 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121}
122
123void GrContext::resetContext() {
124 fGpu->markContextDirty();
125}
126
127void GrContext::freeGpuResources() {
128 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000129
130 fGpu->purgeResources();
131
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000132 fAARectRenderer->reset();
133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 fTextureCache->removeAll();
135 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000136 // a path renderer may be holding onto resources
137 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000138 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000139}
140
twiz@google.com05e70242012-01-27 19:12:00 +0000141size_t GrContext::getGpuTextureCacheBytes() const {
142 return fTextureCache->getCachedResourceBytes();
143}
144
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000145////////////////////////////////////////////////////////////////////////////////
146
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000147int GrContext::PaintStageVertexLayoutBits(
148 const GrPaint& paint,
149 const bool hasTexCoords[GrPaint::kTotalStages]) {
150 int stageMask = paint.getActiveStageMask();
151 int layout = 0;
152 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
153 if ((1 << i) & stageMask) {
154 if (NULL != hasTexCoords && hasTexCoords[i]) {
155 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
156 } else {
157 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
158 }
159 }
160 }
161 return layout;
162}
163
164
165////////////////////////////////////////////////////////////////////////////////
166
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000167GrTexture* GrContext::TextureCacheEntry::texture() const {
168 if (NULL == fEntry) {
169 return NULL;
170 } else {
171 return (GrTexture*) fEntry->resource();
172 }
173}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000174
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000176
177// we should never have more than one stencil buffer with same combo of
178// (width,height,samplecount)
179void gen_stencil_key_values(int width, int height,
180 int sampleCnt, uint32_t v[4]) {
181 v[0] = width;
182 v[1] = height;
183 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000184 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000185}
186
187void gen_stencil_key_values(const GrStencilBuffer* sb,
188 uint32_t v[4]) {
189 gen_stencil_key_values(sb->width(), sb->height(),
190 sb->numSamples(), v);
191}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000192
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000193void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000194 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
195 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
196 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
197 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000198}
199
bsalomon@google.comb505a122012-05-31 18:40:36 +0000200float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000201 *scaleFactor = 1;
202 while (sigma > MAX_BLUR_SIGMA) {
203 *scaleFactor *= 2;
204 sigma *= 0.5f;
205 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000206 *radius = static_cast<int>(ceilf(sigma * 3.0f));
207 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000208 return sigma;
209}
210
211void apply_morphology(GrGpu* gpu,
212 GrTexture* texture,
213 const SkRect& rect,
214 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000215 GrContext::MorphologyType morphType,
216 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000217
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000218 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
219 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000220 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221 drawState->setRenderTarget(target);
222 GrMatrix sampleM;
223 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000224 drawState->sampler(0)->reset(sampleM);
225 SkAutoTUnref<GrCustomStage> morph(
226 new GrMorphologyEffect(direction, radius, morphType));
227 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000228 drawState->setTexture(0, texture);
229 gpu->drawSimpleRect(rect, NULL, 1 << 0);
230}
231
bsalomon@google.comb505a122012-05-31 18:40:36 +0000232void convolve_gaussian(GrGpu* gpu,
233 GrTexture* texture,
234 const SkRect& rect,
235 float sigma,
236 int radius,
237 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000238 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
239 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000240 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000241 drawState->setRenderTarget(target);
242 GrMatrix sampleM;
243 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000244 drawState->sampler(0)->reset(sampleM);
245 SkAutoTUnref<GrConvolutionEffect> conv(new
246 GrConvolutionEffect(direction, radius));
247 conv->setGaussianKernel(sigma);
248 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000249 drawState->setTexture(0, texture);
250 gpu->drawSimpleRect(rect, NULL, 1 << 0);
251}
252
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000253}
254
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000255GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000256 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000257 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000258 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000259 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
260 GrResourceCache::kNested_LockType));
261}
262
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000263bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000264 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000265 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000266 return fTextureCache->hasKey(resourceKey);
267}
268
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000269GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000270 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000271 uint32_t v[4];
272 gen_stencil_key_values(sb, v);
273 GrResourceKey resourceKey(v);
274 return fTextureCache->createAndLock(resourceKey, sb);
275}
276
277GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
278 int sampleCnt) {
279 uint32_t v[4];
280 gen_stencil_key_values(width, height, sampleCnt, v);
281 GrResourceKey resourceKey(v);
282 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
283 GrResourceCache::kSingle_LockType);
284 if (NULL != entry) {
285 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
286 return sb;
287 } else {
288 return NULL;
289 }
290}
291
292void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000293 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000294 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000295}
296
297static void stretchImage(void* dst,
298 int dstW,
299 int dstH,
300 void* src,
301 int srcW,
302 int srcH,
303 int bpp) {
304 GrFixed dx = (srcW << 16) / dstW;
305 GrFixed dy = (srcH << 16) / dstH;
306
307 GrFixed y = dy >> 1;
308
309 int dstXLimit = dstW*bpp;
310 for (int j = 0; j < dstH; ++j) {
311 GrFixed x = dx >> 1;
312 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
313 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
314 for (int i = 0; i < dstXLimit; i += bpp) {
315 memcpy((uint8_t*) dstRow + i,
316 (uint8_t*) srcRow + (x>>16)*bpp,
317 bpp);
318 x += dx;
319 }
320 y += dy;
321 }
322}
323
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000324GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000325 const GrSamplerState* sampler,
326 const GrTextureDesc& desc,
327 void* srcData,
328 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000329 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000330
331#if GR_DUMP_TEXTURE_UPLOAD
332 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
333#endif
334
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000335 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000336
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000337 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000338 desc, false);
339
340 if (GrTexture::NeedsResizing(resourceKey)) {
341 // The desired texture is NPOT and tiled but that isn't supported by
342 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000343 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000344 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000346
347 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000348 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000349 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000350 GrAssert(NULL != clampEntry.texture());
351 if (NULL == clampEntry.texture()) {
352 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000353 }
354 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000355 GrTextureDesc rtDesc = desc;
356 rtDesc.fFlags = rtDesc.fFlags |
357 kRenderTarget_GrTextureFlagBit |
358 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000359 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
360 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361
362 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
363
364 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000365 GrDrawTarget::AutoStateRestore asr(fGpu,
366 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000367 GrDrawState* drawState = fGpu->drawState();
368 drawState->setRenderTarget(texture->asRenderTarget());
369 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000370
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000371 GrSamplerState::Filter filter;
372 // if filtering is not desired then we want to ensure all
373 // texels in the resampled image are copies of texels from
374 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000375 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000376 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000377 } else {
378 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000379 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000380 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
381 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000382
383 static const GrVertexLayout layout =
384 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
385 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
386
387 if (arg.succeeded()) {
388 GrPoint* verts = (GrPoint*) arg.vertices();
389 verts[0].setIRectFan(0, 0,
390 texture->width(),
391 texture->height(),
392 2*sizeof(GrPoint));
393 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000394 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000395 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000396 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000398 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 } else {
400 // TODO: Our CPU stretch doesn't filter. But we create separate
401 // stretched textures when the sampler state is either filtered or
402 // not. Either implement filtered stretch blit on CPU or just create
403 // one when FBO case fails.
404
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000405 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000406 // no longer need to clamp at min RT size.
407 rtDesc.fWidth = GrNextPow2(desc.fWidth);
408 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000409 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000410 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000411 rtDesc.fWidth *
412 rtDesc.fHeight);
413 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
414 srcData, desc.fWidth, desc.fHeight, bpp);
415
416 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
417
418 GrTexture* texture = fGpu->createTexture(rtDesc,
419 stretchedPixels.get(),
420 stretchedRowBytes);
421 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000422 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000423 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000424 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425
426 } else {
427 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
428 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000429 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430 }
431 }
432 return entry;
433}
434
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000435GrContext::TextureCacheEntry GrContext::lockScratchTexture(
436 const GrTextureDesc& inDesc,
437 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000438 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000439 desc.fClientCacheID = kScratch_CacheID;
440
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000441 if (kExact_ScratchTexMatch != match) {
442 // bin by pow2 with a reasonable min
443 static const int MIN_SIZE = 256;
444 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
445 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
446 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000447
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000448 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000449 int origWidth = desc.fWidth;
450 int origHeight = desc.fHeight;
451 bool doubledW = false;
452 bool doubledH = false;
453
454 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000455 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000456 entry = fTextureCache->findAndLock(key,
457 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000458 // if we miss, relax the fit of the flags...
459 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000460 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000461 break;
462 }
463 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
464 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
465 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
466 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
467 } else if (!doubledW) {
468 desc.fFlags = inDesc.fFlags;
469 desc.fWidth *= 2;
470 doubledW = true;
471 } else if (!doubledH) {
472 desc.fFlags = inDesc.fFlags;
473 desc.fWidth = origWidth;
474 desc.fHeight *= 2;
475 doubledH = true;
476 } else {
477 break;
478 }
479
480 } while (true);
481
482 if (NULL == entry) {
483 desc.fFlags = inDesc.fFlags;
484 desc.fWidth = origWidth;
485 desc.fHeight = origHeight;
486 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
487 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000488 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000489 texture->desc(),
490 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000491 entry = fTextureCache->createAndLock(key, texture);
492 }
493 }
494
495 // If the caller gives us the same desc/sampler twice we don't want
496 // to return the same texture the second time (unless it was previously
497 // released). So we detach the entry from the cache and reattach at release.
498 if (NULL != entry) {
499 fTextureCache->detach(entry);
500 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000501 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000502}
503
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000504void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000505 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000506 // If this is a scratch texture we detached it from the cache
507 // while it was locked (to avoid two callers simultaneously getting
508 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000509 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000510 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000511 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000512 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000513 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514}
515
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000516GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000517 void* srcData,
518 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000519 GrTextureDesc descCopy = descIn;
520 descCopy.fClientCacheID = kUncached_CacheID;
521 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522}
523
524void GrContext::getTextureCacheLimits(int* maxTextures,
525 size_t* maxTextureBytes) const {
526 fTextureCache->getLimits(maxTextures, maxTextureBytes);
527}
528
529void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
530 fTextureCache->setLimits(maxTextures, maxTextureBytes);
531}
532
bsalomon@google.com91958362011-06-13 17:58:13 +0000533int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000534 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000535}
536
537int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000538 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539}
540
541///////////////////////////////////////////////////////////////////////////////
542
bsalomon@google.come269f212011-11-07 13:29:52 +0000543GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
544 return fGpu->createPlatformTexture(desc);
545}
546
547GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
548 return fGpu->createPlatformRenderTarget(desc);
549}
550
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000551///////////////////////////////////////////////////////////////////////////////
552
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000553bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000554 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000555 const GrDrawTarget::Caps& caps = fGpu->getCaps();
556 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557 return false;
558 }
559
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
561
562 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000563 bool tiled = NULL != sampler &&
564 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
565 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000566 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567 return false;
568 }
569 }
570 return true;
571}
572
573////////////////////////////////////////////////////////////////////////////////
574
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000575const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
576
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577void GrContext::setClip(const GrClip& clip) {
578 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000579 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580}
581
582void GrContext::setClip(const GrIRect& rect) {
583 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000584 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000585 fGpu->setClip(clip);
586}
587
588////////////////////////////////////////////////////////////////////////////////
589
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000590void GrContext::clear(const GrIRect* rect, const GrColor color) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000591 this->flush();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000592 fGpu->clear(rect, color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593}
594
595void GrContext::drawPaint(const GrPaint& paint) {
596 // set rect to be big enough to fill the space, but not super-huge, so we
597 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000598 GrRect r;
599 r.setLTRB(0, 0,
600 GrIntToScalar(getRenderTarget()->width()),
601 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000602 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000603 SkTLazy<GrPaint> tmpPaint;
604 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 GrAutoMatrix am;
606
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000607 // We attempt to map r by the inverse matrix and draw that. mapRect will
608 // map the four corners and bound them with a new rect. This will not
609 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000610 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000611 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 GrPrintf("Could not invert matrix");
613 return;
614 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615 inverse.mapRect(&r);
616 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000618 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 GrPrintf("Could not invert matrix");
620 return;
621 }
622 tmpPaint.set(paint);
623 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
624 p = tmpPaint.get();
625 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000626 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000628 // by definition this fills the entire clip, no need for AA
629 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 if (!tmpPaint.isValid()) {
631 tmpPaint.set(paint);
632 p = tmpPaint.get();
633 }
634 GrAssert(p == tmpPaint.get());
635 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000636 }
637 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638}
639
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640////////////////////////////////////////////////////////////////////////////////
641
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000642namespace {
643inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
644 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
645}
646}
647
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000648////////////////////////////////////////////////////////////////////////////////
649
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650/* create a triangle strip that strokes the specified triangle. There are 8
651 unique vertices, but we repreat the last 2 to close up. Alternatively we
652 could use an indices array, and then only send 8 verts, but not sure that
653 would be faster.
654 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000655static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656 GrScalar width) {
657 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000659
660 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
661 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
662 verts[2].set(rect.fRight - rad, rect.fTop + rad);
663 verts[3].set(rect.fRight + rad, rect.fTop - rad);
664 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
665 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
666 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
667 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
668 verts[8] = verts[0];
669 verts[9] = verts[1];
670}
671
reed@google.com20efde72011-05-09 17:00:02 +0000672/**
673 * Returns true if the rects edges are integer-aligned.
674 */
675static bool isIRect(const GrRect& r) {
676 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
677 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
678}
679
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681 const GrRect& rect,
682 GrScalar width,
683 const GrMatrix* matrix,
684 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 GrRect* devRect,
686 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000687 // we use a simple coverage ramp to do aa on axis-aligned rects
688 // we check if the rect will be axis-aligned, and the rect won't land on
689 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000690
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 // we are keeping around the "tweak the alpha" trick because
692 // it is our only hope for the fixed-pipe implementation.
693 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000694 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000696 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000697 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000698#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000699 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000700#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000702 } else {
703 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000704 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 const GrDrawState& drawState = target->getDrawState();
707 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 return false;
709 }
710
bsalomon@google.com471d4712011-08-23 15:45:25 +0000711 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 return false;
713 }
714
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000715 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 return false;
717 }
718
719 if (NULL != matrix &&
720 !matrix->preservesAxisAlignment()) {
721 return false;
722 }
723
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 if (NULL != matrix) {
726 combinedMatrix->preConcat(*matrix);
727 GrAssert(combinedMatrix->preservesAxisAlignment());
728 }
729
730 combinedMatrix->mapRect(devRect, rect);
731 devRect->sort();
732
733 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000734 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735 } else {
736 return true;
737 }
738}
739
bsalomon@google.com27847de2011-02-22 20:59:41 +0000740void GrContext::drawRect(const GrPaint& paint,
741 const GrRect& rect,
742 GrScalar width,
743 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000744 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
746 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000747 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000748
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 GrRect devRect = rect;
750 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000751 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000752 bool needAA = paint.fAntiAlias &&
753 !this->getRenderTarget()->isMultisampled();
754 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
755 &combinedMatrix, &devRect,
756 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757
758 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000759 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000760 if (width >= 0) {
761 GrVec strokeSize;;
762 if (width > 0) {
763 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000764 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 strokeSize.setAbs(strokeSize);
766 } else {
767 strokeSize.set(GR_Scalar1, GR_Scalar1);
768 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000769 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
770 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000772 fAARectRenderer->fillAARect(this->getGpu(), target,
773 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000774 }
775 return;
776 }
777
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778 if (width >= 0) {
779 // TODO: consider making static vertex buffers for these cases.
780 // Hairline could be done by just adding closing vertex to
781 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000782 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
783
bsalomon@google.com27847de2011-02-22 20:59:41 +0000784 static const int worstCaseVertCount = 10;
785 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
786
787 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000788 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789 return;
790 }
791
792 GrPrimitiveType primType;
793 int vertCount;
794 GrPoint* vertex = geo.positions();
795
796 if (width > 0) {
797 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000798 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000799 setStrokeRectStrip(vertex, rect, width);
800 } else {
801 // hairline
802 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000803 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000804 vertex[0].set(rect.fLeft, rect.fTop);
805 vertex[1].set(rect.fRight, rect.fTop);
806 vertex[2].set(rect.fRight, rect.fBottom);
807 vertex[3].set(rect.fLeft, rect.fBottom);
808 vertex[4].set(rect.fLeft, rect.fTop);
809 }
810
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000811 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 GrDrawState* drawState = target->drawState();
814 avmr.set(drawState);
815 drawState->preConcatViewMatrix(*matrix);
816 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817 }
818
819 target->drawNonIndexed(primType, 0, vertCount);
820 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000822 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000823 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
824 if (NULL == sqVB) {
825 GrPrintf("Failed to create static rect vb.\n");
826 return;
827 }
828 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000829 GrDrawState* drawState = target->drawState();
830 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000832 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000833 0, rect.height(), rect.fTop,
834 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000835
836 if (NULL != matrix) {
837 m.postConcat(*matrix);
838 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 drawState->preConcatViewMatrix(m);
840 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000841
bsalomon@google.com47059542012-06-06 20:51:20 +0000842 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000843#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000844 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000845#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 }
847}
848
849void GrContext::drawRectToRect(const GrPaint& paint,
850 const GrRect& dstRect,
851 const GrRect& srcRect,
852 const GrMatrix* dstMatrix,
853 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000854 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000855
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000856 // srcRect refers to paint's first texture
857 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000858 drawRect(paint, dstRect, -1, dstMatrix);
859 return;
860 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000861
bsalomon@google.com27847de2011-02-22 20:59:41 +0000862 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
863
864#if GR_STATIC_RECT_VB
865 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000867 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000868 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000869
870 GrMatrix m;
871
872 m.setAll(dstRect.width(), 0, dstRect.fLeft,
873 0, dstRect.height(), dstRect.fTop,
874 0, 0, GrMatrix::I()[8]);
875 if (NULL != dstMatrix) {
876 m.postConcat(*dstMatrix);
877 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000880 // srcRect refers to first stage
881 int otherStageMask = paint.getActiveStageMask() &
882 (~(1 << GrPaint::kFirstTextureStage));
883 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000885 }
886
bsalomon@google.com27847de2011-02-22 20:59:41 +0000887 m.setAll(srcRect.width(), 0, srcRect.fLeft,
888 0, srcRect.height(), srcRect.fTop,
889 0, 0, GrMatrix::I()[8]);
890 if (NULL != srcMatrix) {
891 m.postConcat(*srcMatrix);
892 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000893 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000895 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
896 if (NULL == sqVB) {
897 GrPrintf("Failed to create static rect vb.\n");
898 return;
899 }
900 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000901 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000902#else
903
904 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000905#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000907#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
909#endif
910
tomhudson@google.com93813632011-10-27 20:21:16 +0000911 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
912 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913 srcRects[0] = &srcRect;
914 srcMatrices[0] = srcMatrix;
915
916 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
917#endif
918}
919
920void GrContext::drawVertices(const GrPaint& paint,
921 GrPrimitiveType primitiveType,
922 int vertexCount,
923 const GrPoint positions[],
924 const GrPoint texCoords[],
925 const GrColor colors[],
926 const uint16_t indices[],
927 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000928 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929
930 GrDrawTarget::AutoReleaseGeometry geo;
931
932 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
933
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000934 bool hasTexCoords[GrPaint::kTotalStages] = {
935 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
936 0 // remaining stages use positions
937 };
938
939 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000940
941 if (NULL != colors) {
942 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000944 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945
946 if (sizeof(GrPoint) != vertexSize) {
947 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000948 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949 return;
950 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000951 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000952 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000953 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
954 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000955 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000956 NULL,
957 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000958 void* curVertex = geo.vertices();
959
960 for (int i = 0; i < vertexCount; ++i) {
961 *((GrPoint*)curVertex) = positions[i];
962
963 if (texOffsets[0] > 0) {
964 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
965 }
966 if (colorOffset > 0) {
967 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
968 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000969 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000970 }
971 } else {
972 target->setVertexSourceToArray(layout, positions, vertexCount);
973 }
974
bsalomon@google.com91958362011-06-13 17:58:13 +0000975 // we don't currently apply offscreen AA to this path. Need improved
976 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000977
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000978 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000979 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000980 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000981 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000982 target->drawNonIndexed(primitiveType, 0, vertexCount);
983 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000984}
985
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000986///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000987namespace {
988
bsalomon@google.com93c96602012-04-27 13:05:21 +0000989struct CircleVertex {
990 GrPoint fPos;
991 GrPoint fCenter;
992 GrScalar fOuterRadius;
993 GrScalar fInnerRadius;
994};
995
996/* Returns true if will map a circle to another circle. This can be true
997 * if the matrix only includes square-scale, rotation, translation.
998 */
999inline bool isSimilarityTransformation(const SkMatrix& matrix,
1000 SkScalar tol = SK_ScalarNearlyZero) {
1001 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1002 return true;
1003 }
1004 if (matrix.hasPerspective()) {
1005 return false;
1006 }
1007
1008 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1009 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1010 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1011 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1012
1013 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1014 return false;
1015 }
1016
1017 // it has scales or skews, but it could also be rotation, check it out.
1018 SkVector vec[2];
1019 vec[0].set(mx, sx);
1020 vec[1].set(sy, my);
1021
1022 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1023 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1024 SkScalarSquare(tol));
1025}
1026
1027}
1028
1029// TODO: strokeWidth can't be larger than zero right now.
1030// It will be fixed when drawPath() can handle strokes.
1031void GrContext::drawOval(const GrPaint& paint,
1032 const GrRect& rect,
1033 SkScalar strokeWidth) {
1034 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1035 kUnbuffered_DrawCategory;
1036 GrDrawTarget* target = this->prepareToDraw(paint, category);
1037 GrDrawState* drawState = target->drawState();
1038 GrMatrix vm = drawState->getViewMatrix();
1039
1040 if (!isSimilarityTransformation(vm) ||
1041 !paint.fAntiAlias ||
1042 rect.height() != rect.width()) {
1043 SkPath path;
1044 path.addOval(rect);
1045 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001046 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001047 this->internalDrawPath(paint, path, fill, NULL);
1048 return;
1049 }
1050
1051 const GrRenderTarget* rt = drawState->getRenderTarget();
1052 if (NULL == rt) {
1053 return;
1054 }
1055
1056 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1057
1058 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1059 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1060 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1061
1062 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1063 GrScalar radius = SkScalarHalf(rect.width());
1064
1065 vm.mapPoints(&center, 1);
1066 radius = vm.mapRadius(radius);
1067
1068 GrScalar outerRadius = radius;
1069 GrScalar innerRadius = 0;
1070 SkScalar halfWidth = 0;
1071 if (strokeWidth == 0) {
1072 halfWidth = SkScalarHalf(SK_Scalar1);
1073
1074 outerRadius += halfWidth;
1075 innerRadius = SkMaxScalar(0, radius - halfWidth);
1076 }
1077
1078 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1079 if (!geo.succeeded()) {
1080 GrPrintf("Failed to get space for vertices!\n");
1081 return;
1082 }
1083
1084 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1085
1086 SkScalar L = center.fX - outerRadius;
1087 SkScalar R = center.fX + outerRadius;
1088 SkScalar T = center.fY - outerRadius;
1089 SkScalar B = center.fY + outerRadius;
1090
1091 verts[0].fPos = SkPoint::Make(L, T);
1092 verts[1].fPos = SkPoint::Make(R, T);
1093 verts[2].fPos = SkPoint::Make(L, B);
1094 verts[3].fPos = SkPoint::Make(R, B);
1095
1096 for (int i = 0; i < 4; ++i) {
1097 // this goes to fragment shader, it should be in y-points-up space.
1098 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1099
1100 verts[i].fOuterRadius = outerRadius;
1101 verts[i].fInnerRadius = innerRadius;
1102 }
1103
1104 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001105 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001106}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001107
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001108void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001109 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001110
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001111 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001112 if (GrIsFillInverted(fill)) {
1113 this->drawPaint(paint);
1114 }
1115 return;
1116 }
1117
bsalomon@google.com93c96602012-04-27 13:05:21 +00001118 SkRect ovalRect;
1119 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1120 if (translate) {
1121 ovalRect.offset(*translate);
1122 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001123 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001124 this->drawOval(paint, ovalRect, width);
1125 return;
1126 }
1127
1128 internalDrawPath(paint, path, fill, translate);
1129}
1130
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001131void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001132 GrPathFill fill, const GrPoint* translate) {
1133
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001134 // Note that below we may sw-rasterize the path into a scratch texture.
1135 // Scratch textures can be recycled after they are returned to the texture
1136 // cache. This presents a potential hazard for buffered drawing. However,
1137 // the writePixels that uploads to the scratch will perform a flush so we're
1138 // OK.
1139 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1140 kUnbuffered_DrawCategory;
1141 GrDrawTarget* target = this->prepareToDraw(paint, category);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001142 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001143
bsalomon@google.com289533a2011-10-27 12:34:25 +00001144 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1145
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001146 // An Assumption here is that path renderer would use some form of tweaking
1147 // the src color (either the input alpha or in the frag shader) to implement
1148 // aa. If we have some future driver-mojo path AA that can do the right
1149 // thing WRT to the blend then we'll need some query on the PR.
1150 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001151#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001152 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001153#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001154 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001155 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001156
robertphillips@google.com72176b22012-05-23 13:19:12 +00001157 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001158 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001159#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001160 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001161#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001162 return;
1163 }
1164
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001165 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001166}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001167
bsalomon@google.com27847de2011-02-22 20:59:41 +00001168////////////////////////////////////////////////////////////////////////////////
1169
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001170void GrContext::flush(int flagsBitfield) {
1171 if (kDiscard_FlushBit & flagsBitfield) {
1172 fDrawBuffer->reset();
1173 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001174 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001175 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001176 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001177 fGpu->forceRenderTargetFlush();
1178 }
1179}
1180
bsalomon@google.com27847de2011-02-22 20:59:41 +00001181void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001182 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001183 // With addition of the AA clip path, flushing the draw buffer can
1184 // result in the generation of an AA clip mask. During this
1185 // process the SW path renderer may be invoked which recusively
1186 // calls this method (via internalWriteTexturePixels) creating
1187 // infinite recursion
1188 GrInOrderDrawBuffer* temp = fDrawBuffer;
1189 fDrawBuffer = NULL;
1190
1191 temp->flushTo(fGpu);
1192
1193 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001194 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001195}
1196
bsalomon@google.com6f379512011-11-16 20:36:03 +00001197void GrContext::internalWriteTexturePixels(GrTexture* texture,
1198 int left, int top,
1199 int width, int height,
1200 GrPixelConfig config,
1201 const void* buffer,
1202 size_t rowBytes,
1203 uint32_t flags) {
1204 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001205 ASSERT_OWNED_RESOURCE(texture);
1206
bsalomon@google.com6f379512011-11-16 20:36:03 +00001207 if (!(kDontFlush_PixelOpsFlag & flags)) {
1208 this->flush();
1209 }
1210 // TODO: use scratch texture to perform conversion
1211 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1212 GrPixelConfigIsUnpremultiplied(config)) {
1213 return;
1214 }
1215
1216 fGpu->writeTexturePixels(texture, left, top, width, height,
1217 config, buffer, rowBytes);
1218}
1219
1220bool GrContext::internalReadTexturePixels(GrTexture* texture,
1221 int left, int top,
1222 int width, int height,
1223 GrPixelConfig config,
1224 void* buffer,
1225 size_t rowBytes,
1226 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001227 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001228 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001229
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001230 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001231 GrRenderTarget* target = texture->asRenderTarget();
1232 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001233 return this->internalReadRenderTargetPixels(target,
1234 left, top, width, height,
1235 config, buffer, rowBytes,
1236 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001237 } else {
1238 return false;
1239 }
1240}
1241
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001242#include "SkConfig8888.h"
1243
1244namespace {
1245/**
1246 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1247 * formats are representable as Config8888 and so the function returns false
1248 * if the GrPixelConfig has no equivalent Config8888.
1249 */
1250bool grconfig_to_config8888(GrPixelConfig config,
1251 SkCanvas::Config8888* config8888) {
1252 switch (config) {
1253 case kRGBA_8888_PM_GrPixelConfig:
1254 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1255 return true;
1256 case kRGBA_8888_UPM_GrPixelConfig:
1257 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1258 return true;
1259 case kBGRA_8888_PM_GrPixelConfig:
1260 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1261 return true;
1262 case kBGRA_8888_UPM_GrPixelConfig:
1263 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1264 return true;
1265 default:
1266 return false;
1267 }
1268}
1269}
1270
bsalomon@google.com6f379512011-11-16 20:36:03 +00001271bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1272 int left, int top,
1273 int width, int height,
1274 GrPixelConfig config,
1275 void* buffer,
1276 size_t rowBytes,
1277 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001278 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001279 ASSERT_OWNED_RESOURCE(target);
1280
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001281 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001282 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001283 if (NULL == target) {
1284 return false;
1285 }
1286 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001287
bsalomon@google.com6f379512011-11-16 20:36:03 +00001288 if (!(kDontFlush_PixelOpsFlag & flags)) {
1289 this->flush();
1290 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001291
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001292 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1293 GrPixelConfigIsUnpremultiplied(config) &&
1294 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1295 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1296 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1297 !grconfig_to_config8888(config, &dstConfig8888)) {
1298 return false;
1299 }
1300 // do read back using target's own config
1301 this->internalReadRenderTargetPixels(target,
1302 left, top,
1303 width, height,
1304 target->config(),
1305 buffer, rowBytes,
1306 kDontFlush_PixelOpsFlag);
1307 // sw convert the pixels to unpremul config
1308 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1309 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1310 pixels, rowBytes, srcConfig8888,
1311 width, height);
1312 return true;
1313 }
1314
bsalomon@google.comc4364992011-11-07 15:54:49 +00001315 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001316 bool swapRAndB = NULL != src &&
1317 fGpu->preferredReadPixelsConfig(config) ==
1318 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001319
1320 bool flipY = NULL != src &&
1321 fGpu->readPixelsWillPayForYFlip(target, left, top,
1322 width, height, config,
1323 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001324 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1325 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001326
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001327 if (NULL == src && alphaConversion) {
1328 // we should fallback to cpu conversion here. This could happen when
1329 // we were given an external render target by the client that is not
1330 // also a texture (e.g. FBO 0 in GL)
1331 return false;
1332 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001333 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001334 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001335 if (flipY || swapRAndB || alphaConversion) {
1336 GrAssert(NULL != src);
1337 if (swapRAndB) {
1338 config = GrPixelConfigSwapRAndB(config);
1339 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001340 }
1341 // Make the scratch a render target because we don't have a robust
1342 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001343 GrTextureDesc desc;
1344 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1345 desc.fWidth = width;
1346 desc.fHeight = height;
1347 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001348
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001349 // When a full readback is faster than a partial we could always make
1350 // the scratch exactly match the passed rect. However, if we see many
1351 // different size rectangles we will trash our texture cache and pay the
1352 // cost of creating and destroying many textures. So, we only request
1353 // an exact match when the caller is reading an entire RT.
1354 ScratchTexMatch match = kApprox_ScratchTexMatch;
1355 if (0 == left &&
1356 0 == top &&
1357 target->width() == width &&
1358 target->height() == height &&
1359 fGpu->fullReadPixelsIsFasterThanPartial()) {
1360 match = kExact_ScratchTexMatch;
1361 }
1362 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363 GrTexture* texture = ast.texture();
1364 if (!texture) {
1365 return false;
1366 }
1367 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001368 GrAssert(NULL != target);
1369
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001370 GrDrawTarget::AutoStateRestore asr(fGpu,
1371 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 GrDrawState* drawState = fGpu->drawState();
1373 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001374
bsalomon@google.comc4364992011-11-07 15:54:49 +00001375 GrMatrix matrix;
1376 if (flipY) {
1377 matrix.setTranslate(SK_Scalar1 * left,
1378 SK_Scalar1 * (top + height));
1379 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1380 } else {
1381 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1382 }
1383 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001384 drawState->sampler(0)->reset(matrix);
1385 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001387 GrRect rect;
1388 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1389 fGpu->drawSimpleRect(rect, NULL, 0x1);
1390 left = 0;
1391 top = 0;
1392 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001393 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001394 left, top, width, height,
1395 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001396}
1397
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001398void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1399 GrAssert(target);
1400 ASSERT_OWNED_RESOURCE(target);
1401 // In the future we may track whether there are any pending draws to this
1402 // target. We don't today so we always perform a flush. We don't promise
1403 // this to our clients, though.
1404 this->flush();
1405 fGpu->resolveRenderTarget(target);
1406}
1407
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001408void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1409 if (NULL == src || NULL == dst) {
1410 return;
1411 }
1412 ASSERT_OWNED_RESOURCE(src);
1413
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001414 // Writes pending to the source texture are not tracked, so a flush
1415 // is required to ensure that the copy captures the most recent contents
1416 // of the source texture. See similar behaviour in
1417 // GrContext::resolveRenderTarget.
1418 this->flush();
1419
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001420 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001421 GrDrawState* drawState = fGpu->drawState();
1422 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001423 GrMatrix sampleM;
1424 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001426 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001427 SkRect rect = SkRect::MakeXYWH(0, 0,
1428 SK_Scalar1 * src->width(),
1429 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001430 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1431}
1432
bsalomon@google.com6f379512011-11-16 20:36:03 +00001433void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1434 int left, int top,
1435 int width, int height,
1436 GrPixelConfig config,
1437 const void* buffer,
1438 size_t rowBytes,
1439 uint32_t flags) {
1440 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001441 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001442
1443 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001444 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001445 if (NULL == target) {
1446 return;
1447 }
1448 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001449
1450 // TODO: when underlying api has a direct way to do this we should use it
1451 // (e.g. glDrawPixels on desktop GL).
1452
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001453 // If the RT is also a texture and we don't have to do PM/UPM conversion
1454 // then take the texture path, which we expect to be at least as fast or
1455 // faster since it doesn't use an intermediate texture as we do below.
1456
1457#if !GR_MAC_BUILD
1458 // At least some drivers on the Mac get confused when glTexImage2D is called
1459 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1460 // determine what OS versions and/or HW is affected.
1461 if (NULL != target->asTexture() &&
1462 GrPixelConfigIsUnpremultiplied(target->config()) ==
1463 GrPixelConfigIsUnpremultiplied(config)) {
1464
1465 this->internalWriteTexturePixels(target->asTexture(),
1466 left, top, width, height,
1467 config, buffer, rowBytes, flags);
1468 return;
1469 }
1470#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001471 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1472 GrPixelConfigIsUnpremultiplied(config) &&
1473 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1474 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1475 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1476 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1477 return;
1478 }
1479 // allocate a tmp buffer and sw convert the pixels to premul
1480 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1481 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1482 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1483 src, rowBytes, srcConfig8888,
1484 width, height);
1485 // upload the already premul pixels
1486 this->internalWriteRenderTargetPixels(target,
1487 left, top,
1488 width, height,
1489 target->config(),
1490 tmpPixels, 4 * width, flags);
1491 return;
1492 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001493
1494 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1495 GrPixelConfigSwapRAndB(config);
1496 if (swapRAndB) {
1497 config = GrPixelConfigSwapRAndB(config);
1498 }
1499
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001500 GrTextureDesc desc;
1501 desc.fWidth = width;
1502 desc.fHeight = height;
1503 desc.fConfig = config;
1504
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001505 GrAutoScratchTexture ast(this, desc);
1506 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001507 if (NULL == texture) {
1508 return;
1509 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001510 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1511 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001512
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001513 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001514 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001515
1516 GrMatrix matrix;
1517 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001518 drawState->setViewMatrix(matrix);
1519 drawState->setRenderTarget(target);
1520 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001521
bsalomon@google.com5c638652011-07-18 19:31:59 +00001522 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001523 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1524 GrSamplerState::kNearest_Filter,
1525 matrix);
1526 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527
1528 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1529 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001530 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001531 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1532 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001533 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001534 return;
1535 }
1536 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001537 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001538}
1539////////////////////////////////////////////////////////////////////////////////
1540
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001541void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001542
1543 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1544 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001545 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001546 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001547 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001548 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001549 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001550 }
1551
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001552 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001553
1554 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1555 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001556 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001557 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001558 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001559 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001560 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001561 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001562
1563 // disable all stages not accessible via the paint
1564 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001565 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001566 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001567
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001568 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569
1570 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001572 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001573 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574 }
1575 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001576 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001577 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001578 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001579 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001580 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1582 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001583 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001584 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001585 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001586 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1587 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1588 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001589#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001590 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001592 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1593 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001594#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001595}
1596
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001597GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598 DrawCategory category) {
1599 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001600 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001601 fLastDrawCategory = category;
1602 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001603 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001604 GrDrawTarget* target = fGpu;
1605 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001606 case kUnbuffered_DrawCategory:
1607 target = fGpu;
1608 break;
1609 case kBuffered_DrawCategory:
1610 target = fDrawBuffer;
1611 fDrawBuffer->setClip(fGpu->getClip());
1612 break;
1613 default:
1614 GrCrash("Unexpected DrawCategory.");
1615 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001616 }
1617 return target;
1618}
1619
robertphillips@google.com72176b22012-05-23 13:19:12 +00001620/*
1621 * This method finds a path renderer that can draw the specified path on
1622 * the provided target.
1623 * Due to its expense, the software path renderer has split out so it can
1624 * can be individually allowed/disallowed via the "allowSW" boolean.
1625 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001626GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001627 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001628 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001629 bool antiAlias,
1630 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001631 if (NULL == fPathRendererChain) {
1632 fPathRendererChain =
1633 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1634 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001635
1636 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1637 target,
1638 antiAlias);
1639
1640 if (NULL == pr && allowSW) {
1641 if (NULL == fSoftwarePathRenderer) {
1642 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1643 }
1644
1645 pr = fSoftwarePathRenderer;
1646 }
1647
1648 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001649}
1650
bsalomon@google.com27847de2011-02-22 20:59:41 +00001651////////////////////////////////////////////////////////////////////////////////
1652
bsalomon@google.com27847de2011-02-22 20:59:41 +00001653void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001654 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001655 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001656 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001657 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001658 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001659}
1660
1661GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001662 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001663}
1664
1665const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001666 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001667}
1668
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001669bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1670 return fGpu->isConfigRenderable(config);
1671}
1672
bsalomon@google.com27847de2011-02-22 20:59:41 +00001673const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001674 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001675}
1676
1677void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001678 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001679}
1680
1681void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001682 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683}
1684
1685static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1686 intptr_t mask = 1 << shift;
1687 if (pred) {
1688 bits |= mask;
1689 } else {
1690 bits &= ~mask;
1691 }
1692 return bits;
1693}
1694
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001695GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001696 fGpu = gpu;
1697 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001698 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001699
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001700 fDrawState = new GrDrawState();
1701 fGpu->setDrawState(fDrawState);
1702
bsalomon@google.com30085192011-08-19 15:42:31 +00001703 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001704 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001705
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001706 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
1707 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001708 fFontCache = new GrFontCache(fGpu);
1709
1710 fLastDrawCategory = kUnbuffered_DrawCategory;
1711
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001712 fDrawBuffer = NULL;
1713 fDrawBufferVBAllocPool = NULL;
1714 fDrawBufferIBAllocPool = NULL;
1715
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001716 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001717
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001718 this->setupDrawBuffer();
1719}
1720
1721void GrContext::setupDrawBuffer() {
1722
1723 GrAssert(NULL == fDrawBuffer);
1724 GrAssert(NULL == fDrawBufferVBAllocPool);
1725 GrAssert(NULL == fDrawBufferIBAllocPool);
1726
bsalomon@google.com92edd312012-04-04 21:40:21 +00001727#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001728 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001729 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001730 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1731 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001732 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001733 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001734 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001735 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1736
bsalomon@google.com471d4712011-08-23 15:45:25 +00001737 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1738 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001739 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001740#endif
1741
1742#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001743 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001744#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001745 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001746 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001747}
1748
bsalomon@google.com27847de2011-02-22 20:59:41 +00001749GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001750#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001751 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001752#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001753 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001754#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755}
1756
1757const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1758 return fGpu->getQuadIndexBuffer();
1759}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001760
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001761GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1762 GrAutoScratchTexture* temp1,
1763 GrAutoScratchTexture* temp2,
1764 const SkRect& rect,
1765 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001766 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001767 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1768 GrClip oldClip = this->getClip();
1769 GrTexture* origTexture = srcTexture;
1770 GrAutoMatrix avm(this, GrMatrix::I());
1771 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001772 int scaleFactorX, radiusX;
1773 int scaleFactorY, radiusY;
1774 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1775 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001776
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001777 SkRect srcRect(rect);
1778 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1779 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001780 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1781 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001782 this->setClip(srcRect);
1783
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001784 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1785 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1786 kAlpha_8_GrPixelConfig == srcTexture->config());
1787
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001788 GrTextureDesc desc;
1789 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1790 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1791 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1792 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001793
1794 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001795 if (temp2) {
1796 temp2->set(this, desc);
1797 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001798
1799 GrTexture* dstTexture = temp1->texture();
1800 GrPaint paint;
1801 paint.reset();
1802 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1803
1804 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1805 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1806 srcTexture->height());
1807 this->setRenderTarget(dstTexture->asRenderTarget());
1808 SkRect dstRect(srcRect);
1809 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1810 i < scaleFactorY ? 0.5f : 1.0f);
1811 paint.setTexture(0, srcTexture);
1812 this->drawRectToRect(paint, dstRect, srcRect);
1813 srcRect = dstRect;
1814 SkTSwap(srcTexture, dstTexture);
1815 // If temp2 is non-NULL, don't render back to origTexture
1816 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
1817 }
1818
robertphillips@google.com7a396332012-05-10 15:11:27 +00001819 SkIRect srcIRect;
1820 srcRect.roundOut(&srcIRect);
1821
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001822 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001823 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001824 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001826 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001827 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828 this->clear(&clearRect, 0x0);
1829 }
1830
1831 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001832 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1833 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001834 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001835 if (temp2 && dstTexture == origTexture) {
1836 dstTexture = temp2->texture();
1837 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 }
1839
1840 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001841 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001842 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001844 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001845 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 this->clear(&clearRect, 0x0);
1847 }
1848
1849 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001850 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1851 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001852 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001853 if (temp2 && dstTexture == origTexture) {
1854 dstTexture = temp2->texture();
1855 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001856 }
1857
1858 if (scaleFactorX > 1 || scaleFactorY > 1) {
1859 // Clear one pixel to the right and below, to accommodate bilinear
1860 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001861 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1862 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001863 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001864 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1865 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001866 this->clear(&clearRect, 0x0);
1867 // FIXME: This should be mitchell, not bilinear.
1868 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1869 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1870 srcTexture->height());
1871 this->setRenderTarget(dstTexture->asRenderTarget());
1872 paint.setTexture(0, srcTexture);
1873 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001874 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 this->drawRectToRect(paint, dstRect, srcRect);
1876 srcRect = dstRect;
1877 SkTSwap(srcTexture, dstTexture);
1878 }
1879 this->setRenderTarget(oldRenderTarget);
1880 this->setClip(oldClip);
1881 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001882}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001883
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001884GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1885 const GrRect& rect,
1886 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001887 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001888 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001889 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001890 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1891 GrAutoMatrix avm(this, GrMatrix::I());
1892 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001893 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1894 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895 if (radius.fWidth > 0) {
1896 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001897 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1898 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001899 SkIRect clearRect = SkIRect::MakeXYWH(
1900 SkScalarFloorToInt(rect.fLeft),
1901 SkScalarFloorToInt(rect.fBottom),
1902 SkScalarFloorToInt(rect.width()),
1903 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 this->clear(&clearRect, 0x0);
1905 srcTexture = temp1;
1906 }
1907 if (radius.fHeight > 0) {
1908 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001909 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1910 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001911 srcTexture = temp2;
1912 }
1913 this->setRenderTarget(oldRenderTarget);
1914 this->setClip(oldClip);
1915 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001916}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001917
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001918void GrContext::postClipPush() {
1919 fGpu->postClipPush();
1920}
1921
1922void GrContext::preClipPop() {
1923 fGpu->preClipPop();
1924};
1925
bsalomon@google.comc4364992011-11-07 15:54:49 +00001926///////////////////////////////////////////////////////////////////////////////