| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "GrContext.h" | 9 #include "GrContext.h" |
| 10 | 10 |
| 11 #include "GrAARectRenderer.h" | 11 #include "GrAARectRenderer.h" |
| 12 #include "GrAtlasTextContext.h" | 12 #include "GrAtlasTextContext.h" |
| 13 #include "GrBatch.h" | 13 #include "GrBatch.h" |
| 14 #include "GrBatchFontCache.h" | 14 #include "GrBatchFontCache.h" |
| 15 #include "GrBatchTarget.h" | 15 #include "GrBatchTarget.h" |
| 16 #include "GrBufferAllocPool.h" | |
| 17 #include "GrDefaultGeoProcFactory.h" | 16 #include "GrDefaultGeoProcFactory.h" |
| 18 #include "GrGpuResource.h" | 17 #include "GrGpuResource.h" |
| 19 #include "GrGpuResourcePriv.h" | 18 #include "GrGpuResourcePriv.h" |
| 20 #include "GrDrawTargetCaps.h" | 19 #include "GrDrawTargetCaps.h" |
| 21 #include "GrGpu.h" | 20 #include "GrGpu.h" |
| 22 #include "GrIndexBuffer.h" | 21 #include "GrIndexBuffer.h" |
| 23 #include "GrInOrderDrawBuffer.h" | 22 #include "GrInOrderDrawBuffer.h" |
| 24 #include "GrLayerCache.h" | 23 #include "GrLayerCache.h" |
| 25 #include "GrOvalRenderer.h" | 24 #include "GrOvalRenderer.h" |
| 26 #include "GrPathRenderer.h" | 25 #include "GrPathRenderer.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 43 #include "SkRRect.h" | 42 #include "SkRRect.h" |
| 44 #include "SkStrokeRec.h" | 43 #include "SkStrokeRec.h" |
| 45 #include "SkTLazy.h" | 44 #include "SkTLazy.h" |
| 46 #include "SkTLS.h" | 45 #include "SkTLS.h" |
| 47 #include "SkTraceEvent.h" | 46 #include "SkTraceEvent.h" |
| 48 | 47 |
| 49 #include "effects/GrConfigConversionEffect.h" | 48 #include "effects/GrConfigConversionEffect.h" |
| 50 #include "effects/GrDashingEffect.h" | 49 #include "effects/GrDashingEffect.h" |
| 51 #include "effects/GrSingleTextureEffect.h" | 50 #include "effects/GrSingleTextureEffect.h" |
| 52 | 51 |
| 53 static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; | |
| 54 static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; | |
| 55 | |
| 56 static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11; | |
| 57 static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4; | |
| 58 | |
| 59 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) | 52 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
| 60 #define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; } | 53 #define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; } |
| 61 #define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; } | 54 #define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; } |
| 62 #define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; } | 55 #define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; } |
| 63 | 56 |
| 64 class GrContext::AutoCheckFlush { | 57 class GrContext::AutoCheckFlush { |
| 65 public: | 58 public: |
| 66 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context);
} | 59 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context);
} |
| 67 | 60 |
| 68 ~AutoCheckFlush() { | 61 ~AutoCheckFlush() { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 95 } |
| 103 | 96 |
| 104 GrContext::GrContext(const Options& opts) : fOptions(opts), fUniqueID(next_id())
{ | 97 GrContext::GrContext(const Options& opts) : fOptions(opts), fUniqueID(next_id())
{ |
| 105 fGpu = NULL; | 98 fGpu = NULL; |
| 106 fResourceCache = NULL; | 99 fResourceCache = NULL; |
| 107 fResourceProvider = NULL; | 100 fResourceProvider = NULL; |
| 108 fPathRendererChain = NULL; | 101 fPathRendererChain = NULL; |
| 109 fSoftwarePathRenderer = NULL; | 102 fSoftwarePathRenderer = NULL; |
| 110 fBatchFontCache = NULL; | 103 fBatchFontCache = NULL; |
| 111 fDrawBuffer = NULL; | 104 fDrawBuffer = NULL; |
| 112 fDrawBufferVBAllocPool = NULL; | |
| 113 fDrawBufferIBAllocPool = NULL; | |
| 114 fFlushToReduceCacheSize = false; | 105 fFlushToReduceCacheSize = false; |
| 115 fAARectRenderer = NULL; | 106 fAARectRenderer = NULL; |
| 116 fOvalRenderer = NULL; | 107 fOvalRenderer = NULL; |
| 117 fMaxTextureSizeOverride = 1 << 20; | 108 fMaxTextureSizeOverride = 1 << 20; |
| 118 } | 109 } |
| 119 | 110 |
| 120 bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { | 111 bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { |
| 121 SkASSERT(NULL == fGpu); | 112 SkASSERT(NULL == fGpu); |
| 122 | 113 |
| 123 fGpu = GrGpu::Create(backend, backendContext, this); | 114 fGpu = GrGpu::Create(backend, backendContext, this); |
| 124 if (NULL == fGpu) { | 115 if (NULL == fGpu) { |
| 125 return false; | 116 return false; |
| 126 } | 117 } |
| 127 this->initCommon(); | 118 this->initCommon(); |
| 128 return true; | 119 return true; |
| 129 } | 120 } |
| 130 | 121 |
| 131 void GrContext::initCommon() { | 122 void GrContext::initCommon() { |
| 132 fResourceCache = SkNEW(GrResourceCache); | 123 fResourceCache = SkNEW(GrResourceCache); |
| 133 fResourceCache->setOverBudgetCallback(OverBudgetCB, this); | 124 fResourceCache->setOverBudgetCallback(OverBudgetCB, this); |
| 134 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache)); | 125 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache)); |
| 135 | 126 |
| 136 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); | 127 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); |
| 137 | 128 |
| 138 fAARectRenderer = SkNEW(GrAARectRenderer); | 129 fAARectRenderer = SkNEW(GrAARectRenderer); |
| 139 fOvalRenderer = SkNEW(GrOvalRenderer); | 130 fOvalRenderer = SkNEW(GrOvalRenderer); |
| 140 | 131 |
| 141 fDidTestPMConversions = false; | 132 fDidTestPMConversions = false; |
| 142 | 133 |
| 143 this->setupDrawBuffer(); | 134 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this)); |
| 144 | 135 |
| 145 // GrBatchFontCache will eventually replace GrFontCache | 136 // GrBatchFontCache will eventually replace GrFontCache |
| 146 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this)); | 137 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this)); |
| 147 | 138 |
| 148 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB,
this))); | 139 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB,
this))); |
| 149 } | 140 } |
| 150 | 141 |
| 151 GrContext::~GrContext() { | 142 GrContext::~GrContext() { |
| 152 if (NULL == fGpu) { | 143 if (NULL == fGpu) { |
| 153 return; | 144 return; |
| 154 } | 145 } |
| 155 | 146 |
| 156 this->flush(); | 147 this->flush(); |
| 157 | 148 |
| 158 for (int i = 0; i < fCleanUpData.count(); ++i) { | 149 for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 159 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); | 150 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 160 } | 151 } |
| 161 | 152 |
| 162 SkDELETE(fResourceProvider); | 153 SkDELETE(fResourceProvider); |
| 163 SkDELETE(fResourceCache); | 154 SkDELETE(fResourceCache); |
| 164 SkDELETE(fBatchFontCache); | 155 SkDELETE(fBatchFontCache); |
| 165 SkDELETE(fDrawBuffer); | 156 SkDELETE(fDrawBuffer); |
| 166 SkDELETE(fDrawBufferVBAllocPool); | |
| 167 SkDELETE(fDrawBufferIBAllocPool); | |
| 168 | 157 |
| 169 fAARectRenderer->unref(); | 158 fAARectRenderer->unref(); |
| 170 fOvalRenderer->unref(); | 159 fOvalRenderer->unref(); |
| 171 | 160 |
| 172 fGpu->unref(); | 161 fGpu->unref(); |
| 173 SkSafeUnref(fPathRendererChain); | 162 SkSafeUnref(fPathRendererChain); |
| 174 SkSafeUnref(fSoftwarePathRenderer); | 163 SkSafeUnref(fSoftwarePathRenderer); |
| 175 } | 164 } |
| 176 | 165 |
| 177 void GrContext::abandonContext() { | 166 void GrContext::abandonContext() { |
| 178 fResourceProvider->abandon(); | 167 fResourceProvider->abandon(); |
| 179 // abandon first to so destructors | 168 // abandon first to so destructors |
| 180 // don't try to free the resources in the API. | 169 // don't try to free the resources in the API. |
| 181 fResourceCache->abandonAll(); | 170 fResourceCache->abandonAll(); |
| 182 | 171 |
| 183 fGpu->contextAbandoned(); | 172 fGpu->contextAbandoned(); |
| 184 | 173 |
| 185 // a path renderer may be holding onto resources that | 174 // a path renderer may be holding onto resources that |
| 186 // are now unusable | 175 // are now unusable |
| 187 SkSafeSetNull(fPathRendererChain); | 176 SkSafeSetNull(fPathRendererChain); |
| 188 SkSafeSetNull(fSoftwarePathRenderer); | 177 SkSafeSetNull(fSoftwarePathRenderer); |
| 189 | 178 |
| 190 delete fDrawBuffer; | 179 SkDELETE(fDrawBuffer); |
| 191 fDrawBuffer = NULL; | 180 fDrawBuffer = NULL; |
| 192 | 181 |
| 193 delete fDrawBufferVBAllocPool; | |
| 194 fDrawBufferVBAllocPool = NULL; | |
| 195 | |
| 196 delete fDrawBufferIBAllocPool; | |
| 197 fDrawBufferIBAllocPool = NULL; | |
| 198 | |
| 199 fBatchFontCache->freeAll(); | 182 fBatchFontCache->freeAll(); |
| 200 fLayerCache->freeAll(); | 183 fLayerCache->freeAll(); |
| 201 fTextBlobCache->freeAll(); | 184 fTextBlobCache->freeAll(); |
| 202 } | 185 } |
| 203 | 186 |
| 204 void GrContext::resetContext(uint32_t state) { | 187 void GrContext::resetContext(uint32_t state) { |
| 205 fGpu->markContextDirty(state); | 188 fGpu->markContextDirty(state); |
| 206 } | 189 } |
| 207 | 190 |
| 208 void GrContext::freeGpuResources() { | 191 void GrContext::freeGpuResources() { |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 Geometry& args = fGeoData[0]; | 459 Geometry& args = fGeoData[0]; |
| 477 | 460 |
| 478 int vertexCount = kVertsPerHairlineRect; | 461 int vertexCount = kVertsPerHairlineRect; |
| 479 if (args.fStrokeWidth > 0) { | 462 if (args.fStrokeWidth > 0) { |
| 480 vertexCount = kVertsPerStrokeRect; | 463 vertexCount = kVertsPerStrokeRect; |
| 481 } | 464 } |
| 482 | 465 |
| 483 const GrVertexBuffer* vertexBuffer; | 466 const GrVertexBuffer* vertexBuffer; |
| 484 int firstVertex; | 467 int firstVertex; |
| 485 | 468 |
| 486 void* verts = batchTarget->vertexPool()->makeSpace(vertexStride, | 469 void* verts = batchTarget->makeVertSpace(vertexStride, vertexCount, |
| 487 vertexCount, | 470 &vertexBuffer, &firstVertex); |
| 488 &vertexBuffer, | |
| 489 &firstVertex); | |
| 490 | 471 |
| 491 if (!verts) { | 472 if (!verts) { |
| 492 SkDebugf("Could not allocate vertices\n"); | 473 SkDebugf("Could not allocate vertices\n"); |
| 493 return; | 474 return; |
| 494 } | 475 } |
| 495 | 476 |
| 496 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); | 477 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 497 | 478 |
| 498 GrPrimitiveType primType; | 479 GrPrimitiveType primType; |
| 499 | 480 |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 size_t vertexStride = gp->getVertexStride(); | 795 size_t vertexStride = gp->getVertexStride(); |
| 815 | 796 |
| 816 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? siz
eof(SkPoint) : 0) | 797 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? siz
eof(SkPoint) : 0) |
| 817 + (this->hasColors() ? sizeof(G
rColor) : 0)); | 798 + (this->hasColors() ? sizeof(G
rColor) : 0)); |
| 818 | 799 |
| 819 int instanceCount = fGeoData.count(); | 800 int instanceCount = fGeoData.count(); |
| 820 | 801 |
| 821 const GrVertexBuffer* vertexBuffer; | 802 const GrVertexBuffer* vertexBuffer; |
| 822 int firstVertex; | 803 int firstVertex; |
| 823 | 804 |
| 824 void* verts = batchTarget->vertexPool()->makeSpace(vertexStride, | 805 void* verts = batchTarget->makeVertSpace(vertexStride, this->vertexCount
(), |
| 825 this->vertexCount(), | 806 &vertexBuffer, &firstVertex); |
| 826 &vertexBuffer, | |
| 827 &firstVertex); | |
| 828 | 807 |
| 829 if (!verts) { | 808 if (!verts) { |
| 830 SkDebugf("Could not allocate vertices\n"); | 809 SkDebugf("Could not allocate vertices\n"); |
| 831 return; | 810 return; |
| 832 } | 811 } |
| 833 | 812 |
| 834 const GrIndexBuffer* indexBuffer = NULL; | 813 const GrIndexBuffer* indexBuffer = NULL; |
| 835 int firstIndex = 0; | 814 int firstIndex = 0; |
| 836 | 815 |
| 837 void* indices = NULL; | 816 uint16_t* indices = NULL; |
| 838 if (this->hasIndices()) { | 817 if (this->hasIndices()) { |
| 839 indices = batchTarget->indexPool()->makeSpace(this->indexCount(), | 818 indices = batchTarget->makeIndexSpace(this->indexCount(), &indexBuff
er, &firstIndex); |
| 840 &indexBuffer, | |
| 841 &firstIndex); | |
| 842 | 819 |
| 843 if (!indices) { | 820 if (!indices) { |
| 844 SkDebugf("Could not allocate indices\n"); | 821 SkDebugf("Could not allocate indices\n"); |
| 845 return; | 822 return; |
| 846 } | 823 } |
| 847 } | 824 } |
| 848 | 825 |
| 849 int indexOffset = 0; | 826 int indexOffset = 0; |
| 850 int vertexOffset = 0; | 827 int vertexOffset = 0; |
| 851 for (int i = 0; i < instanceCount; i++) { | 828 for (int i = 0; i < instanceCount; i++) { |
| 852 const Geometry& args = fGeoData[i]; | 829 const Geometry& args = fGeoData[i]; |
| 853 | 830 |
| 854 // TODO we can actually cache this interleaved and then just memcopy | 831 // TODO we can actually cache this interleaved and then just memcopy |
| 855 if (this->hasIndices()) { | 832 if (this->hasIndices()) { |
| 856 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) { | 833 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) { |
| 857 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + ver
texOffset; | 834 *(indices + indexOffset) = args.fIndices[j] + vertexOffset; |
| 858 } | 835 } |
| 859 } | 836 } |
| 860 | 837 |
| 861 for (int j = 0; j < args.fPositions.count(); ++j) { | 838 for (int j = 0; j < args.fPositions.count(); ++j) { |
| 862 *((SkPoint*)verts) = args.fPositions[j]; | 839 *((SkPoint*)verts) = args.fPositions[j]; |
| 863 if (this->hasColors()) { | 840 if (this->hasColors()) { |
| 864 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j]
; | 841 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j]
; |
| 865 } | 842 } |
| 866 if (this->hasLocalCoords()) { | 843 if (this->hasLocalCoords()) { |
| 867 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords
[j]; | 844 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords
[j]; |
| (...skipping 938 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1806 if (dpi >= 250.0f) { | 1783 if (dpi >= 250.0f) { |
| 1807 chosenSampleCount = 4; | 1784 chosenSampleCount = 4; |
| 1808 } else { | 1785 } else { |
| 1809 chosenSampleCount = 16; | 1786 chosenSampleCount = 16; |
| 1810 } | 1787 } |
| 1811 } | 1788 } |
| 1812 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? | 1789 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? |
| 1813 chosenSampleCount : 0; | 1790 chosenSampleCount : 0; |
| 1814 } | 1791 } |
| 1815 | 1792 |
| 1816 void GrContext::setupDrawBuffer() { | |
| 1817 SkASSERT(NULL == fDrawBuffer); | |
| 1818 SkASSERT(NULL == fDrawBufferVBAllocPool); | |
| 1819 SkASSERT(NULL == fDrawBufferIBAllocPool); | |
| 1820 | |
| 1821 fDrawBufferVBAllocPool = | |
| 1822 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, | |
| 1823 DRAW_BUFFER_VBPOOL_BUFFER_SIZE, | |
| 1824 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS
)); | |
| 1825 fDrawBufferIBAllocPool = | |
| 1826 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, | |
| 1827 DRAW_BUFFER_IBPOOL_BUFFER_SIZE, | |
| 1828 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS)
); | |
| 1829 | |
| 1830 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this, | |
| 1831 fDrawBufferVBAllocPool, | |
| 1832 fDrawBufferIBAllocPool)); | |
| 1833 } | |
| 1834 | |
| 1835 GrDrawTarget* GrContext::getTextTarget() { | 1793 GrDrawTarget* GrContext::getTextTarget() { |
| 1836 return this->prepareToDraw(); | 1794 return this->prepareToDraw(); |
| 1837 } | 1795 } |
| 1838 | 1796 |
| 1839 namespace { | 1797 namespace { |
| 1840 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { | 1798 void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
| 1841 GrConfigConversionEffect::PMConversion pmToUPM; | 1799 GrConfigConversionEffect::PMConversion pmToUPM; |
| 1842 GrConfigConversionEffect::PMConversion upmToPM; | 1800 GrConfigConversionEffect::PMConversion upmToPM; |
| 1843 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); | 1801 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upm
ToPM); |
| 1844 *pmToUPMValue = pmToUPM; | 1802 *pmToUPMValue = pmToUPM; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1901 fDrawBuffer->addGpuTraceMarker(marker); | 1859 fDrawBuffer->addGpuTraceMarker(marker); |
| 1902 } | 1860 } |
| 1903 } | 1861 } |
| 1904 | 1862 |
| 1905 void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { | 1863 void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { |
| 1906 fGpu->removeGpuTraceMarker(marker); | 1864 fGpu->removeGpuTraceMarker(marker); |
| 1907 if (fDrawBuffer) { | 1865 if (fDrawBuffer) { |
| 1908 fDrawBuffer->removeGpuTraceMarker(marker); | 1866 fDrawBuffer->removeGpuTraceMarker(marker); |
| 1909 } | 1867 } |
| 1910 } | 1868 } |
| OLD | NEW |