diff --git a/gm/texdata.cpp b/gm/texdata.cpp index 372ff5dc11..70afd22ec5 100644 --- a/gm/texdata.cpp +++ b/gm/texdata.cpp @@ -11,13 +11,56 @@ #if SK_SUPPORT_GPU #include "GrContext.h" +#include "GrContextPriv.h" #include "GrRenderTargetContext.h" +#include "GrTextureContext.h" #include "GrFixedClip.h" #include "SkColorPriv.h" #include "effects/GrPorterDuffXferProcessor.h" #include "effects/GrSimpleTextureEffect.h" constexpr int S = 200; +constexpr int kStride = 2 * S; + +// Fill in the pixels: +// gray | white +// ------------- +// black | gray +static void fill_in_pixels(SkPMColor* pixels) { + const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); + const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); + const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); + + int offset = 0; + + // fill upper-left + for (int y = 0; y < S; ++y) { + for (int x = 0; x < S; ++x) { + pixels[offset + y * kStride + x] = gray; + } + } + // fill upper-right + offset = S; + for (int y = 0; y < S; ++y) { + for (int x = 0; x < S; ++x) { + pixels[offset + y * kStride + x] = white; + } + } + // fill lower left + offset = S * kStride; + for (int y = 0; y < S; ++y) { + for (int x = 0; x < S; ++x) { + pixels[offset + y * kStride + x] = black; + } + } + // fill lower right + offset = S * kStride + S; + for (int y = 0; y < S; ++y) { + for (int x = 0; x < S; ++x) { + pixels[offset + y * kStride + x] = gray; + } + } +} DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { GrRenderTargetContext* renderTargetContext = @@ -32,56 +75,35 @@ DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { return; } + const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType); + SkAutoTArray gTextureData((2 * S) * (2 * S)); - constexpr int stride = 2 * S; - const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); - const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); - const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); for (int i = 0; i < 2; ++i) { - int offset = 0; - // fill upper-left - for (int y = 0; y < S; ++y) { - for (int x = 0; x < S; ++x) { - gTextureData[offset + y * stride + x] = gray; - } - } - // fill upper-right - offset = S; - for (int y = 0; y < S; ++y) { - for (int x = 0; x < S; ++x) { - gTextureData[offset + y * stride + x] = white; - } - } - // fill lower left - offset = S * stride; - for (int y = 0; y < S; ++y) { - for (int x = 0; x < S; ++x) { - gTextureData[offset + y * stride + x] = black; - } - } - // fill lower right - offset = S * stride + S; - for (int y = 0; y < S; ++y) { - for (int x = 0; x < S; ++x) { - gTextureData[offset + y * stride + x] = gray; - } - } + fill_in_pixels(gTextureData.get()); GrSurfaceDesc desc; desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; - desc.fConfig = kSkia8888_GrPixelConfig; + desc.fConfig = kBGRA_8888_GrPixelConfig; desc.fWidth = 2 * S; desc.fHeight = 2 * S; - GrTexture* texture = context->textureProvider()->createTexture( - desc, SkBudgeted::kNo, gTextureData.get(), 0); - if (!texture) { + sk_sp proxy = GrSurfaceProxy::MakeDeferred(*context->caps(), + context->textureProvider(), + desc, SkBudgeted::kNo, + gTextureData.get(), 0); + if (!proxy) { + return; + } + + sk_sp tContext = context->contextPriv().makeWrappedSurfaceContext( + std::move(proxy), nullptr); + + if (!tContext) { return; } - sk_sp au(texture); // setup new clip GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S)); @@ -91,29 +113,28 @@ DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { SkMatrix vm; if (i) { - vm.setRotate(90 * SK_Scalar1, - S * SK_Scalar1, - S * SK_Scalar1); + vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1); } else { vm.reset(); } - paint.addColorTextureProcessor(texture, nullptr, vm); + paint.addColorTextureProcessor(context, sk_ref_sp(tContext->asDeferredTexture()), + nullptr, vm); renderTargetContext->drawRect(clip, GrPaint(paint), GrAA::kNo, vm, SkRect::MakeWH(2 * S, 2 * S)); // now update the lower right of the texture in first pass // or upper right in second pass - offset = 0; for (int y = 0; y < S; ++y) { for (int x = 0; x < S; ++x) { - gTextureData[offset + y * stride + x] = - ((x + y) % 2) ? (i ? green : red) : blue; + gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue; } } - texture->writePixels(S, (i ? 0 : S), S, S, - texture->config(), gTextureData.get(), - 4 * stride); + + if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) { + continue; + } + renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm, SkRect::MakeWH(2 * S, 2 * S)); } diff --git a/include/gpu/GrPaint.h b/include/gpu/GrPaint.h index 3b136ac82e..2649b9e18b 100644 --- a/include/gpu/GrPaint.h +++ b/include/gpu/GrPaint.h @@ -20,6 +20,8 @@ #include "SkTLazy.h" #include "effects/GrPorterDuffXferProcessor.h" +class GrTextureProxy; + /** * The paint describes how color and coverage are computed at each pixel by GrContext draw * functions and the how color is blended with the destination pixel. @@ -116,6 +118,16 @@ public: const GrSamplerParams&); void addCoverageTextureProcessor(GrTexture*, const SkMatrix&, const GrSamplerParams&); + + void addColorTextureProcessor(GrContext*, sk_sp, sk_sp, + const SkMatrix&); + void addColorTextureProcessor(GrContext*, sk_sp, sk_sp, + const SkMatrix&, const GrSamplerParams&); + + void addCoverageTextureProcessor(GrContext*, sk_sp, const SkMatrix&); + void addCoverageTextureProcessor(GrContext*, sk_sp, + const SkMatrix&, const GrSamplerParams&); + int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); } int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); } int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() + diff --git a/include/gpu/GrProcessor.h b/include/gpu/GrProcessor.h index a8e31f2c86..388bd54092 100644 --- a/include/gpu/GrProcessor.h +++ b/include/gpu/GrProcessor.h @@ -211,6 +211,8 @@ public: SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode, GrShaderFlags visibility = kFragment_GrShaderFlag); + TextureSampler(GrTextureProvider*, sk_sp, const GrSamplerParams&); + // MDB TODO: ultimately we shouldn't need the texProvider parameter explicit TextureSampler(GrTextureProvider*, sk_sp, GrSamplerParams::FilterMode = GrSamplerParams::kNone_FilterMode, diff --git a/src/gpu/GrPaint.cpp b/src/gpu/GrPaint.cpp index 5bcd28a69e..c68b4b1089 100644 --- a/src/gpu/GrPaint.cpp +++ b/src/gpu/GrPaint.cpp @@ -44,6 +44,37 @@ void GrPaint::addCoverageTextureProcessor(GrTexture* texture, params)); } +void GrPaint::addColorTextureProcessor(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix) { + this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(ctx, std::move(proxy), + std::move(colorSpaceXform), + matrix)); +} + +void GrPaint::addColorTextureProcessor(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix, + const GrSamplerParams& params) { + this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(ctx, + std::move(proxy), + std::move(colorSpaceXform), + matrix, params)); +} + +void GrPaint::addCoverageTextureProcessor(GrContext* ctx, sk_sp proxy, + const SkMatrix& matrix) { + this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(ctx, std::move(proxy), + nullptr, matrix)); +} + +void GrPaint::addCoverageTextureProcessor(GrContext* ctx, sk_sp proxy, + const SkMatrix& matrix, + const GrSamplerParams& params) { + this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(ctx, std::move(proxy), + nullptr, matrix, params)); +} + bool GrPaint::internalIsConstantBlendedColor(GrColor paintColor, GrColor* color) const { GrProcOptInfo colorProcInfo(paintColor, kRGBA_GrColorComponentFlags); colorProcInfo.analyzeProcessors( diff --git a/src/gpu/GrProcessor.cpp b/src/gpu/GrProcessor.cpp index 3e1346482f..56d3e7f1a4 100644 --- a/src/gpu/GrProcessor.cpp +++ b/src/gpu/GrProcessor.cpp @@ -208,6 +208,14 @@ GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, this->reset(texture, filterMode, tileXAndY, visibility); } +GrProcessor::TextureSampler::TextureSampler(GrTextureProvider* texProvider, + sk_sp proxy, + const GrSamplerParams& params) { + // For now, end the deferral at this time. Once all the TextureSamplers are swapped over + // to taking a GrSurfaceProxy just use the IORefs on the proxy + this->reset(proxy->instantiate(texProvider), params); +} + GrProcessor::TextureSampler::TextureSampler(GrTextureProvider* texProvider, sk_sp proxy, GrSamplerParams::FilterMode filterMode, @@ -215,7 +223,7 @@ GrProcessor::TextureSampler::TextureSampler(GrTextureProvider* texProvider, GrShaderFlags visibility) { // For now, end the deferral at this time. Once all the TextureSamplers are swapped over // to taking a GrSurfaceProxy just use the IORefs on the proxy - this->reset( proxy->instantiate(texProvider), filterMode, tileXAndY, visibility); + this->reset(proxy->instantiate(texProvider), filterMode, tileXAndY, visibility); } void GrProcessor::TextureSampler::reset(GrTexture* texture, diff --git a/src/gpu/effects/GrSimpleTextureEffect.h b/src/gpu/effects/GrSimpleTextureEffect.h index 90f39acba0..bf013e9c83 100644 --- a/src/gpu/effects/GrSimpleTextureEffect.h +++ b/src/gpu/effects/GrSimpleTextureEffect.h @@ -9,6 +9,7 @@ #define GrSimpleTextureEffect_DEFINED #include "GrSingleTextureEffect.h" +#include "GrTextureProxy.h" class GrInvariantOutput; @@ -28,6 +29,14 @@ public: GrSamplerParams::kNone_FilterMode)); } + static sk_sp Make(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix) { + return sk_sp( + new GrSimpleTextureEffect(ctx, std::move(proxy), std::move(colorSpaceXform), matrix, + GrSamplerParams::kNone_FilterMode)); + } + /* clamp mode */ static sk_sp Make(GrTexture* tex, sk_sp colorSpaceXform, @@ -37,6 +46,15 @@ public: new GrSimpleTextureEffect(tex, std::move(colorSpaceXform), matrix, filterMode)); } + static sk_sp Make(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix, + GrSamplerParams::FilterMode filterMode) { + return sk_sp( + new GrSimpleTextureEffect(ctx, std::move(proxy), std::move(colorSpaceXform), + matrix, filterMode)); + } + static sk_sp Make(GrTexture* tex, sk_sp colorSpaceXform, const SkMatrix& matrix, @@ -45,6 +63,15 @@ public: matrix, p)); } + static sk_sp Make(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix, + const GrSamplerParams& p) { + return sk_sp(new GrSimpleTextureEffect(ctx, std::move(proxy), + std::move(colorSpaceXform), + matrix, p)); + } + virtual ~GrSimpleTextureEffect() {} const char* name() const override { return "SimpleTexture"; } @@ -58,6 +85,15 @@ private: this->initClassID(); } + GrSimpleTextureEffect(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix, + GrSamplerParams::FilterMode filterMode) + : GrSingleTextureEffect(ctx, std::move(proxy), std::move(colorSpaceXform), + matrix, filterMode) { + this->initClassID(); + } + GrSimpleTextureEffect(GrTexture* texture, sk_sp colorSpaceXform, const SkMatrix& matrix, @@ -66,6 +102,14 @@ private: this->initClassID(); } + GrSimpleTextureEffect(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& matrix, + const GrSamplerParams& params) + : GrSingleTextureEffect(ctx, std::move(proxy), std::move(colorSpaceXform), matrix, params) { + this->initClassID(); + } + GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; diff --git a/src/gpu/effects/GrSingleTextureEffect.cpp b/src/gpu/effects/GrSingleTextureEffect.cpp index 425e367178..c493920d3d 100644 --- a/src/gpu/effects/GrSingleTextureEffect.cpp +++ b/src/gpu/effects/GrSingleTextureEffect.cpp @@ -7,6 +7,9 @@ #include "effects/GrSingleTextureEffect.h" +#include "GrContext.h" +#include "GrTextureProxy.h" + GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, sk_sp colorSpaceXform, const SkMatrix& m) @@ -39,5 +42,37 @@ GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, this->addTextureSampler(&fTextureSampler); } +GrSingleTextureEffect::GrSingleTextureEffect(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& m) + : fCoordTransform(ctx, m, proxy.get(), GrSamplerParams::kNone_FilterMode) + , fTextureSampler(ctx->textureProvider(), std::move(proxy)) + , fColorSpaceXform(std::move(colorSpaceXform)) { + this->addCoordTransform(&fCoordTransform); + this->addTextureSampler(&fTextureSampler); +} + +GrSingleTextureEffect::GrSingleTextureEffect(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& m, + GrSamplerParams::FilterMode filterMode) + : fCoordTransform(ctx, m, proxy.get(), filterMode) + , fTextureSampler(ctx->textureProvider(), std::move(proxy), filterMode) + , fColorSpaceXform(std::move(colorSpaceXform)) { + this->addCoordTransform(&fCoordTransform); + this->addTextureSampler(&fTextureSampler); +} + +GrSingleTextureEffect::GrSingleTextureEffect(GrContext* ctx, sk_sp proxy, + sk_sp colorSpaceXform, + const SkMatrix& m, + const GrSamplerParams& params) + : fCoordTransform(ctx, m, proxy.get(), params.filterMode()) + , fTextureSampler(ctx->textureProvider(), std::move(proxy), params) + , fColorSpaceXform(std::move(colorSpaceXform)) { + this->addCoordTransform(&fCoordTransform); + this->addTextureSampler(&fTextureSampler); +} + GrSingleTextureEffect::~GrSingleTextureEffect() { } diff --git a/src/gpu/effects/GrSingleTextureEffect.h b/src/gpu/effects/GrSingleTextureEffect.h index 716ad2f294..1d0f27a4ff 100644 --- a/src/gpu/effects/GrSingleTextureEffect.h +++ b/src/gpu/effects/GrSingleTextureEffect.h @@ -15,6 +15,7 @@ #include "SkMatrix.h" class GrTexture; +class GrTextureProxy; /** * A base class for effects that draw a single texture with a texture matrix. This effect has no @@ -43,6 +44,17 @@ protected: const SkMatrix&, const GrSamplerParams&); + /** unfiltered, clamp mode */ + GrSingleTextureEffect(GrContext*, + sk_sp, sk_sp, const SkMatrix&); + /** clamp mode */ + GrSingleTextureEffect(GrContext*, + sk_sp, sk_sp, const SkMatrix&, + GrSamplerParams::FilterMode filterMode); + GrSingleTextureEffect(GrContext*, + sk_sp, sk_sp, const SkMatrix&, + const GrSamplerParams&); + /** * Can be used as a helper to implement subclass onComputeInvariantOutput(). It assumes that * the subclass output color will be a modulation of the input color with a value read from the