Update with current status
[gnash.git] / libcore / Bitmap.cpp
blob24024ae13ca5b727bebb861305d669c6d98feb40
1 //
2 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "Bitmap.h"
21 #include "flash/display/BitmapData_as.h"
22 #include "GnashImage.h"
23 #include "FillStyle.h"
24 #include "DynamicShape.h"
25 #include "SWFRect.h"
26 #include "Renderer.h"
27 #include "VM.h"
28 #include "movie_root.h"
29 #include "RunResources.h"
30 #include "Transform.h"
31 #include "BitmapMovieDefinition.h"
33 namespace gnash {
35 Bitmap::Bitmap(movie_root& mr, as_object* object, BitmapData_as* bd,
36 DisplayObject* parent)
38 DisplayObject(mr, object, parent),
39 _bitmapData(bd),
40 _width(_bitmapData->width()),
41 _height(_bitmapData->height())
43 _shape.setBounds(SWFRect(0, 0,
44 pixelsToTwips(_width), pixelsToTwips(_height)));
45 assert(bd);
46 assert(!bd->disposed());
49 Bitmap::Bitmap(movie_root& mr, as_object* object,
50 const BitmapMovieDefinition* def, DisplayObject* parent)
52 DisplayObject(mr, object, parent),
53 _def(def),
54 _bitmapData(nullptr),
55 _width(def->get_width_pixels()),
56 _height(def->get_height_pixels())
58 _shape.setBounds(def->get_frame_size());
61 Bitmap::~Bitmap()
65 const CachedBitmap*
66 Bitmap::bitmap() const
68 if (_def) return _def->bitmap();
69 if (_bitmapData) return _bitmapData->bitmapInfo();
70 return nullptr;
73 void
74 Bitmap::construct(as_object* /*init*/)
76 if (_bitmapData) _bitmapData->attach(this);
78 if (!_def && !_bitmapData) return;
80 // Width and height are a maximum of 2880, so there is no risk of
81 // overflow
82 const int w = pixelsToTwips(_width);
83 const int h = pixelsToTwips(_height);
85 SWFMatrix mat;
86 mat.set_scale(1.0 / 20, 1.0 / 20);
88 // Can this be tiled? And smoothing?
89 FillStyle fill = BitmapFill(BitmapFill::CLIPPED, bitmap(), mat,
90 BitmapFill::SMOOTHING_UNSPECIFIED);
92 const size_t fillLeft = _shape.addFillStyle(fill);
94 Path bmpath(w, h, fillLeft, 0, 0);
95 bmpath.drawLineTo(w, 0);
96 bmpath.drawLineTo(0, 0);
97 bmpath.drawLineTo(0, h);
98 bmpath.drawLineTo(w, h);
100 _shape.add_path(bmpath);
101 _shape.setBounds(SWFRect(0, 0, w, h));
102 _shape.finalize();
104 set_invalidated();
107 bool
108 Bitmap::pointInShape(std::int32_t x, std::int32_t y) const
110 return pointInBounds(x, y);
113 void
114 Bitmap::display(Renderer& renderer, const Transform& base)
116 /// Don't display cleared Bitmaps.
117 if (!_def && !_bitmapData) return;
119 const Transform xform = base * transform();
121 _shape.display(renderer, xform);
122 clear_invalidated();
125 void
126 Bitmap::add_invalidated_bounds(InvalidatedRanges& ranges, bool force)
128 if (!force && !invalidated()) return;
130 ranges.add(m_old_invalidated_ranges);
132 SWFRect bounds;
133 bounds.expand_to_transformed_rect(getWorldMatrix(*this), getBounds());
134 ranges.add(bounds.getRange());
138 SWFRect
139 Bitmap::getBounds() const
141 return _shape.getBounds();
144 void
145 Bitmap::update()
147 /// Nothing to do for disposed bitmaps.
148 if (!_bitmapData) return;
150 set_invalidated();
152 if (_bitmapData->disposed()) {
153 _bitmapData = nullptr;
154 _shape.clear();