blob: cb652f7a7fd8c2e27b46fb7a4c802a4031b900e1 [file] [log] [blame]
bsalomond309e7a2015-04-30 14:18:54 -07001
2/*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrTextureProvider.h"
10#include "GrTexturePriv.h"
11#include "GrResourceCache.h"
12#include "GrGpu.h"
13
14enum ScratchTextureFlags {
15 kExact_ScratchTextureFlag = 0x1,
16 kNoPendingIO_ScratchTextureFlag = 0x2,
17 kNoCreate_ScratchTextureFlag = 0x4,
18};
19
20GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budgeted,
21 const void* srcData, size_t rowBytes) {
22 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -070023 return nullptr;
bsalomond309e7a2015-04-30 14:18:54 -070024 }
25 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
26 !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
halcanary96fcdcc2015-08-27 07:41:13 -070027 return nullptr;
bsalomond309e7a2015-04-30 14:18:54 -070028 }
29 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
30 static const uint32_t kFlags = kExact_ScratchTextureFlag |
31 kNoCreate_ScratchTextureFlag;
bsalomoneae62002015-07-31 13:59:30 -070032 if (GrTexture* texture = this->refScratchTexture(desc, kFlags)) {
bsalomond309e7a2015-04-30 14:18:54 -070033 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
34 srcData, rowBytes)) {
35 if (!budgeted) {
36 texture->resourcePriv().makeUnbudgeted();
37 }
38 return texture;
39 }
40 texture->unref();
41 }
42 }
43 return fGpu->createTexture(desc, budgeted, srcData, rowBytes);
44}
45
bsalomoneae62002015-07-31 13:59:30 -070046GrTexture* GrTextureProvider::createApproxTexture(const GrSurfaceDesc& desc) {
47 return this->internalCreateApproxTexture(desc, 0);
48}
49
50GrTexture* GrTextureProvider::internalCreateApproxTexture(const GrSurfaceDesc& desc,
51 uint32_t scratchFlags) {
bsalomond309e7a2015-04-30 14:18:54 -070052 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 return nullptr;
bsalomond309e7a2015-04-30 14:18:54 -070054 }
55 // Currently we don't recycle compressed textures as scratch.
56 if (GrPixelConfigIsCompressed(desc.fConfig)) {
halcanary96fcdcc2015-08-27 07:41:13 -070057 return nullptr;
bsalomond309e7a2015-04-30 14:18:54 -070058 } else {
bsalomoneae62002015-07-31 13:59:30 -070059 return this->refScratchTexture(desc, scratchFlags);
bsalomond309e7a2015-04-30 14:18:54 -070060 }
61}
62
bsalomoneae62002015-07-31 13:59:30 -070063GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& inDesc,
brucedawson7b77ac12015-08-05 14:05:17 -070064 uint32_t flags) {
bsalomond309e7a2015-04-30 14:18:54 -070065 SkASSERT(!this->isAbandoned());
66 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
67
68 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
69
70 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
brucedawson7b77ac12015-08-05 14:05:17 -070071 if (!(kExact_ScratchTextureFlag & flags)) {
bsalomond309e7a2015-04-30 14:18:54 -070072 // bin by pow2 with a reasonable min
bsalomon100b8f82015-10-28 08:37:44 -070073 const int kMinSize = 16;
bsalomond309e7a2015-04-30 14:18:54 -070074 GrSurfaceDesc* wdesc = desc.writable();
bsalomon100b8f82015-10-28 08:37:44 -070075 wdesc->fWidth = SkTMax(kMinSize, GrNextPow2(desc->fWidth));
76 wdesc->fHeight = SkTMax(kMinSize, GrNextPow2(desc->fHeight));
bsalomond309e7a2015-04-30 14:18:54 -070077 }
78
79 GrScratchKey key;
80 GrTexturePriv::ComputeScratchKey(*desc, &key);
81 uint32_t scratchFlags = 0;
brucedawson7b77ac12015-08-05 14:05:17 -070082 if (kNoPendingIO_ScratchTextureFlag & flags) {
bsalomond309e7a2015-04-30 14:18:54 -070083 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
84 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
85 // If it is not a render target then it will most likely be populated by
86 // writePixels() which will trigger a flush if the texture has pending IO.
87 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
88 }
robertphillips6e83ac72015-08-13 05:19:14 -070089 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
90 GrSurface::WorseCaseSize(*desc),
91 scratchFlags);
bsalomond309e7a2015-04-30 14:18:54 -070092 if (resource) {
93 GrSurface* surface = static_cast<GrSurface*>(resource);
94 GrRenderTarget* rt = surface->asRenderTarget();
95 if (rt && fGpu->caps()->discardRenderTargetSupport()) {
96 rt->discard();
97 }
98 return surface->asTexture();
99 }
100 }
101
brucedawson7b77ac12015-08-05 14:05:17 -0700102 if (!(kNoCreate_ScratchTextureFlag & flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700103 return fGpu->createTexture(*desc, true, nullptr, 0);
bsalomond309e7a2015-04-30 14:18:54 -0700104 }
105
halcanary96fcdcc2015-08-27 07:41:13 -0700106 return nullptr;
bsalomond309e7a2015-04-30 14:18:54 -0700107}
108
bsalomon6dc6f5f2015-06-18 09:12:16 -0700109GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& desc,
110 GrWrapOwnership ownership) {
bsalomond309e7a2015-04-30 14:18:54 -0700111 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
bsalomond309e7a2015-04-30 14:18:54 -0700113 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700114 return fGpu->wrapBackendTexture(desc, ownership);
bsalomond309e7a2015-04-30 14:18:54 -0700115}
116
117GrRenderTarget* GrTextureProvider::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(desc,
bsalomon6dc6f5f2015-06-18 09:12:16 -0700119 kBorrow_GrWrapOwnership);
bsalomond309e7a2015-04-30 14:18:54 -0700120}
121
122void GrTextureProvider::assignUniqueKeyToResource(const GrUniqueKey& key, GrGpuResource* resource) {
123 if (this->isAbandoned() || !resource) {
124 return;
125 }
126 resource->resourcePriv().setUniqueKey(key);
127}
128
129bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) const {
130 return this->isAbandoned() ? false : fCache->hasUniqueKey(key);
131}
132
133GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
halcanary96fcdcc2015-08-27 07:41:13 -0700134 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
bsalomond309e7a2015-04-30 14:18:54 -0700135}