blob: 72a23a65ccba31cc48292f6b76a2c3dec5bf9c23 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#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.com5782d712011-01-21 21:03:59 +000015
16 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
reed@google.comac10a2d2010-12-22 21:39:39 +000017 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.com5782d712011-01-21 21:03:59 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021 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 */
24static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
25 SkASSERT(SkBitmap::kIndex8_Config == bitmap.config());
bsalomon@google.com5782d712011-01-21 21:03:59 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027 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.com5782d712011-01-21 21:03:59 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 memcpy(dst, ctable->lockColors(), ctable->count() * sizeof(SkPMColor));
37 ctable->unlockColors(false);
bsalomon@google.com5782d712011-01-21 21:03:59 +000038
reed@google.comac10a2d2010-12-22 21:39:39 +000039 // always skip a full 256 number of entries, even if we memcpy'd fewer
bsalomon@google.comfea37b52011-04-25 15:51:06 +000040 dst += kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000041
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.com50398bf2011-07-26 20:45:30 +000059GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx,
60 GrContext::TextureKey key,
61 const GrSamplerState& sampler,
62 const SkBitmap& origBitmap) {
reed@google.comac10a2d2010-12-22 21:39:39 +000063 SkAutoLockPixels alp(origBitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000064 GrContext::TextureCacheEntry entry;
65
reed@google.comac10a2d2010-12-22 21:39:39 +000066 if (!origBitmap.readyToDraw()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000067 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +000068 }
69
70 SkBitmap tmpBitmap;
71
72 const SkBitmap* bitmap = &origBitmap;
bsalomon@google.com5782d712011-01-21 21:03:59 +000073
bsalomon@google.comfea37b52011-04-25 15:51:06 +000074 GrTextureDesc desc = {
75 kNone_GrTextureFlags,
76 kNone_GrAALevel,
reed@google.comac10a2d2010-12-22 21:39:39 +000077 bitmap->width(),
78 bitmap->height(),
79 SkGr::Bitmap2PixelConfig(*bitmap)
80 };
bsalomon@google.com5782d712011-01-21 21:03:59 +000081
reed@google.comac10a2d2010-12-22 21:39:39 +000082 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.com5782d712011-01-21 21:03:59 +000087 size_t imagesize = bitmap->width() * bitmap->height() +
bsalomon@google.comfea37b52011-04-25 15:51:06 +000088 kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000089 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +000090
reed@google.comac10a2d2010-12-22 21:39:39 +000091 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.com4ee7ae52011-06-30 17:30:49 +000095
bsalomon@google.com50398bf2011-07-26 20:45:30 +000096 if (gUNCACHED_KEY != key) {
junov@google.com4ee7ae52011-06-30 17:30:49 +000097 return ctx->createAndLockTexture(key, sampler, desc, storage.get(),
98 bitmap->width());
99 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000100 entry = ctx->lockScratchTexture(desc,
101 GrContext::kExact_ScratchTexMatch);
102 entry.texture()->uploadTextureData(0, 0, bitmap->width(),
junov@google.com4ee7ae52011-06-30 17:30:49 +0000103 bitmap->height(), storage.get(), 0);
104 return entry;
105 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
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.com5782d712011-01-21 21:03:59 +0000112 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000113
114 desc.fFormat = SkGr::Bitmap2PixelConfig(*bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000115 if (gUNCACHED_KEY != key) {
116 return ctx->createAndLockTexture(key, sampler, desc,
117 bitmap->getPixels(),
118 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000119 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000120 entry = ctx->lockScratchTexture(desc,
121 GrContext::kExact_ScratchTexMatch);
122 entry.texture()->uploadTextureData(0, 0, bitmap->width(),
junov@google.com4ee7ae52011-06-30 17:30:49 +0000123 bitmap->height(), bitmap->getPixels(), bitmap->rowBytes());
124 return entry;
125 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000126}
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128///////////////////////////////////////////////////////////////////////////////
129
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130void 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
149GrClipType SkGrClipIterator::getType() const {
150 GrAssert(!this->isDone());
scroggo7b118072011-03-23 15:04:26 +0000151 if (NULL == fCurr->fPath) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000152 return kRect_ClipType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 } else {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000154 return kPath_ClipType;
155 }
156}
157
158GrSetOp 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
180GrPathFill 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.comac10a2d2010-12-22 21:39:39 +0000193 }
194}
195
196///////////////////////////////////////////////////////////////////////////////
197
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000198GrPixelConfig SkGr::BitmapConfig2PixelConfig(SkBitmap::Config config,
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 bool isOpaque) {
200 switch (config) {
201 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000202 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000203 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000204 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000206 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000208 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 case SkBitmap::kARGB_8888_Config:
210 if (isOpaque) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000211 return kRGBX_8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 } else {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000213 return kRGBA_8888_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 }
215 default:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000216 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 }
218}
219