blob: ba98e06f8fbcb066436e63fabab4ac29b47840f9 [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()) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000029 SkDEBUGFAIL("bitmap not ready to draw!");
reed@google.comac10a2d2010-12-22 21:39:39 +000030 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,
bsalomon@google.com1fadb202011-12-12 16:10:08 +000061 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +000062 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,
reed@google.comac10a2d2010-12-22 21:39:39 +000076 bitmap->width(),
77 bitmap->height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +000078 SkGr::Bitmap2PixelConfig(*bitmap),
bsalomon@google.comb9014f42012-03-30 14:22:41 +000079 0 // samples
reed@google.comac10a2d2010-12-22 21:39:39 +000080 };
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);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000102 entry.texture()->writePixels(0, 0, bitmap->width(),
103 bitmap->height(), desc.fConfig,
104 storage.get(), 0);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000105 return entry;
106 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000107
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.com5782d712011-01-21 21:03:59 +0000113 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000115 desc.fConfig = SkGr::Bitmap2PixelConfig(*bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000116 if (gUNCACHED_KEY != key) {
117 return ctx->createAndLockTexture(key, sampler, desc,
118 bitmap->getPixels(),
119 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000120 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000121 entry = ctx->lockScratchTexture(desc,
122 GrContext::kExact_ScratchTexMatch);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000123 entry.texture()->writePixels(0, 0,
124 bitmap->width(), bitmap->height(),
125 desc.fConfig,
126 bitmap->getPixels(),
127 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000128 return entry;
129 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000130}
131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132///////////////////////////////////////////////////////////////////////////////
133
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134void 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
153GrClipType SkGrClipIterator::getType() const {
154 GrAssert(!this->isDone());
scroggo7b118072011-03-23 15:04:26 +0000155 if (NULL == fCurr->fPath) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000156 return kRect_ClipType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 } else {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000158 return kPath_ClipType;
159 }
160}
161
162GrSetOp SkGrClipIterator::getOp() const {
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.
167 GrSetOp skToGrOps[] = {
168 kDifference_SetOp, // kDifference_Op
169 kIntersect_SetOp, // kIntersect_Op
170 kUnion_SetOp, // kUnion_Op
171 kXor_SetOp, // kXOR_Op
172 kReverseDifference_SetOp, // kReverseDifference_Op
173 kIntersect_SetOp // kReplace_op
174 };
175 GR_STATIC_ASSERT(0 == SkRegion::kDifference_Op);
176 GR_STATIC_ASSERT(1 == SkRegion::kIntersect_Op);
177 GR_STATIC_ASSERT(2 == SkRegion::kUnion_Op);
178 GR_STATIC_ASSERT(3 == SkRegion::kXOR_Op);
179 GR_STATIC_ASSERT(4 == SkRegion::kReverseDifference_Op);
180 GR_STATIC_ASSERT(5 == SkRegion::kReplace_Op);
181 return skToGrOps[fCurr->fOp];
182}
183
184GrPathFill SkGrClipIterator::getPathFill() const {
185 switch (fCurr->fPath->getFillType()) {
186 case SkPath::kWinding_FillType:
187 return kWinding_PathFill;
188 case SkPath::kEvenOdd_FillType:
189 return kEvenOdd_PathFill;
190 case SkPath::kInverseWinding_FillType:
191 return kInverseWinding_PathFill;
192 case SkPath::kInverseEvenOdd_FillType:
193 return kInverseEvenOdd_PathFill;
194 default:
195 GrCrash("Unsupported path fill in clip.");
196 return kWinding_PathFill; // suppress warning
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 }
198}
199
200///////////////////////////////////////////////////////////////////////////////
201
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000202GrPixelConfig SkGr::BitmapConfig2PixelConfig(SkBitmap::Config config,
reed@google.comac10a2d2010-12-22 21:39:39 +0000203 bool isOpaque) {
204 switch (config) {
205 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000206 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000208 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000210 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000212 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000214 return kSkia8888_PM_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 default:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000216 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 }
218}
219