epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkDevice.h" |
reed | e010f1c | 2014-09-17 10:49:38 -0700 | [diff] [blame] | 9 | #include "SkDeviceProperties.h" |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 10 | #include "SkDraw.h" |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 11 | #include "SkMetaData.h" |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 12 | #include "SkPatchUtils.h" |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 13 | #include "SkShader.h" |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 14 | #include "SkTextBlob.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 16 | SkBaseDevice::SkBaseDevice() |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 17 | : fLeakyProperties(SkNEW_ARGS(SkDeviceProperties, (SkDeviceProperties::kLegacyLCD_InitType))) |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 18 | #ifdef SK_DEBUG |
| 19 | , fAttachedToCanvas(false) |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 20 | #endif |
| 21 | { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 22 | fOrigin.setZero(); |
| 23 | fMetaData = NULL; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 24 | } |
| 25 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 26 | SkBaseDevice::~SkBaseDevice() { |
reed | e010f1c | 2014-09-17 10:49:38 -0700 | [diff] [blame] | 27 | SkDELETE(fLeakyProperties); |
| 28 | SkDELETE(fMetaData); |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 29 | } |
| 30 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 31 | SkBaseDevice* SkBaseDevice::createCompatibleDevice(const SkImageInfo& info) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 32 | return this->onCreateDevice(info, kGeneral_Usage); |
| 33 | } |
| 34 | |
| 35 | SkBaseDevice* SkBaseDevice::createCompatibleDeviceForSaveLayer(const SkImageInfo& info) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 36 | return this->onCreateDevice(info, kSaveLayer_Usage); |
| 37 | } |
| 38 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 39 | SkMetaData& SkBaseDevice::getMetaData() { |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 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; |
| 44 | } |
| 45 | return *fMetaData; |
| 46 | } |
| 47 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 48 | SkImageInfo SkBaseDevice::imageInfo() const { |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 49 | return SkImageInfo::MakeUnknown(); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 52 | const SkBitmap& SkBaseDevice::accessBitmap(bool changePixels) { |
| 53 | const SkBitmap& bitmap = this->onAccessBitmap(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | if (changePixels) { |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 55 | bitmap.notifyPixelsChanged(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | } |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 57 | return bitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | } |
| 59 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 60 | void SkBaseDevice::setPixelGeometry(SkPixelGeometry geo) { |
| 61 | fLeakyProperties->fPixelGeometry = geo; |
| 62 | } |
| 63 | |
| 64 | SkSurface* SkBaseDevice::newSurface(const SkImageInfo&, const SkSurfaceProps&) { return NULL; } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 65 | |
| 66 | const void* SkBaseDevice::peekPixels(SkImageInfo*, size_t*) { return NULL; } |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 67 | |
| 68 | void 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.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 79 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 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; |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 83 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 84 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, draw.fMatrix); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 85 | |
| 86 | // It automatically adjusts lodX and lodY in case it exceeds the number of indices. |
dandov | 45f7842 | 2014-08-15 06:06:47 -0700 | [diff] [blame] | 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, |
| 91 | paint); |
| 92 | } |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 93 | } |
| 94 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 95 | void SkBaseDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 96 | const SkPaint &paint) { |
| 97 | |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 98 | SkPaint runPaint = paint; |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 99 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 100 | SkTextBlob::RunIterator it(blob); |
| 101 | while (!it.done()) { |
| 102 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 103 | const SkPoint& offset = it.offset(); |
| 104 | // applyFontToPaint() always overwrites the exact same attributes, |
| 105 | // so it is safe to not re-seed the paint. |
| 106 | it.applyFontToPaint(&runPaint); |
| 107 | |
| 108 | switch (it.positioning()) { |
| 109 | case SkTextBlob::kDefault_Positioning: |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame^] | 110 | this->drawText(draw, it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint); |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 111 | break; |
| 112 | case SkTextBlob::kHorizontal_Positioning: |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame^] | 113 | this->drawPosText(draw, it.glyphs(), textLen, it.pos(), 1, |
| 114 | SkPoint::Make(x, y + offset.y()), runPaint); |
| 115 | break; |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 116 | case SkTextBlob::kFull_Positioning: |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame^] | 117 | this->drawPosText(draw, it.glyphs(), textLen, it.pos(), 2, |
| 118 | SkPoint::Make(x, y), runPaint); |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 119 | break; |
| 120 | default: |
| 121 | SkFAIL("unhandled positioning mode"); |
| 122 | } |
| 123 | |
| 124 | it.next(); |
| 125 | } |
| 126 | } |
| 127 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 128 | bool SkBaseDevice::readPixels(const SkImageInfo& info, void* dstP, size_t rowBytes, int x, int y) { |
| 129 | #ifdef SK_DEBUG |
| 130 | SkASSERT(info.width() > 0 && info.height() > 0); |
| 131 | SkASSERT(dstP); |
| 132 | SkASSERT(rowBytes >= info.minRowBytes()); |
| 133 | SkASSERT(x >= 0 && y >= 0); |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 134 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 135 | const SkImageInfo& srcInfo = this->imageInfo(); |
| 136 | SkASSERT(x + info.width() <= srcInfo.width()); |
| 137 | SkASSERT(y + info.height() <= srcInfo.height()); |
| 138 | #endif |
| 139 | return this->onReadPixels(info, dstP, rowBytes, x, y); |
| 140 | } |
| 141 | |
commit-bot@chromium.org | 4ef54f8 | 2014-03-17 17:03:18 +0000 | [diff] [blame] | 142 | bool SkBaseDevice::writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, |
| 143 | int x, int y) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 144 | #ifdef SK_DEBUG |
| 145 | SkASSERT(info.width() > 0 && info.height() > 0); |
| 146 | SkASSERT(pixels); |
| 147 | SkASSERT(rowBytes >= info.minRowBytes()); |
| 148 | SkASSERT(x >= 0 && y >= 0); |
| 149 | |
| 150 | const SkImageInfo& dstInfo = this->imageInfo(); |
| 151 | SkASSERT(x + info.width() <= dstInfo.width()); |
| 152 | SkASSERT(y + info.height() <= dstInfo.height()); |
| 153 | #endif |
| 154 | return this->onWritePixels(info, pixels, rowBytes, x, y); |
| 155 | } |
| 156 | |
| 157 | bool SkBaseDevice::onWritePixels(const SkImageInfo&, const void*, size_t, int, int) { |
| 158 | return false; |
| 159 | } |
| 160 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 161 | bool SkBaseDevice::onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) { |
commit-bot@chromium.org | 2cccf83 | 2014-03-12 03:04:08 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 165 | void* SkBaseDevice::accessPixels(SkImageInfo* info, size_t* rowBytes) { |
| 166 | SkImageInfo tmpInfo; |
| 167 | size_t tmpRowBytes; |
| 168 | if (NULL == info) { |
| 169 | info = &tmpInfo; |
| 170 | } |
| 171 | if (NULL == rowBytes) { |
| 172 | rowBytes = &tmpRowBytes; |
| 173 | } |
| 174 | return this->onAccessPixels(info, rowBytes); |
| 175 | } |
| 176 | |
| 177 | void* SkBaseDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) { |
| 178 | return NULL; |
| 179 | } |
| 180 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 181 | void SkBaseDevice::EXPERIMENTAL_optimize(const SkPicture* picture) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 182 | // The base class doesn't perform any analysis but derived classes may |
| 183 | } |
| 184 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 185 | bool SkBaseDevice::EXPERIMENTAL_drawPicture(SkCanvas*, const SkPicture*, const SkMatrix*, |
| 186 | const SkPaint*) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 187 | // The base class doesn't perform any accelerated picture rendering |
| 188 | return false; |
| 189 | } |