OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 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 "GrTextureProvider.h" | 9 #include "GrTextureProvider.h" |
10 #include "GrTexturePriv.h" | 10 #include "GrTexturePriv.h" |
11 #include "GrResourceCache.h" | 11 #include "GrResourceCache.h" |
12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
13 | 13 |
14 enum ScratchTextureFlags { | |
15 kExact_ScratchTextureFlag = 0x1, | |
16 kNoPendingIO_ScratchTextureFlag = 0x2, | |
17 kNoCreate_ScratchTextureFlag = 0x4, | |
18 }; | |
19 | |
20 GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budg
eted, | 14 GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budg
eted, |
21 const void* srcData, size_t rowBytes
) { | 15 const void* srcData, size_t rowBytes
) { |
22 if (this->isAbandoned()) { | 16 if (this->isAbandoned()) { |
23 return NULL; | 17 return NULL; |
24 } | 18 } |
25 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) && | 19 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) && |
26 !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { | 20 !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
27 return NULL; | 21 return NULL; |
28 } | 22 } |
29 if (!GrPixelConfigIsCompressed(desc.fConfig)) { | 23 if (!GrPixelConfigIsCompressed(desc.fConfig)) { |
30 static const uint32_t kFlags = kExact_ScratchTextureFlag | | 24 static const uint32_t kFlags = kExact_ScratchFlag | kNoCreate_ScratchFla
g; |
31 kNoCreate_ScratchTextureFlag; | |
32 if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags))
{ | 25 if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags))
{ |
33 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight
, desc.fConfig, | 26 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight
, desc.fConfig, |
34 srcData, rowBytes)) { | 27 srcData, rowBytes)) { |
35 if (!budgeted) { | 28 if (!budgeted) { |
36 texture->resourcePriv().makeUnbudgeted(); | 29 texture->resourcePriv().makeUnbudgeted(); |
37 } | 30 } |
38 return texture; | 31 return texture; |
39 } | 32 } |
40 texture->unref(); | 33 texture->unref(); |
41 } | 34 } |
42 } | 35 } |
43 return fGpu->createTexture(desc, budgeted, srcData, rowBytes); | 36 return fGpu->createTexture(desc, budgeted, srcData, rowBytes); |
44 } | 37 } |
45 | 38 |
46 GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& desc, Scrat
chTexMatch match, | 39 GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& desc, Scrat
chMatch match, |
47 bool calledDuringFlush) { | 40 bool calledDuringFlush) { |
48 if (this->isAbandoned()) { | 41 if (this->isAbandoned()) { |
49 return NULL; | 42 return NULL; |
50 } | 43 } |
51 // Currently we don't recycle compressed textures as scratch. | 44 // Currently we don't recycle compressed textures as scratch. |
52 if (GrPixelConfigIsCompressed(desc.fConfig)) { | 45 if (GrPixelConfigIsCompressed(desc.fConfig)) { |
53 return NULL; | 46 return NULL; |
54 } else { | 47 } else { |
55 uint32_t flags = 0; | 48 uint32_t flags = 0; |
56 if (kExact_ScratchTexMatch == match) { | 49 if (kExact_ScratchMatch == match) { |
57 flags |= kExact_ScratchTextureFlag; | 50 flags |= kExact_ScratchFlag; |
58 } | 51 } |
59 if (calledDuringFlush) { | 52 if (calledDuringFlush) { |
60 flags |= kNoPendingIO_ScratchTextureFlag; | 53 flags |= kNoPendingIO_ScratchFlag; |
61 } | 54 } |
62 return this->internalRefScratchTexture(desc, flags); | 55 return this->internalRefScratchTexture(desc, flags); |
63 } | 56 } |
64 } | 57 } |
65 | 58 |
66 GrTexture* GrTextureProvider::internalRefScratchTexture(const GrSurfaceDesc& inD
esc, | 59 GrTexture* GrTextureProvider::internalRefScratchTexture(const GrSurfaceDesc& inD
esc, |
67 uint32_t flags) { | 60 uint32_t flags) { |
68 SkASSERT(!this->isAbandoned()); | 61 SkASSERT(!this->isAbandoned()); |
69 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); | 62 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); |
70 | 63 |
71 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); | 64 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); |
72 | 65 |
73 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_Gr
SurfaceFlag)) { | 66 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_Gr
SurfaceFlag)) { |
74 if (!(kExact_ScratchTextureFlag & flags)) { | 67 if (!(kExact_ScratchFlag & flags)) { |
75 // bin by pow2 with a reasonable min | 68 // bin by pow2 with a reasonable min |
76 static const int MIN_SIZE = 16; | 69 static const int MIN_SIZE = 16; |
77 GrSurfaceDesc* wdesc = desc.writable(); | 70 GrSurfaceDesc* wdesc = desc.writable(); |
78 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth)); | 71 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth)); |
79 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight)); | 72 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight)); |
80 } | 73 } |
81 | 74 |
82 GrScratchKey key; | 75 GrScratchKey key; |
83 GrTexturePriv::ComputeScratchKey(*desc, &key); | 76 GrTexturePriv::ComputeScratchKey(*desc, &key); |
84 uint32_t scratchFlags = 0; | 77 uint32_t scratchFlags = 0; |
85 if (kNoPendingIO_ScratchTextureFlag & flags) { | 78 if (kNoPendingIO_ScratchFlag & flags) { |
86 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; | 79 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
87 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { | 80 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
88 // If it is not a render target then it will most likely be populate
d by | 81 // If it is not a render target then it will most likely be populate
d by |
89 // writePixels() which will trigger a flush if the texture has pendi
ng IO. | 82 // writePixels() which will trigger a flush if the texture has pendi
ng IO. |
90 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; | 83 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
91 } | 84 } |
92 GrGpuResource* resource = fCache->findAndRefScratchResource(key, scratch
Flags); | 85 GrGpuResource* resource = fCache->findAndRefScratchResource(key, scratch
Flags); |
93 if (resource) { | 86 if (resource) { |
94 GrSurface* surface = static_cast<GrSurface*>(resource); | 87 GrSurface* surface = static_cast<GrSurface*>(resource); |
95 GrRenderTarget* rt = surface->asRenderTarget(); | 88 GrRenderTarget* rt = surface->asRenderTarget(); |
96 if (rt && fGpu->caps()->discardRenderTargetSupport()) { | 89 if (rt && fGpu->caps()->discardRenderTargetSupport()) { |
97 rt->discard(); | 90 rt->discard(); |
98 } | 91 } |
99 return surface->asTexture(); | 92 return surface->asTexture(); |
100 } | 93 } |
101 } | 94 } |
102 | 95 |
103 if (!(kNoCreate_ScratchTextureFlag & flags)) { | 96 if (!(kNoCreate_ScratchFlag & flags)) { |
104 return fGpu->createTexture(*desc, true, NULL, 0); | 97 return fGpu->createTexture(*desc, true, NULL, 0); |
105 } | 98 } |
106 | 99 |
107 return NULL; | 100 return NULL; |
108 } | 101 } |
109 | 102 |
110 GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& des
c) { | 103 GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& des
c) { |
111 if (this->isAbandoned()) { | 104 if (this->isAbandoned()) { |
112 return NULL; | 105 return NULL; |
113 } | 106 } |
(...skipping 11 matching lines...) Expand all Loading... |
125 resource->resourcePriv().setUniqueKey(key); | 118 resource->resourcePriv().setUniqueKey(key); |
126 } | 119 } |
127 | 120 |
128 bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) cons
t { | 121 bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) cons
t { |
129 return this->isAbandoned() ? false : fCache->hasUniqueKey(key); | 122 return this->isAbandoned() ? false : fCache->hasUniqueKey(key); |
130 } | 123 } |
131 | 124 |
132 GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKe
y& key) { | 125 GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKe
y& key) { |
133 return this->isAbandoned() ? NULL : fCache->findAndRefUniqueResource(key); | 126 return this->isAbandoned() ? NULL : fCache->findAndRefUniqueResource(key); |
134 } | 127 } |
OLD | NEW |