| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkMagnifierImageFilter.h" | 9 #include "SkMagnifierImageFilter.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkValidationUtils.h" | 13 #include "SkValidationUtils.h" |
| 14 | 14 |
| 15 //////////////////////////////////////////////////////////////////////////////// | 15 //////////////////////////////////////////////////////////////////////////////// |
| 16 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
| 17 #include "effects/GrSingleTextureEffect.h" | 17 #include "effects/GrSingleTextureEffect.h" |
| 18 #include "gl/GrGLEffect.h" | 18 #include "gl/GrGLProcessor.h" |
| 19 #include "gl/builders/GrGLProgramBuilder.h" | 19 #include "gl/builders/GrGLProgramBuilder.h" |
| 20 #include "gl/GrGLSL.h" | 20 #include "gl/GrGLSL.h" |
| 21 #include "gl/GrGLTexture.h" | 21 #include "gl/GrGLTexture.h" |
| 22 #include "GrTBackendEffectFactory.h" | 22 #include "GrTBackendProcessorFactory.h" |
| 23 | 23 |
| 24 class GrGLMagnifierEffect; | 24 class GrGLMagnifierEffect; |
| 25 | 25 |
| 26 class GrMagnifierEffect : public GrSingleTextureEffect { | 26 class GrMagnifierEffect : public GrSingleTextureEffect { |
| 27 | 27 |
| 28 public: | 28 public: |
| 29 static GrEffect* Create(GrTexture* texture, | 29 static GrFragmentProcessor* Create(GrTexture* texture, |
| 30 float xOffset, | 30 float xOffset, |
| 31 float yOffset, | 31 float yOffset, |
| 32 float xInvZoom, | 32 float xInvZoom, |
| 33 float yInvZoom, | 33 float yInvZoom, |
| 34 float xInvInset, | 34 float xInvInset, |
| 35 float yInvInset) { | 35 float yInvInset) { |
| 36 return SkNEW_ARGS(GrMagnifierEffect, (texture, | 36 return SkNEW_ARGS(GrMagnifierEffect, (texture, |
| 37 xOffset, | 37 xOffset, |
| 38 yOffset, | 38 yOffset, |
| 39 xInvZoom, | 39 xInvZoom, |
| 40 yInvZoom, | 40 yInvZoom, |
| 41 xInvInset, | 41 xInvInset, |
| 42 yInvInset)); | 42 yInvInset)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 virtual ~GrMagnifierEffect() {}; | 45 virtual ~GrMagnifierEffect() {}; |
| 46 | 46 |
| 47 static const char* Name() { return "Magnifier"; } | 47 static const char* Name() { return "Magnifier"; } |
| 48 | 48 |
| 49 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 49 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE; |
| 50 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 50 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 51 | 51 |
| 52 float x_offset() const { return fXOffset; } | 52 float x_offset() const { return fXOffset; } |
| 53 float y_offset() const { return fYOffset; } | 53 float y_offset() const { return fYOffset; } |
| 54 float x_inv_zoom() const { return fXInvZoom; } | 54 float x_inv_zoom() const { return fXInvZoom; } |
| 55 float y_inv_zoom() const { return fYInvZoom; } | 55 float y_inv_zoom() const { return fYInvZoom; } |
| 56 float x_inv_inset() const { return fXInvInset; } | 56 float x_inv_inset() const { return fXInvInset; } |
| 57 float y_inv_inset() const { return fYInvInset; } | 57 float y_inv_inset() const { return fYInvInset; } |
| 58 | 58 |
| 59 typedef GrGLMagnifierEffect GLEffect; | 59 typedef GrGLMagnifierEffect GLProcessor; |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 GrMagnifierEffect(GrTexture* texture, | 62 GrMagnifierEffect(GrTexture* texture, |
| 63 float xOffset, | 63 float xOffset, |
| 64 float yOffset, | 64 float yOffset, |
| 65 float xInvZoom, | 65 float xInvZoom, |
| 66 float yInvZoom, | 66 float yInvZoom, |
| 67 float xInvInset, | 67 float xInvInset, |
| 68 float yInvInset) | 68 float yInvInset) |
| 69 : GrSingleTextureEffect(texture, GrCoordTransform::MakeDivByTextureWHMat
rix(texture)) | 69 : GrSingleTextureEffect(texture, GrCoordTransform::MakeDivByTextureWHMat
rix(texture)) |
| 70 , fXOffset(xOffset) | 70 , fXOffset(xOffset) |
| 71 , fYOffset(yOffset) | 71 , fYOffset(yOffset) |
| 72 , fXInvZoom(xInvZoom) | 72 , fXInvZoom(xInvZoom) |
| 73 , fYInvZoom(yInvZoom) | 73 , fYInvZoom(yInvZoom) |
| 74 , fXInvInset(xInvInset) | 74 , fXInvInset(xInvInset) |
| 75 , fYInvInset(yInvInset) {} | 75 , fYInvInset(yInvInset) {} |
| 76 | 76 |
| 77 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 77 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; |
| 78 | 78 |
| 79 GR_DECLARE_EFFECT_TEST; | 79 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 80 | 80 |
| 81 float fXOffset; | 81 float fXOffset; |
| 82 float fYOffset; | 82 float fYOffset; |
| 83 float fXInvZoom; | 83 float fXInvZoom; |
| 84 float fYInvZoom; | 84 float fYInvZoom; |
| 85 float fXInvInset; | 85 float fXInvInset; |
| 86 float fYInvInset; | 86 float fYInvInset; |
| 87 | 87 |
| 88 typedef GrSingleTextureEffect INHERITED; | 88 typedef GrSingleTextureEffect INHERITED; |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 // For brevity | 91 // For brevity |
| 92 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 92 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 93 | 93 |
| 94 class GrGLMagnifierEffect : public GrGLEffect { | 94 class GrGLMagnifierEffect : public GrGLFragmentProcessor { |
| 95 public: | 95 public: |
| 96 GrGLMagnifierEffect(const GrBackendEffectFactory&, const GrEffect&); | 96 GrGLMagnifierEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
| 97 | 97 |
| 98 virtual void emitCode(GrGLProgramBuilder*, | 98 virtual void emitCode(GrGLProgramBuilder*, |
| 99 const GrEffect&, | 99 const GrFragmentProcessor&, |
| 100 const GrEffectKey&, | 100 const GrProcessorKey&, |
| 101 const char* outputColor, | 101 const char* outputColor, |
| 102 const char* inputColor, | 102 const char* inputColor, |
| 103 const TransformedCoordsArray&, | 103 const TransformedCoordsArray&, |
| 104 const TextureSamplerArray&) SK_OVERRIDE; | 104 const TextureSamplerArray&) SK_OVERRIDE; |
| 105 | 105 |
| 106 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 106 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| 107 | 107 |
| 108 private: | 108 private: |
| 109 UniformHandle fOffsetVar; | 109 UniformHandle fOffsetVar; |
| 110 UniformHandle fInvZoomVar; | 110 UniformHandle fInvZoomVar; |
| 111 UniformHandle fInvInsetVar; | 111 UniformHandle fInvInsetVar; |
| 112 | 112 |
| 113 typedef GrGLEffect INHERITED; | 113 typedef GrGLFragmentProcessor INHERITED; |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory,
const GrEffect&) | 116 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendProcessorFactory& factor
y, |
| 117 const GrProcessor&) |
| 117 : INHERITED(factory) { | 118 : INHERITED(factory) { |
| 118 } | 119 } |
| 119 | 120 |
| 120 void GrGLMagnifierEffect::emitCode(GrGLProgramBuilder* builder, | 121 void GrGLMagnifierEffect::emitCode(GrGLProgramBuilder* builder, |
| 121 const GrEffect&, | 122 const GrFragmentProcessor&, |
| 122 const GrEffectKey& key, | 123 const GrProcessorKey& key, |
| 123 const char* outputColor, | 124 const char* outputColor, |
| 124 const char* inputColor, | 125 const char* inputColor, |
| 125 const TransformedCoordsArray& coords, | 126 const TransformedCoordsArray& coords, |
| 126 const TextureSamplerArray& samplers) { | 127 const TextureSamplerArray& samplers) { |
| 127 fOffsetVar = builder->addUniform( | 128 fOffsetVar = builder->addUniform( |
| 128 GrGLProgramBuilder::kFragment_Visibility | | 129 GrGLProgramBuilder::kFragment_Visibility | |
| 129 GrGLProgramBuilder::kVertex_Visibility, | 130 GrGLProgramBuilder::kVertex_Visibility, |
| 130 kVec2f_GrSLType, "Offset"); | 131 kVec2f_GrSLType, "Offset"); |
| 131 fInvZoomVar = builder->addUniform( | 132 fInvZoomVar = builder->addUniform( |
| 132 GrGLProgramBuilder::kFragment_Visibility | | 133 GrGLProgramBuilder::kFragment_Visibility | |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 fsBuilder->appendTextureLookup(samplers[0], "mix_coord"); | 166 fsBuilder->appendTextureLookup(samplers[0], "mix_coord"); |
| 166 fsBuilder->codeAppend(";\n"); | 167 fsBuilder->codeAppend(";\n"); |
| 167 | 168 |
| 168 fsBuilder->codeAppendf("\t\t%s = output_color;", outputColor); | 169 fsBuilder->codeAppendf("\t\t%s = output_color;", outputColor); |
| 169 SkString modulate; | 170 SkString modulate; |
| 170 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 171 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 171 fsBuilder->codeAppend(modulate.c_str()); | 172 fsBuilder->codeAppend(modulate.c_str()); |
| 172 } | 173 } |
| 173 | 174 |
| 174 void GrGLMagnifierEffect::setData(const GrGLProgramDataManager& pdman, | 175 void GrGLMagnifierEffect::setData(const GrGLProgramDataManager& pdman, |
| 175 const GrEffect& effect) { | 176 const GrProcessor& effect) { |
| 176 const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>(); | 177 const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>(); |
| 177 pdman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); | 178 pdman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); |
| 178 pdman.set2f(fInvZoomVar, zoom.x_inv_zoom(), zoom.y_inv_zoom()); | 179 pdman.set2f(fInvZoomVar, zoom.x_inv_zoom(), zoom.y_inv_zoom()); |
| 179 pdman.set2f(fInvInsetVar, zoom.x_inv_inset(), zoom.y_inv_inset()); | 180 pdman.set2f(fInvInsetVar, zoom.x_inv_inset(), zoom.y_inv_inset()); |
| 180 } | 181 } |
| 181 | 182 |
| 182 ///////////////////////////////////////////////////////////////////// | 183 ///////////////////////////////////////////////////////////////////// |
| 183 | 184 |
| 184 GR_DEFINE_EFFECT_TEST(GrMagnifierEffect); | 185 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
| 185 | 186 |
| 186 GrEffect* GrMagnifierEffect::TestCreate(SkRandom* random, | 187 GrFragmentProcessor* GrMagnifierEffect::TestCreate(SkRandom* random, |
| 187 GrContext* context, | 188 GrContext* context, |
| 188 const GrDrawTargetCaps&, | 189 const GrDrawTargetCaps&, |
| 189 GrTexture** textures) { | 190 GrTexture** textures) { |
| 190 GrTexture* texture = textures[0]; | 191 GrTexture* texture = textures[0]; |
| 191 const int kMaxWidth = 200; | 192 const int kMaxWidth = 200; |
| 192 const int kMaxHeight = 200; | 193 const int kMaxHeight = 200; |
| 193 const int kMaxInset = 20; | 194 const int kMaxInset = 20; |
| 194 uint32_t width = random->nextULessThan(kMaxWidth); | 195 uint32_t width = random->nextULessThan(kMaxWidth); |
| 195 uint32_t height = random->nextULessThan(kMaxHeight); | 196 uint32_t height = random->nextULessThan(kMaxHeight); |
| 196 uint32_t x = random->nextULessThan(kMaxWidth - width); | 197 uint32_t x = random->nextULessThan(kMaxWidth - width); |
| 197 uint32_t y = random->nextULessThan(kMaxHeight - height); | 198 uint32_t y = random->nextULessThan(kMaxHeight - height); |
| 198 uint32_t inset = random->nextULessThan(kMaxInset); | 199 uint32_t inset = random->nextULessThan(kMaxInset); |
| 199 | 200 |
| 200 GrEffect* effect = GrMagnifierEffect::Create( | 201 GrFragmentProcessor* effect = GrMagnifierEffect::Create( |
| 201 texture, | 202 texture, |
| 202 (float) width / texture->width(), | 203 (float) width / texture->width(), |
| 203 (float) height / texture->height(), | 204 (float) height / texture->height(), |
| 204 texture->width() / (float) x, | 205 texture->width() / (float) x, |
| 205 texture->height() / (float) y, | 206 texture->height() / (float) y, |
| 206 (float) inset / texture->width(), | 207 (float) inset / texture->width(), |
| 207 (float) inset / texture->height()); | 208 (float) inset / texture->height()); |
| 208 SkASSERT(effect); | 209 SkASSERT(effect); |
| 209 return effect; | 210 return effect; |
| 210 } | 211 } |
| 211 | 212 |
| 212 /////////////////////////////////////////////////////////////////////////////// | 213 /////////////////////////////////////////////////////////////////////////////// |
| 213 | 214 |
| 214 const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const { | 215 const GrBackendFragmentProcessorFactory& GrMagnifierEffect::getFactory() const { |
| 215 return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance(); | 216 return GrTBackendFragmentProcessorFactory<GrMagnifierEffect>::getInstance(); |
| 216 } | 217 } |
| 217 | 218 |
| 218 bool GrMagnifierEffect::onIsEqual(const GrEffect& sBase) const { | 219 bool GrMagnifierEffect::onIsEqual(const GrProcessor& sBase) const { |
| 219 const GrMagnifierEffect& s = sBase.cast<GrMagnifierEffect>(); | 220 const GrMagnifierEffect& s = sBase.cast<GrMagnifierEffect>(); |
| 220 return (this->texture(0) == s.texture(0) && | 221 return (this->texture(0) == s.texture(0) && |
| 221 this->fXOffset == s.fXOffset && | 222 this->fXOffset == s.fXOffset && |
| 222 this->fYOffset == s.fYOffset && | 223 this->fYOffset == s.fYOffset && |
| 223 this->fXInvZoom == s.fXInvZoom && | 224 this->fXInvZoom == s.fXInvZoom && |
| 224 this->fYInvZoom == s.fYInvZoom && | 225 this->fYInvZoom == s.fYInvZoom && |
| 225 this->fXInvInset == s.fXInvInset && | 226 this->fXInvInset == s.fXInvInset && |
| 226 this->fYInvInset == s.fYInvInset); | 227 this->fYInvInset == s.fYInvInset); |
| 227 } | 228 } |
| 228 | 229 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 } | 265 } |
| 265 #endif | 266 #endif |
| 266 | 267 |
| 267 SkMagnifierImageFilter::SkMagnifierImageFilter(const SkRect& srcRect, SkScalar i
nset, | 268 SkMagnifierImageFilter::SkMagnifierImageFilter(const SkRect& srcRect, SkScalar i
nset, |
| 268 SkImageFilter* input) | 269 SkImageFilter* input) |
| 269 : INHERITED(1, &input), fSrcRect(srcRect), fInset(inset) { | 270 : INHERITED(1, &input), fSrcRect(srcRect), fInset(inset) { |
| 270 SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0); | 271 SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0); |
| 271 } | 272 } |
| 272 | 273 |
| 273 #if SK_SUPPORT_GPU | 274 #if SK_SUPPORT_GPU |
| 274 bool SkMagnifierImageFilter::asNewEffect(GrEffect** effect, GrTexture* texture,
const SkMatrix&, | 275 bool SkMagnifierImageFilter::asFragmentProcessor(GrFragmentProcessor** fp, GrTex
ture* texture, |
| 275 const SkIRect&) const { | 276 const SkMatrix&, const SkIRect&
) const { |
| 276 if (effect) { | 277 if (fp) { |
| 277 SkScalar yOffset = (texture->origin() == kTopLeft_GrSurfaceOrigin) ? fSr
cRect.y() : | 278 SkScalar yOffset = (texture->origin() == kTopLeft_GrSurfaceOrigin) ? fSr
cRect.y() : |
| 278 (texture->height() - (fSrcRect.y() + fSrcRect.height(
))); | 279 (texture->height() - (fSrcRect.y() + fSrcRect.height(
))); |
| 279 SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; | 280 SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
| 280 *effect = GrMagnifierEffect::Create(texture, | 281 *fp = GrMagnifierEffect::Create(texture, |
| 281 fSrcRect.x() / texture->width(), | 282 fSrcRect.x() / texture->width(), |
| 282 yOffset / texture->height(), | 283 yOffset / texture->height(), |
| 283 fSrcRect.width() / texture->width(), | 284 fSrcRect.width() / texture->width(), |
| 284 fSrcRect.height() / texture->height(
), | 285 fSrcRect.height() / texture->height(
), |
| 285 texture->width() * invInset, | 286 texture->width() * invInset, |
| 286 texture->height() * invInset); | 287 texture->height() * invInset); |
| 287 } | 288 } |
| 288 return true; | 289 return true; |
| 289 } | 290 } |
| 290 #endif | 291 #endif |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 365 |
| 365 int x_val = SkPin32(SkScalarFloorToInt(x_interp), 0, width - 1); | 366 int x_val = SkPin32(SkScalarFloorToInt(x_interp), 0, width - 1); |
| 366 int y_val = SkPin32(SkScalarFloorToInt(y_interp), 0, height - 1); | 367 int y_val = SkPin32(SkScalarFloorToInt(y_interp), 0, height - 1); |
| 367 | 368 |
| 368 *dptr = sptr[y_val * width + x_val]; | 369 *dptr = sptr[y_val * width + x_val]; |
| 369 dptr++; | 370 dptr++; |
| 370 } | 371 } |
| 371 } | 372 } |
| 372 return true; | 373 return true; |
| 373 } | 374 } |
| OLD | NEW |