OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "GrContext.h" | |
10 | |
11 #include "GrAARectRenderer.h" | 9 #include "GrAARectRenderer.h" |
12 #include "GrAtlasTextContext.h" | |
13 #include "GrBatch.h" | 10 #include "GrBatch.h" |
14 #include "GrBatchFontCache.h" | |
15 #include "GrBatchTarget.h" | |
16 #include "GrBatchTest.h" | 11 #include "GrBatchTest.h" |
17 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
18 #include "GrGpuResource.h" | 13 #include "GrDrawContext.h" |
19 #include "GrGpuResourcePriv.h" | |
20 #include "GrDrawTargetCaps.h" | |
21 #include "GrGpu.h" | |
22 #include "GrImmediateDrawTarget.h" | |
23 #include "GrIndexBuffer.h" | |
24 #include "GrInOrderDrawBuffer.h" | |
25 #include "GrLayerCache.h" | |
26 #include "GrOvalRenderer.h" | 14 #include "GrOvalRenderer.h" |
27 #include "GrPathRenderer.h" | 15 #include "GrPathRenderer.h" |
28 #include "GrPathUtils.h" | |
29 #include "GrRenderTargetPriv.h" | |
30 #include "GrResourceCache.h" | |
31 #include "GrResourceProvider.h" | |
32 #include "GrSoftwarePathRenderer.h" | |
33 #include "GrStencilAndCoverTextContext.h" | |
34 #include "GrStrokeInfo.h" | |
35 #include "GrSurfacePriv.h" | |
36 #include "GrTextBlobCache.h" | |
37 #include "GrTexturePriv.h" | |
38 #include "GrTraceMarker.h" | |
39 #include "GrTracing.h" | |
40 #include "GrVertices.h" | |
41 #include "SkDashPathPriv.h" | |
42 #include "SkConfig8888.h" | |
43 #include "SkGr.h" | |
44 #include "SkRRect.h" | |
45 #include "SkStrokeRec.h" | |
46 #include "SkTLazy.h" | |
47 #include "SkTLS.h" | |
48 #include "SkTraceEvent.h" | |
49 | 16 |
50 #include "effects/GrConfigConversionEffect.h" | 17 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == fContext) |
51 #include "effects/GrDashingEffect.h" | 18 #define RETURN_IF_ABANDONED if (!fDrawTarget) { return; } |
52 #include "effects/GrSingleTextureEffect.h" | 19 #define RETURN_FALSE_IF_ABANDONED if (!fDrawTarget) { return false; } |
| 20 #define RETURN_NULL_IF_ABANDONED if (!fDrawTarget) { return NULL; } |
53 | 21 |
54 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) | 22 class AutoCheckFlush { |
55 #define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; } | |
56 #define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; } | |
57 #define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; } | |
58 | |
59 class GrContext::AutoCheckFlush { | |
60 public: | 23 public: |
61 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context);
} | 24 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context);
} |
62 | 25 ~AutoCheckFlush() { fContext->flushIfNecessary(); } |
63 ~AutoCheckFlush() { | |
64 if (fContext->fFlushToReduceCacheSize) { | |
65 fContext->flush(); | |
66 } | |
67 } | |
68 | 26 |
69 private: | 27 private: |
70 GrContext* fContext; | 28 GrContext* fContext; |
71 }; | 29 }; |
72 | 30 |
73 GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, | 31 GrDrawContext::GrDrawContext(GrContext* context, GrDrawTarget* drawTarget) |
74 const Options* opts) { | 32 : fContext(context) |
75 GrContext* context; | 33 , fDrawTarget(SkRef(drawTarget)) { |
76 if (NULL == opts) { | |
77 context = SkNEW_ARGS(GrContext, (Options())); | |
78 } else { | |
79 context = SkNEW_ARGS(GrContext, (*opts)); | |
80 } | |
81 | |
82 if (context->init(backend, backendContext)) { | |
83 return context; | |
84 } else { | |
85 context->unref(); | |
86 return NULL; | |
87 } | |
88 } | 34 } |
89 | 35 |
90 static int32_t gNextID = 1; | 36 void GrDrawContext::copySurface(GrRenderTarget* dst, GrSurface* src, |
91 static int32_t next_id() { | 37 const SkIRect& srcRect, const SkIPoint& dstPoint
) { |
92 int32_t id; | 38 if (!this->prepareToDraw(dst)) { |
93 do { | |
94 id = sk_atomic_inc(&gNextID); | |
95 } while (id == SK_InvalidGenID); | |
96 return id; | |
97 } | |
98 | |
99 GrContext::GrContext(const Options& opts) : fOptions(opts), fUniqueID(next_id())
{ | |
100 fGpu = NULL; | |
101 fResourceCache = NULL; | |
102 fResourceProvider = NULL; | |
103 fPathRendererChain = NULL; | |
104 fSoftwarePathRenderer = NULL; | |
105 fBatchFontCache = NULL; | |
106 fDrawBuffer = NULL; | |
107 fFlushToReduceCacheSize = false; | |
108 fAARectRenderer = NULL; | |
109 fOvalRenderer = NULL; | |
110 fMaxTextureSizeOverride = 1 << 20; | |
111 } | |
112 | |
113 bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { | |
114 SkASSERT(NULL == fGpu); | |
115 | |
116 fGpu = GrGpu::Create(backend, backendContext, this); | |
117 if (NULL == fGpu) { | |
118 return false; | |
119 } | |
120 this->initCommon(); | |
121 return true; | |
122 } | |
123 | |
124 void GrContext::initCommon() { | |
125 fResourceCache = SkNEW(GrResourceCache); | |
126 fResourceCache->setOverBudgetCallback(OverBudgetCB, this); | |
127 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache)); | |
128 | |
129 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); | |
130 | |
131 fAARectRenderer = SkNEW(GrAARectRenderer); | |
132 fOvalRenderer = SkNEW(GrOvalRenderer); | |
133 | |
134 fDidTestPMConversions = false; | |
135 | |
136 #ifdef IMMEDIATE_MODE | |
137 fDrawBuffer = SkNEW_ARGS(GrImmediateDrawTarget, (this)); | |
138 #else | |
139 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this)); | |
140 #endif | |
141 | |
142 // GrBatchFontCache will eventually replace GrFontCache | |
143 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this)); | |
144 | |
145 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB,
this))); | |
146 } | |
147 | |
148 GrContext::~GrContext() { | |
149 if (NULL == fGpu) { | |
150 return; | 39 return; |
151 } | 40 } |
152 | 41 |
153 this->flush(); | 42 fDrawTarget->copySurface(dst, src, srcRect, dstPoint); |
154 | |
155 for (int i = 0; i < fCleanUpData.count(); ++i) { | |
156 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); | |
157 } | |
158 | |
159 SkDELETE(fResourceProvider); | |
160 SkDELETE(fResourceCache); | |
161 SkDELETE(fBatchFontCache); | |
162 SkDELETE(fDrawBuffer); | |
163 | |
164 fAARectRenderer->unref(); | |
165 fOvalRenderer->unref(); | |
166 | |
167 fGpu->unref(); | |
168 SkSafeUnref(fPathRendererChain); | |
169 SkSafeUnref(fSoftwarePathRenderer); | |
170 } | 43 } |
171 | 44 |
172 void GrContext::abandonContext() { | 45 void GrDrawContext::drawText(GrPipelineBuilder* pipelineBuilder, GrBatch* batch)
{ |
173 fResourceProvider->abandon(); | 46 fDrawTarget->drawBatch(pipelineBuilder, batch); |
174 // abandon first to so destructors | |
175 // don't try to free the resources in the API. | |
176 fResourceCache->abandonAll(); | |
177 | |
178 fGpu->contextAbandoned(); | |
179 | |
180 // a path renderer may be holding onto resources that | |
181 // are now unusable | |
182 SkSafeSetNull(fPathRendererChain); | |
183 SkSafeSetNull(fSoftwarePathRenderer); | |
184 | |
185 SkDELETE(fDrawBuffer); | |
186 fDrawBuffer = NULL; | |
187 | |
188 fBatchFontCache->freeAll(); | |
189 fLayerCache->freeAll(); | |
190 fTextBlobCache->freeAll(); | |
191 } | 47 } |
192 | 48 |
193 void GrContext::resetContext(uint32_t state) { | 49 void GrDrawContext::drawPaths(GrPipelineBuilder* pipelineBuilder, |
194 fGpu->markContextDirty(state); | 50 const GrPathProcessor* pathProc, |
| 51 const GrPathRange* pathRange, |
| 52 const void* indices, |
| 53 GrDrawTarget::PathIndexType indexType, |
| 54 const float transformValues[], |
| 55 GrDrawTarget::PathTransformType transformType, |
| 56 int count, |
| 57 GrPathRendering::FillType fill) { |
| 58 fDrawTarget->drawPaths(pipelineBuilder, pathProc, pathRange, |
| 59 indices, indexType, |
| 60 transformValues, |
| 61 transformType, |
| 62 count, fill); |
195 } | 63 } |
196 | 64 |
197 void GrContext::freeGpuResources() { | 65 void GrDrawContext::discard(GrRenderTarget* renderTarget) { |
198 this->flush(); | 66 RETURN_IF_ABANDONED |
199 | 67 SkASSERT(renderTarget); |
200 if (fDrawBuffer) { | 68 AutoCheckFlush acf(fContext); |
201 fDrawBuffer->purgeResources(); | 69 if (!this->prepareToDraw(renderTarget)) { |
| 70 return; |
202 } | 71 } |
203 | 72 fDrawTarget->discard(renderTarget); |
204 fBatchFontCache->freeAll(); | |
205 fLayerCache->freeAll(); | |
206 // a path renderer may be holding onto resources | |
207 SkSafeSetNull(fPathRendererChain); | |
208 SkSafeSetNull(fSoftwarePathRenderer); | |
209 | |
210 fResourceCache->purgeAllUnlocked(); | |
211 } | 73 } |
212 | 74 |
213 void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes)
const { | 75 void GrDrawContext::clear(GrRenderTarget* renderTarget, |
214 if (resourceCount) { | 76 const SkIRect* rect, |
215 *resourceCount = fResourceCache->getBudgetedResourceCount(); | 77 const GrColor color, |
| 78 bool canIgnoreRect) { |
| 79 RETURN_IF_ABANDONED |
| 80 SkASSERT(renderTarget); |
| 81 |
| 82 AutoCheckFlush acf(fContext); |
| 83 if (!this->prepareToDraw(renderTarget)) { |
| 84 return; |
216 } | 85 } |
217 if (resourceBytes) { | 86 fDrawTarget->clear(rect, color, canIgnoreRect, renderTarget); |
218 *resourceBytes = fResourceCache->getBudgetedResourceBytes(); | |
219 } | |
220 } | 87 } |
221 | 88 |
222 GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget, | |
223 SkGpuDevice* gpuDevice, | |
224 const SkDeviceProperties& | |
225 leakyProperties, | |
226 bool enableDistanceFieldFonts) { | |
227 if (fGpu->caps()->shaderCaps()->pathRenderingSupport() && renderTarget->isMu
ltisampled()) { | |
228 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencil
Attachment(); | |
229 if (sb) { | |
230 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyPr
operties); | |
231 } | |
232 } | |
233 | 89 |
234 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties, enableDi
stanceFieldFonts); | 90 void GrDrawContext::drawPaint(GrRenderTarget* rt, |
235 } | 91 const GrClip& clip, |
236 | 92 const GrPaint& origPaint, |
237 //////////////////////////////////////////////////////////////////////////////// | 93 const SkMatrix& viewMatrix) { |
238 | |
239 bool GrContext::isConfigTexturable(GrPixelConfig config) const { | |
240 return fGpu->caps()->isConfigTexturable(config); | |
241 } | |
242 | |
243 bool GrContext::npotTextureTileSupport() const { | |
244 return fGpu->caps()->npotTextureTileSupport(); | |
245 } | |
246 | |
247 void GrContext::OverBudgetCB(void* data) { | |
248 SkASSERT(data); | |
249 | |
250 GrContext* context = reinterpret_cast<GrContext*>(data); | |
251 | |
252 // Flush the InOrderDrawBuffer to possibly free up some textures | |
253 context->fFlushToReduceCacheSize = true; | |
254 } | |
255 | |
256 void GrContext::TextBlobCacheOverBudgetCB(void* data) { | |
257 SkASSERT(data); | |
258 | |
259 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level,
therefore they | |
260 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The
solution is to move | |
261 // drawText calls to below the GrContext level, but this is not trivial beca
use they call | |
262 // drawPath on SkGpuDevice | |
263 GrContext* context = reinterpret_cast<GrContext*>(data); | |
264 context->flush(); | |
265 } | |
266 | |
267 int GrContext::getMaxTextureSize() const { | |
268 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride); | |
269 } | |
270 | |
271 int GrContext::getMaxRenderTargetSize() const { | |
272 return fGpu->caps()->maxRenderTargetSize(); | |
273 } | |
274 | |
275 int GrContext::getMaxSampleCount() const { | |
276 return fGpu->caps()->maxSampleCount(); | |
277 } | |
278 | |
279 /////////////////////////////////////////////////////////////////////////////// | |
280 | |
281 void GrContext::clear(const SkIRect* rect, | |
282 const GrColor color, | |
283 bool canIgnoreRect, | |
284 GrRenderTarget* renderTarget) { | |
285 RETURN_IF_ABANDONED | |
286 ASSERT_OWNED_RESOURCE(renderTarget); | |
287 SkASSERT(renderTarget); | |
288 | |
289 AutoCheckFlush acf(this); | |
290 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this); | |
291 GrDrawTarget* target = this->prepareToDraw(); | |
292 if (NULL == target) { | |
293 return; | |
294 } | |
295 target->clear(rect, color, canIgnoreRect, renderTarget); | |
296 } | |
297 | |
298 void GrContext::drawPaint(GrRenderTarget* rt, | |
299 const GrClip& clip, | |
300 const GrPaint& origPaint, | |
301 const SkMatrix& viewMatrix) { | |
302 RETURN_IF_ABANDONED | 94 RETURN_IF_ABANDONED |
303 // set rect to be big enough to fill the space, but not super-huge, so we | 95 // set rect to be big enough to fill the space, but not super-huge, so we |
304 // don't overflow fixed-point implementations | 96 // don't overflow fixed-point implementations |
305 SkRect r; | 97 SkRect r; |
306 r.setLTRB(0, 0, | 98 r.setLTRB(0, 0, |
307 SkIntToScalar(rt->width()), | 99 SkIntToScalar(rt->width()), |
308 SkIntToScalar(rt->height())); | 100 SkIntToScalar(rt->height())); |
309 SkTCopyOnFirstWrite<GrPaint> paint(origPaint); | 101 SkTCopyOnFirstWrite<GrPaint> paint(origPaint); |
310 | 102 |
311 // by definition this fills the entire clip, no need for AA | 103 // by definition this fills the entire clip, no need for AA |
(...skipping 14 matching lines...) Expand all Loading... |
326 } | 118 } |
327 inverse.mapRect(&r); | 119 inverse.mapRect(&r); |
328 this->drawRect(rt, clip, *paint, viewMatrix, r); | 120 this->drawRect(rt, clip, *paint, viewMatrix, r); |
329 } else { | 121 } else { |
330 SkMatrix localMatrix; | 122 SkMatrix localMatrix; |
331 if (!viewMatrix.invert(&localMatrix)) { | 123 if (!viewMatrix.invert(&localMatrix)) { |
332 SkDebugf("Could not invert matrix\n"); | 124 SkDebugf("Could not invert matrix\n"); |
333 return; | 125 return; |
334 } | 126 } |
335 | 127 |
336 AutoCheckFlush acf(this); | 128 AutoCheckFlush acf(fContext); |
337 GrPipelineBuilder pipelineBuilder; | 129 GrPipelineBuilder pipelineBuilder; |
338 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, p
aint, &acf); | 130 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, paint)) { |
339 if (NULL == target) { | |
340 return; | 131 return; |
341 } | 132 } |
342 | 133 |
343 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target); | 134 fDrawTarget->drawBWRect(&pipelineBuilder, |
344 target->drawRect(&pipelineBuilder, | 135 paint->getColor(), |
345 paint->getColor(), | 136 SkMatrix::I(), |
346 SkMatrix::I(), | 137 r, |
347 r, | 138 NULL, |
348 NULL, | 139 &localMatrix); |
349 &localMatrix); | |
350 } | 140 } |
351 } | 141 } |
352 | 142 |
353 //////////////////////////////////////////////////////////////////////////////// | |
354 | |
355 static inline bool is_irect(const SkRect& r) { | 143 static inline bool is_irect(const SkRect& r) { |
356 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) && | 144 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) && |
357 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom); | 145 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom); |
358 } | 146 } |
359 | 147 |
360 static bool apply_aa_to_rect(GrDrawTarget* target, | 148 static bool apply_aa_to_rect(GrDrawTarget* target, |
361 GrPipelineBuilder* pipelineBuilder, | 149 GrPipelineBuilder* pipelineBuilder, |
362 SkRect* devBoundRect, | 150 SkRect* devBoundRect, |
363 const SkRect& rect, | 151 const SkRect& rect, |
364 SkScalar strokeWidth, | 152 SkScalar strokeWidth, |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 bool fHairline; | 352 bool fHairline; |
565 }; | 353 }; |
566 | 354 |
567 const static int kVertsPerHairlineRect = 5; | 355 const static int kVertsPerHairlineRect = 5; |
568 const static int kVertsPerStrokeRect = 10; | 356 const static int kVertsPerStrokeRect = 10; |
569 | 357 |
570 BatchTracker fBatch; | 358 BatchTracker fBatch; |
571 SkSTArray<1, Geometry, true> fGeoData; | 359 SkSTArray<1, Geometry, true> fGeoData; |
572 }; | 360 }; |
573 | 361 |
574 void GrContext::drawRect(GrRenderTarget* rt, | 362 void GrDrawContext::drawRect(GrRenderTarget* rt, |
575 const GrClip& clip, | 363 const GrClip& clip, |
576 const GrPaint& paint, | 364 const GrPaint& paint, |
577 const SkMatrix& viewMatrix, | 365 const SkMatrix& viewMatrix, |
578 const SkRect& rect, | 366 const SkRect& rect, |
579 const GrStrokeInfo* strokeInfo) { | 367 const GrStrokeInfo* strokeInfo) { |
580 RETURN_IF_ABANDONED | 368 RETURN_IF_ABANDONED |
581 if (strokeInfo && strokeInfo->isDashed()) { | 369 if (strokeInfo && strokeInfo->isDashed()) { |
582 SkPath path; | 370 SkPath path; |
583 path.setIsVolatile(true); | 371 path.setIsVolatile(true); |
584 path.addRect(rect); | 372 path.addRect(rect); |
585 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo); | 373 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo); |
586 return; | 374 return; |
587 } | 375 } |
588 | 376 |
589 AutoCheckFlush acf(this); | 377 AutoCheckFlush acf(fContext); |
590 GrPipelineBuilder pipelineBuilder; | 378 GrPipelineBuilder pipelineBuilder; |
591 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 379 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
592 if (NULL == target) { | |
593 return; | 380 return; |
594 } | 381 } |
595 | 382 |
596 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target); | |
597 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getWidth(); | 383 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getWidth(); |
598 | 384 |
599 // Check if this is a full RT draw and can be replaced with a clear. We don'
t bother checking | 385 // Check if this is a full RT draw and can be replaced with a clear. We don'
t bother checking |
600 // cases where the RT is fully inside a stroke. | 386 // cases where the RT is fully inside a stroke. |
601 if (width < 0) { | 387 if (width < 0) { |
602 SkRect rtRect; | 388 SkRect rtRect; |
603 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect); | 389 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect); |
604 SkRect clipSpaceRTRect = rtRect; | 390 SkRect clipSpaceRTRect = rtRect; |
605 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType(); | 391 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType(); |
606 if (checkClip) { | 392 if (checkClip) { |
607 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX), | 393 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX), |
608 SkIntToScalar(clip.origin().fY)); | 394 SkIntToScalar(clip.origin().fY)); |
609 } | 395 } |
610 // Does the clip contain the entire RT? | 396 // Does the clip contain the entire RT? |
611 if (!checkClip || clip.quickContains(clipSpaceRTRect)) { | 397 if (!checkClip || clip.quickContains(clipSpaceRTRect)) { |
612 SkMatrix invM; | 398 SkMatrix invM; |
613 if (!viewMatrix.invert(&invM)) { | 399 if (!viewMatrix.invert(&invM)) { |
614 return; | 400 return; |
615 } | 401 } |
616 // Does the rect bound the RT? | 402 // Does the rect bound the RT? |
617 SkPoint srcSpaceRTQuad[4]; | 403 SkPoint srcSpaceRTQuad[4]; |
618 invM.mapRectToQuad(srcSpaceRTQuad, rtRect); | 404 invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
619 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && | 405 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
620 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && | 406 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
621 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && | 407 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
622 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { | 408 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
623 // Will it blend? | 409 // Will it blend? |
624 GrColor clearColor; | 410 GrColor clearColor; |
625 if (paint.isOpaqueAndConstantColor(&clearColor)) { | 411 if (paint.isOpaqueAndConstantColor(&clearColor)) { |
626 target->clear(NULL, clearColor, true, rt); | 412 fDrawTarget->clear(NULL, clearColor, true, rt); |
627 return; | 413 return; |
628 } | 414 } |
629 } | 415 } |
630 } | 416 } |
631 } | 417 } |
632 | 418 |
633 GrColor color = paint.getColor(); | 419 GrColor color = paint.getColor(); |
634 SkRect devBoundRect; | 420 SkRect devBoundRect; |
635 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isM
ultisampled(); | 421 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isM
ultisampled(); |
636 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRe
ct, rect, width, | 422 bool doAA = needAA && apply_aa_to_rect(fDrawTarget, &pipelineBuilder, &devBo
undRect, rect, |
637 viewMatrix, color); | 423 width, viewMatrix, color); |
638 | 424 |
639 if (doAA) { | 425 if (doAA) { |
640 if (width >= 0) { | 426 if (width >= 0) { |
641 fAARectRenderer->strokeAARect(target, | 427 GrAARectRenderer::StrokeAARect(fDrawTarget, |
642 &pipelineBuilder, | 428 &pipelineBuilder, |
643 color, | 429 color, |
644 viewMatrix, | 430 viewMatrix, |
645 rect, | 431 rect, |
646 devBoundRect, | 432 devBoundRect, |
647 *strokeInfo); | 433 *strokeInfo); |
648 } else { | 434 } else { |
649 // filled AA rect | 435 // filled AA rect |
650 fAARectRenderer->fillAARect(target, | 436 GrAARectRenderer::FillAARect(fDrawTarget, |
651 &pipelineBuilder, | 437 &pipelineBuilder, |
652 color, | 438 color, |
653 viewMatrix, | 439 viewMatrix, |
654 rect, | 440 rect, |
655 devBoundRect); | 441 devBoundRect); |
656 } | 442 } |
657 return; | 443 return; |
658 } | 444 } |
659 | 445 |
660 if (width >= 0) { | 446 if (width >= 0) { |
661 StrokeRectBatch::Geometry geometry; | 447 StrokeRectBatch::Geometry geometry; |
662 geometry.fViewMatrix = viewMatrix; | 448 geometry.fViewMatrix = viewMatrix; |
663 geometry.fColor = color; | 449 geometry.fColor = color; |
664 geometry.fRect = rect; | 450 geometry.fRect = rect; |
665 geometry.fStrokeWidth = width; | 451 geometry.fStrokeWidth = width; |
666 | 452 |
667 // Non-AA hairlines are snapped to pixel centers to make which pixels ar
e hit deterministic | 453 // Non-AA hairlines are snapped to pixel centers to make which pixels ar
e hit deterministic |
668 bool snapToPixelCenters = (0 == width && !rt->isMultisampled()); | 454 bool snapToPixelCenters = (0 == width && !rt->isMultisampled()); |
669 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry, snapToPixe
lCenters)); | 455 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry, snapToPixe
lCenters)); |
670 | 456 |
671 // Depending on sub-pixel coordinates and the particular GPU, we may los
e a corner of | 457 // Depending on sub-pixel coordinates and the particular GPU, we may los
e a corner of |
672 // hairline rects. We jam all the vertices to pixel centers to avoid thi
s, but not when MSAA | 458 // hairline rects. We jam all the vertices to pixel centers to avoid thi
s, but not when MSAA |
673 // is enabled because it can cause ugly artifacts. | 459 // is enabled because it can cause ugly artifacts. |
674 pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_
Flag, | 460 pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_
Flag, |
675 snapToPixelCenters); | 461 snapToPixelCenters); |
676 target->drawBatch(&pipelineBuilder, batch); | 462 fDrawTarget->drawBatch(&pipelineBuilder, batch); |
677 } else { | 463 } else { |
678 // filled BW rect | 464 // filled BW rect |
679 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect); | 465 fDrawTarget->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect); |
680 } | 466 } |
681 } | 467 } |
682 | 468 |
683 void GrContext::drawNonAARectToRect(GrRenderTarget* rt, | 469 void GrDrawContext::drawNonAARectToRect(GrRenderTarget* rt, |
684 const GrClip& clip, | 470 const GrClip& clip, |
685 const GrPaint& paint, | 471 const GrPaint& paint, |
686 const SkMatrix& viewMatrix, | 472 const SkMatrix& viewMatrix, |
687 const SkRect& rectToDraw, | 473 const SkRect& rectToDraw, |
688 const SkRect& localRect, | 474 const SkRect& localRect, |
689 const SkMatrix* localMatrix) { | 475 const SkMatrix* localMatrix) { |
690 RETURN_IF_ABANDONED | 476 RETURN_IF_ABANDONED |
691 AutoCheckFlush acf(this); | 477 AutoCheckFlush acf(fContext); |
692 GrPipelineBuilder pipelineBuilder; | 478 GrPipelineBuilder pipelineBuilder; |
693 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 479 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
694 if (NULL == target) { | |
695 return; | 480 return; |
696 } | 481 } |
697 | 482 |
698 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target); | 483 fDrawTarget->drawBWRect(&pipelineBuilder, |
699 | 484 paint.getColor(), |
700 target->drawRect(&pipelineBuilder, | 485 viewMatrix, |
701 paint.getColor(), | 486 rectToDraw, |
702 viewMatrix, | 487 &localRect, |
703 rectToDraw, | 488 localMatrix); |
704 &localRect, | |
705 localMatrix); | |
706 } | 489 } |
707 | 490 |
708 static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, | 491 static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, |
709 bool hasColors, | 492 bool hasColors, |
710 int* colorOffset, | 493 int* colorOffset, |
711 int* texOffset, | 494 int* texOffset, |
712 GrColor color, | 495 GrColor color, |
713 const SkMatrix& viewMatr
ix, | 496 const SkMatrix& viewMatr
ix, |
714 bool coverageIgnored) { | 497 bool coverageIgnored) { |
715 *texOffset = -1; | 498 *texOffset = -1; |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 bool fHasIndices; | 752 bool fHasIndices; |
970 bool fHasLocalCoords; | 753 bool fHasLocalCoords; |
971 int fVertexCount; | 754 int fVertexCount; |
972 int fIndexCount; | 755 int fIndexCount; |
973 }; | 756 }; |
974 | 757 |
975 BatchTracker fBatch; | 758 BatchTracker fBatch; |
976 SkSTArray<1, Geometry, true> fGeoData; | 759 SkSTArray<1, Geometry, true> fGeoData; |
977 }; | 760 }; |
978 | 761 |
979 void GrContext::drawVertices(GrRenderTarget* rt, | 762 void GrDrawContext::drawVertices(GrRenderTarget* rt, |
980 const GrClip& clip, | 763 const GrClip& clip, |
981 const GrPaint& paint, | 764 const GrPaint& paint, |
982 const SkMatrix& viewMatrix, | 765 const SkMatrix& viewMatrix, |
983 GrPrimitiveType primitiveType, | 766 GrPrimitiveType primitiveType, |
984 int vertexCount, | 767 int vertexCount, |
985 const SkPoint positions[], | 768 const SkPoint positions[], |
986 const SkPoint texCoords[], | 769 const SkPoint texCoords[], |
987 const GrColor colors[], | 770 const GrColor colors[], |
988 const uint16_t indices[], | 771 const uint16_t indices[], |
989 int indexCount) { | 772 int indexCount) { |
990 RETURN_IF_ABANDONED | 773 RETURN_IF_ABANDONED |
991 AutoCheckFlush acf(this); | 774 AutoCheckFlush acf(fContext); |
992 GrPipelineBuilder pipelineBuilder; | 775 GrPipelineBuilder pipelineBuilder; |
993 | 776 |
994 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 777 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
995 if (NULL == target) { | |
996 return; | 778 return; |
997 } | 779 } |
998 | 780 |
999 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target); | |
1000 | |
1001 // TODO clients should give us bounds | 781 // TODO clients should give us bounds |
1002 SkRect bounds; | 782 SkRect bounds; |
1003 if (!bounds.setBoundsCheck(positions, vertexCount)) { | 783 if (!bounds.setBoundsCheck(positions, vertexCount)) { |
1004 SkDebugf("drawVertices call empty bounds\n"); | 784 SkDebugf("drawVertices call empty bounds\n"); |
1005 return; | 785 return; |
1006 } | 786 } |
1007 | 787 |
1008 viewMatrix.mapRect(&bounds); | 788 viewMatrix.mapRect(&bounds); |
1009 | 789 |
1010 // If we don't have AA then we outset for a half pixel in each direction to
account for | 790 // If we don't have AA then we outset for a half pixel in each direction to
account for |
1011 // snapping | 791 // snapping |
1012 if (!paint.isAntiAlias()) { | 792 if (!paint.isAntiAlias()) { |
1013 bounds.outset(0.5f, 0.5f); | 793 bounds.outset(0.5f, 0.5f); |
1014 } | 794 } |
1015 | 795 |
1016 DrawVerticesBatch::Geometry geometry; | 796 DrawVerticesBatch::Geometry geometry; |
1017 geometry.fColor = paint.getColor(); | 797 geometry.fColor = paint.getColor(); |
1018 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveTyp
e, viewMatrix, | 798 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveTyp
e, viewMatrix, |
1019 positions, vertexCount
, indices, | 799 positions, vertexCount
, indices, |
1020 indexCount, colors, te
xCoords, | 800 indexCount, colors, te
xCoords, |
1021 bounds)); | 801 bounds)); |
1022 | 802 |
1023 target->drawBatch(&pipelineBuilder, batch); | 803 fDrawTarget->drawBatch(&pipelineBuilder, batch); |
1024 } | 804 } |
1025 | 805 |
1026 /////////////////////////////////////////////////////////////////////////////// | 806 /////////////////////////////////////////////////////////////////////////////// |
1027 | 807 |
1028 void GrContext::drawRRect(GrRenderTarget*rt, | 808 void GrDrawContext::drawRRect(GrRenderTarget*rt, |
1029 const GrClip& clip, | 809 const GrClip& clip, |
1030 const GrPaint& paint, | 810 const GrPaint& paint, |
1031 const SkMatrix& viewMatrix, | 811 const SkMatrix& viewMatrix, |
1032 const SkRRect& rrect, | 812 const SkRRect& rrect, |
1033 const GrStrokeInfo& strokeInfo) { | 813 const GrStrokeInfo& strokeInfo) { |
1034 RETURN_IF_ABANDONED | 814 RETURN_IF_ABANDONED |
1035 if (rrect.isEmpty()) { | 815 if (rrect.isEmpty()) { |
1036 return; | 816 return; |
1037 } | 817 } |
1038 | 818 |
1039 if (strokeInfo.isDashed()) { | 819 if (strokeInfo.isDashed()) { |
1040 SkPath path; | 820 SkPath path; |
1041 path.setIsVolatile(true); | 821 path.setIsVolatile(true); |
1042 path.addRRect(rrect); | 822 path.addRRect(rrect); |
1043 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo); | 823 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo); |
1044 return; | 824 return; |
1045 } | 825 } |
1046 | 826 |
1047 AutoCheckFlush acf(this); | 827 AutoCheckFlush acf(fContext); |
1048 GrPipelineBuilder pipelineBuilder; | 828 GrPipelineBuilder pipelineBuilder; |
1049 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 829 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
1050 if (NULL == target) { | |
1051 return; | 830 return; |
1052 } | 831 } |
1053 | 832 |
1054 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target); | |
1055 | |
1056 GrColor color = paint.getColor(); | 833 GrColor color = paint.getColor(); |
1057 if (!fOvalRenderer->drawRRect(target, | 834 if (!GrOvalRenderer::DrawRRect(fDrawTarget, |
1058 &pipelineBuilder, | 835 &pipelineBuilder, |
1059 color, | 836 color, |
1060 viewMatrix, | 837 viewMatrix, |
1061 paint.isAntiAlias(), | 838 paint.isAntiAlias(), |
1062 rrect, | 839 rrect, |
1063 strokeInfo)) { | 840 strokeInfo)) { |
1064 SkPath path; | 841 SkPath path; |
1065 path.setIsVolatile(true); | 842 path.setIsVolatile(true); |
1066 path.addRRect(rrect); | 843 path.addRRect(rrect); |
1067 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, pain
t.isAntiAlias(), | 844 this->internalDrawPath(fDrawTarget, &pipelineBuilder, viewMatrix, color, |
1068 path, strokeInfo); | 845 paint.isAntiAlias(), path, strokeInfo); |
1069 } | 846 } |
1070 } | 847 } |
1071 | 848 |
1072 /////////////////////////////////////////////////////////////////////////////// | 849 /////////////////////////////////////////////////////////////////////////////// |
1073 | 850 |
1074 void GrContext::drawDRRect(GrRenderTarget* rt, | 851 void GrDrawContext::drawDRRect(GrRenderTarget* rt, |
1075 const GrClip& clip, | 852 const GrClip& clip, |
1076 const GrPaint& paint, | 853 const GrPaint& paint, |
1077 const SkMatrix& viewMatrix, | 854 const SkMatrix& viewMatrix, |
1078 const SkRRect& outer, | 855 const SkRRect& outer, |
1079 const SkRRect& inner) { | 856 const SkRRect& inner) { |
1080 RETURN_IF_ABANDONED | 857 RETURN_IF_ABANDONED |
1081 if (outer.isEmpty()) { | 858 if (outer.isEmpty()) { |
1082 return; | 859 return; |
1083 } | 860 } |
1084 | 861 |
1085 AutoCheckFlush acf(this); | 862 AutoCheckFlush acf(fContext); |
1086 GrPipelineBuilder pipelineBuilder; | 863 GrPipelineBuilder pipelineBuilder; |
1087 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 864 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
1088 | 865 return; |
1089 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target); | 866 } |
1090 | 867 |
1091 GrColor color = paint.getColor(); | 868 GrColor color = paint.getColor(); |
1092 if (!fOvalRenderer->drawDRRect(target, | 869 if (!GrOvalRenderer::DrawDRRect(fDrawTarget, |
1093 &pipelineBuilder, | 870 &pipelineBuilder, |
1094 color, | 871 color, |
1095 viewMatrix, | 872 viewMatrix, |
1096 paint.isAntiAlias(), | 873 paint.isAntiAlias(), |
1097 outer, | 874 outer, |
1098 inner)) { | 875 inner)) { |
1099 SkPath path; | 876 SkPath path; |
1100 path.setIsVolatile(true); | 877 path.setIsVolatile(true); |
1101 path.addRRect(inner); | 878 path.addRRect(inner); |
1102 path.addRRect(outer); | 879 path.addRRect(outer); |
1103 path.setFillType(SkPath::kEvenOdd_FillType); | 880 path.setFillType(SkPath::kEvenOdd_FillType); |
| 881 |
1104 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle); | 882 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle); |
1105 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, pain
t.isAntiAlias(), | 883 this->internalDrawPath(fDrawTarget, &pipelineBuilder, viewMatrix, color, |
1106 path, fillRec); | 884 paint.isAntiAlias(), path, fillRec); |
1107 } | 885 } |
1108 } | 886 } |
1109 | 887 |
1110 /////////////////////////////////////////////////////////////////////////////// | 888 /////////////////////////////////////////////////////////////////////////////// |
1111 | 889 |
1112 void GrContext::drawOval(GrRenderTarget* rt, | 890 void GrDrawContext::drawOval(GrRenderTarget* rt, |
1113 const GrClip& clip, | 891 const GrClip& clip, |
1114 const GrPaint& paint, | 892 const GrPaint& paint, |
1115 const SkMatrix& viewMatrix, | 893 const SkMatrix& viewMatrix, |
1116 const SkRect& oval, | 894 const SkRect& oval, |
1117 const GrStrokeInfo& strokeInfo) { | 895 const GrStrokeInfo& strokeInfo) { |
1118 RETURN_IF_ABANDONED | 896 RETURN_IF_ABANDONED |
1119 if (oval.isEmpty()) { | 897 if (oval.isEmpty()) { |
1120 return; | 898 return; |
1121 } | 899 } |
1122 | 900 |
1123 if (strokeInfo.isDashed()) { | 901 if (strokeInfo.isDashed()) { |
1124 SkPath path; | 902 SkPath path; |
1125 path.setIsVolatile(true); | 903 path.setIsVolatile(true); |
1126 path.addOval(oval); | 904 path.addOval(oval); |
1127 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo); | 905 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo); |
1128 return; | 906 return; |
1129 } | 907 } |
1130 | 908 |
1131 AutoCheckFlush acf(this); | 909 AutoCheckFlush acf(fContext); |
1132 GrPipelineBuilder pipelineBuilder; | 910 GrPipelineBuilder pipelineBuilder; |
1133 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 911 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
1134 if (NULL == target) { | |
1135 return; | 912 return; |
1136 } | 913 } |
1137 | 914 |
1138 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target); | |
1139 | |
1140 GrColor color = paint.getColor(); | 915 GrColor color = paint.getColor(); |
1141 if (!fOvalRenderer->drawOval(target, | 916 if (!GrOvalRenderer::DrawOval(fDrawTarget, |
1142 &pipelineBuilder, | 917 &pipelineBuilder, |
1143 color, | 918 color, |
1144 viewMatrix, | 919 viewMatrix, |
1145 paint.isAntiAlias(), | 920 paint.isAntiAlias(), |
1146 oval, | 921 oval, |
1147 strokeInfo)) { | 922 strokeInfo)) { |
1148 SkPath path; | 923 SkPath path; |
1149 path.setIsVolatile(true); | 924 path.setIsVolatile(true); |
1150 path.addOval(oval); | 925 path.addOval(oval); |
1151 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, pain
t.isAntiAlias(), | 926 this->internalDrawPath(fDrawTarget, &pipelineBuilder, viewMatrix, color, |
1152 path, strokeInfo); | 927 paint.isAntiAlias(), path, strokeInfo); |
1153 } | 928 } |
1154 } | 929 } |
1155 | 930 |
1156 // Can 'path' be drawn as a pair of filled nested rectangles? | 931 // Can 'path' be drawn as a pair of filled nested rectangles? |
1157 static bool is_nested_rects(GrDrawTarget* target, | 932 static bool is_nested_rects(GrDrawTarget* target, |
1158 GrPipelineBuilder* pipelineBuilder, | 933 GrPipelineBuilder* pipelineBuilder, |
1159 GrColor color, | 934 GrColor color, |
1160 const SkMatrix& viewMatrix, | 935 const SkMatrix& viewMatrix, |
1161 const SkPath& path, | 936 const SkPath& path, |
1162 const SkStrokeRec& stroke, | 937 const SkStrokeRec& stroke, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1199 allGoE1 = false; | 974 allGoE1 = false; |
1200 } | 975 } |
1201 if (!SkScalarNearlyEqual(margin, temp)) { | 976 if (!SkScalarNearlyEqual(margin, temp)) { |
1202 allEq = false; | 977 allEq = false; |
1203 } | 978 } |
1204 } | 979 } |
1205 | 980 |
1206 return allEq || allGoE1; | 981 return allEq || allGoE1; |
1207 } | 982 } |
1208 | 983 |
1209 void GrContext::drawPath(GrRenderTarget* rt, | 984 void GrDrawContext::drawPath(GrRenderTarget* rt, |
1210 const GrClip& clip, | 985 const GrClip& clip, |
1211 const GrPaint& paint, | 986 const GrPaint& paint, |
1212 const SkMatrix& viewMatrix, | 987 const SkMatrix& viewMatrix, |
1213 const SkPath& path, | 988 const SkPath& path, |
1214 const GrStrokeInfo& strokeInfo) { | 989 const GrStrokeInfo& strokeInfo) { |
1215 RETURN_IF_ABANDONED | 990 RETURN_IF_ABANDONED |
1216 if (path.isEmpty()) { | 991 if (path.isEmpty()) { |
1217 if (path.isInverseFillType()) { | 992 if (path.isInverseFillType()) { |
1218 this->drawPaint(rt, clip, paint, viewMatrix); | 993 this->drawPaint(rt, clip, paint, viewMatrix); |
1219 } | 994 } |
1220 return; | 995 return; |
1221 } | 996 } |
1222 | 997 |
1223 GrColor color = paint.getColor(); | 998 GrColor color = paint.getColor(); |
1224 | 999 |
1225 // Note that internalDrawPath may sw-rasterize the path into a scratch textu
re. | 1000 // Note that internalDrawPath may sw-rasterize the path into a scratch textu
re. |
1226 // Scratch textures can be recycled after they are returned to the texture | 1001 // Scratch textures can be recycled after they are returned to the texture |
1227 // cache. This presents a potential hazard for buffered drawing. However, | 1002 // cache. This presents a potential hazard for buffered drawing. However, |
1228 // the writePixels that uploads to the scratch will perform a flush so we're | 1003 // the writePixels that uploads to the scratch will perform a flush so we're |
1229 // OK. | 1004 // OK. |
1230 AutoCheckFlush acf(this); | 1005 AutoCheckFlush acf(fContext); |
1231 GrPipelineBuilder pipelineBuilder; | 1006 GrPipelineBuilder pipelineBuilder; |
1232 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &pain
t, &acf); | 1007 if (!this->prepareToDraw(&pipelineBuilder, rt, clip, &paint)) { |
1233 if (NULL == target) { | |
1234 return; | 1008 return; |
1235 } | 1009 } |
1236 | 1010 |
1237 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isC
onvex()); | |
1238 | |
1239 if (!strokeInfo.isDashed()) { | 1011 if (!strokeInfo.isDashed()) { |
1240 bool useCoverageAA = paint.isAntiAlias() && | 1012 bool useCoverageAA = paint.isAntiAlias() && |
1241 !pipelineBuilder.getRenderTarget()->isMultisampled(); | 1013 !pipelineBuilder.getRenderTarget()->isMultisampled(); |
1242 | 1014 |
1243 if (useCoverageAA && strokeInfo.getWidth() < 0 && !path.isConvex()) { | 1015 if (useCoverageAA && strokeInfo.getWidth() < 0 && !path.isConvex()) { |
1244 // Concave AA paths are expensive - try to avoid them for special ca
ses | 1016 // Concave AA paths are expensive - try to avoid them for special ca
ses |
1245 SkRect rects[2]; | 1017 SkRect rects[2]; |
1246 | 1018 |
1247 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, pat
h, strokeInfo, | 1019 if (is_nested_rects(fDrawTarget, &pipelineBuilder, color, viewMatrix
, path, strokeInfo, |
1248 rects)) { | 1020 rects)) { |
1249 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, col
or, viewMatrix, | 1021 GrAARectRenderer::FillAANestedRects(fDrawTarget, &pipelineBuilde
r, color, |
1250 rects); | 1022 viewMatrix, rects); |
1251 return; | 1023 return; |
1252 } | 1024 } |
1253 } | 1025 } |
1254 SkRect ovalRect; | 1026 SkRect ovalRect; |
1255 bool isOval = path.isOval(&ovalRect); | 1027 bool isOval = path.isOval(&ovalRect); |
1256 | 1028 |
1257 if (isOval && !path.isInverseFillType()) { | 1029 if (isOval && !path.isInverseFillType()) { |
1258 if (fOvalRenderer->drawOval(target, | 1030 if (GrOvalRenderer::DrawOval(fDrawTarget, |
1259 &pipelineBuilder, | 1031 &pipelineBuilder, |
1260 color, | 1032 color, |
1261 viewMatrix, | 1033 viewMatrix, |
1262 paint.isAntiAlias(), | 1034 paint.isAntiAlias(), |
1263 ovalRect, | 1035 ovalRect, |
1264 strokeInfo)) { | 1036 strokeInfo)) { |
1265 return; | 1037 return; |
1266 } | 1038 } |
1267 } | 1039 } |
1268 } | 1040 } |
1269 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.is
AntiAlias(), | 1041 this->internalDrawPath(fDrawTarget, &pipelineBuilder, viewMatrix, color, pai
nt.isAntiAlias(), |
1270 path, strokeInfo); | 1042 path, strokeInfo); |
1271 } | 1043 } |
1272 | 1044 |
1273 void GrContext::internalDrawPath(GrDrawTarget* target, | 1045 void GrDrawContext::internalDrawPath(GrDrawTarget* target, |
1274 GrPipelineBuilder* pipelineBuilder, | 1046 GrPipelineBuilder* pipelineBuilder, |
1275 const SkMatrix& viewMatrix, | 1047 const SkMatrix& viewMatrix, |
1276 GrColor color, | 1048 GrColor color, |
1277 bool useAA, | 1049 bool useAA, |
1278 const SkPath& path, | 1050 const SkPath& path, |
1279 const GrStrokeInfo& strokeInfo) { | 1051 const GrStrokeInfo& strokeInfo) { |
1280 RETURN_IF_ABANDONED | 1052 RETURN_IF_ABANDONED |
1281 SkASSERT(!path.isEmpty()); | 1053 SkASSERT(!path.isEmpty()); |
1282 | 1054 |
1283 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target); | |
1284 | |
1285 | 1055 |
1286 // An Assumption here is that path renderer would use some form of tweaking | 1056 // An Assumption here is that path renderer would use some form of tweaking |
1287 // the src color (either the input alpha or in the frag shader) to implement | 1057 // the src color (either the input alpha or in the frag shader) to implement |
1288 // aa. If we have some future driver-mojo path AA that can do the right | 1058 // aa. If we have some future driver-mojo path AA that can do the right |
1289 // thing WRT to the blend then we'll need some query on the PR. | 1059 // thing WRT to the blend then we'll need some query on the PR. |
1290 bool useCoverageAA = useAA && | 1060 bool useCoverageAA = useAA && |
1291 !pipelineBuilder->getRenderTarget()->isMultisampled(); | 1061 !pipelineBuilder->getRenderTarget()->isMultisampled(); |
1292 | 1062 |
1293 | 1063 |
1294 GrPathRendererChain::DrawType type = | 1064 GrPathRendererChain::DrawType type = |
1295 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType : | 1065 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType : |
1296 GrPathRendererChain::kColor_DrawType; | 1066 GrPathRendererChain::kColor_DrawType; |
1297 | 1067 |
1298 const SkPath* pathPtr = &path; | 1068 const SkPath* pathPtr = &path; |
1299 SkTLazy<SkPath> tmpPath; | 1069 SkTLazy<SkPath> tmpPath; |
1300 const GrStrokeInfo* strokeInfoPtr = &strokeInfo; | 1070 const GrStrokeInfo* strokeInfoPtr = &strokeInfo; |
1301 | 1071 |
1302 // Try a 1st time without stroking the path and without allowing the SW rend
erer | 1072 // Try a 1st time without stroking the path and without allowing the SW rend
erer |
1303 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatr
ix, *pathPtr, | 1073 GrPathRenderer* pr = fContext->getPathRenderer(target, pipelineBuilder, view
Matrix, *pathPtr, |
1304 *strokeInfoPtr, false, type); | 1074 *strokeInfoPtr, false, type)
; |
1305 | 1075 |
1306 GrStrokeInfo dashlessStrokeInfo(strokeInfo, false); | 1076 GrStrokeInfo dashlessStrokeInfo(strokeInfo, false); |
1307 if (NULL == pr && strokeInfo.isDashed()) { | 1077 if (NULL == pr && strokeInfo.isDashed()) { |
1308 // It didn't work above, so try again with dashed stroke converted to a
dashless stroke. | 1078 // It didn't work above, so try again with dashed stroke converted to a
dashless stroke. |
1309 if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pa
thPtr)) { | 1079 if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pa
thPtr)) { |
1310 return; | 1080 return; |
1311 } | 1081 } |
1312 pathPtr = tmpPath.get(); | 1082 pathPtr = tmpPath.get(); |
1313 if (pathPtr->isEmpty()) { | 1083 if (pathPtr->isEmpty()) { |
1314 return; | 1084 return; |
1315 } | 1085 } |
1316 strokeInfoPtr = &dashlessStrokeInfo; | 1086 strokeInfoPtr = &dashlessStrokeInfo; |
1317 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr
, *strokeInfoPtr, | 1087 pr = fContext->getPathRenderer(target, pipelineBuilder, viewMatrix, *pat
hPtr, *strokeInfoPtr, |
1318 false, type); | 1088 false, type); |
1319 } | 1089 } |
1320 | 1090 |
1321 if (NULL == pr) { | 1091 if (NULL == pr) { |
1322 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMa
trix, NULL) && | 1092 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMa
trix, NULL) && |
1323 !strokeInfoPtr->isFillStyle()) { | 1093 !strokeInfoPtr->isFillStyle()) { |
1324 // It didn't work above, so try again with stroke converted to a fil
l. | 1094 // It didn't work above, so try again with stroke converted to a fil
l. |
1325 if (!tmpPath.isValid()) { | 1095 if (!tmpPath.isValid()) { |
1326 tmpPath.init(); | 1096 tmpPath.init(); |
1327 } | 1097 } |
1328 dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale())
); | 1098 dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale())
); |
1329 if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) { | 1099 if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) { |
1330 return; | 1100 return; |
1331 } | 1101 } |
1332 pathPtr = tmpPath.get(); | 1102 pathPtr = tmpPath.get(); |
1333 if (pathPtr->isEmpty()) { | 1103 if (pathPtr->isEmpty()) { |
1334 return; | 1104 return; |
1335 } | 1105 } |
1336 dashlessStrokeInfo.setFillStyle(); | 1106 dashlessStrokeInfo.setFillStyle(); |
1337 strokeInfoPtr = &dashlessStrokeInfo; | 1107 strokeInfoPtr = &dashlessStrokeInfo; |
1338 } | 1108 } |
1339 | 1109 |
1340 // This time, allow SW renderer | 1110 // This time, allow SW renderer |
1341 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr
, *strokeInfoPtr, | 1111 pr = fContext->getPathRenderer(target, pipelineBuilder, viewMatrix, *pat
hPtr, *strokeInfoPtr, |
1342 true, type); | 1112 true, type); |
1343 } | 1113 } |
1344 | 1114 |
1345 if (NULL == pr) { | 1115 if (NULL == pr) { |
1346 #ifdef SK_DEBUG | 1116 #ifdef SK_DEBUG |
1347 SkDebugf("Unable to find path renderer compatible with path.\n"); | 1117 SkDebugf("Unable to find path renderer compatible with path.\n"); |
1348 #endif | 1118 #endif |
1349 return; | 1119 return; |
1350 } | 1120 } |
1351 | 1121 |
1352 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeIn
foPtr, useCoverageAA); | 1122 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeIn
foPtr, useCoverageAA); |
1353 } | 1123 } |
1354 | 1124 |
1355 //////////////////////////////////////////////////////////////////////////////// | 1125 bool GrDrawContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder, |
| 1126 GrRenderTarget* rt, |
| 1127 const GrClip& clip, |
| 1128 const GrPaint* paint) { |
| 1129 RETURN_FALSE_IF_ABANDONED |
1356 | 1130 |
1357 void GrContext::flush(int flagsBitfield) { | 1131 ASSERT_OWNED_RESOURCE(rt); |
1358 if (NULL == fDrawBuffer) { | 1132 SkASSERT(rt && paint); |
1359 return; | 1133 pipelineBuilder->setFromPaint(*paint, rt, clip); |
1360 } | |
1361 | |
1362 if (kDiscard_FlushBit & flagsBitfield) { | |
1363 fDrawBuffer->reset(); | |
1364 } else { | |
1365 fDrawBuffer->flush(); | |
1366 } | |
1367 fResourceCache->notifyFlushOccurred(); | |
1368 fFlushToReduceCacheSize = false; | |
1369 } | |
1370 | |
1371 bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t
inRowBytes, | |
1372 const void* inPixels, size_t outRowBytes, void* outPix
els) { | |
1373 SkSrcPixelInfo srcPI; | |
1374 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL))
{ | |
1375 return false; | |
1376 } | |
1377 srcPI.fAlphaType = kUnpremul_SkAlphaType; | |
1378 srcPI.fPixels = inPixels; | |
1379 srcPI.fRowBytes = inRowBytes; | |
1380 | |
1381 SkDstPixelInfo dstPI; | |
1382 dstPI.fColorType = srcPI.fColorType; | |
1383 dstPI.fAlphaType = kPremul_SkAlphaType; | |
1384 dstPI.fPixels = outPixels; | |
1385 dstPI.fRowBytes = outRowBytes; | |
1386 | |
1387 return srcPI.convertPixelsTo(&dstPI, width, height); | |
1388 } | |
1389 | |
1390 bool GrContext::writeSurfacePixels(GrSurface* surface, | |
1391 int left, int top, int width, int height, | |
1392 GrPixelConfig srcConfig, const void* buffer,
size_t rowBytes, | |
1393 uint32_t pixelOpsFlags) { | |
1394 RETURN_FALSE_IF_ABANDONED | |
1395 { | |
1396 GrTexture* texture = NULL; | |
1397 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asT
exture()) && | |
1398 fGpu->canWriteTexturePixels(texture, srcConfig)) { | |
1399 | |
1400 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && | |
1401 surface->surfacePriv().hasPendingIO()) { | |
1402 this->flush(); | |
1403 } | |
1404 return fGpu->writeTexturePixels(texture, left, top, width, height, | |
1405 srcConfig, buffer, rowBytes); | |
1406 // Don't need to check kFlushWrites_PixelOp here, we just did a dire
ct write so the | |
1407 // upload is already flushed. | |
1408 } | |
1409 } | |
1410 | |
1411 // If we didn't do a direct texture write then we upload the pixels to a tex
ture and draw. | |
1412 GrRenderTarget* renderTarget = surface->asRenderTarget(); | |
1413 if (NULL == renderTarget) { | |
1414 return false; | |
1415 } | |
1416 | |
1417 // We ignore the preferred config unless it is a R/B swap of the src config.
In that case | |
1418 // we will upload the original src data to a scratch texture but we will spo
of it as the swapped | |
1419 // config. This scratch will then have R and B swapped. We correct for this
by swapping again | |
1420 // when drawing the scratch to the dst using a conversion effect. | |
1421 bool swapRAndB = false; | |
1422 GrPixelConfig writeConfig = srcConfig; | |
1423 if (GrPixelConfigSwapRAndB(srcConfig) == | |
1424 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) { | |
1425 writeConfig = GrPixelConfigSwapRAndB(srcConfig); | |
1426 swapRAndB = true; | |
1427 } | |
1428 | |
1429 GrSurfaceDesc desc; | |
1430 desc.fWidth = width; | |
1431 desc.fHeight = height; | |
1432 desc.fConfig = writeConfig; | |
1433 SkAutoTUnref<GrTexture> texture(this->textureProvider()->refScratchTexture(d
esc, | |
1434 GrTextureProvider::kApprox_ScratchTexMatch)); | |
1435 if (!texture) { | |
1436 return false; | |
1437 } | |
1438 | |
1439 SkAutoTUnref<const GrFragmentProcessor> fp; | |
1440 SkMatrix textureMatrix; | |
1441 textureMatrix.setIDiv(texture->width(), texture->height()); | |
1442 | |
1443 // allocate a tmp buffer and sw convert the pixels to premul | |
1444 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); | |
1445 | |
1446 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) { | |
1447 if (!GrPixelConfigIs8888(srcConfig)) { | |
1448 return false; | |
1449 } | |
1450 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix)); | |
1451 // handle the unpremul step on the CPU if we couldn't create an effect t
o do it. | |
1452 if (NULL == fp) { | |
1453 size_t tmpRowBytes = 4 * width; | |
1454 tmpPixels.reset(width * height); | |
1455 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer
, tmpRowBytes, | |
1456 tmpPixels.get())) { | |
1457 return false; | |
1458 } | |
1459 rowBytes = tmpRowBytes; | |
1460 buffer = tmpPixels.get(); | |
1461 } | |
1462 } | |
1463 if (NULL == fp) { | |
1464 fp.reset(GrConfigConversionEffect::Create(texture, | |
1465 swapRAndB, | |
1466 GrConfigConversionEffect::kNon
e_PMConversion, | |
1467 textureMatrix)); | |
1468 } | |
1469 | |
1470 // Even if the client told us not to flush, we still flush here. The client
may have known that | |
1471 // writes to the original surface caused no data hazards, but they can't kno
w that the scratch | |
1472 // we just got is safe. | |
1473 if (texture->surfacePriv().hasPendingIO()) { | |
1474 this->flush(); | |
1475 } | |
1476 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height, | |
1477 writeConfig, buffer, rowBytes)) { | |
1478 return false; | |
1479 } | |
1480 | |
1481 SkMatrix matrix; | |
1482 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); | |
1483 | |
1484 // This function can be called in the midst of drawing another object (e.g.,
when uploading a | |
1485 // SW-rasterized clip while issuing a draw). So we push the current geometry
state before | |
1486 // drawing a rect to the render target. | |
1487 // The bracket ensures we pop the stack if we wind up flushing below. | |
1488 { | |
1489 GrDrawTarget* drawTarget = this->prepareToDraw(); | |
1490 if (!drawTarget) { | |
1491 return false; | |
1492 } | |
1493 | |
1494 GrPipelineBuilder pipelineBuilder; | |
1495 pipelineBuilder.addColorProcessor(fp); | |
1496 pipelineBuilder.setRenderTarget(renderTarget); | |
1497 drawTarget->drawSimpleRect(&pipelineBuilder, | |
1498 GrColor_WHITE, | |
1499 matrix, | |
1500 SkRect::MakeWH(SkIntToScalar(width), SkIntToS
calar(height))); | |
1501 } | |
1502 | |
1503 if (kFlushWrites_PixelOp & pixelOpsFlags) { | |
1504 this->flushSurfaceWrites(surface); | |
1505 } | |
1506 | |
1507 return true; | 1134 return true; |
1508 } | 1135 } |
1509 | 1136 |
1510 // toggles between RGBA and BGRA | 1137 bool GrDrawContext::prepareToDraw(GrRenderTarget* rt) { |
1511 static SkColorType toggle_colortype32(SkColorType ct) { | 1138 RETURN_FALSE_IF_ABANDONED |
1512 if (kRGBA_8888_SkColorType == ct) { | |
1513 return kBGRA_8888_SkColorType; | |
1514 } else { | |
1515 SkASSERT(kBGRA_8888_SkColorType == ct); | |
1516 return kRGBA_8888_SkColorType; | |
1517 } | |
1518 } | |
1519 | 1139 |
1520 bool GrContext::readRenderTargetPixels(GrRenderTarget* target, | 1140 ASSERT_OWNED_RESOURCE(rt); |
1521 int left, int top, int width, int height, | 1141 SkASSERT(rt); |
1522 GrPixelConfig dstConfig, void* buffer, si
ze_t rowBytes, | |
1523 uint32_t flags) { | |
1524 RETURN_FALSE_IF_ABANDONED | |
1525 ASSERT_OWNED_RESOURCE(target); | |
1526 SkASSERT(target); | |
1527 | |
1528 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWr
ite()) { | |
1529 this->flush(); | |
1530 } | |
1531 | |
1532 // Determine which conversions have to be applied: flipY, swapRAnd, and/or u
npremul. | |
1533 | |
1534 // If fGpu->readPixels would incur a y-flip cost then we will read the pixel
s upside down. We'll | |
1535 // either do the flipY by drawing into a scratch with a matrix or on the cpu
after the read. | |
1536 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top, | |
1537 width, height, dstConfig, | |
1538 rowBytes); | |
1539 // We ignore the preferred config if it is different than our config unless
it is an R/B swap. | |
1540 // In that case we'll perform an R and B swap while drawing to a scratch tex
ture of the swapped | |
1541 // config. Then we will call readPixels on the scratch with the swapped conf
ig. The swaps during | |
1542 // the draw cancels out the fact that we call readPixels with a config that
is R/B swapped from | |
1543 // dstConfig. | |
1544 GrPixelConfig readConfig = dstConfig; | |
1545 bool swapRAndB = false; | |
1546 if (GrPixelConfigSwapRAndB(dstConfig) == | |
1547 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) { | |
1548 readConfig = GrPixelConfigSwapRAndB(readConfig); | |
1549 swapRAndB = true; | |
1550 } | |
1551 | |
1552 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); | |
1553 | |
1554 if (unpremul && !GrPixelConfigIs8888(dstConfig)) { | |
1555 // The unpremul flag is only allowed for these two configs. | |
1556 return false; | |
1557 } | |
1558 | |
1559 SkAutoTUnref<GrTexture> tempTexture; | |
1560 | |
1561 // If the src is a texture and we would have to do conversions after read pi
xels, we instead | |
1562 // do the conversions by drawing the src to a scratch texture. If we handle
any of the | |
1563 // conversions in the draw we set the corresponding bool to false so that we
don't reapply it | |
1564 // on the read back pixels. | |
1565 GrTexture* src = target->asTexture(); | |
1566 if (src && (swapRAndB || unpremul || flipY)) { | |
1567 // Make the scratch a render so we can read its pixels. | |
1568 GrSurfaceDesc desc; | |
1569 desc.fFlags = kRenderTarget_GrSurfaceFlag; | |
1570 desc.fWidth = width; | |
1571 desc.fHeight = height; | |
1572 desc.fConfig = readConfig; | |
1573 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | |
1574 | |
1575 // When a full read back is faster than a partial we could always make t
he scratch exactly | |
1576 // match the passed rect. However, if we see many different size rectang
les we will trash | |
1577 // our texture cache and pay the cost of creating and destroying many te
xtures. So, we only | |
1578 // request an exact match when the caller is reading an entire RT. | |
1579 GrTextureProvider::ScratchTexMatch match = GrTextureProvider::kApprox_Sc
ratchTexMatch; | |
1580 if (0 == left && | |
1581 0 == top && | |
1582 target->width() == width && | |
1583 target->height() == height && | |
1584 fGpu->fullReadPixelsIsFasterThanPartial()) { | |
1585 match = GrTextureProvider::kExact_ScratchTexMatch; | |
1586 } | |
1587 tempTexture.reset(this->textureProvider()->refScratchTexture(desc, match
)); | |
1588 if (tempTexture) { | |
1589 // compute a matrix to perform the draw | |
1590 SkMatrix textureMatrix; | |
1591 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); | |
1592 textureMatrix.postIDiv(src->width(), src->height()); | |
1593 | |
1594 SkAutoTUnref<const GrFragmentProcessor> fp; | |
1595 if (unpremul) { | |
1596 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix
)); | |
1597 if (fp) { | |
1598 unpremul = false; // we no longer need to do this on CPU aft
er the read back. | |
1599 } | |
1600 } | |
1601 // If we failed to create a PM->UPM effect and have no other convers
ions to perform then | |
1602 // there is no longer any point to using the scratch. | |
1603 if (fp || flipY || swapRAndB) { | |
1604 if (!fp) { | |
1605 fp.reset(GrConfigConversionEffect::Create( | |
1606 src, swapRAndB, GrConfigConversionEffect::kNone_PMCo
nversion, | |
1607 textureMatrix)); | |
1608 } | |
1609 swapRAndB = false; // we will handle the swap in the draw. | |
1610 | |
1611 // We protect the existing geometry here since it may not be | |
1612 // clear to the caller that a draw operation (i.e., drawSimpleRe
ct) | |
1613 // can be invoked in this method | |
1614 { | |
1615 GrPipelineBuilder pipelineBuilder; | |
1616 SkASSERT(fp); | |
1617 pipelineBuilder.addColorProcessor(fp); | |
1618 | |
1619 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget(
)); | |
1620 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToSc
alar(height)); | |
1621 fDrawBuffer->drawSimpleRect(&pipelineBuilder, | |
1622 GrColor_WHITE, | |
1623 SkMatrix::I(), | |
1624 rect); | |
1625 // we want to read back from the scratch's origin | |
1626 left = 0; | |
1627 top = 0; | |
1628 target = tempTexture->asRenderTarget(); | |
1629 } | |
1630 this->flushSurfaceWrites(target); | |
1631 } | |
1632 } | |
1633 } | |
1634 | |
1635 if (!fGpu->readPixels(target, | |
1636 left, top, width, height, | |
1637 readConfig, buffer, rowBytes)) { | |
1638 return false; | |
1639 } | |
1640 // Perform any conversions we weren't able to perform using a scratch textur
e. | |
1641 if (unpremul || swapRAndB) { | |
1642 SkDstPixelInfo dstPI; | |
1643 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NUL
L)) { | |
1644 return false; | |
1645 } | |
1646 dstPI.fAlphaType = kUnpremul_SkAlphaType; | |
1647 dstPI.fPixels = buffer; | |
1648 dstPI.fRowBytes = rowBytes; | |
1649 | |
1650 SkSrcPixelInfo srcPI; | |
1651 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : ds
tPI.fColorType; | |
1652 srcPI.fAlphaType = kPremul_SkAlphaType; | |
1653 srcPI.fPixels = buffer; | |
1654 srcPI.fRowBytes = rowBytes; | |
1655 | |
1656 return srcPI.convertPixelsTo(&dstPI, width, height); | |
1657 } | |
1658 return true; | 1142 return true; |
1659 } | 1143 } |
1660 | 1144 |
1661 void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) { | |
1662 RETURN_IF_ABANDONED | |
1663 SkASSERT(surface); | |
1664 ASSERT_OWNED_RESOURCE(surface); | |
1665 if (surface->surfacePriv().hasPendingIO()) { | |
1666 this->flush(); | |
1667 } | |
1668 GrRenderTarget* rt = surface->asRenderTarget(); | |
1669 if (fGpu && rt) { | |
1670 fGpu->resolveRenderTarget(rt); | |
1671 } | |
1672 } | |
1673 | |
1674 void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) { | |
1675 RETURN_IF_ABANDONED | |
1676 SkASSERT(renderTarget); | |
1677 ASSERT_OWNED_RESOURCE(renderTarget); | |
1678 AutoCheckFlush acf(this); | |
1679 GrDrawTarget* target = this->prepareToDraw(); | |
1680 if (NULL == target) { | |
1681 return; | |
1682 } | |
1683 target->discard(renderTarget); | |
1684 } | |
1685 | |
1686 void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRe
ct, | |
1687 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) { | |
1688 RETURN_IF_ABANDONED | |
1689 if (NULL == src || NULL == dst) { | |
1690 return; | |
1691 } | |
1692 ASSERT_OWNED_RESOURCE(src); | |
1693 ASSERT_OWNED_RESOURCE(dst); | |
1694 | |
1695 // Since we're going to the draw target and not GPU, no need to check kNoFlu
sh | |
1696 // here. | |
1697 | |
1698 GrDrawTarget* target = this->prepareToDraw(); | |
1699 if (NULL == target) { | |
1700 return; | |
1701 } | |
1702 target->copySurface(dst, src, srcRect, dstPoint); | |
1703 | |
1704 if (kFlushWrites_PixelOp & pixelOpsFlags) { | |
1705 this->flush(); | |
1706 } | |
1707 } | |
1708 | |
1709 void GrContext::flushSurfaceWrites(GrSurface* surface) { | |
1710 RETURN_IF_ABANDONED | |
1711 if (surface->surfacePriv().hasPendingWrite()) { | |
1712 this->flush(); | |
1713 } | |
1714 } | |
1715 | |
1716 GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder, | |
1717 GrRenderTarget* rt, | |
1718 const GrClip& clip, | |
1719 const GrPaint* paint, | |
1720 const AutoCheckFlush* acf) { | |
1721 if (NULL == fGpu || NULL == fDrawBuffer) { | |
1722 return NULL; | |
1723 } | |
1724 | |
1725 ASSERT_OWNED_RESOURCE(rt); | |
1726 SkASSERT(rt && paint && acf); | |
1727 pipelineBuilder->setFromPaint(*paint, rt, clip); | |
1728 return fDrawBuffer; | |
1729 } | |
1730 | |
1731 GrDrawTarget* GrContext::prepareToDraw() { | |
1732 if (NULL == fGpu) { | |
1733 return NULL; | |
1734 } | |
1735 return fDrawBuffer; | |
1736 } | |
1737 | |
1738 /* | |
1739 * This method finds a path renderer that can draw the specified path on | |
1740 * the provided target. | |
1741 * Due to its expense, the software path renderer has split out so it can | |
1742 * can be individually allowed/disallowed via the "allowSW" boolean. | |
1743 */ | |
1744 GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target, | |
1745 const GrPipelineBuilder* pipelineBuil
der, | |
1746 const SkMatrix& viewMatrix, | |
1747 const SkPath& path, | |
1748 const GrStrokeInfo& stroke, | |
1749 bool allowSW, | |
1750 GrPathRendererChain::DrawType drawTyp
e, | |
1751 GrPathRendererChain::StencilSupport*
stencilSupport) { | |
1752 | |
1753 if (NULL == fPathRendererChain) { | |
1754 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this)); | |
1755 } | |
1756 | |
1757 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target, | |
1758 pipelineBuilder, | |
1759 viewMatrix, | |
1760 path, | |
1761 stroke, | |
1762 drawType, | |
1763 stencilSupport); | |
1764 | |
1765 if (NULL == pr && allowSW) { | |
1766 if (NULL == fSoftwarePathRenderer) { | |
1767 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this)); | |
1768 } | |
1769 pr = fSoftwarePathRenderer; | |
1770 } | |
1771 | |
1772 return pr; | |
1773 } | |
1774 | |
1775 //////////////////////////////////////////////////////////////////////////////// | |
1776 bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const { | |
1777 return fGpu->caps()->isConfigRenderable(config, withMSAA); | |
1778 } | |
1779 | |
1780 int GrContext::getRecommendedSampleCount(GrPixelConfig config, | |
1781 SkScalar dpi) const { | |
1782 if (!this->isConfigRenderable(config, true)) { | |
1783 return 0; | |
1784 } | |
1785 int chosenSampleCount = 0; | |
1786 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) { | |
1787 if (dpi >= 250.0f) { | |
1788 chosenSampleCount = 4; | |
1789 } else { | |
1790 chosenSampleCount = 16; | |
1791 } | |
1792 } | |
1793 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? | |
1794 chosenSampleCount : 0; | |
1795 } | |
1796 | |
1797 GrDrawTarget* GrContext::getTextTarget() { | |
1798 return this->prepareToDraw(); | |
1799 } | |
1800 | |
1801 namespace { | |
1802 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { | |
1803 GrConfigConversionEffect::PMConversion pmToUPM; | |
1804 GrConfigConversionEffect::PMConversion upmToPM; | |
1805 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); | |
1806 *pmToUPMValue = pmToUPM; | |
1807 *upmToPMValue = upmToPM; | |
1808 } | |
1809 } | |
1810 | |
1811 const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture, | |
1812 bool swapRAndB, | |
1813 const SkMatrix& matrix
) { | |
1814 if (!fDidTestPMConversions) { | |
1815 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); | |
1816 fDidTestPMConversions = true; | |
1817 } | |
1818 GrConfigConversionEffect::PMConversion pmToUPM = | |
1819 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); | |
1820 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { | |
1821 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, mat
rix); | |
1822 } else { | |
1823 return NULL; | |
1824 } | |
1825 } | |
1826 | |
1827 const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture, | |
1828 bool swapRAndB, | |
1829 const SkMatrix& matrix
) { | |
1830 if (!fDidTestPMConversions) { | |
1831 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); | |
1832 fDidTestPMConversions = true; | |
1833 } | |
1834 GrConfigConversionEffect::PMConversion upmToPM = | |
1835 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); | |
1836 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { | |
1837 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, mat
rix); | |
1838 } else { | |
1839 return NULL; | |
1840 } | |
1841 } | |
1842 | |
1843 ////////////////////////////////////////////////////////////////////////////// | |
1844 | |
1845 void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes
) const { | |
1846 if (maxTextures) { | |
1847 *maxTextures = fResourceCache->getMaxResourceCount(); | |
1848 } | |
1849 if (maxTextureBytes) { | |
1850 *maxTextureBytes = fResourceCache->getMaxResourceBytes(); | |
1851 } | |
1852 } | |
1853 | |
1854 void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes)
{ | |
1855 fResourceCache->setLimits(maxTextures, maxTextureBytes); | |
1856 } | |
1857 | |
1858 ////////////////////////////////////////////////////////////////////////////// | |
1859 | |
1860 void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) { | |
1861 fGpu->addGpuTraceMarker(marker); | |
1862 if (fDrawBuffer) { | |
1863 fDrawBuffer->addGpuTraceMarker(marker); | |
1864 } | |
1865 } | |
1866 | |
1867 void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { | |
1868 fGpu->removeGpuTraceMarker(marker); | |
1869 if (fDrawBuffer) { | |
1870 fDrawBuffer->removeGpuTraceMarker(marker); | |
1871 } | |
1872 } | |
1873 | |
1874 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 1145 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
1875 | 1146 |
1876 #ifdef GR_TEST_UTILS | 1147 #ifdef GR_TEST_UTILS |
1877 | 1148 |
1878 BATCH_TEST_DEFINE(StrokeRectBatch) { | 1149 BATCH_TEST_DEFINE(StrokeRectBatch) { |
1879 StrokeRectBatch::Geometry geometry; | 1150 StrokeRectBatch::Geometry geometry; |
1880 geometry.fViewMatrix = GrTest::TestMatrix(random); | 1151 geometry.fViewMatrix = GrTest::TestMatrix(random); |
1881 geometry.fColor = GrRandomColor(random); | 1152 geometry.fColor = GrRandomColor(random); |
1882 geometry.fRect = GrTest::TestRect(random); | 1153 geometry.fRect = GrTest::TestRect(random); |
1883 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f; | 1154 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1990 geometry.fColor = GrRandomColor(random); | 1261 geometry.fColor = GrRandomColor(random); |
1991 return DrawVerticesBatch::Create(geometry, type, viewMatrix, | 1262 return DrawVerticesBatch::Create(geometry, type, viewMatrix, |
1992 positions.begin(), vertexCount, | 1263 positions.begin(), vertexCount, |
1993 indices.begin(), hasIndices ? vertexCount :
0, | 1264 indices.begin(), hasIndices ? vertexCount :
0, |
1994 colors.begin(), | 1265 colors.begin(), |
1995 texCoords.begin(), | 1266 texCoords.begin(), |
1996 bounds); | 1267 bounds); |
1997 } | 1268 } |
1998 | 1269 |
1999 #endif | 1270 #endif |
| 1271 |
OLD | NEW |