1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "GrPrimitiveProcessor.h"
9
10 #include "GrCoordTransform.h"
11
12 /**
13 * We specialize the vertex code for each of these matrix types.
14 */
15 enum MatrixType {
16 kNoPersp_MatrixType = 0,
17 kGeneral_MatrixType = 1,
18 };
19
GrPrimitiveProcessor(ClassID classID)20 GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
21
textureSampler(int i) const22 const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
23 SkASSERT(i >= 0 && i < this->numTextureSamplers());
24 return this->onTextureSampler(i);
25 }
26
27 uint32_t
getTransformKey(const SkTArray<const GrCoordTransform *,true> & coords,int numCoords) const28 GrPrimitiveProcessor::getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
29 int numCoords) const {
30 uint32_t totalKey = 0;
31 for (int t = 0; t < numCoords; ++t) {
32 uint32_t key = 0;
33 const GrCoordTransform* coordTransform = coords[t];
34 if (coordTransform->getMatrix().hasPerspective()) {
35 key |= kGeneral_MatrixType;
36 } else {
37 key |= kNoPersp_MatrixType;
38 }
39 key <<= t;
40 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
41 totalKey |= key;
42 }
43 return totalKey;
44 }
45
46 ///////////////////////////////////////////////////////////////////////////////////////////////////
47
clamp_filter(GrTextureType type,GrSamplerState::Filter requestedFilter)48 static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
49 GrSamplerState::Filter requestedFilter) {
50 if (GrTextureTypeHasRestrictedSampling(type)) {
51 return SkTMin(requestedFilter, GrSamplerState::Filter::kBilerp);
52 }
53 return requestedFilter;
54 }
55
TextureSampler(GrTextureType textureType,GrPixelConfig config,const GrSamplerState & samplerState,uint32_t extraSamplerKey)56 GrPrimitiveProcessor::TextureSampler::TextureSampler(GrTextureType textureType,
57 GrPixelConfig config,
58 const GrSamplerState& samplerState,
59 uint32_t extraSamplerKey) {
60 this->reset(textureType, config, samplerState, extraSamplerKey);
61 }
62
TextureSampler(GrTextureType textureType,GrPixelConfig config,GrSamplerState::Filter filterMode,GrSamplerState::WrapMode wrapXAndY)63 GrPrimitiveProcessor::TextureSampler::TextureSampler(GrTextureType textureType,
64 GrPixelConfig config,
65 GrSamplerState::Filter filterMode,
66 GrSamplerState::WrapMode wrapXAndY) {
67 this->reset(textureType, config, filterMode, wrapXAndY);
68 }
69
reset(GrTextureType textureType,GrPixelConfig config,const GrSamplerState & samplerState,uint32_t extraSamplerKey)70 void GrPrimitiveProcessor::TextureSampler::reset(GrTextureType textureType,
71 GrPixelConfig config,
72 const GrSamplerState& samplerState,
73 uint32_t extraSamplerKey) {
74 SkASSERT(kUnknown_GrPixelConfig != config);
75 fSamplerState = samplerState;
76 fSamplerState.setFilterMode(clamp_filter(textureType, samplerState.filter()));
77 fTextureType = textureType;
78 fConfig = config;
79 fExtraSamplerKey = extraSamplerKey;
80 SkASSERT(!fExtraSamplerKey || textureType == GrTextureType::kExternal);
81 }
82
reset(GrTextureType textureType,GrPixelConfig config,GrSamplerState::Filter filterMode,GrSamplerState::WrapMode wrapXAndY)83 void GrPrimitiveProcessor::TextureSampler::reset(GrTextureType textureType,
84 GrPixelConfig config,
85 GrSamplerState::Filter filterMode,
86 GrSamplerState::WrapMode wrapXAndY) {
87 SkASSERT(kUnknown_GrPixelConfig != config);
88 filterMode = clamp_filter(textureType, filterMode);
89 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
90 fTextureType = textureType;
91 fConfig = config;
92 }
93