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 "GrAtlas.h" |
| 12 | #include "GrGpu.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrRectanizer.h" |
| 14 | #include "GrPlotMgr.h" |
| 15 | |
| 16 | #if 0 |
| 17 | #define GR_PLOT_WIDTH 8 |
| 18 | #define GR_PLOT_HEIGHT 4 |
| 19 | #define GR_ATLAS_WIDTH 256 |
| 20 | #define GR_ATLAS_HEIGHT 256 |
| 21 | |
| 22 | #define GR_ATLAS_TEXTURE_WIDTH (GR_PLOT_WIDTH * GR_ATLAS_WIDTH) |
| 23 | #define GR_ATLAS_TEXTURE_HEIGHT (GR_PLOT_HEIGHT * GR_ATLAS_HEIGHT) |
| 24 | |
| 25 | #else |
| 26 | |
| 27 | #define GR_ATLAS_TEXTURE_WIDTH 1024 |
| 28 | #define GR_ATLAS_TEXTURE_HEIGHT 2048 |
| 29 | |
| 30 | #define GR_ATLAS_WIDTH 341 |
| 31 | #define GR_ATLAS_HEIGHT 341 |
| 32 | |
| 33 | #define GR_PLOT_WIDTH (GR_ATLAS_TEXTURE_WIDTH / GR_ATLAS_WIDTH) |
| 34 | #define GR_PLOT_HEIGHT (GR_ATLAS_TEXTURE_HEIGHT / GR_ATLAS_HEIGHT) |
| 35 | |
| 36 | #endif |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
| 40 | #define BORDER 1 |
| 41 | |
| 42 | #if GR_DEBUG |
| 43 | static int gCounter; |
| 44 | #endif |
| 45 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 46 | GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | fAtlasMgr = mgr; // just a pointer, not an owner |
| 48 | fNext = NULL; |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 49 | fTexture = mgr->getTexture(format); // we're not an owner, just a pointer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | fPlot.set(plotX, plotY); |
| 51 | |
| 52 | fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, |
| 53 | GR_ATLAS_HEIGHT - BORDER); |
| 54 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 55 | fMaskFormat = format; |
| 56 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | #if GR_DEBUG |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 58 | // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | gCounter += 1; |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | GrAtlas::~GrAtlas() { |
| 64 | fAtlasMgr->freePlot(fPlot.fX, fPlot.fY); |
| 65 | |
| 66 | delete fRects; |
| 67 | |
| 68 | #if GR_DEBUG |
| 69 | --gCounter; |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 70 | // GrPrintf("~GrAtlas %p [%d %d] %d\n", this, fPlot.fX, fPlot.fY, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | #endif |
| 72 | } |
| 73 | |
| 74 | static void adjustForPlot(GrIPoint16* loc, const GrIPoint16& plot) { |
| 75 | loc->fX += plot.fX * GR_ATLAS_WIDTH; |
| 76 | loc->fY += plot.fY * GR_ATLAS_HEIGHT; |
| 77 | } |
| 78 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 79 | static uint8_t* zerofill(uint8_t* ptr, int count) { |
| 80 | while (--count >= 0) { |
| 81 | *ptr++ = 0; |
| 82 | } |
| 83 | return ptr; |
| 84 | } |
| 85 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | bool GrAtlas::addSubImage(int width, int height, const void* image, |
| 87 | GrIPoint16* loc) { |
| 88 | if (!fRects->addRect(width + BORDER, height + BORDER, loc)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 92 | SkAutoSMalloc<1024> storage; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 93 | int dstW = width + 2*BORDER; |
| 94 | int dstH = height + 2*BORDER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | if (BORDER) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 96 | const int bpp = GrMaskFormatBytesPerPixel(fMaskFormat); |
| 97 | const size_t dstRB = dstW * bpp; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame^] | 98 | uint8_t* dst = (uint8_t*)storage.reset(dstH * dstRB); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 99 | Gr_bzero(dst, dstRB); // zero top row |
| 100 | dst += dstRB; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | for (int y = 0; y < height; y++) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 102 | dst = zerofill(dst, bpp); // zero left edge |
| 103 | memcpy(dst, image, width * bpp); |
| 104 | dst += width * bpp; |
| 105 | dst = zerofill(dst, bpp); // zero right edge |
| 106 | image = (const void*)((const char*)image + width * bpp); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | } |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 108 | Gr_bzero(dst, dstRB); // zero bottom row |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | image = storage.get(); |
| 110 | } |
| 111 | adjustForPlot(loc, fPlot); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 112 | fTexture->uploadTextureData(loc->fX, loc->fY, dstW, dstH, image, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | |
| 114 | // now tell the caller to skip the top/left BORDER |
| 115 | loc->fX += BORDER; |
| 116 | loc->fY += BORDER; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) { |
| 123 | fGpu = gpu; |
| 124 | gpu->ref(); |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 125 | Gr_bzero(fTexture, sizeof(fTexture)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | fPlotMgr = new GrPlotMgr(GR_PLOT_WIDTH, GR_PLOT_HEIGHT); |
| 127 | } |
| 128 | |
| 129 | GrAtlasMgr::~GrAtlasMgr() { |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 130 | for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) { |
| 131 | GrSafeUnref(fTexture[i]); |
| 132 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | delete fPlotMgr; |
| 134 | fGpu->unref(); |
| 135 | } |
| 136 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 137 | static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 138 | switch (format) { |
| 139 | case kA8_GrMaskFormat: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 140 | return kAlpha_8_GrPixelConfig; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 141 | case kA565_GrMaskFormat: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 142 | return kRGB_565_GrPixelConfig; |
caryclark@google.com | 1eeaf0b | 2011-06-22 13:19:43 +0000 | [diff] [blame] | 143 | case kA888_GrMaskFormat: |
| 144 | return kRGBA_8888_GrPixelConfig; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 145 | default: |
| 146 | GrAssert(!"unknown maskformat"); |
| 147 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 148 | return kUnknown_GrPixelConfig; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 149 | } |
| 150 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas* atlas, |
| 152 | int width, int height, const void* image, |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 153 | GrMaskFormat format, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | GrIPoint16* loc) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 155 | GrAssert(NULL == atlas || atlas->getMaskFormat() == format); |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 156 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 157 | if (atlas && atlas->addSubImage(width, height, image, loc)) { |
| 158 | return atlas; |
| 159 | } |
| 160 | |
| 161 | // If the above fails, then either we have no starting atlas, or the current |
| 162 | // one is full. Either way we need to allocate a new atlas |
| 163 | |
| 164 | GrIPoint16 plot; |
| 165 | if (!fPlotMgr->newPlot(&plot)) { |
| 166 | return NULL; |
| 167 | } |
| 168 | |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 169 | GrAssert(0 == kA8_GrMaskFormat); |
| 170 | GrAssert(1 == kA565_GrMaskFormat); |
| 171 | if (NULL == fTexture[format]) { |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 172 | GrTextureDesc desc = { |
| 173 | kDynamicUpdate_GrTextureFlagBit, |
| 174 | kNone_GrAALevel, |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 175 | GR_ATLAS_TEXTURE_WIDTH, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | GR_ATLAS_TEXTURE_HEIGHT, |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 177 | maskformat2pixelconfig(format) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | }; |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 179 | fTexture[format] = fGpu->createTexture(desc, NULL, 0); |
| 180 | if (NULL == fTexture[format]) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
| 183 | } |
| 184 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 185 | GrAtlas* newAtlas = new GrAtlas(this, plot.fX, plot.fY, format); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | if (!newAtlas->addSubImage(width, height, image, loc)) { |
| 187 | delete newAtlas; |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | newAtlas->fNext = atlas; |
| 192 | return newAtlas; |
| 193 | } |
| 194 | |
| 195 | void GrAtlasMgr::freePlot(int x, int y) { |
| 196 | GrAssert(fPlotMgr->isBusy(x, y)); |
| 197 | fPlotMgr->freePlot(x, y); |
| 198 | } |
| 199 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | |