blob: 047bd18186ef3b135cef6782a821df1bfa49661a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
reed@google.comac10a2d2010-12-22 21:39:39 +00009#ifndef GrTexture_DEFINED
10#define GrTexture_DEFINED
11
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000012#include "GrSurface.h"
bsalomon@google.com26e18b52013-03-29 19:22:36 +000013#include "SkPoint.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000014#include "GrRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
robertphillips@google.coma1e57952012-06-04 20:05:28 +000016class GrResourceKey;
bsalomon@google.comb8670992012-07-25 21:27:09 +000017class GrTextureParams;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000018class GrTextureImpl;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000020class GrTexture : public GrSurface {
reed@google.comac10a2d2010-12-22 21:39:39 +000021public:
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000022 /**
23 * Approximate number of bytes used by the texture
24 */
25 virtual size_t gpuMemorySize() const SK_OVERRIDE;
26
27 // GrSurface overrides
28 virtual bool readPixels(int left, int top, int width, int height,
29 GrPixelConfig config,
30 void* buffer,
31 size_t rowBytes = 0,
32 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
33
34 virtual void writePixels(int left, int top, int width, int height,
35 GrPixelConfig config,
36 const void* buffer,
37 size_t rowBytes = 0,
38 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
39
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000040 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
41 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000042 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.get(); }
43 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fRenderTarget.get(); }
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000044
45 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000046 * Convert from texels to normalized texture coords for POT textures only. Please don't add
47 * new callsites for these functions. They are slated for removal.
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000048 */
49 SkFixed normalizeFixedX(SkFixed x) const {
tfarinaf9dae782014-06-06 06:35:28 -070050 SkASSERT(SkIsPow2(fDesc.fWidth));
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000051 return x >> fShiftFixedX;
52 }
53 SkFixed normalizeFixedY(SkFixed y) const {
tfarinaf9dae782014-06-06 06:35:28 -070054 SkASSERT(SkIsPow2(fDesc.fHeight));
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000055 return y >> fShiftFixedY;
56 }
57
58 /**
59 * Return the native ID or handle to the texture, depending on the
60 * platform. e.g. on OpenGL, return the texture ID.
61 */
62 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000063
junov@chromium.org957ebdd2012-06-12 13:58:36 +000064 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000065 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
66 * changed externally to Skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +000067 */
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000068 virtual void textureParamsModified() = 0;
69 SK_ATTR_DEPRECATED("Renamed to textureParamsModified.")
70 void invalidateCachedState() { this->textureParamsModified(); }
71
72 /**
73 * Informational texture flags. This will be moved to the private GrTextureImpl class soon.
74 */
75 enum FlagBits {
76 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
77
78 /**
79 * This texture should be returned to the texture cache when
80 * it is no longer reffed
81 */
82 kReturnToCache_FlagBit = kFirstBit,
83 };
84
85 void resetFlag(GrTextureFlags flags) {
86 fDesc.fFlags = fDesc.fFlags & ~flags;
87 }
junov@chromium.org957ebdd2012-06-12 13:58:36 +000088
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000089#ifdef SK_DEBUG
90 void validate() const {
91 this->INHERITED::validate();
92
93 this->validateDesc();
94 }
95#endif
96
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000097 GrTextureImpl* impl() { return reinterpret_cast<GrTextureImpl*>(this); }
98 const GrTextureImpl* impl() const { return reinterpret_cast<const GrTextureImpl*>(this); }
99
100protected:
101 // A texture refs its rt representation but not vice-versa. It is up to
102 // the subclass constructor to initialize this pointer.
103 SkAutoTUnref<GrRenderTarget> fRenderTarget;
104
105 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
106 : INHERITED(gpu, isWrapped, desc)
107 , fRenderTarget(NULL) {
108 // only make sense if alloc size is pow2
109 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
110 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
111 }
112
113 virtual ~GrTexture();
114
115 // GrResource overrides
116 virtual void onRelease() SK_OVERRIDE;
117 virtual void onAbandon() SK_OVERRIDE;
118
119 void validateDesc() const;
120
121private:
122 virtual void internal_dispose() const SK_OVERRIDE;
123
124 // these two shift a fixed-point value into normalized coordinates
125 // for this texture if the texture is power of two sized.
126 int fShiftFixedX;
127 int fShiftFixedY;
128
129 typedef GrSurface INHERITED;
130};
131
132class GrTextureImpl : public GrTexture {
133public:
134 SK_DECLARE_INST_COUNT(GrTextureImpl)
135
136 void setFlag(GrTextureFlags flags) {
137 fDesc.fFlags = fDesc.fFlags | flags;
138 }
139 void resetFlag(GrTextureFlags flags) {
140 fDesc.fFlags = fDesc.fFlags & ~flags;
141 }
142 bool isSetFlag(GrTextureFlags flags) const {
143 return 0 != (fDesc.fFlags & flags);
144 }
145
146 void dirtyMipMaps(bool mipMapsDirty);
147
148 bool mipMapsAreDirty() const {
149 return kValid_MipMapsStatus != fMipMapsStatus;
150 }
151
152 bool hasMipMaps() const {
153 return kNotAllocated_MipMapsStatus != fMipMapsStatus;
154 }
155
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000156 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000157 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000158 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000159 const GrCacheID& cacheID);
160 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000161 static bool NeedsResizing(const GrResourceKey& key);
humper@google.comb86add12013-07-25 18:49:07 +0000162 static bool NeedsBilerp(const GrResourceKey& key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000163
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000164protected:
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000165 GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000166 : INHERITED(gpu, isWrapped, desc)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000167 , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000168 }
robertphillips@google.com32716282012-06-04 12:48:45 +0000169
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000170private:
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000171 enum MipMapsStatus {
172 kNotAllocated_MipMapsStatus,
173 kAllocated_MipMapsStatus,
174 kValid_MipMapsStatus
175 };
176
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000177 MipMapsStatus fMipMapsStatus;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000179 typedef GrTexture INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000180};
181
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000182/**
183 * Represents a texture that is intended to be accessed in device coords with an offset.
184 */
185class GrDeviceCoordTexture {
186public:
187 GrDeviceCoordTexture() { fOffset.set(0, 0); }
188
189 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
190 *this = other;
191 }
192
193 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
194 : fTexture(SkSafeRef(texture))
195 , fOffset(offset) {
196 }
197
198 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
199 fTexture.reset(SkSafeRef(other.fTexture.get()));
200 fOffset = other.fOffset;
201 return *this;
202 }
203
204 const SkIPoint& offset() const { return fOffset; }
205
206 void setOffset(const SkIPoint& offset) { fOffset = offset; }
207 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
208
209 GrTexture* texture() const { return fTexture.get(); }
210
211 GrTexture* setTexture(GrTexture* texture) {
212 fTexture.reset(SkSafeRef(texture));
213 return texture;
214 }
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000215
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000216private:
217 SkAutoTUnref<GrTexture> fTexture;
218 SkIPoint fOffset;
219};
220
reed@google.comac10a2d2010-12-22 21:39:39 +0000221#endif