blob: d57c2f5993e75518a87d984ed41c45f280a442eb [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 Phillipsf2361d22016-10-25 14:20:06 -040015class GrOpList;
robertphillips76948d42016-05-04 12:47:41 -070016class GrTextureProxy;
17class GrRenderTargetProxy;
18
Robert Phillipsc7635fa2016-10-28 13:25:24 -040019// This class replicates the functionality GrIORef<GrSurface> but tracks the
20// utilitization for later resource allocation (for the deferred case) and
21// forwards on the utilization in the wrapped case
22class GrIORefProxy : public SkNoncopyable {
23public:
24 void ref() const {
25 this->validate();
26
27 ++fRefCnt;
28 if (fTarget) {
29 fTarget->ref();
30 }
31 }
32
33 void unref() const {
34 this->validate();
35
36 if (fTarget) {
37 fTarget->unref();
38 }
39
40 if (!(--fRefCnt)) {
41 delete this;
42 return;
43 }
44
45 this->validate();
46 }
47
48 void validate() const {
49#ifdef SK_DEBUG
50 SkASSERT(fRefCnt >= 0);
51#endif
52 }
53
54protected:
55 GrIORefProxy() : fRefCnt(1), fTarget(nullptr) {}
56 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1) {
57 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
58 // anything extra.
59 fTarget = surface.release();
60 }
61 virtual ~GrIORefProxy() {
62 // We don't unref 'fTarget' here since the 'unref' method will already
63 // have forwarded on the unref call that got use here.
64 }
65
66 // TODO: add the IO ref counts. Although if we can delay shader creation to flush time
67 // we may not even need to do that.
68 mutable int32_t fRefCnt;
69
70 // For deferred proxies this will be null. For wrapped proxies it will point to the
71 // wrapped resource.
72 GrSurface* fTarget;
73};
74
75class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -070076public:
77 const GrSurfaceDesc& desc() const { return fDesc; }
78
79 GrSurfaceOrigin origin() const {
80 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
81 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
82 return fDesc.fOrigin;
83 }
84 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040085 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -070086 GrPixelConfig config() const { return fDesc.fConfig; }
87
88 uint32_t uniqueID() const { return fUniqueID; }
89
90 /**
robertphillips13a7eee2016-08-31 15:06:24 -070091 * Helper that gets the width and height of the surface as a bounding rectangle.
92 */
93 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
94
95 /**
robertphillips76948d42016-05-04 12:47:41 -070096 * @return the texture proxy associated with the surface proxy, may be NULL.
97 */
98 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
99 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
100
101 /**
102 * @return the render target proxy associated with the surface proxy, may be NULL.
103 */
104 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
105 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
106
robertphillips13a7eee2016-08-31 15:06:24 -0700107 /**
108 * Does the resource count against the resource budget?
109 */
110 SkBudgeted isBudgeted() const { return fBudgeted; }
111
Robert Phillipsf2361d22016-10-25 14:20:06 -0400112 void setLastOpList(GrOpList* opList);
113 GrOpList* getLastOpList() { return fLastOpList; }
114
robertphillips76948d42016-05-04 12:47:41 -0700115protected:
robertphillips8abb3702016-08-31 14:04:06 -0700116 // Deferred version
robertphillips76948d42016-05-04 12:47:41 -0700117 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted)
118 : fDesc(desc)
119 , fFit(fit)
120 , fBudgeted(budgeted)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400121 , fUniqueID(GrGpuResource::CreateUniqueID())
122 , fLastOpList(nullptr) {
robertphillips8abb3702016-08-31 14:04:06 -0700123 }
124
125 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400126 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700127
Robert Phillipsf2361d22016-10-25 14:20:06 -0400128 virtual ~GrSurfaceProxy();
129
robertphillips76948d42016-05-04 12:47:41 -0700130 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
131 const GrSurfaceDesc fDesc;
132 const SkBackingFit fFit; // always exact for wrapped resources
133 const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
robertphillips8abb3702016-08-31 14:04:06 -0700134 const uint32_t fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700135
136private:
Robert Phillipsf2361d22016-10-25 14:20:06 -0400137 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400138 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400139 // to this renderTarget).
140 // This back-pointer is required so that we can add a dependancy between
141 // the opList used to create the current contents of this surface
142 // and the opList of a destination surface to which this one is being drawn or copied.
143 GrOpList* fLastOpList;
144
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400145 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700146};
147
148#endif