Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2010 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #include "GrAtlas.h" |
michael@0 | 10 | #include "GrContext.h" |
michael@0 | 11 | #include "GrGpu.h" |
michael@0 | 12 | #include "GrRectanizer.h" |
michael@0 | 13 | |
michael@0 | 14 | #if 0 |
michael@0 | 15 | #define GR_PLOT_WIDTH 8 |
michael@0 | 16 | #define GR_PLOT_HEIGHT 4 |
michael@0 | 17 | #define GR_ATLAS_WIDTH 256 |
michael@0 | 18 | #define GR_ATLAS_HEIGHT 256 |
michael@0 | 19 | |
michael@0 | 20 | #define GR_ATLAS_TEXTURE_WIDTH (GR_PLOT_WIDTH * GR_ATLAS_WIDTH) |
michael@0 | 21 | #define GR_ATLAS_TEXTURE_HEIGHT (GR_PLOT_HEIGHT * GR_ATLAS_HEIGHT) |
michael@0 | 22 | |
michael@0 | 23 | #else |
michael@0 | 24 | |
michael@0 | 25 | #define GR_ATLAS_TEXTURE_WIDTH 1024 |
michael@0 | 26 | #define GR_ATLAS_TEXTURE_HEIGHT 2048 |
michael@0 | 27 | |
michael@0 | 28 | #define GR_ATLAS_WIDTH 256 |
michael@0 | 29 | #define GR_ATLAS_HEIGHT 256 |
michael@0 | 30 | |
michael@0 | 31 | #define GR_PLOT_WIDTH (GR_ATLAS_TEXTURE_WIDTH / GR_ATLAS_WIDTH) |
michael@0 | 32 | #define GR_PLOT_HEIGHT (GR_ATLAS_TEXTURE_HEIGHT / GR_ATLAS_HEIGHT) |
michael@0 | 33 | |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 37 | |
michael@0 | 38 | // for testing |
michael@0 | 39 | #define FONT_CACHE_STATS 0 |
michael@0 | 40 | #if FONT_CACHE_STATS |
michael@0 | 41 | static int g_UploadCount = 0; |
michael@0 | 42 | #endif |
michael@0 | 43 | |
michael@0 | 44 | GrPlot::GrPlot() : fDrawToken(NULL, 0) |
michael@0 | 45 | , fTexture(NULL) |
michael@0 | 46 | , fAtlasMgr(NULL) |
michael@0 | 47 | , fBytesPerPixel(1) |
michael@0 | 48 | { |
michael@0 | 49 | fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH, |
michael@0 | 50 | GR_ATLAS_HEIGHT); |
michael@0 | 51 | fOffset.set(0, 0); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | GrPlot::~GrPlot() { |
michael@0 | 55 | delete fRects; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | static inline void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset) { |
michael@0 | 59 | loc->fX += offset.fX * GR_ATLAS_WIDTH; |
michael@0 | 60 | loc->fY += offset.fY * GR_ATLAS_HEIGHT; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | bool GrPlot::addSubImage(int width, int height, const void* image, |
michael@0 | 64 | GrIPoint16* loc) { |
michael@0 | 65 | if (!fRects->addRect(width, height, loc)) { |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | SkAutoSMalloc<1024> storage; |
michael@0 | 70 | adjust_for_offset(loc, fOffset); |
michael@0 | 71 | GrContext* context = fTexture->getContext(); |
michael@0 | 72 | // We pass the flag that does not force a flush. We assume our caller is |
michael@0 | 73 | // smart and hasn't referenced the part of the texture we're about to update |
michael@0 | 74 | // since the last flush. |
michael@0 | 75 | context->writeTexturePixels(fTexture, |
michael@0 | 76 | loc->fX, loc->fY, width, height, |
michael@0 | 77 | fTexture->config(), image, 0, |
michael@0 | 78 | GrContext::kDontFlush_PixelOpsFlag); |
michael@0 | 79 | |
michael@0 | 80 | #if FONT_CACHE_STATS |
michael@0 | 81 | ++g_UploadCount; |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | return true; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | void GrPlot::resetRects() { |
michael@0 | 88 | SkASSERT(NULL != fRects); |
michael@0 | 89 | fRects->reset(); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 93 | |
michael@0 | 94 | GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config) { |
michael@0 | 95 | fGpu = gpu; |
michael@0 | 96 | fPixelConfig = config; |
michael@0 | 97 | gpu->ref(); |
michael@0 | 98 | fTexture = NULL; |
michael@0 | 99 | |
michael@0 | 100 | // set up allocated plots |
michael@0 | 101 | size_t bpp = GrBytesPerPixel(fPixelConfig); |
michael@0 | 102 | fPlotArray = SkNEW_ARRAY(GrPlot, (GR_PLOT_WIDTH*GR_PLOT_HEIGHT)); |
michael@0 | 103 | |
michael@0 | 104 | GrPlot* currPlot = fPlotArray; |
michael@0 | 105 | for (int y = GR_PLOT_HEIGHT-1; y >= 0; --y) { |
michael@0 | 106 | for (int x = GR_PLOT_WIDTH-1; x >= 0; --x) { |
michael@0 | 107 | currPlot->fAtlasMgr = this; |
michael@0 | 108 | currPlot->fOffset.set(x, y); |
michael@0 | 109 | currPlot->fBytesPerPixel = bpp; |
michael@0 | 110 | |
michael@0 | 111 | // build LRU list |
michael@0 | 112 | fPlotList.addToHead(currPlot); |
michael@0 | 113 | ++currPlot; |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | GrAtlasMgr::~GrAtlasMgr() { |
michael@0 | 119 | SkSafeUnref(fTexture); |
michael@0 | 120 | SkDELETE_ARRAY(fPlotArray); |
michael@0 | 121 | |
michael@0 | 122 | fGpu->unref(); |
michael@0 | 123 | #if FONT_CACHE_STATS |
michael@0 | 124 | GrPrintf("Num uploads: %d\n", g_UploadCount); |
michael@0 | 125 | #endif |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | void GrAtlasMgr::moveToHead(GrPlot* plot) { |
michael@0 | 129 | if (fPlotList.head() == plot) { |
michael@0 | 130 | return; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | fPlotList.remove(plot); |
michael@0 | 134 | fPlotList.addToHead(plot); |
michael@0 | 135 | }; |
michael@0 | 136 | |
michael@0 | 137 | GrPlot* GrAtlasMgr::addToAtlas(GrAtlas* atlas, |
michael@0 | 138 | int width, int height, const void* image, |
michael@0 | 139 | GrIPoint16* loc) { |
michael@0 | 140 | // iterate through entire plot list for this atlas, see if we can find a hole |
michael@0 | 141 | // last one was most recently added and probably most empty |
michael@0 | 142 | for (int i = atlas->fPlots.count()-1; i >= 0; --i) { |
michael@0 | 143 | GrPlot* plot = atlas->fPlots[i]; |
michael@0 | 144 | if (plot->addSubImage(width, height, image, loc)) { |
michael@0 | 145 | this->moveToHead(plot); |
michael@0 | 146 | return plot; |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // before we get a new plot, make sure we have a backing texture |
michael@0 | 151 | if (NULL == fTexture) { |
michael@0 | 152 | // TODO: Update this to use the cache rather than directly creating a texture. |
michael@0 | 153 | GrTextureDesc desc; |
michael@0 | 154 | desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
michael@0 | 155 | desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; |
michael@0 | 156 | desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; |
michael@0 | 157 | desc.fConfig = fPixelConfig; |
michael@0 | 158 | |
michael@0 | 159 | fTexture = fGpu->createTexture(desc, NULL, 0); |
michael@0 | 160 | if (NULL == fTexture) { |
michael@0 | 161 | return NULL; |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | // now look through all allocated plots for one we can share, in MRU order |
michael@0 | 166 | GrPlotList::Iter plotIter; |
michael@0 | 167 | plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart); |
michael@0 | 168 | GrPlot* plot; |
michael@0 | 169 | while (NULL != (plot = plotIter.get())) { |
michael@0 | 170 | // make sure texture is set for quick lookup |
michael@0 | 171 | plot->fTexture = fTexture; |
michael@0 | 172 | if (plot->addSubImage(width, height, image, loc)) { |
michael@0 | 173 | this->moveToHead(plot); |
michael@0 | 174 | // new plot for atlas, put at end of array |
michael@0 | 175 | *(atlas->fPlots.append()) = plot; |
michael@0 | 176 | return plot; |
michael@0 | 177 | } |
michael@0 | 178 | plotIter.next(); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | // If the above fails, then the current plot list has no room |
michael@0 | 182 | return NULL; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | bool GrAtlasMgr::removePlot(GrAtlas* atlas, const GrPlot* plot) { |
michael@0 | 186 | // iterate through plot list for this atlas |
michael@0 | 187 | int count = atlas->fPlots.count(); |
michael@0 | 188 | for (int i = 0; i < count; ++i) { |
michael@0 | 189 | if (plot == atlas->fPlots[i]) { |
michael@0 | 190 | atlas->fPlots.remove(i); |
michael@0 | 191 | return true; |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | return false; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | // get a plot that's not being used by the current draw |
michael@0 | 199 | GrPlot* GrAtlasMgr::getUnusedPlot() { |
michael@0 | 200 | GrPlotList::Iter plotIter; |
michael@0 | 201 | plotIter.init(fPlotList, GrPlotList::Iter::kTail_IterStart); |
michael@0 | 202 | GrPlot* plot; |
michael@0 | 203 | while (NULL != (plot = plotIter.get())) { |
michael@0 | 204 | if (plot->drawToken().isIssued()) { |
michael@0 | 205 | return plot; |
michael@0 | 206 | } |
michael@0 | 207 | plotIter.prev(); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | return NULL; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | SkISize GrAtlas::getSize() const { |
michael@0 | 214 | return SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, GR_ATLAS_TEXTURE_HEIGHT); |
michael@0 | 215 | } |