bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef GrContext_impl_DEFINED |
| 18 | #define GrContext_impl_DEFINED |
| 19 | |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 20 | struct GrContext::OffscreenRecord { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 21 | OffscreenRecord() { fEntry0 = NULL; fEntry1 = NULL; } |
| 22 | ~OffscreenRecord() { GrAssert(NULL == fEntry0 && NULL == fEntry1); } |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 24 | enum Downsample { |
| 25 | k4x4TwoPass_Downsample, |
| 26 | k4x4SinglePass_Downsample, |
| 27 | kFSAA_Downsample |
| 28 | } fDownsample; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 29 | GrTextureEntry* fEntry0; |
| 30 | GrTextureEntry* fEntry1; |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 31 | GrDrawTarget::SavedDrawState fSavedState; |
| 32 | }; |
| 33 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 34 | template <typename POS_SRC, typename TEX_SRC, |
| 35 | typename COL_SRC, typename IDX_SRC> |
| 36 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 37 | GrPrimitiveType primitiveType, |
| 38 | const POS_SRC& posSrc, |
| 39 | const TEX_SRC* texCoordSrc, |
| 40 | const COL_SRC* colorSrc, |
| 41 | const IDX_SRC* idxSrc) { |
| 42 | |
| 43 | GrVertexLayout layout = 0; |
| 44 | |
| 45 | GrDrawTarget::AutoReleaseGeometry geo; |
| 46 | |
| 47 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 48 | |
| 49 | if (NULL != paint.getTexture()) { |
| 50 | if (NULL != texCoordSrc) { |
| 51 | layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0,0); |
| 52 | } else { |
| 53 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (NULL != colorSrc) { |
| 58 | layout |= GrDrawTarget::kColor_VertexLayoutBit; |
| 59 | } |
| 60 | |
| 61 | int vertexCount = posSrc.count(); |
| 62 | int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0; |
| 63 | |
| 64 | if (!geo.set(target, layout, vertexCount, indexCount)) { |
| 65 | GrPrintf("Failed to get space for vertices!"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | int texOffsets[GrDrawTarget::kMaxTexCoords]; |
| 70 | int colorOffset; |
| 71 | int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout, |
| 72 | texOffsets, |
| 73 | &colorOffset); |
| 74 | void* curVertex = geo.vertices(); |
| 75 | |
| 76 | for (int i = 0; i < vertexCount; ++i) { |
| 77 | posSrc.writeValue(i, (GrPoint*)curVertex); |
| 78 | |
| 79 | if (texOffsets[0] > 0) { |
| 80 | texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0])); |
| 81 | } |
| 82 | if (colorOffset > 0) { |
| 83 | colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset)); |
| 84 | } |
| 85 | curVertex = (void*)((intptr_t)curVertex + vsize); |
| 86 | } |
| 87 | |
| 88 | uint16_t* indices = (uint16_t*) geo.indices(); |
| 89 | for (int i = 0; i < indexCount; ++i) { |
| 90 | idxSrc->writeValue(i, indices + i); |
| 91 | } |
| 92 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 93 | bool doAA = false; |
| 94 | OffscreenRecord record; |
| 95 | GrIRect bounds; |
| 96 | |
| 97 | if (-1 == texOffsets[0] && -1 == colorOffset && |
| 98 | this->doOffscreenAA(target, paint, GrIsPrimTypeLines(primitiveType))) { |
| 99 | GrRect b; |
| 100 | b.setBounds(geo.positions(), vertexCount); |
| 101 | target->getViewMatrix().mapRect(&b); |
| 102 | b.roundOut(&bounds); |
| 103 | if (this->setupOffscreenAAPass1(target, false, bounds, &record)) { |
| 104 | doAA = true; |
| 105 | } |
| 106 | } |
| 107 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 108 | if (NULL == idxSrc) { |
| 109 | target->drawNonIndexed(primitiveType, 0, vertexCount); |
| 110 | } else { |
| 111 | target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount); |
| 112 | } |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 113 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 114 | if (doAA) { |
| 115 | geo.set(NULL, 0, 0, 0); // have to release geom before can draw again |
| 116 | this->offscreenAAPass2(target, paint, bounds, &record); |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 117 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | class GrNullTexCoordSource { |
| 121 | public: |
| 122 | void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); } |
| 123 | }; |
| 124 | |
| 125 | class GrNullColorSource { |
| 126 | public: |
| 127 | void writeValue(int i, GrColor* dstColor) const { GrAssert(false); } |
| 128 | }; |
| 129 | |
| 130 | class GrNullIndexSource { |
| 131 | public: |
| 132 | void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); } |
| 133 | int count() const { GrAssert(false); return 0; } |
| 134 | }; |
| 135 | |
| 136 | template <typename POS_SRC> |
| 137 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 138 | GrPrimitiveType primitiveType, |
| 139 | const POS_SRC& posSrc) { |
| 140 | this->drawCustomVertices<POS_SRC, |
| 141 | GrNullTexCoordSource, |
| 142 | GrNullColorSource, |
| 143 | GrNullIndexSource>(paint, primitiveType, posSrc, |
| 144 | NULL, NULL, NULL); |
| 145 | } |
| 146 | |
| 147 | template <typename POS_SRC, typename TEX_SRC> |
| 148 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 149 | GrPrimitiveType primitiveType, |
| 150 | const POS_SRC& posSrc, |
| 151 | const TEX_SRC* texCoordSrc) { |
| 152 | this->drawCustomVertices<POS_SRC, TEX_SRC, |
| 153 | GrNullColorSource, |
| 154 | GrNullIndexSource>(paint, primitiveType, posSrc, |
| 155 | texCoordSrc, NULL, NULL); |
| 156 | } |
| 157 | |
| 158 | template <typename POS_SRC, typename TEX_SRC, typename COL_SRC> |
| 159 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 160 | GrPrimitiveType primitiveType, |
| 161 | const POS_SRC& posSrc, |
| 162 | const TEX_SRC* texCoordSrc, |
| 163 | const COL_SRC* colorSrc) { |
| 164 | drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC, |
| 165 | GrNullIndexSource>(paint, primitiveType, posSrc, |
| 166 | texCoordSrc, colorSrc, NULL); |
| 167 | } |
| 168 | |
| 169 | #endif |