michael@0: /* michael@0: * Copyright 2012 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 "SkSurface_Base.h" michael@0: #include "SkImagePriv.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkDevice.h" michael@0: #include "SkMallocPixelRef.h" michael@0: michael@0: static const size_t kIgnoreRowBytesValue = (size_t)~0; michael@0: michael@0: class SkSurface_Raster : public SkSurface_Base { michael@0: public: michael@0: static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); michael@0: michael@0: SkSurface_Raster(const SkImageInfo&, void*, size_t rb); michael@0: SkSurface_Raster(SkPixelRef*); michael@0: michael@0: virtual SkCanvas* onNewCanvas() SK_OVERRIDE; michael@0: virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; michael@0: virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; michael@0: virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, michael@0: const SkPaint*) SK_OVERRIDE; michael@0: virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; michael@0: michael@0: private: michael@0: SkBitmap fBitmap; michael@0: bool fWeOwnThePixels; michael@0: michael@0: typedef SkSurface_Base INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { michael@0: static const size_t kMaxTotalSize = SK_MaxS32; michael@0: michael@0: int shift = 0; michael@0: switch (info.fColorType) { michael@0: case kAlpha_8_SkColorType: michael@0: shift = 0; michael@0: break; michael@0: case kRGB_565_SkColorType: michael@0: shift = 1; michael@0: break; michael@0: case kPMColor_SkColorType: michael@0: shift = 2; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: if (kIgnoreRowBytesValue == rowBytes) { michael@0: return true; michael@0: } michael@0: michael@0: uint64_t minRB = (uint64_t)info.fWidth << shift; michael@0: if (minRB > rowBytes) { michael@0: return false; michael@0: } michael@0: michael@0: size_t alignedRowBytes = rowBytes >> shift << shift; michael@0: if (alignedRowBytes != rowBytes) { michael@0: return false; michael@0: } michael@0: michael@0: uint64_t size = sk_64_mul(info.fHeight, rowBytes); michael@0: if (size > kMaxTotalSize) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb) michael@0: : INHERITED(info) michael@0: { michael@0: fBitmap.setConfig(info, rb); michael@0: fBitmap.setPixels(pixels); michael@0: fWeOwnThePixels = false; // We are "Direct" michael@0: } michael@0: michael@0: SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr) michael@0: : INHERITED(pr->info().fWidth, pr->info().fHeight) michael@0: { michael@0: const SkImageInfo& info = pr->info(); michael@0: michael@0: fBitmap.setConfig(info, info.minRowBytes()); michael@0: fBitmap.setPixelRef(pr); michael@0: fWeOwnThePixels = true; michael@0: michael@0: if (!info.isOpaque()) { michael@0: fBitmap.eraseColor(SK_ColorTRANSPARENT); michael@0: } michael@0: } michael@0: michael@0: SkCanvas* SkSurface_Raster::onNewCanvas() { michael@0: return SkNEW_ARGS(SkCanvas, (fBitmap)); michael@0: } michael@0: michael@0: SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { michael@0: return SkSurface::NewRaster(info); michael@0: } michael@0: michael@0: void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, michael@0: const SkPaint* paint) { michael@0: canvas->drawBitmap(fBitmap, x, y, paint); michael@0: } michael@0: michael@0: SkImage* SkSurface_Raster::onNewImageSnapshot() { michael@0: return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); michael@0: } michael@0: michael@0: void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { michael@0: // are we sharing pixelrefs with the image? michael@0: SkASSERT(NULL != this->getCachedImage()); michael@0: if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) { michael@0: SkASSERT(fWeOwnThePixels); michael@0: if (kDiscard_ContentChangeMode == mode) { michael@0: fBitmap.setPixelRef(NULL); michael@0: fBitmap.allocPixels(); michael@0: } else { michael@0: SkBitmap prev(fBitmap); michael@0: prev.deepCopyTo(&fBitmap); michael@0: } michael@0: // Now fBitmap is a deep copy of itself (and therefore different from michael@0: // what is being used by the image. Next we update the canvas to use michael@0: // this as its backend, so we can't modify the image's pixels anymore. michael@0: SkASSERT(NULL != this->getCachedCanvas()); michael@0: this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes) { michael@0: if (!SkSurface_Raster::Valid(info, rowBytes)) { michael@0: return NULL; michael@0: } michael@0: if (NULL == pixels) { michael@0: return NULL; michael@0: } michael@0: michael@0: return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rowBytes)); michael@0: } michael@0: michael@0: SkSurface* SkSurface::NewRaster(const SkImageInfo& info) { michael@0: if (!SkSurface_Raster::Valid(info)) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkAutoTUnref pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); michael@0: if (NULL == pr.get()) { michael@0: return NULL; michael@0: } michael@0: return SkNEW_ARGS(SkSurface_Raster, (pr)); michael@0: }