blob: fb0620d6e6df097ca1f15ef7f1961b1c22373aef [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 2010 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
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000011#ifndef GrEffectStage_DEFINED
12#define GrEffectStage_DEFINED
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000014#include "GrBackendEffectFactory.h"
bsalomon@google.coma469c282012-10-24 18:28:34 +000015#include "GrEffect.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000016#include "SkMatrix.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000017#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.comb8670992012-07-25 21:27:09 +000019#include "SkShader.h"
20
bsalomon@google.com08283af2012-10-26 13:01:20 +000021class GrEffectStage {
reed@google.comac10a2d2010-12-22 21:39:39 +000022public:
bsalomon97b9ab72014-07-08 06:52:35 -070023 explicit GrEffectStage(const GrEffect* effect, int attrIndex0 = -1, int attrIndex1 = -1)
24 : fEffect(SkRef(effect)) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000025 fCoordChangeMatrixSet = false;
26 fVertexAttribIndices[0] = attrIndex0;
27 fVertexAttribIndices[1] = attrIndex1;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000028 }
29
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000030 GrEffectStage(const GrEffectStage& other) {
31 *this = other;
32 }
33
34 GrEffectStage& operator= (const GrEffectStage& other) {
35 fCoordChangeMatrixSet = other.fCoordChangeMatrixSet;
36 if (other.fCoordChangeMatrixSet) {
37 fCoordChangeMatrix = other.fCoordChangeMatrix;
38 }
bsalomon97b9ab72014-07-08 06:52:35 -070039 fEffect.reset(SkRef(other.fEffect.get()));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000040 memcpy(fVertexAttribIndices, other.fVertexAttribIndices, sizeof(fVertexAttribIndices));
41 return *this;
42 }
43
44 bool operator== (const GrEffectStage& other) const {
bsalomon97b9ab72014-07-08 06:52:35 -070045 SkASSERT(NULL != fEffect.get());
46 SkASSERT(NULL != other.fEffect.get());
bsalomon@google.com288d9542012-10-17 12:53:54 +000047
bsalomonf99f8842014-07-07 11:54:23 -070048 if (!this->getEffect()->isEqual(*other.getEffect())) {
bsalomon@google.com288d9542012-10-17 12:53:54 +000049 return false;
50 }
51
robertphillips@google.com6ef912a2013-04-25 17:23:46 +000052 if (fCoordChangeMatrixSet != other.fCoordChangeMatrixSet) {
53 return false;
54 }
55
56 if (!fCoordChangeMatrixSet) {
57 return true;
58 }
59
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000060 return fCoordChangeMatrix == other.fCoordChangeMatrix;
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000061 }
bsalomon@google.com288d9542012-10-17 12:53:54 +000062
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +000063 bool operator!= (const GrEffectStage& s) const { return !(*this == s); }
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000064
bsalomon@google.com288d9542012-10-17 12:53:54 +000065 /**
66 * This is called when the coordinate system in which the geometry is specified will change.
67 *
bsalomon@google.comc7818882013-03-20 19:19:53 +000068 * @param matrix The transformation from the old coord system in which geometry is specified
69 * to the new one from which it will actually be drawn.
bsalomon@google.com288d9542012-10-17 12:53:54 +000070 */
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +000071 void localCoordChange(const SkMatrix& matrix) {
robertphillips@google.com6ef912a2013-04-25 17:23:46 +000072 if (fCoordChangeMatrixSet) {
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +000073 fCoordChangeMatrix.preConcat(matrix);
robertphillips@google.com6ef912a2013-04-25 17:23:46 +000074 } else {
75 fCoordChangeMatrixSet = true;
76 fCoordChangeMatrix = matrix;
77 }
78 }
bsalomon@google.com288d9542012-10-17 12:53:54 +000079
80 class SavedCoordChange {
81 private:
robertphillips@google.com6ef912a2013-04-25 17:23:46 +000082 bool fCoordChangeMatrixSet;
bsalomon@google.comb9086a02012-11-01 18:02:54 +000083 SkMatrix fCoordChangeMatrix;
bsalomon97b9ab72014-07-08 06:52:35 -070084 SkDEBUGCODE(mutable SkAutoTUnref<const GrEffect> fEffect;)
bsalomon@google.com288d9542012-10-17 12:53:54 +000085
bsalomon@google.com08283af2012-10-26 13:01:20 +000086 friend class GrEffectStage;
bsalomon@google.com288d9542012-10-17 12:53:54 +000087 };
88
89 /**
90 * This gets the current coordinate system change. It is the accumulation of
bsalomon@google.comc7818882013-03-20 19:19:53 +000091 * localCoordChange calls since the effect was installed. It is used when then caller
bsalomon@google.com288d9542012-10-17 12:53:54 +000092 * wants to temporarily change the source geometry coord system, draw something, and then
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000093 * restore the previous coord system (e.g. temporarily draw in device coords).
bsalomon@google.com288d9542012-10-17 12:53:54 +000094 */
95 void saveCoordChange(SavedCoordChange* savedCoordChange) const {
robertphillips@google.com6ef912a2013-04-25 17:23:46 +000096 savedCoordChange->fCoordChangeMatrixSet = fCoordChangeMatrixSet;
97 if (fCoordChangeMatrixSet) {
98 savedCoordChange->fCoordChangeMatrix = fCoordChangeMatrix;
99 }
bsalomon97b9ab72014-07-08 06:52:35 -0700100 SkASSERT(NULL == savedCoordChange->fEffect.get());
101 SkDEBUGCODE(SkRef(fEffect.get());)
102 SkDEBUGCODE(savedCoordChange->fEffect.reset(fEffect.get());)
bsalomon@google.com288d9542012-10-17 12:53:54 +0000103 }
104
105 /**
106 * This balances the saveCoordChange call.
107 */
108 void restoreCoordChange(const SavedCoordChange& savedCoordChange) {
robertphillips@google.com6ef912a2013-04-25 17:23:46 +0000109 fCoordChangeMatrixSet = savedCoordChange.fCoordChangeMatrixSet;
110 if (fCoordChangeMatrixSet) {
111 fCoordChangeMatrix = savedCoordChange.fCoordChangeMatrix;
112 }
bsalomon97b9ab72014-07-08 06:52:35 -0700113 SkASSERT(savedCoordChange.fEffect.get() == fEffect);
114 SkDEBUGCODE(savedCoordChange.fEffect.reset(NULL);)
bsalomon@google.com288d9542012-10-17 12:53:54 +0000115 }
116
117 /**
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000118 * Gets the matrix representing all changes of coordinate system since the GrEffect was
119 * installed in the stage.
120 */
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000121 const SkMatrix& getCoordChangeMatrix() const {
robertphillips@google.com6ef912a2013-04-25 17:23:46 +0000122 if (fCoordChangeMatrixSet) {
123 return fCoordChangeMatrix;
124 } else {
skia.committer@gmail.com2cf444f2013-04-26 07:00:58 +0000125 return SkMatrix::I();
robertphillips@google.com6ef912a2013-04-25 17:23:46 +0000126 }
127 }
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000128
bsalomon97b9ab72014-07-08 06:52:35 -0700129 const GrEffect* getEffect() const { return fEffect.get(); }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000130
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000131 const int* getVertexAttribIndices() const { return fVertexAttribIndices; }
bsalomon97b9ab72014-07-08 06:52:35 -0700132 int getVertexAttribIndexCount() const { return fEffect->numVertexAttribs(); }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000133
reed@google.comac10a2d2010-12-22 21:39:39 +0000134private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000135 bool fCoordChangeMatrixSet;
136 SkMatrix fCoordChangeMatrix;
bsalomon97b9ab72014-07-08 06:52:35 -0700137 SkAutoTUnref<const GrEffect> fEffect;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000138 int fVertexAttribIndices[2];
reed@google.comac10a2d2010-12-22 21:39:39 +0000139};
140
141#endif