2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkDeviceProperties.h"
11 #include "SkMetaData.h"
12 #include "SkPatchUtils.h"
14 #include "SkTextBlob.h"
16 SkBaseDevice::SkBaseDevice()
17 : fLeakyProperties(SkNEW_ARGS(SkDeviceProperties, (SkDeviceProperties::kLegacyLCD_InitType)))
19 , fAttachedToCanvas(false)
26 SkBaseDevice::~SkBaseDevice() {
27 SkDELETE(fLeakyProperties);
31 SkBaseDevice* SkBaseDevice::createCompatibleDevice(const SkImageInfo& info) {
32 return this->onCreateDevice(info, kGeneral_Usage);
35 SkBaseDevice* SkBaseDevice::createCompatibleDeviceForSaveLayer(const SkImageInfo& info) {
36 return this->onCreateDevice(info, kSaveLayer_Usage);
39 SkMetaData& SkBaseDevice::getMetaData() {
40 // 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;
48 SkImageInfo SkBaseDevice::imageInfo() const {
49 return SkImageInfo::MakeUnknown();
52 const SkBitmap& SkBaseDevice::accessBitmap(bool changePixels) {
53 const SkBitmap& bitmap = this->onAccessBitmap();
55 bitmap.notifyPixelsChanged();
60 void SkBaseDevice::setPixelGeometry(SkPixelGeometry geo) {
61 fLeakyProperties->fPixelGeometry = geo;
64 SkSurface* SkBaseDevice::newSurface(const SkImageInfo&, const SkSurfaceProps&) { return NULL; }
66 const void* SkBaseDevice::peekPixels(SkImageInfo*, size_t*) { return NULL; }
68 void SkBaseDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
69 const SkRRect& inner, const SkPaint& paint) {
73 path.setFillType(SkPath::kEvenOdd_FillType);
75 const SkMatrix* preMatrix = NULL;
76 const bool pathIsMutable = true;
77 this->drawPath(draw, path, paint, preMatrix, pathIsMutable);
80 void 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;
84 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, draw.fMatrix);
86 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
87 // 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,
95 void SkBaseDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
96 const SkPaint &paint) {
98 SkPaint runPaint = paint;
100 SkDraw localDraw(draw);
103 localMatrix = *draw.fMatrix;
104 localMatrix.preTranslate(x, y);
105 localDraw.fMatrix = &localMatrix;
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.
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);
121 SkTextBlob::RunIterator it(blob);
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);
129 switch (it.positioning()) {
130 case SkTextBlob::kDefault_Positioning:
131 this->drawText(localDraw, it.glyphs(), textLen, offset.x(), offset.y(), runPaint);
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);
139 SkFAIL("unhandled positioning mode");
146 bool SkBaseDevice::readPixels(const SkImageInfo& info, void* dstP, size_t rowBytes, int x, int y) {
148 SkASSERT(info.width() > 0 && info.height() > 0);
150 SkASSERT(rowBytes >= info.minRowBytes());
151 SkASSERT(x >= 0 && y >= 0);
153 const SkImageInfo& srcInfo = this->imageInfo();
154 SkASSERT(x + info.width() <= srcInfo.width());
155 SkASSERT(y + info.height() <= srcInfo.height());
157 return this->onReadPixels(info, dstP, rowBytes, x, y);
160 bool SkBaseDevice::writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
163 SkASSERT(info.width() > 0 && info.height() > 0);
165 SkASSERT(rowBytes >= info.minRowBytes());
166 SkASSERT(x >= 0 && y >= 0);
168 const SkImageInfo& dstInfo = this->imageInfo();
169 SkASSERT(x + info.width() <= dstInfo.width());
170 SkASSERT(y + info.height() <= dstInfo.height());
172 return this->onWritePixels(info, pixels, rowBytes, x, y);
175 bool SkBaseDevice::onWritePixels(const SkImageInfo&, const void*, size_t, int, int) {
179 bool SkBaseDevice::onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) {
183 void* SkBaseDevice::accessPixels(SkImageInfo* info, size_t* rowBytes) {
189 if (NULL == rowBytes) {
190 rowBytes = &tmpRowBytes;
192 return this->onAccessPixels(info, rowBytes);
195 void* SkBaseDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) {
199 void SkBaseDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
200 // The base class doesn't perform any analysis but derived classes may
203 bool SkBaseDevice::EXPERIMENTAL_drawPicture(SkCanvas*, const SkPicture*, const SkMatrix*,
205 // The base class doesn't perform any accelerated picture rendering