blob: e56f6f401c000161b4e32d22b2019182ab576e66 [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 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +000046
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +000047 void dirtyMipMaps(bool mipMapsDirty) {
48 fMipMapsDirty = mipMapsDirty;
49 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +000050
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +000051 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 {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000103 SkASSERT(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 {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000107 SkASSERT(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
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000123#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 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 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000129#endif
robertphillips@google.com03087072013-10-02 16:42:21 +0000130
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000131 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000132 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000133 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000134 const GrCacheID& cacheID);
135 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000136 static bool NeedsResizing(const GrResourceKey& key);
humper@google.comb86add12013-07-25 18:49:07 +0000137 static bool NeedsBilerp(const GrResourceKey& key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000138
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000139protected:
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000140 // A texture refs its rt representation but not vice-versa. It is up to
141 // the subclass constructor to initialize this pointer.
142 SkAutoTUnref<GrRenderTarget> fRenderTarget;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000143
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000144 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
145 : INHERITED(gpu, isWrapped, desc)
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +0000146 , fRenderTarget(NULL)
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000147 , fMipMapsDirty(true) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000148
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000149 // only make sense if alloc size is pow2
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000150 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
151 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000152 }
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000153 virtual ~GrTexture();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000154
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000155 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000156 virtual void onRelease() SK_OVERRIDE;
157 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000158
robertphillips@google.com32716282012-06-04 12:48:45 +0000159 void validateDesc() const;
160
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000161private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 // these two shift a fixed-point value into normalized coordinates
163 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000164 int fShiftFixedX;
165 int fShiftFixedY;
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +0000166
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000167 bool fMipMapsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000169 virtual void internal_dispose() const SK_OVERRIDE;
170
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000171 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000172};
173
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000174/**
175 * Represents a texture that is intended to be accessed in device coords with an offset.
176 */
177class GrDeviceCoordTexture {
178public:
179 GrDeviceCoordTexture() { fOffset.set(0, 0); }
180
181 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
182 *this = other;
183 }
184
185 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
186 : fTexture(SkSafeRef(texture))
187 , fOffset(offset) {
188 }
189
190 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
191 fTexture.reset(SkSafeRef(other.fTexture.get()));
192 fOffset = other.fOffset;
193 return *this;
194 }
195
196 const SkIPoint& offset() const { return fOffset; }
197
198 void setOffset(const SkIPoint& offset) { fOffset = offset; }
199 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
200
201 GrTexture* texture() const { return fTexture.get(); }
202
203 GrTexture* setTexture(GrTexture* texture) {
204 fTexture.reset(SkSafeRef(texture));
205 return texture;
206 }
207private:
208 SkAutoTUnref<GrTexture> fTexture;
209 SkIPoint fOffset;
210};
211
reed@google.comac10a2d2010-12-22 21:39:39 +0000212#endif