blob: c088bdd582b441a0bd5a3d6597a97591cacb847b [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"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014class GrRenderTarget;
robertphillips@google.coma1e57952012-06-04 20:05:28 +000015class GrResourceKey;
bsalomon@google.comb8670992012-07-25 21:27:09 +000016class GrTextureParams;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000018class GrTexture : public GrSurface {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000019
reed@google.comac10a2d2010-12-22 21:39:39 +000020public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000021 SK_DECLARE_INST_COUNT(GrTexture)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000022 // from GrResource
reed@google.comac10a2d2010-12-22 21:39:39 +000023 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000024 * Informational texture flags
25 */
26 enum FlagBits {
27 kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
28
29 /**
30 * This texture should be returned to the texture cache when
31 * it is no longer reffed
32 */
33 kReturnToCache_FlagBit = kFirstBit,
34 };
35
36 void setFlag(GrTextureFlags flags) {
37 fDesc.fFlags = fDesc.fFlags | flags;
38 }
39 void resetFlag(GrTextureFlags flags) {
40 fDesc.fFlags = fDesc.fFlags & ~flags;
41 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 bool isSetFlag(GrTextureFlags flags) const {
43 return 0 != (fDesc.fFlags & flags);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000044 }
45
46 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000047 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000048 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000049 virtual size_t sizeInBytes() const SK_OVERRIDE {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050 return (size_t) fDesc.fWidth *
51 fDesc.fHeight *
robertphillips@google.com32716282012-06-04 12:48:45 +000052 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000053 }
54
bsalomon@google.com0342a852012-08-20 19:22:38 +000055 // GrSurface overrides
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000056 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000057 GrPixelConfig config,
58 void* buffer,
59 size_t rowBytes = 0,
60 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000061
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000062 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000063 GrPixelConfig config,
64 const void* buffer,
65 size_t rowBytes = 0,
66 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000067
68 /**
69 * @return this texture
70 */
71 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
72 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000073
74 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000075 * Retrieves the render target underlying this texture that can be passed to
76 * GrGpu::setRenderTarget().
77 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000078 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +000079 * render target
80 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
82 return fRenderTarget;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000083 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
85 return fRenderTarget;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000086 }
87
88 // GrTexture
89 /**
90 * Convert from texels to normalized texture coords for POT textures
91 * only.
92 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093 GrFixed normalizeFixedX(GrFixed x) const {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000094 GrAssert(GrIsPow2(fDesc.fWidth));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000095 return x >> fShiftFixedX;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000096 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097 GrFixed normalizeFixedY(GrFixed y) const {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000098 GrAssert(GrIsPow2(fDesc.fHeight));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099 return y >> fShiftFixedY;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000100 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
102 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000103 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000105 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000107 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
109 /**
110 * Return the native ID or handle to the texture, depending on the
bsalomon@google.com08afc842012-10-25 18:56:10 +0000111 * platform. e.g. on OpenGL, return the texture ID.
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 */
bsalomon@google.com08afc842012-10-25 18:56:10 +0000113 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000115 /**
116 * Call this when the state of the native API texture object is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000117 * altered directly, without being tracked by skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000118 */
119 virtual void invalidateCachedState() = 0;
120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121#if GR_DEBUG
122 void validate() const {
123 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000124
125 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 }
127#else
128 void validate() const {}
129#endif
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000130 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000131 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000132 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000133 const GrCacheID& cacheID);
134 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000135 static bool NeedsResizing(const GrResourceKey& key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000136 static bool NeedsFiltering(const GrResourceKey& key);
137
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000138protected:
139 GrRenderTarget* fRenderTarget; // texture refs its rt representation
140 // base class cons sets to NULL
141 // subclass cons can create and set
142
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000143 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
144 : INHERITED(gpu, isWrapped, desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000145 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000146
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000147 // only make sense if alloc size is pow2
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000148 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
149 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000150 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000151
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000152 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000153 virtual void onRelease() SK_OVERRIDE;
154 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000155
robertphillips@google.com32716282012-06-04 12:48:45 +0000156 void validateDesc() const;
157
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000158private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 // these two shift a fixed-point value into normalized coordinates
160 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000161 int fShiftFixedX;
162 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000163
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000164 virtual void internal_dispose() const SK_OVERRIDE;
165
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000166 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000167};
168
169#endif