SkCanvasStateUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 2013 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. #include "include/utils/SkCanvasStateUtils.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "src/core/SkClipOpPriv.h"
  10. #include "src/core/SkDevice.h"
  11. #include "src/core/SkRasterClip.h"
  12. #include "src/core/SkWriter32.h"
  13. #include "src/utils/SkCanvasStack.h"
  14. /*
  15. * WARNING: The structs below are part of a stable ABI and as such we explicitly
  16. * use unambigious primitives (e.g. int32_t instead of an enum).
  17. *
  18. * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
  19. * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
  20. * NECESSARY!
  21. *
  22. * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp
  23. * describes how to create a library to pass to the CanvasState tests. The tests
  24. * should succeed when building the library with your changes and passing that to
  25. * the tests running in the unchanged Skia.
  26. */
  27. enum RasterConfigs {
  28. kUnknown_RasterConfig = 0,
  29. kRGB_565_RasterConfig = 1,
  30. kARGB_8888_RasterConfig = 2
  31. };
  32. typedef int32_t RasterConfig;
  33. enum CanvasBackends {
  34. kUnknown_CanvasBackend = 0,
  35. kRaster_CanvasBackend = 1,
  36. kGPU_CanvasBackend = 2,
  37. kPDF_CanvasBackend = 3
  38. };
  39. typedef int32_t CanvasBackend;
  40. struct ClipRect {
  41. int32_t left, top, right, bottom;
  42. };
  43. struct SkMCState {
  44. float matrix[9];
  45. // NOTE: this only works for non-antialiased clips
  46. int32_t clipRectCount;
  47. ClipRect* clipRects;
  48. };
  49. // NOTE: If you add more members, create a new subclass of SkCanvasState with a
  50. // new CanvasState::version.
  51. struct SkCanvasLayerState {
  52. CanvasBackend type;
  53. int32_t x, y;
  54. int32_t width;
  55. int32_t height;
  56. SkMCState mcState;
  57. union {
  58. struct {
  59. RasterConfig config; // pixel format: a value from RasterConfigs.
  60. uint64_t rowBytes; // Number of bytes from start of one line to next.
  61. void* pixels; // The pixels, all (height * rowBytes) of them.
  62. } raster;
  63. struct {
  64. int32_t textureID;
  65. } gpu;
  66. };
  67. };
  68. class SkCanvasState {
  69. public:
  70. SkCanvasState(int32_t version, SkCanvas* canvas) {
  71. SkASSERT(canvas);
  72. this->version = version;
  73. width = canvas->getBaseLayerSize().width();
  74. height = canvas->getBaseLayerSize().height();
  75. }
  76. /**
  77. * The version this struct was built with. This field must always appear
  78. * first in the struct so that when the versions don't match (and the
  79. * remaining contents and size are potentially different) we can still
  80. * compare the version numbers.
  81. */
  82. int32_t version;
  83. int32_t width;
  84. int32_t height;
  85. int32_t alignmentPadding;
  86. };
  87. class SkCanvasState_v1 : public SkCanvasState {
  88. public:
  89. static const int32_t kVersion = 1;
  90. SkCanvasState_v1(SkCanvas* canvas) : INHERITED(kVersion, canvas) {
  91. layerCount = 0;
  92. layers = nullptr;
  93. mcState.clipRectCount = 0;
  94. mcState.clipRects = nullptr;
  95. originalCanvas = canvas;
  96. }
  97. ~SkCanvasState_v1() {
  98. // loop through the layers and free the data allocated to the clipRects
  99. for (int i = 0; i < layerCount; ++i) {
  100. sk_free(layers[i].mcState.clipRects);
  101. }
  102. sk_free(mcState.clipRects);
  103. sk_free(layers);
  104. }
  105. SkMCState mcState;
  106. int32_t layerCount;
  107. SkCanvasLayerState* layers;
  108. private:
  109. SkCanvas* originalCanvas;
  110. typedef SkCanvasState INHERITED;
  111. };
  112. ////////////////////////////////////////////////////////////////////////////////
  113. static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkIRect& clip) {
  114. // initialize the struct
  115. state->clipRectCount = 0;
  116. // capture the matrix
  117. for (int i = 0; i < 9; i++) {
  118. state->matrix[i] = matrix.get(i);
  119. }
  120. /*
  121. * We only support a single clipRect, so we take the clip's bounds. Clients have long made
  122. * this assumption anyway, so this restriction is fine.
  123. */
  124. SkSWriter32<sizeof(ClipRect)> clipWriter;
  125. if (!clip.isEmpty()) {
  126. state->clipRectCount = 1;
  127. state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect));
  128. state->clipRects->left = clip.fLeft;
  129. state->clipRects->top = clip.fTop;
  130. state->clipRects->right = clip.fRight;
  131. state->clipRects->bottom = clip.fBottom;
  132. }
  133. }
  134. SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) {
  135. SkASSERT(canvas);
  136. // Check the clip can be decomposed into rectangles (i.e. no soft clips).
  137. if (canvas->androidFramework_isClipAA()) {
  138. return nullptr;
  139. }
  140. std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
  141. setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds());
  142. /*
  143. * decompose the layers
  144. *
  145. * storage is allocated on the stack for the first 3 layers. It is common in
  146. * some view systems (e.g. Android) that a few non-clipped layers are present
  147. * and we will not need to malloc any additional memory in those cases.
  148. */
  149. SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter;
  150. int layerCount = 0;
  151. for (SkCanvas::LayerIter layer(canvas); !layer.done(); layer.next()) {
  152. // we currently only work for bitmap backed devices
  153. SkPixmap pmap;
  154. if (!layer.device()->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
  155. return nullptr;
  156. }
  157. SkCanvasLayerState* layerState =
  158. (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
  159. layerState->type = kRaster_CanvasBackend;
  160. layerState->x = layer.x();
  161. layerState->y = layer.y();
  162. layerState->width = pmap.width();
  163. layerState->height = pmap.height();
  164. switch (pmap.colorType()) {
  165. case kN32_SkColorType:
  166. layerState->raster.config = kARGB_8888_RasterConfig;
  167. break;
  168. case kRGB_565_SkColorType:
  169. layerState->raster.config = kRGB_565_RasterConfig;
  170. break;
  171. default:
  172. return nullptr;
  173. }
  174. layerState->raster.rowBytes = pmap.rowBytes();
  175. layerState->raster.pixels = pmap.writable_addr();
  176. setup_MC_state(&layerState->mcState, layer.matrix(), layer.clipBounds());
  177. layerCount++;
  178. }
  179. // allocate memory for the layers and then and copy them to the struct
  180. SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState));
  181. canvasState->layerCount = layerCount;
  182. canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
  183. layerWriter.flatten(canvasState->layers);
  184. return canvasState.release();
  185. }
  186. ////////////////////////////////////////////////////////////////////////////////
  187. static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) {
  188. // reconstruct the matrix
  189. SkMatrix matrix;
  190. for (int i = 0; i < 9; i++) {
  191. matrix.set(i, state.matrix[i]);
  192. }
  193. // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds
  194. // of what they sent.
  195. SkIRect bounds = SkIRect::MakeEmpty();
  196. if (state.clipRectCount > 0) {
  197. bounds.set(state.clipRects[0].left,
  198. state.clipRects[0].top,
  199. state.clipRects[0].right,
  200. state.clipRects[0].bottom);
  201. for (int i = 1; i < state.clipRectCount; ++i) {
  202. bounds.join(state.clipRects[i].left,
  203. state.clipRects[i].top,
  204. state.clipRects[i].right,
  205. state.clipRects[i].bottom);
  206. }
  207. }
  208. canvas->clipRect(SkRect::Make(bounds));
  209. canvas->concat(matrix);
  210. }
  211. static std::unique_ptr<SkCanvas>
  212. make_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) {
  213. SkASSERT(kRaster_CanvasBackend == layerState.type);
  214. SkBitmap bitmap;
  215. SkColorType colorType =
  216. layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
  217. layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType :
  218. kUnknown_SkColorType;
  219. if (colorType == kUnknown_SkColorType) {
  220. return nullptr;
  221. }
  222. bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
  223. colorType, kPremul_SkAlphaType),
  224. layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
  225. SkASSERT(!bitmap.empty());
  226. SkASSERT(!bitmap.isNull());
  227. std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
  228. // setup the matrix and clip
  229. setup_canvas_from_MC_state(layerState.mcState, canvas.get());
  230. return canvas;
  231. }
  232. std::unique_ptr<SkCanvas> SkCanvasStateUtils::MakeFromCanvasState(const SkCanvasState* state) {
  233. SkASSERT(state);
  234. // Currently there is only one possible version.
  235. SkASSERT(SkCanvasState_v1::kVersion == state->version);
  236. const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
  237. if (state_v1->layerCount < 1) {
  238. return nullptr;
  239. }
  240. std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
  241. // setup the matrix and clip on the n-way canvas
  242. setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
  243. // Iterate over the layers and add them to the n-way canvas
  244. for (int i = state_v1->layerCount - 1; i >= 0; --i) {
  245. std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
  246. if (!canvasLayer.get()) {
  247. return nullptr;
  248. }
  249. canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
  250. state_v1->layers[i].y));
  251. }
  252. return std::move(canvas);
  253. }
  254. ////////////////////////////////////////////////////////////////////////////////
  255. void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) {
  256. SkASSERT(!state || SkCanvasState_v1::kVersion == state->version);
  257. // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
  258. // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
  259. // instead uses the field "version" to determine how to behave.
  260. delete static_cast<SkCanvasState_v1*>(state);
  261. }