blob: 1e846c5223ff985e6d8412923fa5770582f1280a [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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#ifndef GrSurfaceProxy_DEFINED
9#define GrSurfaceProxy_DEFINED
10
11#include "GrGpuResource.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040012#include "GrSurface.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070013#include "SkRect.h"
robertphillips76948d42016-05-04 12:47:41 -070014
Robert Phillips37430132016-11-09 06:50:43 -050015class GrCaps;
Robert Phillipsf2361d22016-10-25 14:20:06 -040016class GrOpList;
Robert Phillipseaa86252016-11-08 13:49:39 +000017class GrTextureProvider;
robertphillips76948d42016-05-04 12:47:41 -070018class GrTextureProxy;
19class GrRenderTargetProxy;
Robert Phillips37430132016-11-09 06:50:43 -050020class GrTextureProvider;
robertphillips76948d42016-05-04 12:47:41 -070021
Robert Phillipsc7635fa2016-10-28 13:25:24 -040022// This class replicates the functionality GrIORef<GrSurface> but tracks the
23// utilitization for later resource allocation (for the deferred case) and
24// forwards on the utilization in the wrapped case
25class GrIORefProxy : public SkNoncopyable {
26public:
27 void ref() const {
28 this->validate();
29
30 ++fRefCnt;
31 if (fTarget) {
32 fTarget->ref();
33 }
34 }
35
36 void unref() const {
37 this->validate();
38
39 if (fTarget) {
40 fTarget->unref();
41 }
42
43 if (!(--fRefCnt)) {
44 delete this;
45 return;
46 }
47
48 this->validate();
49 }
50
51 void validate() const {
52#ifdef SK_DEBUG
53 SkASSERT(fRefCnt >= 0);
54#endif
55 }
56
57protected:
58 GrIORefProxy() : fRefCnt(1), fTarget(nullptr) {}
59 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1) {
60 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
61 // anything extra.
62 fTarget = surface.release();
63 }
64 virtual ~GrIORefProxy() {
65 // We don't unref 'fTarget' here since the 'unref' method will already
66 // have forwarded on the unref call that got use here.
67 }
68
69 // TODO: add the IO ref counts. Although if we can delay shader creation to flush time
70 // we may not even need to do that.
71 mutable int32_t fRefCnt;
72
73 // For deferred proxies this will be null. For wrapped proxies it will point to the
74 // wrapped resource.
75 GrSurface* fTarget;
76};
77
78class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -070079public:
Robert Phillips37430132016-11-09 06:50:43 -050080 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
81
82 static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, const GrSurfaceDesc&,
83 SkBackingFit, SkBudgeted);
84
85 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
86 // deferred mode
87 static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, GrTextureProvider*,
88 const GrSurfaceDesc&, SkBudgeted,
89 const void* srcData, size_t rowBytes);
90
robertphillips76948d42016-05-04 12:47:41 -070091 const GrSurfaceDesc& desc() const { return fDesc; }
92
93 GrSurfaceOrigin origin() const {
94 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
95 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
96 return fDesc.fOrigin;
97 }
98 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040099 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700100 GrPixelConfig config() const { return fDesc.fConfig; }
101
102 uint32_t uniqueID() const { return fUniqueID; }
103
Robert Phillips37430132016-11-09 06:50:43 -0500104 GrSurface* instantiate(GrTextureProvider* texProvider);
105
robertphillips76948d42016-05-04 12:47:41 -0700106 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700107 * Helper that gets the width and height of the surface as a bounding rectangle.
108 */
109 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
110
111 /**
robertphillips76948d42016-05-04 12:47:41 -0700112 * @return the texture proxy associated with the surface proxy, may be NULL.
113 */
114 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
115 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
116
117 /**
118 * @return the render target proxy associated with the surface proxy, may be NULL.
119 */
120 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
121 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
122
robertphillips13a7eee2016-08-31 15:06:24 -0700123 /**
124 * Does the resource count against the resource budget?
125 */
126 SkBudgeted isBudgeted() const { return fBudgeted; }
127
Robert Phillipsf2361d22016-10-25 14:20:06 -0400128 void setLastOpList(GrOpList* opList);
129 GrOpList* getLastOpList() { return fLastOpList; }
130
Robert Phillips8bc06d02016-11-01 17:28:40 -0400131 /**
132 * Retrieves the amount of GPU memory that will be or currently is used by this resource
133 * in bytes. It is approximate since we aren't aware of additional padding or copies made
134 * by the driver.
135 *
136 * @return the amount of GPU memory used in bytes
137 */
138 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400139 if (kInvalidGpuMemorySize == fGpuMemorySize) {
140 fGpuMemorySize = this->onGpuMemorySize();
141 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
142 }
143 return fGpuMemorySize;
144 }
145
Robert Phillipseaa86252016-11-08 13:49:39 +0000146 bool isWrapped_ForTesting() const;
147
robertphillips76948d42016-05-04 12:47:41 -0700148protected:
robertphillips8abb3702016-08-31 14:04:06 -0700149 // Deferred version
robertphillips76948d42016-05-04 12:47:41 -0700150 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted)
151 : fDesc(desc)
152 , fFit(fit)
153 , fBudgeted(budgeted)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400154 , fUniqueID(GrGpuResource::CreateUniqueID())
Robert Phillips8bc06d02016-11-01 17:28:40 -0400155 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400156 , fLastOpList(nullptr) {
robertphillips8abb3702016-08-31 14:04:06 -0700157 }
158
159 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400160 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700161
Robert Phillipsf2361d22016-10-25 14:20:06 -0400162 virtual ~GrSurfaceProxy();
163
robertphillips76948d42016-05-04 12:47:41 -0700164 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
165 const GrSurfaceDesc fDesc;
166 const SkBackingFit fFit; // always exact for wrapped resources
167 const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
robertphillips8abb3702016-08-31 14:04:06 -0700168 const uint32_t fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700169
Robert Phillips8bc06d02016-11-01 17:28:40 -0400170 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400171 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
172
173private:
174 virtual size_t onGpuMemorySize() const = 0;
175
Robert Phillips8bc06d02016-11-01 17:28:40 -0400176 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
177 // will be called but, when the proxy is deferred, it will compute the answer itself.
178 // If the proxy computes its own answer that answer is checked (in debug mode) in
179 // the instantiation method.
180 mutable size_t fGpuMemorySize;
181
Robert Phillipsf2361d22016-10-25 14:20:06 -0400182 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400183 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400184 // to this renderTarget).
185 // This back-pointer is required so that we can add a dependancy between
186 // the opList used to create the current contents of this surface
187 // and the opList of a destination surface to which this one is being drawn or copied.
188 GrOpList* fLastOpList;
189
Robert Phillips29e52f12016-11-03 10:19:14 -0400190
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400191 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700192};
193
194#endif