blob: d02382981d02fc9c348f082377c33f9e691812d8 [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.com686bcb82013-04-09 15:04:12 +000013#include "GrRenderTarget.h"
bsalomonc44be0e2014-07-25 07:32:33 -070014#include "SkPoint.h"
15#include "SkRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
robertphillips@google.coma1e57952012-06-04 20:05:28 +000017class GrResourceKey;
bsalomon@google.comb8670992012-07-25 21:27:09 +000018class GrTextureParams;
bsalomonafbf2d62014-09-30 12:18:44 -070019class GrTexturePriv;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000021class GrTexture : public GrSurface {
reed@google.comac10a2d2010-12-22 21:39:39 +000022public:
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000023 /**
24 * Approximate number of bytes used by the texture
25 */
26 virtual size_t gpuMemorySize() const SK_OVERRIDE;
27
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000028 virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
29 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000030 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.get(); }
31 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fRenderTarget.get(); }
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000032
33 /**
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000034 * Return the native ID or handle to the texture, depending on the
35 * platform. e.g. on OpenGL, return the texture ID.
36 */
37 virtual GrBackendObject getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000038
junov@chromium.org957ebdd2012-06-12 13:58:36 +000039 /**
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000040 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
41 * changed externally to Skia.
junov@chromium.org957ebdd2012-06-12 13:58:36 +000042 */
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000043 virtual void textureParamsModified() = 0;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000044
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000045#ifdef SK_DEBUG
46 void validate() const {
47 this->INHERITED::validate();
commit-bot@chromium.org59e7d232014-05-09 18:02:51 +000048 this->validateDesc();
49 }
50#endif
51
bsalomonafbf2d62014-09-30 12:18:44 -070052 /** Access methods that are only to be used within Skia code. */
53 inline GrTexturePriv texturePriv();
54 inline const GrTexturePriv texturePriv() const;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000055
56protected:
57 // A texture refs its rt representation but not vice-versa. It is up to
58 // the subclass constructor to initialize this pointer.
59 SkAutoTUnref<GrRenderTarget> fRenderTarget;
60
bsalomonafbf2d62014-09-30 12:18:44 -070061 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000062
63 virtual ~GrTexture();
64
65 // GrResource overrides
66 virtual void onRelease() SK_OVERRIDE;
67 virtual void onAbandon() SK_OVERRIDE;
68
69 void validateDesc() const;
70
71private:
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000072 void dirtyMipMaps(bool mipMapsDirty);
73
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000074 enum MipMapsStatus {
75 kNotAllocated_MipMapsStatus,
76 kAllocated_MipMapsStatus,
77 kValid_MipMapsStatus
78 };
79
bsalomonafbf2d62014-09-30 12:18:44 -070080 MipMapsStatus fMipMapsStatus;
81 // These two shift a fixed-point value into normalized coordinates
82 // for this texture if the texture is power of two sized.
83 int fShiftFixedX;
84 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +000085
bsalomonafbf2d62014-09-30 12:18:44 -070086 friend class GrTexturePriv;
87
88 typedef GrSurface INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000089};
90
bsalomon@google.com26e18b52013-03-29 19:22:36 +000091/**
92 * Represents a texture that is intended to be accessed in device coords with an offset.
93 */
94class GrDeviceCoordTexture {
95public:
96 GrDeviceCoordTexture() { fOffset.set(0, 0); }
97
98 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) {
99 *this = other;
100 }
101
102 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset)
103 : fTexture(SkSafeRef(texture))
104 , fOffset(offset) {
105 }
106
107 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) {
108 fTexture.reset(SkSafeRef(other.fTexture.get()));
109 fOffset = other.fOffset;
110 return *this;
111 }
112
113 const SkIPoint& offset() const { return fOffset; }
114
115 void setOffset(const SkIPoint& offset) { fOffset = offset; }
116 void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
117
118 GrTexture* texture() const { return fTexture.get(); }
119
120 GrTexture* setTexture(GrTexture* texture) {
121 fTexture.reset(SkSafeRef(texture));
122 return texture;
123 }
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000124
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000125private:
126 SkAutoTUnref<GrTexture> fTexture;
127 SkIPoint fOffset;
128};
129
reed@google.comac10a2d2010-12-22 21:39:39 +0000130#endif