const GrFragmentProcessor* GrTextureAdjuster::createFragmentProcessor( const SkMatrix& origTextureMatrix, const SkRect& origConstraintRect, FilterConstraint filterConstraint, bool coordsLimitedToConstraintRect, const GrTextureParams::FilterMode* filterOrNullForBicubic) { SkMatrix textureMatrix = origTextureMatrix; const SkIRect* contentArea = this->contentAreaOrNull(); // Convert the constraintRect to be relative to the texture rather than the content area so // that both rects are in the same coordinate system. SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect); if (contentArea) { SkScalar l = SkIntToScalar(contentArea->fLeft); SkScalar t = SkIntToScalar(contentArea->fTop); constraintRect.writable()->offset(l, t); textureMatrix.postTranslate(l, t); } SkRect domain; GrTextureParams params; if (filterOrNullForBicubic) { params.setFilterMode(*filterOrNullForBicubic); } SkAutoTUnref<GrTexture> texture(this->refTextureSafeForParams(params, nullptr)); if (!texture) { return nullptr; } // If we made a copy then we only copied the contentArea, in which case the new texture is all // content. if (texture != this->originalTexture()) { contentArea = nullptr; } DomainMode domainMode = determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, texture->width(), texture->height(), contentArea, filterOrNullForBicubic, &domain); if (kTightCopy_DomainMode == domainMode) { // TODO: Copy the texture and adjust the texture matrix (both parts need to consider // non-int constraint rect) // For now: treat as bilerp and ignore what goes on above level 0. // We only expect MIP maps to require a tight copy. SkASSERT(filterOrNullForBicubic && GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic); static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode; domainMode = determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, texture->width(), texture->height(), contentArea, &kBilerp, &domain); SkASSERT(kTightCopy_DomainMode != domainMode); } SkASSERT(kNoDomain_DomainMode == domainMode || (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); textureMatrix.postIDiv(texture->width(), texture->height()); return create_fp_for_domain_and_filter(texture, textureMatrix, domainMode, domain, filterOrNullForBicubic); }
bool GrDrawTarget::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) { return false; } GrRenderTarget* rt = dst->asRenderTarget(); GrTexture* tex = src->asTexture(); GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit); this->drawState()->setRenderTarget(rt); SkMatrix matrix; matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX), SkIntToScalar(srcRect.fTop - dstPoint.fY)); matrix.postIDiv(tex->width(), tex->height()); this->drawState()->addColorTextureEffect(tex, matrix); SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height()); this->drawSimpleRect(dstRect); return true; }
sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( const SkMatrix& textureMatrix, const SkRect& constraintRect, FilterConstraint filterConstraint, bool coordsLimitedToConstraintRect, const GrTextureParams::FilterMode* filterOrNullForBicubic, SkColorSpace* dstColorSpace, SkSourceGammaTreatment gammaTreatment) { const GrTextureParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic; if (filterOrNullForBicubic && GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic && kYes_FilterConstraint == filterConstraint) { // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will // read outside the constraint rect. However, as in the adjuster case, we aren't currently // doing that. // We instead we compute the domain as though were bilerping which is only correct if we // only sample level 0. static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode; fmForDetermineDomain = &kBilerp; } GrTextureParams params; if (filterOrNullForBicubic) { params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic); } else { // Bicubic doesn't use filtering for it's texture accesses. params.reset(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); } SkAutoTUnref<GrTexture> texture(this->refTextureForParams(params, gammaTreatment)); if (!texture) { return nullptr; } SkRect domain; DomainMode domainMode = determine_domain_mode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, texture->width(), texture->height(), nullptr, fmForDetermineDomain, &domain); SkASSERT(kTightCopy_DomainMode != domainMode); SkMatrix normalizedTextureMatrix = textureMatrix; normalizedTextureMatrix.postIDiv(texture->width(), texture->height()); sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(this->getColorSpace(), dstColorSpace); return create_fp_for_domain_and_filter(texture, std::move(colorSpaceXform), normalizedTextureMatrix, domainMode, domain, filterOrNullForBicubic); }
bool GrDrawTarget::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { SkASSERT(dst); SkASSERT(src); SkIRect clippedSrcRect; SkIPoint clippedDstPoint; // If the rect is outside the src or dst then we've already succeeded. if (!clip_srcrect_and_dstpoint(dst, src, srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) { return true; } if (this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) { return true; } GrRenderTarget* rt = dst->asRenderTarget(); GrTexture* tex = src->asTexture(); if ((dst == src) || !rt || !tex) { return false; } GrPipelineBuilder pipelineBuilder; pipelineBuilder.setRenderTarget(rt); SkMatrix matrix; matrix.setTranslate(SkIntToScalar(clippedSrcRect.fLeft - clippedDstPoint.fX), SkIntToScalar(clippedSrcRect.fTop - clippedDstPoint.fY)); matrix.postIDiv(tex->width(), tex->height()); pipelineBuilder.addColorTextureProcessor(tex, matrix); SkIRect dstRect = SkIRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(), clippedSrcRect.height()); this->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, SkMatrix::I(), dstRect); return true; }
// Draw a mask using the supplied paint. Since the coverage/geometry // is already burnt into the mask this boils down to a rect draw. // Return true if the mask was successfully drawn. static bool draw_mask(GrDrawContext* drawContext, const GrClip& clip, const SkMatrix& viewMatrix, const SkRect& maskRect, GrPaint* grp, GrTexture* mask) { SkMatrix matrix; matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop); matrix.postIDiv(mask->width(), mask->height()); grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(mask, matrix, kDevice_GrCoordSet))->unref(); SkMatrix inverse; if (!viewMatrix.invert(&inverse)) { return false; } drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), maskRect, inverse); return true; }
// Draw a mask using the supplied paint. Since the coverage/geometry // is already burnt into the mask this boils down to a rect draw. // Return true if the mask was successfully drawn. static bool draw_mask(GrDrawContext* drawContext, const GrClip& clip, const SkMatrix& viewMatrix, const SkIRect& maskRect, GrPaint* grp, GrTexture* mask) { SkMatrix matrix; matrix.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); matrix.postIDiv(mask->width(), mask->height()); matrix.preConcat(viewMatrix); grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(mask, nullptr, matrix)); SkMatrix inverse; if (!viewMatrix.invert(&inverse)) { return false; } drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), SkRect::Make(maskRect), inverse); return true; }
bool GrDrawTarget::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { SkASSERT(dst); SkASSERT(src); SkIRect clippedSrcRect; SkIPoint clippedDstPoint; // If the rect is outside the src or dst then we've already succeeded. if (!clip_srcrect_and_dstpoint(dst, src, srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) { SkASSERT(GrDrawTarget::canCopySurface(dst, src, srcRect, dstPoint)); return true; } if (!GrDrawTarget::canCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) { return false; } GrRenderTarget* rt = dst->asRenderTarget(); GrTexture* tex = src->asTexture(); GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit); this->drawState()->setRenderTarget(rt); SkMatrix matrix; matrix.setTranslate(SkIntToScalar(clippedSrcRect.fLeft - clippedDstPoint.fX), SkIntToScalar(clippedSrcRect.fTop - clippedDstPoint.fY)); matrix.postIDiv(tex->width(), tex->height()); this->drawState()->addColorTextureProcessor(tex, matrix); SkIRect dstRect = SkIRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(), clippedSrcRect.height()); this->drawSimpleRect(dstRect); return true; }
SkMatrix GrGLSLPrimitiveProcessor::GetTransformMatrix(const SkMatrix& localMatrix, const GrCoordTransform& coordTransform) { SkMatrix combined; combined.setConcat(coordTransform.getMatrix(), localMatrix); if (coordTransform.normalize()) { combined.postIDiv(coordTransform.peekTexture()->width(), coordTransform.peekTexture()->height()); } if (coordTransform.reverseY()) { // combined.postScale(1,-1); // combined.postTranslate(0,1); combined.set(SkMatrix::kMSkewY, combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]); combined.set(SkMatrix::kMScaleY, combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]); combined.set(SkMatrix::kMTransY, combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]); } return combined; }
virtual void onDraw(SkCanvas* canvas) { SkBaseDevice* device = canvas->getTopDevice(); GrRenderTarget* target = device->accessRenderTarget(); GrContext* ctx = GetGr(); if (ctx && target) { SkPMColor gTextureData[(2 * S) * (2 * S)]; static const int stride = 2 * S; static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); static 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; } } GrTextureDesc desc; // use RT flag bit because in GL it makes the texture be bottom-up desc.fFlags = i ? kRenderTarget_GrTextureFlagBit : kNone_GrTextureFlags; desc.fConfig = kSkia8888_GrPixelConfig; desc.fWidth = 2 * S; desc.fHeight = 2 * S; GrTexture* texture = ctx->createUncachedTexture(desc, gTextureData, 0); if (!texture) { return; } GrAutoUnref au(texture); GrContext::AutoClip acs(ctx, SkRect::MakeWH(2*S, 2*S)); ctx->setRenderTarget(target); GrPaint paint; paint.setBlendFunc(kOne_GrBlendCoeff, kISA_GrBlendCoeff); SkMatrix vm; if (i) { vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1); } else { vm.reset(); } ctx->setMatrix(vm); SkMatrix tm; tm = vm; tm.postIDiv(2*S, 2*S); paint.addColorTextureEffect(texture, tm); ctx->drawRect(paint, 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; } } texture->writePixels(S, (i ? 0 : S), S, S, texture->config(), gTextureData, 4 * stride); ctx->drawRect(paint, SkRect::MakeWH(2*S, 2*S)); } } }