blob: acb7ade0a86c94b383cc9f4470508253da4af0ec [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;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000019class GrTexture : public GrSurface {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000022 SK_DECLARE_INST_COUNT(GrTexture)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000023 // from GrResource
reed@google.comac10a2d2010-12-22 21:39:39 +000024 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000025 * Informational texture flags
26 */
27 enum FlagBits {
28 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
29
30 /**
31 * This texture should be returned to the texture cache when
32 * it is no longer reffed
33 */
34 kReturnToCache_FlagBit = kFirstBit,
35 };
36
37 void setFlag(GrTextureFlags flags) {
38 fDesc.fFlags = fDesc.fFlags | flags;
39 }
40 void resetFlag(GrTextureFlags flags) {
41 fDesc.fFlags = fDesc.fFlags & ~flags;
42 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043 bool isSetFlag(GrTextureFlags flags) const {
44 return 0 != (fDesc.fFlags & flags);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000045 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +000046
47 void dirtyMipMaps(bool mipMapsDirty) {
48 fMipMapsDirty = mipMapsDirty;
49 }
50
51 bool mipMapsAreDirty() const {
52 return fMipMapsDirty;
53 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000054
55 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000056 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000057 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000058 virtual size_t sizeInBytes() const SK_OVERRIDE {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000059 return (size_t) fDesc.fWidth *
60 fDesc.fHeight *
robertphillips@google.com32716282012-06-04 12:48:45 +000061 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000062 }
63
bsalomon@google.com0342a852012-08-20 19:22:38 +000064 // GrSurface overrides
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000065 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000066 GrPixelConfig config,
67 void* buffer,
68 size_t rowBytes = 0,
69 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000070
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000071 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000072 GrPixelConfig config,
73 const void* buffer,
74 size_t rowBytes = 0,
75 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000076
77 /**
78 * @return this texture
79 */
80 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
81 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000082
83 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000084 * Retrieves the render target underlying this texture that can be passed to
85 * GrGpu::setRenderTarget().
86 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000087 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +000088 * render target
89 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
bsalomon@google.com686bcb82013-04-09 15:04:12 +000091 return fRenderTarget.get();
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000092 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
bsalomon@google.com686bcb82013-04-09 15:04:12 +000094 return fRenderTarget.get();
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000095 }
96
97 // GrTexture
98 /**
99 * Convert from texels to normalized texture coords for POT textures
100 * only.
101 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000102 GrFixed normalizeFixedX(GrFixed x) const {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000103 GrAssert(GrIsPow2(fDesc.fWidth));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104 return x >> fShiftFixedX;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000105 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106 GrFixed normalizeFixedY(GrFixed y) const {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000107 GrAssert(GrIsPow2(fDesc.fHeight));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000108 return y >> fShiftFixedY;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000109 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
111 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 * Return the native ID or handle to the texture, depending on the
bsalomon@google.com08afc842012-10-25 18:56:10 +0000113 * platform. e.g. on OpenGL, return the texture ID.
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 */
bsalomon@google.com08afc842012-10-25 18:56:10 +0000115 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000117 /**
118 * Call this when the state of the native API texture object is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000119 * altered directly, without being tracked by skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000120 */
121 virtual void invalidateCachedState() = 0;
122
reed@google.comac10a2d2010-12-22 21:39:39 +0000123#if GR_DEBUG
124 void validate() const {
125 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000126
127 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 }
129#else
130 void validate() const {}
131#endif
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000132 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000133 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000134 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000135 const GrCacheID& cacheID);
136 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000137 static bool NeedsResizing(const GrResourceKey& key);
humper@google.comb86add12013-07-25 18:49:07 +0000138 static bool NeedsBilerp(const GrResourceKey& key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000139
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000140protected:
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000141 // A texture refs its rt representation but not vice-versa. It is up to
142 // the subclass constructor to initialize this pointer.
143 SkAutoTUnref<GrRenderTarget> fRenderTarget;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000144
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000145 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
146 : INHERITED(gpu, isWrapped, desc)
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000147 , fRenderTarget(NULL)
148 , fMipMapsDirty(true) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000149
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000150 // only make sense if alloc size is pow2
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000151 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
152 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000153 }
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000154 virtual ~GrTexture();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000155
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000156 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000157 virtual void onRelease() SK_OVERRIDE;
158 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000159
robertphillips@google.com32716282012-06-04 12:48:45 +0000160 void validateDesc() const;
161
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000162private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 // these two shift a fixed-point value into normalized coordinates
164 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000165 int fShiftFixedX;
166 int fShiftFixedY;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000167
168 bool fMipMapsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000169
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000170 virtual void internal_dispose() const SK_OVERRIDE;
171
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000172 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000173};
174
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000175/**
176 * Represents a texture that is intended to be accessed in device coords with an offset.
177 */
178class GrDeviceCoordTexture {
179public:
180 GrDeviceCoordTexture() { fOffset.set(0, 0); }
181
182 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
183 *this = other;
184 }
185
186 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
187 : fTexture(SkSafeRef(texture))
188 , fOffset(offset) {
189 }
190
191 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
192 fTexture.reset(SkSafeRef(other.fTexture.get()));
193 fOffset = other.fOffset;
194 return *this;
195 }
196
197 const SkIPoint& offset() const { return fOffset; }
198
199 void setOffset(const SkIPoint& offset) { fOffset = offset; }
200 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
201
202 GrTexture* texture() const { return fTexture.get(); }
203
204 GrTexture* setTexture(GrTexture* texture) {
205 fTexture.reset(SkSafeRef(texture));
206 return texture;
207 }
208private:
209 SkAutoTUnref<GrTexture> fTexture;
210 SkIPoint fOffset;
211};
212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213#endif