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()) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 29 | SkDEBUGFAIL("bitmap not ready to draw!"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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, |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 61 | const GrSamplerState* sampler, |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 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, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | bitmap->width(), |
| 77 | bitmap->height(), |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 78 | SkGr::Bitmap2PixelConfig(*bitmap), |
bsalomon@google.com | b9014f4 | 2012-03-30 14:22:41 +0000 | [diff] [blame] | 79 | 0 // samples |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 102 | entry.texture()->writePixels(0, 0, bitmap->width(), |
| 103 | bitmap->height(), desc.fConfig, |
| 104 | storage.get(), 0); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 105 | return entry; |
| 106 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | |
| 108 | } else { |
| 109 | origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); |
| 110 | // now bitmap points to our temp, which has been promoted to 32bits |
| 111 | bitmap = &tmpBitmap; |
| 112 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 113 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 115 | desc.fConfig = SkGr::Bitmap2PixelConfig(*bitmap); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 116 | if (gUNCACHED_KEY != key) { |
| 117 | return ctx->createAndLockTexture(key, sampler, desc, |
| 118 | bitmap->getPixels(), |
| 119 | bitmap->rowBytes()); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 120 | } else { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 121 | entry = ctx->lockScratchTexture(desc, |
| 122 | GrContext::kExact_ScratchTexMatch); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 123 | entry.texture()->writePixels(0, 0, |
| 124 | bitmap->width(), bitmap->height(), |
| 125 | desc.fConfig, |
| 126 | bitmap->getPixels(), |
| 127 | bitmap->rowBytes()); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 128 | return entry; |
| 129 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | } |
| 131 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 134 | void SkGrClipIterator::reset(const SkClipStack& clipStack) { |
| 135 | fClipStack = &clipStack; |
| 136 | fIter.reset(clipStack); |
| 137 | // Gr has no notion of replace, skip to the |
| 138 | // last replace in the clip stack. |
| 139 | int lastReplace = 0; |
| 140 | int curr = 0; |
| 141 | while (NULL != (fCurr = fIter.next())) { |
| 142 | if (SkRegion::kReplace_Op == fCurr->fOp) { |
| 143 | lastReplace = curr; |
| 144 | } |
| 145 | ++curr; |
| 146 | } |
| 147 | fIter.reset(clipStack); |
| 148 | for (int i = 0; i < lastReplace+1; ++i) { |
| 149 | fCurr = fIter.next(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | GrClipType SkGrClipIterator::getType() const { |
| 154 | GrAssert(!this->isDone()); |
scroggo | 7b11807 | 2011-03-23 15:04:26 +0000 | [diff] [blame] | 155 | if (NULL == fCurr->fPath) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 156 | return kRect_ClipType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 157 | } else { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 158 | return kPath_ClipType; |
| 159 | } |
| 160 | } |
| 161 | |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame^] | 162 | SkRegion::Op SkGrClipIterator::getOp() const { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 163 | // we skipped to the last "replace" op |
| 164 | // when this iter was reset. |
| 165 | // GrClip doesn't allow replace, so treat it as |
| 166 | // intersect. |
robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame^] | 167 | if (SkRegion::kReplace_Op == fCurr->fOp) { |
| 168 | return SkRegion::kIntersect_Op; |
| 169 | } |
| 170 | |
| 171 | return fCurr->fOp; |
| 172 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 173 | } |
| 174 | |
robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 175 | bool SkGrClipIterator::getDoAA() const { |
| 176 | return fCurr->fDoAA; |
| 177 | } |
| 178 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 179 | GrPathFill SkGrClipIterator::getPathFill() const { |
| 180 | switch (fCurr->fPath->getFillType()) { |
| 181 | case SkPath::kWinding_FillType: |
| 182 | return kWinding_PathFill; |
| 183 | case SkPath::kEvenOdd_FillType: |
| 184 | return kEvenOdd_PathFill; |
| 185 | case SkPath::kInverseWinding_FillType: |
| 186 | return kInverseWinding_PathFill; |
| 187 | case SkPath::kInverseEvenOdd_FillType: |
| 188 | return kInverseEvenOdd_PathFill; |
| 189 | default: |
| 190 | GrCrash("Unsupported path fill in clip."); |
| 191 | return kWinding_PathFill; // suppress warning |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 197 | GrPixelConfig SkGr::BitmapConfig2PixelConfig(SkBitmap::Config config, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 198 | bool isOpaque) { |
| 199 | switch (config) { |
| 200 | case SkBitmap::kA8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 201 | return kAlpha_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 202 | case SkBitmap::kIndex8_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 203 | return kIndex_8_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | case SkBitmap::kRGB_565_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 205 | return kRGB_565_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 206 | case SkBitmap::kARGB_4444_Config: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 207 | return kRGBA_4444_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | case SkBitmap::kARGB_8888_Config: |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 209 | return kSkia8888_PM_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | default: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 211 | return kUnknown_GrPixelConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |