gfx/skia/trunk/src/core/SkDevice.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkDevice.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,224 @@
     1.4 +/*
     1.5 + * Copyright 2011 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkDevice.h"
    1.12 +#include "SkMetaData.h"
    1.13 +
    1.14 +#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
    1.15 +    const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kBGRA_Premul_Config8888;
    1.16 +#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
    1.17 +    const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kRGBA_Premul_Config8888;
    1.18 +#else
    1.19 +    const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = (SkCanvas::Config8888) -1;
    1.20 +#endif
    1.21 +
    1.22 +///////////////////////////////////////////////////////////////////////////////
    1.23 +
    1.24 +SkBaseDevice::SkBaseDevice()
    1.25 +    : fLeakyProperties(SkDeviceProperties::MakeDefault())
    1.26 +#ifdef SK_DEBUG
    1.27 +    , fAttachedToCanvas(false)
    1.28 +#endif
    1.29 +{
    1.30 +    fOrigin.setZero();
    1.31 +    fMetaData = NULL;
    1.32 +}
    1.33 +
    1.34 +SkBaseDevice::SkBaseDevice(const SkDeviceProperties& deviceProperties)
    1.35 +    : fLeakyProperties(deviceProperties)
    1.36 +#ifdef SK_DEBUG
    1.37 +    , fAttachedToCanvas(false)
    1.38 +#endif
    1.39 +{
    1.40 +    fOrigin.setZero();
    1.41 +    fMetaData = NULL;
    1.42 +}
    1.43 +
    1.44 +SkBaseDevice::~SkBaseDevice() {
    1.45 +    delete fMetaData;
    1.46 +}
    1.47 +
    1.48 +SkBaseDevice* SkBaseDevice::createCompatibleDevice(const SkImageInfo& info) {
    1.49 +#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
    1.50 +    // We call the old method to support older subclasses.
    1.51 +    // If they have, we return their device, else we use the new impl.
    1.52 +    SkBitmap::Config config = SkColorTypeToBitmapConfig(info.colorType());
    1.53 +    SkBaseDevice* dev = this->onCreateCompatibleDevice(config,
    1.54 +                                                       info.width(),
    1.55 +                                                       info.height(),
    1.56 +                                                       info.isOpaque(),
    1.57 +                                                       kGeneral_Usage);
    1.58 +    if (dev) {
    1.59 +        return dev;
    1.60 +    }
    1.61 +    // fall through to new impl
    1.62 +#endif
    1.63 +    return this->onCreateDevice(info, kGeneral_Usage);
    1.64 +}
    1.65 +
    1.66 +SkBaseDevice* SkBaseDevice::createCompatibleDeviceForSaveLayer(const SkImageInfo& info) {
    1.67 +#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
    1.68 +    // We call the old method to support older subclasses.
    1.69 +    // If they have, we return their device, else we use the new impl.
    1.70 +    SkBitmap::Config config = SkColorTypeToBitmapConfig(info.colorType());
    1.71 +    SkBaseDevice* dev = this->onCreateCompatibleDevice(config,
    1.72 +                                                       info.width(),
    1.73 +                                                       info.height(),
    1.74 +                                                       info.isOpaque(),
    1.75 +                                                       kSaveLayer_Usage);
    1.76 +    if (dev) {
    1.77 +        return dev;
    1.78 +    }
    1.79 +    // fall through to new impl
    1.80 +#endif
    1.81 +    return this->onCreateDevice(info, kSaveLayer_Usage);
    1.82 +}
    1.83 +
    1.84 +#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
    1.85 +SkBaseDevice* SkBaseDevice::createCompatibleDevice(SkBitmap::Config config,
    1.86 +                                                   int width, int height,
    1.87 +                                                   bool isOpaque) {
    1.88 +    SkImageInfo info = SkImageInfo::Make(width, height,
    1.89 +                                         SkBitmapConfigToColorType(config),
    1.90 +                                         isOpaque ? kOpaque_SkAlphaType
    1.91 +                                                  : kPremul_SkAlphaType);
    1.92 +    return this->createCompatibleDevice(info);
    1.93 +}
    1.94 +#endif
    1.95 +
    1.96 +SkMetaData& SkBaseDevice::getMetaData() {
    1.97 +    // metadata users are rare, so we lazily allocate it. If that changes we
    1.98 +    // can decide to just make it a field in the device (rather than a ptr)
    1.99 +    if (NULL == fMetaData) {
   1.100 +        fMetaData = new SkMetaData;
   1.101 +    }
   1.102 +    return *fMetaData;
   1.103 +}
   1.104 +
   1.105 +// TODO: should make this guy pure-virtual.
   1.106 +SkImageInfo SkBaseDevice::imageInfo() const {
   1.107 +    return SkImageInfo::MakeUnknown(this->width(), this->height());
   1.108 +}
   1.109 +
   1.110 +const SkBitmap& SkBaseDevice::accessBitmap(bool changePixels) {
   1.111 +    const SkBitmap& bitmap = this->onAccessBitmap();
   1.112 +    if (changePixels) {
   1.113 +        bitmap.notifyPixelsChanged();
   1.114 +    }
   1.115 +    return bitmap;
   1.116 +}
   1.117 +
   1.118 +bool SkBaseDevice::readPixels(SkBitmap* bitmap, int x, int y,
   1.119 +                              SkCanvas::Config8888 config8888) {
   1.120 +    if (SkBitmap::kARGB_8888_Config != bitmap->config() ||
   1.121 +        NULL != bitmap->getTexture()) {
   1.122 +        return false;
   1.123 +    }
   1.124 +
   1.125 +    const SkBitmap& src = this->accessBitmap(false);
   1.126 +
   1.127 +    SkIRect srcRect = SkIRect::MakeXYWH(x, y, bitmap->width(),
   1.128 +                                              bitmap->height());
   1.129 +    SkIRect devbounds = SkIRect::MakeWH(src.width(), src.height());
   1.130 +    if (!srcRect.intersect(devbounds)) {
   1.131 +        return false;
   1.132 +    }
   1.133 +
   1.134 +    SkBitmap tmp;
   1.135 +    SkBitmap* bmp;
   1.136 +    if (bitmap->isNull()) {
   1.137 +        if (!tmp.allocPixels(SkImageInfo::MakeN32Premul(bitmap->width(),
   1.138 +                                                        bitmap->height()))) {
   1.139 +            return false;
   1.140 +        }
   1.141 +        bmp = &tmp;
   1.142 +    } else {
   1.143 +        bmp = bitmap;
   1.144 +    }
   1.145 +
   1.146 +    SkIRect subrect = srcRect;
   1.147 +    subrect.offset(-x, -y);
   1.148 +    SkBitmap bmpSubset;
   1.149 +    bmp->extractSubset(&bmpSubset, subrect);
   1.150 +
   1.151 +    bool result = this->onReadPixels(bmpSubset,
   1.152 +                                     srcRect.fLeft,
   1.153 +                                     srcRect.fTop,
   1.154 +                                     config8888);
   1.155 +    if (result && bmp == &tmp) {
   1.156 +        tmp.swap(*bitmap);
   1.157 +    }
   1.158 +    return result;
   1.159 +}
   1.160 +
   1.161 +SkSurface* SkBaseDevice::newSurface(const SkImageInfo&) { return NULL; }
   1.162 +
   1.163 +const void* SkBaseDevice::peekPixels(SkImageInfo*, size_t*) { return NULL; }
   1.164 +
   1.165 +void SkBaseDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
   1.166 +                              const SkRRect& inner, const SkPaint& paint) {
   1.167 +    SkPath path;
   1.168 +    path.addRRect(outer);
   1.169 +    path.addRRect(inner);
   1.170 +    path.setFillType(SkPath::kEvenOdd_FillType);
   1.171 +
   1.172 +    const SkMatrix* preMatrix = NULL;
   1.173 +    const bool pathIsMutable = true;
   1.174 +    this->drawPath(draw, path, paint, preMatrix, pathIsMutable);
   1.175 +}
   1.176 +
   1.177 +bool SkBaseDevice::writePixelsDirect(const SkImageInfo& info, const void* pixels, size_t rowBytes,
   1.178 +                                     int x, int y) {
   1.179 +#ifdef SK_DEBUG
   1.180 +    SkASSERT(info.width() > 0 && info.height() > 0);
   1.181 +    SkASSERT(pixels);
   1.182 +    SkASSERT(rowBytes >= info.minRowBytes());
   1.183 +    SkASSERT(x >= 0 && y >= 0);
   1.184 +
   1.185 +    const SkImageInfo& dstInfo = this->imageInfo();
   1.186 +    SkASSERT(x + info.width() <= dstInfo.width());
   1.187 +    SkASSERT(y + info.height() <= dstInfo.height());
   1.188 +#endif
   1.189 +    return this->onWritePixels(info, pixels, rowBytes, x, y);
   1.190 +}
   1.191 +
   1.192 +bool SkBaseDevice::onWritePixels(const SkImageInfo&, const void*, size_t, int, int) {
   1.193 +    return false;
   1.194 +}
   1.195 +
   1.196 +bool SkBaseDevice::onReadPixels(const SkBitmap&, int x, int y, SkCanvas::Config8888) {
   1.197 +    return false;
   1.198 +}
   1.199 +
   1.200 +void* SkBaseDevice::accessPixels(SkImageInfo* info, size_t* rowBytes) {
   1.201 +    SkImageInfo tmpInfo;
   1.202 +    size_t tmpRowBytes;
   1.203 +    if (NULL == info) {
   1.204 +        info = &tmpInfo;
   1.205 +    }
   1.206 +    if (NULL == rowBytes) {
   1.207 +        rowBytes = &tmpRowBytes;
   1.208 +    }
   1.209 +    return this->onAccessPixels(info, rowBytes);
   1.210 +}
   1.211 +
   1.212 +void* SkBaseDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) {
   1.213 +    return NULL;
   1.214 +}
   1.215 +
   1.216 +#ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG
   1.217 +void SkBaseDevice::writePixels(const SkBitmap&, int x, int y, SkCanvas::Config8888) {}
   1.218 +#endif
   1.219 +
   1.220 +void SkBaseDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
   1.221 +    // The base class doesn't perform any analysis but derived classes may
   1.222 +}
   1.223 +
   1.224 +bool SkBaseDevice::EXPERIMENTAL_drawPicture(const SkPicture& picture) {
   1.225 +    // The base class doesn't perform any accelerated picture rendering
   1.226 +    return false;
   1.227 +}

mercurial