michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkCanvasStateUtils.h" michael@0: michael@0: #include "SkBitmapDevice.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkCanvasStack.h" michael@0: #include "SkErrorInternals.h" michael@0: #include "SkWriter32.h" michael@0: michael@0: #define CANVAS_STATE_VERSION 1 michael@0: /* michael@0: * WARNING: The structs below are part of a stable ABI and as such we explicitly michael@0: * use unambigious primitives (e.g. int32_t instead of an enum). michael@0: * michael@0: * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN AN michael@0: * UPDATE OF THE CANVAS_STATE_VERSION. SUCH CHANGES SHOULD ONLY BE MADE IF michael@0: * ABSOLUTELY NECESSARY! michael@0: */ michael@0: enum RasterConfigs { michael@0: kUnknown_RasterConfig = 0, michael@0: kRGB_565_RasterConfig = 1, michael@0: kARGB_8888_RasterConfig = 2 michael@0: }; michael@0: typedef int32_t RasterConfig; michael@0: michael@0: enum CanvasBackends { michael@0: kUnknown_CanvasBackend = 0, michael@0: kRaster_CanvasBackend = 1, michael@0: kGPU_CanvasBackend = 2, michael@0: kPDF_CanvasBackend = 3 michael@0: }; michael@0: typedef int32_t CanvasBackend; michael@0: michael@0: struct ClipRect { michael@0: int32_t left, top, right, bottom; michael@0: }; michael@0: michael@0: struct SkMCState { michael@0: float matrix[9]; michael@0: // NOTE: this only works for non-antialiased clips michael@0: int32_t clipRectCount; michael@0: ClipRect* clipRects; michael@0: }; michael@0: michael@0: // NOTE: If you add more members, bump CanvasState::version. michael@0: struct SkCanvasLayerState { michael@0: CanvasBackend type; michael@0: int32_t x, y; michael@0: int32_t width; michael@0: int32_t height; michael@0: michael@0: SkMCState mcState; michael@0: michael@0: union { michael@0: struct { michael@0: RasterConfig config; // pixel format: a value from RasterConfigs. michael@0: size_t rowBytes; // Number of bytes from start of one line to next. michael@0: void* pixels; // The pixels, all (height * rowBytes) of them. michael@0: } raster; michael@0: struct { michael@0: int32_t textureID; michael@0: } gpu; michael@0: }; michael@0: }; michael@0: michael@0: class SkCanvasState { michael@0: public: michael@0: SkCanvasState(SkCanvas* canvas) { michael@0: SkASSERT(canvas); michael@0: version = CANVAS_STATE_VERSION; michael@0: width = canvas->getDeviceSize().width(); michael@0: height = canvas->getDeviceSize().height(); michael@0: layerCount = 0; michael@0: layers = NULL; michael@0: originalCanvas = SkRef(canvas); michael@0: michael@0: mcState.clipRectCount = 0; michael@0: mcState.clipRects = NULL; michael@0: } michael@0: michael@0: ~SkCanvasState() { michael@0: // loop through the layers and free the data allocated to the clipRects michael@0: for (int i = 0; i < layerCount; ++i) { michael@0: sk_free(layers[i].mcState.clipRects); michael@0: } michael@0: michael@0: sk_free(mcState.clipRects); michael@0: sk_free(layers); michael@0: michael@0: // it is now safe to free the canvas since there should be no remaining michael@0: // references to the content that is referenced by this canvas (e.g. pixels) michael@0: originalCanvas->unref(); michael@0: } michael@0: michael@0: /** michael@0: * The version this struct was built with. This field must always appear michael@0: * first in the struct so that when the versions don't match (and the michael@0: * remaining contents and size are potentially different) we can still michael@0: * compare the version numbers. michael@0: */ michael@0: int32_t version; michael@0: michael@0: int32_t width; michael@0: int32_t height; michael@0: michael@0: SkMCState mcState; michael@0: michael@0: int32_t layerCount; michael@0: SkCanvasLayerState* layers; michael@0: michael@0: private: michael@0: SkCanvas* originalCanvas; michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class ClipValidator : public SkCanvas::ClipVisitor { michael@0: public: michael@0: ClipValidator() : fFailed(false) {} michael@0: bool failed() { return fFailed; } michael@0: michael@0: // ClipVisitor michael@0: virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) SK_OVERRIDE { michael@0: fFailed |= antialias; michael@0: } michael@0: michael@0: virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) SK_OVERRIDE { michael@0: fFailed |= antialias; michael@0: } michael@0: michael@0: virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) SK_OVERRIDE { michael@0: fFailed |= antialias; michael@0: } michael@0: michael@0: private: michael@0: bool fFailed; michael@0: }; michael@0: michael@0: static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkRegion& clip) { michael@0: // initialize the struct michael@0: state->clipRectCount = 0; michael@0: michael@0: // capture the matrix michael@0: for (int i = 0; i < 9; i++) { michael@0: state->matrix[i] = matrix.get(i); michael@0: } michael@0: michael@0: /* michael@0: * capture the clip michael@0: * michael@0: * storage is allocated on the stack for the first 4 rects. This value was michael@0: * chosen somewhat arbitrarily, but does allow us to represent simple clips michael@0: * and some more common complex clips (e.g. a clipRect with a sub-rect michael@0: * clipped out of its interior) without needing to malloc any additional memory. michael@0: */ michael@0: SkSWriter32<4*sizeof(ClipRect)> clipWriter; michael@0: michael@0: if (!clip.isEmpty()) { michael@0: // only returns the b/w clip so aa clips fail michael@0: SkRegion::Iterator clip_iterator(clip); michael@0: for (; !clip_iterator.done(); clip_iterator.next()) { michael@0: // this assumes the SkIRect is stored in l,t,r,b ordering which michael@0: // matches the ordering of our ClipRect struct michael@0: clipWriter.writeIRect(clip_iterator.rect()); michael@0: state->clipRectCount++; michael@0: } michael@0: } michael@0: michael@0: // allocate memory for the clip then and copy them to the struct michael@0: state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.bytesWritten()); michael@0: clipWriter.flatten(state->clipRects); michael@0: } michael@0: michael@0: michael@0: michael@0: SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas* canvas) { michael@0: SkASSERT(canvas); michael@0: michael@0: // Check the clip can be decomposed into rectangles (i.e. no soft clips). michael@0: ClipValidator validator; michael@0: canvas->replayClips(&validator); michael@0: if (validator.failed()) { michael@0: SkErrorInternals::SetError(kInvalidOperation_SkError, michael@0: "CaptureCanvasState does not support canvases with antialiased clips.\n"); michael@0: return NULL; michael@0: } michael@0: michael@0: SkAutoTDelete canvasState(SkNEW_ARGS(SkCanvasState, (canvas))); michael@0: michael@0: // decompose the total matrix and clip michael@0: setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), michael@0: canvas->internal_private_getTotalClip()); michael@0: michael@0: /* michael@0: * decompose the layers michael@0: * michael@0: * storage is allocated on the stack for the first 3 layers. It is common in michael@0: * some view systems (e.g. Android) that a few non-clipped layers are present michael@0: * and we will not need to malloc any additional memory in those cases. michael@0: */ michael@0: SkSWriter32<3*sizeof(SkCanvasLayerState)> layerWriter; michael@0: int layerCount = 0; michael@0: for (SkCanvas::LayerIter layer(canvas, true/*skipEmptyClips*/); !layer.done(); layer.next()) { michael@0: michael@0: // we currently only work for bitmap backed devices michael@0: const SkBitmap& bitmap = layer.device()->accessBitmap(true/*changePixels*/); michael@0: if (bitmap.empty() || bitmap.isNull() || !bitmap.lockPixelsAreWritable()) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkCanvasLayerState* layerState = michael@0: (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState)); michael@0: layerState->type = kRaster_CanvasBackend; michael@0: layerState->x = layer.x(); michael@0: layerState->y = layer.y(); michael@0: layerState->width = bitmap.width(); michael@0: layerState->height = bitmap.height(); michael@0: michael@0: switch (bitmap.colorType()) { michael@0: case kPMColor_SkColorType: michael@0: layerState->raster.config = kARGB_8888_RasterConfig; michael@0: break; michael@0: case kRGB_565_SkColorType: michael@0: layerState->raster.config = kRGB_565_RasterConfig; michael@0: break; michael@0: default: michael@0: return NULL; michael@0: } michael@0: layerState->raster.rowBytes = bitmap.rowBytes(); michael@0: layerState->raster.pixels = bitmap.getPixels(); michael@0: michael@0: setup_MC_state(&layerState->mcState, layer.matrix(), layer.clip()); michael@0: layerCount++; michael@0: } michael@0: michael@0: // allocate memory for the layers and then and copy them to the struct michael@0: SkASSERT(layerWriter.bytesWritten() == layerCount * sizeof(SkCanvasLayerState)); michael@0: canvasState->layerCount = layerCount; michael@0: canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten()); michael@0: layerWriter.flatten(canvasState->layers); michael@0: michael@0: // for now, just ignore any client supplied DrawFilter. michael@0: if (canvas->getDrawFilter()) { michael@0: // SkDEBUGF(("CaptureCanvasState will ignore the canvases draw filter.\n")); michael@0: } michael@0: michael@0: return canvasState.detach(); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void setup_canvas_from_MC_state(const SkMCState& state, SkCanvas* canvas) { michael@0: // reconstruct the matrix michael@0: SkMatrix matrix; michael@0: for (int i = 0; i < 9; i++) { michael@0: matrix.set(i, state.matrix[i]); michael@0: } michael@0: michael@0: // reconstruct the clip michael@0: SkRegion clip; michael@0: for (int i = 0; i < state.clipRectCount; ++i) { michael@0: clip.op(SkIRect::MakeLTRB(state.clipRects[i].left, michael@0: state.clipRects[i].top, michael@0: state.clipRects[i].right, michael@0: state.clipRects[i].bottom), michael@0: SkRegion::kUnion_Op); michael@0: } michael@0: michael@0: canvas->setMatrix(matrix); michael@0: canvas->setClipRegion(clip); michael@0: } michael@0: michael@0: static SkCanvas* create_canvas_from_canvas_layer(const SkCanvasLayerState& layerState) { michael@0: SkASSERT(kRaster_CanvasBackend == layerState.type); michael@0: michael@0: SkBitmap bitmap; michael@0: SkColorType colorType = michael@0: layerState.raster.config == kARGB_8888_RasterConfig ? kPMColor_SkColorType : michael@0: layerState.raster.config == kRGB_565_RasterConfig ? kRGB_565_SkColorType : michael@0: kUnknown_SkColorType; michael@0: michael@0: if (colorType == kUnknown_SkColorType) { michael@0: return NULL; michael@0: } michael@0: michael@0: bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height, michael@0: colorType, kPremul_SkAlphaType), michael@0: layerState.raster.pixels, layerState.raster.rowBytes, michael@0: NULL, NULL); michael@0: michael@0: SkASSERT(!bitmap.empty()); michael@0: SkASSERT(!bitmap.isNull()); michael@0: michael@0: SkAutoTUnref canvas(SkNEW_ARGS(SkCanvas, (bitmap))); michael@0: michael@0: // setup the matrix and clip michael@0: setup_canvas_from_MC_state(layerState.mcState, canvas.get()); michael@0: michael@0: return canvas.detach(); michael@0: } michael@0: michael@0: SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state) { michael@0: SkASSERT(state); michael@0: michael@0: // check that the versions match michael@0: if (CANVAS_STATE_VERSION != state->version) { michael@0: SkDebugf("CreateFromCanvasState version does not match the one use to create the input"); michael@0: return NULL; michael@0: } michael@0: michael@0: if (state->layerCount < 1) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkAutoTUnref canvas(SkNEW_ARGS(SkCanvasStack, (state->width, state->height))); michael@0: michael@0: // setup the matrix and clip on the n-way canvas michael@0: setup_canvas_from_MC_state(state->mcState, canvas); michael@0: michael@0: // Iterate over the layers and add them to the n-way canvas michael@0: for (int i = state->layerCount - 1; i >= 0; --i) { michael@0: SkAutoTUnref canvasLayer(create_canvas_from_canvas_layer(state->layers[i])); michael@0: if (!canvasLayer.get()) { michael@0: return NULL; michael@0: } michael@0: canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state->layers[i].x, michael@0: state->layers[i].y)); michael@0: } michael@0: michael@0: return canvas.detach(); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkCanvasStateUtils::ReleaseCanvasState(SkCanvasState* state) { michael@0: SkDELETE(state); michael@0: }