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

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

Issue 245963010: Move SkShader::fLocalMatrix into SkShader constructor. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add SkLocalMatrixShaderWrapper 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
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 16 matching lines...) Expand all
27 * shader, then the shader's color(s) are use instead, but they are 27 * shader, then the shader's color(s) are use instead, but they are
28 * modulated by the paint's alpha. This makes it easy to create a shader 28 * modulated by the paint's alpha. This makes it easy to create a shader
29 * once (e.g. bitmap tiling or gradient) and then change its transparency 29 * once (e.g. bitmap tiling or gradient) and then change its transparency
30 * w/o having to modify the original shader... only the paint's alpha needs 30 * w/o having to modify the original shader... only the paint's alpha needs
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(const SkMatrix* localMatrix = NULL);
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.
52 * @param localM The shader's new local matrix. 52 * @param localM The shader's new local matrix.
53 */ 53 */
54 void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; } 54 void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; }
scroggo 2014/04/24 17:02:53 Can we go ahead and put this function behind a fla
55 55
56 /** 56 /**
57 * Reset the shader's local matrix to identity. 57 * Reset the shader's local matrix to identity.
58 */ 58 */
59 void resetLocalMatrix() { fLocalMatrix.reset(); } 59 void resetLocalMatrix() { fLocalMatrix.reset(); }
60 60
61 enum TileMode { 61 enum TileMode {
62 /** replicate the edge color if the shader draws outside of its 62 /** replicate the edge color if the shader draws outside of its
63 * original bounds 63 * original bounds
64 */ 64 */
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 * 364 *
365 * If the src is kA8_Config then that mask will be colorized using the colo r on 365 * If the src is kA8_Config then that mask will be colorized using the colo r on
366 * the paint. 366 * the paint.
367 * 367 *
368 * @param src The bitmap to use inside the shader 368 * @param src The bitmap to use inside the shader
369 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection. 369 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection.
370 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection. 370 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection.
371 * @return Returns a new shader object. Note: this function never retur ns null. 371 * @return Returns a new shader object. Note: this function never retur ns null.
372 */ 372 */
373 static SkShader* CreateBitmapShader(const SkBitmap& src, 373 static SkShader* CreateBitmapShader(const SkBitmap& src,
374 TileMode tmx, TileMode tmy); 374 TileMode tmx, TileMode tmy,
375 const SkMatrix* localMatrix = NULL);
375 376
376 /** Call this to create a new shader that will draw with the specified pictu re. 377 /** Call this to create a new shader that will draw with the specified pictu re.
377 * 378 *
378 * @param src The picture to use inside the shader (if not NULL, its ref c ount 379 * @param src The picture to use inside the shader (if not NULL, its ref c ount
379 * is incremented). The SkPicture must not be changed after 380 * is incremented). The SkPicture must not be changed after
380 * successfully creating a picture shader. 381 * successfully creating a picture shader.
381 * FIXME: src cannot be const due to SkCanvas::drawPicture 382 * FIXME: src cannot be const due to SkCanvas::drawPicture
382 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection. 383 * @param tmx The tiling mode to use when sampling the bitmap in the x-dir ection.
383 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection. 384 * @param tmy The tiling mode to use when sampling the bitmap in the y-dir ection.
384 * @return Returns a new shader object. Note: this function never retur ns null. 385 * @return Returns a new shader object. Note: this function never retur ns null.
385 */ 386 */
386 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy); 387 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy);
387 388
389 /** Call this to create a shader wrapper with a new local matrix. It behaves like the
390 * original shader but with the local matrix replaced.
391 *
392 * @param shader The shader being wrapped.
393 * @param localMatrix The new local matrix to replace the original shader' s one.
394 * @return Returns a new shader object. Note: this function nev er returns null.
scroggo 2014/04/24 17:02:53 What if the caller calls with a NULL shader?
Dominik Grewe 2014/04/24 17:16:47 Good point. I currently assert that it's not NULL
395 */
396 static SkShader* CreateLocalMatrixWrapper(SkShader* shader, const SkMatrix& localMatrix);
397
388 SK_TO_STRING_VIRT() 398 SK_TO_STRING_VIRT()
389 SK_DEFINE_FLATTENABLE_TYPE(SkShader) 399 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
390 400
391 protected: 401 protected:
392 402
393 SkShader(SkReadBuffer& ); 403 SkShader(SkReadBuffer& );
394 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 404 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
395 405
396 private: 406 private:
397 SkMatrix fLocalMatrix; 407 SkMatrix fLocalMatrix;
398 408
399 bool computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInverse) con st; 409 bool computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInverse) con st;
400 410
401 typedef SkFlattenable INHERITED; 411 typedef SkFlattenable INHERITED;
402 }; 412 };
403 413
414
415 /**
416 * Wrapper class for SkShader that allows us to overwrite the local matrix
417 * without having to create the shader again.
418 */
419 class SK_API SkLocalMatrixShaderWrapper : public SkShader {
420 public:
421 static SkLocalMatrixShaderWrapper* Create(SkShader* shader, const SkMatrix& localMatrix) {
422 return SkNEW_ARGS(SkLocalMatrixShaderWrapper, (shader, localMatrix));
423 }
424
425 // Forward all methods to the wrapped shader, apart from hasLocalMatrix and
426 // getLocalMatrix which use the wrapper's local matrix.
427 virtual bool isOpaque() const SK_OVERRIDE { return fShader->isOpaque(); }
428 virtual bool validContext(const SkBitmap& device,
429 const SkPaint& paint,
430 const SkMatrix& matrix,
431 SkMatrix* totalInverse = NULL) const SK_OVERRIDE {
432 return fShader->validContext(device, paint, matrix, totalInverse);
433 }
434 virtual Context* createContext(const SkBitmap& device,
435 const SkPaint& paint,
436 const SkMatrix& matrix,
437 void* storage) const SK_OVERRIDE {
438 return fShader->createContext(device, paint, matrix, storage);
439 }
440 virtual size_t contextSize() const SK_OVERRIDE { return fShader->contextSize (); }
441 virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
442 TileMode xy[2]) const SK_OVERRIDE {
443 return fShader->asABitmap(outTexture, outMatrix, xy);
444 }
445 virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
446 return fShader->asAGradient(info);
447 }
448 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) c onst SK_OVERRIDE {
449 return fShader->asNewEffect(context, paint);
scroggo 2014/04/24 17:02:53 This function may call getLocalMatrix (e.g. SkBitm
Dominik Grewe 2014/04/24 17:16:47 Thinking about this, I'm not sure what I've done h
450 }
451
452 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShaderWrapp er);
453
454 protected:
455 SkLocalMatrixShaderWrapper(SkShader* shader, const SkMatrix& localMatrix);
scroggo 2014/04/24 17:02:53 Any reason not to make this private?
Dominik Grewe 2014/04/24 17:16:47 As long as we never have any subclasses we can mak
scroggo 2014/04/24 18:24:47 My vote would be to make it private unless/until w
456 SkLocalMatrixShaderWrapper(SkReadBuffer& buffer);
457 virtual void flatten(SkWriteBuffer& buffer);
458
459 private:
460 SkAutoTUnref<SkShader> fShader;
461
462 typedef SkShader INHERITED;
463 };
464
404 #endif 465 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698