michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FilterNodeD2D1.h" michael@0: michael@0: #include "Logging.h" michael@0: michael@0: #include "SourceSurfaceD2D1.h" michael@0: #include "SourceSurfaceD2D.h" michael@0: #include "SourceSurfaceD2DTarget.h" michael@0: #include "DrawTargetD2D.h" michael@0: #include "DrawTargetD2D1.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: D2D1_COLORMATRIX_ALPHA_MODE D2DAlphaMode(uint32_t aMode) michael@0: { michael@0: switch (aMode) { michael@0: case ALPHA_MODE_PREMULTIPLIED: michael@0: return D2D1_COLORMATRIX_ALPHA_MODE_PREMULTIPLIED; michael@0: case ALPHA_MODE_STRAIGHT: michael@0: return D2D1_COLORMATRIX_ALPHA_MODE_STRAIGHT; michael@0: default: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: } michael@0: michael@0: return D2D1_COLORMATRIX_ALPHA_MODE_PREMULTIPLIED; michael@0: } michael@0: michael@0: D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE D2DAffineTransformInterpolationMode(Filter aFilter) michael@0: { michael@0: switch (aFilter) { michael@0: case Filter::GOOD: michael@0: return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_LINEAR; michael@0: case Filter::LINEAR: michael@0: return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_LINEAR; michael@0: case Filter::POINT: michael@0: return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_NEAREST_NEIGHBOR; michael@0: default: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: } michael@0: michael@0: return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_LINEAR; michael@0: } michael@0: michael@0: D2D1_BLEND_MODE D2DBlendMode(uint32_t aMode) michael@0: { michael@0: switch (aMode) { michael@0: case BLEND_MODE_DARKEN: michael@0: return D2D1_BLEND_MODE_DARKEN; michael@0: case BLEND_MODE_LIGHTEN: michael@0: return D2D1_BLEND_MODE_LIGHTEN; michael@0: case BLEND_MODE_MULTIPLY: michael@0: return D2D1_BLEND_MODE_MULTIPLY; michael@0: case BLEND_MODE_SCREEN: michael@0: return D2D1_BLEND_MODE_SCREEN; michael@0: default: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: } michael@0: michael@0: return D2D1_BLEND_MODE_DARKEN; michael@0: } michael@0: michael@0: D2D1_MORPHOLOGY_MODE D2DMorphologyMode(uint32_t aMode) michael@0: { michael@0: switch (aMode) { michael@0: case MORPHOLOGY_OPERATOR_DILATE: michael@0: return D2D1_MORPHOLOGY_MODE_DILATE; michael@0: case MORPHOLOGY_OPERATOR_ERODE: michael@0: return D2D1_MORPHOLOGY_MODE_ERODE; michael@0: } michael@0: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: return D2D1_MORPHOLOGY_MODE_DILATE; michael@0: } michael@0: michael@0: D2D1_TURBULENCE_NOISE D2DTurbulenceNoise(uint32_t aMode) michael@0: { michael@0: switch (aMode) { michael@0: case TURBULENCE_TYPE_FRACTAL_NOISE: michael@0: return D2D1_TURBULENCE_NOISE_FRACTAL_SUM; michael@0: case TURBULENCE_TYPE_TURBULENCE: michael@0: return D2D1_TURBULENCE_NOISE_TURBULENCE; michael@0: } michael@0: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: return D2D1_TURBULENCE_NOISE_TURBULENCE; michael@0: } michael@0: michael@0: D2D1_COMPOSITE_MODE D2DFilterCompositionMode(uint32_t aMode) michael@0: { michael@0: switch (aMode) { michael@0: case COMPOSITE_OPERATOR_OVER: michael@0: return D2D1_COMPOSITE_MODE_SOURCE_OVER; michael@0: case COMPOSITE_OPERATOR_IN: michael@0: return D2D1_COMPOSITE_MODE_SOURCE_IN; michael@0: case COMPOSITE_OPERATOR_OUT: michael@0: return D2D1_COMPOSITE_MODE_SOURCE_OUT; michael@0: case COMPOSITE_OPERATOR_ATOP: michael@0: return D2D1_COMPOSITE_MODE_SOURCE_ATOP; michael@0: case COMPOSITE_OPERATOR_XOR: michael@0: return D2D1_COMPOSITE_MODE_XOR; michael@0: } michael@0: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: return D2D1_COMPOSITE_MODE_SOURCE_OVER; michael@0: } michael@0: michael@0: D2D1_CHANNEL_SELECTOR D2DChannelSelector(uint32_t aMode) michael@0: { michael@0: switch (aMode) { michael@0: case COLOR_CHANNEL_R: michael@0: return D2D1_CHANNEL_SELECTOR_R; michael@0: case COLOR_CHANNEL_G: michael@0: return D2D1_CHANNEL_SELECTOR_G; michael@0: case COLOR_CHANNEL_B: michael@0: return D2D1_CHANNEL_SELECTOR_B; michael@0: case COLOR_CHANNEL_A: michael@0: return D2D1_CHANNEL_SELECTOR_A; michael@0: } michael@0: michael@0: MOZ_CRASH("Unknown enum value!"); michael@0: return D2D1_CHANNEL_SELECTOR_R; michael@0: } michael@0: michael@0: TemporaryRef GetImageForSourceSurface(DrawTarget *aDT, SourceSurface *aSurface) michael@0: { michael@0: switch (aDT->GetType()) { michael@0: case BackendType::DIRECT2D1_1: michael@0: return static_cast(aDT)->GetImageForSurface(aSurface, ExtendMode::CLAMP); michael@0: case BackendType::DIRECT2D: michael@0: return static_cast(aDT)->GetImageForSurface(aSurface); michael@0: default: michael@0: MOZ_CRASH("Unknown draw target type!"); michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: uint32_t ConvertValue(FilterType aType, uint32_t aAttribute, uint32_t aValue) michael@0: { michael@0: switch (aType) { michael@0: case FilterType::COLOR_MATRIX: michael@0: if (aAttribute == ATT_COLOR_MATRIX_ALPHA_MODE) { michael@0: aValue = D2DAlphaMode(aValue); michael@0: } michael@0: break; michael@0: case FilterType::TRANSFORM: michael@0: if (aAttribute == ATT_TRANSFORM_FILTER) { michael@0: aValue = D2DAffineTransformInterpolationMode(Filter(aValue)); michael@0: } michael@0: break; michael@0: case FilterType::BLEND: michael@0: if (aAttribute == ATT_BLEND_BLENDMODE) { michael@0: aValue = D2DBlendMode(aValue); michael@0: } michael@0: break; michael@0: case FilterType::MORPHOLOGY: michael@0: if (aAttribute == ATT_MORPHOLOGY_OPERATOR) { michael@0: aValue = D2DMorphologyMode(aValue); michael@0: } michael@0: break; michael@0: case FilterType::DISPLACEMENT_MAP: michael@0: if (aAttribute == ATT_DISPLACEMENT_MAP_X_CHANNEL || michael@0: aAttribute == ATT_DISPLACEMENT_MAP_Y_CHANNEL) { michael@0: aValue = D2DChannelSelector(aValue); michael@0: } michael@0: break; michael@0: case FilterType::TURBULENCE: michael@0: if (aAttribute == ATT_TURBULENCE_TYPE) { michael@0: aValue = D2DTurbulenceNoise(aValue); michael@0: } michael@0: break; michael@0: case FilterType::COMPOSITE: michael@0: if (aAttribute == ATT_COMPOSITE_OPERATOR) { michael@0: aValue = D2DFilterCompositionMode(aValue); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: return aValue; michael@0: } michael@0: michael@0: void ConvertValue(FilterType aType, uint32_t aAttribute, IntSize &aValue) michael@0: { michael@0: switch (aType) { michael@0: case FilterType::MORPHOLOGY: michael@0: if (aAttribute == ATT_MORPHOLOGY_RADII) { michael@0: aValue.width *= 2; michael@0: aValue.width += 1; michael@0: aValue.height *= 2; michael@0: aValue.height += 1; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: UINT32 michael@0: GetD2D1InputForInput(FilterType aType, uint32_t aIndex) michael@0: { michael@0: return aIndex; michael@0: } michael@0: michael@0: #define CONVERT_PROP(moz2dname, d2dname) \ michael@0: case ATT_##moz2dname: \ michael@0: return D2D1_##d2dname michael@0: michael@0: UINT32 michael@0: GetD2D1PropForAttribute(FilterType aType, uint32_t aIndex) michael@0: { michael@0: switch (aType) { michael@0: case FilterType::COLOR_MATRIX: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(COLOR_MATRIX_MATRIX, COLORMATRIX_PROP_COLOR_MATRIX); michael@0: CONVERT_PROP(COLOR_MATRIX_ALPHA_MODE, COLORMATRIX_PROP_ALPHA_MODE); michael@0: } michael@0: break; michael@0: case FilterType::TRANSFORM: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(TRANSFORM_MATRIX, 2DAFFINETRANSFORM_PROP_TRANSFORM_MATRIX); michael@0: CONVERT_PROP(TRANSFORM_FILTER, 2DAFFINETRANSFORM_PROP_INTERPOLATION_MODE); michael@0: } michael@0: case FilterType::BLEND: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(BLEND_BLENDMODE, BLEND_PROP_MODE); michael@0: } michael@0: break; michael@0: case FilterType::MORPHOLOGY: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(MORPHOLOGY_OPERATOR, MORPHOLOGY_PROP_MODE); michael@0: } michael@0: break; michael@0: case FilterType::FLOOD: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(FLOOD_COLOR, FLOOD_PROP_COLOR); michael@0: } michael@0: break; michael@0: case FilterType::TILE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(TILE_SOURCE_RECT, TILE_PROP_RECT); michael@0: } michael@0: break; michael@0: case FilterType::TABLE_TRANSFER: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(TABLE_TRANSFER_DISABLE_R, TABLETRANSFER_PROP_RED_DISABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_DISABLE_G, TABLETRANSFER_PROP_GREEN_DISABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_DISABLE_B, TABLETRANSFER_PROP_BLUE_DISABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_DISABLE_A, TABLETRANSFER_PROP_ALPHA_DISABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_TABLE_R, TABLETRANSFER_PROP_RED_TABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_TABLE_G, TABLETRANSFER_PROP_GREEN_TABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_TABLE_B, TABLETRANSFER_PROP_BLUE_TABLE); michael@0: CONVERT_PROP(TABLE_TRANSFER_TABLE_A, TABLETRANSFER_PROP_ALPHA_TABLE); michael@0: } michael@0: break; michael@0: case FilterType::DISCRETE_TRANSFER: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_R, DISCRETETRANSFER_PROP_RED_DISABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_G, DISCRETETRANSFER_PROP_GREEN_DISABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_B, DISCRETETRANSFER_PROP_BLUE_DISABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_A, DISCRETETRANSFER_PROP_ALPHA_DISABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_TABLE_R, DISCRETETRANSFER_PROP_RED_TABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_TABLE_G, DISCRETETRANSFER_PROP_GREEN_TABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_TABLE_B, DISCRETETRANSFER_PROP_BLUE_TABLE); michael@0: CONVERT_PROP(DISCRETE_TRANSFER_TABLE_A, DISCRETETRANSFER_PROP_ALPHA_TABLE); michael@0: } michael@0: break; michael@0: case FilterType::LINEAR_TRANSFER: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(LINEAR_TRANSFER_DISABLE_R, LINEARTRANSFER_PROP_RED_DISABLE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_DISABLE_G, LINEARTRANSFER_PROP_GREEN_DISABLE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_DISABLE_B, LINEARTRANSFER_PROP_BLUE_DISABLE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_DISABLE_A, LINEARTRANSFER_PROP_ALPHA_DISABLE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_R, LINEARTRANSFER_PROP_RED_Y_INTERCEPT); michael@0: CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_G, LINEARTRANSFER_PROP_GREEN_Y_INTERCEPT); michael@0: CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_B, LINEARTRANSFER_PROP_BLUE_Y_INTERCEPT); michael@0: CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_A, LINEARTRANSFER_PROP_ALPHA_Y_INTERCEPT); michael@0: CONVERT_PROP(LINEAR_TRANSFER_SLOPE_R, LINEARTRANSFER_PROP_RED_SLOPE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_SLOPE_G, LINEARTRANSFER_PROP_GREEN_SLOPE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_SLOPE_B, LINEARTRANSFER_PROP_BLUE_SLOPE); michael@0: CONVERT_PROP(LINEAR_TRANSFER_SLOPE_A, LINEARTRANSFER_PROP_ALPHA_SLOPE); michael@0: } michael@0: break; michael@0: case FilterType::GAMMA_TRANSFER: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(GAMMA_TRANSFER_DISABLE_R, GAMMATRANSFER_PROP_RED_DISABLE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_DISABLE_G, GAMMATRANSFER_PROP_GREEN_DISABLE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_DISABLE_B, GAMMATRANSFER_PROP_BLUE_DISABLE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_DISABLE_A, GAMMATRANSFER_PROP_ALPHA_DISABLE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_R, GAMMATRANSFER_PROP_RED_AMPLITUDE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_G, GAMMATRANSFER_PROP_GREEN_AMPLITUDE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_B, GAMMATRANSFER_PROP_BLUE_AMPLITUDE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_A, GAMMATRANSFER_PROP_ALPHA_AMPLITUDE); michael@0: CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_R, GAMMATRANSFER_PROP_RED_EXPONENT); michael@0: CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_G, GAMMATRANSFER_PROP_GREEN_EXPONENT); michael@0: CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_B, GAMMATRANSFER_PROP_BLUE_EXPONENT); michael@0: CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_A, GAMMATRANSFER_PROP_ALPHA_EXPONENT); michael@0: CONVERT_PROP(GAMMA_TRANSFER_OFFSET_R, GAMMATRANSFER_PROP_RED_OFFSET); michael@0: CONVERT_PROP(GAMMA_TRANSFER_OFFSET_G, GAMMATRANSFER_PROP_GREEN_OFFSET); michael@0: CONVERT_PROP(GAMMA_TRANSFER_OFFSET_B, GAMMATRANSFER_PROP_BLUE_OFFSET); michael@0: CONVERT_PROP(GAMMA_TRANSFER_OFFSET_A, GAMMATRANSFER_PROP_ALPHA_OFFSET); michael@0: } michael@0: break; michael@0: case FilterType::CONVOLVE_MATRIX: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(CONVOLVE_MATRIX_BIAS, CONVOLVEMATRIX_PROP_BIAS); michael@0: CONVERT_PROP(CONVOLVE_MATRIX_KERNEL_MATRIX, CONVOLVEMATRIX_PROP_KERNEL_MATRIX); michael@0: CONVERT_PROP(CONVOLVE_MATRIX_DIVISOR, CONVOLVEMATRIX_PROP_DIVISOR); michael@0: CONVERT_PROP(CONVOLVE_MATRIX_KERNEL_UNIT_LENGTH, CONVOLVEMATRIX_PROP_KERNEL_UNIT_LENGTH); michael@0: CONVERT_PROP(CONVOLVE_MATRIX_PRESERVE_ALPHA, CONVOLVEMATRIX_PROP_PRESERVE_ALPHA); michael@0: } michael@0: case FilterType::DISPLACEMENT_MAP: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(DISPLACEMENT_MAP_SCALE, DISPLACEMENTMAP_PROP_SCALE); michael@0: CONVERT_PROP(DISPLACEMENT_MAP_X_CHANNEL, DISPLACEMENTMAP_PROP_X_CHANNEL_SELECT); michael@0: CONVERT_PROP(DISPLACEMENT_MAP_Y_CHANNEL, DISPLACEMENTMAP_PROP_Y_CHANNEL_SELECT); michael@0: } michael@0: break; michael@0: case FilterType::TURBULENCE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(TURBULENCE_BASE_FREQUENCY, TURBULENCE_PROP_BASE_FREQUENCY); michael@0: CONVERT_PROP(TURBULENCE_NUM_OCTAVES, TURBULENCE_PROP_NUM_OCTAVES); michael@0: CONVERT_PROP(TURBULENCE_SEED, TURBULENCE_PROP_SEED); michael@0: CONVERT_PROP(TURBULENCE_STITCHABLE, TURBULENCE_PROP_STITCHABLE); michael@0: CONVERT_PROP(TURBULENCE_TYPE, TURBULENCE_PROP_NOISE); michael@0: } michael@0: break; michael@0: case FilterType::ARITHMETIC_COMBINE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(ARITHMETIC_COMBINE_COEFFICIENTS, ARITHMETICCOMPOSITE_PROP_COEFFICIENTS); michael@0: } michael@0: break; michael@0: case FilterType::COMPOSITE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(COMPOSITE_OPERATOR, COMPOSITE_PROP_MODE); michael@0: } michael@0: break; michael@0: case FilterType::GAUSSIAN_BLUR: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(GAUSSIAN_BLUR_STD_DEVIATION, GAUSSIANBLUR_PROP_STANDARD_DEVIATION); michael@0: } michael@0: break; michael@0: case FilterType::DIRECTIONAL_BLUR: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(DIRECTIONAL_BLUR_STD_DEVIATION, DIRECTIONALBLUR_PROP_STANDARD_DEVIATION); michael@0: CONVERT_PROP(DIRECTIONAL_BLUR_DIRECTION, DIRECTIONALBLUR_PROP_ANGLE); michael@0: } michael@0: break; michael@0: case FilterType::POINT_DIFFUSE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(POINT_DIFFUSE_DIFFUSE_CONSTANT, POINTDIFFUSE_PROP_DIFFUSE_CONSTANT); michael@0: CONVERT_PROP(POINT_DIFFUSE_POSITION, POINTDIFFUSE_PROP_LIGHT_POSITION); michael@0: CONVERT_PROP(POINT_DIFFUSE_COLOR, POINTDIFFUSE_PROP_COLOR); michael@0: CONVERT_PROP(POINT_DIFFUSE_SURFACE_SCALE, POINTDIFFUSE_PROP_SURFACE_SCALE); michael@0: CONVERT_PROP(POINT_DIFFUSE_KERNEL_UNIT_LENGTH, POINTDIFFUSE_PROP_KERNEL_UNIT_LENGTH); michael@0: } michael@0: break; michael@0: case FilterType::SPOT_DIFFUSE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(SPOT_DIFFUSE_DIFFUSE_CONSTANT, SPOTDIFFUSE_PROP_DIFFUSE_CONSTANT); michael@0: CONVERT_PROP(SPOT_DIFFUSE_POINTS_AT, SPOTDIFFUSE_PROP_POINTS_AT); michael@0: CONVERT_PROP(SPOT_DIFFUSE_FOCUS, SPOTDIFFUSE_PROP_FOCUS); michael@0: CONVERT_PROP(SPOT_DIFFUSE_LIMITING_CONE_ANGLE, SPOTDIFFUSE_PROP_LIMITING_CONE_ANGLE); michael@0: CONVERT_PROP(SPOT_DIFFUSE_POSITION, SPOTDIFFUSE_PROP_LIGHT_POSITION); michael@0: CONVERT_PROP(SPOT_DIFFUSE_COLOR, SPOTDIFFUSE_PROP_COLOR); michael@0: CONVERT_PROP(SPOT_DIFFUSE_SURFACE_SCALE, SPOTDIFFUSE_PROP_SURFACE_SCALE); michael@0: CONVERT_PROP(SPOT_DIFFUSE_KERNEL_UNIT_LENGTH, SPOTDIFFUSE_PROP_KERNEL_UNIT_LENGTH); michael@0: } michael@0: break; michael@0: case FilterType::DISTANT_DIFFUSE: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(DISTANT_DIFFUSE_DIFFUSE_CONSTANT, DISTANTDIFFUSE_PROP_DIFFUSE_CONSTANT); michael@0: CONVERT_PROP(DISTANT_DIFFUSE_AZIMUTH, DISTANTDIFFUSE_PROP_AZIMUTH); michael@0: CONVERT_PROP(DISTANT_DIFFUSE_ELEVATION, DISTANTDIFFUSE_PROP_ELEVATION); michael@0: CONVERT_PROP(DISTANT_DIFFUSE_COLOR, DISTANTDIFFUSE_PROP_COLOR); michael@0: CONVERT_PROP(DISTANT_DIFFUSE_SURFACE_SCALE, DISTANTDIFFUSE_PROP_SURFACE_SCALE); michael@0: CONVERT_PROP(DISTANT_DIFFUSE_KERNEL_UNIT_LENGTH, DISTANTDIFFUSE_PROP_KERNEL_UNIT_LENGTH); michael@0: } michael@0: break; michael@0: case FilterType::POINT_SPECULAR: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(POINT_SPECULAR_SPECULAR_CONSTANT, POINTSPECULAR_PROP_SPECULAR_CONSTANT); michael@0: CONVERT_PROP(POINT_SPECULAR_SPECULAR_EXPONENT, POINTSPECULAR_PROP_SPECULAR_EXPONENT); michael@0: CONVERT_PROP(POINT_SPECULAR_POSITION, POINTSPECULAR_PROP_LIGHT_POSITION); michael@0: CONVERT_PROP(POINT_SPECULAR_COLOR, POINTSPECULAR_PROP_COLOR); michael@0: CONVERT_PROP(POINT_SPECULAR_SURFACE_SCALE, POINTSPECULAR_PROP_SURFACE_SCALE); michael@0: CONVERT_PROP(POINT_SPECULAR_KERNEL_UNIT_LENGTH, POINTSPECULAR_PROP_KERNEL_UNIT_LENGTH); michael@0: } michael@0: break; michael@0: case FilterType::SPOT_SPECULAR: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(SPOT_SPECULAR_SPECULAR_CONSTANT, SPOTSPECULAR_PROP_SPECULAR_CONSTANT); michael@0: CONVERT_PROP(SPOT_SPECULAR_SPECULAR_EXPONENT, SPOTSPECULAR_PROP_SPECULAR_EXPONENT); michael@0: CONVERT_PROP(SPOT_SPECULAR_POINTS_AT, SPOTSPECULAR_PROP_POINTS_AT); michael@0: CONVERT_PROP(SPOT_SPECULAR_FOCUS, SPOTSPECULAR_PROP_FOCUS); michael@0: CONVERT_PROP(SPOT_SPECULAR_LIMITING_CONE_ANGLE, SPOTSPECULAR_PROP_LIMITING_CONE_ANGLE); michael@0: CONVERT_PROP(SPOT_SPECULAR_POSITION, SPOTSPECULAR_PROP_LIGHT_POSITION); michael@0: CONVERT_PROP(SPOT_SPECULAR_COLOR, SPOTSPECULAR_PROP_COLOR); michael@0: CONVERT_PROP(SPOT_SPECULAR_SURFACE_SCALE, SPOTSPECULAR_PROP_SURFACE_SCALE); michael@0: CONVERT_PROP(SPOT_SPECULAR_KERNEL_UNIT_LENGTH, SPOTSPECULAR_PROP_KERNEL_UNIT_LENGTH); michael@0: } michael@0: break; michael@0: case FilterType::DISTANT_SPECULAR: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(DISTANT_SPECULAR_SPECULAR_CONSTANT, DISTANTSPECULAR_PROP_SPECULAR_CONSTANT); michael@0: CONVERT_PROP(DISTANT_SPECULAR_SPECULAR_EXPONENT, DISTANTSPECULAR_PROP_SPECULAR_EXPONENT); michael@0: CONVERT_PROP(DISTANT_SPECULAR_AZIMUTH, DISTANTSPECULAR_PROP_AZIMUTH); michael@0: CONVERT_PROP(DISTANT_SPECULAR_ELEVATION, DISTANTSPECULAR_PROP_ELEVATION); michael@0: CONVERT_PROP(DISTANT_SPECULAR_COLOR, DISTANTSPECULAR_PROP_COLOR); michael@0: CONVERT_PROP(DISTANT_SPECULAR_SURFACE_SCALE, DISTANTSPECULAR_PROP_SURFACE_SCALE); michael@0: CONVERT_PROP(DISTANT_SPECULAR_KERNEL_UNIT_LENGTH, DISTANTSPECULAR_PROP_KERNEL_UNIT_LENGTH); michael@0: } michael@0: break; michael@0: case FilterType::CROP: michael@0: switch (aIndex) { michael@0: CONVERT_PROP(CROP_RECT, CROP_PROP_RECT); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: return UINT32_MAX; michael@0: } michael@0: michael@0: bool michael@0: GetD2D1PropsForIntSize(FilterType aType, uint32_t aIndex, UINT32 *aPropWidth, UINT32 *aPropHeight) michael@0: { michael@0: switch (aType) { michael@0: case FilterType::MORPHOLOGY: michael@0: if (aIndex == ATT_MORPHOLOGY_RADII) { michael@0: *aPropWidth = D2D1_MORPHOLOGY_PROP_WIDTH; michael@0: *aPropHeight = D2D1_MORPHOLOGY_PROP_HEIGHT; michael@0: return true; michael@0: } michael@0: break; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static inline REFCLSID GetCLDIDForFilterType(FilterType aType) michael@0: { michael@0: switch (aType) { michael@0: case FilterType::COLOR_MATRIX: michael@0: return CLSID_D2D1ColorMatrix; michael@0: case FilterType::TRANSFORM: michael@0: return CLSID_D2D12DAffineTransform; michael@0: case FilterType::BLEND: michael@0: return CLSID_D2D1Blend; michael@0: case FilterType::MORPHOLOGY: michael@0: return CLSID_D2D1Morphology; michael@0: case FilterType::FLOOD: michael@0: return CLSID_D2D1Flood; michael@0: case FilterType::TILE: michael@0: return CLSID_D2D1Tile; michael@0: case FilterType::TABLE_TRANSFER: michael@0: return CLSID_D2D1TableTransfer; michael@0: case FilterType::LINEAR_TRANSFER: michael@0: return CLSID_D2D1LinearTransfer; michael@0: case FilterType::DISCRETE_TRANSFER: michael@0: return CLSID_D2D1DiscreteTransfer; michael@0: case FilterType::GAMMA_TRANSFER: michael@0: return CLSID_D2D1GammaTransfer; michael@0: case FilterType::DISPLACEMENT_MAP: michael@0: return CLSID_D2D1DisplacementMap; michael@0: case FilterType::TURBULENCE: michael@0: return CLSID_D2D1Turbulence; michael@0: case FilterType::ARITHMETIC_COMBINE: michael@0: return CLSID_D2D1ArithmeticComposite; michael@0: case FilterType::COMPOSITE: michael@0: return CLSID_D2D1Composite; michael@0: case FilterType::GAUSSIAN_BLUR: michael@0: return CLSID_D2D1GaussianBlur; michael@0: case FilterType::DIRECTIONAL_BLUR: michael@0: return CLSID_D2D1DirectionalBlur; michael@0: case FilterType::POINT_DIFFUSE: michael@0: return CLSID_D2D1PointDiffuse; michael@0: case FilterType::POINT_SPECULAR: michael@0: return CLSID_D2D1PointSpecular; michael@0: case FilterType::SPOT_DIFFUSE: michael@0: return CLSID_D2D1SpotDiffuse; michael@0: case FilterType::SPOT_SPECULAR: michael@0: return CLSID_D2D1SpotSpecular; michael@0: case FilterType::DISTANT_DIFFUSE: michael@0: return CLSID_D2D1DistantDiffuse; michael@0: case FilterType::DISTANT_SPECULAR: michael@0: return CLSID_D2D1DistantSpecular; michael@0: case FilterType::CROP: michael@0: return CLSID_D2D1Crop; michael@0: case FilterType::PREMULTIPLY: michael@0: return CLSID_D2D1Premultiply; michael@0: case FilterType::UNPREMULTIPLY: michael@0: return CLSID_D2D1UnPremultiply; michael@0: } michael@0: return GUID_NULL; michael@0: } michael@0: michael@0: /* static */ michael@0: TemporaryRef michael@0: FilterNodeD2D1::Create(DrawTarget* aDT, ID2D1DeviceContext *aDC, FilterType aType) michael@0: { michael@0: if (aType == FilterType::CONVOLVE_MATRIX) { michael@0: return new FilterNodeConvolveD2D1(aDT, aDC); michael@0: } michael@0: michael@0: RefPtr effect; michael@0: HRESULT hr; michael@0: michael@0: hr = aDC->CreateEffect(GetCLDIDForFilterType(aType), byRef(effect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create effect for FilterType: " << hr; michael@0: return nullptr; michael@0: } michael@0: michael@0: switch (aType) { michael@0: case FilterType::LINEAR_TRANSFER: michael@0: case FilterType::GAMMA_TRANSFER: michael@0: case FilterType::TABLE_TRANSFER: michael@0: case FilterType::DISCRETE_TRANSFER: michael@0: return new FilterNodeComponentTransferD2D1(aDT, aDC, effect, aType); michael@0: default: michael@0: return new FilterNodeD2D1(aDT, effect, aType); michael@0: } michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::InitUnmappedProperties() michael@0: { michael@0: switch (mType) { michael@0: case FilterType::TRANSFORM: michael@0: mEffect->SetValue(D2D1_2DAFFINETRANSFORM_PROP_BORDER_MODE, D2D1_BORDER_MODE_HARD); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetInput(uint32_t aIndex, SourceSurface *aSurface) michael@0: { michael@0: UINT32 input = GetD2D1InputForInput(mType, aIndex); michael@0: ID2D1Effect* effect = InputEffect(); michael@0: MOZ_ASSERT(input < effect->GetInputCount()); michael@0: michael@0: if (mType == FilterType::COMPOSITE) { michael@0: UINT32 inputCount = effect->GetInputCount(); michael@0: michael@0: if (aIndex == inputCount - 1 && aSurface == nullptr) { michael@0: effect->SetInputCount(inputCount - 1); michael@0: } else if (aIndex >= inputCount && aSurface) { michael@0: effect->SetInputCount(aIndex + 1); michael@0: } michael@0: } michael@0: michael@0: RefPtr image = GetImageForSourceSurface(mDT, aSurface); michael@0: effect->SetInput(input, image); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetInput(uint32_t aIndex, FilterNode *aFilter) michael@0: { michael@0: UINT32 input = GetD2D1InputForInput(mType, aIndex); michael@0: ID2D1Effect* effect = InputEffect(); michael@0: michael@0: if (mType == FilterType::COMPOSITE) { michael@0: UINT32 inputCount = effect->GetInputCount(); michael@0: michael@0: if (aIndex == inputCount - 1 && aFilter == nullptr) { michael@0: effect->SetInputCount(inputCount - 1); michael@0: } else if (aIndex >= inputCount && aFilter) { michael@0: effect->SetInputCount(aIndex + 1); michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(input < effect->GetInputCount()); michael@0: michael@0: if (aFilter->GetBackendType() != FILTER_BACKEND_DIRECT2D1_1) { michael@0: gfxWarning() << "Unknown input SourceSurface set on effect."; michael@0: MOZ_ASSERT(0); michael@0: return; michael@0: } michael@0: michael@0: effect->SetInputEffect(input, static_cast(aFilter)->OutputEffect()); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, uint32_t aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: if (mType == FilterType::TURBULENCE && aIndex == ATT_TURBULENCE_BASE_FREQUENCY) { michael@0: mEffect->SetValue(input, D2D1::Vector2F(FLOAT(aValue), FLOAT(aValue))); michael@0: return; michael@0: } else if (mType == FilterType::DIRECTIONAL_BLUR && aIndex == ATT_DIRECTIONAL_BLUR_DIRECTION) { michael@0: mEffect->SetValue(input, aValue == BLUR_DIRECTION_X ? 0 : 90.0f); michael@0: return; michael@0: } michael@0: michael@0: mEffect->SetValue(input, ConvertValue(mType, aIndex, aValue)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, Float aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, aValue); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Point &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2DPoint(aValue)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Matrix5x4 &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2DMatrix5x4(aValue)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Point3D &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2DVector3D(aValue)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Size &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2D1::Vector2F(aValue.width, aValue.height)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const IntSize &aValue) michael@0: { michael@0: UINT32 widthProp, heightProp; michael@0: michael@0: if (!GetD2D1PropsForIntSize(mType, aIndex, &widthProp, &heightProp)) { michael@0: return; michael@0: } michael@0: michael@0: IntSize value = aValue; michael@0: ConvertValue(mType, aIndex, value); michael@0: michael@0: mEffect->SetValue(widthProp, (UINT)value.width); michael@0: mEffect->SetValue(heightProp, (UINT)value.height); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Color &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: switch (mType) { michael@0: case FilterType::POINT_DIFFUSE: michael@0: case FilterType::SPOT_DIFFUSE: michael@0: case FilterType::DISTANT_DIFFUSE: michael@0: case FilterType::POINT_SPECULAR: michael@0: case FilterType::SPOT_SPECULAR: michael@0: case FilterType::DISTANT_SPECULAR: michael@0: mEffect->SetValue(input, D2D1::Vector3F(aValue.r, aValue.g, aValue.b)); michael@0: break; michael@0: default: michael@0: mEffect->SetValue(input, D2D1::Vector4F(aValue.r * aValue.a, aValue.g * aValue.a, aValue.b * aValue.a, aValue.a)); michael@0: } michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Rect &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2DRect(aValue)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const IntRect &aValue) michael@0: { michael@0: if (mType == FilterType::TURBULENCE) { michael@0: MOZ_ASSERT(aIndex == ATT_TURBULENCE_RECT); michael@0: michael@0: mEffect->SetValue(D2D1_TURBULENCE_PROP_OFFSET, D2D1::Vector2F(Float(aValue.x), Float(aValue.y))); michael@0: mEffect->SetValue(D2D1_TURBULENCE_PROP_SIZE, D2D1::Vector2F(Float(aValue.width), Float(aValue.height))); michael@0: return; michael@0: } michael@0: michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2D1::RectF(Float(aValue.x), Float(aValue.y), michael@0: Float(aValue.XMost()), Float(aValue.YMost()))); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, bool aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, (BOOL)aValue); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Float *aValues, uint32_t aSize) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, (BYTE*)aValues, sizeof(Float) * aSize); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const IntPoint &aValue) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2DPoint(aValue)); michael@0: } michael@0: michael@0: void michael@0: FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Matrix &aMatrix) michael@0: { michael@0: UINT32 input = GetD2D1PropForAttribute(mType, aIndex); michael@0: MOZ_ASSERT(input < mEffect->GetPropertyCount()); michael@0: michael@0: mEffect->SetValue(input, D2DMatrix(aMatrix)); michael@0: } michael@0: michael@0: FilterNodeConvolveD2D1::FilterNodeConvolveD2D1(DrawTarget *aDT, ID2D1DeviceContext *aDC) michael@0: : FilterNodeD2D1(aDT, nullptr, FilterType::CONVOLVE_MATRIX) michael@0: , mEdgeMode(EDGE_MODE_DUPLICATE) michael@0: { michael@0: // Correctly handling the interaction of edge mode and source rect is a bit michael@0: // tricky with D2D1 effects. We want the edge mode to only apply outside of michael@0: // the source rect (as specified by the ATT_CONVOLVE_MATRIX_SOURCE_RECT michael@0: // attribute). So if our input surface or filter is smaller than the source michael@0: // rect, we need to add transparency around it until we reach the edges of michael@0: // the source rect, and only then do any repeating or edge duplicating. michael@0: // Unfortunately, D2D1 does not have any "extend with transparency" effect. michael@0: // (The crop effect can only cut off parts, it can't make the output rect michael@0: // bigger.) And the border effect does not have a source rect attribute - michael@0: // it only looks at the output rect of its input filter or surface. michael@0: // So we use the following trick to extend the input size to the source rect: michael@0: // Instead of feeding the input directly into the border effect, we first michael@0: // composite it with a transparent flood effect (which is infinite-sized) and michael@0: // use a crop effect on the result in order to get the right size. Then we michael@0: // feed the cropped composition into the border effect, which then finally michael@0: // feeds into the convolve matrix effect. michael@0: // All of this is only necessary when our edge mode is not EDGE_MODE_NONE, so michael@0: // we update the filter chain dynamically in UpdateChain(). michael@0: michael@0: HRESULT hr; michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1ConvolveMatrix, byRef(mEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ConvolveMatrix filter!"; michael@0: return; michael@0: } michael@0: michael@0: mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_BORDER_MODE, D2D1_BORDER_MODE_SOFT); michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1Flood, byRef(mFloodEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ConvolveMatrix filter!"; michael@0: return; michael@0: } michael@0: michael@0: mFloodEffect->SetValue(D2D1_FLOOD_PROP_COLOR, D2D1::Vector4F(0.0f, 0.0f, 0.0f, 0.0f)); michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1Composite, byRef(mCompositeEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ConvolveMatrix filter!"; michael@0: return; michael@0: } michael@0: michael@0: mCompositeEffect->SetInputEffect(1, mFloodEffect.get()); michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1Crop, byRef(mCropEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ConvolveMatrix filter!"; michael@0: return; michael@0: } michael@0: michael@0: mCropEffect->SetInputEffect(0, mCompositeEffect.get()); michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1Border, byRef(mBorderEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ConvolveMatrix filter!"; michael@0: return; michael@0: } michael@0: michael@0: mBorderEffect->SetInputEffect(0, mCropEffect.get()); michael@0: michael@0: UpdateChain(); michael@0: UpdateSourceRect(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::SetInput(uint32_t aIndex, SourceSurface *aSurface) michael@0: { michael@0: MOZ_ASSERT(aIndex == 0); michael@0: michael@0: mInput = GetImageForSourceSurface(mDT, aSurface); michael@0: michael@0: mInputEffect = nullptr; michael@0: michael@0: UpdateChain(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::SetInput(uint32_t aIndex, FilterNode *aFilter) michael@0: { michael@0: MOZ_ASSERT(aIndex == 0); michael@0: michael@0: if (aFilter->GetBackendType() != FILTER_BACKEND_DIRECT2D1_1) { michael@0: gfxWarning() << "Unknown input SourceSurface set on effect."; michael@0: MOZ_ASSERT(0); michael@0: return; michael@0: } michael@0: michael@0: mInput = nullptr; michael@0: mInputEffect = static_cast(aFilter)->mEffect; michael@0: michael@0: UpdateChain(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, uint32_t aValue) michael@0: { michael@0: if (aIndex != ATT_CONVOLVE_MATRIX_EDGE_MODE) { michael@0: return FilterNodeD2D1::SetAttribute(aIndex, aValue); michael@0: } michael@0: michael@0: mEdgeMode = (ConvolveMatrixEdgeMode)aValue; michael@0: michael@0: UpdateChain(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::UpdateChain() michael@0: { michael@0: // The shape of the filter graph: michael@0: // michael@0: // EDGE_MODE_NONE: michael@0: // input --> convolvematrix michael@0: // michael@0: // EDGE_MODE_DUPLICATE or EDGE_MODE_WRAP: michael@0: // input -------v michael@0: // flood --> composite --> crop --> border --> convolvematrix michael@0: michael@0: ID2D1Effect *firstEffect = mCompositeEffect; michael@0: if (mEdgeMode == EDGE_MODE_NONE) { michael@0: firstEffect = mEffect; michael@0: } else { michael@0: mEffect->SetInputEffect(0, mBorderEffect.get()); michael@0: } michael@0: michael@0: if (mInputEffect) { michael@0: firstEffect->SetInputEffect(0, mInputEffect); michael@0: } else { michael@0: firstEffect->SetInput(0, mInput); michael@0: } michael@0: michael@0: if (mEdgeMode == EDGE_MODE_DUPLICATE) { michael@0: mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_X, D2D1_BORDER_EDGE_MODE_CLAMP); michael@0: mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_Y, D2D1_BORDER_EDGE_MODE_CLAMP); michael@0: } else if (mEdgeMode == EDGE_MODE_WRAP) { michael@0: mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_X, D2D1_BORDER_EDGE_MODE_WRAP); michael@0: mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_Y, D2D1_BORDER_EDGE_MODE_WRAP); michael@0: } michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, const IntSize &aValue) michael@0: { michael@0: if (aIndex != ATT_CONVOLVE_MATRIX_KERNEL_SIZE) { michael@0: MOZ_ASSERT(false); michael@0: return; michael@0: } michael@0: michael@0: mKernelSize = aValue; michael@0: michael@0: mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_SIZE_X, aValue.width); michael@0: mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_SIZE_Y, aValue.height); michael@0: michael@0: UpdateOffset(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, const IntPoint &aValue) michael@0: { michael@0: if (aIndex != ATT_CONVOLVE_MATRIX_TARGET) { michael@0: MOZ_ASSERT(false); michael@0: return; michael@0: } michael@0: michael@0: mTarget = aValue; michael@0: michael@0: UpdateOffset(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, const IntRect &aValue) michael@0: { michael@0: if (aIndex != ATT_CONVOLVE_MATRIX_SOURCE_RECT) { michael@0: MOZ_ASSERT(false); michael@0: return; michael@0: } michael@0: michael@0: mSourceRect = aValue; michael@0: michael@0: UpdateSourceRect(); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::UpdateOffset() michael@0: { michael@0: D2D1_VECTOR_2F vector = michael@0: D2D1::Vector2F((Float(mKernelSize.width) - 1.0f) / 2.0f - Float(mTarget.x), michael@0: (Float(mKernelSize.height) - 1.0f) / 2.0f - Float(mTarget.y)); michael@0: michael@0: mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_OFFSET, vector); michael@0: } michael@0: michael@0: void michael@0: FilterNodeConvolveD2D1::UpdateSourceRect() michael@0: { michael@0: mCropEffect->SetValue(D2D1_CROP_PROP_RECT, michael@0: D2D1::RectF(Float(mSourceRect.x), Float(mSourceRect.y), michael@0: Float(mSourceRect.XMost()), Float(mSourceRect.YMost()))); michael@0: } michael@0: michael@0: FilterNodeComponentTransferD2D1::FilterNodeComponentTransferD2D1(DrawTarget *aDT, ID2D1DeviceContext *aDC, michael@0: ID2D1Effect *aEffect, FilterType aType) michael@0: : FilterNodeD2D1(aDT, aEffect, aType) michael@0: { michael@0: // D2D1 component transfer effects do strange things when it comes to michael@0: // premultiplication. michael@0: // For our purposes we only need the transfer filters to apply straight to michael@0: // unpremultiplied source channels and output unpremultiplied results. michael@0: // However, the D2D1 effects are designed differently: They can apply to both michael@0: // premultiplied and unpremultiplied inputs, and they always premultiply michael@0: // their result - at least in those color channels that have not been michael@0: // disabled. michael@0: // In order to determine whether the input needs to be unpremultiplied as michael@0: // part of the transfer, the effect consults the alpha mode metadata of the michael@0: // input surface or the input effect. We don't have such a concept in Moz2D, michael@0: // and giving Moz2D users different results based on something that cannot be michael@0: // influenced through Moz2D APIs seems like a bad idea. michael@0: // We solve this by applying a premultiply effect to the input before feeding michael@0: // it into the transfer effect. The premultiply effect always premultiplies michael@0: // regardless of any alpha mode metadata on inputs, and it always marks its michael@0: // output as premultiplied so that the transfer effect will unpremultiply michael@0: // consistently. Feeding always-premultiplied input into the transfer effect michael@0: // also avoids another problem that would appear when individual color michael@0: // channels disable the transfer: In that case, the disabled channels would michael@0: // pass through unchanged in their unpremultiplied form and the other michael@0: // channels would be premultiplied, giving a mixed result. michael@0: // But since we now ensure that the input is premultiplied, disabled channels michael@0: // will pass premultiplied values through to the result, which is consistent michael@0: // with the enabled channels. michael@0: // We also add an unpremultiply effect that postprocesses the result of the michael@0: // transfer effect because getting unpremultiplied results from the transfer michael@0: // filters is part of the FilterNode API. michael@0: HRESULT hr; michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1Premultiply, byRef(mPrePremultiplyEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ComponentTransfer filter!"; michael@0: return; michael@0: } michael@0: michael@0: hr = aDC->CreateEffect(CLSID_D2D1UnPremultiply, byRef(mPostUnpremultiplyEffect)); michael@0: michael@0: if (FAILED(hr)) { michael@0: gfxWarning() << "Failed to create ComponentTransfer filter!"; michael@0: return; michael@0: } michael@0: michael@0: mEffect->SetInputEffect(0, mPrePremultiplyEffect.get()); michael@0: mPostUnpremultiplyEffect->SetInputEffect(0, mEffect.get()); michael@0: } michael@0: michael@0: } michael@0: }