epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "SkGr.h" |
| 12 | |
| 13 | /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| 14 | based bitmap. [palette (colortable) + indices]. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 15 | |
| 16 | At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | we could detect that the colortable.count is <= 16, and then repack the |
| 18 | indices as nibbles to save RAM, but it would take more time (i.e. a lot |
| 19 | slower than memcpy), so skipping that for now. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 20 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big |
| 22 | as the colortable.count says it is. |
| 23 | */ |
| 24 | static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { |
| 25 | SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 26 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | SkAutoLockPixels apl(bitmap); |
| 28 | if (!bitmap.readyToDraw()) { |
| 29 | SkASSERT(!"bitmap not ready to draw!"); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | SkColorTable* ctable = bitmap.getColorTable(); |
| 34 | char* dst = (char*)buffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 35 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | memcpy(dst, ctable->lockColors(), ctable->count() * sizeof(SkPMColor)); |
| 37 | ctable->unlockColors(false); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 38 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | // always skip a full 256 number of entries, even if we memcpy'd fewer |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 40 | dst += kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | |
| 42 | if (bitmap.width() == bitmap.rowBytes()) { |
| 43 | memcpy(dst, bitmap.getPixels(), bitmap.getSize()); |
| 44 | } else { |
| 45 | // need to trim off the extra bytes per row |
| 46 | size_t width = bitmap.width(); |
| 47 | size_t rowBytes = bitmap.rowBytes(); |
| 48 | const char* src = (const char*)bitmap.getPixels(); |
| 49 | for (int y = 0; y < bitmap.height(); y++) { |
| 50 | memcpy(dst, src, width); |
| 51 | src += rowBytes; |
| 52 | dst += width; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | //////////////////////////////////////////////////////////////////////////////// |
| 58 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 59 | GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx, |
| 60 | GrContext::TextureKey key, |
| 61 | const GrSamplerState& sampler, |
| 62 | const SkBitmap& origBitmap) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | SkAutoLockPixels alp(origBitmap); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 64 | GrContext::TextureCacheEntry entry; |
| 65 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | if (!origBitmap.readyToDraw()) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 67 | return entry; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SkBitmap tmpBitmap; |
| 71 | |
| 72 | const SkBitmap* bitmap = &origBitmap; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 74 | GrTextureDesc desc = { |
| 75 | kNone_GrTextureFlags, |
| 76 | kNone_GrAALevel, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | bitmap->width(), |
| 78 | bitmap->height(), |
| 79 | SkGr::Bitmap2PixelConfig(*bitmap) |
| 80 | }; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 81 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | if (SkBitmap::kIndex8_Config == bitmap->config()) { |
| 83 | // build_compressed_data doesn't do npot->pot expansion |
| 84 | // and paletted textures can't be sub-updated |
| 85 | if (ctx->supportsIndex8PixelConfig(sampler, |
| 86 | bitmap->width(), bitmap->height())) { |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 87 | size_t imagesize = bitmap->width() * bitmap->height() + |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 88 | kGrColorTableSize; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | SkAutoMalloc storage(imagesize); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 90 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | build_compressed_data(storage.get(), origBitmap); |
| 92 | |
| 93 | // our compressed data will be trimmed, so pass width() for its |
| 94 | // "rowBytes", since they are the same now. |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 95 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 96 | if (gUNCACHED_KEY != key) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 97 | return ctx->createAndLockTexture(key, sampler, desc, storage.get(), |
| 98 | bitmap->width()); |
| 99 | } else { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 100 | entry = ctx->lockScratchTexture(desc, |
| 101 | GrContext::kExact_ScratchTexMatch); |
| 102 | entry.texture()->uploadTextureData(0, 0, bitmap->width(), |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 103 | bitmap->height(), storage.get(), 0); |
| 104 | return entry; |
| 105 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | |
| 107 | } else { |
| 108 | origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); |
| 109 | // now bitmap points to our temp, which has been promoted to 32bits |
| 110 | bitmap = &tmpBitmap; |
| 111 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 112 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | |
| 114 | desc.fFormat = SkGr::Bitmap2PixelConfig(*bitmap); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 115 | if (gUNCACHED_KEY != key) { |
| 116 | return ctx->createAndLockTexture(key, sampler, desc, |
| 117 | bitmap->getPixels(), |
| 118 | bitmap->rowBytes()); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 119 | } else { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 120 | entry = ctx->lockScratchTexture(desc, |
| 121 | GrContext::kExact_ScratchTexMatch); |
| 122 | entry.texture()->uploadTextureData(0, 0, bitmap->width(), |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 123 | bitmap->height(), bitmap->getPixels(), bitmap->rowBytes()); |
| 124 | return entry; |
| 125 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | } |
| 127 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 130 | void SkGrClipIterator::reset(const SkClipStack& clipStack) { |
| 131 | fClipStack = &clipStack; |
| 132 | fIter.reset(clipStack); |
| 133 | // Gr has no notion of replace, skip to the |
| 134 | // last replace in the clip stack. |
| 135 | int lastReplace = 0; |
| 136 | int curr = 0; |
| 137 | while (NULL != (fCurr = fIter.next())) { |
| 138 | if (SkRegion::kReplace_Op == fCurr->fOp) { |
| 139 | lastReplace = curr; |
| 140 | } |
| 141 | ++curr; |
| 142 | } |
| 143 | fIter.reset(clipStack); |
| 144 | for (int i = 0; i < lastReplace+1; ++i) { |
| 145 | fCurr = fIter.next(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | GrClipType SkGrClipIterator::getType() const { |
| 150 | GrAssert(!this->isDone()); |
scroggo | 7b11807 | 2011-03-23 15:04:26 +0000 | [diff] [blame] | 151 | if (NULL == fCurr->fPath) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 152 | return kRect_ClipType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | } else { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 154 | return kPath_ClipType; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | GrSetOp SkGrClipIterator::getOp() const { |
| 159 | // we skipped to the last "replace" op |
| 160 | // when this iter was reset. |
| 161 | // GrClip doesn't allow replace, so treat it as |
| 162 | // intersect. |
| 163 | GrSetOp skToGrOps[] = { |
| 164 | kDifference_SetOp, // kDifference_Op |
| 165 | kIntersect_SetOp, // kIntersect_Op |
| 166 | kUnion_SetOp, // kUnion_Op |
| 167 | kXor_SetOp, // kXOR_Op |
| 168 | kReverseDifference_SetOp, // kReverseDifference_Op |
| 169 | kIntersect_SetOp // kReplace_op |
| 170 | }; |
| 171 | GR_STATIC_ASSERT(0 == SkRegion::kDifference_Op); |
| 172 | GR_STATIC_ASSERT(1 == SkRegion::kIntersect_Op); |
| 173 | GR_STATIC_ASSERT(2 == SkRegion::kUnion_Op); |
| 174 | GR_STATIC_ASSERT(3 == SkRegion::kXOR_Op); |
| 175 | GR_STATIC_ASSERT(4 == SkRegion::kReverseDifference_Op); |
| 176 | GR_STATIC_ASSERT(5 == SkRegion::kReplace_Op); |
| 177 | return skToGrOps[fCurr->fOp]; |
| 178 | } |
| 179 | |
| 180 | GrPathFill SkGrClipIterator::getPathFill() const { |
| 181 | switch (fCurr->fPath->getFillType()) { |
| 182 | case SkPath::kWinding_FillType: |
| 183 | return kWinding_PathFill; |
| 184 | case SkPath::kEvenOdd_FillType: |
| 185 | return kEvenOdd_PathFill; |
| 186 | case SkPath::kInverseWinding_FillType: |
| 187 | return kInverseWinding_PathFill; |
| 188 | case SkPath::kInverseEvenOdd_FillType: |
| 189 | return kInverseEvenOdd_PathFill; |
| 190 | default: |
| 191 | GrCrash("Unsupported path fill in clip."); |
| 192 | return kWinding_PathFill; // suppress warning |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | /////////////////////////////////////////////////////////////////////////////// |
| 197 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 198 | GrPixelConfig SkGr::BitmapConfig2PixelConfig(SkBitmap::Config config, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 199 | bool isOpaque) { |
| 200 | switch (config) { |
| 201 | case SkBitmap::kA8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 202 | return kAlpha_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 203 | case SkBitmap::kIndex8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 204 | return kIndex_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | case SkBitmap::kRGB_565_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 206 | return kRGB_565_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 207 | case SkBitmap::kARGB_4444_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 208 | return kRGBA_4444_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | case SkBitmap::kARGB_8888_Config: |
| 210 | if (isOpaque) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 211 | return kRGBX_8888_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | } else { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 213 | return kRGBA_8888_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | } |
| 215 | default: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 216 | return kUnknown_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |