blob: bf312851ab3bb33b81d4a0b116d1549f96eea352 [file] [log] [blame]
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkPictureShader.h"
9
10#include "SkBitmap.h"
11#include "SkBitmapProcShader.h"
12#include "SkCanvas.h"
13#include "SkMatrixUtils.h"
14#include "SkPicture.h"
15#include "SkReadBuffer.h"
16
17#if SK_SUPPORT_GPU
18#include "GrContext.h"
19#endif
20
21SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy)
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000022 : fPicture(SkRef(picture))
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000023 , fTmx(tmx)
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000024 , fTmy(tmy) { }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000025
26SkPictureShader::SkPictureShader(SkReadBuffer& buffer)
27 : INHERITED(buffer) {
28 fTmx = static_cast<SkShader::TileMode>(buffer.read32());
29 fTmy = static_cast<SkShader::TileMode>(buffer.read32());
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000030 fPicture = SkPicture::CreateFromBuffer(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000031}
32
33SkPictureShader::~SkPictureShader() {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000034 fPicture->unref();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000035}
36
37SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy) {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000038 if (!picture || 0 == picture->width() || 0 == picture->height()) {
39 return NULL;
40 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000041 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy));
42}
43
44void SkPictureShader::flatten(SkWriteBuffer& buffer) const {
45 this->INHERITED::flatten(buffer);
46
47 buffer.write32(fTmx);
48 buffer.write32(fTmy);
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000049 fPicture->flatten(buffer);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000050}
51
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000052bool SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const {
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +000053 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000054
55 SkMatrix m;
56 if (this->hasLocalMatrix()) {
57 m.setConcat(matrix, this->getLocalMatrix());
58 } else {
59 m = matrix;
60 }
61
62 // Use a rotation-invariant scale
63 SkPoint scale;
64 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) {
65 // Decomposition failed, use an approximation.
66 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()),
67 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()));
68 }
69 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height());
70
71 SkISize tileSize = scaledSize.toRound();
72 if (tileSize.isEmpty()) {
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000073 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000074 }
75
76 // The actual scale, compensating for rounding.
77 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(),
78 SkIntToScalar(tileSize.height()) / fPicture->height());
79
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000080 if (!fCachedShader || tileScale != fCachedTileScale) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000081 SkBitmap bm;
82 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000083 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000084 }
85 bm.eraseColor(SK_ColorTRANSPARENT);
86
87 SkCanvas canvas(bm);
88 canvas.scale(tileScale.width(), tileScale.height());
89 canvas.drawPicture(*fPicture);
90
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000091 fCachedShader.reset(CreateBitmapShader(bm, fTmx, fTmy));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000092 fCachedTileScale = tileScale;
93 }
94
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000095 SkMatrix shaderMatrix = this->getLocalMatrix();
96 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
97 fCachedShader->setLocalMatrix(shaderMatrix);
98
99 return true;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000100}
101
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000102bool SkPictureShader::setContext(const SkBitmap& device,
103 const SkPaint& paint,
104 const SkMatrix& matrix) {
105 if (!this->buildBitmapShader(matrix)) {
106 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000107 }
108
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000109 if (!this->INHERITED::setContext(device, paint, matrix)) {
110 return false;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000111 }
112
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000113 SkASSERT(fCachedShader);
114 if (!fCachedShader->setContext(device, paint, matrix)) {
115 this->INHERITED::endContext();
116 return false;
commit-bot@chromium.org68ea91a2014-04-17 21:02:29 +0000117 }
118
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000119 return true;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000120}
121
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000122void SkPictureShader::endContext() {
123 SkASSERT(fCachedShader);
124 fCachedShader->endContext();
125
126 this->INHERITED::endContext();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000127}
128
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000129uint32_t SkPictureShader::getFlags() {
130 if (NULL != fCachedShader) {
131 return fCachedShader->getFlags();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000132 }
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000133 return 0;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000134}
135
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000136SkShader::ShadeProc SkPictureShader::asAShadeProc(void** ctx) {
137 if (fCachedShader) {
138 return fCachedShader->asAShadeProc(ctx);
139 }
140 return NULL;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000141}
142
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000143void SkPictureShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
144 SkASSERT(fCachedShader);
145 fCachedShader->shadeSpan(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000146}
147
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000148void SkPictureShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) {
149 SkASSERT(fCachedShader);
150 fCachedShader->shadeSpan16(x, y, dstC, count);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000151}
152
153#ifndef SK_IGNORE_TO_STRING
154void SkPictureShader::toString(SkString* str) const {
155 static const char* gTileModeName[SkShader::kTileModeCount] = {
156 "clamp", "repeat", "mirror"
157 };
158
159 str->appendf("PictureShader: [%d:%d] ",
160 fPicture ? fPicture->width() : 0,
161 fPicture ? fPicture->height() : 0);
162
163 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]);
164
165 this->INHERITED::toString(str);
166}
167#endif
168
169#if SK_SUPPORT_GPU
170GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000171 if (!this->buildBitmapShader(context->getMatrix())) {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000172 return NULL;
173 }
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000174 SkASSERT(fCachedShader);
175 return fCachedShader->asNewEffect(context, paint);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000176}
177#endif