Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "TextRenderer.h" |
michael@0 | 7 | #include "FontData.h" |
michael@0 | 8 | #include "png.h" |
michael@0 | 9 | #include "mozilla/Base64.h" |
michael@0 | 10 | #include "mozilla/layers/Compositor.h" |
michael@0 | 11 | #include "mozilla/layers/TextureHost.h" |
michael@0 | 12 | #include "mozilla/layers/Effects.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace layers { |
michael@0 | 16 | |
michael@0 | 17 | using namespace gfx; |
michael@0 | 18 | using namespace std; |
michael@0 | 19 | |
michael@0 | 20 | const Float sBackgroundOpacity = 0.6f; |
michael@0 | 21 | const SurfaceFormat sTextureFormat = SurfaceFormat::B8G8R8A8; |
michael@0 | 22 | |
michael@0 | 23 | static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr) |
michael@0 | 24 | { |
michael@0 | 25 | png_read_update_info(png_ptr, info_ptr); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass) |
michael@0 | 29 | { |
michael@0 | 30 | MOZ_ASSERT(sTextureFormat == SurfaceFormat::B8G8R8A8); |
michael@0 | 31 | |
michael@0 | 32 | DataSourceSurface::MappedSurface map = static_cast<TextRenderer*>(png_get_progressive_ptr(png_ptr))->GetSurfaceMap(); |
michael@0 | 33 | |
michael@0 | 34 | uint32_t* dst = (uint32_t*)(map.mData + map.mStride * row_num); |
michael@0 | 35 | |
michael@0 | 36 | for (uint32_t x = 0; x < sTextureWidth; x++) { |
michael@0 | 37 | // We blend to a transparent white background, this will make text readable |
michael@0 | 38 | // even if it's on a dark background. Without hurting our ability to |
michael@0 | 39 | // interact with the content behind the text. |
michael@0 | 40 | Float alphaValue = Float(0xFF - new_row[x]) / 255.0f; |
michael@0 | 41 | Float baseValue = sBackgroundOpacity * (1.0f - alphaValue); |
michael@0 | 42 | Color pixelColor(baseValue, baseValue, baseValue, baseValue + alphaValue); |
michael@0 | 43 | dst[x] = pixelColor.ToABGR(); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | TextRenderer::~TextRenderer() |
michael@0 | 48 | { |
michael@0 | 49 | if (mGlyphBitmaps) { |
michael@0 | 50 | mGlyphBitmaps->Unmap(); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | void |
michael@0 | 55 | TextRenderer::RenderText(const string& aText, const IntPoint& aOrigin, |
michael@0 | 56 | const Matrix4x4& aTransform, uint32_t aTextSize, |
michael@0 | 57 | uint32_t aTargetPixelWidth) |
michael@0 | 58 | { |
michael@0 | 59 | EnsureInitialized(); |
michael@0 | 60 | |
michael@0 | 61 | // For now we only have a bitmap font with a 16px cell size, so we just |
michael@0 | 62 | // scale it up if the user wants larger text. |
michael@0 | 63 | Float scaleFactor = Float(aTextSize) / Float(sCellHeight); |
michael@0 | 64 | |
michael@0 | 65 | aTargetPixelWidth /= scaleFactor; |
michael@0 | 66 | |
michael@0 | 67 | uint32_t numLines = 1; |
michael@0 | 68 | uint32_t maxWidth = 0; |
michael@0 | 69 | uint32_t lineWidth = 0; |
michael@0 | 70 | // Calculate the size of the surface needed to draw all the glyphs. |
michael@0 | 71 | for (uint32_t i = 0; i < aText.length(); i++) { |
michael@0 | 72 | // Insert a line break if we go past the TargetPixelWidth. |
michael@0 | 73 | // XXX - this has the downside of overrunning the intended width, causing |
michael@0 | 74 | // things at the edge of a window to be cut off. |
michael@0 | 75 | if (aText[i] == '\n' || (aText[i] == ' ' && lineWidth > aTargetPixelWidth)) { |
michael@0 | 76 | numLines++; |
michael@0 | 77 | lineWidth = 0; |
michael@0 | 78 | continue; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | lineWidth += sGlyphWidths[uint32_t(aText[i])]; |
michael@0 | 82 | maxWidth = std::max(lineWidth, maxWidth); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // Create a surface to draw our glyphs to. |
michael@0 | 86 | RefPtr<DataSourceSurface> textSurf = |
michael@0 | 87 | Factory::CreateDataSourceSurface(IntSize(maxWidth, numLines * sCellHeight), sTextureFormat); |
michael@0 | 88 | |
michael@0 | 89 | DataSourceSurface::MappedSurface map; |
michael@0 | 90 | textSurf->Map(DataSourceSurface::MapType::READ_WRITE, &map); |
michael@0 | 91 | |
michael@0 | 92 | // Initialize the surface to transparent white. |
michael@0 | 93 | memset(map.mData, uint8_t(sBackgroundOpacity * 255.0f), |
michael@0 | 94 | numLines * sCellHeight * map.mStride); |
michael@0 | 95 | |
michael@0 | 96 | uint32_t currentXPos = 0; |
michael@0 | 97 | uint32_t currentYPos = 0; |
michael@0 | 98 | |
michael@0 | 99 | // Copy our glyphs onto the surface. |
michael@0 | 100 | for (uint32_t i = 0; i < aText.length(); i++) { |
michael@0 | 101 | if (aText[i] == '\n' || (aText[i] == ' ' && currentXPos > aTargetPixelWidth)) { |
michael@0 | 102 | currentYPos += sCellHeight; |
michael@0 | 103 | currentXPos = 0; |
michael@0 | 104 | continue; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | uint32_t glyphXOffset = aText[i] % (sTextureWidth / sCellWidth) * sCellWidth * BytesPerPixel(sTextureFormat); |
michael@0 | 108 | uint32_t truncatedLine = aText[i] / (sTextureWidth / sCellWidth); |
michael@0 | 109 | uint32_t glyphYOffset = truncatedLine * sCellHeight * mMap.mStride; |
michael@0 | 110 | |
michael@0 | 111 | for (int y = 0; y < 16; y++) { |
michael@0 | 112 | memcpy(map.mData + (y + currentYPos) * map.mStride + currentXPos * BytesPerPixel(sTextureFormat), |
michael@0 | 113 | mMap.mData + glyphYOffset + y * mMap.mStride + glyphXOffset, |
michael@0 | 114 | sGlyphWidths[uint32_t(aText[i])] * BytesPerPixel(sTextureFormat)); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | currentXPos += sGlyphWidths[uint32_t(aText[i])]; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | textSurf->Unmap(); |
michael@0 | 121 | |
michael@0 | 122 | RefPtr<DataTextureSource> src = mCompositor->CreateDataTextureSource(); |
michael@0 | 123 | |
michael@0 | 124 | if (!src->Update(textSurf)) { |
michael@0 | 125 | // Upload failed. |
michael@0 | 126 | return; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | RefPtr<EffectRGB> effect = new EffectRGB(src, true, Filter::LINEAR); |
michael@0 | 130 | EffectChain chain; |
michael@0 | 131 | chain.mPrimaryEffect = effect; |
michael@0 | 132 | |
michael@0 | 133 | Matrix4x4 transform = aTransform; |
michael@0 | 134 | transform.Scale(scaleFactor, scaleFactor, 1.0f); |
michael@0 | 135 | mCompositor->DrawQuad(Rect(aOrigin.x, aOrigin.y, maxWidth, numLines * 16), |
michael@0 | 136 | Rect(-10000, -10000, 20000, 20000), chain, 1.0f, transform); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | void |
michael@0 | 140 | TextRenderer::EnsureInitialized() |
michael@0 | 141 | { |
michael@0 | 142 | if (mGlyphBitmaps) { |
michael@0 | 143 | return; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | mGlyphBitmaps = Factory::CreateDataSourceSurface(IntSize(sTextureWidth, sTextureHeight), sTextureFormat); |
michael@0 | 147 | |
michael@0 | 148 | mGlyphBitmaps->Map(DataSourceSurface::MapType::READ_WRITE, &mMap); |
michael@0 | 149 | |
michael@0 | 150 | png_structp png_ptr = NULL; |
michael@0 | 151 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
michael@0 | 152 | |
michael@0 | 153 | png_set_progressive_read_fn(png_ptr, this, info_callback, row_callback, nullptr); |
michael@0 | 154 | png_infop info_ptr = NULL; |
michael@0 | 155 | info_ptr = png_create_info_struct(png_ptr); |
michael@0 | 156 | |
michael@0 | 157 | png_process_data(png_ptr, info_ptr, (uint8_t*)sFontPNG, sizeof(sFontPNG)); |
michael@0 | 158 | |
michael@0 | 159 | png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | } |
michael@0 | 163 | } |