blob: c7bfb8c3634b83ca1fdc1ddf7ef3d3ab1f22eb58 [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"
robertphillips@google.com46a86002012-08-08 10:42:44 +000013#include "GrCacheID.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015class GrRenderTarget;
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.com46a86002012-08-08 10:42:44 +000023 GR_DECLARE_RESOURCE_CACHE_TYPE()
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000024
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000025 // from GrResource
reed@google.comac10a2d2010-12-22 21:39:39 +000026 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000027 * Informational texture flags
28 */
29 enum FlagBits {
30 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
31
32 /**
33 * This texture should be returned to the texture cache when
34 * it is no longer reffed
35 */
36 kReturnToCache_FlagBit = kFirstBit,
37 };
38
39 void setFlag(GrTextureFlags flags) {
40 fDesc.fFlags = fDesc.fFlags | flags;
41 }
42 void resetFlag(GrTextureFlags flags) {
43 fDesc.fFlags = fDesc.fFlags & ~flags;
44 }
45 bool isSetFlag(GrTextureFlags flags) const {
46 return 0 != (fDesc.fFlags & flags);
47 }
48
49 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000050 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000051 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000052 virtual size_t sizeInBytes() const SK_OVERRIDE {
robertphillips@google.com32716282012-06-04 12:48:45 +000053 return (size_t) fDesc.fWidth *
54 fDesc.fHeight *
55 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000056 }
57
bsalomon@google.com0342a852012-08-20 19:22:38 +000058 // GrSurface overrides
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000059 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000060 GrPixelConfig config,
61 void* buffer,
62 size_t rowBytes = 0,
63 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000064
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000065 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000066 GrPixelConfig config,
67 const void* buffer,
68 size_t rowBytes = 0,
69 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000070
71 /**
72 * @return this texture
73 */
74 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
75 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000076
77 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000078 * Retrieves the render target underlying this texture that can be passed to
79 * GrGpu::setRenderTarget().
80 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000081 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +000082 * render target
83 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000084 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
85 return fRenderTarget;
86 }
87 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
88 return fRenderTarget;
89 }
90
91 // GrTexture
92 /**
93 * Convert from texels to normalized texture coords for POT textures
94 * only.
95 */
96 GrFixed normalizeFixedX(GrFixed x) const {
97 GrAssert(GrIsPow2(fDesc.fWidth));
98 return x >> fShiftFixedX;
99 }
100 GrFixed normalizeFixedY(GrFixed y) const {
101 GrAssert(GrIsPow2(fDesc.fHeight));
102 return y >> fShiftFixedY;
103 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000104
105 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000106 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000108 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000110 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 /**
113 * Return the native ID or handle to the texture, depending on the
114 * platform. e.g. on opengl, return the texture ID.
115 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000116 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000117
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000118 /**
119 * Call this when the state of the native API texture object is
120 * altered directly, without being tracked by skia.
121 */
122 virtual void invalidateCachedState() = 0;
123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124#if GR_DEBUG
125 void validate() const {
126 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000127
128 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 }
130#else
131 void validate() const {}
132#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000133
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000134 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000135 const GrTextureParams* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000136 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000137 const GrCacheData& cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000138 bool scratch);
139
140 static bool NeedsResizing(const GrResourceKey& key);
141 static bool IsScratchTexture(const GrResourceKey& key);
142 static bool NeedsFiltering(const GrResourceKey& key);
143
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000144protected:
145 GrRenderTarget* fRenderTarget; // texture refs its rt representation
146 // base class cons sets to NULL
147 // subclass cons can create and set
148
robertphillips@google.com32716282012-06-04 12:48:45 +0000149 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000150 : INHERITED(gpu, desc)
151 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000152
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000153 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000154 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
155 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000156 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000157
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000158 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000159 virtual void onRelease() SK_OVERRIDE;
160 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000161
robertphillips@google.com32716282012-06-04 12:48:45 +0000162 void validateDesc() const;
163
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000164private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 // these two shift a fixed-point value into normalized coordinates
166 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000167 int fShiftFixedX;
168 int fShiftFixedY;
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
175#endif
176