Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 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 #define __STDC_LIMIT_MACROS | 7 #define __STDC_LIMIT_MACROS |
| 8 | 8 |
| 9 #include "SkDraw.h" | 9 #include "SkDraw.h" |
| 10 #include "SkBlitter.h" | 10 #include "SkBlitter.h" |
| (...skipping 1872 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1883 src[1] = texs[state.f1]; | 1883 src[1] = texs[state.f1]; |
| 1884 src[2] = texs[state.f2]; | 1884 src[2] = texs[state.f2]; |
| 1885 dst[0] = verts[state.f0]; | 1885 dst[0] = verts[state.f0]; |
| 1886 dst[1] = verts[state.f1]; | 1886 dst[1] = verts[state.f1]; |
| 1887 dst[2] = verts[state.f2]; | 1887 dst[2] = verts[state.f2]; |
| 1888 return matrix->setPolyToPoly(src, dst, 3); | 1888 return matrix->setPolyToPoly(src, dst, 3); |
| 1889 } | 1889 } |
| 1890 | 1890 |
| 1891 class SkTriColorShader : public SkShader { | 1891 class SkTriColorShader : public SkShader { |
| 1892 public: | 1892 public: |
| 1893 SkTriColorShader() {} | 1893 SkTriColorShader(); |
| 1894 | 1894 |
| 1895 size_t contextSize() const override; | 1895 size_t contextSize() const override; |
| 1896 | 1896 |
| 1897 class TriColorShaderContext : public SkShader::Context { | 1897 class TriColorShaderContext : public SkShader::Context { |
| 1898 public: | 1898 public: |
| 1899 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&) ; | 1899 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&) ; |
| 1900 virtual ~TriColorShaderContext(); | 1900 virtual ~TriColorShaderContext(); |
| 1901 | |
| 1902 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int); | |
| 1903 | |
| 1904 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override; | 1901 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override; |
| 1905 | 1902 |
| 1906 private: | 1903 private: |
| 1904 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int); | |
| 1905 | |
| 1907 SkMatrix fDstToUnit; | 1906 SkMatrix fDstToUnit; |
| 1908 SkPMColor fColors[3]; | 1907 SkPMColor fColors[3]; |
| 1908 bool fSetup; | |
| 1909 | 1909 |
| 1910 typedef SkShader::Context INHERITED; | 1910 typedef SkShader::Context INHERITED; |
| 1911 }; | 1911 }; |
| 1912 | 1912 |
| 1913 struct TriColorShaderData { | |
| 1914 const SkPoint* pts; | |
| 1915 const SkColor* colors; | |
| 1916 const VertState *state; | |
| 1917 }; | |
| 1918 | |
| 1913 SK_TO_STRING_OVERRIDE() | 1919 SK_TO_STRING_OVERRIDE() |
| 1914 | 1920 |
| 1915 // For serialization. This will never be called. | 1921 // For serialization. This will never be called. |
| 1916 Factory getFactory() const override { sk_throw(); return nullptr; } | 1922 Factory getFactory() const override { sk_throw(); return nullptr; } |
| 1917 | 1923 |
| 1924 // Supply setup data to context from drawing setup | |
| 1925 void bindSetupData(TriColorShaderData* setupData) { fSetupData = setupData; } | |
| 1926 | |
| 1927 // Take the setup data from context when needed. | |
| 1928 TriColorShaderData* takeSetupData() { | |
| 1929 TriColorShaderData *data = fSetupData; | |
| 1930 fSetupData = NULL; | |
| 1931 return data; | |
| 1932 } | |
| 1933 | |
| 1918 protected: | 1934 protected: |
| 1919 Context* onCreateContext(const ContextRec& rec, void* storage) const overrid e { | 1935 Context* onCreateContext(const ContextRec& rec, void* storage) const overrid e { |
| 1920 return new (storage) TriColorShaderContext(*this, rec); | 1936 return new (storage) TriColorShaderContext(*this, rec); |
| 1921 } | 1937 } |
| 1922 | 1938 |
| 1923 private: | 1939 private: |
| 1940 TriColorShaderData *fSetupData; | |
| 1941 | |
| 1924 typedef SkShader INHERITED; | 1942 typedef SkShader INHERITED; |
| 1925 }; | 1943 }; |
| 1926 | 1944 |
| 1927 bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const S kColor colors[], | 1945 bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const S kColor colors[], |
| 1928 int index0, int index1, int index2) { | 1946 int index0, int index1, int index2) { |
| 1929 | 1947 |
| 1930 fColors[0] = SkPreMultiplyColor(colors[index0]); | 1948 fColors[0] = SkPreMultiplyColor(colors[index0]); |
| 1931 fColors[1] = SkPreMultiplyColor(colors[index1]); | 1949 fColors[1] = SkPreMultiplyColor(colors[index1]); |
| 1932 fColors[2] = SkPreMultiplyColor(colors[index2]); | 1950 fColors[2] = SkPreMultiplyColor(colors[index2]); |
| 1933 | 1951 |
| 1934 SkMatrix m, im; | 1952 SkMatrix m, im; |
| 1935 m.reset(); | 1953 m.reset(); |
| 1936 m.set(0, pts[index1].fX - pts[index0].fX); | 1954 m.set(0, pts[index1].fX - pts[index0].fX); |
| 1937 m.set(1, pts[index2].fX - pts[index0].fX); | 1955 m.set(1, pts[index2].fX - pts[index0].fX); |
| 1938 m.set(2, pts[index0].fX); | 1956 m.set(2, pts[index0].fX); |
| 1939 m.set(3, pts[index1].fY - pts[index0].fY); | 1957 m.set(3, pts[index1].fY - pts[index0].fY); |
| 1940 m.set(4, pts[index2].fY - pts[index0].fY); | 1958 m.set(4, pts[index2].fY - pts[index0].fY); |
| 1941 m.set(5, pts[index0].fY); | 1959 m.set(5, pts[index0].fY); |
| 1942 if (!m.invert(&im)) { | 1960 if (!m.invert(&im)) { |
| 1943 return false; | 1961 return false; |
| 1944 } | 1962 } |
| 1945 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix | 1963 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix |
| 1946 // as our interators are intrinsically tied to the vertices, and nothing els e. | 1964 // as our interators are intrinsically tied to the vertices, and nothing els e. |
| 1947 SkMatrix ctmInv; | 1965 SkMatrix ctmInv; |
| 1948 if (!this->getCTM().invert(&ctmInv)) { | 1966 if (!this->getCTM().invert(&ctmInv)) { |
| 1949 return false; | 1967 return false; |
| 1950 } | 1968 } |
| 1969 // TODO replace INV(m) * INV(ctm) with INV(ctm * m) | |
| 1951 fDstToUnit.setConcat(im, ctmInv); | 1970 fDstToUnit.setConcat(im, ctmInv); |
| 1952 return true; | 1971 return true; |
| 1953 } | 1972 } |
| 1954 | 1973 |
| 1955 #include "SkColorPriv.h" | 1974 #include "SkColorPriv.h" |
| 1956 #include "SkComposeShader.h" | 1975 #include "SkComposeShader.h" |
| 1957 | 1976 |
| 1958 static int ScalarTo256(SkScalar v) { | 1977 static int ScalarTo256(SkScalar v) { |
| 1959 int scale = SkScalarToFixed(v) >> 8; | 1978 int scale = SkScalarToFixed(v) >> 8; |
| 1960 if (scale < 0) { | 1979 if (scale < 0) { |
| 1961 scale = 0; | 1980 scale = 0; |
| 1962 } | 1981 } |
| 1963 if (scale > 255) { | 1982 if (scale > 255) { |
| 1964 scale = 255; | 1983 scale = 255; |
| 1965 } | 1984 } |
| 1966 return SkAlpha255To256(scale); | 1985 return SkAlpha255To256(scale); |
| 1967 } | 1986 } |
| 1968 | 1987 |
| 1988 SkTriColorShader::SkTriColorShader() | |
| 1989 : INHERITED(NULL) | |
| 1990 , fSetupData(NULL) {} | |
| 1969 | 1991 |
| 1970 SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorS hader& shader, | 1992 SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorS hader& shader, |
| 1971 const ContextRec& rec) | 1993 const ContextRec& rec) |
| 1972 : INHERITED(shader, rec) {} | 1994 : INHERITED(shader, rec) |
| 1995 , fSetup(false) {} | |
| 1973 | 1996 |
| 1974 SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {} | 1997 SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {} |
| 1975 | 1998 |
| 1976 size_t SkTriColorShader::contextSize() const { | 1999 size_t SkTriColorShader::contextSize() const { |
| 1977 return sizeof(TriColorShaderContext); | 2000 return sizeof(TriColorShaderContext); |
| 1978 } | 2001 } |
| 1979 void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { | 2002 void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 2003 SkTriColorShader* parent = static_cast<SkTriColorShader*>(const_cast<SkShade r*>(&fShader)); | |
| 2004 TriColorShaderData* set = parent->takeSetupData(); | |
| 2005 if (set) { | |
| 2006 fSetup = setup(set->pts, set->colors, set->state->f0, set->state->f1, se t->state->f2); | |
|
aleksandar_stojiljkovic
2015/11/10 08:58:51
Reed, what do you think about this approach?
Ther
| |
| 2007 } | |
| 2008 | |
| 2009 if (!fSetup) { | |
| 2010 // Invalid matrices. Not checked before so no need to assert. | |
| 2011 return; | |
| 2012 } | |
| 2013 | |
| 1980 const int alphaScale = Sk255To256(this->getPaintAlpha()); | 2014 const int alphaScale = Sk255To256(this->getPaintAlpha()); |
| 1981 | 2015 |
| 1982 SkPoint src; | 2016 SkPoint src; |
| 1983 | 2017 |
| 1984 for (int i = 0; i < count; i++) { | 2018 for (int i = 0; i < count; i++) { |
| 1985 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src); | 2019 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src); |
| 1986 x += 1; | 2020 x += 1; |
| 1987 | 2021 |
| 1988 int scale1 = ScalarTo256(src.fX); | 2022 int scale1 = ScalarTo256(src.fX); |
| 1989 int scale2 = ScalarTo256(src.fY); | 2023 int scale2 = ScalarTo256(src.fY); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2086 // Abort early if we failed to create a shader context. | 2120 // Abort early if we failed to create a shader context. |
| 2087 if (blitter->isNullBlitter()) { | 2121 if (blitter->isNullBlitter()) { |
| 2088 return; | 2122 return; |
| 2089 } | 2123 } |
| 2090 | 2124 |
| 2091 // setup our state and function pointer for iterating triangles | 2125 // setup our state and function pointer for iterating triangles |
| 2092 VertState state(count, indices, indexCount); | 2126 VertState state(count, indices, indexCount); |
| 2093 VertState::Proc vertProc = state.chooseProc(vmode); | 2127 VertState::Proc vertProc = state.chooseProc(vmode); |
| 2094 | 2128 |
| 2095 if (textures || colors) { | 2129 if (textures || colors) { |
| 2130 SkTriColorShader::TriColorShaderData verticesSetup = { vertices, colors, &state }; | |
| 2131 | |
| 2096 while (vertProc(&state)) { | 2132 while (vertProc(&state)) { |
| 2097 if (textures) { | 2133 if (textures) { |
| 2098 SkMatrix tempM; | 2134 SkMatrix tempM; |
| 2099 if (texture_to_matrix(state, vertices, textures, &tempM)) { | 2135 if (texture_to_matrix(state, vertices, textures, &tempM)) { |
| 2100 SkShader::ContextRec rec(p, *fMatrix, &tempM); | 2136 SkShader::ContextRec rec(p, *fMatrix, &tempM); |
| 2101 if (!blitter->resetShaderContext(rec)) { | 2137 if (!blitter->resetShaderContext(rec)) { |
| 2102 continue; | 2138 continue; |
| 2103 } | 2139 } |
| 2104 } | 2140 } |
| 2105 } | 2141 } |
| 2106 if (colors) { | 2142 if (colors) { |
| 2107 // Find the context for triShader. | 2143 triShader.bindSetupData(&verticesSetup); |
| 2108 SkTriColorShader::TriColorShaderContext* triColorShaderContext; | |
| 2109 | |
| 2110 SkShader::Context* shaderContext = blitter->getShaderContext(); | |
| 2111 SkASSERT(shaderContext); | |
| 2112 if (p.getShader() == &triShader) { | |
| 2113 triColorShaderContext = | |
| 2114 static_cast<SkTriColorShader::TriColorShaderContext* >(shaderContext); | |
| 2115 } else { | |
| 2116 // The shader is a compose shader and triShader is its first shader. | |
| 2117 SkASSERT(p.getShader() == composeShader); | |
| 2118 SkASSERT(composeShader->getShaderA() == &triShader); | |
| 2119 SkComposeShader::ComposeShaderContext* composeShaderContext = | |
| 2120 static_cast<SkComposeShader::ComposeShaderContext*>( shaderContext); | |
| 2121 SkShader::Context* shaderContextA = composeShaderContext->ge tShaderContextA(); | |
| 2122 triColorShaderContext = | |
| 2123 static_cast<SkTriColorShader::TriColorShaderContext* >(shaderContextA); | |
| 2124 } | |
| 2125 | |
| 2126 if (!triColorShaderContext->setup(vertices, colors, | |
| 2127 state.f0, state.f1, state.f2)) { | |
| 2128 continue; | |
| 2129 } | |
| 2130 } | 2144 } |
| 2131 | 2145 |
| 2132 SkPoint tmp[] = { | 2146 SkPoint tmp[] = { |
| 2133 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2] | 2147 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2] |
| 2134 }; | 2148 }; |
| 2135 SkScan::FillTriangle(tmp, *fRC, blitter.get()); | 2149 SkScan::FillTriangle(tmp, *fRC, blitter.get()); |
| 2150 triShader.bindSetupData(NULL); | |
| 2136 } | 2151 } |
| 2137 } else { | 2152 } else { |
| 2138 // no colors[] and no texture, stroke hairlines with paint's color. | 2153 // no colors[] and no texture, stroke hairlines with paint's color. |
| 2139 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias()); | 2154 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias()); |
| 2140 const SkRasterClip& clip = *fRC; | 2155 const SkRasterClip& clip = *fRC; |
| 2141 while (vertProc(&state)) { | 2156 while (vertProc(&state)) { |
| 2142 SkPoint array[] = { | 2157 SkPoint array[] = { |
| 2143 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devV erts[state.f0] | 2158 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devV erts[state.f0] |
| 2144 }; | 2159 }; |
| 2145 hairProc(array, 4, clip, blitter.get()); | 2160 hairProc(array, 4, clip, blitter.get()); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2258 mask->fImage = SkMask::AllocImage(size); | 2273 mask->fImage = SkMask::AllocImage(size); |
| 2259 memset(mask->fImage, 0, mask->computeImageSize()); | 2274 memset(mask->fImage, 0, mask->computeImageSize()); |
| 2260 } | 2275 } |
| 2261 | 2276 |
| 2262 if (SkMask::kJustComputeBounds_CreateMode != mode) { | 2277 if (SkMask::kJustComputeBounds_CreateMode != mode) { |
| 2263 draw_into_mask(*mask, devPath, style); | 2278 draw_into_mask(*mask, devPath, style); |
| 2264 } | 2279 } |
| 2265 | 2280 |
| 2266 return true; | 2281 return true; |
| 2267 } | 2282 } |
| OLD | NEW |