Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: include/core/SkShader.h

Issue 241283003: Revert of Extract most of the mutable state of SkShader into a separate Context object. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkEmptyShader.h ('k') | include/effects/SkPerlinNoiseShader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #ifndef SkShader_DEFINED 9 #ifndef SkShader_DEFINED
10 #define SkShader_DEFINED 10 #define SkShader_DEFINED
(...skipping 20 matching lines...) Expand all
31 * to be modified. 31 * to be modified.
32 */ 32 */
33 class SK_API SkShader : public SkFlattenable { 33 class SK_API SkShader : public SkFlattenable {
34 public: 34 public:
35 SK_DECLARE_INST_COUNT(SkShader) 35 SK_DECLARE_INST_COUNT(SkShader)
36 36
37 SkShader(); 37 SkShader();
38 virtual ~SkShader(); 38 virtual ~SkShader();
39 39
40 /** 40 /**
41 * Returns true if the local matrix is not an identity matrix. 41 * Returns true if the local matrix is not an identity matrix.
42 */ 42 */
43 bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); } 43 bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
44 44
45 /** 45 /**
46 * Returns the local matrix. 46 * Returns the local matrix.
47 */ 47 */
48 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; } 48 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
49 49
50 /** 50 /**
51 * Set the shader's local matrix. 51 * Set the shader's local matrix.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 //! set if this shader's shadeSpan16() method can be called 89 //! set if this shader's shadeSpan16() method can be called
90 kHasSpan16_Flag = 0x02, 90 kHasSpan16_Flag = 0x02,
91 91
92 /** Set this bit if the shader's native data type is instrinsically 16 92 /** Set this bit if the shader's native data type is instrinsically 16
93 bit, meaning that calling the 32bit shadeSpan() entry point will 93 bit, meaning that calling the 32bit shadeSpan() entry point will
94 mean the the impl has to up-sample 16bit data into 32bit. Used as a 94 mean the the impl has to up-sample 16bit data into 32bit. Used as a
95 a means of clearing a dither request if the it will have no effect 95 a means of clearing a dither request if the it will have no effect
96 */ 96 */
97 kIntrinsicly16_Flag = 0x04, 97 kIntrinsicly16_Flag = 0x04,
98 98
99 /** set if the spans only vary in X (const in Y). 99 /** set (after setContext) if the spans only vary in X (const in Y).
100 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient 100 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
101 that varies from left-to-right. This flag specifies this for 101 that varies from left-to-right. This flag specifies this for
102 shadeSpan(). 102 shadeSpan().
103 */ 103 */
104 kConstInY32_Flag = 0x08, 104 kConstInY32_Flag = 0x08,
105 105
106 /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16 106 /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16
107 which may not always be the case, since shadeSpan16 may be 107 which may not always be the case, since shadeSpan16 may be
108 predithered, which would mean it was not const in Y, even though 108 predithered, which would mean it was not const in Y, even though
109 the 32bit shadeSpan() would be const. 109 the 32bit shadeSpan() would be const.
110 */ 110 */
111 kConstInY16_Flag = 0x10 111 kConstInY16_Flag = 0x10
112 }; 112 };
113 113
114 /** 114 /**
115 * Called sometimes before drawing with this shader. Return the type of
116 * alpha your shader will return. The default implementation returns 0.
117 * Your subclass should override if it can (even sometimes) report a
118 * non-zero value, since that will enable various blitters to perform
119 * faster.
120 */
121 virtual uint32_t getFlags() { return 0; }
122
123 /**
115 * Returns true if the shader is guaranteed to produce only opaque 124 * Returns true if the shader is guaranteed to produce only opaque
116 * colors, subject to the SkPaint using the shader to apply an opaque 125 * colors, subject to the SkPaint using the shader to apply an opaque
117 * alpha value. Subclasses should override this to allow some 126 * alpha value. Subclasses should override this to allow some
118 * optimizations. 127 * optimizations. isOpaque() can be called at any time, unlike getFlags,
128 * which only works properly when the context is set.
119 */ 129 */
120 virtual bool isOpaque() const { return false; } 130 virtual bool isOpaque() const { return false; }
121 131
122 class Context : public ::SkNoncopyable { 132 /**
123 public: 133 * Return the alpha associated with the data returned by shadeSpan16(). If
124 Context(const SkShader& shader, const SkBitmap& device, 134 * kHasSpan16_Flag is not set, this value is meaningless.
125 const SkPaint& paint, const SkMatrix& matrix); 135 */
126 136 virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
127 virtual ~Context();
128
129 /**
130 * Called sometimes before drawing with this shader. Return the type of
131 * alpha your shader will return. The default implementation returns 0.
132 * Your subclass should override if it can (even sometimes) report a
133 * non-zero value, since that will enable various blitters to perform
134 * faster.
135 */
136 virtual uint32_t getFlags() const { return 0; }
137
138 /**
139 * Return the alpha associated with the data returned by shadeSpan16(). If
140 * kHasSpan16_Flag is not set, this value is meaningless.
141 */
142 virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
143
144 /**
145 * Called for each span of the object being drawn. Your subclass should
146 * set the appropriate colors (with premultiplied alpha) that correspon d
147 * to the specified device coordinates.
148 */
149 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
150
151 typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int coun t);
152 virtual ShadeProc asAShadeProc(void** ctx);
153
154 /**
155 * Called only for 16bit devices when getFlags() returns
156 * kOpaqueAlphaFlag | kHasSpan16_Flag
157 */
158 virtual void shadeSpan16(int x, int y, uint16_t[], int count);
159
160 /**
161 * Similar to shadeSpan, but only returns the alpha-channel for a span.
162 * The default implementation calls shadeSpan() and then extracts the a lpha
163 * values from the returned colors.
164 */
165 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
166
167 /**
168 * Helper function that returns true if this shader's shadeSpan16() met hod
169 * can be called.
170 */
171 bool canCallShadeSpan16() {
172 return SkShader::CanCallShadeSpan16(this->getFlags());
173 }
174
175 protected:
176 // Reference to shader, so we don't have to dupe information.
177 const SkShader& fShader;
178
179 enum MatrixClass {
180 kLinear_MatrixClass, // no perspective
181 kFixedStepInX_MatrixClass, // fast perspective, need to call fi xedStepInX() each
182 // scanline
183 kPerspective_MatrixClass // slow perspective, need to mappoin ts each pixel
184 };
185 static MatrixClass ComputeMatrixClass(const SkMatrix&);
186
187 uint8_t getPaintAlpha() const { return fPaintAlpha; }
188 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
189 MatrixClass getInverseClass() const { return (MatrixClass)fTotal InverseClass; }
190
191 private:
192 SkMatrix fTotalInverse;
193 uint8_t fPaintAlpha;
194 uint8_t fTotalInverseClass;
195
196 typedef SkNoncopyable INHERITED;
197 };
198 137
199 /** 138 /**
200 * Subclasses should be sure to call their INHERITED::validContext() if 139 * Called once before drawing, with the current paint and device matrix.
201 * they override this method. 140 * Return true if your shader supports these parameters, or false if not.
141 * If false is returned, nothing will be drawn. If true is returned, then
142 * a balancing call to endContext() will be made before the next call to
143 * setContext.
144 *
145 * Subclasses should be sure to call their INHERITED::setContext() if they
146 * override this method.
202 */ 147 */
203 virtual bool validContext(const SkBitmap& device, const SkPaint& paint, 148 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
204 const SkMatrix& matrix, SkMatrix* totalInverse = N ULL) const; 149 const SkMatrix& matrix);
205 150
206 /** 151 /**
207 * Create the actual object that does the shading. 152 * Assuming setContext returned true, endContext() will be called when
208 * Returns NULL if validContext() returns false. 153 * the draw using the shader has completed. It is an error for setContext
209 * Size of storage must be >= contextSize. 154 * to be called twice w/o an intervening call to endContext().
155 *
156 * Subclasses should be sure to call their INHERITED::endContext() if they
157 * override this method.
210 */ 158 */
211 virtual Context* createContext(const SkBitmap& device, 159 virtual void endContext();
212 const SkPaint& paint, 160
213 const SkMatrix& matrix, 161 SkDEBUGCODE(bool setContextHasBeenCalled() const { return SkToBool(fInSetCon text); })
214 void* storage) const = 0;
215 162
216 /** 163 /**
217 * Return the size of a Context returned by createContext. 164 * Called for each span of the object being drawn. Your subclass should
165 * set the appropriate colors (with premultiplied alpha) that correspond
166 * to the specified device coordinates.
218 */ 167 */
219 virtual size_t contextSize() const = 0; 168 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
169
170 typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count);
171 virtual ShadeProc asAShadeProc(void** ctx);
172
173 /**
174 * Called only for 16bit devices when getFlags() returns
175 * kOpaqueAlphaFlag | kHasSpan16_Flag
176 */
177 virtual void shadeSpan16(int x, int y, uint16_t[], int count);
178
179 /**
180 * Similar to shadeSpan, but only returns the alpha-channel for a span.
181 * The default implementation calls shadeSpan() and then extracts the alpha
182 * values from the returned colors.
183 */
184 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
185
186 /**
187 * Helper function that returns true if this shader's shadeSpan16() method
188 * can be called.
189 */
190 bool canCallShadeSpan16() {
191 return SkShader::CanCallShadeSpan16(this->getFlags());
192 }
220 193
221 /** 194 /**
222 * Helper to check the flags to know if it is legal to call shadeSpan16() 195 * Helper to check the flags to know if it is legal to call shadeSpan16()
223 */ 196 */
224 static bool CanCallShadeSpan16(uint32_t flags) { 197 static bool CanCallShadeSpan16(uint32_t flags) {
225 return (flags & kHasSpan16_Flag) != 0; 198 return (flags & kHasSpan16_Flag) != 0;
226 } 199 }
227 200
228 /** 201 /**
229 Gives method bitmap should be read to implement a shader. 202 Gives method bitmap should be read to implement a shader.
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 uint32_t fGradientFlags; //!< see SkGradientShader::Flags 315 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
343 }; 316 };
344 317
345 virtual GradientType asAGradient(GradientInfo* info) const; 318 virtual GradientType asAGradient(GradientInfo* info) const;
346 319
347 /** 320 /**
348 * If the shader subclass has a GrEffect implementation, this resturns the effect to install. 321 * If the shader subclass has a GrEffect implementation, this resturns the effect to install.
349 * The incoming color to the effect has r=g=b=a all extracted from the SkPa int's alpha. 322 * The incoming color to the effect has r=g=b=a all extracted from the SkPa int's alpha.
350 * The output color should be the computed SkShader premul color modulated by the incoming 323 * The output color should be the computed SkShader premul color modulated by the incoming
351 * color. The GrContext may be used by the effect to create textures. The G PU device does not 324 * color. The GrContext may be used by the effect to create textures. The G PU device does not
352 * call createContext. Instead we pass the SkPaint here in case the shader needs paint info. 325 * call setContext. Instead we pass the SkPaint here in case the shader nee ds paint info.
353 */ 326 */
354 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) c onst; 327 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) c onst;
355 328
356 ////////////////////////////////////////////////////////////////////////// 329 //////////////////////////////////////////////////////////////////////////
357 // Factory methods for stock shaders 330 // Factory methods for stock shaders
358 331
359 /** Call this to create a new shader that will draw with the specified bitma p. 332 /** Call this to create a new shader that will draw with the specified bitma p.
360 * 333 *
361 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions 334 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
362 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader 335 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
(...skipping 17 matching lines...) Expand all
380 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection. 353 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection.
381 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection. 354 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection.
382 * @return Returns a new shader object. Note: this function never retur ns null. 355 * @return Returns a new shader object. Note: this function never retur ns null.
383 */ 356 */
384 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy); 357 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy);
385 358
386 SK_TO_STRING_VIRT() 359 SK_TO_STRING_VIRT()
387 SK_DEFINE_FLATTENABLE_TYPE(SkShader) 360 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
388 361
389 protected: 362 protected:
363 enum MatrixClass {
364 kLinear_MatrixClass, // no perspective
365 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedS tepInX() each scanline
366 kPerspective_MatrixClass // slow perspective, need to mappoints e ach pixel
367 };
368 static MatrixClass ComputeMatrixClass(const SkMatrix&);
369
370 // These can be called by your subclass after setContext() has been called
371 uint8_t getPaintAlpha() const { return fPaintAlpha; }
372 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
373 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInve rseClass; }
390 374
391 SkShader(SkReadBuffer& ); 375 SkShader(SkReadBuffer& );
392 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 376 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
393
394 private: 377 private:
395 SkMatrix fLocalMatrix; 378 SkMatrix fLocalMatrix;
396 379 SkMatrix fTotalInverse;
397 bool computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInverse) con st; 380 uint8_t fPaintAlpha;
381 uint8_t fTotalInverseClass;
382 SkDEBUGCODE(SkBool8 fInSetContext;)
398 383
399 typedef SkFlattenable INHERITED; 384 typedef SkFlattenable INHERITED;
400 }; 385 };
401 386
402 #endif 387 #endif
OLDNEW
« no previous file with comments | « include/core/SkEmptyShader.h ('k') | include/effects/SkPerlinNoiseShader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698