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

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

Issue 246403013: Revert of 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 (after setContext) if the spans only vary in X (const in Y). 99 /** set 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 /**
124 * Returns true if the shader is guaranteed to produce only opaque 115 * Returns true if the shader is guaranteed to produce only opaque
125 * colors, subject to the SkPaint using the shader to apply an opaque 116 * colors, subject to the SkPaint using the shader to apply an opaque
126 * alpha value. Subclasses should override this to allow some 117 * alpha value. Subclasses should override this to allow some
127 * optimizations. isOpaque() can be called at any time, unlike getFlags, 118 * optimizations.
128 * which only works properly when the context is set.
129 */ 119 */
130 virtual bool isOpaque() const { return false; } 120 virtual bool isOpaque() const { return false; }
131 121
132 /** 122 class Context : public ::SkNoncopyable {
133 * Return the alpha associated with the data returned by shadeSpan16(). If 123 public:
134 * kHasSpan16_Flag is not set, this value is meaningless. 124 Context(const SkShader& shader, const SkBitmap& device,
135 */ 125 const SkPaint& paint, const SkMatrix& matrix);
136 virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; } 126
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 };
137 198
138 /** 199 /**
139 * Called once before drawing, with the current paint and device matrix. 200 * Subclasses should be sure to call their INHERITED::validContext() if
140 * Return true if your shader supports these parameters, or false if not. 201 * they override this method.
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.
147 */ 202 */
148 virtual bool setContext(const SkBitmap& device, const SkPaint& paint, 203 virtual bool validContext(const SkBitmap& device, const SkPaint& paint,
149 const SkMatrix& matrix); 204 const SkMatrix& matrix, SkMatrix* totalInverse = N ULL) const;
150 205
151 /** 206 /**
152 * Assuming setContext returned true, endContext() will be called when 207 * Create the actual object that does the shading.
153 * the draw using the shader has completed. It is an error for setContext 208 * Returns NULL if validContext() returns false.
154 * to be called twice w/o an intervening call to endContext(). 209 * Size of storage must be >= contextSize.
155 *
156 * Subclasses should be sure to call their INHERITED::endContext() if they
157 * override this method.
158 */ 210 */
159 virtual void endContext(); 211 virtual Context* createContext(const SkBitmap& device,
160 212 const SkPaint& paint,
161 SkDEBUGCODE(bool setContextHasBeenCalled() const { return SkToBool(fInSetCon text); }) 213 const SkMatrix& matrix,
214 void* storage) const = 0;
162 215
163 /** 216 /**
164 * Called for each span of the object being drawn. Your subclass should 217 * Return the size of a Context returned by createContext.
165 * set the appropriate colors (with premultiplied alpha) that correspond
166 * to the specified device coordinates.
167 */ 218 */
168 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; 219 virtual size_t contextSize() const = 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 }
193 220
194 /** 221 /**
195 * Helper to check the flags to know if it is legal to call shadeSpan16() 222 * Helper to check the flags to know if it is legal to call shadeSpan16()
196 */ 223 */
197 static bool CanCallShadeSpan16(uint32_t flags) { 224 static bool CanCallShadeSpan16(uint32_t flags) {
198 return (flags & kHasSpan16_Flag) != 0; 225 return (flags & kHasSpan16_Flag) != 0;
199 } 226 }
200 227
201 /** 228 /**
202 Gives method bitmap should be read to implement a shader. 229 Gives method bitmap should be read to implement a shader.
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 uint32_t fGradientFlags; //!< see SkGradientShader::Flags 342 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
316 }; 343 };
317 344
318 virtual GradientType asAGradient(GradientInfo* info) const; 345 virtual GradientType asAGradient(GradientInfo* info) const;
319 346
320 /** 347 /**
321 * If the shader subclass has a GrEffect implementation, this resturns the effect to install. 348 * If the shader subclass has a GrEffect implementation, this resturns the effect to install.
322 * The incoming color to the effect has r=g=b=a all extracted from the SkPa int's alpha. 349 * The incoming color to the effect has r=g=b=a all extracted from the SkPa int's alpha.
323 * The output color should be the computed SkShader premul color modulated by the incoming 350 * The output color should be the computed SkShader premul color modulated by the incoming
324 * color. The GrContext may be used by the effect to create textures. The G PU device does not 351 * color. The GrContext may be used by the effect to create textures. The G PU device does not
325 * call setContext. Instead we pass the SkPaint here in case the shader nee ds paint info. 352 * call createContext. Instead we pass the SkPaint here in case the shader needs paint info.
326 */ 353 */
327 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) c onst; 354 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) c onst;
328 355
329 ////////////////////////////////////////////////////////////////////////// 356 //////////////////////////////////////////////////////////////////////////
330 // Factory methods for stock shaders 357 // Factory methods for stock shaders
331 358
332 /** Call this to create a new shader that will draw with the specified bitma p. 359 /** Call this to create a new shader that will draw with the specified bitma p.
333 * 360 *
334 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions 361 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
335 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader 362 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
(...skipping 19 matching lines...) Expand all
355 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection. 382 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection.
356 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection. 383 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection.
357 * @return Returns a new shader object. Note: this function never retur ns null. 384 * @return Returns a new shader object. Note: this function never retur ns null.
358 */ 385 */
359 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy); 386 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy);
360 387
361 SK_TO_STRING_VIRT() 388 SK_TO_STRING_VIRT()
362 SK_DEFINE_FLATTENABLE_TYPE(SkShader) 389 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
363 390
364 protected: 391 protected:
365 enum MatrixClass {
366 kLinear_MatrixClass, // no perspective
367 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedS tepInX() each scanline
368 kPerspective_MatrixClass // slow perspective, need to mappoints e ach pixel
369 };
370 static MatrixClass ComputeMatrixClass(const SkMatrix&);
371
372 // These can be called by your subclass after setContext() has been called
373 uint8_t getPaintAlpha() const { return fPaintAlpha; }
374 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
375 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInve rseClass; }
376 392
377 SkShader(SkReadBuffer& ); 393 SkShader(SkReadBuffer& );
378 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 394 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
395
379 private: 396 private:
380 SkMatrix fLocalMatrix; 397 SkMatrix fLocalMatrix;
381 SkMatrix fTotalInverse; 398
382 uint8_t fPaintAlpha; 399 bool computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInverse) con st;
383 uint8_t fTotalInverseClass;
384 SkDEBUGCODE(SkBool8 fInSetContext;)
385 400
386 typedef SkFlattenable INHERITED; 401 typedef SkFlattenable INHERITED;
387 }; 402 };
388 403
389 #endif 404 #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