OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
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 "SkGpuDevice.h" | 8 #include "SkGpuDevice.h" |
9 | 9 |
10 #include "effects/GrBicubicEffect.h" | 10 #include "effects/GrBicubicEffect.h" |
11 #include "effects/GrDashingEffect.h" | 11 #include "effects/GrDashingEffect.h" |
12 #include "effects/GrTextureDomain.h" | 12 #include "effects/GrTextureDomain.h" |
13 #include "effects/GrSimpleTextureEffect.h" | 13 #include "effects/GrSimpleTextureEffect.h" |
14 | 14 |
15 #include "GrContext.h" | 15 #include "GrContext.h" |
16 #include "GrBitmapTextContext.h" | 16 #include "GrBitmapTextContext.h" |
17 #include "GrDistanceFieldTextContext.h" | 17 #include "GrDistanceFieldTextContext.h" |
18 #include "GrLayerCache.h" | 18 #include "GrLayerCache.h" |
| 19 #include "GrLayerHoister.h" |
19 #include "GrPictureUtils.h" | 20 #include "GrPictureUtils.h" |
20 #include "GrStrokeInfo.h" | 21 #include "GrStrokeInfo.h" |
21 #include "GrTracing.h" | 22 #include "GrTracing.h" |
22 | 23 |
23 #include "SkGrTexturePixelRef.h" | 24 #include "SkGrTexturePixelRef.h" |
24 | 25 |
25 #include "SkDeviceImageFilterProxy.h" | 26 #include "SkDeviceImageFilterProxy.h" |
26 #include "SkDrawProcs.h" | 27 #include "SkDrawProcs.h" |
27 #include "SkGlyphCache.h" | 28 #include "SkGlyphCache.h" |
28 #include "SkImageFilter.h" | 29 #include "SkImageFilter.h" |
(...skipping 1803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1832 | 1833 |
1833 fContext->getLayerCache()->trackPicture(picture); | 1834 fContext->getLayerCache()->trackPicture(picture); |
1834 } | 1835 } |
1835 | 1836 |
1836 static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* re
sult) { | 1837 static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* re
sult) { |
1837 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 1838 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
1838 result->setInfo(info); | 1839 result->setInfo(info); |
1839 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); | 1840 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
1840 } | 1841 } |
1841 | 1842 |
1842 // Return true if any layers are suitable for hoisting | |
1843 bool SkGpuDevice::FindLayersToHoist(const GrAccelData *gpuData, | |
1844 const SkPicture::OperationList* ops, | |
1845 const SkRect& query, | |
1846 bool* pullForward) { | |
1847 bool anyHoisted = false; | |
1848 | |
1849 // Layer hoisting pre-renders the entire layer since it will be cached and p
otentially | |
1850 // reused with different clips (e.g., in different tiles). Because of this t
he | |
1851 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerM
axSize | |
1852 // is used to limit which clips are pre-rendered. | |
1853 static const int kSaveLayerMaxSize = 256; | |
1854 | |
1855 if (NULL != ops) { | |
1856 // In this case the picture has been generated with a BBH so we use | |
1857 // the BBH to limit the pre-rendering to just the layers needed to cover | |
1858 // the region being drawn | |
1859 for (int i = 0; i < ops->numOps(); ++i) { | |
1860 uint32_t offset = ops->offset(i); | |
1861 | |
1862 // For now we're saving all the layers in the GrAccelData so they | |
1863 // can be nested. Additionally, the nested layers appear before | |
1864 // their parent in the list. | |
1865 for (int j = 0; j < gpuData->numSaveLayers(); ++j) { | |
1866 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(
j); | |
1867 | |
1868 if (pullForward[j]) { | |
1869 continue; // already pulling forward | |
1870 } | |
1871 | |
1872 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID)
{ | |
1873 continue; // the op isn't in this range | |
1874 } | |
1875 | |
1876 // TODO: once this code is more stable unsuitable layers can | |
1877 // just be omitted during the optimization stage | |
1878 if (!info.fValid || | |
1879 kSaveLayerMaxSize < info.fSize.fWidth || | |
1880 kSaveLayerMaxSize < info.fSize.fHeight || | |
1881 info.fIsNested) { | |
1882 continue; // this layer is unsuitable | |
1883 } | |
1884 | |
1885 pullForward[j] = true; | |
1886 anyHoisted = true; | |
1887 } | |
1888 } | |
1889 } else { | |
1890 // In this case there is no BBH associated with the picture. Pre-render | |
1891 // all the layers that intersect the drawn region | |
1892 for (int j = 0; j < gpuData->numSaveLayers(); ++j) { | |
1893 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j); | |
1894 | |
1895 SkRect layerRect = SkRect::MakeXYWH(SkIntToScalar(info.fOffset.fX), | |
1896 SkIntToScalar(info.fOffset.fY), | |
1897 SkIntToScalar(info.fSize.fWidth)
, | |
1898 SkIntToScalar(info.fSize.fHeight
)); | |
1899 | |
1900 if (!SkRect::Intersects(query, layerRect)) { | |
1901 continue; | |
1902 } | |
1903 | |
1904 // TODO: once this code is more stable unsuitable layers can | |
1905 // just be omitted during the optimization stage | |
1906 if (!info.fValid || | |
1907 kSaveLayerMaxSize < info.fSize.fWidth || | |
1908 kSaveLayerMaxSize < info.fSize.fHeight || | |
1909 info.fIsNested) { | |
1910 continue; | |
1911 } | |
1912 | |
1913 pullForward[j] = true; | |
1914 anyHoisted = true; | |
1915 } | |
1916 } | |
1917 | |
1918 return anyHoisted; | |
1919 } | |
1920 | |
1921 bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture
* picture, | 1843 bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture
* picture, |
1922 const SkMatrix* matrix, const SkPaint
* paint) { | 1844 const SkMatrix* matrix, const SkPaint
* paint) { |
1923 // todo: should handle these natively | 1845 // todo: should handle these natively |
1924 if (matrix || paint) { | 1846 if (matrix || paint) { |
1925 return false; | 1847 return false; |
1926 } | 1848 } |
1927 | 1849 |
1928 fContext->getLayerCache()->processDeletedPictures(); | 1850 fContext->getLayerCache()->processDeletedPictures(); |
1929 | 1851 |
1930 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); | 1852 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
1931 | 1853 |
1932 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); | 1854 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); |
1933 if (NULL == data) { | 1855 if (NULL == data) { |
1934 return false; | 1856 return false; |
1935 } | 1857 } |
1936 | 1858 |
1937 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); | 1859 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); |
1938 | 1860 |
1939 if (0 == gpuData->numSaveLayers()) { | 1861 if (0 == gpuData->numSaveLayers()) { |
1940 return false; | 1862 return false; |
1941 } | 1863 } |
1942 | 1864 |
1943 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers()); | 1865 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers()); |
1944 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | |
1945 pullForward[i] = false; | |
1946 } | |
1947 | 1866 |
1948 SkRect clipBounds; | 1867 SkRect clipBounds; |
1949 if (!mainCanvas->getClipBounds(&clipBounds)) { | 1868 if (!mainCanvas->getClipBounds(&clipBounds)) { |
1950 return true; | 1869 return true; |
1951 } | 1870 } |
1952 | 1871 |
1953 SkAutoTDelete<const SkPicture::OperationList> ops( | 1872 SkAutoTDelete<const SkPicture::OperationList> ops( |
1954 picture->EXPERIMENTAL_getActiveOps(clipBounds)); | 1873 picture->EXPERIMENTAL_getActiveOps(clipBounds)); |
1955 | 1874 |
1956 if (!FindLayersToHoist(gpuData, ops.get(), clipBounds, pullForward.get())) { | 1875 if (!GrLayerHoister::FindLayersToHoist(gpuData, ops.get(), clipBounds, pullF
orward.get())) { |
1957 return false; | 1876 return false; |
1958 } | 1877 } |
1959 | 1878 |
1960 SkPictureReplacementPlayback::PlaybackReplacements replacements; | 1879 SkPictureReplacementPlayback::PlaybackReplacements replacements; |
1961 | 1880 |
1962 SkTDArray<GrCachedLayer*> atlased, nonAtlased; | 1881 SkTDArray<GrCachedLayer*> atlased, nonAtlased; |
1963 atlased.setReserve(gpuData->numSaveLayers()); | 1882 atlased.setReserve(gpuData->numSaveLayers()); |
1964 | 1883 |
1965 // Generate the layer and/or ensure it is locked | 1884 // Generate the layer and/or ensure it is locked |
1966 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | 1885 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2008 if (needsRendering) { | 1927 if (needsRendering) { |
2009 if (layer->isAtlased()) { | 1928 if (layer->isAtlased()) { |
2010 *atlased.append() = layer; | 1929 *atlased.append() = layer; |
2011 } else { | 1930 } else { |
2012 *nonAtlased.append() = layer; | 1931 *nonAtlased.append() = layer; |
2013 } | 1932 } |
2014 } | 1933 } |
2015 } | 1934 } |
2016 } | 1935 } |
2017 | 1936 |
2018 this->drawLayers(picture, atlased, nonAtlased); | 1937 GrLayerHoister::DrawLayers(picture, atlased, nonAtlased); |
2019 | 1938 |
2020 // Render the entire picture using new layers | 1939 // Render the entire picture using new layers |
2021 SkPictureReplacementPlayback playback(picture, &replacements, ops.get()); | 1940 SkPictureReplacementPlayback playback(picture, &replacements, ops.get()); |
2022 | 1941 |
2023 playback.draw(mainCanvas, NULL); | 1942 playback.draw(mainCanvas, NULL); |
2024 | 1943 |
2025 this->unlockLayers(picture); | 1944 GrLayerHoister::UnlockLayers(fContext->getLayerCache(), picture); |
2026 | 1945 |
2027 return true; | 1946 return true; |
2028 } | 1947 } |
2029 | 1948 |
2030 void SkGpuDevice::drawLayers(const SkPicture* picture, | |
2031 const SkTDArray<GrCachedLayer*>& atlased, | |
2032 const SkTDArray<GrCachedLayer*>& nonAtlased) { | |
2033 // Render the atlased layers that require it | |
2034 if (atlased.count() > 0) { | |
2035 // All the atlased layers are rendered into the same GrTexture | |
2036 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( | |
2037 atlased[0]->texture()->asRenderTarge
t(), | |
2038 SkSurface::kStandard_TextRenderMode, | |
2039 SkSurface::kDontClear_RenderTargetFl
ag)); | |
2040 | |
2041 SkCanvas* atlasCanvas = surface->getCanvas(); | |
2042 | |
2043 SkPaint paint; | |
2044 paint.setColor(SK_ColorTRANSPARENT); | |
2045 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); | |
2046 | |
2047 for (int i = 0; i < atlased.count(); ++i) { | |
2048 GrCachedLayer* layer = atlased[i]; | |
2049 | |
2050 atlasCanvas->save(); | |
2051 | |
2052 // Add a rect clip to make sure the rendering doesn't | |
2053 // extend beyond the boundaries of the atlased sub-rect | |
2054 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), | |
2055 SkIntToScalar(layer->rect().fTop), | |
2056 SkIntToScalar(layer->rect().width())
, | |
2057 SkIntToScalar(layer->rect().height()
)); | |
2058 atlasCanvas->clipRect(bound); | |
2059 | |
2060 // Since 'clear' doesn't respect the clip we need to draw a rect | |
2061 // TODO: ensure none of the atlased layers contain a clear call! | |
2062 atlasCanvas->drawRect(bound, paint); | |
2063 | |
2064 // info.fCTM maps the layer's top/left to the origin. | |
2065 // Since this layer is atlased, the top/left corner needs | |
2066 // to be offset to the correct location in the backing texture. | |
2067 atlasCanvas->translate(bound.fLeft, bound.fTop); | |
2068 atlasCanvas->concat(layer->ctm()); | |
2069 | |
2070 SkPictureRangePlayback rangePlayback(picture, | |
2071 layer->start(), | |
2072 layer->stop()); | |
2073 rangePlayback.draw(atlasCanvas, NULL); | |
2074 | |
2075 atlasCanvas->restore(); | |
2076 } | |
2077 | |
2078 atlasCanvas->flush(); | |
2079 } | |
2080 | |
2081 // Render the non-atlased layers that require it | |
2082 for (int i = 0; i < nonAtlased.count(); ++i) { | |
2083 GrCachedLayer* layer = nonAtlased[i]; | |
2084 | |
2085 // Each non-atlased layer has its own GrTexture | |
2086 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( | |
2087 layer->texture()->asRenderTarget(), | |
2088 SkSurface::kStandard_TextRenderMode, | |
2089 SkSurface::kDontClear_RenderTargetFl
ag)); | |
2090 | |
2091 SkCanvas* layerCanvas = surface->getCanvas(); | |
2092 | |
2093 // Add a rect clip to make sure the rendering doesn't | |
2094 // extend beyond the boundaries of the atlased sub-rect | |
2095 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), | |
2096 SkIntToScalar(layer->rect().fTop), | |
2097 SkIntToScalar(layer->rect().width()), | |
2098 SkIntToScalar(layer->rect().height())); | |
2099 | |
2100 layerCanvas->clipRect(bound); // TODO: still useful? | |
2101 | |
2102 layerCanvas->clear(SK_ColorTRANSPARENT); | |
2103 | |
2104 layerCanvas->concat(layer->ctm()); | |
2105 | |
2106 SkPictureRangePlayback rangePlayback(picture, | |
2107 layer->start(), | |
2108 layer->stop()); | |
2109 rangePlayback.draw(layerCanvas, NULL); | |
2110 | |
2111 layerCanvas->flush(); | |
2112 } | |
2113 } | |
2114 | |
2115 void SkGpuDevice::unlockLayers(const SkPicture* picture) { | |
2116 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); | |
2117 | |
2118 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); | |
2119 SkASSERT(NULL != data); | |
2120 | |
2121 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); | |
2122 SkASSERT(0 != gpuData->numSaveLayers()); | |
2123 | |
2124 // unlock the layers | |
2125 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | |
2126 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); | |
2127 | |
2128 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture->uni
queID(), | |
2129 info.fSaveLa
yerOpID, | |
2130 info.fRestor
eOpID, | |
2131 info.fOrigin
Xform); | |
2132 fContext->getLayerCache()->unlock(layer); | |
2133 } | |
2134 | |
2135 #if DISABLE_CACHING | |
2136 // This code completely clears out the atlas. It is required when | |
2137 // caching is disabled so the atlas doesn't fill up and force more | |
2138 // free floating layers | |
2139 fContext->getLayerCache()->purge(picture->uniqueID()); | |
2140 | |
2141 fContext->getLayerCache()->purgeAll(); | |
2142 #endif | |
2143 } | |
2144 | |
2145 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { | 1949 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() { |
2146 // We always return a transient cache, so it is freed after each | 1950 // We always return a transient cache, so it is freed after each |
2147 // filter traversal. | 1951 // filter traversal. |
2148 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); | 1952 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize); |
2149 } | 1953 } |
OLD | NEW |