SkDevice.cpp revision c3bd8af6d5722e854feca70c40d92f4954c5b67b
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 */ 7 8#include "SkDevice.h" 9#include "SkMetaData.h" 10 11#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) 12 const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kBGRA_Premul_Config8888; 13#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) 14 const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kRGBA_Premul_Config8888; 15#else 16 const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = (SkCanvas::Config8888) -1; 17#endif 18 19/////////////////////////////////////////////////////////////////////////////// 20 21SkBaseDevice::SkBaseDevice() 22 : fLeakyProperties(SkDeviceProperties::MakeDefault()) 23#ifdef SK_DEBUG 24 , fAttachedToCanvas(false) 25#endif 26{ 27 fOrigin.setZero(); 28 fMetaData = NULL; 29} 30 31SkBaseDevice::SkBaseDevice(const SkDeviceProperties& deviceProperties) 32 : fLeakyProperties(deviceProperties) 33#ifdef SK_DEBUG 34 , fAttachedToCanvas(false) 35#endif 36{ 37 fOrigin.setZero(); 38 fMetaData = NULL; 39} 40 41SkBaseDevice::~SkBaseDevice() { 42 delete fMetaData; 43} 44 45SkBaseDevice* SkBaseDevice::createCompatibleDevice(SkBitmap::Config config, 46 int width, int height, 47 bool isOpaque) { 48 return this->onCreateCompatibleDevice(config, width, height, 49 isOpaque, kGeneral_Usage); 50} 51 52SkBaseDevice* SkBaseDevice::createCompatibleDeviceForSaveLayer(SkBitmap::Config config, 53 int width, int height, 54 bool isOpaque) { 55 return this->onCreateCompatibleDevice(config, width, height, 56 isOpaque, kSaveLayer_Usage); 57} 58 59SkMetaData& SkBaseDevice::getMetaData() { 60 // metadata users are rare, so we lazily allocate it. If that changes we 61 // can decide to just make it a field in the device (rather than a ptr) 62 if (NULL == fMetaData) { 63 fMetaData = new SkMetaData; 64 } 65 return *fMetaData; 66} 67 68// TODO: should make this guy pure-virtual. 69SkImageInfo SkBaseDevice::imageInfo() const { 70 return SkImageInfo::Make(this->width(), this->height(), 71 kUnknown_SkColorType, kIgnore_SkAlphaType); 72} 73 74const SkBitmap& SkBaseDevice::accessBitmap(bool changePixels) { 75 const SkBitmap& bitmap = this->onAccessBitmap(); 76 if (changePixels) { 77 bitmap.notifyPixelsChanged(); 78 } 79 return bitmap; 80} 81 82bool SkBaseDevice::readPixels(SkBitmap* bitmap, int x, int y, 83 SkCanvas::Config8888 config8888) { 84 if (SkBitmap::kARGB_8888_Config != bitmap->config() || 85 NULL != bitmap->getTexture()) { 86 return false; 87 } 88 89 const SkBitmap& src = this->accessBitmap(false); 90 91 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bitmap->width(), 92 bitmap->height()); 93 SkIRect devbounds = SkIRect::MakeWH(src.width(), src.height()); 94 if (!srcRect.intersect(devbounds)) { 95 return false; 96 } 97 98 SkBitmap tmp; 99 SkBitmap* bmp; 100 if (bitmap->isNull()) { 101 if (!tmp.allocPixels(SkImageInfo::MakeN32Premul(bitmap->width(), 102 bitmap->height()))) { 103 return false; 104 } 105 bmp = &tmp; 106 } else { 107 bmp = bitmap; 108 } 109 110 SkIRect subrect = srcRect; 111 subrect.offset(-x, -y); 112 SkBitmap bmpSubset; 113 bmp->extractSubset(&bmpSubset, subrect); 114 115 bool result = this->onReadPixels(bmpSubset, 116 srcRect.fLeft, 117 srcRect.fTop, 118 config8888); 119 if (result && bmp == &tmp) { 120 tmp.swap(*bitmap); 121 } 122 return result; 123} 124 125SkSurface* SkBaseDevice::newSurface(const SkImageInfo&) { return NULL; } 126 127const void* SkBaseDevice::peekPixels(SkImageInfo*, size_t*) { return NULL; } 128