blob: 63a76336482f9f32ddba4c092e476a81a6b64b92 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
robertphillips@google.com53238bc2013-08-30 13:12:10 +00007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkDevice.h"
reede010f1c2014-09-17 10:49:38 -07009#include "SkDeviceProperties.h"
dandovecfff212014-08-04 10:02:00 -070010#include "SkDraw.h"
reed@google.coma7d94852011-03-30 21:23:07 +000011#include "SkMetaData.h"
dandovecfff212014-08-04 10:02:00 -070012#include "SkPatchUtils.h"
fmalita84833262014-09-19 11:40:51 -070013#include "SkShader.h"
fmalitaaa1b9122014-08-28 14:32:24 -070014#include "SkTextBlob.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000016SkBaseDevice::SkBaseDevice()
reed3716fd02014-09-21 09:39:55 -070017 : fLeakyProperties(SkNEW_ARGS(SkDeviceProperties, (SkDeviceProperties::kLegacyLCD_InitType)))
bungeman@google.com532470f2013-01-22 19:25:14 +000018#ifdef SK_DEBUG
19 , fAttachedToCanvas(false)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +000020#endif
21{
reed@google.comaf951c92011-06-16 19:10:39 +000022 fOrigin.setZero();
23 fMetaData = NULL;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000024}
25
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000026SkBaseDevice::~SkBaseDevice() {
reede010f1c2014-09-17 10:49:38 -070027 SkDELETE(fLeakyProperties);
28 SkDELETE(fMetaData);
reed@google.coma7d94852011-03-30 21:23:07 +000029}
30
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000031SkBaseDevice* SkBaseDevice::createCompatibleDevice(const SkImageInfo& info) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000032 return this->onCreateDevice(info, kGeneral_Usage);
33}
34
35SkBaseDevice* SkBaseDevice::createCompatibleDeviceForSaveLayer(const SkImageInfo& info) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000036 return this->onCreateDevice(info, kSaveLayer_Usage);
37}
38
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000039SkMetaData& SkBaseDevice::getMetaData() {
reed@google.coma7d94852011-03-30 21:23:07 +000040 // metadata users are rare, so we lazily allocate it. If that changes we
41 // can decide to just make it a field in the device (rather than a ptr)
42 if (NULL == fMetaData) {
43 fMetaData = new SkMetaData;
44 }
45 return *fMetaData;
46}
47
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000048SkImageInfo SkBaseDevice::imageInfo() const {
reedf252f642014-06-14 04:24:56 -070049 return SkImageInfo::MakeUnknown();
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000050}
51
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000052const SkBitmap& SkBaseDevice::accessBitmap(bool changePixels) {
53 const SkBitmap& bitmap = this->onAccessBitmap();
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 if (changePixels) {
junov@chromium.org1f9767c2012-02-07 16:27:57 +000055 bitmap.notifyPixelsChanged();
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 }
junov@chromium.org1f9767c2012-02-07 16:27:57 +000057 return bitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000058}
59
reed3716fd02014-09-21 09:39:55 -070060void SkBaseDevice::setPixelGeometry(SkPixelGeometry geo) {
61 fLeakyProperties->fPixelGeometry = geo;
62}
63
64SkSurface* SkBaseDevice::newSurface(const SkImageInfo&, const SkSurfaceProps&) { return NULL; }
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000065
66const void* SkBaseDevice::peekPixels(SkImageInfo*, size_t*) { return NULL; }
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +000067
68void SkBaseDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
69 const SkRRect& inner, const SkPaint& paint) {
70 SkPath path;
71 path.addRRect(outer);
72 path.addRRect(inner);
73 path.setFillType(SkPath::kEvenOdd_FillType);
74
75 const SkMatrix* preMatrix = NULL;
76 const bool pathIsMutable = true;
77 this->drawPath(draw, path, paint, preMatrix, pathIsMutable);
78}
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000079
dandovb3c9d1c2014-08-12 08:34:29 -070080void SkBaseDevice::drawPatch(const SkDraw& draw, const SkPoint cubics[12], const SkColor colors[4],
81 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
82 SkPatchUtils::VertexData data;
dandovecfff212014-08-04 10:02:00 -070083
dandovb3c9d1c2014-08-12 08:34:29 -070084 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, draw.fMatrix);
dandovecfff212014-08-04 10:02:00 -070085
86 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
dandov45f78422014-08-15 06:06:47 -070087 // If it fails to generate the vertices, then we do not draw.
88 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
89 this->drawVertices(draw, SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
90 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
91 paint);
92 }
dandovecfff212014-08-04 10:02:00 -070093}
94
fmalitaaa1b9122014-08-28 14:32:24 -070095void SkBaseDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
96 const SkPaint &paint) {
97
fmalita84833262014-09-19 11:40:51 -070098 SkPaint runPaint = paint;
fmalitaaa1b9122014-08-28 14:32:24 -070099 SkMatrix localMatrix;
100 SkDraw localDraw(draw);
101
102 if (x || y) {
103 localMatrix = *draw.fMatrix;
104 localMatrix.preTranslate(x, y);
105 localDraw.fMatrix = &localMatrix;
fmalita84833262014-09-19 11:40:51 -0700106
107 if (paint.getShader()) {
108 // FIXME: We need to compensate for the translate above. This is suboptimal but
109 // temporary -- until we get proper derived class drawTextBlob implementations.
110
111 // TODO: pass x,y down to the other methods so they can handle the additional
112 // translate without needing to allocate a new shader.
113 SkMatrix shaderMatrix;
114 shaderMatrix.setTranslate(-x, -y);
115 SkAutoTUnref<SkShader> wrapper(
116 SkShader::CreateLocalMatrixShader(paint.getShader(), shaderMatrix));
117 runPaint.setShader(wrapper);
118 }
fmalitaaa1b9122014-08-28 14:32:24 -0700119 }
120
fmalitaaa1b9122014-08-28 14:32:24 -0700121 SkTextBlob::RunIterator it(blob);
122 while (!it.done()) {
123 size_t textLen = it.glyphCount() * sizeof(uint16_t);
124 const SkPoint& offset = it.offset();
125 // applyFontToPaint() always overwrites the exact same attributes,
126 // so it is safe to not re-seed the paint.
127 it.applyFontToPaint(&runPaint);
128
129 switch (it.positioning()) {
130 case SkTextBlob::kDefault_Positioning:
131 this->drawText(localDraw, it.glyphs(), textLen, offset.x(), offset.y(), runPaint);
132 break;
133 case SkTextBlob::kHorizontal_Positioning:
134 case SkTextBlob::kFull_Positioning:
135 this->drawPosText(localDraw, it.glyphs(), textLen, it.pos(), offset.y(),
136 SkTextBlob::ScalarsPerGlyph(it.positioning()), runPaint);
137 break;
138 default:
139 SkFAIL("unhandled positioning mode");
140 }
141
142 it.next();
143 }
144}
145
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000146bool SkBaseDevice::readPixels(const SkImageInfo& info, void* dstP, size_t rowBytes, int x, int y) {
147#ifdef SK_DEBUG
148 SkASSERT(info.width() > 0 && info.height() > 0);
149 SkASSERT(dstP);
150 SkASSERT(rowBytes >= info.minRowBytes());
151 SkASSERT(x >= 0 && y >= 0);
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +0000152
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000153 const SkImageInfo& srcInfo = this->imageInfo();
154 SkASSERT(x + info.width() <= srcInfo.width());
155 SkASSERT(y + info.height() <= srcInfo.height());
156#endif
157 return this->onReadPixels(info, dstP, rowBytes, x, y);
158}
159
commit-bot@chromium.org4ef54f82014-03-17 17:03:18 +0000160bool SkBaseDevice::writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
161 int x, int y) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000162#ifdef SK_DEBUG
163 SkASSERT(info.width() > 0 && info.height() > 0);
164 SkASSERT(pixels);
165 SkASSERT(rowBytes >= info.minRowBytes());
166 SkASSERT(x >= 0 && y >= 0);
167
168 const SkImageInfo& dstInfo = this->imageInfo();
169 SkASSERT(x + info.width() <= dstInfo.width());
170 SkASSERT(y + info.height() <= dstInfo.height());
171#endif
172 return this->onWritePixels(info, pixels, rowBytes, x, y);
173}
174
175bool SkBaseDevice::onWritePixels(const SkImageInfo&, const void*, size_t, int, int) {
176 return false;
177}
178
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000179bool SkBaseDevice::onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) {
commit-bot@chromium.org2cccf832014-03-12 03:04:08 +0000180 return false;
181}
182
reed@google.com9c135db2014-03-12 18:28:35 +0000183void* SkBaseDevice::accessPixels(SkImageInfo* info, size_t* rowBytes) {
184 SkImageInfo tmpInfo;
185 size_t tmpRowBytes;
186 if (NULL == info) {
187 info = &tmpInfo;
188 }
189 if (NULL == rowBytes) {
190 rowBytes = &tmpRowBytes;
191 }
192 return this->onAccessPixels(info, rowBytes);
193}
194
195void* SkBaseDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) {
196 return NULL;
197}
198
robertphillips9b14f262014-06-04 05:40:44 -0700199void SkBaseDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000200 // The base class doesn't perform any analysis but derived classes may
201}
202
reedd5fa1a42014-08-09 11:08:05 -0700203bool SkBaseDevice::EXPERIMENTAL_drawPicture(SkCanvas*, const SkPicture*, const SkMatrix*,
204 const SkPaint*) {
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000205 // The base class doesn't perform any accelerated picture rendering
206 return false;
207}