blob: 422814bdcc869c1ee7af3480c45da28ae919f4cd [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 }
46
47 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000048 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000049 */
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000050 virtual size_t sizeInBytes() const SK_OVERRIDE {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051 return (size_t) fDesc.fWidth *
52 fDesc.fHeight *
robertphillips@google.com32716282012-06-04 12:48:45 +000053 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000054 }
55
bsalomon@google.com0342a852012-08-20 19:22:38 +000056 // GrSurface overrides
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000057 virtual bool readPixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000058 GrPixelConfig config,
59 void* buffer,
60 size_t rowBytes = 0,
61 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
bsalomon@google.com6f379512011-11-16 20:36:03 +000062
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000063 virtual void writePixels(int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +000064 GrPixelConfig config,
65 const void* buffer,
66 size_t rowBytes = 0,
67 uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000068
69 /**
70 * @return this texture
71 */
72 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
73 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000074
75 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000076 * Retrieves the render target underlying this texture that can be passed to
77 * GrGpu::setRenderTarget().
78 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000079 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +000080 * render target
81 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
bsalomon@google.com686bcb82013-04-09 15:04:12 +000083 return fRenderTarget.get();
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000084 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
bsalomon@google.com686bcb82013-04-09 15:04:12 +000086 return fRenderTarget.get();
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000087 }
88
89 // GrTexture
90 /**
91 * Convert from texels to normalized texture coords for POT textures
92 * only.
93 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094 GrFixed normalizeFixedX(GrFixed x) const {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000095 GrAssert(GrIsPow2(fDesc.fWidth));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096 return x >> fShiftFixedX;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000097 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000098 GrFixed normalizeFixedY(GrFixed y) const {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000099 GrAssert(GrIsPow2(fDesc.fHeight));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100 return y >> fShiftFixedY;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000101 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000102
103 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 * Return the native ID or handle to the texture, depending on the
bsalomon@google.com08afc842012-10-25 18:56:10 +0000105 * platform. e.g. on OpenGL, return the texture ID.
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 */
bsalomon@google.com08afc842012-10-25 18:56:10 +0000107 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000109 /**
110 * Call this when the state of the native API texture object is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000111 * altered directly, without being tracked by skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000112 */
113 virtual void invalidateCachedState() = 0;
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115#if GR_DEBUG
116 void validate() const {
117 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000118
119 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 }
121#else
122 void validate() const {}
123#endif
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000124 static GrResourceKey ComputeKey(const GrGpu* gpu,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000125 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000126 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000127 const GrCacheID& cacheID);
128 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000129 static bool NeedsResizing(const GrResourceKey& key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000130 static bool NeedsFiltering(const GrResourceKey& key);
131
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000132protected:
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000133 // A texture refs its rt representation but not vice-versa. It is up to
134 // the subclass constructor to initialize this pointer.
135 SkAutoTUnref<GrRenderTarget> fRenderTarget;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000136
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000137 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
138 : INHERITED(gpu, isWrapped, desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000139 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000140
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000141 // only make sense if alloc size is pow2
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000142 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
143 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000144 }
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000145 virtual ~GrTexture();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000146
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000147 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000148 virtual void onRelease() SK_OVERRIDE;
149 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000150
robertphillips@google.com32716282012-06-04 12:48:45 +0000151 void validateDesc() const;
152
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000153private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 // these two shift a fixed-point value into normalized coordinates
155 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000156 int fShiftFixedX;
157 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000159 virtual void internal_dispose() const SK_OVERRIDE;
160
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000161 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000162};
163
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000164/**
165 * Represents a texture that is intended to be accessed in device coords with an offset.
166 */
167class GrDeviceCoordTexture {
168public:
169 GrDeviceCoordTexture() { fOffset.set(0, 0); }
170
171 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
172 *this = other;
173 }
174
175 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
176 : fTexture(SkSafeRef(texture))
177 , fOffset(offset) {
178 }
179
180 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
181 fTexture.reset(SkSafeRef(other.fTexture.get()));
182 fOffset = other.fOffset;
183 return *this;
184 }
185
186 const SkIPoint& offset() const { return fOffset; }
187
188 void setOffset(const SkIPoint& offset) { fOffset = offset; }
189 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
190
191 GrTexture* texture() const { return fTexture.get(); }
192
193 GrTexture* setTexture(GrTexture* texture) {
194 fTexture.reset(SkSafeRef(texture));
195 return texture;
196 }
197private:
198 SkAutoTUnref<GrTexture> fTexture;
199 SkIPoint fOffset;
200};
201
reed@google.comac10a2d2010-12-22 21:39:39 +0000202#endif