bool GraphOperations::_euler_tour(const Graph &graph, vertex_t vertex, component_t& path, std::set<size_t>& visited) { if (visited.size() == graph.number_of_edges()) { path.push_back(vertex); return true; } for (const auto& adjacent : graph.adjacent(vertex)) { if (! visited.count(adjacent.second)) { auto copy = visited; copy.insert(adjacent.second); if (_euler_tour(graph, adjacent.first, path, copy)) { path.push_back(vertex); return true; } } } return false; }
void GGLAssembler::expand(integer_t& d, const component_t& s, int dbits) { if (s.l || (s.flags & CLEAR_HI)) { extract(d, s.reg, s.h, s.l, 32); expand(d, d, dbits); } else { expand(d, integer_t(s.reg, s.size(), s.flags), dbits); } }
void GGLAssembler::component_sat(const component_t& v) { const int one = ((1<<v.size())-1)<<v.l; CMP(AL, v.reg, imm( 1<<v.h )); if (isValidImmediate(one)) { MOV(HS, 0, v.reg, imm( one )); } else if (isValidImmediate(~one)) { MVN(HS, 0, v.reg, imm( ~one )); } else { MOV(HS, 0, v.reg, imm( 1<<v.h )); SUB(HS, 0, v.reg, v.reg, imm( 1<<v.l )); } }
void ConnectedComponents::_dfs(const Graph& graph, vertex_t vertex, bitset_t& visited, component_t& component, id_t id) { visited[vertex] = true; _ids[vertex] = id; component.push_back(vertex); for (auto adjacent : graph.adjacent(vertex)) { if (! visited[adjacent.first]) { _dfs(graph, adjacent.first, visited, component, id); } } }
void GGLAssembler::build_blending( component_t& temp, // incomming fragment / output const pixel_t& pixel, // framebuffer int component, Scratch& regs) { if (!mInfo[component].blend) return; int fs = component==GGLFormat::ALPHA ? mBlendSrcA : mBlendSrc; int fd = component==GGLFormat::ALPHA ? mBlendDstA : mBlendDst; if (fs==GGL_SRC_ALPHA_SATURATE && component==GGLFormat::ALPHA) fs = GGL_ONE; const int blending = blending_codes(fs, fd); if (!temp.size()) { // here, blending will produce something which doesn't depend on // that component (eg: GL_ZERO:GL_*), so the register has not been // allocated yet. Will never be used as a source. temp = component_t(regs.obtain(), CORRUPTIBLE); } // we are doing real blending... // fb: extracted dst // fragment: extracted src // temp: component_t(fragment) and result // scoped register allocator Scratch scratches(registerFile()); comment("blending"); // we can optimize these cases a bit... // (1) saturation is not needed // (2) we can use only one multiply instead of 2 // (3) we can reduce the register pressure // R = S*f + D*(1-f) = (S-D)*f + D // R = S*(1-f) + D*f = (D-S)*f + S const bool same_factor_opt1 = (fs==GGL_DST_COLOR && fd==GGL_ONE_MINUS_DST_COLOR) || (fs==GGL_SRC_COLOR && fd==GGL_ONE_MINUS_SRC_COLOR) || (fs==GGL_DST_ALPHA && fd==GGL_ONE_MINUS_DST_ALPHA) || (fs==GGL_SRC_ALPHA && fd==GGL_ONE_MINUS_SRC_ALPHA); const bool same_factor_opt2 = (fs==GGL_ONE_MINUS_DST_COLOR && fd==GGL_DST_COLOR) || (fs==GGL_ONE_MINUS_SRC_COLOR && fd==GGL_SRC_COLOR) || (fs==GGL_ONE_MINUS_DST_ALPHA && fd==GGL_DST_ALPHA) || (fs==GGL_ONE_MINUS_SRC_ALPHA && fd==GGL_SRC_ALPHA); // XXX: we could also optimize these cases: // R = S*f + D*f = (S+D)*f // R = S*(1-f) + D*(1-f) = (S+D)*(1-f) // R = S*D + D*S = 2*S*D // see if we need to extract 'component' from the destination (fb) integer_t fb; if (blending & (BLEND_DST|FACTOR_DST)) { fb.setTo(scratches.obtain(), 32); extract(fb, pixel, component); if (mDithering) { // XXX: maybe what we should do instead, is simply // expand fb -or- fragment to the larger of the two if (fb.size() < temp.size()) { // for now we expand 'fb' to min(fragment, 8) int new_size = temp.size() < 8 ? temp.size() : 8; expand(fb, fb, new_size); } } } // convert input fragment to integer_t if (temp.l && (temp.flags & CORRUPTIBLE)) { MOV(AL, 0, temp.reg, reg_imm(temp.reg, LSR, temp.l)); temp.h -= temp.l; temp.l = 0; } integer_t fragment(temp.reg, temp.size(), temp.flags); // if not done yet, convert input fragment to integer_t if (temp.l) { // here we know temp is not CORRUPTIBLE fragment.reg = scratches.obtain(); MOV(AL, 0, fragment.reg, reg_imm(temp.reg, LSR, temp.l)); fragment.flags |= CORRUPTIBLE; } if (!(temp.flags & CORRUPTIBLE)) { // temp is not corruptible, but since it's the destination it // will be modified, so we need to allocate a new register. temp.reg = regs.obtain(); temp.flags &= ~CORRUPTIBLE; fragment.flags &= ~CORRUPTIBLE; } if ((blending & BLEND_SRC) && !same_factor_opt1) { // source (fragment) is needed for the blending stage // so it's not CORRUPTIBLE (unless we're doing same_factor_opt1) fragment.flags &= ~CORRUPTIBLE; } if (same_factor_opt1) { // R = S*f + D*(1-f) = (S-D)*f + D integer_t factor; build_blend_factor(factor, fs, component, pixel, fragment, fb, scratches); // fb is always corruptible from this point fb.flags |= CORRUPTIBLE; build_blendFOneMinusF(temp, factor, fragment, fb); } else if (same_factor_opt2) { // R = S*(1-f) + D*f = (D-S)*f + S integer_t factor; // fb is always corrruptible here fb.flags |= CORRUPTIBLE; build_blend_factor(factor, fd, component, pixel, fragment, fb, scratches); build_blendOneMinusFF(temp, factor, fragment, fb); } else { integer_t src_factor; integer_t dst_factor; // if destination (fb) is not needed for the blending stage, // then it can be marked as CORRUPTIBLE if (!(blending & BLEND_DST)) { fb.flags |= CORRUPTIBLE; } // XXX: try to mark some registers as CORRUPTIBLE // in most case we could make those corruptible // when we're processing the last component // but not always, for instance // when fragment is constant and not reloaded // when fb is needed for logic-ops or masking // when a register is aliased (for instance with mAlphaSource) // blend away... if (fs==GGL_ZERO) { if (fd==GGL_ZERO) { // R = 0 // already taken care of } else if (fd==GGL_ONE) { // R = D // already taken care of } else { // R = D*fd // compute fd build_blend_factor(dst_factor, fd, component, pixel, fragment, fb, scratches); mul_factor(temp, fb, dst_factor); } } else if (fs==GGL_ONE) { if (fd==GGL_ZERO) { // R = S // NOP, taken care of } else if (fd==GGL_ONE) { // R = S + D component_add(temp, fb, fragment); // args order matters component_sat(temp); } else { // R = S + D*fd // compute fd build_blend_factor(dst_factor, fd, component, pixel, fragment, fb, scratches); mul_factor_add(temp, fb, dst_factor, component_t(fragment)); component_sat(temp); } } else { // compute fs build_blend_factor(src_factor, fs, component, pixel, fragment, fb, scratches); if (fd==GGL_ZERO) { // R = S*fs mul_factor(temp, fragment, src_factor); } else if (fd==GGL_ONE) { // R = S*fs + D mul_factor_add(temp, fragment, src_factor, component_t(fb)); component_sat(temp); } else { // R = S*fs + D*fd mul_factor(temp, fragment, src_factor); if (scratches.isUsed(src_factor.reg)) scratches.recycle(src_factor.reg); // compute fd build_blend_factor(dst_factor, fd, component, pixel, fragment, fb, scratches); mul_factor_add(temp, fb, dst_factor, temp); if (!same_factor_opt1 && !same_factor_opt2) { component_sat(temp); } } } } // now we can be corrupted (it's the dest) temp.flags |= CORRUPTIBLE; }
void GGLAssembler::mul_factor_add( component_t& d, const integer_t& v, const integer_t& f, const component_t& a) { // XXX: we could have special cases for 1 bit mul Scratch scratches(registerFile()); int vs = v.size(); int fs = f.size(); int as = a.h; int ms = vs+fs; ALOGE_IF(ms>=32, "mul_factor_add overflow vs=%d, fs=%d, as=%d", vs, fs, as); integer_t add(a.reg, a.h, a.flags); // 'a' is a component_t but it is guaranteed to have // its high bits set to 0. However in the dithering case, // we can't get away with truncating the potentially bad bits // so extraction is needed. if ((mDithering) && (a.size() < ms)) { // we need to expand a if (!(a.flags & CORRUPTIBLE)) { // ... but it's not corruptible, so we need to pick a // temporary register. // Try to uses the destination register first (it's likely // to be usable, unless it aliases an input). if (d.reg!=a.reg && d.reg!=v.reg && d.reg!=f.reg) { add.reg = d.reg; } else { add.reg = scratches.obtain(); } } expand(add, a, ms); // extracts and expands as = ms; } if (ms == as) { if (vs<16 && fs<16) SMLABB(AL, d.reg, v.reg, f.reg, add.reg); else MLA(AL, 0, d.reg, v.reg, f.reg, add.reg); } else { int temp = d.reg; if (temp == add.reg) { // the mul will modify add.reg, we need an intermediary reg if (v.flags & CORRUPTIBLE) temp = v.reg; else if (f.flags & CORRUPTIBLE) temp = f.reg; else temp = scratches.obtain(); } if (vs<16 && fs<16) SMULBB(AL, temp, v.reg, f.reg); else MUL(AL, 0, temp, v.reg, f.reg); if (ms>as) { ADD(AL, 0, d.reg, temp, reg_imm(add.reg, LSL, ms-as)); } else if (ms<as) { // not sure if we should expand the mul instead? ADD(AL, 0, d.reg, temp, reg_imm(add.reg, LSR, as-ms)); } } d.h = ms; if (mDithering) { d.l = a.l; } else { d.l = fs>a.l ? fs : a.l; d.flags |= CLEAR_LO; } }