blob: 5d8ecaaaf257c83830b8b40a01578c3b48ef34e3 [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"
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.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 {
83 return fRenderTarget;
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 {
86 return fRenderTarget;
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 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000104 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000105 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000106 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000108 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000109
110 /**
111 * Return the native ID or handle to the texture, depending on the
bsalomon@google.com08afc842012-10-25 18:56:10 +0000112 * platform. e.g. on OpenGL, return the texture ID.
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 */
bsalomon@google.com08afc842012-10-25 18:56:10 +0000114 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000116 /**
117 * Call this when the state of the native API texture object is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000118 * altered directly, without being tracked by skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000119 */
120 virtual void invalidateCachedState() = 0;
121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122#if GR_DEBUG
123 void validate() const {
124 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000125
126 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 }
128#else
129 void validate() const {}
130#endif
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);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000137 static bool NeedsFiltering(const GrResourceKey& key);
138
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000139protected:
140 GrRenderTarget* fRenderTarget; // texture refs its rt representation
141 // base class cons sets to NULL
142 // subclass cons can create and set
143
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000144 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
145 : INHERITED(gpu, isWrapped, desc)
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000146 , fRenderTarget(NULL) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000147
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000148 // only make sense if alloc size is pow2
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000149 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
150 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000151 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000152
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000153 // GrResource overrides
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000154 virtual void onRelease() SK_OVERRIDE;
155 virtual void onAbandon() SK_OVERRIDE;
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000156
robertphillips@google.com32716282012-06-04 12:48:45 +0000157 void validateDesc() const;
158
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000159private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 // these two shift a fixed-point value into normalized coordinates
161 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000162 int fShiftFixedX;
163 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000164
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000165 virtual void internal_dispose() const SK_OVERRIDE;
166
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000167 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000168};
169
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000170/**
171 * Represents a texture that is intended to be accessed in device coords with an offset.
172 */
173class GrDeviceCoordTexture {
174public:
175 GrDeviceCoordTexture() { fOffset.set(0, 0); }
176
177 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
178 *this = other;
179 }
180
181 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
182 : fTexture(SkSafeRef(texture))
183 , fOffset(offset) {
184 }
185
186 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
187 fTexture.reset(SkSafeRef(other.fTexture.get()));
188 fOffset = other.fOffset;
189 return *this;
190 }
191
192 const SkIPoint& offset() const { return fOffset; }
193
194 void setOffset(const SkIPoint& offset) { fOffset = offset; }
195 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
196
197 GrTexture* texture() const { return fTexture.get(); }
198
199 GrTexture* setTexture(GrTexture* texture) {
200 fTexture.reset(SkSafeRef(texture));
201 return texture;
202 }
203private:
204 SkAutoTUnref<GrTexture> fTexture;
205 SkIPoint fOffset;
206};
207
reed@google.comac10a2d2010-12-22 21:39:39 +0000208#endif