Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrStencilAndCoverPathRenderer.cpp
1
2 /*
3  * Copyright 2012 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.
7  */
8
9
10 #include "GrStencilAndCoverPathRenderer.h"
11 #include "GrContext.h"
12 #include "GrDrawTargetCaps.h"
13 #include "GrGpu.h"
14 #include "GrPath.h"
15 #include "SkStrokeRec.h"
16
17 /*
18  * For now paths only natively support winding and even odd fill types
19  */
20 static GrPathRendering::FillType convert_skpath_filltype(SkPath::FillType fill) {
21     switch (fill) {
22         default:
23             SkFAIL("Incomplete Switch\n");
24         case SkPath::kWinding_FillType:
25         case SkPath::kInverseWinding_FillType:
26             return GrPathRendering::kWinding_FillType;
27         case SkPath::kEvenOdd_FillType:
28         case SkPath::kInverseEvenOdd_FillType:
29             return GrPathRendering::kEvenOdd_FillType;
30     }
31 }
32
33 GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
34     SkASSERT(context);
35     SkASSERT(context->getGpu());
36     if (context->getGpu()->caps()->pathRenderingSupport()) {
37         return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
38     } else {
39         return NULL;
40     }
41 }
42
43 GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
44     SkASSERT(gpu->caps()->pathRenderingSupport());
45     fGpu = gpu;
46     gpu->ref();
47 }
48
49 GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
50     fGpu->unref();
51 }
52
53 bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path,
54                                                 const SkStrokeRec& stroke,
55                                                 const GrDrawTarget* target,
56                                                 bool antiAlias) const {
57     return !stroke.isHairlineStyle() &&
58            !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
59            target->getDrawState().getRenderTarget()->getStencilBuffer() &&
60            target->getDrawState().getStencil().isDisabled();
61 }
62
63 GrPathRenderer::StencilSupport
64 GrStencilAndCoverPathRenderer::onGetStencilSupport(const SkPath&,
65                                                    const SkStrokeRec& ,
66                                                    const GrDrawTarget*) const {
67     return GrPathRenderer::kStencilOnly_StencilSupport;
68 }
69
70 static GrPath* get_gr_path(GrGpu* gpu, const SkPath& skPath, const SkStrokeRec& stroke) {
71     GrContext* ctx = gpu->getContext();
72     GrResourceKey resourceKey = GrPath::ComputeKey(skPath, stroke);
73     SkAutoTUnref<GrPath> path(static_cast<GrPath*>(ctx->findAndRefCachedResource(resourceKey)));
74     if (NULL == path || !path->isEqualTo(skPath, stroke)) {
75         path.reset(gpu->pathRendering()->createPath(skPath, stroke));
76         ctx->addResourceToCache(resourceKey, path);
77     }
78     return path.detach();
79 }
80
81 void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path,
82                                                   const SkStrokeRec& stroke,
83                                                   GrDrawTarget* target) {
84     SkASSERT(!path.isInverseFillType());
85     SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
86     target->stencilPath(p, convert_skpath_filltype(path.getFillType()));
87 }
88
89 bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
90                                                const SkStrokeRec& stroke,
91                                                GrDrawTarget* target,
92                                                bool antiAlias) {
93     SkASSERT(!antiAlias);
94     SkASSERT(!stroke.isHairlineStyle());
95
96     GrDrawState* drawState = target->drawState();
97     SkASSERT(drawState->getStencil().isDisabled());
98
99     SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
100
101     if (path.isInverseFillType()) {
102         GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
103             kZero_StencilOp,
104             kZero_StencilOp,
105             // We know our rect will hit pixels outside the clip and the user bits will be 0
106             // outside the clip. So we can't just fill where the user bits are 0. We also need to
107             // check that the clip bit is set.
108             kEqualIfInClip_StencilFunc,
109             0xffff,
110             0x0000,
111             0xffff);
112
113         drawState->setStencil(kInvertedStencilPass);
114
115         // fake inverse with a stencil and cover
116         target->stencilPath(p, convert_skpath_filltype(path.getFillType()));
117
118         GrDrawState::AutoViewMatrixRestore avmr;
119         SkRect bounds = SkRect::MakeLTRB(0, 0,
120                                          SkIntToScalar(drawState->getRenderTarget()->width()),
121                                          SkIntToScalar(drawState->getRenderTarget()->height()));
122         SkMatrix vmi;
123         // mapRect through persp matrix may not be correct
124         if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
125             vmi.mapRect(&bounds);
126             // theoretically could set bloat = 0, instead leave it because of matrix inversion
127             // precision.
128             SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
129             bounds.outset(bloat, bloat);
130         } else {
131             avmr.setIdentity(drawState);
132         }
133         target->drawSimpleRect(bounds);
134     } else {
135         GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
136             kZero_StencilOp,
137             kZero_StencilOp,
138             kNotEqual_StencilFunc,
139             0xffff,
140             0x0000,
141             0xffff);
142
143         drawState->setStencil(kStencilPass);
144         target->drawPath(p, convert_skpath_filltype(path.getFillType()));
145     }
146
147     target->drawState()->stencil()->setDisabled();
148     return true;
149 }