blob: 41f50f5cd34351276992da74d7f160facf43623a [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
rileya@google.com24f3ad12012-07-18 21:47:40 +000059static GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx,
robertphillips@google.com75b3c962012-06-07 12:08:45 +000060 uint64_t 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
robertphillips@google.com75b3c962012-06-07 12:08:45 +000074 GrTextureDesc desc;
75 desc.fWidth = bitmap->width();
76 desc.fHeight = bitmap->height();
rileya@google.com24f3ad12012-07-18 21:47:40 +000077 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +000078 desc.fClientCacheID = key;
bsalomon@google.com5782d712011-01-21 21:03:59 +000079
reed@google.comac10a2d2010-12-22 21:39:39 +000080 if (SkBitmap::kIndex8_Config == bitmap->config()) {
81 // build_compressed_data doesn't do npot->pot expansion
82 // and paletted textures can't be sub-updated
83 if (ctx->supportsIndex8PixelConfig(sampler,
84 bitmap->width(), bitmap->height())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +000085 size_t imagesize = bitmap->width() * bitmap->height() +
bsalomon@google.comfea37b52011-04-25 15:51:06 +000086 kGrColorTableSize;
reed@google.comac10a2d2010-12-22 21:39:39 +000087 SkAutoMalloc storage(imagesize);
bsalomon@google.com5782d712011-01-21 21:03:59 +000088
reed@google.comac10a2d2010-12-22 21:39:39 +000089 build_compressed_data(storage.get(), origBitmap);
90
91 // our compressed data will be trimmed, so pass width() for its
92 // "rowBytes", since they are the same now.
junov@google.com4ee7ae52011-06-30 17:30:49 +000093
robertphillips@google.com75b3c962012-06-07 12:08:45 +000094 if (kUncached_CacheID != key) {
95 return ctx->createAndLockTexture(sampler, desc, storage.get(),
junov@google.com4ee7ae52011-06-30 17:30:49 +000096 bitmap->width());
97 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000098 entry = ctx->lockScratchTexture(desc,
99 GrContext::kExact_ScratchTexMatch);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000100 entry.texture()->writePixels(0, 0, bitmap->width(),
101 bitmap->height(), desc.fConfig,
102 storage.get(), 0);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000103 return entry;
104 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
106 } else {
107 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
108 // now bitmap points to our temp, which has been promoted to 32bits
109 bitmap = &tmpBitmap;
110 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000111 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000112
rileya@google.com24f3ad12012-07-18 21:47:40 +0000113 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000114 if (kUncached_CacheID != key) {
115 return ctx->createAndLockTexture(sampler, desc,
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000116 bitmap->getPixels(),
117 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000118 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000119 entry = ctx->lockScratchTexture(desc,
120 GrContext::kExact_ScratchTexMatch);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000121 entry.texture()->writePixels(0, 0,
122 bitmap->width(), bitmap->height(),
123 desc.fConfig,
124 bitmap->getPixels(),
125 bitmap->rowBytes());
junov@google.com4ee7ae52011-06-30 17:30:49 +0000126 return entry;
127 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000128}
129
reed@google.comac10a2d2010-12-22 21:39:39 +0000130///////////////////////////////////////////////////////////////////////////////
131
rileya@google.com24f3ad12012-07-18 21:47:40 +0000132GrContext::TextureCacheEntry GrLockCachedBitmapTexture(GrContext* ctx,
133 const SkBitmap& bitmap, const GrSamplerState* sampler) {
134 GrContext::TextureCacheEntry entry;
135
136 if (!bitmap.isVolatile()) {
137 uint64_t key = bitmap.getGenerationID();
138 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
139
140 GrTextureDesc desc;
141 desc.fWidth = bitmap.width();
142 desc.fHeight = bitmap.height();
143 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
144 desc.fClientCacheID = key;
145
146 entry = ctx->findAndLockTexture(desc, sampler);
147 if (NULL == entry.texture()) {
148 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
149 bitmap);
150 }
151 } else {
152 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
153 sampler, bitmap);
154 }
155 if (NULL == entry.texture()) {
156 GrPrintf("---- failed to create texture for cache [%d %d]\n",
157 bitmap.width(), bitmap.height());
158 }
159 return entry;
160}
161
162void GrUnlockCachedBitmapTexture(GrContext* ctx, GrContext::TextureCacheEntry cache) {
163 ctx->unlockTexture(cache);
164}
165
166///////////////////////////////////////////////////////////////////////////////
167
bsalomon@google.comd302f142011-03-03 13:54:13 +0000168void SkGrClipIterator::reset(const SkClipStack& clipStack) {
169 fClipStack = &clipStack;
170 fIter.reset(clipStack);
171 // Gr has no notion of replace, skip to the
172 // last replace in the clip stack.
173 int lastReplace = 0;
174 int curr = 0;
175 while (NULL != (fCurr = fIter.next())) {
176 if (SkRegion::kReplace_Op == fCurr->fOp) {
177 lastReplace = curr;
178 }
179 ++curr;
180 }
181 fIter.reset(clipStack);
182 for (int i = 0; i < lastReplace+1; ++i) {
183 fCurr = fIter.next();
184 }
185}
186
187GrClipType SkGrClipIterator::getType() const {
188 GrAssert(!this->isDone());
scroggo7b118072011-03-23 15:04:26 +0000189 if (NULL == fCurr->fPath) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000190 return kRect_ClipType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000191 } else {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000192 return kPath_ClipType;
193 }
194}
195
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000196SkRegion::Op SkGrClipIterator::getOp() const {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000197 // we skipped to the last "replace" op
198 // when this iter was reset.
199 // GrClip doesn't allow replace, so treat it as
200 // intersect.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000201 if (SkRegion::kReplace_Op == fCurr->fOp) {
202 return SkRegion::kIntersect_Op;
203 }
204
205 return fCurr->fOp;
206
bsalomon@google.comd302f142011-03-03 13:54:13 +0000207}
208
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000209bool SkGrClipIterator::getDoAA() const {
210 return fCurr->fDoAA;
211}
212
bsalomon@google.comd302f142011-03-03 13:54:13 +0000213GrPathFill SkGrClipIterator::getPathFill() const {
214 switch (fCurr->fPath->getFillType()) {
215 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000216 return kWinding_GrPathFill;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000217 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000218 return kEvenOdd_GrPathFill;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000219 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000220 return kInverseWinding_GrPathFill;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000221 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000222 return kInverseEvenOdd_GrPathFill;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000223 default:
224 GrCrash("Unsupported path fill in clip.");
bsalomon@google.com47059542012-06-06 20:51:20 +0000225 return kWinding_GrPathFill; // suppress warning
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 }
227}
228
229///////////////////////////////////////////////////////////////////////////////
230
rileya@google.com24f3ad12012-07-18 21:47:40 +0000231GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 switch (config) {
233 case SkBitmap::kA8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000234 return kAlpha_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 case SkBitmap::kIndex8_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000236 return kIndex_8_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 case SkBitmap::kRGB_565_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000238 return kRGB_565_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 case SkBitmap::kARGB_4444_Config:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000240 return kRGBA_4444_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 case SkBitmap::kARGB_8888_Config:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000242 return kSkia8888_PM_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 default:
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000244 // kNo_Config, kA1_Config missing, and kRLE_Index8_Config
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 return kUnknown_GrPixelConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 }
247}
248