OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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 "SkBitmapProcShader.h" | |
9 #include "SkBitmapProvider.h" | |
10 #include "SkImage_Base.h" | |
11 #include "SkImageShader.h" | |
12 #include "SkReadBuffer.h" | |
13 #include "SkWriteBuffer.h" | |
14 | |
15 SkImageShader::SkImageShader(const SkImage* img, TileMode tmx, TileMode tmy, con
st SkMatrix* matrix) | |
16 : INHERITED(matrix) | |
17 , fImage(SkRef(img)) | |
18 , fTileModeX(tmx) | |
19 , fTileModeY(tmy) | |
20 {} | |
21 | |
22 SkFlattenable* SkImageShader::CreateProc(SkReadBuffer& buffer) { | |
23 const TileMode tx = (TileMode)buffer.readUInt(); | |
24 const TileMode ty = (TileMode)buffer.readUInt(); | |
25 SkMatrix matrix; | |
26 buffer.readMatrix(&matrix); | |
27 SkAutoTUnref<SkImage> img(buffer.readImage()); | |
28 if (!img) { | |
29 return nullptr; | |
30 } | |
31 return new SkImageShader(img, tx, ty, &matrix); | |
32 } | |
33 | |
34 void SkImageShader::flatten(SkWriteBuffer& buffer) const { | |
35 buffer.writeUInt(fTileModeX); | |
36 buffer.writeUInt(fTileModeY); | |
37 buffer.writeMatrix(this->getLocalMatrix()); | |
38 buffer.writeImage(fImage); | |
39 } | |
40 | |
41 bool SkImageShader::isOpaque() const { | |
42 return fImage->isOpaque(); | |
43 } | |
44 | |
45 size_t SkImageShader::contextSize() const { | |
46 return SkBitmapProcShader::ContextSize(); | |
47 } | |
48 | |
49 SkShader::Context* SkImageShader::onCreateContext(const ContextRec& rec, void* s
torage) const { | |
50 return SkBitmapProcShader::MakeContext(*this, fTileModeX, fTileModeY, | |
51 SkBitmapProvider(fImage), rec, storag
e); | |
52 } | |
53 | |
54 SkShader* SkImageShader::Create(const SkImage* image, TileMode tx, TileMode ty, | |
55 const SkMatrix* localMatrix) { | |
56 if (!image) { | |
57 return nullptr; | |
58 } | |
59 return new SkImageShader(image, tx, ty, localMatrix); | |
60 } | |
61 | |
62 #ifndef SK_IGNORE_TO_STRING | |
63 void SkImageShader::toString(SkString* str) const { | |
64 const char* gTileModeName[SkShader::kTileModeCount] = { | |
65 "clamp", "repeat", "mirror" | |
66 }; | |
67 | |
68 str->appendf("ImageShader: ((%s %s) ", gTileModeName[fTileModeX], gTileModeN
ame[fTileModeY]); | |
69 fImage->toString(str); | |
70 this->INHERITED::toString(str); | |
71 str->append(")"); | |
72 } | |
73 #endif | |
74 | |
75 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
76 | |
77 #if SK_SUPPORT_GPU | |
78 | |
79 #include "GrTextureAccess.h" | |
80 #include "SkGr.h" | |
81 #include "effects/GrSimpleTextureEffect.h" | |
82 #include "effects/GrBicubicEffect.h" | |
83 #include "effects/GrExtractAlphaFragmentProcessor.h" | |
84 #include "effects/GrSimpleTextureEffect.h" | |
85 | |
86 const GrFragmentProcessor* SkImageShader::asFragmentProcessor(GrContext* context
, | |
87 const SkMatrix& vi
ewM, | |
88 const SkMatrix* lo
calMatrix, | |
89 SkFilterQuality fi
lterQuality, | |
90 GrProcessorDataMan
ager* mgr) const { | |
91 SkMatrix matrix; | |
92 matrix.setIDiv(fImage->width(), fImage->height()); | |
93 | |
94 SkMatrix lmInverse; | |
95 if (!this->getLocalMatrix().invert(&lmInverse)) { | |
96 return nullptr; | |
97 } | |
98 if (localMatrix) { | |
99 SkMatrix inv; | |
100 if (!localMatrix->invert(&inv)) { | |
101 return nullptr; | |
102 } | |
103 lmInverse.postConcat(inv); | |
104 } | |
105 matrix.preConcat(lmInverse); | |
106 | |
107 SkShader::TileMode tm[] = { fTileModeX, fTileModeY }; | |
108 | |
109 // Must set wrap and filter on the sampler before requesting a texture. In t
wo places below | |
110 // we check the matrix scale factors to determine how to interpret the filte
r quality setting. | |
111 // This completely ignores the complexity of the drawVertices case where exp
licit local coords | |
112 // are provided by the caller. | |
113 bool doBicubic; | |
114 GrTextureParams::FilterMode textureFilterMode = | |
115 GrSkFilterQualityToGrFilterMode(filterQuality, viewM, this->getLocalMatrix()
, &doBicubic); | |
116 GrTextureParams params(tm, textureFilterMode); | |
117 | |
118 SkImageUsageType usageType; | |
119 if (kClamp_TileMode == fTileModeX && kClamp_TileMode == fTileModeY) { | |
120 usageType = kUntiled_SkImageUsageType; | |
121 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) { | |
122 usageType = kTiled_Unfiltered_SkImageUsageType; | |
123 } else { | |
124 usageType = kTiled_Filtered_SkImageUsageType; | |
125 } | |
126 | |
127 SkAutoTUnref<GrTexture> texture(as_IB(fImage)->asTextureRef(context, usageTy
pe)); | |
128 if (!texture) { | |
129 return nullptr; | |
130 } | |
131 | |
132 SkAutoTUnref<GrFragmentProcessor> inner; | |
133 if (doBicubic) { | |
134 inner.reset(GrBicubicEffect::Create(mgr, texture, matrix, tm)); | |
135 } else { | |
136 inner.reset(GrSimpleTextureEffect::Create(mgr, texture, matrix, params))
; | |
137 } | |
138 | |
139 if (GrPixelConfigIsAlphaOnly(texture->config())) { | |
140 return SkRef(inner.get()); | |
141 } | |
142 return GrExtractAlphaFragmentProcessor::Create(inner); | |
143 } | |
144 | |
145 #endif | |
OLD | NEW |