2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkImagePriv.h"
11 #include "SkImage_Base.h"
13 static SkImage_Base* as_IB(SkImage* image) {
14 return static_cast<SkImage_Base*>(image);
17 static const SkImage_Base* as_IB(const SkImage* image) {
18 return static_cast<const SkImage_Base*>(image);
21 uint32_t SkImage::NextUniqueID() {
22 static int32_t gUniqueID;
27 id = sk_atomic_inc(&gUniqueID) + 1;
32 void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
33 as_IB(this)->onDraw(canvas, x, y, paint);
36 void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
37 const SkPaint* paint) const {
38 as_IB(this)->onDrawRectToRect(canvas, src, dst, paint);
41 const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const {
42 SkImageInfo infoStorage;
43 size_t rowBytesStorage;
47 if (NULL == rowBytes) {
48 rowBytes = &rowBytesStorage;
50 return as_IB(this)->onPeekPixels(info, rowBytes);
53 bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const {
58 SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
60 // trim against the bitmap, if its already been allocated
61 if (bitmap->pixelRef()) {
62 bounds.fRight = SkMin32(bounds.fRight, bitmap->width());
63 bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height());
64 if (bounds.isEmpty()) {
69 if (subset && !bounds.intersect(*subset)) {
70 // perhaps we could return true + empty-bitmap?
73 return as_IB(this)->onReadPixels(bitmap, bounds);
76 GrTexture* SkImage::getTexture() {
77 return as_IB(this)->onGetTexture();
80 SkShader* SkImage::newShader(SkShader::TileMode tileX,
81 SkShader::TileMode tileY,
82 const SkMatrix* localMatrix) const {
83 return as_IB(this)->onNewShader(tileX, tileY, localMatrix);
86 SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const {
88 if (as_IB(this)->getROPixels(&bm)) {
89 return SkImageEncoder::EncodeData(bm, type, quality);
94 ///////////////////////////////////////////////////////////////////////////////
96 static bool raster_canvas_supports(const SkImageInfo& info) {
97 switch (info.colorType()) {
98 case kN32_SkColorType:
99 return kUnpremul_SkAlphaType != info.alphaType();
100 case kRGB_565_SkColorType:
102 case kAlpha_8_SkColorType:
110 bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const {
111 if (bitmap->pixelRef()) {
112 const SkImageInfo info = bitmap->info();
113 if (kUnknown_SkColorType == info.colorType()) {
116 if (!raster_canvas_supports(info)) {
121 if (!tmp.tryAllocN32Pixels(subset.width(), subset.height())) {
130 dstR.offset(-dstR.left(), -dstR.top());
132 SkCanvas canvas(*bitmap);
135 paint.setXfermodeMode(SkXfermode::kClear_Mode);
136 canvas.drawRect(dstR, paint);
138 const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL);