GrTextureRenderTargetProxy.cpp revision 0a375db9a4c1dc96f9d5856526e074ab2802fb0e
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrTextureRenderTargetProxy.h"
9
10#include "GrCaps.h"
11#include "GrTexture.h"
12#include "GrRenderTarget.h"
13#include "GrSurfaceProxyPriv.h"
14
15// Deferred version
16// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
17// GrRenderTargetProxy) so its constructor must be explicitly called.
18GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
19                                                       const GrSurfaceDesc& desc,
20                                                       SkBackingFit fit,
21                                                       SkBudgeted budgeted,
22                                                       uint32_t flags)
23        : GrSurfaceProxy(desc, fit, budgeted, flags)
24        // for now textures w/ data are always wrapped
25        , GrTextureProxy(desc, fit, budgeted, nullptr, 0, flags)
26        , GrRenderTargetProxy(caps, desc, fit, budgeted, flags) {
27}
28
29// Lazy-callback version
30GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
31                                                       const GrSurfaceDesc& desc,
32                                                       GrMipMapped mipMapped,
33                                                       SkBackingFit fit,
34                                                       SkBudgeted budgeted,
35                                                       uint32_t flags)
36        : GrSurfaceProxy(std::move(callback), desc, fit, budgeted, flags)
37        // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
38        // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
39        , GrTextureProxy(LazyInstantiateCallback(), desc, mipMapped, fit, budgeted, flags)
40        , GrRenderTargetProxy(LazyInstantiateCallback(), desc, fit, budgeted, flags) {
41}
42
43// Wrapped version
44// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
45// GrRenderTargetProxy) so its constructor must be explicitly called.
46GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
47                                                       GrSurfaceOrigin origin)
48        : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
49        , GrTextureProxy(surf, origin)
50        , GrRenderTargetProxy(surf, origin) {
51    SkASSERT(surf->asTexture());
52    SkASSERT(surf->asRenderTarget());
53}
54
55size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
56    int colorSamplesPerPixel = this->numColorSamples() + 1;
57
58    // TODO: do we have enough information to improve this worst case estimate?
59    return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
60                                  colorSamplesPerPixel, this->mipMapped(), !this->priv().isExact());
61}
62
63bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
64    if (LazyState::kNot != this->lazyInstantiationState()) {
65        return false;
66    }
67    static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
68
69    const GrUniqueKey& key = this->getUniqueKey();
70
71    if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
72                               kFlags, this->mipMapped(), this->mipColorMode(),
73                               key.isValid() ? &key : nullptr)) {
74        return false;
75    }
76    if (key.isValid()) {
77        SkASSERT(key == this->getUniqueKey());
78    }
79
80    SkASSERT(fTarget->asRenderTarget());
81    SkASSERT(fTarget->asTexture());
82
83    return true;
84}
85
86sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
87                                                    GrResourceProvider* resourceProvider) const {
88    static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
89
90    sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
91                                                       this->needsStencil(), kFlags,
92                                                       this->mipMapped(), this->mipColorMode());
93    if (!surface) {
94        return nullptr;
95    }
96    SkASSERT(surface->asRenderTarget());
97    SkASSERT(surface->asTexture());
98
99    return surface;
100}
101
102#ifdef SK_DEBUG
103void GrTextureRenderTargetProxy::validateLazyTexture(const GrTexture* texture) {
104    SkASSERT(texture->asRenderTarget());
105    SkASSERT(texture->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
106    SkASSERT(GrMipMapped::kNo == this->mipMapped());
107}
108#endif
109
110